Skip to content
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

Support empty invert_mask in measument gate deserialization #6224

Merged
merged 9 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
24 changes: 12 additions & 12 deletions cirq-google/cirq_google/serialization/circuit_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

"""Support for serializing and deserializing cirq_google.api.v2 protos."""

from typing import cast, Any, Dict, List, Optional
from typing import Any, Dict, List, Optional, Tuple
import sympy

import cirq
Expand Down Expand Up @@ -546,18 +546,18 @@ def _deserialize_gate_op(
arg_function_language=arg_function_language,
required_arg_name=None,
)
invert_mask = cast(
List[bool],
arg_func_langs.arg_from_proto(
operation_proto.measurementgate.invert_mask,
arg_function_language=arg_function_language,
required_arg_name=None,
),
invert_mask_ = arg_func_langs.arg_from_proto(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Having invert_mask and invert_mask_ is pretty confusing. Can we give one a better name?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

renamed to parsed_invert_mask

operation_proto.measurementgate.invert_mask,
arg_function_language=arg_function_language,
required_arg_name=None,
)
if isinstance(invert_mask, list) and isinstance(key, str):
op = cirq.MeasurementGate(
num_qubits=len(qubits), key=key, invert_mask=tuple(invert_mask)
)(*qubits)
invert_mask: Tuple[bool, ...] = ()
if isinstance(invert_mask_, list):
invert_mask = tuple(bool(x) for x in invert_mask_)
if isinstance(invert_mask, tuple) and isinstance(key, str):
op = cirq.MeasurementGate(num_qubits=len(qubits), key=key, invert_mask=invert_mask)(
*qubits
)
else:
raise ValueError(f'Incorrect types for measurement gate {invert_mask} {key}')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -660,3 +660,11 @@ def test_no_constants_table():

with pytest.raises(ValueError, match='Proto has references to constants table'):
serializer._deserialize_gate_op(op)


def test_deserialize() -> None:
NoureldinYosri marked this conversation as resolved.
Show resolved Hide resolved
q = cirq.NamedQubit('q')
circuit = cirq.Circuit(cirq.X(q) ** 0.5, cirq.measure(q))
msg = cg.CIRCUIT_SERIALIZER.serialize(circuit)

assert cg.CIRCUIT_SERIALIZER.deserialize(msg) == circuit
Loading