New release: v0.7.1.

Irregular lattice site removing doubt

How do I use this algorithm? What does that parameter do?
Post Reply
amankumar
Posts: 9
Joined: 17 Apr 2020, 11:14

Irregular lattice site removing doubt

Post by amankumar »

In the new released you updated the Irregular lattice site where one can remove some sites. I read the documentation but did not understood how to specify the sites which one wants to remove. I considered Toric code model(L_x=3,L_y=3) and wanted to remove all sites(18,19,20,21,22,23) which are not shown in Image of Dual square lattice. Below I am writing the interaction term which code provides.

Code: Select all

1.00000 * Sigmaz_0 Sigmaz_3 Sigmaz_4 Sigmaz_6 +
1.00000 * Sigmaz_1 Sigmaz_4 Sigmaz_5 Sigmaz_7 +
1.00000 * Sigmaz_2 Sigmaz_3 Sigmaz_5 Sigmaz_8 +
1.00000 * Sigmax_3 Sigmax_6 Sigmax_8 Sigmax_9 +
1.00000 * Sigmax_4 Sigmax_6 Sigmax_7 Sigmax_10 +
1.00000 * Sigmax_5 Sigmax_7 Sigmax_8 Sigmax_11 +
1.00000 * Sigmaz_6 Sigmaz_9 Sigmaz_10 Sigmaz_12 +
1.00000 * Sigmaz_7 Sigmaz_10 Sigmaz_11 Sigmaz_13 +
1.00000 * Sigmaz_8 Sigmaz_9 Sigmaz_11 Sigmaz_14 +
1.00000 * Sigmax_9 Sigmax_12 Sigmax_14 Sigmax_15 +
1.00000 * Sigmax_10 Sigmax_12 Sigmax_13 Sigmax_16 +
1.00000 * Sigmax_11 Sigmax_13 Sigmax_14 Sigmax_17 +
1.00000 * Sigmaz_12 Sigmaz_15 Sigmaz_16 Sigmaz_18 +
1.00000 * Sigmaz_13 Sigmaz_16 Sigmaz_17 Sigmaz_19 +
1.00000 * Sigmaz_14 Sigmaz_15 Sigmaz_17 Sigmaz_20 +
1.00000 * Sigmax_15 Sigmax_18 Sigmax_20 Sigmax_21 +
1.00000 * Sigmax_16 Sigmax_18 Sigmax_19 Sigmax_22 +
1.00000 * Sigmax_17 Sigmax_19 Sigmax_20 Sigmax_23

Lets say I don't want all the last six interaction term in my model which can be done by removing sites(i>=18).
Below I am attaching the example code, where I do not understand how to provide the 2 D array of lattice sites whch I wants to remove.

Code: Select all

    def init_lattice(self, model_params):
        site = self.init_sites(model_params)
        Lx = model_params.get('Lx', 2)
        Ly = model_params.get('Ly', 2)
        order = model_params.get('order', 'default')
        bc_MPS = model_params.get('bc_MPS', 'infinite')
        bc_x = 'periodic' if bc_MPS == 'infinite' else 'open'
        bc_x = model_params.get('bc_x', bc_x)
        bc_y = model_params.get('bc_y', 'periodic')
        assert bc_y in ['open', 'periodic']
        bc = [bc_x, bc_y]
        lat = IrregularLattice(DualSquare(Lx, Ly, site, order=order, bc=bc, bc_MPS=bc_MPS),remove=[......])
        return lat
Attachments
Figure_1.png
Figure_1.png (72.79 KiB) Viewed 739 times
User avatar
Johannes
Site Admin
Posts: 188
Joined: 21 Jul 2018, 12:52
Location: UC Berkeley

Re: Irregular lattice site removing doubt

Post by Johannes »

First of all: I've updated the documentation on Monday and added a completely new user-guide page describing many details on lattices, including a small section on irregular hattices on the end.
Moreover, I've added some examples in the doc-string of the tenpy.models.lattice.IrregularLattice itself.

