figure a
figure b

1 Introduction

Recently, quantum computing has received much attention, driven by several technological breakthroughs [7] and increasing investments. Prototype quantum computers are already available. The opportunities for the general public—particularly students, researchers, and technology enthusiasts—to access quantum computing devices are rapidly increasing, e.g., through cloud services such as Amazon Braket [1] or IBM Quantum [2]. Due to the complexity and probabilistic nature of quantum computing, the chance of errors in quantum programs is much higher than that of traditional programs, and conventional means for correctness assurance, such as testing, are much less applicable in the quantum world. Quantum programmers need better tools to help them write correct programs. Therefore, researchers anticipate that formal verification will play a crucial role in quantum software quality assurance and have, in recent years, invested significant effort in this direction [5, 11, 21, 41,42,43, 45, 46]. Nevertheless, practical tools for automated quantum program/circuit verification are still missing.

This paper introduces AutoQFootnote 1, a fully automated tool for quantum circuit verification based on the approach proposed in [14]. In particular, AutoQ checks the validity of a Hoare-style specification {Pre} C {Post}, where C is a quantum circuit (a sequence of quantum gates) in the OpenQASM format [17] and the precondition Pre and postcondition Post represent sets of (pure) quantum states. The check is done by executing the circuit with all quantum states satisfying Pre (using a symbolic representation) and testing that all resulting quantum states are in the set denoted by Post.

AutoQ combines two main techniques to efficiently and effectively represent and reason about (potentially infinite) sets of quantum states:

  1. 1.

    As in [14], we use tree automata (TAs), finite-state automata accepting languages of trees, to efficiently represent sets of quantum states: Each quantum state over n qubits can be seen as a binary decision tree over n variables such that, e.g., in a 3-qubit circuit with qubits |x1x2x3, if the computational basis state |010 in a quantum state has the probability amplitude 14, then there will be a branch x10x21x3014 in the corresponding tree. The use of TA-based representation of a set of quantum states has several advantages: (a) It is concise: e.g., in order to represent the set of all 2n basis states of an n-qubit quantum circuit, we suffice with a TA with O(n) states and transitions. (b) It allows to efficiently perform quantum gate operations on the whole set of quantum states represented by a TA at once [14].

  2. 2.

    In this work, we further consider symbolic quantum states, represented by assigning symbolic values to computational basis states (and having an additional formula to relate these symbolic values). For instance, we can represent the set of all n-qubit quantum states where the computational basis |00 has a strictly larger probability of measurement than all other basis states by a symbolic quantum state assigning |00vh and |y1ynv for all y1yn00, together with the formula |vh|2>|v|2|vh|2+(2n1)|v|2=1, where vh and v are symbolic variables ranging over complex numbers

By combining these two techniques, i.e., using TAs with symbolic variables in leaves, we can have a representation of all n-qubit quantum states where an arbitrary basis has a strictly larger amplitude than other basis states using O(n) states and transitions.

Using such a symbolic encoding is essential to allow us to describe relational specifications, e.g., it allows us to express properties like “the probability amplitude of the basis state |000 is increased after executing the circuit C” (for this, in the postcondition, we use TAs accepting trees with predicates in leaves, a subclass of symbolic tree automata of [36]). Such a property can then be verified by executing the quantum circuit symbolically in the spirit of symbolic execution [27] (i.e., such that the values of amplitudes are not complex numbers but, instead, symbolic terms) and checking whether all trees in the language of the resulting TA satisfy the desired property (using a modified antichain-based algorithm for testing TA language inclusion [4, 10]). Combining TAs and symbolic variables as the language for quantum predicates allows full automation and can be used to express many crucial properties of quantum circuits, as we will demonstrate later. AutoQ is the first tool implementing this approach.

Related Work. Our work belongs to the line of Hoare-style verification of quantum programs, which has been widely discussed in the past [22, 29, 35, 40, 44]. This family of approaches follows D’Hondt and Panangaden’s suggestion of using various Hermitian operators as quantum predicates, resulting in a very powerful yet complete proof system [20]. However, specifying properties using Hermitian operators is often not intuitive and is inconvenient for automation due to their enormous matrix sizes. Therefore, often these approaches are implemented on top of proof assistants such as Coq  [9] and Isabelle  [37] and require significant manual work in proof search. The Qbricks  [12] approach alleviates the difficulty of the proof search by combining state-of-the-art theorem provers with decision procedures building on top of the Why3 platform [24]. The approach, however, still requires a significant amount of human intervention.

