1 Introduction

Motivations. Quantum computation is a promising and emerging computational paradigm which can efficiently solve problems considered to be intractable on classical computers [20, 41]. However, the unintuitive nature of quantum mechanics poses challenging questions for the design and analysis of corresponding quantum programming. Indeed, the quantum program dynamics are considerably more complicated compared to the behavior of classical or probabilistic programs. Therefore, formal reasoning requires the development of novel methods and tools, a development that has already started and recently gathered momentum in various areas, like design automation [22, 43], programming languages [2, 15, 23, 31, 39], verification [11, 36], etc.

Among these formal methods, those that allow us to obtain quantitative properties on quantum programs are particularly interesting. They can be used to obtain relevant information about the computations of a quantum program, such as the number of qubits used and the number of unitary operators used, thus enabling the corresponding compiled quantum circuit to be optimized (for example, by minimizing the use of gates that are hard to make fault-tolerant, or by reducing the number of qubits) or to avoid undesirable behavior such as non-termination. Another quantitative property of interest may also be the question whether or not a program terminates almost-surely, that is, whether its probability of non-termination is zero or not. Similarly, we could aim to capture the expected values of (classical) program variables upon program termination. The latter can also be employed to reason about the expected runtime or the expected cost of quantum programs, if we suitably instrument the code with counter variables.

To illustrate this, the program of Figure 1 implements a Repeat-Until-Success algorithm that can be used to simulate quantum unitary operators on input qubit \(\texttt{q}_1\) by using repeated measurements. The quantum step-circuit on the right part corresponds to one iteration of the loop. Variable \(\texttt{i}\) in the program just acts as a counter for \(\texttt{T}\)-gates. Hence an analysis on the expected value of variable \(\texttt{i}\) can be used to infer an upper-bound on the expected T-count, i.e., the expected number of times a \(\texttt{T}\)-gate is used in the fully compiled quantum circuit. Such an approach offers the advantage to allow the programmer to implement quantum programs using fewer \(\texttt{T}\)-gates, which are costly to implement fault-tolerantly [10, 16], and it therefore provides a simple quantum program to illustrate that the study of quantitative properties is paramount.

Fig. 1.
figure 1

Repeat-until-success program \(\texttt{RUS}\) and step-circuit.

In [6, 30], new methodologies named quantum expectation transformers based on predicate transformers [13, 28] and expectation transformers [17, 32] have been put forward to naturally express and study the quantitative properties of quantum programs. However, no attempt was made to automate the corresponding techniques or delineate how complicated such an automation could be. Automation of these formal verification techniques in the context of quantum programs is a particularly difficult problem. Indeed, the consideration of Hilbert spaces as a mathematical framework for describing principles and laws of quantum mechanics makes it seemingly impossible to reason fully automatically about quantitative properties of quantum program: they involve computational objects of exponential dimensions (in the number of qubits) with scalars ranging over an uncountable domain (i.e., complex numbers \(\mathbb {C}\)). This problem is directly linked to the fact that the set \(\mathbb {C}\) includes non-computable numbers [42] and that testing the inequality \(\le \) or the equality \(=\) of two real numbers is not decidable, even if one restricts their study to computable real numbers. Consequently, the particular nature of quantum programs and of their semantic domain, Hilbert spaces, makes it impossible to directly apply the results obtained in the classical and probabilistic setting [24, 37].

Contributions. In this paper, we study the hardness of the quantitative properties of mixed classical-quantum programs and provide a first step towards their (full) automation using quantum expectation transformers.

To this end, we restrict the considered quantum gates to the Clifford+T fragment, which is known to be the simplest approximately universal fragment of quantum mechanics [1]. Clifford+T makes it possible to only consider quantum states with algebraic amplitudes, thus restricting the study to a countable domain. It implies that our results can accommodate quantum gates employed in actual hardware, recently employed to claim quantum advantage, cf [3]. Moreover, the obtained results are very general as it can be extended to any set of gates with algebraic coefficients.

As motivated, our first contribution is about the general hardness of deciding quantitative properties for mixed classical-quantum programs. For a given input state, we study properties such as (positive) almost-sure termination, \((\textsc {P})\textsc {Ast}\) for short; testing problems, \(\textsc {Test}_{\mathcal {R}}\), which consist in comparing a quantum expectation (for example, the mean value of a variable) with a given value (an algebraic and positive real number) wrt the relation \(\mathcal {R}\); and the finiteness problem, \(\textsc {Test}_{\ne \infty }\), which consists in checking that a quantum expectation is finite. For each of those problems, we also study the related universal problem, which consists in checking the corresponding property for every input. We establish a precise mapping (Theorem 1) of the inherent complexity of each problem in the arithmetical hierarchy [34] that is summarized in Table 1 (provided in Section 3). E.g., \(\textsc {Ast}\) is \(\varPi ^0_2\)-complete while \(\textsc {Past}\) is \(\varSigma ^0_2\)-complete.

Our second contribution aims to overcome the aforementioned undecidability results. For that, we study approximations. More precisely, we focus on inferring bounding functions (in general depending on the input) on the expected values of classical program variables upon termination. The decision problem has thus been altered to an inference problem. Further, we restrict the set of potential bounding functions. As a suitable class of functions, we consider polynomials over the real-closed field of the algebraic numbers. The restriction to algebraic numbers guarantees that comparison operations between real numbers remain decidable. On the other hand, for any real closed field, quantifier elimination for formulas over polynomials is decidable, that is, there exists a double-exponential algorithm computing a quantifier-free formula equivalent to the original formula [21]. This recasting of the problem and restriction of the solution space suffices to render the problem decidable. The inference algorithm established remains double-exponential (Theorem 4), thus of similar complexity as the underlying quantifier elimination procedure.

Finally, our last contribution (Section 5) studies effective automation of the inference of upper bounds on the expected values of program variables. To improve upon the double-exponential complexity, we further restrict the class of polynomials considered, that is, to degree-2 polynomials and sketch how techniques from optimization theory can be employed. Several simple quantum algorithms such as program \(\texttt{RUS}\) can be analyzed using this approach (Example 6). This further reduction in expressivity allows the encoding of the problem in SMT and thus paves the way towards (full) automation.

Related Work. Predicate transformers [13, 28]—on which our work is based—were introduced as a method for reasoning about the semantics of imperative programs. They have been adapted to the probabilistic setting, leading to the notion of expectation transformer [17, 32], which has been used to reason about expected values [8, 26], runtimes [27, 33], and costs [4, 7, 33], and to the quantum paradigm, leading to the notion of quantum pre-expectation transformer [6, 30, 35].

The problem of studying the difficulty of analyzing quantitative program properties has been deeply studied in the classical setting. To mention a few,  [14] and [37] study termination properties and runtime/derivational properties of first-order programs, respectively. Further, in [24] completeness results for various quantitative properties of (pure) probabilistic programs have been established. The inference problem of expectation transformers, i.e., establishing an implementation that automates the search for pre-expectations, has been studied extensively. Examples of successful implementation are presented in [7, 8, 33]. Up to now, however, no practical, feasible studies have been carried out on quantum languages. Among the techniques using quantum expectation transformers, we believe [6] to be the most amenable to automation. Indeed, by lifting upper invariants of [27] to the quantum setting, it enables approximate reasoning and eliminate the need to reason about fixpoints or limits, stemming from the semantics of loops.

2 Quantum Programming Language

In this section, we introduce the syntax and operational semantics of the considered mixed-quantum imperative programming language.

Syntax. We make use of three basic datatypes \(\mathcal {B}\), \(\mathcal {N}\) and \(\mathcal {Q}\) for Boolean, numbers (non-negative integers), and qubit data, respectively. Let \(\mathcal {K}\) be an arbitrary classical type in \(\{\mathcal {B},\mathcal {N}\}\). Each program variable comes with a fixed datatype and can be optionally annotated by its type as a superscript. In what follows, we will use \(\texttt{x},\texttt{x}',\texttt{y},\ldots \) to denote classical variables of type \(\mathcal {K}\) and \(\texttt{q},\texttt{q}',\ldots \) to denote quantum variables of type \(\mathcal {Q}\). A program, denoted \(\texttt{P}\), is simply a statement; see Figure 2. Program statements are either classical assignments, conditionals, sequences, loops, quantum assignments \(\overline{\texttt{q}}^\mathcal {Q}\,*\!\!= \texttt{U}\), or measurements \(\texttt{x}^\mathcal {B}= {\textsf{meas}}{\texttt{q}^\mathcal {Q}}\). A quantum assignment consists in the application of a quantum unitary gate \(\texttt{U}\) of arity \(ar(\texttt{U})\) to a sequence of qubits \(\overline{\texttt{q}}\triangleq \texttt{q}_1,\ldots ,\texttt{q}_{ar(\texttt{U})}\). As we will see in the semantics section, a unitary matrix U will be associated with each quantum gate \(\texttt{U}\). A measurement performs a single qubit measurement of \(\texttt{q}\) in the computational basis: the outcome is a Boolean value and the quantum state evolves accordingly. For a given syntactic construct t, let \(\mathcal {B}(t)\) (respectively \(\mathcal {N}(t)\), \(\mathcal {Q}(t)\)) be the set of Boolean (respectively, number, qubit) variables in t.

Notice that the language encompasses qubit-initializing in the basis states. In particular, we will use \(\texttt{q}^\mathcal {Q}= \mathinner {|{0}\rangle }\) as syntactic sugar for \(\texttt{x}= {\textsf{meas}}{\texttt{q}} ; \textsf{if}\texttt{x}\,{{{\texttt{then}}}}\,\texttt{q}\,*\!\!= \texttt{X} \,{{{\texttt{else}}}}\,{{\texttt{skip}}},\) for \(\texttt{X}\) being the Pauli X gate and for some fresh variable \(\texttt{x}\) of type \(\mathcal {B}\).

Fig. 2.
figure 2

Syntax of quantum programs.

Example 1

Consider the program of Figure 3, adapted from [6], as a simple leading example. Let H be the unitary operator computing the Hadamard gate. This program simulates coin tossing by repeatedly measuring the qubit \(\texttt{q}\), until the measurement outcome \(\texttt{ff}\) occurs. The probability to terminate within n steps depends on the initial state \(\rho =\left( {\begin{matrix}\alpha &{} \beta \\ \gamma &{} \delta \end{matrix}}\right) \!\) (a density matrix in \(\mathbb {C}^{2\times 2}\), which implies \(\alpha +\delta =1\) and \(\gamma = \bar{\beta }\)) of the qubit \(\texttt{q}\). Variable \(\texttt{i}\) is increased by one at each iteration, and hence, when the program terminates, \(\texttt{i}\) stores as final value the number of loop iterations performed. The overall probability of termination is 1. The mean value of variable \(\texttt{i}\), that is, the expected number of loop iterations, depends on the program input, in particular on the initial quantum state. After termination, for an initial state \(\rho = \left( {\begin{matrix}\alpha &{} \beta \\ \bar{\beta }&{} \delta \end{matrix}}\right) \!\), its expected value is given by

$$\begin{aligned} F(\rho ) = p_0\times 1 + \sum _{i=1}^\infty \frac{p_1}{2^i}(i+1)=p_0+p_1+2p_1=1+(\alpha -\beta -\bar{\beta }+\delta )= 2-(\beta +\bar{\beta }) ,\end{aligned}$$

where \(p_0 = \frac{\alpha +\beta +\bar{\beta }+\delta }{2} = \frac{1+\beta + \bar{\beta }}{2}\) and \(p_1 = 1 - p_{0}\) are the probabilities of measuring \(\mathinner {|{0}\rangle }\) and \(\mathinner {|{1}\rangle }\), respectively, on the first iteration of the loop. For instance, for a qubit initialized in state , the corresponding density matrix is and hence the expected number of loop iterations is . It will be simply 2 in the case of an initialization in the computational basis \(\mathinner {|{\phi }\rangle }=\mathinner {|{0}\rangle }\) or \(\mathinner {|{\phi }\rangle }=\mathinner {|{1}\rangle }\).

