Error when using calc_H_bond() for both coupling and onsite terms

How do I use this algorithm? What does that parameter do?
Post Reply
Jonas_K
Posts: 4
Joined: 13 Oct 2025, 12:02

Error when using calc_H_bond() for both coupling and onsite terms

Post by Jonas_K »

Hi:)

I am trying to create a model with periodic boundary conditions, but I am running into an error when using the Nearest Neighbor function calc_H_bond().

Below my code can be seen:

Python: Select all

class Arb_Model( CouplingModel, NearestNeighborModel, MPOModel ):
    def __init__(self, model_params):
        self.L = model_params.get( 'L' , 2 )
        self.bc_MPS = model_params.get( 'bc_MPS' , 'finite' )
        self.bc = model_params.get( 'bc' , 'periodic' )
        self.J = 1.
        site = SpinSite( S = 1 , conserve = None )
        lat = Chain( self.L , site , bc = self.bc , bc_MPS = self.bc_MPS )
        
        CouplingModel.__init__( self , lat )
        self.add_onsite( strength = - self.J , u = 0 , opname = 'Sz' )
        self.add_coupling( strength = self.J ,  u1 = 0 , op1 = 'Sx' , u2 = 0 , op2 = 'Sx' , dx = [1] )

        MPOModel.__init__(self, lat, self.calc_H_MPO())
        NearestNeighborModel.__init__(self, lat, self.calc_H_bond()) 

model = Arb_Model( model_params = { 'L' : 3 , 'bc' : 'periodic' , 'bc_MPS' : 'finite' } )
I get the error "ValueError: Can't initialize H_bond for a NearestNeighborModel with non-nearest neighbor couplings added.".
It seems to me that this should work as due to the periodic boundary conditions the first and last particle are connected.
If I use:

Python: Select all

mps_is , mps_js , _ = self.lat.possible_couplings( u1 = 0 , u2 = 0 , dx = [1] , strength = self.J )
I get that mps_is = [0,1,2] and mps_js = [1,2,0], which does couple site 0 and 2.

Looking at the calc_H_bond() function it is clear that the error comes from the requirement that i+1=j (as seen in the code hereafter).

Python: Select all

term_list = self.to_TermList()
for term, strength in zip(term_list.terms, term_list.strength):
    (--- other code not relevant here ---)
    (op_i, i), (op_j, j) = term
    if i + 1 != j:
        raise ValueError('not nearest neighbor')
However, as the i < j for the last term we have that i = 0, j = 2 so i+1=1 != 2 = j leading to the error despite them being neighbors.

My question is then whether this is a bug or I am simply not implementing the boundary conditions (or something else entirely!) correctly.

Secondly, when I only apply the onsite Sz term and analyze the model.H_bond, I get that the bond between the first and last particle is None. It seems that for finite systems, regardless of boundary conditions, the entire weight of the onsite term get distributed to the right (left) for the first (last) term. This again seems a bit odd for periodic boundary conditions as I would expect there to be a nonzero bond between them.

Thanks a lot for any help provided and apologies in advance if this is merely me not understanding how to properly incorporate the boundary conditions in tenpy:)
Jakob
Posts: 29
Joined: 30 Jan 2023, 22:57

Re: Error when using calc_H_bond() for both coupling and onsite terms

Post by Jakob »

This is expected behavior.
The nearest neighbor model needs to be nearest neighbor in the MPS geometry of an open chain. From the MPS point of view, your model has one single long range coupling between the first and last site.
You can have such a model with infinite MPS, but not with finite MPS
Jakob
Posts: 29
Joined: 30 Jan 2023, 22:57

Re: Error when using calc_H_bond() for both coupling and onsite terms

Post by Jakob »

Have a look at the models guide

https://tenpy.readthedocs.io/en/latest/intro/model.html

If you need a finite periodic system, simply dont inherit from NearestNeighbourModel, then you can still do everything except TEBD
Jonas_K
Posts: 4
Joined: 13 Oct 2025, 12:02

Re: Error when using calc_H_bond() for both coupling and onsite terms

Post by Jonas_K »

That makes sense. Thanks for the quick reply!
Post Reply