Skip to content

Commit

Permalink
Merge pull request #509 from sandialabs/bugfix-cptplnd-sign
Browse files Browse the repository at this point in the history
CPTP Serialization Sign Change Fix
  • Loading branch information
sserita authored Dec 2, 2024
2 parents 916877c + b70afbb commit 0ab7af2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
21 changes: 9 additions & 12 deletions pygsti/modelmembers/operations/lindbladerrorgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1408,28 +1408,25 @@ def to_memoized_dict(self, mmg_memo):
mm_dict = super().to_memoized_dict(mmg_memo)

mm_dict['rep_type'] = self._rep_type
#OLD: mm_dict['parameterization'] = self.parameterization.to_nice_serialization()
#OLD: mm_dict['lindblad_basis'] = self.lindblad_basis.to_nice_serialization()
#OLD: mm_dict['coefficients'] = [(str(k), self._encodevalue(v)) for k, v in self.coefficients().items()]
mm_dict['matrix_basis'] = self.matrix_basis.to_nice_serialization()
mm_dict['coefficient_blocks'] = [blk.to_nice_serialization() for blk in self.coefficient_blocks]
#serialize the paramval attribute. Rederiving this from the block data has been leading to sign
#ambiguity on deserialization.
mm_dict['paramvals'] = self._encodemx(self.paramvals)

return mm_dict

@classmethod
def _from_memoized_dict(cls, mm_dict, serial_memo):
#lindblad_term_dict = {_GlobalElementaryErrorgenLabel.cast(k): cls._decodevalue(v)
# for k, v in mm_dict['coefficients']} # convert keys from str->objects
#parameterization = LindbladParameterization.from_nice_serialization(mm_dict['parameterization'])
#lindblad_basis = _Basis.from_nice_serialization(mm_dict['lindblad_basis'])
#truncate = False # shouldn't need to truncate since we're reloading a valid set of coefficients
mx_basis = _Basis.from_nice_serialization(mm_dict['matrix_basis'])
state_space = _statespace.StateSpace.from_nice_serialization(mm_dict['state_space'])
coeff_blocks = [_LindbladCoefficientBlock.from_nice_serialization(blk)
for blk in mm_dict['coefficient_blocks']]

return cls(coeff_blocks, 'auto', mx_basis, mm_dict['evotype'], state_space)
#return cls(lindblad_term_dict, parameterization, lindblad_basis,
# mx_basis, truncate, mm_dict['evotype'], state_space)
ret = cls(coeff_blocks, 'auto', mx_basis, mm_dict['evotype'], state_space)
#reinitialize the paramvals attribute from memoized dict. Rederiving this from the block data has
#been leading to sign ambiguity on deserialization.
ret.paramvals = ret._decodemx(mm_dict['paramvals'])
return ret

def _is_similar(self, other, rtol, atol):
""" Returns True if `other` model member (which it guaranteed to be the same type as self) has
Expand Down
9 changes: 3 additions & 6 deletions test/test_packages/drivers/test_drivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,15 +312,12 @@ def test_StandardGST_checkpointing(self):
advanced_options= {'max_iterations':3})

#Assert that this gives the same result as before:
#diff = norm(result_standardgst.estimates['CPTPLND'].models['final iteration estimate'].to_vector()-
# result_standardgst_warmstart.estimates['CPTPLND'].models['final iteration estimate'].to_vector())
diff = pygsti.tools.logl(result_standardgst.estimates['CPTPLND'].models['final iteration estimate'], ds)- \
pygsti.tools.logl(result_standardgst_warmstart.estimates['CPTPLND'].models['final iteration estimate'], ds)

diff = norm(result_standardgst.estimates['CPTPLND'].models['final iteration estimate'].to_vector()-
result_standardgst_warmstart.estimates['CPTPLND'].models['final iteration estimate'].to_vector())
diff1 = norm(result_standardgst.estimates['full TP'].models['final iteration estimate'].to_vector()-
result_standardgst_warmstart.estimates['full TP'].models['final iteration estimate'].to_vector())

self.assertTrue(abs(diff)<=1e-6)
self.assertTrue(abs(diff)<=1e-10)
self.assertTrue(diff1<=1e-10)

if __name__ == "__main__":
Expand Down

0 comments on commit 0ab7af2

Please sign in to comment.