Abstract 1 Introduction 2 Preliminaries 3 Quantum Programs with Observations 4 Weakest Preconditions 5 Examples 6 Conclusion References

Bayesian Inference in Quantum Programs

Christina Gehnen ORCID RWTH Aachen University, Germany Dominique Unruh ORCID RWTH Aachen University, Germany
University of Tartu, Estonia
Joost-Pieter Katoen ORCID RWTH Aachen University, Germany
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 Semantics
Category:
Track B: Automata, Logic, Semantics, and Theory of Programming
Copyright and License:
[Uncaptioned image] © Christina Gehnen, Dominique Unruh, and Joost-Pieter Katoen; licensed under Creative Commons License CC-BY 4.0
2012 ACM Subject Classification:
Theory of computation Quantum information theory
; Theory of computation Logic and verification ; Theory of computation Program semantics
Related Version:
Full Version: https://arxiv.org/abs/2504.20732 [14]
Funding:
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 Puppis

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 t 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 u, denoted u, is defined as uu. The vector u is called a unit vector if u=1. Vectors u,v are orthogonal (uv) if uv=0. The sequence {ui}i of vectors ui𝒱 is a Cauchy sequence, if for any ϵ>0, there exists a positive integer N such that unum<ϵ for all n,mN. If for any ϵ>0, there exists a positive integer N such that unu<ϵ for all nN, then u is the limit of {ui}i, denoted u=limiui.

A family {ui}iI of vectors in 𝒱 is summable with the sum v=iIui if for every ϵ>0 there exists a finite JI such that viKui<ϵ for every finite KI and JK.

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 {ui}iI of unit vectors if they are pairwise orthogonal (i.e., uiuj for ij,i,jI) and every v can be written as v=iIuivui (in the sense above). The cardinality of I, denoted |I|, 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 X is

l2(X)={nXαn|nαn for all nX and nX|αn|2<}

where the inner product is defined as (nXαn|n,nXαn|n)=nXαn¯αn. By x+yi¯=xyi we denote the complex conjugate of x+yi. An orthonormal basis, also called computational basis, is {|nnX}. For (countably) infinite sets X, the basis is (countably) infinite and thus l2(X) is a (countably) infinite Hilbert space. l2() can be used for quantum integers and is also denoted by . For qubits, we use l2({0,1}) and denote it as 2.

2.2 Operators

In the following, all vector spaces will be over . For vector spaces 𝒱,𝒲, a function f:𝒱𝒲 is called linear if f(ax+y)=af(x)+f(y) for x,y𝒱 and a. If 𝒱,𝒲 are normed vector spaces then f is called bounded linear if f is linear and f(x)cx for some constant c0 for all x𝒱. If is a Hilbert space, we call bounded linear functions on operators. Let B() denote the space of all operators on and A|ϕ the result of applying operator A 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 S𝒱,T𝒲 with 𝑠𝑝𝑎𝑛(S)=𝒱 and 𝑠𝑝𝑎𝑛(T)=𝒲, we call f:ST (bounded) linear iff there exists a (bounded) linear function f¯:𝒱𝒲 such that f¯(s)=f(s) for sS. 𝑠𝑝𝑎𝑛(S) includes all finite linear combinations of S.

Let A and B be operators on 1 and 2 with |ϕ1,|ψ2. By [24, Def. IV.1.3], the tensor product AB is the unique operator that satisfies (AB)(|ϕ|ψ)=A|ϕB|ψ For matrices, the tensor product is also called the Kronecker product.

For every operator A on , there exists an operator A on with |ϕ,A|ψ=A|ϕ,|ψ for all |ϕ,|ψ. An operator A on is called positive if ψ|A|ψ0 for all states |ψ [21]. The identity operator I on is defined by I|ϕ=|ϕ. 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 U is an operator such that its inverse is its adjoint U1=U, i.e., UU=I and UU=I [17]. An (ortho)projector is an operator P: such that P2=P=P. For every closed subspace S, there exists a projector PS with image S [5, Prop. II.3.2 (b)].

An operator A is a trace class operator if there exists an orthonormal basis {|ψi}iI such that {ψi||A||ψi}iI is summable where |A| is the unique positive operator B with BB=AA. Then the trace of A is defined as tr(A)=iIψi|A|ψi where {|ψi}iI is an orthonormal basis. For a trace class operator A, it can be shown that tr(A) is independent of the chosen base [27]. The trace is cyclic, i.e., tr(AB)=tr(BA) [25], linear, i.e., tr(A+B)=tr(A)+tr(B), scalar, i.e., tr(cA)=ctr(A) for a constant c [4] and multiplicative, i.e., tr(AB)=tr(A)tr(B) holds [25] for trace class operators A,B. We use T() to denote the space of trace class operators on . Positive trace class operators with tr(ρ)1 are called partial density operators. The set of partial density operators is denoted 𝒟() with 𝑠𝑝𝑎𝑛(𝒟())=T(). Density operators are partial density operators with tr(ρ)=1. They are denoted as 𝒟(). The support of a partial density operator ρ is the smallest closed subspace S such that PSρPS=ρ.

