ψQuantum PL WIKITHE LANGUAGE OF QUANTUM COMPUTATION中文
VOLUME III · Program semantics

Weakest Preconditions

Weakest Preconditions

Trace adjoints, Löwner order, and fixed points transform quantum postconditions backward into wp and wlp.

A weakest precondition (wp) transformer moves a quantum postcondition effect backward through a program. Let E=⟦C⟧\mathcal E=\llbracket C\rrbracket be a completely positive trace-nonincreasing map and let QQ be an output effect. Its trace adjoint E∗\mathcal E^* is characterized by

tr⁡(E∗(Q)ρ)=tr⁡(QE(ρ)).\operatorname{tr}(\mathcal E^*(Q)\rho)=\operatorname{tr}(Q\mathcal E(\rho)).

This is the Schrödinger forward transformation and Heisenberg backward transformation of Denotational Semantics. Quantum Hoare Logic uses the same duality to express triples.

§6.1 Predicates, Löwner order, and semantic wp

A quantum predicate is a Hermitian effect 0⪯P⪯I0\preceq P\preceq I. The Löwner order A⪯BA\preceq B means that B−AB-A is positive semidefinite, equivalently

A⪯B⟺tr⁡(Aρ)≤tr⁡(Bρ)for every density operator ρ.A\preceq B\quad\Longleftrightarrow\quad \operatorname{tr}(A\rho)\le\operatorname{tr}(B\rho)\quad\text{for every density operator }\rho.

The order therefore compares satisfaction degrees on all states. The effect set is a CPO under this order but generally not a lattice; quantum predicates do not always have a usable conjunction or disjunction. As in the measurement rules on Quantum Hoare Rules, a joint specification must use a tensor product, a Kraus-weighted sum, or another explicit quantum combination.

For a quantum operation E\mathcal E, write {P}E{Q}\{P\}\mathcal E\{Q\} when

tr⁡(Pρ)≤tr⁡(QE(ρ))∀ρ.\operatorname{tr}(P\rho)\le\operatorname{tr}(Q\mathcal E(\rho)) \quad\forall\rho.

wp⁡(E)(Q)\operatorname{wp}(\mathcal E)(Q) is the greatest admissible input effect: it is itself a precondition, and every other precondition PP satisfies P⪯wp⁡(E)(Q)P\preceq\operatorname{wp}(\mathcal E)(Q). The word “weakest” follows the textbook's order convention; this greatest effect should not be read as an unstated Boolean implication.

If E(ρ)=∑iEiρEi†\mathcal E(\rho)=\sum_iE_i\rho E_i^\dagger, then

wp⁡(E)(Q)=E∗(Q)=∑iEi†QEi.(6.4)\operatorname{wp}(\mathcal E)(Q)=\mathcal E^*(Q)=\sum_iE_i^\dagger Q E_i. \tag{6.4}

For a unitary UρU†U\rho U^\dagger, wp⁡U(Q)=U†QU\operatorname{wp}_U(Q)=U^\dagger Q U. If a measurement with Kraus operators MmM_m discards its outcome,

wp⁡meas(Q)=∑mMm†QMm.\operatorname{wp}_{meas}(Q)=\sum_mM_m^\dagger Q M_m.

When an outcome only selects a later branch and is not an explicit output, outcome-indexed branch predicates can handle it without adding a classical register. If the outcome is explicitly retained as program state or output, the postcondition space must include a classical register; the discarded-outcome formula cannot be applied unchanged to a branching program.

Schrödinger–Heisenberg duality

The forward state and backward predicate transformers are

E:ρ↦E(ρ),E∗:Q↦E∗(Q),\mathcal E:\rho\mapsto\mathcal E(\rho),\qquad \mathcal E^*:Q\mapsto\mathcal E^*(Q),

with

tr⁡(QE(ρ))=tr⁡(E∗(Q)ρ).(6.9)\operatorname{tr}(Q\mathcal E(\rho)) =\operatorname{tr}(\mathcal E^*(Q)\rho). \tag{6.9}

Executing the program and then observing QQ therefore has the same satisfaction degree as observing wp⁡(Q)\operatorname{wp}(Q) before execution. In a Kraus representation, E∗\mathcal E^* is completely positive; trace preservation gives E∗(I)=I\mathcal E^*(I)=I, while trace nonincrease gives E∗(I)⪯I\mathcal E^*(I)\preceq I.

Proposition 6.4 of the textbook also gives

wp⁡(λE)=λwp⁡(E),wp⁡(E+F)=wp⁡(E)+wp⁡(F),\operatorname{wp}(\lambda\mathcal E)=\lambda\operatorname{wp}(\mathcal E),\quad \operatorname{wp}(\mathcal E+\mathcal F)=\operatorname{wp}(\mathcal E)+\operatorname{wp}(\mathcal F),

whenever the results remain quantum operations. With the Chapter 5 composition convention, E∘F\mathcal E\circ\mathcal F executes F\mathcal F first and then E\mathcal E, so reverse composition is

