Skip to content

Commit ffae6b5

Browse files
dabaconrht
authored andcommitted
Add json serialization for PauliSum (quantumlib#5367)
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.
1 parent f379169 commit ffae6b5

File tree

5 files changed

+83
-1
lines changed

5 files changed

+83
-1
lines changed

cirq-core/cirq/json_resolver_cache.py

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ def _symmetricalqidpair(qids):
146146
'PauliString': cirq.PauliString,
147147
'PauliStringPhasor': cirq.PauliStringPhasor,
148148
'PauliStringPhasorGate': cirq.PauliStringPhasorGate,
149+
'PauliSum': cirq.PauliSum,
149150
'_PauliX': cirq.ops.pauli_gates._PauliX,
150151
'_PauliY': cirq.ops.pauli_gates._PauliY,
151152
'_PauliZ': cirq.ops.pauli_gates._PauliZ,

cirq-core/cirq/ops/linear_combinations.py

+14
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,20 @@ def _unitary_(self) -> np.ndarray:
498498
return m
499499
raise ValueError(f'{self} is not unitary')
500500

501+
def _json_dict_(self):
502+
def key_json(k: UnitPauliStringT):
503+
return [list(e) for e in sorted(k)]
504+
505+
return {'items': list((key_json(k), v) for k, v in self._linear_dict.items())}
506+
507+
@classmethod
508+
def _from_json_dict_(cls, items, **kwargs):
509+
mapping = {
510+
frozenset(tuple(qid_pauli) for qid_pauli in unit_pauli_string): val
511+
for unit_pauli_string, val in items
512+
}
513+
return cls(linear_dict=value.LinearDict(mapping))
514+
501515
def expectation_from_state_vector(
502516
self,
503517
state_vector: np.ndarray,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"cirq_type": "PauliSum",
3+
"items": [
4+
[
5+
[
6+
[
7+
{
8+
"cirq_type": "LineQubit",
9+
"x": 0
10+
},
11+
{
12+
"cirq_type": "_PauliX",
13+
"exponent": 1.0,
14+
"global_shift": 0.0
15+
}
16+
],
17+
[
18+
{
19+
"cirq_type": "LineQubit",
20+
"x": 1
21+
},
22+
{
23+
"cirq_type": "_PauliX",
24+
"exponent": 1.0,
25+
"global_shift": 0.0
26+
}
27+
]
28+
],
29+
{
30+
"cirq_type": "complex",
31+
"real": 1.0,
32+
"imag": 0.0
33+
}
34+
],
35+
[
36+
[
37+
[
38+
{
39+
"cirq_type": "LineQubit",
40+
"x": 0
41+
},
42+
{
43+
"cirq_type": "_PauliY",
44+
"exponent": 1.0,
45+
"global_shift": 0.0
46+
}
47+
],
48+
[
49+
{
50+
"cirq_type": "LineQubit",
51+
"x": 1
52+
},
53+
{
54+
"cirq_type": "_PauliY",
55+
"exponent": 1.0,
56+
"global_shift": 0.0
57+
}
58+
]
59+
],
60+
{
61+
"cirq_type": "complex",
62+
"real": 0.0,
63+
"imag": 1.0
64+
}
65+
]
66+
]
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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)}))

cirq-core/cirq/protocols/json_test_data/spec.py

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
'DiagonalGate',
4848
'NeutralAtomDevice',
4949
'PauliInteractionGate',
50-
'PauliSum',
5150
'PauliSumCollector',
5251
'PauliSumExponential',
5352
'PauliTransform',

0 commit comments

Comments
 (0)