From 302320d637baf1f1012b495c5c58244620ad864a Mon Sep 17 00:00:00 2001 From: Erick Winston Date: Fri, 30 Sep 2022 04:32:39 -0400 Subject: [PATCH] merged main and renabled checkmap tests --- qiskit/transpiler/passes/routing/utils.py | 2 +- qiskit/transpiler/passes/utils/check_map.py | 6 +-- .../python/transpiler/test_stochastic_swap.py | 49 ++++++++++--------- 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/qiskit/transpiler/passes/routing/utils.py b/qiskit/transpiler/passes/routing/utils.py index 0064c279a10c..ebbdff50a6c2 100644 --- a/qiskit/transpiler/passes/routing/utils.py +++ b/qiskit/transpiler/passes/routing/utils.py @@ -57,7 +57,7 @@ def route_cf_multiblock(tpass, cf_opnode, current_layout, qregs, root_dag, seed= updated_dag_block = tpass.run(full_dag_block) block_dags.append(updated_dag_block) block_layouts.append(tpass.property_set["final_layout"].copy()) - depth_cnt = [bdag.depth() for bdag in block_dags] + depth_cnt = [bdag.depth(recurse=True) for bdag in block_dags] maxind = np.argmax(depth_cnt) block_circuits = [None] * len(block_layouts) p2v = current_layout._p2v diff --git a/qiskit/transpiler/passes/utils/check_map.py b/qiskit/transpiler/passes/utils/check_map.py index 3e764db42961..bd4bcb3500b3 100644 --- a/qiskit/transpiler/passes/utils/check_map.py +++ b/qiskit/transpiler/passes/utils/check_map.py @@ -52,9 +52,9 @@ def run(self, dag): qubit_indices = {bit: index for index, bit in enumerate(dag.qubits)} # Use dist matrix directly to avoid validation overhead dist_matrix = self.coupling_map.distance_matrix - + cf_ops = dag.op_nodes(op=ControlFlowOp) for gate in dag.two_qubit_ops(): - if dag.has_calibration_for(gate): + if dag.has_calibration_for(gate) or gate in cf_ops: continue physical_q0 = qubit_indices[gate.qargs[0]] physical_q1 = qubit_indices[gate.qargs[1]] @@ -67,7 +67,7 @@ def run(self, dag): ) self.property_set["is_swap_mapped"] = False return - for cf_instr in dag.op_nodes(op=ControlFlowOp): + for cf_instr in cf_ops: order = [qubit_indices[bit] for bit in cf_instr.qargs] for block in cf_instr.op.blocks: dag_block = circuit_to_dag(block) diff --git a/test/python/transpiler/test_stochastic_swap.py b/test/python/transpiler/test_stochastic_swap.py index b336aa39e673..ff919f500bc8 100644 --- a/test/python/transpiler/test_stochastic_swap.py +++ b/test/python/transpiler/test_stochastic_swap.py @@ -606,9 +606,9 @@ def test_controlflow_pre_if_else_route(self): expected.barrier(qreg) expected.measure(qreg, creg[new_order]) self.assertEqual(cqc, expected) - # check_map_pass = CheckMap(coupling) - # check_map_pass.run(circuit_to_dag(expected)) - # self.assertTrue(check_map_pass.property_set["is_swap_mapped"]) + check_map_pass = CheckMap(coupling) + check_map_pass.run(circuit_to_dag(expected)) + self.assertTrue(check_map_pass.property_set["is_swap_mapped"]) def test_controlflow_pre_if_else_route_post_x(self): """test swap with if else controlflow construct; pre-cx and post x""" @@ -650,9 +650,9 @@ def test_controlflow_pre_if_else_route_post_x(self): expected.measure(qreg, creg[new_order]) self.assertEqual(cqc, expected) - # check_map_pass = CheckMap(coupling) - # check_map_pass.run(circuit_to_dag(expected)) - # self.assertTrue(check_map_pass.property_set["is_swap_mapped"]) + check_map_pass = CheckMap(coupling) + check_map_pass.run(circuit_to_dag(expected)) + self.assertTrue(check_map_pass.property_set["is_swap_mapped"]) def test_controlflow_post_if_else_route(self): """test swap with if else controlflow construct; post cx""" @@ -694,9 +694,9 @@ def test_controlflow_post_if_else_route(self): expected.barrier(qreg) expected.measure(qreg, creg[[1, 0, 2, 3, 4]]) self.assertEqual(cqc, expected) - # check_map_pass = CheckMap(coupling) - # check_map_pass.run(circuit_to_dag(expected)) - # self.assertTrue(check_map_pass.property_set["is_swap_mapped"]) + check_map_pass = CheckMap(coupling) + check_map_pass.run(circuit_to_dag(expected)) + self.assertTrue(check_map_pass.property_set["is_swap_mapped"]) def test_controlflow_pre_if_else2(self): """test swap with if else controlflow construct; cx in if statement""" @@ -736,9 +736,9 @@ def test_controlflow_pre_if_else2(self): expected.measure(qreg, creg[new_order]) self.assertEqual(cqc, expected) - # check_map_pass = CheckMap(coupling) - # check_map_pass.run(circuit_to_dag(expected)) - # self.assertTrue(check_map_pass.property_set["is_swap_mapped"]) + check_map_pass = CheckMap(coupling) + check_map_pass.run(circuit_to_dag(expected)) + self.assertTrue(check_map_pass.property_set["is_swap_mapped"]) def test_controlflow_intra_if_else_route(self): """test swap with if else controlflow construct""" @@ -779,9 +779,9 @@ def test_controlflow_intra_if_else_route(self): new_order = [1, 2, 0, 4, 3] expected.measure(qreg, creg[new_order]) self.assertEqual(cqc, expected) - # check_map_pass = CheckMap(coupling) - # check_map_pass.run(circuit_to_dag(expected)) - # self.assertTrue(check_map_pass.property_set["is_swap_mapped"]) + check_map_pass = CheckMap(coupling) + check_map_pass.run(circuit_to_dag(expected)) + self.assertTrue(check_map_pass.property_set["is_swap_mapped"]) @data(1, 2) def test_controlflow_pre_intra_if_else(self, seed): @@ -851,9 +851,9 @@ def test_controlflow_pre_intra_if_else(self, seed): raise ValueError(f"unsupported test seed={seed}") self.assertEqual(cqc, expected) - # check_map_pass = CheckMap(coupling) - # check_map_pass.run(circuit_to_dag(expected)) - # self.assertTrue(check_map_pass.property_set["is_swap_mapped"]) + check_map_pass = CheckMap(coupling) + check_map_pass.run(circuit_to_dag(expected)) + self.assertTrue(check_map_pass.property_set["is_swap_mapped"]) def test_controlflow_pre_intra_post_if_else(self): """test swap with if else controlflow construct; cx before, in, and after if @@ -906,9 +906,9 @@ def test_controlflow_pre_intra_post_if_else(self): expected.measure(qreg, creg[[2, 4, 0, 3, 1]]) self.assertEqual(cqc, expected) - # check_map_pass = CheckMap(coupling) - # check_map_pass.run(circuit_to_dag(expected)) - # self.assertTrue(check_map_pass.property_set["is_swap_mapped"]) + check_map_pass = CheckMap(coupling) + check_map_pass.run(circuit_to_dag(expected)) + self.assertTrue(check_map_pass.property_set["is_swap_mapped"]) def test_controlflow_no_layout_change(self): """test controlflow with no layout change needed""" @@ -947,10 +947,11 @@ def test_controlflow_no_layout_change(self): expected.if_else((creg[0], 0), etrue_body, efalse_body, qreg[[1, 4]], creg) expected.barrier(qreg) expected.measure(qreg, creg[[0, 2, 1, 3, 4]]) + self.assertEqual(cqc, expected) - # check_map_pass = CheckMap(coupling) - # check_map_pass.run(circuit_to_dag(expected)) - # self.assertTrue(check_map_pass.property_set["is_swap_mapped"]) + check_map_pass = CheckMap(coupling) + check_map_pass.run(circuit_to_dag(expected)) + self.assertTrue(check_map_pass.property_set["is_swap_mapped"]) @data(1, 2, 3) def test_controlflow_for_loop(self, nloops):