Fig. 3.
figure 3

Quantum Coin tossing

Operational Semantics. Following [6], we model the dynamics of our language as a probabilistic abstract reduction system (see [9]), a transition system where reduction is defined as a relation over probability distributions.

Probabilistic abstract reduction systems. Given a subset \(\mathbb {K}\) of \(\mathbb {R}\), let \(\mathbb {K}^+\) be the set of non-negative numbers in \(\mathbb {K}\), i.e., \(\mathbb {K}^+ \triangleq \mathbb {K} \cap \{x \mid x \ge 0 \}\) and let \(\mathbb {K}^\infty \) be defined by \(\mathbb {K}^\infty \triangleq \mathbb {K}\cup \{\infty \}\).

A discrete (sub)distribution \(\delta \) over a set A is a function \(\delta : A \rightarrow [0,1] \) with countable support \(\textrm{supp}(\delta ) \triangleq \{ a \in A \mid \delta (a) \ne 0 \}\) that maps an element a of A to a probability \(\delta (a)\) such that \(|\delta | \triangleq \sum _{a \in \textrm{supp}(\delta )} \delta (a) = 1\) (\(|\delta | \le 1\)). Any (sub)distribution \(\delta \) can be written as \(\{\delta (a) : a \}_{a \in \textrm{supp}(\delta )}\). The set of subdistributions over A, denoted by \(\mathcal {D}(A)\), is closed under denumerable convex combinations \(\sum _i p_i \cdot \delta _i \triangleq \lambda a.\sum _i p_i \delta _i(a)\), with \(p_i \in [0,1]\) and \(\sum _{i} p_i \le 1\). Slightly simplifying standard notation, given \(f : A \rightarrow \mathbb {R}^{+\infty }\) and a subdistribution \(\delta \in \mathcal {D}(A)\), we define \(\mathbb {E}_\delta (f)\), the expectation of f on \(\delta \), by \(\mathbb {E}_\delta (f) \triangleq \varSigma _{a \in supp(\delta )}\delta (a)f(a)\). Note that \(\mathbb {E}_\delta (f) \in \mathbb {R}^{+\infty }\) is always defined, since the images of f are non-negative reals.

Bournez and Garnier [9] introduced the notion of Probabilistic Abstract Reduction System (PARS) as a means to study reduction systems that evolve probabilistically. A PARS \({\mathop {\rightarrow }\limits ^{}}\) on A is a binary relation \(\cdot {\mathop {\rightarrow }\limits ^{}} \cdot \subseteq A \times \mathcal {D}(A)\). The intended meaning is that when \(a \rightarrow \delta \), then a reduces to \(b \in \textrm{supp}(\delta )\) with probability \(\delta (b)\). Here, we focus on deterministic PARSs, i.e., PARSs \({\mathop {\rightarrow }\limits ^{}}\) with \(a {\mathop {\rightarrow }\limits ^{}} \delta _1\) and \(a {\mathop {\rightarrow }\limits ^{}} \delta _2\) implies \(\delta _1 = \delta _2\). An object \(a \in A\) is called terminal if there is no rule \(a {\mathop {\rightarrow }\limits ^{}} \delta \), which we write as \(a \not \rightarrow \).

Every deterministic PARS \({\mathop {\rightarrow }\limits ^{}}\) over A naturally lifts to a reduction relation over distributions so that , if the reduct distribution \(\varepsilon \) is obtained from \(\delta \) by replacing reducts in \(\textrm{supp}(\delta )\) according to the PARS \({\mathop {\rightarrow }\limits ^{}}\). In fact, we define this lifting in terms of a ternary relation on distributions, where in a step the weight c signifies the probability that a reduction has occurred. This relation is defined wrt. the following three rules.

figure h

We may sometimes use the n-fold (\(n \ge 0\)) composition of , denoted , given by if and the weights satisfy \(c = \sum _{i=1}^n c_i\). Notice that since \({\mathop {\rightarrow }\limits ^{}}\) is deterministic, so is , in the sense that and implies \(c_{1} = c_{2}\) and \(\epsilon _{1} = \epsilon _{2}\). Thus, in particular, for every \(a \in A\) there is precisely one (infinite) reduction

figure p

For any \(b \in A\), \(\delta _{i}(b)\) gives the probability that a reduces to b in i steps. Note that when b is terminal, this probability only increases along reductions (i.e., \(\delta _{i}(b) \le \delta _{i+1}(b)\) for all i). This justifies that we define the terminal distribution of a as the distribution \(\delta (b) \triangleq \lim _{i \rightarrow \infty } \delta _{i}(b)\). Note that \(\delta (b)\) gives the probability that a reaches b in an arbitrary (but finite) number of steps. Since the weights \(c_{i}\) indicate the probability that a step has been performed from \(\delta _{i}\) to \(\delta _{i+1}\), the infinite sum \(\sum _{i=0}^{\infty } c_{i} \in \mathbb {R}^{+\infty }\) gives the expected number of reduction steps carried out, the expected derivation length of a [5].

For a PARS \(\rightarrow \), we denote by \(\textsf{term}_{\rightarrow } : A \rightarrow \mathcal {D}(A)\) the function associating with each \(a \in A\) its terminal distribution. The expected derivation length function \(\textsf{edl}_{\rightarrow } : A \rightarrow \mathbb {R}^{+\infty }\) associates each \(a \in A\) to its expected derivation length. The PARS \(\rightarrow \) is almost surely terminating [40] (a.s. terminating for short) if \(a \in A\) reduces to a terminal object \(b\not \rightarrow \) with probability 1, that is, if \(|\textsf{term}_{\rightarrow }(a)| = 1\) for every a. It is positive almost surely terminating, if the expected derivation length is always finite, that is, \(\textsf{edl}_{\rightarrow }(a) < \infty \) for all \(a \in A\).

Apart from termination, we are interested also in questions related to functional correctness, such as (i) what is the probability that a reaches a terminal b, (ii) what is the probability that a reaches a terminal satisfying predicate P, and more generally, (iii) which value does a function \(f : A \rightarrow \mathbb {R}^{+\infty }\) take, in expectation, when fully reducing an object a. In the literature [32], one tool to answer all of these are given by weakest pre-expectation transformers, the natural generalization of classical weakest pre-condition transformers to a quantitative, probabilistic setting. We suite this notion to PARSs.

Definition 1

(Weakest pre-expectation). The weakest pre-expectation for a PARS \(\rightarrow \) over A is given by the function

$$\begin{aligned} & \textsf{wp}_{\rightarrow } : {(A \rightarrow \mathbb {R}^{+\infty })} \rightarrow {(A \rightarrow \mathbb {R}^{+\infty })} \\ & \textsf{wp}_{\rightarrow } \triangleq \lambda f. \lambda a.\ \mathbb {E}_{\textsf{term}_{\rightarrow }(a)}(f) .\end{aligned}$$

For \(\textbf{1}_{b}\) the indicator function evaluating to 1 on argument b and to 0 otherwise, and by seeing a predicate P as a 0, 1-valued function, \(\textsf{wp}_{\rightarrow } \ \textbf{1}_{b} \ a\) answers question (i), \(\textsf{wp}_{\rightarrow } \ P \ a\) answers (ii), and generally \(\textsf{wp}_{\rightarrow } \ f \ a\) answers question (iii). Note also that a PARS is a.s. terminating iff \(\textsf{wp}_{\rightarrow } \ (\lambda b.\, 1) \ a = 1\) for each \(a \in A\). On the other hand, positive a.s. termination cannot be expressed through an application of \(\textsf{wp}_{\rightarrow }\).

Fig. 4.
figure 4

Operational semantics in terms of PARS.

Quantum programs as PARSs. We now endow quantum programs with an operational semantics defined in terms of a PARS. Given a totally ordered set of qubits \(Q=\{\texttt{q}_1,\ldots ,\texttt{q}_n\}\), let \(\mathcal {H}_{Q}\) be the \(2^{n}\)-dimensional Hilbert space defined by \(\mathcal {H}_{Q} \triangleq \otimes _{i=1}^n \mathcal {H}_{\texttt{q}_i}\), with \(\mathcal {H}_{\texttt{q}} = \mathbb {C}^{2}\) being the vector space of computational basis \(\{\mathinner {|{0}\rangle },\mathinner {|{1}\rangle }\}\) and \(\otimes \) being the tensor product. With \(\mathinner {\langle {k}|}\) we denote the transpose conjugate of \(\mathinner {|{k}\rangle }\), for \(k \in \{0,1\}\). Let \(\mathcal {M}(\mathcal {H}_{Q})\) be the set of complex square matrices acting on the Hilbert space \(\mathcal {H}_{Q}\), i.e., \(\mathcal {M}(\mathcal {H}_{Q})=\mathbb {C}^{2^n \times 2^n}\). Given \(M \in \mathcal {M}(\mathcal {H}_{Q})\), \(M^\dagger \) denotes the transpose conjugate of M, and \(I_{2^n}\) denotes the identity matrix over \(\mathcal {M}(\mathcal {H}_{Q})\). We will write I when the dimension is clear from the context.

Let \(\mathfrak {D}(\mathcal {H}_{Q})\subsetneq \mathcal {M}(\mathcal {H}_{Q})\) be the set of all density operators (or quantum states), i.e., positive semi-definite matrices of trace equal to 1 on \(\mathcal {H}_{Q}\). Density operators can be viewed as the mathematical representation of a (mixed) quantum state. A unitary operator U is a matrix in \(\mathcal {M}(\mathcal {H}_{Q})\) such that \(UU^\dagger = U^\dagger U = I\). A superoperator \(\varPhi _U : \mathfrak {D}(\mathcal {H}_{Q})\rightarrow \mathfrak {D}(\mathcal {H}_{Q})\), an endomorphism over density operators, is attached to each unitary operator U and defined by \(\varPhi _U \triangleq \lambda \rho .U\rho U^\dagger \). By definition, \(\varPhi _U\) is a completely positive trace preserving linear map. Indeed, \(tr(U\rho U^\dagger )=tr(\rho )\), by unitarity. Hence \(U\rho U^\dagger \) is a density operator in \(\mathfrak {D}(\mathcal {H}_{Q})\) for each \(\rho \in \mathfrak {D}(\mathcal {H}_{Q})\).

Regarding measurements, for each i, \(1 \le i \le card(Q)\), we define \(M_{k,i} \in \mathcal {M}(\mathcal {H}_{Q})\), with \(k \in \{0,1\}\), by \(M_{0,i} \triangleq I_{2^{i-1}} \otimes (\mathinner {|{0}\rangle }\mathinner {\langle {0}|}) \otimes I_{2^{n-i}}\) and \(M_{1,i} \triangleq I - M_{0,i}\). The measurement of the qubit \(q_i\) (in the computational basis) of a density matrix \(\rho \in \mathfrak {D}(\mathcal {H}_{Q})\), produces the classical outcome \(k\in \{0,1\}\) with probability \(tr(M_{k,i}\rho )\). The (normalized) quantum state, after the measurement, is defined by

