Skip to content

Commit

Permalink
Rework logic to use the default if larger than CPU_COUNT
Browse files Browse the repository at this point in the history
This commit refactors the logic added in the previous commit to a single
helper function. This reduces the code duplication and makes it easier
to work with. While doing this the logic has been updated so that when
the flag is set and the default number of trials is larger than the
CPU_COUNT we use the default. This means the logic when the flag is set
is to run `max(default_trials, CPU_COUNT)` which should better match
user expectations around the flag.
  • Loading branch information
mtreinish committed Jul 24, 2024
1 parent 64f1bec commit 21ff70f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 49 deletions.
65 changes: 19 additions & 46 deletions qiskit/transpiler/preset_passmanagers/builtin_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,7 @@ def pass_manager(self, pass_manager_config, optimization_level=None) -> PassMana
pass_manager_config.initial_layout,
)
if optimization_level == 0:
if CONFIG.get("sabre_all_threads", None) or os.getenv("QISKIT_SABRE_ALL_THREADS"):
trial_count = CPU_COUNT
else:
trial_count = 5

trial_count = _get_trial_count(5)
routing_pass = SabreSwap(
coupling_map_routing,
heuristic="basic",
Expand All @@ -422,11 +418,7 @@ def pass_manager(self, pass_manager_config, optimization_level=None) -> PassMana
use_barrier_before_measurement=True,
)
if optimization_level == 1:
if CONFIG.get("sabre_all_threads", None) or os.getenv("QISKIT_SABRE_ALL_THREADS"):
trial_count = CPU_COUNT
else:
trial_count = 5

trial_count = _get_trial_count(5)
routing_pass = SabreSwap(
coupling_map_routing,
heuristic="decay",
Expand All @@ -445,10 +437,7 @@ def pass_manager(self, pass_manager_config, optimization_level=None) -> PassMana
use_barrier_before_measurement=True,
)
if optimization_level == 2:
if CONFIG.get("sabre_all_threads", None) or os.getenv("QISKIT_SABRE_ALL_THREADS"):
trial_count = CPU_COUNT
else:
trial_count = 20
trial_count = _get_trial_count(20)

routing_pass = SabreSwap(
coupling_map_routing,
Expand All @@ -467,10 +456,7 @@ def pass_manager(self, pass_manager_config, optimization_level=None) -> PassMana
use_barrier_before_measurement=True,
)
if optimization_level == 3:
if CONFIG.get("sabre_all_threads", None) or os.getenv("QISKIT_SABRE_ALL_THREADS"):
trial_count = CPU_COUNT
else:
trial_count = 20
trial_count = _get_trial_count(20)
routing_pass = SabreSwap(
coupling_map_routing,
heuristic="decay",
Expand Down Expand Up @@ -763,10 +749,7 @@ def _swap_mapped(property_set):
)
layout.append(ConditionalController(choose_layout_1, condition=_layout_not_perfect))

if CONFIG.get("sabre_all_threads", None) or os.getenv("QISKIT_SABRE_ALL_THREADS"):
trial_count = CPU_COUNT
else:
trial_count = 5
trial_count = _get_trial_count(5)

choose_layout_2 = SabreLayout(
coupling_map,
Expand Down Expand Up @@ -800,10 +783,8 @@ def _swap_mapped(property_set):
layout.append(
ConditionalController(choose_layout_0, condition=_choose_layout_condition)
)
if CONFIG.get("sabre_all_threads", None) or os.getenv("QISKIT_SABRE_ALL_THREADS"):
trial_count = CPU_COUNT
else:
trial_count = 20

trial_count = _get_trial_count(20)

choose_layout_1 = SabreLayout(
coupling_map,
Expand Down Expand Up @@ -837,10 +818,8 @@ def _swap_mapped(property_set):
layout.append(
ConditionalController(choose_layout_0, condition=_choose_layout_condition)
)
if CONFIG.get("sabre_all_threads", None) or os.getenv("QISKIT_SABRE_ALL_THREADS"):
trial_count = CPU_COUNT
else:
trial_count = 20

trial_count = _get_trial_count(20)

choose_layout_1 = SabreLayout(
coupling_map,
Expand Down Expand Up @@ -943,10 +922,7 @@ def _swap_mapped(property_set):
layout = PassManager()
layout.append(_given_layout)
if optimization_level == 0:
if CONFIG.get("sabre_all_threads", None) or os.getenv("QISKIT_SABRE_ALL_THREADS"):
trial_count = CPU_COUNT
else:
trial_count = 5
trial_count = _get_trial_count(5)

layout_pass = SabreLayout(
coupling_map,
Expand All @@ -958,10 +934,7 @@ def _swap_mapped(property_set):
and pass_manager_config.routing_method != "sabre",
)
elif optimization_level == 1:
if CONFIG.get("sabre_all_threads", None) or os.getenv("QISKIT_SABRE_ALL_THREADS"):
trial_count = CPU_COUNT
else:
trial_count = 5
trial_count = _get_trial_count(5)

layout_pass = SabreLayout(
coupling_map,
Expand All @@ -973,10 +946,7 @@ def _swap_mapped(property_set):
and pass_manager_config.routing_method != "sabre",
)
elif optimization_level == 2:
if CONFIG.get("sabre_all_threads", None) or os.getenv("QISKIT_SABRE_ALL_THREADS"):
trial_count = CPU_COUNT
else:
trial_count = 20
trial_count = _get_trial_count(20)

layout_pass = SabreLayout(
coupling_map,
Expand All @@ -988,10 +958,7 @@ def _swap_mapped(property_set):
and pass_manager_config.routing_method != "sabre",
)
elif optimization_level == 3:
if CONFIG.get("sabre_all_threads", None) or os.getenv("QISKIT_SABRE_ALL_THREADS"):
trial_count = CPU_COUNT
else:
trial_count = 20
trial_count = _get_trial_count(20)

layout_pass = SabreLayout(
coupling_map,
Expand All @@ -1018,3 +985,9 @@ def _swap_mapped(property_set):
embed = common.generate_embed_passmanager(coupling_map)
layout.append(ConditionalController(embed.to_flow_controller(), condition=_swap_mapped))
return layout


def _get_trial_count(default_trials=5):
if CONFIG.get("sabre_all_threads", None) or os.getenv("QISKIT_SABRE_ALL_THREADS"):
return max(CPU_COUNT, default_trials)
return default_trials
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ features_transpiler:
local CPUs available the results would different when running between
different computers.
It is not recommended you use this option if your local computer has
less than 20 CPUs as this will result in potentially degraded results
compared to the default.
If the default number of trials for a given optimization level is higher
than the number of local CPUs it will use the optimization level default
which is higher.

0 comments on commit 21ff70f

Please sign in to comment.