Skip to main contentIBM Quantum Documentation
You are viewing the API reference for an old version of Qiskit SDK. Switch to latest version


class qiskit.transpiler.passes.LookaheadSwap(*args, **kwargs)

GitHub(opens in a new tab)

Bases: TransformationPass

Map input circuit onto a backend topology via insertion of SWAPs.

Implementation of Sven Jandura’s swap mapper submission for the 2018 Qiskit Developer Challenge, adapted to integrate into the transpiler architecture.

The role of the swapper pass is to modify the starting circuit to be compatible with the target device’s topology (the set of two-qubit gates available on the hardware.) To do this, the pass will insert SWAP gates to relocate the virtual qubits for each upcoming gate onto a set of coupled physical qubits. However, as SWAP gates are particularly lossy, the goal is to accomplish this remapping while introducing the fewest possible additional SWAPs.

This algorithm searches through the available combinations of SWAP gates by means of a narrowed best first/beam search, described as follows:

  • Start with a layout of virtual qubits onto physical qubits.
  • Find any gates in the input circuit which can be performed with the current layout and mark them as mapped.
  • For all possible SWAP gates, calculate the layout that would result from their application and rank them according to the distance of the resulting layout over upcoming gates (see _calc_layout_distance.)
  • For the four (search_width) highest-ranking SWAPs, repeat the above process on the layout that would be generated if they were applied.
  • Repeat this process down to a depth of four (search_depth) SWAPs away from the initial layout, for a total of 256 (search_width^search_depth) prospective layouts.
  • Choose the layout which maximizes the number of two-qubit which could be performed. Add its mapped gates, including the SWAPs generated, to the output circuit.
  • Repeat the above until all gates from the initial circuit are mapped.

For more details on the algorithm, see Sven’s blog post: in a new tab)

LookaheadSwap initializer.




Check if the pass is an analysis pass.

If the pass is an AnalysisPass, that means that the pass can analyze the DAG and write the results of that analysis in the property set. Modifications on the DAG are not allowed by this kind of pass.


Check if the pass is a transformation pass.

If the pass is a TransformationPass, that means that the pass can manipulate the DAG, but cannot modify the property set (but it can be read).



execute(passmanager_ir, state, callback=None)

Execute optimization task for input Qiskit IR.



Optimized Qiskit IR and state of the workflow.

Return type

tuple(opens in a new tab)[Any(opens in a new tab), qiskit.passmanager.compilation_status.PassManagerState]



Name of the pass.

Return type

str(opens in a new tab)



Run the LookaheadSwap pass on dag.


dag (DAGCircuit) – the directed acyclic graph to be mapped


A dag mapped to be compatible with the coupling_map in

the property_set.

Return type



  • TranspilerError – if the coupling map or the layout are not
  • compatible with the DAG**, or **if the coupling_map=None


update_status(state, run_state)

Update workflow status.


  • state (PassManagerState) – Pass manager state to update.
  • run_state (RunState) – Completion status of current task.


Updated pass manager state.

Return type


Was this page helpful?
Report a bug or request content on GitHub.