Skip to content

Commit

Permalink
Fix test.python.circuit.test_controlled_gate.TestControlledGate.test_…
Browse files Browse the repository at this point in the history
…mc_failure_without_annotation:

 * Sloppy attept at adding control flow instructions to target gate name mapping (100% could be more efficient)

 * Suport target with basis_gates but qubits=None in Optimizq1qGatesDecomposition
  • Loading branch information
ElePT committed Sep 20, 2024
1 parent 9078df2 commit df78b1c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ def __init__(self, basis=None, target=None):
elif target is None:
self._global_decomposers = _possible_decomposers(None)
self._basis_gates = None
else:
self._global_decomposers = _possible_decomposers(set(target.operation_names))
self._basis_gates = None

self.error_map = self._build_error_map()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
import warnings

from qiskit.circuit.controlflow import CONTROL_FLOW_OP_NAMES
from qiskit.circuit.controlflow import (
IfElseOp,
WhileLoopOp,
ForLoopOp,
SwitchCaseOp,
)
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 All @@ -35,6 +41,14 @@
from .level2 import level_2_pass_manager
from .level3 import level_3_pass_manager

CONTROL_FLOW_MAPPING = {
"if_else": IfElseOp,
"while_loop": WhileLoopOp,
"for_loop": ForLoopOp,
"switch_case": SwitchCaseOp,
}


def generate_preset_pass_manager(
optimization_level=2,
backend=None,
Expand Down Expand Up @@ -277,7 +291,12 @@ def generate_preset_pass_manager(
stacklevel=2,
)

if target is None and backend is None and scheduling_method is not None and coupling_map is None:
if (
target is None
and backend is None
and scheduling_method is not None
and coupling_map is None
):
warnings.warn(
"A coupling map is required for the scheduling stage. "
"It must be defined through the `target` or "
Expand All @@ -286,7 +305,6 @@ def generate_preset_pass_manager(
stacklevel=2,
)


# Check if a custom inst_map was specified before overwriting inst_map
_given_inst_map = bool(inst_map)
# If there are no loose constraints => use backend target if available
Expand Down Expand Up @@ -330,7 +348,11 @@ def generate_preset_pass_manager(
# try:
target = Target.from_configuration(
basis_gates=basis_gates,
num_qubits=backend.num_qubits if backend is not None else coupling_map.size() if coupling_map is not None else None,
num_qubits=(
backend.num_qubits
if backend is not None
else coupling_map.size() if coupling_map is not None else None
),
coupling_map=coupling_map,
# If the instruction map has custom gates, do not give as config, the information
# will be added to the target with update_from_instruction_schedule_map
Expand Down Expand Up @@ -438,6 +460,7 @@ def _parse_basis_gates(basis_gates, backend, inst_map, skip_target):
for name in default_gates:
if name not in instructions:
instructions.add(name)
name_mapping.update({name: CONTROL_FLOW_MAPPING[name] for name in CONTROL_FLOW_OP_NAMES})
except TypeError:
instructions = None

Expand Down
2 changes: 1 addition & 1 deletion test/python/circuit/test_scheduled_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def test_per_qubit_durations(self):
sc = transpile(
qc,
scheduling_method="alap",
coupling_map=[[0,1]],
coupling_map=[[0, 1]],
basis_gates=["h", "cx"],
instruction_durations=[("h", None, 200), ("cx", [0, 1], 700)],
)
Expand Down

0 comments on commit df78b1c

Please sign in to comment.