Skip to content

Ensure SycamoreGate instances are preserved over serialization #7093

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 2 commits into from
Feb 26, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion cirq-google/cirq_google/serialization/circuit_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from typing import Any, Dict, List, Optional
import warnings
import numpy as np
import sympy

import cirq
Expand All @@ -26,6 +27,7 @@
InternalTag,
FSimViaModelTag,
DynamicalDecouplingTag,
SYC,
)
from cirq_google.ops.calibration_tag import CalibrationTag
from cirq_google.experimental.ops import CouplerPulse
Expand Down Expand Up @@ -783,7 +785,16 @@ def _deserialize_gate_op(
if isinstance(theta, (int, float, sympy.Basic)) and isinstance(
phi, (int, float, sympy.Basic)
):
op = cirq.FSimGate(theta=theta, phi=phi)(*qubits)
if (
isinstance(theta, float)
and isinstance(phi, float)
and np.isclose(theta, np.pi / 2)
and np.isclose(phi, np.pi / 6)
and not operation_proto.fsimgate.translate_via_model
):
op = SYC(*qubits)
Copy link
Collaborator

Choose a reason for hiding this comment

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

What happens if the SYC gate also has the FSimViaModelTag?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good call. Added a condition so that it does not convert to SYC if it is tagged.

else:
op = cirq.FSimGate(theta=theta, phi=phi)(*qubits)
else:
raise ValueError('theta and phi must be specified for FSimGate')
if operation_proto.fsimgate.translate_via_model:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,15 @@ def from_proto(
)


def test_serdes_preserves_syc():
serializer = cg.CircuitSerializer()
c = cirq.Circuit(cg.SYC(cirq.q(0, 0), cirq.q(0, 1)))
msg = serializer.serialize(c)
deserialized_circuit = serializer.deserialize(msg)
assert deserialized_circuit == c
assert isinstance(c[0][cirq.q(0, 0)].gate, cg.SycamoreGate)


@pytest.mark.parametrize('use_constants_table', [True, False])
def test_custom_serializer(use_constants_table: bool):
c = cirq.Circuit(BingBongGate(param=2.5)(cirq.q(0, 0)))
Expand Down