The final version of this paper appeared in Math. Struct. in Comp. Science 14(4):527-586, 2004

Towards a Quantum Programming Language

P E T E R S E L I N G E R^{†}

Department of Mathematics and Statistics University of Ottawa Ottawa, Ontario K1N 6N5, Canada

Email: selinger@mathstat.uottawa.ca

Received 13 Nov 2002, revised 7 Jul 2003

We propose the design of a programming language for quantum computing. Traditionally, quantum algorithms are frequently expressed at the hardware level, for instance in terms of the quantum circuit model or quantum Turing machines. These approaches do not encourage structured programming or abstractions such as data types. In this paper, we describe the syntax and semantics of a simple quantum programming language with high-level features such as loops, recursive procedures, and structured data types. The language is functional in nature, statically typed, free of run-time errors, and it has an interesting denotational semantics in terms of complete partial orders of superoperators.

1. Introduction

Quantum computation is traditionally studied at the hardware level: either in terms of gates and circuits, or in terms of quantum Turing machines. The former viewpoint emphasizes data flow and neglects control flow; indeed, control mechanisms are usually dealt with at the meta-level, as a set of instructions on how to construct a parameterized family of quantum circuits. On the other hand, quantum Turing machines can express both data flow and control flow, but in a sense that is sometimes considered too general to be a suitable foundation for implementations of future quantum computers.

In this paper, we seek to investigate quantum computation from the point of view of programming languages. We propose a view of quantum computation which is able to express both data flow and control flow, while not relying on any particular hardware model. Our approach can be summarized by the slogan “quantum data, classical control”. Thus, the data which is manipulated by programs may involve quantum superpositions, but the control state of a program is always classical; there is no “quantum branching” and no notion of executing a quantum superposition of two different statements. This is more general than quantum circuits, where control flow is not modeled at all, but more restrictive than quantum Turing machines, where both data and control may be “quantum”. The paradigm “quantum data, classical control” seems to be precisely what is embodied in most known practical quantum algorithms, such as Shor’s factoring algorithm, Grover’s search algorithm, or the Quantum Fourier Transform (Shor 1994; Grover 1996).

† Research supported by NSERC.

The programming language presented in this paper is functional, in the sense that each (atomic or composite) statement operates by transforming a specific set of inputs to outputs. This is in contrast to imperative programming languages, which operate by updating global variables. Our language is also statically typed, which implies that the well-formedness of a program can be checked at compile time, rather than relying on run-time checks. For instance, the principle of non-duplication of quantum data, known as the no-cloning property, is enforced by the syntax of our language. Unlike previous proposals for quantum programming languages (Knill 1996;Ömer 1998; Sanders and Zuliani 2000; Bettelli, Calarco, and Serafini 2001), our language guarantees that any well-typed program is free of run-time errors.

The language also provides high-level control features such as loops and recursion, and it can accommodate structured data types such as lists or trees. We provide two alternative syntactic representations of quantum programs: a graphical representation in terms of flow charts, and a textual, more structured representation. The choice of syntax is a matter of taste, because the two representations are semantically equivalent.

Perhaps the most important feature of our programming language is that it admits a denotational semantics. This is achieved by assigning a certain kind of linear operator, called a superoperator, to each program fragment. For the semantics of loops and recursion, we use the fact that the superoperators of each given type form a complete partial order. The denotational semantics is fully abstract in the sense that two program fragments are denotationally equivalent if and only if they are indistinguishable in the context of any larger program.

While the semantics of our quantum programming language can (and will) be described without reference to any particular hardware model, it helps the intuition to think of a particular hardware device on which the language might be implemented. Here, it is understood that actual future quantum hardware may differ; these differences must be handled by implementors and have no direct impact on the language design. A suitable hardware model for our purposes is the QRAM machine of (Knill 1996). This machine consists of a general-purpose classical computer which controls a special quantum hardware device. The quantum device provides a potentially large number of individually addressable quantum bits. Quantum bits can be manipulated via two fundamental operations: (1) unitary transformations and (2) measurements. Typically, the quantum device will implement a fixed, finite set of unitary transformations which operate on one or two quantum bits at a time. The classical controller communicates with the quantum device by sending a sequence of instructions, specifying which fundamental operations are to be performed. The only output from the quantum device consists of the results of measurements, which are sent back to the classical controller. Note that in the QRAM model, it is not necessary for the classical hardware and the quantum device to be in the same physical location; it is even possible for several classical controllers to share access to a single quantum device.

In quantum complexity theory, algorithms are often presented in a certain normal form: a quantum computation consists of an initialization, followed by a unitary transformation, followed by a single final measurement, just before the classical result of the algorithm is read. While no theoretical generality is lost in postponing all measurements until the end, this presentation does not necessarily lead to the most natural programming style. Quantum algorithms are often more naturally described by interleaving quantum and classical operations, allowing the results of measurements to influence subsequent operations. In addition to being conceptually more natural, this style of programming can also lead to savings in the resources consumed by an algorithm, for instance, in the number of quantum bits that must be allocated.

One issue not addressed in this paper is the question of quantum communication. The programming language described here deals with quantum computation in a closed world, i.e., it describes a single process which computes a probabilistic result depending only on its initial input parameters. We do not deal with issues related to the passing of quantum information, or even classical information, between different quantum processes. In particular, our programming language currently does not support primitives for input or output, or other side effects such as updating shared global data structures. While adding such features would not cause any problems from an operational point of view, the denotational semantics of the resulting language is not currently very well understood. In other words, in a setting with side effects, it is not clear how the behavior of a complex system can be described in terms of the behaviors of its individual parts. On the one hand, these difficulties are typical of the passage from purely functional programming languages to concurrent languages. On the other hand, there are particular complications that arise from the very nature of quantum information flow, which is a subject of ongoing research.

Another issue that we do not address in this paper is the issue of “idealized” vs. “real” hardware. Just as in classical programming language theory, we assume that our language runs on idealized hardware, i.e., hardware that never produces flawed results, where quantum states do not deteriorate, where no unintended information is exchanged between programs and their environment, etc. This seems to be a reasonable assumption from the point of view of language design, although it might only be approximated in actual implementations. It is intended that implementations will use quantum error correction techniques to limit the adverse effects of imperfect physical hardware. Ideally, error correction should be handled transparently to the programmer. This can potentially be achieved at several different levels: error correction could be built into the hardware, it could be handled by the operating system, or it could be added automatically by the compiler. The tradeoffs associated with each of these choices remain the subject of future work, and we do not consider them further in this paper.

Since there are some differences between the customary languages of physics and computer science, it seems appropriate to comment on the notation used in this paper. Because this paper was primarily written with an audience of computer scientists in mind, we tend to use computer science notation instead of physics notation. For instance, we write states as columns vectors (not row vectors, as is usual in physics), and we represent operators as matrices, using a fixed basis and fixed indexing conventions. It is hoped that notational issues will not prove too distracting for readers of differing backgrounds.

In order to keep the paper as self-contained as possible, we provide a brief review of some basic concepts from linear algebra and quantum computation in Sections 2 and 3. These introductory sections contain no original contributions except for the definition of the complete partial order of density matrices in Section 3.8. The main body of the paper starts in Sections 4 and 5 with the introduction of the quantum flow chart language. We proceed to discuss its formal semantics in Section 6, and we conclude with some syntactic considerations and a discussion of possible language extensions in Section 7.

Previous work

The recent literature contains several proposals for quantum programming languages. One of the first contributions in this direction is an article by Knill (1996). While not proposing an actual programming language, Knill outlines a set of basic principles for writing pseudo-code for quantum algorithms. These principles have influenced the design of some later language designs. The first actual quantum programming language is due to Ömer (1998). Ömer defines a rich procedural language QCL, which contains a full-fledged classical sublanguage and many useful highlevel quantum features, such as automatic scratch space management and syntactic reversibility of user-defined quantum operators. Partly building on Ömer’s work, Bettelli et al. (2001) present a quantum programming language which is an extension of the well-known language C++. The distinguishing feature of this language is that it treats quantum operators as first-class objects which can be explicitly constructed and manipulated at run-time, even allowing run-time optimizations of operator representations.

A quantum programming language of a somewhat different flavor is given by Sanders and Zuliani (2000). Their language qGCL, which is based on an extension of Dijkstra’s guardedcommand language, is primarily useful as a specification language. Its syntax includes high-level mathematical notation, and the language supports a mechanism for stepwise refinement which can be used for systematic program derivation and verification.

Of the languages surveyed, only that of Sanders and Zuliani possesses a formal semantics. This semantics is purely operational, and it works by assigning to each possible input state a probability distribution on output states. One problem with this approach is that the resulting probability distributions are generally very large, incorporating much information which is not physically observable. The present paper offers a better solution to this problem, using density matrices and superoperators instead of probability distributions.

A common feature of the quantum programming languages surveyed in the literature is that they are realized as imperative programming languages. Quantum data is manipulated in terms of arrays of quantum bit references. This style of data representation requires the insertion of a number of run-time checks into the compiled code, including out-of-bounds checks and distinctness checks. For instance, distinctness checks are necessary because quantum operators can only be applied to lists of distinct quantum bits. In imperative languages, this condition cannot be checked at compile-time. For similar reasons, most optimizations cannot be performed at compile-time and must therefore be handled at run-time in these languages. By contrast, the language proposed in the present paper is a functional programming language with a static type system which guarantees the absence of any run-time errors.

2. Basic notions from linear algebra

In this section, we recall some well-known concepts from linear algebra, for the purpose of fixing the notation used in later parts of this paper. Readers may safely skip this section and refer back to it when needed.

From the outset, we have to make an important choice of presentation. It is generally understood that the basic notions of linear algebra can be expressed in a basis-independent way. Thus, the standard way of introducing the concepts of quantum mechanics is in the language of Hilbert spaces, linear operators, etc. However, in the concrete setting of quantum computation, such generality is not needed, as it turns out that there is always a “preferred” basis to work with. Therefore, we may as well identify our vector spaces with ℂ^{n}. This allows us to express all operations though manipulations of concrete objects, such as columns vectors and matrices. Nevertheless, it is of course understood that all covered concepts are basis independent unless stated otherwise.

2.1. Vectors and matrices

Let ℂ be the set of complex numbers, which are also called scalars. The complex conjugate of z ∈ ℂ is written z̄. We write ℂ^{n} for the space of n-dimensional column vectors, and ℂ^{n×m} for the space of matrices with n rows and m columns. We identify ℂ^{n} with ℂ^{n×1} and ℂ^{1} with ℂ. Matrix multiplication is defined as usual. The identity matrix is written I. The adjoint of a matrix A = (a_{ij}) ∈ ℂ^{n×m} is given by A^{∗} = (ā_{ji}) ∈ ℂ^{m×n}. The trace of a square matrix A = (a_{ij}) ∈ ℂ^{n×n} is defined as trA = ∑_{i} a_{ii}. Note that for A, B ∈ ℂ^{n×n}, tr(AB) = tr(BA).

Column vectors are denoted by u, v, etc. We write e_{i} for the ith canonical basis vector. Note that if u is a column vector, then u^{∗} is a row vector. Note that u^{∗}u = ∑_{i} |u_{i}|^{2} ⩾ 0. The norm ∥u∥ of a vector u is defined by ∥u∥ = \sqrt{u^{∗}u}. The vector u is called a unit vector if ∥u∥ = 1.

If A, B, C and D are matrices of equal dimensions, we often denote the matrix obtained by “horizontal and vertical concatenation” by

( \begin{matrix}A & B \\ C & D \end{matrix} ).

Sometimes, we also use an analogous notation for vectors.

2.2. Unitary matrices

A square matrix S ∈ ℂ^{n×n} is called unitary if S^{∗}S = I is the identity matrix, or equivalently, if S^{∗} = S^{−1}. Unitary matrices are precisely the isometries, because ∥Su∥ = ∥u∥ holds for all u iff S is unitary. If S is unitary and A = SBS^{∗}, then trA = trB. Thus, the trace is invariant under a unitary change of basis.

2.3. Hermitian and positive matrices

A square matrix A ∈ ℂ^{n×n} is called hermitian if A = A^{∗}. Note that if A is hermitian, then u^{∗}Au is always real. A matrix A is called positive semidefinite, or simply positive, if it is hermitian and u^{∗}Au ⩾ 0 for all u ∈ ℂ^{n}. Note that hermitian matrices are closed under addition and real scalar multiples, i.e., they form an ℝ-linear subspace of ℂ^{n×n}. Moreover, the positive matrices are closed under addition and non-negative real scalar multiples.

A matrix A is hermitian if and only if A = SDS^{∗}, for some unitary matrix S and some real-valued diagonal matrix D. The diagonal entries of D are the eigenvalues of A, and they are uniquely determined up to a permutation. The columns of S are the corresponding eigenvectors. Moreover, a hermitian matrix A is positive iff all its eigenvalues are non-negative.

A matrix A ∈ ℂ^{n×n} is called pure if A = vv^{∗} for some v ∈ ℂ^{n}. Every pure matrix is positive; conversely, every positive matrix is a non-negative real linear combination of pure matrices.

In dimension 2, a matrix

( \begin{matrix}a & b \\ c & d \end{matrix} )

is hermitian when b = c̄ and a, d ∈ ℝ. Furthermore, it is positive when the trace a + d and the determinant ad − bc are non-negative, and pure when the determinant is zero.

Remark 2.1. Any complex ℂ^{n×n} matrix is a linear combination of four positive hermitian matrices. Because first, any complex matrix A can be written as a linear combination B + iC of two hermitian matrices, where B = \frac{1}{2}(A^{∗} + A) and C = \frac{i}{2}(A^{∗} − A). Second, any hermitian matrix B can be written as the difference of two positive matrices, namely B = (B +λI)−(λI), where −λ is the most negative eigenvalue of B.

Remark 2.2. Every positive matrix A ∈ ℂ^{n×n} is of the form BB^{∗}, for some B ∈ ℂ^{n×n}. This is evident for diagonal matrices, and follows for arbitrary positive matrices by a change of basis.

2.4. Tensor product

The tensor product of two vector spaces is defined as usual; here we only need two special cases: ℂ^{n} ⊗ ℂ^{m} = ℂ^{nm} and ℂ^{n×n} ⊗ ℂ^{m×m} = ℂ^{nm×nm}.

The tensor product w = u ⊗ v ∈ ℂ^{nm} of two vectors u ∈ ℂ^{n}, v ∈ ℂ^{m} is defined by w_{(i,j)} = u_{i}v_{j}. Similarly, the tensor product C = A ⊗ B ∈ ℂ^{nm×nm} of two matrices is defined by c_{(i,j),(i′,j′)} = a_{ii′}b_{jj′}. Here we order the pairs (i, j) lexicographically; thus, for instance,

( \begin{matrix}0 & 1 \\ −1 & 0 \end{matrix} ) ⊗ B = ( \begin{matrix}0 & B \\ −B & 0 \end{matrix} ) .

3. Basic notions from quantum computation

In this section, we provide a brief review of some basic notions from quantum computation. For a more thorough introduction, see e.g. (Cleve 2000; Preskill 1998; Gruska 1999; Nielsen and Chuang 2000).

3.1. Quantum bits

The basic data type of quantum computation is a quantum bit, also called a qubit. Recall that the possible states of a classical bit b are b = 0 and b = 1. By contrast, the state of a quantum bit q can be any complex linear combination q = α0 + β1, where α, β ∈ ℂ and α, β are not both zero. The coefficients α and β are called the amplitudes of this quantum state, and they are only significant up to scalar multiples, i.e., q = α0+β1 and q′ = α′0+β′1 denote the same quantum state if α′ = γα and β′ = γβ for some non-zero γ ∈ ℂ. One often assumes that the amplitudes α and β are normalized, i.e., that |α|^{2} +|β|^{2} = 1. Note, however, that this normalization does not determine α and β uniquely; they are still only well-defined up to multiplication by a complex unit.

Thus, the classical boolean constants 0 and 1 are identified with two basis vectors e_{0} and e_{1} of a 2-dimensional complex vector space. In the literature, it is common to denote these basis vectors in the so-called “ket” notation as e_{0} = |0⟩ and e_{1} = |1⟩. Thus, the state of a typical quantum bit is written as α |0⟩ + β |1⟩. Here the word ket is the second half of the word bracket; bras also exist, but we shall not use them.

The basis states |0⟩ = 1 |0⟩ + 0 |1⟩ and |1⟩ = 0 |0⟩ + 1 |1⟩ are called the classical states, and any other state is said to be a quantum superposition of |0⟩ and |1⟩.

The first interesting fact about quantum bits is that the state of two or more quantum bits is not just a tuple of its components, as one might have expected. Recall the four possible states of a pair of classical bits: 00, 01, 10, and 11. The state of a pair of quantum bits is a formal complex linear combination of these four classical states, i.e., it is a linear combination of the form

α_{00} |00⟩ + α_{01} |01⟩ + α_{10} |10⟩ + α_{11} |11⟩ = ∑_{i,j∈{0,1}} α_{ij} |ij⟩ .

Again, we have used the ket notation to identify the four classical states with the basis states of a 4-dimensional vector space. As before, we require that at least one of the α_{ij} is non-zero, and the whole state is only well-defined up to a complex scalar multiple. If q = α |0⟩ + β |1⟩ and p = γ |0⟩ + δ |1⟩ are two independent quantum bits, then their combined state is

q ⊗ p = αγ |00⟩ + αδ |01⟩ + βγ |10⟩ + βδ |11⟩ .

However, note that in general, the state of a pair of quantum bits need not be of the form q ⊗ p. For instance, the state

\frac{1}{\sqrt{2}} |00⟩ + \frac{1}{\sqrt{2}} |11⟩

is clearly not of the form q ⊗ p, for any q and p. If the state of a pair of quantum bits is of the form q ⊗ p, then the pair is said to be independent, otherwise it is called entangled.

In general, the state of n quantum bits is a non-zero vector in ℂ^{2^{n}}, i.e., a formal linear combination

∑_{b_{1},...,b_{n}∈{0,1}} α_{b_{1}...b_{n}} |b_{1} … b_{n}⟩ ,

taken modulo scalar multiples.

3.2. Indexing conventions

Consider a quantum state q = α |00⟩+β |01⟩+γ |10⟩+δ |11⟩. By identifying the basis vectors |00⟩, |01⟩, |10⟩, and |11⟩ with the canonical basis of ℂ^{4}, we can write the state q as a column vector

q = ( \begin{matrix}α \\ β \\ γ \\ δ \end{matrix} ).

Here, the ordering of the basis vectors is relevant. In general, we need an indexing convention which determines how the basis vectors |b_{1}b_{2} … b_{n}⟩ are to be identified with the canonical basis vectors of ℂ^{2^{n}}. We use the following convention:

Convention 3.1 (Lexicographic convention). Consider the set of bit vectors of length n, i.e., tuples (b_{1}, b_{2}, …, b_{n}), where b_{1}, …, b_{n} ∈ {0, 1}. We identify each such bit vector with the number i ∈ 0, …, 2^{n} − 1 of which it is the binary representation. We also identify the “ket” |b_{1}b_{2} … b_{n}⟩ with the ith canonical basis vector of ℂ^{2^{n}}.

Note that this convention is equivalent to saying that the bit vectors shall always be ordered lexicographically when they are used to index the rows or columns of some vector or matrix.

