Skip to content

Commit 731f1bf

Browse files
authored
Serialize tags on moments in cirq_google protos (#7493)
- Tags were added to Moments this month. - Adds the ability to serialize tags on Moments using cirq_google protos.
1 parent 3d4f6f1 commit 731f1bf

File tree

5 files changed

+165
-133
lines changed

5 files changed

+165
-133
lines changed

cirq-google/cirq_google/api/v2/program.proto

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ message Moment {
109109

110110
// Deprecated field id, do not use.
111111
reserved 3;
112+
113+
// Indices in the constant table for tags associated with the circuit
114+
repeated int32 tag_indices = 5;
112115
}
113116

114117
// The language in which the program is expressed.

cirq-google/cirq_google/api/v2/program_pb2.py

Lines changed: 130 additions & 130 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cirq-google/cirq_google/api/v2/program_pb2.pyi

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cirq-google/cirq_google/serialization/circuit_serializer.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ def _serialize_circuit(
168168
raw_constants[op] = op_index
169169
moment_proto.operation_indices.append(op_index)
170170

171+
for tag in moment.tags:
172+
self._serialize_tag(
173+
tag, moment_proto, constants=constants, raw_constants=raw_constants
174+
)
175+
171176
# Add this moment to the constants table
172177
constants.append(v2.program_pb2.Constant(moment_value=moment_proto))
173178
moment_index = len(constants) - 1
@@ -314,7 +319,7 @@ def _serialize_gate_op(
314319
def _serialize_tag(
315320
self,
316321
tag: Hashable,
317-
msg: v2.program_pb2.Operation | v2.program_pb2.Circuit,
322+
msg: v2.program_pb2.Operation | v2.program_pb2.Moment | v2.program_pb2.Circuit,
318323
*,
319324
constants: list[v2.program_pb2.Constant],
320325
raw_constants: dict[Any, int],
@@ -502,6 +507,7 @@ def _deserialize_moment(
502507
deserialized_constants: list[Any],
503508
) -> cirq.Moment:
504509
moment_ops = []
510+
tags = []
505511
for op in moment_proto.operations:
506512
if self.op_deserializer and self.op_deserializer.can_deserialize_proto(op):
507513
gate_op = self.op_deserializer.from_proto(
@@ -524,7 +530,10 @@ def _deserialize_moment(
524530
)
525531
for operation_index in moment_proto.operation_indices:
526532
moment_ops.append(deserialized_constants[operation_index])
527-
moment = cirq.Moment(moment_ops)
533+
534+
for tag_index in moment_proto.tag_indices:
535+
tags.append(deserialized_constants[tag_index])
536+
moment = cirq.Moment(moment_ops, tags=tuple(tags))
528537
return moment
529538

530539
def _deserialize_gate_op(

cirq-google/cirq_google/serialization/circuit_serializer_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,20 @@ def test_custom_tag_serializer_with_tags_outside_constants():
12041204
assert serializer.deserialize(circuit_proto) == expected_circuit
12051205

12061206

1207+
def test_moments_with_tags():
1208+
serializer = cg.CircuitSerializer(
1209+
tag_serializer=DiscountTagSerializer(), tag_deserializer=DiscountTagDeserializer()
1210+
)
1211+
original_circuit = cirq.Circuit(
1212+
cirq.Moment(cirq.X(cirq.GridQubit(1, 1))).with_tags(DiscountTag(0.50)),
1213+
cirq.Moment(cirq.Z(cirq.GridQubit(2, 2))).with_tags(cg.CalibrationTag("abc")),
1214+
)
1215+
deserialized_circuit = serializer.deserialize(serializer.serialize(original_circuit))
1216+
assert original_circuit == deserialized_circuit
1217+
assert deserialized_circuit[0].tags == (DiscountTag(0.50),)
1218+
assert deserialized_circuit[1].tags == (cg.CalibrationTag("abc"),)
1219+
1220+
12071221
def test_reset_gate_with_improper_argument():
12081222
serializer = cg.CircuitSerializer()
12091223

0 commit comments

Comments
 (0)