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

Add max_trials parameter to VF2PostLayout. #9963

Merged
merged 5 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion qiskit/transpiler/passes/layout/vf2_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def mapping_to_layout(layout_mapping):
)
chosen_layout = layout
chosen_layout_score = layout_score
if self.max_trials is not None and self.max_trials > 0 and trials >= self.max_trials:
if self.max_trials and trials >= self.max_trials:
kevinhartman marked this conversation as resolved.
Show resolved Hide resolved
logger.debug("Trial %s is >= configured max trials %s", trials, self.max_trials)
break
elapsed_time = time.time() - start_time
Expand Down
9 changes: 9 additions & 0 deletions qiskit/transpiler/passes/layout/vf2_post_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def __init__(
call_limit=None,
time_limit=None,
strict_direction=True,
max_trials=0,
):
"""Initialize a ``VF2PostLayout`` pass instance

Expand All @@ -131,6 +132,8 @@ def __init__(
However, if ``strict_direction=True`` the pass expects the input
:class:`~.DAGCircuit` object to :meth:`~.VF2PostLayout.run` to be in
the target set of instructions.
max_trials (int): The maximum number of trials to run VF2 to find
a layout. A value of ``0`` (the default) means 'unlimited'.

Raises:
TypeError: At runtime, if neither ``coupling_map`` or ``target`` are provided.
Expand All @@ -141,6 +144,7 @@ def __init__(
self.properties = properties
self.call_limit = call_limit
self.time_limit = time_limit
self.max_trials = max_trials
self.seed = seed
self.strict_direction = strict_direction
self.avg_error_map = None
Expand Down Expand Up @@ -310,6 +314,11 @@ def run(self, dag):
)
chosen_layout = layout
chosen_layout_score = layout_score

if self.max_trials and trials >= self.max_trials:
logger.debug("Trial %s is >= configured max trials %s", trials, self.max_trials)
break

elapsed_time = time.time() - start_time
if self.time_limit is not None and elapsed_time >= self.time_limit:
logger.debug(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
features:
- |
Added a new parameter ``max_trials`` to pass :class:`~.VF2PostLayout`
which, when specified,limits the number of trials performed when
searching for the best layout. Previously, all possible layouts were
always considered, which is not suitable when performing layout of
multiple connected components on large devices.
mtreinish marked this conversation as resolved.
Show resolved Hide resolved
33 changes: 33 additions & 0 deletions test/python/transpiler/test_vf2_post_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,39 @@ def test_2q_circuit_5q_backend_controlflow(self):
self.assertLayout(dag, cmap, pass_.property_set)
self.assertNotEqual(pass_.property_set["post_layout"], initial_layout)

def test_2q_circuit_5q_backend_max_trials(self):
"""A simple example, without considering the direction
0 - 1
qr1 - qr0
"""
max_trials = 11
backend = FakeYorktown()

qr = QuantumRegister(2, "qr")
circuit = QuantumCircuit(qr)
circuit.cx(qr[1], qr[0]) # qr1 -> qr0
tqc = transpile(circuit, backend, layout_method="dense")
initial_layout = tqc._layout
dag = circuit_to_dag(tqc)
cmap = CouplingMap(backend.configuration().coupling_map)
props = backend.properties()
pass_ = VF2PostLayout(
coupling_map=cmap, properties=props, seed=self.seed, max_trials=max_trials
)

with self.assertLogs(
"qiskit.transpiler.passes.layout.vf2_post_layout", level="DEBUG"
) as cm:
pass_.run(dag)
self.assertIn(
f"DEBUG:qiskit.transpiler.passes.layout.vf2_post_layout:Trial {max_trials} "
mtreinish marked this conversation as resolved.
Show resolved Hide resolved
f"is >= configured max trials {max_trials}",
cm.output,
)

self.assertLayout(dag, cmap, pass_.property_set)
self.assertNotEqual(pass_.property_set["post_layout"], initial_layout)

def test_best_mapping_ghz_state_full_device_multiple_qregs_v2(self):
"""Test best mappings with multiple registers"""
backend = FakeLimaV2()
Expand Down