Authors’ Contact Information: Yuanjie (Collin) Ren, Massachusetts Institute of Technology, Cambridge, MA, USA and Atomic Inference Inc, Cambridge, MA, USA, yuanjie@mit.edu; Zhicheng Zhang, School of Information and Communication Technology, Griffith University, Brisbane, Australia, iszczhang@gmail.com; Junyi Liu, Joint Center for Quantum Information and Computer Science, University of Maryland, College Park, MD, USA; Jinzheng Li, Department of Physics, Northeastern University, Boston, MA, USA; Yidi Qi, Harvard University, Cambridge, MA, USA; Mingsheng Ying, Centre for Quantum Software and Information, University of Technology Sydney, Sydney, Australia, Mingsheng.Ying@uts.edu.au.
Introduction
Classical software engineering relies on testing and debugging: running code, inspecting intermediate states, and fixing bugs. For quantum programs, this practice is constrained by features of quantum mechanics, notably: measurement generally changes the state of a system, and the no-cloning theorem [15 , 44 ] forbids copying an unknown state for offline analysis. Methods for assessing quantum operations, such as quantum process tomography [10 , 30 ] and randomized benchmarking [23 ] , are either expensive or report only average behavior. Formal verification, which proves by mathematical reasoning that a program satisfies its specification, does not require running the program. It is widely used for safety- and security-critical classical software and provides a principled route to correctness guarantees for quantum programs.
The same physics that constrains testing and debugging also reshapes the semantics a verifier must formalize. The programs considered here combine classical and quantum variables and operations. The classical–quantum interplay in such programs runs in both directions: classical variables determine the control flow, the gates applied, the qubits they act on, and their parameters; conversely, a measurement writes its outcome into the classical state, where it can influence the rest of the computation. Reasoning about such programs is the subject of a substantial body of work on quantum program verification [8 , 17 , 20 , 25 , 26 , 46 , 51 ] .
Hoare logic is a cornerstone of classical program verification. Quantum Hoare logic [46 ] was developed for purely quantum programs and later formalized in Isabelle/HOL [26 ] and Coq [51 ] ; a subsequent extension [17 ] added classical variables. QHL† [47 ] extends this line of work with the features needed in practice: quantum arrays, parameterized gates, richer predicate syntax, and additional proof rules for reasoning about hybrid classical–quantum control. Specifications take the form of quantum Hoare triples { φ , A } P { ψ , B } \{\varphi,A\}\ P\ \{\psi,B\} { φ , A } P { ψ , B } , where the precondition ( φ , A ) (\varphi,A) ( φ , A ) and the postcondition ( ψ , B ) (\psi,B) ( ψ , B ) each pair a classical formula with a quantum predicate that may depend on classical variables.
To use QHL+ ^{+} + for machine-checked program verification, we formalize it in the Lean 4 proof assistant de Moura and Ullrich [13 ] . The formalization spans the mathematics of quantum mechanics, program syntax and semantics, and the proof rules, which we prove sound. Mechanized verification of quantum programs, from the underlying libraries to the program proofs, typically requires substantial manual effort, as reflected in mature frameworks such as CoqQ Zhou et al. [51 ] and Qbricks Chareton et al. [8 ] and in the certified end-to-end verification of Shor’s algorithm Peng et al. [33 ] . Mechanizing QHL+ ^{+} + is more demanding still, since the classical–quantum interplay must be handled consistently at each level of the formalization, as the examples in § 3 illustrate.
This proof-writing effort motivates our use of theorem-proving agents that combine large language models (LLMs) with feedback from proof assistants [7 , 24 , 27 , 36 , 42 ] (Baba et al. 2026). The general approach is to generate candidate formal proofs and revise them using the proof assistant’s diagnostics. In our work, LLM agents working under human supervision formalize the quantum program logic, build its supporting library, and construct the example proofs, all checked by Lean’s kernel. To our knowledge, this is the first mechanized quantum program logic built by LLM agents.
This Paper and Its Contributions
This paper presents QEDIA, a framework for verifying quantum programs with classical variables in Lean 4, based on QHL+ ^{+} + and built on Mathlib The mathlib Community [39 ] .2 Its contributions are:
A formalized quantum Hoare logic with proved soundness. QEDIA formalizes QHL+ ^{+} + for quantum while-programs with classical variables, quantum arrays, and parameterized gates. Its proof system is proved sound in Lean for both partial and total correctness. The formalization handles the classical–quantum interplay consistently at every level, from the mathematics of quantum mechanics to the proof system (§ 5 ).
An agent-based proof-writing framework. A general-purpose core agent coordinates LLM agents to construct and review formal proofs, combining Lean checks with review of the intended mathematics. Its quantum extension supports applying QHL+ ^{+} + rules and adds quantum-specific checks (§ 6 ). We expect this workflow to be useful for future AI-assisted formal verification of quantum programs.
A series of verified program examples. We illustrate QEDIA with eleven verified programs: preparation of the Bell state and a 1D cluster state; parallel Hadamard; the quantum Fourier transform and its inverse; quantum phase estimation; a repeat-until-success circuit; Hamiltonian simulation of the transverse-field Ising model by a Trotter product formula; and the Bravyi–Kitaev encoding, block encoding, and quantum signal processing for the Fermi–Hubbard model (§ 7 and Appendix § 12 ). To the best of our knowledge, some of these examples cannot be directly handled by existing quantum program verifiers, since they involve the classical–quantum interplay at both the program and specification levels.
The remainder of the paper is organized as follows. § 3 uses three motivating examples to identify the levels at which the classical–quantum interplay must be handled. § 4 provides background on QHL+ ^{+} + : its programs, assertions, and proof rules. § 5 presents the Lean formalization of QHL+ ^{+} + and shows how it handles this interplay at each level. § 6 describes the prover agent that constructs the library and the program proofs. Section 6 is devoted to the verified program examples, with detailed derivations for the examples of Section 2. Section 7 discusses related work, and Section 8 concludes. Appendix A gives further details of the formalization, and Appendix B presents the remaining verified examples.
Motivating Examples
We now make the classical–quantum interplay described in Section 1 concrete. Through the three programs in Figure 1 , we illustrate the capabilities that a verifier needs in order to handle this interplay at different levels.
Figure 1. The three motivating programs. Classical variables include loop counters, measurement outcomes, and inputs; q q q is a quantum array, and a quantum operation acts on the qubits named in brackets.
Example 2.1 (Repeat-Until-Success). Our first example is a repeat-until-success (RUS) circuit. Such circuits use mid-circuit measurements and classical feedback, in contrast to ordinary unitary synthesis, to reduce the number of expensive T T T gates in implementing certain non-Clifford unitary gates [5 , 32 ] .
The program in Figure 1(a) presents an RUS implementation of the single-qubit unitary gate V R U S = I + i 2 X 3 V_{\mathrm{RUS}} = \frac{I+i\sqrt{2}X}{\sqrt{3}} V RUS = 3 I + i 2 X , using a two-qubit gate W R U S W_{\mathrm{RUS}} W RUS that is easier to construct from a few T T T gates [32 Fig. 8] . Here, d d d is a data qubit, a a a is an ancilla qubit, and N N N is a classical input bounding the number of attempts. The variables N N N , k k k are natural numbers, and the measurement outcome m m m has type { 0 , 1 } \{0,1\} { 0 , 1 } . The main component of the program is a while loop: in each iteration we reset the ancilla qubit a a a to ∣ 0 ⟩ \lvert0\rangle ∣ 0 ⟩ , apply gate W R U S W_{\mathrm{RUS}} W RUS , measure a a a by the computational basis measurement { ∣ 0 ⟩ ⟨ 0 ∣ , ∣ 1 ⟩ ⟨ 1 ∣ } \{\lvert0\rangle\langle0\rvert,\lvert1\rangle\langle1\rvert\} {∣ 0 ⟩ ⟨ 0 ∣ , ∣ 1 ⟩ ⟨ 1 ∣} , and assign the measurement outcome to the classical variable m m m . Given the specific W R U S W_{\mathrm{RUS}} W RUS , each iteration applies V R U S V_{\mathrm{RUS}} V RUS to data qubit d d d with probability 3 / 4 3/4 3/4 (indicated by m = 0 m=0 m = 0 ) and leaves d d d unchanged with probability 1 / 4 1/4 1/4 (indicated by m = 1 m=1 m = 1 ). The loop stops when m = 0 m=0 m = 0 or the number of attempts reaches N N N .
The correctness of the RUS program in Figure 1 (a) consists of two parts: the program terminates after applying V R U S V_{\mathrm{RUS}} V RUS to d d d or exhausting the N N N attempts, and it applies V R U S V_{\mathrm{RUS}} V RUS with probability bounded below in terms of N N N . Using the QHL+ ^{+} + framework, these two correctness conditions can be conveniently formalized as the following two Hoare triples:
⊨ t o t { N ≥ 1 , I ⊗ ∣ ψ 0 ⟩ ⟨ ψ 0 ∣ } R U S { m = 0 ∨ k ≥ N , J ( m ) } (1) \models_{\mathrm{tot}}\{N \ge1,\ I \otimes\lvert\psi_0\rangle\langle\psi_0\rvert\}\ \mathrm{RUS}\ \{m=0 \lor k \ge N,\ \mathcal{J}(m)\} \tag*{(1)} ⊨ tot { N ≥ 1 , I ⊗ ∣ ψ 0 ⟩ ⟨ ψ 0 ∣} RUS { m = 0 ∨ k ≥ N , J ( m )} (1)
and
⊨ t o t { N ≥ 1 , ( 1 − 4 − N ) ( I ⊗ ∣ ψ 0 ⟩ ⟨ ψ 0 ∣ ) } R U S { m = 0 , ∣ w ⟩ ⟨ w ∣ } . (2) \models_{\mathrm{tot}}\{N \ge1,\ (1-4^{-N})(I \otimes\lvert\psi_0\rangle\langle\psi_0\rvert)\}\ \mathrm{RUS}\ \{m=0,\ \lvert w\rangle\langle w\rvert\}. \tag*{(2)} ⊨ tot { N ≥ 1 , ( 1 − 4 − N ) ( I ⊗ ∣ ψ 0 ⟩ ⟨ ψ 0 ∣)} RUS { m = 0 , ∣ w ⟩ ⟨ w ∣} . (2)
Here, ∣ ψ 0 ⟩ |\psi_0\rangle ∣ ψ 0 ⟩ is an arbitrary initial state of d d d ,
J ( m ) = { ∣ w ⟩ ⟨ w ∣ , m = 0 , I ⊗ ∣ ψ 0 ⟩ ⟨ ψ 0 ∣ , m = 1 , and ∣ w ⟩ = ∣ 0 ⟩ ⊗ V R U S ∣ ψ 0 ⟩ . (3) \mathcal{J}(m)=\begin{cases}
|w\rangle\langle w|, & m=0,\\
I\otimes|\psi_0\rangle\langle\psi_0|, & m=1,
\end{cases}
\qquad\text{and}\qquad
|w\rangle=|0\rangle\otimes V_{RUS}|\psi_0\rangle. \tag*{(3)} J ( m ) = { ∣ w ⟩ ⟨ w ∣ , I ⊗ ∣ ψ 0 ⟩ ⟨ ψ 0 ∣ , m = 0 , m = 1 , and ∣ w ⟩ = ∣0 ⟩ ⊗ V R U S ∣ ψ 0 ⟩ . (3)
We omit a classical precondition or postcondition when it is true.
Intuitively, the first Hoare triple (1) guarantees that, with d d d initially in ∣ ψ 0 ⟩ |\psi_0\rangle ∣ ψ 0 ⟩ , the program will terminate and it either applies V R U S V_{RUS} V R U S successfully or exhausts the N N N attempts and leaves d d d in ∣ ψ 0 ⟩ |\psi_0\rangle ∣ ψ 0 ⟩ . The second Hoare triple (2) guarantees that the overall success probability in implementing the desired V R U S V_{RUS} V R U S is at least 1 − 4 − N 1-4^{-N} 1 − 4 − N .
Through the above example, we can see the classical–quantum interplay at both the program and specification levels. For the program, the classical variable m m m controls the while loop, yet its value in each iteration is set by measuring a a a . For the specification, in Hoare triples (1) and (2), the same classical variable m m m selects the successful branch and parameterizes the quantum predicate J ( m ) \mathcal{J}(m) J ( m ) .
Example 2.2 (Quantum Fourier Transform). Our second example is the quantum Fourier transform (QFT). The QFT is a key subroutine of Shor’s factoring algorithm [38 ] and, through its inverse, the readout stage of quantum phase estimation [11 , 22 ] .
The program in Figure 1 (b) presents a standard QFT implementation [12 ] on a quantum array q q q of n n n qubits. Here, q [ j ] q[j] q [ j ] is the j j j -th qubit of array q q q , and we define the controlled phase gate c R ( γ ) ∣ a , b ⟩ = e i γ a b ∣ a , b ⟩ cR(\gamma)|a,b\rangle=e^{i\gamma ab}|a,b\rangle c R ( γ ) ∣ a , b ⟩ = e iγ ab ∣ a , b ⟩ for a , b ∈ { 0 , 1 } a,b\in\{0,1\} a , b ∈ { 0 , 1 } .
The main component of the program is a nested while loop: for each i i i , we first apply the Hadamard gate H H H to qubit q [ i ] q[i] q [ i ] . Then, for each j j j , we apply c R ( 2 π / 2 j + 2 ) cR(2\pi/2^{j+2}) c R ( 2 π / 2 j + 2 ) to qubits q [ i ] q[i] q [ i ] , q [ i + 1 + j ] q[i+1+j] q [ i + 1 + j ] , where both the qubit indices and the phase 2 π / 2 j + 2 2\pi/2^{j+2} 2 π / 2 j + 2 are parameterized by classical variables i i i and j j j . After these produce the Fourier phases, a final loop of SWAP gates reverses the qubit order.
The correctness of the QFT program in Figure 1 (b) states that, for each fixed n ≥ 1 n\ge1 n ≥ 1 , the program terminates and maps computational basis states to the corresponding Fourier basis states. This can be formalized as the following Hoare triple:
⊨ t o t { ∣ x ⟩ ⟨ x ∣ } Q F T n { ∣ χ ( n , x ) ⟩ ⟨ χ ( n , x ) ∣ } . (4) \models_{\mathrm{tot}}\ \{|x\rangle\langle x|\}\ \mathrm{QFT}_n\ \{|\chi(n,x)\rangle\langle\chi(n,x)|\}. \tag*{(4)} ⊨ tot { ∣ x ⟩ ⟨ x ∣ } QFT n { ∣ χ ( n , x )⟩ ⟨ χ ( n , x ) ∣ } . (4) Here, the classical input x x x is an array of bits representing the integer x ˉ = ∑ j = 0 n − 1 x [ j ] 2 n − 1 − j \bar{x}=\sum_{j=0}^{n-1}x[j]2^{n-1-j} x ˉ = ∑ j = 0 n − 1 x [ j ] 2 n − 1 − j in binary, ∣ x ⟩ = ⨂ i = 0 n − 1 ∣ x [ i ] ⟩ |x\rangle=\bigotimes_{i=0}^{n-1}|x[i]\rangle ∣ x ⟩ = ⨂ i = 0 n − 1 ∣ x [ i ]⟩ , and
∣ χ ( n , x ) ⟩ = 1 2 n ∑ y ∈ { 0 , 1 } n e 2 π i x ˉ y ˉ / 2 n ∣ y ⟩ |\chi(n,x)\rangle=\frac{1}{\sqrt{2^n}}\sum_{y\in\{0,1\}^n}e^{2\pi i\bar{x}\bar{y}/2^n}|y\rangle ∣ χ ( n , x )⟩ = 2 n 1 y ∈ { 0 , 1 } n ∑ e 2 π i x ˉ y ˉ / 2 n ∣ y ⟩ is the Fourier basis state.
Intuitively, the Hoare triple (4) guarantees that, with q q q initially in an arbitrary computational basis state ∣ x ⟩ |x\rangle ∣ x ⟩ , the program will terminate and leave q q q in the corresponding Fourier basis state ∣ χ ( n , x ) ⟩ |\chi(n,x)\rangle ∣ χ ( n , x )⟩ . We further verify the QFT on arbitrary inputs in § 7.2 .
The above example also illustrates the classical–quantum interplay at the program and specification levels. For the program, the classical variables i , j i,j i , j determine both the qubit indices and the phase of the controlled phase gate. For the specification, the quantum predicates ∣ x ⟩ ⟨ x ∣ |x\rangle\langle x| ∣ x ⟩ ⟨ x ∣ and ∣ χ ( n , x ) ⟩ ⟨ χ ( n , x ) ∣ |\chi(n,x)\rangle\langle\chi(n,x)| ∣ χ ( n , x )⟩ ⟨ χ ( n , x ) ∣ both depend on the array x x x .
Example 2.3 (The Trotter Product-Formula Circuit). Our third example is a Trotter product-formula circuit for Hamiltonian simulation. Such circuits approximate the quantum evolution generated by a sum of Hamiltonians [28 , 40 ] .
The program in Figure 1 (c) presents a first-order Trotter implementation of the evolution U θ = e − i θ H U_{\theta}=e^{-i\theta H} U θ = e − i θ H governed by the transverse-field Ising Hamiltonian
H = H X + H Z Z , H X = ∑ j = 0 n − 1 a [ j ] X j , H Z Z = ∑ j = 0 n − 2 b [ j ] Z j Z j + 1 . H=H_X+H_{ZZ},\qquad H_X=\sum_{j=0}^{n-1}a[j]X_j,\qquad H_{ZZ}=\sum_{j=0}^{n-2}b[j]Z_jZ_{j+1}. H = H X + H Z Z , H X = j = 0 ∑ n − 1 a [ j ] X j , H Z Z = j = 0 ∑ n − 2 b [ j ] Z j Z j + 1 .
Here, n ≥ 1 n \ge1 n ≥ 1 is fixed, while the coefficient arrays a a a , b b b , evolution time θ \theta θ , and number N N N of Trotter steps are classical inputs. The operators X j X_j X j , Z j Z_j Z j are the Pauli operators on qubit q [ j ] q[j] q [ j ] . The main component of the program is a while loop: each iteration applies the single-qubit gate R x ( 2 a [ j ] δ ) R_x(2a[j]\delta) R x ( 2 a [ j ] δ ) , with R x ( γ ) = e − i γ X / 2 R_x(\gamma)=e^{-i\gamma X/2} R x ( γ ) = e − iγ X /2 and δ = θ / N \delta=\theta/N δ = θ / N , to every qubit q [ j ] q[j] q [ j ] . It then applies the two-qubit gate R Z Z ( 2 b [ j ] δ ) R_{ZZ}(2b[j]\delta) R Z Z ( 2 b [ j ] δ ) , with R Z Z ( γ ) = e − i γ ( Z ⊗ Z ) / 2 R_{ZZ}(\gamma)=e^{-i\gamma(Z\otimes Z)/2} R Z Z ( γ ) = e − iγ ( Z ⊗ Z ) /2 , to every q [ j ] , q [ j + 1 ] q[j],q[j+1] q [ j ] , q [ j + 1 ] . The loop runs N N N iterations.
The correctness of the Trotter program in Figure 1 (c) is an approximation guarantee: the program terminates, and its output approximates U θ U_\theta U θ applied to the input up to a target error. This correctness condition can be formalized as the following Hoare triple:
⊨ t o t { N ≥ 1 ∧ ε ≥ θ 2 2 N ∥ [ H X , H Z Z ] ∥ , ∣ ψ ⟩ ⟨ ψ ∣ 1 + ε } Trotter n { ∣ U θ ψ ⟩ ⟨ U θ ψ ∣ + ε I 1 + ε } . (5) \models_{\mathrm{tot}}\left\{N\ge1\land\varepsilon\ge\frac{\theta^2}{2N}\left\lVert[H_X,H_{ZZ}]\right\rVert,\frac{|\psi\rangle\langle\psi|}{1+\varepsilon}\right\}\operatorname{Trotter}_n\left\{\frac{|U_\theta\psi\rangle\langle U_\theta\psi|+\varepsilon I}{1+\varepsilon}\right\}. \tag*{(5)} ⊨ tot { N ≥ 1 ∧ ε ≥ 2 N θ 2 ∥ [ H X , H Z Z ] ∥ , 1 + ε ∣ ψ ⟩ ⟨ ψ ∣ } Trotter n { 1 + ε ∣ U θ ψ ⟩ ⟨ U θ ψ ∣ + ε I } . (5)
Here, ∣ ψ ⟩ |\psi\rangle ∣ ψ ⟩ is an arbitrary input state of q q q , the classical input ε ≥ 0 \varepsilon\ge0 ε ≥ 0 is the target error, [ H X , H Z Z ] = H X H Z Z − H Z Z H X [H_X,H_{ZZ}]=H_XH_{ZZ}-H_{ZZ}H_X [ H X , H Z Z ] = H X H Z Z − H Z Z H X , and ∥ ⋅ ∥ \lVert\cdot\rVert ∥ ⋅ ∥ is the operator norm. The classical precondition requires N ≥ 1 N\ge1 N ≥ 1 and that ε \varepsilon ε be at least the first-order Trotter error bound θ 2 2 N ∥ [ H X , H Z Z ] ∥ \frac{\theta^2}{2N}\lVert[H_X,H_{ZZ}]\rVert 2 N θ 2 ∥[ H X , H Z Z ]∥ [9 ] .
Intuitively, the Hoare triple (5) guarantees that, for every ε \varepsilon ε satisfying the classical precondition, the program will terminate and produce an output state with fidelity at least 1 − ε 1-\varepsilon 1 − ε with the target state U θ ∣ ψ ⟩ U_\theta|\psi\rangle U θ ∣ ψ ⟩ under the desired evolution U θ U_\theta U θ .
Through the above example, we can see that the classical–quantum interplay enables us to express a correctness condition involving approximation. Showing exact implementation of the product formula alone does not establish the approximation guarantee of the Trotter circuit. Instead, in the Hoare triple (5) , the classical variable ε \varepsilon ε represents the target error, and the quantum predicates are parameterized by ε \varepsilon ε .
To sum up, these motivating examples illustrate the intricacy in handling the classical–quantum interplay at the following two levels:
(L1) Program syntax and semantics. Quantum programs contain both classical and quantum variables. Classical states can affect quantum states by the guards of control flow, indices for arrays, or parameters for gates and measurements. Quantum states can affect classical states by the outcomes of measurements. A program may also be parameterized, as Q F T n QFT_n QF T n and T r o t t e r n Trotter_n T r o tt e r n are by n n n .
(L2) Specification and proof system. Building on (L1), the specification of program correctness contains both classical and quantum parts. Quantum predicates can depend on classical variables. Consequently, the proof system has to deal with the classical–quantum interplay as well.
Existing quantum verifiers, such as [8 , 20 , 25 , 26 , 34 , 41 , 45 , 51 ] do not directly support the classical–quantum interplay at both levels: they admit the classical and quantum components only in a restricted combination, and none of them directly accommodates all of the specifications (1) –(5) . A detailed comparison is presented in § 8 . QHL* combines classical and quantum components as follows: classical variables, expressions, control flow, and classical formulas in specifications keep the same roles as in classical programs, and the quantum components are built around them. However, mechanizing such a framework as a Lean library requires more than encoding the proof rules. We further need to handle the classical–quantum interplay at the following level:
(L0) Mathematics and quantum mechanics. Formalizing the semantics of quantum programs requires supporting structures from mathematics and quantum mechanics, including Hilbert spaces, states, linear operators, tensor products, probabilities, variables, gates, and measurements. While some of these can build on the existing Mathlib in Lean, one also needs to account for new requirements imposed by the classical–quantum interplay at higher levels (L1) and (L2) . Conversely, the design choices at this level determine how conveniently the higher levels can be expressed.
A good mechanization must therefore handle the classical–quantum interplay at all three levels together. Section 4 describes how QEDIA does so.
Preliminaries: QHL+
In this section, we briefly introduce the background on QHL+ [47 ] . This includes quantum programs, their correctness specification, and its proof system.
Classical and Quantum Variables
Classical variables. A classical variable has a type, such as integers or booleans. A classical state σ \sigma σ assigns to each classical variable a value of the corresponding type. Evaluating a classical expression e e e gives its value σ ( e ) \sigma(e) σ ( e ) . We use σ ⊨ φ \sigma\models\varphi σ ⊨ φ to denote that the classical formula φ \varphi φ holds in σ \sigma σ .
Simple quantum variables. A simple quantum variable has a Hilbert space as its quantum type . The state space of a set S S S of several quantum variables is the tensor product of their Hilbert spaces, denoted by H S = ⨂ q ∈ S H q \mathcal{H}_S = \bigotimes_{q \in S} \mathcal{H}_q H S = ⨂ q ∈ S H q . The joint state space of all quantum variables is denoted by H a l l \mathcal{H}_{\mathrm{all}} H all . An operator on a subsystem acts on a larger system by tensoring it with the identity on the remaining quantum variables.
Quantum arrays and subscripted quantum variables. An array quantum variable of type J → H q J \to\mathcal{H}_q J → H q consists of quantum variables indexed by a classical type J J J , each with state space H q \mathcal{H}_q H q . Its joint state space is ⨂ j ∈ J H q \bigotimes_{j \in J} \mathcal{H}_q ⨂ j ∈ J H q . If e e e is a classical expression of type J J J , the subscripted quantum variable q [ e ] q[e] q [ e ] denotes the quantum variable q [ σ ( e ) ] q[\sigma(e)] q [ σ ( e )] in classical state σ \sigma σ . We write σ ( q [ e ] ) = q [ σ ( e ) ] \sigma(q[e]) = q[\sigma(e)] σ ( q [ e ]) = q [ σ ( e )] . For a simple quantum variable, σ ( q ) = q \sigma(q) = q σ ( q ) = q .
For example, the Trotter circuit Example 2.3 applies gates to q [ j ] q[j] q [ j ] and q [ j + 1 ] q[j+1] q [ j + 1 ] , where the indices are parameterized by the classical variable j j j . For a sequence q ˉ = ( q 1 , … , q k ) \bar{q} = (q_1, \ldots, q_k) q ˉ = ( q 1 , … , q k ) of quantum variables, an operation acting on it should require
σ ⊨ Dist ( q ˉ ) , defined by σ ( q i ) ≠ σ ( q j ) for every i ≠ j . (6) \sigma\models\operatorname{Dist}(\bar{q}), \qquad\text{defined by } \sigma(q_i) \ne\sigma(q_j) \text{ for every } i \ne j. \tag*{(6)} σ ⊨ Dist ( q ˉ ) , defined by σ ( q i ) = σ ( q j ) for every i = j . (6)
For example, CNOT [ q [ i ] , q [ j ] ] \operatorname{CNOT}[q[i],q[j]] CNOT [ q [ i ] , q [ j ]] requires σ ( i ) ≠ σ ( j ) \sigma(i) \ne\sigma(j) σ ( i ) = σ ( j ) .
Syntax and Semantics of qWhile+
Syntax. Figure 2 gives the syntax of qWhile+. Most components are self-explanatory. Here, we emphasize the interplay between classical and quantum parts. As usual, the guards in if and while statements are classical. In U ( t ˉ ) [ q ˉ ] U(\bar{t})[\bar{q}] U ( t ˉ ) [ q ˉ ] , the parameters t ˉ \bar{t} t ˉ and indices for subscripted quantum variables in q ˉ \bar{q} q ˉ are also classical. In the other direction, a measurement x : = M [ q ˉ ] x := M[\bar{q}] x := M [ q ˉ ] assigns its outcome value to a classical variable. That value can enter subsequent expressions: in the RUS program of Example 2.1, the measurement m m m appears in the loop condition m = 1 ∧ k < N m = 1 \land k < N m = 1 ∧ k < N . We write q ˉ : = ∣ 0 ⟩ \bar{q} := \lvert0 \rangle q ˉ := ∣ 0 ⟩ for q 1 : = ∣ 0 ⟩ ; ⋯ ; q k : = ∣ 0 ⟩ q_1 := \lvert0 \rangle; \cdots; q_k := \lvert0 \rangle q 1 := ∣ 0 ⟩ ; ⋯ ; q k := ∣ 0 ⟩ .
Figure 2. Syntax of qWhile+. Here, x x x is a classical variable, e e e and t ˉ \bar{t} t ˉ are classical expressions, b b b is a classical boolean expression, q q q is a quantum variable, q ˉ \bar{q} q ˉ is a sequence of quantum variables, U ( t ˉ ) U(\bar{t}) U ( t ˉ ) is a unitary gate parameterized by t ˉ \bar{t} t ˉ , and M M M is a measurement.
P : : = s k i p ∣ x : = e ∣ q : = ∣ 0 ⟩ ∣ U ( t ˉ ) [ q ˉ ] ∣ x : = M [ q ˉ ] ∣ P 1 ; P 2 ∣ i f b t h e n P 1 e l s e P 0 ∣ w h i l e b d o P . P ::= \mathit{skip} \mid x := e \mid q := \lvert0\rangle\mid U(\bar{t})[\bar{q}] \mid x := M[\bar{q}]
\mid P_1;P_2 \mid\mathit{if}\ b\ \mathit{then}\ P_1\ \mathit{else}\ P_0 \mid\mathit{while}\ b\ \mathit{do}\ P. P ::= skip ∣ x := e ∣ q := ∣ 0 ⟩ ∣ U ( t ˉ ) [ q ˉ ] ∣ x := M [ q ˉ ] ∣ P 1 ; P 2 ∣ if b then P 1 else P 0 ∣ while b do P .
( s k i p , σ , ρ ) ⟶ ( ↓ , σ , ρ ) , ( x : = e , σ , ρ ) ⟶ ( ↓ , σ [ x : = σ ( e ) ] , ρ ) , (\mathit{skip},\sigma,\rho)\longrightarrow(\downarrow,\sigma,\rho),\qquad
(x:=e,\sigma,\rho)\longrightarrow(\downarrow,\sigma[x:=\sigma(e)],\rho), ( skip , σ , ρ ) ⟶ ( ↓ , σ , ρ ) , ( x := e , σ , ρ ) ⟶ ( ↓ , σ [ x := σ ( e )] , ρ ) ,
( q : = ∣ 0 ⟩ , σ , ρ ) ⟶ ( ↓ , σ , E σ ( q ) ( ρ ) ) , σ ⊨ Dist ( q ˉ ) ( U ( t ˉ ) [ q ˉ ] , σ , ρ ) ⟶ ( ↓ , σ , U σ ρ U σ † ) , (q:=\lvert0\rangle,\sigma,\rho)\longrightarrow(\downarrow,\sigma,\mathcal{E}_{\sigma(q)}(\rho)),
\qquad
\frac{\sigma\models\operatorname{Dist}(\bar{q})}{(U(\bar{t})[\bar{q}],\sigma,\rho)\longrightarrow(\downarrow,\sigma,U_{\sigma}\rho U_{\sigma}^{\dagger})}, ( q := ∣ 0 ⟩ , σ , ρ ) ⟶ ( ↓ , σ , E σ ( q ) ( ρ )) , ( U ( t ˉ ) [ q ˉ ] , σ , ρ ) ⟶ ( ↓ , σ , U σ ρ U σ † ) σ ⊨ Dist ( q ˉ ) ,
σ ⊨ Dist ( q ˉ ) ( x : = M [ q ˉ ] , σ , ρ ) ⟶ ( ↓ , σ [ x : = m ] , M σ m ρ ( M σ m ) † ) for every outcome m , \frac{\sigma\models\operatorname{Dist}(\bar{q})}{(x:=M[\bar{q}],\sigma,\rho)\longrightarrow(\downarrow,\sigma[x:=m],M_{\sigma}^{m}\rho(M_{\sigma}^{m})^{\dagger})}
\quad\text{for every outcome }m, ( x := M [ q ˉ ] , σ , ρ ) ⟶ ( ↓ , σ [ x := m ] , M σ m ρ ( M σ m ) † ) σ ⊨ Dist ( q ˉ ) for every outcome m ,
( P 1 , σ , ρ ) ⟶ ( P 1 ′ , σ ′ , ρ ′ ) ( P 1 ; P 2 , σ , ρ ) ⟶ ( P 1 ′ ; P 2 , σ ′ , ρ ′ ) , ↓ ; P 2 = P 2 , \frac{(P_1,\sigma,\rho)\longrightarrow(P_1',\sigma',\rho')}{(P_1;P_2,\sigma,\rho)\longrightarrow(P_1';P_2,\sigma',\rho')},
\qquad
\downarrow;P_2=P_2, ( P 1 ; P 2 , σ , ρ ) ⟶ ( P 1 ′ ; P 2 , σ ′ , ρ ′ ) ( P 1 , σ , ρ ) ⟶ ( P 1 ′ , σ ′ , ρ ′ ) , ↓ ; P 2 = P 2 ,
σ ⊨ b ( i f b t h e n P 1 e l s e P 0 , σ , ρ ) ⟶ ( P 1 , σ , ρ ) , σ ⊨ ¬ b ( i f b t h e n P 1 e l s e P 0 , σ , ρ ) ⟶ ( P 0 , σ , ρ ) , \frac{\sigma\models b}{(\mathit{if}\ b\ \mathit{then}\ P_1\ \mathit{else}\ P_0,\sigma,\rho)\longrightarrow(P_1,\sigma,\rho)},
\qquad
\frac{\sigma\models\neg b}{(\mathit{if}\ b\ \mathit{then}\ P_1\ \mathit{else}\ P_0,\sigma,\rho)\longrightarrow(P_0,\sigma,\rho)}, ( if b then P 1 else P 0 , σ , ρ ) ⟶ ( P 1 , σ , ρ ) σ ⊨ b , ( if b then P 1 else P 0 , σ , ρ ) ⟶ ( P 0 , σ , ρ ) σ ⊨ ¬ b ,
σ ⊨ b ( w h i l e b d o P , σ , ρ ) ⟶ ( P ; w h i l e b d o P , σ , ρ ) , σ ⊨ ¬ b ( w h i l e b d o P , σ , ρ ) ⟶ ( ↓ , σ , ρ ) . \frac{\sigma\models b}{(\mathit{while}\ b\ \mathit{do}\ P,\sigma,\rho)\longrightarrow(P;\mathit{while}\ b\ \mathit{do}\ P,\sigma,\rho)},
\qquad
\frac{\sigma\models\neg b}{(\mathit{while}\ b\ \mathit{do}\ P,\sigma,\rho)\longrightarrow(\downarrow,\sigma,\rho)}. ( while b do P , σ , ρ ) ⟶ ( P ; while b do P , σ , ρ ) σ ⊨ b , ( while b do P , σ , ρ ) ⟶ ( ↓ , σ , ρ ) σ ⊨ ¬ b .
Operational semantics. We define a classical–quantum state, or cq-state, to be a pair ( σ , ρ ) (\sigma,\rho) ( σ , ρ ) , where σ \sigma σ is a classical state and ρ \rho ρ is a partial density operator, that is, ρ ≥ 0 \rho\ge0 ρ ≥ 0 and Tr ( ρ ) ≤ 1 \operatorname{Tr}(\rho)\le1 Tr ( ρ ) ≤ 1 . The quantity Tr ( ρ ) \operatorname{Tr}(\rho) Tr ( ρ ) is the probability carried by this cq-state. This treatment makes it convenient to describe measurements in the operational semantics.
A configuration is ( P , σ , ρ ) (P,\sigma,\rho) ( P , σ , ρ ) , where P P P is a remaining program to be executed or the termination symbol ↓ \downarrow ↓ . The operational semantics of programs in Figure 3 defines rules for the transition ⟶ \longrightarrow ⟶ between configurations.
Figure 3. Operational transition rules of qWhile+. Here, the update ( σ [ x : = v ] ) ( y ) (\sigma[x:=v])(y) ( σ [ x := v ]) ( y ) is defined as v v v if y = x y=x y = x , and σ ( y ) \sigma(y) σ ( y ) if y ≠ x y\ne x y = x . The unitary operator U σ U_{\sigma} U σ represents U ( σ ( t ˉ ) ) U(\sigma(\bar{t})) U ( σ ( t ˉ )) acting on σ ( q ˉ ) \sigma(\bar{q}) σ ( q ˉ ) . Each measurement operator M σ m M_{\sigma}^{m} M σ m corresponds to an outcome m m m and also acts on σ ( q ˉ ) \sigma(\bar{q}) σ ( q ˉ ) . The initialization is defined as E σ ( q ) ( ρ ) = ∑ j ( ∣ 0 ⟩ ⟨ j ∣ ) σ ( q ) ρ ( ∣ j ⟩ ⟨ 0 ∣ ) σ ( q ) \mathcal{E}_{\sigma(q)}(\rho)=\sum_j(\lvert0\rangle\langle j\rvert)_{\sigma(q)}\rho(\lvert j\rangle\langle0\rvert)_{\sigma(q)} E σ ( q ) ( ρ ) = ∑ j (∣ 0 ⟩ ⟨ j ∣ ) σ ( q ) ρ (∣ j ⟩ ⟨ 0 ∣ ) σ ( q ) . The condition Dist \operatorname{Dist} Dist is as defined in (6) .
Denotational semantics and termination. For an input cq-state ( σ , ρ ) (\sigma,\rho) ( σ , ρ ) , the denotational semantics can be defined directly via the operational semantics:
⟦ P ⟧ ( σ , ρ ) = { ∣ ( σ ′ , ρ ′ ) ∣ ( P , σ , ρ ) ⟶ ∗ ( ↓ , σ ′ , ρ ′ ) ∣ } , \llbracket P\rrbracket(\sigma,\rho)=\{ \mid(\sigma',\rho')\mid(P,\sigma,\rho)\longrightarrow^{*}(\downarrow,\sigma',\rho')\mid\}, [ [ P ] ] ( σ , ρ ) = { ∣ ( σ ′ , ρ ′ ) ∣ ( P , σ , ρ ) ⟶ ∗ ( ↓ , σ ′ , ρ ′ ) ∣ } ,
where ⟶ ∗ \longrightarrow^{*} ⟶ ∗ is the reflexive transitive closure of ⟶ \longrightarrow ⟶ and { ∣ ⋅ ∣ } \{\mid\cdot\mid\} { ∣ ⋅ ∣ } denotes a multiset. Here, a multiset is used because there may be transitions from different sequences of measurement outcomes terminating in the same final cq-state. The total probability of terminating outputs satisfies
∑ ( σ ′ , ρ ′ ) ∈ ⟦ P ⟧ ( σ , ρ ) Tr ( ρ ′ ) ≤ Tr ( ρ ) . \sum_{(\sigma',\rho')\in\llbracket P\rrbracket(\sigma,\rho)}\operatorname{Tr}(\rho')\le\operatorname{Tr}(\rho). ( σ ′ , ρ ′ ) ∈ [ [ P ] ] ( σ , ρ ) ∑ Tr ( ρ ′ ) ≤ Tr ( ρ ) .
The non-termination probability starting from the cq-state ( σ , ρ ) (\sigma,\rho) ( σ , ρ ) is then
NT ( P ) ( σ , ρ ) = Tr ( ρ ) − ∑ ( σ ′ , ρ ′ ) ∈ ⟦ P ⟧ ( σ , ρ ) Tr ( ρ ′ ) . \operatorname{NT}(P)(\sigma,\rho)=\operatorname{Tr}(\rho)-\sum_{(\sigma',\rho')\in\llbracket P\rrbracket(\sigma,\rho)}\operatorname{Tr}(\rho'). NT ( P ) ( σ , ρ ) = Tr ( ρ ) − ( σ ′ , ρ ′ ) ∈ [ [ P ] ] ( σ , ρ ) ∑ Tr ( ρ ′ ) .
s : : = ∣ e ⟩ q ∣ s 1 ⊗ s 2 ∣ α 1 s 1 + α 2 s 2 ∣ ( U ( t ˉ ) [ q ˉ ] ) s . s ::= \lvert e\rangle_q \mid s_1 \otimes s_2 \mid\alpha_1s_1 + \alpha_2s_2 \mid\left(U(\bar{t})[\bar{q}]\right)s. s ::= ∣ e ⟩ q ∣ s 1 ⊗ s 2 ∣ α 1 s 1 + α 2 s 2 ∣ ( U ( t ˉ ) [ q ˉ ] ) s .
Sig ( ∣ e ⟩ q ) = { q } , \operatorname{Sig}(\lvert e\rangle_q) = \{q\}, Sig (∣ e ⟩ q ) = { q } ,
Sig ( s 1 ⊗ s 2 ) = Sig ( s 1 ) ∪ Sig ( s 2 ) , \operatorname{Sig}(s_1 \otimes s_2) = \operatorname{Sig}(s_1) \cup\operatorname{Sig}(s_2), Sig ( s 1 ⊗ s 2 ) = Sig ( s 1 ) ∪ Sig ( s 2 ) ,
Sig ( α 1 s 1 + α 2 s 2 ) = Sig ( s 1 ) if Sig ( s 1 ) = Sig ( s 2 ) , \operatorname{Sig}(\alpha_1s_1 + \alpha_2s_2) = \operatorname{Sig}(s_1) \qquad\text{if } \operatorname{Sig}(s_1) = \operatorname{Sig}(s_2), Sig ( α 1 s 1 + α 2 s 2 ) = Sig ( s 1 ) if Sig ( s 1 ) = Sig ( s 2 ) ,
Sig ( ( U ( t ˉ ) [ q ˉ ] ) s ) = Sig ( s ) if q ˉ ⊆ Sig ( s ) . \operatorname{Sig}\left(\left(U(\bar{t})[\bar{q}]\right)s\right) = \operatorname{Sig}(s) \qquad\text{if } \bar{q} \subseteq\operatorname{Sig}(s). Sig ( ( U ( t ˉ ) [ q ˉ ] ) s ) = Sig ( s ) if q ˉ ⊆ Sig ( s ) .
Fig. 4. Syntax and signatures of formal quantum states. Here, q q q is a quantum variable of type H T \mathcal{H}_T H T , e e e is a classical expression of type T T T , and α 1 , α 2 \alpha_1,\alpha_2 α 1 , α 2 are complex-valued classical expressions. The unitary gate U ( t ˉ ) [ q ˉ ] U(\bar{t})[\bar{q}] U ( t ˉ ) [ q ˉ ] is as defined in Figure 2 .
σ ( ∣ e ⟩ q ) = ∣ σ ( e ) ⟩ σ ( q ) , \sigma(\lvert e\rangle_q) = \lvert\sigma(e)\rangle_{\sigma(q)}, σ (∣ e ⟩ q ) = ∣ σ ( e ) ⟩ σ ( q ) ,
σ ( s 1 ⊗ s 2 ) = σ ( s 1 ) ⊗ σ ( s 2 ) if σ ( Sig ( s 1 ) ) ∩ σ ( Sig ( s 2 ) ) = ∅ , \sigma(s_1 \otimes s_2) = \sigma(s_1) \otimes\sigma(s_2) \qquad\text{if } \sigma(\operatorname{Sig}(s_1)) \cap\sigma(\operatorname{Sig}(s_2)) = \varnothing, σ ( s 1 ⊗ s 2 ) = σ ( s 1 ) ⊗ σ ( s 2 ) if σ ( Sig ( s 1 )) ∩ σ ( Sig ( s 2 )) = ∅ ,
σ ( α 1 s 1 + α 2 s 2 ) = σ ( α 1 ) σ ( s 1 ) + σ ( α 2 ) σ ( s 2 ) , \sigma(\alpha_1s_1 + \alpha_2s_2) = \sigma(\alpha_1)\sigma(s_1) + \sigma(\alpha_2)\sigma(s_2), σ ( α 1 s 1 + α 2 s 2 ) = σ ( α 1 ) σ ( s 1 ) + σ ( α 2 ) σ ( s 2 ) ,
σ ( ( U ( t ˉ ) [ q ˉ ] ) s ) = U σ ( σ ( s ) ) if σ ⊨ Dist ( q ˉ ) . \sigma\left(\left(U(\bar{t})[\bar{q}]\right)s\right) = U_\sigma(\sigma(s)) \qquad\text{if } \sigma\models\operatorname{Dist}(\bar{q}). σ ( ( U ( t ˉ ) [ q ˉ ] ) s ) = U σ ( σ ( s )) if σ ⊨ Dist ( q ˉ ) .
Fig. 5. Interpretation of formal quantum states in a classical state σ \sigma σ . The unitary operator U σ U_\sigma U σ is as defined in Figure 3 . The condition Dist \operatorname{Dist} Dist is as defined in (6) .
A : : = K ( t ˉ ) [ q ˉ ] ∣ [ s ] ∣ ¬ A ∣ A 1 ⊗ A 2 ∣ F ( t ˉ ) [ q ˉ ] ( { A j } ) . A ::= K(\bar{t})[\bar{q}] \mid[s] \mid\neg A \mid A_1 \otimes A_2 \mid F(\bar{t})[\bar{q}](\{A_j\}). A ::= K ( t ˉ ) [ q ˉ ] ∣ [ s ] ∣ ¬ A ∣ A 1 ⊗ A 2 ∣ F ( t ˉ ) [ q ˉ ] ({ A j }) .
Sig ( K ( t ˉ ) [ q ˉ ] ) = { q ˉ } , Sig ( [ s ] ) = Sig ( s ) , \operatorname{Sig}(K(\bar{t})[\bar{q}]) = \{\bar{q}\}, \qquad\operatorname{Sig}([s]) = \operatorname{Sig}(s), Sig ( K ( t ˉ ) [ q ˉ ]) = { q ˉ } , Sig ([ s ]) = Sig ( s ) ,
Sig ( ¬ A ) = Sig ( A ) , Sig ( A 1 ⊗ A 2 ) = Sig ( A 1 ) ∪ Sig ( A 2 ) , \operatorname{Sig}(\neg A) = \operatorname{Sig}(A), \qquad\operatorname{Sig}(A_1 \otimes A_2) = \operatorname{Sig}(A_1) \cup\operatorname{Sig}(A_2), Sig ( ¬ A ) = Sig ( A ) , Sig ( A 1 ⊗ A 2 ) = Sig ( A 1 ) ∪ Sig ( A 2 ) ,
Sig ( F ( t ˉ ) [ q ˉ ] ( { A j } ) ) = X , where q ˉ ⊆ Sig ( A j ) = X for each j . \operatorname{Sig}(F(\bar{t})[\bar{q}](\{A_j\})) = X, \qquad\text{where } \bar{q} \subseteq\operatorname{Sig}(A_j) = X \text{ for each } j. Sig ( F ( t ˉ ) [ q ˉ ] ({ A j })) = X , where q ˉ ⊆ Sig ( A j ) = X for each j .
fig:6 . Syntax and signatures of quantum predicate formulas. Here, K K K denotes a family of quantum predicates, and F F F denotes a finite family of operators. Both are parameterized by t ˉ \bar{t} t ˉ and act on q ˉ \bar{q} q ˉ .
Formal Quantum States and Assertions
Formal quantum states. In Section 2, we have seen Hoare triples parameterized by classical variables. For example, ∣ χ ( n , x ) ⟩ ⟨ χ ( n , x ) ∣ \lvert\chi(n,x)\rangle\langle\chi(n,x)\rvert ∣ χ ( n , x )⟩ ⟨ χ ( n , x )∣ depends on the array x x x for fixed n n n in Example 2.2. To describe quantum states like ∣ χ ( n , x ) ⟩ \lvert\chi(n,x)\rangle ∣ χ ( n , x )⟩ , we need the notion of formal (pure) quantum states. Figure 4 gives the syntax and signature of a formal quantum state, where the signature records the quantum variables it involves and is recursively defined. Figure 5 specifies how formal quantum states are interpreted in a classical state.
Quantum predicate formulas. A quantum predicate is a Hermitian operator E E E satisfying 0 ≤ E ≤ I 0 \le E \le I 0 ≤ E ≤ I , where ≤ \le ≤ is the Löwner order. On a partial density operator ρ \rho ρ , its expectation satisfies 0 ≤ Tr ( E ρ ) ≤ Tr ( ρ ) 0 \le\operatorname{Tr}(E\rho) \le\operatorname{Tr}(\rho) 0 ≤ Tr ( E ρ ) ≤ Tr ( ρ ) . Quantum predicate formulas have the syntax in Figure 6. Here, K ( t ˉ ) [ q ˉ ] K(\bar{t})[\bar{q}] K ( t ˉ ) [ q ˉ ] is an atomic formula with classical parameters t ˉ \bar{t} t ˉ , interpreted in a classical state σ \sigma σ as the quantum predicate K ( σ ( t ˉ ) ) K(\sigma(\bar{t})) K ( σ ( t ˉ )) on σ ( q ˉ ) \sigma(\bar{q}) σ ( q ˉ ) . We also use the atomic formula [ s ] [s] [ s ] , where s s s is a formal quantum state. There are three connectives for quantum predicate formulas: negation ¬ \neg ¬ , tensor product ⊗ \otimes ⊗ , and a family F ( t ˉ ) = { F j ( t ˉ ) : j } F(\bar{t}) = \{F_j(\bar{t}) : j\} F ( t ˉ ) = { F j ( t ˉ ) : j } of Kraus operators acting on q ˉ \bar{q} q ˉ .
Figure 7 specifies how quantum predicate formulas are interpreted in a classical state. We write σ ⊨ WD ( A ) \sigma\models\operatorname{WD}(A) σ ⊨ WD ( A ) if σ ( A ) \sigma(A) σ ( A ) is well-defined. Most of the interpretations are self-explanatory. The connective F ( t ˉ ) F(\bar{t}) F ( t ˉ ) can express the following transformations on quantum predicates: (1) reset, by taking F B = { ∣ j ⟩ ⟨ 0 ∣ : j } F_B = \{\lvert j\rangle\langle0\rvert: j\} F B = {∣ j ⟩ ⟨ 0 ∣ : j } ; (2) unitary, by taking F U = { U ( v ˉ ) † } F_U = \{U(\bar{v})^\dagger\} F U = { U ( v ˉ ) † } ; (3) measurement, by taking F M = { M j † : j } F_M = \{M_j^\dagger: j\} F M = { M j † : j }
Figure 7. Interpretation of quantum predicate formulas in a classical state σ \sigma σ . Here, the condition D i s t Dist D i s t is as defined in (6) . The Kraus operators F j σ = F j ( σ ( t ˉ ) ) σ ( q ˉ ) F_j^\sigma=F_j(\sigma(\bar{t}))_{\sigma(\bar{q})} F j σ = F j ( σ ( t ˉ ) ) σ ( q ˉ ) satisfy ∑ j F j σ ( F j σ ) † ≤ I \sum_j F_j^\sigma(F_j^\sigma)^\dagger\le I ∑ j F j σ ( F j σ ) † ≤ I . Each clause also requires its subformulas to be well-defined; otherwise σ ( A ) \sigma(A) σ ( A ) is not well-defined.
σ ( K ( t ˉ ) [ q ˉ ] ) = K ( σ ( t ˉ ) ) σ ( q ˉ ) if σ ⊨ D i s t ( q ˉ ) , σ ( [ s ] ) = ∣ σ ( s ) ⟩ ⟨ σ ( s ) ∣ if σ ( s ) exists and ∥ σ ( s ) ∥ = 1 , σ ( ¬ A ) = I − σ ( A ) , σ ( A 1 ⊗ A 2 ) = σ ( A 1 ) ⊗ σ ( A 2 ) if σ ( S i g ( A 1 ) ) ∩ σ ( S i g ( A 2 ) ) = ∅ , σ ( F ( t ˉ ) [ q ˉ ] ( { A j } ) ) = ∑ j F j σ σ ( A j ) ( F j σ ) † if σ ⊨ D i s t ( q ˉ ) . \sigma(K(\bar{t})[\bar{q}]) = K(\sigma(\bar{t}))_{\sigma(\bar{q})} \quad\text{if } \sigma\models Dist(\bar{q}),
\\
\sigma([s]) = |\sigma(s)\rangle\langle\sigma(s)| \quad\text{if } \sigma(s)\text{ exists and }\|\sigma(s)\|=1,
\\
\sigma(\neg A) = I-\sigma(A),
\\
\sigma(A_1\otimes A_2)=\sigma(A_1)\otimes\sigma(A_2)
\quad\text{if }\sigma(Sig(A_1))\cap\sigma(Sig(A_2))=\varnothing,
\\
\sigma(F(\bar{t})[\bar{q}] (\{A_j\}))=\sum_j F_j^\sigma\sigma(A_j)(F_j^\sigma)^\dagger
\quad\text{if }\sigma\models Dist(\bar{q}). σ ( K ( t ˉ ) [ q ˉ ]) = K ( σ ( t ˉ ) ) σ ( q ˉ ) if σ ⊨ D i s t ( q ˉ ) , σ ([ s ]) = ∣ σ ( s )⟩ ⟨ σ ( s ) ∣ if σ ( s ) exists and ∥ σ ( s ) ∥ = 1 , σ ( ¬ A ) = I − σ ( A ) , σ ( A 1 ⊗ A 2 ) = σ ( A 1 ) ⊗ σ ( A 2 ) if σ ( S i g ( A 1 )) ∩ σ ( S i g ( A 2 )) = ∅ , σ ( F ( t ˉ ) [ q ˉ ] ({ A j })) = j ∑ F j σ σ ( A j ) ( F j σ ) † if σ ⊨ D i s t ( q ˉ ) .
for a measurement M = { M j : j } M=\{M_j:j\} M = { M j : j } ; and (4) masking, by taking F I ( v ) = { I } F_I(v)=\{I\} F I ( v ) = { I } if v v v is true and { 0 } \{0\} { 0 } otherwise. We write b ⋅ A b\cdot A b ⋅ A for F I ( b ) [ q ˉ ] ( A ) F_I(b)[\bar{q}](A) F I ( b ) [ q ˉ ] ( A ) with q ˉ = ∅ \bar{q}=\varnothing q ˉ = ∅ .
Classical–quantum assertions. A c q cq c q -assertion is a pair ( φ , A ) (\varphi,A) ( φ , A ) , where φ \varphi φ is a classical first-order formula and A A A is a quantum predicate formula. For a c q cq c q -state ( σ , ρ ) (\sigma,\rho) ( σ , ρ ) with σ ⊨ φ ∧ W D ( A ) \sigma\models\varphi\land WD(A) σ ⊨ φ ∧ W D ( A ) , the expectation of ( φ , A ) (\varphi,A) ( φ , A ) is Tr ( σ ( A ) ρ ) \operatorname{Tr}(\sigma(A)\rho) Tr ( σ ( A ) ρ ) . We define the entailment by
( φ , A ) ⊨ ( ψ , B ) ⟺ φ ⊨ ψ , φ ⊨ W D ( A ) ↔ W D ( B ) , and σ ( A ) ≤ σ ( B ) for all σ ⊨ φ ∧ W D ( A ) . (\varphi,A)\models(\psi,B)\Longleftrightarrow\varphi\models\psi,\ \varphi\models WD(A)\leftrightarrow WD(B),\text{ and }\sigma(A)\le\sigma(B)\text{ for all }\sigma\models\varphi\land WD(A). ( φ , A ) ⊨ ( ψ , B ) ⟺ φ ⊨ ψ , φ ⊨ W D ( A ) ↔ W D ( B ) , and σ ( A ) ≤ σ ( B ) for all σ ⊨ φ ∧ W D ( A ) .
Let us use A [ e / x ] A[e/x] A [ e / x ] to denote the substitution of e e e for x x x in A A A . For every classical state σ \sigma σ , we define A [ e / x ] = σ [ x : = σ ( e ) ] ( A ) A[e/x]=\sigma[x:=\sigma(e)](A) A [ e / x ] = σ [ x := σ ( e )] ( A ) .
Proof System
The previous ingredients now fit together in a Hoare triple { φ , A } P { ψ , B } \{\varphi,A\}P\{\psi,B\} { φ , A } P { ψ , B } , with pre- and postconditions being c q cq c q -assertions. The validity of Hoare triples is then defined as follows, providing formal meanings to the specifications (1)–(5) in § 3 .
Definition 3.1 (validity in QHL+). A Hoare triple { φ , A } P { ψ , B } \{\varphi,A\}P\{\psi,B\} { φ , A } P { ψ , B } is valid for total correctness, denoted as ⊨ t o t { φ , A } P { ψ , B } \models_{\mathrm{tot}}\{\varphi,A\}P\{\psi,B\} ⊨ tot { φ , A } P { ψ , B } , if for every input c q cq c q -state ( σ , ρ ) (\sigma,\rho) ( σ , ρ ) with σ ⊨ φ ∧ W D ( A ) \sigma\models\varphi\land WD(A) σ ⊨ φ ∧ W D ( A ) ,
Tr ( σ ( A ) ρ ) ≤ ∑ ( σ ′ , ρ ′ ) ∈ ⟦ P ⟧ ( σ , ρ ) σ ′ ⊨ ψ ∧ W D ( B ) Tr ( σ ′ ( B ) ρ ′ ) . (7) \operatorname{Tr}(\sigma(A)\rho)\le
\sum_{\substack{(\sigma',\rho')\in\llbracket P\rrbracket(\sigma,\rho)\\ \sigma'\models\psi\land WD(B)}}
\operatorname{Tr}(\sigma'(B)\rho'). \tag*{(7)} Tr ( σ ( A ) ρ ) ≤ ( σ ′ , ρ ′ ) ∈ [ [ P ] ] ( σ , ρ ) σ ′ ⊨ ψ ∧ W D ( B ) ∑ Tr ( σ ′ ( B ) ρ ′ ) . (7)
The partial correctness ⊨ p a r { φ , A } P { ψ , B } \models_{\mathrm{par}}\{\varphi,A\}P\{\psi,B\} ⊨ par { φ , A } P { ψ , B } is defined similarly, except for adding a N T ( P ) ( σ , ρ ) NT(P)(\sigma,\rho) N T ( P ) ( σ , ρ ) term (accounting for non-termination) to the right-hand side of (7) .
Proof rules. Table 1 shows the proof rules of QHL+ for deriving Hoare triples. Here, f r e e ( φ ) free(\varphi) f r ee ( φ ) is the set of free classical variables of the classical formula φ \varphi φ , and v a r ( t ˉ ) var(\bar{t}) v a r ( t ˉ ) is the set of classical variables in t ˉ \bar{t} t ˉ . We use c v ( A ) , c v ( q ˉ ) cv(A),cv(\bar{q}) c v ( A ) , c v ( q ˉ ) to denote the sets of classical variables in the quantum predicate formula A A A and quantum variables q ˉ \bar{q} q ˉ , respectively. For a program P P P , q v ( P ) qv(P) q v ( P ) is the set of quantum variables in P P P , and c h a n g e ( P ) change(P) c han g e ( P ) consists of classical variables that P P P can modify.
Most axioms and rules are self-explanatory. In (Axiom-Meas), each outcome j j j sets x x x to j j j and contributes M j † A [ j / x ] M j M_j^\dagger A[j/x]M_j M j † A [ j / x ] M j to the precondition, and ∀ x . φ \forall x.\varphi ∀ x . φ requires φ \varphi φ for every outcome. (Rule-Mask) moves the masking condition b b b in the quantum predicate b ⋅ A b\cdot A b ⋅ A to the classical postcondition. For example, for a fresh variable y y y , taking b b b to be x = y x=y x = y allows one to select a single measurement outcome. We can derive the measurement axiom of Ying [47 ] by applying (Axiom-Meas) to { φ ∧ x ≠ y , ( x = y ) ⋅ A } \{\varphi\land x\ne y,(x=y)\cdot A\} { φ ∧ x = y , ( x = y ) ⋅ A } , followed by (Rule-Mask) and (Rule-Conseq), provided φ [ y / x ] ⊨ ∀ x . W D ( A ) \varphi[y/x]\models\forall x.WD(A) φ [ y / x ] ⊨ ∀ x . W D ( A ) (see Appendix § 11 .§ 1 ). In (Rule-Accum1), F F F contains k k k Kraus operators and F ′ F' F ′ contains one, all of the same quantum type. Both (Rule-Accum1) and (Rule-Accum2) require ∑ i F i F i † = I \sum_i F_iF_i^\dagger=I ∑ i F i F i † = I . The notation α \alpha α is defined such that for any classical state σ \sigma σ , σ ⊨ F ( t ‾ ) ∝ F ′ ( t ‾ ) \sigma\models F(\overline{t}) \propto F'(\overline{t}) σ ⊨ F ( t ) ∝ F ′ ( t ) holds if for every i = 1 , … , k i = 1,\ldots,k i = 1 , … , k and density operator ρ \rho ρ on this quantum type,
F i ′ † ( σ ( t ‾ ) ) ρ F i ′ ( σ ( t ‾ ) ) ≤ F i † ( σ ( t ‾ ) ) ρ F i ( σ ( t ‾ ) ) . F_i^{\prime\dagger}(\sigma(\overline{t}))\,\rho\,F_i'(\sigma(\overline{t})) \le F_i^{\dagger}(\sigma(\overline{t}))\,\rho\,F_i(\sigma(\overline{t})). F i ′ † ( σ ( t )) ρ F i ′ ( σ ( t )) ≤ F i † ( σ ( t )) ρ F i ( σ ( t )) .
(Rule-Accum1) combines mutually exclusive classical postconditions by disjunction, while (Rule-Accum2) combines quantum preconditions and postconditions using a Kraus operator.
(a) Partial correctness (QHLp a r + {}^{+}_{par} p a r + )
{ φ , A } s k i p { φ , A } \{\varphi,A\}\ \mathop{\mathrm{skip}}\ \{\varphi,A\} { φ , A } skip { φ , A }
{ φ [ e / x ] , A [ e / x ] } x : = e { φ , A } \{\varphi[e/x],A[e/x]\}\ x := e\ \{\varphi,A\} { φ [ e / x ] , A [ e / x ]} x := e { φ , A }
{ φ , F B [ q ] ( A ) } q : = ∣ 0 ⟩ { φ , A } \{\varphi,F_B[q](A)\}\ q := |0\rangle\ \{\varphi,A\} { φ , F B [ q ] ( A )} q := ∣0 ⟩ { φ , A }
{ φ , F U ( t ‾ ) [ q ‾ ] ( A ) } U ( t ‾ ) [ q ‾ ] { φ , A } \{\varphi,F_U(\overline{t})[\overline{q}](A)\}\ U(\overline{t})[\overline{q}]\ \{\varphi,A\} { φ , F U ( t ) [ q ] ( A )} U ( t ) [ q ] { φ , A }
{ ∀ x . φ , F M [ q ‾ ] ( { A [ j / x ] } ) } x : = M [ q ‾ ] { φ , A } \{\forall x.\,\varphi,F_M[\overline{q}] (\{A[j/x]\})\}\ x := M[\overline{q}]\ \{\varphi,A\} { ∀ x . φ , F M [ q ] ({ A [ j / x ]})} x := M [ q ] { φ , A }
{ φ , A } P 1 { ψ , B } { ψ , B } P 2 { θ , C } { φ , A } P 1 ; P 2 { θ , C } \frac{\{\varphi,A\}\ P_1\ \{\psi,B\}\qquad\{\psi,B\}\ P_2\ \{\theta,C\}}{\{\varphi,A\}\ P_1;P_2\ \{\theta,C\}} { φ , A } P 1 ; P 2 { θ , C } { φ , A } P 1 { ψ , B } { ψ , B } P 2 { θ , C }
{ φ ∧ b , A } P 1 { ψ , B } { φ ∧ ¬ b , A } P 0 { ψ , B } { φ , A } i f b t h e n P 1 e l s e P 0 { ψ , B } \frac{\{\varphi\land b,A\}\ P_1\ \{\psi,B\}\qquad\{\varphi\land\neg b,A\}\ P_0\ \{\psi,B\}}{\{\varphi,A\}\ \mathop{\mathrm{if}}\ b\ \mathop{\mathrm{then}}\ P_1\ \mathop{\mathrm{else}}\ P_0\ \{\psi,B\}} { φ , A } if b then P 1 else P 0 { ψ , B } { φ ∧ b , A } P 1 { ψ , B } { φ ∧ ¬ b , A } P 0 { ψ , B }
{ φ ∧ b , A } P { φ , A } { φ , A } w h i l e b d o P { φ ∧ ¬ b , A } \frac{\{\varphi\land b,A\}\ P\ \{\varphi,A\}}{\{\varphi,A\}\ \mathop{\mathrm{while}}\ b\ \mathop{\mathrm{do}}\ P\ \{\varphi\land\neg b,A\}} { φ , A } while b do P { φ ∧ ¬ b , A } { φ ∧ b , A } P { φ , A }
{ φ , A } P { ψ , b ⋅ B } { φ , A } P { ψ ∧ b , B } \frac{\{\varphi,A\}\ P\ \{\psi,b\cdot B\}}{\{\varphi,A\}\ P\ \{\psi\land b,B\}} { φ , A } P { ψ ∧ b , B } { φ , A } P { ψ , b ⋅ B }
( φ ′ , A ′ ) ⊨ ( φ , A ) { φ , A } P { ψ , B } ( ψ , B ) ⊨ ( ψ ′ , B ′ ) { φ ′ , A ′ } P { ψ ′ , B ′ } \frac{(\varphi',A')\models(\varphi,A)\qquad\{\varphi,A\}\ P\ \{\psi,B\}\qquad(\psi,B)\models(\psi',B')}{\{\varphi',A'\}\ P\ \{\psi',B'\}} { φ ′ , A ′ } P { ψ ′ , B ′ } ( φ ′ , A ′ ) ⊨ ( φ , A ) { φ , A } P { ψ , B } ( ψ , B ) ⊨ ( ψ ′ , B ′ )
{ φ , A i } P { ψ i , B } for every i , ( ∀ i 1 , i 2 ) i 1 ≠ i 2 ⇒ ¬ ( ψ i 1 ∧ ψ i 2 ) { φ , F ( t ‾ ) [ q ‾ ] ( { A i } ) } P { ⋁ i ψ i , F ′ ( t ‾ ) [ q ‾ ] ( B ) } \frac{\{\varphi,A_i\}\ P\ \{\psi_i,B\}\ \text{ for every }i,\qquad(\forall i_1,i_2)\ i_1\ne i_2\Rightarrow\neg(\psi_{i_1}\land\psi_{i_2})}{\{\varphi,F(\overline{t})[\overline{q}](\{A_i\})\}\ P\ \{\bigvee_i\psi_i,F'(\overline{t})[\overline{q}](B)\}} { φ , F ( t ) [ q ] ({ A i })} P { ⋁ i ψ i , F ′ ( t ) [ q ] ( B )} { φ , A i } P { ψ i , B } for every i , ( ∀ i 1 , i 2 ) i 1 = i 2 ⇒ ¬ ( ψ i 1 ∧ ψ i 2 )
F ( t ‾ ) ∝ F ′ ( t ‾ ) q ‾ ∩ q v ( P ) = ∅ [ var ( t ‾ ) ∪ c v ( q ‾ ) ] ∩ change ( P ) = ∅ { φ , F ( t ‾ ) [ q ‾ ] ( { A i } ) } P { ⋁ i ψ i , F ′ ( t ‾ ) [ q ‾ ] ( B ) } \frac{F(\overline{t})\propto F'(\overline{t})\qquad\overline{q}\cap qv(P)=\varnothing\qquad[\operatorname{var}(\overline{t})\cup cv(\overline{q})]\cap\operatorname{change}(P)=\varnothing}{\{\varphi,F(\overline{t})[\overline{q}](\{A_i\})\}\ P\ \{\bigvee_i\psi_i,F'(\overline{t})[\overline{q}](B)\}} { φ , F ( t ) [ q ] ({ A i })} P { ⋁ i ψ i , F ′ ( t ) [ q ] ( B )} F ( t ) ∝ F ′ ( t ) q ∩ q v ( P ) = ∅ [ var ( t ) ∪ c v ( q )] ∩ change ( P ) = ∅
{ φ , A i } P { ψ , B i } for every i ψ → ⋀ i WD ( B i ) { φ , F ( t ‾ ) [ q ‾ ] ( { A i } ) } P { ψ , F ( t ‾ ) [ q ‾ ] ( { B i } ) } \frac{\{\varphi,A_i\}\ P\ \{\psi,B_i\}\ \text{ for every }i\qquad\psi\to\bigwedge_i\operatorname{WD}(B_i)}{\{\varphi,F(\overline{t})[\overline{q}](\{A_i\})\}\ P\ \{\psi,F(\overline{t})[\overline{q}](\{B_i\})\}} { φ , F ( t ) [ q ] ({ A i })} P { ψ , F ( t ) [ q ] ({ B i })} { φ , A i } P { ψ , B i } for every i ψ → ⋀ i WD ( B i )
(b) Total correctness (QHLt o t + {}^{+}_{tot} t o t + ): replace (Rule-Loop-par) by
{ φ ∧ b , A } P { φ , A } { φ ∧ b ∧ t = z , I } P { t < z , I } φ → t ≥ 0 { φ , A } w h i l e b d o P { φ ∧ ¬ b , A } \frac{\{\varphi\land b,A\}\ P\ \{\varphi,A\}\qquad\{\varphi\land b\land t=z,I\}\ P\ \{t<z,I\}\qquad\varphi\to t\ge0}{\{\varphi,A\}\ \mathop{\mathrm{while}}\ b\ \mathop{\mathrm{do}}\ P\ \{\varphi\land\neg b,A\}} { φ , A } while b do P { φ ∧ ¬ b , A } { φ ∧ b , A } P { φ , A } { φ ∧ b ∧ t = z , I } P { t < z , I } φ → t ≥ 0
Table 1. The QHL+ {}^{+} + proof rules [47 ] , with a variant of (Axiom-Meas) and the added (Rule-Mask). Here, F B F_B F B , F U F_U F U , F M F_M F M , and the mask b ⋅ B b\cdot B b ⋅ B are the instances of F ( t ‾ ) F(\overline{t}) F ( t ) defined above. In (Rule-Accum1) and (Rule-Accum2), i i i indexes the Kraus operators of F F F . In (Rule-Loop-tot), the bound function t t t is an integer expression and z z z is a fresh integer variable, as usual for loops [1 ] .
Formalization in Lean and Library Organization
We now describe how QEDIA handles the classical–quantum interplay consistently at the three levels of § 3 . The library follows these levels, each building on those below it.
This consistency comes from the way QHL+ {}^{+} + combines classical and quantum components. Classical components keep their roles from classical programs, and quantum components are built around them. The formalization follows this combination at every level. The classical components are expressed directly in Lean, and the library builds the quantum components on them. At (L0), quantum variables are names from a finite set, and each set of names determines the Hilbert space of those variables. Classical data, such as array indices, can therefore select the quantum variables, and hence the space, on which an operation acts. At (L1), classical expressions and guards are Lean functions on classical states. Classical expressions also supply the indices of subscripted quantum variables and the parameters of gates. Proving the condition Dist ( q ‾ ) \operatorname{Dist}(\overline{q}) Dist ( q ) of (6) thus reduces to classical reasoning about indices. At (L2), the classical formula of a cq-assertion is a Lean predicate on classical states, and its quantum predicate is parameterized by classical variables. Classical reasoning about specifications therefore takes place directly in Lean’s logic.
Table 2 lists the Lean counterparts of the notions of § 4 , with their types. Sections 4.1 and 4.2 formalize (L0) and (L1): state spaces, programs, and their execution paths. Sections 4.3 and 4.4 formalize (L2): assertions and validity, and the proof systems with their soundness. Section 4.5 reports the size of the library and the examples.
Table 2. Lean counterparts of the notions of Section 3. Types are simplified and parameters are omitted; for a Lean type, the third column gives the type it stands for. L ( H ) \mathcal{L}(\mathcal{H}) L ( H ) denotes the linear operators on H \mathcal{H} H , and values of Assn are quantum predicates.
State Spaces and Programs
At (L0), we use, on top of Mathlib, the labeled Hilbert spaces and Dirac notation of [51 ] . Each quantum variable q q q in a finite set L L L of variable names has a finite, nonempty set D q D_q D q of basis labels. A set S ⊆ L S \subseteq L S ⊆ L has the Hilbert space
H S = C ∏ q ∈ S D q ≅ ⨂ q ∈ S C D q , \mathcal{H}_S = \mathbb{C}^{\prod_{q \in S} D_q} \cong\bigotimes_{q \in S} \mathbb{C}^{D_q}, H S = C ∏ q ∈ S D q ≅ q ∈ S ⨂ C D q ,
with H a l l = H L \mathcal{H}_{\mathrm{all}} = \mathcal{H}_L H all = H L . An operator O S O_S O S on H S \mathcal{H}_S H S acts on H a l l \mathcal{H}_{\mathrm{all}} H all through its cylindrical extension O S ⊗ I O_S \otimes I O S ⊗ I , where I I I is the identity on the variables in L ∖ S L \setminus S L ∖ S . Thus, whichever quantum variables the classical state selects, every quantum operation becomes an operator on H a l l \mathcal{H}_{\mathrm{all}} H all , on which quantum states and quantum predicates are also defined.
At (L1), programs are deeply embedded [6 ] , since the operational semantics and the proof rules are defined by cases on program syntax. Programs form the inductive type Prog for the syntax of Figure 2 . For simplicity, classical expressions are shallowly embedded. Lean represents a classical expression e e e by its evaluation σ ↦ σ ( e ) \sigma\mapsto\sigma(e) σ ↦ σ ( e ) , a function on the type Σ \Sigma Σ of classical states. A guard b b b thus has type Σ → B o o l \Sigma\to\mathrm{Bool} Σ → Bool , and proofs about classical computations use Lean’s arithmetic.
Classical expressions also select quantum variables. A sequence q ˉ = ( q 1 , … , q k ) \bar{q} = (q_1,\ldots,q_k) q ˉ = ( q 1 , … , q k ) may contain subscripted quantum variables such as q [ i ] q[i] q [ i ] , so the quantum variables σ ( q ˉ ) \sigma(\bar{q}) σ ( q ˉ ) it refers to depend on the classical state. We represent q ˉ \bar{q} q ˉ by a function r : Σ → { 0 , … , k − 1 } → L r : \Sigma\to\{0,\ldots,k-1\} \to L r : Σ → { 0 , … , k − 1 } → L (RegExpr in Table 2 ) with r ( σ ) = σ ( q ˉ ) r(\sigma) = \sigma(\bar{q}) r ( σ ) = σ ( q ˉ ) . Then σ ⊨ D i s t ( q ˉ ) \sigma\models\mathrm{Dist}(\bar{q}) σ ⊨ Dist ( q ˉ ) holds if and only if r ( σ ) r(\sigma) r ( σ ) is injective. Lean represents a quantum array by an injective map from its indices into L L L . The condition D i s t ( q ˉ ) \mathrm{Dist}(\bar{q}) Dist ( q ˉ ) therefore reduces to conditions on index values, which Lean proves with its arithmetic.
The Prog constructors for assignments, initializations, gates, and measurements take a classical update, a quantum operation, or both. The constructor for x : = e x := e x := e takes the update σ ↦ σ [ x : = σ ( e ) ] \sigma\mapsto\sigma[x := \sigma(e)] σ ↦ σ [ x := σ ( e )] . The constructor for initialization takes a finite set of quantum variables, all reset to ∣ 0 ⟩ |0\rangle ∣0 ⟩ at once, as in q ˉ : = ∣ 0 ⟩ \bar{q} := |0\rangle q ˉ := ∣0 ⟩ . The constructor for U ( t ˉ ) [ q ˉ ] U(\bar{t})[\bar{q}] U ( t ˉ ) [ q ˉ ] takes r r r and, in each classical state σ \sigma σ , the operator U ( σ ( t ˉ ) ) U(\sigma(\bar{t})) U ( σ ( t ˉ )) on H r ( σ ) ( 0 ) ⊗ ⋯ ⊗ H r ( σ ) ( k − 1 ) \mathcal{H}_{r(\sigma)(0)} \otimes\cdots\otimes\mathcal{H}_{r(\sigma)(k-1)} H r ( σ ) ( 0 ) ⊗ ⋯ ⊗ H r ( σ ) ( k − 1 ) . This space is identified with H σ ( q ˉ ) \mathcal{H}_{\sigma(\bar{q})} H σ ( q ˉ ) when Dist ( q ˉ ) \operatorname{Dist}(\bar{q}) Dist ( q ˉ ) holds. The constructor for x : = M [ q ˉ ] x := M[\bar{q}] x := M [ q ˉ ] takes both: r r r and the measurement operators M j σ M_j^\sigma M j σ in each σ \sigma σ , and an update u j : Σ → Σ u_j : \Sigma\to\Sigma u j : Σ → Σ for each outcome j j j , such as σ ↦ σ [ x : = j ] \sigma\mapsto\sigma[x := j] σ ↦ σ [ x := j ] . Since these operators depend on σ \sigma σ , Lean checks, when a program is written, that for every classical state each gate is unitary and each measurement satisfies the completeness relation.
Execution Paths
Lean represents the denotational semantics ⟦ P ⟧ ( σ , ρ ) \llbracket P \rrbracket(\sigma,\rho) [ [ P ] ] ( σ , ρ ) of § 4 by execution paths, on which the soundness proofs of § 5.4 rely. An execution path of a configuration c = ( P , σ , ρ ) c = (P,\sigma,\rho) c = ( P , σ , ρ ) is a finite sequence of transitions of Figure 3 starting from c c c . Let T n ( c ) \mathcal{T}_n(c) T n ( c ) be the finite set of execution paths of length n n n that end in a terminal configuration. We write ( ↓ , σ h , ρ h ) (\downarrow,\sigma_h,\rho_h) ( ↓ , σ h , ρ h ) for the terminal configuration of a path h h h . Each such path contributes ( σ h , ρ h ) (\sigma_h,\rho_h) ( σ h , ρ h ) to ⟦ P ⟧ ( σ , ρ ) \llbracket P \rrbracket(\sigma,\rho) [ [ P ] ] ( σ , ρ ) . Sums over this multiset, such as the one in (7) , thus become sums over paths: for every nonnegative function f f f on cq-states,
∑ ( σ ′ , ρ ′ ) ∈ ⟦ P ⟧ ( σ , ρ ) f ( σ ′ , ρ ′ ) = ∑ n = 0 ∞ ∑ h ∈ T n ( c ) f ( σ h , ρ h ) . (8) \sum_{(\sigma',\rho') \in\llbracket P \rrbracket(\sigma,\rho)} f(\sigma',\rho') = \sum_{n=0}^{\infty} \sum_{h \in\mathcal{T}_n(c)} f(\sigma_h,\rho_h). \tag*{(8)} ( σ ′ , ρ ′ ) ∈ [ [ P ] ] ( σ , ρ ) ∑ f ( σ ′ , ρ ′ ) = n = 0 ∑ ∞ h ∈ T n ( c ) ∑ f ( σ h , ρ h ) . (8)
For f ( σ ′ , ρ ′ ) = Tr ( ρ ′ ) f(\sigma',\rho') = \operatorname{Tr}(\rho') f ( σ ′ , ρ ′ ) = Tr ( ρ ′ ) , the partial sums of the right-hand side of (8) are bounded by Tr ( ρ ) \operatorname{Tr}(\rho) Tr ( ρ ) . The series therefore converges. Partial correctness uses the resulting non-termination probability NT ( P ) ( σ , ρ ) = Tr ( ρ ) − ∑ n ∑ h ∈ T n ( c ) Tr ( ρ h ) \operatorname{NT}(P)(\sigma,\rho) = \operatorname{Tr}(\rho) - \sum_n \sum_{h \in\mathcal{T}_n(c)} \operatorname{Tr}(\rho_h) NT ( P ) ( σ , ρ ) = Tr ( ρ ) − ∑ n ∑ h ∈ T n ( c ) Tr ( ρ h ) .
Assertions and Validity
At (L2), Lean keeps the classical and quantum parts of a cq-assertion apart. Like classical expressions, quantum predicate formulas are shallowly embedded. Lean represents a formula A A A by its interpretation σ ↦ σ ( A ) \sigma\mapsto\sigma(A) σ ↦ σ ( A ) (Figure 7 ), a function of type Assn. As a total function, the interpretation does not record where A A A is well-defined, so the well-definedness condition WD ( A ) \operatorname{WD}(A) WD ( A ) is kept as a separate component. A cq-assertion ( φ , A ) (\varphi,A) ( φ , A ) is thus represented by a SourceAssertion with three components: φ \varphi φ and WD ( A ) \operatorname{WD}(A) WD ( A ) as predicates on Σ \Sigma Σ , and the interpretation of A A A . The entailment ( φ , A ) ⊨ ( ψ , B ) (\varphi,A) \models(\psi,B) ( φ , A ) ⊨ ( ψ , B ) of § 4 uses these components separately. Its first two conditions, φ ⊨ ψ \varphi\models\psi φ ⊨ ψ and φ ⊨ WD ( A ) ↔ WD ( B ) \varphi\models\operatorname{WD}(A) \leftrightarrow\operatorname{WD}(B) φ ⊨ WD ( A ) ↔ WD ( B ) , become implications between Lean predicates. The third, σ ( A ) ≤ σ ( B ) \sigma(A) \leq\sigma(B) σ ( A ) ≤ σ ( B ) , compares the interpretations in the states satisfying φ ∧ WD ( A ) \varphi\land\operatorname{WD}(A) φ ∧ WD ( A ) .
Definition 3.1 uses a cq-assertion ( φ , A ) (\varphi,A) ( φ , A ) only through the expectation Tr ( σ ( A ) ρ ) \operatorname{Tr}(\sigma(A)\rho) Tr ( σ ( A ) ρ ) for classical states σ ⊨ φ ∧ WD ( A ) \sigma\models\varphi\land\operatorname{WD}(A) σ ⊨ φ ∧ WD ( A ) . The function toAssn therefore represents ( φ , A ) (\varphi,A) ( φ , A ) by a single quantum predicate, which is σ ( A ) \sigma(A) σ ( A ) for these states and 0 0 0 for the others. This is the masking b ⋅ A b \cdot A b ⋅ A of § 4 , with the formula φ ∧ WD ( A ) \varphi\land\operatorname{WD}(A) φ ∧ WD ( A ) in place of b b b .
With toAssn, validity is stated for single quantum predicates A \mathcal{A} A and B \mathcal{B} B of type Assn. The proposition validTot A P B \mathcal{A}\ \mathcal{P}\ \mathcal{B} A P B states that, for every classical state σ \sigma σ and positive operator ρ \rho ρ ,
Tr ( A ( σ ) ρ ) ≤ ∑ n = 0 ∞ ∑ h ∈ T n ( σ , ρ ) Tr ( B ( σ h ) ρ h ) . (9) \operatorname{Tr}(\mathcal{A}(\sigma)\rho) \leq\sum_{n=0}^{\infty} \sum_{h \in\mathcal{T}_n(\sigma,\rho)} \operatorname{Tr}(\mathcal{B}(\sigma_h)\rho_h). \tag*{(9)} Tr ( A ( σ ) ρ ) ≤ n = 0 ∑ ∞ h ∈ T n ( σ , ρ ) ∑ Tr ( B ( σ h ) ρ h ) . (9)
The proposition validPar adds NT ( P ) ( σ , ρ ) \operatorname{NT}(P)(\sigma,\rho) NT ( P ) ( σ , ρ ) to the right-hand side. For A = toAssn ( φ , A ) \mathcal{A} = \operatorname{toAssn}(\varphi,A) A = toAssn ( φ , A ) and B = toAssn ( ψ , B ) \mathcal{B} = \operatorname{toAssn}(\psi,B) B = toAssn ( ψ , B ) , (9) is equivalent to the validity of Definition 3.1. By (8) , its right-hand side is a sum over ⟦ P ⟧ ( σ , ρ ) \llbracket P \rrbracket(\sigma,\rho) [ [ P ] ] ( σ , ρ ) , as in (7) . In this sum, masking makes every term with σ h ⊭ ψ ∧ WD ( B ) \sigma_h \not\models\psi\land\operatorname{WD}(B) σ h ⊨ ψ ∧ WD ( B ) zero. This matches the condition on the sum in (7) . On the left-hand side, masking gives zero when σ ⊭ φ ∧ WD ( A ) \sigma\not\models\varphi\land\operatorname{WD}(A) σ ⊨ φ ∧ WD ( A ) , so such states impose no condition. Finally, both sides of (9) are linear in ρ \rho ρ . Quantifying over positive operators is therefore equivalent to quantifying over the partial density operators of Definition 3.1.
Proof Systems and Soundness
The inductive propositions SourceDerivPar and SourceDerivTot have one constructor for each axiom and rule of Table 1, and the constructor for (Rule-Conseq) uses the entailment of § 5.3 . For the axioms, Lean computes the precondition from the postcondition ( φ , A ) (\varphi, A) ( φ , A ) . As for the constructors of § 5.1 , classical updates and quantum operations are treated separately. A classical update is handled by evaluation. For x : = e x := e x := e , the precondition evaluates φ \varphi φ , WD ( A ) \operatorname{WD}(A) WD ( A ) , and the interpretation of A A A in σ [ x : = σ ( e ) ] \sigma[x := \sigma(e)] σ [ x := σ ( e )] . This is the substitution [ e / x ] [e/x] [ e / x ] of (Axiom-Ass), as defined in § 4 . A quantum operation is handled by its transformation. For initialization and gates, the precondition applies F B F_B F B or F U F_U F U in each classical state. A measurement combines both. In a classical state σ ⊨ Dist ( q ⃗ ) \sigma\models\operatorname{Dist}(\vec{q}) σ ⊨ Dist ( q ) , the quantum predicate of its precondition is interpreted as
∑ j ( M j σ ) † ( u j ( σ ) ) ( A ) M j σ . \sum_j (M_j^{\sigma})^\dagger(u_j(\sigma))(A) M_j^\sigma. j ∑ ( M j σ ) † ( u j ( σ )) ( A ) M j σ .
This is F M F_M F M of (Axiom-Meas), with σ [ x : = j ] \sigma[x := j] σ [ x := j ] generalized to u j ( σ ) u_j(\sigma) u j ( σ ) . As in (Axiom-Meas), φ \varphi φ and WD ( A ) \operatorname{WD}(A) WD ( A ) must hold after every update u j u_j u j . Since A A A is interpreted after the update, it can depend on the outcome, as the invariant J ( m ) \mathcal{J}(m) J ( m ) of Example 2.1 does.
Since classical expressions and quantum predicate formulas are shallowly embedded, side conditions about their variables are stated semantically. In (Rule-Loop-tot), the fresh variable z z z becomes an integer quantified outside the program, so the loop body cannot modify it. In (Rule-Accum1) and (Rule-Accum2), the conditions on change ( P ) \operatorname{change}(P) change ( P ) and qv ( P ) \operatorname{qv}(P) qv ( P ) become the following: every transition of P P P preserves the values of t ⃗ \vec{t} t and σ ( q ⃗ ) \sigma(\vec{q}) σ ( q ) , and P P P acts on no quantum variable in σ ( q ⃗ ) \sigma(\vec{q}) σ ( q ) . These systems satisfy the following soundness theorem.
Theorem 4.1 (soundness). For all cq-assertions ( φ , A ) (\varphi, A) ( φ , A ) , ( ψ , B ) (\psi, B) ( ψ , B ) and programs P P P ,
SourceDerivPar ( φ , A ) P ( ψ , B ) ⇒ validPar ( toAssn ( φ , A ) ) P ( toAssn ( ψ , B ) ) , \operatorname{SourceDerivPar}(\varphi,A)\ P\ (\psi,B) \Rightarrow\operatorname{validPar}(\operatorname{toAssn}(\varphi,A))\ P\ (\operatorname{toAssn}(\psi,B)), SourceDerivPar ( φ , A ) P ( ψ , B ) ⇒ validPar ( toAssn ( φ , A )) P ( toAssn ( ψ , B )) , SourceDerivTot ( φ , A ) P ( ψ , B ) ⇒ validTot ( toAssn ( φ , A ) ) P ( toAssn ( ψ , B ) ) . \operatorname{SourceDerivTot}(\varphi,A)\ P\ (\psi,B) \Rightarrow\operatorname{validTot}(\operatorname{toAssn}(\varphi,A))\ P\ (\operatorname{toAssn}(\psi,B)). SourceDerivTot ( φ , A ) P ( ψ , B ) ⇒ validTot ( toAssn ( φ , A )) P ( toAssn ( ψ , B )) .
The proof is by induction on the derivation, with one semantic lemma per constructor. For the axioms, the lemmas follow directly from the preconditions above. For (Rule-Mask), the lemma is immediate, since masking twice combines the conditions: toAssn ( ψ , b ⋅ B ) = toAssn ( ψ ∧ b , B ) \operatorname{toAssn}(\psi,b \cdot B) = \operatorname{toAssn}(\psi\land b,B) toAssn ( ψ , b ⋅ B ) = toAssn ( ψ ∧ b , B ) . For sequential composition, each execution path of P 1 ; P 2 P_1; P_2 P 1 ; P 2 splits into an execution path of P 1 P_1 P 1 followed by one of P 2 P_2 P 2 . The lemma thus combines the validity of the two premises through the intermediate assertion. For loops, induction on the length of execution paths gives the partial-correctness bound. Induction on the initial value of the bound function t t t gives the total-correctness bound. The soundness theorem uses only Lean’s standard axioms propext, Classical.choice, and Quot.sound.
Code Metrics
Table 3 and Table 4 report the size of the library and example files, including integration tests. Table 3 groups the library components by level. Excluding blank lines, the Spec column counts lines outside proofs and comments, and the Proof column counts lines strictly inside proof bodies. At the declaration level, the library contains 2,475 theorem/lemma declarations and 673 definitions. The examples contain 1,117 such declarations and 575 definitions.
Table 3. Code metrics of the library by level and component: upper bounds on line counts.
Table 4. Code metrics of the program examples: upper bounds on line counts.
The Prover Agent: Building the Library and Verifying Programs
We construct the library of § 5 and the program proofs of § 7 with a prover agent. It consists of a general-purpose core agent (§ 6.1 ) and a quantum extension (§ 6.2 ). The agent first builds the library, formalizing Q H L + QHL^{+} Q H L + and the mathematics it relies on, and then uses the library to verify programs. Throughout, the workflow separates fixing what is proved from proving it. A leader fixes the statements, subagents prove them, and the leader accepts a proof only if it proves the fixed statements. Figure 8 summarizes the workflow.
Figure 8. The workflow of the core agent with the quantum extension. Icons show whether Lean, Python scripts, or LLM review performs each acceptance check of § 6.1 . Dotted arrows show where the agents use the guidelines and the quantum extension.
Core Agent
The core agent follows the approach of recent LLM-based theorem-proving agents [7 , 24 , 27 ] . Given a mathematical statement or source, it produces Lean declarations in three stages: task definition, proof construction, and acceptance. The leader coordinates proof-writing subagents, and both are LLM agents. Human reviewers supervise the process. and review the statements, the library, and the proofs. All agents use Mathlib and the library. They follow shared guidelines: preserve the target statement and its assumptions, search for existing results before proving new ones, and place reusable lemmas in the library.
Task definition. The leader reads the source and the library and writes a task specification: the target statements, their permitted assumptions, the required declarations, and the permitted changes. It reviews whether the specification expresses the intended mathematics and gives a meaningful guarantee. It then assigns the task to a subagent once its prerequisites are completed.
Proof construction. A subagent constructs each proof with Lean tools. These tools report proof goals and diagnostics, build files, and search Mathlib and the library. The subagent first tries existing lemmas and automation, and then edits and repairs the proof with Lean’s feedback. It also checks axioms and inspects its own source locally, and reviews the proof against the guidelines, so failures can be repaired before submission.
Acceptance. The leader runs five checks on the combined changes of all subagents, returns failed changes for repair, and repeats the affected checks after any later change. Checks 1 and 5 relate the submitted declarations to the task specification. Checks 2–4 ensure that the proofs are complete and checked by Lean’s kernel with only the standard axioms.
Faithful statements (Lean, LLM review). The leader reviews whether the required declarations, and the definitions they depend on, express the mathematics fixed in the task specification. When a task verifies a program, Lean also checks that its validity theorem has the type of the target Hoare triple fixed there.
Clean build (Lean, Python). The library, including all submitted modules, builds without errors under the required settings.
Axiom dependencies (Lean, Python). For each required theorem, Lean reports the axioms it uses. A script checks that they are exactly propext, Classical.choice, and Quot.sound.
Kernel-checked proofs (Lean, LLM review). The leader inspects the submitted source for sorry, admit, native_decide, and options that bypass kernel checking. Lean checks the proofs under the required settings.
Protected statements and definitions (LLM review). The leader compares the submitted changes with the approved baseline. It reviews whether they stay within the permitted changes and preserve the target statements and protected definitions. When a task extends the library, the leader also reviews new or changed definitions against their mathematical sources.
Quantum Extension
The quantum extension supports applying QHL+ ^{+} + rules and adds quantum-specific checks. In program verification, a task consists of a quantum program and a target Hoare triple. Its required declarations are the program as a Prog term, the precondition and postcondition, a SourceDeriv term or SourceDerivPar derivation, and the validity theorem. The agents construct the derivation, and the leader obtains the validity theorem from it by Theorem 4.1 . Program proofs thus reason about assertions rather than execution paths (§ 5.2 ).
Verification conditions. Applying a rule turns its premises into Lean proof goals. For the axioms, Lean computes the precondition from the postcondition (§ 5.4 ). For the other rules, the agents choose what the conclusion leaves open, such as intermediate assertions, loop invariants, and bound functions. The resulting verification conditions split into classical and quantum conditions, as in Section 4. Classical conditions include the classical parts of entailments, D i s t ( q ˉ ) Dist(\bar{q}) D i s t ( q ˉ ) , and the nonnegativity of a bound function. They are goals about Lean predicates on classical states, proved with Lean’s logic and arithmetic. Quantum conditions include σ ( A ) ≤ σ ( B ) \sigma(A) \le\sigma(B) σ ( A ) ≤ σ ( B ) in the Löwner order and the conditions F ∝ F ′ F \propto F' F ∝ F ′ and ∑ i F i F i † = I \sum_i F_i F_i^\dagger= I ∑ i F i F i † = I of the accumulation rules. They are proved with library results about operators and quantum variables.
Quantum-specific checks. When a task verifies a program, the review in check 1 also examines whether the program implements the intended algorithm, with each gate defined by an explicit unitary in the library and each reused component a verified subprogram (§ 7.1 ). For each example, Lean verifies that the precondition has positive expectation on some input cq-state. This excludes preconditions with zero expectation on every input, for which the left-hand side of (7) vanishes and the Hoare triple holds for every program. For program tasks, the statements and definitions protected by check 5 include the program, the target Hoare triple, the proof systems, and the soundness proofs.
Verified Program Examples
We present eleven verified program examples, constructed using the prover agent (§ 6 ).
Overview
Table 5 summarizes the language features, verified properties, and subprogram reuse of these examples. Examples marked S reuse the verified derivations of the subprograms in the Reuse column. Each such subprogram is placed in the larger program by renaming its quantum variables with an injection and relating its classical state to that of the larger program. All Hoare triples are verified for total correctness, which accounts for termination.
Table 5. Overview of program examples. They illustrate the following program features: L (loop), NL (nested loops), A (quantum arrays), P (classical inputs), C (conditionals), M (measurement-controlled flow), and S (subprogram reuse).
In the following, we present the verification for the QFT, RUS, and Trotter programs introduced in § 3 . Appendix § 12 presents the remaining eight program examples, with the three building blocks for simulating the 1D spinless Fermi–Hubbard model grouped in Appendix § 12 .§ 8 .
Quantum Fourier Transform
We first derive the Hoare triple (4) for the QFT program in Example 2.2. The QFT proof outline presents this derivation. In a proof outline [1 , 31 ] , the program is annotated with assertions. Each program statement, together with the assertions before and after it, forms a Hoare triple. The right column gives the proof rules used in deriving each Hoare triple. (Rule-Seq) is used implicitly to sequentially combine Hoare triples. The target Hoare triple is formed by the first and last assertions in the proof outline.
We explain the notation as follows. For simplicity of presentation, we write a pure state ∣ φ ⟩ |\varphi\rangle ∣ φ ⟩ for its projector ∣ φ ⟩ ⟨ φ ∣ |\varphi\rangle\langle\varphi| ∣ φ ⟩ ⟨ φ ∣ in Hoare triples and write quantum predicates directly as operator expressions. The invariant names are local to each example unless explicitly reused. Let 0. x [ i : n − 1 ] = ∑ t = i n − 1 x [ t ] 2 i − t − 1 0.x[i:n-1] = \sum_{t=i}^{n-1} x[t]2^{i-t-1} 0. x [ i : n − 1 ] = ∑ t = i n − 1 x [ t ] 2 i − t − 1 , θ ( i ) = 2 π ⋅ 0. x [ i : n − 1 ] \theta(i) = 2\pi\cdot0.x[i:n-1] θ ( i ) = 2 π ⋅ 0. x [ i : n − 1 ] , and ∣ ψ ( α ) ⟩ = 1 2 ( ∣ 0 ⟩ + e i α ∣ 1 ⟩ ) |\psi(\alpha)\rangle= \frac{1}{\sqrt{2}}(|0\rangle+ e^{i\alpha}|1\rangle) ∣ ψ ( α )⟩ = 2 1 ( ∣0 ⟩ + e i α ∣1 ⟩) . Then, the final Fourier basis state can be written as
∣ χ ( n , x ) ⟩ = ⨂ j = 0 n − 1 ∣ ψ ( θ ( n − 1 − j ) ) ⟩ . (10) \lvert\chi(n,x)\rangle= \bigotimes_{j=0}^{n-1}\lvert\psi(\theta(n-1-j))\rangle. \tag*{(10)} ∣ χ ( n , x )⟩ = j = 0 ⨂ n − 1 ∣ ψ ( θ ( n − 1 − j ))⟩ . (10)
The key components of the QFT proof outline are the three loops at lines 2, 5, and 12, where we use (Rule-Loop-tot). The first loop invariant is { i ≤ n , J ( i ) } \{i \le n, \mathcal{J}(i)\} { i ≤ n , J ( i )} , where
J ( i ) = ⨂ m = 0 i − 1 ∣ ψ ( θ ( m ) ) ⟩ ⊗ ⨂ m = i n − 1 ∣ x [ m ] ⟩ , \mathcal{J}(i) = \bigotimes_{m=0}^{i-1}\lvert\psi(\theta(m))\rangle\otimes\bigotimes_{m=i}^{n-1}\lvert x[m]\rangle, J ( i ) = m = 0 ⨂ i − 1 ∣ ψ ( θ ( m ))⟩ ⊗ m = i ⨂ n − 1 ∣ x [ m ]⟩ ,
meaning the first i i i quantum variables already carry their Fourier phases. The second loop invariant is { i < n ∧ j ≤ n − 1 − i , J ′ ( i , j ) } \{i < n \land j \le n - 1 - i, \mathcal{J}'(i,j)\} { i < n ∧ j ≤ n − 1 − i , J ′ ( i , j )} , where
J ′ ( i , j ) = ⨂ m = 0 i − 1 ∣ ψ ( θ ( m ) ) ⟩ ⊗ ∣ ψ ( x [ i ] π + ∑ t = 0 j − 1 x [ i + 1 + t ] 2 π 2 t + 2 ) ⟩ ⊗ ⨂ m = i + 1 n − 1 ∣ x [ m ] ⟩ . \mathcal{J}'(i,j) = \bigotimes_{m=0}^{i-1}\lvert\psi(\theta(m))\rangle\otimes\left\lvert\psi\left(x[i]\pi+ \sum_{t=0}^{j-1}x[i+1+t]\frac{2\pi}{2^{t+2}}\right)\right\rangle\otimes\bigotimes_{m=i+1}^{n-1}\lvert x[m]\rangle. J ′ ( i , j ) = m = 0 ⨂ i − 1 ∣ ψ ( θ ( m ))⟩ ⊗ ψ ( x [ i ] π + t = 0 ∑ j − 1 x [ i + 1 + t ] 2 t + 2 2 π ) ⟩ ⊗ m = i + 1 ⨂ n − 1 ∣ x [ m ]⟩ .
The third loop invariant is { i ≤ ⌊ n / 2 ⌋ , J ′ ′ ( i ) } \{i \le\lfloor n/2\rfloor, \mathcal{J}''(i)\} { i ≤ ⌊ n /2 ⌋ , J ′′ ( i )} , where
J ′ ′ ( i ) = ( ∏ m = 0 i − 1 SWAP [ q [ m ] , q [ n − 1 − m ] ] ) ⨂ m = 0 n − 1 ∣ ψ ( θ ( m ) ) ⟩ . \mathcal{J}''(i) = \left(\prod_{m=0}^{i-1} \operatorname{SWAP}[q[m],q[n-1-m]]\right)\bigotimes_{m=0}^{n-1}\lvert\psi(\theta(m))\rangle. J ′′ ( i ) = ( m = 0 ∏ i − 1 SWAP [ q [ m ] , q [ n − 1 − m ]] ) m = 0 ⨂ n − 1 ∣ ψ ( θ ( m ))⟩ .
Table Quantum Fourier transform.
(continued)
{ i < ⌊ n / 2 ⌋ , J ′ ′ ( i + 1 ) } \{i < \lfloor n/2 \rfloor, \mathcal{J}''(i+1)\} { i < ⌊ n /2 ⌋ , J ′′ ( i + 1 )} i : = i + 1 i := i+1 i := i + 1 (Axiom-Ass), (Rule-Conseq) { i ≤ ⌊ n / 2 ⌋ , J ′ ′ ( i ) } \{i \le\lfloor n/2 \rfloor, \mathcal{J}''(i)\} { i ≤ ⌊ n /2 ⌋ , J ′′ ( i )}
od { i = ⌊ n / 2 ⌋ , J ′ ′ ( ⌊ n / 2 ⌋ ) } \{i = \lfloor n/2 \rfloor, \mathcal{J}''(\lfloor n/2 \rfloor)\} { i = ⌊ n /2 ⌋ , J ′′ (⌊ n /2 ⌋)} { t r u e , ∣ χ ( n , x ) ⟩ } \{true, |\chi(n,x)\rangle\} { t r u e , ∣ χ ( n , x )⟩} (Rule-Conseq)
At line 1, J ( 0 ) = ∣ x ⟩ \mathcal{J}(0)=|x\rangle J ( 0 ) = ∣ x ⟩ . The Hadamard gate at line 3 converts J ( i ) \mathcal{J}(i) J ( i ) into J ′ ( i , 0 ) \mathcal{J}'(i,0) J ′ ( i , 0 ) , which is turned into J ′ ( i , j ) \mathcal{J}'(i,j) J ′ ( i , j ) by the second while loop. Inside the loop, at line 6, the controlled rotation adds the phase contributed by x [ i + 1 + j ] x[i+1+j] x [ i + 1 + j ] through cR ( 2 π / 2 j + 2 ) [ q [ i ] , q [ i + 1 + j ] ] J ′ ( i , j ) = J ′ ( i , j + 1 ) \operatorname{cR}(2\pi/2^{j+2})[q[i],q[i+1+j]]\mathcal{J}'(i,j)=\mathcal{J}'(i,j+1) cR ( 2 π / 2 j + 2 ) [ q [ i ] , q [ i + 1 + j ]] J ′ ( i , j ) = J ′ ( i , j + 1 ) . From J ′ ( i , n − 1 − i ) = J ( i + 1 ) \mathcal{J}'(i,n-1-i)=\mathcal{J}(i+1) J ′ ( i , n − 1 − i ) = J ( i + 1 ) , the first while loop ends at line 10 with J ( n ) \mathcal{J}(n) J ( n ) . The third while loop reverses the qubit order by SWAP [ q [ i ] , q [ n − 1 − i ] ] J ′ ′ ( i ) = J ′ ′ ( i + 1 ) \operatorname{SWAP}[q[i],q[n-1-i]]\mathcal{J}''(i)=\mathcal{J}''(i+1) SWAP [ q [ i ] , q [ n − 1 − i ]] J ′′ ( i ) = J ′′ ( i + 1 ) .
The Hoare triple (4) determines the output of each computational basis input up to a phase. To establish correctness on arbitrary inputs, we also derive the following Hoare triple for n ≥ 1 n \ge1 n ≥ 1 :
⊨ t o t { ∣ + ⟩ ⊗ n } QFT n { ∣ 0 ⟩ ⊗ n } . (11) \mathrel{\vDash_{\mathrm{tot}}} \left\{ |+\rangle^{\otimes n} \right\} \operatorname{QFT}_n \left\{ |0\rangle^{\otimes n} \right\}. \tag*{(11)} ⊨ tot { ∣ + ⟩ ⊗ n } QFT n { ∣0 ⟩ ⊗ n } . (11)
Appendix B.1 derives (11) and shows that (4) and (11) together determine the output on every input up to a common global phase.
Repeat-Until-Success
We next derive the Hoare triples (1) and (2) for the RUS program in Example 2.1. The RUS proof outline presents the derivation of (1).
We explain the notation as follows. The gate W R U S W_{\mathrm{RUS}} W RUS is explicitly given by [32 Fig. 8] :
W R U S = ( H ⊗ I ) ( T ⊗ I ) CNOT [ a , d ] ( H ⊗ I ) CNOT [ a , d ] ( T ⊗ I ) ( H ⊗ I ) , (12) W_{\mathrm{RUS}}=(H\otimes I)(T\otimes I)\operatorname{CNOT}[a,d](H\otimes I)\operatorname{CNOT}[a,d](T\otimes I)(H\otimes I), \tag*{(12)} W RUS = ( H ⊗ I ) ( T ⊗ I ) CNOT [ a , d ] ( H ⊗ I ) CNOT [ a , d ] ( T ⊗ I ) ( H ⊗ I ) , (12)
where T T T is the phase-π 4 \frac{\pi}{4} 4 π gate, and a a a is the first tensor factor and the control of each CNOT. It satisfies, for all ∣ ψ 0 ⟩ |\psi_0\rangle ∣ ψ 0 ⟩ ,
W R U S ( ∣ 0 ⟩ ⊗ ∣ ψ 0 ⟩ ) = α ∣ 0 ⟩ ⊗ V R U S ∣ ψ 0 ⟩ + β ∣ 1 ⟩ ⊗ ∣ ψ 0 ⟩ , (13) W_{\mathrm{RUS}}(|0\rangle\otimes|\psi_0\rangle)=\alpha|0\rangle\otimes V_{\mathrm{RUS}}|\psi_0\rangle+\beta|1\rangle\otimes|\psi_0\rangle, \tag*{(13)} W RUS ( ∣0 ⟩ ⊗ ∣ ψ 0 ⟩) = α ∣0 ⟩ ⊗ V RUS ∣ ψ 0 ⟩ + β ∣1 ⟩ ⊗ ∣ ψ 0 ⟩ , (13)
where ∣ α ∣ 2 = 3 / 4 |\alpha|^2=3/4 ∣ α ∣ 2 = 3/4 and ∣ β ∣ 2 = 1 / 4 |\beta|^2=1/4 ∣ β ∣ 2 = 1/4 . Let ∣ v b ⟩ = ∣ b ⟩ ⊗ ∣ ψ 0 ⟩ |v_b\rangle=|b\rangle\otimes|\psi_0\rangle ∣ v b ⟩ = ∣ b ⟩ ⊗ ∣ ψ 0 ⟩ for b ∈ { 0 , 1 } b\in\{0,1\} b ∈ { 0 , 1 } . Then, I ⊗ ∣ ψ 0 ⟩ ⟨ ψ 0 ∣ = ∣ v 0 ⟩ ⟨ v 0 ∣ + ∣ v 1 ⟩ ⟨ v 1 ∣ I\otimes|\psi_0\rangle\langle\psi_0|=|v_0\rangle\langle v_0|+|v_1\rangle\langle v_1| I ⊗ ∣ ψ 0 ⟩ ⟨ ψ 0 ∣ = ∣ v 0 ⟩ ⟨ v 0 ∣ + ∣ v 1 ⟩ ⟨ v 1 ∣ , and (13) means W R U S ∣ v 0 ⟩ = α ∣ w ⟩ + β ∣ v 1 ⟩ W_{\mathrm{RUS}}|v_0\rangle=\alpha|w\rangle+\beta|v_1\rangle W RUS ∣ v 0 ⟩ = α ∣ w ⟩ + β ∣ v 1 ⟩ .
The key component of the RUS proof outline is the while loop at line 3, where we use (Rule-Loop-tot). The loop invariant is { k ≤ N , J ( m ) } \{k\le N,\mathcal{J}(m)\} { k ≤ N , J ( m )} , with J ( m ) \mathcal{J}(m) J ( m ) defined in (3), meaning that the data qubit carries V R U S ∣ ψ 0 ⟩ V_{\mathrm{RUS}}|\psi_0\rangle V RUS ∣ ψ 0 ⟩ if m = 0 m=0 m = 0 and is unchanged if m = 1 m=1 m = 1 , so that each attempt restarts from ∣ v 0 ⟩ |v_0\rangle ∣ v 0 ⟩ .
Table 1 (PDF p. 20).
(continued)
W R U S [ a , d ] W_{\mathrm{RUS}}[a,d] W RUS [ a , d ] (Axiom-Uni) { m = 1 ∧ k < N , α ∣ w ⟩ + β ∣ v 1 ⟩ } \{m=1\land k<N,\ \alpha|w\rangle+\beta|v_1\rangle\} { m = 1 ∧ k < N , α ∣ w ⟩ + β ∣ v 1 ⟩}
m : = M [ a ] m:=M[a] m := M [ a ] (Axiom-Meas), (Rule-Conseq) { k < N , ( 1 − m ) ∣ w ⟩ ⟨ w ∣ + m ∣ v 1 ⟩ ⟨ v 1 ∣ } \{k<N,\ (1-m)|w\rangle\langle w|+m|v_1\rangle\langle v_1|\} { k < N , ( 1 − m ) ∣ w ⟩ ⟨ w ∣ + m ∣ v 1 ⟩ ⟨ v 1 ∣ }
k : = k + 1 k:=k+1 k := k + 1 (Axiom-Ass), (Rule-Conseq) { k ≤ N , J ( m ) } \{k\leq N,\ \mathcal{J}(m)\} { k ≤ N , J ( m )}
od { k ≤ N ∧ ¬ ( m = 1 ∧ k < N ) , J ( m ) } \{k\leq N\land\neg(m=1\land k<N),\ \mathcal{J}(m)\} { k ≤ N ∧ ¬ ( m = 1 ∧ k < N ) , J ( m )} { m = 0 ∨ k ≥ N , J ( m ) } \{m=0\lor k\geq N,\ \mathcal{J}(m)\} { m = 0 ∨ k ≥ N , J ( m )} (Rule-Conseq)
At line 6, (Axiom-Meas) sums over both outcomes of measuring a a a , which updates the classical variable m m m controlling the loop. It converts the postcondition ( 1 − m ) ∣ w ⟩ ⟨ w ∣ + m ∣ v 1 ⟩ ⟨ v 1 ∣ (1-m)|w\rangle\langle w|+m|v_1\rangle\langle v_1| ( 1 − m ) ∣ w ⟩ ⟨ w ∣ + m ∣ v 1 ⟩ ⟨ v 1 ∣ of line 6 into ∣ w ⟩ ⟨ w ∣ + ∣ v 1 ⟩ ⟨ v 1 ∣ ≥ ∣ α w + β v 1 ⟩ ⟨ α w + β v 1 ∣ |w\rangle\langle w|+|v_1\rangle\langle v_1|\geq|\alpha w+\beta v_1\rangle\langle\alpha w+\beta v_1| ∣ w ⟩ ⟨ w ∣ + ∣ v 1 ⟩ ⟨ v 1 ∣ ≥ ∣ α w + β v 1 ⟩ ⟨ α w + β v 1 ∣ . The loop ends at line 8 with m = 0 ∨ k ≥ N m=0\lor k\geq N m = 0 ∨ k ≥ N , which is the postcondition of (1) .
We then derive the success bound (2) in the RUS success proof outline, using the loop invariant { k ≤ N ∧ ( m = 0 ∨ k < N ) , J ′ ( m , k ) } \{k\leq N\land(m=0\lor k<N),\ \mathcal{J}'(m,k)\} { k ≤ N ∧ ( m = 0 ∨ k < N ) , J ′ ( m , k )} , where
J ′ ( m , k ) = ( 1 − m ) ∣ w ⟩ ⟨ w ∣ + m p k ( ∣ v 0 ⟩ ⟨ v 0 ∣ + ∣ v 1 ⟩ ⟨ v 1 ∣ ) , p k = 1 − ∣ β ∣ 2 ( N − k ) . \mathcal{J}'(m,k)=(1-m)|w\rangle\langle w|+mp_k\left(|v_0\rangle\langle v_0|+|v_1\rangle\langle v_1|\right),\qquad p_k=1-|\beta|^{2(N-k)}. J ′ ( m , k ) = ( 1 − m ) ∣ w ⟩ ⟨ w ∣ + m p k ( ∣ v 0 ⟩ ⟨ v 0 ∣ + ∣ v 1 ⟩ ⟨ v 1 ∣ ) , p k = 1 − ∣ β ∣ 2 ( N − k ) .
For m = 1 m=1 m = 1 , p k p_k p k is the probability of at least one success within the remaining N − k N-k N − k attempts. At loop exit, the classical part excludes the exhausted branch m = 1 ∧ k = N m=1\land k=N m = 1 ∧ k = N , so only successful outputs are counted in (2) .
Table Repeat-until-success: success probability.
The quantum invariant is preserved since ∣ α ∣ 2 + ∣ β ∣ 2 p k + 1 = p k |\alpha|^2+|\beta|^2p_{k+1}=p_k ∣ α ∣ 2 + ∣ β ∣ 2 p k + 1 = p k for k < N k<N k < N , that is, the probability ∣ α ∣ 2 |\alpha|^2 ∣ α ∣ 2 of m = 0 m=0 m = 0 in the current attempt plus the probability ∣ β ∣ 2 p k + 1 |\beta|^2p_{k+1} ∣ β ∣ 2 p k + 1 of m = 1 m=1 m = 1 followed by a later success. At line 6, when k + 1 = N k+1=N k + 1 = N , the outcome m = 1 m=1 m = 1 would violate m = 0 ∨ k + 1 < N m=0\lor k+1<N m = 0 ∨ k + 1 < N . We therefore apply (Axiom-Meas) with the mask m = 0 ∨ k + 1 < N m=0\lor k+1<N m = 0 ∨ k + 1 < N on J ′ ( m , k + 1 ) \mathcal{J}'(m,k+1) J ′ ( m , k + 1 ) , and the precondition is still the assertion after line 5 because p N = 0 p_N = 0 p N = 0 . Then (Rule-Mask) converts the mask into the classical postcondition of line 6.
In both outlines, k ≤ N k \le N k ≤ N keeps the bound function N − k N-k N − k nonnegative, and the loop body decreases it under k < N k < N k < N .
The Trotter Product-Formula Circuit
We then derive the Hoare triple (5) for the Trotter program in Example 2.3. The Trotter proof outline presents this derivation.
We explain the notation as follows. Let A j ( δ ) A_j(\delta) A j ( δ ) and B j ( δ ) B_j(\delta) B j ( δ ) be the products of the first j j j rotations applied by the second and third while loops, respectively:
A 0 ( δ ) = B 0 ( δ ) = I , A j + 1 ( δ ) = R x ( 2 a [ j ] δ ) [ q [ j ] ] A j ( δ ) , B j + 1 ( δ ) = R z z ( 2 b [ j ] δ ) [ q [ j ] , q [ j + 1 ] ] B j ( δ ) . (14) \begin{aligned}
A_0(\delta) &= B_0(\delta) = I,\\
A_{j+1}(\delta) &= R_x(2a[j]\delta)[q[j]]A_j(\delta),\\
B_{j+1}(\delta) &= R_{zz}(2b[j]\delta)[q[j],q[j+1]]B_j(\delta). \tag*{(14)}
\end{aligned} A 0 ( δ ) A j + 1 ( δ ) B j + 1 ( δ ) = B 0 ( δ ) = I , = R x ( 2 a [ j ] δ ) [ q [ j ]] A j ( δ ) , = R z z ( 2 b [ j ] δ ) [ q [ j ] , q [ j + 1 ]] B j ( δ ) . (14)
Let A ( δ ) = A n ( δ ) A(\delta) = A_n(\delta) A ( δ ) = A n ( δ ) and B ( δ ) = B n − 1 ( δ ) B(\delta) = B_{n-1}(\delta) B ( δ ) = B n − 1 ( δ ) , which means A ( δ ) = e − i δ H X A(\delta) = e^{-i\delta H_X} A ( δ ) = e − i δ H X and B ( δ ) = e − i δ H Z Z B(\delta) = e^{-i\delta H_{ZZ}} B ( δ ) = e − i δ H Z Z . Each iteration of the first while loop therefore applies W = B ( δ ) A ( δ ) = e − i δ H Z Z e − i δ H X W = B(\delta)A(\delta) = e^{-i\delta H_{ZZ}}e^{-i\delta H_X} W = B ( δ ) A ( δ ) = e − i δ H Z Z e − i δ H X . Let Φ ε \Phi_\varepsilon Φ ε denote the classical precondition N ≥ 1 ∧ ε ≥ θ 2 ∥ [ H X , H Z Z ] ∥ 2 N N \ge1 \land\varepsilon\ge\frac{\theta^2\lVert[H_X,H_{ZZ}]\rVert}{2N} N ≥ 1 ∧ ε ≥ 2 N θ 2 ∥[ H X , H Z Z ]∥ of (5) . Define c = ( 1 + ε ) − 1 c = (1+\varepsilon)^{-1} c = ( 1 + ε ) − 1 . Since a , b , θ , N , ε ∉ change ( T r o t t e r n ) a,b,\theta,N,\varepsilon\notin\operatorname{change}(\mathrm{Trotter}_n) a , b , θ , N , ε ∈ / change ( Trotter n ) , Φ ε \Phi_\varepsilon Φ ε remains in every invariant and supplies the assumptions of the final (Rule-Conseq) step.
The key components of the Trotter proof outline are the three loops at lines 2, 4, and 9, where we use (Rule-Loop-tot). The first loop invariant is { Φ ε ∧ i ≤ N , J ( i ) } \{\Phi_\varepsilon\land i \le N,\mathcal{J}(i)\} { Φ ε ∧ i ≤ N , J ( i )} , where
J ( i ) = c ∣ W i ψ ⟩ ⟨ W i ψ ∣ , \mathcal{J}(i)=c|W^i\psi\rangle\langle W^i\psi|, J ( i ) = c ∣ W i ψ ⟩ ⟨ W i ψ ∣ ,
meaning that the first i i i iterations of the first while loop are completed. The second loop invariant is { Φ ε ∧ i < N ∧ j ≤ n , J ′ ( i , j ) } \{\Phi_\varepsilon\land i < N \land j \le n,\mathcal{J}'(i,j)\} { Φ ε ∧ i < N ∧ j ≤ n , J ′ ( i , j )} , where
J ′ ( i , j ) = c ∣ A j ( δ ) W i ψ ⟩ ⟨ A j ( δ ) W i ψ ∣ , \mathcal{J}'(i,j)=c|A_j(\delta)W^i\psi\rangle\langle A_j(\delta)W^i\psi|, J ′ ( i , j ) = c ∣ A j ( δ ) W i ψ ⟩ ⟨ A j ( δ ) W i ψ ∣ ,
meaning that the first j j j rotations R x R_x R x of the current iteration are applied. The third loop invariant is { Φ ε ∧ i < N ∧ j ≤ n − 1 , J ′ ′ ( i , j ) } \{\Phi_\varepsilon\land i < N \land j \le n-1,\mathcal{J}''(i,j)\} { Φ ε ∧ i < N ∧ j ≤ n − 1 , J ′′ ( i , j )} , where
J ′ ′ ( i , j ) = c ∣ B j ( δ ) A ( δ ) W i ψ ⟩ ⟨ B j ( δ ) A ( δ ) W i ψ ∣ , \mathcal{J}''(i,j)=c|B_j(\delta)A(\delta)W^i\psi\rangle\langle B_j(\delta)A(\delta)W^i\psi|, J ′′ ( i , j ) = c ∣ B j ( δ ) A ( δ ) W i ψ ⟩ ⟨ B j ( δ ) A ( δ ) W i ψ ∣ ,
meaning that all rotations R x R_x R x and the first j j j rotations R z z R_{zz} R z z of the current iteration are applied.
Table 6.
(continued)
{ Φ ε ∧ i < N ∧ j = n , J ′ ( i , n ) } { Φ ε ∧ i < N , c ∣ A ( δ ) W i ψ ⟩ ⟨ A ( δ ) W i ψ ∣ } (Rule-Conseq) 8 j : = 0 (Axiom-Ass), (Rule-Conseq) { Φ ε ∧ i < N ∧ j = 0 , J ′ ′ ( i , j ) } 9 while j < n − 1 do (Rule-Loop-tot), t = ( n − 1 ) − j { Φ ε ∧ i < N ∧ j < n − 1 , J ′ ′ ( i , j ) } 10 R z z ( 2 b [ j ] δ ) [ q [ j ] , q [ j + 1 ] ] (Axiom-Uni) { Φ ε ∧ i < N ∧ j < n − 1 , J ′ ′ ( i , j + 1 ) } 11 j : = j + 1 (Axiom-Ass), (Rule-Conseq) { Φ ε ∧ i < N ∧ j ≤ n − 1 , J ′ ′ ( i , j ) } 12 od { Φ ε ∧ i < N ∧ j = n − 1 , J ′ ′ ( i , n − 1 ) } { Φ ε ∧ i < N , J ( i + 1 ) } (Rule-Conseq) 13 i : = i + 1 (Axiom-Ass), (Rule-Conseq) { Φ ε ∧ i ≤ N , J ( i ) } 14 od { Φ ε ∧ i = N , J ( i ) } { Φ ε , c ∣ W N ψ ⟩ ⟨ W N ψ ∣ } (Rule-Conseq) { t r u e , c ( ∣ U θ ψ ⟩ ⟨ U θ ψ ∣ + ε I ) } (Rule-Conseq) \begin{aligned}
&\{ \Phi_{\varepsilon} \land i<N \land j=n,\ \mathcal{J}'(i,n) \}\\
&\{ \Phi_{\varepsilon} \land i<N,\ c\lvert A(\delta)W^{i}\psi\rangle\langle A(\delta)W^{i}\psi\lvert\}\qquad\text{(Rule-Conseq)}\\
8\quad&j:=0\qquad\text{(Axiom-Ass), (Rule-Conseq)}\\
&\{ \Phi_{\varepsilon} \land i<N \land j=0,\ \mathcal{J}''(i,j) \}\\
9\quad&\textbf{while }j<n-1\textbf{ do}\qquad\text{(Rule-Loop-tot), }t=(n-1)-j\\
&\{ \Phi_{\varepsilon} \land i<N \land j<n-1,\ \mathcal{J}''(i,j) \}\\
10\quad&R_{zz}(2b[j]\delta)[q[j],q[j+1]]\qquad\text{(Axiom-Uni)}\\
&\{ \Phi_{\varepsilon} \land i<N \land j<n-1,\ \mathcal{J}''(i,j+1) \}\\
11\quad&j:=j+1\qquad\text{(Axiom-Ass), (Rule-Conseq)}\\
&\{ \Phi_{\varepsilon} \land i<N \land j\le n-1,\ \mathcal{J}''(i,j) \}\\
12\quad&\textbf{od}\\
&\{ \Phi_{\varepsilon} \land i<N \land j=n-1,\ \mathcal{J}''(i,n-1) \}\\
&\{ \Phi_{\varepsilon} \land i<N,\ \mathcal{J}(i+1) \}\qquad\text{(Rule-Conseq)}\\
13\quad&i:=i+1\qquad\text{(Axiom-Ass), (Rule-Conseq)}\\
&\{ \Phi_{\varepsilon} \land i\le N,\ \mathcal{J}(i) \}\\
14\quad&\textbf{od}\\
&\{ \Phi_{\varepsilon} \land i=N,\ \mathcal{J}(i) \}\\
&\{ \Phi_{\varepsilon},\ c\lvert W^{N}\psi\rangle\langle W^{N}\psi\lvert\}\qquad\text{(Rule-Conseq)}\\
&\{ \mathit{true},\ c(\lvert U_{\theta}\psi\rangle\langle U_{\theta}\psi\lvert+\varepsilon I) \}\qquad\text{(Rule-Conseq)}
\end{aligned} 8 9 10 11 12 13 14 { Φ ε ∧ i < N ∧ j = n , J ′ ( i , n )} { Φ ε ∧ i < N , c ∣ A ( δ ) W i ψ ⟩ ⟨ A ( δ ) W i ψ ∣ } (Rule-Conseq) j := 0 (Axiom-Ass), (Rule-Conseq) { Φ ε ∧ i < N ∧ j = 0 , J ′′ ( i , j )} while j < n − 1 do (Rule-Loop-tot), t = ( n − 1 ) − j { Φ ε ∧ i < N ∧ j < n − 1 , J ′′ ( i , j )} R z z ( 2 b [ j ] δ ) [ q [ j ] , q [ j + 1 ]] (Axiom-Uni) { Φ ε ∧ i < N ∧ j < n − 1 , J ′′ ( i , j + 1 )} j := j + 1 (Axiom-Ass), (Rule-Conseq) { Φ ε ∧ i < N ∧ j ≤ n − 1 , J ′′ ( i , j )} od { Φ ε ∧ i < N ∧ j = n − 1 , J ′′ ( i , n − 1 )} { Φ ε ∧ i < N , J ( i + 1 )} (Rule-Conseq) i := i + 1 (Axiom-Ass), (Rule-Conseq) { Φ ε ∧ i ≤ N , J ( i )} od { Φ ε ∧ i = N , J ( i )} { Φ ε , c ∣ W N ψ ⟩ ⟨ W N ψ ∣ } (Rule-Conseq) { true , c (∣ U θ ψ ⟩ ⟨ U θ ψ ∣ + ε I )} (Rule-Conseq)
At line 1, J ( 0 ) = c ∣ ψ ⟩ ⟨ ψ ∣ \mathcal{J}(0)=c\lvert\psi\rangle\langle\psi\lvert J ( 0 ) = c ∣ ψ ⟩ ⟨ ψ ∣ . By (14) , the rotation at line 5 converts J ′ ( i , j ) \mathcal{J}'(i,j) J ′ ( i , j ) into J ′ ( i , j + 1 ) \mathcal{J}'(i,j+1) J ′ ( i , j + 1 ) , and the rotation at line 10 converts J ′ ′ ( i , j ) \mathcal{J}''(i,j) J ′′ ( i , j ) into J ′ ′ ( i , j + 1 ) \mathcal{J}''(i,j+1) J ′′ ( i , j + 1 ) . From B ( δ ) A ( δ ) = W B(\delta)A(\delta)=W B ( δ ) A ( δ ) = W , we have J ′ ′ ( i , n − 1 ) = J ( i + 1 ) \mathcal{J}''(i,n-1)=\mathcal{J}(i+1) J ′′ ( i , n − 1 ) = J ( i + 1 ) , and the first while loop ends at line 14 with J ( N ) = c ∣ W N ψ ⟩ ⟨ W N ψ ∣ \mathcal{J}(N)=c\lvert W^{N}\psi\rangle\langle W^{N}\psi\lvert J ( N ) = c ∣ W N ψ ⟩ ⟨ W N ψ ∣ .
Only the last (Rule-Conseq) step uses the condition on ε \varepsilon ε in Φ ε \Phi_{\varepsilon} Φ ε . It relies on the first-order Trotter error bound [9 ]
∥ W N − U θ ∥ ≤ θ 2 2 N ∥ [ H X , H Z Z ] ∥ , (15) \lVert W^{N}-U_{\theta}\rVert\le\frac{\theta^{2}}{2N}\lVert[H_{X},H_{ZZ}]\rVert, \tag*{(15)} ∥ W N − U θ ∥ ≤ 2 N θ 2 ∥[ H X , H Z Z ]∥ , (15)
which sums the one-step error δ 2 ∥ [ H X , H Z Z ] ∥ / 2 \delta^{2}\lVert[H_{X},H_{ZZ}]\rVert/2 δ 2 ∥[ H X , H Z Z ]∥ /2 over the N N N iterations. Under Φ ε \Phi_{\varepsilon} Φ ε , (15) gives ∥ W N ψ − U θ ψ ∥ ≤ ε \lVert W^{N}\psi-U_{\theta}\psi\rVert\le\varepsilon ∥ W N ψ − U θ ψ ∥ ≤ ε . Hence ∣ W N ψ ⟩ ⟨ W N ψ ∣ ≤ ∣ U θ ψ ⟩ ⟨ U θ ψ ∣ + ε I \lvert W^{N}\psi\rangle\langle W^{N}\psi\lvert\le\lvert U_{\theta}\psi\rangle\langle U_{\theta}\psi\lvert+\varepsilon I ∣ W N ψ ⟩ ⟨ W N ψ ∣ ≤ ∣ U θ ψ ⟩ ⟨ U θ ψ ∣ + ε I , since projectors onto unit vectors satisfy ∥ ∣ u ⟩ ⟨ u ∣ − ∣ v ⟩ ⟨ v ∣ ∥ ≤ ∥ u − v ∥ \lVert\lvert u\rangle\langle u\lvert-\lvert v\rangle\langle v\lvert\rVert\le\lVert u-v\rVert ∥∣ u ⟩ ⟨ u ∣ − ∣ v ⟩ ⟨ v ∣ ∥ ≤ ∥ u − v ∥ .
Related Work
Quantum Hoare logics. Quantum Hoare logic (QHL) [46 ] provides sound and relatively complete proof systems for partial and total correctness of quantum while-programs without classical variables. The applied quantum Hoare logic [52 ] restricts preconditions and postconditions to projections, which simplifies the proof rules for larger programs. For programs with classical variables, the logic of [17 ] uses families of Hermitian operators indexed by classical states as preconditions and postconditions. QHL+ [47 ] instead pairs a classical first-order formula with a single quantum predicate formula parameterized by classical variables. Specifications thus remain compact, and classical first-order reasoning applies directly to their classical parts. QHL+ also supports quantum arrays and gates whose indices and parameters are given by classical variables. Earlier quantum Hoare logics do not support these features [47 ] .
Mechanized quantum program logics. The QHL prover [26 ] formalizes a finite-dimensional partial-correctness fragment of QHL in Isabelle/HOL and proves it sound and complete. CoqQ [51 ] verifies partial and total correctness of quantum while-programs in Coq, with a machine-checked soundness proof. Neither supports classical program variables. CoqQLR [14 , 45 ] formalizes in Coq a logic for local reasoning about probabilistic properties of classical–quantum programs, with soundness for trace-preserving programs. The qrhl-tool [41 ] supports quantum relational Hoare logic (qRHL) in Isabelle/HOL, with soundness proved on paper.
Verification of circuits and hybrid programs. QWIRE [34 ] embeds a typed circuit language in Coq with dynamic lifting of measurement results. SQIR/VOQC [20 ] verifies circuit optimizations in Coq, and a certified implementation of Shor’s algorithm [33 ] builds on SQIR. Qbricks [8 ] verifies circuit-building programs in Why3. Qafny [25 ] has a type-guided quantum proof system and automates verification by translation into Dafny. Table 10 summarizes these systems.
Table 10. Quantum program verification tools: QEDIA in context.
To our knowledge, QEDIA is the first mechanization of a quantum Hoare logic that supports quantum arrays and gates parameterized by classical program variables, with soundness proved for both partial and total correctness.
Conclusion and Future Work
Quantum programs interleave classical and quantum computation. Verifying them in a proof assistant requires handling this interplay consistently, from the mathematics of quantum mechanics to the proof rules. QEDIA does so for QHL+ in Lean 4 and proves the logic sound for both partial and total correctness. On this foundation, we verified programs ranging from the quantum Fourier transform and repeat-until-success circuits to Trotter-based Hamiltonian simulation and quantum signal processing for the Fermi–Hubbard model. LLM agents built the library and these proofs under human supervision, and Lean’s kernel checks every proof.
In the following, we list a few topics for future work.
Quantum error correction. Quantum error correction underpins fault-tolerant quantum computation. Its syndrome measurement outcomes determine, through classical decoding, which corrections are applied. This makes it a natural next target for QEDIA.
Quantum recursion. Quantum recursive programs [48 , 50 ] combine quantum control flow with recursive procedure calls. Extending QEDIA to them
requires handling this combination, since qWhile{sup}+ has neither quantum control flow nor procedure calls.
Parallel and distributed programs. Hoare-style logics have been developed for parallel [49 ] and distributed [16 ] quantum programs. Mechanizing them raises further challenges, such as entanglement between component programs and the intertwining of nondeterminism from measurements and from parallelism [49 ] .
Acknowledgments
Use of AI. The Lean formalization and proofs were constructed by LLM agents under human supervision (§ 6 ), using OpenAI’s GPT-5.5, GPT Sol (versions 5.6, 6, and 6.1), and GPT-6 Astra, as well as Anthropic’s Claude Opus (versions 4.6, 4.8, and 5.5) and Claude Fable (versions 5 and 5.1). We also used AI assistance during the later stages of the project to organize data and improve the clarity and language of the manuscript. The formal proofs are checked by the Lean 4 kernel. The authors take responsibility for the specifications, interpretation of the results, and final content of the paper.
Details of the Formalization
Deriving the Original Measurement Axiom
We derive the measurement axiom of [47 ] , which treats one outcome at a time, from (Axiom-Meas) and (Rule-Mask). The axiom reads
{ φ [ y / x ] , F M ( y ) [ q ˉ ] ( A [ y / x ] ) } x : = M [ q ˉ ] { φ ∧ x = y , A } . \left\{ \varphi[y/x], F_M(y)[\bar{q}](A[y/x]) \right\}\ x := M[\bar{q}]\ \left\{ \varphi\land x = y, A \right\}. { φ [ y / x ] , F M ( y ) [ q ˉ ] ( A [ y / x ]) } x := M [ q ˉ ] { φ ∧ x = y , A } .
Here, y ∉ free ( φ ) ∪ cv ( A ) ∪ { x } y \notin\operatorname{free}(\varphi) \cup\operatorname{cv}(A) \cup\{x\} y ∈ / free ( φ ) ∪ cv ( A ) ∪ { x } is a classical variable and F M ( y ) = { M ~ y } F_M(y) = \{\widetilde{M}_y\} F M ( y ) = { M y } . The following proof outline derives it, provided φ [ y / x ] ⊨ ∀ x . WD ( A ) \varphi[y/x] \models\forall x.\operatorname{WD}(A) φ [ y / x ] ⊨ ∀ x . WD ( A ) .
Table 7.
At line 1, (Axiom-Meas) gives the precondition { ∀ x . ( φ ∨ x ≠ y ) , F M [ q ˉ ] ( { ( j = y ) ⋅ A [ j / x ] } ) } \{\forall x.(\varphi\lor x \ne y), F_M[\bar{q}](\{(j = y) \cdot A[j/x]\})\} { ∀ x . ( φ ∨ x = y ) , F M [ q ˉ ] ({( j = y ) ⋅ A [ j / x ]})} . Since y y y is fresh, its classical part is equivalent to φ [ y / x ] \varphi[y/x] φ [ y / x ] . In each classical state σ \sigma σ , the masks keep only the term for j = σ ( y ) j = \sigma(y) j = σ ( y ) , so its quantum part has the same interpretation as F M ( y ) [ q ˉ ] ( A [ y / x ] ) F_M(y)[\bar{q}](A[y/x]) F M ( y ) [ q ˉ ] ( A [ y / x ]) . Since φ [ y / x ] ⊨ ∀ x . WD ( A ) \varphi[y/x] \models\forall x.\operatorname{WD}(A) φ [ y / x ] ⊨ ∀ x . WD ( A ) , both quantum parts are well-defined and the classical states satisfying φ [ y / x ] \varphi[y/x] φ [ y / x ] . (Rule-Conseq) then replaces one precondition by the other.
The RUS Program and Specification in Lean
The following excerpts illustrate how the RUS program of Figure 1 (a) and its success specification (2) are represented in Lean. Programs have type Prog (§ 5.1 ). The classical-state type Store (Σ \Sigma Σ in Table 2 ) represents ( N , m , k ) (N,m,k) ( N , m , k ) , with Boolean values false and true encoding the outcomes 0 and 1. Surrounding namespace declarations and theorem proofs are omitted.
noncomputable def body
(W : UnitaryOp (EuclideanSpace ℂ (Bool × Bool))) : Prog Store D :=
.seq (.init fun _ => regA.sig) (.seq (.unitary 2 rAT (gateW W))
(.seq (.measure 2 1 rA measA updM) (.assign incK)))
noncomputable def prog
(W : UnitaryOp (EuclideanSpace ℂ (Bool × Bool))) : Prog Store D :=
.seq (.assign initM) (.seq (.assign initK) (.while guard (body W)))
Here, initM, initK, and incK implement m : = 1 m := 1 m := 1 , k : = 0 k := 0 k := 0 , and k : = k + 1 k := k + 1 k := k + 1 , and guard is m = 1 ∧ k < N m = 1 \land k < N m = 1 ∧ k < N . The expression regA.sig identifies the ancilla to reset; rA and rAT select a a a and ( a , d ) (a,d) ( a , d ) . The operation gateW W applies W W W to that pair, measA is the computational-basis measurement, and updM writes its outcome to m m m . Thus the measurement outcome controls whether the loop executes again.
Here, wPS and vPS are W R U S W_{RUS} W R U S and V R U S V_{RUS} V R U S of Example 2.1.
noncomputable def boundedRusClassicalProg : Prog ClassicalInput.Store D :=
ClassicalInput.prog wPS
theorem boundedRusClassicalSuccessSourceValid (hψ₀ : ‖ψ₀‖ = 1) :
validTot (ClassicalInput.preQ betaPS norm_betaPS_le_one ψ₀ hψ₀).toAssn
boundedRusClassicalProg (ClassicalInput.postQ vPS ψ₀ hψ₀).toAssn := ...
To identify its precondition and postcondition, we use the following local names:
local notation "Pre" =>
ClassicalInput.preQ betaPS norm_betaPS_le_one ψ₀ hψ₀
local notation "Post" => ClassicalInput.postQ vPS ψ₀ hψ₀
At a classical state σ = ( N , m , k ) \sigma= (N,m,k) σ = ( N , m , k ) , their components satisfy
( P r e ) . c l a s s i c a l σ ⟺ N ≥ 1 , (\mathrm{Pre}).\mathrm{classical}\ \sigma\iff N \ge1, ( Pre ) . classical σ ⟺ N ≥ 1 ,
( P o s t ) . c l a s s i c a l σ ⟺ m = 0 , (\mathrm{Post}).\mathrm{classical}\ \sigma\iff m = 0, ( Post ) . classical σ ⟺ m = 0 ,
( P r e ) . d e f i n e d σ ⟺ t r u e ⟺ ( P o s t ) . d e f i n e d σ , (\mathrm{Pre}).\mathrm{defined}\ \sigma\iff\mathrm{true} \iff(\mathrm{Post}).\mathrm{defined}\ \sigma, ( Pre ) . defined σ ⟺ true ⟺ ( Post ) . defined σ ,
( P r e ) . q u a n t u m . o p σ = ( 1 − 4 − N ) ( I ⊗ ∣ ψ 0 ⟩ ⟨ ψ 0 ∣ ) , (\mathrm{Pre}).\mathrm{quantum.op}\ \sigma= (1 - 4^{-N})(I \otimes|\psi_0\rangle\langle\psi_0|), ( Pre ) . quantum.op σ = ( 1 − 4 − N ) ( I ⊗ ∣ ψ 0 ⟩ ⟨ ψ 0 ∣ ) ,
( P o s t ) . q u a n t u m . o p σ = ∣ w ⟩ ⟨ w ∣ . (\mathrm{Post}).\mathrm{quantum.op}\ \sigma= |w\rangle\langle w|. ( Post ) . quantum.op σ = ∣ w ⟩ ⟨ w ∣.
Here, ∣ w ⟩ |w\rangle ∣ w ⟩ is defined in (3) . The coefficient in the precondition is established by classicalSuccessPreQ_op, using the proved failure-amplitude identity
theorem normSq_betaPS : ‖betaPS‖ ^ 2 = 1 / 4 := ...
By the third line of the display, toAssn masks their quantum predicates only by the displayed classical conditions (§ 5.3 ). The validity theorem is (2) ; it follows from the corresponding SourceDerivTot derivation by Theorem 4.1 . Its proof outline is in § 7.3 .
Further Verified Examples
This appendix presents the remaining eight examples of Table 5 , after extending the QFT example of § 7.2 to arbitrary inputs. The proof outlines follow the conventions of § 7.2 .
The QFT on Arbitrary Inputs
We derive the Hoare triple (11) by the QFT proof outline, with the quantum parts of its three loop invariants replaced by J ( i ) = ∣ 0 ⟩ ⊗ i ⊗ ∣ + ⟩ ⊗ ( n − i ) \mathcal{J}(i)=\lvert0\rangle^{\otimes i}\otimes\lvert+\rangle^{\otimes(n-i)} J ( i ) = ∣ 0 ⟩ ⊗ i ⊗ ∣ + ⟩ ⊗ ( n − i ) , J ′ ( i , j ) = J ( i + 1 ) \mathcal{J}'(i,j)=\mathcal{J}(i+1) J ′ ( i , j ) = J ( i + 1 ) , and J ′ ′ ( i ) = ∣ 0 ⟩ \mathcal{J}''(i)=\lvert0\rangle J ′′ ( i ) = ∣ 0 ⟩ . At line 3, H [ q [ i ] ] J ( i ) = J ( i + 1 ) = J ′ ( i , 0 ) H[q[i]]\mathcal{J}(i)=\mathcal{J}(i+1)=\mathcal{J}'(i,0) H [ q [ i ]] J ( i ) = J ( i + 1 ) = J ′ ( i , 0 ) . At line 6, the controlled rotation fixes J ′ ( i , j ) \mathcal{J}'(i,j) J ′ ( i , j ) because q [ i ] q[i] q [ i ] is in ∣ 0 ⟩ \lvert0\rangle ∣ 0 ⟩ . At line 13, the swap fixes J ′ ′ ( i ) \mathcal{J}''(i) J ′′ ( i ) . The first loop starts from J ( 0 ) = ∣ + ⟩ ⊗ n \mathcal{J}(0)=\lvert+\rangle^{\otimes n} J ( 0 ) = ∣ + ⟩ ⊗ n and ends with J ( n ) = J ′ ′ ( 0 ) \mathcal{J}(n)=\mathcal{J}''(0) J ( n ) = J ′′ ( 0 ) .
Let V V V be the unitary applied by QFT n \operatorname{QFT}_n QFT n . For an input state ∣ ψ ⟩ \lvert\psi\rangle ∣ ψ ⟩ , we use the same outline with the loop invariants J ( i ) \mathcal{J}(i) J ( i ) , J ′ ( i , j ) \mathcal{J}'(i,j) J ′ ( i , j ) , and J ′ ′ ( i ) \mathcal{J}''(i) J ′′ ( i ) taken to be the states obtained from ∣ ψ ⟩ \lvert\psi\rangle ∣ ψ ⟩ by the gates of the outline up to the corresponding point. Then the verification conditions at lines 3, 6, and 13 hold by definition, and J ′ ′ ( ⌊ n / 2 ⌋ ) = V ∣ ψ ⟩ \mathcal{J}''(\lfloor n/2\rfloor)=V\lvert\psi\rangle J ′′ (⌊ n /2 ⌋) = V ∣ ψ ⟩ . This establishes
⊨ t o t { ∣ ψ ⟩ } QFT n { V ∣ ψ ⟩ } . (16) \mathrel{\vDash_{\mathrm{tot}}}\{\lvert\psi\rangle\}\operatorname{QFT}_n\{V\lvert\psi\rangle\}. \tag*{(16)} ⊨ tot {∣ ψ ⟩} QFT n { V ∣ ψ ⟩} . (16)
For every program P P P and states ∣ w ⟩ \lvert w\rangle ∣ w ⟩ , ∣ u ⟩ \lvert u\rangle ∣ u ⟩ , and ∣ v ⟩ \lvert v\rangle ∣ v ⟩ ,
⊨ t o t { ∣ w ⟩ } P { ∣ u ⟩ } and ⊨ t o t { ∣ w ⟩ } P { ∣ v ⟩ } ⟹ ∣ u ⟩ ⟨ u ∣ = ∣ v ⟩ ⟨ v ∣ . \mathrel{\vDash_{\mathrm{tot}}}\{\lvert w\rangle\}P\{\lvert u\rangle\}\ \text{and}\ \mathrel{\vDash_{\mathrm{tot}}}\{\lvert w\rangle\}P\{\lvert v\rangle\}\ \Longrightarrow\ \lvert u\rangle\langle u\rvert=\lvert v\rangle\langle v\rvert. ⊨ tot {∣ w ⟩} P {∣ u ⟩} and ⊨ tot {∣ w ⟩} P {∣ v ⟩} ⟹ ∣ u ⟩ ⟨ u ∣ = ∣ v ⟩ ⟨ v ∣ .
Indeed, for the input ∣ w ⟩ \lvert w\rangle ∣ w ⟩ , the output expectations of ∣ u ⟩ \lvert u\rangle ∣ u ⟩ and ∣ v ⟩ \lvert v\rangle ∣ v ⟩ are both at least one, whereas ∣ u ⟩ ⟨ u ∣ + ∣ v ⟩ ⟨ v ∣ ≤ ( 1 + ∣ ⟨ u ∣ v ⟩ ∣ ) I \lvert u\rangle\langle u\rvert+\lvert v\rangle\langle v\rvert\leq(1+\lvert\langle u\mid v\rangle\rvert)I ∣ u ⟩ ⟨ u ∣ + ∣ v ⟩ ⟨ v ∣ ≤ ( 1 + ∣⟨ u ∣ v ⟩∣) I and the total probability of termination is at most one. Hence 2 ≤ 1 + ∣ ⟨ u ∣ v ⟩ ∣ ≤ 2 2\leq1+\lvert\langle u\mid v\rangle\rvert\leq2 2 ≤ 1 + ∣⟨ u ∣ v ⟩∣ ≤ 2 .
Applying this fact to (16) and (4) gives V ∣ x ⟩ = c x ∣ χ ( n , x ) ⟩ V\lvert x\rangle=c_x\lvert\chi(n,x)\rangle V ∣ x ⟩ = c x ∣ χ ( n , x )⟩ with ∣ c x ∣ = 1 \lvert c_x\rvert=1 ∣ c x ∣ = 1 . Together with (11), it gives V ∣ + ⟩ ⊗ n = c ∣ 0 ⟩ ⊗ n V\lvert+\rangle^{\otimes n}=c\lvert0\rangle^{\otimes n} V ∣ + ⟩ ⊗ n = c ∣ 0 ⟩ ⊗ n with ∣ c ∣ = 1 \lvert c\rvert=1 ∣ c ∣ = 1 . The Fourier sum in Example 2.2 yields ∣ 0 ⟩ ⊗ n = 2 − n / 2 ∑ x ∣ χ ( n , x ) ⟩ \lvert0\rangle^{\otimes n}=2^{-n/2}\sum_x\lvert\chi(n,x)\rangle ∣ 0 ⟩ ⊗ n = 2 − n /2 ∑ x ∣ χ ( n , x )⟩ . Since ∣ + ⟩ ⊗ n = 2 − n / 2 ∑ x ∣ x ⟩ \lvert+\rangle^{\otimes n}=2^{-n/2}\sum_x\lvert x\rangle ∣ + ⟩ ⊗ n = 2 − n /2 ∑ x ∣ x ⟩ , linearity gives
2 − n / 2 ∑ x c x ∣ χ ( n , x ) ⟩ = 2 − n / 2 ∑ x c ∣ χ ( n , x ) ⟩ . 2^{-n/2}\sum_x c_x\lvert\chi(n,x)\rangle=2^{-n/2}\sum_x c\lvert\chi(n,x)\rangle. 2 − n /2 x ∑ c x ∣ χ ( n , x )⟩ = 2 − n /2 x ∑ c ∣ χ ( n , x )⟩ .
The Fourier basis states are orthonormal, so c x = c c_x=c c x = c for every x x x . Thus, for every input state ∣ ψ ⟩ = ∑ x ψ x ∣ x ⟩ \lvert\psi\rangle=\sum_x\psi_x\lvert x\rangle ∣ ψ ⟩ = ∑ x ψ x ∣ x ⟩ , V ∣ ψ ⟩ = c ∑ x ψ x ∣ χ ( n , x ) ⟩ V\lvert\psi\rangle=c\sum_x\psi_x\lvert\chi(n,x)\rangle V ∣ ψ ⟩ = c ∑ x ψ x ∣ χ ( n , x )⟩ . The common global phase does not change its projector, and (Rule-Conseq) applied to (16) establishes
⊨ t o t { ∣ ψ ⟩ } QFT n { ∑ x ψ x ∣ χ ( n , x ) ⟩ } . (17) \mathrel{\vDash_{\mathrm{tot}}}\{\lvert\psi\rangle\}\operatorname{QFT}_n\left\{\sum_x\psi_x\lvert\chi(n,x)\rangle\right\}. \tag*{(17)} ⊨ tot {∣ ψ ⟩} QFT n { x ∑ ψ x ∣ χ ( n , x )⟩ } . (17)
Bell-State Preparation
The Bell state ∣ Φ + ⟩ = ( ∣ 00 ⟩ + ∣ 11 ⟩ ) / 2 \lvert\Phi^+\rangle=(\lvert00\rangle+\lvert11\rangle)/\sqrt{2} ∣ Φ + ⟩ = (∣ 00 ⟩ + ∣ 11 ⟩) / 2 is the shared resource of quantum teleportation [3 ] and superdense coding [4 ] , and its preparation is the smallest entangling circuit. The program applies a Hadamard to q 0 q_0 q 0 and then a C N O T CNOT C N O T with control q 0 q_0 q 0 and target q 1 q_1 q 1 . Its Hoare triple is
⊨ t o t { ∣ 00 ⟩ } H [ q 0 ] ; C N O T [ q 0 , q 1 ] { ∣ Φ + ⟩ } . \mathrel{\vDash_{\mathrm{tot}}}\{\lvert00\rangle\}\ H[q_0];\ CNOT[q_0,q_1]\ \{\lvert\Phi^+\rangle\}. ⊨ tot {∣ 00 ⟩} H [ q 0 ] ; C N O T [ q 0 , q 1 ] {∣ Φ + ⟩} .
Table 8.
(continued)
{ true , ∣ Φ + ⟩ } \{\,\text{true},\lvert\Phi^{+}\rangle\,\} { true , ∣ Φ + ⟩ }
The verification conditions at lines 1 and 2 are the gate actions H ∣ 0 ⟩ ⊗ ∣ 0 ⟩ = ∣ + ⟩ ⊗ ∣ 0 ⟩ H\lvert0\rangle\otimes\lvert0\rangle= \lvert+\rangle\otimes\lvert0\rangle H ∣ 0 ⟩ ⊗ ∣ 0 ⟩ = ∣ + ⟩ ⊗ ∣ 0 ⟩ and CNOT ( ∣ + ⟩ ⊗ ∣ 0 ⟩ ) = ∣ Φ + ⟩ \operatorname{CNOT}(\lvert+\rangle\otimes\lvert0\rangle)=\lvert\Phi^{+}\rangle CNOT (∣ + ⟩ ⊗ ∣ 0 ⟩) = ∣ Φ + ⟩ .
Parallel Hadamard
Parallel Hadamard prepares the uniform superposition ∣ + ⟩ ⊗ n \lvert+\rangle^{\otimes n} ∣ + ⟩ ⊗ n , the first step of algorithms such as Grover search [19 ] and phase estimation. The program P H n PH_n P H n applies a Hadamard to each quantum variable q [ i ] q[i] q [ i ] of the quantum array q q q , using the loop counter i i i . Its Hoare triple is, for every n ≥ 1 n \ge1 n ≥ 1 ,
⊨ t o t { ∣ 0 ⟩ ⊗ n } P H n { ∣ + ⟩ ⊗ n } . \models_{\mathrm{tot}}\{\lvert0\rangle^{\otimes n}\}\,PH_n\,\{\lvert+\rangle^{\otimes n}\}. ⊨ tot {∣ 0 ⟩ ⊗ n } P H n {∣ + ⟩ ⊗ n } .
The key component of the parallel-Hadamard proof outline is the loop at line 2, where we use (Rule-Loop-tot). The loop invariant is { t r u e , J ( i ) } \{true,\mathcal{J}(i)\} { t r u e , J ( i )} , where
J ( i ) = ⨂ j = 0 n − 1 { ∣ + ⟩ j < i ∣ 0 ⟩ j ≥ i \mathcal{J}(i)=\bigotimes_{j=0}^{n-1}
\begin{cases}
\lvert+\rangle& j<i\\
\lvert0\rangle& j\ge i
\end{cases} J ( i ) = j = 0 ⨂ n − 1 { ∣ + ⟩ ∣ 0 ⟩ j < i j ≥ i
meaning that the first i i i quantum variables carry ∣ + ⟩ \lvert+\rangle ∣ + ⟩ .
Table 9.
At line 1, J ( 0 ) = ∣ 0 ⟩ ⊗ n \mathcal{J}(0)=\lvert0\rangle^{\otimes n} J ( 0 ) = ∣ 0 ⟩ ⊗ n . At line 3, H [ q [ i ] ] J ( i ) = J ( i + 1 ) H[q[i]]\mathcal{J}(i)=\mathcal{J}(i+1) H [ q [ i ]] J ( i ) = J ( i + 1 ) for i < n i<n i < n . The loop ends at line 5 with i ≥ n i\ge n i ≥ n , which gives J ( i ) = ∣ + ⟩ ⊗ n \mathcal{J}(i)=\lvert+\rangle^{\otimes n} J ( i ) = ∣ + ⟩ ⊗ n .
Inverse QFT
The inverse QFT is the readout stage of quantum phase estimation: it carries the Fourier basis state ∣ χ ( n , x ) ⟩ \lvert\chi(n,x)\rangle ∣ χ ( n , x )⟩ back to ∣ x ⟩ \lvert x\rangle ∣ x ⟩ . The program I Q F T n IQFT_n I QF T n reverses the QFT circuit of § 7.2 , with the same classical variables: the loop counters i i i , j j j and the classical input x x x . It first runs the swap loop of the QFT in the same order, since the swaps act on disjoint pairs of quantum variables. It then runs the iterations of the first while loop of the QFT in reverse order with negated phase angles. Iteration i < n i<n i < n of its second loop reverses iteration n − 1 − i n-1-i n − 1 − i of the first loop of the QFT, and its third loop applies c R ( − 2 π / 2 i + 1 − j ) [ q [ n − 1 − i ] , q [ n − 1 − j ] ] cR(-2\pi/2^{i+1-j})[q[n-1-i],q[n-1-j]] c R ( − 2 π / 2 i + 1 − j ) [ q [ n − 1 − i ] , q [ n − 1 − j ]] for j < i j<i j < i , which reverses iteration i − 1 − j i-1-j i − 1 − j of the second loop of the QFT. Its Hoare triple is
⊨ t o t { ∣ χ ( n , x ) ⟩ } I Q F T n { ∣ x ⟩ } . \mathrel{\vDash_{\mathrm{tot}}} \{|\chi(n,x)\rangle\}\ \mathit{IQFT}_n\ \{|x\rangle\}. ⊨ tot { ∣ χ ( n , x )⟩} IQFT n { ∣ x ⟩} .
The derivation uses the three loops of the QFT proof outline in reverse order. The first loop, which performs the swaps, uses the loop invariant { i ≤ ⌊ n / 2 ⌋ , J ′ ′ ( i ) } \{i\le\lfloor n/2\rfloor,\mathcal{J}''(i)\} { i ≤ ⌊ n /2 ⌋ , J ′′ ( i )} , where, in this example, J ′ ′ ( i ) = ( ∏ m = 0 i − 1 S W A P [ q [ m ] , q [ n − 1 − m ] ] ) ∣ χ ( n , x ) ⟩ \mathcal{J}''(i)=\left(\prod_{m=0}^{i-1}SWAP[q[m],q[n-1-m]]\right)|\chi(n,x)\rangle J ′′ ( i ) = ( ∏ m = 0 i − 1 S W A P [ q [ m ] , q [ n − 1 − m ]] ) ∣ χ ( n , x )⟩ , meaning that the first i i i swaps are applied. It carries ∣ χ ( n , x ) ⟩ |\chi(n,x)\rangle ∣ χ ( n , x )⟩ to ⨂ m = 0 n − 1 ∣ ψ ( θ ( m ) ) ⟩ = J ( n ) \bigotimes_{m=0}^{n-1}|\psi(\theta(m))\rangle=\mathcal{J}(n) ⨂ m = 0 n − 1 ∣ ψ ( θ ( m ))⟩ = J ( n ) . The second loop uses the loop invariant { i ≤ n , J ( n − i ) } \{i\le n,\mathcal{J}(n-i)\} { i ≤ n , J ( n − i )} . In its iteration i < n i<n i < n , the third loop uses { j ≤ i , J ′ ( n − 1 − i , i − j ) } \{j\le i,\mathcal{J}'(n-1-i,i-j)\} { j ≤ i , J ′ ( n − 1 − i , i − j )} : it starts from J ′ ( n − 1 − i , i ) = J ( n − i ) \mathcal{J}'(n-1-i,i)=\mathcal{J}(n-i) J ′ ( n − 1 − i , i ) = J ( n − i ) and ends at J ′ ( n − 1 − i , 0 ) \mathcal{J}'(n-1-i,0) J ′ ( n − 1 − i , 0 ) . The verification conditions are those of lines 3, 6, and 13 of the QFT proof outline, with each gate replaced by its inverse. Each iteration of the third loop removes one phase term by
c R ( − α ) ( ∣ ψ ( β + b α ) ⟩ ⊗ ∣ b ⟩ ) = ∣ ψ ( β ) ⟩ ⊗ ∣ b ⟩ . (18) cR(-\alpha)\left(|\psi(\beta+b\alpha)\rangle\otimes|b\rangle\right)=|\psi(\beta)\rangle\otimes|b\rangle. \tag*{(18)} c R ( − α ) ( ∣ ψ ( β + b α )⟩ ⊗ ∣ b ⟩ ) = ∣ ψ ( β )⟩ ⊗ ∣ b ⟩ . (18)
The Hadamard then gives H ∣ ψ ( x [ k ] π ) ⟩ = ∣ x [ k ] ⟩ H|\psi(x[k]\pi)\rangle=|x[k]\rangle H ∣ ψ ( x [ k ] π )⟩ = ∣ x [ k ]⟩ at k = n − 1 − i k=n-1-i k = n − 1 − i , which yields J ( n − ( i + 1 ) ) \mathcal{J}(n-(i+1)) J ( n − ( i + 1 )) . The second loop ends with J ( 0 ) = ∣ x ⟩ \mathcal{J}(0)=|x\rangle J ( 0 ) = ∣ x ⟩ .
Quantum Phase Estimation
Quantum phase estimation [11 , 22 ] estimates an eigenphase of a unitary and is the core subroutine of Shor’s algorithm [38 ] . This program writes an exactly representable eigenphase into the control qubits and preserves the target eigenstate. Fix n ≥ 1 n\ge1 n ≥ 1 , a single-qubit unitary U U U , and a state ∣ u ⟩ |u\rangle ∣ u ⟩ . The classical input x x x specifies the eigenphase through the classical precondition
φ ≡ U ∣ u ⟩ = e 2 π i x ˉ / 2 n ∣ u ⟩ . \varphi\equiv U|u\rangle=e^{2\pi i\bar{x}/2^n}|u\rangle. φ ≡ U ∣ u ⟩ = e 2 π i x ˉ / 2 n ∣ u ⟩ .
where x ˉ \bar{x} x ˉ is as in Example 2.2 . Thus φ \varphi φ requires ∣ u ⟩ |u\rangle ∣ u ⟩ to be an eigenstate of U U U with eigenphase x ˉ / 2 n \bar{x}/2^n x ˉ / 2 n . The program Q P E n QPE_n QP E n acts on the n n n control qubits q [ 0 ] , … , q [ n − 1 ] q[0],\ldots,q[n-1] q [ 0 ] , … , q [ n − 1 ] and the target qubit q [ n ] q[n] q [ n ] . It prepares the control qubits by P H n PH_n P H n (Appendix § 12 .§ 4 ), then runs a loop whose iteration i i i applies the controlled power c U 2 n − 1 − i cU^{2^{n-1-i}} c U 2 n − 1 − i to q [ i ] , q [ n ] q[i],q[n] q [ i ] , q [ n ] , and finally applies I Q F T n IQFT_n I QF T n (Appendix § 12 .§ 5 ) to the control qubits. Its Hoare triple is
⊨ t o t { φ , ∣ 0 ⟩ ⊗ n ⊗ ∣ u ⟩ } Q P E n { ∣ x ⟩ ⊗ ∣ u ⟩ } . \mathrel{\vDash_{\mathrm{tot}}}\{\varphi,|0\rangle^{\otimes n}\otimes|u\rangle\}\ QPE_n\ \{|x\rangle\otimes|u\rangle\}. ⊨ tot { φ , ∣0 ⟩ ⊗ n ⊗ ∣ u ⟩} QP E n { ∣ x ⟩ ⊗ ∣ u ⟩} .
The key component of the QPE proof outline is the loop at line 3, where we use (Rule-Loop-tot). The loop invariant is { φ ∧ i ≤ n , L ( i ) } \{\varphi\land i\le n,\mathcal{L}(i)\} { φ ∧ i ≤ n , L ( i )} , where
L ( i ) = ⨂ m = 0 i − 1 ∣ ψ ( θ ( n − 1 − m ) ) ⟩ ⊗ ∣ + ⟩ ⊗ ( n − i ) ⊗ ∣ u ⟩ , \mathcal{L}(i)=\bigotimes_{m=0}^{i-1}|\psi(\theta(n-1-m))\rangle\otimes|+\rangle^{\otimes(n-i)}\otimes|u\rangle, L ( i ) = m = 0 ⨂ i − 1 ∣ ψ ( θ ( n − 1 − m ))⟩ ⊗ ∣ + ⟩ ⊗ ( n − i ) ⊗ ∣ u ⟩ ,
meaning that the first i i i control qubits carry their phases.
Table 11.
(continued)
{ φ ∧ i < n , L ( i + 1 ) } \{\varphi\land i < n,\ \mathcal{L}(i+1)\} { φ ∧ i < n , L ( i + 1 )}
i : = i + 1 i := i + 1 i := i + 1 (Axiom-Ass), (Rule-Conseq)
{ φ ∧ i ≤ n , L ( i ) } \{\varphi\land i \le n,\ \mathcal{L}(i)\} { φ ∧ i ≤ n , L ( i )}
od
{ φ ∧ i = n , L ( n ) } \{\varphi\land i = n,\ \mathcal{L}(n)\} { φ ∧ i = n , L ( n )}
IQFT n [ q [ 0 ] , … , q [ n − 1 ] ] \operatorname{IQFT}_{n}[q[0], \ldots, q[n-1]] IQFT n [ q [ 0 ] , … , q [ n − 1 ]] (Rule-Conseq), proved in Appendix B.4
{ true , ∣ x ⟩ ⊗ ∣ u ⟩ } \{\text{true},\ |x\rangle\otimes|u\rangle\} { true , ∣ x ⟩ ⊗ ∣ u ⟩}
Lines 1 and 7 use the Hoare triples of Appendices B.3 and B.4 on the control qubits, which leave φ \varphi φ and the target qubit unchanged. At line 4, the control q [ i ] q[i] q [ i ] is in ∣ + ⟩ = ∣ ψ ( 0 ) ⟩ |+\rangle= |\psi(0)\rangle ∣ + ⟩ = ∣ ψ ( 0 )⟩ , and φ \varphi φ gives
c U 2 n − 1 − i ( ∣ ψ ( 0 ) ⟩ ⊗ ∣ u ⟩ ) = ∣ ψ ( 2 n − 1 − i ⋅ 2 π x ˉ / 2 ) ⟩ ⊗ ∣ u ⟩ = ∣ ψ ( θ ( n − 1 − i ) ) ⟩ ⊗ ∣ u ⟩ . (19) \begin{aligned}
cU^{2^{n-1-i}}\left(|\psi(0)\rangle\otimes|u\rangle\right)
= |\psi(2^{n-1-i}\cdot2\pi\bar{x}/2)\rangle\otimes|u\rangle\\
&= |\psi(\theta(n-1-i))\rangle\otimes|u\rangle. \tag*{(19)}
\end{aligned} c U 2 n − 1 − i ( ∣ ψ ( 0 )⟩ ⊗ ∣ u ⟩ ) = ∣ ψ ( 2 n − 1 − i ⋅ 2 π x ˉ /2 )⟩ ⊗ ∣ u ⟩ = ∣ ψ ( θ ( n − 1 − i ))⟩ ⊗ ∣ u ⟩ . (19)
The gate leaves the other control qubits unchanged, which establishes L ( i + 1 ) \mathcal{L}(i+1) L ( i + 1 ) . The loop ends at line 6 with L ( n ) = ∣ χ ( n , x ) ⟩ ⊗ ∣ u ⟩ \mathcal{L}(n) = |\chi(n,x)\rangle\otimes|u\rangle L ( n ) = ∣ χ ( n , x )⟩ ⊗ ∣ u ⟩ by (10) .
Cluster-State Preparation
This example prepares the 1D cluster state, the resource state of measurement-based quantum computation [35 ] . The program C l u s t e r n \mathit{Cluster}_n Cluster n has one classical variable, the loop counter k k k , which each of its two loops initializes. The first loop applies H H H to q [ k ] q[k] q [ k ] , and the second applies C Z CZ C Z to q [ k ] , q [ k + 1 ] q[k], q[k+1] q [ k ] , q [ k + 1 ] . Its Hoare triple is, for every n ≥ 1 n \ge1 n ≥ 1 ,
⊨ t o t { ∣ 0 ⟩ ⊗ n } C l u s t e r n { ∣ C n ⟩ } , \models_{\mathrm{tot}}\ \{\ |0\rangle^{\otimes n}\ \}\ \mathit{Cluster}_n\ \{\ |C_n\rangle\ \}, ⊨ tot { ∣0 ⟩ ⊗ n } Cluster n { ∣ C n ⟩ } ,
where
∣ C n ⟩ = 1 2 n ∑ y ∈ { 0 , 1 } n ( − 1 ) ∑ j = 0 n − 2 y j y j + 1 ∣ y ⟩ . |C_n\rangle= \frac{1}{\sqrt{2^n}}\sum_{y\in\{0,1\}^n}(-1)^{\sum_{j=0}^{n-2}y_jy_{j+1}}|y\rangle. ∣ C n ⟩ = 2 n 1 y ∈ { 0 , 1 } n ∑ ( − 1 ) ∑ j = 0 n − 2 y j y j + 1 ∣ y ⟩ .
The key components of the cluster-state proof outline are the loops at lines 2 and 7, where we use (Rule-Loop-tot). The first loop uses the loop invariant { k ≤ n , J ( k ) } \{k \le n,\ \mathcal{J}(k)\} { k ≤ n , J ( k )} , with the verification conditions of Appendix B.3. The second uses { k ≤ n − 1 , J ′ ( k ) } \{k \le n - 1,\ \mathcal{J}'(k)\} { k ≤ n − 1 , J ′ ( k )} , where
J ′ ( k ) = 1 2 n ∑ y ∈ { 0 , 1 } n ( − 1 ) ∑ j = 0 k − 1 y j y j + 1 ∣ y ⟩ , \mathcal{J}'(k) = \frac{1}{\sqrt{2^n}}\sum_{y\in\{0,1\}^n}(-1)^{\sum_{j=0}^{k-1}y_jy_{j+1}}|y\rangle, J ′ ( k ) = 2 n 1 y ∈ { 0 , 1 } n ∑ ( − 1 ) ∑ j = 0 k − 1 y j y j + 1 ∣ y ⟩ ,
meaning that the first k k k C Z CZ C Z gates are applied.
Table 12.
(continued)
5 od { k = n , J ( n ) } \{ k = n,\ \mathcal{J}(n) \} { k = n , J ( n )} { t r u e , J ′ ( 0 ) } \{ \mathrm{true},\ \mathcal{J}'(0) \} { true , J ′ ( 0 )} (Rule-Conseq) 6 k : = 0 k := 0 k := 0 (Axiom-Ass), (Rule-Conseq) { k = 0 , J ′ ( k ) } \{ k = 0,\ \mathcal{J}'(k) \} { k = 0 , J ′ ( k )} J ′ ( k ) \mathcal{J}'(k) J ′ ( k ) : the first k k k CZ gates applied 7 while k < n − 1 k < n-1 k < n − 1 do (Rule-Loop-tot), t = ( n − 1 ) − k t = (n-1)-k t = ( n − 1 ) − k { k < n − 1 , J ′ ( k ) } \{ k < n-1,\ \mathcal{J}'(k) \} { k < n − 1 , J ′ ( k )} 8 CZ [ q [ k ] , q [ k + 1 ] ] [q[k],q[k+1]] [ q [ k ] , q [ k + 1 ]] (Axiom-Uni) { k < n − 1 , J ′ ( k + 1 ) } \{ k < n-1,\ \mathcal{J}'(k+1) \} { k < n − 1 , J ′ ( k + 1 )} 9 k : = k + 1 k := k+1 k := k + 1 (Axiom-Ass), (Rule-Conseq) { k ≤ n − 1 , J ′ ( k ) } \{ k \le n-1,\ \mathcal{J}'(k) \} { k ≤ n − 1 , J ′ ( k )} 10 od { k = n − 1 , J ′ ( n − 1 ) } \{ k = n-1,\ \mathcal{J}'(n-1) \} { k = n − 1 , J ′ ( n − 1 )} { t r u e , ∣ C n ⟩ } \{ \mathrm{true},\ |C_n\rangle\} { true , ∣ C n ⟩} (Rule-Conseq)
By definition, J ′ ( 0 ) = ∣ + ⟩ ⊗ n \mathcal{J}'(0)=|+\rangle^{\otimes n} J ′ ( 0 ) = ∣ + ⟩ ⊗ n and J ′ ( n − 1 ) = ∣ C n ⟩ \mathcal{J}'(n-1)=|C_n\rangle J ′ ( n − 1 ) = ∣ C n ⟩ . At line 8, CZ multiplies each computational basis state ∣ y ⟩ |y\rangle ∣ y ⟩ by ( − 1 ) y k y k + 1 (-1)^{y_k y_{k+1}} ( − 1 ) y k y k + 1 , so CZ [ q [ k ] , q [ k + 1 ] ] J ′ ( k ) = J ′ ( k + 1 ) \operatorname{CZ}[q[k],q[k+1]]\mathcal{J}'(k)=\mathcal{J}'(k+1) CZ [ q [ k ] , q [ k + 1 ]] J ′ ( k ) = J ′ ( k + 1 ) for k < n − 1 k<n-1 k < n − 1 by linearity.
Building Blocks for Fermi–Hubbard Simulation
The Fermi–Hubbard model is a standard model of interacting electrons [21 ] and a benchmark for quantum simulation [43 ] . We consider its 1D spinless version on an open chain, with nearest-neighbor hopping and density interactions, and verify three building blocks for simulating it: (a) encode the fermionic states and Hamiltonian into qubits by the Bravyi–Kitaev (BK) transformation; (b) block-encode the encoded Hamiltonian by an LCU circuit; and (c) apply a QSP polynomial transformation to that encoding [18 , 29 ] . The QSP program composes them: it runs the BK circuit on its input and calls the LCU circuit in each iteration.
The Bravyi–Kitaev transformation maps fermionic operators to qubit operators acting on O ( log n ) O(\log n) O ( log n ) qubits, compared with O ( n ) O(n) O ( n ) for the Jordan–Wigner transformation [37 ] . This example verifies the circuit of the Bravyi–Kitaev transformation, the invertible matrix β 2 x \beta_{2^x} β 2 x over Z 2 \mathbb{Z}_2 Z 2 given by its block recursion:
β 1 = ( 1 ) , β 2 x + 1 = ( β 2 x 0 C 2 x β 2 x ) . \beta_1=(1),\qquad\beta_{2^{x+1}}=\begin{pmatrix}\beta_{2^x} & 0 \\ C_{2^x} & \beta_{2^x}\end{pmatrix}. β 1 = ( 1 ) , β 2 x + 1 = ( β 2 x C 2 x 0 β 2 x ) .
where C 2 x C_{2^x} C 2 x is zero except for its last row, which is all ones. The circuit computes β 2 x f \beta_{2^x}f β 2 x f in place [37 ] , level by level. The program has two classical variables, the level counter ℓ \ell ℓ and the block counter k k k . The first while loop runs over the x x x levels, and the second runs over the blocks of one level. Each iteration of the second loop is a conditional. If block k k k exists at level ℓ \ell ℓ , that is, ( k + 1 ) 2 ℓ + 1 ≤ 2 x (k+1)2^{\ell+1}\le2^x ( k + 1 ) 2 ℓ + 1 ≤ 2 x , it applies a CNOT with control q [ ctl ( ℓ , k ) ] q[\operatorname{ctl}(\ell,k)] q [ ctl ( ℓ , k )] and target q [ tgt ( ℓ , k ) ] q[\operatorname{tgt}(\ell,k)] q [ tgt ( ℓ , k )] , where ctl ( ℓ , k ) = k ⋅ 2 ℓ + 1 + 2 ℓ − 1 \operatorname{ctl}(\ell,k)=k\cdot2^{\ell+1}+2^\ell-1 ctl ( ℓ , k ) = k ⋅ 2 ℓ + 1 + 2 ℓ − 1 and tgt ( ℓ , k ) = k ⋅ 2 ℓ + 1 + 2 ℓ + 1 − 1 \operatorname{tgt}(\ell,k)=k\cdot2^{\ell+1}+2^{\ell+1}-1 tgt ( ℓ , k ) = k ⋅ 2 ℓ + 1 + 2 ℓ + 1 − 1 . Otherwise, it skips. Its Hoare triple is, for every x x x and every input state ∣ ψ ⟩ |\psi\rangle ∣ ψ ⟩ of the 2 x 2^x 2 x qubits,
⊨ t o t { ∣ ψ ⟩ } B K x { β 2 x ∣ ψ ⟩ } . \models_{\mathrm{tot}}\ \{|\psi\rangle\}\ \mathrm{BK}_x\ \{\beta_{2^x}|\psi\rangle\}. ⊨ tot { ∣ ψ ⟩} BK x { β 2 x ∣ ψ ⟩} .
Here, ∣ f ⟩ |f\rangle ∣ f ⟩ is the computational basis state of the 2 x 2^x 2 x qubits for f ∈ { 0 , 1 } 2 x f\in\{0,1\}^{2^x} f ∈ { 0 , 1 } 2 x , and we also write β 2 x \beta_{2^x} β 2 x for the unitary that maps each ∣ f ⟩ |f\rangle ∣ f ⟩ to ∣ β 2 x f ⟩ |\beta_{2^x}f\rangle ∣ β 2 x f ⟩ . The key components of the Bravyi–Kitaev proof outline are the loops at lines 2 and 4, where we use (Rule-Loop-tot). The first loop uses the loop invariant { t r u e , J ( ℓ ) } \{\mathrm{true},\mathcal{J}(\ell)\} { true , J ( ℓ )} , where J ( ℓ ) \mathcal{J}(\ell) J ( ℓ ) is the state obtained by applying the first ℓ \ell ℓ levels of the circuit to ∣ ψ ⟩ |\psi\rangle ∣ ψ ⟩ . The second loop uses { ℓ < x ∧ k ≤ 2 x , J ′ ( ℓ , k ) } \{\ell< x \land k \le2^x,\mathcal{J}'(\ell,k)\} { ℓ < x ∧ k ≤ 2 x , J ′ ( ℓ , k )} , where J ′ ( ℓ , k ) \mathcal{J}'(\ell,k) J ′ ( ℓ , k ) is obtained by applying the first k k k iterations of level ℓ \ell ℓ to J ( ℓ ) \mathcal{J}(\ell) J ( ℓ ) .
Table 1 (PDF p. 34).
At line 6, the CNOT carries J ′ ( ℓ , k ) \mathcal{J}'(\ell,k) J ′ ( ℓ , k ) to J ′ ( ℓ , k + 1 ) \mathcal{J}'(\ell,k+1) J ′ ( ℓ , k + 1 ) . At line 7, block k k k does not exist, so J ′ ( ℓ , k + 1 ) = J ′ ( ℓ , k ) \mathcal{J}'(\ell,k+1)=\mathcal{J}'(\ell,k) J ′ ( ℓ , k + 1 ) = J ′ ( ℓ , k ) . The second loop ends with J ′ ( ℓ , 2 x ) = J ( ℓ + 1 ) \mathcal{J}'(\ell,2^x)=\mathcal{J}(\ell+1) J ′ ( ℓ , 2 x ) = J ( ℓ + 1 ) . After line 11, ℓ ≥ x \ell\ge x ℓ ≥ x , and J ( ℓ ) = J ( x ) \mathcal{J}(\ell)=\mathcal{J}(x) J ( ℓ ) = J ( x ) because no block exists at a level ℓ ≥ x \ell\ge x ℓ ≥ x . Each CNOT updates one bit of a computational basis state, so the first ℓ \ell ℓ levels map ∣ f ⟩ |f\rangle ∣ f ⟩ to ∣ net ℓ f ⟩ |\operatorname{net}_{\ell} f\rangle ∣ net ℓ f ⟩ , where net ℓ f \operatorname{net}_{\ell} f net ℓ f is obtained from f f f by the bit updates of the first ℓ \ell ℓ levels. Finally, net x f = β 2 x f \operatorname{net}_{x} f=\beta_{2^x}f net x f = β 2 x f states that the x x x levels compute β 2 x \beta_{2^x} β 2 x , so J ( x ) = β 2 x ∣ ψ ⟩ \mathcal{J}(x)=\beta_{2^x}|\psi\rangle J ( x ) = β 2 x ∣ ψ ⟩ by linearity.
The Fermi–Hubbard LCU Block Encoding
Block encoding by a linear combination of unitaries (LCU) embeds a Hamiltonian, scaled by 1 / λ 1/\lambda 1/ λ , in a block of a unitary, and is the input model of quantum signal processing. This example block-encodes the Fermi–Hubbard Hamiltonian by a program with nested loops and classically computed angles and signs. The fermionic operators a j , a j † a_j,a_j^\dagger a j , a j † are linear combinations of Pauli strings under the BK encoding, and we prove that they satisfy the fermionic anticommutation relations. For n = 2 x n=2^x n = 2 x , the Hamiltonian is
H = − t h ∑ j = 0 n − 2 ( a j † a j + 1 + a j + 1 † a j ) + U ∑ j = 0 n − 2 n j n j + 1 = ∑ l = 0 N − 1 c l P l , n j = a j † a j , H=-t_h\sum_{j=0}^{n-2}\left(a_j^\dagger a_{j+1}+a_{j+1}^\dagger a_j\right)+U\sum_{j=0}^{n-2}n_jn_{j+1}=\sum_{l=0}^{N-1}c_lP_l,\qquad n_j=a_j^\dagger a_j, H = − t h j = 0 ∑ n − 2 ( a j † a j + 1 + a j + 1 † a j ) + U j = 0 ∑ n − 2 n j n j + 1 = l = 0 ∑ N − 1 c l P l , n j = a j † a j ,
with N = 6 ( n − 1 ) N = 6(n - 1) N = 6 ( n − 1 ) Pauli strings P l P_l P l , six per adjacent pair, and real coefficients c l c_l c l computed from ( t h , U ) (t_h,U) ( t h , U ) . Let
λ = ∑ l = 0 N − 1 ∣ c l ∣ , ω l = { ∣ c l ∣ / λ , λ > 0 , δ l , l 0 , λ = 0 , \lambda= \sum_{l=0}^{N-1} |c_l|,
\qquad
\omega_l =
\begin{cases}
\sqrt{|c_l|/\lambda}, & \lambda> 0,\\
\delta_{l,l_0}, & \lambda= 0,
\end{cases} λ = l = 0 ∑ N − 1 ∣ c l ∣ , ω l = { ∣ c l ∣/ λ , δ l , l 0 , λ > 0 , λ = 0 ,
τ j = ( ∑ l = j N − 1 ω l 2 ) 1 / 2 , θ j = { arccos ( ω j / τ j ) , τ j > 0 , π / 2 , τ j = 0. \tau_j = \left(\sum_{l=j}^{N-1}\omega_l^2\right)^{1/2},
\qquad
\theta_j =
\begin{cases}
\arccos(\omega_j/\tau_j), & \tau_j > 0,\\
\pi/2, & \tau_j = 0.
\end{cases} τ j = l = j ∑ N − 1 ω l 2 1/2 , θ j = { arccos ( ω j / τ j ) , π /2 , τ j > 0 , τ j = 0.
where l 0 l_0 l 0 is a fixed index, and let sg l = 1 \operatorname{sg}_l = 1 sg l = 1 for c l ≥ 0 c_l \ge0 c l ≥ 0 and sg l = − 1 \operatorname{sg}_l = -1 sg l = − 1 for c l < 0 c_l < 0 c l < 0 . Let G ( θ ) G(\theta) G ( θ ) be the two-qubit rotation by θ \theta θ in the subspace spanned by ∣ 01 ⟩ |01\rangle ∣01 ⟩ and ∣ 10 ⟩ |10\rangle ∣10 ⟩ ,
G ( θ ) ∣ 10 ⟩ = cos θ ∣ 10 ⟩ + sin θ ∣ 01 ⟩ , G ( θ ) ∣ 01 ⟩ = − sin θ ∣ 10 ⟩ + cos θ ∣ 01 ⟩ , G(\theta)|10\rangle= \cos\theta|10\rangle+ \sin\theta|01\rangle,\qquad
G(\theta)|01\rangle= -\sin\theta|10\rangle+ \cos\theta|01\rangle, G ( θ ) ∣10 ⟩ = cos θ ∣10 ⟩ + sin θ ∣01 ⟩ , G ( θ ) ∣01 ⟩ = − sin θ ∣10 ⟩ + cos θ ∣01 ⟩ ,
which acts as the identity on ∣ 00 ⟩ |00\rangle ∣00 ⟩ and ∣ 11 ⟩ |11\rangle ∣11 ⟩ . The sign gate on a [ l ] a[l] a [ l ] has matrix diag ( 1 , sg l ) \operatorname{diag}(1,\operatorname{sg}_l) diag ( 1 , sg l ) .
The program acts on n n n system qubits q [ s ] q[s] q [ s ] and N N N ancilla qubits a [ l ] a[l] a [ l ] , one per Hamiltonian term. We write ∣ 1 l ⟩ a |1_l\rangle_a ∣ 1 l ⟩ a for the basis state with ancilla qubit l l l in ∣ 1 ⟩ |1\rangle ∣1 ⟩ and every other ancilla qubit in ∣ 0 ⟩ |0\rangle ∣0 ⟩ . The classical variables are the loop counters and the Hamiltonian coefficients ( t h , U ) (t_h,U) ( t h , U ) .
The program begins with the ancilla initialization a ˉ : = ∣ 0 ⟩ \bar{a} := |0\rangle a ˉ := ∣0 ⟩ , where a ˉ \bar{a} a ˉ is the sequence of all N N N ancilla qubits. It continues with the circuit L C U LCU L C U , which has the standard prepare–select–unprepare structure. Line 1 applies X X X to a [ 0 ] a[0] a [ 0 ] , and the first while loop (lines 2–6) applies the rotations G ( θ j ( σ ) ) G(\theta_j(\sigma)) G ( θ j ( σ )) to a [ j ] , a [ j + 1 ] a[j],a[j+1] a [ j ] , a [ j + 1 ] , which carries ∣ 10 ⟩ a |10\rangle_a ∣10 ⟩ a to the state with amplitude ω l ( σ ) \omega_l(\sigma) ω l ( σ ) on each ∣ 1 l ⟩ a |1_l\rangle_a ∣ 1 l ⟩ a . The second loop (lines 7–12) runs over the N N N ancilla qubits. Its iteration l l l applies the sign gate to a [ l ] a[l] a [ l ] and then, controlled on a [ l ] a[l] a [ l ] , the single-qubit factors of P l P_l P l to the system qubits, one factor per iteration of the third loop. The fourth loop (lines 13–17) applies the inverse rotations G ( − θ N − 2 − j ( σ ) ) G(-\theta_{N-2-j}(\sigma)) G ( − θ N − 2 − j ( σ )) at the reversed positions, and line 18 applies X X X to a [ 0 ] a[0] a [ 0 ] again.
The four loops use the loop invariants { j ≤ N − 1 , J 0 ( j ) } \{j \le N - 1,\mathcal{J}_0(j)\} { j ≤ N − 1 , J 0 ( j )} , { l ≤ N , J 1 ( l ) } \{l \le N,\mathcal{J}_1(l)\} { l ≤ N , J 1 ( l )} , { l < N ∧ s ≤ n , J 2 ( l , s ) } \{l < N \mathbin{\wedge} s \le n,\mathcal{J}_2(l,s)\} { l < N ∧ s ≤ n , J 2 ( l , s )} , and { j ≤ N − 1 , J 3 ( j ) } \{j \le N - 1,\mathcal{J}_3(j)\} { j ≤ N − 1 , J 3 ( j )} , where
J 0 ( j ) = ( ∑ i < j ω i ∣ 1 i ⟩ a + τ j ∣ 1 j ⟩ a ) ⊗ ∣ ψ ⟩ , J 3 ( j ) = G j − 1 ∑ l ω l sg l ∣ 1 l ⟩ a ⊗ P l ∣ ψ ⟩ , \mathcal{J}_0(j) = \left(\sum_{i<j}\omega_i|1_i\rangle_a+\tau_j|1_j\rangle_a\right)\otimes|\psi\rangle,\qquad
\mathcal{J}_3(j)=G_{j}^{-1}\sum_l\omega_l\operatorname{sg}_l|1_l\rangle_a\otimes P_l|\psi\rangle, J 0 ( j ) = ( i < j ∑ ω i ∣ 1 i ⟩ a + τ j ∣ 1 j ⟩ a ) ⊗ ∣ ψ ⟩ , J 3 ( j ) = G j − 1 l ∑ ω l sg l ∣ 1 l ⟩ a ⊗ P l ∣ ψ ⟩ ,
J 1 ( l ) = ∑ i < l ω i sg i ∣ 1 i ⟩ a ⊗ P i ∣ ψ ⟩ + ∑ i ≥ l ω i ∣ 1 i ⟩ a ⊗ ∣ ψ ⟩ , \mathcal{J}_1(l)=\sum_{i<l}\omega_i\operatorname{sg}_i|1_i\rangle_a\otimes P_i|\psi\rangle+\sum_{i\ge l}\omega_i|1_i\rangle_a\otimes|\psi\rangle, J 1 ( l ) = i < l ∑ ω i sg i ∣ 1 i ⟩ a ⊗ P i ∣ ψ ⟩ + i ≥ l ∑ ω i ∣ 1 i ⟩ a ⊗ ∣ ψ ⟩ ,
J 2 ( l , s ) = ∑ i < l ω i sg i ∣ 1 i ⟩ a ⊗ P i ∣ ψ ⟩ + ω l sg l ∣ 1 l ⟩ a ⊗ P l , < s ∣ ψ ⟩ + ∑ i > l ω i ∣ 1 i ⟩ a ⊗ ∣ ψ ⟩ . \mathcal{J}_2(l,s)=\sum_{i<l}\omega_i\operatorname{sg}_i|1_i\rangle_a\otimes P_i|\psi\rangle+\omega_l\operatorname{sg}_l|1_l\rangle_a\otimes P_{l,<s}|\psi\rangle+\sum_{i>l}\omega_i|1_i\rangle_a\otimes|\psi\rangle. J 2 ( l , s ) = i < l ∑ ω i sg i ∣ 1 i ⟩ a ⊗ P i ∣ ψ ⟩ + ω l sg l ∣ 1 l ⟩ a ⊗ P l , < s ∣ ψ ⟩ + i > l ∑ ω i ∣ 1 i ⟩ a ⊗ ∣ ψ ⟩ .
Here, P l , < s P_{l,<s} P l , < s is the product of the first s s s single-qubit factors of P l P_l P l , ( P l ) s (P_l)_s ( P l ) s is the factor acting on q [ s ] q[s] q [ s ] , and G j − 1 G_j^{-1} G j − 1 is the product of the first j j j inverse rotations of the fourth loop. The invariant J 0 ( j ) \mathcal{J}_0(j) J 0 ( j ) means that the ancilla amplitudes ω 0 , … , ω j − 1 \omega_0,\ldots,\omega_{j-1} ω 0 , … , ω j − 1 are prepared, and J 1 ( l ) \mathcal{J}_1(l) J 1 ( l ) means that the signed Pauli strings of the first l l l terms are applied. The LCU proof outline derives
∣ = t o t { ∣ 0 ⋯ 0 ⟩ a ⊗ ∣ ψ ⟩ } L C U { ∣ Φ o u t ( σ ) ⟩ } , Φ o u t ( σ ) = X a [ 0 ] J 3 ( N − 1 ) , |=_{\mathrm{tot}}\{|0\cdots0\rangle_a\otimes|\psi\rangle\}\ LCU\ \{|\Phi_{\mathrm{out}}(\sigma)\rangle\},\qquad
\Phi_{\mathrm{out}}(\sigma)=X_{a[0]}\mathcal{J}_3(N-1), ∣ = tot { ∣0 ⋯ 0 ⟩ a ⊗ ∣ ψ ⟩} L C U { ∣ Φ out ( σ )⟩} , Φ out ( σ ) = X a [ 0 ] J 3 ( N − 1 ) ,
for every x > 0 x>0 x > 0 and every input state ∣ ψ ⟩ |\psi\rangle ∣ ψ ⟩ of the system qubits.
Table 13.
(continued)
6 od { j = N − 1 , J ( 0 ) ( N − 1 ) } \{j=N-1,\ \mathcal{J}_{(0)}(N-1)\} { j = N − 1 , J ( 0 ) ( N − 1 )} { t r u e , ∑ l = 0 N − 1 ω l ( σ ) ∣ 1 l ⟩ a ⊗ ∣ ψ ⟩ } \{\mathrm{true},\ \sum_{l=0}^{N-1}\omega_l(\sigma)\lvert1_l\rangle_a\otimes\lvert\psi\rangle\} { true , ∑ l = 0 N − 1 ω l ( σ ) ∣ 1 l ⟩ a ⊗ ∣ ψ ⟩} (Rule-Conseq)
7 l : = 0 l:=0 l := 0 (Axiom-Ass), (Rule-Conseq) { l = 0 , J ( 1 ) ( l ) } \{l=0,\ \mathcal{J}_{(1)}(l)\} { l = 0 , J ( 1 ) ( l )}
8 while l < N l<N l < N do (Rule-Loop-tot), t = N − l t=N-l t = N − l { l < N , J ( 1 ) ( l ) } \{l<N,\ \mathcal{J}_{(1)}(l)\} { l < N , J ( 1 ) ( l )}
9 sgl ( σ ) [ a [ l ] ] \operatorname{sgl}(\sigma)[a[l]] sgl ( σ ) [ a [ l ]] (Axiom-Uni) { l < N , J ( 2 ) ( l , 0 ) } \{l<N,\ \mathcal{J}_{(2)}(l,0)\} { l < N , J ( 2 ) ( l , 0 )}
10 s : = 0 s:=0 s := 0 ; while s < n s<n s < n do (Rule-Loop-tot), t = n − s , J ( 2 ) ( l , s ) t=n-s,\ \mathcal{J}_{(2)}(l,s) t = n − s , J ( 2 ) ( l , s ) c − ( P l ) s [ a [ l ] , q [ s ] ] c-(P_l)_s[a[l],q[s]] c − ( P l ) s [ a [ l ] , q [ s ]] ; s : = s + 1 s:=s+1 s := s + 1 od { l < N , J ( 1 ) ( l + 1 ) } \{l<N,\ \mathcal{J}_{(1)}(l+1)\} { l < N , J ( 1 ) ( l + 1 )}
11 l : = l + 1 l:=l+1 l := l + 1 (Axiom-Ass), (Rule-Conseq) { l ≤ N , J ( 1 ) ( l ) } \{l\le N,\ \mathcal{J}_{(1)}(l)\} { l ≤ N , J ( 1 ) ( l )}
12 od { l = N , J ( 1 ) ( N ) } \{l=N,\ \mathcal{J}_{(1)}(N)\} { l = N , J ( 1 ) ( N )} { t r u e , ∑ l = 0 N − 1 ω l ( σ ) sg l ( σ ) ∣ 1 l ⟩ a ⊗ P l ∣ ψ ⟩ } \{\mathrm{true},\ \sum_{l=0}^{N-1}\omega_l(\sigma)\operatorname{sg}_l(\sigma)\lvert1_l\rangle_a\otimes P_l\lvert\psi\rangle\} { true , ∑ l = 0 N − 1 ω l ( σ ) sg l ( σ ) ∣ 1 l ⟩ a ⊗ P l ∣ ψ ⟩} (Rule-Conseq)
13 j : = 0 j:=0 j := 0 (Axiom-Ass), (Rule-Conseq) { j = 0 , J ( 3 ) ( j ) } \{j=0,\ \mathcal{J}_{(3)}(j)\} { j = 0 , J ( 3 ) ( j )}
14 while j < N − 1 j<N-1 j < N − 1 do (Rule-Loop-tot), t = ( N − 1 ) − j t=(N-1)-j t = ( N − 1 ) − j { j < N − 1 , J ( 3 ) ( j ) } \{j<N-1,\ \mathcal{J}_{(3)}(j)\} { j < N − 1 , J ( 3 ) ( j )}
15 G ( − θ N − 2 − j ( σ ) ) [ a [ N − 2 − j ] , a [ N − 1 − j ] ] G(-\theta_{N-2-j}(\sigma))[a[N-2-j],a[N-1-j]] G ( − θ N − 2 − j ( σ )) [ a [ N − 2 − j ] , a [ N − 1 − j ]] (Axiom-Uni) { j < N − 1 , J ( 3 ) ( j + 1 ) } \{j<N-1,\ \mathcal{J}_{(3)}(j+1)\} { j < N − 1 , J ( 3 ) ( j + 1 )}
16 j : = j + 1 j:=j+1 j := j + 1 (Axiom-Ass), (Rule-Conseq) { j ≤ N − 1 , J ( 3 ) ( j ) } \{j\le N-1,\ \mathcal{J}_{(3)}(j)\} { j ≤ N − 1 , J ( 3 ) ( j )}
17 od { j = N − 1 , J ( 3 ) ( N − 1 ) } \{j=N-1,\ \mathcal{J}_{(3)}(N-1)\} { j = N − 1 , J ( 3 ) ( N − 1 )} { t r u e , J ( 3 ) ( N − 1 ) } \{\mathrm{true},\ \mathcal{J}_{(3)}(N-1)\} { true , J ( 3 ) ( N − 1 )} (Rule-Conseq)
18 X [ a [ 0 ] ] X[a[0]] X [ a [ 0 ]] (Axiom-Uni) { t r u e , ∣ Φ o u t ( σ ) ⟩ } \{\mathrm{true},\ \lvert\Phi_{\mathrm{out}}(\sigma)\rangle\} { true , ∣ Φ out ( σ )⟩}
The verification condition for the rotation at line 4 is
G ( θ j ( σ ) ) [ a [ j ] , a [ j + 1 ] ] ( τ j ∣ 1 j ⟩ a ) = ω j ∣ 1 j ⟩ a + τ j + 1 ∣ 1 j + 1 ⟩ a , (20) G(\theta_j(\sigma))[a[j],a[j+1]]\left(\tau_j\lvert1_j\rangle_a\right)=\omega_j\lvert1_j\rangle_a+\tau_{j+1}\lvert1_{j+1}\rangle_a, \tag*{(20)} G ( θ j ( σ )) [ a [ j ] , a [ j + 1 ]] ( τ j ∣ 1 j ⟩ a ) = ω j ∣ 1 j ⟩ a + τ j + 1 ∣ 1 j + 1 ⟩ a , (20)
which carries J ( 0 ) ( j ) \mathcal{J}_{(0)}(j) J ( 0 ) ( j ) to J ( 0 ) ( j + 1 ) \mathcal{J}_{(0)}(j+1) J ( 0 ) ( j + 1 ) . Since the fourth loop applies the inverses of the rotations of the first loop in reverse order, ( ⟨ 1 0 ∣ a ⊗ I ) G N − 1 − 1 ⋯ G 0 − 1 = ∑ l = 0 N − 1 ω l ( σ ) ( ⟨ 1 l ∣ a ⊗ I ) (\langle1_0\rvert_a\otimes I)G^{-1}_{N-1}\cdots G^{-1}_0=\sum_{l=0}^{N-1}\omega_l(\sigma)(\langle1_l\rvert_a\otimes I) (⟨ 1 0 ∣ a ⊗ I ) G N − 1 − 1 ⋯ G 0 − 1 = ∑ l = 0 N − 1 ω l ( σ ) (⟨ 1 l ∣ a ⊗ I ) . Together with J ( 3 ) ( 0 ) = J ( 1 ) ( N ) \mathcal{J}_{(3)}(0)=\mathcal{J}_{(1)}(N) J ( 3 ) ( 0 ) = J ( 1 ) ( N ) and ω l 2 sg l = c l / λ \omega_l^2\operatorname{sg}_l=c_l/\lambda ω l 2 sg l = c l / λ , this gives, for λ ( σ ) > 0 \lambda(\sigma)>0 λ ( σ ) > 0 , the block-encoding identity
( ⟨ 0 ⋯ 0 ∣ a ⊗ I ) ∣ Φ o u t ( σ ) ⟩ = λ ( σ ) − 1 H ∣ ψ ⟩ . (21) (\langle0\cdots0\rvert_a\otimes I)\lvert\Phi_{\mathrm{out}}(\sigma)\rangle=\lambda(\sigma)^{-1}H\lvert\psi\rangle. \tag*{(21)} (⟨ 0 ⋯ 0 ∣ a ⊗ I ) ∣ Φ out ( σ )⟩ = λ ( σ ) − 1 H ∣ ψ ⟩ . (21)
For every quantum predicate K K K on the system qubits, 0 ≤ K ≤ I 0\le K\le I 0 ≤ K ≤ I , and every x > 0 x>0 x > 0 , we derive
⊨ t o t { λ ( σ ) > 0 , I a ⊗ λ ( σ ) − 2 H † K H ∣ a ˉ : = ∣ 0 ⟩ ; L C U ∣ ∣ 0 ⋯ 0 ⟩ ⟨ 0 ⋯ 0 ∣ a ⊗ K } . (22) \models_{\mathrm{tot}}\{\lambda(\sigma)>0,\ I_a\otimes\lambda(\sigma)^{-2}H^\dagger K H\mid\bar a:=\lvert0\rangle;\ \mathrm{LCU}\mid\lvert0\cdots0\rangle\langle0\cdots0\rvert_a\otimes K\}. \tag*{(22)} ⊨ tot { λ ( σ ) > 0 , I a ⊗ λ ( σ ) − 2 H † K H ∣ a ˉ := ∣ 0 ⟩ ; LCU ∣ ∣ 0 ⋯ 0 ⟩ ⟨ 0 ⋯ 0 ∣ a ⊗ K } . (22)
We derive this Hoare triple backward from the postcondition A = ∣ 0 ⋯ 0 ⟩ ⟨ 0 ⋯ 0 ∣ a ⊗ K A=\lvert0\cdots0\rangle\langle0\cdots0\rvert_a\otimes K A = ∣ 0 ⋯ 0 ⟩ ⟨ 0 ⋯ 0 ∣ a ⊗ K . Each gate and the initialization use the precondition given by their axioms. A loop with bound M M M uses the loop invariant W j † C W j W_j^\dagger C W_j W j † C W j , where C C C is the quantum predicate required after the loop and W j W_j W j is the unitary applied by its iterations j , … , M − 1 j,\ldots,M-1 j , … , M − 1 , which is I I I for j ≥ M j\ge M j ≥ M . Its verification conditions are
W j = I for j ≥ M , U j † ( W j + 1 † C W j + 1 ) U j = W j † C W j for j < M , W_j=I\ \text{ for }j\ge M,\qquad U_j^\dagger(W_{j+1}^\dagger C W_{j+1})U_j=W_j^\dagger C W_j\ \text{ for }j<M, W j = I for j ≥ M , U j † ( W j + 1 † C W j + 1 ) U j = W j † C W j for j < M ,
where U j U_j U j is the unitary applied by iteration j j j . The third loop is derived in the same way. For an initialization or a circuit P P P and a quantum predicate A A A , we write wp ( P , A ) \operatorname{wp}(P,A) wp ( P , A ) for the precondition computed from A A A by the axioms (§ 5.4 ) and these loop invariants. The Hoare triple (22) then follows from (Axiom-Init), the LCU proof outline with these loop invariants, and (Rule-Conseq), whose premise
I a ⊗ λ ( σ ) − 2 H † K H ≤ wp ( a ˉ : = ∣ 0 ⟩ ; LCU , A ) ( σ ) ( λ ( σ ) > 0 ) I_a \otimes\lambda(\sigma)^{-2} H^\dagger K H \le\operatorname{wp}(\bar{a} := |0\rangle; \operatorname{LCU}, A)(\sigma) \qquad(\lambda(\sigma) > 0) I a ⊗ λ ( σ ) − 2 H † K H ≤ wp ( a ˉ := ∣0 ⟩ ; LCU , A ) ( σ ) ( λ ( σ ) > 0 )
holds with equality by the block-encoding identity (21) . Hence the precondition of (22) equals wp ( a ˉ : = ∣ 0 ⟩ ; LCU , A ) \operatorname{wp}(\bar{a} := |0\rangle; \operatorname{LCU}, A) wp ( a ˉ := ∣0 ⟩ ; LCU , A ) . The same derivation applies to I − A I-A I − A . Since the total output trace is at most the input trace, the expectation of A A A in the output equals that of the precondition in the input. Taking K = I K=I K = I gives the success probability λ − 2 ∥ H ∣ ψ ⟩ ∥ 2 \lambda^{-2}\lVert H|\psi\rangle\rVert^{2} λ − 2 ∥ H ∣ ψ ⟩ ∥ 2 . As K K K ranges over all quantum predicates, the precondition determines the successful output λ − 2 H ∣ ψ ⟩ ⟨ ψ ∣ H † \lambda^{-2}H|\psi\rangle\langle\psi|H^\dagger λ − 2 H ∣ ψ ⟩ ⟨ ψ ∣ H † .
Quantum signal processing (QSP) applies polynomial transformations to a block-encoded operator and underlies efficient Hamiltonian simulation [18 , 29 ] . The QSP program alternates the LCU circuit of Appendix § 12 .7.2 with phase gates and reflections on the ancilla qubit a [ 0 ] a[0] a [ 0 ] . After the ancilla initialization a ˉ : = ∣ 0 ⟩ \bar{a} := |0\rangle a ˉ := ∣0 ⟩ , the program Q S P d QSP_d QS P d applies B K x BK_x B K x (Appendix § 12 .7.1) to the system qubits and then runs a loop of depth d d d on the counter k k k . Iteration k k k applies the phase gate p h a s e ( Φ [ k ] ) [ a [ 0 ] ] phase(\Phi[k])[a[0]] p ha se ( Φ [ k ]) [ a [ 0 ]] , with the angle Φ [ k ] \Phi[k] Φ [ k ] given by the classical array Φ \Phi Φ , then L C U LCU L C U , then Z [ a [ 0 ] ] Z[a[0]] Z [ a [ 0 ]] , and increments k k k .
Let e ψ = ∣ 0 ⋯ 0 ⟩ a ⊗ β x 2 ∣ ψ ⟩ e_\psi= |0\cdots0\rangle_a \otimes\beta_x^2|\psi\rangle e ψ = ∣0 ⋯ 0 ⟩ a ⊗ β x 2 ∣ ψ ⟩ , and let U L C U U_{LCU} U L C U , P h ( α ) Ph(\alpha) P h ( α ) , and R Z R_Z R Z be the unitaries applied by L C U LCU L C U , p h a s e ( α ) [ a [ 0 ] ] phase(\alpha)[a[0]] p ha se ( α ) [ a [ 0 ]] , and Z [ a [ 0 ] ] Z[a[0]] Z [ a [ 0 ]] ; the single-qubit matrix of p h a s e ( α ) phase(\alpha) p ha se ( α ) is diag ( 1 , e i α ) \operatorname{diag}(1,e^{i\alpha}) diag ( 1 , e i α ) . Let
Π m = ∏ k = 0 m − 1 R Z U L C U P h ( Φ [ k ] ) \Pi_m = \prod_{k=0}^{m-1} R_Z U_{LCU} Ph(\Phi[k]) Π m = k = 0 ∏ m − 1 R Z U L C U P h ( Φ [ k ])
[18 ] , with the k = 0 k=0 k = 0 factor acting first. The QSP proof outline derives
∣ t o t { ∣ 0 ⋯ 0 ⟩ a ⊗ ∣ ψ ⟩ } Q S P d { ∣ Ξ o u t ( σ ) ⟩ } , Ξ o u t ( σ ) = Π d e ψ , \lvert_{\mathrm{tot}}\{ |0\cdots0\rangle_a \otimes|\psi\rangle\} \ QSP_d\ \{|\Xi_{\mathrm{out}}(\sigma)\rangle\}, \qquad\Xi_{\mathrm{out}}(\sigma)=\Pi_d e_\psi, ∣ tot { ∣0 ⋯ 0 ⟩ a ⊗ ∣ ψ ⟩} QS P d { ∣ Ξ out ( σ )⟩} , Ξ out ( σ ) = Π d e ψ ,
for every x > 0 x>0 x > 0 , depth d d d , and input state ∣ ψ ⟩ |\psi\rangle ∣ ψ ⟩ . The key component of the outline is the loop at line 3, where we use (Rule-Loop-tot). The loop invariant is { k ≤ d , L ( k ) } \{k\le d,\mathcal{L}(k)\} { k ≤ d , L ( k )} , where L ( k ) = Π k e ψ \mathcal{L}(k)=\Pi_k e_\psi L ( k ) = Π k e ψ .
Table 14.
Line 1 uses the Hoare triple of Appendix § 12 .7.1 on the system qubits, which leaves the ancilla qubits unchanged. At line 5, the loops of L C U LCU L C U use as loop invariants the states obtained by applying the gates of each loop up to the current iteration to Ph ( Φ [ k ] ) Π k e ψ \operatorname{Ph}(\Phi[k])\Pi_k e_\psi Ph ( Φ [ k ]) Π k e ψ . The verification conditions hold by the definition of Π k \Pi_k Π k : Π 0 e ψ = e ψ \Pi_0 e_\psi=e_\psi Π 0 e ψ = e ψ , each iteration maps Π k e ψ \Pi_k e_\psi Π k e ψ to R Z U L C U Ph ( Φ [ k ] ) Π k e ψ = Π k + 1 e ψ R_ZU_{\mathrm{LCU}}\operatorname{Ph}(\Phi[k])\Pi_k e_\psi=\Pi_{k+1}e_\psi R Z U LCU Ph ( Φ [ k ]) Π k e ψ = Π k + 1 e ψ , and the loop ends with Π d e ψ = Ξ o u t ( σ ) \Pi_d e_\psi=\Xi_{\mathrm{out}}(\sigma) Π d e ψ = Ξ out ( σ ) .
Let ∣ v ⟩ |v\rangle ∣ v ⟩ be an eigenstate with H ∣ v ⟩ = E ∣ v ⟩ H|v\rangle=E|v\rangle H ∣ v ⟩ = E ∣ v ⟩ , and assume λ > 0 \lambda>0 λ > 0 and ∣ E ∣ < λ |E|<\lambda ∣ E ∣ < λ . We prove that, on the states the loop reaches from ∣ 0 ⋯ 0 ⟩ a ⊗ ∣ v ⟩ |0\cdots0\rangle_a\otimes|v\rangle ∣0 ⋯ 0 ⟩ a ⊗ ∣ v ⟩ , R Z R_Z R Z is the reflection about the subspace with all ancilla qubits in ∣ 0 ⟩ a |0\rangle_a ∣0 ⟩ a , and that the loop preserves a two-dimensional subspace containing ∣ 0 ⋯ 0 ⟩ a ⊗ ∣ v ⟩ |0\cdots0\rangle_a\otimes|v\rangle ∣0 ⋯ 0 ⟩ a ⊗ ∣ v ⟩ [18 ] . In a basis of this subspace that starts with ∣ 0 ⋯ 0 ⟩ a ⊗ ∣ v ⟩ |0\cdots0\rangle_a\otimes|v\rangle ∣0 ⋯ 0 ⟩ a ⊗ ∣ v ⟩ , the upper-left entry of Π d \Pi_d Π d is, with c = E / λ c=E/\lambda c = E / λ ,
P Φ ( c ) = [ ∏ k = 0 d − 1 ( c e i Φ [ k ] 1 − c 2 − 1 − c 2 e i Φ [ k ] c ) ] 00 , (23) P_{\Phi}(c)=\left[\prod_{k=0}^{d-1}\begin{pmatrix} c & e^{i\Phi[k]}\sqrt{1-c^2} \\ -\sqrt{1-c^2} & e^{i\Phi[k]}c \end{pmatrix}\right]_{00}, \tag*{(23)} P Φ ( c ) = [ k = 0 ∏ d − 1 ( c − 1 − c 2 e i Φ [ k ] 1 − c 2 e i Φ [ k ] c ) ] 00 , (23)
with the k = 0 k=0 k = 0 factor acting first, as in Π d \Pi_d Π d . Hence
( ⟨ 0 ⋯ 0 ∣ a ⊗ I ) Π d ( ∣ 0 ⋯ 0 ⟩ a ⊗ ∣ v ⟩ ) = P Φ ( E / λ ) ∣ v ⟩ . (\langle0\cdots0|_a\otimes I)\Pi_d(|0\cdots0\rangle_a\otimes|v\rangle)=P_{\Phi}(E/\lambda)|v\rangle. (⟨ 0 ⋯ 0 ∣ a ⊗ I ) Π d ( ∣0 ⋯ 0 ⟩ a ⊗ ∣ v ⟩) = P Φ ( E / λ ) ∣ v ⟩ .
For arbitrary inputs, assume λ > 0 \lambda>0 λ > 0 and ∣ E ∣ < λ |E|<\lambda ∣ E ∣ < λ for every eigenvalue E E E of H H H , and define P Φ ( H / λ ) P_{\Phi}(H/\lambda) P Φ ( H / λ ) by ∣ v ⟩ ↦ P Φ ( E / λ ) ∣ v ⟩ |v\rangle\mapsto P_{\Phi}(E/\lambda)|v\rangle ∣ v ⟩ ↦ P Φ ( E / λ ) ∣ v ⟩ for each eigenstate ∣ v ⟩ |v\rangle ∣ v ⟩ with H ∣ v ⟩ = E ∣ v ⟩ H|v\rangle=E|v\rangle H ∣ v ⟩ = E ∣ v ⟩ . By linearity, the eigenstate identity extends to
( ⟨ 0 ⋯ 0 ∣ a ⊗ I ) Π d ( ∣ 0 ⋯ 0 ⟩ a ⊗ I ) = P Φ ( H / λ ) . (\langle0\cdots0|_a\otimes I)\Pi_d(|0\cdots0\rangle_a\otimes I)=P_{\Phi}(H/\lambda). (⟨ 0 ⋯ 0 ∣ a ⊗ I ) Π d ( ∣0 ⋯ 0 ⟩ a ⊗ I ) = P Φ ( H / λ ) .
Including the initial BK transformation, the block is therefore
( ⟨ 0 ⋯ 0 ∣ a ⊗ I ) Π d ( I a ⊗ β 2 x ) ( ∣ 0 ⋯ 0 ⟩ a ⊗ I ) = P Φ ( H / λ ) β 2 x . (\langle0\cdots0|_a\otimes I)\Pi_d(I_a\otimes\beta_{2x})(|0\cdots0\rangle_a\otimes I)=P_{\Phi}(H/\lambda)\beta_{2x}. (⟨ 0 ⋯ 0 ∣ a ⊗ I ) Π d ( I a ⊗ β 2 x ) ( ∣0 ⋯ 0 ⟩ a ⊗ I ) = P Φ ( H / λ ) β 2 x .
Let φ s p \varphi_{\mathrm{sp}} φ sp denote these spectral hypotheses. They hold, for example, when x = 1 x=1 x = 1 and t h = U = 1 t_h=U=1 t h = U = 1 . For every quantum predicate K K K on the system qubits, 0 ≤ K ≤ I 0\le K\le I 0 ≤ K ≤ I , we derive
⊨ t o t { φ s p , I a ⊗ β 2 x † P Φ ( H / λ ) † K P Φ ( H / λ ) β 2 x } a ˉ : = ∣ 0 ⟩ ; Q S P d \models_{\mathrm{tot}}\{\varphi_{\mathrm{sp}},I_a\otimes\beta_{2x}^{\dagger}P_{\Phi}(H/\lambda)^{\dagger}KP_{\Phi}(H/\lambda)\beta_{2x}\}\ \bar a:=|0\rangle;\ QSP_d ⊨ tot { φ sp , I a ⊗ β 2 x † P Φ ( H / λ ) † K P Φ ( H / λ ) β 2 x } a ˉ := ∣0 ⟩ ; QS P d
{ ∣ 0 ⋯ 0 ⟩ ⟨ 0 ⋯ 0 ∣ a ⊗ K } . (24) \{|0\cdots0\rangle\langle0\cdots0|_a\otimes K\}. \tag*{(24)} { ∣0 ⋯ 0 ⟩ ⟨ 0 ⋯ 0 ∣ a ⊗ K } . (24)
We derive it in the same way as (22) , with Q S P d QSP_d QS P d in place of L C U LCU L C U and A = ∣ 0 ⋯ 0 ⟩ ⟨ 0 ⋯ 0 ∣ a ⊗ K A=|0\cdots0\rangle\langle0\cdots0|_a\otimes K A = ∣0 ⋯ 0 ⟩ ⟨ 0 ⋯ 0 ∣ a ⊗ K . Its loop invariant is W k † A W k W_k^{\dagger}AW_k W k † A W k , where W k = ∏ r = k d − 1 R Z U L C U Ph ( Φ [ r ] ) W_k=\prod_{r=k}^{d-1}R_ZU_{\mathrm{LCU}}\operatorname{Ph}(\Phi[r]) W k = ∏ r = k d − 1 R Z U LCU Ph ( Φ [ r ]) , with the r = k r=k r = k factor acting first, and the bound function is max ( d − k , 0 ) \max(d-k,0) max ( d − k , 0 ) . The initial (Rule-Conseq) premise holds with equality:
wp ( a ˉ : = ∣ 0 ⟩ ; Q S P d , A ) = I a ⊗ β 2 x † P Φ ( H / λ ) † K P Φ ( H / λ ) β 2 x ( φ s p ) . \operatorname{wp}(\bar a:=|0\rangle;QSP_d,A)=I_a\otimes\beta_{2x}^{\dagger}P_{\Phi}(H/\lambda)^{\dagger}KP_{\Phi}(H/\lambda)\beta_{2x}\quad(\varphi_{\mathrm{sp}}). wp ( a ˉ := ∣0 ⟩ ; QS P d , A ) = I a ⊗ β 2 x † P Φ ( H / λ ) † K P Φ ( H / λ ) β 2 x ( φ sp ) .
Methods for choosing Φ \Phi Φ so that P Φ P_{\Phi} P Φ approximates a target polynomial are given in [18 , 29 ] .