$$ m_{k,i} (\rho ) \triangleq {\left\{ \begin{array}{ll}\frac{M_{k,i}\rho M_{k,i}^\dagger }{tr(M_{k,i}\rho )}, &{}\text { if }tr(M_{k,i}\rho ) \ne 0,\\ \frac{I}{2^n} &{} \text { otherwise.} \end{array}\right. } $$

Note that for all \(\rho \in \mathfrak {D}(\mathcal {H}_{Q}),\ m_{k,i}(\rho ) \in \mathfrak {D}(\mathcal {H}_{Q})\), as it holds that \(tr(m_{k,i}(\rho ))=1\). Indeed, \(tr(M_{k,i}\rho M_{k,i}^\dagger )= tr(M_{k,i}^2\rho )=tr(M_{k,i}\rho )\), as \(M_{k,i}\) is a projection. Hence \(m_{k,i}\) is a map in \( \mathfrak {D}(\mathcal {H}_{Q})\rightarrow \mathfrak {D}(\mathcal {H}_{Q})\).

We set \(\llbracket \mathcal {B} \rrbracket ^{}\triangleq \{0,1\}\) and \(\llbracket \mathcal {N} \rrbracket ^{} \triangleq \mathbb {N}\). The classical state is modeled as a (well-typed) store s of domain dom(s) mapping each variable \(\texttt{x}\) of type \(\mathcal {K}\) to a value in \(\llbracket \mathcal {K} \rrbracket ^{}\). With \(\texttt{Store}\), we denote the set of all such stores. Let \(s[\texttt{x}^\mathcal {K} := k]\) with \(k \in \llbracket \mathcal {K} \rrbracket ^{}\) be the store obtained from s by updating the value assigned to \(\texttt{x}\) in the map s. Given a store \(s\), let \(\llbracket - \rrbracket ^{s}: \mathcal {K}\texttt{Exp} \rightarrow \llbracket \mathcal {K} \rrbracket ^{}\) be the map associating to each expression \(\texttt{ e}\) of type \(\mathcal {K}\) and such that \(\mathcal {B}(\texttt{ e})\cup \mathcal {N}(\texttt{ e}) \subseteq dom(s)\), a value in \(\llbracket \mathcal {K} \rrbracket ^{}\), defined in the obvious way. For example \(\llbracket \texttt{x} \rrbracket ^{s} \triangleq s(\texttt{x})\), \(\llbracket n \rrbracket ^{s} \triangleq n\), \(\llbracket \texttt{tt} \rrbracket ^{s} \triangleq 1\), \(\llbracket \texttt{ n}_1-\texttt{ n}_2 \rrbracket ^{s} \triangleq \max (0, \llbracket \texttt{ n}_1 \rrbracket ^{s}-\llbracket \texttt{ n}_2 \rrbracket ^{s})\), etc.

Let \(\downarrow \) be a special symbol for termination. A configuration \(\mu \), for (extended) statement \(\texttt{stm}\in \texttt{Stmt}\cup \{ \downarrow \}\), store \(s\in \texttt{Store}\), and a quantum state \(\rho \in \mathcal {H}_{Q}\), has the form \((\texttt{stm}, s,\rho )\). Let \(\texttt {Conf}\) be the set of configurations. A configuration \((\texttt{stm},s,\rho )\) is well-formed with respect to the sets of variables B, V, and Q if \(\mathcal {B}(\texttt{stm}) \subseteq B\), \(\mathcal {N}(\texttt{stm}) \subseteq V\), \(\mathcal {Q}(\texttt{stm}) \subseteq Q\), \(dom(s) = B \cup V\), and \(\rho \in \mathfrak {D}(\mathcal {H}_{Q})\). Throughout the paper, we only consider configurations that are well-formed with respect to the sets of variables of the program under consideration.

The operational semantics is described in Figure 4 as a PARS \(\rightarrow _{\!\tiny \textsc {q}}\) over objects in \(\texttt {Conf}\), where terminal objects are precisely the configurations of the shape \((\downarrow ,s,\rho )\). The (classical or quantum) state of a configuration can only be updated by the three rules (Exp), (Op), and (Meas). Rule (Exp) updates the classical store wrt the value of the evaluated expression. Rule (Op) updates the quantum state to a new quantum state \(\varPhi _{U_{\overline{\texttt{q}}}}(\rho ) = U_{\overline{\texttt{q}}} \rho U_{\overline{\texttt{q}}}^\dagger \), where \(U_{\overline{\texttt{q}}}\) is the unitary operator in \(\mathcal {M}(\mathcal {H}_{Q})\) computed by extending the quantum gate \(\texttt{U}\) to the entire set of qubits Q. Rule (Meas) performs a measurement on qubit \(\texttt{q}_i\). This rule returns a distribution of configurations corresponding to the two possible outcomes, \(k=0\) and \(k=1\), with their respective probabilities \(tr(M_{k,i} \rho )\) and, in each case, updates the classical store and the quantum state accordingly. In the particular case where \(tr(M_{k_0,i} \rho )=0\) for some \(k_0\in \{0,1\}\), \( \{tr(M_{k,i}\rho ): (\downarrow ,s[\texttt{x}:= k],m_{k,i}(\rho ))\}_{k \in \{0,1\}} = \{1 : (\downarrow ,s[\texttt{x}:= 1-k_0],m_{1-k_0,i}(\rho )) \}\). Rule (Seq) governs the execution of a sequence of statements \(\texttt{stm}_1;\texttt{stm}_2\), under the covenant that \(\downarrow ; \texttt{stm}\triangleq \texttt{stm}\), for each statement \(\texttt{stm}\). The rule accounts for potential probabilistic behavior when \(\texttt{stm}_1\) performs a measurement and it is otherwise standard. All the other rules are standard.

In a configuration \(\mu = (\texttt{stm},s,\rho )\), the pair \(\sigma \triangleq (s,\rho )\) is called a state. Let \(\texttt{St}^\texttt{stm}\) be the set of states \(\sigma ,\tau ,\ldots \) that are well-formed wrt statement \(\texttt{stm}\). For simplicity, we will denote this set by \(\texttt{St}\) when \(\texttt{stm}\) is clear from the context. To ease the presentation, we sometimes write \((\texttt{stm}, \sigma )\) for the configuration \(\mu \).

We will be interested in expectation-based reasoning on quantum programs. In what follows, we also call functions \(f : \texttt {Conf}\rightarrow \mathbb {R}^{+\infty }\) expectations, for brevity.

Definition 2

For a statement \(\texttt{stm}\) and \(f : \texttt{St}\rightarrow \mathbb {R}^{+\infty }\), we overload the notions of expected derivation length and weakest pre-expectation by:

$$\begin{aligned} & \textsf{edl}_{\texttt{stm}} : \texttt{St}\rightarrow \mathbb {R}^{+\infty }{} & {} \textsf{qwp}_{\texttt{stm}} : {(\texttt{St}\rightarrow \mathbb {R}^{+\infty })} \rightarrow {(\texttt{St}\rightarrow \mathbb {R}^{+\infty })} \\ & \textsf{edl}_{\texttt{stm}}\triangleq \lambda \sigma . \textsf{edl}_{\rightarrow _{\!\tiny \textsc {q}}} (\texttt{stm}, \sigma ){} & {} \textsf{qwp}_{\texttt{stm}} \triangleq \lambda f.\lambda \sigma . \textsf{wp}_{\rightarrow _{\!\tiny \textsc {q}}}(f_{st})(\texttt{stm},\sigma ) ,\end{aligned}$$

where \(f_{st} (\texttt{stm},\tau ) = f(\tau )\).

Example 2

Consider the program \(\texttt{Cntoss}\) given Figure 3. In the setting of the program \(\texttt{Cntoss}\), \(Q=\{\texttt{q}\}\), \(M_{0,1} = \left( {\begin{matrix}1 &{} 0 \\ 0 &{} 0\end{matrix}}\right) \!\) and \(M_{1,1} = \left( {\begin{matrix}0 &{} 0 \\ 0 &{} 1\end{matrix}}\right) \!\). On an initial state \(\sigma = (s,\rho )\), the reduction starts deterministically as in the classical setting, performing the initialization \(\texttt{x}= \texttt{tt}\) and \(\texttt{i}= 0\). From there, evaluation reaches the loop \({\texttt {while}}\,\texttt{x}\ {{\texttt{do}}}\ \texttt{stm}\). At each loop iteration, the loop counter \(\texttt{i}\) is incremented, and the Hadamard gate applied to the quantum variable \(\texttt{q}\). The loop guard is obtained through measuring \(\texttt{q}\).

To see how this is reflected in the semantics, let us first look at an iteration of the loop. If \(\texttt{x}\) was set to false, that is \(\texttt{x}\) holds the value 0, by rule (Wh\(_0\)) the loop terminates within one step:

figure q

On the other hand, when \(\texttt{x}\) was previously set to true, the loop executes its body. Precisely, we have:

figure r

where in the last step, the probability \(p_k\) equals \(\text {tr}(M_{k,1}\varPhi _H(\rho ))\), while the normalized quantum state \(\rho _{k}\) is given as \(m_{k,1}(\varPhi _H(\rho ))\). The above reduction is obtained by applying the rules of Figure 4: rule (Wh\(_1\)) for reduction (1); rules (Exp) and (Seq) for reduction (2); rules (Op) and (Seq) for reduction (3); and finally rules (Meas) and (Seq) for reduction (4).

For an arbitrary initial quantum state \(\rho = \left( {\begin{matrix}\alpha &{} \beta \\ \gamma &{} \delta \end{matrix}}\right) \! \in \mathfrak {D}(\mathcal {H}_{Q})\) (where \(\alpha ,\beta ,\gamma ,\delta \in \mathbb {C}\) and \(tr(\rho )=\alpha +\delta =1\), \(\gamma = \overline{\beta }\), etc.), it follows that

$$\begin{aligned} p_0 = tr(M_{0,1} H\rho H^\dagger ) = tr(\left( {\begin{matrix}1 &{} 0 \\ 0 &{} 0\end{matrix}}\right) \! \frac{1}{2} \left( {\begin{matrix}\alpha +\beta +\gamma +\delta &{}\alpha -\beta +\gamma -\delta \\ \alpha +\beta -\gamma -\delta &{} \alpha -\beta -\gamma +\delta \end{matrix}}\right) \!) = \frac{1+\beta + \gamma }{2} ,\end{aligned}$$

and that, \(p_1 = 1 - p_0 = \frac{1 - (\beta + \gamma )}{2}\). Using \(\rho _{k} = \frac{M_{k,1}H\rho H^\dagger M_{k,1}^\dagger }{tr(M_{k,1} H\rho H^\dagger )} = \frac{(M_{k,1}H)\rho (M_{k,1}H)^\dagger }{p_k}\),

figure s

Summarizing (1)–(4) we thus get:

figure t

Putting everything together, we have

figure u

where terminal configurations are underlined. This reduction converges to the terminal distribution

$$\begin{aligned} \textsf{term}_{\texttt{Cntoss}}(s,\rho ) = \begin{array}{@{}l@{}} \{p_0 : (\downarrow ,[\texttt{x}{:=}0, \texttt{i}{:=}1],\rho _0) \}+ \{\frac{p_1}{2^{i}} : (\downarrow ,[\texttt{x}{:=}0, \texttt{i}{:=}i+1],\rho _0) \}_{i\ge 1} \end{array} ,\end{aligned}$$

with an expected derivation length of

$$ \textsf{edl}_{\texttt{Cntoss}}(s,\left( {\begin{matrix}\alpha &{} \beta \\ \gamma &{} \delta \end{matrix}}\right) \!) = 2 + 4 + (p_0+4p_1) + \sum _{i=1}^\infty \frac{5p_1}{2^i} = 7+8p_1 = 11 - 4(\beta + \gamma ) . $$

For expectation \(f(s,\rho )\triangleq s(\texttt{i})\), measuring the iteration counter \(\texttt{i}\), we have

$$ \textsf{qwp}_{\texttt{Cntoss}}\ f \ (s,\left( {\begin{matrix}\alpha &{} \beta \\ \gamma &{} \delta \end{matrix}}\right) \!) = p_0\times 1 + \sum _{i=1}^\infty \frac{p_1}{2^i}(i+1)=p_0+p_1+2p_1= 2 - (\beta + \gamma ), $$

that is, the mean value held by \(\texttt{i}\) holds after execution is \(2 - (\beta + \gamma )\). The termination probability is

$$ \textsf{qwp}_{\texttt{Cntoss}} \ (\lambda \sigma . 1) \ (s,\left( {\begin{matrix}\alpha &{} \beta \\ \gamma &{} \delta \end{matrix}}\right) \!) = p_0\times 1 + \sum _{i=1}^\infty \frac{p_1}{2^i} \times 1=p_0+p_1=1, $$

i.e., the program is almost surely terminating.

3 Weakest Pre-expectations and Arithmetical Hierarchy

In this section, we study the hardness of some natural quantitative problems for weakest pre-expectations and expected derivation length.

Computability-Aimed Restrictions. This subsection is devoted to putting some restrictions on programs and on the considered notion of expectation to overcome the issues of computability, mentioned in the introduction.

Algebraic numbers. Towards this end, our solution is to target a subset of complex numbers, where simple operations like equality are decidable. We consider the set \(\overline{\mathbb {Q}}\) of algebraic numbers, i.e., complex numbers in \(\mathbb {C}\) that are roots of a non-zero polynomial in \(\mathbb Q[X]\). Let \(\mathbb {A}\triangleq \overline{\mathbb {Q}}\cap \mathbb {R}\) be the real closed field of real algebraic numbers in \(\mathbb {R}\). The following inclusions trivially hold (i) \(\mathbb {N} \subseteq \mathbb {Q} \subseteq \mathbb {A}\subseteq \mathbb {R} \subseteq \mathbb {C}\) and (ii) \(\overline{\mathbb {Q}}\subseteq \mathbb {C}\). It was proved in [18, Proposition 2.2] that equality over \(\overline{\mathbb {Q}}\) and inequality over \(\mathbb {A}\) are decidable using Cohn’s representation [12]. It is well-known that the product and sum over \(\overline{\mathbb {Q}}\) are computable in polynomial time.

We now restrict the program semantics to matrices and density operators over algebraic numbers. Given a totally ordered set of qubits \(Q=\{\texttt{q}_1,\ldots ,\texttt{q}_n\}\), let \(\tilde{\mathcal {H}}_{Q}\) be the Hausdorff pre-Hilbert space \(\overline{\mathbb {Q}}^{2^{n}}\) (i.e., the completeness requirement on Hilbert spaces is withdrawn) of n qubits defined by \(\tilde{\mathcal {H}}_{Q} \triangleq \otimes _{i=1}^n \tilde{\mathcal {H}}_{\texttt{q}_i}\), with \(\tilde{\mathcal {H}_{\texttt{q}}} \triangleq \overline{\mathbb {Q}}^{2}\) being the vector space of computational basis \(\{\mathinner {|{0}\rangle },\mathinner {|{1}\rangle }\}\) over the field \(\overline{\mathbb {Q}}\). Let \(\mathcal {M}(\tilde{\mathcal {H}}_{Q})\) and \(\mathfrak {D}(\tilde{\mathcal {H}}_{Q})\) be the set of matrices and density operators on \(\tilde{\mathcal {H}}_{Q}\), respectively.

Clifford+T gates. For the program semantics to be defined on the space \(\mathfrak {D}(\tilde{\mathcal {H}}_{Q})\), the considered quantum gates are now restricted to gates whose corresponding unitary operators are in \(\mathcal {M}(\tilde{\mathcal {H}}_{Q})\), i.e., have a matrix representation over the algebraic numbers. To this end, we consider a restriction to the Clifford+T gates: \(\texttt{I}\), \( \texttt{X}\), \(\texttt{Y}\), \(\texttt{Z}\), \(\texttt{H}\), \(\texttt{S}\), \(\texttt{CNOT}\), and \(\texttt{T}\), whose unitary matrices are given below:

$$ I\triangleq \left( {\begin{matrix}1 &{} 0 \\ 0 &{}1\end{matrix}}\right) \!, \ X\triangleq \left( {\begin{matrix}0 &{} 1 \\ 1 &{}0\end{matrix}}\right) \!, \ Y\triangleq \left( {\begin{matrix}0 &{} -\mathfrak {i}\\ \mathfrak {i}&{}0\end{matrix}}\right) \!, \ Z\triangleq \left( {\begin{matrix}1 &{} 0 \\ 0 &{}-1\end{matrix}}\right) \!, \ H\triangleq \frac{1}{\sqrt{2}} \left( {\begin{matrix}1 &{} 1 \\ 1 &{}-1 \end{matrix}}\right) \!, $$
$$ S\triangleq \frac{1}{\sqrt{2}} \left( {\begin{matrix}1 &{} 0 \\ 0 &{}\mathfrak {i}\end{matrix}}\right) \!, \ CNOT\triangleq \left( {\begin{matrix} 1 &{} 0 &{} 0 &{}0 \\ 0 &{} 1 &{} 0 &{} 0\\ 0 &{} 0 &{} 0 &{} 1\\ 0 &{} 0 &{} 1 &{} 0\end{matrix}}\right) \!, \ T\triangleq \left( {\begin{matrix}1 &{} 0 \\ 0 &{} e^{\mathfrak {i}\frac{\pi }{4}}\end{matrix}}\right) \! .$$

The Clifford+T fragment is the set of unitary transformations generated by sequential (matrix multiplication) and parallel (Kronecker product) compositions of the gates H, S, CNOT, and T. This constitutes a reasonable restriction for unitary operators as it is known to be the simplest approximately universal fragment of quantum mechanics [1].

A central observation is that the superoperator associated with a unitary operator of the Clifford+T fragment is an endomorphism over density operators in \(\mathfrak {D}(\tilde{\mathcal {H}}_{Q})\).

Lemma 1

The Clifford+T fragment preserves \(\mathfrak {D}(\tilde{\mathcal {H}}_{Q})\), i.e., there exist Q and \(\overline{\texttt{q}}\in Q\) such that for each unitary operator U of the Clifford+T fragment \(\varPhi _{U_{\overline{\texttt{q}}}} \in \mathfrak {D}(\tilde{\mathcal {H}}_{Q})\rightarrow \mathfrak {D}(\tilde{\mathcal {H}}_{Q})\).

Notice that, while a restriction to Clifford+T is reasonable in terms of quantum mechanics and universality, our result can be extended by adding any quantum gate preserving the above lemma. For example, the phase shift gate, defined by \( P_\varphi \triangleq \left( {\begin{matrix}1 &{} 0 \\ 0 &{}e^{\mathfrak {i}\varphi }\end{matrix}}\right) \! \), preserves \(\mathfrak {D}(\tilde{\mathcal {H}}_{Q})\) whenever \(\varphi = r \pi \), for any \(r \in \mathbb {Q}\).

Let \(\texttt{Stmt}_{\texttt{CT}}\) be the set of statements restricted to quantum gates computing Clifford+T unitary operators (hence a subset of \(\texttt{Stmt}\)), \(\texttt{St}_{\texttt{CT}}\) be the set of states whose quantum state is in \(\mathfrak {D}(\tilde{\mathcal {H}}_{Q})\), and \(\texttt {Conf}_{\texttt{CT}}\) be the set of well-formed configurations in \((\texttt{Stmt}_{\texttt{CT}}\cup \{\downarrow \}) \times \texttt{St}_{\texttt{CT}}\). Let \(\texttt{St}_{\texttt{CT}}^\texttt{stm}\) be the set of states in \(\texttt{St}_{\texttt{CT}}\) that are well-formed wrt statement \(\texttt{stm}\). Once again, by abuse of notation, we will denote this set by \(\texttt{St}_{\texttt{CT}}\) when \(\texttt{stm}\) is clear from the context.

A consequence of Lemma 1 is that \(\texttt {Conf}_{\texttt{CT}}\) is closed under reduction, in the following sense. Let \(\mathcal {D}_{\mathbb {A}^+}^{\text {fin}}(A) \subseteq \mathcal {D}(A)\) be the set of finitely supported sub-distributions \(\delta \) with algebraic probabilities, i.e., \(\delta (a) \in \mathbb {A}^+\) for all \(a \in A\).

Lemma 2

The set \(\mathcal {D}_{\mathbb {A}^+}^{\text {fin}}(\texttt {Conf}_{\texttt{CT}})\) is stable under reduction, more precisely, if \(\delta \in \mathcal {D}_{\mathbb {A}^+}^{\text {fin}}(\texttt {Conf}_{\texttt{CT}})\) and , then \(\varepsilon \in \mathcal {D}_{\mathbb {A}^+}^{\text {fin}}(\texttt {Conf}_{\texttt{CT}})\) and \(c \in \mathbb {A}^{+}\).

Computable expectations. We also restrict the expectation codomain to algebraic numbers. Hence the considered expectations will be functions in \( \texttt{St}_{\texttt{CT}}\rightarrow \mathbb {A}^+\). On its own, this restriction is not sufficient for our concerns, as the set \(\texttt{St}_{\texttt{CT}}\rightarrow \mathbb {A}^+\) is not countable. It implies that there exist expectations in \( \texttt{St}_{\texttt{CT}}\rightarrow \mathbb {A}^+\) that are not computable functions. To resolve this issue, we restrict the space of expectations further to computable ones:

$$ \texttt {E}_{\texttt{CT}}\triangleq \{ f \ | \ f : \texttt{St}_{\texttt{CT}}\rightarrow \mathbb {A}^+,\ f \text { computable} \} .$$

An immediate consequence of Lemma 2 is that \(\textsf{term}_{\texttt{stm}}(\sigma ) \in \mathcal {D}(\texttt {Conf}_{\texttt{CT}})\) for any \(\texttt{stm}\in \texttt{Stmt}_{\texttt{CT}}\) and \(\sigma \in \texttt{St}_{\texttt{CT}}\). In consequence, \(\textsf{qwp}_{\texttt{stm}} \ f \ \sigma \) is well-defined for all \(f \in \texttt{St}_{\texttt{CT}}\). This justifies that in our treatment below, we restrict expectations to the class \(\texttt {E}_{\texttt{CT}}\). However, keep in mind that despite Lemma 2, the subdistribution \(\textsf{term}_{\texttt{stm}}(\sigma )\), obtained at the limit, does not fall within \(\mathcal {D}_{\mathbb {A}^+}^{\text {fin}}(A)\). It is neither finite nor are probabilities algebraic (\(\mathbb {A}^{+}\) is not complete). In particular, in general \(\textsf{qwp}_{\texttt{stm}} \ f \ \sigma \) is a real number, rather than an algebraic one.

Quantitative Problems. We now define formally the quantitative problems that we study. Testing problems. Some natural quantitative problems related to weakest pre-expectations are to determine for a given program \(\texttt{stm}\), a given state \(\sigma \), a given expectation f, and a given algebraic number a, whether the corresponding weakest pre-expectation \( \textsf{qwp}_{\texttt{stm}} \ f \ \sigma \) is smaller than or equal to a. In this setting, it makes sense to consider any possible relation in the set \( \{<,\le ,=,\ge ,>\} \subseteq \mathcal {P}(\mathbb {A}\times \mathbb {A})\) as one could be interested in finding precise values, (strict) upper- or lower-bounds.

Definition 3

The testing problem sets \(\textsc {Test}_{\mathcal {R}} \subseteq \texttt {Conf}_{\texttt{CT}}\times \texttt {E}_{\texttt{CT}}\times \mathbb {A}^+\), for \(\mathcal {R} \in \{<,\le ,=,\ge ,>\}\), are defined by:

$$(\texttt{stm},\sigma ,f,a) \in \textsc {Test}_{\mathcal {R}} \mathrel {{:}\!{\iff }}(\textsf{qwp}_{\texttt{stm}} \ f \ \sigma ) \mathrel {\mathcal {R}} a.$$

The consideration of both \(\textsc {Test}_{\le }\) and \(\textsc {Test}_{>}\) may seem redundant, as \(\textsc {Test}_{>}\) can be viewed as the complement of \(\textsc {Test}_{\le }\). However, it makes perfect sense to distinguish both properties, when considering the corresponding universal problems, as we do in a moment.

Finiteness problem. Another problem of interest consists in checking whether the weakest pre-expectations produces some finitary output.

Definition 4

The finiteness problem set \(\textsc {Test}_{\ne \infty } \subseteq \texttt {Conf}_{\texttt{CT}}\times \texttt {E}_{\texttt{CT}}\) is defined by:

$$(\texttt{stm},\sigma ,f) \in \textsc {Test}_{\ne \infty } \mathrel {{:}\!{\iff }}\textsf{qwp}_{\texttt{stm}} \ f \ \sigma < \infty .$$

Termination problems. We also define two termination problems for almost sure termination and positive almost sure termination:

Definition 5

The sets of (positive) almost-sure terminating configurations \(\textsc {Ast}\subseteq \texttt {Conf}_{\texttt{CT}}\) (\(\textsc {Past}\subseteq \texttt {Conf}_{\texttt{CT}}\)) are defined by:

$$\begin{aligned} (\texttt{stm},\sigma ) \in \textsc {Ast}&\mathrel {{:}\!{\iff }}|\textsf{term}_{\texttt{stm}}(\sigma )|=1\\ (\texttt{stm},\sigma ) \in \textsc {Past}&\mathrel {{:}\!{\iff }}\textsf{edl}_{\texttt{stm}}(\sigma ) < \infty .\end{aligned}$$

It is well-known that \(\textsc {Past}\subsetneq \textsc {Ast}\), cf. [9].

Universal problems. Another kind of natural problems arises if one tries to check some properties for each possible program input (i.e., for each state \(\sigma \)). We can thus define universal properties for each of the sets described previously.

Definition 6

The sets of universal testing, finiteness and (positive) a.s. termination problems are defined by:

$$\begin{aligned} (\texttt{stm},f,g) \! \in \! \textsc {UTest}_{\mathcal {R}} \subseteq \texttt{Stmt}_{\texttt{CT}}\times \texttt {E}_{\texttt{CT}}^2 &\iff \forall \sigma \in \texttt{St}_{\texttt{CT}}, \ (\texttt{stm},\sigma ,f,g(\sigma )) \! \in \! \textsc {Test}_{\mathcal {R}}\\ (\texttt{stm},f) \in \textsc {UTest}_{\ne \infty } \subseteq \texttt{Stmt}_{\texttt{CT}}\times \texttt {E}_{\texttt{CT}}& \iff \forall \sigma \in \texttt{St}_{\texttt{CT}}, \ (\texttt{stm},\sigma ,f) \in \textsc {Test}_{\ne \infty }\\ \texttt{stm}\in \textsc {UAst}\subseteq \texttt{Stmt}_{\texttt{CT}}&\iff \forall \sigma \in \texttt{St}_{\texttt{CT}}, \ (\texttt{stm},\sigma ) \in \textsc {Ast}\\ \texttt{stm}\in \textsc {UPast}\subseteq \texttt{Stmt}_{\texttt{CT}}& \iff \forall \sigma \in \texttt{St}_{\texttt{CT}}, \ (\texttt{stm},\sigma ) \in \textsc {Past}\end{aligned}$$

Example 3

We have \(\texttt{Cntoss}\in \textsc {UAst}\) and \(\texttt{Cntoss}\in \textsc {UPast}\), for the program \(\texttt{Cntoss}\) of Figure 3. Indeed, it was shown in Example 2 that \(\texttt{Cntoss}\) terminates with probability 1 and a finite expected derivation length. This property holds for any input of the domain. In the same example, we have proven \((\texttt{Cntoss},f) \in \textsc {Test}_{\ne \infty }\) for \(f(s,\rho ) = s(\texttt{i})\). Indeed, we have shown the stronger property \((\texttt{Cntoss},f,g) \in \textsc {Test}_{=}\), where \(g(s,\left( {\begin{matrix}\alpha &{} \beta \\ \gamma &{} \delta \end{matrix}}\right) \!) = 2 - (\beta + \gamma )\).

Completeness Results in the Arithmetical Hierarchy. In what follows, we place the introduced quantitative problems within the arithmetical hierarchy [34]. The arithmetical hierarchy is a means to classify and relate undecidable problems wrt. to their inherent difficulty, measured in terms of the number of (unbounded) quantifier alternations needed to state the problem as a formula in first-order arithmetic, based on a decidable (recursive) predicate.

Reminder on the arithmetical hierarchy. Classes of the arithmetical hierarchy are defined inductively as follows:

$$\begin{aligned} \varPi ^0_0 = \varSigma _0^0&\triangleq \textsc {rec}, \quad \textsc {rec}\text { being the class of decidable problems (recursive sets)} \\ \varPi ^0_{n+1} &\triangleq \{ \psi \ | \ \exists \phi \in \varSigma ^0_n,\ \forall \overline{x}. (\psi (\overline{x}) \iff \forall \overline{y}.\phi (\overline{x},\overline{y}))\},\\ \varSigma ^0_{n+1} &\triangleq \{ \psi \ | \ \exists \phi \in \varPi ^0_n,\ \forall \overline{x}. (\psi (\overline{x}) \iff \exists \overline{y}.\phi (\overline{x},\overline{y}))\} .\end{aligned}$$

For each n, \(\varPi ^0_n\) is the complement of \(\varSigma ^0_n\) (i.e., \(\varPi ^0_n =\) co-\(\varSigma ^0_n\), and vice versa) and it is a well-known result that \(\varSigma ^0_ 1\) and \(\varPi ^0_1\) correspond to the classes \(\textsc {re}\) of recursively enumerable (i.e., semi-decidable) problems and co-\(\textsc {re}\) of co-recursively enumerable (i.e., co-semi-decidable) problems, respectively. Given the sets \(A \subseteq X\) and \(B \subseteq Y\), we write \(A \le _\textsf{m}B\) (A is many-one reducible to B) if there exists a computable function \(f: X \rightarrow Y\) such that \(\forall x \in X,\ x \in A \iff f(x) \in B\). Given a class \(\textsc {c}\) of the arithmetical hierarchy and a set A, A is \(\textsc {c}\)-hard if \(\forall B \in \textsc {c},\ B \le _\textsf{m}A\). A set A is \(\textsc {c}\)-complete if \(A \in \textsc {c}\) and A is \(\textsc {c}\)-hard. It is well-known that if a set A is \(\textsc {c}\)-complete then its complement, noted co-A, is (co-\(\textsc {c}\))-complete.

Table 1. Completeness results for quantitative problems in the arithmetical hierarchy.

Results. Table 1 associates the quantum decision problems to the corresponding classes in the arithmetical hierarchy for which we have proven them complete, that is, we have proven membership and hardness for the corresponding class. Some of the results may seem surprising. For instance, the testing problem \(\textsc {Test}_{>}\), i.e., deciding \(\textsf{qwp}_{\texttt{stm}} \ f \ \sigma > a\) within the Clifford+T fragment, turns out to be recursive enumerable. It is thus classified identical to the (classical) halting problem \(\mathcal {H}\).Footnote 1 Remarkable, through the restriction to the Clifford+T fragment, corresponding problems are ranked within the arithmetical hierarchy identical to their non-quantum counterparts (see [24, 37]). This observation holds for all problems apart those marked with (‡) which, to the best of our knowledge, have not been studied in a classical/probabilistic setting. \(\varPi ^0_2\)- and \(\varPi ^0_3\)-completeness of the universal testing problems, given relations > and < respectively, has been conjectured by Kaminski in his PhD thesis [25] for probabilistic programs.

A crucial observation towards these results is that, restricting to the Clifford+T fragment, the weakest pre-expectation of a program \(\texttt{P}\) can be approximated through computable transformers \(\textsf{qwp}^{\le n}_{\texttt{stm}} : \texttt {E}_{\texttt{CT}}\rightarrow \texttt {E}_{\texttt{CT}}\) that limit execution of \(\texttt{stm}\) to at most \(n \in \mathbb {N}\) reduction steps. That is,

$$ \textsf{qwp}^{\le n}_{\texttt{stm}} \ f \ \sigma \triangleq \mathbb {E}_{\textsf{term}^{\le n}_{\texttt{stm}}(\sigma )}(f) ,$$

for \(\textsf{term}^{\le n}_{\texttt{stm}}(\sigma )\) the distribution of terminal configurations obtained within n reduction steps, when evaluating \((\texttt{stm},\sigma )\). With regards to the above mentioned \(\textsc {Test}_{>} \in \varSigma ^0_1\) for instance, observe that:

$$\begin{aligned} (\texttt{stm},f,\sigma ,a) \in \textsc {Test}_{>} & \iff \textsf{qwp}_{\texttt{stm}} \ f \ \sigma > a \\ & \iff \lim _{i \rightarrow \infty } \textsf{qwp}^{\le n}_{\texttt{stm}} \ f \ \sigma > a \\ & \iff \exists n \in \mathbb {N},\, \exists \delta \in \mathbb {A}^{+} \setminus \{0\},\ \textsf{qwp}^{\le n}_{\texttt{stm}} \ f \ \sigma \ge a + \delta .\end{aligned}$$

Crucially, the predicate \(\textsf{qwp}^{\le n}_{\texttt{stm}} \ f \ \sigma \ge a + \delta \) becomes computable. In essence, this is a consequence of Lemma 2: The n-th step normal form distribution \(\textsf{term}^{\le n}_{\texttt{stm}}(\sigma )\) is finite and computable, as f is computable so is thus \(\textsf{qwp}^{\le n}_{\texttt{stm}} \ f \ \sigma \). From here, the result follows now as equality on \(\mathbb {A}\) is decidable. The proof of this, as well as all completeness proofs listed in Table 1 can be found in the Appendix. The following constitutes our first main result.

Theorem 1

All completeness results in Table 1 hold.

4 Quantum Expectation Transformers

In what follows, we are interested in deliniating subclasses of testing problems that lead to decidability. To this end, we now define a notion of quantum expectation transformer as a means to compute symbolically the weakest pre-expectation of a program. We first introduce some preliminary notations in order to lighten the presentation.

Notations. For any expression \(\texttt{ e}\), \(\llbracket \texttt{ e} \rrbracket ^{}\) is a shorthand notation for the function \(\lambda (s, \rho ). \llbracket \texttt{ e} \rrbracket ^{s} \in \texttt{St}\rightarrow \mathbb {R}^{+\infty }\). We will also use \(f[\texttt{x}:= \texttt{ e}]\) for the expectation \(\lambda (s, \rho ).f(s[\texttt{x}:= \llbracket \texttt{ e} \rrbracket ^{s}], \rho )\). Similarly, for a given map \(\chi : \mathfrak {D}(\mathcal {H}_{Q})\rightarrow \mathfrak {D}(\mathcal {H}_{Q})\), \(f[\chi ] \triangleq \lambda (s,\rho ). f(s, \chi (\rho ) )\). We will also sometimes group such state modifications, for instance, \(f[\texttt{x}:= \texttt{ e}\text {; }\chi ]\) stands for \((f[\texttt{x}:= \texttt{ e}])[\chi ]\) and \(f[\texttt{x}:= \texttt{ e},\texttt{y}:=\texttt{ e}']\) stands for \((f[\texttt{x}:= \texttt{ e}])[\texttt{y}:=\texttt{ e}']\).

For \(p \in \texttt{St}\rightarrow [0,1]\) and \(f,g \in \texttt{St}\rightarrow \mathbb {R}^{+\infty }\), \(f \mathrel {{+}_{p}} g\) denotes the function \(\lambda \sigma . p(\sigma ) \cdot f(\sigma ) + (1-p(\sigma )) \cdot g(\sigma ) \in \texttt{St}\rightarrow \mathbb {R}^{+\infty }\), similar we use \(f \cdot g\) to denote \(\lambda \sigma . f(\sigma ) \cdot g(\sigma ) \in \texttt{St}\rightarrow \mathbb {R}^{+\infty }\). Thus, for instance, \(f[\texttt{x}:= \texttt{x}+ 1] +_{\llbracket \texttt{x}= 1 \rrbracket ^{}} f\) behaves like f, except that \(\texttt{x}\) is first incremented when applied to states with classical variable \(\texttt{x}\) equal to 1. In correspondence to the normalization of quantum state \(m_{k,i}\), we define probabilities \(p_{k,i} \triangleq \lambda \rho . tr(M_{k,i}\rho M_{k,i}^{\dagger })\). We overload this function from \(\mathfrak {D}(\mathcal {H}_{Q})\) to \(\texttt{St}\) s.t. \(p_{k,i}(s,\rho ) = p_{k,i}(\rho )\). In this way, \(f[\texttt{x}:= 0\text {; }m_{0,i}] \mathrel {{+}_{p_{0,i}}} f[\texttt{x}:= 1\text {; }m_{1,i}]\) computes precisely the expected value of f on the distribution of states obtained by measuring the i-th qubit and assigning the outcome to classical variable \(\texttt{x}\).

Finally, we denote by \(\le \) also the pointwise extension of the order from \(\mathbb {R}^{+\infty }\) to functions, that is, \(f \le g\) holds iff \(\forall \sigma \in \texttt{St},\ f(\sigma ) \le g(\sigma )\).

Fig. 5.
figure 5

Quantum expectation transformer \(\texttt{qet}\!\left[ \,\cdot \,\right] ^{}\!\left\{ \textstyle \cdot \right\} \)

Definition 7

(Quantum expectation transformer). The quantum expectation transformer consists in a program semantics mapping expectations to expectations in a continuation passing style

$$ \texttt{qet}\!\left[ \,\cdot \,\right] ^{}\!\left\{ \textstyle \cdot \right\} : \texttt{Stmt}\rightarrow (\texttt{St}\rightarrow \mathbb {R}^{+\infty }) \rightarrow (\texttt{St}\rightarrow \mathbb {R}^{+\infty }) $$

and is defined inductively on statements in Figure 5.

This transformer corresponds to the notion of expected value transformer of [6] on the Kegelspitze \(\texttt{S}= (\mathbb {R}^{+\infty },+_{\texttt{f}})\), with \(+_{\texttt{f}}\) being the forgetful addition. In the case of loops, the least fixed point \(\textsf{lfp}\) is defined with respect to the pointwise ordering on the function space \(\texttt{St}\rightarrow \mathbb {R}^{+\infty }\). Equipped with this ordering, this space forms a \(\omega \)-CPO. As the quantum transformer can be shown to be \(\omega \)-continuous, the fixed-point is always defined, cf. [44].

Theorem 2

(Adequacy). The following holds:

$$\forall \texttt{stm}\in \texttt{Stmt},\ \forall f : \texttt{St}\rightarrow \mathbb {R}^{+\infty },\ \textsf{qwp}_{\texttt{stm}}(f) = \texttt{qet}\!\left[ \,\texttt{stm}\,\right] ^{}\!\left\{ \textstyle f\right\} .$$

Apart from continuity, the quantum expectation transformer satisfies several useful laws, see Figure 6. The (monotonicity) Law permits us to reason modulo upper-bounds: actual expectations can be always substituted by upper-bounds. It is in fact an immediate consequence from the (continuity) Law, which is defined for any \(\omega \)-chain \((f_i)_i\). The (upper invariance) Law constitutes a generalization of the notion of invariant stemming from Hoare calculus. It is used to find closed-form upper-bounds g to expectations f of loops. The pre-conditions state that g should dominate f on states where the loop would immediately exist, and otherwise, should remain invariant under iteration. It is worth mentioning that this proof rule is not only sound, but also complete, in the sense that any upper-bound satisfies the two constraints. The following example illustrates the use of this rule on the running example.

Fig. 6.
figure 6

Universal laws derivable for the quantum expectation transformer.

Example 4

Following Example 2, we over-approximate \(\texttt{qet}\!\left[ \,\texttt{Cntoss}\,\right] ^{}\!\left\{ \textstyle f\right\} \), for \(f(s,\rho ) = s(\texttt{i})\) the post-expectation measuring the classical variable \(\texttt{i}\).

To this end, observe that the function \(g: \texttt{St}\rightarrow \mathbb {R}^{+\infty }\) is an upper-invariant (Figure 6) to the while loop \({\texttt {while}}\,\texttt{x}\ {{\texttt{do}}}\ \texttt{stm}\), given a post-expectation \(f: \texttt{St}\rightarrow \mathbb {R}^{+\infty }\). Recall that the loop body \(\texttt{stm}\) comprises \((\texttt{i}= \texttt{i}{+}1; \texttt{q}\,*\!\!= \texttt{H}; \texttt{x}= {\textsf{meas}}{\texttt{q}})\). To fulfill the conditions of the (upper invariance) Law the following inequalities have to be met:

$$\begin{aligned} \llbracket \lnot \texttt{x} \rrbracket ^{} \cdot f &\le g & \llbracket \texttt{x} \rrbracket ^{} \cdot \texttt{qet}\!\left[ \,\texttt{i}= \texttt{i}{+}1;\texttt{q}\,*\!\!= \texttt{H}; \texttt{x}= {\textsf{meas}}{\texttt{q}}\,\right] ^{}\!\left\{ \textstyle g\right\} & \le g .\end{aligned}$$
(5)

By unfolding the definition, we see

figure w

By using the identities computed already in Example 2, we thus obtain

$$\begin{aligned} \texttt{qet}\!\left[ \,\texttt{stm}\,\right] ^{}\!\left\{ \textstyle g\right\} (s,\left( {\begin{matrix}\alpha &{} \beta \\ \gamma &{} \delta \end{matrix}}\right) \!) = \sum _{k \in \{0,1\}} p_k \cdot g(s[\texttt{x}= k,\texttt{i}{:=}\texttt{i}{+}1],\rho _{k}) ,\end{aligned}$$
(6)

where, as in Example 2, \(p_{0} = \frac{1 + \beta + \gamma }{2}\), \(p_{1} = \frac{1 - (\beta + \gamma )}{2}\), \(\rho _0 = \left( {\begin{matrix}1 &{} 0 \\ 0 &{} 0\end{matrix}}\right) \!\) and \(\rho _1 = \left( {\begin{matrix}1 &{} 0 \\ 0 &{} 0\end{matrix}}\right) \!\)

We claim that \( g (s,\left( {\begin{matrix} \alpha &{} \beta \\ \gamma &{} \delta \end{matrix}}\right) \!) \triangleq s(\texttt{i}) + s(\texttt{x}) \cdot (2 - (\beta + \gamma )) \) is an upper-bound to the pre-expectation of the while loop wrt. to the post expectation f. To this end, we check (5). The first inequality is trivially satisfied. Concerning the second, notice that by definition,

$$ g(s[\texttt{x}= 0, \texttt{i}{:=}\texttt{i}{+}1],\left( {\begin{matrix}1 &{} 0 \\ 0 &{} 0\end{matrix}}\right) \!) = s(\texttt{i}) + 1 \quad \text {and}\quad g(s[\texttt{x}= 1, \texttt{i}{:=}\texttt{i}{+}1],\left( {\begin{matrix}0 &{} 0 \\ 0 &{} 1\end{matrix}}\right) \!) = s(\texttt{i}) + 3. $$

By (6) we have

$$\begin{aligned} \texttt{qet}\!\left[ \,\texttt{stm}\,\right] ^{}\!\left\{ \textstyle g\right\} (s,\left( {\begin{matrix}\alpha &{} \beta \\ \gamma &{} \delta \end{matrix}}\right) \!) & = \frac{1+\beta +\gamma }{2} (s(\texttt{i}) + 1) + \frac{1-(\beta +\gamma )}{2} (s(\texttt{i}) + 3) \\ & = (s(\texttt{i}) + 2) -(\beta +\gamma ) = g(s, \left( {\begin{matrix}\alpha &{} \beta \\ \gamma &{} \delta \end{matrix}}\right) \!) ,\end{aligned}$$

from which now the second constraint follows by case analysis on the value of \(\texttt{x}\). Hence \(\texttt{qet}\!\left[ \,{\texttt {while}}\,\texttt{x}\ {{\texttt{do}}}\ \texttt{stm}\,\right] ^{}\!\left\{ \textstyle f\right\} \le g\) and, by monotonicity (Figure 6),

$$\begin{aligned} \texttt{qet}\!\left[ \,\texttt{Cntoss}\,\right] ^{}\!\left\{ \textstyle f\right\} (s,\left( {\begin{matrix}\alpha &{} \beta \\ \gamma &{} \delta \end{matrix}}\right) \!) & \le \texttt{qet}\!\left[ \,\texttt{x}= \texttt{tt}; \texttt{i}= 0 \,\right] ^{}\!\left\{ \textstyle g\right\} (s,\left( {\begin{matrix}\alpha &{} \beta \\ \gamma &{} \delta \end{matrix}}\right) \!) \\ & = g([\texttt{x}{:=}1,\texttt{i}{:=}0],\left( {\begin{matrix}\alpha &{} \beta \\ \gamma &{} \delta \end{matrix}}\right) \!) = 2 - (\beta + \gamma ) .\end{aligned}$$

Note that, in this case, the computed bound is exact.

One question of interest is to find the \(\texttt{qet}\!\left[ \,\cdot \,\right] ^{}\!\left\{ \textstyle \cdot \right\} \) of a given statement. We obtain the following completeness results as a corollary of Theorem 1 and Theorem 2 on the Clifford+T fragment.

Corollary 1

The following completeness results hold:

  • \(\{(\texttt{stm},f,g) \in \texttt{Stmt}_{\texttt{CT}}\times \texttt {E}_{\texttt{CT}}^2 \ | \ \forall \sigma , \ \texttt{qet}\!\left[ \,\texttt{stm}\,\right] ^{}\!\left\{ \textstyle f\right\} (\sigma )\ = g(\sigma )\}\) is \(\varPi ^0_2\)-complete.

  • \(\{(\texttt{stm},f,g) \in \texttt{Stmt}_{\texttt{CT}}\times \texttt {E}_{\texttt{CT}}^{2} \ | \ \forall \sigma , \ \texttt{qet}\!\left[ \,\texttt{stm}\,\right] ^{}\!\left\{ \textstyle f\right\} (\sigma )\ \le g(\sigma )\}\) is \(\varPi ^0_1\)-complete.

The same kind of result can be straightforwardly obtained for each of the quantitative problems defined in previous section. All the corresponding sets are undecidable: they are at best (co-)semi-decidable as illustrated by Figure 1. This motivates us for restricting the problem a bit further to find a class of functions for which the quantitative problems for \(\textsf{wp}_{\texttt{stm}}\ f\) can be decided.

5 Decidability of qet Inference over a Real Closed Field

Corollary 1 illustrates that it is not sufficient to relax the problem of finding the quantum expectation transformer of a given statement to upper-bounds, in order to make it decidable. The undecidability of finding the quantum expectation transformer of a given program is due to two other issues: 1) Issue 1: The computation of a fixpoint for \(\texttt{qet}\!\left[ \,\cdot \,\right] ^{}\!\left\{ \textstyle \cdot \right\} \) in the case of while loops, 2) Issue 2: The check for inequalities over functions in \(\texttt {E}_{\texttt{CT}}\), whose first-order theory is not decidable. This section is devoted to overcoming these two issues, by finding an expressive fragment on which the inference of an upper-bound of the quantum expectation transformer becomes decidable.

