Skip to content

Commit

Permalink
Make optimization level a plugin argument
Browse files Browse the repository at this point in the history
  • Loading branch information
mtreinish committed Aug 25, 2022
1 parent 7120d2d commit d731d06
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 101 deletions.
71 changes: 37 additions & 34 deletions qiskit/transpiler/preset_passmanagers/builtin_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

"""Built-in transpiler stage plugins for preset pass managers."""

from qiskit.transpiler.passmanager_config import PassManagerConfig
from qiskit.transpiler.passmanager import PassManager
from qiskit.transpiler.exceptions import TranspilerError
from qiskit.transpiler.passes import BasicSwap
Expand All @@ -27,26 +26,27 @@
class BasicSwapPassManager(PassManagerStagePlugin):
"""Plugin class for routing stage with :class:`~.BasicSwap`"""

def pass_manager(self, pass_manager_config: PassManagerConfig) -> PassManager:
def pass_manager(self, pass_manager_config, optimization_level=None) -> PassManager:
"""Build routing stage PassManager."""
opt_level = pass_manager_config.optimization_level
seed_transpiler = pass_manager_config.seed_transpiler
target = pass_manager_config.target
coupling_map = pass_manager_config.coupling_map
backend_properties = pass_manager_config.backend_properties
routing_pass = BasicSwap(coupling_map)
vf2_call_limit = common.get_vf2_call_limit(
opt_level, pass_manager_config.layout_method, pass_manager_config.initial_layout
optimization_level,
pass_manager_config.layout_method,
pass_manager_config.initial_layout,
)
if opt_level == 0:
if optimization_level == 0:
return common.generate_routing_passmanager(
routing_pass,
target,
coupling_map=coupling_map,
seed_transpiler=seed_transpiler,
use_barrier_before_measurement=True,
)
if opt_level == 1:
if optimization_level == 1:
return common.generate_routing_passmanager(
routing_pass,
target,
Expand All @@ -57,7 +57,7 @@ def pass_manager(self, pass_manager_config: PassManagerConfig) -> PassManager:
check_trivial=True,
use_barrier_before_measurement=True,
)
if opt_level == 2:
if optimization_level == 2:
return common.generate_routing_passmanager(
routing_pass,
target,
Expand All @@ -67,7 +67,7 @@ def pass_manager(self, pass_manager_config: PassManagerConfig) -> PassManager:
seed_transpiler=seed_transpiler,
use_barrier_before_measurement=True,
)
if opt_level == 3:
if optimization_level == 3:
return common.generate_routing_passmanager(
routing_pass,
target,
Expand All @@ -77,36 +77,37 @@ def pass_manager(self, pass_manager_config: PassManagerConfig) -> PassManager:
seed_transpiler=seed_transpiler,
use_barrier_before_measurement=True,
)
raise TranspilerError(f"Invalid optimization level specified: {opt_level}")
raise TranspilerError(f"Invalid optimization level specified: {optimization_level}")


class StochasticSwapPassManager(PassManagerStagePlugin):
"""Plugin class for routing stage with :class:`~.StochasticSwap`"""

def pass_manager(self, pass_manager_config: PassManagerConfig) -> PassManager:
def pass_manager(self, pass_manager_config, optimization_level=None) -> PassManager:
"""Build routing stage PassManager."""
opt_level = pass_manager_config.optimization_level
seed_transpiler = pass_manager_config.seed_transpiler
target = pass_manager_config.target
coupling_map = pass_manager_config.coupling_map
backend_properties = pass_manager_config.backend_properties
vf2_call_limit = common.get_vf2_call_limit(
opt_level, pass_manager_config.layout_method, pass_manager_config.initial_layout
optimization_level,
pass_manager_config.layout_method,
pass_manager_config.initial_layout,
)
if opt_level == 3:
if optimization_level == 3:
routing_pass = StochasticSwap(coupling_map, trials=200, seed=seed_transpiler)
else:
routing_pass = StochasticSwap(coupling_map, trials=20, seed=seed_transpiler)

