Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
49 changes: 34 additions & 15 deletions cirq-google/cirq_google/serialization/circuit_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@
_SERIALIZER_NAME = 'v2_5'


def _serialize_phasedxz_gate(
gate: cirq.PhasedXZGate,
msg: v2.program_pb2.Operation,
arg_function_language: Optional[str] = None,
):
arg_func_langs.float_arg_to_proto(
gate.x_exponent,
out=msg.phasedxzgate.x_exponent,
arg_function_language=arg_function_language,
)
arg_func_langs.float_arg_to_proto(
gate.z_exponent,
out=msg.phasedxzgate.z_exponent,
arg_function_language=arg_function_language,
)
arg_func_langs.float_arg_to_proto(
gate.axis_phase_exponent,
out=msg.phasedxzgate.axis_phase_exponent,
arg_function_language=arg_function_language,
)


class CircuitSerializer(serializer.Serializer):
"""A class for serializing and deserializing programs and operations.

Expand Down Expand Up @@ -178,21 +200,7 @@ def _serialize_gate_op(
elif isinstance(gate, (cirq.PhasedXZGate, cirq.ops.SingleQubitCliffordGate)):
if isinstance(gate, cirq.ops.SingleQubitCliffordGate):
gate = gate.to_phased_xz_gate()
arg_func_langs.float_arg_to_proto(
gate.x_exponent,
out=msg.phasedxzgate.x_exponent,
arg_function_language=arg_function_language,
)
arg_func_langs.float_arg_to_proto(
gate.z_exponent,
out=msg.phasedxzgate.z_exponent,
arg_function_language=arg_function_language,
)
arg_func_langs.float_arg_to_proto(
gate.axis_phase_exponent,
out=msg.phasedxzgate.axis_phase_exponent,
arg_function_language=arg_function_language,
)
_serialize_phasedxz_gate(gate, msg, arg_function_language)
elif isinstance(gate, cirq.CZPowGate):
arg_func_langs.float_arg_to_proto(
gate.exponent,
Expand Down Expand Up @@ -227,6 +235,17 @@ def _serialize_gate_op(
out=msg.waitgate.duration_nanos,
arg_function_language=arg_function_language,
)
elif isinstance(gate, cirq.IdentityGate):
if cirq.num_qubits(gate) != 1:
raise ValueError(
f'Multiqubit identity gate {gate!r} cannot be serialized. '
'Please decompose the operation into single qubit gates.'
)
_serialize_phasedxz_gate(
cirq.PhasedXZGate(x_exponent=0, z_exponent=0, axis_phase_exponent=0),
msg,
arg_function_language,
)
else:
raise ValueError(f'Cannot serialize op {op!r} of type {type(gate)}')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -681,3 +681,16 @@ def test_circuit_with_cliffords():
)
msg = cg.CIRCUIT_SERIALIZER.serialize(circuit)
assert cg.CIRCUIT_SERIALIZER.deserialize(msg) == want


def test_circuit_with_identity():
q = cirq.GridQubit(7, 4)
c = cirq.Circuit(cirq.I(q))
want = cirq.Circuit(cirq.PhasedXZGate(x_exponent=0, z_exponent=0, axis_phase_exponent=0)(q))
msg = cg.CIRCUIT_SERIALIZER.serialize(c)
assert cg.CIRCUIT_SERIALIZER.deserialize(msg) == want

with pytest.raises(ValueError, match='Multiqubit identity gate.*'):
_ = cg.CIRCUIT_SERIALIZER.serialize(
cirq.Circuit(cirq.identity_each(*cirq.LineQubit.range(2)))
)