Symbolic Inference. As a first step towards automated inference, we define a symbolic variant of the quantum expectation transformer in Figure 7. In the case of conditionals, loops, and measurements, we will use fresh variables for expectations; will guarantee that these variables indeed denote (upper-bounds to) the corresponding expectations. This means that the symbolic version yields correct results only when the expectations assigned to these variables satisfy all the side conditions. By solving the generated constraints, viz., by finding an interpretation of ascribed variables that satisfy the imposed side-conditions, we effectively arrive at an inference procedure overcoming Issue 1.

Fig. 7.
figure 7

Term representations of \(\texttt{qinf}\!\left[ \,\cdot \,\right] ^{}\!\left\{ \textstyle \cdot \right\} \) and their corresponding side-conditions.

To formalize this approach, we associate a unique label \(\ell \) with each loop, conditional, and measurement, occurring in the considered program. Notationally, we write \({\texttt {while}}^{\ell }\, \texttt{ b}\ {{\texttt{do}}}\ \texttt{stm}\) / \(\textsf{if}^{\ell }\, \texttt{ b}\,{{{\texttt{then}}}}\,\texttt{stm}_1 \,{{{\texttt{else}}}}\,\texttt{stm}_2\) / \({\textsf{meas}}^{\ell }\,{\texttt{q}}\). Such labels permit us to associate a unique expectation variable \(X_\ell \) to each of these constructs. Given a set of such expectation variables \(\textsf{EVar}\), the set of terms \(\textsf{ETerm}\), upon which the symbolic quantum expectation transformer operates, is defined according to the following grammar:

