Intermediate representations (“IR”) are a common pattern used in classical computers and are often the first step in the code compilation process. As its name indicates, an IR is an intermediary step in the conversion of instructions from source code to machine language. Programming languages are first compiled into an IR by the front-end compiler. A back-end compiler is then responsible for translating the IR into a machine representation. An intermediary pattern like this decouples the source language from the hardware platform, enabling modular compilation. When a new programming language is introduced, new compiler work can focus on the front end, i.e. the step from source code to IR. Backend compilation, from IR to machine language, can remain largely unchanged. The Quantum Intermediate Representation (QIR) fulfills the same role for quantum computers that IRs perform for classical computers. The QIR project was initiated and is being developed by the QIR Alliance and serves as a common interface between quantum programming languages and a variety of targeted quantum computation platforms (Microsoft is a member of the QIR Alliance).
QIR acts as a universal mid-layer quantum representation that enables communication between high-level languages and SDKs, such as Q# and Qiskit, and quantum machines. It is hardware-agnostic and does not specify a quantum instruction or gate set, leaving that to the target computing environment. Instead, QIR uses a set of rules to represent quantum programs in a language- and hardware-agnostic format. This approach allows many source languages to share a common set of optimizers and executable generators. It also makes it easy to compile a single source language for many different targets. QIR thus provides a common platform that can be shared across many sources and targets, allowing a great deal of reuse in compiler machinery.
QIR is based on the popular open-source classical compiler LLVM. LLVM is a collection of modular and reusable compiler and toolchain technologies that has been adapted for a wide range of languages. Because it uses LLVM as the underlying toolchain, QIR is naturally able to process both classical and quantum logic. This feature is essential for hybrid quantum–classical algorithms, which have become increasingly important for applications of quantum computing. In addition, by basing QIR on LLVM, quantum application developers can leverage compilation and optimization tools from classical computing, and therefore reduce the cost of writing translations from classical to quantum computers.