Pricing…Open Lab
Chapter 01 of 12 · ~15 min · FREE

The Circuit You Write Is Not the Circuit the QPU Runs

The circuit you write is not the one the chip runs. A compiler first rewrites your gates into the few gates the chip can do. Then it picks a real qubit for each of your qubits. Then it adds extra steps so every two-qubit gate lands on qubits that are wired together. The new circuit does the same job, but it can have more gates, take more steps, and put your answer on different wires.

What does it mean to compile a quantum circuit?

Why care? The extra work the compiler adds is a big reason real quantum computers make mistakes. If you know what it adds, you can read your results correctly and write circuits that cost less.

The circuit you draw in a notebook or in the Lab is a logical circuit. It is a plan. It says "do these steps to these numbered qubits, in this order." It quietly assumes any qubit can work with any other qubit.

Real chips do not work like that. Each chip has an instruction set. That is the short list of steps its control electronics can really do. Each chip also has a topology. That is a fixed map of which qubit pairs are wired together.

A compiler fixes the gap. Quantum toolkits often call it a transpiler, and the job is called transpiling. It rewrites your logical circuit into a physical circuit. The physical circuit uses only steps the chip supports. It uses them only on pairs that are wired together. That physical circuit is what the machine runs.

An everyday example: you have a recipe written for a big kitchen. Your kitchen has no oven, only a stove and a microwave. So you rewrite the recipe with the tools you have. The dish should taste the same. Here the example breaks. In cooking, extra steps mostly waste time. In a quantum chip, every extra step adds a small chance of error. So a quantum compiler's choices change how often the answer is wrong, not just how fast it runs. A normal computer compiler, like one that turns C code into machine code, mostly changes speed.

What does the compiler actually do to your circuit?

The compiler works in stages, one after another:

  • Translate: rewrite each gate using only the chip's own gates. These are called native gates or basis gates.
  • Map: give each of your qubits a real qubit on the chip. This choice is called the layout.
  • Route: add steps that move qubit states so each two-qubit gate lands on a wired pair.
  • Optimize: remove or merge gates when the math says the result stays the same.
  • Schedule: on some chips, set the exact timing of every control pulse.

Worked example. Take a circuit with two steps. First, an H gate (the Hadamard gate) on qubit 0. H makes a qubit an even 50/50 mix of 0 and 1. Second, a CX gate from qubit 0 to qubit 2. CX is the controlled-NOT gate. It flips the second qubit (the target) whenever the first qubit (the control) is 1.

Now run it on a three-qubit chip wired in a line. Qubit 0 connects only to qubit 1. Qubit 1 connects only to qubit 2.

The CX between qubits 0 and 2 cannot run. They are not wired together. So the compiler adds a SWAP. A SWAP trades the states of two neighbouring qubits. It moves qubit 0's state onto qubit 1, right next to qubit 2. On most chips a SWAP is itself made of 3 CX gates.

Count the cost, step by step. You wrote 2 steps. Only 1 of them was a two-qubit gate. The chip runs H, then SWAP, then CX. With the SWAP opened up, that is 1 + 3 + 1 = 5 steps. Four of them are two-qubit gates.

Say each two-qubit gate works 99% of the time. With 1 such gate, a clean run happens 0.99 of the time. With 4, you multiply: 0.99 × 0.99 = 0.98. Then 0.98 × 0.99 ≈ 0.97. Then 0.97 × 0.99 ≈ 0.961. So about 4 shots in every 100 go wrong instead of 1.

Run the circuit you wrote

Expect two bars, 000 and 101, each near 500 shots. Bitstrings read q2 q1 q0, so the two 1s are qubits 0 and 2.standby
123q0|0⟩q1|0⟩q2|0⟩H
press run to acquire
|000⟩|001⟩|010⟩|011⟩|100⟩|101⟩|110⟩|111⟩
————————
counts: sampledamplitudes: statevector, exactengine: in-browser
Open in the Lab →

Run the circuit a line-shaped chip would need

The same 50/50 split. But the linked pair now shows on q1 and q2, so the bars are 000 and 110.standby
1234q0|0⟩q1|0⟩q2|0⟩H
press run to acquire
|000⟩|001⟩|010⟩|011⟩|100⟩|101⟩|110⟩|111⟩
————————
counts: sampledamplitudes: statevector, exactengine: in-browser
Open in the Lab →

Why do the same steps give different bitstrings?

Both runs do the same thing. They put one qubit into an even 50/50 mix. Then they link a second qubit to it, so the two always agree.

Here are the numbers. Each possible outcome has an amplitude. That is a number that says how strongly the qubit leans toward that outcome. Square it and you get the chance of that outcome. Before H, qubit 0 has amplitude 1 on 0. After H, it has 1/√2 ≈ 0.707 on 0 and 0.707 on 1. Square it: (1/√2) × (1/√2) = 1/2. So about 500 of 1000 shots land on each bar.

The only difference is bookkeeping. In the routed run, the SWAP moved qubit 0's state onto wire 1. So the linked pair reads out on q1 and q2. The compiler writes down this final mapping. The mapping says which real wire holds which of your qubits. You need that record to read your results.

Think of a coat check. You hand over your coat and get a ticket. The coat may hang on any hook. Without the ticket, you cannot find it. Unlike a coat, a qubit's state can't just be looked at to see whose it is. The record is the only way to know.

Two lessons follow. First, many different physical circuits can do the same job as one logical circuit. Compilers make different choices. There is no single "correct" compiled version. Second, many people think the chip runs your diagram exactly as drawn. On almost every chip today, that is false. Something translates, maps and routes it first. QPU137's compiled outputs come from our own reference compiler, built for teaching. They do not predict the exact output of any vendor's compiler.

What changes on a real chip?

The simulator above has no noise. So the routed version and the direct version score the same. On a real chip, every added gate lowers the fidelity, which means how close the result is to perfect. Two-qubit gates cost the most. You can compare chips on the two-qubit fidelity metric page.

Vendor compilers also aim at different gate lists and different wiring maps. You can browse them on the QPU index. When a chip maker has not published a number, the honest value is "not publicly disclosed." We never put in a guess.

Primary sources & further reading
The Circuit You Write Is Not the Circuit the QPU Runs · QPU137