$$ \begin{array}{llll} \textsf{ETerm}& F,G & :\,\!:=& X \mid F[\texttt{x}:= \texttt{ e}] \mid F[\chi ] \mid F \mathrel {{+}_{p}} G, \end{array} $$

where X stand for an arbitrary expectation variable in \(\textsf{EVar}\). As stressed above, X will be used to denote certain expectations wrt. loops, conditionals, and measurements. We have already introduced the notations \(F[\texttt{x}:= \texttt{ e}]\) and \(F[\chi ]\) to represent updates to the classical and quantum state, respectively. Here, \(\chi \) will always denote a finite composition of superoperators \(\phi _U\) and measurements \(m_{k,i}\). By ensuring that normalization of quantum states \(m_{k,i}(\rho )\) is never considered in the degenerate case of a zero-probability measurement \(p_{k,i}(\rho )\), it will thereby always be possible to write \(\chi \) as \(\lambda \rho . \frac{{M}\rho {M}^\dagger }{tr(N\rho N^\dagger )}\), for some \(M \in \mathcal {M}(\tilde{\mathcal {H}}_{Q})\) in the Clifford+T fragment. Finally, following the same reasoning, in the barycentric sum \(F \mathrel {{+}_{p}} G\) the probability p is a function in the quantum state, and will always be of general form \(\lambda \rho . \frac{tr(M \rho M^\dagger )}{tr(N \rho N^\dagger )}\), for some \(M,N \in \mathcal {M}(\tilde{\mathcal {H}}_{Q})\) in the Clifford+T fragment. Similar to before, we may group updates such as in \(F[\texttt{x}:= \texttt{ e};\chi ]\).

