Quantum Programming Languages
Quantum Programming Languages
Quantum while syntax, measurement control, small-step operational semantics, and classical recursion connect circuits to program proofs.
A quantum programming language organizes quantum operations into compositional programs. Three layers matter: syntax specifies legal program forms, typing and resource rules constrain variable use, and semantics defines execution behavior. Compilation may produce quantum circuits, while programs with measurement feedback also need classical control and runtime support.
Prerequisites and variables
The prerequisites are density operators, unitaries, and measurements from Quantum States and Operations. This entry follows the quantum while language in Chapter 5 of Ying's Foundations of Quantum Programming, second edition. A quantum variable has a Hilbert-space type . The Boolean type is a two-dimensional quantum system, not merely a classical Boolean value. The integer type uses a countable basis indexed by integers. A register is a finite sequence of distinct variables; its space is their tensor product.
The core language uses quantum data and classical control: classical measurement outcomes select the next command. It does not introduce a separate classical store. Hybrid languages with explicit classical variables extend this model. Linear types are one approach to resource management, but Chapter 5's variable types should not be mistaken for a complete linear typing discipline.
Core syntax
Definition 5.1 gives six constructs. Here if M[q] = m -> S_m fi abbreviates a measurement conditional listing all outcomes:
S ::= skip
| q := |0>
| qbar := U[qbar]
| S1; S2
| if M[qbar] = m -> S_m fi
| while M[qbar] = 1 do S od
skipleaves the state unchanged and terminates immediately.q := |0>initializes or resets a variable. It neither copies an unknown state nor generally acts unitarily.qbar := U[qbar]applies a unitary to that register.S1; S2runsS1first, then runsS2if the first command terminates.- A conditional performs one measurement with , then executes for the observed outcome.
- A loop tests a binary measurement : outcome 0 exits; outcome 1 runs the body and returns to the guard measurement.
The measurement conditional generalizes a classical case statement. It does not successively test nondisturbing classical guards. General measurement operators need not be orthogonal projections. Outcomes are mutually exclusive within a run, but the measurement changes the state. This control also differs from a coherent quantum case statement, which is a separate construct studied in Chapter 11.
Definition 5.2 recursively defines the variables : sequencing takes a union, a conditional includes its measured register and every branch's variables, and a loop includes both guard and body variables.
See Quantum While Programs: Syntax and Execution for the reset formula and a comparison of pure-state and mixed-state presentations.
Configurations and small-step operational semantics
A configuration in Definition 5.3 is , with a partial density operator and . The symbol denotes termination. A transition
records one execution step, leaving residual command and state . Local operators are implicitly extended as on external registers. The central rules from Figure 5.1 are below, with :
The reset map is , where ranges over 0 and 1 for a qubit, or all integers for the integer type. Sequencing lifts to , with .
Measurement branches are not renormalized in these rules. For normalized input, is the outcome probability; the conditional state is only when . Subnormalized states encode probability and conditional state together. Renormalizing each branch and then summing them would give the wrong result.
Examples: feedback and termination
Let and consider q := |0>; H[q]; if M[q]=0 -> skip | 1 -> X[q] fi, with . Both measurement outcomes have probability . After correcting outcome 1 with , both terminal branches carry . Their sum is . This is measurement-and-feedback reset, not a unitary transformation.
For while M[q]=1 do skip od on input , half the mass exits immediately in ; the other half reaches and continues forever. The output partial density operator is . Replacing the body with X[q] changes the continuing branch to , which exits at the next guard measurement, so termination probability becomes 1. Denotational semantics collects these execution paths into a map.
Recursion with classical control
Section 5.5 adds procedure identifiers and declarations , permitting both self-calls and mutual recursion. A recursive program comprises a main statement and a declaration. The call rule in Figure 5.6 is
For example, X <- if M[q]=0 -> skip | 1 -> S; X fi implements the while loop above. Expanding a procedure body does not copy its quantum resources. Control still follows measurement outcomes; recursion with quantum control belongs to Chapter 12 and needs a different treatment. See Denotational Semantics for Section 5.5's finite unfoldings and least fixed points.
From a Grover program to a proof
Section 5.4 expresses Grover search as initialization, Hadamards, iterations controlled by a counter-register measurement, and a final measurement. The loop body contains an oracle, diffusion, and counter increment; the counter is still a quantum variable in the core model. Choosing an iteration count is not itself a success-probability proof: Section 6.6 supplies assertions and a proof using quantum Hoare logic.
Implementations and scope
Quipper and Q# make different choices about classical variables, allocation, measurement interfaces, and hardware control. The textbook syntax is not their API. Mid-circuit measurement, feedback, and dynamic control depend on the backend. These rules describe ideal program execution; compilation or a simulation run does not establish algorithm correctness.
References
- Mingsheng Ying. Foundations of Quantum Programming, second edition (2024), Chapter 5, pp. 63–90: Definitions 5.1–5.6, Figure 5.1, and Sections 5.4–5.5.
- Alexander S. Green, Peter LeFanu Lumsdaine, Neil J. Ross, Peter Selinger, and Benoît Valiron. Quipper: A Scalable Quantum Programming Language (2013). arXiv:1304.3390.
- Microsoft. Introduction to the quantum programming language Q#. Microsoft Learn.