A lambda calculus for quantum computation with classical control

Peter Selinger, Benoît Valiron

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

Abstract The objective of this paper is to develop a functional programming language for quantum computers. We develop a lambda calculus for the classical control model, following the first author’s work on quantum flow-charts. We define a call-by-value operational semantics, and we give a type system using affine intuitionistic linear logic. The main results of this paper are the safety properties of the language and the development of a type inference algorithm.

1 Introduction

The objective of this paper is to develop a functional programming language for quantum computers. Quantum computing is a theory of computation based on the laws of quantum physics, rather than of classical physics. Quantum computing has become a fast growing research area in recent years. For a good introduction, see e.g. [9,10].

Due to the laws of quantum physics, there are only two kinds of basic operations that one can perform on a quantum state, namely unitary transformations and measurements. Many existing formalisms for quantum computation put an emphasis on the former, i.e., a computation is understood as the evolution of a quantum state by means of unitary gates. Measurements are usually performed at the end of the computation, and outside of the formalism. In these models, a quantum computer is considered as a purely quantum system, i.e., without any classical parts. One example of such a model is the quantum Turing machine [3,6], where the entire machine state, including the tape, the finite control, and the position of the head, is assumed to be in quantum superposition. Another example is the quantum lambda calculus of van Tonder [14,15], which is a higher-order, purely quantum language without an explicit measurement operation.

On the other hand, one might imagine a model of a quantum computer where unitary operations and measurements can be interleaved. One example is the so-called QRAM model of Knill [8], which is also described by Bettelli, Calarco and Serafini [4]. Here, a quantum computer consists of a classical computer connected to a quantum device. In this configuration, the operation of the machine is controlled by a classical program which emits a sequence of instructions to the quantum device for performing measurements and unitary operations. In such a model, the control structures of the machine are classical, and only the data being operated upon is quantum. This situation is summarized by the slogan “quantum data, classical control” [12]. Several programming languages have been proposed to deal with such a model [4,11]. The present paper is based on the work of [12].

In this paper, we propose a higher-order quantum programming language, i.e., one in which functions can be considered as data. A program is a lambda term, possibly with some quantum data embedded inside. The basic idea is that lambda terms encode the control structure of a program, and thus, they would be implemented classically, i.e., on the classical device of the QRAM machine. However, the data on which the lambda terms act is possibly quantum, and is stored on the QRAM quantum device.

Because our language combines classical and quantum features, it is natural to consider two distinct basic data types: a type of classical bits and a type of quantum bits. They behave very differently. For instance, a classical bit can be copied as many times as needed. On the other hand, a quantum bit cannot be duplicated, due to the wellknown no cloning property of quantum states [9,10]. However, quantum data types are very powerful, due to the phenomena of quantum superposition and entanglement.

The semantics described in this paper is operational; a program is an abstract machine with reductions rules. The reduction rules are probabilistic.

Some care is needed when defining a type system for higher-order quantum functions. This is because the question of whether a function is duplicable or not cannot be directly seen from the types of its arguments or of its value, but rather it depends on the types of any free variables occurring in the function definition. As it turns out, the appropriate type system for higher-order quantum functions in our setting is affine intuitionistic linear logic.

We also address the question of finding a type inference algorithm. Using the remark that a linear type is a decoration of an intuitionistic one, we show that the question of deciding whether or not a program is valid can be reduced to the question of finding an intuitionistic type for it and to explore a finite number of linear decorations for the type.

This work is based on the second author’s Master’s thesis [13].

2 Quantum computing basics

We briefly recall the basic definitions of quantum computing; please see [9,10] for a complete introduction to the subject. The basic unit of information in quantum computation is a quantum bit or qubit. The state of a single qubit is a a normalized vector of the 2-dimensional Hilbert space ℂ^{2}. We denote the standard basis of ℂ^{2} as {|0⟩, |1⟩}, so that the general state of a single qubit can be written as α|0⟩ + β|1⟩, where |α|^{2} + |β|^{2} = 1.

The state of n qubits is a normalized vector in ⊗_{i=1}^{n}ℂ^{2} ≅ ℂ^{2^{n}}. We write |xy⟩ = |x⟩ ⊗ |y⟩, so that a standard basis vector of ℂ^{2^{n}} can be denoted |⌜i⌝^{n}⟩, where ⌜i⌝^{n} is the binary representation of i in n digits, for 0 ⩽ i < 2^{n}. As a special case, if n = 0, we denote the unique standard basis vector in ℂ^{1} by |⟩.

The basic operations on quantum states are unitary operations and measurements. A unitary operation maps an n-qubit state to an n-qubit state, and is given by a unitary 2^{n}×2^{n}-matrix. It is common to assume that the computational model provides a certain set of built-in unitary operations, including for example the Hadamard gate H and the controlled not-gate CNOT , among others:

H = \frac{1}{\sqrt{2}} ( \begin{matrix}1 & 1 \\ 1 & −1 \end{matrix} ) , CNOT = ( \begin{matrix}1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{matrix} ) .

The measurement acts as a projection. When a qubit α|0⟩ + β|1⟩ is measured, the observed outcome is a classical bit. The two possible outcomes 0 and 1 are observed with probabilities |α|^{2} and |β|^{2}, respectively. Moreover, the state of the qubit is affected by the measurement, and collapses to |0⟩ if 0 was observed, and to |1⟩ if 1 was observed. More generally, given an n-qubit state |ϕ⟩ = α_{0}|0⟩ ⊗ |ψ_{0}⟩ + α_{1}|1⟩ ⊗ |ψ_{1}⟩, where |ψ_{0}⟩ and |ψ_{1}⟩ are normalized (n − 1)-qubit states, then measuring the leftmost qubit results in the answer i with probability |α_{i}|^{2}, and the resulting state will be |i⟩ ⊗ |ψ_{i}⟩.

3 The untyped quantum lambda calculus

