Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new get_control_flow_name_mapping function #13472

Merged
merged 4 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions qiskit/circuit/__init__.py
jakelishman marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,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 @@ -1307,6 +1312,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'>