From 62707208e78fc8b717ec14792c5b62a2698f3c80 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 c62a468cf6f3..57453f6a22f6 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) @@ -368,7 +372,7 @@ def apply_swaps(dest_dag, swaps, layout): physical_qubits[layout.virtual_to_physical(b)], ) layout.swap_virtual(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.