# Getting a layout for qubits in Qiskit

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)
qc.h(3)
qc.cx(0,6)
qc.h(1)
qc.cx(6,0)
qc.cx(0,1)
qc.cx(3,1)
qc.h(3)
qc.cx(3,0)
qc.measure_all()

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?

• As a layman I think map must be visual and so, non-visual mapping of dictionary can not be viable. I am not sure ! Feb 28 '20 at 7:17

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

IBMQ.load_account()
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:

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

• Thank you, that worked nicely. Just to clarify, is there a non visual representation of the mapping e.g. a dictionary? Dec 4 '19 at 20:27
• @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. Dec 4 '19 at 22:38