Skip to content

Commit

Permalink
Add (de)serializer for PhasedXZGate (#2598)
Browse files Browse the repository at this point in the history
  • Loading branch information
maffoo authored and CirqBot committed Nov 27, 2019
1 parent 7f8a4ff commit 912c82a
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
47 changes: 47 additions & 0 deletions cirq/google/common_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,53 @@ def _near_mod_2(e, t, atol=1e-8):
]


#
# PhasedXZ gate (de)serializer
# TODO(#2602): Move to SINGLE_QUBIT_(DE)SERIALIZERS when server support is in.
#

PHASED_X_Z_SERIALIZER = op_serializer.GateOpSerializer(
gate_type=ops.PhasedXZGate,
serialized_gate_id='xyz',
args=[
op_serializer.SerializingArg(
serialized_name='x_exponent',
serialized_type=float,
gate_getter='x_exponent',
),
op_serializer.SerializingArg(
serialized_name='z_exponent',
serialized_type=float,
gate_getter='z_exponent',
),
op_serializer.SerializingArg(
serialized_name='axis_phase_exponent',
serialized_type=float,
gate_getter='axis_phase_exponent',
),
],
)

PHASED_X_Z_DESERIALIZER = op_deserializer.GateOpDeserializer(
serialized_gate_id='xyz',
gate_constructor=ops.PhasedXZGate,
args=[
op_deserializer.DeserializingArg(
serialized_name='x_exponent',
constructor_arg_name='x_exponent',
),
op_deserializer.DeserializingArg(
serialized_name='z_exponent',
constructor_arg_name='z_exponent',
),
op_deserializer.DeserializingArg(
serialized_name='axis_phase_exponent',
constructor_arg_name='axis_phase_exponent',
),
],
)


#
# Measurement Serializer and Deserializer
#
Expand Down
57 changes: 57 additions & 0 deletions cirq/google/common_serializers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,63 @@ def test_half_pi_does_not_serialize_arbitrary_xy():
HALF_PI_GATE_SET.serialize_op_dict(gate(q))


@pytest.mark.parametrize(('x_exponent', 'z_exponent', 'axis_phase_exponent'), [
(0, 0, 0),
(1, 0, 0),
(0, 1, 0),
(0.5, 0, 0.5),
(0.5, 0.5, 0.5),
(0.25, 0.375, 0.125),
])
def test_serialize_deserialize_arbitrary_xyz(
x_exponent,
z_exponent,
axis_phase_exponent,
):
gateset = cg.serializable_gate_set.SerializableGateSet(
gate_set_name='test_xyz',
serializers=([cgc.PHASED_X_Z_SERIALIZER]),
deserializers=([cgc.PHASED_X_Z_DESERIALIZER]))
gate = cirq.PhasedXZGate(
x_exponent=x_exponent,
z_exponent=z_exponent,
axis_phase_exponent=axis_phase_exponent,
)
op = gate.on(cirq.GridQubit(1, 2))
expected = {
'gate': {
'id': 'xyz'
},
'args': {
'x_exponent': {
'arg_value': {
'float_value': x_exponent
}
},
'z_exponent': {
'arg_value': {
'float_value': z_exponent
}
},
'axis_phase_exponent': {
'arg_value': {
'float_value': axis_phase_exponent
}
}
},
'qubits': [{
'id': '1_2'
}]
}
assert gateset.serialize_op_dict(op) == expected
deserialized_op = gateset.deserialize_op_dict(expected)
cirq.testing.assert_allclose_up_to_global_phase(
cirq.unitary(deserialized_op),
cirq.unitary(op),
atol=1e-7,
)


@pytest.mark.parametrize(('qubits', 'qubit_ids', 'key', 'invert_mask'), [
([cirq.GridQubit(1, 1)], ['1_1'], 'a', ()),
([cirq.GridQubit(1, 2)], ['1_2'], 'b', (True,)),
Expand Down

0 comments on commit 912c82a

Please sign in to comment.