FlowController
class qiskit.passmanager.FlowController(options=None)
Bases: BaseController
A legacy factory for other flow controllers.
This class is primarily for compatibility with legacy versions of Qiskit, and in general, you should prefer simply instantiating the controller you want, and adding it to the relevant PassManager
or other controller. Its use is deprecated.
This allows syntactic sugar for writing pipelines. For example:
FlowController.add_flow_controller("my_condition", CustomController)
controller = FlowController.controller_factory(
[PassA(), PassB()],
{"max_iteration": 1000},
condition=lambda prop_set: prop_set["x"] == 0,
do_while=lambda prop_set: prop_set["x"] < 100,
my_condition=lambda prop_set: prop_set["y"] = "abc",
)
This creates a nested flow controller that runs when the value x
in the PropertySet
is zero and repeats the pipeline until the value becomes 100. In each innermost loop, the custom iteration condition provided by the CustomController
is also evaluated.
BaseController
must be directly subclassed to define a custom flow controller. This class provides a controller factory method, which consumes a class variable registered_controllers
. Subclassing FlowController may cause unexpected behavior in the factory method. Note that factory method implicitly determines the priority of the builtin controllers when multiple controllers are called together, and the behavior of generated controller is hardly debugged.
The class qiskit.passmanager.flow_controllers.FlowController
is deprecated as of qiskit 0.46.0. It will be removed in the 1.0 release. The base class of flow controller is now qiskit.passmanager.BaseController. This class exists only for the controller namespace used to map a controller name tothe corresponding class object, which is used to instantiate a controller with keyword argument in PassManager.append method. This pattern was also deprecated.
Attributes
hierarchy
Default value: ['condition', 'do_while']
registered_controllers
Default value: {'condition': <class 'qiskit.passmanager.flow_controllers.ConditionalController'>, 'do_while': <class 'qiskit.passmanager.flow_controllers.DoWhileController'>}
Methods
add_flow_controller
classmethod add_flow_controller(name, controller)
Adds a flow controller.
The method qiskit.passmanager.flow_controllers.FlowController.add_flow_controller()
is deprecated as of qiskit 0.45.0. It will be removed in the 1.0 release. The controller factory method is deprecated and managing the custom flow controllers with aliases no longer helps building the task pipeline. Controllers must be explicitly instantiated and appended to the pipeline.
Parameters
- name (str) – Alias of controller class in the namespace.
- controller (Type[BaseController]) – Flow controller class.
controller_factory
classmethod controller_factory(passes, options, **controllers)
Create a new flow controller with normalization.
The method qiskit.passmanager.flow_controllers.FlowController.controller_factory()
is deprecated as of qiskit 0.45.0. It will be removed in the 1.0 release. FlowController objects must be explicitly instantiated. Building controllers with keyword arguments ignores the order when multiple keyword arguments are provided together, which is likely unsafe.
Parameters
- passes (Task | list[Task]) – A list of optimization tasks.
- options (dict) – Option for this flow controller.
- controllers – Dictionary of controller callables keyed on flow controller alias.
Returns
An instance of normalized flow controller.
execute
execute(passmanager_ir, state, callback=None)
Execute optimization task for input Qiskit IR.
Parameters
- passmanager_ir (Any) – Qiskit IR to optimize.
- state (PassManagerState) – State associated with workflow execution by the pass manager itself.
- callback (Callable | None) – A callback function which is caller per execution of optimization task.
Returns
Optimized Qiskit IR and state of the workflow.
Return type
tuple[Any, qiskit.passmanager.compilation_status.PassManagerState]
iter_tasks
abstract iter_tasks(state)
A custom logic to choose a next task to run.
Controller subclass can consume the state to build a proper task pipeline. The updated state after a task execution will be fed back in as the “return” value of any yield
statements. This indicates the order of task execution is only determined at running time. This method is not allowed to mutate the given state object.
Parameters
- state (PassManagerState) – The state of the passmanager workflow at the beginning of this flow controller’s execution.
- state – the state of pass manager after the execution of the last task that was yielded. The generator does not need to inspect this if it is irrelevant to its logic, nor update it.
Yields
Task – Next task to run.
Return type
Generator[Task, PassManagerState, None]
remove_flow_controller
classmethod remove_flow_controller(name)
Removes a flow controller.
The method qiskit.passmanager.flow_controllers.FlowController.remove_flow_controller()
is deprecated as of qiskit 0.45.0. It will be removed in the 1.0 release. The controller factory method is deprecated and managing the custom flow controllers with aliases no longer helps building the task pipeline. Controllers must be explicitly instantiated and appended to the pipeline.
Parameters
name (str) – Alias of the controller to remove.
Raises
KeyError – If the controller to remove was not registered.