3.1 Terms Our language uses the notation of the intuitionistic lambda calculus. For a detailed introduction to the lambda calculus, see e.g. [2]. We start from a standard lambda calculus with booleans and finite products. We extend this language with three special quantum operations, which are new, meas, and built-in unitary gates. new maps a classical bit to a quantum bit. meas maps a quantum bit to a classical bit by performing a measurement operation; this is a probabilistic operation. Finally, we assume that there is a set 𝒰^{n} of built-in n-ary unitary gates for each n. We use the letter U to range over built-in unitary gates. Thus, the syntax of our language is as follows:

Term M, N, P ::= x | MN | λx.M | if M then N else P | 0 | 1 | meas
| new | U | ∗ | ⟨M, N⟩ | let ⟨x, y⟩=M in N,

We follow Barendregt’s convention for identifying terms up to α-equivalence. We also sometimes use the shorthand notation ⟨M_{1}, …, M_{n}⟩ = ⟨M_{1}, ⟨M_{2}, … ⟩⟩.

3.2 Programs The reader will have noticed that we have not provided a syntax for constant quantum states such as α|0⟩ + β|1⟩ in our language. One may ask why we did not allow the insertion of quantum states into a lambda term, such as λx.(α|0⟩ + β|1⟩). The reason is that, in the general case, such a syntax would be insufficient. Consider for instance the lambda term (λy.λf.fpy)(q), where p and q are entangled quantum bits in the state |pq⟩ = α|00⟩ + β|11⟩. Such a state cannot be represented locally by replacing p and q with some constant qubit expressions. The non-local nature of quantum states thus forces us to introduce a level of indirection into the representation of a state of a quantum program.

Definition 1. A program state is represented by a triple [Q, L, M], where

– Q is a normalized vector of ⊗_{i=0}^{n−1}ℂ^{2}, for some n ⩾ 0

– M is a lambda term,

– L is a function from W to {0, …, n−1}, where FV (M) ⊆ W ⊆ 𝒱_{term}. L is also called the linking function or the qubit environment.

The purpose of the linking function is to assign specific free variables of M to specific quantum bits in Q. The notion of α-equivalence extends naturally to programs, for instance, the states [|1⟩, {x ↦ 0}, λy.x] and [|1⟩, {z ↦ 0}, λy.z] are equivalent. The set of program states, up to α-equivalence, is denoted by 𝕊.

Convention 1. In order to simplify the notation, we will often use the following convention: we use p_{i} to denote the free variable x such that L(x) = i. A program [Q, L, M] is abbreviated to [Q, M′] with M′ = M[p_{i_{1}}/x_{1}] … [p_{i_{n}}/x_{n}], where i_{k} = L(x_{k}).

3.3 Linearity

An important well-formedness property of quantum programs is that quantum bits should always be uniquely referenced: roughly, this means that no two variable occurrences should refer to the same physical quantum bit. The reason for this restriction is the well-known no-cloning property of quantum physics, which states that a quantum bit cannot be duplicated: there exists no physically meaningful operation which maps an arbitrary quantum bit |ϕ⟩ to |ϕ⟩ ⊗ |ϕ⟩.

Syntactically, the requirement of unique referencing translates into a linearity condition: A lambda abstraction λx.M is called linear if the variable x is used at most once during the evaluation of M. A well-formed program should be such that quantum data is only used linearly; however, classical data, such as ordinary bits, can of course be used non-linearly. Since the decision of which subterms must be used linearly depends on type information, we will not formally enforce any linearity constraints until we discuss a type system in Section 4; nevertheless, we will assume that all our untyped examples are well-formed in the above sense.

3.4 Evaluation strategy

As is usual in defining a programming language, we need to settle on a reduction strategy. The obvious candidates are call-by-name and call-by-value. Because of the probabilistic nature of measurement, the choice of reduction strategy affects the behavior of programs, not just in terms of efficiency, but in terms of the actual answer computed. We demonstrate this in an example. Let plus be the boolean addition function, which is definable as plus = λxy. if x then (if y then 0 else 1) else (if y then 1 else 0). Consider the term M = (λx.plus x x)(meas(H(new 0))).

Call-by-value. Reducing this in the empty environment, using the call-by-value reduction strategy, we obtain the following reductions:

