I'm trying to use Qiskit's built in transpiler passes to do the following. I would like to take a quantum circuit and allocate the logical qubits to physical qubits.

The tutorial doesn't really explain how to do this. I would like to use one of the preset passes (TrivialLayout, DenseLayout, etc) to get this allocation.

from qiskit import QuantumCircuit
from qiskit.compiler import transpile
from qiskit.transpiler import PassManager, passes 
from qiskit.test.mock import FakeMelbourne # NB will need to install dev requirements

""" This is the circuit we are going to look at"""
qc = QuantumCircuit(13, 13)

backend = FakeMelbourne() 
properties = backend.properties() 
coupling_map = backend.configuration().coupling_map

The output of the program should be a dictionary or something that gives tells me which physical qubit each logical qubit is mapped to. How can I do this?

  • $\begingroup$ As a layman I think map must be visual and so, non-visual mapping of dictionary can not be viable. I am not sure ! $\endgroup$
    – Arokiasamy
    Feb 28, 2020 at 7:17

1 Answer 1


Try replacing the last three lines of the code you posted with

backend = IBMQ.get_provider(hub='ibm-q').get_backend('ibmq_16_melbourne')
new_circ_lv0 = transpile(qc, backend=backend, optimization_level=0)
plot_circuit_layout(new_circ_lv0, backend)

and add the necessary import statements (plot_circuit_layout is in qiskit.visualization). This should give you the mapping from your logical qubits to physical qubits on ibmq_16_melbourne, with optimization level 0:

physical qubits

For more information, this tutorial will probably be more helpful for this task than the one you linked.

  • $\begingroup$ Thank you, that worked nicely. Just to clarify, is there a non visual representation of the mapping e.g. a dictionary? $\endgroup$ Dec 4, 2019 at 20:27
  • 2
    $\begingroup$ @user1936752 The method that the drawer is using is circuit._layout.get_virtual_bits() and looking at the code I think this does what you are looking for. $\endgroup$
    – met927
    Dec 4, 2019 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.