Q#

Also known as: Q sharp

On this page 7

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.

ConstructExample syntaxPurpose
Operationoperation Bell() : Result[]Quantum routine that may allocate and measure qubits
Functionfunction Square(x : Int) : IntDeterministic classical computation
Qubit allocationuse qs = Qubit[2];Borrows fresh qubits in the zero state
Measurementlet r = M(q);Returns a Result value of Zero or One
Conjugationwithin { ... } apply { ... }Applies an action and automatically undoes the setup
Namespaces and importsimport Std.Math.*;Organizes code and the standard library
Core constructs of the Q# language

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.

FunctorDeclared withWhat the compiler generatesTypical use
Adjointis AdjThe inverse of the operationUncomputing temporary values
Controlledis CtlA version conditioned on control qubitsPhase estimation and oracles
Bothis Adj + CtlInverse and controlled forms togetherReusable library routines
Q# functors and their effect

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.

TargetWhat it doesMain limitation
Sparse state simulatorRuns programs locally with full classical controlMemory grows exponentially with entangled qubits
Resource estimatorEstimates physical qubits and runtime on fault-tolerant architecturesProduces estimates, not program output
Base profile hardwareExecutes fixed circuits on cloud quantum devicesNo branching on measurement results
Adaptive profile hardwareAllows limited mid-circuit classical logicSupported only by some providers
Execution options for Q# programs

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.

ToolTypeAbstraction levelTypical strength
Q#Dedicated languageHigh, algorithm-orientedTyped operations, functors, resource estimation
QiskitPython SDKCircuit and pulse levelLarge ecosystem and IBM hardware access
CirqPython SDKCircuit level, device-awareFine control for near-term experiments
OpenQASMCircuit description languageLow, gate levelPortable exchange of circuits
QIRIntermediate representationCompiler levelHardware-independent compilation
Q# among neighboring tools in quantum software

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.