⟶_{CBV} [|0⟩, (λx.plus x x)(meas(H p_{0}))]
⟶_{CBV} [\frac{1}{\sqrt{2}}(|0⟩ + |1⟩), (λx.plus x x)(meas p_{0})]
⟶_{CBV} \{ \begin{matrix}[ |0⟩, (λx.plus x x)(0)] \\ [ |1⟩, (λx.plus x x)(1)] \end{matrix} ⟶_{CBV} \{ \begin{matrix}[ |0⟩, plus 0 0] \\ [ |1⟩, plus 1 1] \end{matrix} ⟶_{CBV} \{ \begin{matrix}[ |0⟩, 0] \\ [ |1⟩, 0] \end{matrix}

each with a probability of 1/2. Thus, under call-by-value reduction, this program produces the boolean value 0 with probability 1. Note that we have used Convention 1 for writing these program states.

Call-by-name. Reducing the same term under the call-by-name strategy, we obtain in one step [ |⟩, plus (meas(H(new 0))) (meas(H(new 0))))], and then with probability 1/4, [ |01⟩, 1 ], [ |10⟩, 1 ], [ |00⟩, 0 ] or [ |11⟩, 0 ]. Therefore, the boolean output of this function is 0 or 1 with equal probability.

Mixed strategy. Moreover, if we mix the two reduction strategies, the program can even reduce to an ill-formed term. Namely, reducing by call-by-value until [\frac{1}{\sqrt{2}}(|0⟩ + |1⟩), (λx.plus x x)(meas p_{0})], and then changing to call-by-name, we obtain in one step the term [\frac{1}{\sqrt{2}}(|0⟩ + |1⟩), (plus (meas p_{0}) (meas p_{0})], which is not a valid program since there are 2 occurrences of p_{0}. In the remainder of this paper, we will only consider the call-by-value reduction strategy, which seems to us to be the most natural.

3.5 Probabilistic reduction systems

In order to formalize the operational semantics of the quantum lambda calculus, we need to introduce the notion of a probabilistic reduction system.

Definition 2. A probabilistic reduction system is a tuple (X, U, R, prob) where X is a set of states, U ⊆ X is a subset of value states, R ⊆ (X \U)×X is a set of reductions, and prob : R → [0, 1] is a probability function, where [0, 1] is the real unit interval. Moreover, we impose the following conditions:

– For any x ∈ X, R_{x} = { x′ | (x, x′) ∈ R } is finite.

– ∑_{x′∈R_{x}} prob(x, x′) ⩽ 1

We call prob the one-step reduction, and denote x ⟶_{p} y to be prob(x, y) = p. Let us extend prob to the n-step reduction

prob^{0}(x, y) = \{ \begin{matrix}0 & if x ≠ y \\ 1 & if x = y \end{matrix}
prob^{1}(x, y) = \{ \begin{matrix}prob(x, y) & if (x, y) ∈ R \\ 0 & else \end{matrix}
prob^{n+1}(x, y) = ∑_{z∈R_{x}} prob(x, z)prob^{n}(z, y),

and the notation is extended to x ⟶_{p}^{n} y to mean prob^{n}(x, y) = p.

We say that y is reachable in one step with non-zero probability from x, denoted x ⟶_{>0} y when x ⟶_{p} y with p > 0. We say that y is reachable with non-zero probability from x, denoted x ⟶^{∗}_{>0} y when there exists n such that x ⟶_{p}^{n} y with p > 0.

We can then compute the probability to reach u ∈ U from x: It is a function from X × U to ℝ defined by prob_{U}(x, u) = ∑_{n=0}^{∞} prob^{n}(x, u). The total probability for reaching U from x is prob_{U}(x) = ∑_{n=0}^{∞} ∑_{u∈U} prob^{n}(x, u).

On the other hand, there is also the probability to diverge from x, or never reaching anything. This value is prob_{∞}(x) = lim_{n→∞} ∑_{y∈X} prob^{n}(x, y).

Lemma 1. For all x ∈ X, prob_{U}(x) + prob_{∞}(x) ⩽ 1.

We define the error probability of x to be the number prob_{err}(x) = 1−prob_{U}(x)− prob_{∞}(x).

Definition 3. We can define a notion of equivalence in X:

x ≈ y iff ∀u ∈ U \{ \begin{matrix}prob_{U}(x, u) = prob_{U}(y, u) \\ prob_{∞}(x) = prob_{∞}(y) \end{matrix}

Definition 4. In addition to the notion of reachability with non-zero probability, there is also a weaker notion of reachability, given by R: We will say that y is reachable from x if xRy. By the properties of prob, x ⟶_{>0} y implies x ⇝ y with x ⇝ y for xRy. Let us denote by ⟶^{∗} the relation such that x ⇝^{∗} y iff there exists n such that xR^{n}y, with R^{n} defined as the n-th composition of R. Similarly, x ⟶^{∗}_{>0} y implies x ⇝^{∗} y.

Definition 5. In a probabilistic reduction system, a state x is called an error-state if x ∉ U and ∑_{x′∈X} prob(x, x′) < 1. An element x ∈ X is consistent if there is no error-state e such that x ⇝^{∗} e.

Lemma 2. If x is consistent, then prob_{err}(x) = 0. The converse is false.

Remark 1. We need the weaker notion of reachability x⇝^{∗}y, in addition to reachability with non-zero probability x ⟶_{>0}^{∗} y, because a null probability of getting a certain result is not an absolute warranty of its impossibility. In the QRAM, suppose we have a qubit in state |0⟩. Measuring it cannot theoretically yield the value 1, but in practice, this might happen with small probability, due to imprecision of the physical operations and decoherence. Therefore, when we prove type safety (see Theorem 2), we will use the stronger notion. In short: a type-safe program should not crash, even in the event of random QRAM errors.

3.6 Operational semantics

We will define a probabilistic call-by-value reduction procedure for the quantum lambda calculus. Note that, although the reduction itself is probabilistic, the choice of which redex to reduce at each step is deterministic.

Definition 6. A value is a term of the following form:

Value V, W ::= x | λx.M | 0 | 1 | meas | new | U | ∗ | ⟨V, W ⟩.

The set of value states is 𝕍 = {[Q, L, V] ∈ 𝕊 | V ∈ Value}.

The reduction rules are shown in Table 1, where we have used Convention 1 to shorten the description of states. We write [Q, L, M] ⟶_{p} [Q′, L′, M′] for a singlestep reduction of states which takes place with probability p. In the rule for reducing the term U⟨p_{j_{1}}, …, p_{j_{n}}⟩, U is an n-ary built-in unitary gate, j_{1}, …, j_{n} are pairwise distinct, and Q′ is the quantum state obtained from Q by applying this gate to qubits j_{1}, …, j_{n}. In the rule for measurement, |Q_{0}⟩ and |Q_{1}⟩ are normalized states of the form |Q_{0}⟩ = ∑_{j} α_{j}|ϕ_{j}^{0}⟩ ⊗ |0⟩ ⊗ |ψ_{j}^{0}⟩ and |Q_{1}⟩ = ∑_{j} β_{j}|ϕ_{j}^{1}⟩ ⊗ |1⟩ ⊗ |ψ_{j}^{1}⟩, where ϕ_{j}^{0} and ϕ_{j}^{1} is an i-qubit state (so that the measured qubit is the one pointed to by p_{i}). In the rule for for new, Q is an n-qubit state, so that Q ⊗ |i⟩ is an (n + 1)-qubit state, and p_{n} refers to its rightmost qubit.

We define a weaker relation ⇝. This relation models the transformations that can happen in the presence of decoherence and imprecision of physical operations. We define [Q, M] ⇝ [Q′, M′] to be [Q, M] ⟶_{p} [Q′, M′], even when p = 0, plus the additional rule, if Q and Q′ are vectors of equal dimensions: [Q, M] ⇝ [Q′, M].

[Q, (λx.M)V] ⟶_{1} [Q, M[V/x]] {[Q, N] ⟶_{p} [Q′, N′] \atop [Q, MN] ⟶_{p} [Q′, MN′]} {[Q, M] ⟶_{p} [Q′, M′] \atop [Q, MV] ⟶_{p} [Q′, M′V]} {[Q, M_{1}] ⟶_{p} [Q′, M′_{1}] \atop [Q, ⟨M_{1}, M_{2}⟩] ⟶_{p} [Q′, ⟨M′_{1}, M_{2}⟩]} {[Q, M_{2}] ⟶_{p} [Q′, M′_{2}] \atop [Q, ⟨V_{1}, M_{2}⟩] ⟶_{p} [Q′, ⟨V_{1}, M′_{2}⟩]}

[Q, if 0 then M else N] ⟶_{1} [Q, N]
[Q, if 1 then M else N] ⟶_{1} [Q, M]
[Q, U⟨p_{j_{1}}, …, p_{j_{n}}⟩] ⟶_{1} [Q′, ⟨p_{j_{1}}, …, p_{j_{n}}⟩]
[α|Q_{0}⟩ + β|Q_{1}⟩, meas p_{i}] ⟶_{|α|^{2}} [|Q_{0}⟩, 0]
[α|Q_{0}⟩ + β|Q_{1}⟩, meas p_{i}] ⟶_{|β|^{2}} [|Q_{1}⟩, 1]
[Q, new 0] ⟶_{1} [Q ⊗ |0⟩, p_{n}]
[Q, new 1] ⟶_{1} [Q ⊗ |1⟩, p_{n}]
[Q, P] ⟶_{p} [Q′, P′]

[Q, if P then M else N] ⟶_{p} [Q′, if P′ then M else N]

[Q, M] ⟶_{p} [Q′, M′]

[Q, let ⟨x_{1}, x_{2}⟩ = M in N] ⟶_{p} [Q′, let ⟨x_{1}, x_{2}⟩ = M′ in N]

[Q, let ⟨x_{1}, x_{2}⟩ = ⟨V_{1}, V_{2}⟩ in N] ⟶_{1} [Q, N[V_{1}/x_{1}, V_{2}/x_{2}]]

Table 1. Reductions rules of the quantum lambda calculus

Lemma 3. Let prob be the function such that for x, y ∈ 𝕊, prob(x, y) = p if x ⟶_{p} y and 0 else. Then (𝕊, 𝕍, ⇝, prob) is a probabilistic reduction system. □

This probabilistic reduction system has error states, for example, [Q, H(λx.x)] or [Q, U⟨p_{0}, p_{0}⟩]. Such error states correspond to run-time errors. In the next section, we introduce a type system designed to rule out such error states.

4 The typed quantum lambda-calculus

We will now define a type system designed to eliminate all run-time errors arising from the reduction system of the previous section. We need base types (such as bit and qbit), function types, and product types. In addition, we need the type system to capture a notion of duplicability, as discussed in Section 3.3. We follow the notation of linear logic [7]. By default, a term of type A is assumed to be non-duplicable, and duplicable terms are given the type !A instead. Formally, the set of types is defined as follows, where α ranges over a set of type constants and X ranges over a countable set of type variables:

qType A, B ::= α | X | !A | (A ⊸ B) | ⊤ | (A ⊗ B)

Note that, because all terms are assumed to be non-duplicable by default, the language has a linear function type A ⊸ B and a linear product type A ⊗ B. This reflects the fact that there is in general no canonical diagonal function A → A ⊗ A. Also, ⊤ is the linear unit type. This will be made more formal in the typing rules below. We write !^{n}A for !!! …!!A, with n repetitions of !. We also write A^{n} for the n-fold tensor product A ⊗ … ⊗ A.

4.1 Subtyping

The typing rules will ensure that any value of type !A is duplicable. However, there is no harm in using it only once; thus, such a value should also have type A. For this reason, we define a subtyping relation <: as follows:

\begin{matrix}α <: α (ax) & X <: X (var) & ⊤ <: ⊤ (⊤) & {A <: B \atop !A <: B} (D) & \begin{matrix}!A <: B \\ !A & <: & !B \end{matrix} (!) \\ \begin{matrix}A_{1} <: B_{1} & A_{2} <: B_{2} \\ A_{1} ⊗ A_{2} <: B_{1} ⊗ B_{2} \end{matrix} (⊗) & \begin{matrix}A <: A′ & B <: B′ \\ A′ ⊸ B <: A ⊸ B′ \end{matrix} (⊸) \end{matrix}

Lemma 4. For types A and B, if A <: B and (m = 0) ∨ (n ⩾ 1), then !^{n}A <: !^{m}B. ⊓ ⊔

Notice that one can rewrite types using the notation:

qType A, B ::= !^{n}α | !^{n}X | !^{n}(A ⊸ B) | !^{n}⊤ | !^{n}(A ⊗ B)

with n ∈ ℕ. Using the overall condition on n and m that (m = 0) ∨ (n ⩾ 1), the rules can be re-written as:

\begin{matrix}!^{n}X <: !^{m}X (var_{2}) & !^{n}α <: !^{m}α (α) & !^{n}⊤ <: !^{m}⊤ (⊤) \\ \begin{matrix}A_{1} <: B_{1} & A_{2} <: B_{2} \\ !^{n}(A_{1} ⊗ A_{2}) & <: & !^{m}(B_{1} ⊗ B_{2}) \end{matrix} (⊗) & \begin{matrix}A <: A′ & & B <: B′ \\ !^{n}(A′ ⊸ B) & <: & !^{m}(A ⊸ B′) \end{matrix} (⊸_{2}) \end{matrix}

The two sets of rules are equivalent.

Lemma 5. The rules of the second set are reversible. ⊓ ⊔

Lemma 6. (qType, <:) is reflexive and transitive. If we define an equivalence relation ≑ by A ≑ B iff A <: B and B <: A, (qType/≑, <:) is a poset. ⊓ ⊔

Lemma 7. If A <: !B, then there exists C such that A = !C. ⊓ ⊔

Remark 2. The subtyping rules are a syntactic device, and are not intended to catch all plausible type isomorphisms. For instance, the types !A ⊗!B and !(A ⊗ B) are not subtypes of each other, although an isomorphism between these types is easily definable in the language.

4.2 Typing rules

We need to define what it means for a quantum state [Q, L, M] to be well-typed. It turns out that the typing does not depend on Q and L, but only on M. We introduce typing judgments of the form Δ ▷ M : B. Here M is a term, B is a qType, and Δ is a typing context, i.e., a function from a set of variables to qType. As usual, we write |Δ| for the domain of Δ, and we denote typing contexts as x_{1}:A_{1}, …, x_{n}:A_{n}. As usual, we write Δ, x:A for Δ ∪ {x:A} if x ∉ |Δ|. Also, if Δ = x_{1}:A_{1}, …, x_{n}:A_{n}, we write !Δ = x_{1}:!A_{1}, …, x_{n}:!A_{n}. A typing judgement is called valid if it can be derived from the rules in Table 2.

{A <: B \atop Δ, x:A ▷ x : B} (ax_{1}) {A_{c} <: B \atop Δ ▷ c : B} (ax_{2})
Γ_{1},!Δ ▷ P : bit Γ_{2},!Δ ▷ M : A Γ_{2},!Δ ▷ N : A
Γ_{1}, Γ_{2},!Δ ▷ if P then M else N : A

(if )

\begin{matrix}Γ_{1},!Δ ▷ M : A ⊸ B & Γ_{2},!Δ ▷ N : A \\ Γ_{1}, Γ_{2},!Δ ▷ MN : B \end{matrix} (app)

{x:A, Δ ▷ M : B \atop Δ ▷ λx.M : A ⊸ B} (λ_{1}) \begin{matrix}If F V (M) ∩ |Γ | = ∅: \\ Γ,!Δ, x:A ▷ M : B \end{matrix} ; Γ,!Δ ▷ λx.M : !^{n+1}(A ⊸ B) (λ_{2})
\begin{matrix}!Δ, Γ_{1} ▷ M_{1} : !^{n}A_{1} & !Δ, Γ_{2} ▷ M_{2} : !^{n}A_{2} \\ !Δ, Γ_{1}, Γ_{2} ▷ ⟨M_{1}, M_{2}⟩ : !^{n}(A_{1} ⊗ A_{2}) \end{matrix} (⊗.I) Δ ▷ ∗ : !^{n}⊤ (⊤)

\begin{matrix}!Δ, Γ_{1} ▷ M : !^{n}(A_{1} ⊗ A_{2}) & !Δ, Γ_{2}, x_{1}:!^{n}A_{1}, x_{2}:!^{n}A_{2} ▷ N : A \\ !Δ, Γ_{1}, Γ_{2} ▷ let ⟨x_{1}, x_{2}⟩ = M in N : A \end{matrix} (⊗.E)

Table 2. Typing rules

The typing rule (ax) assumes that to every constant c of the language, we have associated a fixed type A_{c}. The types A_{c} are defined as follows:

A_{0} = !bit  A_{new} = !(bit ⊸ qbit)
A_{1} = !bit  A_{meas} = !(qbit ⊸!bit)  A_{U} = !(qbit^{n} ⊸ qbit^{n})

Note that we have given the type !(bit ⊸ qbit) to the term new. Another possible choice would have been !(!bit ⊸ qbit), which makes sense because all classical bits are duplicable. However, since !(bit ⊸ qbit) <: !(!bit ⊸ qbit), the second type is less general, and can be inferred by the typing rules.

Note that, if [Q, L, M] is a program state, the term M need not be closed; however, all of its free variables must be in the domain of L, and thus must be of type qbit. We therefore define:

Definition 7. A program state [Q, L, M] is well-typed of type B if Δ ▷ M : B is derivable, where Δ = {x: qbit | x ∈ FV (M)}. In this case, we write [Q, L, M] : B.

Note that the type system enforces that variables holding quantum data cannot be duplicated; thus, λx.⟨x, x⟩ is not a valid term of type qbit ⊸ qbit ⊗ qbit. On the other hand, we allow variables to be discarded freely. Other approaches are also possible, for instance, Altenkirch and Grattage [1] propose a syntax that allows duplication but restricts discarding of quantum values.

4.3 Example: quantum teleportation

Let us illustrate the quantum lambda calculus and the typing rules with an example. The following is an implementation of the well-known quantum teleportation protocol (see e.g. [9]). The purpose of the teleportation protocol is to send a qubit from location A to location B, using only classical communication and a pre-existing shared entangled quantum state. In fact, this can be achieved by communicating only the content of two classical bits.

In terms of functional programming, the teleportation procedure can be seen as the creation of two non-duplicable functions f : qbit ⊸ bit ⊗ bit and g : bit ⊗ bit ⊸ qbit, such that f ∘ g(x) = x for an arbitrary qubit x.

We start by defining the following functions EPR : !(⊤ ⊸ (qbit ⊗ qbit)), BellMeasure : !(qbit ⊸(qbit ⊸ bit ⊗ bit)), and U : !(qbit ⊸(bit ⊗ bit ⊸ qbit)):

EPR  = λx.CNOT ⟨H(new 0), new 0⟩,
BellMeasure = λq_{2}.λq_{1}.(let ⟨x, y⟩ = CNOT ⟨q_{1}, q_{2}⟩ in ⟨ meas(Hx), meas y⟩,
U  = λq.λ⟨x, y⟩.if xthen (if y then U_{11}q else U_{10}q) else (if y then U_{01}q else U_{00}q),

where

U_{00} = ( \begin{matrix}1 & 0 \\ 0 & 1 \end{matrix} ) , U_{01} = ( \begin{matrix}0 & 1 \\ 1 & 0 \end{matrix} ) , U_{10} = ( \begin{matrix}1 & 0 \\ 0 & −1 \end{matrix} ) , U_{11} = ( \begin{matrix}0 & 1 \\ −1 & 0 \end{matrix} ) .

The function EPR creates an entangled state \frac{1}{\sqrt{2}}(|00⟩ + |11⟩). The function BellMeasure performs a so-called Bell measurement, and the function U performs a unitary correction on the qubit q depending on the value of two classical bits. We can now construct a pair of functions f : qbit ⊸ bit ⊗ bit and g : bit ⊗ bit ⊸ qbit with the above property by the following code:

let ⟨x, y⟩=EPR ∗ in let f = BellMeasure x

in let g = U y.

in ⟨f, g⟩.

The functions f and g thus created do indeed have the desired property that f∘g(x) = x, where x is any qubit. Note that, since f and g depend on the state of the qubits x and y, respectively, these functions cannot be duplicated, which is reflected in the fact that the types of f and g do not contain a top-level “!”.

4.4 Properties of the type system

We derive some basic properties of the type system.

Definition 8. We extend the subtyping relation to contexts by writing Δ<:Δ′ if |Δ′| = |Δ| and for all x in |Δ′|, Δ_{f}(x) <: Δ′_{f}(x).

Lemma 8. 1. If x ∉ FV (M) and Δ, x:A ▷ M:B, then Δ ▷ M:B.

2. If Δ ▷ M:A, then Γ, Δ ▷ M:A.

3. If Γ <: Δ and Δ ▷ N : A and A <: B, then Γ ▷ N : B.

The next lemma is crucial in the proof of the substitution lemma. Note that it is only true for a value V, and in general fails for an arbitrary term M.

Lemma 9. If V is a value and Δ ▷ V : !A, then for all x ∈ FV (V), there exists some U ∈ qType such that Δ(x) = !U.

Proof. By induction on V.

– If V is a variable x, then the last rule in the derivation was {B <: !A \atop Δ′, x : B ▷ x : !A}.

Since B <: !A, B must be exponential by Lemma 7.

– If V is a constant c, then FV (V) = ∅, hence the result holds vacuously.

– If V = λx.M, the only typing rule that applies is (λ_{2}), and Δ = Γ,!Δ′ with F V (M) ∩ |Δ′| = ∅. So every y ∈ FV (M) except maybe x is exponential. Since FV (λx.M) = (FV (M) \ {x}), this suffices.

– The remaining cases are similar. ⊓ ⊔

Lemma 10 (Substitution). If V is a value such that Γ_{1},!Δ, x:A▷M : B and Γ_{2},!Δ▷ V : A, then Γ_{1}, Γ_{2},!Δ ▷ M[V/x] : B.

Corollary 1. If Γ_{1},!Δ, x:A▷M : B and Γ_{2},!Δ▷V : !^{n}A, then Γ_{1}, Γ_{2},!Δ▷M[V/x] : B.

Proof. From Lemma 10 and Lemma 8(3).

Remark 3. We note that all the usual rules of affine intuitionistic linear logic are derived rules of our type system, except for the general promotion rule. However, the promotion rule is derivable when V is a value:

{!Γ ▷ V : A \atop !Γ ▷ V :!A}.

4.5 Subject reduction and progress

Theorem 1 (Subject Reduction). Given [Q, L, M] : B and [Q, L, M]⇝^{∗}[Q′, L′, M′], then [Q′, L′, M′] : B.

Proof. It suffices to show this for [Q, L, M] ⟶_{p} [Q′, L′, M′], and we proceed by induction on the rules in Table 1. The rule [Q, (λx.M)V] ⟶_{1} [Q, M[V/x]] and the rule for “let” use the substitution lemma. The remaining cases are direct applications of the induction hypothesis. ⊓ ⊔

Theorem 2 (Progress). Let [Q, L, M] : B be a well-typed program. Then [Q, L, M] is not an error state in the sense of Definition 5. In particular, either [Q, L, M] is a value, or else there exist some state [Q′, L′, M′] such that [Q, L, M] ⟶_{p} [Q′, L′, M′]. Moreover, the total probability of all possible single-step reductions from [Q, L, M] is 1.

Corollary 2. Every sequence of reductions of a well-typed program either converges to a value, or diverges.

The proof of the Progress Theorem is similar to the usual proof, with two small differences. The first is the presence of probabilities, and the second is the fact that M is not necessarily closed. However, all the free variables of M are of type qbit, and this property suffices to prove the following lemma, which generalizes the usual lemma on the shape of closed well-typed values:

Lemma 11. Suppose Δ = x_{1}:qbit, …, x_{n}:qbit, and V is a value. If Δ ▷ V : A ⊸ B, then V is new, meas, U, or a lambda abstraction. If Δ▷V : A⊗B, then V = ⟨V_{1}, V_{2}⟩. If Δ ▷ V : bit, then V = 0 or V = 1. ⊓ ⊔

Proof of the Progress Theorem. By induction on M. The claim follows immediately in the cases when M is a value, or when M is a left-hand-side of one of the rules in Table 1 that have no hypotheses. Otherwise, using Lemma 11, M is one of the following: P N, NV, ⟨N, P ⟩, ⟨V, N⟩, if N then P else Q, let ⟨x, y⟩=N in P, where N is not a value. In this case, the free variables of N are still all of type qbit, and by induction hypothesis, the term [Q, L, N] has reductions with total probability 1, and the rules in Table 1 ensure that the same is true for [Q, L, M]. ⊓ ⊔

5 Type inference algorithm

It is well-known that in the simply-typed lambda calculus, as well as in many programming languages, satisfy the principal type property: every untyped expression has a most general type, provided that it has any type at all. Since most principal types can usually be determined automatically, the programmer can be relieved from the need to write any types at all.

In the context of our quantum lambda calculus, it would be nice to have a type inference algorithm; however, the principal type property fails due to the presence of exponentials !A. Not only can an expression have several different types, but in general none of the types is “most general”. For example, the term M = λxy.xy has possible types T_{1} = (A ⊸ B) ⊸ (A ⊸ B) and T_{2} = !(A ⊸ B) ⊸!(A ⊸ B), among others. Neither of T_{1} and T_{2} is a substitution instance of the other, and in fact the most general type subsuming T_{1} and T_{2} is X ⊸ X, which is not a valid type for M. Also, neither of T_{1} and T_{2} is a subtype of the other, and the most general type of which they are both subtypes is (A ⊸ B) ⊸!(A ⊸ B), which is not a valid type for M.

In the absence of the principal type property, we need to design a type inference algorithm based on a different idea. The approach we follow is the one suggested by V. Danos, J.-B. Joinet and H. Schellinx [5]. The basic idea is to view a linear type as a “decoration” of an intuitionistic type. Our type inference algorithm is based on the following technical fact, given below: if a given term has an intuitionistic type derivation π, then it is linearly typable if and only if there exists a linear type derivation which is a decoration of π. Typability can therefore be decided by first doing intuitionistic type inference, and then checking finitely many possible linear decorations.

5.1 Skeletons and decorations

The class of intuitionistic types is

iType U, V ::= α | X | (U ⇒ V) | (U × V) | ⊤

where α ranges over the type constants and X over the type variables.

To each A ∈ qType, we associate its type skeleton ^{†}A ∈ iType, which is obtained by removing all occurrences of “!”. Conversely, every U ∈ iType can be lifted to some ♣U ∈ qType with no occurrences of “!”. Formally:

Definition 9. Define functions † : qType → iType and ♣ : iType → qType by:

\begin{matrix}^{†}!^{n}α = α, ^{†}!^{n}X = X, ^{†}!^{n}⊤ = ⊤, \\ ^{†}!^{n}(A ⊸ B) = ^{†}A ⇒^{†}B, \\ ^{†}!^{n}(A ⊗ B) = ^{†}A ×^{†}B, \end{matrix}  \begin{matrix}^{♣}α = α, ^{♣}X = X, ^{♣}⊤ = ⊤, \\ ^{♣}(U ⇒ V) = ^{♣}U ⊸^{♣}V , \\ ^{♣}(U × V) = ^{♣}U ⊗^{♣}V . \end{matrix}

Lemma 12. If A <: B, then^{†}A = ^{†}B. If U ∈ iType, then U = ^{†}^{♣}U.

Writing Δ ▶ M : U for a typing judgement of the simply-typed lambda calculus, we can extend the notion of skeleton to contexts, typing judgments, and derivations as follows:

\begin{aligned}^{†}\{x_{1}:A_{1}, …, x_{n}:A_{n}\} & = & \{x_{1}:^{†}A_{1}, …, x_{n}:^{†}A_{n}\} \\ ^{†}(Δ ▷ M : A) & = & (^{†}Δ ▶ M : ^{†}A). \end{aligned}

From the rules in Table 2, it is immediate that if Δ ▷ M : A is a valid typing judgment in the quantum lambda-calculus, then ^{†}(Δ ▷ M : A) = (^{†}Δ ▶ M : ^{†}A) is a valid typing judgment in the simply-typed lambda-calculus.

We now turn to the question of how an intuitionistic typing derivation can be “decorated” with exponentials to yield a valid quantum typing derivation. These decorations are going to be the heart of the quantum type inference algorithm.

Definition 10. Given A ∈ qType and U ∈ iType, we define the decoration U ↬ A ∈ qType of U along A by

1. U ↬ !^{n}A = !^{n}(U ↬ A),

2. (U ⇒ V) ↬ (A ⊸ B) = (U ↬ A ⊸ V ↬ B),

3. (U × V) ↬ (A ⊗ B) = (U ↬ A ⊗ V ↬ B), and in all other cases:

4. U ↬ A = ^{♣}U.

The following lemma is the key to the quantum type inference algorithm:

Lemma 13. If M is well-typed in the quantum lambda-calculus with typing judgment Γ ▷ M : A, then for any valid typing judgment Δ ▶ M : U in simply-typed lambdacalculus with |Δ| = |Γ |, the typing judgment Δ ↬ Γ ▷ M : U ↬ A is valid in the quantum lambda-calculus.

5.2 Elimination of repeated exponentials

The type system in Section 4 allows types with repeated exponentials such as !!A. While this is useful for compositionality, it is not very convenient for type inference. We therefore consider a reformulation of the typing rules which only requires single exponentials.

Lemma 14. The following are derived rules of the type system in Table 2, for all τ, σ ∈ {0, 1}.

\begin{matrix}\begin{matrix}!Δ, Γ_{1} ▷ M_{1} : !A_{1} & !Δ, Γ_{2} ▷ M_{2} : !A_{2} \\ !Δ, Γ_{1}, Γ_{2} ▷ ⟨M_{1}, M_{2}⟩ : !(!^{τ} A_{1} ⊗!^{σ}A_{2}) \end{matrix} & (⊗.I′) \\ \begin{matrix}!Δ, Γ_{1} ▷ M : !(!^{τ} A_{1} ⊗!^{σ}A_{2}) & !Δ, Γ_{2}, x_{1}:!A_{1}, x_{2}:!A_{2} ▷ N : A \\ !Δ, Γ_{1}, Γ_{2} ▷ let ⟨x_{1}, x_{2}⟩ = M in N : A \end{matrix} & (⊗.E′) \end{matrix}

Lemma 15. If M is typable in the quantum lambda calculus by some derivation π, then M is typable in the system with the added rules (⊗.I′) and (⊗.E′), by a derivation π′ using no repeated exponentials. Moreover,^{†}π′ = ^{†}π. ⊓ ⊔

5.3 Description of the type inference algorithm

To decide the typability of a given term M, first note the following: if M is not typable in simply-typed lambda calculus, then M is not quantum typable. On the other hand, suppose M admits a typing judgment Γ ▶ M : U in the simply-typed lambda calculus, say with typing derivation π. Moreover, suppose without loss of generality that the derivation π uses no dummy variables, i.e., each sequent Γ′ ▶ M′ : U′ of π satisfies |Γ′| = FV (M′). Then by the proof of Lemma 13, M is quantum typable if and only if M has a quantum derivation whose skeleton is π. Thus we can perform type inference in the quantum lambda-calculus in two steps:

1. Find an intuitionistic typing derivation π, if any, using no dummy variables.

2. Find a decoration of π which is a valid quantum typing derivation, if any.

Step (1) is known to be decidable. For step (2), note that by Lemma 15, it suffices to consider decorations of π without repeated exponentials. Since there are only finitely many such decorations, the typability of M is clearly a decidable problem. Also note that if the algorithm succeeds, then it returns a possible type for M. However, it does not return a description of all possible types.

It should further be noted that the space of all decorations of π, while exponential in size, can be searched efficiently by solving a system of constraints. More precisely, if we create a boolean variable for each place in the type derivation which potentially can hold a “!”, then the constraints imposed by the linear type system can all be written in the form of implications x_{1} ∧ … ∧ x_{n} ⇒ y, where n ⩾ 0, and negations ¬z. It is well-known that such a system can be solved in polynomial time in the number of variables and clauses, which is in turns polynomial in the size of the type derivation. Note, however, that the size of the type derivation need not be polynomial in the size of the term M, as the type of M can be of exponential size in the worst case.

6 Conclusion and further work

In this paper, we have defined a higher-order quantum programming language based on a linear typed lambda calculus. Compared to the quantum lambda calculus of van Tonder [14,15], our language is characterized by the fact that it contains classical as well as quantum features; for instance, we provide classical datatypes and measurements as a primitive feature of our language. Moreover, we provide a subject reduction result and a type inference algorithm. As the language shows, linearity constraints do not just exist at base types, but also at higher types, due to the fact that higher-order function are represented as closures which may in turns contain embedded quantum data. We have shown that affine intuitionistic linear logic provides the right type system to deal with this situation.

There are many open problems for further work. An interesting question is whether the syntax of this language can be extended to include recursion. Another question is to study extensions of the type system, for instance with additive types as in linear logic. One may also study alternative reduction strategies. In this paper, we have only considered the call-by-value case; it would be interesting to see if there is a call-by-name equivalent of this language. Finally, another important open problem is to find a good denotational semantics for a higher order quantum programming language. One approach for finding such a semantics is to extend the framework of Selinger [12] and to identify an appropriate higher-order version of the notion of a superoperator.

References

1. T. Altenkirch and J. Grattage. A functional quantum programming language. Available from arXiv:quant-ph/0409065, 2004.

2. H. P. Barendregt. The Lambda-Calculus, its Syntax and Semantics, volume 103 of Studies in Logic and the Foundation of Mathematics. North Holland, second edition, 1984.

3. P. Benioff. The computer as a physical system: A microscopic quantum mechanical Hamiltonian model of computers as represented by Turing machines. Journal of Statistical Physics, 22:563–591, 1980.

4. S. Bettelli, T. Calarco, and L. Serafini. Toward an architecture for quantum programming. The European Physical Journal D, 25(2):181–200, August 2003.

5. V. Danos, J.-B. Joinet, and H. Schellinx. On the linear decoration of intuitionistic derivations. Archive for Mathematical Logic, 33:387–412, 1995.

6. D. Deutsch. Quantum theory, the Church-Turing principle and the universal quantum computer. Proceedings of the Royal Society of London. Series A, Mathematical and Physical Sciences, 400(1818):97–117, July 1985.

7. J.-Y. Girard. Linear logic. Theoretical Computer Science, 50(1):1–101, 1987.

8. E. Knill. Conventions for quantum pseudocode. Technical Report LAUR-96-2724, Los Alamos National Laboratory, 1996.

9. M. A. Nielsen and I. L. Chuang. Quantum Computation and Quantum Information. Cambridge University Press, 2002.

10. J. Preskill. Lecture notes for Physics 229, quantum computation. Available from http://www.theory.caltech.edu/people/preskill/ph229/#lecture, 1999.

11. J. W. Sanders and P. Zuliani. Quantum programming. In R. Backhouse and J. N. Oliveira, editors, Mathematics of Program Construction: 5th International Conference, volume 1837 of Lecture Notes in Computer Science, pages 80–99, Ponte de Lima, Portugal, July 2000. Springer-Verlag.

12. P. Selinger. Towards a quantum programming language. Mathematical Structures in Computer Science, 14(4):527–586, 2004.

13. Benoît Valiron. A functional programming language for quantum computation with classical control. Master’s thesis, University of Ottawa, September 2004.

14. A. van Tonder. Quantum computation, categorical semantics and linear logic. On arXiv: quant-ph/0312174, 2003.

15. A. van Tonder. A lambda calculus for quantum computation. SIAM Journal of Computing, 33(5):1109–1135, 2004. Available from arXiv:quant-ph/0307150.