From df78b1c459cca906f9ac387e1051253bd2d6b64d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elena=20Pe=C3=B1a=20Tapia?= Date: Fri, 20 Sep 2024 16:35:44 +0200 Subject: [PATCH] Fix test.python.circuit.test_controlled_gate.TestControlledGate.test_mc_failure_without_annotation: * Sloppy attept at adding control flow instructions to target gate name mapping (100% could be more efficient) * Suport target with basis_gates but qubits=None in Optimizq1qGatesDecomposition --- .../optimization/optimize_1q_decomposition.py | 3 ++ .../generate_preset_pass_manager.py | 29 +++++++++++++++++-- test/python/circuit/test_scheduled_circuit.py | 2 +- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/qiskit/transpiler/passes/optimization/optimize_1q_decomposition.py b/qiskit/transpiler/passes/optimization/optimize_1q_decomposition.py index eadff1bfe867..1820624bc550 100644 --- a/qiskit/transpiler/passes/optimization/optimize_1q_decomposition.py +++ b/qiskit/transpiler/passes/optimization/optimize_1q_decomposition.py @@ -94,6 +94,9 @@ def __init__(self, basis=None, target=None): elif target is None: self._global_decomposers = _possible_decomposers(None) self._basis_gates = None + else: + self._global_decomposers = _possible_decomposers(set(target.operation_names)) + self._basis_gates = None self.error_map = self._build_error_map() diff --git a/qiskit/transpiler/preset_passmanagers/generate_preset_pass_manager.py b/qiskit/transpiler/preset_passmanagers/generate_preset_pass_manager.py index f3825fe43410..a2f713c2aa56 100644 --- a/qiskit/transpiler/preset_passmanagers/generate_preset_pass_manager.py +++ b/qiskit/transpiler/preset_passmanagers/generate_preset_pass_manager.py @@ -18,6 +18,12 @@ import warnings from qiskit.circuit.controlflow import CONTROL_FLOW_OP_NAMES +from qiskit.circuit.controlflow import ( + IfElseOp, + WhileLoopOp, + ForLoopOp, + SwitchCaseOp, +) from qiskit.circuit.library.standard_gates import get_standard_gate_name_mapping from qiskit.circuit.quantumregister import Qubit from qiskit.providers.backend import Backend @@ -35,6 +41,14 @@ from .level2 import level_2_pass_manager from .level3 import level_3_pass_manager +CONTROL_FLOW_MAPPING = { + "if_else": IfElseOp, + "while_loop": WhileLoopOp, + "for_loop": ForLoopOp, + "switch_case": SwitchCaseOp, +} + + def generate_preset_pass_manager( optimization_level=2, backend=None, @@ -277,7 +291,12 @@ def generate_preset_pass_manager( stacklevel=2, ) - if target is None and backend is None and scheduling_method is not None and coupling_map is None: + if ( + target is None + and backend is None + and scheduling_method is not None + and coupling_map is None + ): warnings.warn( "A coupling map is required for the scheduling stage. " "It must be defined through the `target` or " @@ -286,7 +305,6 @@ def generate_preset_pass_manager( stacklevel=2, ) - # Check if a custom inst_map was specified before overwriting inst_map _given_inst_map = bool(inst_map) # If there are no loose constraints => use backend target if available @@ -330,7 +348,11 @@ def generate_preset_pass_manager( # try: target = Target.from_configuration( basis_gates=basis_gates, - num_qubits=backend.num_qubits if backend is not None else coupling_map.size() if coupling_map is not None else None, + num_qubits=( + backend.num_qubits + if backend is not None + else coupling_map.size() if coupling_map is not None else None + ), coupling_map=coupling_map, # If the instruction map has custom gates, do not give as config, the information # will be added to the target with update_from_instruction_schedule_map @@ -438,6 +460,7 @@ def _parse_basis_gates(basis_gates, backend, inst_map, skip_target): for name in default_gates: if name not in instructions: instructions.add(name) + name_mapping.update({name: CONTROL_FLOW_MAPPING[name] for name in CONTROL_FLOW_OP_NAMES}) except TypeError: instructions = None diff --git a/test/python/circuit/test_scheduled_circuit.py b/test/python/circuit/test_scheduled_circuit.py index f2458731f271..e314de79050f 100644 --- a/test/python/circuit/test_scheduled_circuit.py +++ b/test/python/circuit/test_scheduled_circuit.py @@ -329,7 +329,7 @@ def test_per_qubit_durations(self): sc = transpile( qc, scheduling_method="alap", - coupling_map=[[0,1]], + coupling_map=[[0, 1]], basis_gates=["h", "cx"], instruction_durations=[("h", None, 200), ("cx", [0, 1], 700)], )