From 463cefcd720bd97e525d61b98b791094fff6ee38 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Wed, 7 Aug 2024 20:18:43 -0400 Subject: [PATCH] Fix short circuit detection for basis translator (#12899) The BasisTranslator transpiler pass has a check at the very start that is designed to return fast if there is nothing to translate; in other words if the instructions in the circuit are already a subset of instructions supported by the target. This avoid doing a lot of unecessary work to determine this later during the operation of the pass. However, this check was not correctly constructed because of a type mismatch and would only ever get triggered if the input circuit was empty. The source basis is collected as a `Set[Tuple[str, int]]` where each tuple is the name and num of qubits for each operation in the circuit. While the target basis is just a `Set[str]` for the names supported on the target. This mismatch caused the subset check to never return True unless it was empty thereby bypassing the intent of the short circuit path. This commit fixes the logic by constructing a temporary set of just the source names to evaluate whether we should return early or not. (cherry picked from commit fa5510c3af382cb51256af83a43c2009ca775c03) --- qiskit/transpiler/passes/basis/basis_translator.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/qiskit/transpiler/passes/basis/basis_translator.py b/qiskit/transpiler/passes/basis/basis_translator.py index 0d597b89de6a..a5a3936b1d7e 100644 --- a/qiskit/transpiler/passes/basis/basis_translator.py +++ b/qiskit/transpiler/passes/basis/basis_translator.py @@ -162,7 +162,8 @@ def run(self, dag): # If the source basis is a subset of the target basis and we have no circuit # instructions on qargs that have non-global operations there is nothing to # translate and we can exit early. - if source_basis.issubset(target_basis) and not qargs_local_source_basis: + source_basis_names = {x[0] for x in source_basis} + if source_basis_names.issubset(target_basis) and not qargs_local_source_basis: return dag logger.info(