From bfedc1de4d604f14b0f958615776fb80ddb59e33 Mon Sep 17 00:00:00 2001 From: Erik Nielsen Date: Tue, 15 Mar 2022 21:32:30 -0600 Subject: [PATCH] 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. --- pygsti/modelmembers/states/tpstate.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/pygsti/modelmembers/states/tpstate.py b/pygsti/modelmembers/states/tpstate.py index 31cb6c4ff..34208db81 100644 --- a/pygsti/modelmembers/states/tpstate.py +++ b/pygsti/modelmembers/states/tpstate.py @@ -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 @@ -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)) @@ -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