import numpy as np
import matplotlib.pyplot as plt
%config InlineBackend.figure_format = "svg"
= 10
S
= 61
nZ = np.linspace(-3,3,nZ)
hzs = np.zeros(nZ)
m_ED = np.zeros(nZ) 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")
HX
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,"z")
hZ
def Ham(S,hz,Jx=1):
#build the Hamiltonian
= Jx*HX - hz*hZ
H return H
%%time
for i in range(nZ):
= Ham(S,hzs[i])
h = np.linalg.eigh(h)
_, vecs = np.zeros(S)
mags for s in range(S):
= np.real(np.conj(vecs[:,0]).T@spin("z",s,S)/2@vecs[:,0]) #spin/2 since pauli matrices shouldn't have 1/2
mags[s] = np.mean(mags) m_ED[i]
CPU times: user 4min 30s, sys: 44.7 s, total: 5min 15s
Wall time: 26.9 s
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 TFIMChain(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("hz", 0.)
hz # add terms
for i in range(len(self.lat.unit_cell)):
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=1, hz=hzs[0], 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
}
= TFIMChain(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
= []
Sz for h in hzs:
"hz"] = h/2 #Since Sz = Sigmaz/2 -- spin-1/2
model_params[= TFIMChain(model_params)
M =M) # (re)initialize DMRG environment with new model
engine.init_env(model# this uses the result from the previous DMRG as first initial guess
= engine.run()
E0, psi "Sz"))
Sz.append(psi.expectation_value(= np.mean(Sz,axis=1) m_DMRG
final DMRG state not in canonical form up to norm_tol=1.00e-05: norm_err=2.41e-05
CPU times: user 1min 50s, sys: 1min 39s, total: 3min 30s
Wall time: 18.3 s
# length and runtime
# 10->17
# 12->37
# 16->80
# 24->230
="ED")
plt.plot(hzs,m_ED,label=".",c="k",label="DMRG")
plt.scatter(hzs,m_DMRG,marker
plt.legend()r"$h^z$")
plt.xlabel(r"$\langle S^z\rangle$") plt.ylabel(
Text(0, 0.5, '$\\langle S^z\\rangle$')