wp⁡(E∘F)(Q)=wp⁡(F)(wp⁡(E)(Q)).\operatorname{wp}(\mathcal E\circ\mathcal F)(Q) =\operatorname{wp}(\mathcal F)(\operatorname{wp}(\mathcal E)(Q)).

The transformer also preserves suprema of increasing operation sequences in the CPO. These properties support structural program rules and loop fixed points.

A commutativity warning

Commuting M,NM,N do not generally imply that wp⁡(E)(M)\operatorname{wp}(\mathcal E)(M) and wp⁡(E)(N)\operatorname{wp}(\mathcal E)(N) commute. Unitary conjugation preserves commutativity. A discarded projective measurement requires each compressed pair PkMPkP_kMP_k and PkNPkP_kNP_k to commute; for a complete basis measurement, all weakest preconditions commute. General Kraus maps need extra assumptions, such as mutual commutativity of M,NM,N and commutation of each with all Kraus operators. See §6.1.2 of Quantum Hoare Logic for the more detailed sufficient conditions; wp is not an ordinary function that automatically preserves every algebraic relation between predicates.

§6.2 Total and partial correctness: wp and wlp

wp corresponds to total correctness:

wp⁡C(Q)=E∗(Q).\operatorname{wp}_C(Q)=\mathcal E^*(Q).

Thus

