Skip to content
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

Serialize SingleQubitCliffordGate #4421

Merged
merged 8 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions cirq-core/cirq/json_resolver_cache.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would be worth having a (parameterized) test in clifford_gate_test.py to run through all the single qubit clifford gates and do the round trip to json and from json and make sure they are equal. This might be a little bit higher confidence than having the single example in json_test_data, especially since that example has an empty inverse map.

Otherwise seems good to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please check now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dstrain115 ping!

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
]
]
]
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
'QuilFormatter',
'QuilOutput',
'SimulationTrialResult',
'SingleQubitCliffordGate',
'SparseSimulatorStep',
'StateVectorMixin',
'TextDiagramDrawer',
Expand Down