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

Fix CheckMap pass target handling #9929

Merged
merged 1 commit into from
Apr 11, 2023
Merged
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
27 changes: 11 additions & 16 deletions qiskit/transpiler/passes/utils/check_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"""Check if a DAG circuit is already mapped to a coupling map."""

from qiskit.transpiler.basepasses import AnalysisPass
from qiskit.transpiler.target import Target
from qiskit.circuit.controlflow import ControlFlowOp


Expand All @@ -27,30 +28,24 @@ class CheckMap(AnalysisPass):
for a target use the :class:`~.CheckGateDirection` pass instead.
"""

def __init__(self, coupling_map=None, target=None):
def __init__(self, coupling_map):
"""CheckMap initializer.

Args:
coupling_map (CouplingMap): Directed graph representing a coupling map.
target (Target): A target representing the target backend, if both
``coupling_map`` and this are specified then this argument will take
precedence and ``coupling_map`` will be ignored.
coupling_map (Union[CouplingMap, Target]): Directed graph representing a coupling map.
"""
super().__init__()
if coupling_map is None and target is None:
if isinstance(coupling_map, Target):
cmap = coupling_map.build_coupling_map()
else:
cmap = coupling_map
if cmap is None:
self.qargs = None
else:
self.qargs = set()
if target is not None:
if target.qargs is not None:
for edge in target.qargs:
if len(edge) == 2:
self.qargs.add(edge)
self.qargs.add((edge[1], edge[0]))
else:
for edge in coupling_map.get_edges():
self.qargs.add(edge)
self.qargs.add((edge[1], edge[0]))
for edge in cmap.get_edges():
self.qargs.add(edge)
self.qargs.add((edge[1], edge[0]))

def run(self, dag):
"""Run the CheckMap pass on `dag`.
Expand Down
5 changes: 4 additions & 1 deletion qiskit/transpiler/preset_passmanagers/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,10 @@ def _run_post_layout_condition(property_set):
return False

routing = PassManager()
routing.append(CheckMap(coupling_map, target=target))
if target is not None:
routing.append(CheckMap(target))
else:
routing.append(CheckMap(coupling_map))

def _swap_condition(property_set):
return not property_set["is_swap_mapped"]
Expand Down
2 changes: 1 addition & 1 deletion qiskit/transpiler/preset_passmanagers/level1.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def _vf2_match_not_found(property_set):
_choose_layout_0 = (
[]
if pass_manager_config.layout_method
else [TrivialLayout(coupling_map_layout), CheckMap(coupling_map, target=target)]
else [TrivialLayout(coupling_map_layout), CheckMap(coupling_map_layout)]
)

_choose_layout_1 = (
Expand Down
4 changes: 2 additions & 2 deletions test/python/transpiler/test_check_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_trivial_nop_map_target(self):
circuit.h(qr)
target = Target()
dag = circuit_to_dag(circuit)
pass_ = CheckMap(target=target)
pass_ = CheckMap(target)
pass_.run(dag)
self.assertTrue(pass_.property_set["is_swap_mapped"])

Expand Down Expand Up @@ -120,7 +120,7 @@ def test_swap_mapped_false_target(self):
target.add_instruction(CXGate(), {(0, 2): None, (2, 1): None})
dag = circuit_to_dag(circuit)

pass_ = CheckMap(target=target)
pass_ = CheckMap(target)
pass_.run(dag)

self.assertFalse(pass_.property_set["is_swap_mapped"])
Expand Down
30 changes: 14 additions & 16 deletions test/python/visualization/references/pass_manager_standard.dot
Original file line number Diff line number Diff line change
Expand Up @@ -53,39 +53,37 @@ fontname=helvetica;
label="[5] ";
labeljust=l;
16 [color=red, fontname=helvetica, label=CheckMap, shape=rectangle];
17 [color=black, fontname=helvetica, fontsize=10, label=coupling_map, shape=ellipse, style=dashed];
17 [color=black, fontname=helvetica, fontsize=10, label=coupling_map, shape=ellipse, style=solid];
17 -> 16;
18 [color=black, fontname=helvetica, fontsize=10, label=target, shape=ellipse, style=dashed];
18 -> 16;
12 -> 16;
}