if opt_level == 0:
if optimization_level == 0:
return common.generate_routing_passmanager(
routing_pass,
target,
coupling_map=coupling_map,
seed_transpiler=seed_transpiler,
use_barrier_before_measurement=True,
)
if opt_level == 1:
if optimization_level == 1:
return common.generate_routing_passmanager(
routing_pass,
target,
Expand All @@ -117,7 +118,7 @@ def pass_manager(self, pass_manager_config: PassManagerConfig) -> PassManager:
check_trivial=True,
use_barrier_before_measurement=True,
)
if opt_level in {2, 3}:
if optimization_level in {2, 3}:
return common.generate_routing_passmanager(
routing_pass,
target,
Expand All @@ -127,23 +128,24 @@ def pass_manager(self, pass_manager_config: PassManagerConfig) -> PassManager:
seed_transpiler=seed_transpiler,
use_barrier_before_measurement=True,
)
raise TranspilerError(f"Invalid optimization level specified: {opt_level}")
raise TranspilerError(f"Invalid optimization level specified: {optimization_level}")


class LookaheadSwapPassManager(PassManagerStagePlugin):
"""Plugin class for routing stage with :class:`~.LookaheadSwap`"""

def pass_manager(self, pass_manager_config: PassManagerConfig) -> PassManager:
def pass_manager(self, pass_manager_config, optimization_level=None) -> PassManager:
"""Build routing stage PassManager."""
opt_level = pass_manager_config.optimization_level
seed_transpiler = pass_manager_config.seed_transpiler
target = pass_manager_config.target
coupling_map = pass_manager_config.coupling_map
backend_properties = pass_manager_config.backend_properties
vf2_call_limit = common.get_vf2_call_limit(
opt_level, pass_manager_config.layout_method, pass_manager_config.initial_layout
optimization_level,
pass_manager_config.layout_method,
pass_manager_config.initial_layout,
)
if opt_level == 0:
if optimization_level == 0:
routing_pass = LookaheadSwap(coupling_map, search_depth=2, search_width=2)
return common.generate_routing_passmanager(
routing_pass,
Expand All @@ -152,7 +154,7 @@ def pass_manager(self, pass_manager_config: PassManagerConfig) -> PassManager:
seed_transpiler=seed_transpiler,
use_barrier_before_measurement=True,
)
if opt_level == 1:
if optimization_level == 1:
routing_pass = LookaheadSwap(coupling_map, search_depth=4, search_width=4)
return common.generate_routing_passmanager(
routing_pass,
Expand All @@ -164,7 +166,7 @@ def pass_manager(self, pass_manager_config: PassManagerConfig) -> PassManager:
check_trivial=True,
use_barrier_before_measurement=True,
)
if opt_level == 2:
if optimization_level == 2:
routing_pass = LookaheadSwap(coupling_map, search_depth=5, search_width=6)
return common.generate_routing_passmanager(
routing_pass,
Expand All @@ -175,7 +177,7 @@ def pass_manager(self, pass_manager_config: PassManagerConfig) -> PassManager:
seed_transpiler=seed_transpiler,
use_barrier_before_measurement=True,
)
if opt_level == 3:
if optimization_level == 3:
routing_pass = LookaheadSwap(coupling_map, search_depth=5, search_width=6)
return common.generate_routing_passmanager(
routing_pass,
Expand All @@ -186,23 +188,24 @@ def pass_manager(self, pass_manager_config: PassManagerConfig) -> PassManager:
seed_transpiler=seed_transpiler,
use_barrier_before_measurement=True,
)
raise TranspilerError(f"Invalid optimization level specified: {opt_level}")
raise TranspilerError(f"Invalid optimization level specified: {optimization_level}")


class SabreSwapPassManager(PassManagerStagePlugin):
"""Plugin class for routing stage with :class:`~.SabreSwap`"""

