Skip to content

Commit

Permalink
Add new get_control_flow_name_mapping function (Qiskit#13472)
Browse files Browse the repository at this point in the history
* Add new get_control_flow_name_mapping function, replace use in generate_preset_pass_manager and add reno.

* Document function

* Reword documentation

* Use get_control_flow_name_mapping in convert_to_target
  • Loading branch information
ElePT authored Jan 30, 2025
1 parent 8ab91fa commit 3c805dd
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 21 deletions.
6 changes: 6 additions & 0 deletions qiskit/circuit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,11 @@
Set of the instruction names of Qiskit's known control-flow operations.
The :func:`.get_control_flow_name_mapping` function allows to access the control-flow operation
classes associated to each name.
.. autofunction:: get_control_flow_name_mapping
These control-flow operations (:class:`IfElseOp`, :class:`WhileLoopOp`,
:class:`SwitchCaseOp` and :class:`ForLoopOp`) all have specific state that defines the branching
conditions and strategies, but contain all the different subcircuit blocks that might be entered in
Expand Down Expand Up @@ -1313,6 +1318,7 @@ def __array__(self, dtype=None, copy=None):
BreakLoopOp,
ContinueLoopOp,
CONTROL_FLOW_OP_NAMES,
get_control_flow_name_mapping,
)

from .annotated_operation import AnnotatedOperation, InverseModifier, ControlModifier, PowerModifier
Expand Down
29 changes: 29 additions & 0 deletions qiskit/circuit/controlflow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,32 @@

CONTROL_FLOW_OP_NAMES = frozenset(("for_loop", "while_loop", "if_else", "switch_case"))
"""Set of the instruction names of Qiskit's known control-flow operations."""


def get_control_flow_name_mapping():
"""Return a dictionary mapping the names of control-flow operations
to their corresponding classes."
Examples:
.. code-block:: python
from qiskit.circuit import get_control_flow_name_mapping
ctrl_flow_name_map = get_control_flow_name_mapping()
if_else_object = ctrl_flow_name_map["if_else"]
print(if_else_object)
.. code-block:: text
<class 'qiskit.circuit.controlflow.if_else.IfElseOp'>
"""

name_mapping = {
"if_else": IfElseOp,
"while_loop": WhileLoopOp,
"for_loop": ForLoopOp,
"switch_case": SwitchCaseOp,
}
return name_mapping
10 changes: 2 additions & 8 deletions qiskit/providers/backend_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from qiskit.providers.backend import QubitProperties
from qiskit.providers.models.backendconfiguration import BackendConfiguration
from qiskit.providers.models.backendproperties import BackendProperties
from qiskit.circuit.controlflow import CONTROL_FLOW_OP_NAMES
from qiskit.circuit.controlflow import CONTROL_FLOW_OP_NAMES, get_control_flow_name_mapping
from qiskit.providers.models.pulsedefaults import PulseDefaults
from qiskit.providers.options import Options
from qiskit.providers.exceptions import BackendPropertyError
Expand Down Expand Up @@ -79,7 +79,6 @@ def _convert_to_target(
Target,
InstructionProperties,
)
from qiskit.circuit.controlflow import ForLoopOp, IfElseOp, SwitchCaseOp, WhileLoopOp
from qiskit.circuit.library.standard_gates import get_standard_gate_name_mapping
from qiskit.circuit.parameter import Parameter
from qiskit.circuit.gate import Gate
Expand All @@ -91,12 +90,7 @@ def _convert_to_target(
if custom_name_mapping:
qiskit_inst_mapping.update(custom_name_mapping)

qiskit_control_flow_mapping = {
"if_else": IfElseOp,
"while_loop": WhileLoopOp,
"for_loop": ForLoopOp,
"switch_case": SwitchCaseOp,
}
qiskit_control_flow_mapping = get_control_flow_name_mapping()

in_data = {"num_qubits": configuration.num_qubits}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,7 @@
import copy
import warnings

from qiskit.circuit.controlflow import (
CONTROL_FLOW_OP_NAMES,
IfElseOp,
WhileLoopOp,
ForLoopOp,
SwitchCaseOp,
)
from qiskit.circuit.controlflow import CONTROL_FLOW_OP_NAMES, get_control_flow_name_mapping
from qiskit.circuit.library.standard_gates import get_standard_gate_name_mapping
from qiskit.circuit.quantumregister import Qubit
from qiskit.providers.backend import Backend
Expand Down Expand Up @@ -456,12 +450,7 @@ def _parse_basis_gates(basis_gates, backend, inst_map, skip_target):
standard_gates = get_standard_gate_name_mapping()
# Add control flow gates by default to basis set and name mapping
default_gates = {"measure", "delay", "reset"}.union(CONTROL_FLOW_OP_NAMES)
name_mapping = {
"if_else": IfElseOp,
"while_loop": WhileLoopOp,
"for_loop": ForLoopOp,
"switch_case": SwitchCaseOp,
}
name_mapping = get_control_flow_name_mapping()
try:
instructions = set(basis_gates)
for name in default_gates:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
features_circuits:
- |
Added a new :func:`.get_control_flow_name_mapping` convenience function that returns a
mapping of Qiskit's control-flow operation names to their corresponding class.
Example usage:
.. code-block:: python
from qiskit.circuit import get_control_flow_name_mapping
ctrl_flow_name_map = get_control_flow_name_mapping()
if_else_object = ctrl_flow_name_map["if_else"]
print(if_else_object)
.. code-block:: text
<class 'qiskit.circuit.controlflow.if_else.IfElseOp'>

0 comments on commit 3c805dd

Please sign in to comment.