subgraph cluster_19 {
subgraph cluster_18 {
fontname=helvetica;
label="[6] do_while";
labeljust=l;
20 [color=blue, fontname=helvetica, label=BarrierBeforeFinalMeasurements, shape=rectangle];
16 -> 20;
19 [color=blue, fontname=helvetica, label=BarrierBeforeFinalMeasurements, shape=rectangle];
16 -> 19;
}

subgraph cluster_21 {
subgraph cluster_20 {
fontname=helvetica;
label="[7] ";
labeljust=l;
22 [color=blue, fontname=helvetica, label=GateDirection, shape=rectangle];
23 [color=black, fontname=helvetica, fontsize=10, label=coupling_map, shape=ellipse, style=solid];
23 -> 22;
24 [color=black, fontname=helvetica, fontsize=10, label=target, shape=ellipse, style=dashed];
24 -> 22;
20 -> 22;
21 [color=blue, fontname=helvetica, label=GateDirection, shape=rectangle];
22 [color=black, fontname=helvetica, fontsize=10, label=coupling_map, shape=ellipse, style=solid];
22 -> 21;
23 [color=black, fontname=helvetica, fontsize=10, label=target, shape=ellipse, style=dashed];
23 -> 21;
19 -> 21;
}

subgraph cluster_25 {
subgraph cluster_24 {
fontname=helvetica;
label="[8] ";
labeljust=l;
26 [color=blue, fontname=helvetica, label=RemoveResetInZeroState, shape=rectangle];
22 -> 26;
25 [color=blue, fontname=helvetica, label=RemoveResetInZeroState, shape=rectangle];
21 -> 25;
}

}
30 changes: 14 additions & 16 deletions test/python/visualization/references/pass_manager_style.dot
Original file line number Diff line number Diff line change
Expand Up @@ -53,39 +53,37 @@ fontname=helvetica;
label="[5] ";
labeljust=l;
16 [color=green, fontname=helvetica, label=CheckMap, shape=rectangle];
17 [color=black, fontname=helvetica, fontsize=10, label=coupling_map, shape=ellipse, style=dashed];
17 [color=black, fontname=helvetica, fontsize=10, label=coupling_map, shape=ellipse, style=solid];
17 -> 16;
18 [color=black, fontname=helvetica, fontsize=10, label=target, shape=ellipse, style=dashed];
18 -> 16;
12 -> 16;
}

subgraph cluster_19 {
subgraph cluster_18 {
fontname=helvetica;
label="[6] do_while";
labeljust=l;
20 [color=blue, fontname=helvetica, label=BarrierBeforeFinalMeasurements, shape=rectangle];
16 -> 20;
19 [color=blue, fontname=helvetica, label=BarrierBeforeFinalMeasurements, shape=rectangle];
16 -> 19;
}

subgraph cluster_21 {
subgraph cluster_20 {
fontname=helvetica;
label="[7] ";
labeljust=l;
22 [color=blue, fontname=helvetica, label=GateDirection, shape=rectangle];
23 [color=black, fontname=helvetica, fontsize=10, label=coupling_map, shape=ellipse, style=solid];
23 -> 22;
24 [color=black, fontname=helvetica, fontsize=10, label=target, shape=ellipse, style=dashed];
24 -> 22;
20 -> 22;
21 [color=blue, fontname=helvetica, label=GateDirection, shape=rectangle];
22 [color=black, fontname=helvetica, fontsize=10, label=coupling_map, shape=ellipse, style=solid];
22 -> 21;
23 [color=black, fontname=helvetica, fontsize=10, label=target, shape=ellipse, style=dashed];
23 -> 21;
19 -> 21;
}

subgraph cluster_25 {
subgraph cluster_24 {
fontname=helvetica;
label="[8] ";
labeljust=l;
26 [color=grey, fontname=helvetica, label=RemoveResetInZeroState, shape=rectangle];
22 -> 26;
25 [color=grey, fontname=helvetica, label=RemoveResetInZeroState, shape=rectangle];
21 -> 25;
}

}