-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add json serialization for PauliSum #5367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@mpharrigan and @95-martin-orion our Cereal-ization experts. |
@@ -498,6 +498,23 @@ def _unitary_(self) -> np.ndarray: | |||
return m | |||
raise ValueError(f'{self} is not unitary') | |||
|
|||
def _json_dict_(self): | |||
def key_json(k): | |||
# k is a frozenset, each element of frozen set is a tuple. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use type annotations instead of a comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done,
def _json_dict_(self): | ||
def key_json(k): | ||
# k is a frozenset, each element of frozen set is a tuple. | ||
return [[x for x in e] for e in sorted(list(k))] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorted(list(...))
is redundant: can just do sorted()
[x for x in e]
can just be list(e)
by default, python can serialize tuples. It just makes them lists, so you have to take care when de-serializing.
Final:
return sorted(k)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
'keys': [key_json(k) for k in self._linear_dict.keys()], | ||
'values': list(self._linear_dict.values()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
elsewhere, we serialize dictionaries (and dict-like things) with
{'items': list(linear_dict.items())}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
mapping = dict() | ||
for k, v in zip(keys, values): | ||
mapping[frozenset(tuple(x) for x in k)] = v |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use a dict comprehension here
{frozenset(tuple(x) for x in k): v for k, v in zip(keys, values)}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we use slightly more helpful names than k
x
and e
? Maybe a scheme where their length relates to their degree of nestiness?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renamed. I tend to stop list comprehensions when they get to be double nested, but did this as it doesn't look horrible.
@@ -47,7 +47,6 @@ | |||
'DiagonalGate', | |||
'NeutralAtomDevice', | |||
'PauliInteractionGate', | |||
'PauliSum', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
w00t
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
take one down, pass it around, 51 classes to be serialized on the wall
Thanks @mpharrigan ! Updated per your comments. |
Fixes quantumlib#3071 One approach to this would have been to serialize frozenset and tuple in Cirq, but this instead takes the approach of not relying on this but appropriately serializing and deserializing these.
Fixes quantumlib#3071 One approach to this would have been to serialize frozenset and tuple in Cirq, but this instead takes the approach of not relying on this but appropriately serializing and deserializing these.
Fixes #3071
One approach to this would have been to serialize frozenset and tuple in Cirq, but this instead takes the approach of not relying on this but appropriately serializing and deserializing these.