The symbolic variation of the expectation transformer can now be defined as

$$ \texttt{qinf}\!\left[ \,\cdot \,\right] ^{}\!\left\{ \textstyle \cdot \right\} : \texttt{Stmt}\rightarrow \textsf{ETerm}\rightarrow \textsf{ETerm}, $$

generating also a set of side-conditions of the shape \(\Gamma \vdash F \le G\), with the intended meaning that G binds F on all input states that satisfy the predicate \(\Gamma \). The full definition of \(\textsf{qinfer}\) is given in Figure 7. As already hinted, the side conditions ensure that introduced variables \(X_{\ell }\) indeed yield an upper-bound on the corresponding expectation, in the case of conditionals by case-analysis, and in the case of loops via an application of the upper-invariant law from Figure 6. In the case of measurements, \(m_{k,i}\) and \(p_{k,i}\) are defined exactly as before. Here, we single out the two cases where the probability of a measurement, either \(p_{0,i}(\rho ) = tr(M_{0,i}\rho ) = tr(M_{0,i}\rho M_{0,i}^\dagger )\) or \(p_{1,i}(\rho ) = 1 - p_{0,i}(\rho )\), is zero. This way, we avoid the case analysis underlying the definition of \(m_{k,i}\) and may, wlog., assume that it is indeed of the form \(\lambda \rho . \frac{M_{k,i}\rho M_{k,i}^\dagger }{tr(M_{k,i}\rho M_{k,i}^\dagger )}\), with non-zero trace \(tr(M_{k,i}\rho M_{k,i}^\dagger )\).

