From a506ba9d3c6ae3f4130d8853e1638b5a4c935662 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Mon, 2 Nov 2020 10:59:12 -0500 Subject: [PATCH] Avoid deepcopy in sabre_swap (#5316) This commit changes the deepcopy usage in the sabreswap pass to a shallow copy. In sabre_swap the deepcopy was run on the DAGCircuit node when remapping it, however this was not necessary because it's not actually used where shared references matter. The output from the remapper is not used directly and instead a copy of the DAGNode object is just used as a container for the required args in apply_operation_back() where a new DAGNode is always created from the op, qargs, etc. The remapping just replaces the qargs parameter with the remapped one. So this commit changes the deepcopy to a shallow copy which won't have the performance overhead. Fixes #5197 Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- qiskit/transpiler/passes/routing/sabre_swap.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/qiskit/transpiler/passes/routing/sabre_swap.py b/qiskit/transpiler/passes/routing/sabre_swap.py index 59954b86bf88..74feece5c84e 100644 --- a/qiskit/transpiler/passes/routing/sabre_swap.py +++ b/qiskit/transpiler/passes/routing/sabre_swap.py @@ -13,7 +13,7 @@ """Routing via SWAP insertion using the SABRE method from Li et al.""" import logging -from copy import deepcopy +from copy import copy from itertools import cycle import numpy as np @@ -336,7 +336,7 @@ def _score_heuristic(self, heuristic, front_layer, extended_set, layout, swap_qu def _transform_gate_for_layout(op_node, layout): """Return node implementing a virtual op on given layout.""" - mapped_op_node = deepcopy(op_node) + mapped_op_node = copy(op_node) device_qreg = op_node.qargs[0].register premap_qargs = op_node.qargs