Skip to content

Commit bfedc1d

Browse files
author
Erik Nielsen
committed
Adds _from_memoized_dict to TPState so serialization works again.
After changing signature of __init__ to include 'basis' arg, we forgot to add this function, which overrides the DenseState implementation and correctly supplies basis=None to skip 1st-element checking.
1 parent 89c1e0d commit bfedc1d

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

pygsti/modelmembers/states/tpstate.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import numpy as _np
1515

1616
from pygsti.baseobjs import Basis as _Basis
17+
from pygsti.baseobjs import statespace as _statespace
1718
from pygsti.modelmembers.states.densestate import DenseState as _DenseState
1819
from pygsti.modelmembers.states.state import State as _State
1920
from pygsti.baseobjs.protectedarray import ProtectedArray as _ProtectedArray
@@ -53,12 +54,13 @@ class TPState(_DenseState):
5354
# alpha = 1/sqrt(d) = 1/(len(vec)**0.25).
5455
def __init__(self, vec, basis="pp", evotype="default", state_space=None):
5556
vector = _State._to_vector(vec)
56-
if not isinstance(basis, _Basis):
57-
basis = _Basis.cast(basis, len(vector)) # don't perform this cast if we're given a basis
58-
firstEl = basis.elsize**-0.25 # not dim, as the dimension of the vector space may be less
59-
if not _np.isclose(vector[0], firstEl):
60-
raise ValueError("Cannot create TPState: "
61-
"first element must equal %g!" % firstEl)
57+
if basis is not None:
58+
if not isinstance(basis, _Basis):
59+
basis = _Basis.cast(basis, len(vector)) # don't perform this cast if we're given a basis
60+
firstEl = basis.elsize**-0.25 # not dim, as the dimension of the vector space may be less
61+
if not _np.isclose(vector[0], firstEl):
62+
raise ValueError("Cannot create TPState: first element must equal %g!" % firstEl)
63+
# if basis is None, don't check first element (hackfor de-serialization, so we don't need to store basis)
6264

6365
_DenseState.__init__(self, vector, evotype, state_space)
6466
assert(isinstance(self.columnvec, _ProtectedArray))
@@ -188,3 +190,9 @@ def has_nonzero_hessian(self):
188190
bool
189191
"""
190192
return False
193+
194+
@classmethod
195+
def _from_memoized_dict(cls, mm_dict, serial_memo):
196+
vec = cls._decodemx(mm_dict['dense_superket_vector'])
197+
state_space = _statespace.StateSpace.from_nice_serialization(mm_dict['state_space'])
198+
return cls(vec, None, mm_dict['evotype'], state_space) # use basis=None to skip 1st element check

0 commit comments

Comments
 (0)