Quantum circuit compilation is where an abstract circuit becomes executable on a particular simulator or quantum processor. This guide provides a reusable workflow for mapping circuits to hardware, controlling depth and two-qubit gates, comparing compilation results, and checking the output before running an experiment.
Overview
A circuit written with ideal gates assumes more freedom than most devices provide. A target backend may support only a limited gate set, connect some qubits but not others, and impose constraints on which two-qubit operations can run directly. Compilation, often called transpilation in SDK workflows, converts the circuit into operations that match those constraints.
The conversion can add swap operations when interacting qubits are not adjacent on the device. It can also decompose convenient gates into basis operations, reorder or cancel gates, and optimize repeated patterns. These changes are necessary for execution, but they can increase circuit depth and the number of two-qubit gates. Because two-qubit operations are often more sensitive to hardware noise than single-qubit operations, hardware mapping should be treated as an optimization problem rather than a final formatting step.
The practical objective is not always the shortest possible circuit. A useful compilation balances several measurements:
- Logical qubits: the qubits required by the original algorithm.
- Physical qubits: the device qubits assigned to the logical circuit.
- Depth: the longest sequence of operations that cannot run in parallel.
- Two-qubit count: the number of entangling operations after decomposition and routing.
- Added routing: swaps or equivalent operations introduced to satisfy connectivity.
- Basis compatibility: whether the final operations match the target's supported instruction set.
Before comparing results, define the metric that matters for the experiment. For a shallow variational circuit, reducing two-qubit gates may be more important than a small reduction in total depth. For a time-sensitive algorithm, critical-path depth may matter most. For a simulator, connectivity may be less important than memory and execution time. The quantum circuit resource estimation guide can help establish a baseline before compilation.
Checklist by scenario
Scenario 1: Compiling for a specific hardware backend
- Freeze the logical circuit. Save the circuit before compilation, including qubit order, measurements, parameter values or symbols, and any required barriers. This gives you a stable reference for later comparisons.
- Inspect the target. Record the backend's available qubits, coupling or connectivity map, supported operations, measurement conventions, and any constraints exposed by the SDK. Do not assume that a backend with enough qubits can execute every pairwise interaction directly.
- Choose an initial layout deliberately. Place highly connected logical qubits onto physical locations that are close together when possible. For a QAOA or VQE circuit, this may mean grouping qubits that frequently interact. A poor initial layout can force routing before optimization has an opportunity to simplify the circuit.
- Compile with a reproducible configuration. Record the SDK version, backend target, optimization setting, routing method, initial layout, seed or randomness setting if applicable, and whether the circuit was calibrated or scheduled. Reproducibility is essential when comparing changes.
- Measure the result. Compare depth, total gate count, two-qubit count, and routing overhead with the original and with alternative configurations. A successful compilation should be judged by the final executable circuit, not only by the compiler setting selected.
In a Qiskit-style Python workflow, the shape of the process may look like this. Exact option names and backend interfaces can vary by SDK release, so treat it as a pattern to adapt to the installed documentation:
from qiskit import QuantumCircuit, transpile
logical = QuantumCircuit(4)
logical.h(0)
logical.cx(0, 1)
logical.cx(1, 3)
logical.rz(0.25, 3)
compiled = transpile(
logical,
backend=target_backend,
optimization_level=2,
initial_layout=[0, 1, 2, 3],
seed_transpiler=7,
)
print("logical depth:", logical.depth())
print("compiled depth:", compiled.depth())
print("compiled operations:", compiled.count_ops())The important practice is to preserve the configuration and inspect the output. A higher optimization level is not automatically better for every circuit, and an initial layout that helps one ansatz may hurt another.
Scenario 2: The circuit has heavy two-qubit interaction
Start by identifying the interaction graph: each logical qubit is a node, and each two-qubit operation is an edge. Compare that graph with the target's connectivity. If the algorithm repeatedly couples distant logical qubits, consider whether the logical qubit ordering can be changed, whether the circuit can be rewritten to use a more hardware-compatible interaction pattern, or whether a different ansatz preserves the application goal with less routing.
For QAOA, inspect whether problem edges align with nearby physical qubits. For VQE, look at the Pauli terms and entangling structure rather than only the number of qubits. These choices can have more effect than applying a late optimization pass. See the QAOA Max-Cut tutorial and VQE tutorial for algorithm-specific context.
Scenario 3: Comparing a simulator with hardware
Use the simulator to verify logical behavior, but compile a hardware-targeted version before drawing conclusions about execution cost. A simulator may accept operations or connectivity that the processor does not. Conversely, a hardware-emulation mode may expose routing and noise effects that an ideal statevector run hides. Keep the logical circuit, ideal compiled circuit, and hardware-targeted compiled circuit as separate artifacts.
When the circuit is part of a hybrid quantum-AI workflow, benchmark the full loop: parameter binding, compilation, execution, measurement processing, and classical optimization. Recompiling an unchanged parameterized structure at every iteration can create avoidable overhead, while compiling too early can prevent parameter or layout decisions from being applied correctly. The hybrid quantum-AI experiment workflow provides a useful structure for making these comparisons reproducible.
Compilation choices at a glance
| Decision | What it controls | What to compare | Practical guidance |
|---|---|---|---|
| Connectivity | Which qubit pairs can interact directly | Added swaps, two-qubit count, depth | Match frequent logical interactions to nearby physical qubits. |
| Basis gates | Which primitive operations appear in the output | Decomposition size and hardware compatibility | Use the target's supported instruction set rather than a generic basis. |
| Optimization level | How aggressively the compiler rewrites the circuit | Depth, gate count, compile time, semantic equivalence | Test multiple settings on representative circuits; do not infer quality from the level alone. |
| Routing strategy | How non adjacent interactions are made executable | Swap overhead and critical-path depth | Compare routing methods with the same layout and metrics. |
| Initial layout | Logical-to-physical qubit assignment | Early routing, final depth, interaction locality | Use algorithm structure and device connectivity to create a baseline. |
| Depth metric | How sequential the compiled circuit is | Overall depth and two-qubit-layer depth | Track both when noise or timing is important. |
What to double-check before execution
- Semantic equivalence: Confirm that measurements, classical conditions, parameter bindings, and qubit ordering still mean what the application expects.
- Routing overhead: Count inserted swaps or their decomposed equivalents. A small increase in total gates can conceal a large increase in entangling operations.
- Two-qubit layers: Inspect how many entangling operations occur sequentially, not only their total count. Parallel gates may have a different effect on depth than serialized gates.
- Basis compliance: Verify that the final circuit contains only operations accepted by the target interface. Avoid relying on an implicit conversion that may change between environments.
- Layout assumptions: Record the logical-to-physical mapping and use the correct mapping when interpreting counts, observables, or tomography results.
- Parameter behavior: If the circuit is parameterized, confirm that compilation preserves symbols and that binding parameters after compilation behaves as intended.
- Validation path: Run small instances through both the original and compiled circuits on an ideal simulator or another trusted reference. The circuit visualization guide can help expose unexpected swaps, decompositions, or ordering changes.
For noisy experiments, compilation is only one part of the error budget. Readout processing, error mitigation, circuit cutting, and shot allocation may also affect the result. They should be evaluated separately so that a routing change is not incorrectly credited for an improvement caused by post-processing.
Common mistakes
Optimizing the abstract circuit instead of the target circuit
A compact logical circuit may become expensive after routing. Always inspect the compiled form for the actual target, especially when the algorithm uses nonlocal interactions.
Assuming the highest optimization setting wins
More aggressive compilation can increase compile time, make debugging harder, or produce a circuit whose trade-offs do not suit the experiment. Treat optimization settings as candidates to benchmark, not as a universal ranking.
Changing several variables at once
If you change the layout, routing method, basis, and optimization setting together, you cannot tell which choice helped. Establish a baseline, change one major variable, and retain the resulting circuit and metrics.
Counting gates without separating gate types
Total gate count is too coarse for many hardware experiments. Report at least single-qubit count, two-qubit count, total depth, and two-qubit depth or layer count when available.
Ignoring compiler and target drift
Backend properties, SDK behavior, supported instructions, and defaults can change. A result that was optimal in one environment may not be optimal after an upgrade or when targeting another device.
When to revisit
Revisit the compilation checklist whenever the logical circuit changes, including a new ansatz depth, embedding strategy, problem graph, or parameterized block. Also repeat the comparison when switching backends, simulators, SDK versions, basis gates, routing strategies, or optimization settings. A change that appears minor at the source level can alter layout decisions and routing overhead throughout the compiled circuit.
As a practical maintenance routine, keep a small compilation report with the circuit identifier, target, environment version, layout, routing configuration, optimization setting, depth, two-qubit count, and validation result. Review it before seasonal planning cycles or any new hardware campaign. If results regress, first compare the compiled circuits side by side, then test layout and routing alternatives on the same logical circuit.
The final pre-run checklist is simple: freeze the logical circuit, inspect the target constraints, choose and record a layout, compile with reproducible settings, compare depth and two-qubit operations, validate semantics, and only then submit the job. This turns quantum hardware mapping from an opaque SDK step into a measurable developer workflow.