-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Allow ability to plug in custom (de)serializers for cirq_google protos #7059
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
Changes from 2 commits
de977d8
e04cbfc
0fbf150
cb3043a
68399d3
1122030
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,7 @@ | |
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from typing import Any, List | ||
| from typing import Any, Callable, List | ||
|
|
||
| import abc | ||
| import sympy | ||
|
|
@@ -31,13 +31,12 @@ class OpDeserializer(abc.ABC): | |
|
|
||
| @property | ||
| @abc.abstractmethod | ||
| def serialized_id(self) -> str: | ||
| """Returns the string identifier for the accepted serialized objects. | ||
| def can_deserialize_predicate(self) -> Callable[[Any], bool]: | ||
| """The method used to determine if this can deserialize a proto.""" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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 (Same for
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea. I don't remember why this was done originally, but it is no longer needed. |
||
|
|
||
| This ID denotes the serialization format this deserializer consumes. For | ||
| example, one of the common deserializers converts objects with the id | ||
| 'xy' into PhasedXPowGates. | ||
| """ | ||
| def can_deserialize_proto(self, proto) -> bool: | ||
| """Whether the given operation can be serialized by this serializer.""" | ||
| return self.can_deserialize_predicate(proto) | ||
|
|
||
| @abc.abstractmethod | ||
| def from_proto( | ||
|
|
@@ -67,8 +66,8 @@ class CircuitOpDeserializer(OpDeserializer): | |
| """Describes how to serialize CircuitOperations.""" | ||
|
|
||
| @property | ||
| def serialized_id(self): | ||
| return 'circuit' | ||
| def can_deserialize_predicate(self): | ||
| return lambda proto: isinstance(proto, v2.program_pb2.CircuitOperation) # pragma: nocover | ||
|
|
||
| def from_proto( | ||
| self, | ||
|
|
||
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.
Please update class docstring which refers to
serializer_id.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. Also warnings suppressed.