Skip to content

Commit

Permalink
Use singleton SwapGate in Sabre reconstruction (#10865)
Browse files Browse the repository at this point in the history
Since `SwapGate` is now a singleton instance in most cases, we can
directly reuse the same instance during Sabre reconstruction rather than
wasting cycles re-retrieving the singleton instance.
  • Loading branch information
jakelishman authored Sep 20, 2023
1 parent 22b94a1 commit 82aaef5
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion qiskit/transpiler/passes/routing/sabre_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,10 @@ def _apply_sabre_result(
:class:`.DAGCircuit` that represents the same thing.
"""

# The swap gate is a singleton instance, so we don't need to waste time reconstructing it each
# time we need to use it.
swap_singleton = SwapGate()

def empty_dag(block):
empty = DAGCircuit()
empty.add_qubits(out_dag.qubits)
Expand All @@ -362,7 +366,7 @@ def apply_swaps(dest_dag, swaps, layout):
for a, b in swaps:
qubits = (physical_qubits[a], physical_qubits[b])
layout.swap_physical(a, b)
dest_dag.apply_operation_back(SwapGate(), qubits, (), check=False)
dest_dag.apply_operation_back(swap_singleton, qubits, (), check=False)

def recurse(dest_dag, source_dag, result, root_logical_map, layout):
"""The main recursive worker. Mutates ``dest_dag`` and ``layout`` and returns them.
Expand Down

0 comments on commit 82aaef5

Please sign in to comment.