← Back to section · Software & Developer Tools
How the Qiskit Transpiler Works: From Abstract Circuits to Hardware-Executable Circuits
Circuits users write are abstract; hardware only accepts its own basis gates and topology. The transpiler performs all the rewriting in between: basis decomposition, layout, routing and optimization. This article explains the pipeline's stages, what the optimization levels mean, and the common tuning points.
A circuit that runs perfectly on a simulator often cannot execute on real hardware at all: hardware supports only a limited set of basis gates, its qubits are not all mutually connected, and circuits must be as shallow as possible to fight decoherence. Rewriting an abstract circuit into the form “this chip can run, and run as well as possible” is the transpiler’s entire job. In Qiskit this step is driven by the transpile function and is an unskippable link between circuits and hardware.
Why rewriting is needed
There are three gaps between abstract circuits and hardware reality. The first is the gate set: arbitrary rotations and controlled gates that users write freely must be decomposed into equivalent sequences of the few native gates hardware provides. The second is topology: qubits on a chip connect directly to only a few neighbours, so entangling two non-adjacent qubits requires shuttling quantum states along a path. The third is quality variation: error rates differ across physical qubits and links, so placing critical operations on good qubits and avoiding bad links changes result quality substantially. Transpilation is the rewriting process that bridges all three gaps at once.
The four stages of the pipeline
Qiskit’s transpiler is a pipeline of passes, roughly in four stages. Stage one is decomposition: high-level gates and composite instructions are expanded and translated into the target backend’s basis gate set. Stage two is layout: each logical qubit of the circuit is assigned a physical qubit, with choices informed by error rates in calibration data. Stage three is routing: for two-qubit gates that violate topology constraints, SWAP gates are inserted to move quantum states so that every two-qubit operation lands on a real link; inserted SWAPs add gates and depth and are the main source of transpilation cost. Stage four is optimization: adjacent rotations are merged, inverse gate pairs cancelled and redundant structures simplified, compressing gate count and depth under equivalence. Each stage internally chains many small passes, and users can assemble their own pass managers, but for everyday use the default pipeline suffices.
Choosing an optimization level
The optimization_level parameter of transpile runs from 0 to 3. Level 0 does only the necessary decomposition and mapping with almost no optimization — fastest, suited to debugging circuit logic. Level 1 performs light optimization such as cancelling obvious redundant gates. Level 2 adds more aggressive simplification and layout strategies. Level 3 is the most aggressive, attempting heavier rearrangement and synthesis, usually yielding the shallowest circuit at the cost of the longest transpilation time. As a rule of thumb, use level 0 or 1 to validate logic, and levels 2 or 3 for real hardware runs, comparing outcomes. Another frequently adjusted parameter is the random seed: layout and routing involve randomness, so the same circuit can transpile differently across runs; fixing the seed makes experiments reproducible, and trying several seeds to pick the shallowest result is also a common trick.
Connecting to hardware information
Transpilation quality depends heavily on the accuracy of backend information: the basis gate set, the coupling map, and per-gate and readout error rates all come from the backend’s configuration and calibration data. Transpile against a real backend’s properties and layout will avoid bad qubits; transpile against a fake backend or stale calibration and the rewriting is merely formally compliant. Two habits are worth forming before submitting to hardware: first, inspect the transpiled circuit’s two-qubit gate count and depth and compare with the original — abnormal inflation usually signals topology mismatch or too low an optimization level; second, confirm that the backend used for transpilation is the same backend you will submit to, since a mismatch wastes all the rewriting.
A concrete way to observe it
To ground the four stages, take a small circuit and run a comparison experiment. Take a circuit containing a two-qubit gate between non-adjacent qubits, transpile it to the same real backend at optimization levels 0 and 3, and compare three numbers: total gate count, two-qubit gate count and circuit depth. Typically level 3 shows clearly lower two-qubit counts and depth, the difference coming mainly from the routing stage finding shorter paths and the optimization stage merging more gates. Then draw the transpiled circuit, find where the SWAPs were inserted and understand why they appeared. Do this “same circuit, multiple levels, compare the numbers” observation two or three times and the transpiler turns from a black box into a predictable tool.
Also worth observing is the effect of the initial layout. Manually map the two most frequently interacting qubits of a circuit to distant positions on the chip and the transpiled depth deteriorates immediately; let the default layout strategy choose freely and the result is usually much better. This shows layout is not a detail but the main battleground: when the qubit count approaches the chip’s scale, layout quality can influence results even more than how the circuit itself is written. Writing circuits so that frequently interacting qubits are adjacent in logical numbering is a low-cost habit that does the layout stage a favour.
Transpilation is not omnipotent
Be clear about transpilation’s limits. It can only work within equivalence-preserving rewrites: the total two-qubit interaction an algorithm inherently needs does not disappear through transpilation, and on sparsely connected chips depth inflation has a floor. Nor does it fix waste at the algorithm level — an intrinsically verbose circuit can only be trimmed at the margins. Truly large optimizations usually happen at the algorithm and circuit-design level: reducing interactions, choosing structures better suited to the topology, exploiting symmetry. Transpilation is the last process, not the first. Getting this order right stops you from pinning all hope on the optimization level.
Summary
The transpiler is translator and optimizer between abstract algorithms and physical hardware: decomposition solves the gate set, layout and routing solve topology, and optimization solves depth. It is not a black-box switch — understanding the four stages and the optimization levels lets you judge, when real-hardware results disappoint, whether the problem lies in the circuit itself or in the rewriting strategy. That judgement is the watershed between “it runs” and “it runs well”.
Tags: QiskitTranspilerCircuit Optimization