-
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
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 commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
return { | ||
'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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
|
||
@classmethod | ||
def _from_json_dict_(cls, keys, values, **kwargs): | ||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. can we use slightly more helpful names than There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
return cls(linear_dict=value.LinearDict(mapping)) | ||
|
||
def expectation_from_state_vector( | ||
self, | ||
state_vector: np.ndarray, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
{ | ||
"cirq_type": "PauliSum", | ||
"keys": [ | ||
[ | ||
[ | ||
{ | ||
"cirq_type": "LineQubit", | ||
"x": 0 | ||
}, | ||
{ | ||
"cirq_type": "_PauliX", | ||
"exponent": 1.0, | ||
"global_shift": 0.0 | ||
} | ||
], | ||
[ | ||
{ | ||
"cirq_type": "LineQubit", | ||
"x": 1 | ||
}, | ||
{ | ||
"cirq_type": "_PauliX", | ||
"exponent": 1.0, | ||
"global_shift": 0.0 | ||
} | ||
] | ||
], | ||
[ | ||
[ | ||
{ | ||
"cirq_type": "LineQubit", | ||
"x": 0 | ||
}, | ||
{ | ||
"cirq_type": "_PauliY", | ||
"exponent": 1.0, | ||
"global_shift": 0.0 | ||
} | ||
], | ||
[ | ||
{ | ||
"cirq_type": "LineQubit", | ||
"x": 1 | ||
}, | ||
{ | ||
"cirq_type": "_PauliY", | ||
"exponent": 1.0, | ||
"global_shift": 0.0 | ||
} | ||
] | ||
] | ||
], | ||
"values": [ | ||
{ | ||
"cirq_type": "complex", | ||
"real": 1.0, | ||
"imag": 0.0 | ||
}, | ||
{ | ||
"cirq_type": "complex", | ||
"real": 0.0, | ||
"imag": 1.0 | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
cirq.PauliSum(cirq.LinearDict({frozenset({(cirq.LineQubit(1), cirq.X), (cirq.LineQubit(0), cirq.X)}): (1+0j), frozenset({(cirq.LineQubit(1), cirq.Y), (cirq.LineQubit(0), cirq.Y)}): (1j)})) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,6 @@ | |
'DiagonalGate', | ||
'NeutralAtomDevice', | ||
'PauliInteractionGate', | ||
'PauliSum', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 |
||
'PauliSumCollector', | ||
'PauliSumExponential', | ||
'PauliTransform', | ||
|
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,