To your question: you specify Lx=3, Ly=3 for the lattice, which produces the figure you attached with 18 sites, so there are no sites 18, 19, 20, ... 22. Those sites appear for example if you have Lx=4, did you mean that?
Which sites do you want to remove? Those on bonds not belonging to any Plaquette of the original square lattice? That would only be 3 (for Ly=3, namely 15, 16, 17 in your figure.
Site 15 corresponds to lattice index (x,y,u) = (3, 0, 1), Site 16 is (x,y,u) = (3, 1, 1) and so on, so I think you want

Code: Select all

IrregularLattice(DualSquare(Lx, Ly, site, order=order, bc=bc, bc_MPS=bc_MPS),
                 remove=[[Lx-1, y, 1] for y in range(Ly)])

PS: I've moved the topic into the HowTo forum, the "Papers using TeNPy" forum is for the case that you used TeNPy to write an awsome paper you want to tell us about ;-)
amankumar
Posts: 9
Joined: 17 Apr 2020, 11:14

Re: Irregular lattice site removing doubt

Post by amankumar »

I am sorry for posting question in "Papers using TeNPy", I don't remember how did I posted it there.
I tried to use Irregular Lattice for Toric Code model by removing those sites which do not belong to any Plaquette of the original square lattice. I am getting an unusual error. Can you help me in this regard?

Code: Select all

File "/tenpy/models/lattice.py", line 544, in lat2mps_idx
    i += i_shift * self.N_sites // self.N_rings
TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'
I am attaching the Full Code below.

Code: Select all

# Copyright 2018-2020 TeNPy Developers, GNU GPLv3

import numpy as np

from tenpy.networks.mps import MPS
from tenpy.algorithms import dmrg
from tenpy.models.toric_code import ToricCode


def example_run_TC():
    model_params = dict(Lx=3, Ly=3, Jv=1, Jp=1, bc_MPS="infinite", verbose= 1)

    Lx = model_params['Lx']
    Ly = model_params['Ly']
    M = ToricCode(model_params)
    product_state=[0]*((2*Ly*Lx)-Ly)
    psi = MPS.from_product_state(M.lat.mps_sites(), product_state, bc = "infinite")
    dmrg_params = {
        'mixer': True,  # setting this to True helps to escape local minima
        'trunc_params': {
            'chi_max': 100,
            'svd_min': 1.e-10,
        },
        'max_E_err': 1.e-10,
        'verbose': 1,
    }

    results = dmrg.run(psi, M, dmrg_params)
    print("Energy per site: ", results['E'])
if __name__ == "__main__":
    example_run_TC()

Code: Select all

# Copyright 2018-2020 TeNPy Developers, GNU GPLv3

import numpy as np

from .lattice import Lattice, get_order, _parse_sites,IrregularLattice
from ..networks.site import SpinHalfSite
from .model import MultiCouplingModel, CouplingMPOModel
from ..tools.params import asConfig
from ..tools.misc import any_nonzero

__all__ = ['DualSquare', 'ToricCode']


class DualSquare(Lattice):
    def __init__(self, Lx, Ly, sites, **kwargs):
        sites = _parse_sites(sites, 2)
        basis = np.eye(2)
        pos = np.array([[0., 0.5], [0.5, 0.]])
        kwargs.setdefault('basis', basis)
        kwargs.setdefault('positions', pos)
        NN = [(1, 0, np.array([0, 0])), (1, 0, np.array([1, 0])), (0, 1, np.array([-1, 1])),
              (0, 1, np.array([0, 1]))]
        nNN = [(i, i, dx) for i in [0, 1] for dx in [np.array([1, 0]), np.array([0, 1])]]
        nnNN = [(i, i, dx) for i in [0, 1] for dx in [np.array([1, 1]), np.array([-1, 1])]]
        kwargs.setdefault('pairs', {})
        kwargs['pairs'].setdefault('nearest_neighbors', NN)
        kwargs['pairs'].setdefault('next_nearest_neighbors', nNN)
        kwargs['pairs'].setdefault('next_next_nearest_neighbors', nnNN)
        super().__init__([Lx, Ly], sites, **kwargs)

    def ordering(self, order):
        if isinstance(order, str):
            if order == "default":
                priority = (0, 2, 1)
                snake_winding = (False, False, False)
                return get_order(self.shape, snake_winding, priority)
        return super().ordering(order)


class ToricCode(CouplingMPOModel, MultiCouplingModel):
    def init_sites(self, model_params):
        conserve = model_params.get('conserve', 'parity')
        site = SpinHalfSite(conserve)
        return site

    def init_lattice(self, model_params):
        site = self.init_sites(model_params)
        Lx = model_params.get('Lx', 2)
        Ly = model_params.get('Ly', 2)
        order = model_params.get('order', 'default')
        bc_MPS = model_params.get('bc_MPS', 'infinite')
        bc_x = 'periodic' if bc_MPS == 'infinite' else 'open'
        bc_x = model_params.get('bc_x', bc_x)
        bc_y = model_params.get('bc_y', 'periodic')
        assert bc_y in ['open', 'periodic']
        bc = [bc_x, bc_y]
        lat=IrregularLattice(DualSquare(Lx, Ly, site, order=order, bc=bc,bc_MPS=bc_MPS),
                 remove=[[Lx-1, y, 1] for y in range(Ly)])
        return lat
    def init_terms(self, model_params):
        Jv = model_params.get('Jv', 1.)
        Jp = model_params.get('Jp', 1.)
        # vertex/star term
        self.add_multi_coupling(Jv, [('Sigmax', [0, 0], 1), ('Sigmax', [0, 0], 0),
                                     ('Sigmax', [-1, 0], 1), ('Sigmax', [0, -1], 0)],category='spins')
        # plaquette term
        self.add_multi_coupling(Jp, [('Sigmaz', [0, 0], 1), ('Sigmaz', [0, 0], 0),
                                     ('Sigmaz', [0, 1], 1), ('Sigmaz', [1, 0], 0)],category='spins')
        # done
                                                                                                                                                                                          69,1          Bot

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

Re: Irregular lattice site removing doubt

Post by Johannes »

Sorry, that was a small bug. I've fixed it in 037ad76272825bf6f4de1a72fb4e5529a1fcdec9.
User avatar
Johannes
Site Admin
Posts: 188
Joined: 21 Jul 2018, 12:52
Location: UC Berkeley

Re: Irregular lattice site removing doubt

Post by Johannes »

By the way, you don't have to modify the model inside the tenpy repo for that, you can do something like this:

Code: Select all

from tenpy.models import toric_code
from tenpy.models.lattice import IrregularLattice

class MyToricCode(toric_code.ToricCode):

    def init_lattice(self, model_params):
        reg_lat = super().init_lattice(model_params) # calls the `ToricCode.init_lattice`
        Lx, Ly = reg_lat.Ls
        lat = IrregularLattice(reg_lat, remove=[[Lx-1, y, 1] for y in range(Ly)])
        return lat
Post Reply