diff --git a/cirq-core/cirq/_compat.py b/cirq-core/cirq/_compat.py index 566ec1c2a34..7b0923701d5 100644 --- a/cirq-core/cirq/_compat.py +++ b/cirq-core/cirq/_compat.py @@ -191,6 +191,9 @@ def _print(self, expr, **kwargs): if isinstance(value, Dict): return '{' + ','.join(f"{proper_repr(k)}: {proper_repr(v)}" for k, v in value.items()) + '}' + if hasattr(value, "__qualname__"): + return f"{value.__module__}.{value.__qualname__}" + return repr(value) diff --git a/cirq-core/cirq/devices/noise_utils.py b/cirq-core/cirq/devices/noise_utils.py index 4086ba5c741..7fb30a040ba 100644 --- a/cirq-core/cirq/devices/noise_utils.py +++ b/cirq-core/cirq/devices/noise_utils.py @@ -16,6 +16,7 @@ import numpy as np from cirq import ops, protocols, value +from cirq._compat import proper_repr if TYPE_CHECKING: import cirq @@ -78,20 +79,21 @@ def __str__(self): return f'{self.gate_type}{self.qubits}' def __repr__(self) -> str: - fullname = f'{self.gate_type.__module__}.{self.gate_type.__qualname__}' qubits = ', '.join(map(repr, self.qubits)) - return f'cirq.devices.noise_utils.OpIdentifier({fullname}, {qubits})' + return f'cirq.devices.noise_utils.OpIdentifier({proper_repr(self.gate_type)}, {qubits})' def _value_equality_values_(self) -> Any: return (self.gate_type, self.qubits) def _json_dict_(self) -> Dict[str, Any]: - gate_json = protocols.json_cirq_type(self._gate_type) - return {'gate_type': gate_json, 'qubits': self._qubits} + if hasattr(self.gate_type, '__name__'): + return {'gate_type': protocols.json_cirq_type(self._gate_type), 'qubits': self._qubits} + return {'gate_type': self._gate_type, 'qubits': self._qubits} @classmethod def _from_json_dict_(cls, gate_type, qubits, **kwargs) -> 'OpIdentifier': - gate_type = protocols.cirq_type_from_json(gate_type) + if isinstance(gate_type, str): + gate_type = protocols.cirq_type_from_json(gate_type) return cls(gate_type, *qubits) diff --git a/cirq-core/cirq/devices/noise_utils_test.py b/cirq-core/cirq/devices/noise_utils_test.py index 412ff1fa192..df9833424ec 100644 --- a/cirq-core/cirq/devices/noise_utils_test.py +++ b/cirq-core/cirq/devices/noise_utils_test.py @@ -62,6 +62,13 @@ def test_op_id_swap(): assert cirq.CZ(q1, q0) in swap_id +def test_op_id_instance(): + q0 = cirq.LineQubit.range(1)[0] + gate = cirq.SingleQubitCliffordGate.from_xz_map((cirq.X, False), (cirq.Z, False)) + op_id = OpIdentifier(gate, q0) + cirq.testing.assert_equivalent_repr(op_id) + + @pytest.mark.parametrize( 'decay_constant,num_qubits,expected_output', [(0.01, 1, 1 - (0.99 * 1 / 2)), (0.05, 2, 1 - (0.95 * 3 / 4))], diff --git a/cirq-core/cirq/protocols/json_test_data/OpIdentifier.json b/cirq-core/cirq/protocols/json_test_data/OpIdentifier.json index d33b909367d..e9e3f35d109 100644 --- a/cirq-core/cirq/protocols/json_test_data/OpIdentifier.json +++ b/cirq-core/cirq/protocols/json_test_data/OpIdentifier.json @@ -1,10 +1,45 @@ -{ - "cirq_type": "OpIdentifier", - "gate_type": "XPowGate", - "qubits": [ - { - "cirq_type": "LineQubit", - "x": 1 - } - ] -} \ No newline at end of file +[ + { + "cirq_type": "OpIdentifier", + "gate_type": "XPowGate", + "qubits": [ + { + "cirq_type": "LineQubit", + "x": 1 + } + ] + }, + { + "cirq_type": "OpIdentifier", + "gate_type": { + "cirq_type": "CliffordGate", + "n": 1, + "rs": [ + false, + false + ], + "xs": [ + [ + true + ], + [ + false + ] + ], + "zs": [ + [ + false + ], + [ + true + ] + ] + }, + "qubits": [ + { + "cirq_type": "LineQubit", + "x": 0 + } + ] + } +] \ No newline at end of file diff --git a/cirq-core/cirq/protocols/json_test_data/OpIdentifier.repr b/cirq-core/cirq/protocols/json_test_data/OpIdentifier.repr index 6b991bb0b2c..010a806a9e2 100644 --- a/cirq-core/cirq/protocols/json_test_data/OpIdentifier.repr +++ b/cirq-core/cirq/protocols/json_test_data/OpIdentifier.repr @@ -1,4 +1,10 @@ +[ cirq.devices.noise_utils.OpIdentifier( cirq.ops.common_gates.XPowGate, cirq.LineQubit(1) -) \ No newline at end of file +), +cirq.devices.noise_utils.OpIdentifier( + 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)), + cirq.LineQubit(0) +) +] \ No newline at end of file