Sometimes, we need to consider a permutation of quantum bits, such as exchanging the first with the second quantum bit in a sequence. This induces a corresponding permutation of states. More precisely, any permutation of n elements, ϕ : {1, …, n} → {1, …, n}, induces a permutation 2^{ϕ} of the set of bit vectors of length n, which is defined by 2^{ϕ}(x_{1}, …, x_{n}) = (x_{ϕ^{−1}(1)}, …, x_{ϕ^{−1}(n)}), for x_{1}, …, x_{n} ∈ {0, 1}. Note that this definition is covariant, in the sense that 2^{ϕ} ∘ 2^{ψ} = 2^{ϕ∘ψ}. As an illustration, consider the permutation ϕ such that ϕ(1) = 2, ϕ(2) = 3, and ϕ(3) = 1. Then 2^{ϕ}(b_{1}, b_{2}, b_{3}) = (b_{3}, b_{1}, b_{2}). Thus, 2^{ϕ} maps 000 ↦ 000, 100 ↦ 010, 110 ↦ 011, and so forth.

3.3. Unitary transformations

There are precisely two kinds of operations by which we can manipulate a quantum state: unitary transformations and measurements. We describe unitary transformations in this section and measurements in the next one.

The state of a quantum system can be transformed by applying a unitary transformation to it. For instance, consider a quantum bit in state u = α |0⟩+β |1⟩, and let S be a unitary 2×2-matrix. Then we can perform the operation

( \begin{matrix}α \\ β \end{matrix} ) ↦ S( \begin{matrix}α \\ β \end{matrix} ).

Similarly, if v = α |00⟩ + β |01⟩ + γ |10⟩ + δ |11⟩ is the state of a two-qubit quantum system, we can transform it by a unitary 4 × 4-matrix, and similarly for three or more quantum bits. A unitary operation on n quantum bits is also known as an n-ary quantum gate. While every unitary matrix can be realized in principle, one usually assumes a fixed finite set of gates that are built into the hardware. The particular choice of basic gates is not important, because a compiler will easily be able to translate one such set to another. One possible choice is the following, which consists of four unary and five binary gates:

N = ( \begin{matrix}0 & 1 \\ 1 & 0 \end{matrix} ), H = \frac{1}{\sqrt{2}}( \begin{matrix}1 & 1 \\ 1 & −1 \end{matrix} ), V = ( \begin{matrix}1 & 0 \\ 0 & i \end{matrix} ), W = ( \begin{matrix}1 & 0 \\ 0 & \sqrt{i} \end{matrix} ),

N_{c} = ( \begin{matrix}I & 0 \\ 0 & N \end{matrix} ), H_{c} = ( \begin{matrix}I & 0 \\ 0 & H \end{matrix} ),  V_{c} = ( \begin{matrix}I & 0 \\ 0 & V \end{matrix} ), W_{c} = ( \begin{matrix}I & 0 \\ 0 & W \end{matrix} ),

X = ( \begin{matrix}1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{matrix} ).

The unary gate N is called the not-gate, because it induces the following mapping of basis vectors: |0⟩ ↦ |1⟩ and |1⟩ ↦ |0⟩. Geometrically, it corresponds to the symmetry of axis π/4.

The unary gate H is called the Hadamard gate, and it corresponds to the symmetry of axis π/8. The unary gates V and W represent complex phase changes.

The binary gate N_{c} is called the controlled not-gate. It corresponds to a permutation of basis vectors: |00⟩ ↦ |00⟩, |01⟩ ↦ |01⟩, |10⟩ ↦ |11⟩, and |11⟩ ↦ |10⟩. Its action can be described as follows: if the first bit is 0, do nothing, else apply the N-gate to the second bit. In this sense, the first bit controls the action of the second one. More generally, for each unary gate S, there is a corresponding binary controlled gate S_{c}. Finally, the binary X-gate corresponds to an exchange of two qubits: it maps |b_{1}b_{2}⟩ to |b_{2}b_{1}⟩, whenever b_{1}, b_{2} ∈ {0, 1}. The X-gate is classical in the sense that it can be implemented as an operation on addresses of qubits, rather than on qubits themselves. In the quantum computation literature, its existence is often assumed implicitly.

Unitary transformations can be used to create quantum superpositions, and they can also be used to create entanglement between quantum bits. For example, the Hadamard gate H, when applied to the classical state |0⟩, creates a quantum superposition \frac{1}{\sqrt{2}} |0⟩ + \frac{1}{\sqrt{2}} |1⟩. Also, the controlled not-gate, applied to two independent quantum bits (\frac{1}{\sqrt{2}} |0⟩ + \frac{1}{\sqrt{2}} |1⟩) ⊗ |0⟩, creates an entangled state \frac{1}{\sqrt{2}} |00⟩ + \frac{1}{\sqrt{2}} |11⟩.

A quantum gate can be applied in the presence of additional quantum bits. For example, to apply a unary gate S to the second quantum bit in a 4-bit system, we transform the system by the matrix I ⊗ S ⊗ I ⊗ I. Here I is the 2 × 2-identity matrix.

Clearly, the above set of nine standard gates contains some redundancy; for instance, V = W^{2}, N = HV^{2}H, etc. On the other hand, the set is complete, in the sense that any n-ary unitary gate can be approximated, up to an arbitrarily small error ϵ, by a combination of the given gates. In fact, three of the nine gates suffice:

Proposition 3.2 (see (Cleve 2000, Thm. 1)). Let n ⩾ 2. For any unitary matrix S ∈ ℂ^{2^{n}×2^{n}} and any ϵ > 0, there exists a unitary matrix S′ and a unit complex number λ such that ∥S −λS′∥ < ϵ and such that S′ can be written as a product of matrices of the form I ⊗ A ⊗ J, where I, J are identity matrices of the appropriate dimensions, and A is one of the gates H, V_{c}, and X.

Thus, if we assume that the above nine gates (or some other finite complete set of gates) are implemented in hardware, then any other unitary gate can be simulated by software. In practice, decomposing a given unitary matrix into standard gates is not an easy problem, and the best known algorithms are not very efficient. However, we will ignore this problem and simply assume that we can apply arbitrary unitary gates to a quantum state.

3.4. Measurement

The second fundamental operation on the state of a quantum system is known as a measurement. This occurs when we try to “observe” the value of a quantum bit and convert it into a classical bit. Consider for instance the state of a single quantum bit q = α |0⟩ + β |1⟩. For simplicity, let us assume that the amplitudes have been normalized such that |α|^{2} + |β|^{2} = 1. The act of measuring this quantum bit will yield an answer which is either 0 or 1. The answer 0 will occur with probability |α|^{2}, and 1 with probability |β|^{2}. Moreover, the measurement causes the quantum state to collapse: after the measurement, the quantum state will have changed to either |0⟩ or |1⟩, depending on the result of the measurement. In particular, if we immediately measure the same quantum bit again, we always get the same answer as the first time.

The situation is more complex if more than one quantum bit is involved. Consider a two-qubit system in the state α |00⟩ + β |01⟩ + γ |10⟩ + δ |11⟩. We assume again that the amplitudes have been normalized. If we measure the value of the first bit, one of the following things will happen:

— with probability |α|^{2} + |β|^{2}, the result of the measurement will be 0 and the quantum state will collapse to α |00⟩ + β |01⟩, and

— with probability |γ|^{2} + |δ|^{2}, the result of the measurement will be 1 and the quantum state will collapse to γ |10⟩ + δ |11⟩. Note that only the portion of the quantum state pertaining to the bit that we are observing

collapses. If we were observing the second bit instead, the observed answer would be 0 with probability |α|^{2} + |γ|^{2}, and 1 with probability |β|^{2} + |δ|^{2}, and the quantum state would collapse, respectively, to α |00⟩ + γ |10⟩ or β |01⟩ + δ |11⟩.

Now let us see what happens if we measure the second quantum bit after the first one. The situation can be summarized in the following diagram:

\xymatrix{ &  &  & α |00⟩ + β |01⟩ + γ |10⟩ + δ |11⟩ \ar[dll]_{p_{0}=|α|^{2}+|β|^{2}}^{0} \ar[drr]^{p_{1}=|γ|^{2}+|δ|^{2}}_{1} \\  & α |00⟩ + β |01⟩ \ar[dl]_{p_{00}= \frac{|α|^{2}}{|α|^{2}+|β|^{2}}}^{0} \ar[dr]^{p_{01}= \frac{|β|^{2}}{|α|^{2}+|β|^{2}}}_{1} &  &  &  & γ |10⟩ + δ |11⟩ \ar[dl]_{p_{10}= \frac{|γ|^{2}}{|γ|^{2}+|δ|^{2}}}^{0} \ar[dr]^{p_{11}= \frac{|δ|^{2}}{|γ|^{2}+|δ|^{2}}}_{1} \\ α |00⟩ &  & β |01⟩ &  & γ |10⟩ &  & δ |11⟩}

In this tree, the nodes are labeled with states, and the transitions are labeled with probabilities. Note that the overall probability of observing 00 as the result of the two measurements is p_{0}p_{00} = |α|^{2}, the probability of observing 01 is p_{0}p_{01} = |β|^{2}, and so forth. In particular, these probabilities are independent of the order of measurement; thus, the result does not depend on which quantum bit we measure first, or indeed, on whether we measure both of them simultaneously or in sequence. The technical term for this phenomenon is that the two measurements commute. In general quantum mechanics, two measurements need not always commute; however, in quantum computation, they always do. This is due to the fact that we only measure along subspaces spanned by standard coordinate vectors.

We mentioned that it is customary to normalize quantum states so that the sum of the squares of the amplitudes is 1. However, in light of the above diagram, we find that it is often more convenient to normalize states differently.

Convention 3.3 (Normalization convention). We normalize each state in such a way that the sum of the squares of the amplitudes is equal to the total probability that this state is reached.

With this convention, it becomes unnecessary to renormalize the state after a measurement has been performed. We will see later that this convention greatly simplifies our computations.

3.5. Pure and mixed states

We know that the state of a quantum system at any given time can be completely described by its state vector u ∈ ℂ^{2^{n}}, modulo a scalar multiple. However, an outside observer might have incomplete knowledge about the state of the system. For instance, suppose that we know that a given system is either in state u or in state v, with equal probability. We use the ad hoc notation \frac{1}{2} \{u\} + \frac{1}{2} \{v\} to describe this situation. In general, we write λ_{1} \{u_{1}\} + … + λ_{m} \{u_{m}\} for a system which, from the viewpoint of some observer, is known to be in state u_{i} with probability λ_{i}, where ∑_{i} λ_{i} = 1. Such a probability distribution on quantum states is called a mixed state. The underlying quantum states, such as u, are sometimes called pure states to distinguish them from mixed states.

It is important to realize that a mixed state is a description of an observers knowledge of the state of a quantum system, rather than a property of the system itself. In particular, a given system might be in two different mixed states from the viewpoints of two different observers. Thus, the notion of mixed states does not have an independent physical meaning. Physically, any quantum system is in a (possibly unknown) pure state at any given time.

Unitary transformations operate componentwise on mixed states. Thus, the unitary transformation S maps the mixed state λ_{1} {u_{1}} + … + λ_{m} {u_{m}} to λ_{1} {Su_{1}} + … + λ_{m} {Su_{m}}.

The collapse of a quantum bit takes pure states to mixed states. For instance, if we measure a quantum bit in state α |0⟩ + β |1⟩, but ignore the outcome of the measurement, the system enters (from our point of view) the mixed state |α|^{2} {|0⟩} + |β|^{2} {|1⟩}.

3.6. Density matrices

We now introduce a better notation for mixed quantum states, which is due to von Neumann. First, consider a pure state, represented as usual by a unit column vector u. In von Neumann’s notation, this quantum state is represented by the matrix uu^{∗}, called its density matrix. For example, the state of a quantum bit u = \frac{1}{\sqrt{2}} |0⟩ − \frac{1}{\sqrt{2}} |1⟩ is represented by the density matrix

uu^{∗} = ( \begin{matrix}\frac{1}{2} & −\frac{1}{2} \\ −\frac{1}{2} & \frac{1}{2} \end{matrix} ).

Note that no information about a pure state is lost in this notation, because the vector u is uniquely determined, up to a scalar multiple, by the matrix uu^{∗}. There are several advantages to the density matrix notation. A mundane advantage is that we do not have to write so many square roots. More importantly, if u = γv, for some complex scalar γ with |γ| = 1, then uu^{∗} = γγ̄vv^{∗} = vv^{∗}. Thus, the scalar factor disappears, and the normalized representation of each pure quantum state as a density matrix is unique. Also note that tr(uu^{∗}) = ∥u∥^{2}. In particular, if u is a unit vector, then the density matrix has trace 1.

But the real strength of density matrices lies in the fact that they also provide a very economical notation for mixed states. A mixed state λ_{1} {u_{1}}+…+λ_{n} {u_{n}} is simply represented as a linear combination of the density matrices of its pure components, i.e., as λ_{1} u_{1}u_{1}^{∗} + … + λ_{n} u_{n}u_{n}^{∗}. For example, the mixed state \frac{1}{2} \{|0⟩\} + \frac{1}{2} \{|1⟩\} is expressed as the matrix

\frac{1}{2}( \begin{matrix}1 & 0 \\ 0 & 0 \end{matrix} ) + \frac{1}{2}( \begin{matrix}0 & 0 \\ 0 & 1 \end{matrix} ) = ( \begin{matrix}\frac{1}{2} & 0 \\ 0 & \frac{1}{2} \end{matrix} ).

Remark 3.4. Some information about a mixed state is lost in the density matrix notation. For example, let u = \frac{1}{\sqrt{2}} |0⟩ + \frac{1}{\sqrt{2}} |1⟩ and v = \frac{1}{\sqrt{2}} |0⟩ − \frac{1}{\sqrt{2}} |1⟩. Then the mixed state \frac{1}{2} \{u\} + \frac{1}{2} \{v\} gives rise to the following density matrix:

\frac{1}{2}( \begin{matrix}\frac{1}{2} & \frac{1}{2} \\ \frac{1}{2} & \frac{1}{2} \end{matrix} ) + \frac{1}{2}( \begin{matrix}\frac{1}{2} & −\frac{1}{2} \\ −\frac{1}{2} & \frac{1}{2} \end{matrix} ) = ( \begin{matrix}\frac{1}{2} & 0 \\ 0 & \frac{1}{2} \end{matrix} ),

which is the same as the density matrix for the mixed state \frac{1}{2} \{|0⟩\} + \frac{1}{2} \{|1⟩\} calculated above. But as we shall see in the next section, there is no observable difference between two mixed states that share the same density matrix, and thus there is no harm (and indeed, much benefit) in identifying such states. Another example of this phenomenon of two different, but indistinguishable mixed states will be discussed later in Example 4.6.

We note that a matrix A is of the form λ_{1} u_{1}u_{1}^{∗} + … + λ_{n} u_{n}u_{n}^{∗}, for unit vectors u_{i} and non-negative coefficients λ_{i} with ∑_{i} λ_{i} ⩽ 1, if and only if A is positive hermitian and satisfies trA ⩽ 1. (The reason for allowing ∑_{i} λ_{i} ⩽ 1, rather than ∑_{i} λ_{i} = 1, will become apparent later). The left-to-right direction is trivial, and the right-to-left direction follows because any such A can be diagonalized as A = SDS^{∗}, for some D = ∑_{i} λ_{i} e_{i}e_{i}^{∗}. We then have A = ∑_{i} λ_{i} u_{i}u_{i}^{∗}, where u_{i} = Se_{i}. This motivates the following definition:

Definition (Density matrix). A density matrix is a positive hermitian matrix A which satisfies trA ⩽ 1. We write D_{n} ⊆ ℂ^{n×n} for the set of density matrices of dimension n.

Remark 3.5. In any dimension, a density matrix is pure iff its rank is at most 1. More generally, any density matrix of rank k can be decomposed into a sum of k pure density matrices. In particular, since the addition of positive hermitians can only be rank-increasing, the sum of a pure and an impure matrix is always impure.

3.7. Quantum operations on density matrices

The two kinds of quantum operations, namely unitary transformation and measurement, can both be expressed with respect to density matrices. A unitary transformation S maps a pure quantum state u to Su. Thus, it maps a pure density matrix uu^{∗} to Suu^{∗}S^{∗}. Moreover, a unitary transformation extends linearly to mixed states, and thus, it takes any mixed density matrix A to SAS^{∗}.

Now consider the effect of a measurement on a density matrix. We begin by considering a pure state uu^{∗}, for some unit vector u. Suppose that

u = ( \frac{v}{w} ), therefore uu^{∗} = ( \begin{matrix}vv^{∗} & vw^{∗} \\ wv^{∗} & ww^{∗} \end{matrix} ).

Assuming that the rows of u are ordered according to the lexicographic convention (Convention 3.1), then if we perform a measurement on the first bit, the outcome will be ( \frac{v}{0} ) with

probability ∥v∥^{2}, and ( \frac{0}{w} ) with probability ∥w∥^{2}. Or in density matrix notation, the outcome will be

( \begin{matrix}vv^{∗} & 0 \\ 0 & 0 \end{matrix} ) or ( \begin{matrix}0 & 0 \\ 0 & ww^{∗} \end{matrix} ),

where the first matrix occurs with probability ∥v∥^{2}, and the second matrix occurs with probability ∥w∥^{2}. Note that the probability that each matrix occurs is equal to its trace: ∥v∥^{2} = tr(vv^{∗}) and ∥w∥^{2} = tr(ww^{∗}). Thus, the normalization convention (Convention 3.3) extends naturally to density matrices: the density matrix of a state shall be normalized in such a way that its trace corresponds to the overall probability that this state is reached. Note that with this convention, each of the two possible outcomes of a measurement is a linear function of the incoming state.

The measurement operation extends linearly from pure to mixed states. Thus, performing a measurement on a mixed state of the form

( \begin{matrix}A & B \\ C & D \end{matrix} )

results in one of the two states

( \begin{matrix}A & 0 \\ 0 & 0 \end{matrix} ) or ( \begin{matrix}0 & 0 \\ 0 & D \end{matrix} ),

where each of the two matrices occurs with probability equal to its trace. If one ignores the classical bit of information that is observed from the measurement, then the resulting state is a mixed state

( \begin{matrix}A & 0 \\ 0 & D \end{matrix} ).

Thus, collapsing a quantum bit (measuring it while ignoring the result) corresponds to setting a certain region of the density matrix to 0.

We have seen that the effect of the two fundamental operations of quantum mechanics, unitary transformations and measurements, can be described in terms of their action on density matrices. Since unitary transformations and measurements are our only means of interacting with a quantum state, it follows that there is no observable difference between mixed states which have the same density matrix representation.

3.8. The complete partial order of density matrices

Recall that D_{n} is the set of density matrices of dimension n:

D_{n} = {A ∈ ℂ^{n×n} | A positive hermitian and trA ⩽ 1}.

Definition (Löwner partial order). For matrices A, B ∈ ℂ^{n×n}, we define A ⊑ B if the matrix B − A is positive.

It is immediately obvious that ⊑ defines a partial order on the set ℂ^{n×n}. This partial order is known in the literature as the Löwner partial order (Löwner 1934), and it is commonly used in an area of linear programming known as semidefinite programming. When restricted to the set D_{n} of density matrices, this partial order has the zero matrix 0 as its least element. We also denote the zero matrix by ⊥ in this context.

Proposition 3.6. The poset (D_{n}, ⊑) is a complete partial order, i.e., it has least upper bounds of increasing sequences.