def pass_manager(self, pass_manager_config: PassManagerConfig) -> PassManager:
def pass_manager(self, pass_manager_config, optimization_level=None) -> PassManager:
"""Build routing stage PassManager."""
opt_level = pass_manager_config.optimization_level
seed_transpiler = pass_manager_config.seed_transpiler
target = pass_manager_config.target
coupling_map = pass_manager_config.coupling_map
backend_properties = pass_manager_config.backend_properties
vf2_call_limit = common.get_vf2_call_limit(
opt_level, pass_manager_config.layout_method, pass_manager_config.initial_layout
optimization_level,
pass_manager_config.layout_method,
pass_manager_config.initial_layout,
)
if opt_level == 0:
if optimization_level == 0:
routing_pass = SabreSwap(coupling_map, heuristic="basic", seed=seed_transpiler)
return common.generate_routing_passmanager(
routing_pass,
Expand All @@ -211,7 +214,7 @@ def pass_manager(self, pass_manager_config: PassManagerConfig) -> PassManager:
seed_transpiler=seed_transpiler,
use_barrier_before_measurement=True,
)
if opt_level == 1:
if optimization_level == 1:
routing_pass = SabreSwap(coupling_map, heuristic="lookahead", seed=seed_transpiler)
return common.generate_routing_passmanager(
routing_pass,
Expand All @@ -223,7 +226,7 @@ def pass_manager(self, pass_manager_config: PassManagerConfig) -> PassManager:
check_trivial=True,
use_barrier_before_measurement=True,
)
if opt_level == 2:
if optimization_level == 2:
routing_pass = SabreSwap(coupling_map, heuristic="decay", seed=seed_transpiler)
return common.generate_routing_passmanager(
routing_pass,
Expand All @@ -234,7 +237,7 @@ def pass_manager(self, pass_manager_config: PassManagerConfig) -> PassManager:
seed_transpiler=seed_transpiler,
use_barrier_before_measurement=True,
)
if opt_level == 3:
if optimization_level == 3:
routing_pass = SabreSwap(coupling_map, heuristic="decay", seed=seed_transpiler)
return common.generate_routing_passmanager(
routing_pass,
Expand All @@ -245,13 +248,13 @@ def pass_manager(self, pass_manager_config: PassManagerConfig) -> PassManager:
seed_transpiler=seed_transpiler,
use_barrier_before_measurement=True,
)
raise TranspilerError(f"Invalid optimization level specified: {opt_level}")
raise TranspilerError(f"Invalid optimization level specified: {optimization_level}")


class NoneRoutingPassManager(PassManagerStagePlugin):
"""Plugin class for routing stage with error on routing."""

def pass_manager(self, pass_manager_config: PassManagerConfig) -> PassManager:
def pass_manager(self, pass_manager_config, optimization_level=None) -> PassManager:
"""Build routing stage PassManager."""
seed_transpiler = pass_manager_config.seed_transpiler
target = pass_manager_config.target
Expand Down
22 changes: 8 additions & 14 deletions qiskit/transpiler/preset_passmanagers/level0.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ def level_0_pass_manager(pass_manager_config: PassManagerConfig) -> StagedPassMa
unitary_synthesis_method = pass_manager_config.unitary_synthesis_method
unitary_synthesis_plugin_config = pass_manager_config.unitary_synthesis_plugin_config
target = pass_manager_config.target
# Override an unset optimization_level for stage plugin use.
# it will be restored to None before this is returned
optimization_level = pass_manager_config.optimization_level
if optimization_level is None:
pass_manager_config.optimization_level = 0

# Choose an initial layout if not set by user (default: trivial layout)
_given_layout = SetLayout(initial_layout)
Expand Down Expand Up @@ -123,7 +118,7 @@ def _choose_layout_condition(property_set):
)
else:
routing_pm = plugin_manager.get_passmanager_stage(
"routing", routing_method, pass_manager_config
"routing", routing_method, pass_manager_config, optimization_level=0
)