Regarding other quantum program/circuit/protocol verification tools, circuit equivalence checkers [5, 11, 15, 26, 39] are often quite efficient but less flexible in specifying the desired property (only equivalence). They are particularly useful in compiler validation; notable tools include Qcec  [11], and Feynman  [5]. Quantum model checking supports a rich specification language (flavors of temporal logic [23, 30, 38]) and is more suitable for verifying high-level protocols due to the quite limited scalability [6]. One notable tool in this category is QPMC  [23]. Quantum abstract interpretation [32, 43] is particularly efficient in processing large-scale circuits, but it grossly over-approximates the state space (it cannot verify basic properties of, e.g., Grover’s algorithm) and cannot conclude anything when verification fails. In contrast, AutoQ can be conveniently used for quantum program development and debugging since it automatically computes the exact set of reachable statesFootnote 2. The mentioned tools are fully automated but have different goals or address different parts of the software development cycle than AutoQ.

Contributions. AutoQ evolved from a simple prototype used for performance evaluation in [14] into a robust tool. In addition, we added the following major extensions:

  1. 1.

    We combined the TA specification with symbolic variables, allowing users to specify advanced relational properties of quantum circuits.

  2. 2.

    We developed a new entailment-checking algorithm for the symbolic TA specification based on the antichain algorithm for automata language inclusion testing.

  3. 3.

    We introduced a high-level language to simplify writing TA specifications.

These improvements are pushing the capabilities of AutoQ, and also of practical quantum circuit verification itself, much further.

Outline. In Sect. 2, we describe our approach to TA-based specification and verification of quantum circuits. In Sect. 3, we discuss the new entailment-checking algorithm for the symbolic TA representation. We discuss the architecture of AutoQ in Sect. 4 and demonstrate the use of the specification language and AutoQ for automated verification of several case studies in Sect. 5.

Fig. 1.
figure 1

Verification of a circuit C amplifying the amplitude of |00 w.r.t. the specification {P,φ} C {Q} with φ:|vh+3v|>|2vh|. R is the TA obtained by executing P on C.

2 Tree Automata-Based Verification of Quantum Circuits

We will begin with minimal formal definitions of the TA-based specification and demonstrate how to use them to verify quantum circuits in AutoQ with examples. We assume a basic knowledge of quantum computation (see, e.g., the classical textbook [31]).

Let us fix a finite set of quantum variables X={x1,,xn} with a linear ordering (we assume x1<<xn) and a disjoint non-empty leaf alphabet Σ. We will, in particular, work with Σ=ΣtΣp where Σt is the alphabet of terms and Σp is the alphabet of predicates in a suitable first-order theory (discussed later).

We use {0,1}n to denote 0in{0,1}i. A (symbolic binary decision) tree over X and Σ is a function τ:{0,1}n(XΣ) such that for all positions p{0,1}i with i<n, we have τ(p)=xi+1 and for all positions p{0,1}n, we have τ(p)Σ. An example of a tree τ can be found in Fig. 1b, where Σ={vh,v}, τ(ϵ)=x1, τ(0)=τ(1)=x2, τ(00)=vh, and τ(p)=v for p{0,1}2{00}.

(symbolic) tree automaton (TA) is a tuple A=(S,Δ,F) where S is a finite set of states, Δ(S×X×S×S)(S×Σ) is a transition relation, and FS is the set of root (final) states. We denote transitions from Δ as sxi(s0,s1) and sa() respectively. An example of a TA with the set of root states {s} can be found in Fig. 1a.

run of A on τ is a function ρ:{0,1}nS s.t. for all positions p{0,1}i with i<n, it holds that ρ(p)τ(p)(ρ(p.0),ρ(p.1))Δ and for all positions p{0,1}n, it holds that ρ(p)τ(p)()Δ. The run ρ is accepting iff ρ(ϵ)F and the language of A is L(A)={τA has an accepting run on τ}. Observe that the tree in Fig. 1b is in the language of the TA P in Fig. 1a with the run ρ such that ρ(ϵ)=s, ρ(0)=s1, ρ(1)=s0, ρ(00)=s3, and ρ(p)=s2 for p{0,1}2{00}.

