Skip to content

Commit

Permalink
Serialize SingleQubitCliffordGate (#4421)
Browse files Browse the repository at this point in the history
  • Loading branch information
ishmum123 committed Aug 24, 2021
1 parent 932f065 commit 58cda7a
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 1 deletion.
1 change: 1 addition & 0 deletions cirq-core/cirq/json_resolver_cache.py
Expand Up @@ -128,6 +128,7 @@ def two_qubit_matrix_gate(matrix):
'QuantumFourierTransformGate': cirq.QuantumFourierTransformGate,
'RepetitionsStoppingCriteria': cirq.work.RepetitionsStoppingCriteria,
'ResetChannel': cirq.ResetChannel,
'SingleQubitCliffordGate': cirq.SingleQubitCliffordGate,
'SingleQubitMatrixGate': single_qubit_matrix_gate,
'SingleQubitPauliStringGateOperation': cirq.SingleQubitPauliStringGateOperation,
'SingleQubitReadoutCalibrationResult': cirq.experiments.SingleQubitReadoutCalibrationResult,
Expand Down
15 changes: 15 additions & 0 deletions cirq-core/cirq/ops/clifford_gate.py
Expand Up @@ -436,6 +436,21 @@ def __repr__(self) -> str:
f'Y:{y_sign}{y.to!s}, Z:{z_sign}{z.to!s})'
)

@classmethod
def _from_json_dict_(cls, _rotation_map, _inverse_map, **kwargs):
return cls(
_rotation_map=dict([[k, PauliTransform(*v)] for k, v in _rotation_map]),
_inverse_map=dict([[k, PauliTransform(*v)] for k, v in _inverse_map]),
)

def _json_dict_(self) -> Dict[str, Any]:
return {
'cirq_type': self.__class__.__name__,
# JSON requires mappings to have string keys.
'_rotation_map': list(self._rotation_map.items()),
'_inverse_map': list(self._inverse_map.items()),
}

def _circuit_diagram_info_(
self, args: 'cirq.CircuitDiagramInfoArgs'
) -> 'cirq.CircuitDiagramInfo':
Expand Down
5 changes: 5 additions & 0 deletions cirq-core/cirq/ops/clifford_gate_test.py
Expand Up @@ -443,6 +443,11 @@ def test_commutes_single_qubit_gate(gate, other):
assert_allclose_up_to_global_phase(mat, mat_swap, rtol=1e-7, atol=1e-7)


@pytest.mark.parametrize('gate', _all_clifford_gates())
def test_parses_single_qubit_gate(gate):
assert gate == cirq.read_json(json_text=(cirq.to_json(gate)))


@pytest.mark.parametrize(
'gate,pauli,half_turns',
itertools.product(_all_clifford_gates(), _paulis, (1.0, 0.25, 0.5, -0.5)),
Expand Down
@@ -0,0 +1,97 @@
{
"cirq_type": "SingleQubitCliffordGate",
"_rotation_map": [
[
{
"cirq_type": "_PauliX",
"exponent": 1.0,
"global_shift": 0.0
},
[
{
"cirq_type": "_PauliX",
"exponent": 1.0,
"global_shift": 0.0
},
false
]
],
[
{
"cirq_type": "_PauliZ",
"exponent": 1.0,
"global_shift": 0.0
},
[
{
"cirq_type": "_PauliY",
"exponent": 1.0,
"global_shift": 0.0
},
false
]
],
[
{
"cirq_type": "_PauliY",
"exponent": 1.0,
"global_shift": 0.0
},
[
{
"cirq_type": "_PauliZ",
"exponent": 1.0,
"global_shift": 0.0
},
true
]
]
],
"_inverse_map": [
[
{
"cirq_type": "_PauliX",
"exponent": 1.0,
"global_shift": 0.0
},
[
{
"cirq_type": "_PauliX",
"exponent": 1.0,
"global_shift": 0.0
},
false
]
],
[
{
"cirq_type": "_PauliY",
"exponent": 1.0,
"global_shift": 0.0
},
[
{
"cirq_type": "_PauliZ",
"exponent": 1.0,
"global_shift": 0.0
},
false
]
],
[
{
"cirq_type": "_PauliZ",
"exponent": 1.0,
"global_shift": 0.0
},
[
{
"cirq_type": "_PauliY",
"exponent": 1.0,
"global_shift": 0.0
},
true
]
]
]
}
@@ -0,0 +1,2 @@
cirq.SingleQubitCliffordGate(_rotation_map={cirq.X: cirq.PauliTransform(to=cirq.X, flip=False), cirq.Z: cirq.PauliTransform(to=cirq.Y, flip=False), cirq.Y: cirq.PauliTransform(to=cirq.Z, flip=True)},
_inverse_map={cirq.X: cirq.PauliTransform(to=cirq.X, flip=False), cirq.Y: cirq.PauliTransform(to=cirq.Z, flip=False), cirq.Z: cirq.PauliTransform(to=cirq.Y, flip=True)})
1 change: 0 additions & 1 deletion cirq-core/cirq/protocols/json_test_data/spec.py
Expand Up @@ -64,7 +64,6 @@
'QuilFormatter',
'QuilOutput',
'SimulationTrialResult',
'SingleQubitCliffordGate',
'SparseSimulatorStep',
'StateVectorMixin',
'TextDiagramDrawer',
Expand Down

0 comments on commit 58cda7a

Please sign in to comment.