Skip to content

Commit

Permalink
rmove continue handling. raise error on unsupported control flow op.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewinston committed Aug 29, 2022
1 parent 12fb4bc commit 2b8d5bb
Showing 1 changed file with 2 additions and 18 deletions.
20 changes: 2 additions & 18 deletions qiskit/transpiler/passes/routing/stochastic_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,8 @@ def _transpile_controlflow_op(self, cf_op, current_layout):
return self._transpile_controlflow_multiblock(cf_op, current_layout)
elif isinstance(cf_op, (ForLoopOp, WhileLoopOp)):
return self._transpile_controlflow_looping(cf_op, current_layout)
return cf_op, current_layout
else:
raise TranspilerError(f'unsupported control flow operation: {cf_op}')

def _transpile_controlflow_multiblock(self, cf_op, current_layout):
# pylint: disable=cyclic-import
Expand Down Expand Up @@ -424,32 +425,15 @@ def _transpile_controlflow_looping(self, cf_op, current_layout):
"""
# pylint: disable=cyclic-import
from qiskit.transpiler.passes.routing import LayoutTransformation

def _move_continue_to_end(dag):
"""Check if continue exists in block. If it does move it to the end of the circuit
so layout_xform doesn't get skipped."""
continue_nodes = dag.named_nodes("continue_loop")
if continue_nodes:
if len(continue_nodes) > 1:
raise CircuitError("Multiple 'continue' statements contained in loop")
if len(continue_nodes) <= 1:
c_node = continue_nodes[-1]
dag.remove_op_node(c_node)
dag.apply_operation_back(c_node.op, c_node.qargs, c_node.cargs)
return dag

new_coupling = layout_transform(self.coupling_map, current_layout)
dag_block = circuit_to_dag(cf_op.blocks[0])
_pass = self.__class__(new_coupling, initial_layout=None, seed=self.seed)
start_qreg = QuantumRegister(len(self._qubit_indices), "q")
start_layout = Layout.generate_trivial_layout(start_qreg)
updated_dag_block = _pass.run(dag_block)
updated_layout = _pass.property_set["final_layout"].copy()

layout_xform = LayoutTransformation(new_coupling, updated_layout, start_layout)
match_dag = layout_xform.run(updated_dag_block)
match_dag = _move_continue_to_end(match_dag)

match_circ = dag_to_circuit(match_dag)
return cf_op.replace_blocks([match_circ]), current_layout

Expand Down

0 comments on commit 2b8d5bb

Please sign in to comment.