∎
Symbolic Reasoning about Quantum Circuits in Coq
The final authenticated version is available online at: https://doi.org/10.1007/s11390-021-1637-9)
Abstract
A quantum circuit is a computational unit that transforms an input quantum state to an output state. A natural way to reason about its behavior is to compute explicitly the unitary matrix implemented by it. However, when the number of qubits increases, the matrix dimension grows exponentially and the computation becomes intractable.
In this paper, we propose a symbolic approach to reasoning about quantum circuits. It is based on a small set of laws involving some basic manipulations on vectors and matrices. This symbolic reasoning scales better than the explicit one and is well suited to be automated in Coq, as demonstrated with some typical examples.
Keywords:
Symbolic reasoning Quantum circuits Dirac notation Coq.1 Introduction
A quantum circuit is a natural model of quantum computation NC11 . It is a computational unit that transforms an input quantum state to an output state. Once a quantum circuit is designed to implement an algorithm, it is indispensable to analyze the circuit and ensure that it indeed conforms to the requirements of the algorithm. When a large number of qubits are involved, manually reasoning about a circuit’s behavior is tedious and error-prone. A way of reasoning about quantum circuits (semi-) automatically and reliably is to mechanize the reasoning procedure in an interactive theorem prover, such as the Coq proof assistant Coq . For example, Paykin et al. PRZ17 verified a few quantum algorithms in Coq, using some semi-automated strategies to generate machine-checkable proofs.
Existing approaches have apparent drawbacks in both efficiency and human readability. Quantum states and operations are represented and computed using matrices explicitly, and their comparison is made in an element-wise way, thus is highly non-scalable with respect to qubit numbers. Furthermore, as the system dimension grows, it is almost impossible for human beings to read the matrices printed by the theorem prover.
In this paper, we propose a symbolic approach to reasoning about the behavior of quantum circuits in Coq, which improves both efficiency of the reasoning procedure and readability of matrix representations. The main contributions of this paper include:
-
•
A matrix representation in Coq using the Dirac notation Dir , which is commonly used in quantum mechanics. Matrices are represented as combinations of , , scalars, and a set of basic operators such as tensor product and matrix multiplication. Here and are the Dirac notation for 2-dimensional column vectors and , respectively. In this way, we have a concise representation for sparse matrices, commonly used in quantum computation.
-
•
A tactic library for (semi-)automated symbolic reasoning about matrices. The tactics are based on a small set of inference laws (lemmas in Coq). The key idea is to reduce matrix multiplications in the form of into scalars, and simplify the matrix representation by absorbing ones and eliminating zeros. In this way, our approach reasons about matrices by (semi-)automated rewriting instead of actually computing matrices, and outperforms Paykin et al.’s computational approach PRZ17 , as shown in proving the functional correctness of some typical quantum algorithms in Section 6.
We illustrate the intuition of our tactics by the following simple example which computes the result of applying the Pauli-X gate to the state. In an explicit matrix-vector multiplication form, it reads as follows.
and four multiplications are required for the whole computation. By contrast, if we use the Dirac notation for and apply distribution and associativity laws, then
Note that the two terms and are reduced (symbolically) to 0 and 1, respectively. Consequently, no multiplication is required at all.
The rest of the paper is structured as follows. In Section 2 we recall some basic notation from linear algebra and quantum mechanics. In Section 3 we introduce a symbolic approach to reasoning about quantum circuits. In Section 5 we discuss some problems in representing matrices using Coq’s type system and our solutions. In Section 4 we propose two notions of equivalence for quantum circuits. In Section 6 we conduct a few case studies. In Section 7 we discuss some related work. Finally, we conclude the paper in Section 8.
The Coq scripts of our tactic library and the examples used in our case studies are available at the following link
https://github.com/Vickyswj/DiracRepr.
2 Preliminaries
For the convenience of the reader, we briefly recall some basic notions from linear algebra and quantum theory which are needed in this paper. For more details, we refer the reader to NC11 .
2.1 Basic linear algebra
A Hilbert space is a complete vector space equipped with an inner product
such that
-
1.
for any , with equality if and only if ;
-
2.
;
-
3.
,
where is the set of complex numbers, and for each , stands for the complex conjugate of . A vector is normalised if its length is equal to . Two vectors and are orthogonal if . An orthonormal basis of a Hilbert space is a basis where each is normalised and any pair of them is orthogonal.
Let be a set of linear operators on . For any , is Hermitian if where is the adjoint operator of such that for any . The fundamental spectral theorem states that the set of all normalised eigenvectors of a Hermitian operator in constitutes an orthonormal basis for . That is, there exists a so-called spectral decomposition for each Hermitian such that
where the set constitutes an orthonormal basis of , denotes the set of eigenvalues of , and is the projector to the corresponding eigenspace of . A linear operator is unitary if where is the identity operator on . The trace of is defined as for some given orthonormal basis of . It is worth noting that the trace function is actually independent of the orthonormal basis selected. It is also easy to check that the trace function is linear and for any .
Let and be two Hilbert spaces. Their tensor product is defined as a vector space consisting of linear combinations of the vectors with and . Here the tensor product of two vectors is defined by a new vector such that
Then is also a Hilbert space where the inner product is defined in the following way: for any and ,
where is the inner product of . For any and , is defined as a linear operator in such that for each and ,
2.2 Basic quantum mechanics
According to von Neumann’s formalism of quantum mechanics vN55 , an isolated physical system is associated with a Hilbert space which is called the state space of the system. A pure state of a quantum system is a normalised vector in its state space, and a mixed state is represented by a density operator on the state space. Here a density operator on Hilbert space is a positive linear operator such that . Another equivalent representation of a density operator is an ensemble NC11 of pure states. In particular, given an ensemble where , , and are pure states, then is a density operator. Conversely, each density operator can be generated by an ensemble of pure states in this way. Finally, a pure state can be regarded as a special mixed state.
The state space of a composite system (for example, a quantum system consisting of many qubits) is the tensor product of the state spaces of its components. Note that in general, the state of a composite system cannot be decomposed into a tensor product of the reduced states on its component systems. A well-known example is the 2-qubit state
This kind of state is called an entangled state. Entanglement is an important feature of quantum mechanics which has no counterpart in the classical world, and is the key to many quantum information processing tasks.
Let be a state vector and a real number. In quantum mechanics, the state is considered to be equal to , up to the global phase factor . The reason is that from an observational point of view, global phases are irrelevant to the observed properties of the physical system under consideration and can thus be ignored as far as quantum states are concerned NC11 .
The evolution of a closed quantum system is described by a unitary operator on its state space. If the states of the system at times and are and , respectively, then for some unitary operator which depends only on and . A convenient way to understand unitary operators is in terms of their matrix representations. In fact, the unitary operator and matrix viewpoints turn out to be completely equivalent. An by complex unitary matrix with entries can be considered as a unitary operator sending vectors in the vector space to the vector space , under matrix multiplication of the matrix by a vector in .
We often denote a single qubit as a vector parameterized by two complex numbers satisfying the condition . A unitary operator for a qubit is then described by a unitary matrix. Quantum circuits are a popular model for quantum computation, where quantum gates usually stand for basic unitary operators whose mathematical meanings are given by appropriate unitary matrices. Some commonly used quantum gates to appear in the current work include the -qubit Hadamard gate , the Pauli gates , the controlled-NOT gate performed on two qubits, and the -qubit Toffoli gate. Their matrix representations are given below:
For example, in Figure 1 we display a circuit that can generate the -qubit Greenberger–Horne–Zeilinger state (GHZ state) GHZ , which is . In the circuit, a Hadamard gate is applied on the first qubit, then two controlled-NOT gates are used, with the first qubit controlling the second, which in turn controls the third.
A quantum measurement is described by a collection of measurement operators, where the indices refer to the measurement outcomes. It is required that the measurement operators satisfy the completeness equation . If the state of a quantum system is immediately before the measurement, then the probability that result occurs is given by
and the state of the system after the measurement is If the states of the system at times and are mixed, say and , respectively, then after the unitary operation is applied on the system. For the same measurement as above, if the system is in the mixed state , then the probability that measurement result occurs is given by
and the state of the post-measurement system is provided that .
3 Symbolic reasoning
In this section, we first introduce the terms that will appear in our symbolic reasoning and some laws that are useful for reducing terms. Then we present in detail the strategeis that we developed to simplify circuits.
Scalars: | |
---|---|
Basic vectors: | , |
Operators: | , , , , |
Laws: | L1 , |
L2 Associativity of | |
L3 , , | |
L4 | |
L5 | |
L6 | |
L7 | |
L8 , | |
L9 | |
L10 | |
L11 , | |
L12 , | |
L13 | |
L14 , | |
L15 , | |
L16 |
3.1 Terms and laws
Our symbolic reasoning is based on terms constructed from scalars and basic vectors using some constructors:
- •
-
•
Basic vectors are the base states of a qubit, i.e., and in the Dirac notation. Mathematically, stands for the vector and for .
-
•
Constructors include the scalar product , matrix product , matrix addition , tensor product , and the conjugate transpose of a matrix .
In Dirac notations, represents the dual of , i.e. ; similarly for . The term is abbreviated into , for any . This notation introduces an intuitive explanation of quantum operation. For example, the effect of the operator is to map into and into . Thus we define in Coq as , instead of . Then it is obvious that and similarly for .
Some commonly used vectors and gates can be derived from the basic terms. For example, we define the vectors and as follows.
We also define four simple matrices .
(1) |
The Hadamard matrix is a combination of the four matrices above.
Similarly, the Pauli-X gate and the controlled-NOT gate are given as follows.
(2) |
Notice that tensor products of matrices from constitute an orthonormal basis for the space of all matrices of dimension , for all . Thus any matrix that appeared in the computation of a quantum circuit can be represented as a term.
Suppose the state of a quantum system is represented by a vector. The central idea of our symbolic reasoning is to employ the laws in Table 1 to rewrite terms, trying to put together the basic vectors and simplify them using the laws in L1. Technically, we design a series of strategies for that purpose.
3.2 Soundness of the laws
Let be a law that relates two terms and . We say that the law is sound if both terms can be reduced to exactly the same matrix, after their metavariables (e.g., ) are instantiated by concrete matrices (e.g., ). We define a strategy called orthogonalreduce to verify that the laws in L1 are sound. In this case, we explicitly represent and as matrices. Both of them are matrices, so we can use matrix multiplication to prove that the corresponding elements in the matrices on both sides of the equation are the same. For example, the law is actually shown via . We add the laws in L1 to the set named S_db. The laws in L2-16 are also proved through explicit matrix representation.
Theorem 3.1
All the laws in Table 1 are sound.
We have given a formal soundness proof in Coq for each of the laws in Table 1. We collect the soundness properties in a library that contains many useful properties about matrices.
3.3 Strategy for basic matrices
The four matrices defined in (1) are the basic building blocks to represent matrices, and are intensively used in our symbolic reasoning. We design a strategy called basereduce to prove some equations about them acting on the base states and . For example, let us consider the equation . We first represent by , then use the associativity of matrix multiplication to form the subterm . Now we can use the proved laws in L1 to rewrite into . The last step is to deal with scalar multiplications. We add these equations to the set named B_db.
3.4 Strategy for the Pauli and Hadamard gates
The Pauli and Hadamard gates are probably the most widely used single-qubit gates. We introduce a strategy called gatereduce to prove some equations about the matrices acting on base states. For example, consider the equation . We first expand into . In order to prove the equation , we use the distributivity of matrix multiplication over addition to rewrite the left hand side of the equation into the sum of and . Then we employ the proved laws in B_db to rewrite them. Eventually, we deal with scalar multiplications and cancel zero matrices. We add these equations to the set named G_db.
Furthermore, we add into S_db and G_db some commonly used equations about the matrices and acting on states and . For example, we have and , etc.
3.5 Strategy for circuits
We propose a strategy called operatereduce that puts together all the results above to reason about circuits applied to input states represented in the vector form. We will have a close look at the strategy by using an example. Let us revisit the 3-qubit GHZ state. The state can be generated by applying the circuit in Figure 1 to the initial state . We would like to verify that the output state is indeed what we expect by establishing the following equation.
(3) |
We first use the right associativity of tensor product and matrix multiplication to change the expression on the left hand side in (3) into
(4) |
Then we calculate the inner layer matrix multiplications in sequence. We first calculate . We exploit the law in L13 to change a matrix product of tensored terms into a tensor product of matrix multiplications. Then we employ the laws established in G_db and B_db to rewrite terms. This transformation is as follows.
Correspondingly, the expression in (4) turns into
The inner layer matrix multiplication to be calculated next is the following expression: . Different from the calculation of the previous layer, we have to expand the multiple-qubit quantum gates first. Here is the CX gate. After this step, we use the distributivity of tensor product and matrix product over addition to rewriting it. We also exploit the law in L13 as well as the equations in G_db and B_db. So the inference goes as follows.
Other laws such as the associativity and distributivity of scalar multiplication may also be used in the inference, though they are not demonstrated in this example.
We continue in this way until no more matrix multiplication is possible. It is worth noting that if there is a zero matrix in the summands of a certain layer of calculation, it can be eliminated immediately, without being carried into the next layer of calculation. Finally, we simplify the expressions about complex numbers.
The above steps appear a bit complex, but they can be fully automated in Coq, fortunately. The script for implementing the strategy operatereduce is as follows.
Ltac inner_reduce := unfold_operator; kron_plus_distr; isolate_scale; assoc_right; try repeat rewrite Mmult_plus_distr_l; try repeat rewrite Mmult_plus_distr_r; repeat rewrite <- Mscale_kron_dist_r; repeat mult_kron; repeat rewrite Mscale_mult_dist_r; repeat (autorewrite with G_db; repeat cancel_0; repeat rewrite Mscale_kron_dist_r); repeat rewrite <- Mmult_plus_distr_l. Ltac operate_reduce := assoc_right; repeat inner_reduce; reduce_scale; unified_base.
In summary, using the strategy operatereduce, we can formally prove the equation in (3) automatically.
As we can see, our general framework is to formalize circuits symbolically as terms, and simplify terms involving matrices by (semi-)automated term rewriting instead of actually computing the matrices. The strategies in Sections 3.2-3.5 are designed with the common goal: to reduce matrix multiplications in the form of into scalars, and simplify the matrix representation by absorbing ones and eliminating zeros. This symbolic approach of reasoning about circuits turns out to be effective; see Section 6.7 for more detailed discussion.
3.6 Density matrices as quantum states
Although it is very intuitive to represent pure quantum states by vectors, there is an inconvenience. In quantum mechanics, the global phase of a qubit is often ignored. For example, we would not distinguish from for any . However, when written in vector form, and may be different because of the coefficient present in the latter but not in the former. Therefore, we use the symbol to denote such an equivalence, i.e. . As a matter of fact, we can be more general and define an observational equivalence for matrices, as given below.
Definition obs_equiv {m n : nat} (A B : Matrix m n) : Prop := exists c : C, Cmod c = R1 / c .* A = B. Infix "≈" := obs_equiv.In the above definition, the condition Cmod c = R1 means that the norm of the complex number c is 1 and c .* A = B says that the matrix A is equal to B after a scalar product with the coefficient c. See Section 6.1 for more concrete examples that use the relation .
Note that if quantum states are represented by density matrices, we have
Therefore, the discrepancy entailed by the global phase disappears: the two vectors and correspond to the same density matrix . Representing states by density matrices can thus omit unnecessary details and in some cases simplify our reasoning. This is a small but useful trick in formal verification of quantum circuits, which does not seem to have been exploited in the literature. In Sections 6.2 and 6.3, we give two examples where the input and output quantum states of the circuits are given in terms of density matrices.
If the state of a quantum system is represented by a density matrix, the reasoning strategies discussed above can still be used. For instance, suppose a system is in the initial state given by density matrix . After the execution of a quantum circuit implementing some unitary transformation , the system changes into the new state . Let be its spectral decomposition, where are eigenvalues of and the vectors the corresponding eigenvectors. It follows that
(5) |
Therefore, we can first simplify into a vector, take its dual and then obtain easily. Our approach to symbolic reasoning also applies in this setting.
We define two functions density and super in advance. The former converts states in the vector form into corresponding states in the density matrix form. The latter formalizes the transformation process between states in the density matrix form.
Definition density {n} ( : Matrix n 1) : Matrix n n := × †. Definition super {m n} (M : Matrix m n) : Matrix n n -> Matrix m m := fun => M × × M†.We introduce the simplification strategy called superreduce for states in the density matrix form.
Ltac super_reduce:= unfold super,density; (* Expand super and density *) match goal with (* Match the pattern of target with U × × † × U† *) ||-context [ (?A × ?B) × ?A† ] => match B with | (?C × ?C†) => transitivity ((A × C) × (C† × A†) (* Cast uniform types *) end end; [repeat rewrite <- Mmult_assoc; reflexivity|..]; rewrite <- Mmult_adjoint; (* Extract adjoint *) let Hs := fresh "Hs" in match goal with ||-context [ (?A × ?B) × ?C† ) = ?D × ?D†]=> match C with | ?A × ?B=> assert (A × B = D) as Hs end end; (* Use operate_reduce to prove vector states and rewrite it in density matrix form *) [try reflexivity; try operate_reduce | repeat rewrite Hs; reflexivity].In the above strategy, we first expand the density and super functions in the target. Next, we match the pattern of the target to see whether it is in the form (the middle of the equation in (5)) and cast uniform types, for the reasons to be discussed in Section 5. Then we exploit the law in L14 to extract adjoint of multiplication terms so the target becomes as in the right hand side of the equation in (5). Finally, we use the strategy operatereduce to conduct the proof for states in vector form and rewrite it back in density matrix form. Note that we omit dimensions for presentation purpose, in practice we need to specify these implicit arguments.
3.7 Mixed state
As mentioned in Section 2.2, the mixed state is an ensemble of pure states , where is the probability of the pure state with . In Coq, we use a list of pairs of real numbers and density operators to define mixed states.
Definition Pure n := (R * (Matrix n n)). Definition Mix n := (list (Pure n)).
Recall that the application of unitary operator on the mixed state is in the following form:
(6) |
We formalize (6) in Coq as follows.
Fixpoint UnitMix {n} (A : Matrix n n) (m : Mix n): Mix n := match m with | [] => [] | a :: b => (match a with |(x , y) => (x , super A y) end) :: (UnitMix A b) end.
After a measurement, results can be expressed with mixed states, as introduced in Section 2.2. For simplicity, we use projection measurements, so for each measurement operator we have . We first define the measurement operators of any dimension as follows, where the two parameters and stand for the space dimension and the position of an active qubit.
Definition Mea0 (n k : N) := (I (2^k) ⊗ ∣0⟩⟨0∣ ⊗ I (2^(n-k))). Definition Mea1 (n k : N) := (I (2^k) ⊗ ∣1⟩⟨1∣ ⊗ I (2^(n-k))). Definition Mea (n k : N) := Mea0 n k .+ Mea1 n k.Then we formalize measurements on the mixed state in Coq as follows. Note that a pure state can be regarded as a special mixed state .
Fixpoint MeaMix {n} (m k : N) (l : Mix n) : Mix n := match l with | [] => [] | a :: b => match a with | (x , y) => [((x * (trace((Mea0 m k) × y))), /(trace ((Mea0 m k)× y)).* super(Mea0 m k) y); ((x * (trace((Mea1 m k) × y))), /(trace ((Mea1 m k)× y)).* super(Mea1 m k) y)] end ++ (MeaMix m k b) end.
4 Equivalences of circuits
In order to judge whether two circuits have the same behavior, we need to formally define reasonable notions of equivalence for circuits in the first place. In this section, we propose two candidate relations: one is called matrix equivalence and the other observational equivalence.
4.1 Matrix equivalence
A natural way of interpreting a quantum circuit without measurements is to consider each quantum gate as a unitary matrix and the whole circuit as a composition of matrices that eventually reduces to a single matrix. From this viewpoint, two circuits are equivalent if they denote the same unitary matrix, that is, matrix equivalence suffices to stand for circuit equivalence.
= | = | ||||
= | = | ||||
= | = | ||||
= | = | ||||
= | = | ||||
= | = | ||||
= | = | ||||
= | = |
Directly showing that two matrices are equivalent requires to inspect their elements and compare them component-wisely. Instead, we can take a functional view of matrix equivalence. Let be two matrices, then if and only if for any vector , where is the space of all -qubit states.
Lemma MatrixEquiv_spec: forall {n} (A B: Matrix n n), A = B <-> (forall v: Vector n, A × v = B × v).At first sight, it appears difficult to verify whether for all vectors , since there are infinitely many vectors in the state space . However, both matrices and represent linear operators, which means that it suffices to consider the vectors in an orthonormal basis of , where there are only vectors.
In Figure 2 we list some laws that are often useful in simplifying circuits before showing that they are equivalent. Let us verify the validity of the laws. Take the first one as an example. Its validity is stated in Lemma unitX. In order to prove that lemma, we apply MatrixEquivspec and reduce it to Lemma unitX’, which can be easily proved by the strategy operatereduce.
Lemma unit_X : X × X = I_2. Lemma unit_X’ : forall v : Vector 2, X × X × v = I_2 × v.
In the right column of Figure 2, the subscripts of and indicate on which qubits the quantum gates are applied. For example, means that the Pauli-X gate is applied on the second qubit. Thus, the operation actually stands for .
In Figure 3, we display some equivalent circuits. In diagram (a), on the right of = is a schematic specification of swapping two qubits, which is implemented by the circuit on the left. Mathematically, the equality is described by Lemma Eq15 below.
Definition SWAP := B0 ⊗ B0 .+ B1 ⊗ B2 .+ B2 ⊗ B1 .+ B3 ⊗ B3. Definition XC := X ⊗ B3 .+ I_2 ⊗ B0. Lemma Eq15 : SWAP = CX × XC × CX.In diagram (b), there is a controlled operation performed on the second qubit, conditioned on the first qubit being set to zero. It is equivalent to a gate enclosed by two Pauli-X gates on the first qubit. The equality is specified by Lemma Eq16 below.
Definition not_CX := B0 ⊗ X .+ B3 ⊗ I_2. Lemma Eq16 : not_CX = (X ⊗ I_2) × CX × (X ⊗ I_2).In diagram (c), the controlled phase shift gate on the left is equivalent to a circuit for two qubits on the right. Lemma Eq17 gives a description of this equality.
Definition CE (u: R) := B0 ⊗ I_2 .+ B3 ⊗ (Cexp u .* B0 .+ Cexp u .* B3). Lemma Eq17 : CE u = (B0 .+ Cexp u .* B3) ⊗ I_2.In diagram (d), a controlled gate with two targets is equivalent to the concatenation of two gates. This is formalized by Lemma Eq18 below.
Definition CXX := B0 ⊗ I_2 ⊗ I_2 .+ B3 ⊗ X ⊗ X. Definition CIX := B0 ⊗ I_2 ⊗ I_2 .+ B3 ⊗ I_2 ⊗ X. Lemma Eq18 : CXX = CIX × (CX ⊗ I_2).The previous four lemmas can all be proved by using the strategy operatereduce in conjunction with MatrixEquivspec.
In Section 3 we have formalized the preparation of the 3-qubit GHZ state (cf. Figure 1). Now let us have a look at the Bell states. Depending on the input states, the circuit in Figure 4 gives four possible output states. The correctness of the circuit is validated by the four lemmas below, where the states are given in terms of density matrices and the circuit is described by a super-operator. It is easy to prove them by using our strategy superreduce.
Definition bl00 := /√2 .* (∣0,0⟩) .+ /√2 .* (∣1,1⟩). Definition bl01 := /√2 .* (∣0,1⟩) .+ /√2 .* (∣1,0⟩). Definition bl10 := /√2 .* (∣0,0⟩) .+ (-/√2) .* (∣1,1⟩). Definition bl11 := /√2 .* (∣0,1⟩) .+ (-/√2) .* (∣1,0⟩). Lemma pb00 : super (CX × (H ⊗ I_2)) (density ∣0,0⟩) = density bl00. Lemma pb01 : super (CX × (H ⊗ I_2)) (density ∣0,1⟩) = density bl01. Lemma pb10 : super (CX × (H ⊗ I_2)) (density ∣1,0⟩) = density bl10. Lemma pb11 : super (CX × (H ⊗ I_2)) (density ∣1,1⟩) = density bl11.
4.2 Observational equivalence
An alternative way of interpreting a quantum circuit without measurements is to consider it as an operator that changes input quantum states to output states and abstracts away unobservable details. Therefore, we reuse the notion of matrix equivalence introduced in Section 3 and call it observational equivalence for quantum circuits.
The rationale of using is that from an observational point of view, global phases are irrelevant to the observed properties of the physical system under consideration and can be ignored as far as quantum states are concerned.
The following lemma provides a functional view of observational equivalence. It is a counterpart of MatrixEquivspec given in Section 4.1. Let be two operators, we have if and only if for any state .
Lemma ObsEquiv_operator: forall {n} (A B: Matrix n n), A ≈ B <-> (forall : Matrix n 1, A × ≈ B × ).Furthermore, two states are equal modulo a global phase, i.e. if and only if their density matrices are exactly the same, i.e. . We formally prove this property as it motivated us to introduce density matrices to represent quantum states in Section 3.6.
Lemma ObsEquiv_state: forall {n} ( : Matrix n 1), ≈ <-> × (†) = × (†) .
Although both matrix equivalence and observational equivalence can be used for relating circuits, the former is strictly finer than the latter in the sense that implies but not the other way around. Therefore, in the rest of the paper, we relate circuits by whenever possible, as they are also related by .
Moreover, it is not difficult to see that is a congruence relation. For example, if are two quantum gates and , then we can add a control qubit to form controlled- and controlled- gates, which are still identified by . However, the relation does not satisfy such kind of congruence property. To see this, note that . But the controlled- gate is quite different from controlled-(): the latter transforms into whereas the former keeps it unchanged. In Section 6.1 we will see a concrete example of using , where quantum states are identified by purposefully ignoring their global phases.
5 Problems from Coq’s type system and our solution
In principle, the Dirac notation is fully symbolic, i.e. no matter how we formalize it, the relevant laws and their proofs should remain unchanged. However, it turns out that different design choices in the formalization do make a difference.
In Coq, there are three kinds of equivalence between expressions: (1) syntactic equality, (2) -reduction, and (3) provable equivalence. Specifically, and are syntactically equal if they are the same Coq expression. If and are not the same Coq expression, it is still possible for them to be -reducible to the same term.111Coq is an extension of the lambda calculus. The -reduction here means the -reduction of the underlying lambda calculus. In that case, and can be used interchangeably in Coq, i.e. if is a well-typed proposition, then so is , and every proof of is also a proof of . Moreover, if is a legal Coq type, then so is , and every element of is also an element of . If and are not -equivalent, it is still possible for them to be provably equivalent. For example, the two expressions and are not -equivalent in Coq for a general variable but they are provably equal to each other. In other words, a proof of is NOT necessarily a proof of although we can always derive a proof of from a proof of . Moreover, if is a type, its element is not necessarily an element of type .
Matrix definition.
When it comes to matrix definitions. The first problem is to decide whether matrices and matrices are -equivalent Coq types or not. Intuitively, since these two kinds of matrices are mathematically the same object, they should be used interchangeably. However, and are not -equivalent. Thus, we have to carefully define the Coq type of matrices so that those two kinds above are -equivalent types. We follow the approach used in QWIRE PRZ17 and define matrices (no matter how large they are) to be functions from two natural numbers (row and column numbers) to complex numbers.
Definition Matrix (m n : N) := nat -> nat -> C.But still, there are two problems that need to be solved.
The elements outside the range of a matrix.
Intuitively, this definition above says that, given a pair of numbers , if and then the entry of the matrix in the -th row and -th column is a complex number determined by the mapping. However, if or then the number determined by the mapping does not correspond to a valid entry in the matrix. In manual proofs we can simply ignore those elements outside the range of the matrix.
(7) |
(8) |
For example, in (7) and (8) we give two mappings in the form of two infinite-dimensional matrices and , respectively. Basically, is the same as except that all the elements with rows (resp. columns) greater than or equal to (resp. ) are in the former but they are in the latter. In computer-aided proofs, we could choose to only reason about well-formed matrices whose “outside elements” are all zero like above. Paykin et al. PRZ17 heavily used this approach in their work. They would consider well-formed but ill-formed. However, only reasoning about well-formed matrices imposes a heavy burden for formal proofs because the condition of well-formedness needs to be checked each time we manipulate matrices. In our development, we choose a relaxed notion of matrix equivalence, which also appeared in PRZ17 , so that two matrices are deemed to be equivalent if they are equal component-wisely within the desired dimensions, and outside the dimensions the corresponding elements can be different. For instance, this notion of equivalence allows us to identify with . With a slight abuse of notation, we still use the symbol to denote the newly defined matrix equivalence222Nevertheless, we keep our Coq script in the repository at Github more rigid. There we use to stand for the relaxed notion of matrix equivalence and reserve for the stronger notion of equivalence in the sense that means the two matrices and are equal component-wisely both within and outside their dimensions., and prove its elementary properties about scale product, matrix product, matrix addition, tensor product and conjugate transpose. Reasoning about matrices modulo that equivalence turns out to be convenient in Coq. Specifically, the automation of the rewriting strategies mentioned above does not require side condition proofs about well-formedness.
Coq type casting for rewriting.
In math, is a matrix and it is only verbose to say it is a matrix. Even though is convertible to , these two typing claims are not syntactically identical in Coq but only -equivalent to each other. This difference is significant in rewriting. For example, the associativity of matrix multiplication is usually described as follows.
But more formally, the associativity means for any natural numbers , , , and matrix , matrix and matrix ,
where we use subscripts under to indicate matrices’ dimensions. These parameters do appear (implicitly) in Coq’s formalization of matrix multiplication’s associativity. Thus rewriting does not work in the following case
because rewriting uses an exact syntax match. This problem of type mismatch often occurs after we use the law L13 for rewriting. We choose to build a customized rewrite tactic to overcome this problem. Using the example above, we want to rewrite via the associativity of multiplication. We first do a pattern matching for expressions of the form
no matter whether n1 and p1 coincide with n2 and p2, respectively. We then use Coq’s built-in unification to unify n1, p1 with n2, p2. This unification must succeed or else the original expression of matrix computation is not well-formed. After the expression is changed to
we can use Coq’s original rewrite tactic via the associativity of multiplication.
We handle the above mentioned type problems silently and whoever uses our system to formalize his/her own proof will not even feel these problems.
6 Case studies
To illustrate the power of our symbolic approach of reasoning about quantum circuits, we conduct a few case studies and compare the approach with the computational one in PRZ17 .
6.1 Deutsch’s algorithm
Given a boolean function , Deutsch Deu85 presented a quantum algorithm that can compute in a single evaluation of . The algorithm can tell whether equals or not, without giving any information about the two values individually. The quantum circuit in Figure 5 gives an implementation of the algorithm. It makes use of a quantum oracle that maps any state to the state , where . More specifically, the unitary operator can be in one of the following four forms:
-
•
if , then ;
-
•
if , then ;
-
•
if and , then ;
-
•
if and , then .
We formalize Deutsch’s algorithm in Coq and use our symbolic approach to prove its correctness. Let us suppose that is the input state. There are three phases in this quantum circuit. The first phase applies the Hadamard gate to each of the two qubits. So we define the initial state and express the state after the first phase as follows.
Definition 0 := ∣0⟩ ⊗ ∣1⟩. Definition 1 := (H ⊗ H) × 0. Lemma step1 : 1 = ∣+⟩ ⊗ ∣-⟩.Lemma step1 claims that the intermediate state after the first phase is . We can use the strategy operatereduce designed in Section 3 to prove its correctness.
The second phase applies the unitary operator to . Since has four possible forms, we consider four cases.
Definition 20 := (I_2 ⊗ I_2) × 1. Definition 21 := (I_2 ⊗ X) × 1. Definition 22 := CX × 1. Definition 23 := (B0 ⊗ X .+ B3 ⊗ I_2) × 1. Lemma step20 : 20 = ∣+⟩ ⊗ ∣-⟩. Lemma step21 : 21 = -1 .* ∣+⟩ ⊗ ∣-⟩. Lemma step22 : 22 = ∣-⟩ ⊗ ∣-⟩. Lemma step23 : 23 = -1 .* ∣-⟩ ⊗ ∣-⟩.Each of the above four lemmas corresponds to one case. They claim that the intermediate state is after the second phase when , and when . We prove the four lemmas by rewriting with Lemma step1 and using the strategy operatereduce again.
The last phase applies the Hadamard gate to the first qubit of . So we still have four cases.
Definition 30 := (H ⊗ I_2) × 20. ... Lemma step30 : 30 = ∣0⟩ ⊗ ∣-⟩. Lemma step31 : 31 = -1 .* ∣0⟩ ⊗ ∣-⟩. Lemma step32 : 32 = ∣1⟩ ⊗ ∣-⟩. Lemma step33 : 33 = -1 .* ∣1⟩ ⊗ ∣-⟩.Observe that the only difference between and lies in the global phase , which can be ignored. Similarly for and . Formally, we can prove the following lemmas.
Lemma step31’ : 31 ≈ ∣0⟩ ⊗ ∣-⟩. Lemma step33’ : 33 ≈ ∣1⟩ ⊗ ∣-⟩.Therefore, after the last phase, we have when , and when . This is proved by using the intermediate results obtained in the first two phases and the strategy operatereduce.
The above reasoning about the Deutsch’s algorithm proceeds step by step and shows all the intermediate states in each phase. Alternatively, one may be only interested in the output state of the circuit once an input state is fed. In other words, we would like to show a property like
Formally, we need to prove four equations depending on the forms of .
Lemma deutsch00 : (H ⊗ I_2) × (I_2 ⊗ I_2) × (H ⊗ H) × (∣0⟩ ⊗ ∣1⟩) = ∣0⟩ ⊗ ∣-⟩ . Lemma deutsch01 : (H ⊗ I_2) × (I_2 ⊗ X) × (H ⊗ H) × (∣0⟩ ⊗ ∣1⟩) = -1 .* ∣0⟩ ⊗ ∣-⟩ . Lemma deutsch10 : (H ⊗ I_2) × CX × (H ⊗ H) × (∣0⟩ ⊗ ∣1⟩ = ∣1⟩ ⊗ ∣-⟩ . Lemma deutsch11 : (H ⊗ I_2) × (B0 ⊗ X .+ B3 ⊗ I_2) × (H ⊗ H) × (∣0⟩ ⊗ ∣1⟩) = -1 .* ∣1⟩ ⊗ ∣-⟩.The second and fourth equations can be written in a simpler form as follows.
Lemma deutsch01’ : (H ⊗ I_2) × (I_2 ⊗ X) × (H ⊗ H) × (∣0⟩ ⊗ ∣1⟩) ≈ ∣0⟩ ⊗ ∣-⟩ . Lemma deutsch11’ : (H ⊗ I_2) × (B0 ⊗ X .+ B3 ⊗ I_2) × (H ⊗ H) × (∣0⟩ ⊗ ∣1⟩) ≈ ∣1⟩ ⊗ ∣-⟩ .Using our symbolic reasoning, these lemmas can be easily proved by rewrite ObsEquivstate and operatereduce. Thus, we know that the first qubit of the result state is when , and otherwise. That is, as expected.
6.2 Teleportation
Quantum teleportation BB93 is one of the most important protocols in quantum information theory. It teleports an unknown quantum state by only sending classical information, by making use of a maximally entangled state. In particular, the algorithm involves performing intermediate measurements on the quantum state and then applying different operations on the intermediate measurement results represented by mixed states.
Let the sender and the receiver be and , respectively. The quantum teleportation protocol goes as follows, as illustrated by the quantum circuit in Figure 6.
-
1.
and prepare an EPR state together. Then they share the qubits, holding and holding .
-
2.
To transmit the state of the quantum qubit , applies a operation on and followed by an operation on .
-
3.
measures and and sends the outcome to .
-
4.
When receives , he applies appropriate Pauli gates on his qubit to recover the original state of .
We formalize the quantum teleportation protocol in Coq and use our symbolic approach to prove its correctness. Let be any vector used as a part of the input state. The other part is , which needs extra preparation. For simplicity, we directly represent with a combination of and . So we define the input state as follows.
Variables ( : C). Hypothesis Normalise: ||^2 + ||^2 = 1. Definition : Vector 2 := .* ∣0⟩ .+ .* ∣1⟩. Definition 0 := ⊗ bl00.
The input state goes through the quantum circuit that comprises four phases. We can easily define the quantum pure state after the second phase as follows, and prove Lemma tele1 by operate_reduce.
Definition 2 := (H ⊗ I_2 ⊗ I_2) × (CX ⊗ I_2) × 0. Lemma tele1 : 2 = .* (∣+⟩ ⊗ bl00) .+ .* (∣-⟩ ⊗ bl01).
In the third phase, due to the measurement with measurement operators , where and , there are four possible cases for the state , and the probability for each case can be calculated as
So we define and as follows, where are the measurement outcomes for the top two qubits. Then we prove Lemma tele2 about mixed states by super_reduce and some data processing. (We only consider projection operators as measurement operators, so we can replace with ).
Definition p00 := trace ((B0 ⊗ B0 ⊗ I_2) × (density 2)). Definition 300 := 1/p00 .* (super (B0 ⊗ B0 ⊗ I_2)(density 2)). ... Lemma tele2 : MeaMix 2 1 (MeaDen 2 0 (density 2)) .= [(1/4, 4 .* density (/√2 .* (∣00⟩ ⊗ ( .* ∣0⟩ .+ .* ∣1⟩)))); [(1/4, 4 .* density (/√2 .* (∣01⟩ ⊗ ( .* ∣1⟩ .+ .* ∣0⟩)))); [(1/4, 4 .* density (/√2 .* (∣10⟩ ⊗ ( .* ∣0⟩ .+ - .* ∣1⟩)))); [(1/4, 4 .* density (/√2 .* (∣11⟩ ⊗ ( .* ∣1⟩ .+ - .* ∣0⟩))))].
Finally, according to the different measurement results on the first two qubits, we apply corresponding Pauli gates on the third qubit. So the quantum state after the fourth phase becomes . We can formalize it in Coq as follows.
Definition 400 := 30. Definition 401 := super (I_2 ⊗ I_2 ⊗ X) 310. Definition 410 := super (I_2 ⊗ I_2 ⊗ Z) 310. Definition 411 := super ((I_2 ⊗ I_2 ⊗ Z)×(I_2 ⊗ I_2 ⊗ X)) 311. Lemma tele3 : [(1/4, 400);[(1/4, 401); [(1/4, 410);[(1/4, 411)] .= [(1/4, density (∣0,0⟩ ⊗ ); (1/4, density (∣0,1⟩ ⊗ ); [(1/4, density (∣1,0⟩ ⊗ ); (1/4, density (∣1,1⟩ ⊗ )].
Using the simplification strategies discussed in Section 3, it is easy to prove that can be simplified to be , and the probability of each case is .
In summary, we can use the following equality to express the whole protocol.
It shows that the third qubit of the result state is always equal to , the state to be teleported from Alice to Bob.
6.3 Simon’s Algorithm
The Simon’s problem was raised in 1994 Simon97 . Although it is an artificial problem, it inspired Shor to discover a polynomial time algorithm to solve the integer factorization problem.
Given a function , suppose there exists a string such that the following property is satisfied
(9) |
for all . Here is the bit-wise modulo 2 addition of two bit-strings. The goal of Simon’s algorithm is to find the string . The algorithm consists of iterating the quantum circuit and then performing some classical post-processing.
-
1.
Set an initial state , and apply Hadamard gates to the first qubits respectively.
-
2.
Apply an oracle to all the qubits, where .
-
3.
Apply Hadamard gates to the first qubits again and then measure them.
When , the probability of obtaining each string is
Therefore, it can be seen that the result string must satisfy and be evenly distributed. Repeating this process times, we will get strings so that for . Thus we have linear equations with unknowns ( is the number of bits in s). The goal is to solve this system of equations to get . We can get a unique non-zero solution if we are lucky and are linearly independent. Otherwise, we repeat the entire process and will find a linearly independent set with a high probability.
As an example, we consider the Simon’s algorithm with . The quantum circuit is displayed in Figure 7. We design the oracle as the gates in the dotted box , where the gate is defined in page 3. For this oracle, satisfies property (9). The change of states can be seen as follows.
We can establish the following lemma with our symbolic approach and use the strategy superreduce to prove it.
Lemma simon : super ((H ⊗ H ⊗ I_2 ⊗ I_2) × (I_2 ⊗ CX ⊗ I_2) × (CIX ⊗ X) × (H ⊗ H ⊗ I_2 ⊗ I_2)) (density ∣0,0,0,0⟩) = density (/2 .* ∣0,0,0,1⟩ .+ /2 .* ∣1,1,0,1⟩ .+ /2 .* ∣0,0,1,1⟩ .+ -/2 .* ∣1,1,1,1⟩).
We analyze the cases where the last two qubits are in the state or . The corresponding first two qubits are in or , each occurs with equal probability. By property (9), it means that or , so we obtain that = 11.
6.4 Grover’s algorithm
In this section we consider Grover’s search algorithm . The algorithm starts from the initial state . It first uses (the gate applied to each of the qubits) to obtain a uniform superposition state, and then applies the Grover iteration repeatedly. An implementation of the Grover iteration has four steps:
-
1.
Apply the oracle ;
-
2.
Apply the Hadamard transform ;
-
3.
Perform a conditional phase shift on , if ;
-
4.
Apply the Hadamard transform again.
Here the conditional phase-shift unitary operator in the third step is . We can merge the last three steps as follows,
where with . Therefore, the Grover iteration becomes .
As a concrete example, we consider the Grover’s algorithm with two qubits. The size of the search space of this algorithm is four. So we need to consider four search cases with . The oracle must satisfy that if , then , otherwise . So in accordance with , we design four oracles , which are implemented by the four circuits in Figure 8.
Definition ORA0 := B0 ⊗ (B0 ⊗ X .+ B3 ⊗ I_2) .+ B3 ⊗ I_2 ⊗ I_2. Definition ORA1 := B0 ⊗ CX .+ B3 ⊗ I_2 ⊗ I_2. Definition ORA2 := B0 ⊗ I_2 ⊗ I_2 .+ B3 ⊗ (B0 ⊗ X .+ B3 ⊗ I_2). Definition ORA3 := B0 ⊗ I_2 ⊗ I_2 .+ B3 ⊗ CX.
The whole algorithm is illustrated by the circuit in Figure 9. The gates in the dotted box perform the conditional phase shift operation . We then merge the front and back gates to it and get the operation as follows.
Definition MI := (B0 .+ B1 .+ B2 .+ B3) ⊗ (B0 .+ B1 .+ B2 .+ B3). Definition CPS := (((/2 .* MI) .+ (-1) .* (I_2 ⊗ I_2)) ⊗ I_2).So we have the Grover iteration . Let the initial state be . After the Hadamard transform , we only perform Grover iteration once to get the search solution. In summary, we formalize the Grover’s algorithm with two qubits in the vector form as follows, and use operatereduce to prove them. The reasoning using density matrices can also be done.
Lemma Gro0: (I_2 ⊗ I_2 ⊗ H) × CPS × ORA0 × (H ⊗ H ⊗ H) × ∣0,0,1⟩ = ∣0,0,1⟩. Lemma Gro1: (I_2 ⊗ I_2 ⊗ H) × CPS × ORA1 × (H ⊗ H ⊗ H) × ∣0,0,1⟩ = ∣0,1,1⟩. Lemma Gro2: (I_2 ⊗ I_2 ⊗ H) × CPS × ORA2 × (H ⊗ H ⊗ H) × ∣0,0,1⟩ = ∣1,0,1⟩. Lemma Gro3: (I_2 ⊗ I_2 ⊗ H) × CPS × ORA3 × (H ⊗ H ⊗ H) × ∣0,0,1⟩ = ∣1,1,1⟩.
For all the case studies in the current and previous subsections, we employ the same schema: first represent the quantum circuits and input states symbolically, and then apply the strategies developed in Section 3 to show that the output states are equal to our desired states. When the states are in the vector form, we use the strategy operatereduce. When the states are in the density matrix form, we make use of the strategy superreduce, which decomposes density matrices into multiplications of vectors and then essentially resort to operatereduce, as discussed in Section 3. In addition, if it is necessary to identify states up to an ignorance of global phases, we have the extra task of rewriting terms with ObsEquivstate.
6.5 Deutsch-Jozsa algorithm family
Representing a family of algorithms with unbounded qubit size, the Deutsch-Jozsa algorithm is a generalization application of the Deutsch algorithm on qubits. Given a boolean function as a quantum oracle, the algorithm judges whether is a constant or a balanced function. Here is balanced if it is equal to 1 for half of the input , and 0 for the other half. As shown in Figure 11, the overall steps of the Deutsch-Jozas algorithm are as follows:
-
1.
Apply Hadamard gates to all the qubits respectively;
-
2.
Apply the oracle to the qubits, where ;
-
3.
Apply Hadamard gates to the first qubits again and then measure them.
Suppose the input state is . After each of the above three steps, we get three states , and respectively.
From , we easily get . Then due to the fact that , we have . Finally, for a single qubit we know that , we obtain , where is the bitwise inner product of and . After measuring the first qubits, we analyse the probability of the event that the final result is . When is , the coefficient of is , which is equal to . Thus, when is a constant function, the measurement result must be . Otherwise, when is a balanced one, the result would not be . In summary, based on the measurement of the first qubits, we can determine whether is a constant or a balanced function and only query the oracle once.
In particular, consider the circuit of shown in Figure 11, we specify in Coq as follows.
Fixpoint Uf (n:nat): Matrix (2*2^(N.of_nat n)) (2*2^(N.of_nat n)):= match n with | O => I 2 | S n’ => (CX ⊗ I (2^(N.of_nat n’))) × (I 2 ⊗ (Uf n’)) × (CX ⊗ I (2^(N.of_nat n’))) end.For convenience, we predefine an auxiliary function , which stands for . When is 1, it degenerates into , i.e. 1.
Fixpoint kron_n (n:nat) m1 m2 (A : Matrix m1 m2) : Matrix (m1^(N.of_nat n)) (m2^(N.of_nat n)) := match n with | 0 => I 1 | S n’ => kron A (kron_n n’ A) end.According to the above three steps, we use the following three lemmas to prove the correctness of the Deutsch-Jozsa algorithm.
Lemma DJ_0 : ((kron_n n H) ⊗ H) × ((kron_n n ∣0⟩) ⊗ ∣1⟩) = (kron_n n ∣+⟩) ⊗ ∣-⟩. Lemma DJ_1 : (n > 0)%nat -> (Uf n) × ((kron_n n ∣+⟩) ⊗ ∣-⟩) = (kron_n n ∣+⟩) ⊗ ∣-⟩. Lemma DJ_2 : ((kron_n n H) ⊗ H) × ((kron_n n ∣+⟩) ⊗ ∣-⟩) = (kron_n n ∣0⟩) ⊗ ∣1⟩.
By using the proof assistant Coq, we can formalise the proof about qubits with induction and rewriting strategies. Since the number is unbounded, the correctness of the algorithm is difficult to be proved without proof assistant platforms. As an example, we consider the second lemma. We first focus on the case of ,
This case can be easily proved by operatereduce. According to the inductive hypothesis, we assume that the algorithm is correct for the case of . Then we are going to prove the case of with it and some laws in Table 1. The proof steps are as follows.
After applying the Hadamard gates to the first qubits, we can get that the first qubits of the result is , So we know that the described as is a constant function.
6.6 Preparation of an entangled state
So far we have seen some simple examples with only a few qubits. Now let us consider a bigger example with a dozen qubits. The quantum circuit in Figure 12 can be used to create a maximally entangled state.
We start with the initial state with qubits. First, we apply the H gate on the first qubit to change the global state into with the first qubit in a superposition. It is equivalent to . Then we apply the CX gate on the first and second qubits and , which leads to the state . Then we apply the Toffoli gate on the first three qubits to yield . In a similar way, we apply the Toffoli gate on the qubits , and for in turn. Eventually, we obtain a maximally entangled state .
The correctness of the quantum circuit is stated by the following lemma.
Lemma Entangled_state_12 : (I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ TOF ) × (I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ TOF ⊗ I_2) × (I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ TOF ⊗ I_2 ⊗ I_2) × (I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ TOF ⊗ I_2 ⊗ I_2 ⊗ I_2) × (I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ TOF ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2) × (I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ TOF ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2) × (I_2 ⊗ I_2 ⊗ I_2 ⊗ TOF ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2) × (I_2 ⊗ I_2 ⊗ TOF ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2) × (I_2 ⊗ TOF ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2) × (TOF ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2) × (CX ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2) × (H ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2 ⊗ I_2) × ∣0,0,0,0,0,0,0,0,0,0,0,0⟩ =≡ /√2 .* ∣0,0,0,0,0,0,0,0,0,0,0,0⟩ .+ /√2 .* ∣1,1,1,1,1,1,1,1,1,1,1,1⟩.By just using the strategy operate_reduce, we can prove the above property in half an hour. Note that this example cannot be handled by the computational approach — going beyond qubits is very difficult for that approach.
6.7 Experiments
Deutsch | Simon | Teleportation | Secret Sharing | QFT | Grover | |
---|---|---|---|---|---|---|
Symbolic | 3656 | 53795 | 39715 | 68919 | 25096 | 146834 |
Computational | 25190 | 180724 | 46450 | 170490 | 68730 | 934570 |
We have conducted experiments on Deutsch’s algorithm, Simon’s algorithm, quantum teleportation, quantum secret sharing protocol, quantum Fourier transform (QFT) with three qubits, and Grover’s search algorithm with two qubits. In Table 2, we record the execution time of those examples in milliseconds in CoqIDE 8.10.0 running on a PC with Intel Core i5-7200 CPU and 8 GB RAM. As we can see in the table, our symbolic approach always outperforms the computational one in PRZ17 .
The computational approach is slow because of the explicit representation of matrices and inefficient tactics for evaluating matrix multiplications. Let us consider a simple example. In the computational approach, the Hadamard gate is defined by ha below:
Definition ha : Matrix 2 2 := fun x y => match x, y with | 0, 0 => (1 / √2) | 0, 1 => (1 / √2) | 1, 0 => (1 / √2) | 1, 1 => -(1 / √2) | _, _ => 0 end.Since is unitary, we have and the following property becomes straightforward.
Lemma H3_ket0: (ha ⊗ ha ⊗ ha) × (ha ⊗ ha ⊗ ha) × (∣0,0,0⟩) = (∣0,0,0⟩).However, to prove the above lemma with the computational approach is far from being trivial. To see this, we literally go through a few steps. Firstly, we apply the associativity of matrix multiplication on the left hand side of the equation so to rewrite it into
Secondly, each explicitly represented matrix is converted into a two-dimensional list and matrix multiplications are calculated in order. Finally, we need to show that each of the eight elements in the vector on the left is equal to the corresponding element on the right. Let and . With the computational approach, obvious simplifications such as multiplication and addition with and are carried out for the elements in and , and no more complicated simplification is effectively handled. So is a two-dimensional list with each element in the form and is a two-dimensional list whose first element is
which is a summation of eight identical summands with multiplied with itself six times; other elements are in similar forms. From this simple example, we can already see that the explicit matrix representation and ineffective simplification in matrix multiplication make the intermediate expressions very cumbersome.
On the contrary, in the symbolic approach we have
Notice that here we have kept the structure of tensor products rather than to eliminate them. In fact, we lazily evaluate tensor products because they are expensive to calculate and preserving more higher-level structures opens more opportunities for rewriting. The symbolic reasoning not only renders the intermediate expressions more readable, but also greatly reduces the time cost of arithmetic calculations.
In general, in the computational approach a multiplication of two matrices of -length expressions results in a matrix of -length expressions, and those expressions are not effectively simplified. At the end of the computation, a matrix of -length expressions is obtained if matrices of size are multiplied together, which takes exponential time to simplify. In our approach, we represent matrices symbolically and simplify intermediate expressions efficiently on the fly, which has a much better performance.
7 Related work
Formal verification in quantum computing has been growing rapidly, especially in Coq. Boender et al. BKN15 presented a framework for modeling and analyzing quantum protocols using Coq. They made use of the Coq repository C-CoRN LCHF04 and built a matrix library with dependent types. Cano et al. CCDMS16 specifically designed CoqEAL, a library built on top of ssreflect ssref to develop efficient computer algebra programs with proofs of correctness. They represented a matrix as a list of lists for efficient generic matrix computation in Coq but they did not consider optimizations specific for matrices commonly used in quantum computation. Paykin et al. PRZ17 defined a quantum circuit language QWIRE in Coq, and formally verified some quantum programs expressed in that language RPZ18 ; RPLZ19 . Reasoning using their matrix library usually requires explicit computation, which does not scale well, as discussed in Section 6.7. Hietala et al. HRHWH19 developed a quantum circuit compiler VOQC in Coq, which uses several peephole optimization techniques such as replacement, propagation, and cancellation as proposed by Nam et al. YNYA18 to reduce the number of unitary transformations. It is very different from our symbolic approach of simplifying matrix operations using the Dirac notation. Mahmoud et al. MF19 formalized the semantics of Proto-Quipper in Coq and formally proved the type soundness property. They developed a linear logical framework within the Hybrid system FM12 and used it to represent and reason about the linear type system of Quipper GLRSV13 .
Our formalization of matrices is partly based on QWIRE PRZ17 . We choose to use their formalization of matrices and matrix operations (, , , , ), but without well-formness assumptions. And apart from that, our formalization of gates using Dirac notation, the notion of observational equivalence for circuits, and our systematic tactic library are novel.
Note that although sparse matrix computation is well studied in other areas of Computer Science, we are not aware of any library in Coq dedicated to sparse matrices. We consider the symbolic approach proposed in the current work as a contribution in this perspective. For example, let us consider the gate CX. It is represented as a sparse matrix with out of the entries being non-zero (cf. Section 2.2). As we can see in (2), its symblic representation is
(10) |
using the terms and (). Those terms are the basic building blocks of our formalization of quantum circuits and our symbolic reasoning. The expression in (10) can be viewed as a compact way of representing the sparse matrix of gate . Furthermore, representing sparse matrices using Dirac notation is convenient for readability and cancelling zero matrices due to orthogonality of basic vectors.
Apart from Coq, other proof assistants have also been used to verify quantum circuits and programs. Liu et al. LZWYLLYZ19 used the theorem prover Isabelle/HOL NPW02 to formalize a quantum Hoare logic Yin16 and verify its soundness and completeness for partial correctness. Unruh [6] developed a relational quantum Hoare logic and implemented an Isabelle-based tool to prove the security of post-quantum cryptography and quantum protocols. Beillahi et al. BMT19 verified quantum circuits with up to 190 two-qubit gates in HOL Light. It relies on the formalization of Hilbert spaces in HOL Light proposed by Mahmoud et al. in MAT13 , where a number of laws about complex functions and linear operators are proved. Although linear operators correspond to matrices in the finite-dimension case, our results are not implied by those in BMT19 ; MAT13 . For instance, a quantum state in BMT19 is necessarily a vector, which means that only pure states can be represented. In contrast, we can also deal with mixed states in the form of density matrices. Chareton et al. Chr21 proposed a verification framework QBRICKS embedded in the Why3 deductive verification tool WHY . It represents quantum circuits by a variant of path-sum representation Amy18 called parametrized path-sums and can describe circuits in a functional language efficiently. However, this representation is measurement-free, which is different from the our approach as we can directly describe quantum measurements on quantum states represented by density matrices.
Notice that the laws in Table 1 play an important role in our symbolic reasoning of quantum circuits. Although they resemble to some laws in a ring, the matrices under our consideration can be of various dimensions and they do not form a ring. It is also critical that the multiplication of two matrices, e.g. a row vector and a column vector, could be a scalar number (and even zero). Thus, rings are not enough here. The proof-by-reflection technique for rings might be useful but are usually hard to develop. We have shown that the tactic-based method is already efficient in our application scenario, and also flexible for both fully-automated and interactive proofs.
8 Conclusion and future work
We have proposed a symbolic approach to reasoning about quantum circuits in Coq. It is based on a small set of equational laws which are exploited to design some simplification strategies. According to our case studies, the approach is more efficient than the usual one of explicitly representing matrices and is well suited to be automated in Coq.
Notice that in the current work the Dirac notation is restricted to states on a computational basis. However, the notation is general enough to express states on other bases as well as linear operators. We leave it as a future work to investigate the more general scenarios so as to take full advantage of the Dirac notation.
Dealing with quantum circuits is our intermediate goal. More interesting algorithms such as the Shor’s algorithm Sho94 also require classical computation. In the near future, we plan to formalize in Coq the semantics of a quantum programming language with both classical and quantum features.
References
- (1) L. Anticoli, C. Piazza, L. Taglialegne, P. Zuliani. Towards Quantum Programs Verification: From Quipper Circuits to QPMC. In International Conference on Reversible Computation, LNCS 9720: 213-219, 2016.
- (2) C.H. Bennett, G. Brassard, C. Crepeau, R. Jozsa, A. Peres, and W. Wootters. Tele- porting an unknown quantum state via dual classical and EPR channels. Physical Review Letters, 70:1895–1899, 1993.
- (3) S.M. Beillahi, M.Y. Mahmoud, S. Tahar. A Modeling and Verification Framework for Optical Quantum Circuits. Formal Aspects of Computing 31: 321-351, 2019.
- (4) Jaap Boender, Florian Kammller, Rajagopal Nagarajan. Formalization of Quantum Protocols using Coq. In Proceedings of the 12th International Workshop on Quantum Physics and Logic, EPTCS 195:71-83, 2015.
-
(5)
Sylvie Boldo, Catherine Lelay, Guillaume Melquiond. Coquelicot. Available at
http://coquelicot.saclay.inria.fr/. - (6) Guillaume Cano, Cyril Cohen, Maxime Dens, Anders Mrtberg, Vincent Siles. CoqEAL - The Coq Effective Algebra Library. https://github.com/CoqEAL/CoqEAL, 2016.
- (7) Coq Development Team. The Coq Proof Assistant Reference Manual. Electronic resource, available from http://coq.inria.fr.
- (8) Luis Cruz-Filipe, Herman Geuvers, Freek Wiedijk. C-CoRN, the Constructive Coq Repository at Nijmegen. Mathematical Knowledge Management, Lecture Notes in Computer Science 3119: 88-103, 2004.
- (9) David Deutsch. Quantum theory, the Church-Turing principle, and the Universal Quantum Computer. Proceedings of the Royal Society of London A, 400:97-117, 1985.
- (10) P.A.M. Dirac. A New Notation for Quantum Mechanics. Mathematical Proceedings of the Cambridge Philosophical Society 35(3): 416-418, 1939.
- (11) A.P. Felty, A. Momigliano. Hybrid: A Definitional Two-Level Approach to Reasoning with Higher-Order Abstract Syntax. Journal of Automated Reasoning, 48(1): 43-105, 2012
- (12) A.S. Green, P.L. Lumsdaine, N.J. Ross, P. Selinger, B. Valiron. Quipper: A Scalable Quantum Programming Language. Acm Sigplan Notices, 48(6): 333-342, 2013.
- (13) Daniel M. Greenberger, Michael A. Horne, Anton Zeilinger. Bell’s theorem, Quantum Theory, and Conceptions of the Universe. pp. 73-76, Kluwer Academics, Dordrecht, The Netherlands 1989.
- (14) Kesha Hietala, Robert Rand, Shih-Han Hung, Xiaodi Wu, Michael Hicks. Verified Optimization in a Quantum Intermediate Representation. In Proceedings of the 16th International Conference on Quantum Physics and Logic, CoRR abs/1904.06319, 2019.
- (15) Junyi Liu, Bohua Zhan, Shuling Wang, Shenggang Ying, Tao Liu, Yangjia Li, Mingsheng Ying, Naijun Zhan. Formal Verification of Quantum Algorithms Using Quantum Hoare Logic. In Proc. CAV 2019, LNCS 11562: 187-207. Springer, 2019.
- (16) M.Y. Mahmoud, Y. Aravantinos, S. Tahar. Formalization of Infinite Dimension Linear Spaces with Application to Quantum Theory. In Nasa Formal Methods Symposium, LNCS 7871: 413-427, 2013.
- (17) M.Y. Mahmoud, A.P. Felty. Formalization of Metatheory of the Quipper Quantum Programming Language in a Linear Logic. Journal of Automated Reasoning, 63(4): 967-1002, 2019.
- (18) Mathematical Components team. Mathematical Components. https://math-comp.github.io
- (19) Yunseong Nam, Neil J. Ross, Yuan Su, Andrew M. Childs, Dmitri Maslov. Automated Optimization of Large Quantum Circuits with Continuous Parameters. npj Quantum Information, 4(1): 23, 2018.
- (20) M.A. Nielsen, I.L.Chuang. Quantum Computation and Quantum Information: 10th Anniversary Edition. Cambridge University Press, 2011.
- (21) Tobias Nipkow, Lawrence C. Paulson, Markus Wenzel. Isabelle/HOL: A Proof Assistant for Higher-Order Logic. Lecture Notes in Computer Science. Springer, 2002.
- (22) Jennifer Paykin, Robert Rand, Steve Zdancewic. QWIRE: A Core Language for Quantum Circuits. In Proceedings of the 44th ACM SIGPLAN Symposium on Principles of Programming Languages, 52: 846-858, 2017.
- (23) Robert Rand, Jennifer Paykin, Steve Zdancewic. QWIRE Practice: Formal Verification of Quantum Circuits in Coq. In Proceedings of the 14th International Conference on Quantum Physics and Logic, EPTCS 266: 119-132, 2018.
- (24) Robert Rand, Jennifer Paykin, Dong-Ho Lee, Steve Zdancewic. ReQWIRE: Reasoning about Reversible Quantum Circuits. In Proceedings of the 15th International Conference on Quantum Physics and Logic, EPTCS 287: 299-312, 2019.
- (25) P.W. Shor. Algorithms for Quantum Computation: Discrete Log and Factoring. In Proc. FOCS 1994, 124-133, IEEE Computer Society, 1994.
- (26) Daniel R. Simon, On the power of quantum computation, SIAM Journal on Computing 26 (5), 1474 - 1483, 1997.
- (27) J. von Neumann. States, Effects and Operations: Fundamental Notions of Quantum Theory. Princeton University Press, 1955.
- (28) Mingsheng Ying. Foundations of Quantum Programming. Morgan Kaufmann, 2016.
- (29) Christophe Chareton, Sébastien Bardin, François Bobot, Valentin Perrelle, Benoit Valiron. An Automated Deductive Verification Framework for Circuit-building Quantum Programs. In Proc. ESOP 2021, LNCS 12648: 148 - 177. Springer, 2021.
- (30) Matthias Felleisen, Philippa Gardner. Why3 - Where Programs Meet Provers. In Proc. ESOP 2013, LNCS 7792: 125-128. Springer, 2013.
- (31) Matthew Amy. Towards Large-scale Functional Verification of Universal Quantum Circuits. In Proceedings of the 15th International Conference on Quantum Physics and Logic, EPTCS 287: 1-21, 2018.