ψQuantum PL WIKITHE LANGUAGE OF QUANTUM COMPUTATION中文
VOLUME IV · Logic & verification

Quantum Hoare Rules and Loop Proofs

Quantum Hoare Rules and Loop Proofs

Basic qPD and qTD rules, loop invariants, ranking functions, and side conditions for the quantum while language.

This page follows Quantum While Programs: Syntax and Execution and uses the notation of Quantum Hoare Logic and Weakest Preconditions. We use finite-dimensional registers, unnormalized execution branches, and denotational semantics that collects terminating paths. Let E=⟦S⟧E=\llbracket S\rrbracket. Assertions are effects 0⪯P⪯I0\preceq P\preceq I, and A⪯BA\preceq B means that B−AB-A is positive semidefinite.

Assertions and two notions of correctness

For every partial density operator ρ\rho, total and partial correctness require respectively

⊨tot{P}S{Q}⟺tr⁡(Pρ)≤tr⁡(QE(ρ)),\models_{tot}\{P\}S\{Q\}\Longleftrightarrow \operatorname{tr}(P\rho)\le\operatorname{tr}(QE(\rho)),
⊨par{P}S{Q}⟺tr⁡(Pρ)≤tr⁡(QE(ρ))+tr⁡(ρ)−tr⁡(E(ρ)).\models_{par}\{P\}S\{Q\}\Longleftrightarrow \operatorname{tr}(P\rho)\le\operatorname{tr}(QE(\rho)) +\operatorname{tr}(\rho)-\operatorname{tr}(E(\rho)).

Subscripts tt and pp below denote total and partial correctness. Total correctness imposes a termination obligation only over the effect promised by the precondition. An arbitrary total triple does not imply global termination on every input: P=IP=I forces termination on normalized inputs, while P=0P=0 gives a trivial triple. A derivation must keep one correctness convention throughout.

Basic axioms

{P} skip {P}∗,{U†PU} qˉ:=U[qˉ] {P}∗.\{P\}\ \mathbf{skip}\ \{P\}_*,\qquad \{U^\dagger P U\}\ \bar q:=U[\bar q]\ \{P\}_*.

Initialization is defined by Rq(ρ)=∑iAiρAi†\mathcal R_q(\rho)=\sum_iA_i\rho A_i^\dagger, where Ai=∣0⟩⟨i∣q⊗IA_i=|0\rangle\langle i|_q\otimes I. Its rule is

{∑iAi†PAi} q:=0 {P}∗.\left\{\sum_i A_i^\dagger P A_i\right\}\ q:=0\ \{P\}_*.

The precondition therefore cannot be obtained by mechanically substituting 0 for qq in a matrix assertion. For the single-qubit postcondition P=∣0⟩⟨0∣P=|0\rangle\langle0|, the calculated precondition is II.

Sequencing and consequence

{P}S1{R}∗{R}S2{Q}∗{P}S1;S2{Q}∗.\frac{\{P\}S_1\{R\}_*\quad\{R\}S_2\{Q\}_*} {\{P\}S_1;S_2\{Q\}_*}.
P⪯P′{P′}S{Q′}∗Q′⪯Q{P}S{Q}∗.\frac{P\preceq P'\quad\{P'\}S\{Q'\}_*\quad Q'\preceq Q} {\{P\}S\{Q\}_*}.

The directions follow from the trace inequalities: decreasing the precondition effect or increasing the postcondition effect preserves validity. Classical set implication cannot simply replace the Löwner order.

Measurement case rule

If {Pm}Sm{Q}∗\{P_m\}S_m\{Q\}_* holds for every outcome mm and ∑mMm†Mm=I\sum_mM_m^\dagger M_m=I, then

{∑mMm†PmMm}if (□mM=m→Sm) fi {Q}∗.\left\{\sum_mM_m^\dagger P_mM_m\right\} \mathbf{if}\ (\square_m M=m\to S_m)\ \mathbf{fi}\ \{Q\}_*.

These are branches of one measurement, not successive tests of independent Boolean conditions. Measurement disturbance is represented by MmM_m on both sides. An input entangled with untouched registers must still be interpreted on a common Hilbert space.

qPD: the partial-correctness loop rule

Let W=while M=1 do S odW=\mathbf{while}\ M=1\ \mathbf{do}\ S\ \mathbf{od} with measurement {M0,M1}\{M_0,M_1\}. Choose PP as the assertion before the body on the continuing branch and QQ as the postcondition after exit, and set

R=M0†QM0+M1†PM1.R=M_0^\dagger QM_0+M_1^\dagger PM_1.

The qPD loop rule is

{P}S{R}p{R}W{Q}p.\frac{\{P\}S\{R\}_p}{\{R\}W\{Q\}_p}.

RR is the assertion before the measurement, PP constrains only states entering the body on the continuing branch, and QQ constrains exit states. They are generally different. The rule permits a divergent body or loop; the partial-correctness compensation term accounts for remaining mass. It is the same rule as (R.LP) on the main Quantum Hoare Logic page.

qPD also contains skip, initialization, unitary, sequencing, case, and Löwner consequence rules. Theorem 6.1 (printed p. 117) proves qPD sound, and Theorem 6.2 (printed p. 120) proves relative completeness, assuming facts about the complex field and an assertion language expressive enough for the required wlp. Relative completeness does not mean that a tool will synthesize an invariant automatically.

qTD: total correctness and ranking functions

Simply replacing pp by tt in the qPD loop rule is unsound: a nonterminating loop may still satisfy the body-preservation premise. Definition 6.10 (printed p. 121) calls

