Skip to content

Commit 527d7af

Browse files
authored
Make OpIdentifier serializable for all inputs (quantumlib#6295)
1 parent 921ecc7 commit 527d7af

File tree

5 files changed

+69
-16
lines changed

5 files changed

+69
-16
lines changed

cirq/_compat.py

+3
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ def _print(self, expr, **kwargs):
191191
if isinstance(value, Dict):
192192
return '{' + ','.join(f"{proper_repr(k)}: {proper_repr(v)}" for k, v in value.items()) + '}'
193193

194+
if hasattr(value, "__qualname__"):
195+
return f"{value.__module__}.{value.__qualname__}"
196+
194197
return repr(value)
195198

196199

cirq/devices/noise_utils.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import numpy as np
1717

1818
from cirq import ops, protocols, value
19+
from cirq._compat import proper_repr
1920

2021
if TYPE_CHECKING:
2122
import cirq
@@ -78,20 +79,21 @@ def __str__(self):
7879
return f'{self.gate_type}{self.qubits}'
7980

8081
def __repr__(self) -> str:
81-
fullname = f'{self.gate_type.__module__}.{self.gate_type.__qualname__}'
8282
qubits = ', '.join(map(repr, self.qubits))
83-
return f'cirq.devices.noise_utils.OpIdentifier({fullname}, {qubits})'
83+
return f'cirq.devices.noise_utils.OpIdentifier({proper_repr(self.gate_type)}, {qubits})'
8484

8585
def _value_equality_values_(self) -> Any:
8686
return (self.gate_type, self.qubits)
8787

8888
def _json_dict_(self) -> Dict[str, Any]:
89-
gate_json = protocols.json_cirq_type(self._gate_type)
90-
return {'gate_type': gate_json, 'qubits': self._qubits}
89+
if hasattr(self.gate_type, '__name__'):
90+
return {'gate_type': protocols.json_cirq_type(self._gate_type), 'qubits': self._qubits}
91+
return {'gate_type': self._gate_type, 'qubits': self._qubits}
9192

9293
@classmethod
9394
def _from_json_dict_(cls, gate_type, qubits, **kwargs) -> 'OpIdentifier':
94-
gate_type = protocols.cirq_type_from_json(gate_type)
95+
if isinstance(gate_type, str):
96+
gate_type = protocols.cirq_type_from_json(gate_type)
9597
return cls(gate_type, *qubits)
9698

9799

cirq/devices/noise_utils_test.py

+7
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ def test_op_id_swap():
6262
assert cirq.CZ(q1, q0) in swap_id
6363

6464

65+
def test_op_id_instance():
66+
q0 = cirq.LineQubit.range(1)[0]
67+
gate = cirq.SingleQubitCliffordGate.from_xz_map((cirq.X, False), (cirq.Z, False))
68+
op_id = OpIdentifier(gate, q0)
69+
cirq.testing.assert_equivalent_repr(op_id)
70+
71+
6572
@pytest.mark.parametrize(
6673
'decay_constant,num_qubits,expected_output',
6774
[(0.01, 1, 1 - (0.99 * 1 / 2)), (0.05, 2, 1 - (0.95 * 3 / 4))],
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,45 @@
1-
{
2-
"cirq_type": "OpIdentifier",
3-
"gate_type": "XPowGate",
4-
"qubits": [
5-
{
6-
"cirq_type": "LineQubit",
7-
"x": 1
8-
}
9-
]
10-
}
1+
[
2+
{
3+
"cirq_type": "OpIdentifier",
4+
"gate_type": "XPowGate",
5+
"qubits": [
6+
{
7+
"cirq_type": "LineQubit",
8+
"x": 1
9+
}
10+
]
11+
},
12+
{
13+
"cirq_type": "OpIdentifier",
14+
"gate_type": {
15+
"cirq_type": "CliffordGate",
16+
"n": 1,
17+
"rs": [
18+
false,
19+
false
20+
],
21+
"xs": [
22+
[
23+
true
24+
],
25+
[
26+
false
27+
]
28+
],
29+
"zs": [
30+
[
31+
false
32+
],
33+
[
34+
true
35+
]
36+
]
37+
},
38+
"qubits": [
39+
{
40+
"cirq_type": "LineQubit",
41+
"x": 0
42+
}
43+
]
44+
}
45+
]
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
[
12
cirq.devices.noise_utils.OpIdentifier(
23
cirq.ops.common_gates.XPowGate,
34
cirq.LineQubit(1)
4-
)
5+
),
6+
cirq.devices.noise_utils.OpIdentifier(
7+
cirq.CliffordGate.from_clifford_tableau(cirq.CliffordTableau(1,rs=np.array([False, False],dtype=np.dtype('bool')), xs=np.array([[True], [False]], dtype=np.dtype('bool')),zs=np.array([[False], [True]], dtype=np.dtype('bool')), initial_state=0)),
8+
cirq.LineQubit(0)
9+
)
10+
]

0 commit comments

Comments
 (0)