ψQuantum PL WIKITHE LANGUAGE OF QUANTUM COMPUTATION中文
VOLUME II · Languages & types

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 qq has a Hilbert-space type Hq\mathcal H_q. 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 qˉ\bar q 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
  • skip leaves 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; S2 runs S1 first, then runs S2 if the first command terminates.
  • A conditional performs one measurement M={Mm}M=\{M_m\} with ∑mMm†Mm=I\sum_m M_m^\dagger M_m=I, then executes SmS_m for the observed outcome.
  • A loop tests a binary measurement {M0,M1}\{M_0,M_1\}: 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 var⁡(S)\operatorname{var}(S): 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 ⟨S,ρ⟩\langle S,\rho\rangle, with a partial density operator ρ⪰0\rho\succeq0 and tr⁡(ρ)≤1\operatorname{tr}(\rho)\leq1. The symbol ↓\downarrow denotes termination. A transition

⟨S,ρ⟩⟶⟨S′,ρ′⟩\langle S,\rho\rangle\longrightarrow\langle S',\rho'\rangle

records one execution step, leaving residual command S′S' and state ρ′\rho'. Local operators are implicitly extended as A⊗IA\otimes I on external registers. The central rules from Figure 5.1 are below, with W=while M[qˉ]=1 do S odW=\texttt{while }M[\bar q]=1\texttt{ do }S\texttt{ od}:

⟨skip,ρ⟩→⟨↓,ρ⟩,⟨q:=∣0⟩,ρ⟩→⟨↓,Rq(ρ)⟩,⟨U[qˉ],ρ⟩→⟨↓,UρU†⟩,⟨if M[qˉ]=m→Sm fi,ρ⟩→⟨Sm,MmρMm†⟩,⟨W,ρ⟩→⟨↓,M0ρM0†⟩,⟨W,ρ⟩→⟨S;W,M1ρM1†⟩.\begin{aligned} \langle\mathrm{skip},\rho\rangle&\to\langle\downarrow,\rho\rangle,\\ \langle q:=|0\rangle,\rho\rangle&\to\langle\downarrow,\mathcal R_q(\rho)\rangle,\\ \langle U[\bar q],\rho\rangle&\to\langle\downarrow,U\rho U^\dagger\rangle,\\ \langle\mathrm{if}\ M[\bar q]=m\to S_m\ \mathrm{fi},\rho\rangle&\to\langle S_m,M_m\rho M_m^\dagger\rangle,\\ \langle W,\rho\rangle&\to\langle\downarrow,M_0\rho M_0^\dagger\rangle,\\ \langle W,\rho\rangle&\to\langle S;W,M_1\rho M_1^\dagger\rangle. \end{aligned}

The reset map is Rq(ρ)=∑j∣0⟩q⟨j∣ρ∣j⟩q⟨0∣\mathcal R_q(\rho)=\sum_j |0\rangle_q\langle j|\rho|j\rangle_q\langle0|, where jj ranges over 0 and 1 for a qubit, or all integers for the integer type. Sequencing lifts ⟨S1,ρ⟩→⟨S1′,ρ′⟩\langle S_1,\rho\rangle\to\langle S_1',\rho'\rangle to ⟨S1;S2,ρ⟩→⟨S1′;S2,ρ′⟩\langle S_1;S_2,\rho\rangle\to\langle S_1';S_2,\rho'\rangle, with ↓;S2=S2\downarrow;S_2=S_2.

Measurement branches are not renormalized in these rules. For normalized input, pm=tr⁡(MmρMm†)p_m=\operatorname{tr}(M_m\rho M_m^\dagger) is the outcome probability; the conditional state is MmρMm†/pmM_m\rho M_m^\dagger/p_m only when pm>0p_m>0. 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 Pb=∣b⟩⟨b∣P_b=|b\rangle\langle b| and consider q := |0>; H[q]; if M[q]=0 -> skip | 1 -> X[q] fi, with M={P0,P1}M=\{P_0,P_1\}. Both measurement outcomes have probability 1/21/2. After correcting outcome 1 with XX, both terminal branches carry 12∣0⟩⟨0∣\tfrac12|0\rangle\langle0|. Their sum is ∣0⟩⟨0∣|0\rangle\langle0|. This is measurement-and-feedback reset, not a unitary transformation.

For while M[q]=1 do skip od on input ∣+⟩|+\rangle, half the mass exits immediately in ∣0⟩|0\rangle; the other half reaches ∣1⟩|1\rangle and continues forever. The output partial density operator is 12∣0⟩⟨0∣\tfrac12|0\rangle\langle0|. Replacing the body with X[q] changes the continuing branch to ∣0⟩|0\rangle, 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 XiX_i and declarations D={Xi⇐Si}D=\{X_i\Leftarrow S_i\}, 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

⟨Xi,ρ⟩→D⟨Si,ρ⟩(Xi⇐Si∈D).\langle X_i,\rho\rangle\to_D\langle S_i,\rho\rangle\quad(X_i\Leftarrow S_i\in D).

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.
An introductory guide. Consult the original references for technical details and formal citations.
READ NEXTDenotational Semantics→

Search the collection