• Fundamentals
  • Also: QIR
  • Also: quantum IR

Quantum Intermediate Representation

A compiler-level abstraction layer between high-level quantum programs and hardware-specific instructions, enabling optimization and portability across different quantum backends.

A quantum intermediate representation (quantum IR) is a structured format used by quantum compilers to represent quantum programs at an abstraction level between the user’s source code and the hardware’s native instruction set. Just as classical compilers use intermediate representations (like LLVM IR) to separate language-specific frontends from hardware-specific backends, quantum IRs enable multi-platform targeting, program optimization, and analysis that would be difficult at either the source or machine level. The most prominent example is QIR (Quantum Intermediate Representation) developed by Microsoft, which extends LLVM IR with quantum operations.

Why quantum IRs matter

Without an intermediate representation, each quantum programming framework must include its own compiler pipeline for each target backend. This creates an M×NM \times N problem: MM frontends (Qiskit, Cirq, PennyLane, Q#) each need NN backend-specific compilers (IBM, Google, Rigetti, IonQ). An intermediate representation reduces this to M+NM + N: each frontend compiles to the IR, and each backend compiles from the IR.

Beyond portability, IRs enable optimization passes that are independent of both the source language and the target hardware:

  • Gate cancellation: Adjacent inverse gates (HH=IH \cdot H = I, CNOTCNOT=I\text{CNOT} \cdot \text{CNOT} = I) can be detected and removed.
  • Gate commutation: Reordering gates that commute to enable further cancellations or reduce circuit depth.
  • Rotation merging: Consecutive rotations around the same axis (Rz(α)Rz(β)=Rz(α+β)R_z(\alpha) \cdot R_z(\beta) = R_z(\alpha + \beta)) can be combined.
  • Template matching: Replacing known circuit patterns with equivalent but shorter sequences.

These optimizations are easier to implement on a well-structured IR than on raw circuit descriptions.

QIR (Microsoft)

QIR is the most mature quantum intermediate representation. Key design decisions:

  • Built on LLVM IR: QIR extends LLVM’s existing infrastructure, inheriting its optimization passes, tooling, and ecosystem. Quantum operations appear as calls to intrinsic functions (__quantum__qis__h__body, __quantum__qis__cnot__body, etc.) that operate on opaque qubit and result types.

  • Mixed quantum-classical: QIR naturally represents programs that interleave quantum operations with classical computation, including mid-circuit measurement and feed-forward. Classical parts are standard LLVM IR and can be optimized by existing LLVM passes.

  • Profile system: QIR defines “profiles” that restrict the full specification to match hardware capabilities. The “base profile” supports only sequential gate application and end-of-circuit measurement. More advanced profiles add branching, loops, and dynamic qubit allocation.

  • Adoption: QIR is used by Q# and Azure Quantum, and has been adopted by several other projects as a compilation target.

  • MLIR quantum dialects: MLIR (Multi-Level Intermediate Representation), part of the LLVM project, supports domain-specific “dialects.” Several quantum dialects have been developed, including quantum dialect proposals that represent quantum operations at multiple abstraction levels within a single IR framework.

  • QSSA (Quantum Static Single Assignment): A research IR that applies the SSA form from classical compilers to quantum programs. In QSSA, each qubit value is defined exactly once, making dataflow analysis and optimization more straightforward. The no-cloning theorem aligns naturally with SSA form: you cannot copy a quantum value, and in SSA form, you cannot reuse a definition.

  • Quartz: A superoptimizer for quantum circuits that exhaustively searches for equivalent but smaller circuit implementations. It operates on its own internal IR that supports efficient pattern matching and equivalence checking.

  • t|ket> IR: Quantinuum’s compiler uses an internal IR based on a directed acyclic graph where nodes represent gates and edges represent qubit dependencies. This representation enables efficient routing and optimization for trapped-ion hardware.

The compilation pipeline

A typical quantum compilation pipeline using an IR:

  1. Frontend parsing: The user’s program (Q#, Qiskit, Cirq) is parsed into the IR.
  2. High-level optimization: Logical-level optimizations (gate cancellation, rotation merging) that are hardware-independent.
  3. Lowering: Abstract gates are decomposed into the target hardware’s native gate set.
  4. Routing and mapping: Logical qubits are assigned to physical qubits, and SWAP gates are inserted for connectivity constraints.
  5. Hardware-specific optimization: Pulse-level optimization, scheduling, and calibration-aware gate replacement.
  6. Code generation: The optimized IR is translated to the hardware’s executable format (OpenQASM, Quil, or direct pulse sequences).

Why it matters for learners

Quantum IRs represent the maturation of quantum software engineering. As quantum computers scale, the gap between algorithm design and hardware execution widens, and the compiler stack must bridge that gap efficiently. Understanding quantum IRs helps explain why the same algorithm can perform differently on different platforms (different compilation pipelines make different trade-offs) and why transpilation is not a solved problem. For students coming from classical computing, quantum IRs provide a familiar conceptual bridge: many of the same principles (SSA form, dataflow analysis, optimization passes) apply, with quantum-specific constraints like the no-cloning theorem adding new dimensions.

See also