Proof. Positive hermitian matrices in ℂ^{n×n} are in one-to-one correspondence with positive quadratic forms on ℂ^{n}. The order on hermitians is just the pointwise order of quadratic forms because A ⊑ B iff for all u, u^{∗}Au ⩽ u^{∗}Bu. Moreover, for all positive A with trA ⩽ 1, we have |u^{∗}Au| ⩽ ∥u∥^{2}; this follows from diagonalization. Thus, any trace-bounded increasing sequence of quadratic forms has a pointwise least upper bound, which is clearly a quadratic form; the limit satisfies the trace condition by continuity of the trace.

Remark 3.7. The poset (D_{n}, ⊑) is not a lattice for n ⩾ 2; in fact, it does not even have least upper bounds of bounded sets. For instance, in D_{2},

( \begin{matrix}0.3 & 0 \\ 0 & 0.3 \end{matrix} ) and ( \begin{matrix}0.4 & 0.2 \\ 0.2 & 0.4 \end{matrix} )

are two different minimal upper bounds of

( \begin{matrix}0.3 & 0 \\ 0 & 0 \end{matrix} ) and ( \begin{matrix}0 & 0 \\ 0 & 0.3 \end{matrix} ).

Remark 3.8. For any increasing sequence in D_{n}, A is the least upper bound if and only if A is the topological limit of the sequence, with respect to the standard Euclidean topology on ℂ^{n×n}. It follows, among other things, that a monotone function f : D_{n} → D_{m} is Scott continuous (i.e., preserves least upper bounds of increasing sequences) if it is topologically continuous. The converse is not in general true; a counterexample is f : D_{2} → D_{1} which maps ( \begin{matrix}a & b \\ c & d \end{matrix} ) to a/(1 − d) if d ≠ 1, and to 0 otherwise.

Remark 3.9. For a density matrix ( \begin{matrix}A & B \\ C & D \end{matrix} ) with B, C ≠ 0, we have

( \begin{matrix}0 & 0 \\ 0 & 0 \end{matrix} ) ⊑ ( \begin{matrix}A & 0 \\ 0 & 0 \end{matrix} ) ⊑ ( \begin{matrix}A & 0 \\ 0 & D \end{matrix} ),

but

( \begin{matrix}A & 0 \\ 0 & 0 \end{matrix} ) ⋢ ( \begin{matrix}A & B \\ C & D \end{matrix} ) and ( \begin{matrix}A & 0 \\ 0 & D \end{matrix} ) ⋢ ( \begin{matrix}A & B \\ C & D \end{matrix} ).

The latter inequalities fail because the difference of the two matrices has null entries on the diagonal, and thus can be positive only if the corresponding non-diagonal entries also vanish.

4. Quantum flow charts (QFC)

We now turn to the question of how to design a quantum programming language, and how to define its semantics. One of the first issues that we are faced with is the choice of a convenient syntax. Out of the many possibilities, we choose to represent programs as flow charts, also known as control flow diagrams. However, unlike traditional flow charts for imperative programming languages, our flow charts have a more “functional” flavor; in our setting, commands act by transforming specific inputs to outputs, rather than by updating global variables. Thus, we combine the language of control flow with some elements of data flow.

In this section and the next one, we describe the syntax of flow charts, and we introduce their semantics informally. The formal semantics is given in Section 6. Some alternative language choices are discussed in Section 7.

[figure: input b, c : bit; branch b; 0; 1; b, c : bit; b, c : bit; b, c : bit; b := c; b, c : bit; c := 0; b, c : bit; ∘; b, c : bit; output b, c : bit]

Fig. 1. A simple classical flow chart

4.1. Classical flow charts

The concept of a flow chart in “functional style” is best illustrated by giving some examples. It is instructive to consider the classical (i.e., not quantum) case first. Consider the simple flow chart shown in Figure 1.

Unless otherwise indicated by arrows, the flow of control is from top to bottom. Each edge is labeled with a typing judgment, i.e., by a list of typed variables. These are the variables which are available at that given point in the program. For simplicity, we consider only a single data type for now: the type bit of a classical bit (also known as the type of booleans). In the example in Figure 1, no variables are created or disposed of, so each edge is labeled with the same typing judgment b, c : bit.

This program fragment inputs a pair of bits, performs a conditional branch and some updates, and then outputs the pair (b, c). The semantics of this program can be described as a map from its inputs to its outputs. Specifically, the map computed by this program is:

\begin{matrix}00 & ↦ & 00 \\ 01 & ↦ & 01 \\ 10 & ↦ & 00 \\ 11 & ↦ & 10 \end{matrix}

The state of the program, between instructions, is given by a pair (e, ρ), where e is an edge of the flow chart (thought of as the program counter), and ρ is an assignment of values to the variables with which e is labeled.

An important observation is that the two components of the state, instruction pointer and value assignment, are fundamentally of the same nature. Thus, the instruction pointer could be thought of as a variable (and indeed, in most hardware implementations, it is represented by a machine register). Conversely, the content of a boolean variable can be thought of as encoding a choice between two alternative control paths. For example, an edge labeled with a boolean variable b

[figure: 00; 01; 10; 11; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; ∘; 00; 01; 10; 11; A; B; C; D; A; B; 0; 0; 0; 0; C; D; C; 0; 0; D; C; 0; D; 0; B; D; 0; A + C; (∗ branch b ∗); (∗ b := c ∗); (∗ c := 0 ∗); (∗ merge ∗); input b, c : bit; output b, c : bit]

Fig. 2. Classical flow chart from Figure 1, with boolean variables expanded

can be equivalently replaced by two parallel (unlabeled) edges, corresponding to the states b = 0 and b = 1, respectively. Similarly, an edge labeled with two boolean variables can be replaced by four parallel edges, corresponding to the four possible states 00, 01, 10, and 11, and so on. In this way, each classical flow chart can be transformed into an equivalent (though much larger) flow chart that uses no variables. After such a transformation, each conditional branch has a predetermined outcome, and each assignment corresponds to a jump to the appropriate parallel component. To illustrate this point, the expansion of the flow chart from Figure 1 is explicitly shown in Figure 2. Here, the four entrance points of the expanded program correspond to the four possible pairs of boolean inputs of the original program, and the four exit points correspond to the four potential pairs of boolean outputs.

It is this connection between control and classical state, namely, the fact that a control edge labeled with a tuple of classical variables can be replaced by a number of parallel control edges, which we mean when we say “control is classical”. We will see later that a similar observation does not apply to quantum data; quantum data is of a fundamentally different nature.

4.2. Probabilistic view of classical flow charts

Consider again the flow chart from Figure 2. We will describe an alternative view of its semantics. This time, imagine that one of the four possible entrance points 00, 01, 10, or 11 is selected randomly, with respective probabilities A, B, C, and D. Then we can annotate, in a top-down fashion, each edge of the flow chart with the probability that this edge will be reached. In particular, any edge that is unreachable will be annotated with “0”. The resulting annotation is shown in Figure 2. We find that the probabilities of the final outcomes 00, 01, 10, and 11 are A + C, B, D, and 0, respectively. In this way, each program gives rise to a function from tuples of input probabilities to tuples of output probabilities. In our example, this function is F (A, B, C, D) = (A + C, B, D, 0).

Note that our original reading of a flow chart, as a function from inputs to outputs, is completely subsumed by this probabilistic view. For instance, the fact that the input 11 is mapped to 10 is easily recovered from the fact that F (0, 0, 0, 1) = (0, 0, 1, 0).

In practice, it is usually preferable to think of a small number of variables rather than of a large number of control paths. Therefore, we will of course continue to draw flow charts in the style of Figure 1, and not in that of Figure 2. However, the preceding discussion of probabilities still applies, with one modification: each edge that is labeled with n boolean variables should be annotated by a tuple of 2^{n} probabilities, and not just a single probability.

4.3. Summary of classical flow chart components

The basic operations for boolean flow charts are summarized in Figure 3. Here, Γ denotes an arbitrary typing context, and A and B denote tuples of probabilities with sum at most 1. If A and B are tuples of equal length, we use the notation (A, B) to denote the concatenation of A and B.

We distinguish between the label of an edge and its annotation. A label is a typing context, and it is part of the syntax of our flow chart language. An annotation is a tuple of probabilities, and it is part of the semantics. We use the equality symbol “=” to separate labels from annotations. Thus, Figure 3 defines both the syntax and the semantics of the language. Note that the statement of the rules makes use of the indexing convention of Section 3.2, because the order of the probabilities in each tuple is determined by the lexicographical ordering of the corresponding states.

There are four rules in Figure 3 that have not been discussed so far: “new” allocates a new variable and initializes it to 0, “discard” deallocates a variable, “initial” creates an unreachable control path (this was used e.g.in Figure 2), and “permute” is a dummy operation which allows us to rearrange the variables in the current typing context. Note that the statement of the “permute” rule uses the operation 2^{ϕ} on permutations which was defined in Section 3.2.

It is interesting to note that, although we have not imposed a block structure on the uses of “new” and “discard”, the typing rules nevertheless ensure that every allocated variable is eventually deallocated, unless it appears in the output. Also note that the “initial” node is essentially a 0-ary version of “merge”.

Naturally, we will allow various kinds of syntactic sugar in writing flow charts that are not directly covered by the rules in Figure 3. For instance, an assignment of the form b := c can be regarded as an abbreviation for the following conditional assignment:

Allocate bit:

Γ = A

new bit b := 0

b : bit, Γ = (A, 0)

Assignment:

b : bit, Γ = (A, B)

b := 0

b : bit, Γ = (A + B, 0)

b : bit, Γ = (A, B)
b := 1
b : bit, Γ = (0, A + B)

Discard bit:

b : bit, Γ = (A, B)

discard b

Γ = A + B

Branching:

[figure: branch b; 0; b : bit, Γ = (A, 0); 1; b : bit, Γ = (0, B); b : bit, Γ = (A, B)]

Merge:

[figure: Γ = A; Γ = B; ∘; Γ = A + B]

Initial:

[figure: ∘; Γ = 0]

Permutation:

b_{1}, …, b_{n} : bit = A_{0}, …, A_{2^{n}−1}

permute ϕ

b_{ϕ(1)}, …, b_{ϕ(n)} : bit = A_{2^{ϕ}(0)}, …, A_{2^{ϕ}(2^{n}−1)}

Fig. 3. Rules for classical flow charts

[figure: branch c; 0; b, c : bit, Γ = (A, 0, C, 0); 1; b, c : bit, Γ = (0, B, 0, D); b, c : bit, Γ = (A, B, C, D); b := 0; b, c : bit, Γ = (A + C, 0, 0, 0); b := 1; b, c : bit, Γ = (0, 0, 0, B + D); ∘; b, c : bit, Γ = (A + C, 0, 0, B + D)]

4.4. Quantum flow charts

Quantum flow charts are similar to classical flow charts, except that we add a new type qbit of quantum bits, and two new operations: unitary transformations and measurements. A unitary transformation operates on one or more quantum bits; we write q ∗= S for the operation of applying a unary quantum gate S to the quantum bit q. Note that this operation updates q; the

[figure: input p, q : qbit; (a); measure p; 0; p, q : qbit; 1; p, q : qbit; p, q : qbit; q ∗= N; p, q : qbit; p ∗= N; p, q : qbit; ∘; p, q : qbit; output p, q : qbit; input p, q : qbit; (b); measure p; 0; p, q : qbit = ( \begin{matrix}A & 0 \\ 0 & 0 \end{matrix} ); 1; p, q : qbit = ( \begin{matrix}0 & 0 \\ 0 & D \end{matrix} ); p, q : qbit = ( \begin{matrix}A & B \\ C & D \end{matrix} ); q ∗= N; p, q : qbit = ( \begin{matrix}NAN^{∗} & 0 \\ 0 & 0 \end{matrix} ); p ∗= N; p, q : qbit = ( \begin{matrix}D & 0 \\ 0 & 0 \end{matrix} ); ∘; p, q : qbit = ( \begin{matrix}NAN^{∗} + D & 0 \\ 0 & 0 \end{matrix} ); output p, q : qbit]

Fig. 4. A simple quantum flow chart

notation is analogous to the notation b := 1 for classical assignment. For the application of a binary quantum gate S to a pair p, q of quantum bits, we use the notation p, q ∗= S, and so forth for gates of higher arity. Sometimes we also use special notations such as q ⊕= 1 for q ∗= N, and q ⊕= p for p, q ∗= N_{c}, where N and N_{c} are the not- and controlled not-gate, respectively. The second new operation, the measurement, is a branching statement since it can have two possible outcomes.

Note that we give the name qbit to the type of qubits, thus dropping the letter “u”. We do this for the sake of brevity. Note that there is a long tradition in computer science of such abbreviations; for instance, many programming languages use bool for the type of booleans, thus dropping the final “e” from George Boole’s name.

As a first example, consider the simple flow chart shown in Figure 4(a). This program fragment inputs two quantum bits p and q, measures p, and then performs one of two possible unitary transformations depending on the outcome of the measurement. The output is the modified pair p, q.

The behavior of a quantum flow chart can be described as a function from inputs to outputs. For instance, in the flow chart of Figure 4(a), the input |00⟩ leads to the output |01⟩, and the input \frac{1}{\sqrt{2}} |00⟩+\frac{1}{\sqrt{2}} |01⟩ leads to the output \frac{1}{\sqrt{2}} |00⟩+\frac{1}{\sqrt{2}} |01⟩. However, due to the probabilistic nature of measurement, the output is not always a pure state: for example, the input \frac{1}{\sqrt{2}} |00⟩ + \frac{1}{\sqrt{2}} |10⟩ will lead to the outputs |00⟩ or |01⟩ with equal probability. We can represent this outcome by the mixed state \frac{1}{2} \{|00⟩\} + \frac{1}{2} \{|01⟩\}.

As the example shows, the output of a quantum flow chart is in general a mixed state. We may take the input to be a mixed state as well. Thus, the semantics of a quantum flow chart is given as a function from mixed states to mixed states. To calculate this function, let us use density matrix notation, and let us assume that the input to the program is some mixed state

M = ( \begin{matrix}A & B \\ C & D \end{matrix} ),

where each of A, B, C, D is a 2 × 2-matrix. Recall that the indexing convention of Section 3.2 prescribes that the rows and columns of M are indexed by the basic states 00, 01, 10, and 11 in this respective order. We can now decorate the flow chart in top-down fashion, by annotating each edge with the mixed state that the program is in when it reaches that edge. The resulting annotation is shown in Figure 4(b). The semantics of the entire program fragment is thus given by the following function of density matrices:

F ( \begin{matrix}A & B \\ C & D \end{matrix} ) = ( \begin{matrix}NAN^{∗} + D & 0 \\ 0 & 0 \end{matrix} ).

Note that we have followed our usual convention of normalizing all density matrices so that their trace equals the probability that the corresponding edge is reached. This convention has several nice properties: First and foremost, it ensures that the annotation of each edge is a linear function of the input. In particular, the merge operation (the joining of two control edges) amounts to a simple matrix addition. The normalization convention also implies that the traces of the matrices along any horizontal section of the flow chart add up to 1 (assuming that the trace of the input matrix is 1).

Another interesting observation about the program in Figure 4 is that if the input is a pure state, then the states along each of the two branches of the measurement continue to be pure. Unitary transformations also preserve pure states. It is finally the merge operation, denoted by a small circle “∘”, which combines two pure states into an impure state. Thus, the source of impure states in a quantum system is not the measurement operation (as one might have thought), but rather the merge operation, i.e., the erasure of classical information.

4.5. Summary of quantum flow chart operations

The operations for basic quantum flow charts are summarized in Figure 5. As before, we distinguish between the label of an edge and its annotation. The label is a typing context Γ, and it is part of the syntax of the language. The annotation is a density matrix, and it is part of the semantics. Quantum bits can be allocated and discarded; here it is understood that allocating a quantum bit means to request an unused quantum bit from the operating system. Such newly allocated quantum bits are assumed to be initialized to |0⟩. Unitary transformations and measurement were discussed in the previous section, and “merge”, “initial”, and “permute” are as for classical flow charts. The permutation rule again uses the notation 2^{ϕ} from Section 3.2.

4.6. Detour on implementation issues

We briefly interrupt our description of quantum flow charts to contemplate some possible implementation issues. It is understood that no actual quantum hardware currently exists; thus any discussion of implementations is necessarily speculative. But as outlined in the introduction, it is useful to keep in mind a hypothetical hardware device on which the language can be implemented. Following Knill (1996), we imagine that practical quantum computing will take place on

Allocate qubit:

Γ = A

new qbit q := 0

q : qbit, Γ = ( \begin{matrix}A & 0 \\ 0 & 0 \end{matrix} )

Unitary transformation:

q̄: qbit, Γ = A

q̄ ∗= S

q̄: qbit, Γ = (S ⊗ I)A(S ⊗ I)^{∗}

Discard qubit:

q : qbit, Γ = ( \begin{matrix}A & B \\ C & D \end{matrix} )

discard q

Γ = A + D

Measurement:

[figure: measure q; 0; q : qbit, Γ = ( \begin{matrix}A & 0 \\ 0 & 0 \end{matrix} ); 1; q : qbit, Γ = ( \begin{matrix}0 & 0 \\ 0 & D \end{matrix} ); q : qbit, Γ = ( \begin{matrix}A & B \\ C & D \end{matrix} )]

Merge:

[figure: Γ = A; Γ = B; ∘; Γ = A + B]

Initial:

[figure: ∘; Γ = 0]

Permutation:

q_{1}, …, q_{n} : qbit = (a_{ij})_{ij}

permute ϕ

q_{ϕ(1)}, …, q_{ϕ(n)} : qbit = (a_{2^{ϕ}(i),2^{ϕ}(j)})_{ij}

Fig. 5. Rules for quantum flow charts

a QRAM machine, which consists of a general-purpose classical computer controlling a special quantum hardware device which provides a bank of individually addressable quantum bits. The classical computer determines the sequence of elementary quantum operations (built-in unitary gates and measurements) to be performed by the quantum device.

To make the quantum device available to user programs, we further imagine that the operating system provides a number of services. One of these services is access control. The operating system keeps a list of quantum bits that are currently in use by each process. When a process requests a new qubit, the operating system finds a qubit that is not currently in use, marks that qubit as being in use by the process, initializes the contents to |0⟩, and returns the address of the newly allocated qubit. The process can then manipulate the qubit, for instance via operating system calls which take the qubit’s address as a parameter. The operating system ensures that processes cannot access qubits that are not currently allocated to them – this is very similar to classical memory management. Finally, when a process is finished using a certain qubit, it may deallocate it via another operating system call; the operating system will then reset the qubit to |0⟩, and mark it as unused.

In practice, there are many ways of making this scheme more efficient, for instance by dividing the available qubits into regions, and allocating and deallocating them in blocks, rather than individually. However, for the purpose of this theoretical discussion, we are not too concerned with such implementation details. What is important is the interface presented to user programs by the operating system. In particular, the above discussion is intended to clarify the operations of “allocating” and “discarding” qubits; clearly, these concepts do not refer to physical acts of creation and destruction of qubits, but rather to access control functions performed by the operating system.

We have mentioned several instances in which the operating system resets or initializes a qubit to 0. This is indeed possible, and can be implemented by first measuring the qubit, and then performing a conditional “not” operation dependent on the outcome of the measurement. The following program fragment illustrates how an arbitrary qubit q can be reset to 0:

[figure: measure q; 0; 1; q : qbit, Γ = ( \begin{matrix}0 & 0 \\ 0 & D \end{matrix} ); q : qbit, Γ = ( \begin{matrix}A & B \\ C & D \end{matrix} ); q : qbit, Γ = ( \begin{matrix}A & 0 \\ 0 & 0 \end{matrix} ); q ⊕= 1; q : qbit, Γ = ( \begin{matrix}D & 0 \\ 0 & 0 \end{matrix} ); ∘; q : qbit, Γ = ( \begin{matrix}A + D & 0 \\ 0 & 0 \end{matrix} )]

