Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 13 additions & 5 deletions cirq-google/cirq_google/ops/internal_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
from cirq import value
from cirq_google.api.v2 import program_pb2

# from cirq_google.serialization import arg_func_langs


@value.value_equality
class InternalTag:
Expand Down Expand Up @@ -62,18 +60,28 @@ def _value_equality_values_(self):
return (self.name, self.package, tag_args_eq_values)

def to_proto(self, msg: Optional[program_pb2.Tag] = None) -> program_pb2.Tag:
# To avoid circular import
from cirq_google.serialization import arg_func_langs

if msg is None:
msg = program_pb2.Tag()
msg.internal_tag.tag_name = self.name
msg.internal_tag.tag_package = self.package
for k, v in self.tag_args.items():
arg_func_langs.arg_to_proto(v, out=msg.internal_tag.tag_args[k])
return msg

@staticmethod
def from_proto(msg: program_pb2.Tag) -> 'InternalTag':
# To avoid circular import
from cirq_google.serialization import arg_func_langs

kw_dict = {}
for k, v in msg.internal_tag.tag_args.items():
kw_dict[k] = arg_func_langs.arg_from_proto(v, arg_function_language='exp')
Copy link
Copy Markdown
Collaborator

@pavoljuhas pavoljuhas Feb 21, 2025

Choose a reason for hiding this comment

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

Should we set arg_function_language also in the to_proto method above at line 71?

Also, let us do the msg.WhichOneof check (L83) first before building kw_dict.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done!


if msg.WhichOneof("tag") != "internal_tag":
raise ValueError(f"Message is not a InternalTag, {msg}")
return InternalTag(
name=msg.internal_tag.tag_name,
package=msg.internal_tag.tag_package,
**msg.internal_tag.tag_args,
name=msg.internal_tag.tag_name, package=msg.internal_tag.tag_package, **kw_dict
)
2 changes: 1 addition & 1 deletion cirq-google/cirq_google/ops/internal_tag_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_internal_tag_with_hashable_args_is_hashable():


def test_proto():
tag = cirq_google.InternalTag(name="TagWithNoParams", package='test')
tag = cirq_google.InternalTag(name="TagWithNoParams", package='test', param1=2.5)
msg = tag.to_proto()
assert tag == cirq_google.InternalTag.from_proto(msg)

Expand Down