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 all commits
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
14 changes: 14 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,20 @@ def _unitary_(self) -> np.ndarray:
return m
raise ValueError(f'{self} is not unitary')

def _json_dict_(self):
def key_json(k: UnitPauliStringT):
return [list(e) for e in sorted(k)]

return {'items': list((key_json(k), v) for k, v in self._linear_dict.items())}

@classmethod
def _from_json_dict_(cls, items, **kwargs):
mapping = {
frozenset(tuple(qid_pauli) for qid_pauli in unit_pauli_string): val
for unit_pauli_string, val in items
}
return cls(linear_dict=value.LinearDict(mapping))

def expectation_from_state_vector(
self,
state_vector: np.ndarray,
Expand Down
67 changes: 67 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,67 @@
{
"cirq_type": "PauliSum",
"items": [
[
[
[
{
"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": "complex",
"real": 1.0,
"imag": 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
}
]
],
{
"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