From 32a8842f62d8ba13669e16aa0f239d1bb8531762 Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Wed, 20 Sep 2023 13:32:39 +0100 Subject: [PATCH] Use singleton `SwapGate` in Sabre reconstruction 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. --- qiskit/transpiler/passes/routing/sabre_swap.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/qiskit/transpiler/passes/routing/sabre_swap.py b/qiskit/transpiler/passes/routing/sabre_swap.py index 253eb0ad6aa4..b78bd539b99b 100644 --- a/qiskit/transpiler/passes/routing/sabre_swap.py +++ b/qiskit/transpiler/passes/routing/sabre_swap.py @@ -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) @@ -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.