Skip to content

Commit

Permalink
plugin convenience function for getting the entry point object (#9275)
Browse files Browse the repository at this point in the history
* plugin convenience function for getting the entry point object

* simpler code, thanks to Jake

Co-authored-by: Jake Lishman <[email protected]>

* typehint and docstring

* reno update

* thanks Jake

* Fix docs build

---------

Co-authored-by: Jake Lishman <[email protected]>
  • Loading branch information
1ucian0 and jakelishman authored Apr 18, 2023
1 parent ebf83b6 commit c4f1b0b
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
47 changes: 46 additions & 1 deletion qiskit/transpiler/preset_passmanagers/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,11 @@ def pass_manager(self, pass_manager_config, optimization_level):
PassManagerStagePlugin
PassManagerStagePluginManager
list_stage_plugins
passmanager_stage_plugins
"""

import abc
from typing import List, Optional
from typing import List, Optional, Dict

import stevedore

Expand Down Expand Up @@ -301,3 +302,47 @@ def list_stage_plugins(stage_name: str) -> List[str]:
return plugin_mgr.scheduling_plugins.names()
else:
raise TranspilerError(f"Invalid stage name: {stage_name}")


def passmanager_stage_plugins(stage: str) -> Dict[str, PassManagerStagePlugin]:
"""Return a dict with, for each stage name, the class type of the plugin.
This function is useful for getting more information about a plugin:
.. code-block:: python
from qiskit.transpiler.preset_passmanagers.plugin import passmanager_stage_plugins
routing_plugins = passmanager_stage_plugins('routing')
basic_plugin = routing_plugins['basic']
help(basic_plugin)
.. code-block:: text
Help on BasicSwapPassManager in module ...preset_passmanagers.builtin_plugins object:
class BasicSwapPassManager(...preset_passmanagers.plugin.PassManagerStagePlugin)
| Plugin class for routing stage with :class:`~.BasicSwap`
|
| Method resolution order:
| BasicSwapPassManager
| ...preset_passmanagers.plugin.PassManagerStagePlugin
| abc.ABC
| builtins.object
...
Args:
stage: The stage name to get
Returns:
dict: the key is the name of the plugin and the value is the class type for each.
Raises:
TranspilerError: If an invalid stage name is specified.
"""
plugin_mgr = PassManagerStagePluginManager()
try:
manager = getattr(plugin_mgr, f"{stage}_plugins")
except AttributeError as exc:
raise TranspilerError(f"Passmanager stage {stage} not found") from exc

return {name: manager[name].obj for name in manager.names()}
18 changes: 18 additions & 0 deletions releasenotes/notes/entry_point_obj-60625d9d797df1d9.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
features:
- |
The function ``passmanager_stage_plugins`` in the module
``qiskit.transpiler.preset_passmanagers.plugin`` was added to obtain a map between
plugin names and their class type.
This allows to identify and query passmanager plugin documentation. For example::
>>> from qiskit.transpiler.preset_passmanagers.plugin import passmanager_stage_plugins
>>> passmanager_stage_plugins('routing')['lookahead'].__class__
qiskit.transpiler.preset_passmanagers.builtin_plugins.LookaheadSwapPassManager
>>> help(passmanager_stage_plugins('routing')['lookahead'])
Help on BasicSwapPassManager in module qiskit.transpiler.preset_passmanagers.builtin_plugins object:
class BasicSwapPassManager(qiskit.transpiler.preset_passmanagers.plugin.PassManagerStagePlugin)
| Plugin class for routing stage with :class:`~.BasicSwap`
...
12 changes: 12 additions & 0 deletions test/python/transpiler/test_stage_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
from qiskit.compiler.transpiler import transpile
from qiskit.test import QiskitTestCase
from qiskit.transpiler import PassManager, PassManagerConfig, CouplingMap
from qiskit.transpiler.preset_passmanagers.builtin_plugins import BasicSwapPassManager
from qiskit.transpiler.preset_passmanagers.plugin import (
PassManagerStagePluginManager,
list_stage_plugins,
passmanager_stage_plugins,
)
from qiskit.transpiler.exceptions import TranspilerError
from qiskit.providers.basicaer import QasmSimulatorPy
Expand All @@ -51,6 +53,16 @@ def test_list_stage_plugins_invalid_stage_name(self):
with self.assertRaises(TranspilerError):
list_stage_plugins("not_a_stage")

def test_passmanager_stage_plugins(self):
"""Test entry_point_obj function."""
basic_obj = passmanager_stage_plugins("routing")
self.assertIsInstance(basic_obj["basic"], BasicSwapPassManager)

def test_passmanager_stage_plugins_not_found(self):
"""Test entry_point_obj function with nonexistent stage"""
with self.assertRaises(TranspilerError):
passmanager_stage_plugins("foo_stage")

def test_build_pm_invalid_plugin_name_valid_stage(self):
"""Test get pm from plugin with invalid plugin name and valid stage."""
plugin_manager = PassManagerStagePluginManager()
Expand Down

0 comments on commit c4f1b0b

Please sign in to comment.