4.7. Combining classical data with quantum data

We observed in Section 4.1 that classical data can be equivalently viewed in terms of control paths. Since our quantum flow charts from Section 4.4 already combine quantum data with control paths, there is nothing particularly surprising in the way we are going to combine classical data with quantum data. The combined language has two data types, bit and qbit. Typing contexts are defined as before. For the semantics, observe that an edge which is labeled with n bits and m qubits can be equivalently replaced by 2^{n} edges which are labeled with m qubits only. Thus, a state for a typing context Γ containing n bits and m qubits is given by a 2^{n}-tuple (A_{0}, …, A_{2^{n}−1}) of density matrices, each of dimension 2^{m} × 2^{m}. We extend the notions of trace, adjoints, and matrix multiplication to tuples of matrices as follows:

\begin{aligned}tr(A_{0}, …, A_{2^{n}−1}) & := & ∑_{i} trA_{i}, \\ (A_{0}, …, A_{2^{n}−1})^{∗} & := & (A_{0}^{∗}, …, A_{2^{n}−1}^{∗}), \\ S(A_{0}, …, A_{2^{n}−1})S^{∗} & := & (SA_{0}S^{∗}, …, SA_{2^{n}−1}S^{∗}). \end{aligned}

We often denote tuples by letters such as A, B, C, and as before, we use the notation (A, B) for concatenation of tuples, if A and B have the same number of components. If A, B, C, D are tuples of matrices of identical dimensions, then we write

( \begin{matrix}A & B \\ C & D \end{matrix} )

to denote the tuple whose ith component is ( \begin{matrix}A_{i} & B_{i} \\ C_{i} & D_{i} \end{matrix} ), where A_{i} is the ith component of A

etc. In this way, we use componentwise notation both along the “tuple dimension” and along the “matrix dimension”.

Flow charts are acyclic graphs whose edges are labeled with typing contexts, and whose nodes are of the types shown in Figures 3 and 5. A flow chart may have any number of global incoming (input) and outgoing (output) edges.

An annotation of a flow chart is an assignment of a matrix tuple to each edge, consistent with the rules of Figures 3 and 5. Here it is now understood that A, B, C, D denote matrix tuples of the correct dimensions determined by the corresponding typing context. For reasons that will be explained in Section 5.3, we allow flow charts to be annotated with arbitrary matrices, and not just density matrices.

The annotation of a flow chart is uniquely determined by the annotation of its input edges, and can be calculated in a top-down fashion. The semantics of a flow chart is given by the function which maps each annotation of the input edges to the resulting annotation of the output edges. By inspecting the rules in Figures 3 and 5, we observe that this function is necessarily linear. Moreover, it takes adjoints to adjoints, and thus it preserves hermitian matrix tuples. Moreover, this function preserves positivity, and it preserves trace, in the sense that the sum of the traces of the inputs is equal to the sum of the traces of the outputs. Intuitively, the last property reflects the fact that the probability of entering a program fragment is equal to the probability of leaving it. When we introduce loops into the flow chart language in Section 5, we will see that the trace preservation property no longer holds: in the presence of loops, some programs have a non-zero probability of non-termination. For such programs, the trace of the outputs will in general be less than the trace of the inputs.

Remark. By working with tuples of matrices, we separate the classical data (“tuple dimension”) from the quantum data (“matrix dimension”). Formally, it is possible to suppress this distinction by identifying a tuple of matrices (A_{0}, …, A_{k−1}) with the single block matrix which has A_{0}, …, A_{k−1} along the diagonal, and 0 everywhere else. This notation would leave all our operations well-defined, but would incur the additional overhead of having to state explicitly which matrix entries are assumed to be 0. This is analogous to the situation in functional analysis, where an algebra of functions may be represented as an algebra of commuting diagonal operators. However, we do not currently find a conceptual or formal advantage in working with block matrices, and thus we stick to the “tuples of matrices” formulation.

4.8. Static typing and the no-cloning property

By the well-known no-cloning property, it is not possible to duplicate a quantum bit, i.e., there is no physically meaningful operation which maps α |0⟩+β |1⟩ to (α |0⟩+β |1⟩)⊗(α |0⟩+β |1⟩). Semantically, this is immediately clear, as this operation is not linear in α and β, and thus can never be the denotation of any quantum flow chart. One interesting feature of our formulation of quantum flow charts is that cloning is prohibited by the syntax, which means that compliance with the no-cloning property can be checked statically when the program is written, rather than when it is run. This is an improvement over other formalisms (Knill 1996; Ömer 1998; Sanders and Zuliani 2000; Bettelli, Calarco, and Serafini 2001), where cloning is syntactically allowed (via duplication of a reference to a qubit), but can lead to errors at run-time (for instance when a unitary operation is applied to multiple copies of the same qubit).

Formally, our system has the property that within any given typing context, syntactically distinct variables refer to distinct objects at run-time. In particular, in the rule for unitary transformations in Figure 5, q̄ stands for a list of distinct variables. This takes care of the requirement that “multi-qubit operations need distinct physical locations”, in the words of Bettelli et al. (2001, footnote 9). As a consequence, a well-typed program in our language can never produce a runtime error. This property remains true in the presence of loops and recursion, which will be introduced in Section 5, and in the presence of structured types, which will be introduced in Section 7.3.

4.9. Examples

We give some examples of flow charts that can be built from the basic components.

Example 4.1. The first example shows how a probabilistic fair coin toss can be implemented, i.e., a branching statement which selects one of two possible outcomes with equal probability.

[figure: Γ = A; new qbit q := 0; q : qbit, Γ = ( \begin{matrix}A & 0 \\ 0 & 0 \end{matrix} ); q ∗= H; measure q; 0; q : qbit, Γ = \frac{1}{2}( \begin{matrix}A & 0 \\ 0 & 0 \end{matrix} ); 1; q : qbit, Γ = \frac{1}{2}( \begin{matrix}0 & 0 \\ 0 & A \end{matrix} ); q : qbit, Γ = \frac{1}{2}( \begin{matrix}A & A \\ A & A \end{matrix} ); discard q; Γ = \frac{1}{2}A; discard q; Γ = \frac{1}{2}A]

Here, H is the Hadamard matrix introduced in Section 3.3. Coin tosses with probabilities other than \frac{1}{2} can be implemented by replacing H with some other appropriate unitary matrix.

Example 4.2. The next example shows the correctness of a program transformation: a measurement followed by deallocation is equivalent to a simple deallocation.

[figure: measure q; 0; 1; q : qbit, Γ = ( \begin{matrix}A & B \\ C & D \end{matrix} ); q : qbit, Γ = ( \begin{matrix}A & 0 \\ 0 & 0 \end{matrix} ); q : qbit, Γ = ( \begin{matrix}0 & 0 \\ 0 & D \end{matrix} ); ∘; q : qbit, Γ = ( \begin{matrix}A & 0 \\ 0 & D \end{matrix} ); discard q; Γ = A + D]

q : qbit, Γ = ( \begin{matrix}A & B \\ C & D \end{matrix} )

discard q

Γ = A + D

The correctness of this program transformation is of course due to the fact that the “discard” operation already has an implicit measurement built into it.

Example 4.3. This example shows how to define a “rename” operation for renaming a variable of type qbit. The given implementation is not very efficient, because it involves the application of a quantum gate. In practice, a compiler might be able to implement such renamings by a pointer operation with minimal runtime cost.

q : qbit, Γ = A
rename p ← q
p : qbit, Γ = A

is definable as

q : qbit, Γ = A
new qbit p := 0
p ⊕= q
q ⊕= p
discard q
p : qbit, Γ = A

Of course, variables of type bit can be similarly renamed. We will from now on use the rename operation as if it were part of the language.

Example 4.4. This example formalizes a point that was made in Section 4.1: a control edge labeled with a classical bit is equivalent to two parallel control edges. In other words, the following two constructions are mutually inverse:

[figure: Γ = A; Γ = B; new bit b := 0; b : bit, Γ = (A, 0); new bit b := 1; b : bit, Γ = (0, B); ∘; b : bit, Γ = (A, B); branch b; 0; b : bit, Γ = (A, 0); 1; b : bit, Γ = (0, B); b : bit, Γ = (A, B); discard b; Γ = A; discard b; Γ = B]

Example 4.5. This example shows that if a program fragment X has an outgoing edge which is reached with probability 0, then this edge can be eliminated. Here we have used an obvious abbreviation for multiple “discard” and “new” nodes.

[figure: Γ = 0; X; Γ′ = A; discard Γ; ∅ = 0; new Γ′; Γ′ = 0; ∘; Γ′ = A]

Example 4.6. The next example shows something more interesting: it is possible to collapse a quantum bit q by means of a coin toss, without actually measuring q. The coin toss can be implemented as in Example 4.1. Let Γ = q : qbit, Γ′.

[figure: coin toss; 0; 1; Γ = \frac{1}{2}( \begin{matrix}A & B \\ C & D \end{matrix} ); Γ = ( \begin{matrix}A & B \\ C & D \end{matrix} ); Γ = \frac{1}{2}( \begin{matrix}A & B \\ C & D \end{matrix} ); q ∗= ( \begin{matrix}1 & 0 \\ 0 & −1 \end{matrix} ); Γ = \frac{1}{2}( \begin{matrix}A & −B \\ −C & D \end{matrix} ); ∘; Γ = ( \begin{matrix}A & 0 \\ 0 & D \end{matrix} ); measure q; 0; 1; Γ = ( \begin{matrix}A & B \\ C & D \end{matrix} ); Γ = ( \begin{matrix}A & 0 \\ 0 & 0 \end{matrix} ); Γ = ( \begin{matrix}0 & 0 \\ 0 & D \end{matrix} ); ∘; Γ = ( \begin{matrix}A & 0 \\ 0 & D \end{matrix} )]

This example shows that it is possible for two programs to have the same observable behavior, despite the fact that their physical behavior is obviously different. In particular, the correctness of the left program depends critically on the fact that the outcome of the coin toss is “forgotten” when the two control paths are merged.

To understand this example, it is helpful to consider what each of these programs would do when run in the pure state uu^{∗}, where u = \frac{3}{5} |0⟩ + \frac{4}{5} |1⟩. In this case, A = \frac{9}{25}, B = C = \frac{12}{25}, and D = \frac{16}{25}. The left program will leave the state unchanged with probability 1/2, and perform a phase change with probability 1/2. Thus, it ends up in the mixed state \frac{1}{2} \{\frac{3}{5} |0⟩ + \frac{4}{5} |1⟩\} + \frac{1}{2} \{\frac{3}{5} |0⟩ − \frac{4}{5} |1⟩\}. The right program simply measures the qubit, leading to the mixed state \frac{9}{25} \{|0⟩\}+ \frac{16}{25} \{|1⟩\}. The point of the example is that these two different mixed states correspond to the same density matrix, and thus they are indistinguishable:

\frac{1}{2}( \begin{matrix}\frac{9}{25} & \frac{12}{25} \\ \frac{12}{25} & \frac{16}{25} \end{matrix} ) + \frac{1}{2}( \begin{matrix}\frac{9}{25} & \frac{−12}{25} \\ \frac{−12}{25} & \frac{16}{25} \end{matrix} ) = ( \begin{matrix}\frac{9}{25} & 0 \\ 0 & \frac{16}{25} \end{matrix} ), \frac{9}{25}( \begin{matrix}1 & 0 \\ 0 & 0 \end{matrix} ) + \frac{16}{25}( \begin{matrix}0 & 0 \\ 0 & 1 \end{matrix} ) = ( \begin{matrix}\frac{9}{25} & 0 \\ 0 & \frac{16}{25} \end{matrix} )

Note that, if the outcome of the coin toss is known to some outside observer (e.g., to someone who has been eavesdropping), then it is possible, for this outside observer, to restore the initial state of the left program from its final state, simply by undoing the conditional unitary operation. On the other hand, the initial state of the right program is irretrievably lost after the measurement. This apparent paradox is due to the fact, as discussed in Section 3.5, that a mixed state is a description of our knowledge of a physical state, rather than of a physical state itself. The two program fragments are equivalent in the sense that they will behave in the same way as part of any larger program, not in the sense that they cannot be distinguished by an outside observer with privileged knowledge. It is precisely for this reason that the theory of quantum communication, i.e., of quantum programs which interactive input and output, is much more complicated than the theory of closed-world programs considered in this paper.

Example 4.7. This example shows that the discarding of a quantum bit can always be postponed. Thus, the following two flow charts are equivalent, provided that X is a flow chart not containing q:

[figure: q : qbit, Γ_{1}; q : qbit, Γ_{n}; discard q; Γ_{1}; ⋯; discard q; Γ_{n}; Δ_{1}; ⋯; Δ_{m}; X; q : qbit, Γ_{1} ⋯; q : qbit, Γ_{n}; q : qbit, Δ_{1}; q : qbit, Δ_{m}; X; discard q; Δ_{1}; ⋯; discard q; Δ_{m}]

This can be easily shown by induction on flow charts. Note that this observation implies that there is effectively no difference between “discarding” a quantum bit and simply “forgetting” the quantum bit (by dropping any reference to it). In particular, it is not observable whether a “forgotten” quantum bit has been collapsed or not. This is true even if the quantum bit was entangled with other data in the computation — but only as long as no information about the collapsed quantum bit is leaked back to the program, not even via a third party such as the operating system. Because of the possibility of such unintended leaks, the discard operation should in practice always be implemented via an explicit collapse of the kind discussed at the end of Section 4.6.

5. Loops, procedures, and recursion

5.1. Loops

A loop in a flow chart is constructed as in the following illustration:

[figure: (∗∗); (∗); …; …; X]

Here X stands for an arbitrary flow chart fragment with n + 1 incoming and m + 1 outgoing edges. After adding the loop, there are n incoming and m outgoing edges left. The semantics of loops is initially defined by “infinite unwinding”. The above loop is unwound as follows:

[figure: ∘; F_{11}(A); (∗) A; F_{21}(A); X; ∘; F_{22}F_{21}(A); X; ∘; F_{11}(A) + F_{12}F_{21}(A); ∘; F_{22}F_{22}F_{21}(A); X; ∘; ⋮; (∗∗) G(A); ⋮]

Here, we have simplified the typesetting by representing potential multiple parallel control edges by a single line. Thus, A = (A_{1}, …, A_{n}) denotes a tuple of input matrices. We can decorate the unwound diagram with states in the usual top-down fashion. Specifically, suppose that the semantics of X is given by the linear function F (A_{1}, …, A_{n}, B) = (C_{1}, …, C_{m}, D). We can split this function into four components F_{11}, F_{12}, F_{21}, and F_{22} such that F (A, 0) = (F_{11}(A), F_{21}(A)) and F (0, B) = (F_{12}(B), F_{22}(B)). Then we can label the states of the unwound loop diagram as shown in the illustration above. We find that the state at the edge (or tuple of edges) labeled (∗∗) is given by the infinite sum

G(A) = F_{11}(A) + ∑_{i=0}^{∞} F_{12}(F_{22}^{i}(F_{21}(A))).  (1)

This formula is similar to the execution formula of Girard’s Geometry of Interaction (Girard

(a) [figure: input p, q : qbit; Proc1:; p, q : qbit; p, q ∗= S; measure p; 0; 1; p, q : qbit; p, q : qbit; p, q : qbit; discard p; q : qbit; output1 p, q : qbit; output2 q : qbit] (b) [figure: a : qbit; ∘; a : qbit; new qbit b := 0; a, b : qbit; input a, b; output1 c, d; c, d : qbit; output2 a; Proc1; a : qbit]

Fig. 6. A procedure and a procedure call

1989). We will see in Section 6 that this sum indeed always converges. Furthermore, if A is positive, then so is G(A), and trG(A) ⩽ trA.

An interesting point is that the inequality of traces may be strict, i.e., it is possible that trG(A) < trA. This can happen if there is a non-zero probability that the loop may not terminate. In this case, the probability that the program reaches state (∗∗) is strictly less than the probability that it reaches state (∗).

Note that the formula (1) allows us to calculate the semantics of the loop directly from the semantics of X, without the need to unwind the loop explicitly. This is an example of a compositional semantics, which we will explore in more detail in Section 6.

5.2. Procedures

A procedure is a flow chart fragment with a name and a type. Consider for example the procedure Proc1 defined in Figure 6(a). This procedure has one entrance and two possible exits. The input to the procedure is a pair of qubits. The output is a pair of qubits when exiting through the first exit, or a single qubit when exiting through the second exit. The type of the procedure captures this information, and it is

Proc1 : qbit × qbit → qbit × qbit; qbit.

Here, it is understood that “×” binds more tightly than “;”. In general, the type of a procedure is of the form Γ̄ → Γ̄′, where Γ̄, Γ̄′ are lists of products of basic types. Most procedures have a single entrance and a single exit, but there is no general reason why this should be so; we allow procedures with multiple entrances as well as multiple exits.

Figure 6(b) shows an example of a call to the procedure Proc1 just defined. The example illustrates several points. The procedure call respects the type of the procedure, in the sense that it has as many incoming and outgoing edges as the procedure, and the number and type of parameters matches that of the procedure. The actual parameters are named inside the procedure call box. The order of the parameters is significant, and they are subject to one important restriction: the parameters corresponding to any one procedure entrance or exit must be distinct. Thus, we cannot for instance invoke Proc1 with parameters (a, a).

We do not require that the names of the actual parameters match those of the formal parameters. For instance, in Figure 6, the actual parameters a, b correspond to the formal parameters p, q in the input of the procedure. We do not even require that the actual parameters must match the formal parameters consistently: for instance, the formal parameter q corresponds to the actual parameter b in the input, but to a in the second output. This is not important, as the compiler can implicitly insert renaming operations as in Example 4.3.

In general, a procedure may be called in a context which contains other variables besides those that are parameters to the procedure call. For instance, the procedure of Figure 6(a) can be invoked in the presence of an additional typing context Γ as follows:

[figure: a, b : qbit, Γ; input a, b; output1 c, d; c, d : qbit, Γ; output2 a; Proc1; a : qbit, Γ]

Here, the set Γ of unused variables must be identical for all inputs and outputs of the procedure call. Intuitively, the variables in Γ are “not changed” by the procedure call; however, in reality, the behavior is more subtle because some of the variables from Γ might be quantum entangled with the procedure parameters, and thus may be indirectly affected by the procedure call. However, we will see that the semantics of procedure calls is nevertheless compositional; i.e., once the behavior of a procedure is known in the empty context, this uniquely determines the behavior in any other context.

5.3. Context extension

Before we can fully describe the semantics of procedure calls, we first need to explore the concept of context extension, by which we mean the addition of dummy variables to a flow chart. Recall that the semantics of a flow chart X is given by a linear function F from matrix tuples to matrix tuples, as discussed in Section 4.7. This situation is shown schematically in Figure 7(a). In general, X may have several incoming and outgoing control edges, but for simplicity we consider the case where there is only one of each.

Now suppose that we modify the flow chart X by picking a fresh boolean variable b and adding it to the context of all the edges of X. The result is a new flow chart X_{b}, which is schematically shown in Figure 7(b). We claim that the semantics of the modified flow chart X_{b} is given by G(A, B) = (F (A), F (B)). This is easily proved by induction on flow charts: all the basic components have this property, and the property is preserved under horizontal and vertical

