Crash during calculation of <Sy>

How do I use this algorithm? What does that parameter do?
Post Reply
quarkonia
Posts: 4
Joined: 27 Jul 2022, 15:51

Crash during calculation of <Sy>

Post by quarkonia »

Hi everyone, for some reason, I have been having a weird problem with my setup where I receive the following error, Process finished with exit code 139 (interrupted by signal 11: SIGSEGV),whenever I try to calculate <Sy> or <Sy_iSy_j> . Minimal example can be found below, basically the XXZ chain tutorial.

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'))
User avatar
Johannes
Site Admin
Posts: 337
Joined: 21 Jul 2018, 12:52
Location: TU Munich

Re: Crash during calculation of <Sy>

Post by Johannes »

I checked, and the code runs without issues on my machine.
I'd guess it's some incompatibility/issue with the BLAS code being called in the end.
First of all, try whether it works with export TENPY_OPTIMIZE=0, which forces TeNPy to not load the compiled Cython parts.

How did you install TeNPy? Did you compile it yourself or use pip/conda?
quarkonia
Posts: 4
Joined: 27 Jul 2022, 15:51

Re: Crash during calculation of <Sy>

Post by quarkonia »

I used conda to install TenPy, I will try your suggestion.
quarkonia
Posts: 4
Joined: 27 Jul 2022, 15:51

Re: Crash during calculation of <Sy>

Post by quarkonia »

Thank you very much for your help. It was indeed about BLAS and I think there were some clashes with MKL during the run as well. I created a separate env for tenpy from scratch, everything works as expected.
User avatar
Johannes
Site Admin
Posts: 337
Joined: 21 Jul 2018, 12:52
Location: TU Munich

Re: Crash during calculation of <Sy>

Post by Johannes »

Glad to hear that :)
Post Reply