You are reading without signing in. Sign in or continue as guest to take quizzes and climb the ranks.
Classical Compiler vs. Quantum Transpiler
What happens when you press Compile, explained by comparing it with how C or Python code becomes machine instructions.
- 01Match each transpiler stage to its counterpart in a classical compiler.
- 02Explain why a quantum compiler must also care where qubits sit on the chip.
- 03Name the native gates of IBM chips: rz, sx and x plus ECR (Eagle) or CZ (Heron).
A classical compiler turns a line like a = b * c + d into the few instructions a CPU understands, decides which variables live in the CPU's handful of registers, and removes wasted work. A quantum transpiler does the same three jobs with two extra constraints: a chip can only apply a two-qubit gate between qubits that are physically wired together, and every extra gate costs accuracy, because qubits lose their quantum state while the circuit runs.
| Classical compiler | Quantum transpiler | Example in QubitLab |
|---|---|---|
| Instruction selection: code → CPU instructions (x86, ARM) | Translation: any gate → the chip's basis gates | A Toffoli (ccx) becomes six CNOTs plus single-qubit gates |
| Register allocation: variables → a few CPU registers | Layout: q[0], q[1], … → physical qubits | Prefers well-connected qubits with low error |
| No real counterpart: any register can feed any instruction | Routing: insert SWAPs so two-qubit gates act on neighbours | Each SWAP costs three CNOTs |
| Optimisation flags -O0 to -O3 | Optimisation levels 0 to 3 | Fewer gates and lower depth |
01Why every gate counts
A wasted CPU instruction costs a fraction of a nanosecond and never changes the answer. A wasted two-qubit gate on today's chips adds an error rate typically between 0.1% and 1%, and every extra layer of depth gives the qubits more time to decohere. That is why the transpiler works so hard on layout and routing, and why the optimisation level matters more than -O2 does for a C program.
Compile a Toffoli
Compile tab, target fake_manila (five qubits in a line), level 0, then level 3. The ccx turns into CX, rz and sx. Three qubits in a line cannot all be neighbours, so at level 0 the router adds SWAPs and the CX count lands well above six; level 3 searches harder and removes much of that overhead. Then pick fake_torino (a Heron twin): the two-qubit gate becomes cz.
OPENQASM 3.0;
include "stdgates.inc";
qubit[3] q;
bit[3] c;
h q[0];
h q[1];
ccx q[0], q[1], q[2];
c = measure q;
2 questions · one try each counts
Which transpiler stage has no real counterpart in a classical compiler?
Sign in to answer and climb the ranks; you will come back to this lesson.
Why does a quantum compiler care about circuit depth much more than a C compiler cares about instruction count?
Sign in to answer and climb the ranks; you will come back to this lesson.