Example 5

In correspondence to Example 4, let us consider the application of the inference procedure on the program \(\texttt{Cntoss}\), wrt. to the post-expectation \(f(s,\rho ) = s(\texttt{i})\). We label the loop and measurement with \(\textsf{m}\) and \(\textsf{w}\), respectively.

Let X denote the post-expectation f. Unfolding the definition, we see

$$\begin{aligned} \texttt{qinf}\!\left[ \,\texttt{Cntoss}\,\right] ^{}\!\left\{ \textstyle X\right\} & = \texttt{qinf}\!\left[ \,\texttt{x}= \texttt{tt}; \texttt{i}= 0; {\texttt {while}}^{\texttt {w}}\, \texttt{x}\ {{\texttt{do}}}\ \texttt{stm}\,\right] ^{}\!\left\{ \textstyle X\right\} \\ &= X_\textsf{w}[\texttt{x}{:=}1; \texttt{i}{:=}0] ,\end{aligned}$$

generating the side-conditions and . The left-hand side of the first constraint is obtained from

$$\begin{aligned} \texttt{qinf}\!\left[ \,\texttt{stm}\,\right] ^{}\!\left\{ \textstyle X_\textsf{w}\right\} & = \texttt{qinf}\!\left[ \,\texttt{i}= \texttt{i}{+}1\,\right] ^{}\!\left\{ \textstyle \texttt{qinf}\!\left[ \,\texttt{q}\,*\!\!= H\,\right] ^{}\!\left\{ \textstyle \texttt{qinf}\!\left[ \,{\textsf{meas}}^{\textsf{m}}\,{\texttt{q}}\,\right] ^{}\!\left\{ \textstyle X_\textsf{w}\right\} \right\} \right\} \\ & = X_\textsf{m}[\varPhi _{H};\texttt{i}{:=}\texttt{i}{+}1] .\end{aligned}$$

Note that this expansion generates further constraints, this time on \(X_\textsf{m}\) representing the measurement. Specifically, it yields the following constraints:

figure aa

Using the analysis from Example 4, we interpret \(X_\textsf{w}\) and \(X_\textsf{m}\) as:

$$\begin{aligned} \alpha (X_\textsf{w}) & \triangleq \lambda (s,\left( {\begin{matrix}\alpha &{} \beta \\ \gamma &{} \delta \end{matrix}}\right) \!).\ s(\texttt{i}) + s(\texttt{x}) (2 - (\beta + \gamma )),\\ \alpha (X_\textsf{m}) & \triangleq \lambda (s,\left( {\begin{matrix}\alpha &{} \beta \\ \gamma &{} \delta \end{matrix}}\right) \!).\ s(\texttt{i}) + 2 - 2 \alpha .\end{aligned}$$

Furthermore, we interpret the input variable X as f, i.e., \(\alpha (X) \triangleq \lambda (s,\rho ).\ s(\texttt{i})\). Notice how \(\alpha (X_\textsf{w})\) just corresponds to the upper-invariant g derived in Example 4. Using the assignment, it is now standard to check that it is a solution to the five constraints. For instance, considering states \(\sigma =(\{\texttt{i}{:=}n, \texttt{x}{:=}x\},\left( {\begin{matrix}\alpha &{} \beta \\ \gamma &{} \delta \end{matrix}}\right) \!)\), the ultimate constraint amount to the implication

$$ \alpha \ne 0 \ne \delta \Rightarrow n +_{\alpha } (n + 2) \le n + 2 - 2\alpha ,$$

which trivially holds. Finally, recall \(\texttt{qinf}\!\left[ \,\texttt{Cntoss}\,\right] ^{}\!\left\{ \textstyle X\right\} = X_\textsf{w}[\texttt{x}{:=}1; \texttt{i}{:=}0]\). This term is interpreted as \(\lambda (s,\left( {\begin{matrix}\alpha &{} \beta \\ \gamma &{} \delta \end{matrix}}\right) \!).\ 2 - (\beta + \gamma )\), yielding the optimal bound computed in Example 4.

Example 6

Re-consider program \(\texttt{RUS}\) depicted in Figure 1. Here, we are interested in an upper-bound on the number of T-gates, counted by the program variable \(\texttt{i}\). As before, we label the loop and measurement with \(\textsf{m}\) and \(\textsf{w}\), respectively. Let

$$ \texttt{stm}= \overbrace{\texttt{q}_2 = \mathinner {|{0}\rangle }; \ldots }^{\texttt{stm}_0} ; \texttt{x}= {\textsf{meas}}^{\textsf{m}}\,{\texttt{q}_2} ,$$

be the body of the while loop statement (see Figure 1). We proceed with the analysis backwards. By the rules of Figure 7 it holds that \(\texttt{qinf}\!\left[ \,\texttt{stm}_0\,\right] ^{}\!\left\{ \textstyle F\right\} = F[\varPhi ; \texttt{i}{:=}\texttt{i}{+}2]\) for any F, where \(\varPhi \) gives the quantum state updates within \(\texttt{stm}_0\). Unfolding definitions, we have \(\texttt{qinf}\!\left[ \,\texttt{RUS}{}\,\right] ^{}\!\left\{ \textstyle X\right\} = X_\textsf{w}[\texttt{x}{:=}0;\texttt{i}{:=}1]\) with and since, by the above observation,

$$ \texttt{qinf}\!\left[ \,\texttt{stm}\,\right] ^{}\!\left\{ \textstyle X_\textsf{w}\right\} = \texttt{qinf}\!\left[ \,\texttt{stm}_0\,\right] ^{}\!\left\{ \textstyle \texttt{qinf}\!\left[ \,\texttt{x}= {\textsf{meas}}^{\textsf{m}}\,{\texttt{q}_2}\,\right] ^{}\!\left\{ \textstyle X_\textsf{w}\right\} \right\} =X_\textsf{m}[\varPhi ;\texttt{i}{:=}\texttt{i}{+}2] ,$$

subject to the following additional constraints stemming from measurements:

figure ad

Taking \(\alpha (X) \triangleq \lambda (s,\rho ).\ s(\texttt{i})\) and solving the constraints yields a constant upper bound of on the expected number of T-gates used by the program. This is due to the fact that the probability of the internal measurement is always \(\frac{3}{4}\). Note that this bound is tight.

The transformer \(\textsf{qinfer}\) can be linked to \(\textsf{qet}\) of course only when variables \(X_{\ell }\) are interpreted in a way that the side conditions generated by \(\textsf{infer}\) are met. To spell this out formally, let \(\alpha : \textsf{EVar}\rightarrow \texttt {E}_{\texttt{CT}}\) be an assignment of expectations to variables in \(\textsf{EVar}\), and let \(\llbracket F \rrbracket ^{\alpha } : \texttt {E}_{\texttt{CT}}\) denote the interpretation of \(F \in \textsf{ETerm}\) under \(\alpha \) defined in the natural way, e.g., \(\llbracket X_{\ell } \rrbracket ^{\alpha } = \alpha (X_{\ell })\), \(\llbracket F[\chi ] \rrbracket ^{\alpha } = \llbracket F \rrbracket ^{\alpha }[\chi ]\), etc.

