Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop shuffling coupling map node indices in VF2 passes #13492

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 19 additions & 22 deletions qiskit/transpiler/preset_passmanagers/builtin_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ class BasicSwapPassManager(PassManagerStagePlugin):

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
coupling_map = pass_manager_config.coupling_map
backend_properties = pass_manager_config.backend_properties
Expand All @@ -257,7 +256,7 @@ def pass_manager(self, pass_manager_config, optimization_level=None) -> PassMana
routing_pass,
target,
coupling_map=coupling_map,
seed_transpiler=seed_transpiler,
seed_transpiler=-1,
use_barrier_before_measurement=True,
)
if optimization_level == 1:
Expand All @@ -268,7 +267,7 @@ def pass_manager(self, pass_manager_config, optimization_level=None) -> PassMana
vf2_call_limit=vf2_call_limit,
vf2_max_trials=vf2_max_trials,
backend_properties=backend_properties,
seed_transpiler=seed_transpiler,
seed_transpiler=-1,
check_trivial=True,
use_barrier_before_measurement=True,
)
Expand All @@ -280,7 +279,7 @@ def pass_manager(self, pass_manager_config, optimization_level=None) -> PassMana
vf2_call_limit=vf2_call_limit,
vf2_max_trials=vf2_max_trials,
backend_properties=backend_properties,
seed_transpiler=seed_transpiler,
seed_transpiler=-1,
use_barrier_before_measurement=True,
)
if optimization_level == 3:
Expand All @@ -291,7 +290,7 @@ def pass_manager(self, pass_manager_config, optimization_level=None) -> PassMana
vf2_call_limit=vf2_call_limit,
vf2_max_trials=vf2_max_trials,
backend_properties=backend_properties,
seed_transpiler=seed_transpiler,
seed_transpiler=-1,
use_barrier_before_measurement=True,
)
raise TranspilerError(f"Invalid optimization level specified: {optimization_level}")
Expand Down Expand Up @@ -324,7 +323,7 @@ def pass_manager(self, pass_manager_config, optimization_level=None) -> PassMana
routing_pass,
target,
coupling_map=coupling_map,
seed_transpiler=seed_transpiler,
seed_transpiler=-1,
use_barrier_before_measurement=True,
)
if optimization_level == 1:
Expand All @@ -335,7 +334,7 @@ def pass_manager(self, pass_manager_config, optimization_level=None) -> PassMana
vf2_call_limit=vf2_call_limit,
vf2_max_trials=vf2_max_trials,
backend_properties=backend_properties,
seed_transpiler=seed_transpiler,
seed_transpiler=-1,
check_trivial=True,
use_barrier_before_measurement=True,
)
Expand All @@ -347,7 +346,7 @@ def pass_manager(self, pass_manager_config, optimization_level=None) -> PassMana
vf2_call_limit=vf2_call_limit,
vf2_max_trials=vf2_max_trials,
backend_properties=backend_properties,
seed_transpiler=seed_transpiler,
seed_transpiler=-1,
use_barrier_before_measurement=True,
)
raise TranspilerError(f"Invalid optimization level specified: {optimization_level}")
Expand All @@ -358,7 +357,6 @@ class LookaheadSwapPassManager(PassManagerStagePlugin):

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
coupling_map = pass_manager_config.coupling_map
coupling_map_routing = target
Expand All @@ -376,7 +374,7 @@ def pass_manager(self, pass_manager_config, optimization_level=None) -> PassMana
routing_pass,
target,
coupling_map=coupling_map,
seed_transpiler=seed_transpiler,
seed_transpiler=-1,
use_barrier_before_measurement=True,
)
if optimization_level == 1:
Expand All @@ -388,7 +386,7 @@ def pass_manager(self, pass_manager_config, optimization_level=None) -> PassMana
vf2_call_limit=vf2_call_limit,
vf2_max_trials=vf2_max_trials,
backend_properties=backend_properties,
seed_transpiler=seed_transpiler,
seed_transpiler=-1,
check_trivial=True,
use_barrier_before_measurement=True,
)
Expand All @@ -401,7 +399,7 @@ def pass_manager(self, pass_manager_config, optimization_level=None) -> PassMana
vf2_call_limit=vf2_call_limit,
vf2_max_trials=vf2_max_trials,
backend_properties=backend_properties,
seed_transpiler=seed_transpiler,
seed_transpiler=-1,
use_barrier_before_measurement=True,
)
if optimization_level == 3:
Expand All @@ -413,7 +411,7 @@ def pass_manager(self, pass_manager_config, optimization_level=None) -> PassMana
vf2_call_limit=vf2_call_limit,
vf2_max_trials=vf2_max_trials,
backend_properties=backend_properties,
seed_transpiler=seed_transpiler,
seed_transpiler=-1,
use_barrier_before_measurement=True,
)
raise TranspilerError(f"Invalid optimization level specified: {optimization_level}")
Expand Down Expand Up @@ -448,7 +446,7 @@ def pass_manager(self, pass_manager_config, optimization_level=None) -> PassMana
routing_pass,
target,
coupling_map=coupling_map,
seed_transpiler=seed_transpiler,
seed_transpiler=-1,
use_barrier_before_measurement=True,
)
if optimization_level == 1:
Expand All @@ -466,7 +464,7 @@ def pass_manager(self, pass_manager_config, optimization_level=None) -> PassMana
vf2_call_limit=vf2_call_limit,
vf2_max_trials=vf2_max_trials,
backend_properties=backend_properties,
seed_transpiler=seed_transpiler,
seed_transpiler=-1,
check_trivial=True,
use_barrier_before_measurement=True,
)
Expand All @@ -486,7 +484,7 @@ def pass_manager(self, pass_manager_config, optimization_level=None) -> PassMana
vf2_call_limit=vf2_call_limit,
vf2_max_trials=vf2_max_trials,
backend_properties=backend_properties,
seed_transpiler=seed_transpiler,
seed_transpiler=-1,
use_barrier_before_measurement=True,
)
if optimization_level == 3:
Expand All @@ -504,7 +502,7 @@ def pass_manager(self, pass_manager_config, optimization_level=None) -> PassMana
vf2_call_limit=vf2_call_limit,
vf2_max_trials=vf2_max_trials,
backend_properties=backend_properties,
seed_transpiler=seed_transpiler,
seed_transpiler=-1,
use_barrier_before_measurement=True,
)
raise TranspilerError(f"Invalid optimization level specified: {optimization_level}")
Expand All @@ -515,7 +513,6 @@ class NoneRoutingPassManager(PassManagerStagePlugin):

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
coupling_map = pass_manager_config.coupling_map
routing_pass = Error(
Expand All @@ -527,7 +524,7 @@ def pass_manager(self, pass_manager_config, optimization_level=None) -> PassMana
routing_pass,
target,
coupling_map=coupling_map,
seed_transpiler=seed_transpiler,
seed_transpiler=-1,
use_barrier_before_measurement=True,
)

