What is QIR
QIR is a new Microsoft-developed intermediate representation for quantum programs. It is based on the popular open-source LLVM intermediate language. QIR specifies a set of rules for representing quantum constructs in LLVM. It does not require any extensions or modifications to LLVM.
QIR is intended to serve as a common interface between many languages and target quantum computation platforms. Thus, while it supports Q#, QIR is not specific to Q#: any programming language for gate-based quantum computing can be represented in QIR. Similarly, QIR is hardware-agnostic: it does not specify a quantum instruction or gate set, leaving that to the target computing environment.
As quantum computing capabilities mature, we expect most large-scale quantum applications will take full advantage of both classical and quantum computing resources working together. LLVM provides QIR with full capabilities for describing rich classical computation fully integrated with quantum computation. Using LLVM also facilitates integration with the many classical languages and tools which are already supported by the LLVM tool chain. It also promotes the development of common language- and backend-independent optimizations and code transformations, based on a well-known and robust open-source framework.
In the coming years, we expect there to be exciting advances in how classical and quantum computations can interact at the hardware level. With QIR we intend to provide a single representation that can be used for both today’s restricted capabilities and the more powerful systems of the future. This will allow the community to experiment with and develop optimizations and code transformations that work today and that will remain useful tomorrow.
What is an Intermediate Representation
A common pattern in compilers is to start by compiling the source language into an intermediate representation. This intermediate form is typically designed to allow many different source languages to be represented. While still in the intermediate representation, the code can be optimized and transformed. Finally, once the actual target execution platform is known, the intermediate representation can be compiled to actual executable code.
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. The intermediate representation provides a common platform that can be shared across many sources and targets and allows a great deal of re-use in compiler machinery.
What Does QIR Look Like
Since QIR is based on LLVM, QIR looks like LLVM.
For example, here is some simple Q# code to generate a Bell pair:
// Assumes that qb1 and qb2 are already in the |0> state
operation BellPair(qb1 : Qubit, qb2 : Qubit) : Unit
{
H(qb1);
CNOT(qb1, qb2);
}