From 4be9cb09cca4c48232ad739f7e28bf4728aa1081 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Fri, 7 Apr 2023 10:31:19 -0400 Subject: [PATCH] Fix CheckMap pass target handling In the recently merged 5672c70649b4f83a6c7146c68a6a543d4a3908fd the CheckMap transpiler pass had issues when handling some Target inputs (which was added as a supported type in that PR). These issues were actually pointed out in code review during #9263 but the follow up slipped through. This commit makes the required fixes and also updates the signature of the CheckMap to match the other changes made in #9263. --- qiskit/transpiler/passes/utils/check_map.py | 27 +++++++---------- .../transpiler/preset_passmanagers/common.py | 5 +++- .../transpiler/preset_passmanagers/level1.py | 2 +- test/python/transpiler/test_check_map.py | 4 +-- .../references/pass_manager_standard.dot | 30 +++++++++---------- .../references/pass_manager_style.dot | 30 +++++++++---------- 6 files changed, 46 insertions(+), 52 deletions(-) diff --git a/qiskit/transpiler/passes/utils/check_map.py b/qiskit/transpiler/passes/utils/check_map.py index f014bdb78203..69bc9707d318 100644 --- a/qiskit/transpiler/passes/utils/check_map.py +++ b/qiskit/transpiler/passes/utils/check_map.py @@ -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 @@ -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`. diff --git a/qiskit/transpiler/preset_passmanagers/common.py b/qiskit/transpiler/preset_passmanagers/common.py index 04a1347a761a..a340aee45d8d 100644 --- a/qiskit/transpiler/preset_passmanagers/common.py +++ b/qiskit/transpiler/preset_passmanagers/common.py @@ -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"] diff --git a/qiskit/transpiler/preset_passmanagers/level1.py b/qiskit/transpiler/preset_passmanagers/level1.py index b5d435c9b6e3..0a1b857691b1 100644 --- a/qiskit/transpiler/preset_passmanagers/level1.py +++ b/qiskit/transpiler/preset_passmanagers/level1.py @@ -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 = ( diff --git a/test/python/transpiler/test_check_map.py b/test/python/transpiler/test_check_map.py index 329eb7edc7cc..f5283b240366 100644 --- a/test/python/transpiler/test_check_map.py +++ b/test/python/transpiler/test_check_map.py @@ -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"]) @@ -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"]) diff --git a/test/python/visualization/references/pass_manager_standard.dot b/test/python/visualization/references/pass_manager_standard.dot index c3af9aaa37af..d4b1d30422d5 100644 --- a/test/python/visualization/references/pass_manager_standard.dot +++ b/test/python/visualization/references/pass_manager_standard.dot @@ -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; } } diff --git a/test/python/visualization/references/pass_manager_style.dot b/test/python/visualization/references/pass_manager_style.dot index 54815259b050..5cfd2a95ea56 100644 --- a/test/python/visualization/references/pass_manager_style.dot +++ b/test/python/visualization/references/pass_manager_style.dot @@ -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; } }