Skip to content

Commit

Permalink
Adds _from_memoized_dict to TPState so serialization works again.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Erik Nielsen committed Mar 16, 2022
1 parent 89c1e0d commit bfedc1d
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions pygsti/modelmembers/states/tpstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import numpy as _np

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

_DenseState.__init__(self, vector, evotype, state_space)
assert(isinstance(self.columnvec, _ProtectedArray))
Expand Down Expand Up @@ -188,3 +190,9 @@ def has_nonzero_hessian(self):
bool
"""
return False

@classmethod
def _from_memoized_dict(cls, mm_dict, serial_memo):
vec = cls._decodemx(mm_dict['dense_superket_vector'])
state_space = _statespace.StateSpace.from_nice_serialization(mm_dict['state_space'])
return cls(vec, None, mm_dict['evotype'], state_space) # use basis=None to skip 1st element check

0 comments on commit bfedc1d

Please sign in to comment.