From b9d5c9c6aeb4568b6e9fba8943517ebed300886d Mon Sep 17 00:00:00 2001 From: Alexander Ivrii Date: Thu, 14 Nov 2024 18:42:02 +0200 Subject: [PATCH] HLS fix to not synthesize instructions already supported (#13417) * HLS fix to not synthesize instructions already supported * reno * fix for control-flow-operations The control-flow operations such as 'for-loop` are considered to be a part of 'basis_gates`, yet they need to be recursively synthesized. * using faster is_control_flow check * Update test/python/transpiler/test_high_level_synthesis.py Co-authored-by: Julien Gacon --------- Co-authored-by: Julien Gacon --- .../passes/synthesis/high_level_synthesis.py | 10 ++++------ ...hls-supported-instructions-0d80ea33b3d2257b.yaml | 8 ++++++++ test/python/transpiler/test_high_level_synthesis.py | 13 +++++++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 releasenotes/notes/fix-hls-supported-instructions-0d80ea33b3d2257b.yaml diff --git a/qiskit/transpiler/passes/synthesis/high_level_synthesis.py b/qiskit/transpiler/passes/synthesis/high_level_synthesis.py index d7b5b0d535b..95adee1d05c 100644 --- a/qiskit/transpiler/passes/synthesis/high_level_synthesis.py +++ b/qiskit/transpiler/passes/synthesis/high_level_synthesis.py @@ -814,6 +814,7 @@ def _definitely_skip_node( dag._has_calibration_for(node) or len(node.qargs) < self._min_qubits or node.is_directive() + or (self._instruction_supported(node.name, qubits) and not node.is_control_flow()) ): return True @@ -831,15 +832,12 @@ def _definitely_skip_node( # If all the above constraints hold, and it's already supported or the basis translator # can handle it, we'll leave it be. and ( - self._instruction_supported(node.name, qubits) # This uses unfortunately private details of `EquivalenceLibrary`, but so does the # `BasisTranslator`, and this is supposed to just be temporary til this is moved # into Rust space. - or ( - self._equiv_lib is not None - and equivalence.Key(name=node.name, num_qubits=node.num_qubits) - in self._equiv_lib.keys() - ) + self._equiv_lib is not None + and equivalence.Key(name=node.name, num_qubits=node.num_qubits) + in self._equiv_lib.keys() ) ) diff --git a/releasenotes/notes/fix-hls-supported-instructions-0d80ea33b3d2257b.yaml b/releasenotes/notes/fix-hls-supported-instructions-0d80ea33b3d2257b.yaml new file mode 100644 index 00000000000..1e9c1a1cebe --- /dev/null +++ b/releasenotes/notes/fix-hls-supported-instructions-0d80ea33b3d2257b.yaml @@ -0,0 +1,8 @@ +--- +fixes: + - | + Previously the :class:`.HighLevelSynthesis` transpiler pass synthesized an + instruction for which a synthesis plugin is available, regardless of + whether the instruction is already supported by the target or a part of + the explicitly passed ``basis_gates``. This behavior is now fixed, so that + such already supported instructions are no longer synthesized. diff --git a/test/python/transpiler/test_high_level_synthesis.py b/test/python/transpiler/test_high_level_synthesis.py index 9fcd10bd1ef..11aa8501afd 100644 --- a/test/python/transpiler/test_high_level_synthesis.py +++ b/test/python/transpiler/test_high_level_synthesis.py @@ -46,6 +46,7 @@ IGate, MCXGate, SGate, + QAOAAnsatz, ) from qiskit.circuit.library import LinearFunction, PauliEvolutionGate from qiskit.quantum_info import Clifford, Operator, Statevector, SparsePauliOp @@ -664,6 +665,18 @@ def test_synth_fails_definition_exists(self): out = hls(circuit) self.assertEqual(out.count_ops(), {"u": 1}) + def test_both_basis_gates_and_plugin_specified(self): + """Test that a gate is not synthesized when it belongs to basis_gates, + regardless of whether there is a plugin method available. + + See: https://github.com/Qiskit/qiskit/issues/13412 for more + details. + """ + qc = QAOAAnsatz(SparsePauliOp("Z"), initial_state=QuantumCircuit(1)) + pm = PassManager([HighLevelSynthesis(basis_gates=["PauliEvolution"])]) + qct = pm.run(qc) + self.assertEqual(qct.count_ops()["PauliEvolution"], 2) + class TestPMHSynthesisLinearFunctionPlugin(QiskitTestCase): """Tests for the PMHSynthesisLinearFunction plugin for synthesizing linear functions."""