Introduction
Quantum computers aim to solve highly complex problems by leveraging quantum superposition, which enables substantially faster algorithms, such as Shor’s algorithm, than classical algorithms. Because of the exponential state space of quantum superpositions and the inherently probabilistic, nondeterministic nature of quantum systems, traditional validation techniques based on runtime testing are virtually impossible to develop for large quantum programs. Many frameworks have been proposed to verify quantum programs [3 , 11 ] using interactive theorem provers, such as Isabelle, Rocq, and Why3, while others have proposed some proof automation by creating proof systems [5 , 15 , 17 , 19 , 26 , 28 , 35 ] , but quantum program verification is still time-consuming.
While quantum computation might be more powerful than classical computation, an interesting question is whether verifying quantum programs is more difficult than verifying classical programs. The key is to establish an effective compilation from quantum program verification to a verification problem in a classical framework, thereby relating quantum and classical operations and providing a classical analogy for quantum programs.
In this paper, we propose a set of classical aggregate operations as the target for compiling quantum operations. Such operations are well-known in higher-order functional programming and are associated with a large and effective verification library. Based on these operations, we transform the Qafny quantum proof system to classical Hoare logic, and illustrate the same approach for Quantum Hoare Logic (QHL) [8 ] to show its generality. While these classical operations might be computationally expensive, their verification might not be expensive, i.e., it suffices to verify correctness by examining a symbolic representative value of the quantum state after applying a quantum operation. When one applies a quantum operation to a quantum state that can be thought of as a set of classical state values, it cannot manipulate a singleton classical state value without affecting the others. In fact, the manipulation of the operation has a similar effect across different classical state values, which can be summarized by a symbolic state representing the quantum state.
Authors’ Contact Information: Liyi Li, Iowa State University, USA, liyili2@iastate.edu; Zouk arneini Difaizi Tagba, Iowa State University, USA, difaizi@iastate.edu; Alex Potanin, Australian National University, Australia, alex.potanin@anu.edu.au.
To connect quantum and classical proof systems, we develop a new methodology for relating them. In verifying the transformation from quantum proof systems to classical Hoare logic, we develop soundness and completeness theorems based on such theorems in the compilation sense (Figure 1 ), rather than relying on the proof system soundness and relative completeness, which relate proof systems and their underlying language semantics. The right demonstrates the flow of what would happen by using proof system soundness and relative completeness for our transformation, which fails in place of utilizing the Hoare logic relative completeness to transform a semantic system back to logical assertions, because relative completeness cannot suggest the existence of an effective program verification via the transformation.
Figure 1. Two different kinds of soundness and completeness theorems (ours on the left).
Essentially, the main issue arises at predicate transformation. Consider a simple quantum operation ( q ˉ ← λ x . ∣ x + 3 ⟩ ) (\bar{q} \leftarrow\lambda x. \lvert x + 3 \rangle) ( q ˉ ← λ x . ∣ x + 3 ⟩) to add 3 on each basis-vector of a quantum state φ \varphi φ for q ˉ \bar{q} q ˉ , where φ \varphi φ is a superposition state ∑ j α j ∣ j ⟩ \sum_j \alpha_j \lvert j \rangle ∑ j α j ∣ j ⟩ . When performing verification, users also need to support a logical post-condition q ˉ = ∑ j α j ∣ j + 3 ⟩ \bar{q} = \sum_j \alpha_j \lvert j + 3 \rangle q ˉ = ∑ j α j ∣ j + 3 ⟩ , where ∑ j α j ∣ j + 3 ⟩ \sum_j \alpha_j \lvert j + 3 \rangle ∑ j α j ∣ j + 3 ⟩ is an expression for describing the quantum state φ \varphi φ after the application, resulting in a quantum state φ ′ \varphi' φ ′ . The post-condition transformation based on the right side of Figure 1 is listed below.
q ˉ = ∑ j α j ∣ j + 3 ⟩ → { q ˉ : φ ′ } → { q ˉ : trans ( φ ′ ) } → q ˉ = list ( trans ( φ ′ ) ) \bar{q} = \sum_j \alpha_j \lvert j + 3 \rangle\to\{\bar{q} : \varphi'\} \to\{\bar{q} : \operatorname{trans}(\varphi')\} \to\bar{q} = \operatorname{list}(\operatorname{trans}(\varphi')) q ˉ = j ∑ α j ∣ j + 3 ⟩ → { q ˉ : φ ′ } → { q ˉ : trans ( φ ′ )} → q ˉ = list ( trans ( φ ′ ))
The first transformation above transforms the post-condition to a post-state, a map structure mapping from q ˉ \bar{q} q ˉ to a quantum state φ ′ \varphi' φ ′ . Writing down the state can potentially be exponential. The next step is to transform the state by using some classical state structure (trans), e.g., lists or arrays. The last step turns the classical state structure back into a predicate, yielding a possibly exponential listing of different singleton states in the quantum superposition state φ ′ \varphi' φ ′ .
To connect quantum and classical proof systems properly, as in the left of Figure 1 , we develop completeness and soundness theorems for the translation itself, in the style of compiler correctness, to relate the validity of the two systems; i.e., the translation completeness theorem states that everything derivable in the quantum proof system is also derivable in Hoare logic, while the translation soundness theorem states that if the translated program is derivable in Hoare logic, then the quantum proof system also derives it, up to semantic equivalence of the assertions.
Contributions and Roadmap
Qafny [17 ] proposes a quantum proof system, and Qafnyc {}^{c} c [5 ] realizes it by compiling Qafny verification conditions into Dafny, reporting 37 verified programs; that compilation, however, comes with no correctness argument. This paper supplies the missing foundation, developed for Qafny and illustrated for QHL: every quantum program verification via the Qafny proof system can be viewed as a verification in the Hoare logic system, which is our translation completeness theorem. Translation soundness adds that a valid translated Hoare triple certifies only specifications that Qafny itself derives, so the transformation from Qafny to Hoare logic does not produce garbage. Below are the detailed contributions.
We invent an intermediate Hoare logic IR (HLIR) as the target language. We then develop a transformation from a quantum proof system to Hoare logic based on HLIR. We demonstrate the transformation by compiling the Qafny proof system to Hoare logic with array theory (HLIR), demonstrating that quantum program verification can be carried out via a classical Hoare logic proof system.
We prove translation completeness and translation soundness theorems for the compilation, establishing a theoretical foundation: every quantum program verification can be done effectively in Hoare logic, indicating that quantum program verification is no harder than classical program verification.
We also examine tensor-based separation conjunctions in Qafny as well as other proof systems [15 , 30 ] . We show that these quantum separation conjunctions are pure, in the sense that they can be replaced by classical conjunctions.
Background
Quantum Data. A quantum datum consists of one or more quantum bits (qubits), which can be expressed as a two-dimensional vector ( z 1 z 2 ) \begin{pmatrix} z_1 \\ z_2 \end{pmatrix} ( z 1 z 2 ) where the amplitudes z 1 z_1 z 1 and z 2 z_2 z 2 are complex numbers and ∣ z 1 ∣ 2 + ∣ z 2 ∣ 2 = 1 \lvert z_1\rvert^2 + \lvert z_2\rvert^2 = 1 ∣ z 1 ∣ 2 + ∣ z 2 ∣ 2 = 1 . We frequently write the qubit vector as z 1 ∣ 0 ⟩ + z 2 ∣ 1 ⟩ z_1\lvert0\rangle+ z_2\lvert1\rangle z 1 ∣ 0 ⟩ + z 2 ∣ 1 ⟩ (the Dirac notation [Dirac 1939]), where ∣ 0 ⟩ = ( 1 0 ) \lvert0\rangle= \begin{pmatrix} 1 \\ 0 \end{pmatrix} ∣ 0 ⟩ = ( 1 0 ) and ∣ 1 ⟩ = ( 0 1 ) \lvert1\rangle= \begin{pmatrix} 0 \\ 1 \end{pmatrix} ∣ 1 ⟩ = ( 0 1 ) are computational basis-kets. When both z 1 z_1 z 1 and z 2 z_2 z 2 are non-zero, we can think of the qubit being “both 0 and 1 at once,” a.k.a. in a superposition [22 ] , e.g., 1 2 ( ∣ 0 ⟩ + ∣ 1 ⟩ ) \frac{1}{\sqrt{2}}(\lvert0\rangle+ \lvert1\rangle) 2 1 (∣ 0 ⟩ + ∣ 1 ⟩) represents a superposition of ∣ 0 ⟩ \lvert0\rangle ∣ 0 ⟩ and ∣ 1 ⟩ \lvert1\rangle ∣ 1 ⟩ . Larger quantum data can be formed by composing smaller ones with the tensor product (⊗ \otimes ⊗ ) from linear algebra, e.g., the two-qubit datum ∣ 0 ⟩ ⊗ ∣ 1 ⟩ \lvert0\rangle\otimes\lvert1\rangle ∣ 0 ⟩ ⊗ ∣ 1 ⟩ (also written as ∣ 01 ⟩ \lvert01\rangle ∣ 01 ⟩ ) corresponds to the vector [ 0 1 0 0 ] T \begin{bmatrix} 0 & 1 & 0 & 0 \end{bmatrix}^{T} [ 0 1 0 0 ] T . However, many multi-qubit data cannot be separated and expressed as the tensor product of smaller data; such inseparable data are called entangled , e.g. 1 2 ( ∣ 00 ⟩ + ∣ 11 ⟩ ) \frac{1}{\sqrt{2}}(\lvert00\rangle+ \lvert11\rangle) 2 1 (∣ 00 ⟩ + ∣ 11 ⟩) , known as a Bell pair , which can be rewritten to ∑ d = 0 1 1 2 ∣ d d ⟩ \sum_{d=0}^{1} \frac{1}{\sqrt{2}}\lvert dd\rangle ∑ d = 0 1 2 1 ∣ dd ⟩ , where d d dd dd is a bit string consisting of two bits, each of which must be the same datum (i.e., d = 0 d = 0 d = 0 or d = 1 d = 1 d = 1 ). Each term 1 2 ∣ d d ⟩ \frac{1}{\sqrt{2}}\lvert dd\rangle 2 1 ∣ dd ⟩ is named a basis-ket [22 ] , consisting of an amplitude (1 2 \frac{1}{\sqrt{2}} 2 1 ) and a basis vector ∣ d d ⟩ \lvert dd\rangle ∣ dd ⟩ .
Quantum Computation and Measurement. Computation on a quantum datum consists of a series of quantum operations , each acting on a subset of qubits in the quantum datum. In the standard form, quantum computations are expressed as circuits , as in Figure 2 , which depicts a circuit that prepares the Greenberger-Horne-Zeilinger (GHZ) state [10 ] — an n n n -qubit entangled datum of the form: ∣ G H Z n ⟩ = 1 2 ( ∣ 0 ⟩ ⊗ n + ∣ 1 ⟩ ⊗ n ) \lvert\mathrm{GHZ}^{n}\rangle= \frac{1}{\sqrt{2}}(\lvert0\rangle^{\otimes n} + \lvert1\rangle^{\otimes n}) ∣ GHZ n ⟩ = 2 1 (∣ 0 ⟩ ⊗ n + ∣ 1 ⟩ ⊗ n ) , where ∣ d ⟩ ⊗ n = ⨂ d = 0 n − 1 ∣ d ⟩ \lvert d\rangle^{\otimes n} = \bigotimes_{d=0}^{n-1}\lvert d\rangle ∣ d ⟩ ⊗ n = ⨂ d = 0 n − 1 ∣ d ⟩ . In these circuits, each horizontal wire represents a qubit, and boxes on these wires indicate quantum operations, or gates . The circuit in Figure 2 uses n n n qubits and applies n n n gates: a Hadamard (H) gate and n − 1 n - 1 n − 1 controlled-not (CNOT) gates. Applying a gate to a quantum datum evolves it. Its traditional semantics is expressed by multiplying the datum’s vector form by the gate’s corresponding matrix representation: n n n -qubit gates are 2 n 2^{n} 2 n -by-2 n 2^{n} 2 n matrices. Except for measurement gates, a gate’s matrix must be unitary and thus preserve appropriate invariants of quantum data’s amplitudes. A measurement operation extracts classical information from a quantum datum. It collapses the datum to a basis state with a probability related to the datum’s amplitudes (measurement probability ), e.g., measuring 1 2 ( ∣ 0 ⟩ + ∣ 1 ⟩ ) \frac{1}{\sqrt{2}}(\lvert0\rangle+ \lvert1\rangle) 2 1 (∣ 0 ⟩ + ∣ 1 ⟩) collapses the datum to ∣ 0 ⟩ \lvert0\rangle ∣ 0 ⟩ with probability 1 2 \frac{1}{2} 2 1 , and likewise for ∣ 1 ⟩ \lvert1\rangle ∣ 1 ⟩ , returning classical value 0 or 1, respectively. A more general form of quantum measurement is partial measurement , which measures a subset of qubits in a qubit array; such operations often have simultaneity effects due to entanglement, i.e., in a Bell pair 1 2 ( ∣ 00 ⟩ + ∣ 11 ⟩ ) \frac{1}{\sqrt{2}}(\lvert00\rangle+ \lvert11\rangle) 2 1 (∣ 00 ⟩ + ∣ 11 ⟩) , measuring one qubit guarantees the same outcome for the other — if the first bit is measured as 0, the second bit is too.
Figure 2. GHZ Circuit
High-level Operation Abstraction. Controlled quantum gates, such as controlled-not gates (CNOT), can be thought of as quantum versions of classical conditionals, called quantum conditionals, where we view a quantum datum as an array of basis-kets and apply an array map corresponding to the classical operation to each basis-ket. This is evident when using Dirac notation. For example, in preparing a two-qubit GHZ state (Figure 2 , also a Bell pair) for qubit array x x x , the H gate evolves the datum to 1 2 ∣ 00 ⟩ + 1 2 ∣ 10 ⟩ \frac{1}{\sqrt{2}}\lvert00\rangle+ \frac{1}{\sqrt{2}}\lvert10\rangle 2 1 ∣ 00 ⟩ + 2 1 ∣ 10 ⟩ (same as 1 2 ( ∣ 0 ⟩ + ∣ 1 ⟩ ) ⊗ ∣ 0 ⟩ \frac{1}{\sqrt{2}}(\lvert0\rangle+ \lvert1\rangle) \otimes\lvert0\rangle 2 1 (∣ 0 ⟩ + ∣ 1 ⟩) ⊗ ∣ 0 ⟩ ). The quantum conditional maps the classical conditional if ( x [ 0 ] ) { x [ 1 ] ← x [ 1 ] + 1 } (x[0])\ \{x[1] \leftarrow x[1] + 1\} ( x [ 0 ]) { x [ 1 ] ← x [ 1 ] + 1 } onto the two basis-kets, where the operation x [ 1 ] + 1 x[1] + 1 x [ 1 ] + 1 acts as a modulo 2 addition to flip x [ 1 ] x[1] x [ 1 ] ’s bit. Here, we do not flip the x [ 1 ] x[1] x [ 1 ] position in the first basis-ket ( 1 2 ∣ 00 ⟩ ) (\frac{1}{\sqrt{2}}\lvert00\rangle) ( 2 1 ∣ 00 ⟩) due to x [ 0 ] = 0 x[0] = 0 x [ 0 ] = 0 , and we flip x [ 1 ] x[1] x [ 1 ] in the second basis-ket because of x [ 0 ] = 1 x[0] = 1 x [ 0 ] = 1 .
Another abstraction is quantum oracle operations. Quantum algorithms manipulate input information encoded in “oracles,” which are callable black-box circuits. Quantum oracles are usually quantum-reversible implementations of classical operations, especially arithmetic operations . The behavior of a quantum arithmetic operation resembles a high-order array map operation in Figure 3 line (7) over a single arithmetic operation applied to every basis-ket of a quantum datum. oqasm [16 ] is a language that permits the definitions of quantum oracles.
Figure 3. Running example for Deutsch–Jozsa algorithm when f f f is constant; circuit on the right; the grayed out part is the Qafny state types; ∣ 0 ⟩ |0\rangle ∣0 ⟩ is an abbreviation of ⨂ n ∣ 0 ⟩ \bigotimes^n |0\rangle ⨂ n ∣0 ⟩ ; line 12 has several simplification and r r r is actually equal to 1 after simplification; y [ 0 ] y[0] y [ 0 ] state in lines 6 to 12 are separable from x [ 0 , n ) x[0,n) x [ 0 , n ) , as ∑ b = 0 1 1 2 ( − 1 ) b ∣ b ⟩ \sum_{b=0}^{1}\frac{1}{\sqrt{2}}(-1)^b|b\rangle ∑ b = 0 1 2 1 ( − 1 ) b ∣ b ⟩ .
Hoare Logic with Array Theory. Hoare logic (also known as Floyd–Hoare logic or Hoare rules) [9 , 12 ] is a formal system with a set of logical rules for reasoning rigorously about the correctness of computer programs. The central feature of Hoare logic is the Hoare triple. A triple describes how the execution of a piece of code changes the state of the computation. A Hoare triple is of the form { P } C { Q } \{P\}\ C\ \{Q\} { P } C { Q } where P P P and Q Q Q are assertions and C C C is a C-like program. Here, P P P is named the pre-condition and Q Q Q the post-condition. When the pre-condition is met, executing the command establishes the post-condition. Assertions are formulae in predicate logic.
{ P [ f ( A ) / A 1 ] } A 1 : = f ( A ) { P } { P [ A { E 1 ← E 2 } / A ] } A [ E 1 ] : = E 2 { P } \{P[f(A)/A_1]\}\ A_1 := f(A)\ \{P\}
\qquad
\{P[A\{E_1 \leftarrow E_2\}/A]\}\ A[E_1] := E_2\ \{P\} { P [ f ( A ) / A 1 ]} A 1 := f ( A ) { P } { P [ A { E 1 ← E 2 } / A ]} A [ E 1 ] := E 2 { P }
The left-hand side shows the key assignment rule in Hoare logic, which establishes the relation between the post-condition P P P and the pre-condition P [ f ( A ) / A 1 ] P[f(A)/A_1] P [ f ( A ) / A 1 ] for the assignment operation A 1 : = f ( A ) A_1 := f(A) A 1 := f ( A ) . The variables A A A and A 1 A_1 A 1 are not restricted to any particular data structure, as they certainly can represent array variables. To verify array assignments, we can define the operation A [ E 1 ] : = E 2 A[E_1] := E_2 A [ E 1 ] := E 2 , i.e., the index E 1 E_1 E 1 of the array A A A is assigned to E 2 E_2 E 2 . As the proof rule on the right-hand above, starting from the post-condition P P P , the pre-condition P [ A { E 1 ← E 2 } / A ] P[A\{E_1 \leftarrow E_2\}/A] P [ A { E 1 ← E 2 } / A ] is a replacement of every array variable A A A with the updated array A { E 1 ← E 2 } A\{E_1 \leftarrow E_2\} A { E 1 ← E 2 } , i.e., we assume the ← \leftarrow ← operation updates the E 1 E_1 E 1 position with E 2 E_2 E 2 in A A A .
We assume some useful predicates and conventions to capture the properties of array indexing. First, we use R ‾ \overline{R} R to refer to an array of elements having type R R R , range ( m ‾ ) (\overline{m}) ( m ) refers to a list of ranges [ 0 , m 0 ) , … , [ 0 , m n − 1 ) [0,m_0),\ldots,[0,m_{n-1}) [ 0 , m 0 ) , … , [ 0 , m n − 1 ) for some n n n , and k ‾ ∈ range ( m ‾ ) \overline{k} \in\operatorname{range}(\overline{m}) k ∈ range ( m ) means k 0 ∈ [ 0 , m 0 ) , … , k n − 1 ∈ [ 0 , m n − 1 ) k_0 \in[0,m_0),\ldots,k_{n-1} \in[0,m_{n-1}) k 0 ∈ [ 0 , m 0 ) , … , k n − 1 ∈ [ 0 , m n − 1 ) . Second, the predicate Q l e n ( A , m ‾ ) Q_{\mathrm{len}}(A,\overline{m}) Q len ( A , m ) states that A A A , a multi-dimensional array, has all lengths in different dimensions as m ‾ \overline{m} m , and we refer Q l e n ( A 0 , … , A n , m ‾ ) Q_{\mathrm{len}}(A_0,\ldots,A_n,\overline{m}) Q len ( A 0 , … , A n , m ) to that the arrays A 0 , … , A n A_0,\ldots,A_n A 0 , … , A n all have lengths m ‾ \overline{m} m . Third, we refer m ‾ ⊑ m ′ ‾ \overline{m} \sqsubseteq\overline{m'} m ⊑ m ′ to that m ‾ \overline{m} m is a prefix of m ′ ‾ \overline{m'} m ′ , i.e., if m ‾ \overline{m} m has n n n terms and m ′ ‾ \overline{m'} m ′ has n + k n+k n + k terms, then m ′ ‾ = m 0 , … , m n − 1 , m n ′ , … , m n + k − 1 ′ \overline{m'} = m_0,\ldots,m_{n-1},m'_n,\ldots,m'_{n+k-1} m ′ = m 0 , … , m n − 1 , m n ′ , … , m n + k − 1 ′ .
The following lemma links a pointwise specification of an array-producing statement to the assignment rule: once a statement is known to fill A ′ A' A ′ with f ( A [ k ‾ ] ) f(A[\overline{k}]) f ( A [ k ]) at every index, any post-condition P P P can be pushed backwards through it by substituting the pointwise-updated array for A ′ A' A ′ . A ′ A' A ′ , exactly as the assignment rule does for a scalar. It is the step that turns each HLIR primitive’s loop into a single substitution in § 7 .
Lemma 2.1 (Pointwise Update). Given Q ≜ Q l e n ( A , m ‾ ) ∧ Q l e n ( A ′ , m ‾ ′ ) Q \triangleq Q_{\mathrm{len}}(A,\overline{m}) \land Q_{\mathrm{len}}(A',\overline{m}') Q ≜ Q len ( A , m ) ∧ Q len ( A ′ , m ′ ) , m ‾ ⊑ m ‾ ′ \overline{m} \sqsubseteq\overline{m}' m ⊑ m ′ , and
{ Q } A ′ : = φ ( A ) { ∀ k ‾ ∈ range ( m ‾ ) . A ′ [ k ‾ ] = f ( A [ k ‾ ] ) } , \{Q\}\ A' := \varphi(A)\ \{\forall\overline{k} \in\operatorname{range}(\overline{m}). A'[\overline{k}] = f(A[\overline{k}])\}, { Q } A ′ := φ ( A ) { ∀ k ∈ range ( m ) . A ′ [ k ] = f ( A [ k ])} , we have
{ P [ A ′ { ∀ k ‾ ∈ range ( m ‾ ) . k ‾ ↦ f ( A [ k ‾ ] ) } / A ′ ] ∧ Q } A ′ : = φ ( A ) { P ∧ Q } . \{P[A'\{\forall\overline{k} \in\operatorname{range}(\overline{m}).\overline{k} \mapsto f(A[\overline{k}])\}/A'] \land Q\}\ A' := \varphi(A)\ \{P \land Q\}. { P [ A ′ { ∀ k ∈ range ( m ) . k ↦ f ( A [ k ])} / A ′ ] ∧ Q } A ′ := φ ( A ) { P ∧ Q } .
Proof. By using the Hoare logic assignment rule, we have { P [ f ( A ) / A ′ ] } A ′ : = φ ( A ) { P } \{P[f(A)/A']\}\ A' := \varphi(A)\ \{P\} { P [ f ( A ) / A ′ ]} A ′ := φ ( A ) { P } . With the assumption in the corollary, we result in the following post-condition,
P [ A ′ { ∀ k ‾ ∈ range ( m ‾ ) . k ‾ ↦ f ( A [ k ‾ ] ) } / A ′ ] ∧ ∀ k ‾ ∈ range ( m ‾ ) . A ′ [ k ‾ ] = f ( A [ k ‾ ] ) P[A'\{\forall\overline{k} \in\operatorname{range}(\overline{m}).\overline{k} \mapsto f(A[\overline{k}])\}/A'] \land\forall\overline{k} \in\operatorname{range}(\overline{m}). A'[\overline{k}] = f(A[\overline{k}]) P [ A ′ { ∀ k ∈ range ( m ) . k ↦ f ( A [ k ])} / A ′ ] ∧ ∀ k ∈ range ( m ) . A ′ [ k ] = f ( A [ k ])
, which is equivalent to P P P . □
The Q l e n Q_{\mathrm{len}} Q len predicate is widely used in various proofs in the paper, so we will sometimes assume it without explicit mention.
Overview
Here, we provide an overview of embedding quantum program proofs in Hoare logic. We utilize a running example based on the Deutsch–Jozsa algorithm in Figure 3 : given a function f f f that is either constant or balanced, represented as an oracle f f f in the circuit, the algorithm determines whether f f f is constant by outputting v = 0 v = 0 v = 0 or another value. In the algorithm, we first use a length n n n qubit array x x x and an extra qubit y y y , where x [ 0 , n ) x[0,n) x [ 0 , n ) and y [ 0 ] y[0] y [ 0 ] is in a uniform superposition by applying n + 1 n + 1 n + 1 Hadamard gates in lines 2 and 5. The superposition state spans all possible values for the two-qubit arrays.
We then apply the oracle operation f f f in line 7 to the locus x [ 0 , n ) ∥ y [ 0 ] x[0,n) \mathbin{\|} y[0] x [ 0 , n ) ∥ y [ 0 ] , i.e., the list of qubit ranges, possibly entangled, that the operation acts on (loci are defined in § 4.2 ), and n n n Hadamard gates to x [ 0 , n ) x[0,n) x [ 0 , n ) in line 9. The oracle operation queries the function f f f and stores the result in the y [ 0 ] y[0] y [ 0 ] qubit. The n n n H gates associate the query results with the amplitudes, determining the probability of each basis-ket in the quantum state. Here, the state ∣ 0 ⟩ \lvert0\rangle ∣ 0 ⟩ yields an amplitude 1 if f f f is a constant function, and the measurement operation in line 11 outputs v = 0 v = 0 v = 0 with probability r = 1 r = 1 r = 1 .
Line 1 specifies the pre-condition: x x x starts in the ∣ 0 ‾ ⟩ \lvert\overline{0}\rangle ∣ 0 ⟩ state and y [ 0 ] y[0] y [ 0 ] in the ∣ 1 ⟩ \lvert1\rangle ∣ 1 ⟩ state. Line 12 specifies the post-condition to be verified: that the measurement result v = 0 v = 0 v = 0 correctly indicates that the function f f f is constant, with output probability r = 1 r = 1 r = 1 . The other assertions are the step-wise post-conditions. The EN ( n ) \operatorname{EN}(n) EN ( n ) type information is specific to Qafny and is introduced in the next section.
Relating Quantum Operations with Classical Ones in a Proof Triple. Here, we provide an overview of viewing a quantum operation as a behavioral equivalence sequence of classical operations. The transformation does not introduce computational benefits, but these classical operations are well-known structures that can be effectively verified; thus, the transformation could provide benefits in verifying quantum programs.
We now highlight the key strategy by showing the transformation of the proof step for Figure 3 lines 6–8. We list the quantum triple with conditions in Qafny as an example. Such a triple captures the key features in many other quantum proof systems [8 , 17 ] , where we discuss one of them next.
{ x [ 0 , n ) ⊞ y [ 0 ] : EN ( 2 ) ↦ ∑ j = 0 2 n − 1 ∑ b = 0 1 1 2 n + 1 ( − 1 ) b ∣ j ⟩ ∣ b ⟩ } \{x[0,n) \mathbin{\boxplus} y[0]:\operatorname{EN}(2)\mapsto\sum_{j=0}^{2^n-1}\sum_{b=0}^{1}\frac{1}{\sqrt{2^{n+1}}}(-1)^b|j\rangle|b\rangle\} { x [ 0 , n ) ⊞ y [ 0 ] : EN ( 2 ) ↦ j = 0 ∑ 2 n − 1 b = 0 ∑ 1 2 n + 1 1 ( − 1 ) b ∣ j ⟩ ∣ b ⟩}
x [ 0 , n ) ⊞ y [ 0 ] ← λ ∣ u ⟩ ∣ v ⟩ . ∣ u ⟩ ∣ v ⊕ f ( u ) ⟩ ; x[0,n)\mathbin{\boxplus} y[0]\leftarrow\lambda|u\rangle|v\rangle.\ |u\rangle|v\oplus f(u)\rangle; x [ 0 , n ) ⊞ y [ 0 ] ← λ ∣ u ⟩ ∣ v ⟩ . ∣ u ⟩ ∣ v ⊕ f ( u )⟩ ;
{ x [ 0 , n ) ⊞ y [ 0 ] : EN ( 2 ) ↦ ∑ j = 0 2 n − 1 ∑ b = 0 1 1 2 n + 1 ( − 1 ) b ∣ j ⟩ ∣ b ⊕ f ( j ) ⟩ } \{x[0,n)\mathbin{\boxplus} y[0]:\operatorname{EN}(2)\mapsto\sum_{j=0}^{2^n-1}\sum_{b=0}^{1}\frac{1}{\sqrt{2^{n+1}}}(-1)^b|j\rangle|b\oplus f(j)\rangle\} { x [ 0 , n ) ⊞ y [ 0 ] : EN ( 2 ) ↦ j = 0 ∑ 2 n − 1 b = 0 ∑ 1 2 n + 1 1 ( − 1 ) b ∣ j ⟩ ∣ b ⊕ f ( j )⟩}
In handling the oracle operation λ ∣ u ⟩ ∣ v ⟩ . ∣ u ⟩ ∣ v ⊕ f ( u ) ⟩ \lambda|u\rangle|v\rangle.\ |u\rangle|v\oplus f(u)\rangle λ ∣ u ⟩ ∣ v ⟩ . ∣ u ⟩ ∣ v ⊕ f ( u )⟩ , we pattern-match it with the ket structure ( ∣ j ⟩ ∣ b ⟩ ) (|j\rangle|b\rangle) ( ∣ j ⟩ ∣ b ⟩) of each basis-ket in the pre-condition state, and infer that the ket structure of each basis-ket in the post-condition state should be ∣ j ⟩ ∣ b ⊕ f ( j ) ⟩ |j\rangle|b\oplus f(j)\rangle ∣ j ⟩ ∣ b ⊕ f ( j )⟩ . In relating the quantum state predicates to a classical Hoare logic format, we think of a linear sum operator as a single dimension array concatenation; thus, the nested sum operator ( ∑ j = 0 2 n − 1 ∑ b = 0 1 1 2 n + 1 ( − 1 ) b ∣ j ⟩ ∣ b ⟩ ) (\sum_{j=0}^{2^n-1}\sum_{b=0}^{1}\frac{1}{\sqrt{2^{n+1}}}(-1)^b|j\rangle|b\rangle) ( ∑ j = 0 2 n − 1 ∑ b = 0 1 2 n + 1 1 ( − 1 ) b ∣ j ⟩ ∣ b ⟩) introduces a two dimensional array where each element property can be described by the expression 1 2 n + 1 ( − 1 ) b ∣ j ⟩ ∣ b ⟩ \frac{1}{\sqrt{2^{n+1}}}(-1)^b|j\rangle|b\rangle 2 n + 1 1 ( − 1 ) b ∣ j ⟩ ∣ b ⟩ .
In our system, we can think of the number (n n n ) of sum operators in a quantum state predicate as being transformed into an n n n -dimensional array capturing the predicate relation (this is the essence of the EN ( n ) \operatorname{EN}(n) EN ( n ) type). When n = 1 n=1 n = 1 , conceptually, we can think of the state as the array below.
[ [ 1 2 ( − 1 ) 0 ∣ 0 ⟩ ∣ 0 ⟩ , 1 2 ( − 1 ) 1 ∣ 0 ⟩ ∣ 1 ⟩ ] , [ 1 2 ( − 1 ) 0 ∣ 1 ⟩ ∣ 0 ⟩ , 1 2 ( − 1 ) 1 ∣ 1 ⟩ ∣ 1 ⟩ ] ] (1) \left[\left[\frac{1}{2}(-1)^0|0\rangle|0\rangle,\frac{1}{2}(-1)^1|0\rangle|1\rangle\right],\left[\frac{1}{2}(-1)^0|1\rangle|0\rangle,\frac{1}{2}(-1)^1|1\rangle|1\rangle\right]\right] \tag*{(1)} [ [ 2 1 ( − 1 ) 0 ∣0 ⟩ ∣0 ⟩ , 2 1 ( − 1 ) 1 ∣0 ⟩ ∣1 ⟩ ] , [ 2 1 ( − 1 ) 0 ∣1 ⟩ ∣0 ⟩ , 2 1 ( − 1 ) 1 ∣1 ⟩ ∣1 ⟩ ] ] (1)
Note that the locus for the above state is x [ 0 , n ) ⊞ y [ 0 ] x[0,n)\mathbin{\boxplus}y[0] x [ 0 , n ) ⊞ y [ 0 ] , meaning that the two ket structures, ∣ j ⟩ |j\rangle ∣ j ⟩ and ∣ b ⟩ |b\rangle ∣ b ⟩ , have different lengths if we think of j j j and b b b in their bitstring representations. The above simple transformation can be summarized by viewing a quantum state as a predicate over an array of the same dimension as the number of linear-sum terms. Although the state representation might be the right choice, such a simple transformation might not work for many cases. In the example above, if we bit-flip x [ i ] x[i] x [ i ] (i ∈ [ 0 , n ) i\in[0,n) i ∈ [ 0 , n ) ), using x [ i ] ← λ ∣ u ⟩ . ∣ ( u + 1 ) % 2 ⟩ x[i]\leftarrow\lambda|u\rangle.\ |(u+1)\%2\rangle x [ i ] ← λ ∣ u ⟩ . ∣ ( u + 1 ) %2 ⟩ , we need to develop a way to locate the specific bit in the right ket structure. Additionally, one cannot assume the qubit order in a ket structure is always well-ordered; e.g., the range x [ 0 , n ) x[0,n) x [ 0 , n ) points to the ket ∣ j ⟩ |j\rangle ∣ j ⟩ above, but we cannot assume that x [ 0 ] x[0] x [ 0 ] must point to the first bit in ∣ j ⟩ |j\rangle ∣ j ⟩ 's bitstring and x [ n − 1 ] x[n-1] x [ n − 1 ] points to the last, i.e., the order depends on the layout of the array.
We compile the above operation and its proof to a sequence of classical operations, easily verifiable in Hoare logic, and develop a general procedure to verify the operation via the proof for the sequence. Assume that a set of loci partitions all quantum qubits in φ \varphi φ into separable regions, and the qubits in the EN(2) typed locus x [ 0 , n ) ∥ y [ 0 ] ∥ η x[0,n) \mathbin{\|} y[0] \mathbin{\|} \eta x [ 0 , n ) ∥ y [ 0 ] ∥ η are stored in a two-dimensional array A A A . The operation ind φ ( x [ 0 , n ) ∥ y [ 0 ] ∥ η ) \operatorname{ind}^{\varphi}(x[0,n) \mathbin{\|} y[0] \mathbin{\|} \eta) ind φ ( x [ 0 , n ) ∥ y [ 0 ] ∥ η ) produces a set of indices in A A A for the qubits in x [ 0 , n ) ∥ y [ 0 ] ∥ η x[0,n) \mathbin{\|} y[0] \mathbin{\|} \eta x [ 0 , n ) ∥ y [ 0 ] ∥ η with the state being φ \varphi φ . The array A A A can have arbitrary dimensions, i.e., an EN(k k k ) typed state is compiled to k k k -dimensional. Each element θ \theta θ in A A A has the following record type.
θ : : = element { amp = z , vector = c } A : : = [ … [ θ ] n − 1 … ] 0 \theta::= \operatorname{element}\{\operatorname{amp}=z,\ \operatorname{vector}=c\}
\qquad
A ::= [\ldots[\theta]_{n-1}\ldots]_{0} θ ::= element { amp = z , vector = c } A ::= [ … [ θ ] n − 1 … ] 0
Assuming that A A A is two-dimensional with two-level lengths m 0 m_0 m 0 and m 1 m_1 m 1 , we identify four consecutive operations to construct the Hoare logic proof for the oracle operation above, as follows.
A 1 : = locateN ( A , ( m 0 , m 1 ) , ind φ ( x [ 0 , n ) ∥ y [ 0 ] ) ) ; A 2 : = mapN ( A 1 , ( m 0 , m 1 ) , λ ( t , ( u , v ) ) . ( t , ( u , v ⊕ f ( u ) ) ) ) ; A 3 : = assignN ( A , A 2 , ( m 0 , m 1 ) , ind φ ( x [ 0 , n ) ∥ y [ 0 ] ) , λ u v . amp ( v ) ) ; A : = A 3 \begin{aligned}
A_1 &:= \operatorname{locateN}(A,(m_0,m_1),\operatorname{ind}^{\varphi}(x[0,n) \mathbin{\|} y[0]));\\
A_2 &:= \operatorname{mapN}(A_1,(m_0,m_1),\lambda(t,(u,v)).(t,(u,v\mathbin{\oplus}f(u))));\\
A_3 &:= \operatorname{assignN}(A,A_2,(m_0,m_1),\operatorname{ind}^{\varphi}(x[0,n) \mathbin{\|} y[0]),\lambda u\,v.\operatorname{amp}(v));\\
A &:= A_3
\end{aligned} A 1 A 2 A 3 A := locateN ( A , ( m 0 , m 1 ) , ind φ ( x [ 0 , n ) ∥ y [ 0 ])) ; := mapN ( A 1 , ( m 0 , m 1 ) , λ ( t , ( u , v )) . ( t , ( u , v ⊕ f ( u )))) ; := assignN ( A , A 2 , ( m 0 , m 1 ) , ind φ ( x [ 0 , n ) ∥ y [ 0 ]) , λ u v . amp ( v )) ; := A 3
Below are example transitions that apply the above operations to the state in Equation (1) .
locateN , A 1 ⟶ [ [ 1 2 ( − 1 ) 0 ∣ 0 ⟩ ∣ 0 ⟩ , 1 2 ( − 1 ) 1 ∣ 0 ⟩ ∣ 1 ⟩ ] , [ 1 2 ( − 1 ) 0 ∣ 1 ⟩ ∣ 0 ⟩ , 1 2 ( − 1 ) 1 ∣ 1 ⟩ ∣ 1 ⟩ ] ] mapN , A 2 ⟶ [ [ 1 2 ( − 1 ) 0 ∣ 0 ⟩ ∣ 1 ⟩ , 1 2 ( − 1 ) 1 ∣ 0 ⟩ ∣ 0 ⟩ ] , [ 1 2 ( − 1 ) 0 ∣ 1 ⟩ ∣ 1 ⟩ , 1 2 ( − 1 ) 1 ∣ 1 ⟩ ∣ 0 ⟩ ] ] mapN , A 3 ⟶ [ [ 1 2 ( − 1 ) 0 ∣ 0 ⟩ ∣ 1 ⟩ , 1 2 ( − 1 ) 1 ∣ 0 ⟩ ∣ 0 ⟩ ] , [ 1 2 ( − 1 ) 0 ∣ 1 ⟩ ∣ 1 ⟩ , 1 2 ( − 1 ) 1 ∣ 1 ⟩ ∣ 0 ⟩ ] ] A : = A 3 ⟶ [ [ 1 2 ( − 1 ) 0 ∣ 0 ⟩ ∣ 1 ⟩ , 1 2 ( − 1 ) 1 ∣ 0 ⟩ ∣ 0 ⟩ ] , [ 1 2 ( − 1 ) 0 ∣ 1 ⟩ ∣ 1 ⟩ , 1 2 ( − 1 ) 1 ∣ 1 ⟩ ∣ 0 ⟩ ] ] \begin{aligned}
\operatorname{locateN},A_1 &\longrightarrow\left[\left[\tfrac{1}{2}(-1)^0\lvert0\rangle\lvert0\rangle,\tfrac{1}{2}(-1)^1\lvert0\rangle\lvert1\rangle\right],\left[\tfrac{1}{2}(-1)^0\lvert1\rangle\lvert0\rangle,\tfrac{1}{2}(-1)^1\lvert1\rangle\lvert1\rangle\right]\right]\\
\operatorname{mapN},A_2 &\longrightarrow\left[\left[\tfrac{1}{2}(-1)^0\lvert0\rangle\lvert1\rangle,\tfrac{1}{2}(-1)^1\lvert0\rangle\lvert0\rangle\right],\left[\tfrac{1}{2}(-1)^0\lvert1\rangle\lvert1\rangle,\tfrac{1}{2}(-1)^1\lvert1\rangle\lvert0\rangle\right]\right]\\
\operatorname{mapN},A_3 &\longrightarrow\left[\left[\tfrac{1}{2}(-1)^0\lvert0\rangle\lvert1\rangle,\tfrac{1}{2}(-1)^1\lvert0\rangle\lvert0\rangle\right],\left[\tfrac{1}{2}(-1)^0\lvert1\rangle\lvert1\rangle,\tfrac{1}{2}(-1)^1\lvert1\rangle\lvert0\rangle\right]\right]\\
A:=A_3 &\longrightarrow\left[\left[\tfrac{1}{2}(-1)^0\lvert0\rangle\lvert1\rangle,\tfrac{1}{2}(-1)^1\lvert0\rangle\lvert0\rangle\right],\left[\tfrac{1}{2}(-1)^0\lvert1\rangle\lvert1\rangle,\tfrac{1}{2}(-1)^1\lvert1\rangle\lvert0\rangle\right]\right]
\end{aligned} locateN , A 1 mapN , A 2 mapN , A 3 A := A 3 ⟶ [ [ 2 1 ( − 1 ) 0 ∣ 0 ⟩ ∣ 0 ⟩ , 2 1 ( − 1 ) 1 ∣ 0 ⟩ ∣ 1 ⟩ ] , [ 2 1 ( − 1 ) 0 ∣ 1 ⟩ ∣ 0 ⟩ , 2 1 ( − 1 ) 1 ∣ 1 ⟩ ∣ 1 ⟩ ] ] ⟶ [ [ 2 1 ( − 1 ) 0 ∣ 0 ⟩ ∣ 1 ⟩ , 2 1 ( − 1 ) 1 ∣ 0 ⟩ ∣ 0 ⟩ ] , [ 2 1 ( − 1 ) 0 ∣ 1 ⟩ ∣ 1 ⟩ , 2 1 ( − 1 ) 1 ∣ 1 ⟩ ∣ 0 ⟩ ] ] ⟶ [ [ 2 1 ( − 1 ) 0 ∣ 0 ⟩ ∣ 1 ⟩ , 2 1 ( − 1 ) 1 ∣ 0 ⟩ ∣ 0 ⟩ ] , [ 2 1 ( − 1 ) 0 ∣ 1 ⟩ ∣ 1 ⟩ , 2 1 ( − 1 ) 1 ∣ 1 ⟩ ∣ 0 ⟩ ] ] ⟶ [ [ 2 1 ( − 1 ) 0 ∣ 0 ⟩ ∣ 1 ⟩ , 2 1 ( − 1 ) 1 ∣ 0 ⟩ ∣ 0 ⟩ ] , [ 2 1 ( − 1 ) 0 ∣ 1 ⟩ ∣ 1 ⟩ , 2 1 ( − 1 ) 1 ∣ 1 ⟩ ∣ 0 ⟩ ] ]
locateN ( A , ( m 0 , m 1 ) , ind φ ( x [ 0 , n ) ∥ y [ 0 ] ) ) \operatorname{locateN}(A,(m_0,m_1),\operatorname{ind}^{\varphi}(x[0,n) \mathbin{\|} y[0])) locateN ( A , ( m 0 , m 1 ) , ind φ ( x [ 0 , n ) ∥ y [ 0 ])) shrinks high-dimensional element vectors to focus on the indices (e.g., j j j ) representing the locus fragment x [ 0 , n ) ∥ y [ 0 ] x[0,n) \mathbin{\|} y[0] x [ 0 , n ) ∥ y [ 0 ] in A A A , as we assign the result to a new array A 1 A_1 A 1 (same dimension as A A A ), as ∀ j . A 1 [ j ] = vector ( A [ j ] ) [ ind φ ( x [ 0 , n ) ∥ y [ 0 ] ) ] \forall j.A_1[j]=\operatorname{vector}(A[j])[\operatorname{ind}^{\varphi}(x[0,n) \mathbin{\|} y[0])] ∀ j . A 1 [ j ] = vector ( A [ j ]) [ ind φ ( x [ 0 , n ) ∥ y [ 0 ])] . The vector operation accesses the vector A [ j ] A[j] A [ j ] but restricts it to the indices of the locus x [ 0 , n ) ∥ y [ 0 ] x[0,n) \mathbin{\|} y[0] x [ 0 , n ) ∥ y [ 0 ] . For the state in Equation (1) , A 1 A_1 A 1 and A A A are identical.
The mapN \operatorname{mapN} mapN operation applies the function ( λ ( t , ( u , v ) ) . ( t , ( u , v ⊕ f ( u ) ) ) ) (\lambda(t,(u,v)).(t,(u,v\mathbin{\oplus}f(u)))) ( λ ( t , ( u , v )) . ( t , ( u , v ⊕ f ( u )))) to each element in A 1 A_1 A 1 and assigns the result to a new array A 2 A_2 A 2 , where t t t and ( u , v ) (u,v) ( u , v ) refer to the element’s amplitude and vector, respectively. In Figure 3 , f f f flips the result bit because it is constant, as the result after
mapN , A 2 ⟶ \operatorname{mapN},A_2 \longrightarrow mapN , A 2 ⟶
assignN \operatorname{assignN} assignN merges the vector in A 2 A_2 A 2 back to A A A , i.e., each vector element (vector ( A 3 [ j ] ) \operatorname{vector}(A_3[j]) vector ( A 3 [ j ]) ) is the concatenation of vector ( A 2 [ j ] ) [ ind φ ( x [ 0 , n ) ∥ y [ 0 ] ) ] \operatorname{vector}(A_2[j])[\operatorname{ind}^{\varphi}(x[0,n) \mathbin{\|} y[0])] vector ( A 2 [ j ]) [ ind φ ( x [ 0 , n ) ∥ y [ 0 ])] and vector ( A [ j ] ) [ ind φ ( η ) ] \operatorname{vector}(A[j])[\operatorname{ind}^{\varphi}(\eta)] vector ( A [ j ]) [ ind φ ( η )] , where η \eta η is the locus fragment outside of x [ 0 , n ) ∥ y [ 0 ] x[0,n) \mathbin{\|} y[0] x [ 0 , n ) ∥ y [ 0 ] . The function ( λ u v . amp ( v ) ) (\lambda u\,v.\operatorname{amp}(v)) ( λ u v . amp ( v )) determines how we merge the amplitudes of the two arrays; we use A 2 A_2 A 2 ’s amplitudes here. Finally, we assign A 3 A_3 A 3 back to A A A .
The transformation is generalizable to other quantum proof systems. In Quantum Hoare Logic (QHL) [8 ] , a proof triple can be written as { P , q 1 } ϵ { Q , q 2 } \{P,q_1\}\ \epsilon\ \{Q,q_2\} { P , q 1 } ϵ { Q , q 2 } , where P P P and Q Q Q are pre- and post-conditions for classical variables and q 1 q_1 q 1 and q 2 q_2 q 2 are quantum state predicates. For example, the above proof triple for the oracle operation can be expressed as follows.
{ true , ∑ j = 0 2 n − 1 ∑ b = 0 1 1 2 n + 1 ( − 1 ) b ∣ j ⟩ x [ 0 , n ) ∣ b ⟩ y [ 0 ] } x [ 0 , n ) ∥ y [ 0 ] ← λ ∣ u ⟩ ∣ v ⟩ . ∣ u ⟩ ∣ v ⊕ f ( u ) ⟩ ; { true , ∑ j = 0 2 n − 1 ∑ b = 0 1 1 2 n + 1 ( − 1 ) b ∣ j ⟩ x [ 0 , n ) ∣ b ⊕ f ( j ) ⟩ y [ 0 ] } \begin{aligned}
\{&\operatorname{true},\sum_{j=0}^{2^n-1}\sum_{b=0}^{1}\frac{1}{\sqrt{2^{n+1}}}(-1)^b\lvert j\rangle_{x[0,n)}\lvert b\rangle_{y[0]}\}\\
&x[0,n)\mathbin{\|}y[0]\leftarrow\lambda\lvert u\rangle\lvert v\rangle.\lvert u\rangle\lvert v\mathbin{\oplus}f(u)\rangle;\\
\{&\operatorname{true},\sum_{j=0}^{2^n-1}\sum_{b=0}^{1}\frac{1}{\sqrt{2^{n+1}}}(-1)^b\lvert j\rangle_{x[0,n)}\lvert b\mathbin{\oplus}f(j)\rangle_{y[0]}\}
\end{aligned} { { true , j = 0 ∑ 2 n − 1 b = 0 ∑ 1 2 n + 1 1 ( − 1 ) b ∣ j ⟩ x [ 0 , n ) ∣ b ⟩ y [ 0 ] } x [ 0 , n ) ∥ y [ 0 ] ← λ ∣ u ⟩ ∣ v ⟩ . ∣ u ⟩ ∣ v ⊕ f ( u )⟩ ; true , j = 0 ∑ 2 n − 1 b = 0 ∑ 1 2 n + 1 1 ( − 1 ) b ∣ j ⟩ x [ 0 , n ) ∣ b ⊕ f ( j ) ⟩ y [ 0 ] }
Here, there are no conditions for classical variables. In the quantum state predicates, we encode each range in the locus information in the corresponding basis-vector, e.g., ∣ j ⟩ x [ 0 , n ) ∣ b ⟩ y [ 0 ] \lvert j\rangle_{x[0,n)}\lvert b\rangle_{y[0]} ∣ j ⟩ x [ 0 , n ) ∣ b ⟩ y [ 0 ] .1 Essentially, transforming the above triple is similar to the Qafny one, in which we still transform a state into a high-dimensional array. We illustrate the QHL case by examples here and in § 5 ; a detailed discussion is in Section B and the full QHL translation is left to future work.
The main theorems. We first present the paper’s main result: quantum program verification can be effectively carried out within the classical Hoare logic proof system. We use Qafny as the example proof system for the main theorems in the paper. Given a Qafny proof triple, σ ⊢ { φ } μ { φ ′ } \sigma\vdash\{\varphi\}\ \mu\ \{\varphi'\} σ ⊢ { φ } μ { φ ′ } , there are three main theorems in the paper.
Proposition 3.1 (Translation Completeness). Translating a derivable Qafny triple σ ⊢ { φ } μ { φ ′ } \sigma\vdash\{\varphi\}\ \mu\ \{\varphi'\} σ ⊢ { φ } μ { φ ′ } results in a valid Hoare logic triple { P } e { Q } \{P\}\ e\ \{Q\} { P } e { Q } .
Proposition 3.2 (Translation Soundness). Let a well-typed Qafny program μ \mu μ and well-typed conditions φ \varphi φ and φ ′ \varphi' φ ′ translate to e e e , P P P , and Q Q Q . If the Hoare triple { P } e { Q } \{P\}\ e\ \{Q\} { P } e { Q } is valid, then Qafny derives a triple σ ⊢ { φ 1 } μ { φ 1 ′ } \sigma\vdash\{\varphi_1\}\ \mu\ \{\varphi'_1\} σ ⊢ { φ 1 } μ { φ 1 ′ } with ⟦ φ ⟧ ≡ ⟦ φ 1 ⟧ \llbracket\varphi\rrbracket\equiv\llbracket\varphi_1\rrbracket [ [ φ ] ] ≡ [ [ φ 1 ] ] and ⟦ φ ′ ⟧ ≡ ⟦ φ 1 ′ ⟧ \llbracket\varphi'\rrbracket\equiv\llbracket\varphi'_1\rrbracket [ [ φ ′ ] ] ≡ [ [ φ 1 ′ ] ] ; by the semantic soundness of Qafny, μ \mu μ satisfies the specification ( φ , φ ′ ) (\varphi,\varphi') ( φ , φ ′ ) .
Proposition 3.3 (Translation Size). The translation produces at most eleven HLIR statements per Qafny statement, and the translated assertions and the Hoare proof skeleton for each statement have size polynomial in the size of the Qafny triple and the array dimension, measured in the symbolic array representation of § 7 .
Throughout, the ground truth is the quantum semantics. A Qafny triple is derived by the Qafny proof system, which is sound with respect to that semantics [17 ] ; the classical side never sees quantum states, only the translated program e e e and assertions P P P and Q Q Q . Our two theorems relate derivability in Qafny to validity of the translated Hoare triple in both directions, and we name them from the perspective of Hoare logic as the verifier. Translation completeness says that nothing Qafny can prove is lost in translation: Hoare logic with array theory is expressive enough. Translation soundness says that the classical verifier cannot certify a false quantum claim: a valid translated triple corresponds to a Qafny derivation with semantically equivalent conditions and hence, by Qafny’s semantic soundness, to a true statement about the program. Both directions are needed for the claim in the abstract. The size bound shows that the translation is effective: the verification conditions it generates are polynomial in the input, so the classical verifier faces a problem no larger than the quantum one, up to its own proving power. The bound concerns the size of the conditions, not solver time.
We formally detail the compilation of this operation and others in the next section.
The Source Proof System: Qafny
We introduce a part of Qafny as an example quantum proof system for demonstrating the compilation procedure.
Qafny Syntax and A Running Example
Figure 4 shows Qafny’s syntax, which is imperative and designed with high-level quantum programming operations. A Qafny program consists of statements ( μ ) (\mu) ( μ ) , including control flow constructs (SKIP { } \{\} { } , for-loops, and quantum conditionals), classical assignments (x : = a x := a x := a ), computational basis quantum measurement ( p , x : = measure ( η ) ) (p, x := \operatorname{measure}(\eta)) ( p , x := measure ( η )) , and quantum heap mutations ( η ← ρ ) (\eta\leftarrow\rho) ( η ← ρ ) . A measurement operation is a let-binding that measures the locus η \eta η , stores the classical value of the result in x x x with the probability p p p , and uses the result in μ \mu μ . A heap mutation applies a quantum operation – such as state preparation operations (Hadamard gate H and quantum Fourier transformation gate QFT), and a quantum arithmetic oracle operation (ι \iota ι ) to a locus η \eta η ; such behavior is analogous to mutating a qubit heap array by applying the operation to each basis-ket element in the array.
OQASM Expr ι \iota ι
Arith Expr a : : = x ∣ x [ i , j ) ∣ v ∣ a + a ∣ a ⋅ a ∣ … a ::= x \mid x[i,j) \mid v \mid a+a \mid a\cdot a \mid\ldots a ::= x ∣ x [ i , j ) ∣ v ∣ a + a ∣ a ⋅ a ∣ …
Bool Expr b : : = x [ a ] ∣ ( a 1 = a 2 ) @ x [ a ] ∣ ( a 1 < a 2 ) @ x [ a ] ∣ … b ::= x[a] \mid(a_1=a_2)@x[a] \mid(a_1<a_2)@x[a] \mid\ldots b ::= x [ a ] ∣ ( a 1 = a 2 ) @ x [ a ] ∣ ( a 1 < a 2 ) @ x [ a ] ∣ …
Quantum Expr o p : : = H ∣ QFT [ − 1 ] ∣ ι op ::= H \mid\operatorname{QFT}^{[-1]} \mid\iota o p ::= H ∣ QFT [ − 1 ] ∣ ι
Statement μ : : = η ← o p ∣ p , x : = measure ( η ) ∣ if ( b ) μ ∣ { } ∣ μ ; μ ∣ for j ∈ [ a 1 , a 2 ) ( invariant P ) ∗ { μ } \mu::= \eta\gets op \mid p,x := \operatorname{measure}(\eta) \mid\operatorname{if}(b)\ \mu\mid\{\} \mid\mu;\mu\mid\operatorname{for}\ j\in[a_1,a_2)\ (\operatorname{invariant}\ P)^* \{\mu\} μ ::= η ← o p ∣ p , x := measure ( η ) ∣ if ( b ) μ ∣ { } ∣ μ ; μ ∣ for j ∈ [ a 1 , a 2 ) ( invariant P ) ∗ { μ }
Figure 4. Qafny syntax. We focus on the operations marked in black, and the red ones are essentially classical.
Quantum reversible Boolean guards b b b are implemented as OQASM oracle operations, expressed by one of ( a 1 = a 2 ) @ x [ a ] (a_1=a_2)@x[a] ( a 1 = a 2 ) @ x [ a ] , ( a 1 < a 2 ) @ x [ a ] (a_1<a_2)@x[a] ( a 1 < a 2 ) @ x [ a ] , and x [ a ] x[a] x [ a ] , which intuitively amounts to computing a 1 = a 2 a_1=a_2 a 1 = a 2 , a 1 < a 2 a_1<a_2 a 1 < a 2 and false respectively as b 0 b_0 b 0 and storing the result of b 0 ⊕ x [ a ] b_0\oplus x[a] b 0 ⊕ x [ a ] as a binary in qubit x [ a ] x[a] x [ a ] .2 In both conditionals and loops, guards b b b are used to represent the qubits being controlled.
Quantum State Predicates and Qafny Triples
Quantum program proofs are based on the Qafny triples σ ⊢ { φ } e { φ ′ } \sigma\vdash\{\varphi\}\ e\ \{\varphi'\} σ ⊢ { φ } e { φ ′ } , meaning that a quantum program verification for e e e is valid if we start with a pre-condition φ \varphi φ and the execution of e e e satisfies the post-condition φ ′ \varphi' φ ′ , where σ \sigma σ is the typed environment for φ \varphi φ and e e e . Here, φ \varphi φ and φ ′ \varphi' φ ′ are syntactic logical forms representing quantum states, e.g., the one in Figure 5 ; such quantum state predicates are generic to different quantum proof systems.
Figure 5. State syntax. The part marked in red is specific to Qafny, while the other part is generic across different quantum proof systems. A range x [ a 1 , a 2 ) x[a_1,a_2) x [ a 1 , a 2 ) in a locus is the interval [ a 1 , a 2 ) [a_1,a_2) [ a 1 , a 2 ) in a qubit array x x x . Loci are finite lists; states are finite sets. Natural-number literals are ranged over by ℓ \ell ℓ ; range bounds are arithmetic expressions and may mention program variables, such as n n n in x [ 0 , n ) x[0,n) x [ 0 , n ) . The ops after "concatenated op" are concatenations.
A program verification in Qafny can be expressed as a separation logic triple σ ⊢ { φ } e { φ ′ } \sigma\vdash\{\varphi\}\ e\ \{\varphi'\} σ ⊢ { φ } e { φ ′ } . Qafny verifies such a triple by constructing a proof tree, as in Li et al. [ [17 ] . We explain below how Qafny state representations are organized, and discuss the program syntax and proof translation in the following subsections. As in Figure 5 , the states in Qafny triples are syntactic entities that represent quantum states and program transitions. A state φ \varphi φ describes program variable behavior, where variables are partitioned into two different kinds: C for classical scalar values, such as natural numbers or reals, and Q(n n n ) for a physical n n n -length qubit array conceptually residing in a quantum heap. We permit users to write scalar types, e.g., nat and real, in a Qafny program, recognized as C-kind.
In Qafny, we abstract a quantum state as a predicate that represents a heap structure, with qubits corresponding to physical heap locations. A state predicate is expressed as η : τ ⊢ q \eta:\tau\vdash q η : τ ⊢ q , meaning that a group of possibly entangled qubits η \eta η maps to a quantum datum q q q of type τ \tau τ . Elements in a quantum state predicate are separated by the ⊕ \oplus ⊕ operation, conceptually equivalent to the tensor operator ⊗ \otimes ⊗ . Similar to the separation conjunction in separation logic, it enables modular reasoning about the quantum state predicate and ensures that different unentangled qubit groups are disjoint. Before we explain the types and loci, we show an example of expressing a Qafny triple from the Deutsch–Jozsa algorithm in Figure 3 (left). Line 1 is the pre-state predicate before any operation is executed, with EN(0) typed data ∣ 0 ⟩ |0\rangle ∣0 ⟩ (abbreviation of ⨂ n ∣ 0 ⟩ \bigotimes^n|0\rangle ⨂ n ∣0 ⟩ ) for the separable qubit group x [ 0 , n ) x[0,n) x [ 0 , n ) . Applying the Hadamard operation to x [ 0 , n ) x[0,n) x [ 0 , n ) in line 2 turns x [ 0 , n ) x[0,n) x [ 0 , n ) ’s state to a EN(1) typed datum in line 3. The pre- and post-conditions and the operation form a Qafny triple as follows.
σ ⊢ { x [ 0 , n ) : E N ( 0 ) ↦ ∣ 0 ⟩ } ← H { x [ 0 , n ) : E N ( 1 ) ↦ ∑ j = 0 2 n − 1 1 2 n ∣ j ⟩ } \sigma\vdash\{x[0,n):\mathrm{EN}(0)\mapsto|0\rangle\} \xleftarrow{H} \{x[0,n):\mathrm{EN}(1)\mapsto\sum_{j=0}^{2^n-1}\frac{1}{\sqrt{2^n}}|j\rangle\} σ ⊢ { x [ 0 , n ) : EN ( 0 ) ↦ ∣0 ⟩} H { x [ 0 , n ) : EN ( 1 ) ↦ j = 0 ∑ 2 n − 1 2 n 1 ∣ j ⟩}
Qafny utilizes types to classify quantum data, allowing quantum program verification managed in different views of a quantum state, e.g., every Qafny triple comes with a type environment σ \sigma σ , where its well-typedness is defined as σ ( η ) = type ( φ ( η ) ) \sigma(\eta) = \operatorname{type}(\varphi(\eta)) σ ( η ) = type ( φ ( η )) for all x x x . We use σ \sigma σ to track changes across different quantum state types, leading to distinct verification strategies, as captured in the proof translation in § 6 .
As mentioned above, many entangled quantum data are not expressible as tensor compositions of individual qubits. Instead, they can be expressed in the basis-ket view, i.e., the quantum datum, classified as E N ( n ) \mathrm{EN}(n) EN ( n ) type in Qafny, can be viewed as a linear sum (array) of basis-kets (q q q ), each representing a possible outcome for measuring the qubits. The n n n flag represents the number of summation operations in a quantum state representation syntactically. For example, we merge two qubit groups x [ 0 , n ) x[0,n) x [ 0 , n ) and y [ 0 ] y[0] y [ 0 ] into the entangled datum (E N ( 1 ) \mathrm{EN}(1) EN ( 1 ) ) in line 4, as the locus x [ 0 , n ) ⊔ y [ 0 ] x[0,n) \mathbin{\sqcup} y[0] x [ 0 , n ) ⊔ y [ 0 ] . Here, the whole datum has 2 n 2^n 2 n possible basis-ket states, ∣ j ⟩ ∣ 1 ⟩ |j\rangle|1\rangle ∣ j ⟩ ∣1 ⟩ for j ∈ [ 0 , 2 n ) j \in[0,2^n) j ∈ [ 0 , 2 n ) , i.e., the basis-vector fragment for x [ 0 , n ) x[0,n) x [ 0 , n ) is represented as j j j while the basis-vector fragment for y [ 0 ] y[0] y [ 0 ] is ∣ 1 ⟩ |1\rangle ∣1 ⟩ .
The E N ( 0 ) \mathrm{EN}(0) EN ( 0 ) typed datum, e.g., x [ 0 , n ) x[0,n) x [ 0 , n ) ’s state in line 1, represents a non-superposition state, since it contains only a singleton basis-ket. A single qubit state could be in superposition but not entangled, e.g., the state in Figure 3 line 3 can be represented as an n n n -length qubit array (⨂ j = 0 n − 1 x [ j , j + 1 ) \bigotimes_{j=0}^{n-1}x[j,j+1) ⨂ j = 0 n − 1 x [ j , j + 1 ) ), each of which is an E N ( 1 ) \mathrm{EN}(1) EN ( 1 ) superposition state 1 2 ( ∣ 0 ⟩ + ∣ 1 ⟩ ) \frac{1}{\sqrt{2}}(|0\rangle+|1\rangle) 2 1 ( ∣0 ⟩ + ∣1 ⟩) , for the range x [ j , j + 1 ) x[j,j+1) x [ j , j + 1 ) ; the state is not entangled.
As a state predicate, physical qubit locations might be hard to track, e.g., the entangled qubit information in line 8 is spread into different basis-kets. Therefore, we develop a locus structure x [ 0 , n ) ⊔ y [ 0 ] x[0,n) \mathbin{\sqcup} y[0] x [ 0 , n ) ⊔ y [ 0 ] to indicate that the two ranges x [ 0 , n ) x[0,n) x [ 0 , n ) and y [ 0 ] y[0] y [ 0 ] are possibly entangled. It is also information location indicator, i.e., the range x [ 0 , n ) x[0,n) x [ 0 , n ) points to the first n n n bits in each basis-ket and y [ 0 ] y[0] y [ 0 ] points to the last bit (∣ b ⊕ f ( j ) ⟩ \lvert b \oplus f(j)\rangle ∣ b ⊕ f ( j )⟩ ).
Embedding Separation Conjunction and the Frame Rule
Here, we discuss the translation of the frame rule in Qafny and QHL, and separation conjunctions, and present our first discovery: tensor-based quantum separation conjunctions are pure, a key contribution of the paper.
In separation logic, an assertion is pure when its truth depends only on the store and not on the heap. When an assertion is pure, a separation conjunction (∗ * ∗ ) is equivalent to an ordinary conjunction (∧ \wedge ∧ ). Here, we generalize the pureness as follows. Given P ∗ Q P * Q P ∗ Q , for any program operation, if we can show that modifying the operation in Q Q Q will never affect the evaluation of P P P , there is pureness on the particular separation conjunction, and P ∗ Q P * Q P ∗ Q is equivalent to P ∧ Q P \wedge Q P ∧ Q .
A non-pureness scenario is based on a predicate P ∗ l ↦ v P * l \mapsto v P ∗ l ↦ v , where l l l is a memory heap location pointing to v v v . Even though P P P might not mention l l l , a variable x x x in P P P could alias l l l , so modifying v v v might affect the value in x x x . This scenario cannot arise in Qafny: there is no qubit reference type, loci are ranges of named qubit arrays, and the type environment keeps the loci of a state disjoint, so two names never denote the same physical qubit. In a language with qubit references, or under a reference semantics, the disjointness assumption must be discharged separately, which is what a separating conjunction provides there.
Example 5.1 (Pureness Example). As an example of showing pureness, the separation conjunction in the proof lines 1-3 in Figure 3 can be replaced by a normal conjunction.
{ x [ 0 , n ) : EN ( 0 ) ↦ ∣ 0 ˉ ⟩ } x [ 0 , n ) ← H ; { x [ 0 , n ) : EN ( 1 ) ↦ ∑ j = 0 2 n − 1 1 2 n ∣ j ⟩ } \{x[0,n):\operatorname{EN}(0)\mapsto\lvert\bar{0}\rangle\}\ x[0,n)\leftarrow H;\ \{x[0,n):\operatorname{EN}(1)\mapsto\sum_{j=0}^{2^n-1}\frac{1}{\sqrt{2^n}}\lvert j\rangle\} { x [ 0 , n ) : EN ( 0 ) ↦ ∣ 0 ˉ ⟩} x [ 0 , n ) ← H ; { x [ 0 , n ) : EN ( 1 ) ↦ j = 0 ∑ 2 n − 1 2 n 1 ∣ j ⟩}
{ x [ 0 , n ) : EN ( 0 ) ↦ ∣ 0 ˉ ⟩ ∧ y [ 0 ] : EN ( 0 ) ↦ ∣ 1 ⟩ } x [ 0 , n ) ← H ; { x [ 0 , n ) : EN ( 1 ) ↦ ∑ j = 0 2 n − 1 1 2 n ∣ j ⟩ ∧ y [ 0 ] : EN ( 0 ) ↦ ∣ 1 ⟩ } \{x[0,n):\operatorname{EN}(0)\mapsto\lvert\bar{0}\rangle\wedge y[0]:\operatorname{EN}(0)\mapsto\lvert1\rangle\}\ x[0,n)\leftarrow H;\ \{x[0,n):\operatorname{EN}(1)\mapsto\sum_{j=0}^{2^n-1}\frac{1}{\sqrt{2^n}}\lvert j\rangle\wedge y[0]:\operatorname{EN}(0)\mapsto\lvert1\rangle\} { x [ 0 , n ) : EN ( 0 ) ↦ ∣ 0 ˉ ⟩ ∧ y [ 0 ] : EN ( 0 ) ↦ ∣ 1 ⟩} x [ 0 , n ) ← H ; { x [ 0 , n ) : EN ( 1 ) ↦ j = 0 ∑ 2 n − 1 2 n 1 ∣ j ⟩ ∧ y [ 0 ] : EN ( 0 ) ↦ ∣ 1 ⟩}
If we have the example in QHL, the same separation conjunction can also be rewritten as a normal conjunction. Here, φ s \varphi_s φ s can be viewed as a predicate s ↦ φ s\mapsto\varphi s ↦ φ , e.g., ∣ 0 ˉ ⟩ x [ 0 , n ) \lvert\bar{0}\rangle_{x[0,n)} ∣ 0 ˉ ⟩ x [ 0 , n ) means x [ 0 , n ) ↦ 0 ˉ x[0,n)\mapsto\bar{0} x [ 0 , n ) ↦ 0 ˉ .
{ ∣ 0 ˉ ⟩ x [ 0 , n ) } x [ 0 , n ) ← H ; { ∑ j = 0 2 n − 1 1 2 n ∣ j ⟩ x [ 0 , n ) } \{\lvert\bar{0}\rangle_{x[0,n)}\}\ x[0,n)\leftarrow H;\ \{\sum_{j=0}^{2^n-1}\frac{1}{\sqrt{2^n}}\lvert j\rangle_{x[0,n)}\} {∣ 0 ˉ ⟩ x [ 0 , n ) } x [ 0 , n ) ← H ; { j = 0 ∑ 2 n − 1 2 n 1 ∣ j ⟩ x [ 0 , n ) }
{ ∣ 0 ˉ ⟩ x [ 0 , n ) ∧ ∣ 1 ⟩ y [ 0 ] } x [ 0 , n ) ← H ; { ∑ j = 0 2 n − 1 1 2 n ∣ j ⟩ x [ 0 , n ) ∧ ∣ 1 ⟩ y [ 0 ] } \{\lvert\bar{0}\rangle_{x[0,n)}\wedge\lvert1\rangle_{y[0]}\}\ x[0,n)\leftarrow H;\ \{\sum_{j=0}^{2^n-1}\frac{1}{\sqrt{2^n}}\lvert j\rangle_{x[0,n)}\wedge\lvert1\rangle_{y[0]}\} {∣ 0 ˉ ⟩ x [ 0 , n ) ∧ ∣ 1 ⟩ y [ 0 ] } x [ 0 , n ) ← H ; { j = 0 ∑ 2 n − 1 2 n 1 ∣ j ⟩ x [ 0 , n ) ∧ ∣ 1 ⟩ y [ 0 ] }
In the above example, we replace the tensor operators connecting different unentangled qubit regions with normal conjunctions. In quantum operations, if we know that the operation, e.g., x [ 0 , n ) ← H x[0,n)\leftarrow H x [ 0 , n ) ← H , does not mention a qubit region y [ 0 ] y[0] y [ 0 ] , the operation will not affect the region. This is different from the need for separation conjunction, where a point operation might affect a memory field even if the operation does not explicitly mention the field. We have the following theorem.
Theorem 5.2 (Pureness). Given μ \mu μ , φ 1 \varphi_1 φ 1 , φ 1 ′ \varphi'_1 φ 1 ′ , and φ 2 \varphi_2 φ 2 , where φ 1 \varphi_1 φ 1 and φ 2 \varphi_2 φ 2 describe disjoint sets of qubits and F V ( μ ) ∩ F V ( φ 2 ) = ∅ FV(\mu)\cap FV(\varphi_2)=\varnothing F V ( μ ) ∩ F V ( φ 2 ) = ∅ , we have the following in QHL.
{ P , φ 1 ⊗ φ 2 } μ { Q , φ 1 ′ ⊗ φ 2 } ⟺ { P , φ 1 ∧ φ 2 } μ { Q , φ 1 ′ ∧ φ 2 } , \{P,\varphi_1\otimes\varphi_2\}\mathbin{\mu}\{Q,\varphi'_1\otimes\varphi_2\}\ \Longleftrightarrow\ \{P,\varphi_1\wedge\varphi_2\}\mathbin{\mu}\{Q,\varphi'_1\wedge\varphi_2\}, { P , φ 1 ⊗ φ 2 } μ { Q , φ 1 ′ ⊗ φ 2 } ⟺ { P , φ 1 ∧ φ 2 } μ { Q , φ 1 ′ ∧ φ 2 } , as well as the following in Qafny.
σ 1 ⊕ σ 2 ⊢ { φ 1 ∗ φ 2 } μ { φ 1 ′ ∗ φ 2 } ⟺ σ 1 ⊕ σ 2 ⊢ { φ 1 ∧ φ 2 } μ { φ 1 ′ ∧ φ 2 } . \sigma_1\oplus\sigma_2\vdash\{\varphi_1 * \varphi_2\}\mathbin{\mu}\{\varphi'_1 * \varphi_2\}\ \Longleftrightarrow\ \sigma_1\oplus\sigma_2\vdash\{\varphi_1\wedge\varphi_2\}\mathbin{\mu}\{\varphi'_1\wedge\varphi_2\}. σ 1 ⊕ σ 2 ⊢ { φ 1 ∗ φ 2 } μ { φ 1 ′ ∗ φ 2 } ⟺ σ 1 ⊕ σ 2 ⊢ { φ 1 ∧ φ 2 } μ { φ 1 ′ ∧ φ 2 } . Here, T 1 ⟷ T 2 T_1\longleftrightarrow T_2 T 1 ⟷ T 2 means that T 1 T_1 T 1 is valid if and only if T 2 T_2 T 2 is valid. We also require σ 1 ⊢ μ ▹ σ 1 ′ \sigma_1\vdash\mu\triangleright\sigma'_1 σ 1 ⊢ μ ▹ σ 1 ′ , σ 1 ⊢ φ 1 \sigma_1\vdash\varphi_1 σ 1 ⊢ φ 1 , σ 2 ⊢ φ 2 \sigma_2\vdash\varphi_2 σ 2 ⊢ φ 2 , and dom ( σ 1 ) ∩ dom ( σ 2 ) = ∅ \operatorname{dom}(\sigma_1)\cap\operatorname{dom}(\sigma_2)=\varnothing dom ( σ 1 ) ∩ dom ( σ 2 ) = ∅ : the loci of φ 1 \varphi_1 φ 1 and φ 2 \varphi_2 φ 2 are disjoint, which the Qafny
type environment enforces and which the translated frame rule below turns into the classical side condition F V ( R ) ∩ mod ( trans ( μ ) ) = ∅ FV(R) \cap\operatorname{mod}(\operatorname{trans}(\mu)) = \varnothing F V ( R ) ∩ mod ( trans ( μ )) = ∅ .
The proof of the above theorem for the Qafny version relies on its type system, and the proof for QHL relies on a clear separation for distinguishing qubit locus regions and demonstrates that the execution of μ \mu μ does not affect φ 2 \varphi_2 φ 2 . The Qafny locus type system purely separates two state conditions if they mention disjoint loci, as we have a stronger result for Qafny below.
Theorem 5.3 (Qafny Pureness). Given σ 1 ⊢ μ ▹ σ 1 ′ \sigma_1 \vdash\mu\triangleright\sigma_1' σ 1 ⊢ μ ▹ σ 1 ′ and σ 1 ⊎ σ 2 ⊢ { φ 1 ∗ φ 2 } μ { φ ′ } \sigma_1 \uplus\sigma_2 \vdash\{\varphi_1 * \varphi_2\}\ \mu\ \{\varphi'\} σ 1 ⊎ σ 2 ⊢ { φ 1 ∗ φ 2 } μ { φ ′ } , such that σ 1 ⊢ φ 1 \sigma_1 \vdash\varphi_1 σ 1 ⊢ φ 1 and σ 2 ⊢ φ 2 \sigma_2 \vdash\varphi_2 σ 2 ⊢ φ 2 , there exists φ 1 ′ \varphi_1' φ 1 ′ , such that σ 1 ′ ⊢ φ 1 ′ \sigma_1' \vdash\varphi_1' σ 1 ′ ⊢ φ 1 ′ and φ ′ = φ 1 ′ ∗ φ 2 \varphi' = \varphi_1' * \varphi_2 φ ′ = φ 1 ′ ∗ φ 2 .
Proof. The proof utilizes the Qafny proof system soundness [17 ] 4 , as we inductively show that for any e e e , if a state φ \varphi φ can be partitioned into φ 1 \varphi_1 φ 1 and φ 2 \varphi_2 φ 2 based on disjoint loci in dom ( σ 1 ) \operatorname{dom}(\sigma_1) dom ( σ 1 ) and dom ( σ 2 ) \operatorname{dom}(\sigma_2) dom ( σ 2 ) and e e e only operates on φ 1 \varphi_1 φ 1 , then φ 2 \varphi_2 φ 2 is never affected. □ \square □
dom ( σ ) ∩ F V ( φ 2 ) = ∅ \operatorname{dom}(\sigma) \cap FV(\varphi_2) = \varnothing dom ( σ ) ∩ F V ( φ 2 ) = ∅
mod ( μ ) ⊆ dom ( σ ) σ ⊢ { φ 1 } μ { φ 1 ′ } \operatorname{mod}(\mu) \subseteq\operatorname{dom}(\sigma) \qquad\sigma\vdash\{\varphi_1\}\ \mu\ \{\varphi_1'\} mod ( μ ) ⊆ dom ( σ ) σ ⊢ { φ 1 } μ { φ 1 ′ }
σ ⊎ σ 2 ⊢ { φ 1 ∗ φ 2 } μ { φ 1 ′ ∗ φ 2 } ⟶ F V ( R ) ∩ mod ( trans ( μ ) ) = ∅ \sigma\uplus\sigma_2 \vdash\{\varphi_1 * \varphi_2\}\ \mu\ \{\varphi_1' * \varphi_2\}
\qquad\longrightarrow\qquad
FV(R) \cap\operatorname{mod}(\operatorname{trans}(\mu)) = \varnothing σ ⊎ σ 2 ⊢ { φ 1 ∗ φ 2 } μ { φ 1 ′ ∗ φ 2 } ⟶ F V ( R ) ∩ mod ( trans ( μ )) = ∅
{ P ∧ R } trans ( μ ) { Q ∧ R } \{P \land R\}\ \operatorname{trans}(\mu)\ \{Q \land R\} { P ∧ R } trans ( μ ) { Q ∧ R }
Theorem 5.2 suggests that separation conjunctions can be replaced by ordinary ones if the two pieces describe disjoint loci. It indicates that the separation conjunctions in the Qafny and QHL frame rule – where the Qafny one is listed on the left above – can be rewritten to a formula containing only ordinary conjunctions; thus, translating the two quantum proof systems to Hoare logic will not require adding separation conjunctions. Here, trans ( μ ) \operatorname{trans}(\mu) trans ( μ ) represents the translated Hoare logic program for μ \mu μ , P P P , Q Q Q , and R R R are the translated predicates for φ 1 \varphi_1 φ 1 , φ 1 ′ \varphi_1' φ 1 ′ , and φ 2 \varphi_2 φ 2 . The details of these translated functions are given in § 7 .
In fact, the above result is general for all the tensor (⊗ \otimes ⊗ ) based quantum separation conjunctions [15 , 35 ] : once the disjointness of the two qubit regions is tracked, as the Qafny type environment does, these separation conjunctions are pure, i.e., they can be treated as normal conjunctions.
The Intermediate Representation Language based on Hoare Logic
We describe HLIR based on Hoare logic, where every operation is definable and provable as a composition of operations in the classical Hoare logic language with array assignments. Mainly, we discuss the syntax (Figure 6 ), the informal meaning, and array-state representations. We detail HLIR with proof examples, along with our translation proof from Qafny to HLIR in the next section.
Figure 6. HLIR Syntax
Index Function ρ \rho ρ ::= ind φ ( η ) \operatorname{ind}^{\varphi}(\eta) ind φ ( η )
Array Element θ \theta θ ::= element { amp = z , vector = c } \operatorname{element}\{\operatorname{amp}=z,\operatorname{vector}=c\} element { amp = z , vector = c }
Function χ \chi χ ::= up a ( θ , z ) ∣ up b ( θ , c , ρ ) ∣ range ( m ‾ ) ∣ Q len ( A , m ‾ ) ∣ enum ( n ) ∣ merge ( ρ , c , c ) \operatorname{up}^{a}(\theta,z)\mid\operatorname{up}^{b}(\theta,c,\rho)\mid\operatorname{range}(\overline{m})\mid Q_{\operatorname{len}}(A,\overline{m})\mid\operatorname{enum}(n)\mid\operatorname{merge}(\rho,c,c) up a ( θ , z ) ∣ up b ( θ , c , ρ ) ∣ range ( m ) ∣ Q len ( A , m ) ∣ enum ( n ) ∣ merge ( ρ , c , c )
Statement e e e ::= A ′ : = mapN ( A , m ‾ , f ) ∣ A ′ : = filterN ( A , b , ρ ) ∣ A 2 : = assignN ( A , A 1 , m ‾ , ρ , f ) A' := \operatorname{mapN}(A,\overline{m},f)\mid A' := \operatorname{filterN}(A,b,\rho)\mid A_2 := \operatorname{assignN}(A,A_1,\overline{m},\rho,f) A ′ := mapN ( A , m , f ) ∣ A ′ := filterN ( A , b , ρ ) ∣ A 2 := assignN ( A , A 1 , m , ρ , f )
∣ r : = sqrtSum ( A , m ‾ ) ∣ A 2 : = copyN ( A , A 1 , ρ ) ∣ A : = A \mid\ r := \operatorname{sqrtSum}(A,\overline{m})\mid A_2 := \operatorname{copyN}(A,A_1,\rho)\mid A := A ∣ r := sqrtSum ( A , m ) ∣ A 2 := copyN ( A , A 1 , ρ ) ∣ A := A
∣ A ′ : = locateN ( A , m ‾ , ρ ) ∣ A ′ : = copyN ( A , m ‾ , ρ ) ∣ A ′ : = enumN ( A , m ‾ , ρ ) \mid\ A' := \operatorname{locateN}(A,\overline{m},\rho)\mid A' := \operatorname{copyN}(A,\overline{m},\rho)\mid A' := \operatorname{enumN}(A,\overline{m},\rho) ∣ A ′ := locateN ( A , m , ρ ) ∣ A ′ := copyN ( A , m , ρ ) ∣ A ′ := enumN ( A , m , ρ )
∣ A 2 : = ampN ( A , A 1 , m ‾ , ρ ) ∣ A ′ : = upDim ( A , m ‾ , m ‾ ′ ) \mid\ A_2 := \operatorname{ampN}(A,A_1,\overline{m},\rho)\mid A' := \operatorname{upDim}(A,\overline{m},\overline{m}') ∣ A 2 := ampN ( A , A 1 , m , ρ ) ∣ A ′ := upDim ( A , m , m ′ )
HLIR State Representation. As we mentioned, for an EN(n n n )-typed quantum state, loci partition the state into disjoint qubit regions, where every fragment in a locus is disjoint from other fragments in the locus. We model the EN(n n n )-typed state in Hoare logic as an n n n -dimensional array, containing elements θ \theta θ , modeled as a record in Figure 6 . The array indexing operation A [ j 0 , … , j n − 1 ] A[j_0, \ldots, j_{n-1}] A [ j 0 , … , j n − 1 ] can be abbreviated as A [ j ‾ ] A[\overline{j}] A [ j ] , which represents a basis-ket state; the array can be thought of as a linear sum of basis-kets. In each element, one can access the amplitude via amp and the vector via vector. The two functions up a ( θ , z ) \operatorname{up}^{a}(\theta,z) up a ( θ , z ) and up b ( θ , c , ρ ) \operatorname{up}^{b}(\theta,c,\rho) up b ( θ , c , ρ ) update the amp of an element and the ρ \rho ρ positions of its vector, respectively.
Essentially, vector is a bit sequence. We maintain a map ρ ≜ ind φ ( η ) \rho\triangleq\operatorname{ind}^{\varphi}(\eta) ρ ≜ ind φ ( η ) from the qubits in the locus fragment η \eta η to the indices in the bit sequence, to record the index positions. As an instance, Equation (1) is an array representation of a quantum state, pointed to by the locus x [ 0 ] ∣ y [ 0 ] x[0] \mathbin{|} y[0] x [ 0 ] ∣ y [ 0 ] , where ind φ ( x [ 0 ] ) \operatorname{ind}^{\varphi}(x[0]) ind φ ( x [ 0 ]) , with φ \varphi φ being the state in Equation (1) , points to the index position 0, while y [ 0 ] y[0] y [ 0 ] points to the index position 1. We have the following lemma for ind.
Lemma 6.1 (Disjointness Preservation). Given a locus η \eta η , an EN(n n n ) typed state φ \varphi φ , and a locus map ind, where indices ( A ) = ind φ ( η ) \operatorname{indices}(A)=\operatorname{ind}^{\varphi}(\eta) indices ( A ) = ind φ ( η ) and for every j 0 , … , j n − 1 j_0,\ldots,j_{n-1} j 0 , … , j n − 1 , we have ∣ A [ j 0 , … , j n − 1 ] ∣ = ∣ η ∣ \lvert A[j_0,\ldots,j_{n-1}]\rvert=\lvert\eta\rvert ∣ A [ j 0 , … , j n − 1 ]∣ = ∣ η ∣ , and for every locus fragment η ′ \eta' η ′ , s.t. η ′ ⊆ η \eta'\subseteq\eta η ′ ⊆ η and indices ( A ′ ) = ind φ ( η ′ ) \operatorname{indices}(A')=\operatorname{ind}^{\varphi}(\eta') indices ( A ′ ) = ind φ ( η ′ ) , for j 0 , … , j n − 1 j_0,\ldots,j_{n-1} j 0 , … , j n − 1 , vector ( A ′ [ j 0 , … , j n − 1 ] ) ⊆ vector ( A [ j 0 , … , j n − 1 ] ) \operatorname{vector}(A'[j_0,\ldots,j_{n-1}])\subseteq\operatorname{vector}(A[j_0,\ldots,j_{n-1}]) vector ( A ′ [ j 0 , … , j n − 1 ]) ⊆ vector ( A [ j 0 , … , j n − 1 ]) .
The above lemma has a hidden assumption when applied to Qafny: we assume we can always refer to a finite global universe containing all qubits in the state φ \varphi φ . Such a statement is valid in Qafny as well as many other quantum proof systems. We present the following lemma, which states that the resulting quantum state after executing a Qafny program is always a subset of the input state. Thus, for any program, there is a quantum state φ \varphi φ that acts as its universe.
Lemma 6.2 (Quantum State Shrinking). Given a state φ \varphi φ , a Qafny program μ \mu μ , and a type environment σ \sigma σ , such that σ ⊢ φ \sigma\vdash\varphi σ ⊢ φ , σ ⊢ μ ▹ σ ′ \sigma\vdash\mu\mathrel{\triangleright}\sigma' σ ⊢ μ ▹ σ ′ , and ( φ , μ ) ⇓ φ ′ (\varphi,\mu)\Downarrow\varphi' ( φ , μ ) ⇓ φ ′ (the Qafny program μ \mu μ is evaluated to φ ′ \varphi' φ ′ by the input state φ \varphi φ ), we have dom ( φ ′ ) ⊆ dom ( φ ) \operatorname{dom}(\varphi')\subseteq\operatorname{dom}(\varphi) dom ( φ ′ ) ⊆ dom ( φ ) .
Briefing the HLIR Operation Syntax. The functions and statements in Figure 6 are used in the translation proof in § 7 . We provide informal descriptions below.
We have a set of helper functions to define small array properties. The operations range ( m ‾ ) \operatorname{range}(\overline{m}) range ( m ) and Q len ( A , m ‾ ) Q_{\operatorname{len}}(A,\overline{m}) Q len ( A , m ) are described in § 2 . enum ( n ) \operatorname{enum}(n) enum ( n ) enumerates numbers from 0 to n − 1 n-1 n − 1 , merge ( ρ , c 1 , c 2 ) \operatorname{merge}(\rho,c_1,c_2) merge ( ρ , c 1 , c 2 ) creates a new vector by replacing c 1 c_1 c 1 's ρ \rho ρ indices with c 2 c_2 c 2 , and vector ( c ) [ ρ ] \operatorname{vector}(c)[\rho] vector ( c ) [ ρ ] shrinks vector c c c to ρ \rho ρ .
We divide the statements into two sets: a set (marked black) of essential operations and a set (marked blue ) of compositional operations definable in terms of the essential set. Most of the operations in the essential set are standard functional programming operations. mapN \operatorname{mapN} mapN is a high-order map operation mapping the function f f f to every element in the array A A A , filterN \operatorname{filterN} filterN filters out elements in an array (A A A ) that do not satisfy the boolean function b b b , concat \operatorname{concat} concat concatenates two arrays if they have the same dimensions, ∣ A ∣ \lvert A\rvert ∣ A ∣ produces the array lengths, which can be high-dimensional, and sqrtSum \operatorname{sqrtSum} sqrtSum computes the square root of the sum of the squared amplitude norms over all array elements, as it can be built on top of a higher-order folding function. assignN ( A , A 1 , m ‾ , ρ , f ) \operatorname{assignN}(A,A_1,\overline{m},\rho,f) assignN ( A , A 1 , m , ρ , f ) is an update function that updates A A A with A 1 A_1 A 1 's element amplitudes and vectors (the shrunk ρ \rho ρ fragments), assuming ∣ A ∣ = ∣ A 1 ∣ \lvert A\rvert=\lvert A_1\rvert ∣ A ∣ = ∣ A 1 ∣ , where f f f defines how to combine the amplitudes of A A A and A 1 A_1 A 1 's elements.
The set of compositional operations is definable by the essential set. We examine them in § 7 and briefly describe them here. locateN \operatorname{locateN} locateN creates a new array A ′ A' A ′ by shrinking the vectors in every element to the indices ρ \rho ρ . copyN ( A , m ‾ , ind φ ( η ) ) \operatorname{copyN}(A,\overline{m},\operatorname{ind}^{\varphi}(\eta)) copyN ( A , m , ind φ ( η )) and enumN ( A , m ‾ , ind φ ( η ) ) \operatorname{enumN}(A,\overline{m},\operatorname{ind}^{\varphi}(\eta)) enumN ( A , m , ind φ ( η )) are special case of mapN \operatorname{mapN} mapN . The former creates an n + 1 n+1 n + 1 dimensional array A ′ A' A ′ if A A A is n n n dimensional, by replacing each element with a list of $2^{|\eta|}$ copies of that element. If the inner dimension has length 2 ∣ η ∣ 2^{|\eta|} 2 ∣ η ∣ , enumN \operatorname{enumN} enumN creates an array A ′ A' A ′ by copying A A A but differing in terms of the vectors, where $\operatorname{vector}(A'[\overline{j},i],\operatorname{ind}^{\varphi}(\eta))=i and vector ( A [ j ˉ , i ] , { x ∣ x ∉ ind ρ ( η ) } ) = A [ j ˉ , i ] \operatorname{vector}(A[\bar{j},i], \{x \mid x \notin\operatorname{ind}^{\rho}(\eta)\}) = A[\bar{j},i] vector ( A [ j ˉ , i ] , { x ∣ x ∈ / ind ρ ( η )}) = A [ j ˉ , i ] . ampN ( A , A 1 , m ˉ , ρ ) \operatorname{ampN}(A,A_1,\bar{m},\rho) ampN ( A , A 1 , m ˉ , ρ ) multiplies A 1 A_1 A 1 and A A A 's element amplitudes. upDim ( A , m ˉ , m ˉ ′ ) \operatorname{upDim}(A,\bar{m},\bar{m}') upDim ( A , m ˉ , m ˉ ′ ) upgrades A A A 's dimensions to m ˉ ′ \bar{m}' m ˉ ′ , assuming that m ˉ ⊑ m ˉ ′ \bar{m} \sqsubseteq\bar{m}' m ˉ ⊑ m ˉ ′ (defined in § 2 ). The operation fills the new dimensions by copying the elements in the original A A A .
Translating Qafny Operations to HLIR
This section describes the translation proof from Qafny to HLIR. The translation of Qafny to HLIR turns Qafny proof triples ( σ ⊢ { φ } μ { φ ′ } ) (\sigma\vdash\{\varphi\} \ \mu\ \{\varphi'\}) ( σ ⊢ { φ } μ { φ ′ }) to Hoare logic triples { P } e { Q } \{P\}\ e\ \{Q\} { P } e { Q } , including two pieces: translating the Qafny program μ \mu μ to an IR code e e e and, respectively, translating the pre- and post-conditions φ \varphi φ and φ ′ \varphi' φ ′ to P P P and Q Q Q . The two pieces can be described by two judgments: σ ⊢ μ ▹ σ ′ ≫ e \sigma\vdash\mu\triangleright\sigma' \gg e σ ⊢ μ ▹ σ ′ ≫ e and σ ⊢ φ ≫ P \sigma\vdash\varphi\gg P σ ⊢ φ ≫ P . The former extends the Qafny type system ( σ ⊢ μ ▹ σ ′ ) (\sigma\vdash\mu\triangleright\sigma') ( σ ⊢ μ ▹ σ ′ ) and the latter extends the well-typed quantum state ( σ ⊢ φ ) (\sigma\vdash\varphi) ( σ ⊢ φ ) . In § 7.1 to § 7.5 , we discuss translations for different Qafny operations, along with validity arguments, the completeness direction: for any derivable Qafny triple for an operation, we show that its translated Hoare triple is valid. Finally, we state the translation completeness, translation soundness, and translation size theorems in § 7.6 .
The translations are organized by the effect of an operation on the array representation. (1) Operations that create superposition add a dimension: H and QFT (and, in the same way, rotations that create superposition) translate to copyN, enumN, and ampN (§ 7.2 ); the amplitudes need not be uniform, since ampN multiplies by an arbitrary function of the old and new basis vectors, of which the uniform Hadamard amplitude is one instance. (2) Operations that map elements in place, i.e., basis permutations and phases such as X, RZ, and the arithmetic oracles ι \iota ι , translate to locateN, mapN, and assignN (§ 7.1 ). (3) Controlled operations, i.e., quantum conditionals, split the array with filterN, translate the body, and reassemble it with concat (§ 7.3 ). (4) Measurement filters and renormalizes (§ 7.4 ), and classical statements are translated structurally (§ 7.5 ). Any gate given by its action on basis kets falls into class (1) or (2), possibly under a control of class (3).
Translating Oracle Operations
Here, we discuss the transformation of the oracle operation proof rule below to HLIR.
{ η ↦ η ′ : EN ( n ) } ⊢ { η ↦ η ′ → q } η ← μ { η ↦ η ′ → ⟦ μ ⟧ η ′ q } \{\eta\mapsto\eta' : \operatorname{EN}(n)\} \vdash\{\eta\mapsto\eta' \to q\}\ \eta\leftarrow\mu\ \{\eta\mapsto\eta' \to\llbracket\mu\rrbracket^{\eta'}q\} { η ↦ η ′ : EN ( n )} ⊢ { η ↦ η ′ → q } η ← μ { η ↦ η ′ → [ [ μ ] ] η ′ q }
This rule relies on a frame rule to focus state predicates to the locus η ↦ η ′ \eta\mapsto\eta' η ↦ η ′ . Because the Qafny separation conjunction is pure, it is no different from a normal conjunction. We can rewrite the above Qafny proof rule to the version below, which relates a general post-condition φ \varphi φ to a pre-condition, in a style similar to the Hoare logic array assignment rule.
{ η ↦ η ′ : EN ( n ) } ⊢ σ ⊢ { φ [ ( η ↦ η ′ ) { η ↦ ⟦ μ ⟧ ( η ) } / η ↦ η ′ ] } η ← μ { φ } \{\eta\mapsto\eta' : \operatorname{EN}(n)\} \vdash\sigma\vdash\{\varphi\bigl[(\eta\mapsto\eta')\{ \eta\mapsto\llbracket\mu\rrbracket(\eta) \}/\eta\mapsto\eta'\bigr]\}\ \eta\leftarrow\mu\ \{\varphi\} { η ↦ η ′ : EN ( n )} ⊢ σ ⊢ { φ [ ( η ↦ η ′ ) { η ↦ [ [ μ ] ] ( η )} / η ↦ η ′ ] } η ← μ { φ }
To translate the proof rule to Hoare logic, we construct a composition of classical operations representing the effect of the operation η ← μ \eta\leftarrow\mu η ← μ , including three components: the operations locateN, mapN, and assignN, mentioned in § 3 . One example is given in § 3 .
Validity Proof. We state the translated Hoare triple as a lemma below, showing its validity.
Lemma 7.1 (Valid Oracle Application Transformation). Given a quantum state φ \varphi φ and an operation η ← μ \eta\leftarrow\mu η ← μ in the locus η ↦ η ′ \eta\mapsto\eta' η ↦ η ′ , with the EN ( n ) \operatorname{EN}(n) EN ( n ) -typed quantum datum φ ( η ↦ η ′ ) \varphi(\eta\mapsto\eta') φ ( η ↦ η ′ ) , transformed to an n n n dimensional array A A A having the collection of indices ( ind φ ( η ↦ η ′ ) ) (\operatorname{ind}^{\varphi}(\eta\mapsto\eta')) ( ind φ ( η ↦ η ′ )) , where lens ( A ) = m ˉ = m 0 , … , m n − 1 \operatorname{lens}(A)=\bar{m}=m_0,\ldots,m_{n-1} lens ( A ) = m ˉ = m 0 , … , m n − 1 , we have the following valid Hoare triple.
{ P [ A { ∀ j ⃗ ∈ range ( m ⃗ ) . j ⃗ ↦ u p b ( A [ j ⃗ ] , ⟦ μ ⟧ ( vector ( A [ j ⃗ ] ) [ ind φ ( η ) ] ) , ind φ ( η ) ) } / A ] \left\{P\left[A\left\{\forall\vec{j}\in\operatorname{range}(\vec{m}).\vec{j}\mapsto up^{b}\left(A[\vec{j}],\llbracket\mu\rrbracket\left(\operatorname{vector}(A[\vec{j}])[\operatorname{ind}^{\varphi}(\eta)]\right),\operatorname{ind}^{\varphi}(\eta)\right)\right\}/A\right]\right. { P [ A { ∀ j ∈ range ( m ) . j ↦ u p b ( A [ j ] , [ [ μ ] ] ( vector ( A [ j ]) [ ind φ ( η )] ) , ind φ ( η ) ) } / A ]
∧ Q len ( A { ∀ j ⃗ ∈ range ( m ⃗ ) . j ⃗ ↦ u p b ( A [ j ⃗ ] , ⟦ μ ⟧ ( vector ( A [ j ⃗ ] ) [ ind φ ( η ) ] ) , ind φ ( η ) ) } , m ⃗ ) \land Q_{\operatorname{len}}\left(A\left\{\forall\vec{j}\in\operatorname{range}(\vec{m}).\vec{j}\mapsto up^{b}\left(A[\vec{j}],\llbracket\mu\rrbracket\left(\operatorname{vector}(A[\vec{j}])[\operatorname{ind}^{\varphi}(\eta)]\right),\operatorname{ind}^{\varphi}(\eta)\right)\right\},\vec{m}\right) ∧ Q len ( A { ∀ j ∈ range ( m ) . j ↦ u p b ( A [ j ] , [ [ μ ] ] ( vector ( A [ j ]) [ ind φ ( η )] ) , ind φ ( η ) ) } , m )
A 1 : = locateN ( A , m ⃗ , ind φ ( η ) ) ; A 2 : = mapN ( A 1 , m ⃗ , λ ( x , y ) . ( x , ⟦ μ ⟧ y ) ) ; A_{1}:=\operatorname{locateN}(A,\vec{m},\operatorname{ind}^{\varphi}(\eta));\ A_{2}:=\operatorname{mapN}\left(A_{1},\vec{m},\lambda(x,y).(x,\llbracket\mu\rrbracket y)\right); A 1 := locateN ( A , m , ind φ ( η )) ; A 2 := mapN ( A 1 , m , λ ( x , y ) . ( x , [ [ μ ] ] y ) ) ;
A 3 : = assignN ( A , A 2 , m ⃗ , ind φ ( η ) , λ u v . amp ( u ) ) ; A : = A 3 A_{3}:=\operatorname{assignN}\left(A,A_{2},\vec{m},\operatorname{ind}^{\varphi}(\eta),\lambda u\ v.\operatorname{amp}(u)\right);\ A:=A_{3} A 3 := assignN ( A , A 2 , m , ind φ ( η ) , λ u v . amp ( u ) ) ; A := A 3
{ P ∧ Q len ( A , m ⃗ ) } \left\{P\land Q_{\operatorname{len}}(A,\vec{m})\right\} { P ∧ Q len ( A , m ) }
Here, ∀ j ⃗ ∈ range ( m ⃗ ) \forall\vec{j}\in\operatorname{range}(\vec{m}) ∀ j ∈ range ( m ) refers to ∀ j 0 ∈ [ 0 , m 0 ) , … , j n ∈ [ 0 , m n ) \forall j_{0}\in[0,m_{0}),\ldots,j_{n}\in[0,m_{n}) ∀ j 0 ∈ [ 0 , m 0 ) , … , j n ∈ [ 0 , m n ) where j ⃗ \vec{j} j and m ⃗ \vec{m} m are expanded to ( j 0 , … , j n ) (j_{0},\ldots,j_{n}) ( j 0 , … , j n ) and ( m 0 , … , m n ) (m_{0},\ldots,m_{n}) ( m 0 , … , m n ) ; u p b up^{b} u p b and u p a up^{a} u p a are defined as follows, where merge ( ρ , c , c ′ ) \operatorname{merge}(\rho,c,c') merge ( ρ , c , c ′ ) (Figure 6 ) replaces the ρ \rho ρ positions of c c c with the bits c ′ c' c ′ , with ∣ c ′ ∣ = ∣ ρ ∣ \lvert c'\rvert=\lvert\rho\rvert ∣ c ′ ∣ = ∣ ρ ∣ .
u p b ( element { amp = z , vector = c } , c ′ , ρ ) = element { amp = z , vector = merge ( ρ , c , c ′ ) } up^{b}(\operatorname{element}\{\operatorname{amp}=z,\operatorname{vector}=c\},c',\rho)=\operatorname{element}\{\operatorname{amp}=z,\operatorname{vector}=\operatorname{merge}(\rho,c,c')\} u p b ( element { amp = z , vector = c } , c ′ , ρ ) = element { amp = z , vector = merge ( ρ , c , c ′ )}
u p a ( element { amp = z , vector = c } , z ′ ) = element { amp = z ′ , vector = c } up^{a}(\operatorname{element}\{\operatorname{amp}=z,\operatorname{vector}=c\},z')=\operatorname{element}\{\operatorname{amp}=z',\operatorname{vector}=c\} u p a ( element { amp = z , vector = c } , z ′ ) = element { amp = z ′ , vector = c }
Proof. The validity proof utilizes Lemmas 7.2 and 7.3. Its mechanized proof is in Rocq. □
We define and prove below the three operations locateN, mapN, and assignN. Essentially, locateN and mapN are similar, where the former is a special case of the latter, described below
locateN ( A , m ⃗ , ind φ ( η ) ) = mapN ( A , m ⃗ , λ ( x , y ) . ( x , y [ ind φ ( η ) ] ) ) . \operatorname{locateN}(A,\vec{m},\operatorname{ind}^{\varphi}(\eta))=\operatorname{mapN}\left(A,\vec{m},\lambda(x,y).(x,y[\operatorname{ind}^{\varphi}(\eta)])\right). locateN ( A , m , ind φ ( η )) = mapN ( A , m , λ ( x , y ) . ( x , y [ ind φ ( η )]) ) .
We now show the proof for mapN. For all the proofs, we assume the array length axioms Q len ( A , A ′ , … , m ⃗ ) Q_{\operatorname{len}}(A,A',\ldots,\vec{m}) Q len ( A , A ′ , … , m ) , stating that the n n n -dimensional arrays A , A ′ , … A,A',\ldots A , A ′ , … have n n n -dimensional lengths m ⃗ = m 0 , … , m n − 1 \vec{m}=m_{0},\ldots,m_{n-1} m = m 0 , … , m n − 1 .
Lemma 7.2 (The Valid MapN Definition). Given n n n dimensional arrays A A A and A ′ A' A ′ , with Q len ( A , A ′ , m ⃗ ) Q_{\operatorname{len}}(A,A',\vec{m}) Q len ( A , A ′ , m ) , and a function f f f , we have the valid Hoare triple for A ′ : = mapN ( A , m ⃗ , f ) A':=\operatorname{mapN}(A,\vec{m},f) A ′ := mapN ( A , m , f ) :
{ P [ A ′ { ∀ j ⃗ ∈ range ( m ⃗ ) . j ⃗ ↦ f ( A [ j ⃗ ] ) } / A ′ ] ∧ Q len ( A , A ′ , m ⃗ ) } \left\{P\left[A'\left\{\forall\vec{j}\in\operatorname{range}(\vec{m}).\vec{j}\mapsto f(A[\vec{j}])\right\}/A'\right]\land Q_{\operatorname{len}}(A,A',\vec{m})\right\} { P [ A ′ { ∀ j ∈ range ( m ) . j ↦ f ( A [ j ]) } / A ′ ] ∧ Q len ( A , A ′ , m ) }
A ′ : = mapN ( A , m ⃗ , f ) A':=\operatorname{mapN}(A,\vec{m},f) A ′ := mapN ( A , m , f )
{ P ∧ Q len ( A , A ′ , m ⃗ ) } \left\{P\land Q_{\operatorname{len}}(A,A',\vec{m})\right\} { P ∧ Q len ( A , A ′ , m ) }
where A ′ : = mapN ( A , m ⃗ , f ) A':=\operatorname{mapN}(A,\vec{m},f) A ′ := mapN ( A , m , f ) is defined as:
j := 0;
while(j₀ < m₀){
while(jₙ₋₁ < mₙ₋₁){
A′[k₀, ..., kₙ₋₁] := f(A[k₀, ..., kₙ₋₁]); jₙ₋₁ := jₙ₋₁ + 1
}
... j₀ := j₀ + 1}
Proof. By Lemma 2.1, it is enough to show the validity of the following triple.
{ Q len ( A , A ′ , m ⃗ ) } A ′ : = mapN ( A , m ⃗ , f ) { ∀ j ⃗ ∈ range ( m ⃗ ) . A ′ [ j ⃗ ] = f ( A [ j ⃗ ] ) ∧ Q len ( A , A ′ , m ⃗ ) } \left\{Q_{\operatorname{len}}(A,A',\vec{m})\right\}\quad A':=\operatorname{mapN}(A,\vec{m},f)\quad\left\{\forall\vec{j}\in\operatorname{range}(\vec{m}).A'[\vec{j}]=f(A[\vec{j}])\land Q_{\operatorname{len}}(A,A',\vec{m})\right\} { Q len ( A , A ′ , m ) } A ′ := mapN ( A , m , f ) { ∀ j ∈ range ( m ) . A ′ [ j ] = f ( A [ j ]) ∧ Q len ( A , A ′ , m ) }
The proof-tree derivation, with loop invariants at each level, is given in Section A.1. □
assignN ( A , A 2 , m ⃗ , ρ , λ u v . f ( u , v ) ) \operatorname{assignN}(A,A_{2},\vec{m},\rho,\lambda u\ v.f(u,v)) assignN ( A , A 2 , m , ρ , λ u v . f ( u , v )) updates A A A ’s element amplitudes with the combination of A A A and A 2 A_{2} A 2 ’s amplitudes via λ u v . f ( u , v ) \lambda u\ v.f(u,v) λ u v . f ( u , v ) , as well as updates A A A ’s bit-vectors, only for the ρ \rho ρ part, with A 2 A_{2} A 2 ’s. We now show the validity lemma for assignN.
Lemma 7.3 (The Valid AssignN Definition). Given n n n dimensional arrays A A A , A 1 A_{1} A 1 , and A 2 A_{2} A 2 (assuming that A 2 A_{2} A 2 is pre-initialized to an n n n dimensional array with bounds m ⃗ \vec{m} m ), with Q len ( A , A 1 , A 2 , m ⃗ ) Q_{\operatorname{len}}(A,A_{1},A_{2},\vec{m}) Q len ( A , A 1 , A 2 , m ) , we have the following valid Hoare triple.
{ P [ A 2 { ∀ j ˉ ∈ range ( m ˉ ) . j ˉ ↦ u p a ( u p b ( A [ j ˉ ] , vector ( A 1 [ j ˉ ] ) , ind φ ( η ) ) , f ( A [ j ˉ ] , A 1 [ j ˉ ] ) ) } / A 2 ] ∧ Q l e n ( A 2 , A , A 1 , m ˉ ) A 2 : = assignN ( A , A 1 , m ˉ , ind φ ( η ) , λ u v . f ( u , v ) ) P ∧ Q l e n ( A 2 , A , A 1 , m ˉ ) \left\{
\begin{aligned}
&P\left[A_2\left\{\forall\bar{j}\in\operatorname{range}(\bar{m}).\bar{j}\mapsto up^a\left(up^b\left(A[\bar{j}],\operatorname{vector}(A_1[\bar{j}]),\operatorname{ind}^{\varphi}(\eta)\right),f\left(A[\bar{j}],A_1[\bar{j}]\right)\right)\right\}/A_2\right]\\
&\quad\land Q_{\mathrm{len}}(A_2,A,A_1,\bar{m})\\
&A_2:=\operatorname{assignN}(A,A_1,\bar{m},\operatorname{ind}^{\varphi}(\eta),\lambda u\,v.f(u,v))\\
&P\land Q_{\mathrm{len}}(A_2,A,A_1,\bar{m})
\end{aligned}
\right. ⎩ ⎨ ⎧ P [ A 2 { ∀ j ˉ ∈ range ( m ˉ ) . j ˉ ↦ u p a ( u p b ( A [ j ˉ ] , vector ( A 1 [ j ˉ ]) , ind φ ( η ) ) , f ( A [ j ˉ ] , A 1 [ j ˉ ] ) ) } / A 2 ] ∧ Q len ( A 2 , A , A 1 , m ˉ ) A 2 := assignN ( A , A 1 , m ˉ , ind φ ( η ) , λ u v . f ( u , v )) P ∧ Q len ( A 2 , A , A 1 , m ˉ )
Proof. By Lemma 2.1 , it is enough to show the validity of the following triple.
{ Q l e n ( A 2 , A , A 1 , m ˉ ) A 2 : = assignN ( A , A 1 , m ˉ , ind φ ( η ) , λ u v . f ( u , v ) ) ∀ j ˉ ∈ range ( m ˉ ) . A 2 [ j ˉ ] = u p a ( u p b ( A [ j ˉ ] , vector ( A 1 [ j ˉ ] ) , ind φ ( η ) ) , f ( A [ j ˉ ] , A 1 [ j ˉ ] ) ) ∧ Q l e n ( A 2 , A , A 1 , m ˉ ) \left\{
\begin{aligned}
&Q_{\mathrm{len}}(A_2,A,A_1,\bar{m})\\
&A_2:=\operatorname{assignN}(A,A_1,\bar{m},\operatorname{ind}^{\varphi}(\eta),\lambda u\,v.f(u,v))\\
&\forall\bar{j}\in\operatorname{range}(\bar{m}).A_2[\bar{j}]=up^a\left(up^b\left(A[\bar{j}],\operatorname{vector}(A_1[\bar{j}]),\operatorname{ind}^{\varphi}(\eta)\right),f\left(A[\bar{j}],A_1[\bar{j}]\right)\right)\land Q_{\mathrm{len}}(A_2,A,A_1,\bar{m})
\end{aligned}
\right. ⎩ ⎨ ⎧ Q len ( A 2 , A , A 1 , m ˉ ) A 2 := assignN ( A , A 1 , m ˉ , ind φ ( η ) , λ u v . f ( u , v )) ∀ j ˉ ∈ range ( m ˉ ) . A 2 [ j ˉ ] = u p a ( u p b ( A [ j ˉ ] , vector ( A 1 [ j ˉ ]) , ind φ ( η ) ) , f ( A [ j ˉ ] , A 1 [ j ˉ ] ) ) ∧ Q len ( A 2 , A , A 1 , m ˉ )
The rest of the proof derives a Hoare logic proof tree and has a procedure as the one in Lemma 7.2, except that we update A 2 [ j ˉ ] A_2[\bar{j}] A 2 [ j ˉ ] with u p a ( u p b ( A [ j ˉ ] , vector ( A 1 [ j ˉ ] ) , ind φ ( η ) ) , f ( A [ j ˉ ] , A 1 [ j ˉ ] ) ) up^a\left(up^b\left(A[\bar{j}],\operatorname{vector}(A_1[\bar{j}]),\operatorname{ind}^{\varphi}(\eta)\right),f\left(A[\bar{j}],A_1[\bar{j}]\right)\right) u p a ( u p b ( A [ j ˉ ] , vector ( A 1 [ j ˉ ]) , ind φ ( η ) ) , f ( A [ j ˉ ] , A 1 [ j ˉ ] ) ) . The proof-tree derivation is given in Section A.2.
Translating Hadamard and QFT Operations
The translations of Hadamard and QFT operations to HLIR are similar; it suffices to demonstrate the Hadamard operation translation. We list the proof rule below.
{ η ⊞ η ′ : EN ( n ) } ⊢ { η ⊞ η ′ ↦ q } η ← H { η ⊞ η ′ ↦ ⟦ H ⟧ η ′ q } \{\eta\mathbin{\boxplus}\eta':\operatorname{EN}(n)\}\vdash\{\eta\mathbin{\boxplus}\eta'\mapsto q\}\ \eta\mathbin{\leftarrow}H\ \{\eta\mathbin{\boxplus}\eta'\mapsto\llbracket H\rrbracket^{\eta'}q\} { η ⊞ η ′ : EN ( n )} ⊢ { η ⊞ η ′ ↦ q } η ← H { η ⊞ η ′ ↦ [ [ H ] ] η ′ q }
Similar to the oracle function case above, we develop the translation based on a generalized version connecting the pre- and post-conditions P P P , listed below.
{ η ⊞ η ′ : EN ( n ) } ⊢ { P [ ( η ⊞ η ′ ) { η ↦ ⟦ H ⟧ ( η ) } / η ⊞ η ′ ] } η ← H { P } \{\eta\mathbin{\boxplus}\eta':\operatorname{EN}(n)\}\vdash\{P[(\eta\mathbin{\boxplus}\eta')\{\eta\mapsto\llbracket H\rrbracket(\eta)\}/\eta\mathbin{\boxplus}\eta']\}\ \eta\mathbin{\leftarrow}H\ \{P\} { η ⊞ η ′ : EN ( n )} ⊢ { P [( η ⊞ η ′ ) { η ↦ [ [ H ] ] ( η )} / η ⊞ η ′ ]} η ← H { P }
We first show the effect of the operation. The operation’s Qafny type rule, σ [ η ⊞ η ′ ↦ EN ( n ) ] ⊢ η ← H ⊣ σ [ η ⊞ η ′ ↦ EN ( n + 1 ) ] \sigma[\eta\mathbin{\boxplus}\eta'\mapsto\operatorname{EN}(n)]\vdash\eta\mathbin{\leftarrow}H\mathbin{\dashv}\sigma[\eta\mathbin{\boxplus}\eta'\mapsto\operatorname{EN}(n+1)] σ [ η ⊞ η ′ ↦ EN ( n )] ⊢ η ← H ⊣ σ [ η ⊞ η ′ ↦ EN ( n + 1 )] , indicates that a Hadamard operation lifts an n n n dimensional array A A A to n + 1 n+1 n + 1 dimensional. An example is to apply x [ 0 , 2 ) ← H x[0,2)\mathbin{\leftarrow}H x [ 0 , 2 ) ← H to a EN ( 1 ) \operatorname{EN}(1) EN ( 1 ) typed state with the locus x [ 0 , 3 ) x[0,3) x [ 0 , 3 ) . The quantum datum is shown on the left below, with its one-dimensional array view on the right.
1 2 ( ∣ 000 ⟩ + ∣ 111 ⟩ ) [ ( 1 2 , ∣ 000 ⟩ ) , ( 1 2 , ∣ 111 ⟩ ) ] \frac{1}{\sqrt{2}}\left(|000\rangle+|111\rangle\right)
\qquad
\left[\left(\frac{1}{\sqrt{2}},|000\rangle\right),\left(\frac{1}{\sqrt{2}},|111\rangle\right)\right] 2 1 ( ∣000 ⟩ + ∣111 ⟩ ) [ ( 2 1 , ∣000 ⟩ ) , ( 2 1 , ∣111 ⟩ ) ]
To construct a proof in Hoare logic, we can split the operation into three steps, (copyN, enumN, and ampN), on the first two bits, corresponding to the locus fragment x [ 0 , 2 ) x[0,2) x [ 0 , 2 ) . Below are their transitions.
[ ( 1 2 , ∣ 000 ⟩ ) , ( 1 2 , ∣ 111 ⟩ ) ] → c o p y N [ [ ( 1 2 , ∣ 000 ⟩ ) , ( 1 2 , ∣ 000 ⟩ ) , ( 1 2 , ∣ 000 ⟩ ) , ( 1 2 , ∣ 000 ⟩ ) ] , [ ( 1 2 , ∣ 111 ⟩ ) , ( 1 2 , ∣ 111 ⟩ ) , ( 1 2 , ∣ 111 ⟩ ) , ( 1 2 , ∣ 111 ⟩ ) ] ] \left[\left(\frac{1}{\sqrt{2}},|000\rangle\right),\left(\frac{1}{\sqrt{2}},|111\rangle\right)\right]
\xrightarrow{\mathrm{copyN}}
\left[
\left[
\left(\frac{1}{\sqrt{2}},|000\rangle\right),\left(\frac{1}{\sqrt{2}},|000\rangle\right),\left(\frac{1}{\sqrt{2}},|000\rangle\right),\left(\frac{1}{\sqrt{2}},|000\rangle\right)
\right],
\left[
\left(\frac{1}{\sqrt{2}},|111\rangle\right),\left(\frac{1}{\sqrt{2}},|111\rangle\right),\left(\frac{1}{\sqrt{2}},|111\rangle\right),\left(\frac{1}{\sqrt{2}},|111\rangle\right)
\right]
\right] [ ( 2 1 , ∣000 ⟩ ) , ( 2 1 , ∣111 ⟩ ) ] copyN [ [ ( 2 1 , ∣000 ⟩ ) , ( 2 1 , ∣000 ⟩ ) , ( 2 1 , ∣000 ⟩ ) , ( 2 1 , ∣000 ⟩ ) ] , [ ( 2 1 , ∣111 ⟩ ) , ( 2 1 , ∣111 ⟩ ) , ( 2 1 , ∣111 ⟩ ) , ( 2 1 , ∣111 ⟩ ) ] ]
[ [ ( 1 2 , ∣ 000 ⟩ ) , ( 1 2 , ∣ 000 ⟩ ) , ( 1 2 , ∣ 000 ⟩ ) , ( 1 2 , ∣ 000 ⟩ ) ] , [ ( 1 2 , ∣ 111 ⟩ ) , ( 1 2 , ∣ 111 ⟩ ) , ( 1 2 , ∣ 111 ⟩ ) , ( 1 2 , ∣ 111 ⟩ ) ] ] → e n u m N [ [ ( 1 2 , ∣ 000 ⟩ ) , ( 1 2 , ∣ 100 ⟩ ) , ( 1 2 , ∣ 010 ⟩ ) , ( 1 2 , ∣ 110 ⟩ ) ] , [ ( 1 2 , ∣ 001 ⟩ ) , ( 1 2 , ∣ 101 ⟩ ) , ( 1 2 , ∣ 011 ⟩ ) , ( 1 2 , ∣ 111 ⟩ ) ] ] \left[
\left[
\left(\frac{1}{\sqrt{2}},|000\rangle\right),\left(\frac{1}{\sqrt{2}},|000\rangle\right),\left(\frac{1}{\sqrt{2}},|000\rangle\right),\left(\frac{1}{\sqrt{2}},|000\rangle\right)
\right],
\left[
\left(\frac{1}{\sqrt{2}},|111\rangle\right),\left(\frac{1}{\sqrt{2}},|111\rangle\right),\left(\frac{1}{\sqrt{2}},|111\rangle\right),\left(\frac{1}{\sqrt{2}},|111\rangle\right)
\right]
\right]
\xrightarrow{\mathrm{enumN}}
\left[
\left[
\left(\frac{1}{\sqrt{2}},|000\rangle\right),\left(\frac{1}{\sqrt{2}},|100\rangle\right),\left(\frac{1}{\sqrt{2}},|010\rangle\right),\left(\frac{1}{\sqrt{2}},|110\rangle\right)
\right],
\left[
\left(\frac{1}{\sqrt{2}},|001\rangle\right),\left(\frac{1}{\sqrt{2}},|101\rangle\right),\left(\frac{1}{\sqrt{2}},|011\rangle\right),\left(\frac{1}{\sqrt{2}},|111\rangle\right)
\right]
\right] [ [ ( 2 1 , ∣000 ⟩ ) , ( 2 1 , ∣000 ⟩ ) , ( 2 1 , ∣000 ⟩ ) , ( 2 1 , ∣000 ⟩ ) ] , [ ( 2 1 , ∣111 ⟩ ) , ( 2 1 , ∣111 ⟩ ) , ( 2 1 , ∣111 ⟩ ) , ( 2 1 , ∣111 ⟩ ) ] ] enumN [ [ ( 2 1 , ∣000 ⟩ ) , ( 2 1 , ∣100 ⟩ ) , ( 2 1 , ∣010 ⟩ ) , ( 2 1 , ∣110 ⟩ ) ] , [ ( 2 1 , ∣001 ⟩ ) , ( 2 1 , ∣101 ⟩ ) , ( 2 1 , ∣011 ⟩ ) , ( 2 1 , ∣111 ⟩ ) ] ]
[ [ ( 1 2 ( − 1 ) ( 00 ) ⋅ ( 00 ) 2 , ∣ 000 ⟩ ) , ( 1 2 ( − 1 ) ( 00 ) ⋅ ( 10 ) 2 , ∣ 100 ⟩ ) , ( 1 2 ( − 1 ) ( 00 ) ⋅ ( 01 ) 2 , ∣ 010 ⟩ ) , ( 1 2 ( − 1 ) ( 00 ) ⋅ ( 11 ) 2 , ∣ 110 ⟩ ) ] , [ ( 1 2 ( − 1 ) ( 11 ) ⋅ ( 00 ) 2 , ∣ 001 ⟩ ) , ( 1 2 ( − 1 ) ( 11 ) ⋅ ( 10 ) 2 , ∣ 101 ⟩ ) , ( 1 2 ( − 1 ) ( 11 ) ⋅ ( 01 ) 2 , ∣ 011 ⟩ ) , ( 1 2 ( − 1 ) ( 11 ) ⋅ ( 11 ) 2 , ∣ 111 ⟩ ) ] ] \left[
\left[
\left(\frac{1}{\sqrt{2}}\frac{(-1)^{(00)\cdot(00)}}{2},|000\rangle\right),
\left(\frac{1}{\sqrt{2}}\frac{(-1)^{(00)\cdot(10)}}{2},|100\rangle\right),
\left(\frac{1}{\sqrt{2}}\frac{(-1)^{(00)\cdot(01)}}{2},|010\rangle\right),
\left(\frac{1}{\sqrt{2}}\frac{(-1)^{(00)\cdot(11)}}{2},|110\rangle\right)
\right],
\left[
\left(\frac{1}{\sqrt{2}}\frac{(-1)^{(11)\cdot(00)}}{2},|001\rangle\right),
\left(\frac{1}{\sqrt{2}}\frac{(-1)^{(11)\cdot(10)}}{2},|101\rangle\right),
\left(\frac{1}{\sqrt{2}}\frac{(-1)^{(11)\cdot(01)}}{2},|011\rangle\right),
\left(\frac{1}{\sqrt{2}}\frac{(-1)^{(11)\cdot(11)}}{2},|111\rangle\right)
\right]
\right] [ [ ( 2 1 2 ( − 1 ) ( 00 ) ⋅ ( 00 ) , ∣000 ⟩ ) , ( 2 1 2 ( − 1 ) ( 00 ) ⋅ ( 10 ) , ∣100 ⟩ ) , ( 2 1 2 ( − 1 ) ( 00 ) ⋅ ( 01 ) , ∣010 ⟩ ) , ( 2 1 2 ( − 1 ) ( 00 ) ⋅ ( 11 ) , ∣110 ⟩ ) ] , [ ( 2 1 2 ( − 1 ) ( 11 ) ⋅ ( 00 ) , ∣001 ⟩ ) , ( 2 1 2 ( − 1 ) ( 11 ) ⋅ ( 10 ) , ∣101 ⟩ ) , ( 2 1 2 ( − 1 ) ( 11 ) ⋅ ( 01 ) , ∣011 ⟩ ) , ( 2 1 2 ( − 1 ) ( 11 ) ⋅ ( 11 ) , ∣111 ⟩ ) ] ]
The copyN step turns each array element into an array of 2 ∣ x [ 0 , 2 ) ∣ 2^{|x[0,2)|} 2 ∣ x [ 0 , 2 ) ∣ (4) elements by copying the original element, i.e., for the element ( 1 2 , ∣ 000 ⟩ ) \left(\frac{1}{\sqrt{2}},|000\rangle\right) ( 2 1 , ∣000 ⟩ ) , we make an array of 4 copied elements. This step also turns the array dimension from one (typed EN ( 1 ) \operatorname{EN}(1) EN ( 1 ) ) to two (typed EN ( 2 ) \operatorname{EN}(2) EN ( 2 ) ). For the first two bits of each element vector in the inner array, enumN enumerates from 0 to 2 ∣ x [ 0 , 2 ) ∣ − 1 2^{|x[0,2)|}-1 2 ∣ x [ 0 , 2 ) ∣ − 1 in binary. As shown above, we change the first two bits at each basis-vector position to 00 (integer 0), 10 (1), 01 (2), and 11 (3) for each inner array. ampN multiplies each amplitude position by two pieces. We first multiply by 1 ( 2 ) ∣ x [ 0 , 2 ) ∣ \frac{1}{(\sqrt{2})^{|x_{[0,2)}|}} ( 2 ) ∣ x [ 0 , 2 ) ∣ 1 , and then by a phase ( − 1 ) o l d ⋅ n e w (-1)^{\mathrm{old}\cdot\mathrm{new}} ( − 1 ) old ⋅ new , where old and new refer to the first two bits of element vectors before and after applying enumN, respectively, and ⋅ \cdot ⋅ is the dot product. QFTs are similar to Hadamard operations, with the addition of phase multiplication.
Validity Proof. Below, we present the valid Hoare triple definition as a lemma for Hadamard operations, followed by the proofs for individual operations.
Lemma 7.4 (Valid Hadamard Application). Given a quantum state φ \varphi φ , an operation η ← H \eta\leftarrow\mathrm{H} η ← H in the locus η ⊢ η ′ \eta\mathbin{\vdash} \eta' η ⊢ η ′ with the E N ( n ) \mathrm{EN}(n) EN ( n ) -typed quantum datum φ ( η ⊢ η ′ ) \varphi(\eta\mathbin{\vdash} \eta') φ ( η ⊢ η ′ ) , transformed to an n n n -dimensional array A A A , with ind φ ( η ⊢ η ′ ) \operatorname{ind}^{\varphi}(\eta\mathbin{\vdash} \eta') ind φ ( η ⊢ η ′ ) and lens ( A ) = m ‾ \operatorname{lens}(A)=\overline{m} lens ( A ) = m , the result array has n + 1 n+1 n + 1 dimension, with the inner array bound 2 ∣ η ∣ 2^{|\eta|} 2 ∣ η ∣ . The valid Hoare logic triple is given as follows.
let E : = [ ∀ k ∈ [ 0 , 2 ∣ η ∣ ) . up a ( up b ( A [ j ‾ ] , k , ind φ ( η ) ) , ( − 1 ) k ⋅ vector ( A [ j ‾ ] ) [ ind φ ( η ) ] ) ] in \operatorname{let} E := [\forall k \in[0,2^{|\eta|}).\operatorname{up}^{a}(\operatorname{up}^{b}(A[\overline{j}],k,\operatorname{ind}^{\varphi}(\eta)),(-1)^{k\cdot\operatorname{vector}(A[\overline{j}])[\operatorname{ind}^{\varphi}(\eta)])]\ \operatorname{in}} let E := [ ∀ k ∈ [ 0 , 2 ∣ η ∣ ) . up a ( up b ( A [ j ] , k , ind φ ( η )) , ( − 1 ) k ⋅ vector ( A [ j ]) [ ind φ ( η )])] in { P [ A { ∀ j ‾ ∈ range ( m ‾ ) . j ‾ ↦ E } / A ] ∧ Q len ( A , m ‾ ) ∧ Q len ( A { ∀ j ‾ ∈ range ( m ‾ ) . j ‾ ↦ E } , ( m ‾ , 2 ∣ η ∣ ) ) } \{P[A\{\forall\overline{j}\in\operatorname{range}(\overline{m}).\overline{j}\mapsto E\}/A]\land Q_{\operatorname{len}}(A,\overline{m})\land Q_{\operatorname{len}}(A\{\forall\overline{j}\in\operatorname{range}(\overline{m}).\overline{j}\mapsto E\},(\overline{m},2^{|\eta|}))\} { P [ A { ∀ j ∈ range ( m ) . j ↦ E } / A ] ∧ Q len ( A , m ) ∧ Q len ( A { ∀ j ∈ range ( m ) . j ↦ E } , ( m , 2 ∣ η ∣ ))} A 1 : = copyN ( A , m ‾ , ind φ ( η ) ) ; A 2 : = enumN ( A 1 , m ‾ , ind φ ( η ) ) ; A 3 : = ampN ( A 1 , A 2 , m ‾ , ind φ ( η ) ) ; A : = A 3 A_{1}:=\operatorname{copyN}(A,\overline{m},\operatorname{ind}^{\varphi}(\eta));\ A_{2}:=\operatorname{enumN}(A_{1},\overline{m},\operatorname{ind}^{\varphi}(\eta));\ A_{3}:=\operatorname{ampN}(A_{1},A_{2},\overline{m},\operatorname{ind}^{\varphi}(\eta));\ A:=A_{3} A 1 := copyN ( A , m , ind φ ( η )) ; A 2 := enumN ( A 1 , m , ind φ ( η )) ; A 3 := ampN ( A 1 , A 2 , m , ind φ ( η )) ; A := A 3 { P ∧ Q len ( A , m ‾ ) ∧ Q len ( A { ∀ j ‾ ∈ range ( m ‾ ) . j ‾ ↦ E } , ( m ‾ , 2 ∣ η ∣ ) ) } \{P\land Q_{\operatorname{len}}(A,\overline{m})\land Q_{\operatorname{len}}(A\{\forall\overline{j}\in\operatorname{range}(\overline{m}).\overline{j}\mapsto E\},(\overline{m},2^{|\eta|}))\} { P ∧ Q len ( A , m ) ∧ Q len ( A { ∀ j ∈ range ( m ) . j ↦ E } , ( m , 2 ∣ η ∣ ))}
Proof. The validity proof below comprises the results for copyN, enumN, and ampN. Its mechanized proof is in Rocq. □
We now demonstrate the definitions and proofs for the three key IR operations. We first show the valid Hoare triple for copyN.
Lemma 7.5 (The Valid copyN Hoare Triple). Given a locus η ′ ⊢ η \eta' \mathbin{\vdash} \eta η ′ ⊢ η , an n n n dimensional array A A A , with the bounds m ‾ \overline{m} m , and the valid Hoare triple of copyN( A , m ‾ , ind φ ( η ) ) (A,\overline{m},\operatorname{ind}^{\varphi}(\eta)) ( A , m , ind φ ( η )) is listed below with its implementation. We assume that the result array A 1 A_{1} A 1 is initialized to have n + 1 n+1 n + 1 dimensions, with the additional dimension bounded by 2 ∣ η ∣ 2^{|\eta|} 2 ∣ η ∣ .
{ P [ A 1 { ∀ j ‾ ∈ range ( m ‾ ) . ∀ k ∈ [ 0 , 2 ∣ η ∣ ) . ( j ‾ , k ) ↦ A [ j ‾ ] / A 1 ] ∧ Q len ( A , m ‾ ) ∧ Q len ( A 1 , ( m ‾ , 2 ∣ η ∣ ) ) } \{P[A_{1}\{\forall\overline{j}\in\operatorname{range}(\overline{m}).\forall k\in[0,2^{|\eta|}).(\overline{j},k)\mapsto A[\overline{j}]/A_{1}]\land Q_{\operatorname{len}}(A,\overline{m})\land Q_{\operatorname{len}}(A_{1},(\overline{m},2^{|\eta|}))\} { P [ A 1 { ∀ j ∈ range ( m ) .∀ k ∈ [ 0 , 2 ∣ η ∣ ) . ( j , k ) ↦ A [ j ] / A 1 ] ∧ Q len ( A , m ) ∧ Q len ( A 1 , ( m , 2 ∣ η ∣ ))} A 1 : = copyN ( A , m ‾ , ind φ ( η ) ) A_{1}:=\operatorname{copyN}(A,\overline{m},\operatorname{ind}^{\varphi}(\eta)) A 1 := copyN ( A , m , ind φ ( η )) { P ∧ Q len ( A , m ‾ ) ∧ Q len ( A 1 , ( m ‾ , 2 ∣ η ∣ ) ) } \{P\land Q_{\operatorname{len}}(A,\overline{m})\land Q_{\operatorname{len}}(A_{1},(\overline{m},2^{|\eta|}))\} { P ∧ Q len ( A , m ) ∧ Q len ( A 1 , ( m , 2 ∣ η ∣ ))}
where copyN( A , m ‾ , ind φ ( η ) ) (A,\overline{m},\operatorname{ind}^{\varphi}(\eta)) ( A , m , ind φ ( η )) is defined as: mapN( A , m ‾ , λ ( x , y ) . copy ( 2 ∣ η ∣ , ( x , y ) ) ) (A,\overline{m},\lambda(x,y).\operatorname{copy}(2^{|\eta|},(x,y))) ( A , m , λ ( x , y ) . copy ( 2 ∣ η ∣ , ( x , y ))) ; copy( N , v ) (N,v) ( N , v ) is a function to generate an array of N N N copies of the element v v v .
Proof. By Lemma 2.1 , it is enough to show the validity of the following triple.
{ Q len ( A , m ‾ ) ∧ Q len ( A 1 , ( m ‾ , 2 ∣ η ∣ ) ) } \{Q_{\operatorname{len}}(A,\overline{m})\land Q_{\operatorname{len}}(A_{1},(\overline{m},2^{|\eta|}))\} { Q len ( A , m ) ∧ Q len ( A 1 , ( m , 2 ∣ η ∣ ))}
A 1 : = mapN ( A , m ‾ , λ ( x , y ) . copy ( 2 ∣ η ∣ , ( x , y ) ) ) A_{1}:=\operatorname{mapN}(A,\overline{m},\lambda(x,y).\operatorname{copy}(2^{|\eta|},(x,y))) A 1 := mapN ( A , m , λ ( x , y ) . copy ( 2 ∣ η ∣ , ( x , y )))
{ ∀ j ‾ ∈ range ( m ‾ ) . ∀ k ∈ [ 0 , 2 ∣ η ∣ ) . A 1 [ j ‾ , k ] = A [ j ‾ ] ∧ Q len ( A 1 , m ‾ , 2 ∣ η ∣ ) ∧ Q len ( A , m ‾ ) ∧ Q len ( A 1 , ( m ‾ , 2 ∣ η ∣ ) ) } \{\forall\overline{j}\in\operatorname{range}(\overline{m}).\forall k\in[0,2^{|\eta|}).A_{1}[\overline{j},k]=A[\overline{j}]\land Q_{\operatorname{len}}(A_{1},\overline{m},2^{|\eta|})\land Q_{\operatorname{len}}(A,\overline{m})\land Q_{\operatorname{len}}(A_{1},(\overline{m},2^{|\eta|}))\} { ∀ j ∈ range ( m ) .∀ k ∈ [ 0 , 2 ∣ η ∣ ) . A 1 [ j , k ] = A [ j ] ∧ Q len ( A 1 , m , 2 ∣ η ∣ ) ∧ Q len ( A , m ) ∧ Q len ( A 1 , ( m , 2 ∣ η ∣ ))}
The proof is given by deriving a proof tree and using a single operation mapN. Its details are mechanized in Rocq. □
We now present the proof of enumN below, where we construct an n + 1 n+1 n + 1 dimensional array (A 2 A_{2} A 2 ) based on A 1 A_{1} A 1 , by replacing the vector in the locus η \eta η with the enumeration from 0 to 2 ∣ η ∣ 2^{|\eta|} 2 ∣ η ∣ .
Lemma 7.6 (The Valid EnumN Hoare Triple). Given a locus η ′ ⊢ η \eta' \mathbin{\vdash} \eta η ′ ⊢ η , an n + 1 n+1 n + 1 dimensional array A 1 A_{1} A 1 , with the bounds ( m ‾ , 2 ∣ η ∣ ) (\overline{m},2^{|\eta|}) ( m , 2 ∣ η ∣ ) , the valid Hoare triple of enumN( A 1 , ( m ‾ , 2 ∣ η ∣ ) , ind φ ( η ) ) (A_{1},(\overline{m},2^{|\eta|}),\operatorname{ind}^{\varphi}(\eta)) ( A 1 , ( m , 2 ∣ η ∣ ) , ind φ ( η )) is listed below with its implementation. The result array A 2 A_{2} A 2 is initialized as an n + 1 n+1 n + 1 dimensional array. j ‾ , k \overline{j},k j , k has the first n n n indices j ‾ \overline{j} j and the most inner index k k k .
{ P [ A 2 { ∀ j ⃗ ∈ range ( m ‾ ) . ∀ k ∈ [ 0 , 2 ∣ η ∣ ) . ( j ⃗ , k ) ↦ u p b ( A 1 [ j ⃗ , k ] , k , ind φ ( η ) ) } / A 2 ] ∧ Q len ( A 1 , A 2 , ( m ‾ , 2 ∣ η ∣ ) ) } \left\{P\left[A_{2}\left\{\forall\vec{j}\in\operatorname{range}(\overline{m}).\forall k\in[0,2^{|\eta|}).(\vec{j},k)\mapsto u p^{b}(A_{1}[\vec{j},k],k,\operatorname{ind}^{\varphi}(\eta))\right\}/A_{2}\right]\land Q_{\operatorname{len}}(A_{1},A_{2},(\overline{m},2^{|\eta|}))\right\} { P [ A 2 { ∀ j ∈ range ( m ) .∀ k ∈ [ 0 , 2 ∣ η ∣ ) . ( j , k ) ↦ u p b ( A 1 [ j , k ] , k , ind φ ( η )) } / A 2 ] ∧ Q len ( A 1 , A 2 , ( m , 2 ∣ η ∣ )) }
enumN ( A 1 , ( m ‾ , 2 ∣ η ∣ ) , ind φ ( η ) ) \operatorname{enumN}(A_{1},(\overline{m},2^{|\eta|}),\operatorname{ind}^{\varphi}(\eta)) enumN ( A 1 , ( m , 2 ∣ η ∣ ) , ind φ ( η ))
{ P ∧ Q len ( A 1 , A 2 , ( m ‾ , 2 ∣ η ∣ ) ) } \left\{P\land Q_{\operatorname{len}}(A_{1},A_{2},(\overline{m},2^{|\eta|}))\right\} { P ∧ Q len ( A 1 , A 2 , ( m , 2 ∣ η ∣ )) }
where enumN is defined as follows; enum(2 ∣ η ∣ 2^{|\eta|} 2 ∣ η ∣ ) produces an array listing all numbers in [ 0 , 2 ∣ η ∣ ) [0,2^{|\eta|}) [ 0 , 2 ∣ η ∣ ) .
A a : = locateN ( A 1 , m ‾ , ind φ ( η ) ) ; A_{a}:=\operatorname{locateN}(A_{1},\overline{m},\operatorname{ind}^{\varphi}(\eta)); A a := locateN ( A 1 , m , ind φ ( η )) ;
A b : = mapN ( A a , m ‾ , λ ( x , y ) . ( x , enum ( 2 ∣ η ∣ ) ) ) ; A_{b}:=\operatorname{mapN}(A_{a},\overline{m},\lambda(x,y).(x,\operatorname{enum}(2^{|\eta|}))); A b := mapN ( A a , m , λ ( x , y ) . ( x , enum ( 2 ∣ η ∣ ))) ;
A 2 : = assignN ( A 1 , A b , m ‾ , ind φ ( η ) , λ u v . amp ( u ) ) A_{2}:=\operatorname{assignN}(A_{1},A_{b},\overline{m},\operatorname{ind}^{\varphi}(\eta),\lambda u\,v.\operatorname{amp}(u)) A 2 := assignN ( A 1 , A b , m , ind φ ( η ) , λ u v . amp ( u ))
Proof. By Lemma 2.1, it is enough to show the validity of the following triple.
{ Q len ( A 1 , A 2 , ( m ‾ , 2 ∣ η ∣ ) ) } \left\{Q_{\operatorname{len}}(A_{1},A_{2},(\overline{m},2^{|\eta|}))\right\} { Q len ( A 1 , A 2 , ( m , 2 ∣ η ∣ )) }
enumN ( A 1 , ( m ‾ , 2 ∣ η ∣ ) , ind φ ( η ) ) \operatorname{enumN}(A_{1},(\overline{m},2^{|\eta|}),\operatorname{ind}^{\varphi}(\eta)) enumN ( A 1 , ( m , 2 ∣ η ∣ ) , ind φ ( η ))
{ ∀ j ⃗ ∈ range ( m ‾ ) . ∀ k ∈ [ 0 , 2 ∣ η ∣ ) . A 2 [ j ⃗ , k ] = u p b ( A 1 [ j ⃗ , k ] , k , ind φ ( η ) ) ∧ Q len ( A 1 , A 2 , ( m ‾ , 2 ∣ η ∣ ) ) } \left\{\forall\vec{j}\in\operatorname{range}(\overline{m}).\forall k\in[0,2^{|\eta|}).A_{2}[\vec{j},k]=u p^{b}(A_{1}[\vec{j},k],k,\operatorname{ind}^{\varphi}(\eta))\land Q_{\operatorname{len}}(A_{1},A_{2},(\overline{m},2^{|\eta|}))\right\} { ∀ j ∈ range ( m ) .∀ k ∈ [ 0 , 2 ∣ η ∣ ) . A 2 [ j , k ] = u p b ( A 1 [ j , k ] , k , ind φ ( η )) ∧ Q len ( A 1 , A 2 , ( m , 2 ∣ η ∣ )) }
The proof-tree derivation is given in Section A.3. □
We now demonstrate the validity of the Hoare triple for ampN, and it suffices to show the n n n -dimensional case. Here, we generate an n n n -dimensional array A 3 A_{3} A 3 , having the same basis-vectors as A 1 A_{1} A 1 , but multiplying its amp elements by a phase relating the amp elements of A 1 A_{1} A 1 and A 2 A_{2} A 2 .
Lemma 7.7 (The ampN Proof). Given a locus η ′ ⊣ η \eta' \mathrel{\dashv} \eta η ′ ⊣ η , two n n n dimensional arrays A 1 A_{1} A 1 and A 2 A_{2} A 2 , with the bounds of each dimension to be m ‾ \overline{m} m , and the valid Hoare triple of the ampN( A 1 , A 2 , m ‾ , ind φ ( η ) ) (A_{1},A_{2},\overline{m},\operatorname{ind}^{\varphi}(\eta)) ( A 1 , A 2 , m , ind φ ( η )) function is listed below. The result array A 3 A_{3} A 3 is initialized to be n n n -dimensional with bounds m ‾ \overline{m} m .
{ P [ A 3 { ∀ j ⃗ ∈ range ( m ‾ ) . j ⃗ ↦ u p a ( u p b ( A 1 [ j ⃗ ] , vector ( A 2 [ j ⃗ ] ) [ ind φ ( η ) ] , ind φ ( η ) ) , ( − 1 ) vector ( A 1 [ j ⃗ ] ) [ ind φ ( η ) ] ⋅ vector ( A 2 [ j ⃗ ] ) [ ind φ ( η ) ] ) } / A 3 ] ∧ Q len ( A 1 , A 2 , A 3 , ( m ‾ , 2 ∣ η ∣ ) ) } \left\{P\left[A_{3}\left\{\forall\vec{j}\in\operatorname{range}(\overline{m}).\vec{j}\mapsto u p^{a}\left(u p^{b}(A_{1}[\vec{j}],\operatorname{vector}(A_{2}[\vec{j}])[\operatorname{ind}^{\varphi}(\eta)],\operatorname{ind}^{\varphi}(\eta)),(-1)^{\operatorname{vector}(A_{1}[\vec{j}])[\operatorname{ind}^{\varphi}(\eta)]\cdot\operatorname{vector}(A_{2}[\vec{j}])[\operatorname{ind}^{\varphi}(\eta)]}\right)\right\}/A_{3}\right]\land Q_{\operatorname{len}}(A_{1},A_{2},A_{3},(\overline{m},2^{|\eta|}))\right\} { P [ A 3 { ∀ j ∈ range ( m ) . j ↦ u p a ( u p b ( A 1 [ j ] , vector ( A 2 [ j ]) [ ind φ ( η )] , ind φ ( η )) , ( − 1 ) vector ( A 1 [ j ]) [ ind φ ( η )] ⋅ vector ( A 2 [ j ]) [ ind φ ( η )] ) } / A 3 ] ∧ Q len ( A 1 , A 2 , A 3 , ( m , 2 ∣ η ∣ )) } A 3 : = ampN ( A 1 , A 2 , m ‾ , ind φ ( η ) ) A_{3}:=\operatorname{ampN}(A_{1},A_{2},\overline{m},\operatorname{ind}^{\varphi}(\eta)) A 3 := ampN ( A 1 , A 2 , m , ind φ ( η )) { P ∧ Q len ( A 1 , A 2 , A 3 , ( m ‾ , 2 ∣ η ∣ ) ) } \left\{P\land Q_{\operatorname{len}}(A_{1},A_{2},A_{3},(\overline{m},2^{|\eta|}))\right\} { P ∧ Q len ( A 1 , A 2 , A 3 , ( m , 2 ∣ η ∣ )) } where ampN( A 1 , A 2 , m ‾ , ind φ ( η ) ) (A_{1},A_{2},\overline{m},\operatorname{ind}^{\varphi}(\eta)) ( A 1 , A 2 , m , ind φ ( η )) is defined as:
assignN ( A 1 , A 2 , m ‾ , ind φ ( η ) , λ u v . amp ( v ) ∗ ( − 1 ) vector ( u ) [ ind φ ( η ) ] ⋅ vector ( v ) [ ind φ ( η ) ] ) \operatorname{assignN}(A_{1},A_{2},\overline{m},\operatorname{ind}^{\varphi}(\eta),\lambda u\,v.\operatorname{amp}(v)*(-1)^{\operatorname{vector}(u)[\operatorname{ind}^{\varphi}(\eta)]\cdot\operatorname{vector}(v)[\operatorname{ind}^{\varphi}(\eta)]}) assignN ( A 1 , A 2 , m , ind φ ( η ) , λ u v . amp ( v ) ∗ ( − 1 ) vector ( u ) [ ind φ ( η )] ⋅ vector ( v ) [ ind φ ( η )] ) Proof. The proof is given as deriving a proof tree and building on top of assignN. The detail is mechanized in Rocq. □
Translating Quantum Conditionals
We now present the translation of a quantum conditional, whose proof and type rules in Qafny are given below and contain two predicate transformers, F \mathcal{F} F and U \mathcal{U} U .
F V ( b ) = η { η ′ : E N ( n ) } ⊢ φ [ F ( b , η , η ′ ) / η ∣ η ′ ] μ { φ ′ } { η ⊣ η ′ : E N ( n ) } ⊢ { φ } i f ( b ) μ { φ [ U ( ¬ b , η , η ′ ⊣ η ) / η ∣ η ′ ] ∗ φ ′ [ U ( b , η , η ′ ⊣ η ′ ) / η ] } \begin{aligned}
\mathrm{FV}(b)&=\eta\\
\{\eta':\mathrm{EN}(n)\}\vdash\varphi[\mathcal{F}(b,\eta,\eta')/\eta\mid\eta']\ \mu\{\varphi'\}\\
\{\eta\mathrel{\dashv}\eta':\mathrm{EN}(n)\}\vdash\{\varphi\}\ \mathrm{if}\ (b)\ \mu\ \{\varphi[\mathcal{U}(\neg b,\eta,\eta'\mathrel{\dashv}\eta)/\eta\mid\eta']*\varphi'[\mathcal{U}(b,\eta,\eta'\mathrel{\dashv}\eta')/\eta]\}
\end{aligned} FV ( b ) { η ′ : EN ( n )} ⊢ φ [ F ( b , η , η ′ ) / η ∣ η ′ ] μ { φ ′ } { η ⊣ η ′ : EN ( n )} ⊢ { φ } if ( b ) μ { φ [ U ( ¬ b , η , η ′ ⊣ η ) / η ∣ η ′ ] ∗ φ ′ [ U ( b , η , η ′ ⊣ η ′ ) / η ]} = η
F V ( b ) = η F V ( e ) ⊆ η ′ σ ⊎ { η ′ : E N ( n ) } ⊢ e ▹ σ ⊎ { η ′ : E N ( n ′ ) } σ ⊎ { η ⊣ η ′ : E N ( n ) } ⊢ i f ( b ) e ▹ σ ⊎ { η ⊣ η ′ : E N ( n ′ ) } \begin{aligned}
\mathrm{FV}(b)&=\eta\qquad\mathrm{FV}(e)\subseteq\eta' \qquad\sigma\mathbin{\uplus}\{\eta':\mathrm{EN}(n)\}\vdash e\triangleright\sigma\mathbin{\uplus}\{\eta':\mathrm{EN}(n')\}\\
\sigma\mathbin{\uplus}\{\eta\mathrel{\dashv}\eta':\mathrm{EN}(n)\}\vdash\mathrm{if}\ (b)\ e\triangleright\sigma\mathbin{\uplus}\{\eta\mathrel{\dashv}\eta':\mathrm{EN}(n')\}
\end{aligned} FV ( b ) σ ⊎ { η ⊣ η ′ : EN ( n )} ⊢ if ( b ) e ▹ σ ⊎ { η ⊣ η ′ : EN ( n ′ )} = η FV ( e ) ⊆ η ′ σ ⊎ { η ′ : EN ( n )} ⊢ e ▹ σ ⊎ { η ′ : EN ( n ′ )}
In Rule P-If, the bottom layer selects basis-kets w w w , where ⟦ b ⟧ η ′ ( w ) \llbracket b\rrbracket^{\eta'}(w) [ [ b ] ] η ′ ( w ) is true and trims the basis-ket states to the locus region η ′ \eta' η ′ . With the shrunk basis-ket states, the top level applies the μ \mu μ operation. We first demonstrate the Qafny proof via the Bell pair example.
Example 7.8 (The Quantum Conditional Example in Bell Pair). The Bell pair circuit (§ 2 ) applies a Hadamard to qubit x [ 0 ] x[0] x [ 0 ] followed by a controlled-not on y [ 0 ] y[0] y [ 0 ] guarded by x [ 0 ] x[0] x [ 0 ] , and e = y [ 0 , 1 ) ← y [ 0 , 1 ) + 1 e = y[0,1) \leftarrow y[0,1) + 1 e = y [ 0 , 1 ) ← y [ 0 , 1 ) + 1 . With pre- and post-conditions, the Qafny triple is:
σ [ y [ 0 , 1 ) ↦ EN ( 1 ) ] ⊢ { y [ 0 , 1 ) : EN ( 1 ) ↦ 1 2 ∣ 1 ⟩ ∣ 0 ⟩ } ⊕ { y [ 0 , 1 ) : EN ( 1 ) ↦ 1 2 ∣ 1 ⟩ ∣ 1 ⟩ } \sigma[y[0,1) \mapsto\operatorname{EN}(1)] \vdash\left\{y[0,1):\operatorname{EN}(1) \mapsto\frac{1}{\sqrt{2}} \lvert1\rangle\lvert0\rangle\right\} \oplus\left\{y[0,1):\operatorname{EN}(1) \mapsto\frac{1}{\sqrt{2}} \lvert1\rangle\lvert1\rangle\right\} σ [ y [ 0 , 1 ) ↦ EN ( 1 )] ⊢ { y [ 0 , 1 ) : EN ( 1 ) ↦ 2 1 ∣ 1 ⟩ ∣ 0 ⟩ } ⊕ { y [ 0 , 1 ) : EN ( 1 ) ↦ 2 1 ∣ 1 ⟩ ∣ 1 ⟩ }
σ ⊢ { x [ 0 , 1 ) ⊞ y [ 0 , 1 ) : EN ( 1 ) ↦ ∑ b = 0 1 1 2 ∣ b ⟩ ∣ 0 ⟩ } i f ( x [ 0 ] ) { e } { x [ 0 , 1 ) ⊞ y [ 0 , 1 ) : EN ( 1 ) ↦ ∑ b = 0 1 1 2 ∣ b ⟩ ∣ b ⟩ } \sigma\vdash\left\{x[0,1) \boxplus y[0,1):\operatorname{EN}(1) \mapsto\sum_{b=0}^{1} \frac{1}{\sqrt{2}}\lvert b\rangle\lvert0\rangle\right\}\ \mathrm{if}\ (x[0])\ \{e\}\ \left\{x[0,1) \boxplus y[0,1):\operatorname{EN}(1) \mapsto\sum_{b=0}^{1} \frac{1}{\sqrt{2}}\lvert b\rangle\lvert b\rangle\right\} σ ⊢ { x [ 0 , 1 ) ⊞ y [ 0 , 1 ) : EN ( 1 ) ↦ b = 0 ∑ 1 2 1 ∣ b ⟩ ∣ 0 ⟩ } if ( x [ 0 ]) { e } { x [ 0 , 1 ) ⊞ y [ 0 , 1 ) : EN ( 1 ) ↦ b = 0 ∑ 1 2 1 ∣ b ⟩ ∣ b ⟩ }
The pre-condition is an EN(1) typed entangled state for x [ 0 , 1 ) ⊞ y [ 0 , 1 ) x[0,1) \boxplus y[0,1) x [ 0 , 1 ) ⊞ y [ 0 , 1 ) . The upper-level pre-condition checks x [ 0 , 1 ) x[0,1) x [ 0 , 1 ) ’s basis-ket fragment ∣ b ⟩ \lvert b\rangle ∣ b ⟩ , masks the 1 2 ∣ 0 ⟩ ∣ 0 ⟩ \frac{1}{\sqrt{2}}\lvert0\rangle\lvert0\rangle 2 1 ∣ 0 ⟩ ∣ 0 ⟩ case, and leaves the case of 1 2 ∣ 1 ⟩ ∣ 0 ⟩ \frac{1}{\sqrt{2}}\lvert1\rangle\lvert0\rangle 2 1 ∣ 1 ⟩ ∣ 0 ⟩ , where we hide x [ 0 , 1 ) x[0,1) x [ 0 , 1 ) ’s basis-vector ∣ 1 ⟩ \lvert1\rangle ∣ 1 ⟩ since it is not mentioned in the locus domain. Finally, we assemble the upper level post-condition with ∣ 1 ⟩ \lvert1\rangle ∣ 1 ⟩ and 1 2 ∣ 1 ⟩ ∣ 1 ⟩ \frac{1}{\sqrt{2}}\lvert1\rangle\lvert1\rangle 2 1 ∣ 1 ⟩ ∣ 1 ⟩ .
F V ( b ) = η { η ′ : EN ( n ) } ⊢ { φ [ F ( b , n , η ′ ) / n ⊞ η ′ ] } μ { φ 1 [ F ( b , n , η ′ ) / n ⊞ η ′ ] } { η ⊞ η ′ : EN ( n ) } ⊢ { φ } i f ( b ) μ { φ 1 } FV(b)=\eta\qquad\frac{\left\{\eta' : \operatorname{EN}(n)\right\}\vdash\left\{\varphi[F(b,n,\eta')/n \boxplus\eta']\right\}\ \mu\ \left\{\varphi_1[F(b,n,\eta')/n \boxplus\eta']\right\}}{\left\{\eta\boxplus\eta' : \operatorname{EN}(n)\right\}\vdash\left\{\varphi\right\}\ \mathrm{if}\ (b)\ \mu\ \left\{\varphi_1\right\}} F V ( b ) = η { η ⊞ η ′ : EN ( n ) } ⊢ { φ } if ( b ) μ { φ 1 } { η ′ : EN ( n ) } ⊢ { φ [ F ( b , n , η ′ ) / n ⊞ η ′ ] } μ { φ 1 [ F ( b , n , η ′ ) / n ⊞ η ′ ] }
The key to embedding the proof rule is to connect the upper- and bottom-level (pre- and post-) conditions. To do so, we can think of two program points ∙ \bullet ∙ and ∘ \circ ∘ around the translated expression e e e , where we prepare inserted predicates before ∙ \bullet ∙ to transform P P P to the right form of the upper-level pre-condition and transform ∘ \circ ∘ , representing the upper-level post-condition, to the bottom-level post-condition Q Q Q . We manage the translation as follows.
Lemma 7.9 (The Quantum Conditional Transformation). Given i f ( b ) μ \mathrm{if}\ (b)\ \mu if ( b ) μ , where we do expression translation for μ \mu μ , { η ′ : EN ( n ) } ⊢ μ ▹ { η ′ : EN ( n ′ ) } ≫ e \{\eta' : \operatorname{EN}(n)\} \vdash\mu\triangleright\{\eta' : \operatorname{EN}(n')\} \gg e { η ′ : EN ( n )} ⊢ μ ▹ { η ′ : EN ( n ′ )} ≫ e and pre- and post-condition translations, as { η ⊞ η ′ : EN ( n ) } ⊢ φ ≫ P \{\eta\boxplus\eta' : \operatorname{EN}(n)\} \vdash\varphi\gg P { η ⊞ η ′ : EN ( n )} ⊢ φ ≫ P and { η ⊞ η ′ : EN ( n ′ ) } ⊢ φ ′ ≫ Q \{\eta\boxplus\eta' : \operatorname{EN}(n')\} \vdash\varphi' \gg Q { η ⊞ η ′ : EN ( n ′ )} ⊢ φ ′ ≫ Q ; without losing generality, we assume the locus η ⊞ η ′ \eta\boxplus\eta' η ⊞ η ′ is translated to an n n n dimensional array A A A , such that η ⊞ η ′ ⊢ A \eta\boxplus\eta' \vdash A η ⊞ η ′ ⊢ A , we express the valid Hoare logic triple as follows. t ˉ \bar{t} t ˉ and ( t ˉ , 1 ˉ ) (\bar{t},\bar{1}) ( t ˉ , 1 ˉ ) represent the lengths for an n n n and n ′ n' n ′ (> n >n > n ) dimensional arrays, respectively, where ( t ˉ , 1 ˉ ) (\bar{t},\bar{1}) ( t ˉ , 1 ˉ ) extends t ˉ \bar{t} t ˉ with additional n ′ − n n'-n n ′ − n dimensions with length 1 in each dimension; u ˉ ′ + ( t ˉ , 1 ˉ ) \bar{u}' + (\bar{t},\bar{1}) u ˉ ′ + ( t ˉ , 1 ˉ ) adds the lengths in each dimensions.
{ P ∧ Q l e n ( A , m ‾ ) } \{P \land Q_{\mathrm{len}}(A,\overline{m})\} { P ∧ Q len ( A , m )}
A 1 : = filterN ( A , b , ind φ ( η ) ) ; u ‾ : = ∣ A 1 ∣ ; A 2 : = filterN ( A , ¬ b , ind φ ( η ) ) ; t ‾ : = ∣ A 2 ∣ ; A 3 : = locateN ( A 1 , u ‾ , ind φ ( η ′ ) ) ; e ; ∙ { P [ concat ( upDim ( A 2 , t ‾ , ( t ‾ , 1 ) ) , upDim ( A 1 , u ‾ , ∣ A 3 ∣ ) { ∀ j ∈ range ( ∣ A 3 ∣ ) . j ↦ up a ( up b ( upDim ( A 1 , u ‾ , ∣ A 3 ∣ ) [ j ] , vector ( A 3 [ j ] ) , ind φ ( η ′ ) ) , amp ( A 3 [ j ] ) ) } ) / A ] } ∘ { Q [ concat ( upDim ( A 2 , t ‾ , ( t ‾ , 1 ) ) , upDim ( A 1 , u ‾ , ∣ A 3 ∣ ) { ∀ j ∈ range ( ∣ A 3 ∣ ) . j ↦ up a ( up b ( upDim ( A 1 , u ‾ , ∣ A 3 ∣ ) [ j ] , vector ( A 3 [ j ] ) , ind φ ( η ′ ) ) , amp ( A 3 [ j ] ) ) } ) / A ] } u ‾ ′ : = ∣ A 3 ∣ ; A 4 : = upDim ( A 1 , u ‾ , u ‾ ′ ) ; A 5 : = upDim ( A 2 , t ‾ , ( t ‾ , 1 ) ) ; A 6 : = assignN ( A 4 , A 3 , u ‾ ′ , ind φ ( η ′ ) , λ v l . amp ( l ) ) ; A 7 : = concat ( A 5 , A 6 ) ; A : = A 7 \begin{aligned}
A_1 &:= \operatorname{filterN}(A,b,\operatorname{ind}^{\varphi}(\eta));\\
\overline{u} &:= |A_1|;\\
A_2 &:= \operatorname{filterN}(A,\neg b,\operatorname{ind}^{\varphi}(\eta));\\
\overline{t} &:= |A_2|;\\
A_3 &:= \operatorname{locateN}(A_1,\overline{u},\operatorname{ind}^{\varphi}(\eta'));\\
e;&\\
&\quad\bullet\ \left\{P\left[\operatorname{concat}\left(\operatorname{upDim}(A_2,\overline{t},(\overline{t},1)),\operatorname{upDim}(A_1,\overline{u},|A_3|)\left\{\forall j\in\operatorname{range}(|A_3|).j\mapsto\operatorname{up}^{a}\left(\operatorname{up}^{b}\left(\operatorname{upDim}(A_1,\overline{u},|A_3|)[j],\operatorname{vector}(A_3[j]),\operatorname{ind}^{\varphi}(\eta')\right),\operatorname{amp}(A_3[j])\right)\right\}\right)/A\right]\right\}\\
&\quad\circ\ \left\{Q\left[\operatorname{concat}\left(\operatorname{upDim}(A_2,\overline{t},(\overline{t},1)),\operatorname{upDim}(A_1,\overline{u},|A_3|)\left\{\forall j\in\operatorname{range}(|A_3|).j\mapsto\operatorname{up}^{a}\left(\operatorname{up}^{b}\left(\operatorname{upDim}(A_1,\overline{u},|A_3|)[j],\operatorname{vector}(A_3[j]),\operatorname{ind}^{\varphi}(\eta')\right),\operatorname{amp}(A_3[j])\right)\right\}\right)/A\right]\right\}\\
\overline{u}' &:= |A_3|;\\
A_4 &:= \operatorname{upDim}(A_1,\overline{u},\overline{u}');\\
A_5 &:= \operatorname{upDim}(A_2,\overline{t},(\overline{t},1));\\
A_6 &:= \operatorname{assignN}(A_4,A_3,\overline{u}',\operatorname{ind}^{\varphi}(\eta'),\lambda v\,l.\operatorname{amp}(l));\\
A_7 &:= \operatorname{concat}(A_5,A_6);\\
A &:= A_7
\end{aligned} A 1 u A 2 t A 3 e ; u ′ A 4 A 5 A 6 A 7 A := filterN ( A , b , ind φ ( η )) ; := ∣ A 1 ∣ ; := filterN ( A , ¬ b , ind φ ( η )) ; := ∣ A 2 ∣ ; := locateN ( A 1 , u , ind φ ( η ′ )) ; ∙ { P [ concat ( upDim ( A 2 , t , ( t , 1 )) , upDim ( A 1 , u , ∣ A 3 ∣ ) { ∀ j ∈ range ( ∣ A 3 ∣ ) . j ↦ up a ( up b ( upDim ( A 1 , u , ∣ A 3 ∣ ) [ j ] , vector ( A 3 [ j ]) , ind φ ( η ′ ) ) , amp ( A 3 [ j ]) ) } ) / A ] } ∘ { Q [ concat ( upDim ( A 2 , t , ( t , 1 )) , upDim ( A 1 , u , ∣ A 3 ∣ ) { ∀ j ∈ range ( ∣ A 3 ∣ ) . j ↦ up a ( up b ( upDim ( A 1 , u , ∣ A 3 ∣ ) [ j ] , vector ( A 3 [ j ]) , ind φ ( η ′ ) ) , amp ( A 3 [ j ]) ) } ) / A ] } := ∣ A 3 ∣ ; := upDim ( A 1 , u , u ′ ) ; := upDim ( A 2 , t , ( t , 1 )) ; := assignN ( A 4 , A 3 , u ′ , ind φ ( η ′ ) , λ v l . amp ( l )) ; := concat ( A 5 , A 6 ) ; := A 7
{ Q ∧ Q l e n ( A , u ‾ ′ + ( t ‾ , 1 ) ) } \{Q\land Q_{\mathrm{len}}(A,\overline{u}'+(\overline{t},1))\} { Q ∧ Q len ( A , u ′ + ( t , 1 ))}
In the lemma, filterN \operatorname{filterN} filterN collects all the elements in A A A satisfying b b b on the vector-bits corresponding to η \eta η , upDim ( A , u ‾ , v ‾ ) \operatorname{upDim}(A,\overline{u},\overline{v}) upDim ( A , u , v ) updates A A A ’s dimension from u ‾ \overline{u} u to v ‾ \overline{v} v , and concat ( A 5 , A 6 ) \operatorname{concat}(A_5,A_6) concat ( A 5 , A 6 ) creates a new array A 7 A_7 A 7 by concatenating A 6 A_6 A 6 to the end of A 5 A_5 A 5 .
As an example of compiling Example 7.8, we name all the compiled operations in Lemma 7.9 as if_h ( e ) \operatorname{if\_h}(e) if_h ( e ) , where the Qafny triple in Example 7.8 is translated to the Hoare triple below.
{ A 3 = [ ( 1 2 , ∣ 0 ⟩ ) ] } e { A 3 = [ ( 1 2 , ∣ 1 ⟩ ) ] } \left\{A_3=\left[\left(\frac{1}{\sqrt{2}},|0\rangle\right)\right]\right\}\ e\ \left\{A_3=\left[\left(\frac{1}{\sqrt{2}},|1\rangle\right)\right]\right\} { A 3 = [ ( 2 1 , ∣0 ⟩ ) ] } e { A 3 = [ ( 2 1 , ∣1 ⟩ ) ] }
{ A = [ ( 1 2 , ∣ 00 ⟩ ) , ( 1 2 , ∣ 10 ⟩ ) ] } if_h ( e ) { A = [ ( 1 2 , ∣ 00 ⟩ ) , ( 1 2 , ∣ 11 ⟩ ) ] } \left\{A=\left[\left(\frac{1}{\sqrt{2}},|00\rangle\right),\left(\frac{1}{\sqrt{2}},|10\rangle\right)\right]\right\}\ \operatorname{if\_h}(e)\ \left\{A=\left[\left(\frac{1}{\sqrt{2}},|00\rangle\right),\left(\frac{1}{\sqrt{2}},|11\rangle\right)\right]\right\} { A = [ ( 2 1 , ∣00 ⟩ ) , ( 2 1 , ∣10 ⟩ ) ] } if_h ( e ) { A = [ ( 2 1 , ∣00 ⟩ ) , ( 2 1 , ∣11 ⟩ ) ] }
We examine the step-by-step effect of the operation below. By applying the filterN functions to A A A for Boolean expressions b b b and ¬ b \neg b ¬ b , we assign A 1 A_1 A 1 and A 2 A_2 A 2 , with both lengths being 1 (u ‾ \overline{u} u for A 1 A_1 A 1 and t ‾ \overline{t} t for A 2 A_2 A 2 ). We list the post-condition after applying the first four operations below.
{ A 1 = [ ( 1 2 , ∣ 10 ⟩ ) ] ∧ u ‾ = 1 ∧ A 2 = [ ( 1 2 , ∣ 00 ⟩ ) ] ∧ t ‾ = 1 } \{A_1=\left[\left(\frac{1}{\sqrt{2}},|10\rangle\right)\right]\land\overline{u}=1\land A_2=\left[\left(\frac{1}{\sqrt{2}},|00\rangle\right)\right]\land\overline{t}=1\} { A 1 = [ ( 2 1 , ∣10 ⟩ ) ] ∧ u = 1 ∧ A 2 = [ ( 2 1 , ∣00 ⟩ ) ] ∧ t = 1 }
We then apply locateN to A 1 A_1 A 1 and assign A 3 A_3 A 3 to be [ ( 1 2 , ∣ 0 ⟩ ) ] \left[\left(\frac{1}{\sqrt{2}},|0\rangle\right)\right] [ ( 2 1 , ∣0 ⟩ ) ] . By applying pre-condition strengthening, we reach the pre-condition { A 3 = [ ( 1 2 , ∣ 0 ⟩ ) ] } \{A_3=\left[\left(\frac{1}{\sqrt{2}},|0\rangle\right)\right]\} { A 3 = [ ( 2 1 , ∣0 ⟩ ) ] } on the top level above.
In Example 7.8, the upper level e e e is a translation of μ \mu μ given by the oracle operation compilation in Lemma 7.1 . We list the translated version below.
A a : = locateN ( A 3 , u ‾ , ind φ ( η ′ ) ) ; A b : = mapN ( A a , u ‾ , λ ( x , y ) . ( x , ( y + 1 ) % 2 ) ) ; A c : = assignN ( A 3 , A b , u ‾ , ind φ ( η ) , λ v l . amp ( l ) ) ; A 3 : = A c \begin{aligned}
A_a &:= \operatorname{locateN}(A_3,\overline{u},\operatorname{ind}^{\varphi}(\eta'));\\
A_b &:= \operatorname{mapN}(A_a,\overline{u},\lambda(x,y).(x,(y+1)\%2));\\
A_c &:= \operatorname{assignN}(A_3,A_b,\overline{u},\operatorname{ind}^{\varphi}(\eta),\lambda v\,l.\operatorname{amp}(l));\\
A_3 &:= A_c
\end{aligned} A a A b A c A 3 := locateN ( A 3 , u , ind φ ( η ′ )) ; := mapN ( A a , u , λ ( x , y ) . ( x , ( y + 1 ) %2 )) ; := assignN ( A 3 , A b , u , ind φ ( η ) , λ v l . amp ( l )) ; := A c
After applying the expression e e e , we reach the upper level post-condition { A 3 = [ ( 1 2 , ∣ 1 ⟩ ) ] } \{A_3=\left[\left(\frac{1}{\sqrt{2}},|1\rangle\right)\right]\} { A 3 = [ ( 2 1 , ∣1 ⟩ ) ] } , with the length u ‾ ′ = 1 \overline{u}'=1 u ′ = 1 . Since A 3 A_3 A 3 ’s dimension is not modified in the upper-level computation, i.e., A 3 A_3 A 3 ’s dimension is the same as A 1 A_1 A 1 ’s, the first upDim operation sets A 4 A_4 A 4 to be the same as A 1 A_1 A 1 . The same situation applies to the second upDim. We list below the post-condition, including only A 3 A_3 A 3 , A 4 A_4 A 4 , and A 5 A_5 A 5 after the two upDim operations.
{ A 3 = [ ( 1 2 , ∣ 1 ⟩ ) ] ∧ A 4 = [ ( 1 2 , ∣ 10 ⟩ ) ] ∧ A 5 = [ ( 1 2 , ∣ 00 ⟩ ) ] } \{A_3 = [(\frac{1}{\sqrt{2}}, \lvert1\rangle)] \land A_4 = [(\frac{1}{\sqrt{2}}, \lvert10\rangle)] \land A_5 = [(\frac{1}{\sqrt{2}}, \lvert00\rangle)]\} { A 3 = [( 2 1 , ∣ 1 ⟩)] ∧ A 4 = [( 2 1 , ∣ 10 ⟩)] ∧ A 5 = [( 2 1 , ∣ 00 ⟩)]}
The assignN operation assigns to A 6 A_6 A 6 with A 3 A_3 A 3 ’s amplitude as well as the basis-vector bit shrunk to the second position (y [ 0 ] y[0] y [ 0 ] ’s position), where A 6 A_6 A 6 is assigned as [ ( 1 2 , ∣ 11 ⟩ ) ] [(\frac{1}{\sqrt{2}}, \lvert11\rangle)] [( 2 1 , ∣ 11 ⟩)] . Then, we concatenate A 5 A_5 A 5 and A 6 A_6 A 6 and assign A 7 A_7 A 7 back to A A A . Finally, we reach the bottom-level post-condition listed in the Hoare triple above.
Validity Proof. The validity proof is based on the Qafny proof rule for quantum conditionals, with generalized pre- and post-conditions, where we set the bottom-level pre- and post-conditions to be arbitrary P P P and Q Q Q in P-If. In Lemma 7.9, the operations before ∙ \bullet ∙ transform P P P to the pre-condition of the upper level (P-If) and the operations after ∘ \circ ∘ transform the post-condition in the upper level to Q Q Q , i.e., the pre- and post-conditions around e e e can be thought of as { ∙ } e { ∘ } \{\bullet\}\ e\ \{\circ\} { ∙ } e { ∘ } , where ∙ \bullet ∙ and ∘ \circ ∘ represent the translated upper-level pre- and post-conditions. We outline the proof below.
Proof. The proof requires deriving a Hoare logic proof tree, including the array-length axioms for A A A and A 1 A_1 A 1 to A 7 A_7 A 7 . We assume that ( t ⃗ , 1 ⃗ ) (\vec{t}, \vec{1}) ( t , 1 ) defines the length for i + j i+j i + j dimensional array, where t ⃗ \vec{t} t defines the lengths for i i i dimensions and the remaining j j j dimensions have length 1 1 1 . The proof relies on the axiom that concat(filterN(A , b A,b A , b ), filterN(A , ¬ b A,\neg b A , ¬ b )) gives back A A A , i.e., if we partition A A A into two subsets and merge them back, it will produce the same A A A . This is valid in quantum states since they do not care about ordering. The proof-tree derivation is given in Section A.4. □
We now present the proofs for some of the functions involved above. The filter (filterN), array concatenation (concat), and array length (∣ A ∣ |A| ∣ A ∣ ) are well known, as many previous works [27 ] discuss their Hoare logic proofs. Their mechanized proof is in Rocq and has a similar style to Lemma 7.2. We mainly focus on upDim(A , m ‾ , u ‾ A,\overline{m},\overline{u} A , m , u ), which updates A A A ’s dimension from m ‾ \overline{m} m to u ‾ \overline{u} u , provided that m ‾ ⊑ u ‾ \overline{m} \sqsubseteq\overline{u} m ⊑ u , i.e., m ‾ \overline{m} m is a prefix of u ‾ \overline{u} u as ∃ m ′ ‾ . u ‾ = ( m ‾ , m ′ ‾ ) \exists\overline{m'}.\overline{u} = (\overline{m},\overline{m'}) ∃ m ′ . u = ( m , m ′ ) .
Lemma 7.10 (The Valid UpDim Hoare Triple). Given a locus η ′ ⊢ η \eta' \mathrel{\vdash} \eta η ′ ⊢ η , an n n n dimensional array A A A , with the bounds m ‾ \overline{m} m and a length list m ′ ‾ \overline{m'} m ′ , and the valid Hoare triple of upDim( A , m ‾ , ( m ‾ , m ′ ‾ ) ) (A,\overline{m},(\overline{m},\overline{m'})) ( A , m , ( m , m ′ )) is listed below with its implementation. We assume that the result array A 1 A_1 A 1 is initialized to n + ∣ m ′ ‾ ∣ n + |\overline{m'}| n + ∣ m ′ ∣ dimension with the additional dimension bounds m ′ ‾ \overline{m'} m ′ .
{ P [ A 1 { ∀ j ‾ ∈ range ( m ‾ ) . ∀ k ‾ ∈ range ( m ′ ‾ ) . ( j ‾ , k ‾ ) ↦ A [ j ‾ ] } / A 1 ] ∧ Q l e n ( A , m ‾ ) ∧ Q l e n ( A 1 , ( m ‾ , m ′ ‾ ) ) } \{P[A_1\{\forall\overline{j} \in\operatorname{range}(\overline{m}).\forall\overline{k} \in\operatorname{range}(\overline{m'}).(\overline{j},\overline{k}) \mapsto A[\overline{j}]\}/A_1] \land Q_{\mathrm{len}}(A,\overline{m}) \land Q_{\mathrm{len}}(A_1,(\overline{m},\overline{m'}))\} { P [ A 1 { ∀ j ∈ range ( m ) .∀ k ∈ range ( m ′ ) . ( j , k ) ↦ A [ j ]} / A 1 ] ∧ Q len ( A , m ) ∧ Q len ( A 1 , ( m , m ′ ))} A 1 : = upDim ( A , m ‾ , ( m ‾ , m ′ ‾ ) ) A_1 := \operatorname{upDim}(A,\overline{m},(\overline{m},\overline{m'})) A 1 := upDim ( A , m , ( m , m ′ ))
{ P ∧ Q l e n ( A , m ‾ ) ∧ Q l e n ( A 1 , ( m ‾ , m ′ ‾ ) ) } \{P \land Q_{\mathrm{len}}(A,\overline{m}) \land Q_{\mathrm{len}}(A_1,(\overline{m},\overline{m'}))\} { P ∧ Q len ( A , m ) ∧ Q len ( A 1 , ( m , m ′ ))} where upDim( A , m ‾ , ( m ‾ , m ′ ‾ ) ) (A,\overline{m},(\overline{m},\overline{m'})) ( A , m , ( m , m ′ )) is defined as: mapN( A , m ‾ , λ ( x , y ) . copy ( m ′ ‾ , ( x , y ) ) ) (A,\overline{m},\lambda(x,y).\operatorname{copy}(\overline{m'},(x,y))) ( A , m , λ ( x , y ) . copy ( m ′ , ( x , y ))) ; copy( m ′ ‾ , v ) (\overline{m'},v) ( m ′ , v ) is a generalized version of the copy function in Lemma 7.5 to produce a m ′ ‾ \overline{m'} m ′ dimensional array of elements v v v .
Proof. The proof is given by deriving a proof tree using a single operation mapN, and it follows a similar style to the proof of Lemma 7.5 . Its detail is mechanized in Rocq. □
Translating Quantum Measurement
Below is the Qafny proof rule for quantum measurements, containing a predicate transformer M \mathcal{M} M .
σ ⊢ { η ⊢ η ′ : EN ( n ) } ⊢ { P [ M ( x , r , ∣ η , η ′ ⟩ ) / η ⊢ η ′ ] } r , x : = measure ( η ) { P } \sigma\vdash\{\eta\vdash\eta' : \operatorname{EN}(n)\} \vdash\{P[\mathcal{M}(x,r,\lvert\eta,\eta'\rangle)/\eta\vdash\eta']\}\ r,\ x := \operatorname{measure}(\eta)\ \{P\} σ ⊢ { η ⊢ η ′ : EN ( n )} ⊢ { P [ M ( x , r , ∣ η , η ′ ⟩) / η ⊢ η ′ ]} r , x := measure ( η ) { P }
The predicate transformer M \mathcal{M} M performs two tasks: 1) trim down the locus η ∣ ∣ η ′ \eta\mathbin{\vert\mkern-1.5mu\vert} \eta' η ∣ ∣ η ′ to be η ′ \eta' η ′ since η \eta η is measured away, and 2) assign the measurement result, both measurement output bits and probability value, to the binding variables x x x and r r r , respectively. The operation is similar to a series of array assignments, as in the quantum oracle operation, except that the measurement result is nondeterministic. If we view a quantum state as an array representing a linear sum, we can always assume the existence of the index i ˉ \bar{i} i ˉ to be picked; thus, x = A 1 [ i ˉ ] x = A_1[\bar{i}] x = A 1 [ i ˉ ] , if A 1 A_1 A 1 refers to the sub-array of η \eta η , and the probability value r r r is the sum of the squares of all amplitudes in A 1 [ k ˉ ] A_1[\bar{k}] A 1 [ k ˉ ] (for all k ˉ \bar{k} k ˉ ), such that A 1 [ i ˉ ] = A 1 [ k ˉ ] A_1[\bar{i}] = A_1[\bar{k}] A 1 [ i ˉ ] = A 1 [ k ˉ ] . We define the following valid Hoare triple for quantum measurement.
Lemma 7.11 (The Measurement Valid Triple). Given a quantum state φ \varphi φ , an operation r , x : = measure ( η ) r,x := \operatorname{measure}(\eta) r , x := measure ( η ) in the locus η ∣ ∣ η ′ \eta\mathbin{\vert\mkern-1.5mu\vert} \eta' η ∣ ∣ η ′ with the EN ( n ) \operatorname{EN}(n) EN ( n ) -typed quantum datum φ ( η ∣ ∣ η ′ ) \varphi(\eta\mathbin{\vert\mkern-1.5mu\vert} \eta') φ ( η ∣ ∣ η ′ ) , transformed to an n n n dimensional array A A A , where the collection of indices ind φ ( η ∣ ∣ η ′ ) \operatorname{ind}^{\varphi}(\eta\mathbin{\vert\mkern-1.5mu\vert} \eta') ind φ ( η ∣ ∣ η ′ ) . Assuming that the index i ∈ [ 0 , m ˉ ) i \in[0,\bar{m}) i ∈ [ 0 , m ˉ ) is picked for A A A , the valid Hoare triple is listed as follows, where E 1 : = filterN ( A , λ y . y = x , ind φ ( η ) ) E_1 := \operatorname{filterN}(A,\lambda y.y = x,\operatorname{ind}^{\varphi}(\eta)) E 1 := filterN ( A , λ y . y = x , ind φ ( η )) .
{ P [ E 1 { ∀ j ˉ ∈ range ( ∣ E 1 ∣ ) . j ˉ ↦ up a ( up b ( E 1 [ j ˉ ] , vector ( E 1 [ j ˉ ] ) , ind φ ( η ′ ) ) , amp ( E 1 ) r ) } / A ] [ ∑ j ˉ ∈ range ( ∣ E 1 ∣ ) ∣ amp ( E 1 ) ∣ 2 / r ] [ vector ( A [ i ˉ ] ) [ ind φ ( η ) ] / x ] ∧ Q len ( A , m ˉ ) x : = vector ( A [ i ˉ ] ) [ ind φ ( η ) ] ; A 1 : = filterN ( A , λ y . y = x , ind φ ( η ) ) ; t ˉ : = ∣ A 1 ∣ ; A 2 : = locateN ( A 1 , t ˉ , ind φ ( η ′ ) ) ; r : = sqrtSum ( A 2 , t ˉ ) ; A 3 : = mapN ( A 2 , t ˉ , λ ( y , u ) . ( y / r , u ) ) ; A : = A 3 \left\{
\begin{aligned}
&\frac{P\left[E_1\left\{\forall\bar{j}\in\operatorname{range}(\lvert E_1\rvert).\bar{j}\mapsto\operatorname{up}^{a}\left(\operatorname{up}^{b}\left(E_1[\bar{j}],\operatorname{vector}(E_1[\bar{j}]),\operatorname{ind}^{\varphi}(\eta')\right),\frac{\operatorname{amp}(E_1)}{r}\right)\right\}/A\right]}{\left[\sqrt{\sum_{\bar{j}\in\operatorname{range}(\lvert E_1\rvert)}\lvert\operatorname{amp}(E_1)\rvert^2/r}\right]\left[\operatorname{vector}(A[\bar{i}])[\operatorname{ind}^{\varphi}(\eta)]/x\right]\wedge Q_{\operatorname{len}}(A,\bar{m})} \\
&x := \operatorname{vector}(A[\bar{i}])[\operatorname{ind}^{\varphi}(\eta)];\\
&A_1 := \operatorname{filterN}(A,\lambda y.y = x,\operatorname{ind}^{\varphi}(\eta));\\
&\bar{t} := \lvert A_1\rvert;\\
&A_2 := \operatorname{locateN}(A_1,\bar{t},\operatorname{ind}^{\varphi}(\eta'));\\
&r := \operatorname{sqrtSum}(A_2,\bar{t});\\
&A_3 := \operatorname{mapN}(A_2,\bar{t},\lambda(y,u).(y/r,u)); A := A_3
\end{aligned}
\right. ⎩ ⎨ ⎧ [ ∑ j ˉ ∈ range (∣ E 1 ∣) ∣ amp ( E 1 ) ∣ 2 / r ] [ vector ( A [ i ˉ ]) [ ind φ ( η )] / x ] ∧ Q len ( A , m ˉ ) P [ E 1 { ∀ j ˉ ∈ range (∣ E 1 ∣) . j ˉ ↦ up a ( up b ( E 1 [ j ˉ ] , vector ( E 1 [ j ˉ ]) , ind φ ( η ′ ) ) , r amp ( E 1 ) ) } / A ] x := vector ( A [ i ˉ ]) [ ind φ ( η )] ; A 1 := filterN ( A , λ y . y = x , ind φ ( η )) ; t ˉ := ∣ A 1 ∣ ; A 2 := locateN ( A 1 , t ˉ , ind φ ( η ′ )) ; r := sqrtSum ( A 2 , t ˉ ) ; A 3 := mapN ( A 2 , t ˉ , λ ( y , u ) . ( y / r , u )) ; A := A 3 P ∧ Q len ( A , t ˉ ) ∧ t ˉ ≤ m ˉ P \wedge Q_{\operatorname{len}}(A,\bar{t}) \wedge\bar{t}\leq\bar{m} P ∧ Q len ( A , t ˉ ) ∧ t ˉ ≤ m ˉ
Following the Bell pair example in Example 7.8, assume that we are given the task of verifying whether 0 0 0 and ∣ 0 ⟩ \lvert0\rangle ∣ 0 ⟩ are the valid outputs after measuring the first qubit, and the state before the measurement is represented as an array A = [ 1 2 ∣ 0 ⟩ ∣ 0 ⟩ , 1 2 ∣ 1 ⟩ ∣ 1 ⟩ ] A = [\frac{1}{\sqrt{2}}\lvert0\rangle\lvert0\rangle,\frac{1}{\sqrt{2}}\lvert1\rangle\lvert1\rangle] A = [ 2 1 ∣ 0 ⟩ ∣ 0 ⟩ , 2 1 ∣ 1 ⟩ ∣ 1 ⟩] . When applying the proof sequence in Lemma 7.11 to the example, we first pick 0 0 0 as the output index, as A [ 0 ] A[0] A [ 0 ] , where we assign x x x to vector ( A [ 0 ] , ind φ ( x [ 0 ] ) ) = 0 \operatorname{vector}(A[0],\operatorname{ind}^{\varphi}(x[0])) = 0 vector ( A [ 0 ] , ind φ ( x [ 0 ])) = 0 , and we assign A 1 A_1 A 1 and A 2 A_2 A 2 to be [ 1 2 ∣ 0 ⟩ ∣ 0 ⟩ ] [\frac{1}{\sqrt{2}}\lvert0\rangle\lvert0\rangle] [ 2 1 ∣ 0 ⟩ ∣ 0 ⟩] and [ 1 2 ∣ 0 ⟩ ] [\frac{1}{\sqrt{2}}\lvert0\rangle] [ 2 1 ∣ 0 ⟩] with t ˉ = 1 \bar{t}=1 t ˉ = 1 . Finally, r r r is assigned to 1 2 \frac{1}{\sqrt{2}} 2 1 where we assign A 3 A_3 A 3 to be [ ∣ 0 ⟩ ] [\lvert0\rangle] [∣ 0 ⟩] by dividing the element amplitude by r r r .
Measurement need not be the last step of a program: r , x : = measure ( η ) r,x := \operatorname{measure}(\eta) r , x := measure ( η ) binds the outcome x x x and its probability r r r for use in the continuation. Because the outcome index is chosen once and the remaining array is a function of the pre-state and that choice, the continuation sees x x x as an ordinary classical variable; feed-forward control on measurement outcomes, as in teleportation or repeat-until-success loops, therefore translates to ordinary classical control in Hoare logic, and no black-box probability distribution is involved.
Validity Proof. We show the proof of Lemma 7.11 below.
Proof. The proof derives a Hoare logic proof tree by combining the proof results for the filterN, locateN, sqrtSum, and other functions. For simplicity, we omit the Q len Q_{\operatorname{len}} Q len judgments. The proof-tree derivation is given in Section A.5. □
For the functions involved in the proof, aside from sqrtSum, the others have been discussed above. We now present the proof of the sqrtSum function below, which is essentially the same as the proof for a classical sum function.
Lemma 7.12 (The SqrtSum Hoare Triple). Given a locus η \eta η , an n n n -dimensional array A A A with bounds m ˉ \bar{m} m ˉ , the valid Hoare triple for the sqrtSum(A,\bar{m}) function is listed below.
{ P [ ∑ j ∈ range ( m ‾ ) ∣ amp ( A [ j ‾ ] ) ∣ 2 r ] ∧ Q l e n ( A , m ‾ ) } \left\{P\left[\sqrt{\sum_{j \in\operatorname{range}(\overline{m})} \frac{|\operatorname{amp}(A[\overline{j}])|^2}{r}}\right] \land Q_{\mathrm{len}}(A,\overline{m})\right\} { P [ ∑ j ∈ range ( m ) r ∣ amp ( A [ j ]) ∣ 2 ] ∧ Q len ( A , m ) }
r : = sqrtSum ( A , m ‾ ) r := \operatorname{sqrtSum}(A,\overline{m}) r := sqrtSum ( A , m )
{ P } \{P\} { P }
where sqrtSum ( A , m ‾ ) \operatorname{sqrtSum}(A,\overline{m}) sqrtSum ( A , m ) is defined as:
r := 0;
j := 0;
while (j_0 < m_0) {
...
while (j_{n-1} < m_{n-1}) {
r += |amp(A[k_0, ..., k_{n-1}])|^2; j_{n-1} := j_{n-1} + 1
}
... j_0 := j_0 + 1
}
r := √r
Proof. The proof is given by deriving a proof tree with a structure similar to the proofs of the classical sum function and of Lemma 7.2.
Translating Classical Operations
The Qafny proof system contains some proof rules for classical operations, where we list two below for sequencing and for-loop operations.
P-Seq
σ ⊢ μ 1 ▹ σ 1 σ 1 ⊢ μ 2 ▹ σ 2 σ ⊢ μ 1 ; μ 2 ▹ σ 2 \frac{\sigma\vdash\mu_1 \triangleright\sigma_1 \qquad\sigma_1 \vdash\mu_2 \triangleright\sigma_2}{\sigma\vdash\mu_1;\mu_2 \triangleright\sigma_2} σ ⊢ μ 1 ; μ 2 ▹ σ 2 σ ⊢ μ 1 ▹ σ 1 σ 1 ⊢ μ 2 ▹ σ 2
P-Loop
σ [ a 1 / j ] ⊢ for j ∈ [ a 1 , a 2 ) { e } ▹ σ [ a 2 / j ] σ ⊢ { φ ∧ a 1 ≤ j < a 2 } e { φ } σ [ a 1 / j ] ⊢ for j ∈ [ a 1 , a 2 ) { e } { φ ∧ j = a 2 } \frac{\sigma[a_1/j] \vdash\text{for } j \in[a_1,a_2)\ \{e\} \triangleright\sigma[a_2/j] \qquad\sigma\vdash\{\varphi\land a_1 \le j < a_2\}\ e\ \{\varphi\}}{\sigma[a_1/j] \vdash\text{for } j \in[a_1,a_2)\ \{e\}\ \{\varphi\land j=a_2\}} σ [ a 1 / j ] ⊢ for j ∈ [ a 1 , a 2 ) { e } { φ ∧ j = a 2 } σ [ a 1 / j ] ⊢ for j ∈ [ a 1 , a 2 ) { e } ▹ σ [ a 2 / j ] σ ⊢ { φ ∧ a 1 ≤ j < a 2 } e { φ }
Except for the Qafny typing, the proof rules are similar to the sequencing and for-loop rules in Hoare logic. These rule transformations are natural, i.e., we transform the sub-terms and simply transform these operations to the proof rules for similar operations in Hoare logic. Sequencing is compositional: the translation of μ 1 ; μ 2 \mu_1;\mu_2 μ 1 ; μ 2 is the sequence of the two translations, and P-Seq becomes the Hoare sequencing rule. A for-loop is a classical loop whose body is translated as above, so P-Loop becomes the Hoare loop rule with the translated invariant. The only non-trivial step is the invariant itself: its locus typically grows with the loop index, as x [ 0 , j ) x[0,j) x [ 0 , j ) does in a loop that entangles one more qubit per iteration, so the invariant is translated with the index map ind φ ( x [ 0 , j ) ) \operatorname{ind}^{\varphi}(x[0,j)) ind φ ( x [ 0 , j )) and the array bounds m ‾ \overline{m} m parameterised by j j j . The translated invariant P ( j ) P(j) P ( j ) is then an ordinary Hoare invariant, and its preservation over one iteration is the translation of the loop body from P ( j ) P(j) P ( j ) to P ( j + 1 ) P(j+1) P ( j + 1 ) .
Translation Completeness and Soundness
This section presents the two translation theorems; translation completeness (Theorem 7.13 ) and its component lemmas are mechanized in Rocq, while translation soundness (Theorem 7.14 ) is proved on paper. Based on the individual component proofs in § 7 , we express the combined theorem below.
Theorem 7.13 (Translation Completeness). Given a Qafny proof σ ⊢ { φ } μ { φ ′ } \sigma\vdash\{\varphi\}\ \mu\ \{\varphi'\} σ ⊢ { φ } μ { φ ′ } , let σ ⊢ μ ▹ σ ′ ≫ e \sigma\vdash\mu\triangleright\sigma' \gg e σ ⊢ μ ▹ σ ′ ≫ e , σ ⊢ φ ≫ P \sigma\vdash\varphi\gg P σ ⊢ φ ≫ P , and σ ′ ⊢ φ ′ ≫ Q \sigma' \vdash\varphi' \gg Q σ ′ ⊢ φ ′ ≫ Q ; thus, { P } e { Q } \{P\}\ e\ \{Q\} { P } e { Q } .
Proof. The proof is based on induction on the Qafny type system and a combination of the proofs for the cases in Lemmas 7.1 , 7.4 , 7.9 and 7.11. The proof of the for-loop and a Hoare logic loop are the same except that we need to translate the loop invariant, since the for-loop operation is classical in Qafny. □
The above theorem is the completeness direction: as we translate the program μ \mu μ , pre-condition φ \varphi φ , and post-condition φ ′ \varphi' φ ′ to a Hoare logic program e e e and conditions P P P and Q Q Q , the triple { P } e { Q } \{P\}\ e\ \{Q\} { P } e { Q } is valid in Hoare logic, so nothing Qafny can prove is lost in translation. Since the translation is guided by the Qafny type system σ ⊢ μ ▹ σ ′ \sigma\vdash\mu\triangleright\sigma' σ ⊢ μ ▹ σ ′ , the converse direction, translation soundness, must be shown separately: when a translated { P } e { Q } \{P\}\ e\ \{Q\} { P } e { Q } is valid, Qafny derives a triple for μ \mu μ with semantically equivalent conditions, so the classical verifier cannot certify a false quantum claim. This is the direction that makes a classical verdict about a quantum program trustworthy. In the following theorem, ⟦ φ ⟧ \llbracket\varphi\rrbracket [ [ φ ] ] is the semantic interpretation of the quantum state φ \varphi φ .
Theorem 7.14 (Translation Soundness). Given a Qafny program μ \mu μ , with the typed translation σ ⊢ μ ▹ σ ′ ≫ e \sigma\vdash\mu\triangleright\sigma' \gg e σ ⊢ μ ▹ σ ′ ≫ e where σ ⊢ μ ▹ σ ′ \sigma\vdash\mu\triangleright\sigma' σ ⊢ μ ▹ σ ′ , two conditions φ \varphi φ and φ ′ \varphi' φ ′ , such that σ ⊢ φ ≫ P \sigma\vdash\varphi\gg P σ ⊢ φ ≫ P and σ ′ ⊢ φ ′ ≫ Q \sigma' \vdash\varphi' \gg Q σ ′ ⊢ φ ′ ≫ Q , if { P } e { Q } \{P\}\ e\ \{Q\} { P } e { Q } , there exists φ 1 \varphi_1 φ 1 and φ 1 ′ \varphi'_1 φ 1 ′ , such that σ ⊢ { φ 1 } μ { φ 1 ′ } \sigma\vdash\{\varphi_1\}\ \mu\ \{\varphi'_1\} σ ⊢ { φ 1 } μ { φ 1 ′ } , ⟦ φ ⟧ ≡ ⟦ φ 1 ⟧ \llbracket\varphi\rrbracket\equiv\llbracket\varphi_1\rrbracket [ [ φ ] ] ≡ [ [ φ 1 ] ] , and ⟦ φ ′ ⟧ ≡ ⟦ φ 1 ′ ⟧ \llbracket\varphi'\rrbracket\equiv\llbracket\varphi'_1\rrbracket [ [ φ ′ ] ] ≡ [ [ φ 1 ′ ] ] .
Proof. The proof is based on induction on the Qafny typing rules σ ⊢ μ ▹ σ ′ \sigma\vdash\mu\triangleright\sigma' σ ⊢ μ ▹ σ ′ . Note that the expression e e e is translated from μ \mu μ via fixed translation functions. If the three entities can construct a valid Hoare triple, { P } e { Q } \{P\}\ e\ \{Q\} { P } e { Q } , we can find conditions φ 1 \varphi_1 φ 1 and φ 1 ′ \varphi'_1 φ 1 ′ to construct a valid Qafny triple σ ⊢ { φ 1 } μ { φ 1 ′ } \sigma\vdash\{\varphi_1\}\ \mu\ \{\varphi'_1\} σ ⊢ { φ 1 } μ { φ 1 ′ } . When we translate μ \mu μ to e e e , we translate an individual quantum operation to a composition of HLIR operations. If we can construct a Hoare triple for the HLIR operations, we can also construct a Qafny triple for μ \mu μ .
Now, we need to construct the relations between φ 1 \varphi_1 φ 1 and φ \varphi φ and between φ 1 ′ \varphi'_1 φ 1 ′ and φ ′ \varphi' φ ′ . When we translate φ / φ ′ \varphi/\varphi' φ / φ ′ to P / Q P/Q P / Q , we restrict their types to σ / σ ′ \sigma/\sigma' σ / σ ′ ; these types are based on the locus structures. Given { P } e { Q } \{P\}\ e\ \{Q\} { P } e { Q } , we can show that the types for φ 1 \varphi_1 φ 1 and φ 1 ′ \varphi'_1 φ 1 ′ are also σ \sigma σ and σ ′ \sigma' σ ′ , respectively. With the same program μ \mu μ , we are able to show that φ 1 / φ 1 ′ \varphi_1/\varphi'_1 φ 1 / φ 1 ′ and φ / φ ′ \varphi/\varphi' φ / φ ′ are semantically equivalent, as ⟦ φ ⟧ ≡ ⟦ φ 1 ⟧ \llbracket\varphi\rrbracket\equiv\llbracket\varphi_1\rrbracket [ [ φ ] ] ≡ [ [ φ 1 ] ] and ⟦ φ ′ ⟧ ≡ ⟦ φ 1 ′ ⟧ \llbracket\varphi'\rrbracket\equiv\llbracket\varphi'_1\rrbracket [ [ φ ′ ] ] ≡ [ [ φ 1 ′ ] ] . □
Finally, we bound the size of the translation. Sizes are measured in the symbolic array representation used throughout § 7 : a dimension of length 2 m 2^m 2 m is a single bound expression and assertions quantify over index vectors, so a Hadamard operation on m m m qubits adds one dimension (copyN, enumN, ampN) rather than 2 m 2^m 2 m elements.
Theorem 7.15 (Translation Size). Let σ ⊢ μ ▹ σ ′ ≫ e \sigma\vdash\mu\triangleright\sigma' \gg e σ ⊢ μ ▹ σ ′ ≫ e , σ ⊢ φ ≫ P \sigma\vdash\varphi\gg P σ ⊢ φ ≫ P , and σ ′ ⊢ φ ′ ≫ Q \sigma' \vdash\varphi' \gg Q σ ′ ⊢ φ ′ ≫ Q , and let n n n be the largest array dimension (the largest k k k in an EN( k ) (k) ( k ) type) occurring in σ \sigma σ and σ ′ \sigma' σ ′ . Then (i) e e e contains at most eleven HLIR statements per Qafny statement of μ \mu μ , so ∣ e ∣ |e| ∣ e ∣ is linear in ∣ μ ∣ |\mu| ∣ μ ∣ ; (ii) ∣ P ∣ |P| ∣ P ∣ and ∣ Q ∣ |Q| ∣ Q ∣ are polynomial in ∣ φ ∣ + n |\varphi|+n ∣ φ ∣ + n and ∣ φ ′ ∣ + n |\varphi'|+n ∣ φ ′ ∣ + n , respectively; (iii) for each HLIR statement, the Hoare proof-tree skeleton of § 7 and Section A has size polynomial in the size of its pre- and post-conditions and in n n n .
The theorem bounds the size of the generated verification conditions; it says nothing about the time a classical verifier needs to discharge them.
Proof. By cases on the translation of each Qafny statement. For (i), an oracle application translates to four statements (Lemma 7.1: locateN, mapN, assignN, and an assignment), a Hadamard or QFT application to four (Lemma 7.4 ), a quantum conditional to eleven plus the translation of its body (Lemma 7.9 ), and a measurement to seven (Lemma 7.11); sequencing and for-loops add no statements beyond those of their sub-statements (§ 7.5 ). For (ii), the translation of a state predicate maps each locus η : EN ( k ) ↦ q \eta: \operatorname{EN}(k) \mapsto q η : EN ( k ) ↦ q to one k k k -dimensional array, a quantifier over its index vector, and the basis-ket expression of q q q , whose size is linear in ∣ q ∣ |q| ∣ q ∣ plus k ≤ n k \le n k ≤ n ; each predicate transformer in § 7 substitutes one array expression of that size per occurrence of the array variable, so the translated assertions remain polynomial. For (iii), the skeleton of each primitive (Lemma 7.2 and Section A) consists of one loop per dimension whose invariant repeats the post-condition with a bounded index prefix, that is, O ( n ) O(n) O ( n ) copies of an assertion of polynomial size; copyN, enumN, ampN, locateN, and upDim are instances of mapN or assignN, sqrtSum is a classical sum, and the conditional and measurement skeletons compose a constant number of such steps. □
Related Work
We discuss the related work below.
Quantum Proof Systems. Quantum proof systems include Qafny [5 , 17 ] , QBricks [3 ] based on the path-sum method [1 , 4 ] , Quantum Hoare Logic (QHL) [8 , 19 , 28 , 29 , 32 , 33 ] , quantum separation logic (QSL) [15 , 35 ] , quantum relational logic [18 , 26 ] , and probabilistic Hoare logic for quantum programs [14 ] . These proof systems are developed based on quantum program semantics and states, such as interpreting superoperators [28 ] or Dirac notation [3 ] as program-state syntax, whereas our work demonstrates the translation of a quantum proof system to the classical Hoare logic system.
The key difference between the quantum proof systems and our work is that we develop translation completeness and soundness theorems to show that quantum verification can be effectively done in Hoare logic, i.e., we compile proof rules in a quantum proof system to Hoare logic directly, and show the soundness and completeness based on the compilation. Moreover, the compilation is not specific to Qafny; we illustrate it for QHL as well. As mentioned in § 5 , there are also quantum separation logic proof systems [15 , 35 ] that view tensor products as a kind of separation conjunction, and we show that these separation conjunctions are pure.
Qafny in Dafny. Qafnyc ^{\mathrm c} c [5 ] compiles Qafny verification conditions into Dafny sequences and verifies 37 quantum programs with the Dafny verifier, but proves nothing about the compilation itself. Our translation targets an idealized Hoare logic with array theory, of which Dafny is one instance, and Theorems 7.13 and 7.14 are what justify trusting a Dafny verdict about a quantum program: nothing Qafny can prove is lost, and nothing false is certified. Beyond that foundation, this paper contains results that Qafnyc ^{\mathrm c} c does not: the pureness of tensor-based separating conjunction (§ 5 ), the representation of an E N ( n ) EN(n) E N ( n ) state as an n n n -dimensional array with the index map ind (§ 6 ), the filter-and-renormalize treatment of measurement (§ 7.4 ), and the size bound (Theorem 7.15 ).
Symbolic Execution of Quantum Programs. Fang and Ying [2024] and Chareton et al. [2026] execute quantum programs symbolically, the former with symbolic stabilizer states for error-correction programs and the latter with hybrid path-sums for hybrid quantum programs, and decide properties of the symbolic state of a given program. These are engines; our contribution is meta-theoretic: a translation between two proof systems with guarantees in both directions (Theorems 7.13 and 7.14 ), with the completeness direction mechanized in Rocq, together with the pureness result of § 5 . We share the engine-level view that superposition should be represented symbolically. What the translation adds, and what a symbolic execution engine does not imply, is that the classical Hoare rules for arrays already suffice, and why: separating conjunction is pure, dynamic locus reordering is handled by an index map, and measurement is filter-and-renormalize.
Classical Proof Systems. We are informed by Hoare and separation logic, as articulated in the classic paper [9 , 23 ] , and other papers as well [13 , 20 , 21 , 24 , 25 , 34 ] . Primarily, we show a compilation from Qafny to Hoare logic with array operators [9 ] .
Conclusion, Future Work, and Limitations
We have shown that verifying a quantum program need be no harder than verifying a classical program, e.g., a parallel program: the Qafny proof system translates into Hoare logic with array theory completely and soundly, and the completeness direction is mechanized in Rocq. Three observations make the embedding tractable. First, Qafny’s separation conjunctions over disjoint loci are pure and reduce to ordinary conjunctions. Second, an E N ( n ) \mathrm{EN}(n) EN ( n ) -typed quantum state admits a faithful representation as an n n n -dimensional array of basis-kets. Third, the semantics of quantum operations, applied to that representation, factor through a small set of array-aggregate primitives, packaged here as the Hoare-logic intermediate representation HLIR. We also bound the size of the translation: in the symbolic array representation, the generated verification conditions are polynomial in the size of the Qafny triple and the array dimension.
The embedding is constructive: every Qafny proof rule maps to an explicit HLIR derivation, and every HLIR primitive is realized by a proof-tree skeleton in Hoare logic with array theory. Quantum proof obligations therefore become deterministic and can be discharged by classical automated verifiers (e.g. Dafny, SMT-based separation logic, natural-proof engines) with no quantum-specific infrastructure. Future work includes extending the embedding to richer quantum types (such as oracle and Hadamard variants), connecting HLIR to a concrete automated back-end for end-to-end verified quantum software, and investigating whether the same determinization technique generalizes to other hybrid classical/quantum proof systems.
Our compilation only shows that quantum program verification is no harder than classical program verification, i.e., we do not claim that all quantum program verification is easy, as many quantum programs based on statistical methods, such as quantum machine learning [2 ] , are hard to verify. However, we believe the same holds for classical program verification, as many classical AI and machine-learning-based programs are also hard to verify.
References
Proof-Tree Derivations for the Translation Lemmas
This appendix5 collects the Hoare logic proof-tree derivations for the lemmas in § 7 ; the main text keeps each lemma, its translated triple, and the reduction step, and points here for the derivation. In every derivation, the right-hand column names the Hoare rule applied at each step.
Proof Tree for mapN
We continue the proof of Lemma 7.2 (§ 7.1 ), showing the validity of the triple for A ′ : = mapN ( A , m ‾ , f ) A' := \operatorname{mapN}(A,\overline{m},f) A ′ := mapN ( A , m , f ) obtained there via Lemma 2.1 .
We expand m ‾ \overline{m} m to ( m 0 , … , m n − 1 ) (m_0,\ldots,m_{n-1}) ( m 0 , … , m n − 1 ) . The proof requires deriving a Hoare logic proof tree. We demonstrate the simplified proof sequences with loop invariants at each level. The right-hand side indicates the Hoare rules to apply.
{ Q len ( A , A ′ , ( m 0 , … , m n − 1 ) ) } j 0 : = 0 ; … j n − 1 : = 0 ; Assign { j 0 = 0 ∧ … ∧ j n − 1 = 0 ∧ Q len ( A , A ′ , ( m 0 , … , m n − 1 ) ) } ⇒ { Q len ( A , A ′ , ( m 0 , … , m n − 1 ) ) ∧ j 0 ∈ [ 0 , m 0 ] ∧ … ∧ j n − 1 ∈ [ 0 , m n − 1 ] ∧ ∀ k 0 ∈ [ 0 , j 0 ) . … ∀ k n − 1 ∈ [ 0 , j n − 1 ) . A ′ [ k 0 , … , k n − 1 ] = f ( A [ k 0 , … , k n − 1 ] ) } Pre-Strength while ( j 0 < m 0 ) While { Q len ( A , A ′ , ( m 0 , … , m n − 1 ) ) ∧ j 0 ∈ [ 0 , m 0 ] ∧ … ∧ j n − 1 ∈ [ 0 , m n − 1 ] ∧ ∀ k 0 ∈ [ 0 , j 0 ) . … ∀ k n − 1 ∈ [ 0 , j n − 1 ) . A ′ [ k 0 , … , k n − 1 ] = f ( A [ k 0 , … , k n − 1 ] ) } Invariant { … while ( j n − 1 < m n − 1 ) { Q len ( A , A ′ , ( m 0 , … , m n − 1 ) ) ∧ j 0 ∈ [ 0 , m 0 ] ∧ … ∧ j n − 1 ∈ [ 0 , m n − 1 ] ∧ ∀ k n − 1 ∈ [ 0 , j n − 1 ) . A ′ [ j 0 , … , j n − 2 , k n − 1 ] = f ( A [ j 0 , … , j n − 2 , k n − 1 ] ) } Invariant { A ′ [ k 0 , … , k n − 1 ] : = f ( A [ k 0 , … , k n − 1 ] ) ; j n − 1 : = j n − 1 + 1 } … } j 0 : = j 0 + 1 } { Q len ( A , A ′ , ( m 0 , … , m n − 1 ) ) ∧ ¬ ( j 0 < m 0 ) ∧ … ∧ ¬ ( j n − 1 < m n − 1 ) ∧ j 0 ∈ [ 0 , m 0 ] ∧ … ∧ j n − 1 ∈ [ 0 , m n − 1 ] ∧ ∀ k 0 ∈ [ 0 , j 0 ) . … ∀ k n − 1 ∈ [ 0 , j n − 1 ) . A { k 0 , … , k n − 1 ↦ f ( A [ k 0 , … , k n − 1 ] ) } } ⇒ { Q len ( A , A ′ , ( m 0 , … , m n − 1 ) ) ∧ ∀ k 0 ∈ [ 0 , m 0 ) . … ∀ k n − 1 ∈ [ 0 , m n − 1 ) . A { k 0 , … , k n − 1 ↦ f ( A [ k 0 , … , k n − 1 ] ) } } Post-Weakening \begin{aligned}
&\{Q_{\operatorname{len}}(A,A',(m_0,\ldots,m_{n-1}))\}\\
&\qquad j_0 := 0;\ldots j_{n-1} := 0; && \text{Assign}\\
&\{j_0=0\land\ldots\land j_{n-1}=0\land Q_{\operatorname{len}}(A,A',(m_0,\ldots,m_{n-1}))\}\\
&\Rightarrow\{Q_{\operatorname{len}}(A,A',(m_0,\ldots,m_{n-1}))\land j_0\in[0,m_0]\land\ldots\land j_{n-1}\in[0,m_{n-1}]\\
&\qquad\land\forall k_0\in[0,j_0).\ldots\forall k_{n-1}\in[0,j_{n-1}).\ A'[k_0,\ldots,k_{n-1}]=f(A[k_0,\ldots,k_{n-1}])\} && \text{Pre-Strength}\\
&\qquad\textbf{while} (j_0<m_0) && \text{While}\\
&\{Q_{\operatorname{len}}(A,A',(m_0,\ldots,m_{n-1}))\land j_0\in[0,m_0]\land\ldots\land j_{n-1}\in[0,m_{n-1}]\\
&\qquad\land\forall k_0\in[0,j_0).\ldots\forall k_{n-1}\in[0,j_{n-1}).\ A'[k_0,\ldots,k_{n-1}]=f(A[k_0,\ldots,k_{n-1}])\} && \text{Invariant}\\
&\qquad\{ \ldots\\
&\qquad\qquad\textbf{while}(j_{n-1}<m_{n-1})\\
&\qquad\qquad\{Q_{\operatorname{len}}(A,A',(m_0,\ldots,m_{n-1}))\land j_0\in[0,m_0]\land\ldots\land j_{n-1}\in[0,m_{n-1}]\\
&\qquad\qquad\land\forall k_{n-1}\in[0,j_{n-1}).\ A'[j_0,\ldots,j_{n-2},k_{n-1}]=f(A[j_0,\ldots,j_{n-2},k_{n-1}])\} && \text{Invariant}\\
&\qquad\qquad\{\\
&\qquad\qquad\quad A'[k_0,\ldots,k_{n-1}] := f(A[k_0,\ldots,k_{n-1}]);\\
&\qquad\qquad\quad j_{n-1} := j_{n-1}+1\\
&\qquad\qquad\}\\
&\qquad\qquad\ldots\\
&\qquad\}\\
&\qquad j_0 := j_0+1\\
&\qquad\}\\
&\{Q_{\operatorname{len}}(A,A',(m_0,\ldots,m_{n-1}))\land\\
&\qquad\neg(j_0<m_0)\land\ldots\land\neg(j_{n-1}<m_{n-1})\land j_0\in[0,m_0]\land\ldots\land j_{n-1}\in[0,m_{n-1}]\\
&\qquad\land\forall k_0\in[0,j_0).\ldots\forall k_{n-1}\in[0,j_{n-1}).\ A\{k_0,\ldots,k_{n-1}\mapsto f(A[k_0,\ldots,k_{n-1}])\}\}\\
&\Rightarrow\{Q_{\operatorname{len}}(A,A',(m_0,\ldots,m_{n-1}))\land\\
&\qquad\forall k_0\in[0,m_0).\ldots\forall k_{n-1}\in[0,m_{n-1}).\ A\{k_0,\ldots,k_{n-1}\mapsto f(A[k_0,\ldots,k_{n-1}])\}\} && \text{Post-Weakening}
\end{aligned} { Q len ( A , A ′ , ( m 0 , … , m n − 1 ))} j 0 := 0 ; … j n − 1 := 0 ; { j 0 = 0 ∧ … ∧ j n − 1 = 0 ∧ Q len ( A , A ′ , ( m 0 , … , m n − 1 ))} ⇒ { Q len ( A , A ′ , ( m 0 , … , m n − 1 )) ∧ j 0 ∈ [ 0 , m 0 ] ∧ … ∧ j n − 1 ∈ [ 0 , m n − 1 ] ∧ ∀ k 0 ∈ [ 0 , j 0 ) . … ∀ k n − 1 ∈ [ 0 , j n − 1 ) . A ′ [ k 0 , … , k n − 1 ] = f ( A [ k 0 , … , k n − 1 ])} while ( j 0 < m 0 ) { Q len ( A , A ′ , ( m 0 , … , m n − 1 )) ∧ j 0 ∈ [ 0 , m 0 ] ∧ … ∧ j n − 1 ∈ [ 0 , m n − 1 ] ∧ ∀ k 0 ∈ [ 0 , j 0 ) . … ∀ k n − 1 ∈ [ 0 , j n − 1 ) . A ′ [ k 0 , … , k n − 1 ] = f ( A [ k 0 , … , k n − 1 ])} { … while ( j n − 1 < m n − 1 ) { Q len ( A , A ′ , ( m 0 , … , m n − 1 )) ∧ j 0 ∈ [ 0 , m 0 ] ∧ … ∧ j n − 1 ∈ [ 0 , m n − 1 ] ∧ ∀ k n − 1 ∈ [ 0 , j n − 1 ) . A ′ [ j 0 , … , j n − 2 , k n − 1 ] = f ( A [ j 0 , … , j n − 2 , k n − 1 ])} { A ′ [ k 0 , … , k n − 1 ] := f ( A [ k 0 , … , k n − 1 ]) ; j n − 1 := j n − 1 + 1 } … } j 0 := j 0 + 1 } { Q len ( A , A ′ , ( m 0 , … , m n − 1 )) ∧ ¬ ( j 0 < m 0 ) ∧ … ∧ ¬ ( j n − 1 < m n − 1 ) ∧ j 0 ∈ [ 0 , m 0 ] ∧ … ∧ j n − 1 ∈ [ 0 , m n − 1 ] ∧ ∀ k 0 ∈ [ 0 , j 0 ) . … ∀ k n − 1 ∈ [ 0 , j n − 1 ) . A { k 0 , … , k n − 1 ↦ f ( A [ k 0 , … , k n − 1 ])}} ⇒ { Q len ( A , A ′ , ( m 0 , … , m n − 1 )) ∧ ∀ k 0 ∈ [ 0 , m 0 ) . … ∀ k n − 1 ∈ [ 0 , m n − 1 ) . A { k 0 , … , k n − 1 ↦ f ( A [ k 0 , … , k n − 1 ])}} Assign Pre-Strength While Invariant Invariant Post-Weakening
Proof Tree for assignN
We demonstrate the proof-tree derivation for Lemma 7.3 (assignN) below, for the instance f = λ u v . amp ( v ) f=\lambda u\,v.\operatorname{amp}(v) f = λ u v . amp ( v ) in which the amplitude is taken from A 1 A_1 A 1 ; the general case replaces amp ( A 1 [ k 0 , … , k n − 1 ] ) \operatorname{amp}(A_1[k_0,\ldots,k_{n-1}]) amp ( A 1 [ k 0 , … , k n − 1 ]) by f ( A [ k 0 , … , k n − 1 ] , A 1 [ k 0 , … , k n − 1 ] ) f(A[k_0,\ldots,k_{n-1}],A_1[k_0,\ldots,k_{n-1}]) f ( A [ k 0 , … , k n − 1 ] , A 1 [ k 0 , … , k n − 1 ]) throughout.
Proof. The proof requires a derivation of a Hoare logic proof tree, having a simplified proof sequence as follows. Mainly, we implement the assignN function as the program with assertions below. Here, u p b \mathrm{up}^{b} up b inserts A 1 A_{1} A 1 ’s bits into A A A ’s vector at the indices ind φ ( η ) \operatorname{ind}^{\varphi}(\eta) ind φ ( η ) (via merge), and its proof is given in the Rocq mechanization.
{ Q l e n ( A , A 1 , A 2 , ( m 0 , … , m n − 1 ) ) } j 0 : = 0 ; … ; j n − 1 : = 0 ; Assign { Q l e n ( A , A 1 , A 2 , ( m 0 , … , m n − 1 ) ) ∧ j 0 = 0 ∧ … ∧ j n − 1 = 0 } ⇒ { Q l e n ( A , A 1 , A 2 , ( m 0 , … , m n − 1 ) ) ∧ j 0 ∈ [ 0 , m 0 ] ∧ … ∧ j n − 1 ∈ [ 0 , m n − 1 ] ∧ ∀ k 0 ∈ [ 0 , j 0 ) … ∀ k n − 1 ∈ [ 0 , j n − 1 ) . A 2 [ k 0 , … , k n − 1 ] = u p a ( u p b ( A [ k 0 , … , k n − 1 ] , vector ( A 1 [ k 0 , … , k n − 1 ] ) , ind φ ( η ) ) , amp ( A 1 [ k 0 , … , k n − 1 ] ) ) } while ( j 0 < m 0 ) While { Q l e n ( A , A 1 , A 2 , ( m 0 , … , m n − 1 ) ) ∧ j 0 ∈ [ 0 , m 0 ] ∧ … ∧ j n − 1 ∈ [ 0 , m n − 1 ] ∧ ∀ k 0 ∈ [ 0 , j 0 ) … ∀ k n − 1 ∈ [ 0 , j n − 1 ) . A 2 [ k 0 , … , k n − 1 ] = u p a ( u p b ( A [ k 0 , … , k n − 1 ] , vector ( A 1 [ k 0 , … , k n − 1 ] ) , ind φ ( η ) ) , amp ( A 1 [ k 0 , … , k n − 1 ] ) ) } Invariant { … while ( j n − 1 < m n − 1 ) { Q l e n ( A , A 1 , A 2 , ( m 0 , … , m n − 1 ) ) ∧ j 0 ∈ [ 0 , m 0 ] ∧ … ∧ j n − 1 ∈ [ 0 , m n − 1 ] ∧ ∀ k n − 1 ∈ [ 0 , j n − 1 ) . A 2 [ j 0 , … , j n − 2 , k n − 1 ] = u p a ( u p b ( A [ j 0 , … , j n − 2 , k n − 1 ] , vector ( A 1 [ j 0 , … , j n − 2 , k n − 1 ] ) , ind φ ( η ) ) , amp ( A 1 [ j 0 , … , j n − 2 , k n − 1 ] ) ) } Invariant t : = u p b ( A [ k 0 , … , k n − 1 ] , vector ( A 1 [ k 0 , … , k n − 1 ] ) , ind φ ( η ) ) ; A 2 [ k 0 , … , k n − 1 ] : = u p a ( t , amp ( A 1 [ k 0 , … , k n − 1 ] ) ) ; j n − 1 : = j n − 1 + 1 ; } … } j 0 : = j 0 + 1 ; } { Q l e n ( A , A 1 , A 2 , ( m 0 , … , m n − 1 ) ) ∧ ¬ ( j 0 < m 0 ) ∧ … ∧ ¬ ( j n − 1 < m n − 1 ) ∧ j 0 ∈ [ 0 , m 0 ] ∧ … ∧ j n − 1 ∈ [ 0 , m n − 1 ] ∧ ∀ k 0 ∈ [ 0 , j 0 ) … ∀ k n − 1 ∈ [ 0 , j n − 1 ) . A 2 [ k 0 , … , k n − 1 ] = u p a ( u p b ( A [ k 0 , … , k n − 1 ] , vector ( A 1 [ k 0 , … , k n − 1 ] ) , ind φ ( η ) ) , amp ( A 1 [ k 0 , … , k n − 1 ] ) ) } ⇒ { Q l e n ( A , A 1 , A 2 , ( m 0 , … , m n − 1 ) ) ∧ ∀ k 0 ∈ [ 0 , m 0 ) … ∀ k n − 1 ∈ [ 0 , m n − 1 ) . A 2 [ k 0 , … , k n − 1 ] = u p a ( u p b ( A [ k 0 , … , k n − 1 ] , vector ( A 1 [ k 0 , … , k n − 1 ] ) , ind φ ( η ) ) , amp ( A 1 [ k 0 , … , k n − 1 ] ) ) } Post-Weakening \begin{aligned}
&\{Q_{\mathrm{len}}(A,A_{1},A_{2},(m_{0},\ldots,m_{n-1}))\}\\
&\qquad j_{0}:=0;\ldots;j_{n-1}:=0; && \text{Assign}\\
&\{Q_{\mathrm{len}}(A,A_{1},A_{2},(m_{0},\ldots,m_{n-1}))\land j_{0}=0\land\ldots\land j_{n-1}=0\}\\
&\Rightarrow\{Q_{\mathrm{len}}(A,A_{1},A_{2},(m_{0},\ldots,m_{n-1}))\land j_{0}\in[0,m_{0}]\land\ldots\land j_{n-1}\in[0,m_{n-1}]\\
&\qquad\land\forall k_{0}\in[0,j_{0})\ldots\forall k_{n-1}\in[0,j_{n-1}).\ A_{2}[k_{0},\ldots,k_{n-1}]\\
&\qquad\qquad=\mathrm{up}^{a}(\mathrm{up}^{b}(A[k_{0},\ldots,k_{n-1}],\operatorname{vector}(A_{1}[k_{0},\ldots,k_{n-1}]),\operatorname{ind}^{\varphi}(\eta)),\operatorname{amp}(A_{1}[k_{0},\ldots,k_{n-1}]))\}\\
&\qquad\texttt{while}(j_{0}<m_{0}) && \text{While}\\
&\{Q_{\mathrm{len}}(A,A_{1},A_{2},(m_{0},\ldots,m_{n-1}))\land j_{0}\in[0,m_{0}]\land\ldots\land j_{n-1}\in[0,m_{n-1}]\\
&\qquad\land\forall k_{0}\in[0,j_{0})\ldots\forall k_{n-1}\in[0,j_{n-1}).\ A_{2}[k_{0},\ldots,k_{n-1}]\\
&\qquad\qquad=\mathrm{up}^{a}(\mathrm{up}^{b}(A[k_{0},\ldots,k_{n-1}],\operatorname{vector}(A_{1}[k_{0},\ldots,k_{n-1}]),\operatorname{ind}^{\varphi}(\eta)),\operatorname{amp}(A_{1}[k_{0},\ldots,k_{n-1}]))\} && \text{Invariant}\\
&\qquad\{\,\ldots\\
&\qquad\texttt{while}(j_{n-1}<m_{n-1})\\
&\qquad\{Q_{\mathrm{len}}(A,A_{1},A_{2},(m_{0},\ldots,m_{n-1}))\land j_{0}\in[0,m_{0}]\land\ldots\land j_{n-1}\in[0,m_{n-1}]\\
&\qquad\qquad\land\forall k_{n-1}\in[0,j_{n-1}).\ A_{2}[j_{0},\ldots,j_{n-2},k_{n-1}]\\
&\qquad\qquad\qquad=\mathrm{up}^{a}(\mathrm{up}^{b}(A[j_{0},\ldots,j_{n-2},k_{n-1}],\operatorname{vector}(A_{1}[j_{0},\ldots,j_{n-2},k_{n-1}]),\operatorname{ind}^{\varphi}(\eta)),\operatorname{amp}(A_{1}[j_{0},\ldots,j_{n-2},k_{n-1}]))\} && \text{Invariant}\\
&\qquad t:=\mathrm{up}^{b}(A[k_{0},\ldots,k_{n-1}],\operatorname{vector}(A_{1}[k_{0},\ldots,k_{n-1}]),\operatorname{ind}^{\varphi}(\eta));\\
&\qquad A_{2}[k_{0},\ldots,k_{n-1}]:=\mathrm{up}^{a}(t,\operatorname{amp}(A_{1}[k_{0},\ldots,k_{n-1}]));\\
&\qquad j_{n-1}:=j_{n-1}+1;\\
&\qquad\}\\
&\qquad\ldots\\
&\qquad\}\\
&\qquad j_{0}:=j_{0}+1;\\
&\qquad\}\\
&\{Q_{\mathrm{len}}(A,A_{1},A_{2},(m_{0},\ldots,m_{n-1}))\land\neg(j_{0}<m_{0})\land\ldots\land\neg(j_{n-1}<m_{n-1})\\
&\qquad\land j_{0}\in[0,m_{0}]\land\ldots\land j_{n-1}\in[0,m_{n-1}]\\
&\qquad\land\forall k_{0}\in[0,j_{0})\ldots\forall k_{n-1}\in[0,j_{n-1}).\ A_{2}[k_{0},\ldots,k_{n-1}]\\
&\qquad\qquad=\mathrm{up}^{a}(\mathrm{up}^{b}(A[k_{0},\ldots,k_{n-1}],\operatorname{vector}(A_{1}[k_{0},\ldots,k_{n-1}]),\operatorname{ind}^{\varphi}(\eta)),\operatorname{amp}(A_{1}[k_{0},\ldots,k_{n-1}]))\}\\
&\Rightarrow\{Q_{\mathrm{len}}(A,A_{1},A_{2},(m_{0},\ldots,m_{n-1}))\\
&\qquad\land\forall k_{0}\in[0,m_{0})\ldots\forall k_{n-1}\in[0,m_{n-1}).\ A_{2}[k_{0},\ldots,k_{n-1}]\\
&\qquad\qquad=\mathrm{up}^{a}(\mathrm{up}^{b}(A[k_{0},\ldots,k_{n-1}],\operatorname{vector}(A_{1}[k_{0},\ldots,k_{n-1}]),\operatorname{ind}^{\varphi}(\eta)),\operatorname{amp}(A_{1}[k_{0},\ldots,k_{n-1}]))\} && \text{Post-Weakening}
\end{aligned} { Q len ( A , A 1 , A 2 , ( m 0 , … , m n − 1 ))} j 0 := 0 ; … ; j n − 1 := 0 ; { Q len ( A , A 1 , A 2 , ( m 0 , … , m n − 1 )) ∧ j 0 = 0 ∧ … ∧ j n − 1 = 0 } ⇒ { Q len ( A , A 1 , A 2 , ( m 0 , … , m n − 1 )) ∧ j 0 ∈ [ 0 , m 0 ] ∧ … ∧ j n − 1 ∈ [ 0 , m n − 1 ] ∧ ∀ k 0 ∈ [ 0 , j 0 ) … ∀ k n − 1 ∈ [ 0 , j n − 1 ) . A 2 [ k 0 , … , k n − 1 ] = up a ( up b ( A [ k 0 , … , k n − 1 ] , vector ( A 1 [ k 0 , … , k n − 1 ]) , ind φ ( η )) , amp ( A 1 [ k 0 , … , k n − 1 ]))} while ( j 0 < m 0 ) { Q len ( A , A 1 , A 2 , ( m 0 , … , m n − 1 )) ∧ j 0 ∈ [ 0 , m 0 ] ∧ … ∧ j n − 1 ∈ [ 0 , m n − 1 ] ∧ ∀ k 0 ∈ [ 0 , j 0 ) … ∀ k n − 1 ∈ [ 0 , j n − 1 ) . A 2 [ k 0 , … , k n − 1 ] = up a ( up b ( A [ k 0 , … , k n − 1 ] , vector ( A 1 [ k 0 , … , k n − 1 ]) , ind φ ( η )) , amp ( A 1 [ k 0 , … , k n − 1 ]))} { … while ( j n − 1 < m n − 1 ) { Q len ( A , A 1 , A 2 , ( m 0 , … , m n − 1 )) ∧ j 0 ∈ [ 0 , m 0 ] ∧ … ∧ j n − 1 ∈ [ 0 , m n − 1 ] ∧ ∀ k n − 1 ∈ [ 0 , j n − 1 ) . A 2 [ j 0 , … , j n − 2 , k n − 1 ] = up a ( up b ( A [ j 0 , … , j n − 2 , k n − 1 ] , vector ( A 1 [ j 0 , … , j n − 2 , k n − 1 ]) , ind φ ( η )) , amp ( A 1 [ j 0 , … , j n − 2 , k n − 1 ]))} t := up b ( A [ k 0 , … , k n − 1 ] , vector ( A 1 [ k 0 , … , k n − 1 ]) , ind φ ( η )) ; A 2 [ k 0 , … , k n − 1 ] := up a ( t , amp ( A 1 [ k 0 , … , k n − 1 ])) ; j n − 1 := j n − 1 + 1 ; } … } j 0 := j 0 + 1 ; } { Q len ( A , A 1 , A 2 , ( m 0 , … , m n − 1 )) ∧ ¬ ( j 0 < m 0 ) ∧ … ∧ ¬ ( j n − 1 < m n − 1 ) ∧ j 0 ∈ [ 0 , m 0 ] ∧ … ∧ j n − 1 ∈ [ 0 , m n − 1 ] ∧ ∀ k 0 ∈ [ 0 , j 0 ) … ∀ k n − 1 ∈ [ 0 , j n − 1 ) . A 2 [ k 0 , … , k n − 1 ] = up a ( up b ( A [ k 0 , … , k n − 1 ] , vector ( A 1 [ k 0 , … , k n − 1 ]) , ind φ ( η )) , amp ( A 1 [ k 0 , … , k n − 1 ]))} ⇒ { Q len ( A , A 1 , A 2 , ( m 0 , … , m n − 1 )) ∧ ∀ k 0 ∈ [ 0 , m 0 ) … ∀ k n − 1 ∈ [ 0 , m n − 1 ) . A 2 [ k 0 , … , k n − 1 ] = up a ( up b ( A [ k 0 , … , k n − 1 ] , vector ( A 1 [ k 0 , … , k n − 1 ]) , ind φ ( η )) , amp ( A 1 [ k 0 , … , k n − 1 ]))} Assign While Invariant Invariant Post-Weakening
Proof Tree for enumN
We continue the proof of Lemma 7.6 7.6 7.6 (§ 7.2 ), showing the validity of the triple obtained there via Lemma 2.1 2.1 2.1 .
The proof tree is shown below. The pre-condition strengthening rules utilize the Q l e n Q_{\mathrm{len}} Q len axioms for A 1 A_{1} A 1 , A a A_{a} A a , A b A_{b} A b , A 2 A_{2} A 2 , A a { E 1 } A_{a}\{E_{1}\} A a { E 1 } , A b { E 2 } A_{b}\{E_{2}\} A b { E 2 } , and A 2 { E 3 } A_{2}\{E_{3}\} A 2 { E 3 } , as they all have the same bounds ( m ‾ , 2 ∣ n ∣ ) (\overline{m},2^{|n|}) ( m , 2 ∣ n ∣ ) . let
E 1 : = ∀ j ∈ range ( m ‾ ) . ∀ k ∈ [ 0 , 2 l n ) . ( j ‾ , k ) ↦ u p b ( A 1 [ j ‾ , k ] , vector ( A 1 [ j ‾ , k ] ) [ ind φ ( η ) ] , ind φ ( η ) ) E_1 := \forall j \in\operatorname{range}(\overline{m}).\forall k \in[0,2^{l^n}).(\overline{j},k) \mapsto u p^b(A_1[\overline{j},k],\operatorname{vector}(A_1[\overline{j},k])[\operatorname{ind}^{\varphi}(\eta)],\operatorname{ind}^{\varphi}(\eta)) E 1 := ∀ j ∈ range ( m ) .∀ k ∈ [ 0 , 2 l n ) . ( j , k ) ↦ u p b ( A 1 [ j , k ] , vector ( A 1 [ j , k ]) [ ind φ ( η )] , ind φ ( η ))
E 2 : = ∀ j ∈ range ( m ‾ ) . ∀ k ∈ [ 0 , 2 l n ) . ( j ‾ , k ) ↦ u p b ( A a [ j ‾ , k ] , k , ind φ ( η ) ) E_2 := \forall j \in\operatorname{range}(\overline{m}).\forall k \in[0,2^{l^n}).(\overline{j},k) \mapsto u p^b(A_a[\overline{j},k],k,\operatorname{ind}^{\varphi}(\eta)) E 2 := ∀ j ∈ range ( m ) .∀ k ∈ [ 0 , 2 l n ) . ( j , k ) ↦ u p b ( A a [ j , k ] , k , ind φ ( η ))
E 3 : = ∀ j ∈ range ( m ‾ ) . ∀ k ∈ [ 0 , 2 l n ) . ( j , k ) ↦ u p a ( u p b ( A 1 [ j ‾ , k ] , vector ( A b [ j ‾ , k ] ) , ind φ ( η ) ) , amp ( A b [ j ‾ , k ] ) ) E_3 := \forall j \in\operatorname{range}(\overline{m}).\forall k \in[0,2^{l^n}).(j,k) \mapsto u p^a(u p^b(A_1[\overline{j},k],\operatorname{vector}(A_b[\overline{j},k]),\operatorname{ind}^{\varphi}(\eta)),\operatorname{amp}(A_b[\overline{j},k])) E 3 := ∀ j ∈ range ( m ) .∀ k ∈ [ 0 , 2 l n ) . ( j , k ) ↦ u p a ( u p b ( A 1 [ j , k ] , vector ( A b [ j , k ]) , ind φ ( η )) , amp ( A b [ j , k ]))
{ Q len ( A 1 , m ‾ , 2 l n ) } \begin{aligned}
\{Q_{\operatorname{len}}(A_1,\overline{m},2^{l^n})\}
\end{aligned} { Q len ( A 1 , m , 2 l n )}
⇒ { ∀ j ∈ range ( m ‾ ) . ∀ k ∈ [ 0 , 2 l n ) . j ‾ , k ↦ u p b ( A 1 [ j ‾ , k ] , k , ind φ ( η ) ) = u p b ( A 1 [ j ‾ , k ] , k , ind φ ( η ) ) ∧ Q len ( A 1 , A a { E 1 } , m ‾ , 2 l n ) } \begin{aligned}
\Rightarrow\ \{&\forall j \in\operatorname{range}(\overline{m}).\forall k \in[0,2^{l^n}).\overline{j},k \mapsto u p^b(A_1[\overline{j},k],k,\operatorname{ind}^{\varphi}(\eta))\\
&=u p^b(A_1[\overline{j},k],k,\operatorname{ind}^{\varphi}(\eta))\land Q_{\operatorname{len}}(A_1,A_a\{E_1\},\overline{m},2^{l^n})\}
\end{aligned} ⇒ { ∀ j ∈ range ( m ) .∀ k ∈ [ 0 , 2 l n ) . j , k ↦ u p b ( A 1 [ j , k ] , k , ind φ ( η )) = u p b ( A 1 [ j , k ] , k , ind φ ( η )) ∧ Q len ( A 1 , A a { E 1 } , m , 2 l n )}
Pre-Strength
≡ { ∀ j ∈ range ( m ‾ ) . ∀ k ∈ [ 0 , 2 l n ) . A 2 { E 3 [ A b { E 2 [ A a { E 1 } / A a ] } / A b ] } [ j ‾ , k ] = u p b ( A 1 [ j ‾ , k ] , k , ind φ ( η ) ) ∧ Q len ( A 1 , A a { E 1 } , ( m ‾ , 2 l n ) ) } \begin{aligned}
\equiv\ \{&\forall j \in\operatorname{range}(\overline{m}).\forall k \in[0,2^{l^n}).A_2\{E_3[A_b\{E_2[A_a\{E_1\}/A_a]\}/A_b]\}[\overline{j},k]\\
&=u p^b(A_1[\overline{j},k],k,\operatorname{ind}^{\varphi}(\eta))\land Q_{\operatorname{len}}(A_1,A_a\{E_1\},(\overline{m},2^{l^n}))\}
\end{aligned} ≡ { ∀ j ∈ range ( m ) .∀ k ∈ [ 0 , 2 l n ) . A 2 { E 3 [ A b { E 2 [ A a { E 1 } / A a ]} / A b ]} [ j , k ] = u p b ( A 1 [ j , k ] , k , ind φ ( η )) ∧ Q len ( A 1 , A a { E 1 } , ( m , 2 l n ))}
A a : = locateN ( A 1 , m ‾ , ind φ ( η ) ) A_a := \operatorname{locateN}(A_1,\overline{m},\operatorname{ind}^{\varphi}(\eta)) A a := locateN ( A 1 , m , ind φ ( η )) ; LocateN
{ ∀ j ∈ range ( m ‾ ) . ∀ k ∈ [ 0 , 2 l n ) . A 2 { E 3 [ A b { E 2 } / A b ] } [ j ‾ , k ] = u p b ( A 1 [ j ‾ , k ] , k , ind φ ( η ) ) ∧ Q len ( A 1 , A a , ( m ‾ , 2 l n ) ) } \begin{aligned}
\{&\forall j \in\operatorname{range}(\overline{m}).\forall k \in[0,2^{l^n}).A_2\{E_3[A_b\{E_2\}/A_b]\}[\overline{j},k]\\
&=u p^b(A_1[\overline{j},k],k,\operatorname{ind}^{\varphi}(\eta))\land Q_{\operatorname{len}}(A_1,A_a,(\overline{m},2^{l^n}))\}
\end{aligned} { ∀ j ∈ range ( m ) .∀ k ∈ [ 0 , 2 l n ) . A 2 { E 3 [ A b { E 2 } / A b ]} [ j , k ] = u p b ( A 1 [ j , k ] , k , ind φ ( η )) ∧ Q len ( A 1 , A a , ( m , 2 l n ))}
⇒ { ∀ j ∈ range ( m ‾ ) . ∀ k ∈ [ 0 , 2 l n ) . A 2 { E 3 [ A b { E 2 } / A b ] } [ j ‾ , k ] = u p b ( A 1 [ j ‾ , k ] , k , ind φ ( η ) ) ∧ Q len ( A 1 , A 2 { E 3 [ A b { E 2 } / A b ] } , A a , A b { E 2 } , ( m ‾ , 2 l n ) ) } \begin{aligned}
\Rightarrow\ \{&\forall j \in\operatorname{range}(\overline{m}).\forall k \in[0,2^{l^n}).A_2\{E_3[A_b\{E_2\}/A_b]\}[\overline{j},k]\\
&=u p^b(A_1[\overline{j},k],k,\operatorname{ind}^{\varphi}(\eta))\land Q_{\operatorname{len}}(A_1,A_2\{E_3[A_b\{E_2\}/A_b]\},A_a,A_b\{E_2\},(\overline{m},2^{l^n}))\}
\end{aligned} ⇒ { ∀ j ∈ range ( m ) .∀ k ∈ [ 0 , 2 l n ) . A 2 { E 3 [ A b { E 2 } / A b ]} [ j , k ] = u p b ( A 1 [ j , k ] , k , ind φ ( η )) ∧ Q len ( A 1 , A 2 { E 3 [ A b { E 2 } / A b ]} , A a , A b { E 2 } , ( m , 2 l n ))}
Pre-Strength
A b : = mapN ( A a , m ‾ , λ ( x , y ) . ( x , enum ( 2 l n ) ) ) A_b := \operatorname{mapN}(A_a,\overline{m},\lambda(x,y).(x,\operatorname{enum}(2^{l^n}))) A b := mapN ( A a , m , λ ( x , y ) . ( x , enum ( 2 l n ))) ; MapN
{ ∀ j ∈ range ( m ‾ ) . ∀ k ∈ [ 0 , 2 l n ) . A 2 { E 3 } [ j ‾ , k ] = u p b ( A 1 [ j ‾ , k ] , k , ind φ ( η ) ) ∧ Q len ( A 1 , A 2 { E 3 } , A a , A b , ( m ‾ , 2 l n ) ) } \begin{aligned}
\{&\forall j \in\operatorname{range}(\overline{m}).\forall k \in[0,2^{l^n}).A_2\{E_3\}[\overline{j},k]=u p^b(A_1[\overline{j},k],k,\operatorname{ind}^{\varphi}(\eta))\\
&\land Q_{\operatorname{len}}(A_1,A_2\{E_3\},A_a,A_b,(\overline{m},2^{l^n}))\}
\end{aligned} { ∀ j ∈ range ( m ) .∀ k ∈ [ 0 , 2 l n ) . A 2 { E 3 } [ j , k ] = u p b ( A 1 [ j , k ] , k , ind φ ( η )) ∧ Q len ( A 1 , A 2 { E 3 } , A a , A b , ( m , 2 l n ))}
A 2 : = assignN ( A 1 , A b , m ‾ , ind φ ( η ) , λ u v . amp ( u ) ) A_2 := \operatorname{assignN}(A_1,A_b,\overline{m},\operatorname{ind}^{\varphi}(\eta),\lambda u\ v.\operatorname{amp}(u)) A 2 := assignN ( A 1 , A b , m , ind φ ( η ) , λ u v . amp ( u )) ; AssignN
{ ∀ j ∈ range ( m ‾ ) . ∀ k ∈ [ 0 , 2 l n ) . A 2 [ j ‾ , k ] = u p b ( A 1 [ j ‾ , k ] , k , ind φ ( η ) ) ∧ Q len ( A 1 , A 2 , A a , A b , ( m ‾ , 2 l n ) ) } \begin{aligned}
\{\forall j \in\operatorname{range}(\overline{m}).\forall k \in[0,2^{l^n}).A_2[\overline{j},k]=u p^b(A_1[\overline{j},k],k,\operatorname{ind}^{\varphi}(\eta))\\
\land Q_{\operatorname{len}}(A_1,A_2,A_a,A_b,(\overline{m},2^{l^n}))\}
\end{aligned} { ∀ j ∈ range ( m ) .∀ k ∈ [ 0 , 2 l n ) . A 2 [ j , k ] = u p b ( A 1 [ j , k ] , k , ind φ ( η )) ∧ Q len ( A 1 , A 2 , A a , A b , ( m , 2 l n ))}
⇒ { ∀ j ∈ range ( m ‾ ) . ∀ k ∈ [ 0 , 2 l n ) . A 2 [ j ‾ , k ] = u p b ( A 1 [ j ‾ , k ] , k , ind φ ( η ) ) ∧ Q len ( A 1 , A 2 , ( m ‾ , 2 l n ) ) } \begin{aligned}
\Rightarrow\ \{\forall j \in\operatorname{range}(\overline{m}).\forall k \in[0,2^{l^n}).A_2[\overline{j},k]=u p^b(A_1[\overline{j},k],k,\operatorname{ind}^{\varphi}(\eta))\\
\land Q_{\operatorname{len}}(A_1,A_2,(\overline{m},2^{l^n}))\}
\end{aligned} ⇒ { ∀ j ∈ range ( m ) .∀ k ∈ [ 0 , 2 l n ) . A 2 [ j , k ] = u p b ( A 1 [ j , k ] , k , ind φ ( η )) ∧ Q len ( A 1 , A 2 , ( m , 2 l n ))}
Post-Weakening
There is a single enum function above. We show the enum function proof below.
Lemma A.1 (A Single Enumeration Function). Given a length n n n , we define a single enumeration function enum ( n ) \operatorname{enum}(n) enum ( n ) to generate an array A A A as the following valid Hoare triple.
{ 0 ≤ n } A : = enum ( n ) { ∀ k ∈ [ 0 , n ) . A [ k ] = k ∧ ∣ A ∣ = n } \{0 \le n\}\ A := \operatorname{enum}(n)\ \{\forall k \in[0,n).A[k]=k\land|A|=n\} { 0 ≤ n } A := enum ( n ) { ∀ k ∈ [ 0 , n ) . A [ k ] = k ∧ ∣ A ∣ = n }
Proof. The proof of the valid Hoare triple is given as follows.
A : = init ( n ) ; A := \operatorname{init}(n); A := init ( n ) ;
{ 0 ≤ n ∧ ∣ A ∣ = n } ⇒ { j ∈ [ 0 , n ] ∧ ∀ k ∈ [ 0 , j ) . A [ k ] = k ∧ ∣ A ∣ = n } \begin{aligned}
\{0 \le n\land|A|=n\} \\
&\Rightarrow\ \{j \in[0,n]\land\forall k \in[0,j).A[k]=k\land|A|=n\}
\end{aligned} { 0 ≤ n ∧ ∣ A ∣ = n } ⇒ { j ∈ [ 0 , n ] ∧ ∀ k ∈ [ 0 , j ) . A [ k ] = k ∧ ∣ A ∣ = n }
while ( j < n ) \operatorname{while}(j<n) while ( j < n )
{ j ∈ [ 0 , n ] ∧ ∀ k ∈ [ 0 , j ) . A [ k ] = k ∧ ∣ A ∣ = n } \{j \in[0,n]\land\forall k \in[0,j).A[k]=k\land|A|=n\} { j ∈ [ 0 , n ] ∧ ∀ k ∈ [ 0 , j ) . A [ k ] = k ∧ ∣ A ∣ = n }
{ A [ j ] : = j ; j : = j + 1 ; \left\{
\begin{aligned}
A[j] &:= j;\\
j &:= j+1;
\end{aligned}
\right. { A [ j ] j := j ; := j + 1 ;
{ ¬ ( j < n ) ∧ j ∈ [ 0 , n ] ∧ ∀ k ∈ [ 0 , j ) . A [ k ] = k ∧ ∣ A ∣ = n } ⇒ { ∀ k ∈ [ 0 , n ) . A [ k ] = k ∧ ∣ A ∣ = n } \begin{aligned}
\{\neg(j<n)\land j \in[0,n]\land\forall k \in[0,j).A[k]=k\land|A|=n\} \\
&\Rightarrow\ \{\forall k \in[0,n).A[k]=k\land|A|=n\}
\end{aligned} { ¬ ( j < n ) ∧ j ∈ [ 0 , n ] ∧ ∀ k ∈ [ 0 , j ) . A [ k ] = k ∧ ∣ A ∣ = n } ⇒ { ∀ k ∈ [ 0 , n ) . A [ k ] = k ∧ ∣ A ∣ = n }
We show the proof for a single ampMult function below.
Lemma A.2 (A Single Amplitude Multiplication Function). Given two bitstring arrays A 1 A_1 A 1 and A 2 A_2 A 2 , a collection of indices B B B , and a complex number α \alpha α , we define a single amplitude multiplication function to compute a complex number α ′ \alpha' α ′ below. ⋅ \cdot ⋅ is a dot product.
{ ∣ B ∣ ≤ ∣ A 1 ∣ ∧ ∣ B ∣ ≤ ∣ A 2 ∣ ∧ ∀ k ∈ [ 0 , ∣ B ∣ ) . B [ k ] ∈ [ 0 , ∣ A 1 ∣ ) ∧ B [ k ] ∈ [ 0 , ∣ A 2 ∣ ) } α ′ : = ampMult ( A 1 , A 2 , α , B ) { α ′ = α ∗ exp ( 2 π i ( group ( ∣ A 1 ∣ , A 1 , B ) ⋅ group ( ∣ A 2 ∣ , A 2 , B ) ) ) } \begin{aligned}
&\{|B| \le|A_1| \land|B| \le|A_2| \land\forall k \in[0,|B|). B[k] \in[0,|A_1|) \land B[k] \in[0,|A_2|)\}\\
&\alpha' := \operatorname{ampMult}(A_1,A_2,\alpha,B)\\
&\{\alpha' = \alpha* \exp(2\pi i(\operatorname{group}(|A_1|,A_1,B)\cdot\operatorname{group}(|A_2|,A_2,B)))\}
\end{aligned} { ∣ B ∣ ≤ ∣ A 1 ∣ ∧ ∣ B ∣ ≤ ∣ A 2 ∣ ∧ ∀ k ∈ [ 0 , ∣ B ∣ ) . B [ k ] ∈ [ 0 , ∣ A 1 ∣ ) ∧ B [ k ] ∈ [ 0 , ∣ A 2 ∣ )} α ′ := ampMult ( A 1 , A 2 , α , B ) { α ′ = α ∗ exp ( 2 π i ( group ( ∣ A 1 ∣ , A 1 , B ) ⋅ group ( ∣ A 2 ∣ , A 2 , B )))}
Proof. We implement ampMult ( A 1 , A 2 , α , B ) \operatorname{ampMult}(A_1,A_2,\alpha,B) ampMult ( A 1 , A 2 , α , B ) to be the program A 1 ′ : = group ( ∣ A 1 ∣ , A 1 , B ) ; A 2 ′ : = group ( ∣ A 2 ∣ , A 2 , B ) ; α ′ : = α ⋅ exp ( 2 π i ( A 1 ′ ⋅ A 2 ′ ) ) A'_1 := \operatorname{group}(|A_1|,A_1,B); A'_2 := \operatorname{group}(|A_2|,A_2,B); \alpha' := \alpha\cdot\exp(2\pi i(A'_1 \cdot A'_2)) A 1 ′ := group ( ∣ A 1 ∣ , A 1 , B ) ; A 2 ′ := group ( ∣ A 2 ∣ , A 2 , B ) ; α ′ := α ⋅ exp ( 2 π i ( A 1 ′ ⋅ A 2 ′ )) .
{ ∣ B ∣ ≤ ∣ A 1 ∣ ∧ ∣ B ∣ ≤ ∣ A 2 ∣ ∧ ∀ k ∈ [ 0 , ∣ B ∣ ) . B [ k ] ∈ [ 0 , ∣ A 1 ∣ ) ∧ B [ k ] ∈ [ 0 , ∣ A 2 ∣ ) } A 1 ′ : = group ( ∣ A 1 ∣ , A 1 , B ) ; { ∣ B ∣ ≤ ∣ A 1 ∣ ∧ ∣ B ∣ ≤ ∣ A 2 ∣ ∧ ∀ k ∈ [ 0 , ∣ B ∣ ) . B [ k ] ∈ [ 0 , ∣ A 1 ∣ ) ∧ B [ k ] ∈ [ 0 , ∣ A 2 ∣ ) ∧ ∀ k ∈ [ 0 , ∣ B ∣ ) . A 1 ′ [ k ] = A 1 [ B [ k ] ] } A 2 ′ : = group ( ∣ A 2 ∣ , A 2 , B ) ; { ∣ B ∣ ≤ ∣ A 1 ∣ ∧ ∣ B ∣ ≤ ∣ A 2 ∣ ∧ ∀ k ∈ [ 0 , ∣ B ∣ ) . B [ k ] ∈ [ 0 , ∣ A 1 ∣ ) ∧ B [ k ] ∈ [ 0 , ∣ A 2 ∣ ) ∧ ∀ k ∈ [ 0 , ∣ B ∣ ) . A 1 ′ [ k ] = A 1 [ B [ k ] ] ∧ A 2 ′ [ k ] = A 2 [ B [ k ] ] } α ′ : = α ⋅ exp ( 2 π i ( A 1 ′ ⋅ A 2 ′ ) ) ; { ∣ B ∣ ≤ ∣ A 1 ∣ ∧ ∣ B ∣ ≤ ∣ A 2 ∣ ∧ ∀ k ∈ [ 0 , ∣ B ∣ ) . B [ k ] ∈ [ 0 , ∣ A 1 ∣ ) ∧ B [ k ] ∈ [ 0 , ∣ A 2 ∣ ) ∧ ∀ k ∈ [ 0 , ∣ B ∣ ) . A 1 ′ [ k ] = A 1 [ B [ k ] ] ∧ A 2 ′ [ k ] = A 2 [ B [ k ] ] ∧ α ′ = α ⋅ exp ( 2 π i ( ∑ k = 0 ∣ B ∣ − 1 A 1 ′ [ k ] ⋅ A 2 ′ [ k ] ) ) } ⇒ { α ′ = α ∗ exp ( 2 π i ( group ( ∣ A 1 ∣ , A 1 , B ) ⋅ group ( ∣ A 2 ∣ , A 2 , B ) ) ) } \begin{aligned}
&\{|B| \le|A_1| \land|B| \le|A_2| \land\forall k \in[0,|B|). B[k] \in[0,|A_1|) \land B[k] \in[0,|A_2|)\}\\
&A'_1 := \operatorname{group}(|A_1|,A_1,B);\\
&\{|B| \le|A_1| \land|B| \le|A_2| \land\forall k \in[0,|B|). B[k] \in[0,|A_1|) \land B[k] \in[0,|A_2|) \land\forall k \in[0,|B|). A'_1[k] = A_1[B[k]]\}\\
&A'_2 := \operatorname{group}(|A_2|,A_2,B);\\
&\{|B| \le|A_1| \land|B| \le|A_2| \land\forall k \in[0,|B|). B[k] \in[0,|A_1|) \land B[k] \in[0,|A_2|)\\
&\qquad\land\forall k \in[0,|B|). A'_1[k] = A_1[B[k]] \land A'_2[k] = A_2[B[k]]\}\\
&\alpha' := \alpha\cdot\exp(2\pi i(A'_1 \cdot A'_2));\\
&\{|B| \le|A_1| \land|B| \le|A_2| \land\forall k \in[0,|B|). B[k] \in[0,|A_1|) \land B[k] \in[0,|A_2|)\\
&\qquad\land\forall k \in[0,|B|). A'_1[k] = A_1[B[k]] \land A'_2[k] = A_2[B[k]] \land\alpha' = \alpha\cdot\exp(2\pi i(\sum_{k=0}^{|B|-1} A'_1[k]\cdot A'_2[k]))\}\\
&\Rightarrow\{\alpha' = \alpha* \exp(2\pi i(\operatorname{group}(|A_1|,A_1,B)\cdot\operatorname{group}(|A_2|,A_2,B)))\}
\end{aligned} { ∣ B ∣ ≤ ∣ A 1 ∣ ∧ ∣ B ∣ ≤ ∣ A 2 ∣ ∧ ∀ k ∈ [ 0 , ∣ B ∣ ) . B [ k ] ∈ [ 0 , ∣ A 1 ∣ ) ∧ B [ k ] ∈ [ 0 , ∣ A 2 ∣ )} A 1 ′ := group ( ∣ A 1 ∣ , A 1 , B ) ; { ∣ B ∣ ≤ ∣ A 1 ∣ ∧ ∣ B ∣ ≤ ∣ A 2 ∣ ∧ ∀ k ∈ [ 0 , ∣ B ∣ ) . B [ k ] ∈ [ 0 , ∣ A 1 ∣ ) ∧ B [ k ] ∈ [ 0 , ∣ A 2 ∣ ) ∧ ∀ k ∈ [ 0 , ∣ B ∣ ) . A 1 ′ [ k ] = A 1 [ B [ k ]]} A 2 ′ := group ( ∣ A 2 ∣ , A 2 , B ) ; { ∣ B ∣ ≤ ∣ A 1 ∣ ∧ ∣ B ∣ ≤ ∣ A 2 ∣ ∧ ∀ k ∈ [ 0 , ∣ B ∣ ) . B [ k ] ∈ [ 0 , ∣ A 1 ∣ ) ∧ B [ k ] ∈ [ 0 , ∣ A 2 ∣ ) ∧ ∀ k ∈ [ 0 , ∣ B ∣ ) . A 1 ′ [ k ] = A 1 [ B [ k ]] ∧ A 2 ′ [ k ] = A 2 [ B [ k ]]} α ′ := α ⋅ exp ( 2 π i ( A 1 ′ ⋅ A 2 ′ )) ; { ∣ B ∣ ≤ ∣ A 1 ∣ ∧ ∣ B ∣ ≤ ∣ A 2 ∣ ∧ ∀ k ∈ [ 0 , ∣ B ∣ ) . B [ k ] ∈ [ 0 , ∣ A 1 ∣ ) ∧ B [ k ] ∈ [ 0 , ∣ A 2 ∣ ) ∧ ∀ k ∈ [ 0 , ∣ B ∣ ) . A 1 ′ [ k ] = A 1 [ B [ k ]] ∧ A 2 ′ [ k ] = A 2 [ B [ k ]] ∧ α ′ = α ⋅ exp ( 2 π i ( k = 0 ∑ ∣ B ∣ − 1 A 1 ′ [ k ] ⋅ A 2 ′ [ k ]))} ⇒ { α ′ = α ∗ exp ( 2 π i ( group ( ∣ A 1 ∣ , A 1 , B ) ⋅ group ( ∣ A 2 ∣ , A 2 , B )))}
Proof Tree for Quantum Conditionals
We continue the proof of Lemma 7.9 (§ 7.3 ), using the array-length axioms and the concat/filterN axiom stated there.
let E 1 : = ∀ j ∈ range ( u ‾ ) . j ↦ up b ( A 1 [ j ‾ ] , vector ( A 1 [ j ‾ ] ) , ind φ ( η ) ) E 2 : = ∀ j ∈ range ( u ‾ ) . j ↦ up a ( up b ( A 4 [ j ‾ ] , vector ( A 3 [ j ‾ ] ) , ind φ ( η ) ) , amp ( A 3 [ j ‾ ] ) ) \begin{aligned}
\text{let}\\
E_1 := \forall j \in\operatorname{range}(\overline{u}).j \mapsto\operatorname{up}^{b}(A_1[\overline{j}],\operatorname{vector}(A_1[\overline{j}]),\operatorname{ind}^{\varphi}(\eta))\\
E_2 := \forall j \in\operatorname{range}(\overline{u}).j \mapsto\operatorname{up}^{a}(\operatorname{up}^{b}(A_4[\overline{j}],\operatorname{vector}(A_3[\overline{j}]),\operatorname{ind}^{\varphi}(\eta)),\operatorname{amp}(A_3[\overline{j}]))
\end{aligned} let E 1 := ∀ j ∈ range ( u ) . j ↦ up b ( A 1 [ j ] , vector ( A 1 [ j ]) , ind φ ( η )) E 2 := ∀ j ∈ range ( u ) . j ↦ up a ( up b ( A 4 [ j ] , vector ( A 3 [ j ]) , ind φ ( η )) , amp ( A 3 [ j ]))
{ P } ⇒ { P [ concat ( filterN ( A , ¬ b , ind φ ( η ) ) , filterN ( A , b , ind φ ( η ) ) ) / A ] } A 1 : = filterN ( A , b , ind φ ( η ) ) ; u ˉ : = ∣ A 1 ∣ ; FilterN, Len { P [ concat ( filterN ( A , ¬ b , ind φ ( η ) ) , A 1 ) / A ] } A 2 : = filterN ( A , ¬ b , ind φ ( η ) ) ; t ˉ : = ∣ A 2 ∣ ; FilterN, Len { P [ concat ( A 2 , A 1 ) / A ] } ⇒ { P [ concat ( A 2 , t ˉ , ( t ˉ , 1 ˉ ) , upDim ( A 1 , u ˉ , ∣ A 3 { E 1 } ∣ ) { ∀ j ˉ ∈ range ( ∣ A 3 { E 1 } ∣ ) . j ˉ ↦ up a ( up b ( upDim ( A 1 , u ˉ , ∣ A 3 { E 1 } ∣ ) [ j ˉ ] , vector ( A 3 { E 1 } [ j ˉ ] ) , ind φ ( η ′ ) ) , amp ( A 3 { E 1 } [ j ˉ ] ) ) } ) A ] } A 3 : = locateN ( A 1 , u ˉ , ind φ ( η ′ ) ) ; LocateN { P [ concat ( upDim ( A 2 , t ˉ , ( t ˉ , 1 ˉ ) ) , upDim ( A 1 , u ˉ , ∣ A 3 ∣ ) { ∀ j ˉ ∈ range ( ∣ A 3 ∣ ) . j ˉ ↦ up a ( up b ( upDim ( A 1 , u ˉ , ∣ A 3 ∣ ) [ j ˉ ] , vector ( A 3 [ j ˉ ] ) , ind φ ( η ′ ) ) , amp ( A 3 [ j ˉ ] ) ) } ) A ] } e ; Upper { Q [ concat ( upDim ( A 2 , t ˉ , ( t ˉ , 1 ˉ ) ) , upDim ( A 1 , u ˉ , ∣ A 3 ∣ ) { ∀ j ˉ ∈ range ( ∣ A 3 ∣ ) . j ˉ ↦ up a ( up b ( upDim ( A 1 , u ˉ , ∣ A 3 ∣ ) [ j ˉ ] , vector ( A 3 [ j ˉ ] ) , ind φ ( η ′ ) ) , amp ( A 3 [ j ˉ ] ) ) } ) A ] } ⇒ { Q [ concat ( upDim ( A 2 , t ˉ , ( t ˉ , 1 ˉ ) ) , A 6 { E 2 [ upDim ( A 1 , u ˉ , u ˉ ) / A 4 ] [ ∣ A 3 ∣ / u ˉ ] } ) A ] ∧ Q len ( A 6 { E 2 [ upDim ( A 1 , u ˉ , u ˉ ) / A 4 ] [ ∣ A 3 ∣ / u ˉ ] } , ∣ A 3 ∣ ) ∧ Q len ( upDim ( A 2 , t ˉ , ( t ˉ , 1 ˉ ) ) , ( t ˉ , 1 ˉ ) ) } u ˉ ′ : = ∣ A 3 ∣ ; A 4 : = upDim ( A 1 , u ˉ , u ˉ ) ; Len, UpDim { Q [ concat ( upDim ( A 2 , t ˉ , ( t ˉ , 1 ˉ ) ) , A 6 { E 2 } ) A ] ∧ Q len ( A 6 { E 2 } , u ˉ ′ ) ∧ Q len ( upDim ( A 2 , t ˉ , ( t ˉ , 1 ˉ ) ) , ( t ˉ , 1 ˉ ) ) } A 5 : = upDim ( A 2 , t ˉ , ( t ˉ , 1 ˉ ) ) ; UpDim { Q [ concat ( A 5 , A 6 { E 2 } ) A ] ∧ Q len ( A 6 { E 2 } , u ˉ ′ ) ∧ Q len ( A 5 , ( t ˉ , 1 ˉ ) ) } A 6 : = assignN ( A 4 , A 3 , u ˉ ′ , ind φ ( η ′ ) , λ v l . amp ( l ) ) ; AssignN { Q [ concat ( A 5 , A 6 ) A ] ∧ Q len ( concat ( A 5 , A 6 ) , u ˉ ′ + ( t ˉ , 1 ˉ ) ) } A 7 : = concat ( A 5 , A 6 ) ; Concat { Q [ A 7 / A ] ∧ Q len ( A 7 , u ˉ ′ + ( t ˉ , 1 ˉ ) ) } A : = A 7 ; Assign { Q ∧ Q len ( A , u ˉ ′ + ( t ˉ , 1 ˉ ) ) } \begin{aligned}
\{P\} &\Rightarrow\{P[\operatorname{concat}(\operatorname{filterN}(A,\neg b,\operatorname{ind}^{\varphi}(\eta)),\operatorname{filterN}(A,b,\operatorname{ind}^{\varphi}(\eta)))/A]\} \\
&A_{1}:=\operatorname{filterN}(A,b,\operatorname{ind}^{\varphi}(\eta));\ \bar{u}:=|A_{1}|; \qquad\text{FilterN, Len}\\
&\{P[\operatorname{concat}(\operatorname{filterN}(A,\neg b,\operatorname{ind}^{\varphi}(\eta)),A_{1})/A]\}\\
&A_{2}:=\operatorname{filterN}(A,\neg b,\operatorname{ind}^{\varphi}(\eta));\ \bar{t}:=|A_{2}|; \qquad\text{FilterN, Len}\\
&\{P[\operatorname{concat}(A_{2},A_{1})/A]\}\\
&\Rightarrow\{P[\frac{\operatorname{concat}(A_{2},\bar{t},(\bar{t},\bar{1}),\operatorname{upDim}(A_{1},\bar{u},|A_{3}\{E_{1}\}|)\{\forall\bar{j}\in\operatorname{range}(|A_{3}\{E_{1}\}|).\ \bar{j}\mapsto\operatorname{up}^{a}(\operatorname{up}^{b}(\operatorname{upDim}(A_{1},\bar{u},|A_{3}\{E_{1}\}|)[\bar{j}],\operatorname{vector}(A_{3}\{E_{1}\}[\bar{j}]),\operatorname{ind}^{\varphi}(\eta')),\operatorname{amp}(A_{3}\{E_{1}\}[\bar{j}]))\})}{A}]\}\\
&A_{3}:=\operatorname{locateN}(A_{1},\bar{u},\operatorname{ind}^{\varphi}(\eta')); \qquad\text{LocateN}\\
&\{P[\frac{\operatorname{concat}(\operatorname{upDim}(A_{2},\bar{t},(\bar{t},\bar{1})),\operatorname{upDim}(A_{1},\bar{u},|A_{3}|)\{\forall\bar{j}\in\operatorname{range}(|A_{3}|).\ \bar{j}\mapsto\operatorname{up}^{a}(\operatorname{up}^{b}(\operatorname{upDim}(A_{1},\bar{u},|A_{3}|)[\bar{j}],\operatorname{vector}(A_{3}[\bar{j}]),\operatorname{ind}^{\varphi}(\eta')),\operatorname{amp}(A_{3}[\bar{j}]))\})}{A}]\}\\
&\qquad e; \qquad\text{Upper}\\
&\{Q[\frac{\operatorname{concat}(\operatorname{upDim}(A_{2},\bar{t},(\bar{t},\bar{1})),\operatorname{upDim}(A_{1},\bar{u},|A_{3}|)\{\forall\bar{j}\in\operatorname{range}(|A_{3}|).\ \bar{j}\mapsto\operatorname{up}^{a}(\operatorname{up}^{b}(\operatorname{upDim}(A_{1},\bar{u},|A_{3}|)[\bar{j}],\operatorname{vector}(A_{3}[\bar{j}]),\operatorname{ind}^{\varphi}(\eta')),\operatorname{amp}(A_{3}[\bar{j}]))\})}{A}]\}\\
&\Rightarrow\{Q[\frac{\operatorname{concat}(\operatorname{upDim}(A_{2},\bar{t},(\bar{t},\bar{1})),A_{6}\{E_{2}[\operatorname{upDim}(A_{1},\bar{u},\bar{u})/A_{4}][|A_{3}|/\bar{u}]\})}{A}]\wedge Q_{\operatorname{len}}(A_{6}\{E_{2}[\operatorname{upDim}(A_{1},\bar{u},\bar{u})/A_{4}][|A_{3}|/\bar{u}]\},|A_{3}|)\wedge Q_{\operatorname{len}}(\operatorname{upDim}(A_{2},\bar{t},(\bar{t},\bar{1})),(\bar{t},\bar{1}))\}\\
&\qquad\bar{u}':=|A_{3}|;\ A_{4}:=\operatorname{upDim}(A_{1},\bar{u},\bar{u}); \qquad\text{Len, UpDim}\\
&\{Q[\frac{\operatorname{concat}(\operatorname{upDim}(A_{2},\bar{t},(\bar{t},\bar{1})),A_{6}\{E_{2}\})}{A}]\wedge Q_{\operatorname{len}}(A_{6}\{E_{2}\},\bar{u}')\wedge Q_{\operatorname{len}}(\operatorname{upDim}(A_{2},\bar{t},(\bar{t},\bar{1})),(\bar{t},\bar{1}))\}\\
&\qquad A_{5}:=\operatorname{upDim}(A_{2},\bar{t},(\bar{t},\bar{1})); \qquad\text{UpDim}\\
&\{Q[\frac{\operatorname{concat}(A_{5},A_{6}\{E_{2}\})}{A}]\wedge Q_{\operatorname{len}}(A_{6}\{E_{2}\},\bar{u}')\wedge Q_{\operatorname{len}}(A_{5},(\bar{t},\bar{1}))\}\\
&\qquad A_{6}:=\operatorname{assignN}(A_{4},A_{3},\bar{u}',\operatorname{ind}^{\varphi}(\eta'),\lambda v\,l.\operatorname{amp}(l)); \qquad\text{AssignN}\\
&\{Q[\frac{\operatorname{concat}(A_{5},A_{6})}{A}]\wedge Q_{\operatorname{len}}(\operatorname{concat}(A_{5},A_{6}),\bar{u}'+(\bar{t},\bar{1}))\}\\
&\qquad A_{7}:=\operatorname{concat}(A_{5},A_{6}); \qquad\text{Concat}\\
&\{Q[A_{7}/A]\wedge Q_{\operatorname{len}}(A_{7},\bar{u}'+(\bar{t},\bar{1}))\}\\
&\qquad A:=A_{7}; \qquad\text{Assign}\\
&\{Q\wedge Q_{\operatorname{len}}(A,\bar{u}'+(\bar{t},\bar{1}))\}
\end{aligned} { P } ⇒ { P [ concat ( filterN ( A , ¬ b , ind φ ( η )) , filterN ( A , b , ind φ ( η ))) / A ]} A 1 := filterN ( A , b , ind φ ( η )) ; u ˉ := ∣ A 1 ∣ ; FilterN, Len { P [ concat ( filterN ( A , ¬ b , ind φ ( η )) , A 1 ) / A ]} A 2 := filterN ( A , ¬ b , ind φ ( η )) ; t ˉ := ∣ A 2 ∣ ; FilterN, Len { P [ concat ( A 2 , A 1 ) / A ]} ⇒ { P [ A concat ( A 2 , t ˉ , ( t ˉ , 1 ˉ ) , upDim ( A 1 , u ˉ , ∣ A 3 { E 1 } ∣ ) { ∀ j ˉ ∈ range ( ∣ A 3 { E 1 } ∣ ) . j ˉ ↦ up a ( up b ( upDim ( A 1 , u ˉ , ∣ A 3 { E 1 } ∣ ) [ j ˉ ] , vector ( A 3 { E 1 } [ j ˉ ]) , ind φ ( η ′ )) , amp ( A 3 { E 1 } [ j ˉ ]))}) ]} A 3 := locateN ( A 1 , u ˉ , ind φ ( η ′ )) ; LocateN { P [ A concat ( upDim ( A 2 , t ˉ , ( t ˉ , 1 ˉ )) , upDim ( A 1 , u ˉ , ∣ A 3 ∣ ) { ∀ j ˉ ∈ range ( ∣ A 3 ∣ ) . j ˉ ↦ up a ( up b ( upDim ( A 1 , u ˉ , ∣ A 3 ∣ ) [ j ˉ ] , vector ( A 3 [ j ˉ ]) , ind φ ( η ′ )) , amp ( A 3 [ j ˉ ]))}) ]} e ; Upper { Q [ A concat ( upDim ( A 2 , t ˉ , ( t ˉ , 1 ˉ )) , upDim ( A 1 , u ˉ , ∣ A 3 ∣ ) { ∀ j ˉ ∈ range ( ∣ A 3 ∣ ) . j ˉ ↦ up a ( up b ( upDim ( A 1 , u ˉ , ∣ A 3 ∣ ) [ j ˉ ] , vector ( A 3 [ j ˉ ]) , ind φ ( η ′ )) , amp ( A 3 [ j ˉ ]))}) ]} ⇒ { Q [ A concat ( upDim ( A 2 , t ˉ , ( t ˉ , 1 ˉ )) , A 6 { E 2 [ upDim ( A 1 , u ˉ , u ˉ ) / A 4 ] [ ∣ A 3 ∣/ u ˉ ]}) ] ∧ Q len ( A 6 { E 2 [ upDim ( A 1 , u ˉ , u ˉ ) / A 4 ] [ ∣ A 3 ∣/ u ˉ ]} , ∣ A 3 ∣ ) ∧ Q len ( upDim ( A 2 , t ˉ , ( t ˉ , 1 ˉ )) , ( t ˉ , 1 ˉ ))} u ˉ ′ := ∣ A 3 ∣ ; A 4 := upDim ( A 1 , u ˉ , u ˉ ) ; Len, UpDim { Q [ A concat ( upDim ( A 2 , t ˉ , ( t ˉ , 1 ˉ )) , A 6 { E 2 }) ] ∧ Q len ( A 6 { E 2 } , u ˉ ′ ) ∧ Q len ( upDim ( A 2 , t ˉ , ( t ˉ , 1 ˉ )) , ( t ˉ , 1 ˉ ))} A 5 := upDim ( A 2 , t ˉ , ( t ˉ , 1 ˉ )) ; UpDim { Q [ A concat ( A 5 , A 6 { E 2 }) ] ∧ Q len ( A 6 { E 2 } , u ˉ ′ ) ∧ Q len ( A 5 , ( t ˉ , 1 ˉ ))} A 6 := assignN ( A 4 , A 3 , u ˉ ′ , ind φ ( η ′ ) , λ v l . amp ( l )) ; AssignN { Q [ A concat ( A 5 , A 6 ) ] ∧ Q len ( concat ( A 5 , A 6 ) , u ˉ ′ + ( t ˉ , 1 ˉ ))} A 7 := concat ( A 5 , A 6 ) ; Concat { Q [ A 7 / A ] ∧ Q len ( A 7 , u ˉ ′ + ( t ˉ , 1 ˉ ))} A := A 7 ; Assign { Q ∧ Q len ( A , u ˉ ′ + ( t ˉ , 1 ˉ ))}
Proof Tree for Quantum Measurement
We continue the proof of Lemma 7.11 (§ 7.4 ); as there, the Q len Q_{\operatorname{len}} Q len judgments are omitted.
let E 1 : = filterN ( A , λ y . y = x , ind φ ( η ) ) E_{1}:=\operatorname{filterN}(A,\lambda y.y=x,\operatorname{ind}^{\varphi}(\eta)) E 1 := filterN ( A , λ y . y = x , ind φ ( η )) and E 2 : = up b ( A 1 [ j ˉ ] , vector ( A 1 [ j ˉ ] ) , ind φ ( η ′ ) ) E_{2}:=\operatorname{up}^{b}(A_{1}[\bar{j}],\operatorname{vector}(A_{1}[\bar{j}]),\operatorname{ind}^{\varphi}(\eta')) E 2 := up b ( A 1 [ j ˉ ] , vector ( A 1 [ j ˉ ]) , ind φ ( η ′ ))
{ P [ E 1 { ∀ j ˉ ∈ range ( ∣ E 1 ∣ ) . j ˉ ↦ up a ( up b ( E 1 [ j ˉ ] , vector ( E 1 [ j ˉ ] ) , ind φ ( η ) ) , amp ( E 1 ) r ) } / A ] [ ∑ j ˉ ∈ range ( ∣ E 1 ∣ ) ∣ amp ( E 1 ) ∣ 2 r [ vector ( A [ i ˉ ] ) [ ind φ ( η ) ] / x ] ] } Indexing \left\{
\begin{aligned}
&P\left[E_1\left\{\forall\bar{j}\in\operatorname{range}(|E_1|).\bar{j}\mapsto\operatorname{up}^{a}\left(\operatorname{up}^{b}\left(E_1[\bar{j}],\operatorname{vector}(E_1[\bar{j}]),\operatorname{ind}^{\varphi}(\eta)\right),\frac{\operatorname{amp}(E_1)}{r}\right)\right\}/A\right]\\
&\qquad\left[\sqrt{\sum_{\bar{j}\in\operatorname{range}(|E_1|)}\frac{|\operatorname{amp}(E_1)|^2}{r}}\,[\operatorname{vector}(A[\bar{i}])[\operatorname{ind}^{\varphi}(\eta)]/x]\right]
\end{aligned}
\right\}
\quad\text{Indexing} ⎩ ⎨ ⎧ P [ E 1 { ∀ j ˉ ∈ range ( ∣ E 1 ∣ ) . j ˉ ↦ up a ( up b ( E 1 [ j ˉ ] , vector ( E 1 [ j ˉ ]) , ind φ ( η ) ) , r amp ( E 1 ) ) } / A ] j ˉ ∈ range ( ∣ E 1 ∣ ) ∑ r ∣ amp ( E 1 ) ∣ 2 [ vector ( A [ i ˉ ]) [ ind φ ( η )] / x ] ⎭ ⎬ ⎫ Indexing
x : = vector ( A [ i ˉ ] ) [ ind φ ( η ) ] x:=\operatorname{vector}(A[\bar{i}])[\operatorname{ind}^{\varphi}(\eta)] x := vector ( A [ i ˉ ]) [ ind φ ( η )] ; \quad Indexing
{ P [ E 1 { ∀ j ˉ ∈ range ( ∣ E 1 ∣ ) . j ˉ ↦ up a ( up b ( E 1 [ j ˉ ] , vector ( E 1 [ j ˉ ] ) , ind φ ( η ) ) , amp ( E 1 ) r ) } / A ] [ ∑ j ˉ ∈ range ( ∣ E 1 ∣ ) ∣ amp ( E 1 ) ∣ 2 r ] } \left\{
\begin{aligned}
&P\left[E_1\left\{\forall\bar{j}\in\operatorname{range}(|E_1|).\bar{j}\mapsto\operatorname{up}^{a}\left(\operatorname{up}^{b}\left(E_1[\bar{j}],\operatorname{vector}(E_1[\bar{j}]),\operatorname{ind}^{\varphi}(\eta)\right),\frac{\operatorname{amp}(E_1)}{r}\right)\right\}/A\right]\\
&\qquad\left[\sqrt{\sum_{\bar{j}\in\operatorname{range}(|E_1|)}\frac{|\operatorname{amp}(E_1)|^2}{r}}\right]
\end{aligned}
\right\} ⎩ ⎨ ⎧ P [ E 1 { ∀ j ˉ ∈ range ( ∣ E 1 ∣ ) . j ˉ ↦ up a ( up b ( E 1 [ j ˉ ] , vector ( E 1 [ j ˉ ]) , ind φ ( η ) ) , r amp ( E 1 ) ) } / A ] j ˉ ∈ range ( ∣ E 1 ∣ ) ∑ r ∣ amp ( E 1 ) ∣ 2 ⎭ ⎬ ⎫
A 1 : = filterN ( A , λ y . y = x , ind φ ( η ) ) A_1:=\operatorname{filterN}(A,\lambda y.y=x,\operatorname{ind}^{\varphi}(\eta)) A 1 := filterN ( A , λ y . y = x , ind φ ( η )) ; \quad FilterN
{ P [ A 1 { ∀ j ˉ ∈ range ( ∣ A 1 ∣ ) . j ˉ ↦ up a ( E 2 , amp ( A 1 ) r ) } / A ] [ ∑ j ˉ ∈ range ( ∣ A 1 ∣ ) ∣ amp ( A 1 ) ∣ 2 r ] } \left\{P\left[A_1\left\{\forall\bar{j}\in\operatorname{range}(|A_1|).\bar{j}\mapsto\operatorname{up}^{a}\left(E_2,\frac{\operatorname{amp}(A_1)}{r}\right)\right\}/A\right]\left[\sqrt{\sum_{\bar{j}\in\operatorname{range}(|A_1|)}\frac{|\operatorname{amp}(A_1)|^2}{r}}\right]\right\} ⎩ ⎨ ⎧ P [ A 1 { ∀ j ˉ ∈ range ( ∣ A 1 ∣ ) . j ˉ ↦ up a ( E 2 , r amp ( A 1 ) ) } / A ] j ˉ ∈ range ( ∣ A 1 ∣ ) ∑ r ∣ amp ( A 1 ) ∣ 2 ⎭ ⎬ ⎫
⇒ { P [ A 3 { ∀ j ˉ ∈ range ( ∣ A 1 ∣ ) . j ˉ ↦ up a ( E 2 , amp ( E 2 ) r ) } / A ] [ ∑ j ˉ ∈ range ( ∣ A 1 ∣ ) ∣ amp ( E 2 ) ∣ 2 r ] } \Rightarrow\left\{P\left[A_3\left\{\forall\bar{j}\in\operatorname{range}(|A_1|).\bar{j}\mapsto\operatorname{up}^{a}\left(E_2,\frac{\operatorname{amp}(E_2)}{r}\right)\right\}/A\right]\left[\sqrt{\sum_{\bar{j}\in\operatorname{range}(|A_1|)}\frac{|\operatorname{amp}(E_2)|^2}{r}}\right]\right\} ⇒ { P [ A 3 { ∀ j ˉ ∈ range ( ∣ A 1 ∣ ) . j ˉ ↦ up a ( E 2 , r amp ( E 2 ) ) } / A ] [ ∑ j ˉ ∈ range ( ∣ A 1 ∣ ) r ∣ amp ( E 2 ) ∣ 2 ] } \quad Pre-Strength
t : = ∣ A 1 ∣ t:=|A_1| t := ∣ A 1 ∣ ; \quad Len
{ P [ A 3 { ∀ j ˉ ∈ range ( t ) . j ˉ ↦ up a ( E 2 , amp ( E 2 ) r ) } / A ] [ ∑ j ˉ ∈ range ( t ) ∣ amp ( E 2 ) ∣ 2 r ] } \left\{P\left[A_3\left\{\forall\bar{j}\in\operatorname{range}(t).\bar{j}\mapsto\operatorname{up}^{a}\left(E_2,\frac{\operatorname{amp}(E_2)}{r}\right)\right\}/A\right]\left[\sqrt{\sum_{\bar{j}\in\operatorname{range}(t)}\frac{|\operatorname{amp}(E_2)|^2}{r}}\right]\right\} ⎩ ⎨ ⎧ P [ A 3 { ∀ j ˉ ∈ range ( t ) . j ˉ ↦ up a ( E 2 , r amp ( E 2 ) ) } / A ] j ˉ ∈ range ( t ) ∑ r ∣ amp ( E 2 ) ∣ 2 ⎭ ⎬ ⎫
A 2 : = locateN ( A 1 , t , ind φ ( η ) ) A_2:=\operatorname{locateN}(A_1,t,\operatorname{ind}^{\varphi}(\eta)) A 2 := locateN ( A 1 , t , ind φ ( η )) ; \quad LocateN
{ P [ A 3 { ∀ j ˉ ∈ range ( t ) . j ˉ ↦ up a ( A 2 [ j ˉ ] , amp ( A 2 [ j ˉ ] ) r ) } / A ] [ ∑ j ˉ ∈ range ( t ) ∣ amp ( A 2 [ j ˉ ] ) ∣ 2 r ] } \left\{P\left[A_3\left\{\forall\bar{j}\in\operatorname{range}(t).\bar{j}\mapsto\operatorname{up}^{a}\left(A_2[\bar{j}],\frac{\operatorname{amp}(A_2[\bar{j}])}{r}\right)\right\}/A\right]\left[\sqrt{\sum_{\bar{j}\in\operatorname{range}(t)}\frac{|\operatorname{amp}(A_2[\bar{j}])|^2}{r}}\right]\right\} ⎩ ⎨ ⎧ P [ A 3 { ∀ j ˉ ∈ range ( t ) . j ˉ ↦ up a ( A 2 [ j ˉ ] , r amp ( A 2 [ j ˉ ]) ) } / A ] j ˉ ∈ range ( t ) ∑ r ∣ amp ( A 2 [ j ˉ ]) ∣ 2 ⎭ ⎬ ⎫
r : = sqrtSum ( A 2 , t ) r:=\operatorname{sqrtSum}(A_2,t) r := sqrtSum ( A 2 , t ) ; \quad SqrtSum
{ P [ A 3 { ∀ j ˉ ∈ range ( t ) . j ˉ ↦ up a ( A 2 [ j ˉ ] , amp ( A 2 [ j ˉ ] ) r ) } / A ] } \left\{P\left[A_3\left\{\forall\bar{j}\in\operatorname{range}(t).\bar{j}\mapsto\operatorname{up}^{a}\left(A_2[\bar{j}],\frac{\operatorname{amp}(A_2[\bar{j}])}{r}\right)\right\}/A\right]\right\} { P [ A 3 { ∀ j ˉ ∈ range ( t ) . j ˉ ↦ up a ( A 2 [ j ˉ ] , r amp ( A 2 [ j ˉ ]) ) } / A ] }
A 3 : = mapN ( A 2 , t , λ ( y , u ) . ( y / r , u ) ) A_3:=\operatorname{mapN}(A_2,t,\lambda(y,u).(y/r,u)) A 3 := mapN ( A 2 , t , λ ( y , u ) . ( y / r , u )) ; \quad MapN
{ P [ A 3 / A ] } \{P[A_3/A]\} { P [ A 3 / A ]}
A : = A 3 A:=A_3 A := A 3 \quad Assign
{ P } \{P\} { P }
Transformation for QHL
We show below the transformation for quantum Hoare logic (QHL) augmented with classical variables. A chain of QHL work [8 , 30 , 31 , 32 ] combines classical variables with quantum data in a QHL proof rule. In this paper, we call this chain CQHL, and we describe the proof rule as follows.
{ A , ∣ φ ⟩ } C { B , ∣ ψ ⟩ } \{A,|\varphi\rangle\}\ C\ \{B,|\psi\rangle\} { A , ∣ φ ⟩} C { B , ∣ ψ ⟩}
The rule suggests that the execution of the program C C C under a state described by the pre-condition { A , ∣ φ ⟩ } \{A,|\varphi\rangle\} { A , ∣ φ ⟩} results in a post-state described by the post-condition { B , ∣ ψ ⟩ } \{B,|\psi\rangle\} { B , ∣ ψ ⟩} , where A A A and B B B are pre- and post-conditions on the classical state, while ∣ φ ⟩ |\varphi\rangle ∣ φ ⟩ and ∣ ψ ⟩ |\psi\rangle ∣ ψ ⟩ are quantum data predicates written in the Dirac notation of a quantum datum.
CQHL distinguishes classical variables (x x x or y y y ) from quantum variables (q q q or q ˉ \bar{q} q ˉ ). Quantum variables can be single-qubit variables (q q q ) or quantum array variables (q ˉ \bar{q} q ˉ ). A quantum array variable can be written in the form q ˉ [ x , y ) \bar{q}[x,y) q ˉ [ x , y ) where x x x and y y y are two indices in the array q ˉ \bar{q} q ˉ , i.e., we have x ≤ y x\le y x ≤ y and q ˉ [ x , y ) = q ˉ [ x ] ∣ ⋯ ∣ q ˉ [ y − 1 ] \bar{q}[x,y)=\bar{q}[x]\mathbin{|}\cdots\mathbin{|}\bar{q}[y-1] q ˉ [ x , y ) = q ˉ [ x ] ∣ ⋯ ∣ q ˉ [ y − 1 ] . Quantum array variables are indexed like classical arrays. CQHL labels kets with quantum variable subscripts, e.g., ∣ d ⟩ q |d\rangle_q ∣ d ⟩ q refers to a basis-ket state in the qubit q q q position and ∣ ψ ⟩ q ˉ |\psi\rangle_{\bar{q}} ∣ ψ ⟩ q ˉ refers to a quantum datum ∣ ψ ⟩ |\psi\rangle ∣ ψ ⟩ in the quantum array q ˉ \bar{q} q ˉ .
{ A , U † ( ∣ φ ⟩ q ˉ ) ∣ ψ ⟩ q ˉ } q ˉ ← U { A , ∣ φ ⟩ q ˉ ∣ ψ ⟩ q ˉ } \{A,U^{\dagger}(|\varphi\rangle_{\bar{q}})|\psi\rangle_{\bar{q}}\}\ \bar{q}\leftarrow U\ \{A,|\varphi\rangle_{\bar{q}}|\psi\rangle_{\bar{q}}\} { A , U † ( ∣ φ ⟩ q ˉ ) ∣ ψ ⟩ q ˉ } q ˉ ← U { A , ∣ φ ⟩ q ˉ ∣ ψ ⟩ q ˉ }
We show an example proof triple for lines 1-3, based on the proof rule for quantum unitary operation above.
{ t r u e , H † ( 1 2 ( ∣ 0 ⟩ q 1 + ∣ 1 ⟩ q 1 ) ) ∣ 0 ⟩ q 2 ∣ 0 ⟩ q 3 } q 1 ← H { t r u e , 1 2 ( ∣ 0 ⟩ q 1 + ∣ 1 ⟩ q 1 ) ⊗ ∣ 0 ⟩ q 2 ∣ 0 ⟩ q 3 } \left\{\mathrm{true},H^\dagger\left(\frac{1}{\sqrt{2}}\left(|0\rangle_{q_1}+|1\rangle_{q_1}\right)\right)|0\rangle_{q_2}|0\rangle_{q_3}\right\}\ q_1\leftarrow H\ \left\{\mathrm{true},\frac{1}{\sqrt{2}}\left(|0\rangle_{q_1}+|1\rangle_{q_1}\right)\otimes|0\rangle_{q_2}|0\rangle_{q_3}\right\} { true , H † ( 2 1 ( ∣0 ⟩ q 1 + ∣1 ⟩ q 1 ) ) ∣0 ⟩ q 2 ∣0 ⟩ q 3 } q 1 ← H { true , 2 1 ( ∣0 ⟩ q 1 + ∣1 ⟩ q 1 ) ⊗ ∣0 ⟩ q 2 ∣0 ⟩ q 3 }
{ t r u e , ∣ 0 ⟩ q 1 ∣ 0 ⟩ q 2 ∣ 0 ⟩ q 3 } q 1 ← H { t r u e , 1 2 ( ∣ 0 ⟩ q 1 + ∣ 1 ⟩ q 1 ) ⊗ ∣ 0 ⟩ q 2 ∣ 0 ⟩ q 3 } \left\{\mathrm{true},|0\rangle_{q_1}|0\rangle_{q_2}|0\rangle_{q_3}\right\}\ q_1\leftarrow H\ \left\{\mathrm{true},\frac{1}{\sqrt{2}}\left(|0\rangle_{q_1}+|1\rangle_{q_1}\right)\otimes|0\rangle_{q_2}|0\rangle_{q_3}\right\} { true , ∣0 ⟩ q 1 ∣0 ⟩ q 2 ∣0 ⟩ q 3 } q 1 ← H { true , 2 1 ( ∣0 ⟩ q 1 + ∣1 ⟩ q 1 ) ⊗ ∣0 ⟩ q 2 ∣0 ⟩ q 3 }
The triple applies a Hadamard operation. Here, we utilize the CQHL consequence rule to rewrite the bottom pre-condition to the upper one, where the inverse of H gate (H † H^\dagger H † ) is H itself, and applying the H gate to 1 2 ( ∣ 0 ⟩ q 1 + ∣ 1 ⟩ q 1 ) \frac{1}{\sqrt{2}}(|0\rangle_{q_1}+|1\rangle_{q_1}) 2 1 ( ∣0 ⟩ q 1 + ∣1 ⟩ q 1 ) yields ∣ 0 ⟩ |0\rangle ∣0 ⟩ .
The CQHL syntax is in Figure 7 : skip ({ } \{\} { } ), quantum unitary application q ‾ ← U \overline{q}\leftarrow U q ← U , sequencing, classical conditional (if b b b then C C C else C C C fi), quantum conditional (qif [ q ] [q] [ q ] (∣ 0 ⟩ → C |0\rangle\to C ∣0 ⟩ → C ) ⊔ \sqcup ⊔ (∣ 1 ⟩ → C |1\rangle\to C ∣1 ⟩ → C ) fiq), classical assignment (x : = e x:=e x := e ), and function call (f ( e ) f(e) f ( e ) ). In the language, e e e and B B B refer to classical arithmetic and Boolean expressions, respectively. Boolean expressions (B B B ) consist of Boolean values (true and false), equality (e = e e=e e = e ), negation (¬ B \neg B ¬ B ), and other operations.
Figure 7. CQHL Syntax.
A unitary operation is either a single-qubit gate (H or X) or a quantum oracle λ q ‾ . ∣ f ( q ‾ ) ⟩ \lambda\overline{q}.|f(\overline{q})\rangle λ q .∣ f ( q )⟩ , which maps a quantum datum ∑ j α j ∣ j ⟩ \sum_j\alpha_j|j\rangle ∑ j α j ∣ j ⟩ on q ‾ \overline{q} q to ∑ j α j ∣ f ( j ) ⟩ \sum_j\alpha_j|f(j)\rangle ∑ j α j ∣ f ( j )⟩ .
We define a valid program C C C to have no free classical variables, F V ( C ) = ∅ FV(C)=\varnothing F V ( C ) = ∅ , i.e., classical variables are all bound by a let-binding (let x = e x=e x = e in C C C ). In CQHL, a quantum variable q q q refers to a single qubit and a quantum array q ‾ \overline{q} q to a group of qubits. The range [ e 1 , e 2 ) [e_1,e_2) [ e 1 , e 2 ) of the array q ‾ \overline{q} q is written q ‾ [ e 1 , e 2 ) \overline{q}[e_1,e_2) q [ e 1 , e 2 ) , where e 1 e_1 e 1 and e 2 e_2 e 2 are classical expressions.
To transform CQHL to classical Hoare logic, we first translate CQHL to Qafny and then apply the translation of § 7 to the result.
We first translate a CQHL predicate A , ∣ φ ⟩ A,|\varphi\rangle A , ∣ φ ⟩ to a Qafny state predicate. Here, A A A is a classical predicate, which translates directly. In CQHL, the quantum predicate ∣ φ ⟩ |\varphi\rangle ∣ φ ⟩ is written in Dirac notation as ∑ j z j ∣ c j ⟩ q ‾ \sum_j z_j|c_j\rangle_{\overline{q}} ∑ j z j ∣ c j ⟩ q , where the qubit array variable q ‾ \overline{q} q gives the qubit locations of the state. We translate the predicate as follows.
A ∧ q ‾ : EN ( 1 ) ↦ ∑ j z j ∣ c j ⟩ A\land\overline{q}:\operatorname{EN}(1)\mapsto\sum_j z_j|c_j\rangle A ∧ q : EN ( 1 ) ↦ j ∑ z j ∣ c j ⟩
To translate a CQHL program to Qafny, note that classical assignments, classical conditionals, function calls, and sequencing are classical operations and map directly to their Qafny counterparts. Only the two quantum operations, q ‾ ← U \overline{q}\leftarrow U q ← U and qif [ q ] [q] [ q ] (∣ 0 ⟩ → C |0\rangle\to C ∣0 ⟩ → C ) ⊔ \sqcup ⊔ (∣ 1 ⟩ → C |1\rangle\to C ∣1 ⟩ → C ) fiq, need care.
The translation of q ‾ ← U \overline{q}\leftarrow U q ← U depends on U U U . If U U U is an oracle operation of Qafny; if U U U is the Hadamard gate H, we translate it to the Hadamard operation of Qafny. Since H together with oracle operations is universal, any other gate can be decomposed into a sequence of these two operations.
A CQHL quantum conditional qif [ q ] [q] [ q ] (∣ 0 ⟩ → C 1 |0\rangle\to C_1 ∣0 ⟩ → C 1 ) ⊔ \sqcup ⊔ (∣ 1 ⟩ → C 2 |1\rangle\to C_2 ∣1 ⟩ → C 2 ) fiq translates to a sequence of two Qafny quantum conditionals, as shown below.
if ( q [ 0 ] ) { C 2 } ; if ( ¬ q [ 0 ] ) { C 1 } \text{if } (q[0])\ \{C_2\};\ \text{if } (\neg q[0])\ \{C_1\} if ( q [ 0 ]) { C 2 } ; if ( ¬ q [ 0 ]) { C 1 }
This completes the translation of CQHL to Qafny; the translation of § 7 then takes the resulting Qafny triple to classical Hoare logic.