(a)  [figure: Γ = A; X; Γ′ = F (A)] (b)  [figure: b : bit, Γ = (A, B); X_{b}; b : bit, Γ′ = (F (A), F (B))] (c)  [figure: q : qbit, Γ = ( \begin{matrix}A & B \\ C & D \end{matrix} ); X_{q}; q : qbit, Γ′ = ( \begin{matrix}F (A) & F (B) \\ F (C) & F (D) \end{matrix} )]

Fig. 7. Context extension

composition and under the introduction of loops. Intuitively, since the variable b does not occur in X, its value is neither altered not does it affect the computation of F.

Analogously, we can modify X by adding a fresh quantum variable q to all its edges, as shown schematically in Figure 7(c). Then the semantics of the modified chart X_{q} is given by the function

G( \begin{matrix}A & B \\ C & D \end{matrix} ) = ( \begin{matrix}F (A) & F (B) \\ F (C) & F (D) \end{matrix} ).

This, too, is easily proved by induction.

Remark 5.1. At this point, we should make the following interesting observation. Note that, if

( \begin{matrix}A & B \\ C & D \end{matrix} ) is a density matrix, then so are A and D, but not necessarily B and C. In fact, up

to a scalar multiple, B may be completely arbitrary. If the function F had been defined only on density matrices, then F (B) and F (C) would be in general undefined, and thus, G would be undefined. This is the reason why, in Section 4.7, we defined the semantics of a flow chart to be a function on arbitrary matrices, and not just on density matrices, as one might have expected.

However, this remark is only of notational, not of fundamental, importance. By Remark 2.1, the density matrices span ℂ^{n×n} as a complex vector space. Since F is a linear function, this implies that F is already determined by its value on density matrices. Thus, the fact that F is given as a function on all matrices conveys no additional information.

5.4. Semantics of non-recursive procedure calls

The intended semantics of a non-recursive procedure call is that of “inlining”: a procedure call should behave exactly as if the body of the procedure was inserted in its place. Before the procedure body can be inserted, it needs to be transformed in two steps: first, appropriate renamings (as in Example 4.3) need to be inserted to match the formal parameters with the actual ones. Second, the context of the procedure body needs to be extended in the sense of Section 5.3, i.e., all variables in the context of the procedure call that are not parameters must be added as dummy variables to the procedure body. If necessary, the local variables of the procedure body must be renamed to avoid name clashes with these dummy variables.

The semantics of a procedure call can be computed compositionally, i.e., without having to do the actual inlining. Namely, the renaming step does not affect the semantics at all, and the semantics of the context extension step can be computed as in Section 5.3.

Peter Selinger 32

(a) [figure: input p, q : qbit; X:; measure p; 0; 1; p, q : qbit; p, q : qbit; p, q : qbit; new qbit r := 0; p, q, r : qbit; q, r ∗= H_{c}; p, q, r : qbit; input q, r; output q, r; X; p, q, r : qbit; q ⊕= r; p, q, r : qbit; discard r; p, q : qbit; ∘; p, q : qbit; output p, q : qbit] (b) [figure: input p, q : qbit; X:; measure p; 0; 1; p, q : qbit; p, q : qbit; p, q : qbit; new qbit r := 0; p, q, r : qbit; q, r ∗= H_{c}; measure q; 0; 1; p, q, r : qbit; p, q, r : qbit; p, q, r : qbit; new qbit s := 0; p, q, r, s : qbit; r, s ∗= H_{c}; p, q, r, s : qbit; ⋯; p, q, r, s : qbit; r ⊕= s; p, q, r, s : qbit; discard s; p, q, r : qbit; ∘; p, q, r : qbit; q ⊕= r; p, q, r : qbit; discard r; p, q : qbit; ∘; p, q : qbit; output p, q : qbit]

Fig. 8. A recursive procedure and its unwinding

5.5. Recursive procedures

A procedure is recursive if it invokes itself, either directly or indirectly. An example of a recursive procedure is shown in Figure 8(a). Before reading on, the reader is invited to figure out what this procedure does.

The intended semantics of recursive procedures is given by infinite unwinding, similar to the way we treated loops. Unwinding the procedure X from Figure 8(a) yields the infinite flow chart shown in Figure 8(b). This example demonstrates that the unwinding of a recursive procedure may lead to a flow chart with an unbounded number of variables, as new local variables are introduced at each level of nesting. The typing conventions enforce that such qubits will eventually be deallocated before the procedure returns.

To compute the semantics of a recursive procedure, we could, in principle, annotate its infinite unwinding with states just as we did for loops. However, since the number of variables keeps increasing with each nesting level, this would require writing an infinite number of larger and larger matrices, and the computation of the resulting limits would be rather cumbersome. Therefore, we skip the explicit annotation and move on to a more denotational (and more practical) approach to calculating the semantics of X.

To find a good description of the unwinding process, let us write X(Y) for the flow chart which is the same as X, except that it has another flow chart Y substituted in place of the recursive call. We can then define the ith unwinding of X to be the flow chart Y_{i}, where Y_{0} is a non-terminating program, and Y_{i+1} = X(Y_{i}).

Now let us write F_{i} for the semantics of the flow chart Y_{i} just defined. By compositionality, the semantics of X(Y) is a function of the semantics of Y. If Φ denotes this function, then we can recursively compute F_{i} for all i via the clauses F_{0} = 0 and F_{i+1} = Φ(F_{i}). Finally, it is natural to define the semantics of X to be the limit of this sequence,

G = lim_{i→∞} F_{i}.  (2)

The existence of this limit will be justified in Section 6. For now, let us demonstrate the use of this method by computing the denotation of the sample flow chart from Figure 8. If A = (a_{ij})_{ij}, then we find

F_{1}(A) = ( \begin{matrix}a_{00} & a_{01} & 0 & 0 \\ a_{10} & a_{11} & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \end{matrix} ), F_{2}(A) = ( \begin{matrix}a_{00} & a_{01} & 0 & 0 \\ a_{10} & a_{11} & 0 & 0 \\ 0 & 0 & a_{22} & 0 \\ 0 & 0 & 0 & 0 \end{matrix} ), F_{3}(A) = ( \begin{matrix}a_{00} & a_{01} & 0 & 0 \\ a_{10} & a_{11} & 0 & 0 \\ 0 & 0 & a_{22} & 0 \\ 0 & 0 & 0 & \frac{1}{2}a_{33} \end{matrix} ),

F_{4}(A) = ( \begin{matrix}a_{00} & a_{01} & 0 & 0 \\ a_{10} & a_{11} & 0 & 0 \\ 0 & 0 & a_{22} + \frac{1}{4}a_{33} & 0 \\ 0 & 0 & 0 & \frac{1}{2}a_{33} \end{matrix} ), F_{5}(A) = ( \begin{matrix}a_{00} & a_{01} & 0 & 0 \\ a_{10} & a_{11} & 0 & 0 \\ 0 & 0 & a_{22} + \frac{1}{4}a_{33} & 0 \\ 0 & 0 & 0 & \frac{1}{2}a_{33} + \frac{1}{8}a_{33} \end{matrix} ),

F_{6}(A) = ( \begin{matrix}a_{00} & a_{01} & 0 & 0 \\ a_{10} & a_{11} & 0 & 0 \\ 0 & 0 & a_{22} + \frac{1}{4}a_{33} + \frac{1}{16}a_{33} & 0 \\ 0 & 0 & 0 & \frac{1}{2}a_{33} + \frac{1}{8}a_{33} \end{matrix} ),

and so forth. The limit is

G(A) = ( \begin{matrix}a_{00} & a_{01} & 0 & 0 \\ a_{10} & a_{11} & 0 & 0 \\ 0 & 0 & a_{22} + \frac{1}{3}a_{33} & 0 \\ 0 & 0 & 0 & \frac{2}{3}a_{33} \end{matrix} ),

and this is the denotation of X. Note that, in this example, trG(A) = trA, which means this particular procedure terminates with probability 1. In general, it is possible that trG(A) < trA.

(a) [figure: X] (b) [figure: ∘; A] (c) A : [figure: X; ∘; A; ∘]

Fig. 9. Loops from recursion

5.6. Recursion vs. loops

It is possible to encode loops in terms of recursion. Namely, the loop in Figure 9(a) can be expressed as the procedure call (b), where A is defined recursively as in (c). On the other hand, recursion cannot in general be encoded in terms of loops. This is because recursive procedures can allocate an unbounded number of variables.

6. Formal semantics

In this section, we give a more systematic and formal treatment of the semantics of quantum flow charts. We justify the well-definedness of the various constructions that were introduced informally in Sections 4 and 5, and in particular the existence of the limits in equations (1) and (2). We also consider a more abstract view of the semantics in terms of CPO-enriched traced monoidal categories.

6.1. Signatures and matrix tuples

As outlined informally in Section 4.7, the denotation of a flow chart is given as a certain linear function from matrix tuples to matrix tuples. We begin our formal semantics by defining the spaces of such matrix tuples.

A signature is a list of non-zero natural numbers σ = n_{1}, …, n_{s}. To each signature we associate a complex vector space

V_{σ} = ℂ^{n_{1}×n_{1}} × … × ℂ^{n_{s}×n_{s}}.

The elements of V_{σ} are tuples of matrices of the form A = (A_{1}, …, A_{s}), where the number and dimensions of the matrices A_{i} are determined by σ. As before, we often use the letters A, B, … to denote elements of V_{σ}. We define the trace of a matrix tuple to be the sum of the traces of its components:

trA := ∑_{i} trA_{i}.

We say that a matrix tuple A ∈ V_{σ} is hermitian (respectively, positive) if A_{i} is hermitian (respectively, positive) for all i. We define the set D_{σ} ⊆ V_{σ} of density matrix tuples to be the obvious generalization of the set D_{n} of density matrices:

D_{σ} = {A ∈ V_{σ} | A positive hermitian and trA ⩽ 1}.

We extend the Löwner partial order to matrix tuples by letting A ⊑ B if B − A is positive. This makes D_{σ} into a complete partial order with least element 0. Completeness follows from Proposition 3.6, together with the fact that D_{σ} is a closed subset of D_{n_{1}} × … × D_{n_{s}}.

Definition (Special signatures). Several signatures have special names; we write

bit = 1, 1
qbit = 2
I = 1
0 = ϵ (the empty list)

We call a signature simple if it is a singleton list. Thus, for example, qbit and I are simple, whereas bit and 0 are not.

6.2. The category V

Definition. The category V has as its objects signatures σ = n_{1}, …, n_{s}. A morphism from σ to σ′ is any complex linear function F : V_{σ} → V_{σ′}.

Note that V, as a category, is equivalent to the category of finite dimensional complex vector spaces. However, we will later use the additional, non-categorical structure on objects to define an interesting subcategory Q which has fewer isomorphic objects than V.

Let σ ⊕ σ′ denote concatenation of signatures. Then σ ⊕ σ′ is a product and coproduct in V, with the obvious injection and projection maps. The co-pairing map [F, G] : σ ⊕ σ′ → τ is given by [F, G](A, B) = F (A) + G(B), and the pairing map ⟨F, G⟩ : σ → τ ⊕ τ′ is given by ⟨F, G⟩(A) = (F A, GA). The neutral object for this biproduct is the empty signature 0.

Tensor product. If σ = n_{1}, …, n_{s} and τ = m_{1}, …, m_{t} are signatures, their tensor product σ ⊗ τ is defined as

σ ⊗ τ = n_{1}m_{1}, …, n_{1}m_{t}, …, n_{s}m_{1}, …, n_{s}m_{t}.

Note that the components of σ ⊗ τ are ordered lexicographically. The operation ⊗ extends to a symmetric monoidal structure on V with unit I = 1. The morphism part of the tensor product is defined as in the category of vector spaces; thus, if F : σ → τ and G : σ′ → τ′, then F ⊗G : σ⊗σ′ → τ ⊗τ′ is defined on a basis element A⊗B via (F ⊗G)(A⊗B) = F (A)⊗G(B), and extends to arbitrary elements by linearity. We note that this monoidal structure is strict (i.e., the associativity and unit morphisms are identity maps, rather than just isomorphisms). We also have the following strict distributivity law:

(σ ⊕ σ′) ⊗ τ = (σ ⊗ τ) ⊕ (σ′ ⊗ τ).

6.3. Superoperators

Definition (Completely positive operator, superoperator). Let F : V_{σ} → V_{σ′} be a linear function. We say that F is positive if F (A) is positive for all positive A ∈ V_{σ}. We say that F is completely positive if id_{τ} ⊗ F : V_{τ⊗σ} → V_{τ⊗σ′} is positive for all signatures τ. Finally, F is called a superoperator if it is completely positive and satisfies the following trace condition: trF (A) ⩽ trA, for all positive A ∈ V_{σ}.

Remark 6.1. In the physics literature, superoperators are usually assumed to be trace preserving, i.e., satisfying trF (A) = trA for all A (see e.g. (Preskill 1998)). In our setting, it is appropriate to relax this condition in view of possible non-termination of programs.

Example 6.2. To illustrate the concept of a completely positive operator, consider the following three linear maps F_{i} : V_{2} → V_{2}.

F_{1}( \begin{matrix}a & b \\ c & d \end{matrix} ) = ( \begin{matrix}a & 2b \\ 2c & d \end{matrix} ), F_{2}( \begin{matrix}a & b \\ c & d \end{matrix} ) = ( \begin{matrix}a & c \\ b & d \end{matrix} ), F_{3}( \begin{matrix}a & b \\ c & d \end{matrix} ) = ( \begin{matrix}a & 0 \\ 0 & d \end{matrix} ).

All three maps are linear, and thus morphisms in V. Also, all three maps preserve trace and hermitian matrices. F_{1} is not positive, because it maps a positive matrix to a non-positive matrix:

F_{1}( \begin{matrix}1 & 1 \\ 1 & 1 \end{matrix} ) = ( \begin{matrix}1 & 2 \\ 2 & 1 \end{matrix} ).

F_{2} is positive, but not completely positive. While F_{2} maps positive matrices to positive matrices, the same is not true for id_{2} ⊗ F_{2}, for instance,

(id_{2} ⊗ F_{2})( \begin{matrix}1 & 0 & 0 & 1 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 1 & 0 & 0 & 1 \end{matrix} ) = ( \begin{matrix}1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{matrix} ),

which is not positive. Finally, F_{3} is completely positive.

As we will see in Section 6.9, superoperators are precisely the functions which arise as the denotations of quantum programs.

Lemma 6.3. The following hold in the category V:

(a) Identity morphisms are superoperators, and superoperators are closed under composition.

(b) The canonical injections in_{1} : σ → σ ⊕ σ′ and in_{2} : σ′ → σ ⊕ σ′ are superoperators, and if F : σ → σ′ and G : τ → σ′ are superoperators, then so is [F, G] : σ ⊕ τ → σ′.

(c) If F : σ → σ′ and G : τ → τ′ are superoperators, then so are F ⊕ G : σ ⊕ σ′ → τ ⊕ τ′ and F ⊗ G : σ ⊗ σ′ → τ ⊗ τ′.

(d) A morphism F : σ → σ′ is completely positive if and only if id_{τ} ⊗F is positive, for all simple signatures τ.

(e) Let S be a unitary n × n-matrix. Then the morphism F : n → n defined by F (A) = SAS^{∗} is a superoperator.

(f) Let S_{1} and S_{2} be n × n-matrices such that S_{1}^{∗}S_{1} + S_{2}^{∗}S_{2} = I. Then the morphism F : n → n, n defined by F (A) = (S_{1}AS_{1}^{∗}, S_{2}AS_{2}^{∗}) is a superoperator.

Proof. (a) and (b) are trivial. The first part of (c) follows from (b). For the second part of (c), note that F ⊗ G = (id_{σ′} ⊗ G) ∘ (F ⊗ id_{τ}). The two component maps are completely positive by definition, and they clearly satisfy the trace condition. For (d), only the right-to-left implication is interesting. Any object τ can be written as a sum τ = τ_{1} ⊕ … ⊕ τ_{t} of simple objects. Then by distributivity, id_{τ} ⊗ F = (id_{τ_{1}} ⊗ F) ⊕ … ⊕ (id_{τ_{t}} ⊗ F), which is positive by assumption and (c).

For (e), first note that if A is positive, then so is SAS^{∗}, and trSAS^{∗} = trA. Thus F is positive and satisfies the trace condition. To see that it is completely positive, note that for any n and identity n × n-matrix I, (id_{n} ⊗ F)(A) = (I ⊗ S)A(I ⊗ S)^{∗}. But I ⊗ S is unitary, thus id_{n} ⊗ F is again of the same form as F, hence positive. By (d), it follows that F is a superoperator. For (f), note that F preserves positivity and trace, thus F is positive. The fact that it is completely positive follows as in (e).

6.4. The category Q

Definition. The category Q is the subcategory of V which has the same objects of V, and whose morphisms are the superoperators.

By Lemma 6.3(a)–(c), Q is indeed a subcategory of V, and it inherits coproducts and the symmetric monoidal structure from V. However, unlike V, the category Q does not have finite products. This is because the diagonal morphism ⟨id, id⟩ : τ → τ ⊕ τ does not respect trace, and hence it is not a superoperator. However, the two projections π_{1} : σ ⊕ σ′ → σ and π_{2} : σ ⊕ σ′ → σ′ are present in Q.

Also note that the category Q distinguishes more objects than V; for instance, the objects bit ⊕ bit = 1, 1, 1, 1 and qbit = 2 are isomorphic in V, but not in Q.

CPO-enrichment. Recall that D_{σ} is the subset of V_{σ} consisting of density matrix tuples, i.e., of positive matrix tuples A with trA ⩽ 1. Every superoperator F : V_{σ} → V_{τ} restricts to a function F : D_{σ} → D_{τ}. We note that F respects the Löwner partial order: if A ⊑ B ∈ D_{σ}, then B = A+A′ for some A′ ∈ D_{σ}, and thus F (B) = F (A)+F (A′), which implies F (A) ⊑ F (B). Also, F : D_{σ} → D_{σ′} preserves least upper bounds of increasing sequences. This follows from Remark 3.8 and the fact that F, as a linear function on a finite-dimensional vector space, is continuous with respect to the usual Euclidean topology. Thus, we obtain a forgetful functor D : Q → CPO, from Q to the category of complete partial orders, which maps σ to D_{σ} and F to itself.

If σ and σ′ are objects of Q, we can also define a partial order on the hom-set Q(σ, σ′), by letting F ⊑ G if for all τ and all A ∈ D_{τ⊗σ}, (id_{τ} ⊗ F)(A) ⊑ (id_{τ} ⊗ G)(A).

Lemma 6.4. The poset Q(σ, σ′) is a complete partial order.

Proof. Let F_{0} ⊑ F_{1} ⊑ … be an increasing sequence of morphisms in Q(σ, σ′). Define F : D_{σ} → D_{σ′} as the pointwise limit: F (A) = ⋁_{i} F_{i}(A). By Remark 3.8, F (A) is also the topological limit F (A) = lim_{i→∞} F_{i}(A), and it follows by continuity that F is linear on the convex subset D_{σ} ⊆ V_{σ}. Since, by Remark 2.1, D_{σ} spans V_{σ}, F can be extended to a unique linear function F : V_{σ} → V_{σ′}, i.e., to a morphism of V. F satisfies the trace condition and is positive by construction. To see that it is completely positive, note that for any object τ and any B ∈ D_{τ⊗σ}, (τ ⊗F)(B) = lim_{i→∞}(τ ⊗F_{i})(B), and hence τ ⊗F is positive for the same reason as F. Thus, F : σ → σ′ is a morphism of Q, and hence the desired least upper bound of (F_{i})_{i}.

