Skip to content

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

Merged
merged 4 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cirq-core/cirq/json_resolver_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ def _symmetricalqidpair(qids):
'PauliString': cirq.PauliString,
'PauliStringPhasor': cirq.PauliStringPhasor,
'PauliStringPhasorGate': cirq.PauliStringPhasorGate,
'PauliSum': cirq.PauliSum,
'_PauliX': cirq.ops.pauli_gates._PauliX,
'_PauliY': cirq.ops.pauli_gates._PauliY,
'_PauliZ': cirq.ops.pauli_gates._PauliZ,
Expand Down
17 changes: 17 additions & 0 deletions cirq-core/cirq/ops/linear_combinations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,

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


return {
'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

}

@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
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.

return cls(linear_dict=value.LinearDict(mapping))

def expectation_from_state_vector(
self,
state_vector: np.ndarray,
Expand Down
65 changes: 65 additions & 0 deletions cirq-core/cirq/protocols/json_test_data/PauliSum.json
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
}
]
}
1 change: 1 addition & 0 deletions cirq-core/cirq/protocols/json_test_data/PauliSum.repr
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)}))
1 change: 0 additions & 1 deletion cirq-core/cirq/protocols/json_test_data/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

'PauliSumCollector',
'PauliSumExponential',
'PauliTransform',
Expand Down