⊨tot{P}C{Q}⟺P⪯wp⁡C(Q).(6.12’)\models_{tot}\{P\}C\{Q\} \quad\Longleftrightarrow\quad P\preceq\operatorname{wp}_C(Q). \tag{6.12'}

The weakest liberal precondition (wlp) corresponds to partial correctness:

wlp⁡C(Q)=E∗(Q)+I−E∗(I).\operatorname{wlp}_C(Q)=\mathcal E^*(Q)+I-\mathcal E^*(I).

Since E∗(I)⪯I\mathcal E^*(I)\preceq I, wlp is still an effect, and

⊨par{P}C{Q}⟺P⪯wlp⁡C(Q).(6.13’)\models_{par}\{P\}C\{Q\} \quad\Longleftrightarrow\quad P\preceq\operatorname{wlp}_C(Q). \tag{6.13'}

For every partial density operator ρ\rho,

tr⁡(wlp⁡C(Q)ρ)=tr⁡(QE(ρ))+tr⁡(ρ)−tr⁡(E(ρ)).\operatorname{tr}(\operatorname{wlp}_C(Q)\rho) =\operatorname{tr}(Q\mathcal E(\rho)) +\operatorname{tr}(\rho)-\operatorname{tr}(\mathcal E(\rho)).

The final two terms together compensate for nontermination. If the program is trace-preserving, wp and wlp agree. A never-terminating program has wp⁡(Q)=0\operatorname{wp}(Q)=0 but wlp⁡(Q)=I\operatorname{wlp}(Q)=I. Total correctness for an arbitrary precondition imposes only the corresponding weighted obligation: P=IP=I is what forces global termination on normalized inputs, while P=0P=0 makes the triple trivial.

§6.3 Structural program equations

For syntax, wp (and wlp with each subprogram wp replaced by wlp) satisfies

wp⁡skip(Q)=Q,wp⁡q:=U[q](Q)=U†QU,wp⁡C1;C2(Q)=wp⁡C1(wp⁡C2(Q)),wp⁡if(M→Cm)(Q)=∑mMm†wp⁡Cm(Q)Mm.\begin{aligned} \operatorname{wp}_{skip}(Q)&=Q,\\ \operatorname{wp}_{q:=U[q]}(Q)&=U^\dagger Q U,\\ \operatorname{wp}_{C_1;C_2}(Q)&=\operatorname{wp}_{C_1}(\operatorname{wp}_{C_2}(Q)),\\ \operatorname{wp}_{if(M\to C_m)}(Q)&=\sum_mM_m^\dagger\operatorname{wp}_{C_m}(Q)M_m. \end{aligned}

Sequencing reverses execution order: calculate the transformer for C2C_2 first, then for C1C_1. For Boolean initialization,

wp⁡q:=∣0⟩(Q)=∣0⟩ ⁣⟨0∣qQ∣0⟩ ⁣⟨0∣q+∣1⟩ ⁣⟨0∣qQ∣0⟩ ⁣⟨1∣q;\operatorname{wp}_{q:=|0\rangle}(Q)= |0\rangle\!\langle0|_q Q|0\rangle\!\langle0|_q +|1\rangle\!\langle0|_q Q|0\rangle\!\langle1|_q;

for an integer register, sum ∣n⟩⟨0∣Q∣0⟩⟨n∣|n\rangle\langle0|Q|0\rangle\langle n| over all basis values. This preserves the full operator action on entangled and untouched registers.

Loop least and greatest fixed points

Let the loop be while M[q] = 1 do S od, with M0†M0+M1†M1=IM_0^\dagger M_0+M_1^\dagger M_1=I and postcondition effect PP. Define

Fwp(X)=M0†PM0+M1†wp⁡S(X)M1,F_{wp}(X)=M_0^\dagger P M_0+M_1^\dagger\operatorname{wp}_S(X)M_1,
Fwlp(X)=M0†PM0+M1†wlp⁡S(X)M1.F_{wlp}(X)=M_0^\dagger P M_0+M_1^\dagger\operatorname{wlp}_S(X)M_1.

Then

wp⁡while(P)=lfp⁡(Fwp)=⨆n≥0Xn,X0=0,Xn+1=Fwp(Xn),\operatorname{wp}_{while}(P)=\operatorname{lfp}(F_{wp}) =\bigsqcup_{n\ge0}X_n, \quad X_0=0,\quad X_{n+1}=F_{wp}(X_n),

while

wlp⁡while(P)=gfp⁡(Fwlp),\operatorname{wlp}_{while}(P)=\operatorname{gfp}(F_{wlp}),

which can be approached downward from Y0=IY_0=I. The least fixed point collects the terminating contribution justified by finite unfoldings; the greatest fixed point also credits permitted nontermination. For every ρ\rho,

tr⁡(wp⁡while(P)ρ)=tr⁡(P⟦while⟧(ρ)),\operatorname{tr}(\operatorname{wp}_{while}(P)\rho) =\operatorname{tr}(P\llbracket while\rrbracket(\rho)),
tr⁡(wlp⁡while(P)ρ)=tr⁡(P⟦while⟧(ρ))+tr⁡(ρ)−tr⁡(⟦while⟧(ρ)).\operatorname{tr}(\operatorname{wlp}_{while}(P)\rho) =\operatorname{tr}(P\llbracket while\rrbracket(\rho)) +\operatorname{tr}(\rho)-\operatorname{tr}(\llbracket while\rrbracket(\rho)).

These least/greatest fixed-point statements are Proposition 6.14 (printed pp. 113–114). They do not say that every loop's termination mass is automatically zero. Total correctness also needs the ranking-function obligation in §qTD of Quantum Hoare Rules or another proved termination argument.

A gate and measurement example

For the Pauli XX gate,

wp⁡X(∣1⟩⟨1∣)=∣0⟩⟨0∣.\operatorname{wp}_X(|1\rangle\langle1|)=|0\rangle\langle0|.

The satisfaction of “input is ∣0⟩|0\rangle” therefore equals the satisfaction of “output is ∣1⟩|1\rangle” after XX. If a computational-basis measurement discards its outcome, the wp of Q=∣+⟩⟨+∣Q=|+\rangle\langle+| is I/2I/2: after dephasing, every normalized input has probability 1/21/2 of a subsequent ++ outcome. These are semantic calculations, not machine-checked proofs of a hardware or compiler implementation.

Relation to proof systems and scope

wp and wlp are backward proof obligations: prove P⪯wp⁡C(Q)P\preceq\operatorname{wp}_C(Q) for a total triple and P⪯wlp⁡C(Q)P\preceq\operatorname{wlp}_C(Q) for a partial triple. qPD and qTD use the same transformers for case, sequencing, and loop rules, providing partial and total correctness respectively; qTD additionally requires an (A,ϵ)(A,\epsilon)-ranking function on the measured continuing branch. Soundness means that derivability implies semantic validity. Relative completeness depends on the assertion language, complex-field Löwner-order facts, and the ability to express the required fixed points and ranking functions; it does not mean that proofs are generated automatically.

An outcome used only to select a control branch can be handled by outcome-dependent predicates; explicitly storing or outputting the outcome requires an extended classical–quantum state space. Postselection must specify the meaning of its trace loss, and infinite-dimensional spaces require additional convergence and domain conditions. Numerical matrix comparisons need justified error bounds, especially near zero eigenvalues.

References and page index

This entry was checked against Chapter 6 of Foundations of Quantum Programming: Definitions 6.2–6.4 and Propositions 6.1–6.4 are PDF pp. 5–9 / printed pp. 95–99; commutativity Propositions 6.5–6.10 are PDF pp. 10–12 / printed pp. 100–102; correctness Definitions 6.7–6.8 are PDF pp. 13–14 / printed pp. 103–104; program Definition 6.9 and Propositions 6.12–6.14 are PDF pp. 18–24 / printed pp. 108–114. Full qPD/qTD rules, soundness and relative completeness, Grover, and auxiliary rules are summarized in Quantum Hoare Logic and Quantum Hoare Rules; long proofs and all exercises are not reproduced.

  • Ellie D'Hondt and Prakash Panangaden, Quantum Weakest Preconditions, arXiv:quant-ph/0501157.
  • Mingsheng Ying, Foundations of Quantum Programming, Chapter 6, 2024, DOI.
  • Roman Gielerak and Marek Sawerwain, Generalised quantum weakest preconditions, arXiv:0710.5239.
An introductory guide. Consult the original references for technical details and formal citations.
READ NEXTVerification Methods and Tools→

Search the collection