Thanks
Code: Select all
import numpy as np
from tenpy.networks.mps import MPS
from tenpy.algorithms import dmrg
from tenpy.models.lattice import Lattice, _parse_sites, Square
from tenpy.models.model import CouplingModel, NearestNeighborModel,MPOModel
from tenpy.tools.params import get_parameter, unused_parameters
from tenpy.networks.site import SpinHalfSite
class TFIModel(CouplingModel, MPOModel):
r"""Transverse field Ising model on a general (2D) lattice.
... documentation ..."""
def __init__(self, model_param):
# 0) read out/set default parameters
lat = get_parameter(model_param, 'lattice', "Square", self.__class__)
if isinstance(lat, str):
Lx = get_parameter(model_param, 'Lx', 1, self.__class__)
Ly = get_parameter(model_param, 'Ly', 4, self.__class__)
bc_MPS = get_parameter(model_param, 'bc_MPS', 'infinite', self.__class__)
bc_y = get_parameter(model_param, 'bc_y', 'cylinder', self.__class__)
order = get_parameter(model_param, 'order', 'default', self.__class__)
conserve = get_parameter(model_param, 'conserve', None, self.__class__)
assert conserve != 'Sz' # invalid!
assert bc_y in ['cylinder', 'ladder']
# 1-3)
site = SpinHalfSite(conserve=conserve)
# 4) lattice
bc_x = 'periodic' if bc_MPS == 'infinite' else 'open'
bc_y = 'periodic' if bc_y == 'cylinder' else 'open'
lat = lattice.__dict__.get(lat) # the corresponding class
lat = lat(Lx, Ly, site, order=order, bc=[bc_x, bc_y], bc_MPS=bc_MPS) # instance
J = get_parameter(model_param, 'J', 1., self.__class__)
g = get_parameter(model_param, 'g', 1., self.__class__)
unused_parameters(model_param, self.__class__) # checks for mistyped parameters
# 5) initialize CouplingModel
CouplingModel.__init__(self, lat)
# 6) add terms of the Hamiltonian
# (u is always 0 as we have only one site in the unit cell)
self.add_onsite(-np.asarray(g), 0, 'Sigmaz')
J = np.asarray(J)
if all([site.conserve is None for site in lat.unit_cell]):
for u1, u2, dx in self.lat.nearest_neighbors:
self.add_coupling(-J, u1, 'Sigmax', u2, 'Sigmax', dx)
else:
for op1, op2 in [('Sp', 'Sp'), ('Sp', 'Sm'), ('Sm', 'Sp'), ('Sm', 'Sm')]:
for u1, u2, dx in self.lat.nearest_neighbors:
self.add_coupling(-J, u1, op1, u2, op2, dx)
# 7) initialize MPO
MPOModel.__init__(self, lat, self.calc_H_MPO())
def example_run_TFI2D():
model_params = dict(Lx=16, Ly=4, J=1, g=1, bc_MPS="infinite", verbose= 1)
Lx = model_params['Lx']
Ly = model_params['Ly']
model = TFIModel(model_params)
psi = MPS.from_product_state(model.lat.mps_sites(), [0]*(Ly*Lx), bc = "infinite")
dmrg_params = {'mixer': True,
'chi_list': {0: 80},
'trunc_params': {'svd_min': 1.e-10},
'verbose': 1}
results = dmrg.run(psi, model, dmrg_params)
print("Energy per site: ", results['E'])
print("Entanglement Entropy: ", psi.entanglement_entropy())
if __name__ == "__main__":
example_run_TFI2D()