From c0511545722ce020d3787cb591cf1c7f34094ab4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elena=20Pe=C3=B1a=20Tapia?= Date: Thu, 21 Nov 2024 12:06:00 +0100 Subject: [PATCH 1/4] Add new get_control_flow_name_mapping function, replace use in generate_preset_pass_manager and add reno. --- qiskit/circuit/__init__.py | 1 + qiskit/circuit/controlflow/__init__.py | 29 +++++++++++++++++++ .../generate_preset_pass_manager.py | 15 ++-------- ...rl-flow-name-mapping-21842a23726e6e77.yaml | 19 ++++++++++++ 4 files changed, 51 insertions(+), 13 deletions(-) create mode 100644 releasenotes/notes/add-ctrl-flow-name-mapping-21842a23726e6e77.yaml diff --git a/qiskit/circuit/__init__.py b/qiskit/circuit/__init__.py index ab7ff9a84160..73918bfb6618 100644 --- a/qiskit/circuit/__init__.py +++ b/qiskit/circuit/__init__.py @@ -1307,6 +1307,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 diff --git a/qiskit/circuit/controlflow/__init__.py b/qiskit/circuit/controlflow/__init__.py index 2679b45d7782..864e62646faf 100644 --- a/qiskit/circuit/controlflow/__init__.py +++ b/qiskit/circuit/controlflow/__init__.py @@ -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 name of control-flow operations + to its corresponding class" + + 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 + + + """ + + name_mapping = { + "if_else": IfElseOp, + "while_loop": WhileLoopOp, + "for_loop": ForLoopOp, + "switch_case": SwitchCaseOp, + } + return name_mapping diff --git a/qiskit/transpiler/preset_passmanagers/generate_preset_pass_manager.py b/qiskit/transpiler/preset_passmanagers/generate_preset_pass_manager.py index 8f08a8337376..7ca794a58f9c 100644 --- a/qiskit/transpiler/preset_passmanagers/generate_preset_pass_manager.py +++ b/qiskit/transpiler/preset_passmanagers/generate_preset_pass_manager.py @@ -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 @@ -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: diff --git a/releasenotes/notes/add-ctrl-flow-name-mapping-21842a23726e6e77.yaml b/releasenotes/notes/add-ctrl-flow-name-mapping-21842a23726e6e77.yaml new file mode 100644 index 000000000000..fd0f657895dc --- /dev/null +++ b/releasenotes/notes/add-ctrl-flow-name-mapping-21842a23726e6e77.yaml @@ -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 + + From 5184f7c8c305736ba3295070c796b923f58d12a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elena=20Pe=C3=B1a=20Tapia?= Date: Thu, 21 Nov 2024 14:47:16 +0100 Subject: [PATCH 2/4] Document function --- qiskit/circuit/__init__.py | 5 +++++ qiskit/circuit/controlflow/__init__.py | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/qiskit/circuit/__init__.py b/qiskit/circuit/__init__.py index 73918bfb6618..be69de4655fd 100644 --- a/qiskit/circuit/__init__.py +++ b/qiskit/circuit/__init__.py @@ -693,6 +693,11 @@ Set of the instruction names of Qiskit's known control-flow operations. +In addition to this, the function :func:`.get_control_flow_name_mapping` returns a dictionary +mapping the names of the control-flow operations to their corresponding classes." + +.. 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 diff --git a/qiskit/circuit/controlflow/__init__.py b/qiskit/circuit/controlflow/__init__.py index 864e62646faf..4f48e7229dab 100644 --- a/qiskit/circuit/controlflow/__init__.py +++ b/qiskit/circuit/controlflow/__init__.py @@ -29,8 +29,8 @@ def get_control_flow_name_mapping(): - """Return a dictionary mapping the name of control-flow operations - to its corresponding class" + """Return a dictionary mapping the names of control-flow operations + to their corresponding classes." Examples: From aed5901be2031eaa5673bf6281bb68d3b58d9cb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elena=20Pe=C3=B1a=20Tapia?= Date: Thu, 21 Nov 2024 15:21:30 +0100 Subject: [PATCH 3/4] Reword documentation --- qiskit/circuit/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/qiskit/circuit/__init__.py b/qiskit/circuit/__init__.py index be69de4655fd..bc8781532fa7 100644 --- a/qiskit/circuit/__init__.py +++ b/qiskit/circuit/__init__.py @@ -693,8 +693,8 @@ Set of the instruction names of Qiskit's known control-flow operations. -In addition to this, the function :func:`.get_control_flow_name_mapping` returns a dictionary -mapping the names of the control-flow operations to their corresponding classes." +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 From 54345832bc005333fc92734f2dd8641716f8661f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elena=20Pe=C3=B1a=20Tapia?= Date: Fri, 22 Nov 2024 17:39:25 +0100 Subject: [PATCH 4/4] Use get_control_flow_name_mapping in convert_to_target --- qiskit/providers/backend_compat.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/qiskit/providers/backend_compat.py b/qiskit/providers/backend_compat.py index 6971f8b8328b..571500fa35e9 100644 --- a/qiskit/providers/backend_compat.py +++ b/qiskit/providers/backend_compat.py @@ -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 @@ -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 @@ -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}