Bayesian Inference in Quantum Programs
Abstract
Conditioning is a key feature in probabilistic programming to enable modeling the influence of data (also known as observations) to the probability distribution described by such programs. Determining the posterior distribution is also known as Bayesian inference. This paper equips a quantum while-language with conditioning, defines its denotational and operational semantics over infinite-dimensional Hilbert spaces, and shows their equivalence. We provide sufficient conditions for the existence of weakest (liberal) precondition-transformers and derive inductive characterizations of these transformers. It is shown how w(l)p-transformers can be used to assess the effect of Bayesian inference on (possibly diverging) quantum programs.
Keywords and phrases:
Quantum Program Logics, Weakest Preconditions, Bayesian Inference, Program SemanticsCategory:
Track B: Automata, Logic, Semantics, and Theory of ProgrammingCopyright and License:
![[Uncaptioned image]](x1.png)
2012 ACM Subject Classification:
Theory of computation Quantum information theory ; Theory of computation Logic and verification ; Theory of computation Program semanticsFunding:
This work is supported by the ERC Grant 819317 CerQuS and the Interdisciplinary Doctoral Program in Quantum Systems Integration funded by the BMW group.Editors:
Keren Censor-Hillel, Fabrizio Grandoni, Joël Ouaknine, and Gabriele PuppisSeries and Publisher:

1 Introduction
Quantum verification is a important part of the rapidly evolving field of quantum computing and information. The importance comes from several factors. Firstly, quantum computers operate in a completely different way than classical computers do. Principles of quantum mechanics are important to algorithm designers but in general unintuitive to most people. This leads to a higher risk of introducing logical errors. Secondly, quantum algorithms are often used in safely critical areas such as cryptography and optimization where those mistakes can lead to serious issues. Classical testing and debugging methods do not directly apply to quantum computing. Testing on quantum computers is challenging due to high execution costs, probabilistic outcomes, and noise from environmental interactions. While simulators help, they have limitations such as scalability. Debugging is also difficult, as measuring quantum variables alters their state, preventing traditional inspection methods.
Testing only verifies specific inputs without guaranteeing overall correctness, whereas formal verification ensures correctness for all inputs. Weakest preconditions define input states that ensure a given postcondition holds after execution. Inspired by the importance of conditioning and Bayesian inference in probabilistic programs, we extend the calculus from [7, 27] to incorporate “observations”. Combining weakest preconditions for total correctness and weakest liberal preconditions for partial correctness, we determine whether a predicate holds assuming all observations hold, i.e, compute a conditional probability.
This new statement could be used to aid in debugging to locate logical mistakes. Assume having a theoretical algorithm and a wrong implementation. To figure out which parts are wrong, fixing variable values by observations can help identify errors by comparing implementation samples with the expected distribution. Similarly, given a complex (possibly wrong) algorithm, adding observations can help understanding parts of the algorithm by comparing it to its intuitive understanding. For instance, in a random walk algorithm with a random starting point, analyzing success probability from a “good” starting point can help to understand the algorithm. Unlike traditional assertions, observations can be useful even when they don’t always hold. Another possible application is error correction, where outputs are often analysed assuming no more than qubit errors occurred per step to ensure successful error correction.
Related Work
The general idea of weakest preconditions was first developed by Dijkstra for classical programs [10, 9], then for probabilistic programs [16, 20] and later for quantum programs [7]. D’Hondt and Panangaden [7] defined predicates as positive operators as we do and focused on total correctness and finite-dimensional Hilbert spaces. [27] extended this approach to partial correctness and gave an explicit representation of the predicate transformer for the quantum while-language. An alternative to define predicates is to use projections [29]. There have been several extensions like adding classical variables [6, 13] or non-determinism [12].
A runtime assertion scheme using projective predicates for testing and debugging has been introduced in [18]. In contrast, our approach enables debugging, but in addition provides formal guarantees on the correctness based on the satisfaction of assertions and allows infinite-dimensional Hilbert spaces. A survey about studies and approaches of debugging of quantum programs is given in [8]. Another idea to locate bugs is to use incorrectness logic with projective predicates [26]. The idea of conditional weakest preconditions has been introduced in [22, 23] for probabilistic programs.
The concept of choosing specific measurement outcomes is also known as postselection. [1] shows that the class of problems solvable by quantum programs with postselection in polynomial time, called Postselected Bounded-Error Quantum Polynomial-Time (PostBQP), is the same as the ones in the complexity class Probabilistic Polynomial-Time (PP). This equivalence is shown by solving a representative PP-complete problem, MAJ-SAT, using a quantum program with postselection. We confirm the correctness of this program in Section 5 by using conditional weakest preconditions.
Main Contributions
-
Conditional weakest-precondition transformers: We define a weakest precondition calculus for reasoning about programs with an “observe” statement. The conditional weakest precondition, defined in terms of weakest (liberal) preconditions transformers, reveals the probability of a postcondition given all observations succeed.
The definition of the transformers is semantic, i.e., formulated in a generic way based on the denotational semantics and not tied to a specific syntax of programs (but we also give explicit rules for our syntax by recursion over the structure of a program).
-
Semantics: We develop both denotational and operational semantics of a simple quantum while-language with “observe” statements and show their equivalence.
-
Our definition of weakest (liberal) preconditions is a conservative extension of [27], supporting “observe” statements. Further differences include: Our definition is semantic and we support infinite-dimensional quantum systems (e.g., to support quantum integers)111Notice that [27] also defines a language with quantum integers. However, they do not explicitly specify the various notions of convergence of operators (e.g., operator topologies, convergence of infinite sums, existence of suprema), making it difficult to verify whether their rules are sound in the infinite-dimensional case..
Structure
We first recall important definitions in Section 2. The main contributions are in Section 3 and Section 4: Section 3 introduces the “observe” statement and its semantics whereas Section 4 defines weakest (liberal) preconditions and finally conditional weakest (liberal) preconditions. Two examples in Section 5 illustrate our approach, followed by conclusions in Section 6.
2 Preliminaries
2.1 Hilbert Spaces
Let denote the inner product over a vector space . The norm (or length) of a vector , denoted , is defined as . The vector is called a unit vector if . Vectors are orthogonal () if . The sequence of vectors is a Cauchy sequence, if for any , there exists a positive integer such that for all . If for any , there exists a positive integer such that for all , then is the limit of , denoted .
A family of vectors in is summable with the sum if for every there exists a finite such that for every finite and .
A Hilbert space is a complete inner product space, i.e, every Cauchy sequence of vectors in has a limit [27]. An orthonormal basis of a Hilbert space is a (possibly infinite) family of unit vectors if they are pairwise orthogonal (i.e., for ) and every can be written as (in the sense above). The cardinality of , denoted , is the dimension of . Hilbert spaces and its elements can be combined using the tensor product [24, Def. IV.1.2].
We use Dirac notation to denote vectors of a vector space where is the dual vector of [11], i.e., .
Example 1.
A typical Hilbert space over the set is
where the inner product is defined as . By we denote the complex conjugate of . An orthonormal basis, also called computational basis, is . For (countably) infinite sets , the basis is (countably) infinite and thus is a (countably) infinite Hilbert space. can be used for quantum integers and is also denoted by . For qubits, we use and denote it as .
2.2 Operators
In the following, all vector spaces will be over . For vector spaces , a function is called linear if for and . If are normed vector spaces then is called bounded linear if is linear and for some constant for all . If is a Hilbert space, we call bounded linear functions on operators. Let denote the space of all operators on and the result of applying operator to . For this work, we additionally generalize the notion of linearity to functions that are defined on subsets of the vector space: For (normed) vector spaces with and , we call (bounded) linear iff there exists a (bounded) linear function such that for . includes all finite linear combinations of .
Let and be operators on and with . By [24, Def. IV.1.3], the tensor product is the unique operator that satisfies For matrices, the tensor product is also called the Kronecker product.
For every operator on , there exists an operator on with for all . An operator on is called positive if for all states [21]. The identity operator on is defined by . The zero operator on , denoted by , maps every vector to the zero vector. We omit if it is clear from the context. An unitary operator is an operator such that its inverse is its adjoint , i.e., and [17]. An (ortho)projector is an operator such that . For every closed subspace , there exists a projector with image [5, Prop. II.3.2 (b)].
An operator is a trace class operator if there exists an orthonormal basis such that is summable where is the unique positive operator with . Then the trace of is defined as where is an orthonormal basis. For a trace class operator , it can be shown that is independent of the chosen base [27]. The trace is cyclic, i.e., [25], linear, i.e., , scalar, i.e., for a constant [4] and multiplicative, i.e., holds [25] for trace class operators . We use to denote the space of trace class operators on . Positive trace class operators with are called partial density operators. The set of partial density operators is denoted with . Density operators are partial density operators with . They are denoted as . The support of a partial density operator is the smallest closed subspace such that .
Let us consider some properties of functions that map operators to operators. with is trace-reducing if for all positive . is positive if is positive for positive and subunital if and , where is defined just below.
2.2.1 The Loewner Partial Order
To order operators, the Loewner partial order is used. For any operators , it is defined by iff is a positive operator. This is equivalent to for all partial density operators [27]. The Loewner order is compatible w.r.t. addition (also known as monotonic), i.e., implies for any , and w.r.t. multiplication of non-negative scalars, i.e., implies for [3].
Using this order, we can define predicates [7]. A quantum predicate on a Hilbert space is defined as an operator on with . The set of quantum predicates on is denoted by and .
The Loewner partial order is an -complete partial order (-cpo) on the set of partial density operators [28]. Thus each increasing sequence of partial density operators has a least upper bound. This also holds for the set of predicates [7].
An important property that we need is continuity of the trace operator. First of all, we note that the trace-operator is order-continuous on partial density operators with respect to , i.e., for any increasing sequence of partial density operators . Without going further into details, this holds because for an increasing sequence of real numbers, the least upper bound and the limit coincide, the same also holds for partial density operators [25] and because the trace is linear and bounded, it is also trace-norm continuous. Continuity w.r.t. predicates means for every and increasing sequence of predicates . Without going into further detail, we can show that a function defined by for a fixed is weak∗-continuous and convergence of positive bounded operators in the weak∗-topology coincides with the supremum [25]. Similar, the same property holds for decreasing sequences of predicates and the greatest lower bound .
2.3 Quantum-specific Preliminaries
Due to a postulate of quantum mechanics, the state space of an isolated quantum system can be described as a Hilbert space where states correspond to unit vectors (up to a phase shift) in its state space [27]. A quantum state is called pure if it can be described by a vector in the Hilbert space; otherwise mixed, i.e., it is a probabilistic distribution over pure states. We use partial density operators to describe mixed states, in particular to capture the current state of a program. If a quantum system is in a pure state with probability (with ), then this is represented by the partial density operator .
To obtain the current value of e.g. a quantum variable, we cannot simply look at it. In quantum mechanics, each measurement can impact the current state of a qubit.
A measurement is a (possible infinite) family of operators where is the measurement outcome and 111As in [25], we mean convergence of sums with respect to SOT (strong operator topology) which is the topology where holds iff for all : [5, Prop. IX.1.3(c)].. If the quantum system is in state before the measurement , then the probability for result is and the post-measurement state is . An important kind of measurement is the projective measurement. It is a set of projections over with . An important property of projective measurements is that if a state is measured by a projective measurement and holds, then is not changed.
2.4 Markov Chains
A Markov chain (MC) is a tuple where
-
is a nonempty (possibly uncountable) set of states,
-
with is the transition probability function. Let denote .
-
is the initial state.
Note that in comparison to [2, 23], can be uncountable. However, in our setting the reachable set of states will be countable as every state can only have a countable number of successor states with . Therefore, even if is uncountable, the set of reachable states is countable and all results from [2, 23] still apply.
A path of a MC is an infinite sequence with and for all . We use to denote the set of paths in and for the finite path prefixes. If it is clear from the context, we omit . The probability distribution on is defined using cylinder sets as in [2]. In a slight abuse of notation, we write for for where denotes the cylinder set of . We write where is the probability to reach from . Given a target set of reachable states , let be the (measurable) set of infinite paths that reach the target set . The probability of reaching is . Analogously, let be the set of paths that never reach ; .
3 Quantum Programs with Observations
We assume to be a finite set of quantum variables with two types: Boolean and integer. As in [27], the corresponding Hilbert spaces are
Each variable has a type . Its state space is if and otherwise. The state space of a quantum register is defined by the tensor product of state spaces of through .
3.1 Syntax
A quantum while-program has the following syntax:
where
-
is a quantum variable,
-
is a quantum register,
-
from statement is a unitary operator on and is the same on both sides,
-
in is a projection on
-
the measurement in is on and is a family of quantum programs where each corresponds to an outcome ,
-
the measurement in on has the form .
Our programs extend [27] with the new statement . We only allow projective predicates for observations. It is conceivable that it can also be based on more general predicates but it is not clear what the intuitive operational meaning of such would be, so we choose to pursue the simpler case.
We use as syntactic sugar for a measurement statement with and .
By we denote syntactic equality of quantum programs. We use to denote the set of variables occurring in program . The Hilbert space of is denoted by . If the set of variables is clear from the context, we just write .
3.2 Semantics
In this section, we define an operational and denotational semantics for quantum while-programs with observations and show their equivalence.
3.2.1 Operational Semantics
We start by defining the operational semantics of a program as a Markov chain inspired by [23] instead of non-deterministic relations in comparison to [27]. A quantum configuration is a tuple with density operator . Note that we consider normalized density operators instead of partial density operators . Intuitively, is the program that is left to evaluate and is the current state. We use to denote that there is no program left to evaluate. The set of all configurations over is denoted as . The quantum configuration for violated observations is and for termination is .
Definition 2.
The operational semantics of a program with initial state is defined as the Markov chain where:
-
,
-
,
-
P is the smallest function satisfying the inference rules in Figure 1 where means . For all other pairs of states the transition probability is .
The meaning of a transition is that after evaluating program on state , with probability the new state is and the program left to execute is . For the observe statement, there are two successors, and . The observation is satisfied by state with probability and then it terminates successfully. If the observation is violated (with probability ), the successor state is , the state that captures paths with violated observations. For details of the other rules we refer to [27].
3.2.2 Denotational Semantics
We now provide a denotational semantics for quantum while-programs. To handle observations and distinguish between non-terminating runs and those that violate observations, we introduce denotational semantics in a slightly different way than [27]. To do so, we start with defining some basics:
For tuples , we define multiplication with a constant and addition entrywise: and .
The least upper bound (lub) of a set of tuples is defined as the entrywise lub provided it exists, i.e., where is the lub w.r.t. the Loewner partial order and is the lub w.r.t. to the classical ordering on .
As the probability of violating observations depends on the density operator, we introduce
is isomorphic to the set of operators of the form . Thus the trace and the norm from apply. Specifically, and for .
Definition 3.
The denotational semantics of a quantum program is defined as a mapping . For , is used for density-transformer semantics as defined in [27] and for the probability of an observation violation.
The denotational semantics for is given by
-
.
-
-
.
-
.
-
.
-
with and .
-
with where loop unfoldings are defined inductively
where is a syntactic quantum program with as in [27].
We write and to denote the first/second component of . It follows directly that our definition is a conservative extension of [27]:
Proposition 4.
For an observe-free program , input state and , is where is the denotational semantics as defined in [27].
Some intuition behind those tuples: If for a program with initial pair , then the probability of violating an observation while executing on is . The probability of terminating normally (without violating an observation) is given by and the probability for non-termination is . As in the observe-free case, is the (non-normalized) state after has been executed (and terminated) on . It is easy to see that only the observation statement can change the value of the second entry.
Proposition 5.
For and program :
-
1.
-
2.
-
3.
if then
-
4.
-
5.
is well defined, i.e., and the least upper bound exists.
-
6.
is linear
Proof.
All claims can be shown by doing an induction over , see [14]. As , we use instead. Three consequences of Proposition 5:
Lemma 6.
For with and programs :
-
1.
, i.e., is trace-reducing
-
2.
-
3.
is bounded linear
The proof can be found in [14].
3.2.3 Equivalence of Semantics
The following lemma asserts the equivalence of our operational and denotational semantics. Intuitively, the denotational semantics gives a distribution over final states and its second component captures the probability to reach , the state for violated observations. As the operational semantics is only defined for , we only consider this case:
Lemma 7.
For any program and initial state
Proof.
The first item can be shown by induction over . For the second item we use that every path that eventually reaches passes through either a or a state. For more details on both proofs, see [14].
4 Weakest Preconditions
In this section, we consider how we can extend the weakest precondition calculus to capture observations and thus compute conditional probabilities of quantum programs using deductive verification. Recall that a predicate satisfies . Let be the probability that satisfies . Note that if is a projector, then equals the probability that gives answer “yes” in a measurement defined by . Even if is not a projection, is the average value of measuring with the measurement described by the observable . If not given directly, all proofs can be found in [14].
4.1 Total and Partial Correctness
Defining the semantics in a different way also changes the definition of Hoare logic with total and partial correctness [27]:
Definition 8.
Let , a program, and a correctness formula. Then
-
1.
(total correctness) iff
-
2.
(partial correctness) iff
Let us explain this definition. Assume , otherwise all probabilities mentioned in the following are non-normalized. Recall that is the probability that state satisfies predicate and is the probability that the state after execution of starting with satisfies predicate . Total correctness entails that the probability of a state satisfying precondition is at most the probability that it satisfies postcondition after execution of . This only involves terminating runs. In the formula of partial correctness, the summand captures the probability that an observation is violated during executing program on state . As before, captures the probability that on state does not terminate.
Similar to [27], we have some nice but different properties:
Proposition 9.
-
1.
implies
-
2.
. However, does not hold in general.
-
3.
For and with : implies
Proof.
-
1.
Follows from definition and (Proposition 5)
-
2.
follows from definition, and for all . For disproving , consider a program (with only one variable ) . Then the statement does not holds with and because .
-
3.
Follows from the linearity of the trace and the definition of .
4.2 Weakest (Liberal) Preconditions
Given a postcondition and a program, we are interested in the best (weakest) precondition w.r.t. total and partial correctness:
Definition 10.
Let program and predicate .
-
1.
The weakest precondition is defined as . Thus and implies for all .
-
2.
The weakest liberal precondition is defined as . Thus and implies for all .
The following lemmas show that these suprema indeed exist. Both proofs are based on the Schrödinger-Heisenberg duality [25].
Lemma 11.
For a function with properties as in Proposition 5, the weakest precondition exists and is bounded linear and subunital. It satisfies for all and it is the only function of this type with this property.
This lemma (and the following one) does not require to be a denotational semantics of some program . In contrast to [27], this result thus still holds if the language is extended as long as the conditions still holds.
Lemma 12.
For a function with properties as in Proposition 5, the weakest liberal precondition exists and is subunital. It satisfies
for each and it is the only function of this type with this property.
This general theorem about the existence of weakest liberal preconditions also applies for programs without observations (because and for each and for each observation-free program , Proposition 4). Lemma 11 and 12 extend [7] to the infinite-dimensional case and to partial correctness, i.e., the existence of weakest liberal preconditions.
Now we consider some healthiness properties about weakest (liberal) preconditions:
Proposition 13.
For every program , the function satisfies:
-
Bounded linearity
-
Subunitality:
-
Monotonicity: implies
-
Order-continuity: if exists
Proposition 14.
For every program , the function satisfies:
-
Affinity: The function with is linear. Note that this implies convex-linearity and sublinearity.
-
Subunitality:
-
Monotonicity: implies
-
Order-continuity: if exists
For our denotational semantics , we can also give an explicit representation of :
Proposition 15.
Let :
-
-
-
-
-
-
-
with
and denoting the least upper bound w.r.t. .
Proof.
We prove , which then, together with Lemma 11 implies that it is indeed the weakest precondition. In this and the following proposition we mean convergence of sums with respect to the SOT, more details can be found in [25].
For weakest liberal preconditions the explicit representation looks quite similar:
Proposition 16.
Let . For most cases, is defined analogous to (replacing every occurrence of by ). The only significant difference is the while-loop: with
and denoting the greatest lower bound w.r.t. .
Proof.
This proof is similar to Proposition 15 except that we show that holds which then implies together with Lemma 12 that it is indeed the weakest liberal precondition. Both explicit representations above are conservative extensions of the weakest (liberal) precondition calculus in [27].
For the following explanations, assume , otherwise the probabilities are not normalized. To understand those definitions, consider . Due to the duality from Lemma 11, , so it is the probability that the result of running program (without violating any observations) on the initial state satisfies predicate . Similarly, adds the probability of non-termination too. This is equivalent to the standard interpretation of weakest (liberal) preconditions in [27].
For programs with observations does not include runs that violate an observation. Thus, gives the probability that no observation is violated during the run of on input state (while for programs without observations and in [27], this will always be ). The probability that a program state will satisfy the postcondition after executing program while not violating any observation is then a conditional probability. To handle this case, we introduce conditional weakest preconditions inspired by [23] in the next section.
4.3 Conditional Weakest Preconditions
In the following, we consider pairs of predicates. Addition and multiplication are interpreted pointwise, i.e., and resp. where can be a constant or an operator. Multiplication binds stronger than addition.
We define a natural ordering on pairs of predicates that is used for example to express healthiness conditions:
Definition 17.
We define on as follows: where is the Loewner partial order. The least element is and the greatest element . The least upper bound of an increasing chain for is given pointwise by .
Lemma 18.
is an -cpo on .
Proof.
We have to show that exists for any increasing chain . If is increasing with respect to , then is increasing and decreasing with respect to . The existence of follows directly from being an -cpo on . For , we use the same trick as in the proof of Proposition 16: is an increasing chain of predicates and thus exists too. That means both and exist and thus also . Combining weakest preconditions and liberal weakest preconditions, we can define conditional weakest preconditions similar to the probabilistic case [23]:
Definition 19.
The conditional weakest precondition transformer is a mapping defined as .
Similar to the weakest precondition calculus, we can also give an explicit representation which can be found in [14].
Again, assume in the following, otherwise the probabilities are not normalized. Note that is the probability that no observation is violated and the probability that is satisfied after has been executed on (see above). We are interested in the conditional probability of establishing the postcondition given that no observation is violated, namely . Notice that for , this is simply . That means we can immediately read of this conditional probability from . Formally, we use
We now establish some properties of conditional weakest preconditions:
Proposition 20.
For every program , the function satisfies:
-
Has a linear interpretation: for all and with
-
Affinity: The function is linear. Note that this implies convex-linearity and sublinearity.
-
Monotonicity: implies
-
Continuity: if exists
4.4 Conditional Weakest Liberal Preconditions
Similar to the conditional weakest precondition, we can also define the same with weakest liberal preconditions for partial correctness:
Definition 21.
The conditional weakest liberal precondition is defined as for each program and predicates .
Definition 22.
We define on as follows where is the Loewner partial order. The least element is and the greatest element . The least upper bound of an increasing chain for is given pointwise by .
Note that in contrast to , both components are ordered in the same direction. Here it follows directly that is an -cpo on .
Similar as for , we can now read off the conditional satisfaction of when we want non-termination to count as satisfaction: which is equal to dividing the probability to satisfy after execution (including non-termination) by the probability to not violate an observation111So far, we considered conditional weakest preconditions for total and partial correctness, i.e., and . In [15, Sect. 8.3] it is argued why other combinations such as and only make sense if a program is almost-surely terminating, i.e., without non-termination. Their arguments apply without change in our setting, so we do not consider these combinations either..
We can also conclude some properties about conditional weakest liberal preconditions:
Proposition 23.
For every program , the function satisfies:
-
Affinity: The function is linear. Note that this implies convex-linearity and sublinearity.
-
Monotonicity: implies
-
Continuity: if
4.5 Observation-Free Programs
For observation-free programs, our interpretations coincides with the satisfaction of weakest (liberal) preconditions from [27]:
Lemma 24.
For an observation-free program , predicate and state :
Proof.
For every observation-free program is [27] and which means for (and not for all ) is equal to
4.6 Correspondence to Operational MC Semantics
The aim of this section is to establish a correspondence between and the operational semantics of . In order to reason about in terminal states of the Markov chain, we use rewards. First, we equip the Markov chain used for the operational semantics with a reward function with regard to a postcondition :
Definition 25.
For program and postcondition , the Markov reward chain is the MC extended with a function such that and for all other states .
The (liberal) reward of a path of is defined as and expect if , then .
The expected reward of is the expected value of for all , i.e., . The liberal version adds rewards of non-terminating paths, i.e., .
Now we start by showing some auxiliary results, similar to [23, Lemma 5.5, 5.6]:
Lemma 26.
For a program , state , predicate we have
Proof.
Follows from Lemma 7, Lemma 11 and Lemma 12. We are interested in the conditional (liberal) expected reward of reaching from the initial state , conditioned on not visiting :
This reward is equivalent to our interpretation of , analogous to [23, Theorem 5.7]:
Theorem 27.
For a program , state , predicates we have
Proof.
Assuming , then is equal to
by Lemma 26. If , then is undefined which means both sides of the statement are undefined and thus equal.
5 Examples
In this section we provide two examples on how conditional quantum weakest preconditions can be applied.
5.1 The Quantum Fast-Dice-Roller
In probabilistic programs, generating a uniform distribution using fair coins is a challenge. The fast dice roller efficiently simulates the throw of a fair dice, generating a uniform distribution about possible outcomes [19]. We solve this problem for with quantum gates by creating qubits with Hadamard gates and using the observe statement to reject the case, leaving possible outcomes (), see Figure 2.
The Markov chain representing the operational semantics can be found in [14]. To prove correctness, we focus on the probability of termination and reaching the desired state without violating the observation. This probability cannot be directly read from the operational semantics, even for this simple program. To specify this property formally, we use the reward MC as defined in Definition 25. The desired probability can be computed using conditional weakest preconditions, see Theorem 27.
To terminate in a state where the probability of all six outcomes is equal and forms a distribution, we verify that we reach the uniform superposition
over states. Measuring in the computational basis yields a uniform distribution. After computing the conditional weakest precondition, we can determine the likelihood of each input state reaching the fixed uniform superposition and producing a uniform distribution, assuming the observation is not violated. We use the decoupling of and compute and separately where and stands for our fast-dice roller program (Figure 2). The probability that an input state will reach the desired uniform superposition is , that is
will reach the desired superposition with probability assuming no observation is violated. We can also see that so even with the “best” input, our conditional weakest precondition calculus gives more information than .
5.2 MAJ-SAT
To demonstrate our approach, we will verify the correctness of a program that is used to solve MAJ-SAT. Unlike SAT, which asks whether there exists at least one satisfying assignment of a Boolean formula, MAJ-SAT asks whether a Boolean formula is satisfied by at least half of all possible variable assignments. MAJ-SAT is known to be PP-complete and [1] uses it to prove the equivalence of the complexity classes PostBQP and PP.
Formally, we are faced with the following problem: A formula with variables can be represented by a function with . The goal is to determine whether holds or not. Aaronson [1] presents a PostBQP algorithm for this problem. A PostBQP algorithm is one that runs in polynomial time, is allowed to perform measurements to check whether certain conditions are satisfied (analogous to our statement) and is required to produce the correct result with high probability conditioned on those measurements succeeding. The algorithm from [1] is as follows:
where is given in Figure 3 and succeeding means that measuring in the basis returns . The core idea is to show that succeeds with probability for all if and with probability for at least one otherwise. Hence the overall algorithm solves MAJ-SAT. To keep this example manageable, we focus on the analysis of alone.
We use conditional weakest preconditions and determine (which depends on the parameters ). Here the postcondition corresponds to being in state , formally . Then the probability that succeeds is for initial state .
We computed the cwp symbolically using a computer algebra system, but the resulting formulas were quite unreadable. So for the sake of this example, we present numerical results of computing cwp instead. Since does not contain any loops, the cwp can be computed by mechanic application of the rules for observation, assignment, and application of unitaries. Performing these calculations for selected values of and and all , we find that in each case the cwp is of the form for some . This is to be expected since all variables are initialized at the beginning of the program, so the cwp should not depend on the initial state, i.e., all matrices should be multiples of the identity. In that case, . In Figure 4, we show for selected . (The claim from [1] is that the success probability of is for some if and for all otherwise, so we only care about the maximum over all .) We see that is indeed and in those two cases. This confirms the calculation from [1], using our logic. (At least for the values of we computed.)
… | ||||||
… | ||||||
… | ||||||
… | ||||||
… | … | … | … | … | … |
6 Conclusion
We introduced the observe statement in the quantum setting for infinite-dimensional cases, supported by operational, denotational and weakest precondition semantics. We defined conditional weakest preconditions, proved their equivalence to the operational semantics and applied them to an example using Bayesian inference. Future work includes the interpretation of predicates and exploration of alternatives to observe statements such as rejection sampling or hoisting in the probabilistic case. Additionally, the challenge of combining non-determinism with conditioning in probabilistic systems [23] may extend to quantum programs.
References
- [1] Scott Aaronson. Quantum computing, postselection, and probabilistic polynomial-time. Proceedings of the Royal Society A: Mathematical, Physical and Engineering Sciences, 461, September 2005. doi:10.1098/rspa.2005.1546.
- [2] Christel Baier and Joost-Pieter Katoen. Principles of model checking. MIT Press, 2008.
- [3] Stephen Boyd and Lieven Vandenberghe. Convex Optimization. Cambridge University Press, 2004.
- [4] John B. Conway. A Course in Operator Theory. Graduate Studies in Mathematics. American Mathematical Society, 2000.
- [5] John B. Conway. A Course in Functional Analysis. Graduate Texts in Mathematics. Springer New York, 2007. doi:10.1007/978-1-4757-4383-8.
- [6] Yuxin Deng and Yuan Feng. Formal semantics of a classical-quantum language. Theoretical Computer Science, 913:73–93, 2022. doi:10.1016/j.tcs.2022.02.017.
- [7] Ellie D’Hondt and Prakash Panangaden. Quantum weakest preconditions. Math. Struct. Comput. Sci., 16(3):429–451, 2006. doi:10.1017/S0960129506005251.
- [8] Olivia Di Matteo. On the need for effective tools for debugging quantum programs. In Proceedings of the 5th ACM/IEEE International Workshop on Quantum Software Engineering, Q-SE 2024, pages 17–20, New York, NY, USA, 2024. Association for Computing Machinery. doi:10.1145/3643667.3648226.
- [9] Edsger W. Dijkstra. Guarded commands, nondeterminacy and formal derivation of programs. Commun. ACM, 18(8):453–457, 1975. doi:10.1145/360933.360975.
- [10] Edsger W. Dijkstra. A Discipline of Programming. Prentice-Hall, 1976.
- [11] Paul A. M. Dirac. The Principles of Quantum Mechanics. Clarendon Press, Oxford, 1930.
- [12] Yuan Feng and Yingte Xu. Verification of nondeterministic quantum programs. In Proceedings of the 28th ACM International Conference on Architectural Support for Programming Languages and Operating Systems, Volume 3, ASPLOS 2023, pages 789–805, New York, NY, USA, 2023. Association for Computing Machinery. doi:10.1145/3582016.3582039.
- [13] Yuan Feng and Mingsheng Ying. Quantum Hoare logic with classical variables. ACM Transactions on Quantum Computing, 2(4), 2021. doi:10.1145/3456877.
- [14] Christina Gehnen, Dominique Unruh, and Joost-Pieter Katoen. Bayesian inference in quantum programs, 2025. arXiv:2504.20732.
- [15] Benjamin L. Kaminski. Advanced Weakest Precondition Calculi for Probabilistic Programs. PhD thesis, RWTH Aachen University, February 2019. doi:10.18154/RWTH-2019-01829.
- [16] Dexter Kozen. A probabilistic PDL. Journal of Computer and System Sciences, 30(2):162–178, 1985. doi:10.1016/0022-0000(85)90012-1.
- [17] Marco Lewis, Sadegh Soudjani, and Paolo Zuliani. Formal verification of quantum programs: Theory, tools, and challenges. ACM Transactions on Quantum Computing, 2023. doi:10.1145/3624483.
- [18] Gushu Li, Li Zhou, Nengkun Yu, Yufei Ding, Mingsheng Ying, and Yuan Xie. Projection-based runtime assertions for testing and debugging quantum programs. Proc. ACM Program. Lang., 4(OOPSLA), 2020. doi:10.1145/3428218.
- [19] Jérémie O. Lumbroso. Optimal discrete uniform generation from coin flips, and applications. CoRR, abs/1304.1916, 2013. arXiv:1304.1916.
- [20] Annabelle McIver and Carroll Morgan. Abstraction, Refinement and Proof for Probabilistic Systems. Monographs in Computer Science. Springer, 2005. doi:10.1007/b138392.
- [21] Michael A. Nielsen and Isaac L. Chuang. Quantum Computation and Quantum Information: 10th Anniversary Edition. Cambridge University Press, 2010. doi:10.1017/CBO9780511976667.
- [22] Aditya V. Nori, Chung-Kil Hur, Sriram K. Rajamani, and Selva Samuel. R2: an efficient mcmc sampler for probabilistic programs. In Proceedings of the Twenty-Eighth AAAI Conference on Artificial Intelligence, AAAI’14, pages 2476–2482. AAAI Press, 2014. doi:10.1609/AAAI.V28I1.9060.
- [23] Federico Olmedo, Friedrich Gretz, Nils Jansen, Benjamin L. Kaminski, Joost-Pieter Katoen, and Annabelle Mciver. Conditioning in probabilistic programming. ACM Trans. Program. Lang. Syst., 40(1), 2018. doi:10.1145/3156018.
- [24] Masamichi Takesaki. Theory of Operator Algebras I. Number Bd. 1 in Encyclopaedia of Mathematical Sciences. Springer New York, 1979. doi:10.1007/978-1-4612-6188-9.
- [25] Dominique Unruh. Quantum references, 2024. arXiv:2105.10914v3.
- [26] Peng Yan, Hanru Jiang, and Nengkun Yu. On incorrectness logic for quantum programs. Proc. ACM Program. Lang., 6(OOPSLA1), 2022. doi:10.1145/3527316.
- [27] Mingsheng Ying. Floyd-Hoare logic for quantum programs. ACM Trans. Program. Lang. Syst., 2012. doi:10.1145/2049706.2049708.
- [28] Mingsheng Ying, Runyao Duan, Yuan Feng, and Zhengfeng Ji. Predicate transformer semantics of quantum programs. Semantic Techniques in Quantum Computation, 2010. doi:10.1017/CBO9781139193313.009.
- [29] Li Zhou, Nengkun Yu, and Mingsheng Ying. An applied quantum Hoare logic. In Proceedings of the 40th ACM SIGPLAN Conference on Programming Language Design and Implementation, PLDI 2019, pages 1149–1162, New York, NY, USA, 2019. Association for Computing Machinery. doi:10.1145/3314221.3314584.