unroll_3q = None
Expand All @@ -138,7 +133,7 @@ def _choose_layout_condition(property_set):
)
if layout_method not in {"trivial", "dense", "noise_adaptive", "sabre"}:
layout = plugin_manager.get_passmanager_stage(
"layout", layout_method, pass_manager_config
"layout", layout_method, pass_manager_config, optimization_level=0
)
else:
layout = PassManager()
Expand All @@ -151,7 +146,7 @@ def _choose_layout_condition(property_set):
routing = None
if translation_method not in {"translator", "synthesis", "unroller"}:
translation = plugin_manager.get_passmanager_stage(
"translation", translation_method, pass_manager_config
"translation", translation_method, pass_manager_config, optimization_level=0
)
else:
translation = common.generate_translation_passmanager(
Expand Down Expand Up @@ -181,21 +176,20 @@ def _choose_layout_condition(property_set):
)
else:
sched = plugin_manager.get_passmanager_stage(
"scheduling", scheduling_method, pass_manager_config
"scheduling", scheduling_method, pass_manager_config, optimization_level=0
)
if init_method is not None:
init = plugin_manager.get_passmanager_stage("init", init_method, pass_manager_config)
init = plugin_manager.get_passmanager_stage(
"init", init_method, pass_manager_config, optimization_level=0
)
else:
init = unroll_3q
optimization = None
if optimization_method is not None:
optimization = plugin_manager.get_passmanager_stage(
"optimization", optimization_method, pass_manager_config
"optimization", optimization_method, pass_manager_config, optimization_level=0
)

# Restore PassManagerConfig optimization_level override
pass_manager_config.optimization_level = optimization_level

return StagedPassManager(
init=init,
layout=layout,
Expand Down
24 changes: 11 additions & 13 deletions qiskit/transpiler/preset_passmanagers/level1.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ def level_1_pass_manager(pass_manager_config: PassManagerConfig) -> StagedPassMa
unitary_synthesis_plugin_config = pass_manager_config.unitary_synthesis_plugin_config
timing_constraints = pass_manager_config.timing_constraints or TimingConstraints()
target = pass_manager_config.target
# Override an unset optimization_level for stage plugin use.
# it will be restored to None before this is returned
optimization_level = pass_manager_config.optimization_level
if optimization_level is None:
pass_manager_config.optimization_level = 1

# Use trivial layout if no layout given
_given_layout = SetLayout(initial_layout)
Expand Down Expand Up @@ -187,7 +182,10 @@ def _vf2_match_not_found(property_set):
)
else:
routing_pm = plugin_manager.get_passmanager_stage(
"routing", routing_method, pass_manager_config
"routing",
routing_method,
pass_manager_config,
optimization_level=1,
)

# Build optimization loop: merge 1q rotations and cancel CNOT gates iteratively
Expand All @@ -212,7 +210,7 @@ def _opt_control(property_set):
)
if layout_method not in {"trivial", "dense", "noise_adaptive", "sabre"}:
layout = plugin_manager.get_passmanager_stage(
"layout", layout_method, pass_manager_config
"layout", layout_method, pass_manager_config, optimization_level=1
)
else:
layout = PassManager()
Expand All @@ -229,7 +227,7 @@ def _opt_control(property_set):
routing = None
if translation_method not in {"translator", "synthesis", "unroller"}:
translation = plugin_manager.get_passmanager_stage(
"translation", translation_method, pass_manager_config
"translation", translation_method, pass_manager_config, optimization_level=1
)
else:
translation = common.generate_translation_passmanager(
Expand Down Expand Up @@ -262,22 +260,22 @@ def _opt_control(property_set):
optimization.append(opt_loop, do_while=_opt_control)
else:
optimization = plugin_manager.get_passmanager_stage(
"optimization", optimization_method, pass_manager_config
"optimization", optimization_method, pass_manager_config, optimization_level=1
)
if scheduling_method is None or scheduling_method in {"alap", "asap"}:
sched = common.generate_scheduling(
instruction_durations, scheduling_method, timing_constraints, inst_map
)
else:
sched = plugin_manager.get_passmanager_stage(
"scheduling", scheduling_method, pass_manager_config
"scheduling", scheduling_method, pass_manager_config, optimization_level=1
)
if init_method is not None:
init = plugin_manager.get_passmanager_stage("init", init_method, pass_manager_config)
init = plugin_manager.get_passmanager_stage(
"init", init_method, pass_manager_config, optimization_level=1
)
else:
init = unroll_3q
# Restore PassManagerConfig optimization_level override
pass_manager_config.optimization_level = optimization_level

return StagedPassManager(
init=init,
Expand Down
Loading

0 comments on commit d731d06

Please sign in to comment.