Skip to content

Commit

Permalink
Fix FSim serialization for zeros and ints (#4646)
Browse files Browse the repository at this point in the history
* Fix FSim serialization for zeros and ints

- Conversion of float args changes 0.0 to a int of 0,
so this fails validation, since the current code
checks isinstance float.
- This PR adds ints to the type checking to fix this.
  • Loading branch information
dstrain115 authored Nov 9, 2021
1 parent 1f0fc1d commit 6f71b55
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
4 changes: 3 additions & 1 deletion cirq-google/cirq_google/serialization/circuit_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,9 @@ def _deserialize_gate_op(
arg_function_language=arg_function_language,
required_arg_name=None,
)
if isinstance(theta, (float, sympy.Basic)) and isinstance(phi, (float, sympy.Basic)):
if isinstance(theta, (int, float, sympy.Basic)) and isinstance(
phi, (int, float, sympy.Basic)
):
op = cirq.FSimGate(theta=theta, phi=phi)(*qubits)
else:
raise ValueError('theta and phi must be specified for FSimGate')
Expand Down
35 changes: 35 additions & 0 deletions cirq-google/cirq_google/serialization/circuit_serializer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,23 @@ def circuit_proto(json: Dict, qubits: List[str]):
}
),
),
(
cirq.FSimGate(theta=2 + sympy.Symbol('a'), phi=1)(Q0, Q1),
op_proto(
{
'fsimgate': {
'theta': {
'func': {
'type': 'add',
'args': [{'arg_value': {'float_value': 2.00}}, {'symbol': 'a'}],
}
},
'phi': {'float_value': 1.0},
},
'qubit_constant_index': [0, 1],
}
),
),
(
cirq.FSimGate(theta=0.5, phi=0.25)(Q0, Q1),
op_proto(
Expand All @@ -226,6 +243,24 @@ def circuit_proto(json: Dict, qubits: List[str]):
}
),
),
(
cirq.FSimGate(theta=0.5, phi=0.0)(Q0, Q1),
op_proto(
{
'fsimgate': {'theta': {'float_value': 0.5}, 'phi': {'float_value': 0.0}},
'qubit_constant_index': [0, 1],
}
),
),
(
cirq.FSimGate(theta=2, phi=1)(Q0, Q1),
op_proto(
{
'fsimgate': {'theta': {'float_value': 2.0}, 'phi': {'float_value': 1.0}},
'qubit_constant_index': [0, 1],
}
),
),
(
cirq.WaitGate(duration=cirq.Duration(nanos=15))(Q0),
op_proto(
Expand Down

0 comments on commit 6f71b55

Please sign in to comment.