This paper was converted on www.awesomepapers.org from LaTeX by an anonymous user.
Want to know more? Visit the Converter page.

11institutetext: East China Normal University, China 22institutetext: Shanghai Jiao Tong University, China 33institutetext: East China Normal University, China 44institutetext: Yanqi Lake Beijing Institute of Mathematical Sciences and Applications, China 55institutetext: University of Technology Sydney, Australia

Symbolic Reasoning about Quantum Circuits in Coq

Wenjun Shi    Qinxiang Cao   
Yuxin Deng
   Hanru Jiang    Yuan Feng
(This is a pre-print of an article published in Journal of Computer Science and Technology.
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 |0\ket{0}, |1\ket{1}, scalars, and a set of basic operators such as tensor product and matrix multiplication. Here |0\ket{0} and |1\ket{1} are the Dirac notation for 2-dimensional column vectors [1 0]T[1\ 0]^{T} and [0 1]T[0\ 1]^{T}, 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 i|j\braket{i}{j} 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 |0\ket{0} state. In an explicit matrix-vector multiplication form, it reads as follows.

X|0=[0110][10]=[0×1+1×01×1+0×0]=[01]=|1X\ket{0}=\begin{bmatrix}0&1\\ 1&0\end{bmatrix}\begin{bmatrix}1\\ 0\end{bmatrix}=\begin{bmatrix}0\times 1+1\times 0\\ 1\times 1+0\times 0\end{bmatrix}=\begin{bmatrix}0\\ 1\end{bmatrix}=\ket{1}

and four multiplications are required for the whole computation. By contrast, if we use the Dirac notation for XX and apply distribution and associativity laws, then

X|0=(|01|+|10|)|0=|01|0+|10|0=0|0+1|1=|1.\begin{array}[]{rcl}X\ket{0}&=&\left(\ket{0}\bra{1}+\ket{1}\bra{0}\right)\ket{0}\\ &=&\ket{0}\braket{1}{0}+\ket{1}\braket{0}{0}\\ &=&0\ket{0}+1\ket{1}\\ &=&\ket{1}.\end{array}

Note that the two terms 1|0\braket{1}{0} and 0|0\braket{0}{0} 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 \mathcal{H} is a complete vector space equipped with an inner product

|:×\langle\cdot|\cdot\rangle:\mathcal{H}\times\mathcal{H}\rightarrow\mathbb{C}

such that

  1. 1.

    ψ|ψ0\langle\psi|\psi\rangle\geq 0 for any |ψ|\psi\rangle\in\mathcal{H}, with equality if and only if |ψ=0|\psi\rangle=0;

  2. 2.

    ϕ|ψ=ψ|ϕ\langle\phi|\psi\rangle=\langle\psi|\phi\rangle^{\ast};

  3. 3.

    ϕ|ici|ψi=iciϕ|ψi\langle\phi|\sum_{i}c_{i}|\psi_{i}\rangle=\sum_{i}c_{i}\langle\phi|\psi_{i}\rangle,

where \mathbb{C} is the set of complex numbers, and for each cc\in\mathbb{C}, cc^{\ast} stands for the complex conjugate of cc. A vector |ψ|\psi\rangle\in\mathcal{H} is normalised if its length ψ|ψ\sqrt{\langle\psi|\psi\rangle} is equal to 11. Two vectors |ψ|\psi\rangle and |ϕ|\phi\rangle are orthogonal if ψ|ϕ=0\langle\psi|\phi\rangle=0. An orthonormal basis of a Hilbert space \mathcal{H} is a basis {|i}\{|i\rangle\} where each |i|i\rangle is normalised and any pair of them is orthogonal.

Let ()\mathcal{L(H)} be a set of linear operators on \mathcal{H}. For any A()A\in\mathcal{L(H)}, AA is Hermitian if A=AA^{\dagger}=A where AA^{\dagger} is the adjoint operator of AA such that ψ|A|ϕ=ϕ|A|ψ\langle\psi|A^{\dagger}|\phi\rangle=\langle\phi|A|\psi\rangle^{*} for any |ψ,|ϕ|\psi\rangle,|\phi\rangle\in\mathcal{H}. The fundamental spectral theorem states that the set of all normalised eigenvectors of a Hermitian operator in ()\mathcal{L(H)} constitutes an orthonormal basis for \mathcal{H}. That is, there exists a so-called spectral decomposition for each Hermitian AA such that

A=iλi|ii|,A~{}=~{}\sum_{i}\lambda_{i}|i\rangle\langle i|,

where the set {|i}\{|i\rangle\} constitutes an orthonormal basis of \mathcal{H}, {λi}\{\lambda_{i}\} denotes the set of eigenvalues of AA, and |ii||i\rangle\langle i| is the projector to the corresponding eigenspace of λi\lambda_{i}. A linear operator A()A\in\mathcal{L(H)} is unitary if AA=AA=IA^{\dagger}A=AA^{\dagger}=I_{\mathcal{H}} where II_{\mathcal{H}} is the identity operator on \mathcal{H}. The trace of AA is defined as tr(A)=ii|A|i{\rm tr}(A)=\sum_{i}\langle i|A|i\rangle for some given orthonormal basis {|i}\{|i\rangle\} of \mathcal{H}. 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 tr(AB)=tr(BA){\rm tr}(AB)={\rm tr}(BA) for any A,B()A,B\in\mathcal{L(H)}.

Let 1\mathcal{H}_{1} and 2\mathcal{H}_{2} be two Hilbert spaces. Their tensor product 12\mathcal{H}_{1}\otimes\mathcal{H}_{2} is defined as a vector space consisting of linear combinations of the vectors |ψ1ψ2=|ψ1|ψ2=|ψ1|ψ2|\psi_{1}\psi_{2}\rangle=|\psi_{1}\rangle|\psi_{2}\rangle=|\psi_{1}\rangle\otimes|\psi_{2}\rangle with |ψ11|\psi_{1}\rangle\in\mathcal{H}_{1} and |ψ22|\psi_{2}\rangle\in\mathcal{H}_{2}. Here the tensor product of two vectors is defined by a new vector such that

(iλi|ψi)(jμj|ϕj)=i,jλiμj|ψi|ϕj.\left(\sum_{i}\lambda_{i}|\psi_{i}\rangle\right)\otimes\left(\sum_{j}\mu_{j}|\phi_{j}\rangle\right)=\sum_{i,j}\lambda_{i}\mu_{j}|\psi_{i}\rangle\otimes|\phi_{j}\rangle.

Then 12\mathcal{H}_{1}\otimes\mathcal{H}_{2} is also a Hilbert space where the inner product is defined in the following way: for any |ψ1,|ϕ11|\psi_{1}\rangle,|\phi_{1}\rangle\in\mathcal{H}_{1} and |ψ2,|ϕ22|\psi_{2}\rangle,|\phi_{2}\rangle\in\mathcal{H}_{2},

ψ1ψ2|ϕ1ϕ2=ψ1|ϕ11ψ2|ϕ22,\langle\psi_{1}\otimes\psi_{2}|\phi_{1}\otimes\phi_{2}\rangle=\langle\psi_{1}|\phi_{1}\rangle_{\mathcal{H}_{1}}\langle\psi_{2}|\phi_{2}\rangle_{\mathcal{H}_{2}},

where |i\langle\cdot|\cdot\rangle_{\mathcal{H}_{i}} is the inner product of i\mathcal{H}_{i}. For any A1(1)A_{1}\in\mathcal{L}(\mathcal{H}_{1}) and A2(2)A_{2}\in\mathcal{L}(\mathcal{H}_{2}), A1A2A_{1}\otimes A_{2} is defined as a linear operator in (12)\mathcal{L}(\mathcal{H}_{1}\otimes\mathcal{H}_{2}) such that for each |ψ11|\psi_{1}\rangle\in\mathcal{H}_{1} and |ψ22|\psi_{2}\rangle\in\mathcal{H}_{2},

(A1A2)|ψ1ψ2=A1|ψ1A2|ψ2.(A_{1}\otimes A_{2})|\psi_{1}\psi_{2}\rangle=A_{1}|\psi_{1}\rangle\otimes A_{2}|\psi_{2}\rangle.

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 ρ\rho on Hilbert space \mathcal{H} is a positive linear operator such that tr(ρ)=1{\rm tr}(\rho)=1. Another equivalent representation of a density operator is an ensemble NC11 of pure states. In particular, given an ensemble {(pi,|ψi)}\{(p_{i},|\psi_{i}\rangle)\} where pi0p_{i}\geq 0, ipi=1\sum_{i}p_{i}=1, and |ψi|\psi_{i}\rangle are pure states, then ρ=ipi|ψiψi|\rho=\sum_{i}p_{i}|\psi_{i}\rangle\langle\psi_{i}| 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

|Ψ=12(|00+|11).|\Psi\rangle=\frac{1}{\sqrt{2}}(|00\rangle+|11\rangle).

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 |ψ|\psi\rangle be a state vector and θ\theta a real number. In quantum mechanics, the state eiθ|ψe^{i\theta}|\psi\rangle is considered to be equal to |ψ|\psi\rangle, up to the global phase factor eiθe^{i\theta}. 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 t1t_{1} and t2t_{2} are |ψ1|\psi_{1}\rangle and |ψ2|\psi_{2}\rangle, respectively, then |ψ2=U|ψ1|\psi_{2}\rangle=U|\psi_{1}\rangle for some unitary operator UU which depends only on t1t_{1} and t2t_{2}. 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 mm by nn complex unitary matrix UU with entries UijU_{ij} can be considered as a unitary operator sending vectors in the vector space n\mathbb{C}^{n} to the vector space m\mathbb{C}^{m}, under matrix multiplication of the matrix UU by a vector in n\mathbb{C}^{n}.

We often denote a single qubit as a vector |ψ=α|0+β|1|\psi\rangle=\alpha|0\rangle+\beta|1\rangle parameterized by two complex numbers satisfying the condition |α|2+|β|2=1|\alpha|^{2}+|\beta|^{2}=1. A unitary operator for a qubit is then described by a 2×22\times 2 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 11-qubit Hadamard gate HH, the Pauli gates I2,X,Y,ZI_{2},X,Y,Z, the controlled-NOT gate CXCX performed on two qubits, and the 33-qubit Toffoli gate. Their matrix representations are given below:

I2=(1001),X=(0110),Y=(0ii0),Z=(1001),I_{2}=\left(\begin{array}[]{cc}1&0\\ 0&1\\ \end{array}\right),\qquad X=\left(\begin{array}[]{cc}0&1\\ 1&0\\ \end{array}\right),\qquad Y=\left(\begin{array}[]{cc}0&-i\\ i&0\\ \end{array}\right),\qquad Z=\left(\begin{array}[]{cc}1&0\\ 0&-1\\ \end{array}\right),
H=12(1111),CX=(1000010000010010),TOF=(1000000001000000001000000001000000001000000001000000000100000010).H=\frac{1}{\sqrt{2}}\left(\begin{array}[]{cc}1&1\\ 1&-1\\ \end{array}\right),\qquad CX=\left(\begin{array}[]{cccc}1&0&0&0\\ 0&1&0&0\\ 0&0&0&1\\ 0&0&1&0\end{array}\right),\qquad TOF=\left(\begin{array}[]{cccccccc}1&0&0&0&0&0&0&0\\ 0&1&0&0&0&0&0&0\\ 0&0&1&0&0&0&0&0\\ 0&0&0&1&0&0&0&0\\ 0&0&0&0&1&0&0&0\\ 0&0&0&0&0&1&0&0\\ 0&0&0&0&0&0&0&1\\ 0&0&0&0&0&0&1&0\end{array}\right).

For example, in Figure 1 we display a circuit that can generate the 33-qubit Greenberger–Horne–Zeilinger state (GHZ state) GHZ , which is |000+|1112\frac{|000\rangle+|111\rangle}{\sqrt{2}}. 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.

\Qcircuit@C=.8em@R=1.7em\lstick|0&\qw\gateH\qw\ctrl1\qw\qw\qw\qw\lstick|0\qw\qw\qw\targ\qw\ctrl1\qw\qw\lstick|0\qw\qw\qw\qw\qw\targ\qw\qw\Qcircuit@C=.8em@R=1.7em{\lstick{\ket{0}}&\qw\gate{H}\qw\ctrl{1}\qw\qw\qw\qw\\ \lstick{\ket{0}}\qw\qw\qw\targ\qw\ctrl{1}\qw\qw\\ \lstick{\ket{0}}\qw\qw\qw\qw\qw\targ\qw\qw\\ }
Figure 1: A circuit for creating the 3-qubit GHZ state

A quantum measurement is described by a collection {Mm}\{M_{m}\} of measurement operators, where the indices mm refer to the measurement outcomes. It is required that the measurement operators satisfy the completeness equation mMmMm=I\sum_{m}M_{m}^{{\dagger}}M_{m}=I_{\mathcal{H}}. If the state of a quantum system is |ψ|\psi\rangle immediately before the measurement, then the probability that result mm occurs is given by

p(m)=ψ|MmMm|ψ,p(m)=\langle\psi|M_{m}^{\dagger}M_{m}|\psi\rangle,

and the state of the system after the measurement is Mm|ψp(m).\frac{M_{m}|\psi\rangle}{\sqrt{p(m)}}. If the states of the system at times t1t_{1} and t2t_{2} are mixed, say ρ1\rho_{1} and ρ2\rho_{2}, respectively, then ρ2=Uρ1U\rho_{2}=U\rho_{1}U^{{\dagger}} after the unitary operation UU is applied on the system. For the same measurement {Mm}\{M_{m}\} as above, if the system is in the mixed state ρ\rho, then the probability that measurement result mm occurs is given by

p(m)=tr(MmMmρ),p(m)={\rm tr}(M_{m}^{{\dagger}}M_{m}\rho),

and the state of the post-measurement system is MmρMmp(m)\frac{M_{m}\rho M_{m}^{{\dagger}}}{p(m)} provided that p(m)>0p(m)>0.

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: \mathbb{C}
Basic vectors: |0|0\rangle, |1|1\rangle
Operators: \cdot, ×\times, ++, \otimes, {\dagger}
Laws: L1 0|0=1|1=1\langle 0|0\rangle=\langle 1|1\rangle=1,  0|1=1|0=0\langle 0|1\rangle=\langle 1|0\rangle=0
L2 Associativity of ,×,+,\cdot,\ \times,\ +,\ \otimes
L3 0Am×n=0m×n0\cdot A_{m\times n}=\textbf{0}_{m\times n},  c0=0c\cdot\textbf{0}=\textbf{0},  1A=A1\cdot A=A
L4 c(A+B)=cA+cBc\cdot(A+B)=c\cdot A+c\cdot B
L5 c(A×B)=(cA)×B=A×(cB)c\cdot(A\times B)=(c\cdot A)\times B=A\times(c\cdot B)
L6 c(AB)=(cA)B=A(cB)c\cdot(A\otimes B)=(c\cdot A)\otimes B=A\otimes(c\cdot B)
L7 0m×n×An×p=0m×p=Am×n×0n×p\textbf{0}_{m\times n}\times A_{n\times p}=\textbf{0}_{m\times p}=A_{m\times n}\times\textbf{0}_{n\times p}
L8 Im×Am×n=Am×n=Am×n×InI_{m}\times A_{m\times n}=A_{m\times n}=A_{m\times n}\times I_{n},   ImIn=ImnI_{m}\otimes I_{n}=I_{mn}
L9 0+A=A=A+0\textbf{0}+A=A=A+\textbf{0}
L10 0m×nAp×q=0mp×nq=Ap×q0m×n\textbf{0}_{m\times n}\otimes A_{p\times q}=\textbf{0}_{mp\times nq}=A_{p\times q}\otimes\textbf{0}_{m\times n}
L11 (A+B)×C=A×C+B×C(A+B)\times C=A\times C+B\times C,  C×(A+B)=C×A+C×BC\times(A+B)=C\times A+C\times B
L12 (A+B)C=AC+BC(A+B)\otimes C=A\otimes C+B\otimes C,  C(A+B)=CA+CBC\otimes(A+B)=C\otimes A+C\otimes B
L13 (AB)×(CD)=(A×C)(B×D)(A\otimes B)\times(C\otimes D)=(A\times C)\otimes(B\times D)
L14 (cA)=cA(c\cdot A)^{\dagger}=c^{*}\cdot A^{\dagger},  (A×B)=B×A(A\times B)^{\dagger}=B^{\dagger}\times A^{\dagger}
L15 (A+B)=A+B(A+B)^{\dagger}=A^{\dagger}+B^{\dagger},  (AB)=AB(A\otimes B)^{\dagger}=A^{\dagger}\otimes B^{\dagger}
L16 (A)=A(A^{\dagger})^{\dagger}=A
Table 1: Terms and laws

3.1 Terms and laws

Our symbolic reasoning is based on terms constructed from scalars and basic vectors using some constructors:

  • Scalars are complex numbers. We write \mathbb{C} for the set of complex numbers. Our formal treatment of complex numbers is based on the definitions and lemmas in the library of Paykin et al. PRZ17 for complex numbers, which, in turn, is adapted from the Coquelicot library BLM .

  • Basic vectors are the base states of a qubit, i.e., |0|0\rangle and |1|1\rangle in the Dirac notation. Mathematically, |0|0\rangle stands for the vector [1 0]T[1\ 0]^{T} and |1|1\rangle for [0 1]T[0\ 1]^{T}.

  • Constructors include the scalar product \cdot, matrix product ×\times, matrix addition ++, tensor product \otimes, and the conjugate transpose AA^{\dagger} of a matrix AA.

In Dirac notations, 0|\langle 0| represents the dual of |0|0\rangle, i.e. |0|0\rangle^{\dagger}; similarly for 1|\langle 1|. The term j|×|k\langle j|\times|k\rangle is abbreviated into j|k\langle j|k\rangle, for any j,k{0,1}j,k\in\{0,1\}. This notation introduces an intuitive explanation of quantum operation. For example, the effect of the XX operator is to map |0|0\rangle into |1|1\rangle and |1|1\rangle into |0|0\rangle. Thus we define XX in Coq as |01|+|10||0\rangle\langle 1|+|1\rangle\langle 0|, instead of [0 11 0]\begin{bmatrix}0\,1\\ 1\,0\end{bmatrix}. Then it is obvious that X|0=|10|0+|01|0=|1X|0\rangle=|1\rangle\langle 0|0\rangle+|0\rangle\langle 1|0\rangle=|1\rangle and similarly for X|1X|1\rangle.

Some commonly used vectors and gates can be derived from the basic terms. For example, we define the vectors |+|+\rangle and ||-\rangle as follows.

|+=12|0+12|1|=12|0+(12)|1|+\rangle~{}=~{}\frac{1}{\sqrt{2}}\cdot|0\rangle+\frac{1}{\sqrt{2}}\cdot|1\rangle\qquad\qquad|-\rangle~{}=~{}\frac{1}{\sqrt{2}}\cdot|0\rangle+(-\frac{1}{\sqrt{2}})\cdot|1\rangle

We also define four simple matrices B0,,B3B_{0},...,B_{3}.

B0=|0×0|B1=|0×1|B2=|1×0|B3=|1×1|B_{0}~{}=~{}|0\rangle\times\langle 0|\quad\quad B_{1}~{}=~{}|0\rangle\times\langle 1|\quad\quad B_{2}~{}=~{}|1\rangle\times\langle 0|\quad\quad B_{3}~{}=~{}|1\rangle\times\langle 1| (1)

The Hadamard matrix HH is a combination of the four matrices above.

H=12B0+12B1+12B2+(12)B3H~{}=~{}\frac{1}{\sqrt{2}}\cdot B_{0}+\frac{1}{\sqrt{2}}\cdot B_{1}+\frac{1}{\sqrt{2}}\cdot B_{2}+(-\frac{1}{\sqrt{2}})\cdot B_{3}

Similarly, the Pauli-X gate and the controlled-NOT gate CXCX are given as follows.

X=B1+B2CX=B0I2+B3XX~{}=~{}B_{1}+B_{2}\qquad\qquad CX~{}=~{}B_{0}\otimes I_{2}+B_{3}\otimes X (2)

Notice that tensor products of matrices from {Bj:j=0,1,2,3}\{B_{j}:j=0,1,2,3\} constitute an orthonormal basis for the space of all matrices of dimension 2n2^{n}, for all n1n\geq 1. 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 T1=T2T_{1}=T_{2} be a law that relates two terms T1T_{1} and T2T_{2}. We say that the law is sound if both terms can be reduced to exactly the same matrix, after their metavariables (e.g., |0\ket{0}) are instantiated by concrete matrices (e.g., [1 0]T[1\ 0]^{T}). We define a strategy called orthogonal_\_reduce to verify that the laws in L1 are sound. In this case, we explicitly represent |0|0\rangle and |1|1\rangle as matrices. Both of them are 2×12\times 1 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 0|0=1\langle 0|0\rangle=1 is actually shown via [1 0][10]=1[1\ 0]\begin{bmatrix}1\\ 0\end{bmatrix}=1. 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 B0,,B3B_{0},...,B_{3} defined in (1) are the basic building blocks to represent 2×22\times 2 matrices, and are intensively used in our symbolic reasoning. We design a strategy called base_\_reduce to prove some equations about them acting on the base states |0|0\rangle and |1|1\rangle. For example, let us consider the equation B0×|0=|0B_{0}\times|0\rangle=|0\rangle. We first represent B0B_{0} by |0×0||0\rangle\times\langle 0|, then use the associativity of matrix multiplication to form the subterm 0|×|0\langle 0|\times|0\rangle. Now we can use the proved laws in L1 to rewrite 0|×|0\langle 0|\times|0\rangle into 11. 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 gate_\_reduce to prove some equations about the matrices I2,X,Y,Z,HI_{2},X,Y,Z,H acting on base states. For example, consider the equation X×|0=|1X\times|0\rangle=|1\rangle. We first expand XX into B1+B2B_{1}+B_{2}. In order to prove the equation (B1+B2)×|0=|1(B_{1}+B_{2})\times|0\rangle=|1\rangle, we use the distributivity of matrix multiplication over addition to rewrite the left hand side of the equation into the sum of B1×|0B_{1}\times|0\rangle and B2×|0B_{2}\times|0\rangle. 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 B0,,B3B_{0},...,B_{3} and I2,X,Y,Z,HI_{2},X,Y,Z,H acting on states |+|+\rangle and ||-\rangle. For example, we have H|+=|0H|+\rangle=|0\rangle and H|=|1H|-\rangle=|1\rangle, etc.

3.5 Strategy for circuits

We propose a strategy called operate_\_reduce 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 |0|0|0|0\rangle\otimes|0\rangle\otimes|0\rangle. We would like to verify that the output state is indeed what we expect by establishing the following equation.

(I2CX)×(CXI2)×(HI2I2)×(|0|0|0)=12(|0|0|0)+12(|1|1|1)\begin{array}[]{rl}&(I_{2}\otimes CX)\times(CX\otimes I_{2})\times(H\otimes I_{2}\otimes I_{2})\times(|0\rangle\otimes|0\rangle\otimes|0\rangle)\\ =&\frac{1}{\sqrt{2}}\cdot(|0\rangle\otimes|0\rangle\otimes|0\rangle)+\frac{1}{\sqrt{2}}\cdot(|1\rangle\otimes|1\rangle\otimes|1\rangle)\end{array} (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

(I2CX)×((CXI2)×((H(I2I2))×(|0(|0|0)))).\begin{array}[]{rl}&(I_{2}\otimes CX)\times((CX\otimes I_{2})\times((H\otimes(I_{2}\otimes I_{2}))\times(|0\rangle\otimes(|0\rangle\otimes|0\rangle)))).\end{array} (4)

Then we calculate the inner layer matrix multiplications in sequence. We first calculate (H(I2I2))×(|0(|0|0))(H\otimes(I_{2}\otimes I_{2}))\times(|0\rangle\otimes(|0\rangle\otimes|0\rangle)). 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.

(H(I2I2))×(|0(|0|0))=(H×|0)((I2×|0)(I2×|0))=|+(|0|0)\begin{array}[]{rl}&(H\otimes(I_{2}\otimes I_{2}))\times(|0\rangle\otimes(|0\rangle\otimes|0\rangle))\\ =&(H\times|0\rangle)\otimes((I_{2}\times|0\rangle)\otimes(I_{2}\times|0\rangle))\\ =&|+\rangle\otimes(|0\rangle\otimes|0\rangle)\\ \end{array}

Correspondingly, the expression in (4) turns into

(I2CX)×((CXI2)×(|+(|0|0))).\begin{array}[]{rl}&(I_{2}\otimes CX)\times((CX\otimes I_{2})\times(|+\rangle\otimes(|0\rangle\otimes|0\rangle))).\end{array}

The inner layer matrix multiplication to be calculated next is the following expression: (CXI2)×(|+(|0|0))(CX\otimes I_{2})\times(|+\rangle\otimes(|0\rangle\otimes|0\rangle)). 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.

(CXI2)×(|+(|0|0))=((B0I2+B3X)I2)×(|+(|0|0))=((B0I2I2+B3XI2)×(|+(|0|0))=((B0(I2I2))×(|+(|0|0))+((B3(XI2)×(|+(|0|0))=((B0×|+)(I2×|0)(I2×|0))+((B3×|+)(X×|0)(I2×|0))=12(|0(|0|0))+12(|1(|1|0))\begin{array}[]{rl}&(CX\otimes I_{2})\times(|+\rangle\otimes(|0\rangle\otimes|0\rangle))\\ =&((B_{0}\otimes I_{2}+B_{3}\otimes X)\otimes I_{2})\times(|+\rangle\otimes(|0\rangle\otimes|0\rangle))\\ =&((B_{0}\otimes I_{2}\otimes I_{2}+B_{3}\otimes X\otimes I_{2})\times(|+\rangle\otimes(|0\rangle\otimes|0\rangle))\\ =&((B_{0}\otimes(I_{2}\otimes I_{2}))\times(|+\rangle\otimes(|0\rangle\otimes|0\rangle))+((B_{3}\otimes(X\otimes I_{2})\times(|+\rangle\otimes(|0\rangle\otimes|0\rangle))\\ =&((B_{0}\times|+\rangle)\otimes(I_{2}\times|0\rangle)\otimes(I_{2}\times|0\rangle))+((B_{3}\times|+\rangle)\otimes(X\times|0\rangle)\otimes(I_{2}\times|0\rangle))\\ =&\frac{1}{\sqrt{2}}\cdot(|0\rangle\otimes(|0\rangle\otimes|0\rangle))+\frac{1}{\sqrt{2}}\cdot(|1\rangle\otimes(|1\rangle\otimes|0\rangle))\end{array}

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 operate_\_reduce 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 operate_\_reduce, 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 i|j\braket{i}{j} 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 |ψ|\psi\rangle from eiθ|ψe^{i\theta}|\psi\rangle for any θ\theta. However, when written in vector form, |ψ|\psi\rangle and eiθ|ψe^{i\theta}|\psi\rangle may be different because of the coefficient eiθe^{i\theta} present in the latter but not in the former. Therefore, we use the symbol \approx to denote such an equivalence, i.e. eiθ|ψ|ψe^{i\theta}|\psi\rangle\approx|\psi\rangle. 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 /\\backslash  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 \approx.

Note that if quantum states are represented by density matrices, we have

(eiθ|ψ)(eiθ|ψ)=(eiθ|ψ)(eiθψ|)=|ψψ|.(e^{i\theta}|\psi\rangle)(e^{i\theta}|\psi\rangle)^{\dagger}~{}=~{}(e^{i\theta}|\psi\rangle)(e^{-i\theta}\langle\psi|)~{}=~{}|\psi\rangle\langle\psi|.

Therefore, the discrepancy entailed by the global phase disappears: the two vectors eiθ|ψe^{i\theta}|\psi\rangle and |ψ|\psi\rangle correspond to the same density matrix |ψψ||\psi\rangle\langle\psi|. 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 ρ\rho. After the execution of a quantum circuit implementing some unitary transformation UU, the system changes into the new state ρ=UρU\rho^{\prime}=U\rho U^{\dagger}. Let ρ=jλj|jj|\rho=\sum_{j}\lambda_{j}|j\rangle\langle j| be its spectral decomposition, where λj\lambda_{j} are eigenvalues of ρ\rho and the vectors |j|j\rangle the corresponding eigenvectors. It follows that

ρ=U(jλj|jj|)U=jλjU|j(U|j).\rho^{\prime}~{}=~{}U(\sum_{j}\lambda_{j}|j\rangle\langle j|)U^{\dagger}~{}=~{}\sum_{j}\lambda_{j}U|j\rangle(U|j\rangle)^{\dagger}\ . (5)

Therefore, we can first simplify U|jU|j\rangle into a vector, take its dual and then obtain ρ\rho^{\prime} 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} (ψ\psi : Matrix n 1) : Matrix n n := ψ\psi × ψ\psi†.
Definition super {m n} (M : Matrix m n) : Matrix n n -> Matrix m m :=
   fun ρ\rho => M × ρ\rho × M†.
        
We introduce the simplification strategy called super_\_reduce 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 × ψ\psi × ψ\psi† × 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 U×|ψ×ψ|×UU\times|\psi\rangle\times\langle\psi|\times U^{\dagger} (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 U×|ψ×(U×|ψ)U\times|\psi\rangle\times(U\times|\psi\rangle)^{\dagger} as in the right hand side of the equation in (5). Finally, we use the strategy operate_\_reduce 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 ρ=ipi|ψiψi|\rho=\sum_{i}p_{i}|\psi_{i}\rangle\langle\psi_{i}| is an ensemble {(pi,|ψi)}\{(p_{i},|\psi_{i}\rangle)\} of pure states |ψi|\psi_{i}\rangle, where pip_{i} is the probability of the pure state |ψi|\psi_{i}\rangle with ipi=1\sum_{i}p_{i}=1. 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 UU on the mixed state ρ\rho is in the following form:

ρ=ipi|ψiψi|𝑈UρU=ipjU|ψiψi|U.\rho=\sum_{i}p_{i}|\psi_{i}\rangle\langle\psi_{i}|~{}\xrightarrow{U}~{}U\rho U^{\dagger}=\sum_{i}p_{j}U|\psi_{i}\rangle\langle\psi_{i}|U^{\dagger}. (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 MmM_{m} we have MmMm=MmM_{m}^{{\dagger}}M_{m}=M_{m}. We first define the measurement operators of any dimension as follows, where the two parameters nn and kk 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 ρ\rho can be regarded as a special mixed state [(1,ρ)][(1,\rho)].
    
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.

XXXX = I2I_{2}                  12(X+Z)\frac{1}{\sqrt{2}}\cdot(X+Z) = HH
YYYY = I2I_{2} H2×CX×H2H_{2}×CX×H_{2} = CZCZ
ZZZZ = I2I_{2} CX×X1×CXCX\times X_{1}\times CX = X1×X2X_{1}\times X_{2}
HHHH = I2I_{2} CX×Y1×CXCX\times Y_{1}\times CX = Y1×X2Y_{1}\times X_{2}
CX×CXCX\times CX = I4I_{4} CX×Z1×CXCX\times Z_{1}\times CX = Z1Z_{1}
HXHHXH = ZZ CX×X2×CXCX\times X_{2}\times CX = X2X_{2}
HYHHYH = Y-Y CX×Y2×CXCX\times Y_{2}\times CX = Z1×Y2Z_{1}\times Y_{2}
HZHHZH = XX CX×Z2×CXCX\times Z_{2}\times CX = Z1×Z2Z_{1}\times Z_{2}
Figure 2: More laws

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 A,BA,B be two 2m×2m2^{m}\times 2^{m} matrices, then A=BA=B if and only if A|v=B|vA|v\rangle=B|v\rangle for any vector |v|v\rangle\in\mathcal{H}, where \mathcal{H} is the space of all mm-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 A|v=B|vA|v\rangle=B|v\rangle for all vectors |v|v\rangle, since there are infinitely many vectors in the state space \mathcal{H}. However, both matrices AA and BB represent linear operators, which means that it suffices to consider the vectors in an orthonormal basis of \mathcal{H}, where there are only 2m2^{m} 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 unit_\_X. In order to prove that lemma, we apply MatrixEquiv_\_spec and reduce it to Lemma unit_\_X’, which can be easily proved by the strategy operate_\_reduce.


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 X,Y,ZX,Y,Z and HH indicate on which qubits the quantum gates are applied. For example, X2X_{2} means that the Pauli-X gate is applied on the second qubit. Thus, the operation Y1×X2Y_{1}\times X_{2} actually stands for (YI2)×(I2X)(Y\otimes I_{2})\times(I_{2}\otimes X).

\Qcircuit@C=.7em@R=1em&\ctrl2\qw\targ\qw\ctrl2\qw\qw\qswap\qw\qw\push = \qwx
\targ\qw\ctrl
2\qw\targ\qw\qw\qswap\qwx\qw\qw
\Qcircuit@C=.7em@R=1em{&\ctrl{2}\qw\targ\qw\ctrl{2}\qw\qw\qswap\qw\qw\\ \push{\rule{3.00003pt}{0.0pt}=\rule{3.00003pt}{0.0pt}}\qwx\\ \targ\qw\ctrl{-2}\qw\targ\qw\qw\qswap\qwx\qw\qw\\ }
\Qcircuit@C=.6em@R=1em&\qw\ctrlo2\qw\qw\qw\gateX\ctrl2\gateX\qw\qw\push = 
\qw\targ\qw\qw\qw\qw\targ\qw\qw\qw
\Qcircuit@C=.6em@R=1em{&\qw\ctrlo{2}\qw\qw\qw\gate{X}\ctrl{2}\gate{X}\qw\qw\\ \push{\rule{3.00003pt}{0.0pt}=\rule{3.00003pt}{0.0pt}}\\ \qw\targ\qw\qw\qw\qw\targ\qw\qw\qw\\ }
\Qcircuit@C=.4em@R=.1em&\ctrl2\qw\qw\gate[100eiα]\qw\push = 
\gate
[eiα00eiα]\qw\qw\qw\qw
\Qcircuit@C=.4em@R=.1em{&\ctrl{2}\qw\qw\gate{\left[\begin{array}[]{cc}1&0\\ 0&e^{i\alpha}\\ \end{array}\right]}\qw\\ \push{\rule{3.00003pt}{0.0pt}=\rule{3.00003pt}{0.0pt}}\\ \gate{\left[\begin{array}[]{cc}e^{i\alpha}&0\\ 0&e^{i\alpha}\\ \end{array}\right]}\qw\qw\qw\qw\\ }
\Qcircuit@C=.8em@R=1.7em&\qw\ctrl2\qw\qw\qw\ctrl1\qw\ctrl2\qw\qw\qw\targ\qw\qw\push = \qw\targ\qw\qw\qw\qw
\qw\targ\qw\qw\qw\qw\qw\targ\qw\qw
\Qcircuit@C=.8em@R=1.7em{&\qw\ctrl{2}\qw\qw\qw\ctrl{1}\qw\ctrl{2}\qw\qw\\ \qw\targ\qw\qw\push{\rule{3.00003pt}{0.0pt}=\rule{3.00003pt}{0.0pt}}\qw\targ\qw\qw\qw\qw\\ \qw\targ\qw\qw\qw\qw\qw\targ\qw\qw\\ }
Figure 3: Some equivalent circuits

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 CXCX 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 CXCX 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 operate_\_reduce in conjunction with MatrixEquiv_\_spec.

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 super_\_reduce.

\Qcircuit@C=.8em@R=1.2em&\lstickx\qw\gateH\qw\ctrl2\qw\qw\rstickβxy\lsticky\qw\qw\qw\targ\qw\qw\Qcircuit@C=.8em@R=1.2em{&\\ \lstick{x}\qw\gate{H}\qw\ctrl{2}\qw\qw\\ \rstick{\beta_{xy}}\\ \lstick{y}\qw\qw\qw\targ\qw\qw}
Figure 4: The Bell states
|β00=12|0|0+12|1|1|β01=12|0|1+12|1|0|β10=12|0|012|1|1|β11=12|0|112|1|0\begin{array}[]{rcl}|\beta_{00}\rangle&=&\frac{1}{\sqrt{2}}\cdot|0\rangle\otimes|0\rangle+\frac{1}{\sqrt{2}}\cdot|1\rangle\otimes|1\rangle\\ |\beta_{01}\rangle&=&\frac{1}{\sqrt{2}}\cdot|0\rangle\otimes|1\rangle+\frac{1}{\sqrt{2}}\cdot|1\rangle\otimes|0\rangle\\ |\beta_{10}\rangle&=&\frac{1}{\sqrt{2}}\cdot|0\rangle\otimes|0\rangle-\frac{1}{\sqrt{2}}\cdot|1\rangle\otimes|1\rangle\\ |\beta_{11}\rangle&=&\frac{1}{\sqrt{2}}\cdot|0\rangle\otimes|1\rangle-\frac{1}{\sqrt{2}}\cdot|1\rangle\otimes|0\rangle\end{array}

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 \approx introduced in Section 3 and call it observational equivalence for quantum circuits.

The rationale of using \approx 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 MatrixEquiv_\_spec given in Section 4.1. Let A,BA,B be two operators, we have ABA\approx B if and only if A|ψB|ψA|\psi\rangle\approx B|\psi\rangle for any state |ψ|\psi\rangle.


Lemma ObsEquiv_operator: forall {n} (A B: Matrix n n),
   A ≈ B <-> (forall ψ\psi: Matrix n 1, A × ψ\psi ≈  B × ψ\psi).

Furthermore, two states are equal modulo a global phase, i.e. |ψ|ϕ|\psi\rangle\approx|\phi\rangle if and only if their density matrices are exactly the same, i.e. |ψψ|=|ϕϕ||\psi\rangle\langle\psi|=|\phi\rangle\langle\phi|. 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} (ψ\psi ϕ\phi: Matrix n 1),
   ψ\psiϕ\phi <-> ψ\psi × (ψ\psi†) = ϕ\phi × (ϕ\phi†) .

Although both matrix equivalence == and observational equivalence \approx can be used for relating circuits, the former is strictly finer than the latter in the sense that A=BA=B implies ABA\approx B 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 \approx.

Moreover, it is not difficult to see that == is a congruence relation. For example, if A,BA,B are two quantum gates and A=BA=B, then we can add a control qubit to form controlled-AA and controlled-BB gates, which are still identified by ==. However, the relation \approx does not satisfy such kind of congruence property. To see this, note that III\approx-I. But the controlled-II gate is quite different from controlled-(I-I): the latter transforms 12(|00+|11)\frac{1}{\sqrt{2}}(|00\rangle+|11\rangle) into 12(|00|11)\frac{1}{\sqrt{2}}(|00\rangle-|11\rangle) whereas the former keeps it unchanged. In Section 6.1 we will see a concrete example of using \approx, 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) βη\beta\eta-reduction, and (3) provable equivalence. Specifically, aa and bb are syntactically equal if they are the same Coq expression. If aa and bb are not the same Coq expression, it is still possible for them to be βη\beta\eta-reducible to the same term.111Coq is an extension of the lambda calculus. The βη\beta\eta-reduction here means the βη\beta\eta-reduction of the underlying lambda calculus. In that case, aa and bb can be used interchangeably in Coq, i.e. if P(a)P(a) is a well-typed proposition, then so is P(b)P(b), and every proof of P(a)P(a) is also a proof of P(b)P(b). Moreover, if T(a)T(a) is a legal Coq type, then so is T(b)T(b), and every element of T(a)T(a) is also an element of T(b)T(b). If aa and bb are not βη\beta\eta-equivalent, it is still possible for them to be provably equivalent. For example, the two expressions (1+n)(1+n) and (n+1)(n+1) are not βη\beta\eta-equivalent in Coq for a general variable nn but they are provably equal to each other. In other words, a proof of P(1+n)P(1+n) is NOT necessarily a proof of P(n+1)P(n+1) although we can always derive a proof of P(n+1)P(n+1) from a proof of P(1+n)P(1+n). Moreover, if T(1+n)T(1+n) is a type, its element is not necessarily an element of type T(n+1)T(n+1).

Matrix definition.

When it comes to matrix definitions. The first problem is to decide whether 2n+1×2n+12^{n+1}\times 2^{n+1} matrices and 21+n×21+n2^{1+n}\times 2^{1+n} matrices are βη\beta\eta-equivalent Coq types or not. Intuitively, since these two kinds of matrices are mathematically the same object, they should be used interchangeably. However, (1+n)(1+n) and (n+1)(n+1) are not βη\beta\eta-equivalent. Thus, we have to carefully define the Coq type of matrices so that those two kinds above are βη\beta\eta-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 (k,l)(k,l), if k<mk<m and l<nl<n then the entry of the matrix in the kk-th row and ll-th column is a complex number determined by the mapping. However, if kmk\geq m or lnl\geq n 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.

Am×n=[a00a0(n1)0a(m1)0a(m1)(n1)0000]A_{m\times n}=\begin{bmatrix}a_{00}&\cdots&a_{0(n-1)}&0&\cdots\\ \vdots&&\vdots&\vdots&\\ a_{(m-1)0}&\cdots&a_{(m-1)(n-1)}&0&\cdots\\ 0&\cdots&0&0&\cdots\\ \vdots&&\vdots&\vdots&\end{bmatrix} (7)
Bm×n=[a00a0(n1)1a(m1)0a(m1)(n1)1111]B_{m\times n}=\begin{bmatrix}a_{00}&\cdots&a_{0(n-1)}&1&\cdots\\ \vdots&&\vdots&\vdots&\\ a_{(m-1)0}&\cdots&a_{(m-1)(n-1)}&1&\cdots\\ 1&\cdots&1&1&\cdots\\ \vdots&&\vdots&\vdots&\end{bmatrix} (8)

For example, in (7) and (8) we give two mappings in the form of two infinite-dimensional matrices Am×nA_{m\times n} and Bm×nB_{m\times n}, respectively. Basically, Am×nA_{m\times n} is the same as Bm×nB_{m\times n} except that all the elements with rows (resp. columns) greater than or equal to mm (resp. nn) are 0 in the former but they are 11 in the latter. In computer-aided proofs, we could choose to only reason about well-formed matrices whose “outside elements” are all zero like Am×nA_{m\times n} above. Paykin et al. PRZ17 heavily used this approach in their work. They would consider Am×nA_{m\times n} well-formed but Bm×nB_{m\times n} 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 Am×nA_{m\times n} with Bm×nB_{m\times n}. 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 \equiv to stand for the relaxed notion of matrix equivalence and reserve == for the stronger notion of equivalence in the sense that A=BA=B means the two matrices AA and BB 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, |0|0|0\rangle\otimes|0\rangle is a 4×14\times 1 matrix and it is only verbose to say it is a (22)×(11)(2\cdot 2)\,\times(1\cdot 1) matrix. Even though (22)×(11)(2\cdot 2)\,\times(1\cdot 1) is convertible to 4×14\times 1, these two typing claims are not syntactically identical in Coq but only βη\beta\eta-equivalent to each other. This difference is significant in rewriting. For example, the associativity of matrix multiplication is usually described as follows.

A×(B×C)=(A×B)×CA\times(B\times C)=(A\times B)\times C

But more formally, the associativity means for any natural numbers mm, nn, oo, pp and m×nm\times n matrix AA, n×on\times o matrix BB and o×po\times p matrix CC,

A×m,n,p(B×n,o,pC)=(A×m,n,oB)×m,o,pC,A\underset{m,n,p}{\times}(B\underset{n,o,p}{\times}C)=(A\underset{m,n,o}{\times}B)\underset{m,o,p}{\times}C,

where we use subscripts under ×\times 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

A×1,1,1(B×11,1,11C),A\underset{1,1,1}{\times}(B\underset{1\cdot 1,1,1\cdot 1}{\times}C),

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

A×m,n1,p1(B×n2,o,p2C),A\underset{m,n_{1},p_{1}}{\times}(B\underset{n_{2},o,p_{2}}{\times}C),

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

A×m,n1,p1(B×n1,o,p1C),A\underset{m,n_{1},p_{1}}{\times}(B\underset{n_{1},o,p_{1}}{\times}C),

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 f:{0,1}{0,1}f:\{0,1\}\rightarrow\{0,1\}, Deutsch Deu85 presented a quantum algorithm that can compute f(0)f(1)f(0)\oplus f(1) in a single evaluation of ff. The algorithm can tell whether f(0)f(0) equals f(1)f(1) 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 |x|y|x\rangle\otimes|y\rangle to the state |x|yf(x)|x\rangle\otimes|y\oplus f(x)\rangle, where x,y{0,1}x,y\in\{0,1\}. More specifically, the unitary operator UfU_{f} can be in one of the following four forms:

  • if f(0)=f(1)=0f(0)=f(1)=0, then Uf=Uf00=I2I2U_{f}=U_{f00}=I_{2}\otimes I_{2};

  • if f(1)=f(1)=1f(1)=f(1)=1, then Uf=Uf11=I2XU_{f}=U_{f11}=I_{2}\otimes X;

  • if f(0)=0f(0)=0 and f(1)=1f(1)=1, then Uf=Uf01=CXU_{f}=U_{f01}=CX;

  • if f(0)=1f(0)=1 and f(1)=0f(1)=0, then Uf=Uf10=B0X+B3I2U_{f}=U_{f10}=B_{0}\otimes X+B_{3}\otimes I_{2}.

\Qcircuit@C=0.8em@R=1.8em\lstick|0&\qw\qw\gateH\qw\qw\multigate2𝓍𝓍𝒰𝒻𝓎𝓎𝒻(𝓍)\qw\gateH\qw\qw\lstick|1\qw\qw\gateH\qw\qw\ghost𝓍𝓍𝒰𝒻𝓎𝓎𝒻(𝓍)\qw\qw\qw\qw\rstick|ψ0\rstick|ψ1\lstick|ψ2\lstick|ψ3\Qcircuit@C=0.8em@R=1.8em{\lstick{\ket{0}}&\qw\qw\gate{H}\qw\qw\multigate{2}{\mathcal{\begin{array}[]{cccc}x&&&x\\ &&&\\ &&U_{f}&\\ &&&\\ y&&&y\oplus f(x)\\ \end{array}}}\qw\gate{H}\qw\qw\\ \\ \lstick{\ket{1}}\qw\qw\gate{H}\qw\qw\ghost{\mathcal{\begin{array}[]{cccc}x&&&x\\ &&&\\ &&U_{f}&\\ &&&\\ y&&&y\oplus f(x)\\ \end{array}}}\qw\qw\qw\qw\\ \rstick{\ket{\psi_{0}}}\rstick{\ket{\psi_{1}}}\lstick{\ket{\psi_{2}}}\lstick{\ket{\psi_{3}}}}
Figure 5: Deutsch’s algorithm

We formalize Deutsch’s algorithm in Coq and use our symbolic approach to prove its correctness. Let us suppose that |ψ0=|01|\psi_{0}\rangle=\ket{01} 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 ψ\psi0 := ∣0⟩ ⊗ ∣1⟩.
Definition ψ\psi1 := (H ⊗ H) × ψ\psi0.

Lemma step1 : ψ\psi1 = ∣+⟩ ⊗ ∣-⟩.

Lemma step1 claims that the intermediate state after the first phase is |+||+\rangle\otimes|-\rangle. We can use the strategy operate_\_reduce designed in Section 3 to prove its correctness.

The second phase applies the unitary operator UfU_{f} to |ψ1|\psi_{1}\rangle. Since UfU_{f} has four possible forms, we consider four cases.


Definition ψ\psi20 := (I_2 ⊗ I_2) × ψ\psi1.
Definition ψ\psi21 := (I_2 ⊗ X) × ψ\psi1.
Definition ψ\psi22 := CX × ψ\psi1.
Definition ψ\psi23 := (B0 ⊗ X .+ B3 ⊗ I_2) × ψ\psi1.

Lemma step20 : ψ\psi20 = ∣+⟩ ⊗ ∣-⟩.
Lemma step21 : ψ\psi21 = -1 .* ∣+⟩ ⊗ ∣-⟩.
Lemma step22 : ψ\psi22 = ∣-⟩ ⊗ ∣-⟩.
Lemma step23 : ψ\psi23 = -1 .* ∣-⟩ ⊗ ∣-⟩.

Each of the above four lemmas corresponds to one case. They claim that the intermediate state |ψ2|\psi_{2}\rangle is ±1|+|\pm 1\cdot|+\rangle\otimes|-\rangle after the second phase when f(0)=f(1)f(0)=f(1), and ±1||\pm 1\cdot|-\rangle\otimes|-\rangle when f(0)f(1)f(0)\neq f(1). We prove the four lemmas by rewriting |ψ1|\psi_{1}\rangle with Lemma step1 and using the strategy operate_\_reduce again.

The last phase applies the Hadamard gate to the first qubit of |ψ2|\psi_{2}\rangle. So we still have four cases.


Definition ψ\psi30 := (H ⊗ I_2) × ψ\psi20.
...

Lemma step30 : ψ\psi30 = ∣0⟩ ⊗ ∣-⟩.
Lemma step31 : ψ\psi31 = -1 .* ∣0⟩ ⊗ ∣-⟩.
Lemma step32 : ψ\psi32 = ∣1⟩ ⊗ ∣-⟩.
Lemma step33 : ψ\psi33 = -1 .* ∣1⟩ ⊗ ∣-⟩.

Observe that the only difference between |ψ30|\psi_{30}\rangle and |ψ31|\psi_{31}\rangle lies in the global phase 1-1, which can be ignored. Similarly for |ψ32|\psi_{32}\rangle and |ψ33|\psi_{33}\rangle. Formally, we can prove the following lemmas.
    
Lemma step31’ : ψ\psi31 ≈ ∣0⟩ ⊗ ∣-⟩.
Lemma step33’ : ψ\psi33 ≈ ∣1⟩ ⊗ ∣-⟩.

Therefore, after the last phase, we have |ψ3=|0||\psi_{3}\rangle=|0\rangle\otimes|-\rangle when f(0)=f(1)f(0)=f(1), and |ψ3=|1||\psi_{3}\rangle=|1\rangle\otimes|-\rangle when f(0)f(1)f(0)\neq f(1). This is proved by using the intermediate results obtained in the first two phases and the strategy operate_\_reduce.

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

|ψ3ij=(HI2)×Ufij×(HH)×|ψ0.|\psi_{3ij}\rangle=(H\otimes I_{2})\times U_{fij}\times(H\otimes H)\times|\psi_{0}\rangle.

Formally, we need to prove four equations depending on the forms of UfU_{f}.


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 ObsEquiv_\_state and operate_\_reduce. Thus, we know that the first qubit of the result state is |0|0\rangle when f(0)=f(1)f(0)=f(1), and |1|1\rangle otherwise. That is, |ψ3ij=|f(0)f(1)||\psi_{3ij}\rangle=|f(0)\oplus f(1)\rangle|-\rangle 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.

\Qcircuit@C=0.5em@R=1.2em\lstick|φ&\qw\qw\qw\ctrl1\qw\qw\qw\gateH\qw\qw\qw\measureDM1\cw\cw\cw\cw\control\cw\cwx[2]\lstick\qw\qw\qw\targ\qw\qw\qw\qw\qw\qw\qw\measureDM2\cw\cw\control\cw\cwx[1]\lstick\qw\qw\qw\qw\qw\qw\qw\qw\qw\qw\qw\qw\qw\qw\gateXM2\qw\gateZM1\qw\qw\rstick|φ\inputgroupv230.5em1.5em|β00\rstick|φ0\rstick|φ1\lstick|φ2\lstick|φ3\lstick|φ4\Qcircuit@C=0.5em@R=1.2em{\lstick{\ket{\varphi}}&\qw\qw\qw\ctrl{1}\qw\qw\qw\gate{H}\qw\qw\qw\measureD{M_{1}}\cw\cw\cw\cw\control\cw\cwx[2]\\ \lstick{}\qw\qw\qw\targ\qw\qw\qw\qw\qw\qw\qw\measureD{M_{2}}\cw\cw\control\cw\cwx[1]\\ \lstick{}\qw\qw\qw\qw\qw\qw\qw\qw\qw\qw\qw\qw\qw\qw\gate{X^{M_{2}}}\qw\gate{Z^{M_{1}}}\qw\qw\rstick{\ket{\varphi}}\inputgroupv{2}{3}{0.5em}{1.5em}{\ket{\beta_{00}}}\\ \rstick{\ket{\varphi_{0}}}\rstick{\ket{\varphi_{1}}}\lstick{\ket{\varphi_{2}}}\lstick{\ket{\varphi_{3}}}\lstick{\ket{\varphi_{4}}}}
Figure 6: Teleportation NC11

Let the sender and the receiver be AliceAlice and BobBob, respectively. The quantum teleportation protocol goes as follows, as illustrated by the quantum circuit in Figure 6.

  1. 1.

    AliceAlice and BobBob prepare an EPR state |β00q2,q3|\beta_{00}\rangle_{q_{2},q_{3}} together. Then they share the qubits, AliceAlice holding q2q_{2} and BobBob holding q3q_{3}.

  2. 2.

    To transmit the state |φ|\varphi\rangle of the quantum qubit q1q_{1}, AliceAlice applies a CXCX operation on q1q_{1} and q2q_{2} followed by an HH operation on q1q_{1}.

  3. 3.

    AliceAlice measures q1q_{1} and q2q_{2} and sends the outcome xx to BobBob.

  4. 4.

    When BobBob receives xx, he applies appropriate Pauli gates on his qubit q3q_{3} to recover the original state |φ|\varphi\rangle of q1q_{1}.

We formalize the quantum teleportation protocol in Coq and use our symbolic approach to prove its correctness. Let |φ=α|0+β|1|\varphi\rangle=\alpha\ket{0}+\beta\ket{1} be any vector used as a part of the input state. The other part is |β00|\beta_{00}\rangle, which needs extra preparation. For simplicity, we directly represent |β00|\beta_{00}\rangle with a combination of |0|0\rangle and |1|1\rangle. So we define the input state |φ0|\varphi_{0}\rangle as follows.

    
Variables (α\alpha β\beta : C).
Hypothesis Normalise: |α\alpha|^2 + |β\beta|^2 = 1.
Definition φ\varphi : Vector 2 := α\alpha .* ∣0⟩ .+ β\beta .* ∣1⟩.

Definition φ\varphi0 := φ\varphi ⊗ bl00.

The input state goes through the quantum circuit that comprises four phases. We can easily define the quantum pure state |φ2|\varphi_{2}\rangle after the second phase as follows, and prove Lemma tele1 by operate_reduce.

    
Definition φ\varphi2 := (H ⊗ I_2 ⊗ I_2) × (CX ⊗ I_2) × φ\varphi0.

Lemma tele1 : φ\varphi2 = α\alpha .* (∣+⟩ ⊗ bl00) .+ β\beta .* (∣-⟩ ⊗ bl01).

In the third phase, due to the measurement with measurement operators {N0,N1}\{N_{0},N_{1}\}, where N0=B0N_{0}=B_{0} and N1=B3N_{1}=B_{3}, there are four possible cases for the state |φ3|\varphi_{3}\rangle, and the probability for each case can be calculated as

tr((NiNjI2)×(NiNjI2)×|φ2φ2|)=1/4.\begin{array}[]{rcl}{\rm tr}((N_{i}\otimes N_{j}\otimes I_{2})^{\dagger}\times(N_{i}\otimes N_{j}\otimes I_{2})\times|\varphi_{2}\rangle\langle\varphi_{2}|)~{}=~{}1/4.\end{array}

So we define pijp_{ij} and ρ3ij\rho_{3ij} as follows, where i,j{0,1}i,j\in\{0,1\} 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 N×NN^{\dagger}\times N with NN).


Definition p00 := trace ((B0 ⊗ B0 ⊗ I_2) × (density φ\varphi2)).
Definition ρ\rho300 := 1/p00 .* (super (B0 ⊗ B0 ⊗ I_2)(density φ\varphi2)).
...
Lemma tele2 :
  MeaMix 2 1 (MeaDen 2 0 (density φ\varphi2)) .=
  [(1/4, 4 .* density (/√2 .* (∣00⟩ ⊗ (α\alpha .* ∣0⟩ .+ β\beta .* ∣1⟩))));
  [(1/4, 4 .* density (/√2 .* (∣01⟩ ⊗ (α\alpha .* ∣1⟩ .+ β\beta .* ∣0⟩))));
  [(1/4, 4 .* density (/√2 .* (∣10⟩ ⊗ (α\alpha .* ∣0⟩ .+ -β\beta .* ∣1⟩))));
  [(1/4, 4 .* density (/√2 .* (∣11⟩ ⊗ (α\alpha .* ∣1⟩ .+ -β\beta .* ∣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 |φ4ij|\varphi_{4ij}\rangle. We can formalize it in Coq as follows.

|φ4ij:=(I2I2Zi)×(I2I2Xj)×|φ3ij\begin{array}[]{rcl}|\varphi_{4ij}\rangle:=(I_{2}\otimes I_{2}\otimes Z^{i})\times(I_{2}\otimes I_{2}\otimes X^{j})\times|\varphi_{3ij}\rangle\end{array}
    
Definition ρ\rho400 := ρ\rho30.
Definition ρ\rho401 := super (I_2 ⊗ I_2 ⊗ X) ρ\rho310.
Definition ρ\rho410 := super (I_2 ⊗ I_2 ⊗ Z) ρ\rho310.
Definition ρ\rho411 := super ((I_2 ⊗ I_2 ⊗ Z)×(I_2 ⊗ I_2 ⊗ X)) ρ\rho311.
        
Lemma tele3 :
  [(1/4, ρ\rho400);[(1/4, ρ\rho401); [(1/4, ρ\rho410);[(1/4, ρ\rho411)] .=
  [(1/4, density (∣0,0⟩ ⊗ ψ\psi); (1/4, density (∣0,1⟩ ⊗ ψ\psi);
  [(1/4, density (∣1,0⟩ ⊗ ψ\psi); (1/4, density (∣1,1⟩ ⊗ ψ\psi)].
    

Using the simplification strategies discussed in Section 3, it is easy to prove that |φ4ij|\varphi_{4ij}\rangle can be simplified to be |i|j|φ|i\rangle\otimes|j\rangle\otimes|\varphi\rangle, and the probability of each case is 14\frac{1}{4}.

In summary, we can use the following equality to express the whole protocol.

|φ4ij=(I2I2Zi)×(I2I2Xj)×(NiNjI2)×(HI2I2)×(CXI2)×(|φ|β00).\begin{array}[]{cl}|\varphi_{4ij}\rangle=(I_{2}\otimes I_{2}\otimes Z^{i})\times(I_{2}\otimes I_{2}\otimes X^{j})\times(N_{i}\otimes N_{j}\otimes I_{2})\\ \times(H\otimes I_{2}\otimes I_{2})\times(CX\otimes I_{2})\times(|\varphi\rangle\otimes|\beta_{00}\rangle).\end{array}

It shows that the third qubit of the result state is always equal to |φ|\varphi\rangle, 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 f:{0,1}n{0,1}nf:\{0,1\}^{n}\rightarrow\{0,1\}^{n}, suppose there exists a string s{0,1}ns\in\{0,1\}^{n} such that the following property is satisfied

f(x)=f(y)x=y or xy=s,\begin{array}[]{cl}f(x)=f(y)~{}~{}\Leftrightarrow~{}~{}x=y\ \mbox{ or }\ x\oplus y=s,\end{array} (9)

for all x,y{0,1}nx,y\in\{0,1\}^{n}. Here \oplus is the bit-wise modulo 2 addition of two nn bit-strings. The goal of Simon’s algorithm is to find the string ss. The algorithm consists of iterating the quantum circuit and then performing some classical post-processing.

  1. 1.

    Set an initial state |0n|0n|0\rangle^{\otimes n}\otimes|0\rangle^{\otimes n}, and apply Hadamard gates to the first nn qubits respectively.

  2. 2.

    Apply an oracle UfU_{f} to all the 2n2n qubits, where Uf:|x|y|x|f(x)yU_{f}:|x\rangle|y\rangle\mapsto|x\rangle|f(x)\oplus y\rangle.

  3. 3.

    Apply Hadamard gates to the first nn qubits again and then measure them.

When s0ns\neq 0^{n}, the probability of obtaining each string y{0,1}ny\in\{0,1\}^{n} is

py={2(n1)ifsy=00ifsy=1.p_{y}=\left\{\begin{array}[]{ll}2^{-(n-1)}&{\rm if}\quad s\cdot y=0\\ 0&{\rm if}\quad s\cdot y=1.\end{array}\right.

Therefore, it can be seen that the result string yy must satisfy sy=0s\cdot y=0 and be evenly distributed. Repeating this process n1n-1 times, we will get n1n-1 strings y1,,yn1y_{1},\cdots,y_{n-1} so that yis=0y_{i}\cdot s=0 for 1in11\leq i\leq n-1. Thus we have n1n-1 linear equations with nn unknowns (nn is the number of bits in s). The goal is to solve this system of equations to get ss. We can get a unique non-zero solution ss if we are lucky and y1,,yn1y_{1},...,y_{n-1} are linearly independent. Otherwise, we repeat the entire process and will find a linearly independent set with a high probability.

\Qcircuit@C=1em@R=1em&\lstick|0\qw\gateH\qw\ctrl2\qw\qw\gateH\qw\qw\lstick|0\qw\gateH\qw\qw\ctrl1\qw\gateH\qw\qw\lstick|0\qw\qw\qw\targ\targ\qw\qw\qw\qw\lstick|0\qw\qw\qw\gateX\qw\qw\qw\qw\qw\gategroup1467.7em.\Qcircuit@C=1em@R=1em{&\\ \lstick{\ket{0}}\qw\gate{H}\qw\ctrl{2}\qw\qw\gate{H}\qw\qw\\ \lstick{\ket{0}}\qw\gate{H}\qw\qw\ctrl{1}\qw\gate{H}\qw\qw\\ \lstick{\ket{0}}\qw\qw\qw\targ\targ\qw\qw\qw\qw\\ \lstick{\ket{0}}\qw\qw\qw\gate{X}\qw\qw\qw\qw\qw\\ \quad\gategroup{1}{4}{6}{7}{.7em}{.}\\ }
Figure 7: Simon’s algorithm with nn = 2 and ss = 11 

As an example, we consider the Simon’s algorithm with n=2n=2. The quantum circuit is displayed in Figure 7. We design the oracle as the gates in the dotted box Uf=(I2CXI2)×(CIXX)U_{f}=(I_{2}\otimes CX\otimes I_{2})\times(CIX\otimes X), where the gate CIXCIX is defined in page 3. For this oracle, s=11s=11 satisfies property (9). The change of states can be seen as follows.

|0000HHI2I2|++|00Uf12[(|00+|11)|01+(|01+|10)|11]HHI2I212[(|00+|11)|01+(|00|11)|11]\begin{array}[]{rl}|0000\rangle&\xrightarrow{H\otimes H\otimes I_{2}\otimes I_{2}}|++\rangle|00\rangle\\ &\xrightarrow{U_{f}}\frac{1}{2}[(|00\rangle+|11\rangle)|01\rangle+(|01\rangle+|10\rangle)|11\rangle]\\ &\xrightarrow{H\otimes H\otimes I_{2}\otimes I_{2}}\frac{1}{2}[(|00\rangle+|11\rangle)|01\rangle+(|00\rangle-|11\rangle)|11\rangle]\end{array}

We can establish the following lemma with our symbolic approach and use the strategy super_\_reduce 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 |01|01\rangle or |11|11\rangle. The corresponding first two qubits are in |00|00\rangle or |11|11\rangle, each occurs with equal probability. By property (9), it means that xy=00x\oplus y=00 or 1111, so we obtain that ss = 11.

6.4 Grover’s algorithm

In this section we consider Grover’s search algorithm . The algorithm starts from the initial state |0n|0\rangle^{\otimes n}. It first uses HnH^{\otimes n} (the HH gate applied to each of the nn qubits) to obtain a uniform superposition state, and then applies the Grover iteration repeatedly. An implementation of the Grover iteration has four steps:

  1. 1.

    Apply the oracle OO;

  2. 2.

    Apply the Hadamard transform HnH^{\otimes n};

  3. 3.

    Perform a conditional phase shift on |x|x\rangle, if |x|0|x\rangle\neq|0\rangle;

  4. 4.

    Apply the Hadamard transform HnH^{\otimes n} again.

Here the conditional phase-shift unitary operator in the third step is 2|00|I2|0\rangle\langle 0|-I. We can merge the last three steps as follows,

Hn×(2|00|I)×Hn=2|ϕϕ|I\begin{array}[]{cl}H^{\otimes n}\times(2|0\rangle\langle 0|-I)\times H^{\otimes n}=2|\phi\rangle\langle\phi|-I\end{array}

where |ϕ=1Nx=0N1|x|\phi\rangle=\frac{1}{\sqrt{N}}\sum\limits_{x=0}^{N-1}|x\rangle with N=2nN=2^{n}. Therefore, the Grover iteration becomes G=(2|ϕϕ|I)×OG=(2|\phi\rangle\langle\phi|-I)\times O.

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 x=0,1,2,3x^{*}=0,1,2,3. The oracle must satisfy that if x=xx=x^{*}, then f(x)=1f(x^{*})=1, otherwise f(x)=0f(x)=0. So in accordance with x=0,1,2,3x^{*}=0,1,2,3, we design four oracles ORA0,,ORA3ORA_{0},...,ORA_{3}, which are implemented by the four circuits in Figure 8.

\Qcircuit@C=.8em@R=1.7em&\qw\ctrlo2\qw\qw\qw\ctrlo1\qw\qw\qw\targ\qw\qw\Qcircuit@C=.8em@R=1.7em{&\qw\ctrlo{2}\qw\qw\\ \qw\ctrlo{1}\qw\qw\\ \qw\targ\qw\qw\\ \\ }
(a) ORA0
\Qcircuit@C=.8em@R=1.7em&\qw\ctrlo2\qw\qw\qw\ctrl1\qw\qw\qw\targ\qw\qw\Qcircuit@C=.8em@R=1.7em{&\qw\ctrlo{2}\qw\qw\\ \qw\ctrl{1}\qw\qw\\ \qw\targ\qw\qw\\ \\ }
(b) ORA1
\Qcircuit@C=.8em@R=1.7em&\qw\ctrl2\qw\qw\qw\ctrlo1\qw\qw\qw\targ\qw\qw\Qcircuit@C=.8em@R=1.7em{&\qw\ctrl{2}\qw\qw\\ \qw\ctrlo{1}\qw\qw\\ \qw\targ\qw\qw\\ \\ }
(c) ORA2
\Qcircuit@C=.8em@R=1.7em&\qw\ctrl2\qw\qw\qw\ctrl1\qw\qw\qw\targ\qw\qw\Qcircuit@C=.8em@R=1.7em{&\qw\ctrl{2}\qw\qw\\ \qw\ctrl{1}\qw\qw\\ \qw\targ\qw\qw\\ \\ }
(d) ORA3
Figure 8: The quantum circuit of different Oracle

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.

\Qcircuit@C=.8em@R=1.7em&\qw\gateH\qw\multigate2ORA\qw\gateH\qw\gateX\qw\qw\qw\ctrl1\qw\qw\qw\gateX\qw\gateH\qw\qw\gateH\qw\ghost𝒪𝒜\qw\gateH\qw\gateX\qw\gateH\qw\targ\qw\gateH\qw\gateX\qw\gateH\qw\qw\gateH\qw\ghost𝒪𝒜\qw\qw\qw\qw\qw\qw\qw\qw\qw\qw\qw\qw\qw\gateH\qw\gategroup19218.7em.\Qcircuit@C=.8em@R=1.7em{&\qw\gate{H}\qw\multigate{2}{ORA}\qw\gate{H}\qw\gate{X}\qw\qw\qw\ctrl{1}\qw\qw\qw\gate{X}\qw\gate{H}\qw\\ \qw\gate{H}\qw\ghost{\mathcal{ORA}}\qw\gate{H}\qw\gate{X}\qw\gate{H}\qw\targ\qw\gate{H}\qw\gate{X}\qw\gate{H}\qw\\ \qw\gate{H}\qw\ghost{\mathcal{ORA}}\qw\qw\qw\qw\qw\qw\qw\qw\qw\qw\qw\qw\qw\gate{H}\qw\gategroup{1}{9}{2}{18}{.7em}{.}}
Figure 9: the Grover’s algorithm with two qubits

The whole algorithm is illustrated by the circuit in Figure 9. The gates in the dotted box perform the conditional phase shift operation 2|00|I2|0\rangle\langle 0|-I. We then merge the front and back HHH\otimes H gates to it and get the operation CPSCPS 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 G=(2|ϕϕ|I)×O=CPS×ORAiG=(2|\phi\rangle\langle\phi|-I)\times O=CPS\times ORA_{i}. Let the initial state be |0|0|1|0\rangle\otimes|0\rangle\otimes|1\rangle. After the Hadamard transform H3H^{\otimes 3}, 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 operate_\_reduce 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 operate_\_reduce. When the states are in the density matrix form, we make use of the strategy super_\_reduce, which decomposes density matrices into multiplications of vectors and then essentially resort to operate_\_reduce, 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 ObsEquiv_\_state.

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 nn qubits. Given a boolean function f:{0,1}n{0,1}f:\{0,1\}^{n}\rightarrow\{0,1\} as a quantum oracle, the algorithm judges whether ff is a constant or a balanced function. Here f(x)f(x) is balanced if it is equal to 1 for half of the input xx, and 0 for the other half. As shown in Figure 11, the overall steps of the Deutsch-Jozas algorithm are as follows:

\Qcircuit@C=0.8em@R=1.8em\lstick|0&\qw/n\qw\qw\gateH\qw\qw\multigate2𝓍𝓍𝒰𝒻𝓎𝓎𝒻(𝓍)\qw\gateH\qw\qw\lstick|1\qw\qw\qw\gateH\qw\qw\ghost𝓍𝓍𝒰𝒻𝓎𝓎𝒻(𝓍)\qw\qw\qw\qw\rstick|ψ0\rstick|ψ1\lstick|ψ2\lstick|ψ3\Qcircuit@C=0.8em@R=1.8em{\lstick{\ket{0}}&\qw{/^{n}}\qw\qw\gate{H^{\otimes}}\qw\qw\multigate{2}{\mathcal{\begin{array}[]{cccc}x&&&x\\ &&&\\ &&U_{f}&\\ &&&\\ y&&&y\oplus f(x)\\ \end{array}}}\qw\gate{H^{\otimes}}\qw\qw\\ \\ \lstick{\ket{1}}\qw\qw\qw\gate{H}\qw\qw\ghost{\mathcal{\begin{array}[]{cccc}x&&&x\\ &&&\\ &&U_{f}&\\ &&&\\ y&&&y\oplus f(x)\\ \end{array}}}\qw\qw\qw\qw\\ \rstick{\ket{\psi_{0}}}\rstick{\ket{\psi_{1}}}\lstick{\ket{\psi_{2}}}\lstick{\ket{\psi_{3}}}}
Figure 10: Deutsch-Jozsa algorithm
\Qcircuit@C=1.0em@R=.8em&\qw\ctrl1\qw\qw\qw\qw\qw\qw\qw\qw\ctrl1\qw\qw\targ\ctrl1\qw\qw\qw\qw\qw\qw\ctrl1\targ\qw\qw\qw\targ\qw\qw\qw\qw\qw\qw\targ\qw\qw\qw\qw\qw\qw\ctrl1\qw\ctrl1\qw\qw\qw\qw\qw\qw\qw\qw\qw\targ\ctrl1\targ\qw\qw\qw\qw\qw\qw\qw\qw\qw\qw\targ\qw\qw\qw\qw\qw\qw\Qcircuit@C=1.0em@R=.8em{&\qw\ctrl{1}\qw\qw\qw\qw\qw\qw\qw\qw\ctrl{1}\qw\\ \qw\targ\ctrl{1}\qw\qw\qw\qw\qw\qw\ctrl{1}\targ\qw\\ \qw\qw\targ\qw\qw\qw\qw\qw\qw\targ\qw\qw\\ \cdots\cdots\\ \qw\qw\qw\qw\ctrl{1}\qw\ctrl{1}\qw\qw\qw\qw\qw\\ \qw\qw\qw\qw\targ\ctrl{1}\targ\qw\qw\qw\qw\qw\\ \qw\qw\qw\qw\qw\targ\qw\qw\qw\qw\qw\qw\\ }
Figure 11: The circuit of UfU_{f}
  1. 1.

    Apply Hadamard gates to all the n+1n+1 qubits respectively;

  2. 2.

    Apply the oracle UfU_{f} to the n+1n+1 qubits, where Uf:|x|y|x|yf(x)U_{f}:|x\rangle|y\rangle\mapsto|x\rangle|y\oplus f(x)\rangle;

  3. 3.

    Apply Hadamard gates to the first nn qubits again and then measure them.

Suppose the input state is |ψ0=|0n|1|\psi_{0}\rangle=|0\rangle^{\otimes n}|1\rangle. After each of the above three steps, we get three states |ψ1|\psi_{1}\rangle, |ψ2|\psi_{2}\rangle and |ψ3|\psi_{3}\rangle respectively.

|ψ1:=x=02n1|x2n||ψ2:=x=02n1(1)f(x)|x2n||ψ3:=x=02n1z=02n1(1)f(x)+xz|z2n|\begin{array}[]{rcl}|\psi_{1}\rangle&:=&\sum\limits_{x=0}^{2^{n}-1}\frac{|x\rangle}{\sqrt{2^{n}}}|-\rangle\\ |\psi_{2}\rangle&:=&\sum\limits_{x=0}^{2^{n}-1}\frac{(-1)^{f(x)}|x\rangle}{\sqrt{2^{n}}}|-\rangle\\ |\psi_{3}\rangle&:=&\sum\limits_{x=0}^{2^{n}-1}\sum\limits_{z=0}^{2^{n}-1}\frac{(-1)^{f(x)+x\cdot z}|z\rangle}{2^{n}}|-\rangle\end{array}

From Hn|0n=x|x/2nH^{\otimes n}|0\rangle^{\otimes n}=\sum_{x}|x\rangle/\sqrt{2^{n}} , we easily get |ψ1|\psi_{1}\rangle. Then due to the fact that Uf|=(1)f(x)|U_{f}|-\rangle=(-1)^{f(x)}|-\rangle, we have |ψ2|\psi_{2}\rangle. Finally, for a single qubit we know that H|x=z(1)xz|z/2H|x\rangle=\sum_{z}{(-1)^{xz}|z\rangle}/\sqrt{2} , we obtain |ψ3|\psi_{3}\rangle, where xzx\cdot z is the bitwise inner product of xx and zz. After measuring the first nn qubits, we analyse the probability of the event that the final result is |0n|0\rangle^{\otimes n}. When |z|z\rangle is |0|0\rangle, the coefficient of |ψ3|\psi_{3}\rangle is x(1)f(x)+xz/2n|z=0\sum_{x}(-1)^{f(x)+x\cdot z}/{2^{n}}|_{z=0} , which is equal to x(1)f(x)/2n\sum_{x}(-1)^{f(x)}/{2^{n}} . Thus, when ff is a constant function, the measurement result must be |0n|0\rangle^{\otimes n}. Otherwise, when ff is a balanced one, the result would not be |0n|0\rangle^{\otimes n}. In summary, based on the measurement of the first nn qubits, we can determine whether ff is a constant or a balanced function and only query the oracle once.

In particular, consider the circuit of UfU_{f} shown in Figure 11, we specify UfU_{f} 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 kron_nnAkron\_n\ n\ A, which stands for AnA^{\otimes n}. When nn is 1, it degenerates into I1I_{1}, 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 nn qubits with induction and rewriting strategies. Since the number nn 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 n=1n=1,

CX×(I2I2)×CX×(|+|)=|+|.\begin{array}[]{cl}CX\times(I_{2}\otimes I_{2})\times CX\times(|+\rangle\otimes|-\rangle)=|+\rangle\otimes|-\rangle.\end{array}

This case can be easily proved by operate_\_reduce. According to the inductive hypothesis, we assume that the algorithm is correct for the case of n=kn=k. Then we are going to prove the case of n=k+1n=k+1 with it and some laws in Table 1. The proof steps are as follows.

Ufk+1×(|+(k+1)|)=(CXI2k)×(I2Ufk)×(CXI2k)×(|+|+|+(k1)|)=(CXI2k)×(I2Ufk)×((CXI2k)×((|+|+)(|+(k1)|)))=(CXI2k)×(I2Ufk)×((CX×(|+|+))(I2k×(|+(k1)|)))=(CXI2k)×((I2Ufk)×((|+|+)(|+(k1)|)))=(CXI2k)×((I2×|+)(Ufk×(|+k|)))=(CXI2k)×(|+(|+k|))=(CXI2k)×((|+|+)(|+k1|))=(CX×(|+|+))(I2k×(|+k1|))=|+(k+1)|1\begin{array}[]{cl}&U_{f}^{k+1}\times(|+\rangle^{\otimes(k+1)}\otimes|-\rangle)\\ =&(CX\otimes I_{2^{k}})\times(I_{2}\otimes U_{f}^{k})\times(CX\otimes I_{2^{k}})\times(|+\rangle\otimes|+\rangle\otimes|+\rangle^{\otimes(k-1)}\otimes|-\rangle)\\ =&(CX\otimes I_{2^{k}})\times(I_{2}\otimes U_{f}^{k})\times((CX\otimes I_{2^{k}})\times((|+\rangle\otimes|+\rangle)\otimes(|+\rangle^{\otimes(k-1)}\otimes|-\rangle)))\\ =&(CX\otimes I_{2^{k}})\times(I_{2}\otimes U_{f}^{k})\times((CX\times(|+\rangle\otimes|+\rangle))\otimes(I_{2^{k}}\times(|+\rangle^{\otimes(k-1)}\otimes|-\rangle)))\\ =&(CX\otimes I_{2^{k}})\times((I_{2}\otimes U_{f}^{k})\times((|+\rangle\otimes|+\rangle)\otimes(|+\rangle^{\otimes(k-1)}\otimes|-\rangle)))\\ =&(CX\otimes I_{2^{k}})\times((I_{2}\times|+\rangle)\otimes(U_{f}^{k}\times(|+\rangle^{\otimes k}\otimes|-\rangle)))\\ =&(CX\otimes I_{2^{k}})\times(|+\rangle\otimes(|+\rangle^{\otimes k}\otimes|-\rangle))\\ =&(CX\otimes I_{2^{k}})\times((|+\rangle\otimes|+\rangle)\otimes(|+\rangle^{\otimes k-1}\otimes|-\rangle))\\ =&(CX\times(|+\rangle\otimes|+\rangle))\otimes(I_{2^{k}}\times(|+\rangle^{\otimes k-1}\otimes|-\rangle))\\ =&|+\rangle^{\otimes(k+1)}\otimes|1\rangle\\ \end{array}

After applying the Hadamard gates to the first nn qubits, we can get that the first nn qubits of the result is |0n|0\rangle^{\otimes n}, So we know that the ff described as UfU_{f} 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.

\Qcircuit@C=1.0em@R=1.0em&\gateH\ctrl1\ctrl2\qw\qw\qw\qw\qw\qw\qw\qw\targ\ctrl1\ctrl2\qw\qw\qw\qw\qw\qw\qw\qw\targ\ctrl1\qw\qw\qw\qw\qw\qw\qw\qw\qw\targ\qw\qw\qw\qw\qw\qw\qw\qw\qw\qw\qw\qw\qw\ctrl2\qw\qw\qw\qw\qw\qw\qw\qw\qw\ctrl1\ctrl2\qw\qw\qw\qw\qw\qw\qw\qw\targ\ctrl1\qw\qw\qw\qw\qw\qw\qw\qw\qw\targ\qw\Qcircuit@C=1.0em@R=1.0em{&\gate{H}\ctrl{1}\ctrl{2}\qw\qw\qw\qw\qw\qw\qw\\ \qw\targ\ctrl{1}\ctrl{2}\qw\qw\qw\qw\qw\qw\\ \qw\qw\targ\ctrl{1}\qw\qw\qw\qw\qw\qw\\ \qw\qw\qw\targ\qw\qw\qw\qw\qw\qw\\ \cdots\\ \qw\qw\qw\qw\qw\qw\qw\ctrl{2}\qw\qw\\ \qw\qw\qw\qw\qw\qw\qw\ctrl{1}\ctrl{2}\qw\\ \qw\qw\qw\qw\qw\qw\qw\targ\ctrl{1}\qw\\ \qw\qw\qw\qw\qw\qw\qw\qw\targ\qw\\ }
Figure 12: Preparing an entangled state
|000..00[12(|0+|1)]|00..0012(|000..00+|100..00)12(|000..00+|110..00)12(|000..00+|111..00)12(|000..00+|111..10)12(|000..00+|111..11)\begin{array}[]{rcl}|000..00\rangle&\longrightarrow&[\frac{1}{\sqrt{2}}(|0\rangle+|1\rangle)]\otimes|00..00\rangle\\ &\equiv&\frac{1}{\sqrt{2}}(|000..00\rangle+|100..00\rangle)\\ &\longrightarrow&\frac{1}{\sqrt{2}}(|000..00\rangle+|110..00\rangle)\\ &\longrightarrow&\frac{1}{\sqrt{2}}(|000..00\rangle+|111..00\rangle)\\ &&\cdots\\ &\longrightarrow&\frac{1}{\sqrt{2}}(|000..00\rangle+|111..10\rangle)\\ &\longrightarrow&\frac{1}{\sqrt{2}}(|000..00\rangle+|111..11\rangle)\\ \end{array}

We start with the initial state |000..00|000..00\rangle with 1212 qubits. First, we apply the H gate on the first qubit q1q_{1} to change the global state into [12(|0+|1)]|00..00[\frac{1}{\sqrt{2}}(|0\rangle+|1\rangle)]\otimes|00..00\rangle with the first qubit in a superposition. It is equivalent to 12(|000..00+|100..00)\frac{1}{\sqrt{2}}(|000..00\rangle+|100..00\rangle). Then we apply the CX gate on the first and second qubits q1q_{1} and q2q_{2}, which leads to the state 12(|000..00+|110..00)\frac{1}{\sqrt{2}}(|000..00\rangle+|110..00\rangle). Then we apply the Toffoli gate on the first three qubits to yield 12(|000..00+|111..00)\frac{1}{\sqrt{2}}(|000..00\rangle+|111..00\rangle). In a similar way, we apply the Toffoli gate on the qubits qiq_{i}, qi+1q_{i+1} and qi+2q_{i+2} for i2,3,,n2i\in{2,3,...,n-2} in turn. Eventually, we obtain a maximally entangled state 12(|000..00+|111..11)\frac{1}{\sqrt{2}}(|000..00\rangle+|111..11\rangle).

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 66 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
Table 2: Comparison of two approaches with verification time in milliseconds

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 HH 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 HH is unitary, we have HH=IHH=I 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
(HHH)×((HHH)×(|0|0|0)).\begin{array}[]{rl}&(H\otimes H\otimes H)\times((H\otimes H\otimes H)\times(|0\rangle\otimes|0\rangle\otimes|0\rangle)).\end{array}

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 A0=(HHH)×(|0|0|0)A_{0}=(H\otimes H\otimes H)\times(|0\rangle\otimes|0\rangle\otimes|0\rangle) and A1=(HHH)×A0A_{1}=(H\otimes H\otimes H)\times A_{0}. With the computational approach, obvious simplifications such as multiplication and addition with 0 and 11 are carried out for the elements in A0A_{0} and A1A_{1}, and no more complicated simplification is effectively handled. So A0A_{0} is a two-dimensional list with each element in the form 12×12×12\frac{1}{\sqrt{2}}\times\frac{1}{\sqrt{2}}\times\frac{1}{\sqrt{2}} and A1A_{1} is a two-dimensional list whose first element is

(12×12×12×(12×12×12))+(12×12×12×(12×12×12))++(12×12×12×(12×12×12)),\begin{array}[]{l}(\frac{1}{\sqrt{2}}\times\frac{1}{\sqrt{2}}\times\frac{1}{\sqrt{2}}\times(\frac{1}{\sqrt{2}}\times\frac{1}{\sqrt{2}}\times\frac{1}{\sqrt{2}}))\\ +(\frac{1}{\sqrt{2}}\times\frac{1}{\sqrt{2}}\times\frac{1}{\sqrt{2}}\times(\frac{1}{\sqrt{2}}\times\frac{1}{\sqrt{2}}\times\frac{1}{\sqrt{2}}))\\ +\ ...\\ +(\frac{1}{\sqrt{2}}\times\frac{1}{\sqrt{2}}\times\frac{1}{\sqrt{2}}\times(\frac{1}{\sqrt{2}}\times\frac{1}{\sqrt{2}}\times\frac{1}{\sqrt{2}})),\end{array}

which is a summation of eight identical summands with 12\frac{1}{\sqrt{2}} 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

A1=(HHH)×(HHH)×(|0|0|0)=(H(HH))×((H(HH))×(|0(|0|0)))=(H(HH))×((H×|0)((H×|0)(H×|0)))=(H(HH))×(|+(|+|+))=(H×|+)((H×|+)(H×|+))=|0(|0|0).\begin{array}[]{rcl}A_{1}&=&(H\otimes H\otimes H)\times(H\otimes H\otimes H)\times(|0\rangle\otimes|0\rangle\otimes|0\rangle)\\ &=&(H\otimes(H\otimes H))\times((H\otimes(H\otimes H))\times(|0\rangle\otimes(|0\rangle\otimes|0\rangle)))\\ &=&(H\otimes(H\otimes H))\times((H\times|0\rangle)\otimes((H\times|0\rangle)\otimes(H\times|0\rangle)))\\ &=&(H\otimes(H\otimes H))\times(|+\rangle\otimes(|+\rangle\otimes|+\rangle))\\ &=&(H\times|+\rangle)\otimes((H\times|+\rangle)\otimes(H\times|+\rangle))\\ &=&|0\rangle\otimes(|0\rangle\otimes|0\rangle).\end{array}

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 N×NN\times N matrices of O(k)O(k)-length expressions results in a matrix of O(Nk)O(Nk)-length expressions, and those expressions are not effectively simplified. At the end of the computation, a matrix of O(Nm)O(N^{m})-length expressions is obtained if m+1m+1 matrices of size N×NN\times N 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 (\cdot, ×\times, ++, \otimes, {\dagger}), 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 4×44\times 4 matrix with 44 out of the 1616 entries being non-zero (cf. Section 2.2). As we can see in (2), its symblic representation is

B0I2+B3(B1+B2),B_{0}\otimes I_{2}+B_{3}\otimes(B_{1}+B_{2}), (10)

using the terms I2I_{2} and BjB_{j} (j{0,,3}j\in\{0,...,3\}). 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 CXCX. 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 Kammu¨\ddot{u}ller, 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 De´\acute{e}ene`\grave{e}s, Anders Mo¨\ddot{o}rtberg, 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.