Let us consider some properties of functions that map operators to operators. f:T1T2 with T1T(1),T2T(2) is trace-reducing if tr(f(ρ))tr(ρ) for all positive ρT1. f:B1B(1)B2B(2) is positive if f(a) is positive for positive aB1 and subunital if f(I1)I2 and I1B1, where is defined just below.

2.2.1 The Loewner Partial Order

To order operators, the Loewner partial order is used. For any operators A,B, it is defined by AB iff BA is a positive operator. This is equivalent to tr(Aρ)tr(Bρ) for all partial density operators ρ𝒟() [27]. The Loewner order is compatible w.r.t. addition (also known as monotonic), i.e., AB implies A+CB+C for any C, and w.r.t. multiplication of non-negative scalars, i.e., AB implies cAcB for c0 [3].

Using this order, we can define predicates [7]. A quantum predicate on a Hilbert space is defined as an operator P on with 𝟎PI. The set of quantum predicates on is denoted by 𝒫() and 𝑠𝑝𝑎𝑛(𝒫())=B().

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., itr(ρi)=tr(iρi) for any increasing sequence of partial density operators {ρi}i. 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 itr(Piρ)=tr((iPi)ρ) for every ρ𝒟() and increasing sequence of predicates {Pi}i. Without going into further detail, we can show that a function f:() defined by f(A)=tr(Aρ) 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 {Pi}i and the greatest lower bound iPi.

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 |ψi with probability pi (with ipi1), then this is represented by the partial density operator ρ=ipi|ψiψi|.

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 {Mm}mI where m is the measurement outcome and mIMmMm=I 111As in [25], we mean convergence of sums with respect to SOT (strong operator topology) which is the topology where limiai=a holds iff for all ϕ: limiaiϕ=aϕ [5, Prop. IX.1.3(c)].. If the quantum system is in state ρ𝒟() before the measurement {Mm}, then the probability for result m is p(m)=tr(MmρMm) and the post-measurement state is ρm=MmρMmp(m). An important kind of measurement is the projective measurement. It is a set of projections {Pm} over with mPm=I. An important property of projective measurements is that if a state ρ is measured by a projective measurement {P,IP} and supp(ρ)P holds, then ρ is not changed.

2.4 Markov Chains

A Markov chain (MC) is a tuple =(Σ,P,sinit) where

  • Σ is a nonempty (possibly uncountable) set of states,

  • P:Σ×Σ[0,1] with sΣP(s,s)=1 is the transition probability function. Let s𝑝s denote P(s,s)=p.

  • sinitΣ 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 s can only have a countable number of successor states s with P(s,s)>0. 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 s0s1s2Σω with s0=sinit and P(si,si+1)>0 for all i. 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 Pr on 𝑃𝑎𝑡ℎ𝑠() is defined using cylinder sets as in [2]. In a slight abuse of notation, we write Pr(π^) for Pr(Cyl(π^)) for π^𝑃𝑎𝑡ℎ𝑠𝑓𝑖𝑛() where Cyl(π^) denotes the cylinder set of π^. We write s0psn where p=s0sn𝑃𝑎𝑡ℎ𝑠𝑓𝑖𝑛()P(s0sn) is the probability to reach sn from s0. Given a target set of reachable states TΣ, let T be the (measurable) set of infinite paths that reach the target set T. The probability of reaching T is Pr(T)=π^𝑃𝑎𝑡ℎ𝑠𝑓𝑖𝑛()(Σ\T)TPr(π^). Analogously, let ¬T be the set of paths that never reach T; Pr(¬T)=1Pr(T).

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

2={α|0+β|1α,β},
={nαn|nαn for all n and n|αn|2<}.

Each variable q𝑉𝑎𝑟 has a type type(q){𝐵𝑜𝑜𝑙,𝐼𝑛𝑡}. Its state space q is 2 if type(q)=𝐵𝑜𝑜𝑙 and otherwise. The state space of a quantum register q¯=q1,,qn is defined by the tensor product q¯=i=1nqi of state spaces of q1 through qn.

3.1 Syntax

A quantum while-program has the following syntax:

S::=𝐬𝐤𝐢𝐩q:=0q¯:=Uq¯𝐨𝐛𝐬𝐞𝐫𝐯𝐞 (q¯,O)S1;S2𝐦𝐞𝐚𝐬𝐮𝐫𝐞 M[q¯]:S¯𝐰𝐡𝐢𝐥𝐞 M[q¯]=1 𝐝𝐨 S

where

  • q is a quantum variable,

  • q¯ is a quantum register,

  • U from statement q¯:=Uq¯ is a unitary operator on q¯ and q¯ is the same on both sides,

  • O in 𝐨𝐛𝐬𝐞𝐫𝐯𝐞 (q¯,O) is a projection on q¯

  • the measurement M={Mm}mI in 𝐦𝐞𝐚𝐬𝐮𝐫𝐞 M[q¯]:S¯ is on q¯ and S¯={Sm}mI is a family of quantum programs where each Sm corresponds to an outcome mI,

  • the measurement in 𝐰𝐡𝐢𝐥𝐞 M[q¯]=1 𝐝𝐨 S on q¯ has the form M={M0,M1}.

Our programs extend [27] with the new statement 𝐨𝐛𝐬𝐞𝐫𝐯𝐞 (q¯,O). We only allow projective predicates O for observations. It is conceivable that it can also be based on more general predicates O𝒫() but it is not clear what the intuitive operational meaning of such O would be, so we choose to pursue the simpler case.

We use 𝐢𝐟 M[q¯]=1 𝐭𝐡𝐞𝐧 S1 𝐞𝐥𝐬𝐞 S0 as syntactic sugar for a measurement statement with M={M0,M1} and S¯={S0,S1}.

By we denote syntactic equality of quantum programs. We use 𝑣𝑎𝑟(S) to denote the set of variables occurring in program S. The Hilbert space of 𝑣𝑎𝑟(S) is denoted by 𝑎𝑙𝑙. If the set of variables is clear from the context, we just write .

For q¯=q1,,qn and operator A on q¯, we define its cylinder extension by AIVar\{q¯} on all and abbreviate it by A if it is clear from the context. Let |ϕψ|q denote the value of quantum variable q in the state |ϕψ|. We sometimes refer to it meaning its cylinder extension on 𝑎𝑙𝑙 [27]. This notation is equivalent to q(|ϕψ|) in [25].

3.2 Semantics

In this section, we define an operational and denotational semantics for quantum while-programs with observations and show their equivalence.

Figure 1: Transition probability function of MC ρS for all σ𝒟() where ;S2S2.

3.2.1 Operational Semantics

We start by defining the operational semantics of a program S as a Markov chain inspired by [23] instead of non-deterministic relations in comparison to [27]. A quantum configuration is a tuple S,ρ with density operator ρ𝒟(). Note that we consider normalized density operators 𝒟() instead of partial density operators 𝒟(). Intuitively, S 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 sink.

Definition 2.

The operational semantics of a program S with initial state ρ𝒟() is defined as the Markov chain ρS=(Σ,P,sinit) where:

  • Σ=𝒞(){,sink},

  • sinit=S,ρ,

  • P is the smallest function satisfying the inference rules in Figure 1 where c𝑝c means P(c,c)=p>0. For all other pairs of states the transition probability is 0.

The meaning of a transition S,σ𝑝S,σ is that after evaluating program S on state σ, with probability p the new state is σ and the program left to execute is S. For the observe statement, there are two successors, 𝐨𝐛𝐬𝐞𝐫𝐯𝐞 (q¯,O),σtr(OσO),OσOtr(OσO) and 𝐨𝐛𝐬𝐞𝐫𝐯𝐞 (q¯,O),σ1tr(OσO). The observation O is satisfied by state σ with probability tr(OσO) and then it terminates successfully. If the observation is violated (with probability 1tr(OσO)), 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 (ρ,p),(σ,q)𝒟()×0, we define multiplication with a constant a0 and addition entrywise: a(ρ,p):=(aρ,ap) and (ρ,p)+(σ,q):=(ρ+σ,p+q).

The least upper bound (lub) of a set of tuples is defined as the entrywise lub provided it exists, i.e., n=0(ρn,pn):=(n=0ρn,n=0pn) where n=0ρn is the lub w.r.t. the Loewner partial order and n=0pn is the lub w.r.t. to the classical ordering on 0.

As the probability of violating observations depends on the density operator, we introduce

𝒟={(ρ,p)𝒟()×0tr(ρ)+p1}T()×.

T()× is isomorphic to the set of operators of the form (ρp)T(). Thus the trace and the norm from T() apply. Specifically, tr~(ρ,p):=tr(ρ)+p and (ρ,p):=ρ+|p| for (ρ,p)T()×.