tϵ:D(Hall)→Nt_\epsilon:\mathcal D(\mathcal H_{all})\to\mathbb N

an (A,ϵ)(A,\epsilon)-ranking function, for an effect AA and ϵ>0\epsilon>0, exactly when, for every partial density operator ρ\rho,

T(ρ)=⟦S⟧(M1ρM1†),tϵ(T(ρ))≤tϵ(ρ),T(\rho)=\llbracket S\rrbracket(M_1\rho M_1^\dagger),\qquad t_\epsilon(T(\rho))\le t_\epsilon(\rho),

and

tr⁡(Aρ)≥ϵ⟹tϵ(T(ρ))<tϵ(ρ).\operatorname{tr}(A\rho)\ge\epsilon \Longrightarrow t_\epsilon(T(\rho))<t_\epsilon(\rho).

The measured continuing branch must be part of TT; the classical comparison t(⟦S⟧(ρ))<t(ρ)t(\llbracket S\rrbracket(\rho))<t(\rho) is not the quantum obligation. Well-foundedness of N\mathbb N rules out an infinite strictly descending chain.

With this page's notation R=M0†QM0+M1†PM1R=M_0^\dagger QM_0+M_1^\dagger PM_1, the textbook (R.LT) rule (Figure 6.5, printed p. 123) is

{P}S{R}t∀ϵ>0, tϵ is a (M1†PM1,ϵ)-ranking function{R}W{Q}t.\frac{ \{P\}S\{R\}_t\quad \forall\epsilon>0,\ t_\epsilon\text{ is a }(M_1^\dagger PM_1,\epsilon)\text{-ranking function} }{ \{R\}W\{Q\}_t}.

The textbook figure uses PP for the final assertion and QQ for the body assertion, exactly the opposite letter convention from this page; the rule has the same meaning. The ranking function concerns mass relevant to the precondition and does not require global termination for every input. Neither the body's total-correctness premise nor the condition for every positive threshold ϵ\epsilon may be dropped.

Lemma 6.6 (printed p. 122) gives the equivalent residual-limit criterion. For any ranking-related effect AA, an (A,ϵ)(A,\epsilon)-ranking function exists for every ϵ>0\epsilon>0 exactly when

lim⁡n→∞tr⁡(A(⟦S⟧∘E1)n(ρ))=0,E1(ρ)=M1ρM1†,\lim_{n\to\infty}\operatorname{tr}\left(A(\llbracket S\rrbracket\circ E_1)^n(\rho)\right)=0, \qquad E_1(\rho)=M_1\rho M_1^\dagger,

for every ρ\rho. In this page's (R.LT), substitute A=M1†PM1A=M_1^\dagger PM_1, where PP is the body precondition. This is the vanishing of residual mass during continuing iterations as observed by AA, not a claim that a global termination mass automatically vanishes for an arbitrary precondition. Theorem 6.3 (printed p. 124) and Theorem 6.4 (printed p. 126) establish qTD soundness and relative completeness.

Weakest preconditions and fixed points

For the trace adjoint E∗E^*,

wp⁡S(Q)=E∗(Q),wlp⁡S(Q)=E∗(Q)+I−E∗(I),\operatorname{wp}_S(Q)=E^*(Q),\qquad \operatorname{wlp}_S(Q)=E^*(Q)+I-E^*(I),

and total and partial triples are equivalent to P⪯wp⁡S(Q)P\preceq\operatorname{wp}_S(Q) and P⪯wlp⁡S(Q)P\preceq\operatorname{wlp}_S(Q) respectively. A loop wp is obtained as the least fixed point by iterating

FQ(X)=M0†QM0+M1†wp⁡S(X)M1F_Q(X)=M_0^\dagger QM_0+M_1^\dagger\operatorname{wp}_S(X)M_1

from the zero effect. Replacing the body wp by wlp and approximating downward from II gives the greatest fixed point. See Weakest Preconditions for the transformer equations.

Example: a measurement loop that resets a qubit

For computational-basis measurement and body XX, choose exit postcondition Q=∣0⟩⟨0∣Q=|0\rangle\langle0| and continuing-branch body precondition P=∣1⟩⟨1∣P=|1\rangle\langle1|. Then R=IR=I and {P}X{I}p\{P\}X\{I\}_p holds, so qPD yields {I}W{Q}p\{I\}W\{Q\}_p. After XX, the continuing branch exits at the next measurement, so this particular loop terminates almost surely and the same triple is totally correct. That termination fact belongs to the example; it is not a consequence of the qPD loop rule alone.

References and audit page index

This page is aligned with the main Quantum Hoare Logic page and checked against Chapter 6 of Foundations of Quantum Programming: qPD Figure 6.3 is PDF p. 25 / printed p. 115; qPD soundness and completeness are PDF pp. 27 and 30 / printed pp. 117 and 120; Definition 6.10 is PDF p. 31 / printed p. 121; Lemma 6.6 is PDF p. 32 / printed p. 122; qTD Figure 6.5 and (R.LT) are PDF p. 33 / printed p. 123; qTD soundness and completeness are PDF pp. 34 and 36 / printed pp. 124 and 126. Definitions, structural wp/wlp equations, and auxiliary rules are summarized on the main page.

  • Mingsheng Ying, Foundations of Quantum Programming, Chapter 6, 2024, DOI.
  • Mingsheng Ying, Floyd–Hoare Logic for Quantum Programs, 2011, DOI.
An introductory guide. Consult the original references for technical details and formal citations.
READ NEXTWeakest Preconditions→

Search the collection