← Quantum Store · Guides · Lessons · Glossary · About · Türkçe

Qiskit course: real quantum programming · Lesson 6 / 6 · 12 min

Onto a real quantum computer: transpile and IBM Quantum

Translate your circuit to a device’s gates with transpile, connect your IBM Quantum account, run the circuit on a real quantum computer and interpret the noisy results.

Devices have their own gates

A real quantum computer can only apply a few basic gates directly. On IBM devices these are single-qubit gates such as rz, sx and x plus one kind of two-qubit gate. Also, not every qubit is connected to every other; if a CNOT is needed between two distant qubits, SWAPs are inserted.

transpile translates your circuit into the gates and qubit connections the device supports and shortens it where it can. In the example below the H gate is broken down into rz and sx gates. optimization_level is a number from 0 to 3; the higher it is, the harder transpile works and the shorter the circuit usually gets.

from qiskit import QuantumCircuit, transpile

qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()

tqc = transpile(qc, basis_gates=['rz', 'sx', 'x', 'cx'], optimization_level=1)
print(tqc.draw())
print(tqc.count_ops(), tqc.depth())

An IBM Quantum account

With a free account on IBM Quantum Platform you can use real devices for a limited time every month. Create an API key on your account page and install the qiskit-ibm-runtime library (pip install qiskit-ibm-runtime). Saving the key once is enough; keep it like a password and never paste it into shared code.

from qiskit_ibm_runtime import QiskitRuntimeService

# Run only once: the key is saved on your computer.
QiskitRuntimeService.save_account(token='YOUR_API_KEY', set_as_default=True)

Send the circuit to a device

least_busy picks the real device with the shortest queue at that moment. The circuit is transpiled for that device and sent to IBM’s Sampler; the rest of the code is the same as with the simulator. If the queue is long the job may wait a few minutes; with the job_id you can also find it later on IBM Quantum Platform.

from qiskit import transpile
from qiskit_ibm_runtime import QiskitRuntimeService, SamplerV2 as Sampler

service = QiskitRuntimeService()
backend = service.least_busy(operational=True, simulator=False)
tqc = transpile(qc, backend=backend)

sampler = Sampler(mode=backend)
job = sampler.run([tqc], shots=1000)
print(job.job_id())
counts = job.result()[0].data.meas.get_counts()
print(backend.name, counts)

Why are real results imperfect?

In the simulator the Bell circuit only gives 00 and 11. On a real device you also see 01 and 10 a few percent of the time. The reason is noise: gates make small errors, qubits lose their state over time and measurements are sometimes misread.

The shorter the circuit, the less error builds up, which is why transpile tries to shorten it. Ways of dealing with noise, error mitigation and error correction, are the subject of the school’s later courses.

Try it yourself

The simulator is an ideal device: the GHZ example only gives 000 and 111, while on a real device the same circuit also gives a few wrong results such as 001 or 110. Remove the second CNOT: q2 no longer joins the entanglement and the results become 000 and 011.

Open the circuit in your browser

The circuit playground is in Turkish. Button guide: “1000 kez ölç” = measure 1000 times, “Durum vektörünü göster” = show the state vector, “Bu devrenin Qiskit kodu” = this circuit’s Qiskit code, “Geri al” = undo, “Temizle” = clear. Click a gate in the circuit to remove it.

Key points

Test yourself

What does transpile do?

Show the answer

Translates the circuit into the gates and qubit connections the device supports. transpile neither runs the circuit nor corrects errors; it turns the circuit into one that fits the target device’s basic gates and connections, shorter where possible.

← Rotation gates and parameterised circuits · Read the guides →

Share: X · LinkedIn