isi-usc-edu / pyLIQTR

Lincoln Laboratory Quantum Algorithm Test and Research
https://isi-usc-edu.github.io/pyLIQTR/
MIT License
26 stars 6 forks source link

Cannot instantiate Heisenberg model with zero local terms #12

Closed tyleratrigetti closed 2 months ago

tyleratrigetti commented 7 months ago

Trying to initialize a Heisenberg model with h=(0,0,0) fails.

Minimum code to reproduce: pyLIQTR 1.1.1 python 3.9.10 OS: macOS 14.4.1 Chip: Apple M1 Max

from pyLIQTR.ProblemInstances.spin_models import Heisenberg
from pyLIQTR.clam.lattice_definitions import SquareLattice

instance = Heisenberg((2,2),J=(1,2,3),h=(0.,0.,0),cell=SquareLattice)

However, this works:

instance = Heisenberg((2,2),J=(1,2,3),h=(0.,0.,0.1),cell=SquareLattice)

I believe this stems from clam.utilities.get_nonzero_match returns an empty list for h, which is then not handled properly downstream when building the cell.

Stacktrace:

File ~/Library/Caches/pypoetry/virtualenvs/rigetti-application-instances-7GxG9IR2-py3.9/lib/python3.9/site-packages/pyLIQTR/ProblemInstances/LatticeInstance.py:77, in LatticeInstance.__init__(self, shape, cell, cell_subtype, pbcs, rescale, td, td_onsite, model_prefix, **kwargs)
     73     self._UC = self._cell( self._td,
     74                            onsite=self._td_onsite,
     75                            type=self._cell_subtype )
     76 else:
---> 77     self._UC = self._cell(self._td,type=self._cell_subtype)
     79 self._LT = OperatorLattice( size=self._shape, 
     80                             unit=self._UC, 
     81                             periodic=self._pbcs, 
     82                             type='op-qubit')
     84 self._ops = self._LT.op_strings()

File ~/Library/Caches/pypoetry/virtualenvs/rigetti-application-instances-7GxG9IR2-py3.9/lib/python3.9/site-packages/pyLIQTR/clam/lattice_definitions.py:104, in SquareLattice.__init__(self, td, double, onsite, type, dimension)
    101 else:
    102     raise NotImplementedError
--> 104 OperatorUnitCell.__init__( self,
    105                        td,
    106                        sites=self.__sites,
    107                        edges=self.__edges,
    108                        dummy_sites=self.__dummy_sites,
    109                        vectors=self.__vectors,
    110                        double=double,
    111                        onsite=onsite )

File ~/Library/Caches/pypoetry/virtualenvs/rigetti-application-instances-7GxG9IR2-py3.9/lib/python3.9/site-packages/pyLIQTR/clam/operator_lattice.py:106, in OperatorUnitCell.__init__(self, td, sites, dummy_sites, edges, asymmetric_ops, double, onsite, vectors)
    102 self.vectors        =  vectors
    104 self.double         =  double
--> 106 self.build_cell()
    108 include_sites = self.site_set + self.edge_set
    110 UnitCell.__init__(self, sites=include_sites)

File ~/Library/Caches/pypoetry/virtualenvs/rigetti-application-instances-7GxG9IR2-py3.9/lib/python3.9/site-packages/pyLIQTR/clam/operator_lattice.py:149, in OperatorUnitCell.build_cell(self)
    146 def build_cell(self):
    148     for site in self.__sites.keys():
--> 149         self.add_site(site,self.__sites[site])
    151     for edge in self.__edges:
    153         label = ""

File ~/Library/Caches/pypoetry/virtualenvs/rigetti-application-instances-7GxG9IR2-py3.9/lib/python3.9/site-packages/pyLIQTR/clam/operator_lattice.py:180, in OperatorUnitCell.add_site(self, s, label)
    179 def add_site(self, s, label):
--> 180     site = Site(s, ops=self.td[label][0], cons=self.td[label][1], 
    181                   label=label)
    182     self.site_set.append(site)
    183     return

File ~/Library/Caches/pypoetry/virtualenvs/rigetti-application-instances-7GxG9IR2-py3.9/lib/python3.9/site-packages/pyLIQTR/clam/operator_complex.py:96, in Site.__init__(self, base, ops, cons, label)
     95 def __init__(self,base,ops=None,cons=None,label=None):
---> 96     OperatorCell.__init__(self, np.array([base]),ops=ops,cons=cons,label=label)

File ~/Library/Caches/pypoetry/virtualenvs/rigetti-application-instances-7GxG9IR2-py3.9/lib/python3.9/site-packages/pyLIQTR/clam/cell_complex.py:419, in OperatorCell.__init__(self, basis, ops, cons, label, boundary)
    417 #EDIT: Changed d=2 to d=1 in the ops listify
    418 ops   = listify(ops,   d=1)
--> 419 cons  = listify(cons,  d=2)
    421 if ( (ops is not None) and (cons is not None) ):
    422     props = []

File ~/Library/Caches/pypoetry/virtualenvs/rigetti-application-instances-7GxG9IR2-py3.9/lib/python3.9/site-packages/pyLIQTR/clam/utilities.py:43, in listify(item, d)
     40 if (d == 2):
     41     if (hasattr(item, "__len__") and (type(item) != str) ):
---> 43         if (hasattr(item[0], "__len__") and (type(item[0]) != str) ):
     44             return(item)
     45         elif (hasattr(item[0], "__len__") and (type(item[0]) == str) ):

IndexError: list index out of range
rroodll commented 4 months ago

I was able to recreate this @tyleratrigetti, thank you for the detail! This problem still exists in the latest version of pyLIQTR. I've added this ticket to the next release.

It has been fixed and is waiting for merge and release.

rroodll commented 2 months ago

fixed in release 1.3.0