4
$\begingroup$

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?

$\endgroup$
  • $\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 at 7:17
5
$\begingroup$

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:

physical qubits

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

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you, that worked nicely. Just to clarify, is there a non visual representation of the mapping e.g. a dictionary? $\endgroup$ – user1936752 Dec 4 '19 at 20:27
  • $\begingroup$ @user1936752 Sorry, I'm not aware of a method that returns a dictionary-type mapping you're looking for. $\endgroup$ – Jonathan Trousdale Dec 4 '19 at 21:39
  • 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 '19 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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