Also, the categorical operations (composition, co-pairing, and tensor) are Scott-continuous, i.e., they preserve least upper bounds of increasing sequences. This makes Q into a CPOenriched category.

Trace. A monoidal trace on a monoidal category (Q, ⊕) is a natural family of operations

Tr_{σ,σ′}^{τ} : Q(σ ⊕ τ, σ′ ⊕ τ) → Q(σ, σ′),

subject to a number of equations (Joyal, Street, and Verity 1996; Hasegawa 1997; Selinger 1999). A monoidal category with a monoidal trace is called a traced monoidal category. A monoidal trace is usually just called a “trace”, but we add the adjective “monoidal” here to avoid confusion with the trace of a matrix as in Section 2.1.

The category Q is equipped with a monoidal trace for the monoid which is given by coproducts ⊕ (not for the tensor product ⊗). In fact, the construction of this monoidal trace is an instance of a general construction which works in any CPO-enriched category with coproducts.

To define the monoidal trace of a morphism F : σ ⊕ τ → σ′ ⊕ τ, we construct a family of morphisms H_{i} : σ ⊕ τ → σ′ as follows. We let H_{0} = 0, the constant zero function. For all i, we define H_{i+1} = [id_{σ′}, H_{i} ∘ in_{2}] ∘ F. Then H_{0} ⊑ H_{1}, because H_{0} is the least element in the given partial order. By monotonicity of the categorical operations (a consequence of CPOenrichment), it follows that H_{i} ⊑ H_{i+1} for all i. Hence (H_{i})_{i} is an increasing sequence. Let H = ⋁_{i} H_{i} : σ ⊕ τ → σ′ be the least upper bound. Finally, define Tr F = H ∘ in_{1} : σ → σ′. It is standard to check that this construction indeed defines a monoidal trace, i.e., that it satisfies all the necessary equations (Hasegawa 1997).

In more concrete terms, suppose that F : σ ⊕ τ → σ′ ⊕ τ has been decomposed into components F_{11} : σ → σ′, F_{21} : σ → τ, F_{12} : τ → σ′, and F_{22} : τ → τ as in Section 5.1. Then we have

H_{0}(A, 0) = 0,
H_{1}(A, 0) = F_{11}(A),
H_{2}(A, 0) = F_{11}(A) + F_{12}F_{21}(A), etc,

so that

(Tr F)(A) = H(A, 0) = F_{11}(A) + ∑_{i=0}^{∞} F_{12}(F_{22}^{i}(F_{21}(A))).

Comparing this to equation (1) of Section 5.1, we find that the monoidal trace is precisely the construction we need for the interpretation of loops. In particular, this justifies the convergence of the infinite sum in equation (1).

We also note that the monoidal trace is related to the tensor ⊗ by the following property: if F : σ ⊕ τ → σ′ ⊕ τ, and ρ is any object, then

Tr(F ⊗ ρ) = (Tr F) ⊗ ρ.

Here it is understood that we identify the objects (σ ⊕ τ) ⊗ ρ and (σ ⊗ ρ) ⊕ (τ ⊗ ρ) (which happen to be identical anyway). We can summarize this property together with distributivity by saying that for any ρ, the functor (−) ⊗ ρ is a traced monoidal functor. We call a traced monoidal category with this additional structure a distributively traced monoidal category.

6.5. The interpretation of flow charts

To each type A, we associate an object [[A]] of the category Q. There are only two types, and their interpretations are suggested by the names of the corresponding objects: [[bit]] = bit and [[qbit]] = qbit. To each typing context Γ = x_{1}:A_{1}, …, x_{n}:A_{n}, we associate an object [[Γ]] as follows:

[[Γ]] = [[A_{1}]] ⊗ … ⊗ [[A_{n}]]

Further, if Γ̄ = Γ_{1}; …; Γ_{n} is a list of typing contexts, we define [[Γ̄]] = [[Γ_{1}]] ⊕ … ⊕ [[Γ_{n}]]. Each quantum flow chart

[figure: Γ_{1}; Γ_{2}; ⋯; Γ_{n}; Γ′_{1}; Γ′_{2}; ⋯; Γ′_{m}; X]

is interpreted as a morphism

[[X]] : [[Γ_{1}]] ⊕ … ⊕ [[Γ_{n}]] → [[Γ′_{1}]] ⊕ … ⊕ [[Γ′_{m}]]

in the category Q. The interpretation is defined by induction on the construction of the flow chart.

Atomic charts. The basic flow charts from Figures 3 and 5, with context Γ empty, are interpreted as the following morphisms.

[[new bit b := 0]] = newbit :  I → bit :  a ↦ (a, 0)
[[new qbit q := 0]] = newqbit : I → qbit :  a ↦ ( \begin{matrix}a & 0 \\ 0 & 0 \end{matrix} )
[[discard b]]  = discardbit : bit → I :  (a, b) ↦ a + b
[[discard q]]  = discardqbit : qbit → I :  ( \begin{matrix}a & b \\ c & d \end{matrix} ) ↦ a + d
[[b := 0]]  = set_{0} :  bit → bit :  (a, b) ↦ (a + b, 0)
[[b := 1]]  = set_{1} :  bit → bit :  (a, b) ↦ (0, a + b)
[[q̄ ∗= S]]  = unitary_{S} : qbit^{n} → qbit^{n} :  A ↦ SAS^{∗}
[[branch b]]  = branch :  bit → bit ⊕ bit :  (a, b) ↦ (a, 0, 0, b)
[[measure q]]  = measure : qbit → qbit ⊕ qbit : ( \begin{matrix}a & b \\ c & d \end{matrix} ) ↦ (( \begin{matrix}a & 0 \\ 0 & 0 \end{matrix} ),( \begin{matrix}0 & 0 \\ 0 & d \end{matrix} ))
[[merge]]  = merge :  I ⊕ I → I :  (a, b) ↦ a + b
[[initial]]  = initial :  0 → I :  () ↦ 0
[[permute ϕ]]  = permute_{ϕ} : A_{1} ⊗ … ⊗ A_{n} → A_{ϕ(1)} ⊗ … ⊗ A_{ϕ(n)}

Here, permute_{ϕ} is the natural permutation map based on the symmetric tensor ⊗. There is also the trivial flow chart, consisting of one edge only, which is naturally interpreted as the identity map, and the flow chart consisting of two wires crossing each other, which is interpreted as the symmetry map for coproducts, [in_{2}, in_{1}] : σ ⊕ τ → τ ⊕ σ.

(a) [figure: ⋯; ⋯; X; ⋯; Y] (b) [figure: ⋯; ⋯; ⋯; X; ⋯; Y] (c) [figure: ⋯; ⋯; X] (d) X : [figure: Y_{i}; ⋯; ⋯; ⋯; ⋯]

Fig. 10. Some composite flow charts

Composite charts. Complex flow charts are built by combining simpler ones in one of the following ways:

— Adding variables (context extension): If Y is obtained from X by adding an additional context Γ to all the edges of X, then

[[Y]] = [[X]] ⊗ [[Γ]].

— Vertical composition: If A is the vertical composition of X and Y, as in Figure 10(a), then

[[A]] = [[Y]] ∘ [[X]].

— Horizontal composition: If B is the horizontal composition of X and Y, as in Figure 10(b),

then

[[B]] = [[X]] ⊕ [[Y]]

— Loops: If C is obtained from X by introducing a loop, as in Figure 10(c), then

[[C]] = Tr([[X]])

Two important results from the theory of traced monoidal categories ensure that this interpretation is well-defined: first, every possible flow chart (not containing procedure calls) can be build up from basic flow charts and the operations in Figure 10(a)-(c). Second, if there is more than one way of constructing a given flow chart from smaller pieces, the equations of distributively traced monoidal categories guarantee that the resulting interpretations coincide, i.e., the interpretation is independent of this choice.

Procedures and recursion. For dealing with procedures, we formally augment the flow chart language with a set Y_{1}, …, Y_{n} of flow chart variables, each with a type Y_{i} : Δ̄_{i} → Δ̄′_{i}. If Y_{i} is such a variable, then we allow the flow chart

[figure: Δ_{i1}; ⋯; Δ_{ik}; Δ′_{i1}; ⋯; Δ′_{il}; Y_{i}]

to be used as a basic component. We write X = X(Y_{1}, …, Y_{n}) for a flow chart X which depends on Y_{1}, …, Y_{n}, a situation which is shown schematically in Figure 10(d). The interpretation of such an X is given relative to an environment ρ, which is an assignment which maps each variable Y_{i} : Δ̄_{i} → Δ̄′_{i} to a morphism ρ(Y_{i}) : σ_{i} → σ′_{i} of the appropriate type in Q. The interpretation [[X]]_{ρ} of X with respect to an environment ρ is given inductively, with base case [[Y_{i}]]_{ρ} = ρ(Y_{i}), and inductive cases as before. In this way, each flow chart X(Y_{1}, …, Y_{n}) defines a function

Φ_{X} : Q(σ_{1}, σ′_{1}) × … × Q(σ_{n}, σ′_{n}) → Q(τ, τ′),

which maps an environment ρ = (f_{1}, …, f_{n}) to [[X]]_{ρ}. An easy induction shows that this function is Scott-continuous; in fact, it is given by a term in the language of distributively traced monoidal categories.

The interpretation of recursive procedures is then given by the solution of fixpoint equations. In concrete terms, if the procedures Y_{1}, …, Y_{n} are defined by mutual recursion by a set of equations Y_{i} = X_{i}(Y_{1}, …, Y_{n}), for i = 1, …, n, then their joint interpretation is given as the least fixpoint of the Scott-continuous function

⟨Φ_{X_{1}}, …, Φ_{X_{n}}⟩ : Q(σ_{1}, σ′_{1}) × … × Q(σ_{n}, σ′_{n}) → Q(σ_{1}, σ′_{1}) × … × Q(σ_{n}, σ′_{n}).

As a special case, let us consider the case of a single recursive procedure Y, defined by Y = X(Y) for some flow chart X. In this case, X defines a Scott-continuous function

Φ_{X} : Q(σ, σ′) → Q(σ, σ′).

The interpretation [[Y]] will be given as the least fixpoint of Φ_{X}. This fixpoint can be calculated as the limit of an increasing sequence F_{0} ⊑ F_{1} ⊑ …, where F_{0} = 0, the constant zero function, and F_{i+1} = Φ_{X}(F_{i}). We find that

[[Y]] = ⋁_{i} F_{i} = lim_{i} F_{i}.

Comparing this to equation (2) of Section 5.5, we find that this least fixpoint is precisely the required interpretation of the recursively defined procedure Y = X(Y). In particular, since least fixpoints of Scott-continuous endofunctions on pointed complete partial orders always exist, this justifies the convergence of the limit in equation (2).

6.6. Structural and denotational equivalence

The interpretation of quantum flow charts can be generalized from the category Q to any category which has the requisite structure.

Definition (Elementary quantum flow chart category). An elementary quantum flow chart category is a symmetric monoidal category with traced finite coproducts, such that A ⊗ (−) is a traced monoidal functor for every object A, together with a distinguished object qbit and morphisms ι : I ⊕ I → qbit and p : qbit → I ⊕ I, such that p ∘ ι = id. Here I is the unit object of the symmetric monoidal structure.

In an elementary quantum flow chart category, we define an object bit := I ⊕I. Then the morphisms newbit, discardbit, set_{0}, set_{1}, branch, merge, and initial, needed in the interpretation of atomic quantum flow charts, are definable from the finite coproduct structure. Furthermore, the morphisms newqbit, discardqbit, and measure can be defined in terms of ι and p. The only additional piece of information needed to interpret quantum flow charts in an elementary quantum flow chart category is an interpretation of built-in unitary operators.

Consider a flow chart language with loops, no recursion, and a certain set of built-in unitary operator symbols. Let C be an elementary quantum flow chart category, and let η be an assignment which maps each built-in n-ary operator symbol S to a morphism η_{S} : qbit^{n} → qbit^{n} in C. Then there is an evident interpretation of quantum flow charts, which maps each X : Γ̄ → Γ̄′ to a morphism [[X]]_{η} : [[Γ̄]] → [[Γ̄′]], defined inductively as in Section 6.5. Further, if the category C is CPO-enriched, then we can also interpret recursively defined flow charts in it.

Definition (Structural equivalence, denotational equivalence). We say that two quantum flow charts X, Y : Γ̄ → Γ̄′ are structurally equivalent if for every elementary quantum flow chart category C and every interpretation η of basic operator symbols, [[X]]_{η} = [[Y]]_{η}. Further, X and Y are said to be denotationally equivalent if [[X]] = [[Y]] for the canonical interpretation in the category Q of signatures and completely positive operators.

Clearly, structural equivalence implies denotational equivalence, and the converse is not true. Structural equivalence is essentially a syntactic notion: if two flow charts are structurally equivalent, then one can be obtained from the other by purely symbolic manipulations, without any assumptions about the behavior of the built-in unitary operators. For instance, the two flow charts in Example 4.2 are structurally equivalent, as are those in Example 4.7. On the other hand, the two flow charts in Example 4.6 are not structurally equivalent. Structural equivalence is probably the smallest reasonable equivalence which one might want to consider on flow charts.

Denotational equivalence, on the other hand, is a semantic notion. It captures precisely our concept of “behavior” of quantum programs, fully taking into account the meaning of the builtin operators. We should remark that, like any denotational notion of “behavior”, denotational equivalence abstracts from some aspects of the actual physical behavior of a system; for instance, issues like the running time or space usage of an algorithm are not modeled. Denotational equivalence is only concerned with the probabilistic input-output relationship of programs. It is the largest possible equivalence on quantum flow charts in the following sense: if X, Y : Γ̄ → Γ̄′ are not denotationally equivalent, then there exists a context C[−] (a flow chart with a “hole”) such that C[X] and C[Y] are of type I → bit, and C[X] evaluates to 0 with a different probability than C[Y].

6.7. Characterizations of completely positive operators and superoperators

We will now give some basic and well-known characterizations of superoperators. These results will be used in Section 6.9 to prove that every superoperator arises as the denotation of a quantum flow chart.

As before, let e_{i} denote the ith canonical unit column vector. The space ℂ^{n×n} of n × nmatrices has a canonical basis (as a vector space), consisting of the matrices E_{ij} = e_{i}e_{j}^{∗}. Any linear function F : ℂ^{n×n} → ℂ^{m×m} is uniquely determined by its action on the basis elements.

Definition (Characteristic matrix, characteristic matrix tuple). The characteristic matrix of a linear function F : ℂ^{n×n} → ℂ^{m×m} is the matrix χ_{F} ∈ ℂ^{nm×nm} defined by

χ_{F} = ( \begin{matrix}F (E_{11}) & ⋯ & F (E_{1n}) \\ ⋮ & ... & ⋮ \\ F (E_{n1}) & ⋯ & F (E_{nn}) \end{matrix} ) .

More generally, let σ = n_{1}, …, n_{s} and τ = m_{1}, …, m_{t} be signatures, and let F : V_{σ} → V_{τ} be a linear function. We define the ij-component of F to be the function F_{ij} = π_{j} ∘ F ∘ in_{i} : ℂ^{n_{i}×n_{i}} → ℂ^{m_{j}×m_{j}}. The characteristic matrix tuple of F is

χ_{F} = (χ_{F_{11}}, …, χ_{F_{1t}}, …, χ_{F_{s1}}, …, χ_{F_{st}}).

Note that if F : V_{σ} → V_{τ} is a linear function, its characteristic matrix tuple is an element χ_{F} ∈ V_{σ⊗τ}. Moreover, F and χ_{F} determine each other uniquely.

One might ask whether it is possible to characterize the completely positive operators, or respectively the superoperators, in terms of a property of their characteristic matrices. This is indeed possible. In the following theorems, we start by considering the simple case, i.e., the case of operators F : ℂ^{n×n} → ℂ^{m×m}. The general non-simple case is treated afterwards.

Theorem 6.5. Let F : ℂ^{n×n} → ℂ^{m×m} be a linear operator, and let χ_{F} ∈ ℂ^{nm×nm} be its characteristic matrix.

(a) F is of the form F (A) = UAU^{∗}, for some U ∈ ℂ^{m×n}, if and only if χ_{F} is pure.

(b) The following are equivalent:

(i) F is completely positive.

(ii) χ_{F} is positive.

(iii)F is of the form F (A) = ∑_{i} U_{i}AU_{i}^{∗}, for some finite sequence of matrices U_{1}, …, U_{k} ∈ ℂ^{m×n}.

Proof. For part (a), observe that the matrix χ_{F} is pure iff it is of the form χ_{F} = uu^{∗}, for some u ∈ ℂ^{nm}. We can write

u = ( v_{1} ; ⋮ ; v_{n} ) ,

for some vectors v_{i} ∈ ℂ^{m}, and let U = (v_{1}| … |v_{n}) ∈ ℂ^{m×n}. Then F (E_{ij}) = v_{i}v_{j}^{∗} = UE_{ij}U^{∗}, for all i, j, and thus F (A) = UAU^{∗} for all A. Conversely if F (A) = UAU^{∗}, then χ_{F} = uu^{∗} with u constructed from U as above.

For part (b), to show (i) ⇒ (ii), it suffices to observe that the matrix

E = ( \begin{matrix}E_{11} & ⋯ & E_{1n} \\ ⋮ & ... & ⋮ \\ E_{n1} & ⋯ & E_{nn} \end{matrix} )

is positive, and that χ_{F} = (id_{n} ⊗ F)(E). To prove that (ii) ⇒ (iii), assume that χ_{F} is positive. Then χ_{F} can be written as a sum of pure matrices, say, χ_{F} = B_{1} + … + B_{k}. For each i = 1, …, k, let F_{i} be the linear operator whose characteristic matrix is χ_{F_{i}} = B_{i}. By part (a), F_{i}(A) = U_{i}AU_{i}^{∗}, hence F (A) = ∑_{i} U_{i}AU_{i}^{∗} as desired. Finally, the implication (iii) ⇒ (i) is trivial.

Corollary 6.6. Two linear functions F, G : ℂ^{n×n} → ℂ^{m×m} satisfy F ⊑ G if and only if χ_{F} ⊑ χ_{G}. Here F ⊑ G is defined as in Section 6.4.

Next, we wish to characterize superoperators, i.e., completely positive operators which satisfy the trace condition. We start with some preliminary observations. First, note that for any signature σ, the trace operator tr_{σ} : V_{σ} → V_{1} is a superoperator. We also call it the erasure map, as it corresponds to an erasure of (quantum and classical) information.

Definition (Trace characteristic matrix tuple). The trace characteristic matrix tuple of a linear function F : V_{σ} → V_{τ} is defined to be χ_{F}^{(tr)} = χ_{tr_{τ} ∘F} ∈ V_{σ}, i.e., the characteristic matrix tuple of tr_{τ} ∘ F. Note that χ_{F}^{(tr)} is easily calculated by taking a “partial trace” of χ_{F}, i.e., χ_{F}^{(tr)} = (id_{σ} ⊗ tr_{τ})(χ_{F}).

Theorem 6.7. Let F : ℂ^{n×n} → ℂ^{m×m} be a completely positive operator. The following are equivalent:

(i) F is a superoperator. (ii) χ_{F}^{(tr)} ⊑ I_{n}, where I_{n} ∈ ℂ^{n×n} is the identity matrix. (iii)F is of the form F (A) = ∑_{i} U_{i}AU_{i}^{∗}, for matrices U_{1}, …, U_{k} with ∑_{i} U_{i}^{∗}U_{i} ⊑ I_{n}.