Now we are ready to demonstrate how to write specifications of quantum circuits with TAs using a running example. We assume that C is a 2-qubit circuit that amplifies the amplitude of the basis state |00 (under some constraint φ over input states) and reduces the amplitudes of other basis states. We first prepare the precondition of C, which consists of a pair (P,φ), where P is a TA with the root state s, a set of terms Σt as the leaf alphabet, and the set of transitions from Fig. 1a, and φ is a first-order constraint over the variables used in Σt. In Σt, we use two variables over complex numbers, v and vh, to denote the corresponding amplitude (low and high). The constraint φ states that |vh+3v|>|2vh| (required by this circuit C, cf. Sect. 5.4). Recall that the TA P from Fig. 1a accepts the tree from Fig. 1b, which in turn represents the quantum state

s=vh|00+v|01+v|10+v|11.
(1)

AutoQ will execute the gates in C to transform the TA P to another TA R capturing the effect of executing C over all quantum states encoded in P. The algorithm for gate operations is almost the same as the one in [14], except that now the update of leaf symbols works symbolically (similarly to symbolic execution [27]: each leaf symbol is a term over vh and v and quantum gates change the terms by accumulating the operations that would be performed on them, potentially simplifying them). In this example, the TA R will accept only one tree representing the quantum state

s=(vh+3v2)|00+(vhv2)|01+(vhv2)|10+(vhv2)|11,
(2)

Observe that under the precondition φ=|vh+3v|>|2vh|, the probability of |00 is indeed increased (|vh+3v2|2>|vh|2). The tree representation of s can be found in Fig. 1c. The TA Q of the postcondition can be found in Fig. 1a. The leaf alphabet of Q is the set of predicates Σp={||>|vh|,||<|v|} where denotes a free variable. Observe that Q accepts the tree from Fig. 1e.

2.1 High-Level Specification Language

In AutoQ, we provide a simple specification language that can be automatically translated to TAs. The language allows users to focus on the properties they want to express without the need to specify details of the TA structure. Our language is particularly suitable for describing sets of states with one high probability branch and other branches with uniformly low or zero probability, a very common pattern of quantum circuit’s correctness properties. For example, in the language, we can use (|00:vh,|:v), where “|” denotes “other basis states,” to define the tree language of the TA in Fig. 1a, which accepts a single tree representing the quantum state vh|00+v|01+v|10+v|11 from Fig. 1b. Similarly, we can use (|00:||>|vh|,|:||<|v|) to represent the language of the TA in Fig. 1d. The set of all 2-qubit basis states {|ii{0,1}2} is expressed as i{0,1}2:(|i:1,|:0) (we can see it as a predicate that is satisfied by the described quantum states). We also allow the tensor product  operator, which multiplies the amplitude of the product basis states. For example, (|00:1,|:0)(|00:vh,|:v)(|00:1,|:0) represents the (singleton) set of states compactly {vh|000000+j{01,11,10}v|00j00}.

A more challenging example is to represent the set of states

{vh|ii000+j{0,1}3jiv|ij000 | i{0,1}3}.
(3)

Such a set can be described with the help of the and operators as follows:

i{0,1}3:(|i:1,|:0)(|i:vh,|:v)(|000:1,|:0).
(4)

Below is the grammar of specification spec:

spec::=state|i{0,1}n:state|spec,statestate::=(|c1:t,,|ck:t,|:t)|(|i:t,|:t)|statestatetΣ, nN and c1,,ck{0,1}n

spec is ill-formed when a free variable i appears in state, if some basis is repeated in the rule (|c1:t,,|ck:t,|:t), or if the previous rule contains two bases of different lengths. If all basis states of the given length are specified in (|c1:t,,|ck:t,|:t), the |:t part is not required any more. The specification is then converted into a TA using a straightforward algorithm; in the following we often confuse a TA and its specification.

2.2 Complex Number Representation

In a (pure) quantum state, the amplitude of a basis computational state is a complex number, and the corresponding probability is the square of the absolute value of the amplitude. For verification, we need an exact representation of complex numbers that can be used in computers. In AutoQ, we use a subset of complex numbers that can be expressed by the following algebraic encoding (cf. [14, 34, 46]):

