Skip to content

Commit

Permalink
Added a public routing swap tag (quantumlib#5844)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ammareltigani authored Aug 29, 2022
1 parent 901a33d commit e364769
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions cirq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@
reset,
reset_each,
ResetChannel,
RoutingSwapTag,
riswap,
Rx,
Ry,
Expand Down
1 change: 1 addition & 0 deletions cirq/json_resolver_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ def _symmetricalqidpair(qids):
'ResetChannel': cirq.ResetChannel,
'Result': cirq.ResultDict, # Keep support for Cirq < 0.14.
'ResultDict': cirq.ResultDict,
'RoutingSwapTag': cirq.RoutingSwapTag,
'Rx': cirq.Rx,
'Ry': cirq.Ry,
'Rz': cirq.Rz,
Expand Down
2 changes: 1 addition & 1 deletion cirq/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
SwapPowGate,
)

from cirq.ops.tags import VirtualTag
from cirq.ops.tags import RoutingSwapTag, VirtualTag

from cirq.ops.three_qubit_gates import (
CCNOT,
Expand Down
23 changes: 23 additions & 0 deletions cirq/ops/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,26 @@ def _json_dict_(self) -> Dict[str, str]:

def __hash__(self):
return hash(VirtualTag)


class RoutingSwapTag:
"""A 'cirq.TaggedOperation' tag indicated that the operation is an inserted SWAP.
A RoutingSwapTag is meant to be used to distinguish SWAP operations that are inserted during
a routing procedure and SWAP operations that are part of the original circuit before routing.
"""

def __eq__(self, other):
return isinstance(other, RoutingSwapTag)

def __str__(self) -> str:
return '<r>'

def __repr__(self) -> str:
return 'cirq.RoutingSwapTag()'

def _json_dict_(self) -> Dict[str, str]:
return {}

def __hash__(self):
return hash(RoutingSwapTag)
12 changes: 12 additions & 0 deletions cirq/ops/tags_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,15 @@ def test_virtual_tag():

cirq.testing.assert_equivalent_repr(tag1)
cirq.testing.assert_equivalent_repr(tag2)


def test_routing_swap_tag():
tag1 = cirq.ops.RoutingSwapTag()
tag2 = cirq.ops.RoutingSwapTag()

assert tag1 == tag2
assert str(tag1) == str(tag2) == '<r>'
assert hash(tag1) == hash(tag2)

cirq.testing.assert_equivalent_repr(tag1)
cirq.testing.assert_equivalent_repr(tag2)
3 changes: 3 additions & 0 deletions cirq/protocols/json_test_data/RoutingSwapTag.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"cirq_type": "RoutingSwapTag"
}
1 change: 1 addition & 0 deletions cirq/protocols/json_test_data/RoutingSwapTag.repr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cirq.RoutingSwapTag()

0 comments on commit e364769

Please sign in to comment.