Proof. For the equivalence of (i) and (ii), note that F is a superoperator iff trF (A) ⩽ trA, for all positive A. This is the case iff tr_{m} ∘F ⊑ tr_{n}, and by Corollary 6.6, iff χ_{tr_{m} ∘F} ⊑ χ_{tr_{n}}. But χ_{tr_{n}} = I_{n}, and thus this is equivalent to χ_{F}^{(tr)} ⊑ I_{n}. For the equivalence of (ii) and (iii), first note that by Theorem 6.5, F can be written as F (A) = ∑_{i} U_{i}AU_{i}^{∗}, for some matrices U_{1}, …, U_{k}. But then, χ_{F}^{(tr)} = \overline{∑_{i} U_{i}^{∗}U_{i}}, the complex conjugate of ∑_{i} U_{i}^{∗}U_{i}. Thus the equivalence follows.

The equivalence (i) ⇔ (iii) is known as the Kraus Representation Theorem. Note that F is trace preserving, i.e., trF (A) = trA for all A, iff χ_{F}^{(tr)} = I_{n} iff ∑_{i} U_{i}^{∗}U_{i} = I_{n}. Theorems 6.5 and 6.7 can be straightforwardly generalized to the non-simple case, as summarized in the next theorem.

Theorem 6.8. Let σ = n_{1}, …, n_{s} and τ = m_{1}, …, m_{t} be signatures, and let F : V_{σ} → V_{τ} be a linear function.

(a) F is completely positive iff χ_{F} is positive.

(b) F is a superoperator iff χ_{F} is positive and χ_{F}^{(tr)} ⊑ I_{σ}, where I_{σ} ∈ V_{σ} is the tuple consisting of identity matrices.

(c) F is a superoperator iff it can be written in the form

F (A_{1}, …, A_{s}) = (∑_{il} U_{i1l}A_{i}U_{i1l}^{∗}, …, ∑_{il} U_{itl}A_{i}U_{itl}^{∗}),

for matrices U_{ijl} ∈ ℂ^{m_{j}×n_{i}} where ∑_{jl} U_{ijl}^{∗}U_{ijl} ⊑ I_{n_{i}} for all i. Here, l ranges over some finite index set.

Proof. All three parts follow from straightforward componentwise arguments. Let F_{ij} = π_{j} ∘ F ∘ in_{i} : V_{n_{i}} → V_{m_{j}} be the ij-component of F as before, and let F_{i} = F ∘ in_{i} : V_{n_{i}} → V_{τ}. For (a), observe that F is completely positive iff each F_{ij} is completely positive. For (b), note that F satisfies the trace condition iff each F_{i} satisfies it. This is the case iff tr_{τ} ∘F_{i} ⊑ tr_{n_{i}}, or equivalently, χ_{F_{i}}^{(tr)} ⊑ I_{n_{i}}, for all i. The latter is equivalent to χ_{F}^{(tr)} ⊑ I_{σ}. For (c), first note that by Theorem 6.5, each F_{ij} can be written as F_{ij}(A) = ∑_{l} U_{ijl}A_{i}U_{ijl}^{∗}, where l = 1, …, k_{ij}. By setting k = max_{ij} k_{ij} and U_{ijl} = 0 if l > k_{ij}, we may assume that l ranges uniformly over 1, …, k. Thus, F can be written in the desired form; further trF_{i}(A) = tr∑_{jl} U_{ijl}A_{i}U_{ijl}^{∗}, and hence, by Theorem 6.7(iii), F is a superoperator iff ∑_{jl} U_{ijl}^{∗}U_{ijl} ⊑ I_{n_{i}} for all i.

Remark 6.9 (Compact closed structure up to scalar multiples). Recall that V and Q are symmetric monoidal categories whose objects are signatures, and whose morphisms are, respectively, linear functions, and superoperators. The characteristic matrix determines a one-to-one correspondence between hom-sets V(ρ ⊗ σ, τ) ≅ V(ρ, σ ⊗ τ) (they both correspond to matrix tuples in V_{ρ⊗σ⊗τ}). Moreover, this one-to-one correspondence is natural in ρ and τ. A category with this property is called compact closed. Abramsky and Coecke (2003) suggest that such a compact closed structure could be taken as the basis for a semantics of higher-order types in a quantum programming language.

Let W be the category whose objects are signatures and whose morphisms are completely positive operators. Thus Q, W, and V share the same objects, and Q ⊆ W ⊆ V. Theorem 6.8(a) implies that W inherits the compact closed structure from V. However, by Theorem 6.8(b), the category Q of superoperators is not compact closed; indeed, if F : V_{σ} → V_{τ} is a superoperator, then is characteristic matrix χ_{F} is not in general a density matrix, because in general, trχ_{F} > 1. Nevertheless, one has the following, weaker property: λχ_{F} is a density matrix for some scalar 0 < λ ⩽ 1. In this sense, we may say that Q possesses a compact closed structure up to scalar multiples. Whether or not such a weaker structure can serve as a useful implementation of higherorder types is an interesting question.

Remark 6.10 (Basis-free presentation of the characteristic matrix). If V and W are finite dimensional vector spaces and V^{∗} and W^{∗} are their respective dual spaces, then linear maps F : V^{∗}⊗V → W^{∗}⊗W are in canonical one-to-one correspondence with elements χ_{F} ∈ U^{∗}⊗U, where U = V^{∗} ⊗ W. Explicitly, the correspondence is given by tr(w_{1} ⊗ w_{2}^{∗} ⊗ F (v_{1}^{∗} ⊗ v_{2})) = tr(v_{2} ⊗ w_{2}^{∗} ⊗ v_{1}^{∗} ⊗ w_{1} ⊗ χ_{F}), for all v_{1}^{∗} ∈ V^{∗}, v_{2} ∈ V, w_{1} ∈ W, w_{2}^{∗} ∈ W^{∗}. If moreover V and W are Hilbert spaces, then one may speak of positive matrices as certain elements of V^{∗} ⊗ V. In this case, F : V^{∗} ⊗ V → W^{∗} ⊗ W is completely positive if and only if χ_{F} is positive. This is the basis-free formulation of Theorem 6.5(b) (i)⇔(ii).

Note that linear maps F : V^{∗}⊗V → W^{∗}⊗W are also in canonical one-to-one correspondence with elements of Ũ^{∗}⊗Ũ, where Ũ = V ⊗W. However, this latter correspondence does not enjoy good properties for our purposes; in particular, it does not satisfy the equivalent of Theorem 6.5.

6.8. Normal form for superoperators

As a consequence of Theorems 6.5 and 6.7, we obtain a normal form for superoperators: any superoperator can be expressed as a sub-unitary transformation, followed by an erasure and a measurement. This normal form is not unique.

Definition (Sub-unitary). A matrix U ∈ ℂ^{m×n} is said to be sub-unitary if U is a submatrix of some unitary matrix U′, i.e., if there exist matrices U_{1}, U_{2}, U_{3} (not necessarily of the same dimensions as U) such that

U′ = ( \begin{matrix}U & U_{1} \\ U_{2} & U_{3} \end{matrix} )

is unitary. A linear function F : ℂ^{n×n} → ℂ^{m×m} is called sub-unitary if it is of the form F (A) = UAU^{∗}, for some sub-unitary matrix U ∈ ℂ^{m×n}. More generally, a linear function F : V_{σ} → V_{τ} is called sub-unitary if it is of the form F (A_{1}, …, A_{s}) = (U_{1}A_{1}U_{1}^{∗}, …, U_{s}A_{s}U_{s}^{∗}), for sub-unitary matrices U_{i} ∈ ℂ^{m_{i}×n_{i}}, where σ = n_{1}, …, n_{s} and τ = m_{1}, …, m_{s}.

Lemma 6.11. A matrix U ∈ ℂ^{m×n} is sub-unitary iff UU^{∗} ⊑ I_{m} iff U^{∗}U ⊑ I_{n}.

Proof. Clearly, U is sub-unitary iff there exists a matrix U_{1} such that the rows of (U|U_{1}) form an orthonormal set. This is the case iff UU^{∗} + U_{1}U_{1}^{∗} = I_{m}, and by Remark 2.2, iff UU^{∗} ⊑ I_{m}. The second equivalence is similar.

Definition (Measurement operator, erasure operator). Let σ = n_{1}, …, n_{s} be a signature, and let σ̃ = n_{1} + … + n_{s}, an integer regarded as a simple signature. The measurement operator μ_{σ} : Vσ̃ → V_{σ} is defined as

μ_{σ} ( \begin{matrix}A_{11} & ⋯ & A_{1s} \\ ⋮ & ... & ⋮ \\ A_{s1} & ⋯ & A_{ss} \end{matrix} ) = (A_{11}, A_{22}, …, A_{ss}),

where A_{ij} ∈ ℂ^{n_{i}×n_{j}}. An erasure operator, also known as a partial trace operator, is an operator of the form (tr_{σ} ⊗ id_{τ}) : V_{σ⊗τ} → V_{τ}.

Note that sub-unitary transformations, measurements and erasures are superoperators; the following theorem states that any superoperator is a combination of these three basic ones.

Theorem 6.12.

(a) Every superoperator F : ℂ^{n×n} → ℂ^{m×m} can be factored as F = E ∘ G, where G is sub-unitary and E is an erasure operator.

(b) Every superoperator F : V_{σ} → V_{τ} can be factored as F = M ∘E∘G, where G is sub-unitary, E is an erasure operator, and M is a measurement operator.

Proof. (a) By Theorem 6.7, there exist matrices U_{1}, …, U_{k} ∈ ℂ^{m×n} such that F (A) = ∑_{i} U_{i}AU_{i}^{∗} and ∑_{i} U_{i}^{∗}U_{i} ⊑ I_{n}. Let U be the vertical stacking of the matrices U_{1}, …, U_{k},

U = ( U_{1} ; ⋮ ; U_{k} ) ,

and define G : ℂ^{n×n} → ℂ^{km×km} by G(A) = UAU^{∗}. Since U^{∗}U = ∑_{i} U_{i}^{∗}U_{i}, the matrix U is sub-unitary by Lemma 6.11. Also, let E = (tr_{k} ⊗ id_{m}) : ℂ^{km×km} → ℂ^{m×m}. Then E(G(A)) = (tr_{k} ⊗ id_{m})(UAU^{∗}) = ∑_{i} U_{i}AU_{i}^{∗} = F (A), as claimed.

(b) Suppose σ = n_{1}, …, n_{s} and τ = m_{1}, …, m_{t}. By Theorem 6.8(c), F can be written as F (A_{1}, …, A_{s}) = (∑_{il} U_{i1l}A_{i}U_{i1l}^{∗}, …, ∑_{il} U_{itl}A_{i}U_{itl}^{∗}), for matrices U_{ijl} ∈ ℂ^{m_{j}×n_{i}} where ∑_{jl} U_{ijl}^{∗}U_{ijl} ⊑ I_{n_{i}} for all i, and where l ranges over 1, …, k, for some k. For each i, let U_{i} ∈ ℂ^{k(m_{1}+...+m_{t})×n_{i}} be the vertical stacking of the matrices U_{i11}, …, U_{it1}, …, U_{i1k}, …, U_{itk}. Let σ′ = k, …, k be a list of length s. Define G : V_{σ} → V_{σ′⊗τ̃} by

G(A_{1}, …, A_{s}) = (U_{1}A_{1}U_{1}^{∗}, …, U_{s}A_{s}U_{s}^{∗}).

Clearly, U_{i}^{∗}U_{i} = ∑_{jl} U_{ijl}^{∗}U_{ijl} ⊑ I_{n_{i}} for all i, and thus G is sub-unitary. Let E = (tr_{σ′} ⊗ idτ̃) : V_{σ′⊗τ̃} → Vτ̃, and let M = μ_{τ} : Vτ̃ → V_{τ}. An easy calculation shows that M∘E∘G(A) = F (A), as desired.

6.9. Fullness of the interpretation

We defined the interpretation of a quantum flow chart X to be a morphism [[X]] in the category Q, i.e., a superoperator. We now want to show that every superoperator is definable in this way. More precisely, we want to show that the interpretation is full: whenever Γ̄ and Γ̄′ are lists of typing contexts and F : [[Γ̄]] → [[Γ̄′]] is a superoperator, then there exists a quantum flow chart X : Γ̄ → Γ̄′ such that [[X]] = F.

For the purpose of this section, we consider the flow chart language which has loops, and which contains all unitary operators as built-in operators. In a more realistic setting, one would only have a finite, but complete set of built-in operators (in the sense of Proposition 3.2); in this case, fullness is true up to epsilon, i.e., for every ϵ > 0, one can find X such that ∥[[X]] −F ∥ < ϵ.

Lemma 6.13.

(a) If Γ̄ and Γ̄′ are lists of typing contexts such that [[Γ̄]] = [[Γ̄′]] = σ, then there exists a flow chart X : Γ̄ → Γ̄′ such that [[X]] = id_{σ}.

(b) Suppose n = 2^{k}, m = 2^{l}, and F : ℂ^{n×n} → ℂ^{m×m} is sub-unitary. Then there exists a quantum flow chart X : qbit^{k} → qbit^{l} such that [[X]] = F.

Proof. For part (a), first note that if neither Γ̄ nor Γ̄′ contain the type bit, then Γ̄ = Γ̄′ and there is nothing to show. Further, all occurrences of the type bit can be removed by repeated application of the transformation from Example 4.4.

For (b), we have F (A) = UAU^{∗} for some sub-unitary matrix U ∈ ℂ^{m×n}. Then there exist matrices U_{1}, U_{2}, U_{3}, not necessarily of the same dimensions as U, such that

U′ = ( \begin{matrix}U & U_{1} \\ U_{2} & U_{3} \end{matrix} ) ∈ ℂ^{p×p}

is unitary. Without loss of generality, we may assume that p = 2^{r} is a power of two. Then F = [[X]], where X is the flow chart shown in Figure 11. Here we have used obvious abbreviations for multiple “new”, “measure” and “discard” operations. Note that all but one branch of the measurements lead into an infinite loop; this is due to the fact that F may not be trace preserving.

Theorem 6.14 (Fullness). For given lists of typing contexts Γ̄, Γ̄′, if F : [[Γ̄]] → [[Γ̄′]] is a superoperator, then there exists a quantum flow chart X : Γ̄ → Γ̄′ such that [[X]] = F.

[figure: q_{1}, …, q_{k} : qbit = A; new qbit q_{k+1}, …, q_{r} := 0; q_{1}, …, q_{r} : qbit = ( \begin{matrix}A & 0 \\ 0 & 0 \end{matrix} ); q_{1}, …, q_{r} ∗= U′; measure q_{l+1}, …, q_{r}; q_{1}, …, q_{r} : qbit = ( \begin{matrix}UAU^{∗} & UAU_{2}^{∗} \\ U_{2}AU^{∗} & U_{2}AU_{2}^{∗} \end{matrix} ); 0,...,0; otherwise; discard q_{l+1}, …, q_{r}; q_{1}, …, q_{l} : qbit = UAU^{∗}; ∘]

Fig. 11. Flow chart realizing a sub-unitary transformation

Proof. This is an almost trivial consequence of Theorem 6.12 and Lemma 6.13. First, by Lemma 6.13(a), it is sufficient to consider the case where Γ̄ = qbit^{k_{1}}; …; qbit^{k_{s}} and Γ̄′ = qbit^{l_{1}}; …; qbit^{l_{t}}. Second, let l ⩾ l_{i} for all i, and let 2^{r} > t. Then F can be factored as F_{2} ∘ F_{1}, where F_{1} : [[Γ̄]] → [[qbit^{l} ×bit^{r}]] is a superoperator and F_{2} : [[qbit^{l} ×bit^{r}]] → [[Γ̄′]] is a canonical projection. F_{2} is clearly definable. Let σ = [[Γ̄]] and τ = [[qbit^{l} × bit^{r}]]. Then τ̃ = [[qbit^{l+r}]]. By the proof of Theorem 6.12, F_{1} can be factored as M ∘ E ∘ G, where G : σ → σ′ ⊗ τ̃ is sub-unitary, E : σ′ ⊗ τ̃ → τ̃ is the canonical erasure operator, and M : τ̃ → τ is the canonical measurement operator. Moreover, σ′ = k, …, k is a list of length s, and without loss of generality, we may assume that k = 2^{p} is a power of two, so that σ′ = [[qbit^{p}; …; qbit^{p}]]. Now M is definable by Lemma 6.13(b), E is definable by a sequence of “discard” and “merge” operations, and M : [[qbit^{l+r}]] → [[qbit^{l} × bit^{r}]] is definable by r measurements.

7. Towards a structured syntax

In previous sections, we have presented a view of quantum programming in terms of flow charts. The reasons were partly pedagogical, because flow charts explicitly highlight the “atomic” concepts of control flow, which are often left implicit in more structured programming languages. Particularly the “merge” operation, with its associated erasure of classical information, is of fundamental importance to quantum computing because it causes the passage from pure to impure states. Another reason for presenting the language in terms of flow charts was semantical: flow charts, because of their close connection with traced monoidal categories, provide a convenient setting for describing the semantics of quantum programs.

However, for actual programming, flow charts are generally too cumbersome as a notation. The reasons for this are the same as in classical programming language theory: their graphical nature makes flow charts difficult to manipulate, and they also discourage a structured approach to programming. We now present a more “textual” syntax for quantum programs, which is also more “structured” in the sense of structured programming languages such as Pascal.

It is worth emphasizing, once again, that we are describing a functional programming language, despite the fact that its syntax superficially looks imperative. Each statement acts as a function from an explicitly identified set of inputs to outputs, rather than operating on an implicitly defined global state.

7.1. The language QPL

We assume a countable set of variables, denoted x, y, b, q, . . .. We also assume a countable set of procedure variables X, Y, . . ..

Types. A type t, s is either bit or qbit. A procedure type T is defined to be an expression of the form t_{1}, …, t_{n} → s_{1}, …, s_{m}, where t_{i} and s_{j} are types. A typing context Γ is a finite list of pairs of a variable and a type, such that no variable occurs more than once. Typing contexts are written in the usual way as x_{1}:t_{1}, …, x_{n}:t_{n}. A procedure context Π is defined similarly, except that it consists of procedure variables and procedure types. We use the notation x:t, Γ and X:T, Π for extension of contexts, and in using this notation, we always implicitly assume that the resulting context is well-defined, i.e., that x does not already occur in Γ and X does not already occur in Π.

Terms. The set of QPL terms is defined by the following abstract syntax:

QPL Terms P, Q ::= new bit b := 0 new qbit q := 0 discard x
b := 0 b := 1 q_{1}, …, q_{n} ∗= S
skip P; Q
if b then P else Q measure q then P else Q while b do P
proc X : Γ → Γ′ { P } in Q y_{1}, …, y_{m} = X(x_{1}, …, x_{n})

Here, S denotes a built-in unitary transformation of arity n, and Γ, Γ′ denote typing contexts. The intended meaning of the basic terms is the same as that of the corresponding atomic flow chart components. P; Q denotes sequential composition of terms, and the skip command does nothing. (proc X : Γ → Γ′ { P } in Q) defines a procedure X with body P and scope Q; Γ and Γ′ are bindings of the formal parameters for input and output. The term ȳ = X(x̄) denotes a procedure call. In writing programs, it is common to use certain derived terms, writing for instance b := c as an abbreviation for (if c then b := 0 else b := 1), or (if b then P ) for (if b then P else skip ).

Typing judgments. A typing judgment is an expression of the form

Π ⊢ ⟨Γ⟩ P ⟨Γ′⟩,

