From bfd25d059c4a65cb35c0ee7fb0d8d5af9537818f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elena=20Pe=C3=B1a=20Tapia?= Date: Tue, 14 Jan 2025 15:24:14 +0100 Subject: [PATCH] Fix lint and improve style --- .../passes/synthesis/high_level_synthesis.py | 10 ++-- .../passes/utils/check_gate_direction.py | 3 +- .../transpiler/passes/utils/gate_direction.py | 5 +- .../preset_passmanagers/builtin_plugins.py | 53 +++++++------------ .../transpiler/preset_passmanagers/common.py | 1 - qiskit/transpiler/target.py | 4 +- .../transpiler/test_elide_permutations.py | 1 - 7 files changed, 33 insertions(+), 44 deletions(-) diff --git a/qiskit/transpiler/passes/synthesis/high_level_synthesis.py b/qiskit/transpiler/passes/synthesis/high_level_synthesis.py index 11fd8fcf8260..eaf895728c52 100644 --- a/qiskit/transpiler/passes/synthesis/high_level_synthesis.py +++ b/qiskit/transpiler/passes/synthesis/high_level_synthesis.py @@ -248,13 +248,14 @@ def __init__( self._basis_gates = basis_gates self._min_qubits = min_qubits - # include cases where target exists with no basis gates, or the basis gates - # are an empty list + # When basis gates haven't been provided as an input, and + # there are no target basis gates (None or empty list), + # treat as "top level only" (no HLS). self._top_level_only = (self._basis_gates is None or len(self._basis_gates) == 0) and ( self._target is None or len(self._target.operation_names) == 0 ) - # include path for when target exists but target.num_qubits is None (BasicSimulator) + # Account for when target exists but target.num_qubits is None (BasicSimulator case) if not self._top_level_only and (self._target is None or self._target.num_qubits is None): basic_insts = {"measure", "reset", "barrier", "snapshot", "delay", "store"} self._device_insts = basic_insts | set(self._basis_gates) @@ -846,7 +847,8 @@ def _definitely_skip_node( ) def _instruction_supported(self, name: str, qubits: tuple[int] | None) -> bool: - # include path for when target exists but target.num_qubits is None (BasicSimulator) + # Do not use target's method when there is no target, target.num_qubits is + # None (BasicSimulator), or target.operation_names is an empty list. if ( self._target is None or self._target.num_qubits is None diff --git a/qiskit/transpiler/passes/utils/check_gate_direction.py b/qiskit/transpiler/passes/utils/check_gate_direction.py index a8ba3d39df43..d5f0db821614 100644 --- a/qiskit/transpiler/passes/utils/check_gate_direction.py +++ b/qiskit/transpiler/passes/utils/check_gate_direction.py @@ -45,7 +45,8 @@ def run(self, dag): Args: dag (DAGCircuit): DAG to check. """ - + # Only use "check_gate_direction_target" if a target exists and target.operation_names + # is not empty, else use "check_gate_direction_coupling". if self.target is None: self.property_set["is_direction_mapped"] = check_gate_direction_coupling( dag, set(self.coupling_map.get_edges()) diff --git a/qiskit/transpiler/passes/utils/gate_direction.py b/qiskit/transpiler/passes/utils/gate_direction.py index 1c12ecdb4a5d..195b6599bce9 100644 --- a/qiskit/transpiler/passes/utils/gate_direction.py +++ b/qiskit/transpiler/passes/utils/gate_direction.py @@ -81,10 +81,11 @@ def run(self, dag): TranspilerError: If the circuit cannot be mapped just by flipping the cx nodes. """ + # Only use "fix_gate_direction_target" if a target exists and target.operation_names + # is not empty, else use "fix_gate_direction_coupling". if self.target is None: return fix_gate_direction_coupling(dag, set(self.coupling_map.get_edges())) - # if target.operation_names is an empty list, extract and use the coupling map - if self.target is not None and len(self.target.operation_names) == 0: + elif len(self.target.operation_names) == 0: return fix_gate_direction_coupling( dag, set(self.target.build_coupling_map().get_edges()) ) diff --git a/qiskit/transpiler/preset_passmanagers/builtin_plugins.py b/qiskit/transpiler/preset_passmanagers/builtin_plugins.py index 21a0113542fe..dce66d55546e 100644 --- a/qiskit/transpiler/preset_passmanagers/builtin_plugins.py +++ b/qiskit/transpiler/preset_passmanagers/builtin_plugins.py @@ -598,28 +598,22 @@ def _opt_control(property_set): elif optimization_level == 2: # Steps for optimization level 2 - # If there are no basis gates (None/empty list), don't run - # Optimize1qGatesDecomposition + _opt = [ + RemoveIdentityEquivalent( + approximation_degree=pass_manager_config.approximation_degree, + target=pass_manager_config.target, + ) + ] + # Only run Optimize1qGatesDecomposition if there are basis_gates in the config if ( pass_manager_config.basis_gates is not None and len(pass_manager_config.basis_gates) > 0 ): - _opt = [ - RemoveIdentityEquivalent( - approximation_degree=pass_manager_config.approximation_degree, - target=pass_manager_config.target, - ), + _opt += [ Optimize1qGatesDecomposition( basis=pass_manager_config.basis_gates, target=pass_manager_config.target ), ] - else: - _opt = [ - RemoveIdentityEquivalent( - approximation_degree=pass_manager_config.approximation_degree, - target=pass_manager_config.target, - ) - ] _opt += [ CommutativeCancellation(target=pass_manager_config.target), ] @@ -632,8 +626,8 @@ def _opt_control(property_set): approximation_degree=pass_manager_config.approximation_degree, ), ] - # If there are no basis gates (None/empty list), don't run - # Optimize1qGatesDecomposition or UnitarySynthesis + # Only run UnitarySynthesis and Optimize1qGatesDeecomposeition if + # there are basis_gates in the config if ( pass_manager_config.basis_gates is not None and len(pass_manager_config.basis_gates) > 0 @@ -680,19 +674,22 @@ def _unroll_condition(property_set): if optimization_level == 3: optimization.append(_minimum_point_check) elif optimization_level == 2: - # If there are no basis gates (None/empty list), don't run - # UnitarySynthesis + optimization.append( + [ + ConsolidateBlocks( + basis_gates=pass_manager_config.basis_gates, + target=pass_manager_config.target, + approximation_degree=pass_manager_config.approximation_degree, + ), + ] + ) + # Only run UnitarySynthesis if there are basis_gates in the config if ( pass_manager_config.basis_gates is not None and len(pass_manager_config.basis_gates) > 0 ): optimization.append( [ - ConsolidateBlocks( - basis_gates=pass_manager_config.basis_gates, - target=pass_manager_config.target, - approximation_degree=pass_manager_config.approximation_degree, - ), UnitarySynthesis( pass_manager_config.basis_gates, approximation_degree=pass_manager_config.approximation_degree, @@ -704,16 +701,6 @@ def _unroll_condition(property_set): ), ] ) - else: - optimization.append( - [ - ConsolidateBlocks( - basis_gates=pass_manager_config.basis_gates, - target=pass_manager_config.target, - approximation_degree=pass_manager_config.approximation_degree, - ), - ] - ) optimization.append(_depth_check + _size_check) else: optimization.append(_depth_check + _size_check) diff --git a/qiskit/transpiler/preset_passmanagers/common.py b/qiskit/transpiler/preset_passmanagers/common.py index 51428fe8aa59..c62955bd5896 100644 --- a/qiskit/transpiler/preset_passmanagers/common.py +++ b/qiskit/transpiler/preset_passmanagers/common.py @@ -415,7 +415,6 @@ def _direction_condition(property_set): condition=_direction_condition, ) ) - if remove_reset_in_zero: pre_opt.append(RemoveResetInZeroState()) return pre_opt diff --git a/qiskit/transpiler/target.py b/qiskit/transpiler/target.py index 2d17caa6beee..a19e17ce71c7 100644 --- a/qiskit/transpiler/target.py +++ b/qiskit/transpiler/target.py @@ -1338,7 +1338,7 @@ def __init__(self, coupling_map=None, **kwargs): def __len__(self): return len(self._gate_map) - def build_coupling_map(self, *args, **kwargs): + def build_coupling_map(self, *args, **kwargs): # pylint: disable=unused-argument return copy.deepcopy(self._coupling_map) def instruction_supported(self, *args, **kwargs): @@ -1354,9 +1354,9 @@ def instruction_supported(self, *args, **kwargs): @classmethod def from_configuration( cls, + *args, num_qubits: int | None = None, coupling_map: CouplingMap | list | None = None, - *args, **kwargs, ) -> _FakeTarget: diff --git a/test/python/transpiler/test_elide_permutations.py b/test/python/transpiler/test_elide_permutations.py index 2ad0f589c591..edef139af2ee 100644 --- a/test/python/transpiler/test_elide_permutations.py +++ b/test/python/transpiler/test_elide_permutations.py @@ -433,7 +433,6 @@ def test_unitary_equivalence_routing_and_basis_translation(self): self.assertTrue(Operator.from_circuit(res).equiv(Operator(qc))) with self.subTest("larger coupling map"): - spm = generate_preset_pass_manager( optimization_level=3, seed_transpiler=42,