Skip to content

Commit 8f2f198

Browse files
authored
Ensure SycamoreGate instances are preserved over serialization (#7093)
* Ensure SycamoreGate instances are preserved over serialization - Put in a special case for fsimgate deserialization so that sycamore gates are serialized to that representation. - This will avoid potential floating point errors when deserializing. * Don't convert to SYC if tags on the FSim.
1 parent 61548e0 commit 8f2f198

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

cirq-google/cirq_google/serialization/circuit_serializer.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from typing import Any, Dict, List, Optional
1818
import warnings
19+
import numpy as np
1920
import sympy
2021

2122
import cirq
@@ -26,6 +27,7 @@
2627
InternalTag,
2728
FSimViaModelTag,
2829
DynamicalDecouplingTag,
30+
SYC,
2931
)
3032
from cirq_google.ops.calibration_tag import CalibrationTag
3133
from cirq_google.experimental.ops import CouplerPulse
@@ -621,7 +623,16 @@ def _deserialize_gate_op(
621623
if isinstance(theta, (int, float, sympy.Basic)) and isinstance(
622624
phi, (int, float, sympy.Basic)
623625
):
624-
op = cirq.FSimGate(theta=theta, phi=phi)(*qubits)
626+
if (
627+
isinstance(theta, float)
628+
and isinstance(phi, float)
629+
and np.isclose(theta, np.pi / 2)
630+
and np.isclose(phi, np.pi / 6)
631+
and not operation_proto.fsimgate.translate_via_model
632+
):
633+
op = SYC(*qubits)
634+
else:
635+
op = cirq.FSimGate(theta=theta, phi=phi)(*qubits)
625636
else:
626637
raise ValueError('theta and phi must be specified for FSimGate')
627638
if operation_proto.fsimgate.translate_via_model:

cirq-google/cirq_google/serialization/circuit_serializer_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,15 @@ def from_proto(
998998
)
999999

10001000

1001+
def test_serdes_preserves_syc():
1002+
serializer = cg.CircuitSerializer()
1003+
c = cirq.Circuit(cg.SYC(cirq.q(0, 0), cirq.q(0, 1)))
1004+
msg = serializer.serialize(c)
1005+
deserialized_circuit = serializer.deserialize(msg)
1006+
assert deserialized_circuit == c
1007+
assert isinstance(c[0][cirq.q(0, 0)].gate, cg.SycamoreGate)
1008+
1009+
10011010
@pytest.mark.parametrize('use_constants_table', [True, False])
10021011
def test_custom_serializer(use_constants_table: bool):
10031012
c = cirq.Circuit(BingBongGate(param=2.5)(cirq.q(0, 0)))

0 commit comments

Comments
 (0)