Expand Down Expand Up @@ -793,7 +790,7 @@ def _swap_mapped(property_set):
)
choose_layout_1 = VF2Layout(
coupling_map=pass_manager_config.coupling_map,
seed=pass_manager_config.seed_transpiler,
seed=-1,
call_limit=int(5e4), # Set call limit to ~100ms with rustworkx 0.10.2
properties=pass_manager_config.backend_properties,
target=pass_manager_config.target,
Expand Down Expand Up @@ -826,7 +823,7 @@ def _swap_mapped(property_set):
elif optimization_level == 2:
choose_layout_0 = VF2Layout(
coupling_map=pass_manager_config.coupling_map,
seed=pass_manager_config.seed_transpiler,
seed=-1,
call_limit=int(5e6), # Set call limit to ~10s with rustworkx 0.10.2
properties=pass_manager_config.backend_properties,
target=pass_manager_config.target,
Expand Down Expand Up @@ -861,7 +858,7 @@ def _swap_mapped(property_set):
elif optimization_level == 3:
choose_layout_0 = VF2Layout(
coupling_map=pass_manager_config.coupling_map,
seed=pass_manager_config.seed_transpiler,
seed=-1,
call_limit=int(3e7), # Set call limit to ~60s with rustworkx 0.10.2
properties=pass_manager_config.backend_properties,
target=pass_manager_config.target,
Expand Down
9 changes: 6 additions & 3 deletions qiskit/transpiler/preset_passmanagers/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def generate_routing_passmanager(
coupling_map=None,
vf2_call_limit=None,
backend_properties=None,
seed_transpiler=None,
seed_transpiler=-1,
check_trivial=False,
use_barrier_before_measurement=True,
vf2_max_trials=None,
Expand All @@ -300,7 +300,10 @@ def generate_routing_passmanager(
backend_properties (BackendProperties): Properties of a backend to
synthesize for (e.g. gate fidelities).
seed_transpiler (int): Sets random seed for the stochastic parts of
the transpiler.
the transpiler. This is currently only used for :class:`.VF2PostLayout` and the
default value of ``-1`` is strongly recommended (which is no randomization).
If a value of ``None`` is provided this will seed from system
entropy.
check_trivial (bool): If set to true this will condition running the
:class:`~.VF2PostLayout` pass after routing on whether a trivial
layout was tried and was found to not be perfect. This is only
Expand Down Expand Up @@ -358,7 +361,7 @@ def _swap_condition(property_set):
target,
coupling_map,
backend_properties,
seed_transpiler,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lint caught that the seed_transpiler argument of generate_routing_passmanager is not being used anymore. We should probably not have it as an input argument if it's not going to be used, but I feel like this might make the change more... permanent? We at least should make sure we want to stick to the fixed seed. On the other hand, I've checked and it looks like generate_routing_passmanager is not part of the public API, so technically we could remove the argument without deprecation, but I see this as another argument to leave this PR for 2.0.

Copy link
Member Author

@mtreinish mtreinish Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is unfortunately a public function: https://docs.quantum.ibm.com/api/qiskit/transpiler_preset#generate_routing_passmanager so we can't just change it. We could in 2.0 with an appropriate deprecation added in 1.4. Personally for this case if we were to include it in 1.3 I'd just disable the lint with a comment about preserving the signature.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opted to restore this argument's functionality and instead change the default to -1 to not use any randomization. Since we're in 2.0 I figured a change in default was the least bad option here. I'm still not convinced there is a use case where the randomization is actually helping because in all the testing I've done it only just makes it harder for vf2 to find matches, but I can understand people wanting to experiment with this and it's why I didn't remove it from the pass itself.

seed=seed_transpiler,
call_limit=vf2_call_limit,
max_trials=vf2_max_trials,
strict_direction=False,
Expand Down
12 changes: 12 additions & 0 deletions releasenotes/notes/vf2-order-3ef2b4ca5ebd0588.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
upgrade_transpiler:
- |
The default value for the :func:`.generate_routing_passmanager` argument
``seed_transpiler`` has changed from ``None`` to ``-1``. This was done
because this flag was only used to configure the :class:`.VF2PostLayout`
transpiler pass in the output, and for that pass in particular the
randomization typically only hurts performance and is not desirable.
If you were relying on the previous default value you can restore this
behavior by explicitly setting the argument ``seed_transpiler=None``. If
you were explicitly setting a seed value for this parameter there is no
change in behavior.
2 changes: 1 addition & 1 deletion test/python/primitives/test_backend_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ def test_layout(self, backend):
estimator.set_transpile_options(seed_transpiler=15, optimization_level=1)
value = estimator.run(qc, op, shots=10000).result().values[0]
if optionals.HAS_AER:
ref_value = -0.9954 if isinstance(backend, GenericBackendV2) else -0.916
ref_value = -0.9954 if isinstance(backend, GenericBackendV2) else -0.934
else:
ref_value = -1
self.assertEqual(value, ref_value)
Expand Down
Loading