Skip to content

Make OpIdentifier serializable for all inputs #6295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions cirq-core/cirq/devices/noise_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,25 @@ 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})'
if hasattr(self.gate_type, '__qualname__'):
gate = f'{self.gate_type.__module__}.{self.gate_type.__qualname__}'
else:
gate = repr(self.gate_type)
return f'cirq.devices.noise_utils.OpIdentifier({gate}, {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)
)
]