Skip to content

Commit c9c0581

Browse files
ammareltiganirht
authored andcommitted
Added a public routing swap tag (quantumlib#5844)
A RoutingSwapTag is a tag optionally applied on swaps that were inserted as part of a routed procedure. This serves to differentiate between swaps native to the original circuit and swaps added during routing.
1 parent 95d3bc2 commit c9c0581

File tree

7 files changed

+42
-1
lines changed

7 files changed

+42
-1
lines changed

Diff for: cirq-core/cirq/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@
287287
reset,
288288
reset_each,
289289
ResetChannel,
290+
RoutingSwapTag,
290291
riswap,
291292
Rx,
292293
Ry,

Diff for: cirq-core/cirq/json_resolver_cache.py

+1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ def _symmetricalqidpair(qids):
204204
'ResetChannel': cirq.ResetChannel,
205205
'Result': cirq.ResultDict, # Keep support for Cirq < 0.14.
206206
'ResultDict': cirq.ResultDict,
207+
'RoutingSwapTag': cirq.RoutingSwapTag,
207208
'Rx': cirq.Rx,
208209
'Ry': cirq.Ry,
209210
'Rz': cirq.Rz,

Diff for: cirq-core/cirq/ops/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@
188188
SwapPowGate,
189189
)
190190

191-
from cirq.ops.tags import VirtualTag
191+
from cirq.ops.tags import RoutingSwapTag, VirtualTag
192192

193193
from cirq.ops.three_qubit_gates import (
194194
CCNOT,

Diff for: cirq-core/cirq/ops/tags.py

+23
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,26 @@ def _json_dict_(self) -> Dict[str, str]:
4343

4444
def __hash__(self):
4545
return hash(VirtualTag)
46+
47+
48+
class RoutingSwapTag:
49+
"""A 'cirq.TaggedOperation' tag indicated that the operation is an inserted SWAP.
50+
51+
A RoutingSwapTag is meant to be used to distinguish SWAP operations that are inserted during
52+
a routing procedure and SWAP operations that are part of the original circuit before routing.
53+
"""
54+
55+
def __eq__(self, other):
56+
return isinstance(other, RoutingSwapTag)
57+
58+
def __str__(self) -> str:
59+
return '<r>'
60+
61+
def __repr__(self) -> str:
62+
return 'cirq.RoutingSwapTag()'
63+
64+
def _json_dict_(self) -> Dict[str, str]:
65+
return {}
66+
67+
def __hash__(self):
68+
return hash(RoutingSwapTag)

Diff for: cirq-core/cirq/ops/tags_test.py

+12
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,15 @@ def test_virtual_tag():
2424

2525
cirq.testing.assert_equivalent_repr(tag1)
2626
cirq.testing.assert_equivalent_repr(tag2)
27+
28+
29+
def test_routing_swap_tag():
30+
tag1 = cirq.ops.RoutingSwapTag()
31+
tag2 = cirq.ops.RoutingSwapTag()
32+
33+
assert tag1 == tag2
34+
assert str(tag1) == str(tag2) == '<r>'
35+
assert hash(tag1) == hash(tag2)
36+
37+
cirq.testing.assert_equivalent_repr(tag1)
38+
cirq.testing.assert_equivalent_repr(tag2)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"cirq_type": "RoutingSwapTag"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cirq.RoutingSwapTag()

0 commit comments

Comments
 (0)