Skip to content

Commit

Permalink
Fix lint and improve style
Browse files Browse the repository at this point in the history
  • Loading branch information
ElePT committed Jan 14, 2025
1 parent c11219b commit bfd25d0
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 44 deletions.
10 changes: 6 additions & 4 deletions qiskit/transpiler/passes/synthesis/high_level_synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,14 @@ def __init__(
self._basis_gates = basis_gates
self._min_qubits = min_qubits

# include cases where target exists with no basis gates, or the basis gates
# are an empty list
# When basis gates haven't been provided as an input, and
# there are no target basis gates (None or empty list),
# treat as "top level only" (no HLS).
self._top_level_only = (self._basis_gates is None or len(self._basis_gates) == 0) and (
self._target is None or len(self._target.operation_names) == 0
)

# include path for when target exists but target.num_qubits is None (BasicSimulator)
# Account for when target exists but target.num_qubits is None (BasicSimulator case)
if not self._top_level_only and (self._target is None or self._target.num_qubits is None):
basic_insts = {"measure", "reset", "barrier", "snapshot", "delay", "store"}
self._device_insts = basic_insts | set(self._basis_gates)
Expand Down Expand Up @@ -846,7 +847,8 @@ def _definitely_skip_node(
)

def _instruction_supported(self, name: str, qubits: tuple[int] | None) -> bool:
# include path for when target exists but target.num_qubits is None (BasicSimulator)
# Do not use target's method when there is no target, target.num_qubits is
# None (BasicSimulator), or target.operation_names is an empty list.
if (
self._target is None
or self._target.num_qubits is None
Expand Down
3 changes: 2 additions & 1 deletion qiskit/transpiler/passes/utils/check_gate_direction.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def run(self, dag):
Args:
dag (DAGCircuit): DAG to check.
"""

# Only use "check_gate_direction_target" if a target exists and target.operation_names
# is not empty, else use "check_gate_direction_coupling".
if self.target is None:
self.property_set["is_direction_mapped"] = check_gate_direction_coupling(
dag, set(self.coupling_map.get_edges())
Expand Down
5 changes: 3 additions & 2 deletions qiskit/transpiler/passes/utils/gate_direction.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ def run(self, dag):
TranspilerError: If the circuit cannot be mapped just by flipping the
cx nodes.
"""
# Only use "fix_gate_direction_target" if a target exists and target.operation_names
# is not empty, else use "fix_gate_direction_coupling".
if self.target is None:
return fix_gate_direction_coupling(dag, set(self.coupling_map.get_edges()))
# if target.operation_names is an empty list, extract and use the coupling map
if self.target is not None and len(self.target.operation_names) == 0:
elif len(self.target.operation_names) == 0:
return fix_gate_direction_coupling(
dag, set(self.target.build_coupling_map().get_edges())
)
Expand Down
53 changes: 20 additions & 33 deletions qiskit/transpiler/preset_passmanagers/builtin_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,28 +598,22 @@ def _opt_control(property_set):
elif optimization_level == 2:
# Steps for optimization level 2

# If there are no basis gates (None/empty list), don't run
# Optimize1qGatesDecomposition
_opt = [
RemoveIdentityEquivalent(
approximation_degree=pass_manager_config.approximation_degree,
target=pass_manager_config.target,
)
]
# Only run Optimize1qGatesDecomposition if there are basis_gates in the config
if (
pass_manager_config.basis_gates is not None
and len(pass_manager_config.basis_gates) > 0
):
_opt = [
RemoveIdentityEquivalent(
approximation_degree=pass_manager_config.approximation_degree,
target=pass_manager_config.target,
),
_opt += [
Optimize1qGatesDecomposition(
basis=pass_manager_config.basis_gates, target=pass_manager_config.target
),
]
else:
_opt = [
RemoveIdentityEquivalent(
approximation_degree=pass_manager_config.approximation_degree,
target=pass_manager_config.target,
)
]
_opt += [
CommutativeCancellation(target=pass_manager_config.target),
]
Expand All @@ -632,8 +626,8 @@ def _opt_control(property_set):
approximation_degree=pass_manager_config.approximation_degree,
),
]
# If there are no basis gates (None/empty list), don't run
# Optimize1qGatesDecomposition or UnitarySynthesis
# Only run UnitarySynthesis and Optimize1qGatesDeecomposeition if
# there are basis_gates in the config
if (
pass_manager_config.basis_gates is not None
and len(pass_manager_config.basis_gates) > 0
Expand Down Expand Up @@ -680,19 +674,22 @@ def _unroll_condition(property_set):
if optimization_level == 3:
optimization.append(_minimum_point_check)
elif optimization_level == 2:
# If there are no basis gates (None/empty list), don't run
# UnitarySynthesis
optimization.append(
[
ConsolidateBlocks(
basis_gates=pass_manager_config.basis_gates,
target=pass_manager_config.target,
approximation_degree=pass_manager_config.approximation_degree,
),
]
)
# Only run UnitarySynthesis if there are basis_gates in the config
if (
pass_manager_config.basis_gates is not None
and len(pass_manager_config.basis_gates) > 0
):
optimization.append(
[
ConsolidateBlocks(
basis_gates=pass_manager_config.basis_gates,
target=pass_manager_config.target,
approximation_degree=pass_manager_config.approximation_degree,
),
UnitarySynthesis(
pass_manager_config.basis_gates,
approximation_degree=pass_manager_config.approximation_degree,
Expand All @@ -704,16 +701,6 @@ def _unroll_condition(property_set):
),
]
)
else:
optimization.append(
[
ConsolidateBlocks(
basis_gates=pass_manager_config.basis_gates,
target=pass_manager_config.target,
approximation_degree=pass_manager_config.approximation_degree,
),
]
)
optimization.append(_depth_check + _size_check)
else:
optimization.append(_depth_check + _size_check)
Expand Down
1 change: 0 additions & 1 deletion qiskit/transpiler/preset_passmanagers/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,6 @@ def _direction_condition(property_set):
condition=_direction_condition,
)
)

if remove_reset_in_zero:
pre_opt.append(RemoveResetInZeroState())
return pre_opt
Expand Down
4 changes: 2 additions & 2 deletions qiskit/transpiler/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,7 @@ def __init__(self, coupling_map=None, **kwargs):
def __len__(self):
return len(self._gate_map)

def build_coupling_map(self, *args, **kwargs):
def build_coupling_map(self, *args, **kwargs): # pylint: disable=unused-argument
return copy.deepcopy(self._coupling_map)

def instruction_supported(self, *args, **kwargs):
Expand All @@ -1354,9 +1354,9 @@ def instruction_supported(self, *args, **kwargs):
@classmethod
def from_configuration(
cls,
*args,
num_qubits: int | None = None,
coupling_map: CouplingMap | list | None = None,
*args,
**kwargs,
) -> _FakeTarget:

Expand Down
1 change: 0 additions & 1 deletion test/python/transpiler/test_elide_permutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,6 @@ def test_unitary_equivalence_routing_and_basis_translation(self):
self.assertTrue(Operator.from_circuit(res).equiv(Operator(qc)))

with self.subTest("larger coupling map"):

spm = generate_preset_pass_manager(
optimization_level=3,
seed_transpiler=42,
Expand Down

0 comments on commit bfd25d0

Please sign in to comment.