Code: Select all

```
import math
import tenpy
from tenpy.networks.mps import MPS
import numpy as np
from tenpy.algorithms import dmrg
import sys
from tenpy.models.lattice import Site,Chain
from tenpy.models.model import CouplingModel,NearestNeighborModel,MPOModel,CouplingMPOModel
from tenpy.linalg import np_conserved as npc
from tenpy.tools.params import asConfig
from tenpy.networks.site import SpinHalfSite
from tenpy.tools.fit import fit_with_sum_of_exp, sum_of_exp
np.set_printoptions(precision=8)
np.set_printoptions(suppress=True)
np.set_printoptions(threshold=sys.maxsize)
#The CouplingModel way of defining the model ####
class XXZChain(CouplingModel, NearestNeighborModel, MPOModel):
r"""Spin-1/2 XXZ chain with Sz conservation.
The Hamiltonian reads:
.. math ::
H = \sum_i \mathtt{Jxx}/2 (S^{+}_i S^{-}_{i+1} + S^{-}_i S^{+}_{i+1})
+ \mathtt{Jz} S^z_i S^z_{i+1} \\
- \sum_i \mathtt{hz} S^z_i
All parameters are collected in a single dictionary `model_params`, which
is turned into a :class:`~tenpy.tools.params.Config` object.
Parameters
----------
model_params : :class:`~tenpy.tools.params.Config`
Parameters for the model. See :cfg:config:`XXZChain` below.
Options
-------
.. cfg:config :: XXZChain
:include: CouplingMPOModel
L : int
Length of the chain.
Jxx, Jz, hz : float | array
Coupling as defined for the Hamiltonian above.
bc_MPS : {'finite' | 'infinte'}
MPS boundary conditions. Coupling boundary conditions are chosen appropriately.
"""
def __init__(self, model_params):
model_params = asConfig(model_params, "XXZChain")
L = model_params.get('L', 2)
Jxx = model_params.get('Jxx', 1.)
Jz = model_params.get('Jz', 1.)
hz = model_params.get('hz', 0.)
hx = model_params.get('hx', 0.)
bc_MPS = model_params.get('bc_MPS', 'finite')
USE_PREDEFINED_SITE = True
if not USE_PREDEFINED_SITE:
leg = npc.LegCharge.from_qflat(npc.ChargeInfo([1], ['2*Sz']), [1, -1])
Sp = [[0., 1.], [0., 0.]]
Sm = [[0., 0.], [1., 0.]]
Sz = [[0.5, 0.], [0., -0.5]]
site = Site(leg, ['up', 'down'], Sp=Sp, Sm=Sm, Sz=Sz)
else:
site = SpinHalfSite(conserve='None')
bc = 'open' if bc_MPS == 'finite' else 'periodic'
lat = Chain(L, site, bc=bc, bc_MPS=bc_MPS)
CouplingModel.__init__(self, lat)
self.add_onsite(hz*2, 0, 'Sz')
self.add_onsite(hx*2, 0, 'Sx')
self.add_coupling(Jxx*4 , 0, 'Sp', 0, 'Sm', 1, plus_hc=True)
self.add_coupling(Jz*4, 0, 'Sz', 0, 'Sz', 1)
MPOModel.__init__(self, lat, self.calc_H_MPO())
#################################################
if __name__ == "__main__":
spin = SpinHalfSite(conserve="None")
# print(spin.Sz.to_ndarray())
N = 20 # number of sites
sites = [spin] * N # repeat entry of list N times
pstate = ["down"] * N # Neel state
psi = MPS.from_product_state(sites, pstate, bc="finite")
mod_par = {'L':20, 'Jxx':1,'Jz':2, 'hx':0 ,'hz':1,'bc_MPS':'finite'}
mod_par = asConfig(mod_par,"MyChain")
XXZ = XXZChain(mod_par)
print("<psi|H|psi> =", XXZ.H_MPO.expectation_value(psi))
#print(psi.expectation_value('Sz'))
dmrg_params = {"trunc_params": {"chi_max": 100, "svd_min": 1.e-10}, "mixer": True}
info = dmrg.run(psi, XXZ, dmrg_params)
print("<SySy>", psi.correlation_function('Sy','Sy'))
print("<Sy>", psi.expectation_value('Sy'))
```