Skip to content

Commit

Permalink
Add back initial layout to level1
Browse files Browse the repository at this point in the history
Since there seems to be a pretty baked in asumption for level 1 that it
will use the trivial layout by default if it's a perfect mapping. This
was causing the majority of the test failures and might be an unexpected
breakage for people. However, in a future release we should remove this
(likely when vf2layout is made noise aware). To anticipate this a
FutureWarning is emitted when a trivial layout is used to indicate that
this behavior will change in the future for level 1 and if you're
relying on it you should explicitly set the layout_method='trivial'.
  • Loading branch information
mtreinish committed Nov 3, 2021
1 parent 1ea2322 commit 890aeb8
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions qiskit/transpiler/preset_passmanagers/level1.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
Level 1 pass manager: light optimization by simple adjacent gate collapsing.
"""

import warnings

from qiskit.transpiler.passmanager_config import PassManagerConfig
from qiskit.transpiler.timing_constraints import TimingConstraints
from qiskit.transpiler.passmanager import PassManager
Expand All @@ -33,6 +35,7 @@
from qiskit.transpiler.passes import NoiseAdaptiveLayout
from qiskit.transpiler.passes import SabreLayout
from qiskit.transpiler.passes import BarrierBeforeFinalMeasurements
from qiskit.transpiler.passes import Layout2qDistance
from qiskit.transpiler.passes import BasicSwap
from qiskit.transpiler.passes import LookaheadSwap
from qiskit.transpiler.passes import StochasticSwap
Expand Down Expand Up @@ -106,6 +109,24 @@ def level_1_pass_manager(pass_manager_config: PassManagerConfig) -> PassManager:
def _choose_layout_condition(property_set):
return not property_set["layout"]

def _trivial_not_perfect(property_set):
# Verify that a trivial layout is perfect. If trivial_layout_score > 0
# the layout is not perfect. The layout is unconditionally set by trivial
# layout so we need to clear it before contuing.
if property_set["trivial_layout_score"] is not None:
if property_set["trivial_layout_score"] != 0:
property_set["layout"]._wrapped = None
return True
warnings.warn(
"The current implicit default of using a trivial layout if it's a "
"perfect layout will change in a future release, if you're "
"depending on this behavior it is better to explicit set "
"layout_method='trivial' when calling transpile()",
FutureWarning,
stacklevel=4, # stack_level=4 to target caller of transpile()
)
return False

def _vf2_match_not_found(property_set):
# If a layout hasn't been set by the time we run vf2 layout we need to
# run layout
Expand All @@ -125,6 +146,15 @@ def _vf2_match_not_found(property_set):
vf2_seed = -1

_choose_layout_0 = (
[]
if pass_manager_config.layout_method
else [
TrivialLayout(coupling_map),
Layout2qDistance(coupling_map, property_name="trivial_layout_score"),
]
)

_choose_layout_1 = (
[] if pass_manager_config.layout_method else VF2Layout(coupling_map, seed=vf2_seed)
)

Expand Down Expand Up @@ -286,6 +316,7 @@ def _contains_delay(property_set):
pm1.append(_unroll3q)
pm1.append(_given_layout)
pm1.append(_choose_layout_0, condition=_choose_layout_condition)
pm1.append(_choose_layout_1, condition=_trivial_not_perfect)
pm1.append(_improve_layout, condition=_vf2_match_not_found)
pm1.append(_embed)
pm1.append(_swap_check)
Expand Down

0 comments on commit 890aeb8

Please sign in to comment.