Quantum in 7 Sessions › Session 4
Session 4 — Real hardware: compilation and noise
Session 4 of 7Self-studySimulator only
On this page: Read · Key ideas · Worked example · Try it · Second experiment · Common mistakes · Check yourself · Project
1. Read
- Chapter 4 — Circuits, Compilation, and Real Hardware
- Chapter 7 — Noise, Error Detection, and Quantum Error Correction
Read the chapters first. The summary below is a reminder, not a replacement.
2. Key ideas
- A real device supports only a small native gate set, and only certain pairs of qubits can interact directly (its connectivity).
- Compilation (transpilation) rewrites your circuit into native gates and inserts extra operations to move information between distant qubits.
- Every extra gate, and especially every two-qubit gate, is another chance for error. Depth and two-qubit gate count are the first numbers to check before a hardware run.
- Errors come in two basic kinds, bit flips and phase flips. Error correction spreads information over several qubits so errors can be detected without copying the unknown state, which is forbidden.
3. Worked example
Work through this with pen and paper before touching the code.
Estimate how much routing costs. Suppose every two-qubit gate succeeds with probability 0.99, and ignore every other error.
- Logical circuit, 3 two-qubit gates: 0.99³ ≈ 0.970.
- Compiled circuit, 6 two-qubit gates: 0.99⁶ ≈ 0.941.
- Routing alone moved the estimate from about 97% to about 94%, before counting single-qubit gates, readout, or idle time.
This is a rough model, not a prediction for any particular device. Its use is to compare two versions of the same circuit before you spend hardware time.
4. Try it
Take a 4-qubit GHZ circuit in which one qubit controls all the others, and compile it for a device whose qubits sit in a line. Write down what you expect before you run the code.
from qiskit import QuantumCircuit, transpile
# Logical circuit: a 4-qubit GHZ state with qubit 0 controlling all the others
qc = QuantumCircuit(4)
qc.h(0)
qc.cx(0, 1)
qc.cx(0, 2)
qc.cx(0, 3)
# A device whose qubits sit in a line: 0-1-2-3
line = [[0, 1], [1, 0], [1, 2], [2, 1], [2, 3], [3, 2]]
hw = transpile(qc, basis_gates=["rz", "sx", "x", "cx"], coupling_map=line,
optimization_level=1, seed_transpiler=7)
print("logical : depth", qc.depth(), " two-qubit gates", qc.count_ops().get("cx", 0))
print("hardware: depth", hw.depth(), " two-qubit gates", hw.count_ops().get("cx", 0))
Output (counts will vary slightly from run to run):
logical : depth 4 two-qubit gates 3 hardware: depth 9 two-qubit gates 6
What to notice
The logical circuit needs 3 two-qubit gates. On a line, qubit 0 is not next to qubits 2 and 3, so the compiler adds routing and the count roughly doubles, with a much larger depth. Nothing about the algorithm changed; only the hardware layout did. Exact numbers can differ between Qiskit versions.
5. Second experiment
Build the same GHZ state as a chain, so that every CNOT acts on neighbouring qubits. Predict first.
from qiskit import QuantumCircuit, transpile
# The same GHZ state, built as a chain: each CNOT acts on neighbours
qc = QuantumCircuit(4)
qc.h(0)
qc.cx(0, 1)
qc.cx(1, 2)
qc.cx(2, 3)
line = [[0, 1], [1, 0], [1, 2], [2, 1], [2, 3], [3, 2]]
hw = transpile(qc, basis_gates=["rz", "sx", "x", "cx"], coupling_map=line,
optimization_level=1, seed_transpiler=7)
print("hardware: depth", hw.depth(), " two-qubit gates", hw.count_ops().get("cx", 0))
Output:
hardware: depth 6 two-qubit gates 3
What it shows
Now the compiled circuit needs only 3 two-qubit gates, the same as the logical one. The state prepared is identical; only the order of the CNOTs changed. Designing with the hardware layout in mind removed all the routing overhead.
6. Common mistakes and tips
Common mistakes
- Reporting the depth of the circuit you drew instead of the circuit that ran.
- Assuming all qubits on a device are equally good and all pairs are connected.
- Blaming every difference from the ideal result on “noise” without separating sampling error, compilation, and device error.
Tips
- Always save both the logical and the compiled circuit, and record depth and two-qubit gate count for each.
- Rewrite the circuit to respect the device layout when you can. It is often cheaper than letting the compiler route.
- Fix the transpiler seed so that your compiled circuit is reproducible.
7. Check yourself
Answer each question on paper, then open the answer to compare.
- Rewrite the logical circuit as a chain (0→1, 1→2, 2→3). It prepares the same GHZ state. What happens to the compiled gate count?
Show answer
It stays at 3, because every CNOT already acts on neighbours. See the second experiment.
- Why can a quantum error-correcting code not simply copy the qubit three times?
Show answer
An unknown quantum state cannot be copied (the no-cloning principle). Error correction instead spreads the information over several qubits using entanglement.
- Before spending hardware time, which three numbers would you record for a circuit?
Show answer
Circuit depth, number of two-qubit gates, and the number of jobs and shots (with the estimated cost or credits).
8. Do a project
Pick one. Each follows the routine from Guide 1: predict, run the baseline, change one variable, analyze.
← Session 3Session 5: Algorithms: search and phase estimation →
For instructors: Teacher notes for Session 4