Skip to content
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

Add json serialization for PauliSum #5367

Merged
merged 4 commits into from
May 16, 2022
Merged

Conversation

dabacon
Copy link
Collaborator

@dabacon dabacon commented May 13, 2022

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.

@dabacon dabacon requested review from a team, vtomole and cduck as code owners May 13, 2022 23:39
@dabacon dabacon requested a review from mpharrigan May 13, 2022 23:39
@CirqBot CirqBot added the size: M 50< lines changed <250 label May 13, 2022
@dabacon
Copy link
Collaborator Author

dabacon commented May 13, 2022

@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.
Copy link
Collaborator

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?

Copy link
Collaborator Author

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))]
Copy link
Collaborator

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)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines 507 to 508
'keys': [key_json(k) for k in self._linear_dict.keys()],
'values': list(self._linear_dict.values()),
Copy link
Collaborator

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())}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines 513 to 515
mapping = dict()
for k, v in zip(keys, values):
mapping[frozenset(tuple(x) for x in k)] = v
Copy link
Collaborator

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)}

Copy link
Collaborator

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?

Copy link
Collaborator Author

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',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

w00t

Copy link
Collaborator Author

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

@dabacon
Copy link
Collaborator Author

dabacon commented May 14, 2022

Thanks @mpharrigan ! Updated per your comments.

@dabacon dabacon added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label May 16, 2022
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label May 16, 2022
@CirqBot CirqBot merged commit 1e181f0 into quantumlib:master May 16, 2022
@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels May 16, 2022
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: M 50< lines changed <250
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement JSON serialization for PauliSum
3 participants