FlowController

class qiskit.passmanager.FlowController(options=None)[source]

Bases: BaseController

A legacy factory for other flow controllers.

Warning

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.

Warning

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.

Create new flow controller.

Parameters:

options (dict[str, Any] | None) – Option for this flow controller.

Attributes

hierarchy = ['condition', 'do_while']
registered_controllers = {'condition': <class 'qiskit.passmanager.flow_controllers.ConditionalController'>, 'do_while': <class 'qiskit.passmanager.flow_controllers.DoWhileController'>}

Methods

classmethod add_flow_controller(name, controller)[source]

Adds a flow controller.

Deprecated since version 0.45.0: The method qiskit.passmanager.flow_controllers.FlowController.add_flow_controller() is deprecated as of qiskit 0.45.0. It will be removed no earlier than 3 months after the release date. Controller factory method is deprecated and managing the custom flow controllers with alias 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.

classmethod controller_factory(passes, options, **controllers)[source]

Create a new flow controller with normalization.

Deprecated since version 0.45.0: The method qiskit.passmanager.flow_controllers.FlowController.controller_factory() is deprecated as of qiskit 0.45.0. It will be removed no earlier than 3 months after the release date. Controller object must be explicitly instantiated. Building controller with keyword arguments may yield race condition 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(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]

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]

classmethod remove_flow_controller(name)[source]

Removes a flow controller.

Deprecated since version 0.45.0: The method qiskit.passmanager.flow_controllers.FlowController.remove_flow_controller() is deprecated as of qiskit 0.45.0. It will be removed no earlier than 3 months after the release date. Controller factory method is deprecated and managing the custom flow controllers with alias 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.