QubitLab
IDELearnPlaygroundDocsCommunity
Log in
QubitLab
IDELearnPlaygroundDocsCommunity
Log in
QubitLab
IDELearnPlaygroundDocsCommunity
Log in
HomeLearnCatalogRanksPlaygroundDocs
02 · Classical Compiler vs. Quantum Transpiler0%

You are reading without signing in. Sign in or continue as guest to take quizzes and climb the ranks.

Learn/QC 203Hardware and Compilation·Lesson 2 of 6

Classical Compiler vs. Quantum Transpiler

What happens when you press Compile, explained by comparing it with how C or Python code becomes machine instructions.

Intermediate12 min read5 sections · 2 questions+45
By the end you will be able to
  1. 01Match each transpiler stage to its counterpart in a classical compiler.
  2. 02Explain why a quantum compiler must also care where qubits sit on the chip.
  3. 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 compilerQuantum transpilerExample in QubitLab
Instruction selection: code → CPU instructions (x86, ARM)Translation: any gate → the chip's basis gatesA Toffoli (ccx) becomes six CNOTs plus single-qubit gates
Register allocation: variables → a few CPU registersLayout: q[0], q[1], … → physical qubitsPrefers well-connected qubits with low error
No real counterpart: any register can feed any instructionRouting: insert SWAPs so two-qubit gates act on neighboursEach SWAP costs three CNOTs
Optimisation flags -O0 to -O3Optimisation levels 0 to 3Fewer 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.

IBM Eagle: {Rz​, X​, X, ECR}IBM Heron: {Rz​, X​, X, CZ}
The whole gate library you write with is rebuilt from these few native gates. In QASM, √X is written sx.
Hands-on

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
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;
Sign in to run (nothing is saved)
Open in the IDE Open in the Playground
Knowledge check

2 questions · one try each counts

Question 1

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.

Question 2

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.

Key takeaway

Pressing Compile does what a classical compiler does (select instructions, allocate registers, optimise) plus one job a CPU never needs: routing qubits that are not wired together. Every gate it saves is accuracy you keep.

Finished reading?

Knowledge check: 2 questions waiting above. Questions are optional. Mark it complete, or press Next: moving on marks it complete too.

Up next
Lesson 3 · 12 min
Transpilation and basis gates
What the compiler does in four stages, what the optimisation levels change, and how to read the Compile preview.
Further reading
  • Docs: transpiler stages and levels
More in Hardware and Compilation
  • From an abstract qubit to a device
  • Circuit Identities & Gate Cancellation
  • Topology and SWAP cost
From an abstract qubit to a deviceTranspilation and basis gates
In this lesson0%
  1. Introduction
  2. Why every gate counts
  3. Try it: Compile a Toffoli
  4. Knowledge check
  5. Key takeaway
QubitLab
IDELearnPlaygroundDocsCommunity
Log in
QubitLab
IDELearnPlaygroundDocsCommunity
Log in