(12)k(a+bω+cω2+dω3),
(5)

where a,b,c,dZ, kN, and ω=eiπ4=cos45+isin45=22+i22, the unit vector that makes an angle of 45 with the positive real axis in the complex plane. A complex number is then represented by a quadruple (abcd) of integers and a normalization factor k. Although the considered set of complex numbers is only a small subset of all complex numbers (it is countable, while the set of all complex numbers is uncountable), the subset is sufficient to describe various standard quantum gates. Currently, AutoQ supports the set of quantum gates X, H, Y, Z, S, T, Rx(π2), Ry(π2), CNOT, CZ, Toffoli (cf. the list in [14]), which already includes a set of universal quantum gates. From the Solovay-Kitaev theorem [18], gates performing rotations of π2n, used, e.g., in Shor’s algorithm [33] and quantum Fourier transform (QFT) [16], can be approximated with an error rate ϵ by O(log3.97(1ϵ))-many H, CNOT, and T gates. The algebraic representation is also sufficient to represent all reachable states in OpenQASM circuits with the set of supported gates, where the initial basis state is |00.

AutoQ operates on the introduced representation of complex numbers. More precisely, for a specification {P,φ} C {Q}, the leaf symbols of P are quadruples of integer terms (abcd). We assume that all leaf symbols of P share a common normalization factor k, so we do not store the value of k explicitly since it can be inferred from the fact that the probability sum over all basis states is one. Instead, we remember a constant natural number value kc, the difference of the k value between P and R, and use it to normalize the amplitudes. Recall that R is the TA accepting all states after executing C from some states accepted by P. The initial value of kc is zero, and each application of H, Rx(π2), or Ry(π2) gates will increase it by one (cf. [14]). We normalize all quadruple leaf symbols (abcd) of R by multiplying them with (12)kc once R is computed.

Next, we show how to compose a specification of our running example from Fig. 1 using the algebraic representation. The specification can now be written as

