Skip to content

Commit 17c4e95

Browse files
authored
Add CompressDurationTag (#7543)
- This tag will signal the compiler to remove operations of zero duration. - Normally, these operations would have the same duration as their typical gate (i.e. X**0 waits for the length of an X gate). - Also adds JSON and cirq_google serialization for this tag.
1 parent 1ed3fdf commit 17c4e95

12 files changed

+245
-93
lines changed

cirq-google/cirq_google/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
AnalogDetuneCouplerOnly as AnalogDetuneCouplerOnly,
5757
AnalogDetuneQubit as AnalogDetuneQubit,
5858
CalibrationTag as CalibrationTag,
59+
CompressDurationTag as CompressDurationTag,
5960
Coupler as Coupler,
6061
FSimGateFamily as FSimGateFamily,
6162
FSimViaModelTag as FSimViaModelTag,

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,9 @@ message Tag {
347347

348348
// Calibration Tag
349349
CalibrationTag calibration_tag = 9;
350+
351+
// Compress duration to zero, if possible.
352+
CompressDurationTag compress_duration = 10;
350353

351354
// Catch-all for all gates that do not fit into the
352355
// above tags.
@@ -392,6 +395,10 @@ message CalibrationTag {
392395
string token = 1;
393396
}
394397

398+
// Tag to specify that hardware should compress the duration to zero
399+
// if possible. For instance, in X**0 or PhasedXZGate with zero angles.
400+
message CompressDurationTag {}
401+
395402
// Tag to represent any internal tags or tags not yet
396403
// implemented in the proto.
397404
message InternalTag {

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

Lines changed: 92 additions & 90 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: 23 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cirq-google/cirq_google/json_resolver_cache.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def _old_xmon(*args, **kwargs):
4949
'Calibration': cirq_google.Calibration,
5050
'CalibrationTag': cirq_google.CalibrationTag,
5151
'CalibrationLayer': cirq_google.CalibrationLayer,
52+
'CompressDurationTag': cirq_google.CompressDurationTag,
5253
'CouplerPulse': cirq_google.experimental.CouplerPulse,
5354
'Coupler': cirq_google.Coupler,
5455
'GoogleNoiseProperties': cirq_google.GoogleNoiseProperties,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"cirq_type": "CompressDurationTag"
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cirq_google.CompressDurationTag()

cirq-google/cirq_google/ops/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
from cirq_google.ops.calibration_tag import CalibrationTag as CalibrationTag
2222

23+
from cirq_google.ops.compress_duration_tag import CompressDurationTag as CompressDurationTag
24+
2325
from cirq_google.ops.coupler import Coupler as Coupler
2426

2527
from cirq_google.ops.fsim_gate_family import FSimGateFamily as FSimGateFamily
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright 2025 The Cirq Developers
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""A class that can be used to denote a physical Z gate."""
16+
17+
from __future__ import annotations
18+
19+
from typing import Any
20+
21+
import cirq
22+
from cirq_google.api.v2 import program_pb2
23+
24+
25+
class CompressDurationTag:
26+
"""Class to direct hardware to compress operations of zero duration.
27+
28+
By default, parameters of gates that could lead to no-ops do not
29+
change the original duration of the gate. For instance, X**0
30+
will cause a wait of the same duration as an X gate. (This is so
31+
that parameter sweeps do not inadvertently change the duration of
32+
the circuit, leading to artifacts in data).
33+
34+
When applied to an operation, this will cause the operation to
35+
compress to zero duration if possible. Currently, this will affect
36+
gates with angles of zero.
37+
38+
This can also affect PhasedXZGates to turn them into virtual Z gates
39+
if the resulting gate has a Z phase but no X component.
40+
"""
41+
42+
def __str__(self) -> str:
43+
return 'CompressDurationTag()'
44+
45+
def __repr__(self) -> str:
46+
return 'cirq_google.CompressDurationTag()'
47+
48+
def _json_dict_(self) -> dict[str, Any]:
49+
return cirq.obj_to_dict_helper(self, [])
50+
51+
def __eq__(self, other) -> bool:
52+
return isinstance(other, CompressDurationTag)
53+
54+
def __hash__(self) -> int:
55+
return 456789
56+
57+
def to_proto(self, msg: program_pb2.Tag | None = None) -> program_pb2.Tag:
58+
if msg is None:
59+
msg = program_pb2.Tag()
60+
msg.compress_duration.SetInParent()
61+
return msg
62+
63+
@staticmethod
64+
def from_proto(msg: program_pb2.Tag) -> CompressDurationTag:
65+
return CompressDurationTag()
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2025 The Cirq Developers
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import annotations
16+
17+
import cirq
18+
import cirq_google
19+
20+
21+
def test_equality():
22+
assert cirq_google.CompressDurationTag() == cirq_google.CompressDurationTag()
23+
assert hash(cirq_google.CompressDurationTag()) == hash(cirq_google.CompressDurationTag())
24+
25+
26+
def test_syc_str_repr():
27+
assert str(cirq_google.CompressDurationTag()) == 'CompressDurationTag()'
28+
assert repr(cirq_google.CompressDurationTag()) == 'cirq_google.CompressDurationTag()'
29+
cirq.testing.assert_equivalent_repr(
30+
cirq_google.CompressDurationTag(), setup_code=('import cirq\nimport cirq_google\n')
31+
)
32+
33+
34+
def test_proto():
35+
tag = cirq_google.CompressDurationTag()
36+
msg = tag.to_proto()
37+
assert tag == cirq_google.CompressDurationTag.from_proto(msg)

0 commit comments

Comments
 (0)