Skip to content

Commit 1d06580

Browse files
committed
backward compat
1 parent d0edee1 commit 1d06580

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

glue/cirq/stimcirq/_obs_annotation.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def __init__(
1616
*,
1717
parity_keys: Iterable[str] = (),
1818
relative_keys: Iterable[int] = (),
19-
pauli_keys: Iterable[tuple[cirq.Qid, str]] = (),
19+
pauli_keys: Iterable[tuple[cirq.Qid, str]] | Iterable[str] = (),
2020
observable_index: int,
2121
):
2222
"""
@@ -29,7 +29,14 @@ def __init__(
2929
"""
3030
self.parity_keys = frozenset(parity_keys)
3131
self.relative_keys = frozenset(relative_keys)
32-
self.pauli_keys = frozenset(pauli_keys)
32+
_pauli_keys = []
33+
for k in pauli_keys:
34+
if isinstance(k, str):
35+
# For backward compatibility
36+
_pauli_keys.append((cirq.LineQubit(int(k[1:])), k[0]))
37+
else:
38+
_pauli_keys.append(tuple(k))
39+
self.pauli_keys = frozenset(_pauli_keys)
3340
self.observable_index = observable_index
3441

3542
@property

glue/cirq/stimcirq/_obs_annotation_test.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,19 @@ def test_json_serialization_with_pauli_keys():
209209
def test_json_backwards_compat_exact():
210210
raw = stimcirq.CumulativeObservableAnnotation(parity_keys=['z'], relative_keys=[-2], observable_index=5)
211211
packed_v1 = '{\n "cirq_type": "CumulativeObservableAnnotation",\n "parity_keys": [\n "z"\n ],\n "observable_index": 5,\n "relative_keys": [\n -2\n ]\n}'
212-
packed_v2 ='{\n "cirq_type": "CumulativeObservableAnnotation",\n "parity_keys": [\n "z"\n ],\n "observable_index": 5,\n "pauli_keys": [],\n "relative_keys": [\n -2\n ]\n}'
212+
packed_v2 = '{\n "cirq_type": "CumulativeObservableAnnotation",\n "parity_keys": [\n "z"\n ],\n "observable_index": 5,\n "pauli_keys": [],\n "relative_keys": [\n -2\n ]\n}'
213+
packed_v3 = '{\n "cirq_type": "CumulativeObservableAnnotation",\n "parity_keys": [\n "z"\n ],\n "observable_index": 5,\n "pauli_keys": [],\n "relative_keys": [\n -2\n ]\n}'
213214
assert cirq.read_json(json_text=packed_v1, resolvers=[*cirq.DEFAULT_RESOLVERS, stimcirq.JSON_RESOLVER]) == raw
214215
assert cirq.read_json(json_text=packed_v2, resolvers=[*cirq.DEFAULT_RESOLVERS, stimcirq.JSON_RESOLVER]) == raw
215-
assert cirq.to_json(raw) == packed_v2
216+
assert cirq.read_json(json_text=packed_v3, resolvers=[*cirq.DEFAULT_RESOLVERS, stimcirq.JSON_RESOLVER]) == raw
217+
assert cirq.to_json(raw) == packed_v3
216218

217219
# With pauli_keys
218220
pauli_keys = [(cirq.LineQubit(0), "X"), (cirq.LineQubit(1), "Y"), (cirq.LineQubit(2), "Z")]
219221
raw = stimcirq.CumulativeObservableAnnotation(parity_keys=['z'], relative_keys=[-2], observable_index=5, pauli_keys=pauli_keys)
220-
packed_v2 ='{\n "cirq_type": "CumulativeObservableAnnotation",\n "parity_keys": [\n "z"\n ],\n "observable_index": 5,\n "pauli_keys": [\n [\n {\n "cirq_type": "LineQubit",\n "x": 0\n },\n "X"\n ],\n [\n {\n "cirq_type": "LineQubit",\n "x": 1\n },\n "Y"\n ],\n [\n {\n "cirq_type": "LineQubit",\n "x": 2\n },\n "Z"\n ]\n ],\n "relative_keys": [\n -2\n ]\n}'
222+
packed_v2 ='{\n "cirq_type": "CumulativeObservableAnnotation",\n "parity_keys": [\n "z"\n ],\n "observable_index": 5,\n "pauli_keys": [\n "X0",\n "Y1",\n "Z2"\n ],\n "relative_keys": [\n -2\n ]\n}'
223+
packed_v3 ='{\n "cirq_type": "CumulativeObservableAnnotation",\n "parity_keys": [\n "z"\n ],\n "observable_index": 5,\n "pauli_keys": [\n [\n {\n "cirq_type": "LineQubit",\n "x": 0\n },\n "X"\n ],\n [\n {\n "cirq_type": "LineQubit",\n "x": 1\n },\n "Y"\n ],\n [\n {\n "cirq_type": "LineQubit",\n "x": 2\n },\n "Z"\n ]\n ],\n "relative_keys": [\n -2\n ]\n}'
224+
221225
assert cirq.read_json(json_text=packed_v2, resolvers=[*cirq.DEFAULT_RESOLVERS, stimcirq.JSON_RESOLVER]) == raw
222-
assert cirq.to_json(raw) == packed_v2
226+
assert cirq.read_json(json_text=packed_v3, resolvers=[*cirq.DEFAULT_RESOLVERS, stimcirq.JSON_RESOLVER]) == raw
227+
assert cirq.to_json(raw) == packed_v3

0 commit comments

Comments
 (0)