P:(|00:(vha,vhb,vhc,vhd),|:(va,vb,vc,vd)Q:(|00:|(1,2,3,4)|2>|(vha,vhb,vhc,vhd)|2,|:|(1,2,3,4)|2<|(va,vb,vc,vd)|2),
 where |(a,b,c,d)|2=|a+bω+cω2+dω3|2=|a+b(22+22i)+ci+d(22+22i)|2=(a+b22d22)2+(b22c+d22)2

2.3 Precise Semantics of the Specification

As mentioned above, for verifying {P,φ} C {Q}, we start with a TA P representing the set of all quantum states satisfying the precondition and compute a TA R representing the set of states reachable after executing the circuit C. Then, we test whether R entails Q (w.r.t. φ), i.e., whether all reachable states satisfy the postcondition.

Formally, we say that a tree τ1 is entailed by a tree τ2 w.r.t. a first-order formula φ, denoted as τ1φτ2, if for all positions p{0,1}n it holds that either (i) τ1(p)=τ2(p) or (ii) τ1(p)=(t1,,tk)Σt, τ2(p)=ψΣp, and φψ[t1/1][tk/k]. We lift the entailment to TAs: A1φA2 iff for all trees τ1L(A1) there exists a tree τ2L(A2) s.t. τ1φτ2.Footnote 3

3 Entailment Checking

figure c

We will now describe how we perform the entailment check RφQ. Since we operate with trees and tree automata over symbolic values, we cannot establish entailment by running a classical TA language inclusion test based on complementing the automaton Q first. Instead, our algorithm for testing the entailment RφQ is based on an on-the-fly TA inclusion checking algorithm [4, 10], which avoids complementation. The on-the-fly inclusion-checking algorithm can be seen as an optimization of the classical construction, which would establish L(R)L(Q)¯=? by first computing the complement Q of Q (using a bottom-up TA determinization), followed by computing the intersection A of Q and R, and, finally, checking language emptiness of A. In particular, the on-the-fly inclusion checking algorithm can be seen as doing all the operations at once. Furthermore, the algorithms in [4, 10] also make use of the so-called antichains and TA simulation to prune the explored state space.

Our modification of the inclusion algorithm to test TA entailment, given in Algorithm 1, mainly differs from [4, 10] in the way initial sets of state pairs are computed on Line 3. In particular, we match a state sr that can perform a leaf transition over tr in R with the set Uq of all states in Q that can perform a leaf transition either over tr or over a predicate pq such that φpq[tr/] (we use pq[tr/] for a tuple tr to denote the substitution of the tuple’s components into the corresponding free variables of pq).

After that, the algorithms perform a simultaneous bottom-up traversal through R (represented by states sr) and the determinized version of Q (represented by sets of states Uq). For each such pair (sr,Uq), the algorithm first checks whether sr is a root state and Uq does not contain any root state (cf. Line 6; this would mean that R accepts some tree that is not accepted by Q). If this does not hold, then the algorithm tries to find all already processed pairs that can make a transition with (sr,Uq) (cf. Line 8) and continue from all such pairs. Each bottom-up successor (sr,Uq) is then added to Worklist in the case it has not been seen previously (cf. Line 13).

The algorithm uses the function Min (cf. Lines 3, 7, and 13) to minimize the sets Worklist and Processed w.r.t. a subsumption relation, and the downward closure for ProcessedWorklist on Line 12 to prune the explored state space. Due to lack of space, we refer to the works [4, 10] for more details about these optimizations.

4 Architecture

Fig. 2.
figure 2

The architecture of AutoQ. The input verification problem is {P,φ} C {Q}.

We illustrate the architecture of AutoQ in Fig. 2. The tool is written in C++ and uses the following external tools: the TA library Vata  [28] for efficient testing of TA inclusion (when the postcondition uses only the term alphabet Σt) and the SMT solver Z3 for entailment checking of leaf symbols in Algorithm 1. We allow any theory solver supported by Z3. In our experiment, we use QF_NIRA. AutoQ takes as an input a quantum circuit in the OpenQASM format accompanied with the specification written as tree automata (.aut files) or the high-level specification language (.hsl files) introduced in Sect. 2.1.

Preprocessor reads the input files (.aut, .smt, .qasm, and .hsl files), translates specifications in the .hsl files into tree automata, and stores them using AutoQ ’s internal data structures. Circuit Executor then reads the circuit C and the TA P and generates another TA R obtained as the result after executing C from states in P, using the approach of [14] with the symbolic extension discussed in Sect. 2. AutoQ can also output the TA R for further analysis. Finally, Entailment Checker checks whether RφQ and reports “verified” when the entailment holds and “bug found” otherwise.

5 Use Cases

In this section, we describe several use cases of quantum algorithms and their important properties that we were able to verify using AutoQ fully automatically. We focus on the use of symbolic TA in this set of experiments and refer the readers to [14] for other experimental results. A selection of the obtained results is given in Table 1. An artifact that allows reproduction of the results is available as [13].

5.1 Hadamard Square is Identity

Our first use case shows that the single qubit gate C that runs two consecutive H gates has the same effect as an identity matrix. We use the specification {P,φ} C {Q} with

P:(|0:(va,vb,vc,vd),|1:(va,vb,vc,vd)),φ:true,Q:(|0:(a,b,c,d)=(va,vb,vc,vd),|1:(a,b,c,d)=(va,vb,vc,vd)).

In this simple example, the precondition P encodes an infinite number of quantum states, which is not expressible using the technique in [14]. We also included a buggy version by altering one of the H gates, and AutoQ managed to detect the injected bug. The results can be found in rows H2 in Table 1.

5.2 Zero Imaginary Part of Amplitudes

One property, which is shared by multiple algorithms, e.g., Bernstein-Vazirani’s [8] and Grover’s algorithm [25], is that the imaginary part of all amplitudes of the result is zero.

Let us focus on Bernstein-Vazirani’s algorithm [8], which finds a secret bit-string s from an oracle using a single query. The algorithm begins with the quantum state |0n, where n is the length of s, and ends with the quantum state |s. The amplitudes of all basis states are either zero or one, the imaginary part of the amplitudes is, therefore, always zero. For a three-qubit circuit C implementing the algorithm, we can therefore use the specification: {P,φ} C {Q} with

P:(|000:(1,0,0,0),|:(0,0,0,0)),φ:true,Q:(|:ψ),

where ψ(b=dc=0) (it will also be used later). In the definition of P, recall that we use the integer-quadruple representation of complex numbers (cf. Eq. (5)). In the postcondition Q, the free variables a,b,c,d are to be substituted by the corresponding terms in the obtained integer term quadruple (abcd) in the entailment check. Note that (abcd) represents the complex number (a+b22d22)+i(b22c+d22) (obtained from Eq. (5)). Because abcd are all integers, for the imaginary part to be zero, it must hold that c=0 and b=d.

When we run C from P, we obtain a TA R encoding (|010:(1,0,0,0), |:(0,0,0,0)) and the entailment RφQ holds. See the rows BV(n) in Table 1 for the results of verifying the algorithm for circuits with secrets of size n. As in the previous example, we also included a buggy version to demonstrate AutoQ ’s bug-finding capability. We can see that AutoQ could verify the algorithm for secrets of a quite large size.

Table 1. Results of verifying our use cases with AutoQ. The maximum peak memory consumption was 52 MiB for GroverAll(9). In most cases, the time of entailment was negligible, with the exception of GroverAll circuits. For instance, GroverAll(8) takes 2 m18 s for entailment checking (70% of the total time) and GroverAll(9) takes 21 m36 s for entailment checking (85% of the total time).

5.3 Probability of Measuring the Correct Answer

Grover’s algorithm [25] assumes a Boolean function f over n bits with only one satisfying assignment s and an oracle that evaluates f for a given input. The algorithm finds s with a high probability, say >0.9, using only O(2n) oracle queries. The algorithm works iteratively, where each Grover iteration queries the oracle once and amplifies the amplitude of |s. First, let C be a 6-qubit circuit implementing Grover’s search with the satisfying assignment s=010, where the first three qubits of C are the work tape, and the following three are the ancillae. We use the following specification:

P:(|000000:1,|:0)  where 1=(1,0,0,0) and 0=(0,0,0,0),φ:true,Q:(|010:|a|2>0.9ψ,|:|a|2<0.1ψ)(|000:1,|:0).

Note that the postcondition Q also checks that all amplitudes in the result of the algorithm have a zero imaginary part (using ψ). See rows GroverSingle(n) in Table 1 for the results on circuits for n-bit functions f and a single oracle.

Next, we also show the correctness of Grover’s algorithm w.r.t. all possible 3-qubit oracles. Let C be a 9-qubit circuit implementing the algorithm, where the first three qubits are used for oracle generation, and the following six are the work tape and ancillae, similarly to GroverSingle. Our specification is now

P:i{0,1}3:(|i000000:1,|:0),φ:true,Q:i{0,1}3:(|i:1,|:0)(|i:|a|2>0.9ψ,|:|a|2<0.1ψ)(|000:1,|:0).

Note that in the postcondition, we use i to relate the oracle value and the value on the work tape. The results are in rows GroverAll(n) in Table 1.

5.4 Increasing Amplitude of the Correct Answer

Above, we show that we are able to automatically verify moderate-sized circuits for Grover’s algorithm for the values of n up to 9 (for GroverAll) and 20 (for GroverSingle), which is quite large, but have difficulties going beyond that. The size of the circuit is O(2n), which is quite large. Therefore, we also verify the algorithm w.r.t. a weaker property, which is, that in one iteration, the amplitude of the correct answer will increase.

Consider a function f over 2 bits with 01 being the only satisfying assignment and let C be a 4-qubit circuit encoding one Grover iteration, with two qubits as the work tape and two ancilla qubits. From Grover’s correctness proof [25], we can derive that when v>0vh>0(2n1)v>vh, a correct implementation will increase the probability of |01 and reduce others. We specify the verification problem as follows:

P:(|01:(vh,0,0,0),|:(v,0,0,0))(|00:1,|:0),φ:v>0vh>0(221)v>vh,Q:(|01:|a|>|vh|ψ,|:|a|<|v|ψ)(|00:1,|:0).

The results can be found in rows GroverIter(n) in Table 1. We can see that verification of one Grover iteration w.r.t. the weaker (but still quite useful) property scales much better than verification of full Grover’s circuits, scaling to sizes of n100.

6 Conclusion

We presented a specification language for specifying useful properties of quantum circuits and a tool AutoQ that can establish the correctness of the specification using an approach combining the technique from [14] with symbolic execution. Using the tool, we were able to fully automatically verify several important properties of a selection of quantum circuits. To the best of our knowledge, for some of the properties, we are the first ones that could verify them fully automatically.