We say that a constraint \(\Gamma \vdash F \le G\) is valid under \(\alpha \) if \(\llbracket F \rrbracket ^{\alpha }(\sigma )\le \llbracket G \rrbracket ^{\alpha }(\sigma )\) holds for all states \(\sigma \in \texttt{St}_{\texttt{CT}}\) with \(\Gamma (\sigma )\). An assignment \(\alpha \) is a solution to a set of constraints \(\mathcal {C}\), if it makes every constraint in \(\mathcal {C}\) valid. Finally, we say \(\alpha \) is a solution to \(\texttt{qinf}\!\left[ \,\texttt{stm}\,\right] ^{}\!\left\{ \textstyle f\right\} \) if it is a solution to the set of constraints generated by \(\texttt{qinf}\!\left[ \,\texttt{stm}\,\right] ^{}\!\left\{ \textstyle f\right\} \). We have the following correspondence:

Theorem 3

For any \(\alpha \in \textsf{EVar}\rightarrow \texttt {E}_{\texttt{CT}}\), if \(\alpha \) is solution to \(\texttt{qinf}\!\left[ \,\texttt{stm}\,\right] ^{}\!\left\{ \textstyle F\right\} = G\), then it holds that \(\texttt{qet}\!\left[ \,\texttt{stm}\,\right] ^{}\!\left\{ \textstyle \llbracket F \rrbracket ^{\alpha }\right\} \le \llbracket G \rrbracket ^{\alpha }\).

It is worth mentioning that the above procedure could have been defined without restriction to the full space \(\texttt{St}\rightarrow \mathbb {R}^{+\infty }\) of expectations. In this case, this symbolic approach is also complete, in the sense that if \(\texttt{qet}\!\left[ \,\texttt{stm}\,\right] ^{}\!\left\{ \textstyle f\right\} = g\) then \(\texttt{qinf}\!\left[ \,\texttt{stm}\,\right] ^{}\!\left\{ \textstyle X\right\} = G\) for some G such that the side-conditions have a solution \(\alpha \), with \(\alpha (X) = f\) and \(\llbracket G \rrbracket ^{\alpha } = g\). As our main focus is on decidability, however, we have made the choice to restrict ourself to the Clifford+T setting.

Restriction to Polynomials over the Real Closed Field \(\mathbb {A}\) . We now turn our eyes towards constraint solving, addressing the remaining Issue 2 through restricting the domain of expectations to polynomials over algebraic numbers. To be more precise, we consider the following problem.

Definition 8

Let \(E \subseteq \texttt {E}_{\texttt{CT}}\) be a class of expectations. The inference problem \(\textsc {Qinfer}(E) \subseteq \texttt{Stmt}_{\texttt{CT}}\times E \times (\textsf{EVar}\rightarrow E)\) is given by

$$ (\texttt{stm},f,\alpha ) \in \textsc {Qinfer}(E) \iff \alpha [X := f]\text { is solution to }\texttt{qinf}\!\left[ \,\texttt{stm}\,\right] ^{}\!\left\{ \textstyle X\right\} $$

In the above definition, \( (\texttt{stm},f,\alpha ) \in \textsc {Qinfer}(E)\) is satisfied if the statement \(\texttt{stm}\) has solution \(\alpha [X := f]\) wrt. the expectation f. Hence it can be seen as checking whether f is a post-expectation for \(\texttt{stm}\). In particular, any solution \(\alpha [X := f]\) constitutes an upper bound on the weakest pre-expectation of f (see Theorem 3). We will now see that \(\textsc {Qinfer}(E)\) is decidable, for E the set of (real algebraic) polynomial expectations of (arbitrary but fixed) degree d. For states \(\texttt{St}_{\texttt{CT}}\) over n classical variables \(\texttt{y}_{1},\dots ,\texttt{y}_n\) and m qubits, let \(\mathbb {A}^d[\texttt{St}_{\texttt{CT}}]\) denotes the class of functions of polynomial expectations of the form

$$\begin{aligned} \lambda (\{ \texttt{y}_i := Y_i\}_{1 \le i \le n},(A_{j,k} + \mathfrak {i}B_{j,k})_{1 \le j,k \le 2^m}).\ P ,\end{aligned}$$
(7)

where variables \(Y_{i}\) refer to the classical, and variables \(A_{j,k}\) and \(B_{j,k}\) refer to the real part and imaginary part, respectively, of each algebraic coefficient in the quantum state. Further, \(P\in \mathbb {A}[Y_1,\ldots ,Y_n,A_{1,1},\ldots ,A_{2^m,2^m},B_{1,1},\ldots ,B_{2^m,2^m}]\) is a multivariate polynomial with coefficients in \(\mathbb {A}\). The index d refers to the (total) degree of the underlying polynomial P. For instance,

$$ \lambda (\{\texttt{x}:= X; \texttt{i}:= I\}, \left( {\begin{matrix}A_{1,1}+\mathfrak {i}B_{1,1} &{} A_{1,2}+\mathfrak {i}B_{1,2} \\ A_{2,1}+\mathfrak {i}B_{2,1} &{} A_{2,2}+\mathfrak {i}B_{2,2}\end{matrix}}\right) \!). \ I + X ( 2 - (A_{1,2} + A_{2,1})) \in \mathbb {A}^2[\texttt{St}_{\texttt{CT}}] $$

One important remark here is that we allow for possibly negative polynomials whereas expectations only output positive real algebraic numbers. Consequently, some side conditions are put on the admissible coefficients \(A_{j,k}\) and \(B_{j,k}\) of the input density matrix to preserve this condition (the matrix is positive, has trace 1, is hermitian). For example, \(\sum _{i=1}^{2^m}A_{i,i}=1\), \(\sum _{i=1}^{2^m}B_{i,i}=0\) (trace is 1) and \(\forall i,\ k,\ A_{i,k}=A_{k,i}\) and \(B_{i,k}=-B_{k,i}\) (self-adjointness). One can easily check that the expectations defined in Example 5 are in \(\mathbb {A}^d[\texttt{St}_{\texttt{CT}}]\), for \(d \ge 1\).

The restriction to polynomials is made on purpose, as quantifier elimination is decidable in the theory of real closed fields, a well known result due to Tarski and Seidenberg. Recall that the theory of real closed fields is the first-order theory in which the primitive operations are multiplication, addition, the order relation \(\le \), and the constants 0 and 1. Consequently, the only numbers that can be defined are the real algebraic numbers. Specifically, we will make use of the following result, quantifying the complexity of the quantifier elimination decision procedure as a function exponential in number of variables, and double-exponential in the number of quantifier alternations.

Proposition 1

([21, Theorem 6]). Let \(\textbf{A}\) be an integral ring over a real closed field \(\textbf{R}\). Let \(\psi = Q_1 \vec{x}_1. Q_{2}\vec{x}_2. \cdots Q_{l} \vec{x}_l.\ \phi \) be a formula in prenex-normal form, where \(\forall k,\ Q_k \in \{\forall ,\exists \}\), \(Q_k \ne Q_{k+1}\), and \(\phi \) is a quantifier-free formula over i variables and j atomic propositions of the shape \(P \ge 0\), each P being a polynomial of degree at most d with coefficients in \(\textbf{A}\). There exists an algorithm computing a quantifier-free formula equivalent to \(\psi \) in time \(O(|\psi |) \cdot (jd)^{i^{O(l)}}\).

As \(\mathbb {A}\) constitutes both an integral ring and a real closed field, the above theorem is in particular applicable taking \(\textbf{A} = \textbf{R} \triangleq \mathbb {A}\). In the particular case where \(\psi \) is a closed formula, the resulting quantifier-free formula is simply a Boolean combination of inequalities over constants from \(\mathbb {A}\). Since we already observed that these can be decided in polynomial time, the above proposition thus implies that validity of \(\psi \) is decidable under the given time bound.

By restricting assignment \(\alpha \) to polynomial expectations, it becomes decidable to check that \(\alpha \) is a solution to a given constraint set C. Indeed, under such a polynomial assignment \(\alpha \), a constraint \(\Gamma \vdash F \le G\) becomes expressible as a formula in the theory of real closed field \(\mathbb {A}\). By letting \(\alpha \) range over polynomial expectations with undetermined coefficients, we can this way arrive at the main decidability result of this section.

Theorem 4

For any degree \(d \in \mathbb {N}\), \(d\ge 1\), the problem \(\textsc {Qinfer}(\mathbb {A}^d[\texttt{St}_{\texttt{CT}}])\) is decidable in time \(2^{2^{dO(n)}}\), where n is the size of the considered program.

Practical Algorithm. Theorem 4 established a computable algorithm on the inference of upper bounds on weakest pre-expectation on quantitative program properties of any given mixed classical-quantum program. Nevertheless, the complexity of this algorithm — double-exponential in the program size — is forbiddingly high. In order to turn this procedure into a practical algorithm, we have to tame this inherent complexity. For this, significant further restrictions on the class of bounding functions are necessary. We propose to proceed as follows. (1) Bounding functions: in (7) we restricted the class of expectations to polynomials, which in turn yield a bound on the weakest pre-expectation. Based on an analysis of concrete examples considered in the literature (e.g., [6, 30]), this can be tightened further to degree 2 polynomials. (2) Approximate solutions: Theorem 4 rests upon (the decidability) of quantifier elimination. Thus the constraints C induced through the symbolic inference of \(\texttt{qinf}\!\left[ \,\texttt{stm}\,\right] ^{}\!\left\{ \textstyle X\right\} = G\) (\(G,X \in \textsf{ETerm}\)) are solved exactly. Over-approximation, however, suffices, if we are only interested in soundness of the inference mechanism.

The restriction of the class of bounding functions is in essence a question of applicability of the automation, taking into account particular use-cases. With respect to approximate solutions, we observe that the actual constraints C considered have at most one quantifier alternation and admit a quantifier prenex of the form \(\exists ^*\forall ^*\), that is, a sequence of existential quantifier follows by a sequence of universal quantifiers. Roughly speaking the existential quantifiers refer to the inference of coefficients in the bounding polynomials, while the universal quantifiers refer to program variables. It is well-known that universal quantification in optimization problems can be turned into existential quantification, like Farka’s lemma or generalizations thereof, cf. [19, 38]. (E.g., [7, 29] for instances of this approach for the inference of expected program costs.)

Summarizing, the inference mechanism detailed in Section 5 can be over-approximated to generate purely existential constraints. The latter can be effectively solved via SMT. We expect that (full) automation of the inference mechanism can captialize on these ideas. Working out the details and in particular implementation of an effective prototype is subject to future work.

6 Conclusion and Future Work

We have studied the complexity and inference of techniques for obtaining qualitative program properties. One particular property of interest would be the cost of quantum programs, that is average time, average number of gates, mean value of a variable, etc. We show that these problems were undecidable in general by placing them in the arithmetic hierarchy and saw that inference could become decidable on a restricted fragment: quantum gates in Clifford+T and a function space with a decidable theory (polynomials of bounded degree over a real closed field). Further, we sketch how the latter can be transformed into an efficient synthesis method.

Many open questions remain. The studied notion of expectation transformer describes local properties of the quantum state, while it would be interesting to extend this technique to the global state so as to study a mixed state in a quantum-only setting (without classical variables and stores). Another question of interest is to what extent a characterization of the quantum class \(\textsc {zbqp}\), the class of problems computed by quantum programs in polynomial expected runtime, could be obtained using this tool.