import numpy as np
import matplotlib.pyplot as plt
%config InlineBackend.figure_format = "svg"
= 10
S =0.1,0.2,0.5 #random selection of couplings
Jx,Jy,Jz=0.1,-0.2,0.5 #random selection of fields
hx,hy,hz
= np.zeros((S,3))
m_ED = np.zeros((S,3)) m_DMRG
= np.array([[1,0],[0,1]],dtype=complex)
s0 = np.array([[0,1],[1,0]],dtype=complex)
sx = np.array([[0,-1j],[1j,0]],dtype=complex)
sy = np.array([[1,0],[0,-1]],dtype=complex)
sz
= {"0" : s0,
pauli "x" : sx,
"y" : sy,
"z" : sz}
def spin(polarization,s,S):
if(s==0):
= pauli[polarization]
temp else:
= s0
temp for i in range(1,S,1):
if(i==s):
= np.kron(temp,pauli[polarization])
temp else:
= np.kron(temp,s0)
temp return temp
def H_s(S,dir): #open boundaries
= np.zeros((2**S,2**S),dtype=complex)
H for i in range(0,S-1,1):
+= spin(dir,i,S)@spin(dir,i+1,S)
H #H += spin(dir,S-1,S)@spin(dir,0,S) #periodic boundaries
return H
= H_s(S,"x"),H_s(S,"y"),H_s(S,"z")
HX,HY,HZ
def h_s(S,dir):
= np.zeros((2**S,2**S),dtype=complex)
H for i in range(0,S,1):
+= spin(dir,i,S)
H return H
= h_s(S,"x"),h_s(S,"y"),h_s(S,"z")
hX,hY,hZ
def Ham(S):
#build the Hamiltonian
= Jx*HX + Jy*HY + Jz*HZ - (hx*hX + hy*hY + hz*hZ)
H return H
%%time
= Ham(S)
h = np.linalg.eigh(h)
_, vecs = np.zeros(S)
mags = ["x","y","z"]
spins for s in range(S):
for d in range(3):
= np.real(np.conj(vecs[:,0]).T@spin(spins[d],s,S)/2@vecs[:,0]) #spin/2 since pauli matrices shouldn't have 1/2 m_ED[s,d]
CPU times: user 6.34 s, sys: 1.98 s, total: 8.32 s
Wall time: 740 ms
from tenpy.networks.mps import MPS
from tenpy.algorithms import dmrg
from tenpy.models.model import CouplingMPOModel
from tenpy.models.model import NearestNeighborModel
from tenpy.networks.site import SpinHalfSite
/opt/homebrew/lib/python3.11/site-packages/tenpy/tools/optimization.py:307: UserWarning: Couldn't load compiled cython code. Code will run a bit slower.
warnings.warn("Couldn't load compiled cython code. Code will run a bit slower.")
class XYZChain(CouplingMPOModel, NearestNeighborModel):
= "Chain"
default_lattice = True
force_default_lattice
def init_sites(self, model_params):
return SpinHalfSite(conserve="None")
def init_terms(self, model_params):
# read out parameters
= model_params.get("Jx", 1.)
Jx = model_params.get("Jy", 0.)
Jy = model_params.get("Jz", 0.)
Jz = model_params.get("hx", 0.)
hx = model_params.get("hy", 0.)
hy = model_params.get("hz", 0.)
hz # add terms
for i in range(len(self.lat.unit_cell)):
self.add_onsite(-hx, i, "Sx")
self.add_onsite(-hy, i, "Sy")
self.add_onsite(-hz, i, "Sz")
for i1, i2, dx in self.lat.pairs["nearest_neighbors"]:
self.add_coupling(Jx, i1, "Sx", i2, "Sx", dx)
self.add_coupling(Jy, i1, "Sy", i2, "Sy", dx)
self.add_coupling(Jz, i1, "Sz", i2, "Sz", dx)
%%time
= dict(L=S, Jx=Jx,Jy=Jy,Jz=Jz, hx=hx/2,hy=hy/2,hz=hz/2, bc_MPS="finite")
model_params = {
dmrg_params "trunc_params": {
"chi_max": 100, #bond dimension
"svd_min": 1*10**-10
},"max_E_err": 0.0001, #energy convergence step threshold
"max_S_err": 0.0001, #entropy convergence step threshold
"max_sweeps": 100 #may or may not be enough to converge
}
= XYZChain(model_params)
M = MPS.from_product_state(M.lat.mps_sites(), (["up", "down"] * S)[:S], M.lat.bc_MPS)
psi = dmrg.TwoSiteDMRGEngine(psi, M, dmrg_params)
engine = engine.run()
E0, psi 0] = psi.expectation_value("Sx")
m_DMRG[:,1] = psi.expectation_value("Sy")
m_DMRG[:,2] = psi.expectation_value("Sz") m_DMRG[:,
CPU times: user 20.9 s, sys: 36.5 s, total: 57.4 s
Wall time: 5.04 s
print(m_ED,"\n")
print(m_DMRG)
[[ 0.06453661 -0.12891305 0.36018393]
[ 0.01218607 0.00158949 -0.2576881 ]
[ 0.04121896 -0.08623101 0.29688275]
[ 0.01192666 -0.01439035 -0.09926392]
[ 0.01928473 -0.04117888 0.11982084]
[ 0.01928473 -0.04117888 0.11982084]
[ 0.01192666 -0.01439035 -0.09926392]
[ 0.04121896 -0.08623101 0.29688275]
[ 0.01218607 0.00158949 -0.2576881 ]
[ 0.06453661 -0.12891305 0.36018393]]
[[ 0.06453661 -0.12891305 0.36018393]
[ 0.01218607 0.00158949 -0.2576881 ]
[ 0.04121896 -0.08623101 0.29688275]
[ 0.01192666 -0.01439035 -0.09926392]
[ 0.01928473 -0.04117888 0.11982084]
[ 0.01928473 -0.04117888 0.11982084]
[ 0.01192666 -0.01439035 -0.09926392]
[ 0.04121896 -0.08623101 0.29688275]
[ 0.01218607 0.00158949 -0.2576881 ]
[ 0.06453661 -0.12891305 0.36018393]]
for d in range(3):
1,S,S,endpoint=True),m_ED[:,d],label=r"ED, $S^{}$".format(spins[d]))
plt.plot(np.linspace(1,S,S,endpoint=True),m_DMRG[:,d],marker=".",label="DMRG")
plt.scatter(np.linspace(=1)
plt.legend(locr"site $i$")
plt.xlabel(r"$S_i$") plt.ylabel(
Text(0, 0.5, '$\\langle S\\rangle$')
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.patches import FancyArrowPatch
from mpl_toolkits.mplot3d import proj3d
import matplotlib.colors as clr
= clr.LinearSegmentedColormap.from_list("",[(0,"#EEEEEE"),(0.5,"#8888EE"),(1,"#0000EE")])
my_cmap
class Arrow3D(FancyArrowPatch):
def __init__(self, xs, ys, zs, *args, **kwargs):
super().__init__((0,0), (0,0), *args, **kwargs)
self._verts3d = xs, ys, zs
def do_3d_projection(self, renderer=None):
= self._verts3d
xs3d, ys3d, zs3d = proj3d.proj_transform(xs3d, ys3d, zs3d, self.axes.M)
xs, ys, zs self.set_positions((xs[0],ys[0]),(xs[1],ys[1]))
return np.min(zs)
def axes():
= dict(mutation_scale=20, arrowstyle='-|>', color="k", shrinkA=0, shrinkB=0,linewidth=2)
arrow_prop_dict = Arrow3D([0,S+1], [0, 0], [0, 0], **arrow_prop_dict)
a
ax.add_artist(a)= dict(mutation_scale=20, arrowstyle='-|>', color="k", shrinkA=0, shrinkB=0,linewidth=2)
arrow_prop_dict = Arrow3D([0,0], [0, 1], [0, 0], **arrow_prop_dict)
a
ax.add_artist(a)= dict(mutation_scale=20, arrowstyle='-|>', color="k", shrinkA=0, shrinkB=0,linewidth=2)
arrow_prop_dict = Arrow3D([0,0], [0, 0], [0, 1], **arrow_prop_dict)
a
ax.add_artist(a)
def arrow(site,vec):
len = np.sqrt(vec[0]**2+vec[1]**2+vec[2]**2)
= dict(mutation_scale=20, arrowstyle='-|>', color=my_cmap(len), shrinkA=0, shrinkB=0,linewidth=2)
arrow_prop_dict = Arrow3D([site,site+vec[0]], [0, vec[1]], [0, vec[2]], **arrow_prop_dict)
a ax.add_artist(a)
= plt.figure(figsize=(15,5))
fig = fig.add_subplot(111, projection="3d")
ax "ED")
ax.set_title(
ax.set_axis_off()
axes()+1,0,0,"x")
ax.text(S0,1,0,"y")
ax.text(0,0,1,"z")
ax.text(= np.sqrt(hx**2+hy**2+hz**2)
lenh /lenh,hy/lenh,hz/lenh,"h")
ax.text(hx= dict(mutation_scale=20, arrowstyle="-|>", color="r", shrinkA=0, shrinkB=0,linewidth=2)
arrow_prop_dict = Arrow3D([0,hx/lenh], [0, hy/lenh], [0, hz/lenh], **arrow_prop_dict)
a
ax.add_artist(a)
#plot magnetizations
= 3 #to make arrows the right scale
scale for i in range(S):
+1,scale*m_ED[i])
arrow(i
-1,S+1)
ax.set_xlim(-1,1)
ax.set_ylim(-1,1)
ax.set_zlim(
30, -45)
ax.view_init(
= fig.add_subplot(121, projection="3d")
ax "DMRG")
ax.set_title(
ax.set_axis_off()
axes()+1,0,0,"x")
ax.text(S0,1,0,"y")
ax.text(0,0,1,"z")
ax.text(= np.sqrt(hx**2+hy**2+hz**2)
lenh /lenh,hy/lenh,hz/lenh,"h")
ax.text(hx= dict(mutation_scale=20, arrowstyle="-|>", color="r", shrinkA=0, shrinkB=0,linewidth=2)
arrow_prop_dict = Arrow3D([0,hx/lenh], [0, hy/lenh], [0, hz/lenh], **arrow_prop_dict)
a
ax.add_artist(a)
#plot magnetizations
= 3 #to make arrows the right scale
scale for i in range(S):
+1,scale*m_DMRG[i])
arrow(i
-1,S+1)
ax.set_xlim(-1,1)
ax.set_ylim(-1,1)
ax.set_zlim(
30, -45) ax.view_init(