Skip to content

Commit

Permalink
fix check_map
Browse files Browse the repository at this point in the history
  • Loading branch information
ewinston committed Sep 2, 2022
1 parent ef35278 commit ee78880
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
9 changes: 6 additions & 3 deletions qiskit/transpiler/passes/routing/stochastic_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,9 @@ def _mapper(self, circuit_graph, coupling_graph, trials=20):
cf_layer = False
cf_layout = None
for node in subdag.op_nodes(op=ControlFlowOp):
updated_ctrl_op, cf_layout = self._transpile_controlflow_op(node.op, layout)
updated_ctrl_op, cf_layout = self._transpile_controlflow_op(
node.op, layout, circuit_graph.qubits
)
node.op = updated_ctrl_op
cf_layer = True
if cf_layer:
Expand Down Expand Up @@ -366,9 +368,10 @@ def _mapper(self, circuit_graph, coupling_graph, trials=20):
return circuit_graph
return dagcircuit_output

def _transpile_controlflow_op(self, cf_op, current_layout):
def _transpile_controlflow_op(self, cf_op, current_layout, qubits):
"""handle controlflow ops by type"""
new_coupling = layout_transform(self.coupling_map, current_layout)
new_coupling = layout_transform(self.coupling_map, current_layout, qubits)
breakpoint()
_pass = self.__class__(new_coupling, initial_layout=None, seed=self.seed)
if isinstance(cf_op, IfElseOp):
return transpile_cf_multiblock(
Expand Down
15 changes: 5 additions & 10 deletions qiskit/transpiler/passes/routing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from qiskit.transpiler.coupling import CouplingMap
from qiskit.transpiler.layout import Layout
from qiskit.circuit import QuantumRegister
from qiskit.circuit.exceptions import CircuitError


def transpile_cf_multiblock(tpass, cf_op, current_layout, coupling, qregs, seed=None):
Expand Down Expand Up @@ -50,9 +49,6 @@ def transpile_cf_multiblock(tpass, cf_op, current_layout, coupling, qregs, seed=
updated_dag_block = tpass.run(dag_block)
block_dags.append(updated_dag_block)
block_layouts.append(tpass.property_set["final_layout"].copy())
changed_layouts = [current_layout != layout for layout in block_layouts]
# if not any(changed_layouts):
# return cf_op, current_layout
depth_cnt = [bdag.depth() for bdag in block_dags]
maxind = np.argmax(depth_cnt)
for i, dag in enumerate(block_dags):
Expand Down Expand Up @@ -84,6 +80,7 @@ def transpile_cf_looping(tpass, cf_op, current_layout, coupling, seed=None):
current_layout (Layout): The current layout at the start and by the
end of the instruction.
coupling (CouplingMap): the coupling map to use within the control flow instruction.
seed (int): seed for RNG of internal layout transformation.
Returns:
tuple(ControlFlowOp, Layout): Transpiled control flow
Expand Down Expand Up @@ -137,22 +134,20 @@ def combine_permutations(*permutations):
return order


def layout_transform(cmap, layout, qreg=None):
def layout_transform(cmap, layout, qubits):
"""Transform coupling map according to layout.
Args:
cmap (CouplingMap): coupling map to transform
layout (Layout): layout to apply
qreg (QuantumRegister): register to use for indexing
layout (Layout): device layout
qubits (list(Qubit)): ordered list of qubits
Returns:
CouplingMap: coupling map under specified layout.
"""
if qreg is None:
qreg = QuantumRegister(len(layout), "q")
new_map = []
vmap = layout.get_virtual_bits()
for bit0, bit1 in cmap.get_edges():
qubit0, qubit1 = qreg[bit0], qreg[bit1]
qubit0, qubit1 = qubits[bit0], qubits[bit1]
new_map.append([vmap[qubit0], vmap[qubit1]])
return CouplingMap(couplinglist=new_map)
6 changes: 5 additions & 1 deletion qiskit/transpiler/passes/utils/check_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ def run(self, dag):
return
for cf_instr in dag.op_nodes(op=ControlFlowOp):
layout = Layout(dict(enumerate(cf_instr.qargs)))
check_map = CheckMap(layout_transform(self.coupling_map, layout))
check_map = CheckMap(layout_transform(self.coupling_map, layout, dag.qubits))
# check_map = CheckMap(layout_transform2(self.coupling_map, layout))
for block in cf_instr.op.blocks:
dag_block = circuit_to_dag(block)
check_map.run(dag_block)
if not check_map.property_set["is_swap_mapped"]:
self.property_set["is_swap_mapped"] = False
return
32 changes: 31 additions & 1 deletion test/python/transpiler/test_check_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_swap_mapped_cf_true(self):
self.assertTrue(pass_.property_set["is_swap_mapped"])

def test_swap_mapped_cf_false(self):
"""Check control flow blocks are mapped."""
"""Check control flow blocks are not mapped."""
num_qubits = 3
coupling = CouplingMap([(i, i + 1) for i in range(num_qubits - 1)])
qr = QuantumRegister(3)
Expand All @@ -116,6 +116,36 @@ def test_swap_mapped_cf_false(self):
pass_.run(dag)
self.assertFalse(pass_.property_set["is_swap_mapped"])

def test_swap_mapped_cf_layout_change_false(self):
"""Check control flow blocks with layout change are not mapped."""
num_qubits = 3
coupling = CouplingMap([(i, i + 1) for i in range(num_qubits - 1)])
qr = QuantumRegister(3)
cr = ClassicalRegister(3)
circuit = QuantumCircuit(qr, cr)
true_body = QuantumCircuit(qr)
true_body.cx(1, 2)
circuit.if_else((cr[0], 0), true_body, None, qr[[1, 0, 2]], cr)
dag = circuit_to_dag(circuit)
pass_ = CheckMap(coupling)
pass_.run(dag)
self.assertFalse(pass_.property_set["is_swap_mapped"])

def test_swap_mapped_cf_layout_change_true(self):
"""Check control flow blocks with layout change are mapped."""
num_qubits = 3
coupling = CouplingMap([(i, i + 1) for i in range(num_qubits - 1)])
qr = QuantumRegister(3)
cr = ClassicalRegister(3)
circuit = QuantumCircuit(qr, cr)
true_body = QuantumCircuit(qr)
true_body.cx(0, 2)
circuit.if_else((cr[0], 0), true_body, None, qr[[1, 0, 2]], cr)
dag = circuit_to_dag(circuit)
pass_ = CheckMap(coupling)
pass_.run(dag)
self.assertTrue(pass_.property_set["is_swap_mapped"])


if __name__ == "__main__":
unittest.main()

0 comments on commit ee78880

Please sign in to comment.