-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add ability to add custom tag (de)serializers #7169
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
Add ability to add custom tag (de)serializers #7169
Conversation
dstrain115
commented
Mar 20, 2025
- This adds custom tag serializers for serializing tags.
- This allows greater flexibility and a much easier way to specify a serializer to serialize and deserialize internal tags.
- This adds custom tag serializers for serializing tags. - This allows greater flexibility and a much easier way to specify a serializer to serialize and deserialize internal tags.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #7169 +/- ##
==========================================
- Coverage 98.13% 98.13% -0.01%
==========================================
Files 1093 1095 +2
Lines 95579 95649 +70
==========================================
+ Hits 93797 93864 +67
- Misses 1782 1785 +3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with some small tweaks, please see inline.
| self.discount = discount | ||
|
|
||
| def __eq__(self, other): | ||
| if isinstance(other, DiscountTag) and self.discount == other.discount: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit - __eq__ should return NotImplemented for unhandled types (I realize this is a test class). Consider using attrs instead which define __eq__ and __hash__ with less code
@attrs.frozen
class DiscountTag:
discount: float
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea. Done.
| ), | ||
| constants=[v2.program_pb2.Constant(qubit=v2.program_pb2.Qubit(id='1_1'))], | ||
| ) | ||
| expected_circuit_no_tag = cirq.Circuit( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit - the _no_tag suffix is a bit confusing, because there is a tagged operation.
How about plain expected_circuit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed.
| constant = v2.program_pb2.Constant() | ||
| tag_index = len(constants) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use constant in both if-branches. It is also safer to get tag_index after the tag_serializer.to_proto() call in case it changes the constants list:
diff --git a/cirq-google/cirq_google/serialization/circuit_serializer.py b/cirq-google/cirq_google/serialization/circuit_serializer.py
index b76b23f7..ae940471 100644
--- a/cirq-google/cirq_google/serialization/circuit_serializer.py
+++ b/cirq-google/cirq_google/serialization/circuit_serializer.py
@@ -331,17 +331,17 @@ class CircuitSerializer(serializer.Serializer):
tag.to_proto(msg=msg.tags.add())
if (tag_index := raw_constants.get(tag, None)) is None:
constant = v2.program_pb2.Constant()
- tag_index = len(constants)
if self.tag_serializer and self.tag_serializer.can_serialize_tag(tag):
- tag_proto = self.tag_serializer.to_proto(
- tag, constants=constants, raw_constants=raw_constants
+ self.tag_serializer.to_proto(
+ tag,
+ msg=constant.tag_value,
+ constants=constants,
+ raw_constants=raw_constants,
)
- constants.append(v2.program_pb2.Constant(tag_value=tag_proto))
- if raw_constants is not None:
- raw_constants[tag] = tag_index
- msg.tag_indices.append(tag_index)
elif getattr(tag, 'to_proto', None) is not None:
tag.to_proto(constant.tag_value) # type: ignore
+ if constant.WhichOneof('const_value'):
+ tag_index = len(constants)
constants.append(constant)
if raw_constants is not None:
raw_constants[tag] = tag_index
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed.
| msg: Optional[v2.program_pb2.CircuitOperation] = None, | ||
| *, | ||
| constants: List[v2.program_pb2.Constant], | ||
| raw_constants: Dict[Any, int], | ||
| ) -> v2.program_pb2.CircuitOperation: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo in annotation -
| msg: Optional[v2.program_pb2.CircuitOperation] = None, | |
| *, | |
| constants: List[v2.program_pb2.Constant], | |
| raw_constants: Dict[Any, int], | |
| ) -> v2.program_pb2.CircuitOperation: | |
| msg: Optional[v2.program_pb2.Tag] = None, | |
| *, | |
| constants: List[v2.program_pb2.Constant], | |
| raw_constants: Dict[Any, int], | |
| ) -> v2.program_pb2.Tag: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the quick review!
| msg: Optional[v2.program_pb2.CircuitOperation] = None, | ||
| *, | ||
| constants: List[v2.program_pb2.Constant], | ||
| raw_constants: Dict[Any, int], | ||
| ) -> v2.program_pb2.CircuitOperation: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
| ), | ||
| constants=[v2.program_pb2.Constant(qubit=v2.program_pb2.Qubit(id='1_1'))], | ||
| ) | ||
| expected_circuit_no_tag = cirq.Circuit( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed.
| constant = v2.program_pb2.Constant() | ||
| tag_index = len(constants) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed.
| self.discount = discount | ||
|
|
||
| def __eq__(self, other): | ||
| if isinstance(other, DiscountTag) and self.discount == other.discount: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea. Done.
| if constant.WhichOneof('const_value'): | ||
| constants.append(constant) | ||
| if raw_constants is not None: | ||
| raw_constants[tag] = tag_index |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tag_index is unassigned here. nice catch by mypy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM modulo the tag_index typo
* Add ability to add custom tag (de)serializers - This adds custom tag serializers for serializing tags. - This allows greater flexibility and a much easier way to specify a serializer to serialize and deserialize internal tags. * Fix types. * Fix coverage * Address comments. * Fix unassigned variable.