Skip to content

Commit

Permalink
Make OpIdentifier serializable for all inputs (#6295)
Browse files Browse the repository at this point in the history
  • Loading branch information
epelaaez authored Sep 26, 2023
1 parent 1948e73 commit fd18da5
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 16 deletions.
3 changes: 3 additions & 0 deletions cirq-core/cirq/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
12 changes: 7 additions & 5 deletions cirq-core/cirq/devices/noise_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)


Expand Down
7 changes: 7 additions & 0 deletions cirq-core/cirq/devices/noise_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))],
Expand Down
55 changes: 45 additions & 10 deletions cirq-core/cirq/protocols/json_test_data/OpIdentifier.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
{
"cirq_type": "OpIdentifier",
"gate_type": "XPowGate",
"qubits": [
{
"cirq_type": "LineQubit",
"x": 1
}
]
}
[
{
"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
}
]
}
]
8 changes: 7 additions & 1 deletion cirq-core/cirq/protocols/json_test_data/OpIdentifier.repr
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
[
cirq.devices.noise_utils.OpIdentifier(
cirq.ops.common_gates.XPowGate,
cirq.LineQubit(1)
)
),
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)
)
]

0 comments on commit fd18da5

Please sign in to comment.