Q# (pronounced Q sharp) is a domain-specific programming language for quantum algorithms, developed by Microsoft and distributed with the Microsoft Quantum Development Kit. In quantum software, it occupies the level above circuit description formats: programs are written as typed operations and functions with classical control flow, then compiled to an intermediate representation that simulators and hardware back ends can execute, which makes it one of the main high-level languages in quantum computing.
Q# was first released in 2017. The Modern QDK, introduced in 2024, rebuilt the compiler and tooling for speed and lighter installation, so Q# now runs in a code editor extension, in Python notebooks and in the browser. The language is designed for algorithm development and for resource estimation of future fault-tolerant machines, not only for running small circuits on current devices.
How Q# programs are structured
A Q# program is built from operations, which may act on qubits and have quantum side effects, and functions, which are purely classical and deterministic. This separation lets the compiler reason about which parts of a program can be reversed, controlled or simulated classically.
Qubits are treated as opaque references, not values. A program allocates them with the use statement, applies intrinsic gates such as H, CNOT and Rz, and obtains classical results with measurement operations such as M or MResetZ. Qubits must be returned to a known state before release, which mirrors how physical qubit reuse works on real hardware.
The following table summarizes the core language constructs and their purpose.
| Construct | Example syntax | Purpose |
|---|---|---|
| Operation | operation Bell() : Result[] | Quantum routine that may allocate and measure qubits |
| Function | function Square(x : Int) : Int | Deterministic classical computation |
| Qubit allocation | use qs = Qubit[2]; | Borrows fresh qubits in the zero state |
| Measurement | let r = M(q); | Returns a Result value of Zero or One |
| Conjugation | within { ... } apply { ... } | Applies an action and automatically undoes the setup |
| Namespaces and imports | import Std.Math.*; | Organizes code and the standard library |
Functors and automatic circuit transformations
A distinctive feature of Q# is its functor system, which generates related versions of an operation automatically. Declaring that an operation supports Adj and Ctl lets the compiler derive its adjoint (inverse) and its controlled form without the programmer writing them by hand.
This matters because many quantum algorithms, such as quantum phase estimation and amplitude amplification, require controlled and inverted copies of large subroutines. Writing those by hand is error-prone; deriving them from a single definition keeps the program correct by construction.
| Functor | Declared with | What the compiler generates | Typical use |
|---|---|---|---|
| Adjoint | is Adj | The inverse of the operation | Uncomputing temporary values |
| Controlled | is Ctl | A version conditioned on control qubits | Phase estimation and oracles |
| Both | is Adj + Ctl | Inverse and controlled forms together | Reusable library routines |
Compilation, targets and QIR
Q# source code is compiled to QIR (Quantum Intermediate Representation), an LLVM-based format that describes quantum instructions alongside classical logic. QIR lets the same program be sent to different hardware providers through Azure Quantum, provided the target supports the features the program uses.
Because real devices differ in what classical processing they allow during a circuit, the compiler checks programs against a target profile. A program that branches on mid-circuit measurement results will compile for an adaptive profile target but not for a hardware back end that only accepts fixed circuits.
| Target | What it does | Main limitation |
|---|---|---|
| Sparse state simulator | Runs programs locally with full classical control | Memory grows exponentially with entangled qubits |
| Resource estimator | Estimates physical qubits and runtime on fault-tolerant architectures | Produces estimates, not program output |
| Base profile hardware | Executes fixed circuits on cloud quantum devices | No branching on measurement results |
| Adaptive profile hardware | Allows limited mid-circuit classical logic | Supported only by some providers |
Q# compared with other quantum programming tools
Q# is a standalone language, whereas Qiskit and Cirq are Python libraries that build circuits as data structures. OpenQASM and Quil sit lower, describing circuits close to the gate level, while QIR is a compiler target rather than a language most developers write by hand.
| Tool | Type | Abstraction level | Typical strength |
|---|---|---|---|
| Q# | Dedicated language | High, algorithm-oriented | Typed operations, functors, resource estimation |
| Qiskit | Python SDK | Circuit and pulse level | Large ecosystem and IBM hardware access |
| Cirq | Python SDK | Circuit level, device-aware | Fine control for near-term experiments |
| OpenQASM | Circuit description language | Low, gate level | Portable exchange of circuits |
| QIR | Intermediate representation | Compiler level | Hardware-independent compilation |
In practice the tools are complementary. Q# can be called from Python through the `qsharp` package, and the Modern QDK can import OpenQASM circuits, so teams often combine Python data handling with Q# for the quantum kernels.
Strengths, limitations and common misconceptions
The main strength of Q# is expressing algorithms at scale: its type system, functors and libraries suit large fault-tolerant programs whose cost must be estimated before hardware exists. Its main limitation is a smaller community than Python SDKs and fewer direct hardware integrations.
- Q# is not tied to one machine. Programs compile to QIR and can target several providers, subject to their profiles.
- Simulation is not free. A local simulator handles a few dozen entangled qubits at most, depending on memory and circuit structure.
- Classical code belongs in functions or the host. Heavy data processing is usually better done in Python around Q# calls.
- The classic QDK and the Modern QDK differ. Older samples may use syntax or libraries that the current compiler no longer supports.
In everyday language
Q# is a programming language made for quantum computers. Ordinary languages describe steps for normal processors. Q# describes steps for qubits, such as putting them into superposition, linking them and measuring them.
Its main advantage is that it writes the tedious parts for you. If a program needs to undo a step, Q# can produce the reverse automatically. It can also tell researchers how big a future quantum computer must be to run their program.
Seen in the real world
A chemistry research team wants to know whether simulating a catalyst molecule on a quantum computer is realistic within the next decade. The team writes the phase estimation part of the algorithm in Q#, using functors to generate the controlled versions of its time-evolution routine instead of coding them by hand.
The researchers first test small instances of the molecule on a local simulator to confirm that the energy estimates are correct. They then run the full-size program through the resource estimator with assumptions about qubit error rates of 0.1 percent and 0.01 percent. The result shows that the task needs roughly a million physical qubits at the higher error rate and several times fewer at the lower one, which gives the team a concrete basis for planning hardware requirements rather than guessing.