Allow ability to plug in custom (de)serializers for cirq_google protos#7059
Allow ability to plug in custom (de)serializers for cirq_google protos#7059dstrain115 merged 6 commits intoquantumlib:mainfrom
Conversation
dstrain115
commented
Feb 11, 2025
- This will allow users to plug in custom serializers and deserializers, which can parse gates before falling back to the default.
- This enables internal libraries to parse and deserialize non-public gates, tags, and operations.
- This will allow users to plug in custom serializers and deserializers, which can parse gates before falling back to the default. - This enables internal libraries to parse and deserialize non-public gates, tags, and operations.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #7059 +/- ##
=======================================
Coverage 98.18% 98.18%
=======================================
Files 1089 1089
Lines 95208 95237 +29
=======================================
+ Hits 93478 93507 +29
Misses 1730 1730 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Please see comment on temporary predicate lambda objects. Otherwise LGTM.
On a bit unrelated note - can you please apply the patch below to suppress intentional warnings in pytest cirq-google/cirq_google/serialization/circuit_serializer_test.py.
Note it flips the test names so they are in sync with the warnings produced.
diff --git a/cirq-google/cirq_google/serialization/circuit_serializer_test.py b/cirq-google/cirq_google/serialization/circuit_serializer_test.py
index a09de744..a1d1cfc3 100644
--- a/cirq-google/cirq_google/serialization/circuit_serializer_test.py
+++ b/cirq-google/cirq_google/serialization/circuit_serializer_test.py
@@ -859,5 +859,6 @@ def test_circuit_with_tag(tag):
-def test_unknown_tag_is_ignored():
+@pytest.mark.filterwarnings('ignore:Unrecognized Tag .*DingDongTag')
+def test_unrecognized_tag_is_ignored():
class DingDongTag:
pass
@@ -869,5 +870,6 @@ def test_unknown_tag_is_ignored():
-def test_unrecognized_tag_is_ignored():
+@pytest.mark.filterwarnings('ignore:Unknown tag msg=phase_match')
+def test_unknown_tag_is_ignored():
op_tag = v2.program_pb2.Operation()
op_tag.xpowgate.exponent.float_value = 1.0
| def can_deserialize_predicate(self) -> Callable[[Any], bool]: | ||
| """The method used to determine if this can deserialize a proto.""" |
There was a problem hiding this comment.
Can this be a regular method returning a bool instead? Or a staticmethod if you want to have it decoupled from OpDeserializer object?
As it is, each call of CircuitOpDeserializer.can_deserialize_proto creates lambda object, calls it once, and discards it after the call.
(Same for OpSerializer.can_serialize_predicate.)
There was a problem hiding this comment.
Good idea. I don't remember why this was done originally, but it is no longer needed.
|
|
||
| @property | ||
| @abc.abstractmethod | ||
| def serialized_id(self) -> str: |
There was a problem hiding this comment.
Please update class docstring which refers to serializer_id.
There was a problem hiding this comment.
Done. Also warnings suppressed.
pavoljuhas
left a comment
There was a problem hiding this comment.
LGTM, but the warning filters need to be flipped so that
check/pytest -n0 cirq-google/cirq_google/serialization/circuit_serializer_test.py
has no warnings to report.
Currently the test_unknown_tag_is_ignored test gives warning about unrecognized tag
and test_unrecognized_tag_is_ignored about unknown tag. (My previous patch had test names flipped for test-name / warning consistency).
diff --git a/cirq-google/cirq_google/serialization/circuit_serializer_test.py b/cirq-google/cirq_google/serialization/circuit_serializer_test.py
index cd067ffb..d2c06e78 100644
--- a/cirq-google/cirq_google/serialization/circuit_serializer_test.py
+++ b/cirq-google/cirq_google/serialization/circuit_serializer_test.py
@@ -860,3 +860,3 @@ def test_circuit_with_tag(tag):
-@pytest.mark.filterwarnings('ignore:Unknown tag msg=phase_match')
+@pytest.mark.filterwarnings('ignore:Unrecognized Tag .*DingDongTag')
def test_unknown_tag_is_ignored():
@@ -871,3 +871,3 @@ def test_unknown_tag_is_ignored():
-@pytest.mark.filterwarnings('ignore:Unrecognized Tag .*DingDongTag')
+@pytest.mark.filterwarnings('ignore:Unknown tag msg=phase_match')
def test_unrecognized_tag_is_ignored():
…5/Cirq-1 into add_custom_serializers
quantumlib#7059) * Allow ability to plug in custom (de)serializers for cirq_google protos - This will allow users to plug in custom serializers and deserializers, which can parse gates before falling back to the default. - This enables internal libraries to parse and deserialize non-public gates, tags, and operations. * Fix coverage and get rid of unneeded junk. * Address comments. * Flip warnings.