Definition 3.

The denotational semantics of a quantum program S is defined as a mapping S:𝒟𝒟. For (ρ,p)𝒟, ρ is used for density-transformer semantics as defined in [27] and p for the probability of an observation violation.

The denotational semantics for (ρ,p)𝒟 is given by

  • 𝐬𝐤𝐢𝐩(ρ,p)=(ρ,p).

  • q:=0(ρ,p)={(|00|qρ|00|q+|01|qρ|10|q,p), if type(q)=𝐵𝑜𝑜𝑙(n|0n|qρ|n0|q,p), if type(q)=𝐼𝑛𝑡.

  • q¯:=Uq¯(ρ,p)=(UρU,p).

  • 𝐨𝐛𝐬𝐞𝐫𝐯𝐞 (q¯,O)(ρ,p)=(OρO,p+tr(ρ)tr(OρO)).

  • S1;S2(ρ,p)=S2(S1(ρ,p)).

  • 𝐦𝐞𝐚𝐬𝐮𝐫𝐞 M[q¯]:S¯(ρ,p)=mSm(MmρMm,0)+(𝟎,p) with M={Mm}mI and S¯={Sm}mI.

  • 𝐰𝐡𝐢𝐥𝐞 M[q¯]=1 𝐝𝐨 S(ρ,p)=n=0(𝐰𝐡𝐢𝐥𝐞 M[q¯]=1 𝐝𝐨 S)n(ρ,p) with M={M0,M1} where loop unfoldings are defined inductively

    (𝐰𝐡𝐢𝐥𝐞 M[q¯]=1 𝐝𝐨 S)0 Ω
    (𝐰𝐡𝐢𝐥𝐞 M[q¯]=1 𝐝𝐨 S)n+1 𝐢𝐟 M[q¯]=1 𝐭𝐡𝐞𝐧 S;(𝐰𝐡𝐢𝐥𝐞 M[q¯]=1 𝐝𝐨 S)n 𝐞𝐥𝐬𝐞 𝐬𝐤𝐢𝐩

    where Ω is a syntactic quantum program with Ω(ρ,p)=(𝟎,p) as in [27].

We write Sρ(ρ,p) and S(ρ,p) to denote the first/second component of S(ρ,p). It follows directly that our definition is a conservative extension of [27]:

Proposition 4.

For an observe-free program S, input state ρ𝒟() and p0, is S(ρ,p)=(Sog(ρ),p) where Sog(ρ) is the denotational semantics as defined in [27].

Some intuition behind those tuples: If S(ρ,0)=(ρ,p) for a program S with initial pair (ρ,0), then the probability of violating an observation while executing S on ρ𝒟() is p. The probability of terminating normally (without violating an observation) is given by tr(ρ) and the probability for non-termination is 1tr(ρ)p. As in the observe-free case, ρ is the (non-normalized) state after S 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 (ρ,p),(ρ,q)𝒟 and program S:

  1. 1.

    Sρ(ρ,p)=Sρ(ρ,q)

  2. 2.

    pS(ρ,p)

  3. 3.

    if (ρ,q+p)𝒟 then S(ρ,q+p)=S(ρ,q)+p

  4. 4.

    tr~(S(ρ,p))tr~(ρ,p)

  5. 5.

    S is well defined, i.e., S(ρ,p)𝒟 and the least upper bound exists.

  6. 6.

    S is linear

Proof.

All claims can be shown by doing an induction over S, see [14]. As Sρ(ρ,p)=Sρ(ρ,q), we use Sρ(ρ) instead. Three consequences of Proposition 5:

Lemma 6.

For (ρ,p),(σ,q)𝒟 with (ρ+σ,p+q)𝒟 and programs S,S1,S2:

  1. 1.

    tr(Sρ(ρ,p))tr(ρ), i.e., Sρ is trace-reducing

  2. 2.

    S1;S2(ρ,q+p)=S2(S1ρ(ρ,0),q)+S1(ρ,p)

  3. 3.

    Sρ 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 tr(ρ)=1, we only consider this case:

Lemma 7.

For any program S and initial state ρ𝒟()

  • S(ρ,0)=(ρPrρS(,ρ)ρ,PrρS())

  • PrρS(sink)=tr(Sρ(ρ,0))+S(ρ,0)

Proof.

The first item can be shown by induction over S. For the second item we use that every path that eventually reaches sink 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 P satisfies 𝟎PI. Let tr(Pρ) be the probability that ρ satisfies P. Note that if P is a projector, then tr(Pρ) equals the probability that ρ gives answer “yes” in a measurement defined by P. Even if P is not a projection, tr(Pρ) is the average value of measuring ρ with the measurement described by the observable P. 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 P,Q𝒫(), S a program, ρ𝒟() and {P}S{Q} a correctness formula. Then

  1. 1.

    (total correctness) tot{P}S{Q} iff tr(Pρ)tr(QSρ(ρ,0))

  2. 2.

    (partial correctness) par{P}S{Q} iff tr(Pρ)tr(QSρ(ρ,0))+tr(ρ)tr(Sρ(ρ,0))S(ρ,0)

Let us explain this definition. Assume tr(ρ)=1, otherwise all probabilities mentioned in the following are non-normalized. Recall that tr(Pρ) is the probability that state ρ satisfies predicate P and tr(QSρ(ρ,0)) is the probability that the state after execution of S starting with ρ satisfies predicate Q. Total correctness entails that the probability of a state satisfying precondition P is at most the probability that it satisfies postcondition Q after execution of S. This only involves terminating runs. In the formula of partial correctness, the summand S(ρ,0) captures the probability that an observation is violated during executing program S on state ρ. As before, tr(ρ)tr(Sρ(ρ,0)) captures the probability that S on state ρ does not terminate.

Similar to [27], we have some nice but different properties:

Proposition 9.
  1. 1.

    tot{P}S{Q} implies par{P}S{Q}

  2. 2.

    tot{𝟎}S{Q}. However, par{P}S{I} does not hold in general.

  3. 3.

    For P1,P2,Q1,Q2𝒫() and λ1,λ20 with λ1P1+λ2P2,λ1Q1+λ2Q2𝒫(): tot{P1}S{Q1}tot{P2}S{Q2} implies tot{λ1P1+λ2P2}S{λ1Q1+λ2Q2}

Proof.
  1. 1.

    Follows from definition and tr(Sρ(ρ,0))+S(ρ,0)tr(ρ)+0 (Proposition 5)

  2. 2.

    tot{𝟎}S{Q} follows from definition, tr(𝟎)=0 and tr(Qσ)0 for all σ𝒟(). For disproving par{P}S{I}, consider a program (with only one variable q) S𝐨𝐛𝐬𝐞𝐫𝐯𝐞(|11|,q). Then the statement does not holds with P=I and ρ=|00| because S(ρ,0)>0.

  3. 3.

    Follows from the linearity of the trace and the definition of tot.

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 S and predicate P𝒫().

  1. 1.

    The weakest precondition is defined as qwpS(P)=sup{Q tot{Q}S{P}}. Thus tot{qwpS(P)}S{P} and tot{Q}S{P} implies QqwpS(P) for all Q𝒫().

  2. 2.

    The weakest liberal precondition is defined as qwlpS(P)=sup{Q par{Q}S{P}}. Thus par{qwlpS(P)}S{P} and par{Q}S{P} implies QqwlpS(P) for all Q𝒫().

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 S:𝒟𝒟 with properties as in Proposition 5, the weakest precondition qwpS:𝒫()𝒫() exists and is bounded linear and subunital. It satisfies tr(qwpS(P)ρ)=tr(PSρ(ρ,0)) for all ρ𝒟(),P𝒫() and it is the only function of this type with this property.

This lemma (and the following one) does not require S to be a denotational semantics of some program S. 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 S:𝒟𝒟 with properties as in Proposition 5, the weakest liberal precondition qwlpS:𝒫()𝒫() exists and is subunital. It satisfies

tr(qwlpS(P)ρ)=tr(PSρ(ρ,0))+tr(ρ)tr(Sρ(ρ,0))S(ρ,0)

for each ρ𝒟(),P𝒫() 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 S(ρ,0)=0 and Sρ(ρ,0)=Sog(ρ) for each ρ𝒟() and for each observation-free program S, 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 S, the function qwpS:𝒫()𝒫() satisfies:

  • Bounded linearity

  • Subunitality: qwpS(I)I

  • Monotonicity: PQ implies qwpS(P)qwpS(Q)

  • Order-continuity: qwpS(i=0Pi)=i=0qwpS(Pi) if i=0Pi exists

Proposition 14.

For every program S, the function qwlpS:𝒫()𝒫() satisfies:

  • Affinity: The function f:𝒫()𝒫() with f(P)=qwlpS(P)qwlpS(𝟎) is linear. Note that this implies convex-linearity and sublinearity.

  • Subunitality: qwlpS(I)I

  • Monotonicity: PQ implies qwlpS(P)qwlpS(Q)

  • Order-continuity: qwlpS(i=0Pi)=i=0qwlpS(Pi) if i=0Pi exists

For our denotational semantics S, we can also give an explicit representation of qwpS:

Proposition 15.

Let P𝒫():

  • qwp𝐬𝐤𝐢𝐩(P)=P

  • qwpq:=0(P)={|0q0|P|00|q+|10|qP|01|q,if type(q)=𝐵𝑜𝑜𝑙n|n0|qP|0n|q,if type(q)=𝐼𝑛𝑡

  • qwpq¯:=Uq¯(P)=UPU

  • qwp𝐨𝐛𝐬𝐞𝐫𝐯𝐞 (q¯,O)(P)=OPO

  • qwpS1;S2(P)=qwpS1(qwpS2(P))

  • qwp𝐦𝐞𝐚𝐬𝐮𝐫𝐞 M[q¯]:S¯(P)ρ=mMm(qwpSm(P))Mm

  • qwp𝐰𝐡𝐢𝐥𝐞 M[q¯]=1 𝐝𝐨 S(P)=n=0Pn with

    P0=𝟎, Pn+1=[M0PM0]+[M1(qwpS(Pn))M1]

    and n=0 denoting the least upper bound w.r.t. .

Proof.

We prove tr(qwpS(P)ρ)=tr(PSρ(ρ,0)), 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 P𝒫(). For most cases, qwlpS(P) is defined analogous to qwpS(P) (replacing every occurrence of qwp by qwlp). The only significant difference is the while-loop: qwlp𝐰𝐡𝐢𝐥𝐞 M[q¯]=1 𝐝𝐨 S(P)=n=0Pn with

P0=I, Pn+1=[M0PM0]+[M1(qwlpS(Pn))M1]

and n=0 denoting the greatest lower bound w.r.t. .

Proof.

This proof is similar to Proposition 15 except that we show that tr(qwlpS(P)ρ)=tr(PSρ(ρ,0))+tr(ρ)tr(Sρ(ρ,0))S(ρ,0) 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 tr(ρ)=1, otherwise the probabilities are not normalized. To understand those definitions, consider tr(qwpS(P)ρ). Due to the duality from Lemma 11, tr(qwpS(P)ρ)=tr(PSρ(ρ,0)), so it is the probability that the result of running program S (without violating any observations) on the initial state ρ satisfies predicate P. Similarly, tr(qwlpS(P)ρ) adds the probability of non-termination too. This is equivalent to the standard interpretation of weakest (liberal) preconditions in [27].

For programs with observations tr(qwlpS(P)ρ) does not include runs that violate an observation. Thus, tr(qwlpS(I)ρ) gives the probability that no observation is violated during the run of S on input state ρ (while for programs without observations and in [27], this will always be tr(ρ)=1). The probability that a program state ρ will satisfy the postcondition P after executing program S 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., (P,Q)+(P,Q)=(P+P,Q+Q) and M(P,Q)=(MP,MQ) resp. (P,Q)M=(PM,QM) where M 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 𝒫()2 as follows: (P,Q)(P,Q)PPQQ where is the Loewner partial order. The least element is (𝟎,I) and the greatest element (I,𝟎). The least upper bound of an increasing chain {(Pi,Qi)}i for (Pi,Qi)𝒫()2 is given pointwise by i=0(Pi,Qi)=(i=0Pi,i=0Qi).

Lemma 18.

is an ω-cpo on 𝒫()2.

Proof.

We have to show that i=0(Pi,Qi) exists for any increasing chain {(Pi,Qi)}i. If {(Pi,Qi)}i is increasing with respect to , then {Pi}i is increasing and {Qi}i decreasing with respect to . The existence of i=0Pi follows directly from being an ω-cpo on 𝒫(). For i=0Qi, we use the same trick as in the proof of Proposition 16: {IQi}i is an increasing chain of predicates and thus i=0Qi=Ii=0(IQi) exists too. That means both i=0Pi and i=0Qi exist and thus also i=0(Pi,Qi). 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 qcwpS:𝒫()2𝒫()2 defined as qcwpS(P,Q):=(qwpS(P),qwlpS(Q)).

Similar to the weakest precondition calculus, we can also give an explicit representation which can be found in [14].

Again, assume tr(ρ)=1 in the following, otherwise the probabilities are not normalized. Note that tr(qwlpS(I)ρ) is the probability that no observation is violated and tr(qwpS(P)ρ) the probability that P is satisfied after S has been executed on ρ (see above). We are interested in the conditional probability of establishing the postcondition given that no observation is violated, namely tr(qwpS(P)ρ)tr(qwlpS(I)ρ). Notice that for qcwpS(P,I)=(A,B), this is simply tr(Aρ)tr(Bρ). That means we can immediately read of this conditional probability from qcwpS. Formally, we use

tr^(Aρ,Bρ):={tr(Aρ)tr(Bρ), if tr(Bρ)0undefined, otherwise.

We now establish some properties of conditional weakest preconditions:

Proposition 20.

For every program S, the function qcwpS:𝒫()2𝒫()2 satisfies:

  • Has a linear interpretation: for all ρ𝒟(),a,b0 and P,P𝒫() with aP+bP𝒫()

    tr^(qcwpS(aP+bP,I)ρ)=atr^(qcwpS(P,I)ρ)+btr^(qcwpS(P,I)ρ)
  • Affinity: The function qcwpS(P,Q)qcwpS(𝟎,𝟎) is linear. Note that this implies convex-linearity and sublinearity.

  • Monotonicity: (P,P)(Q,Q) implies qcwpS(P,P)qcwpS(Q,Q)

  • Continuity: qcwpS(i=0(Pi,Qi))=i=0qcwpS(Pi,Qi) if i=0(Pi,Qi) 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 qcwlp:𝒫()2𝒫()2 is defined as qcwlpS(P,Q):=(qwlpS(P),qwlpS(Q)) for each program S and predicates P,Q𝒫().

Definition 22.

We define ˙ on 𝒫()2 as follows (P,Q)˙(P,Q)PPQQ where is the Loewner partial order. The least element is (𝟎,𝟎) and the greatest element (I,I). The least upper bound of an increasing chain {(Pi,Qi)}i for (Pi,Qi)𝒫()2 is given pointwise by i=0(Pi,Qi)=(i=0Pi,i=0Qi).

Note that in contrast to , both components are ordered in the same direction. Here it follows directly that ˙ is an ω-cpo on 𝒫()2.

Similar as for qcwp, we can now read off the conditional satisfaction of P when we want non-termination to count as satisfaction: tr^(qcwlpS(P,I)ρ)=tr(qwlpS(P)ρ)tr(qwlpS(I)ρ) which is equal to dividing the probability to satisfy P 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., qcwpS(P,I)=(qwpS(P),qwlpS(I)) and qcwlpS(P,I)=(qwlpS(P),qwlpS(I)). In [15, Sect. 8.3] it is argued why other combinations such as (qwpS(P),qwpS(I)) and (qwlpS(P),qwpS(I)) 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 S, the function qcwlpS:𝒫()2𝒫()2 satisfies:

  • Affinity: The function qcwlpS(P,Q)qcwlpS(𝟎,𝟎) is linear. Note that this implies convex-linearity and sublinearity.

  • Monotonicity: (P,Q)˙(P,Q) implies qcwlpS(P,Q)˙qcwlpS(P,Q)

  • Continuity: qcwlpS(i=0(Pi,Qi))=i=0qcwlpS(Pi,Qi) if i=0(Pi,Qi)

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 S, predicate P𝒫() and state ρ𝒟():

tr^(qcw(l)pS(P,I)ρ)=tr(qw(l)pS(P)ρ)
Proof.

For every observation-free program S is qwlpS(I)=I [27] and which means for ρ𝒟() (and not for all ρ𝒟()) tr^(qcw(l)pS(P,I)ρ) is equal to

tr(qw(l)pS(P)ρ)tr(qwlpS(I)ρ)=tr(qw(l)pS(P)ρ)tr(ρ)=tr(qw(l)pS(P)ρ).

4.6 Correspondence to Operational MC Semantics

The aim of this section is to establish a correspondence between qcwpS(P,I) and the operational semantics of S. In order to reason about P 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 P:

Definition 25.

For program S and postcondition P, the Markov reward chain ρPS is the MC ρS extended with a function r:Σ0 such that r(,ρ)=tr(Pρ) and r(s)=0 for all other states sΣ.

The (liberal) reward of a path π of ρPS is defined as r(π)={tr(Pρ), if ,ρπ0, else and lr(π)=r(π) expect if sinkπ, then lr(π)=1.

The expected reward of sink is the expected value of r(π) for all πsink, i.e., ERρPS(sink)=ρPrρPS(,ρ)tr(Pρ). The liberal version adds rewards of non-terminating paths, i.e., LERρPS(sink)=ERρPS(sink)+PrρPS(¬sink).

Now we start by showing some auxiliary results, similar to [23, Lemma 5.5, 5.6]:

Lemma 26.

For a program S, state ρ𝒟(), predicate P𝒫() we have

PrρPS(¬)=tr(qwlpS(I)ρ), (L)ERρPS(sink)=tr(qw(l)pS(P)ρ)
Proof.

Follows from Lemma 7, Lemma 11 and Lemma 12. We are interested in the conditional (liberal) expected reward of reaching sink from the initial state S,ρ, conditioned on not visiting :

C(L)ERρPS(sink¬):= (L)ERρPS(sink)PrρIS(¬)

This reward is equivalent to our interpretation of qcw(l)p, analogous to [23, Theorem 5.7]:

Theorem 27.

For a program S, state ρ𝒟(), predicates P,Q𝒫() we have

C(L)ERρPS(sink¬) =tr^(qcw(l)pS(P,I)ρ)
Proof.

Assuming tr(qwlpS(I)ρ)>0, then C(L)ERρPS(sink¬) is equal to

(L)ERρPS(sink)PrρIS(¬)=tr(qw(l)pS(P)ρ)tr(qwlpS(I)ρ)=tr^(qcw(l)pS(P,I)ρ)

by Lemma 26. If tr(qwlpS(I)ρ)=0, then C(L)ERρPS(sink¬) 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 N possible outcomes [19]. We solve this problem for N=6 with quantum gates by creating qubits q,p,r with Hadamard gates and using the observe statement to reject the qp=11 case, leaving 6 possible outcomes (qpr=000,,101), see Figure 2.

Figure 2: Quantum Fast-Dice-Roller. For the identity operator on 22 we use I4.

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

|ϕ=16(|000+|001+|010+|011+|100+|101)

over 6 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 qcwpS(P,I) and compute qwpS(P) and qwlpS(I) separately where P=|ϕϕ| and S stands for our fast-dice roller program (Figure 2). The probability that an input state ρ will reach the desired uniform superposition is tr^(qcwpS(P,I)ρ), that is

tr(qwpS(P)ρ)tr(qwlpS(I)ρ)={1,if ρ=|000000|0,if ρ=|xx| with x{001,011,101,111}0.1111,if ρ=|xx| with x{010,100,110}.

ρ=|000000| will reach the desired superposition with probability 1 assuming no observation is violated. We can also see that tr(qwpS(P)|000000|)1 so even with the “best” input, our conditional weakest precondition calculus gives more information than qwpS(P).

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.

Figure 3: Inner loop body Sk of the quantum algorithm solving MAJ-SAT as presented in [1]. y,z are qubits, q¯ is an n-qubit sized register (formally n qubits q1,,qn). We use q¯:=0n to denote that all n qubits of q¯ are set of 0. Rk=11+4k(12k2k1) is a rotation matrix depending on the parameter k and CH is a controlled Hadamard.

Formally, we are faced with the following problem: A formula with n variables can be represented by a function f:{0,1}n{0,1} with s=|{f(x)=1}|. The goal is to determine whether s<2n1 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:

for k=n,,n:repeat n times:Skif Sk succeeded more than 34n times:return truereturn false

where Sk is given in Figure 3 and succeeding means that measuring z in the |+,| basis returns |+. The core idea is to show that Sk succeeds with probability 12 for all k if s2n1 and with probability (1+26)20.971 for at least one k otherwise. Hence the overall algorithm solves MAJ-SAT. To keep this example manageable, we focus on the analysis of Sk alone.

We use conditional weakest preconditions and determine qcwpSk(P,In+2) (which depends on the parameters n,s,k). Here the postcondition P corresponds to z being in state |+, formally P=|++|zI. Then the probability that Sk succeeds is Prnsk:=tr^(qcwpSk(P,In+2)ρ) 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 Sk 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 n and s and all k=n,,n, we find that in each case the cwp is of the form (cI,cI) for some c,c. This is to be expected since all variables q¯,z,y 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, Prnsk=c/c. In Figure 4, we show maxkPrnsk for selected s,n. (The claim from [1] is that the success probability of Sk is 0.971 for some k if s<2n1 and 1/2 for all k otherwise, so we only care about the maximum over all k.) We see that maxkPrnsk is indeed 0.971 and 1/2 in those two cases. This confirms the calculation from [1], using our logic. (At least for the values of s,n we computed.)

s=2 s=3 s=4 s=7 s=8
n=2 0.5 0.3838 0.3286
n=3 0.9714¯ 0.9991¯ 0.5 0.4247 0.4123
n=4 0.9991¯ 0.9933¯ 0.9714¯ 0.9889¯ 0.5
n=5 0.9889¯ 0.9828¯ 0.9991¯ 0.9977¯ 0.9714¯
Figure 4: Maximum of Prnsk=tr^(qcwpSk(P,In+2)ρ) for k[n,n]. The cases where s<2n1 are underlined.

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.