where Π is a procedure context and Γ, Γ′ are typing contexts. The intended meaning is that under the procedure binding Π, P is a well-typed term which transforms a set of variables Γ into a set of variables Γ′. The typing rules are shown in Figure 12.

Note that the typing rules enforce that in the term (q_{1}, …, q_{n} ∗= S), the variables q_{1}, …, q_{n} are distinct. Similarly, in a procedure call ȳ = X(x̄), each of x̄ and ȳ is a list of distinct variables (although it is possible that x_{i} = y_{j}). Also note that each term has explicit inputs and outputs; for instance, the term ȳ = X(x̄) has inputs x̄ and outputs ȳ, whereas the term b := 0 has input b Peter Selinger 50

(newbit)

Π ⊢ ⟨Γ⟩ new bit b := 0 ⟨b:bit, Γ⟩

(newqbit)

Π ⊢ ⟨Γ⟩ new qbit q := 0 ⟨q:qbit, Γ⟩

(discard)

Π ⊢ ⟨x:t, Γ⟩ discard x ⟨Γ⟩

(assign_{0})

Π ⊢ ⟨b:bit, Γ⟩ b := 0 ⟨b:bit, Γ⟩

(assign_{1})

Π ⊢ ⟨b:bit, Γ⟩ b := 1 ⟨b:bit, Γ⟩
(unitary)  \frac{S is of arity n}{Π ⊢ ⟨q_{1}:qbit, …, q_{n}:qbit, Γ⟩ q̄ ∗= S ⟨q_{1}:qbit, …, q_{n}:qbit, Γ⟩}
(skip)  \overline{Π ⊢ ⟨Γ⟩ skip ⟨Γ⟩}
(compose)  \frac{Π ⊢ ⟨Γ⟩ P ⟨Γ′⟩  Π ⊢ ⟨Γ′⟩ Q ⟨Γ′′⟩}{Π ⊢ ⟨Γ⟩ P; Q ⟨Γ′′⟩}
(if )  \frac{Π ⊢ ⟨b:bit, Γ⟩ P ⟨Γ′⟩  Π ⊢ ⟨b:bit, Γ⟩ Q ⟨Γ′⟩}{Π ⊢ ⟨b:bit, Γ⟩ if b then P else Q ⟨Γ′⟩}
(measure)  \frac{Π ⊢ ⟨q:qbit, Γ⟩ P ⟨Γ′⟩  Π ⊢ ⟨q:qbit, Γ⟩ Q ⟨Γ′⟩}{Π ⊢ ⟨q:qbit, Γ⟩ measure q then P else Q ⟨Γ′⟩}
(while)  \frac{Π ⊢ ⟨b:bit, Γ⟩ P ⟨b:bit, Γ⟩}{Π ⊢ ⟨b:bit, Γ⟩ while b do P ⟨b:bit, Γ⟩}
(proc) \frac{X:t̄→ s̄, Π ⊢ ⟨x̄:t̄⟩ P ⟨ȳ:s̄⟩ X:t̄→ s̄, Π ⊢ ⟨Γ⟩ Q ⟨Γ′⟩}{Π ⊢ ⟨Γ⟩ proc X : x̄:t̄→ ȳ:s̄ \{ P \} in Q ⟨Γ′⟩}
(call)  \overline{X:t̄→ s̄, Π ⊢ ⟨x̄:t̄, Γ⟩ ȳ = X(x̄) ⟨ȳ:s̄, Γ⟩}
(permute) \frac{Π ⊢ ⟨Γ⟩ P ⟨Δ⟩, Π′, Γ′, Δ′ permutations of Π, Γ, Δ}{Π′ ⊢ ⟨Γ′⟩ P ⟨Δ′⟩}

Fig. 12. Typing rules for QPL

and output b. The latter term should be thought of as consuming a variable b, then creating a new one with the same name. A more “functional” way of expressing this would be to write b = 0(b) instead of b := 0.

Semantics. The language QPL can be immediately translated into the flow chart language of Section 4. The semantics of a QPL term is simply the semantics of the corresponding flow chart. Alternatively, the semantics can be defined directly by induction on typing judgments; the rules for doing so are obvious and we omit them here. It suffices to say that each typing judgment

X_{1}:t̄_{1} → s̄_{1}, …, X_{n}:t̄_{n} → s̄_{n} ⊢ ⟨Γ⟩ P ⟨Γ′⟩

is interpreted as a Scott-continuous function

[[P]] : Q([[t̄_{1}]], [[s̄_{1}]]) × … × Q([[t̄_{n}]], [[s̄_{n}]]) → Q([[Γ]], [[Γ′]]).

The language QPL differs from the flow chart language in some minor ways. Specifically, QPL contains the following restrictions:

— branchings and loops must be properly nested,

— merge operations can only occur in the context of a branching or loop,

— each program fragment has a unique incoming and a unique outgoing control path. In particular, procedures have only one entry and exit. Note that the typing rules for QPL allow procedure definitions to be recursive; however, we

have omitted a facility for defining two or more mutually recursive procedures. However, this is not a genuine restriction, because mutually recursive procedures can be easily expanded into simply recursive ones. Where desired, one can augment the syntax in the standard way to allow mutual recursion to be expressed directly.

7.2. Block QPL

The language QPL imposes a block structure on control constructs such as if and while, but not on memory management. Unlike in block-oriented languages such as Pascal, we have allowed variables to be allocated and deallocated anywhere, subject only to the typing rules. This is not unsafe, because the type system is there to ensure that variables are deallocated when they are no longer used. However, allowing non-nested allocations and deallocations carries an implementation cost, because it means that variables must be allocated from a heap rather than a stack. It is therefore interesting to investigate an alternate version of QPL in which a stricter block structure is imposed. We call this alternative language “Block QPL”.

Thus, in Block QPL, we want to restrict allocations and deallocations to occur in properly nested pairs. Moreover, this nesting should also respect the nesting of the control constructs if, measure , and while. We thus introduce the notion of a block, which is a program fragment enclosed in curly brackets { P }. The convention is that the scope of any variable declaration extends only to the end of the current block; moreover, the bodies of conditional statements, loops, and procedures are implicitly regarded as blocks in this sense.

In the presence of such a block structure, the explicit discard command is no longer needed, so we remove it from the language. Also, we note that with these changes, the incoming and outgoing variables of any procedure must necessarily be the same. Thus, we also modify the syntax of procedure calls, writing call X(x_{1}, …, x_{n}) instead of x_{1}, …, x_{n} = X(x_{1}, …, x_{n}). This leaves us with the following syntax for Block QPL:

Block QPL Terms P, Q ::= new bit b := 0 new qbit q := 0
b := 0 b := 1 q_{1}, …, q_{n} ∗= S
skip P; Q { P }
if b then P else Q measure q then P else Q while b do P
proc X : Γ → Γ { P } in Q call X(x_{1}, …, x_{n})

The typing rules remain unchanged, except for a new rule (block), and appropriate changes to the rules for branchings and procedures.

(block)  \frac{Π ⊢ ⟨Γ⟩ P ⟨Γ′⟩}{Π ⊢ ⟨Γ⟩ \{ P \} ⟨Γ⟩}

(if )  \frac{Π ⊢ ⟨b:bit, Γ⟩ P ⟨Γ′⟩  Π ⊢ ⟨b:bit, Γ⟩ Q ⟨Γ′′⟩}{Π ⊢ ⟨b:bit, Γ⟩ if b then P else Q ⟨b:bit, Γ⟩}

(measure)  \frac{Π ⊢ ⟨q:qbit, Γ⟩ P ⟨Γ′⟩  Π ⊢ ⟨q:qbit, Γ⟩ Q ⟨Γ′′⟩}{Π ⊢ ⟨q:qbit, Γ⟩ measure q then P else Q ⟨b:bit, Γ⟩}

(proc)  \frac{X:t̄→ t̄, Π ⊢ ⟨x̄:t̄⟩ P ⟨Γ′⟩  X:t̄→ t̄, Π ⊢ ⟨Γ⟩ Q ⟨Γ′′⟩}{Π ⊢ ⟨Γ⟩ proc X : x̄:t̄→ x̄:t̄\{ P \} in Q ⟨Γ⟩}

(call)  \overline{X:t̄→ t̄, Π ⊢ ⟨x̄:t̄, Γ⟩ call X(x̄) ⟨x̄:t̄, Γ⟩}

Note that, for any valid typing judgment Π ⊢ ⟨Γ⟩ P ⟨Γ′⟩, we necessarily have Γ ⊆ Γ′; thus the rule (block)has a similar effect as the QPL rule (discard).

The advantage of having a strict block structure as in Block QPL is that allocation follows a stack discipline, thus potentially simplifying implementations. However, there seems to be little added benefit for the programmer, particularly since the QPL type system already prevents memory leaks. In fact, the restrictions of Block QPL seem to encourage an unnatural programming style; on balance, it is probably not worth having these restrictions.

7.3. Extensions of the type system

So far, the only data types we have considered are bit and qbit, because this is the bare minimum needed to discuss the interaction of quantum data, classical data, and classical control. In practice, such a finitary type system is much too restrictive; for instance, the full power of loops and recursion does not manifest itself unless programs can operate on variable size data. In this section, we briefly discuss how the QPL type system can be extended with more complex types, and particularly infinitary types. It is remarkable that these extensions work seamlessly, even when mixing classical and quantum types in a data structure. In discussing possible extensions to the type system, we keep in mind the semantic framework of Section 6, as well as the potential physical realizability of the resulting types.

Tuples. We extend the type system of QPL adding a new type t_{1} ⊗ … ⊗ t_{n}, whenever t_{1}, …, t_{n} are types. We introduce statements for constructing and deconstructing tuples:

(tuple)  \overline{Π ⊢ ⟨x_{1}:t_{1}, …, x_{n}:t_{n}, Γ⟩ x = (x_{1}, …, x_{n}) ⟨x : t_{1} ⊗ … ⊗ t_{n}, Γ⟩}

(untuple) \overline{Π ⊢ ⟨x : t_{1} ⊗ … ⊗ t_{n}, Γ⟩ (x_{1}, …, x_{n}) = x ⟨x_{1}:t_{1}, …, x_{n}:t_{n}, Γ⟩}

The semantics of such tuples can be given in the framework of Section 6 without any changes; one simply adds an equation [[t ⊗ s]] = [[t]] ⊗ [[s]], and interprets the basic tupling and untupling operations as the semantic identity map.

Tuples can be used to encode fixed-length classical or quantum integers. For instance, the type of 4-bit classical integers is defined as int_{4} = bit ⊗ bit ⊗ bit ⊗ bit. If desired, one may add appropriate built-in functions to facilitate the manipulation of such integers.

Sums. We further extend the type system by introducing a sum type t_{1} ⊕ … ⊕ t_{n}, whenever t_{1}, …, t_{n} are types. A sum type expresses a choice between several alternatives. Note that the selection of alternatives is classical; for instance, the type of classical booleans is definable as bit = I ⊕ I. Elements of sum types are constructed and deconstructed via injection and case statements, much as in other functional programming languages:

(inj)  \overline{Π ⊢ ⟨x:t_{i}, Γ⟩ y = in_{i} x : t_{1} ⊕ … ⊕ t_{n} ⟨y : t_{1} ⊕ … ⊕ t_{n}, Γ⟩}

(case) \frac{Π ⊢ ⟨x_{1}:t_{1}, Γ⟩ P_{1} ⟨Γ′⟩  …  Π ⊢ ⟨x_{n}:t_{n}, Γ⟩ P_{n} ⟨Γ′⟩}{Π ⊢ ⟨y : t_{1} ⊕ … ⊕ t_{n}, Γ⟩ case y of in_{1} x_{1} ⇒ P_{1} | … | in_{n} x_{n} ⇒ P_{n} ⟨Γ′⟩}

Note that by adding sum types to the language QPL, it is possible to encode procedures with multiple points of entry and exit.

Infinite types. The semantics of Section 6 cannot directly handle infinite types, since it is based on finite dimensional vector spaces. However, it is not difficult to adapt the semantics to the infinite-dimensional case. This allows us, for instance, to accommodate an infinite type of classical integers, which is defined as the countable sum int = I ⊕ I ⊕ . . .. The semantics of infinite types is based on positive linear operators of bounded trace; details will be given elsewhere.

Perhaps more controversial than infinite sums are infinite tensor products. For instance, a naive implementation of “arbitrary size quantum integers” would be as the infinite tensor qbit ⊗qbit ⊗ . . .. While infinite tensor products create no particular problem from the point of view of denotational semantics, a sensible implementation can only use finitely many quantum bits at any given time. This can be achieved by imposing a semantic “zero tail state” condition, which means that only finitely many non-zero bits are allowed to occur at any given time in the computation. The compiler or the operating system has to implement a mechanism by which the zero tail state condition is enforced. This requires some overhead, but might be a useful abstraction for programmers.

Structured types. A particularly useful class of infinite sum types is the class of structured recursive types, such as lists, trees, etc. For example, the type of lists of quantum bits can be defined recursively as qlist := I ⊕ (qbit ⊗ qlist). Note that, because the use of the ⊕ operator implies a classical choice, quantum data occurs only “at the leaves” of a structured type, while the structure itself is classical. Thus, the length of a list of quantum bits is a classically known quantity. This view of structured quantum types fits well with our paradigm of “classical control”.

Lists of quantum bits are a good candidate for an implementation of a type of “variable-size quantum integers”. With this implementation, each quantum integer has a classically determined size. One can thus write programs which operate on quantum integers of arbitrary, but known, size. This seems to be precisely what is required by many currently known number-theoretic quantum algorithms, such as Shor’s factoring algorithm or the Quantum Fourier Transform. Moreover, representing quantum integers as lists, rather than as arrays as is usually done (Ömer 1998; Bettelli, Calarco, and Serafini 2001), means that no out-of-bounds checks or distinctness checks are necessary at run-time; the syntax automatically guarantees that distinct identifiers refer to distinct objects at run-time. An example of a quantum algorithm using lists is given in Section 7.4.

On a more speculative note, one might ask whether it is possible to have structured types whose very structure is “quantum” (for instance, a quantum superposition of a list of length 1 and a list of length 2). Such types do not readily fit into our “classical control” paradigm. It is an interesting question whether there is a physically feasible representation of such types, and whether they can be manipulated in an algorithmically useful way.

Higher-order functions. Unlike typical functional programming languages, the language QPL does not presently incorporate higher-order functions. There is currently no mechanism for abstracting procedures and considering them as data to be manipulated. It is an interesting question whether it is possible to augment the language with functional closures in the style of a typed linear lambda calculus. To account for the non-duplicability of quantum data due to the no-cloning property, it appears that such a language should be equipped with a linear type system along the lines of Girard’s linear logic (Girard 1987).

7.4. Example: The Quantum Fourier Transform.

We given an example of the use of recursive types. Let qlist be the type of lists of quantum bits, defined by the recursive equation qlist := I ⊕ (qbit ⊗ qlist). Figure 13 shows an implementation of the Quantum Fourier Transform (QFT) (Shor 1994; Preskill 1998), which is of type qlist → qlist. The algorithm shown differs from the standard Quantum Fourier Transform in that we have omitted the final step which reverses the order of the bits in the output. Note that the procedure QFT uses recursion to traverse its input list; it also uses an auxiliary procedure rotate which

[figure: input l:qlist; QFT:; case l; l:qlist; _{_{in}_{1}} _{nil}; _{in}_{2} b; b:qbit ⊗ qlist; nil:I; (h, t) = b; h:qbit, t:qlist; h ∗= H; h:qbit, t:qlist; new int n := 2; h:qbit, t:qlist, n:int; (h, t) = rotate(h, t, n); h:qbit, t:qlist; QF T (t); h:qbit, t:qlist; l = in_{1}(nil); l:qlist; l = in_{2}(h, t); l:qlist; ∘; l:qlist; output l:qlist]

[figure: input h:qbit, t:qlist, n:int; rotate:; case t; h:qbit, t:qlist, n:int; _{_{in}_{1}} _{nil}; _{in}_{2} c; h:qbit, c:qbit ⊗ qlist, n:int; h:qbit, nil:I, n:int; (x, y) = c; h, x:qbit, y:qlist, n:int; x, h ∗= R_{n}; h, x:qbit, y:qlist, n:int; discard n; h:qbit, nil:I; n := n + 1; h, x:qbit, y:qlist, n:int; (h, y) = rotate(h, y, n); h, x:qbit, y:qlist; t = in_{1}(nil); h:qbit, t:qlist; t = in_{2}(x, y); h:qbit, t:qlist; ∘; h:qbit, t:qlist; output h:qbit, t:qlist]

Fig. 13. The Quantum Fourier Transform

is recursive in its own right. For simplicity, we have augmented the language by a classical integer type int with built-in addition, and we have added an obvious case statement of type A ⊕ B → A; B. We also use the Hadamard operator H, as well as a parameterized family of unitary operators R_{n}, which are defined by

R_{n} = ( \begin{matrix}1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & e^{2πi/2^{n}} \end{matrix} ).

References

S. Abramsky and B. Coecke. Physical traces: Quantum vs. classical information processing. In Proceedings of Category Theory and Computer Science, CTCS’02, Ottawa, Canada, Electronic Notes in Theoretical Computer Science 69, 2003.

Peter Selinger 56

S. Bettelli, T. Calarco, and L. Serafini. Toward an architecture for quantum programming. arXiv:cs.PL/

0103009 v2, Nov. 2001. R. Cleve. An introduction to quantum complexity theory. In C. Macchiavello, G. Palma, and A. Zeilinger,

editors, Collected Papers on Quantum Computation and Quantum Information Theory, pages 103–127. World Scientific, 2000.

J.-Y. Girard. Linear logic. Theoretical Computer Science, 50:1–102, 1987. J.-Y. Girard. Geometry of interaction I: Interpretation of system F. In Logic Colloquium ’88, pages 221–

260. North Holland, Amsterdam, 1989. L. K. Grover. A fast quantum mechanical algorithm for database search. In Proceedings of the 28th Annual

ACM Symposium on Theory of Computing (STOC ’96), pages 212–219, 1996. J. Gruska. Quantum Computing. McGraw Hill, 1999. M. Hasegawa. Models of Sharing Graphs: A Categorical Semantics of let and letrec. PhD thesis, Depart-

ment of Computer Science, University of Edinburgh, July 1997. A. Joyal, R. Street, and D. Verity. Traced monoidal categories. Mathematical Proceedings of the Cambridge

Philosophical Society, 119:447–468, 1996. E. H. Knill. Conventions for quantum pseudocode. LANL report LAUR-96-2724, 1996. K. Löwner. Über monotone Matrixfunktionen. Mathematische Zeitschrift, 38:177–216, 1934. M. A. Nielsen and I. L. Chuang. Quantum Computation and Quantum Information. Cambridge University

Press, 2000. B. Ömer. A procedural formalism for quantum computing. Master’s thesis, Department of Theoretical

Physics, Technical University of Vienna, July 1998. http://tph.tuwien.ac.at/˜ oemer/qcl.html. J. Preskill. Quantum information and computation. Lecture Notes for Physics 229, California Institute of

Technology, 1998. J. W. Sanders and P. Zuliani. Quantum programming. In Mathematics of Program Construction, Springer

LNCS 1837, pages 80–99, 2000. P. Selinger. Categorical structure of asynchrony. In Proceedings of MFPS 15, New Orleans, Electronic

Notes in Theoretical Computer Science 20, 1999. P. Shor. Algorithms for quantum computation: discrete log and factoring. In Proceedings of the 35th IEEE

FOCS, pages 124–134, 1994.