Skip to content

Commit

Permalink
Raise error eagerly on bad unitary synthesis method (Qiskit#11367)
Browse files Browse the repository at this point in the history
* fix issue Qiskit#11355

* update docstring

* added test

* rename non-method

* Update releasenotes/notes/move-synthesis-plugin-error-61e3683bf5a0c225.yaml

* Reword messages

---------

Co-authored-by: Kevin J. Sung <[email protected]>
Co-authored-by: Jake Lishman <[email protected]>
  • Loading branch information
3 people authored Dec 5, 2023
1 parent 6137248 commit b1fc8e9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
16 changes: 8 additions & 8 deletions qiskit/transpiler/passes/synthesis/unitary_synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@ def __init__(
target: The optional :class:`~.Target` for the target device the pass
is compiling for. If specified this will supersede the values
set for ``basis_gates``, ``coupling_map``, and ``backend_props``.
Raises:
TranspilerError: if ``method`` was specified but is not found in the
installed plugins list. The list of installed plugins can be queried with
:func:`~qiskit.transpiler.passes.synthesis.plugin.unitary_synthesis_plugin_names`
"""
super().__init__()
self._basis_gates = set(basis_gates or ())
Expand Down Expand Up @@ -358,6 +363,9 @@ def __init__(

self._synth_gates = set(self._synth_gates) - self._basis_gates

if self.method != "default" and self.method not in self.plugins.ext_plugins:
raise TranspilerError(f"Specified method '{self.method}' not found in plugin list")

def run(self, dag: DAGCircuit) -> DAGCircuit:
"""Run the UnitarySynthesis pass on ``dag``.
Expand All @@ -366,15 +374,7 @@ def run(self, dag: DAGCircuit) -> DAGCircuit:
Returns:
Output dag with UnitaryGates synthesized to target basis.
Raises:
TranspilerError: if ``method`` was specified for the class and is not
found in the installed plugins list. The list of installed
plugins can be queried with
:func:`~qiskit.transpiler.passes.synthesis.plugin.unitary_synthesis_plugin_names`
"""
if self.method != "default" and self.method not in self.plugins.ext_plugins:
raise TranspilerError("Specified method: %s not found in plugin list" % self.method)

# If there aren't any gates to synthesize in the circuit we can skip all the iteration
# and just return.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
The :class:`.UnitarySynthesis` transpiler pass will now generate an error on initialization when
a nonexistent sythesis plugin is specified, rather than waiting until runtime to raise. Fixed
`#11355 <https://github.com/Qiskit/qiskit/issues/11355>`__.
14 changes: 14 additions & 0 deletions test/python/transpiler/test_stage_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from qiskit.compiler.transpiler import transpile
from qiskit.test import QiskitTestCase
from qiskit.transpiler import PassManager, PassManagerConfig, CouplingMap
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit.transpiler.preset_passmanagers.builtin_plugins import BasicSwapPassManager
from qiskit.transpiler.preset_passmanagers.plugin import (
PassManagerStagePluginManager,
Expand Down Expand Up @@ -113,3 +114,16 @@ def test_routing_plugins(self, optimization_level, routing_method):
backend = QasmSimulatorPy()
counts = backend.run(tqc, shots=1000).result().get_counts()
self.assertDictAlmostEqual(counts, {"0000": 500, "1111": 500}, delta=100)

@combine(
optimization_level=list(range(4)),
)
def test_unitary_synthesis_plugins(self, optimization_level):
"""Test unitary synthesis plugins"""
backend = QasmSimulatorPy()
with self.assertRaises(TranspilerError):
_ = generate_preset_pass_manager(
optimization_level=optimization_level,
backend=backend,
unitary_synthesis_method="not a method",
)

0 comments on commit b1fc8e9

Please sign in to comment.