Skip to content

Commit

Permalink
Fix target handling in discrete basis check
Browse files Browse the repository at this point in the history
In Qiskit#12727 a check was added to the default init stage's construction to
avoid running 2q gate consolidation in the presence of targets with
only discrete gates. However the way the target was being used in this
check was incorrect. The name for an instruction in the target should be
used as its identifier and then if we need the object representation
that should query the target for that object based on the name. However
the check was doing this backwards getting a list of operation objects
and then using the name contained in the object. This will cause issues
for instructions that use custom names such as when there are tuned
variants or a custom gate instance with a unique name.

While there is some question over whether we need this check as we will
run the consolidate 2q blocks pass as part of the optimization stage
which will have the same effect, this opts to just fix the target usage
for it to minimize the diff. Also while not the explicit goal of this
check it is protecting against some bugs in the consolidate blocks pass
that occur when custom gates are used. So for the short term this check
is retained, but in the future when these bugs in consolidate blocks are
fixed we can revisit whether we want to remove the conditional logic.
  • Loading branch information
mtreinish committed Aug 3, 2024
1 parent 1fdd527 commit a97732b
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions qiskit/transpiler/preset_passmanagers/builtin_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
from qiskit.transpiler.passes import Depth, Size, FixedPoint, MinimumPoint
from qiskit.transpiler.passes.utils.gates_basis import GatesInBasis
from qiskit.transpiler.passes.synthesis.unitary_synthesis import UnitarySynthesis
from qiskit.transpiler.target import Target
from qiskit.passmanager.flow_controllers import ConditionalController, DoWhileController
from qiskit.transpiler.timing_constraints import TimingConstraints
from qiskit.transpiler.passes.layout.vf2_layout import VF2LayoutStopReason
Expand Down Expand Up @@ -178,28 +179,35 @@ def pass_manager(self, pass_manager_config, optimization_level=None) -> PassMana
# use rz, sx, x, cx as basis, rely on physical optimziation to fix everything later one
stdgates = get_standard_gate_name_mapping()

def _is_one_op_non_discrete(ops):
def _is_one_op_non_discrete(target):
"""Checks if one operation in `ops` is not discrete, i.e. is parameterizable
Args:
ops (List(Operation)): list of operations to check
Returns
True if at least one operation in `ops` is not discrete, False otherwise
"""
found_one_continuous_gate = False
for op in ops:
if isinstance(op, str):
if op in _discrete_skipped_ops:
continue
op = stdgates.get(op, None)

if op is not None and op.name in _discrete_skipped_ops:
is_target = isinstance(target, Target)
if is_target:
op_names = target.operation_names
else:
op_names = target
for op_name in op_names:
if op_name in _discrete_skipped_ops:
continue

if op is None or not isinstance(op, Instruction):
if is_target:
op = target.operation_from_name(op_name)
else:
op = stdgates.get(op_name, None)
if op is None:
return False
if not isinstance(op, Instruction):
return False

if len(op.params) > 0:
found_one_continuous_gate = True
break
return found_one_continuous_gate

target = pass_manager_config.target
Expand All @@ -209,9 +217,7 @@ def _is_one_op_non_discrete(ops):
# * target has been specified, and we have one non-discrete gate in the target's spec
# * basis gates have been specified, and we have one non-discrete gate in that set
do_consolidate_blocks_init = target is None and basis is None
do_consolidate_blocks_init |= target is not None and _is_one_op_non_discrete(
target.operations
)
do_consolidate_blocks_init |= target is not None and _is_one_op_non_discrete(target)
do_consolidate_blocks_init |= basis is not None and _is_one_op_non_discrete(basis)

if do_consolidate_blocks_init:
Expand Down

0 comments on commit a97732b

Please sign in to comment.