code issue TFI2D model implementation

How do I use this algorithm? What does that parameter do?
Post Reply
shaktiman
Posts: 1
Joined: 19 Apr 2020, 19:58

code issue TFI2D model implementation

Post by shaktiman »

Hi I am new to tenpy package and tried to evaluate in 2D transverse field ising model and implemented code is here. But i am getting bug in this code. Can someone help me to fix the bug.
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()

User avatar
Johannes
Site Admin
Posts: 179
Joined: 21 Jul 2018, 12:52
Location: UC Berkeley

Re: code issue TFI2D model implementation

Post by Johannes »

Please include at least the error message for requests like this.

It seems to be just a python issue, line 31 of the code above, lattice is not defined.
When adding the line

Code: Select all

from tenpy.models import lattice
in the beginning, it runs fine.

I presume you want to extend the TFI model and add further terms, otherwise I'd recommend to just use

Code: Select all

from tenpy.models.tf_ising import TFIModel
and

Code: Select all

model_params = dict(lattice="Square", Lx=2, Ly=4, J=1, g=1, bc_MPS="infinite", verbose= 1)
Note that Lx=2 is totally sufficient, you can even use Lx=1 in many cases.
If you run into DMRG convergence issues, changing Lx might help: sometimes DMRG tends to break the translation invariance.

Post Reply