• No results found

The quantum/non-quantum (QNQ) calculus

This chapter will present an embedded linear lambda calculus based on Selinger and Val- iron’s quantum lambda calculus, which we call thequantum/non-quantum (QNQ) calculus. QNQ is an example of a linear/non-linear embedded DSL with first-order functions and primitives for quantum computing, and it illustrates how the LNL model can be highly expressive and also semantically sound. It will also serve as the basis for the calculi in Chapters 7 and 8, which explore different meta-theoretic aspects of quantum programming languages.

We start with an embedded language with multiplicative unit and pairs, and non-linear types Lower α. We choose to restrict our semantics to finite-dimensional vector spaces, so we add a restriction that α must be finite in order for it to be embedded in the linear language, as shown in Figure 6.1. The restriction to finite types is less restrictive than it seems, because we will encode data structures, typically represented as infinite types like lists, as dependent indexed data structuresn⊗σ. For any particular numbern, ann-tuple n⊗σ of σ’s is a finite type, but there are a countably infinite collection of such types available.

The basic β equivalences of linear/non-linear type systems hold for this fragment, and η equivalences hold for the multiplicative unit and product.

letx∶=ein e′∼β e′{e/x} let() ∶= ()in e′∼β e′ let(x1, x2) ∶= (e1, e2)in e′∼β e′{e1/x1, e2/x2} puta>!f ∼β f a ∆⊢QNQe∶LUnit ∆′, x∶LUnit⊢QNQe′∶τ e′{e/x} ∼ηlet() ∶=eine′{()/x} ∆⊢QNQe∶σ1⊗σ2 ∆′, x∶σ1⊗σ2⊢QNQe′∶τ e′{e/x} ∼η let(x1, x2) ∶=ein e′{(x1, x2)/x}

Eta equivalence for the typeLowerαis more subtle, however. Semantically, the operation>! corresponds to quantum measurement, so a traditionalη rule that introduces a>! operator could be problematic. If the type system guarantees that all data of typeLowerαis always

x∶σ⊢QNQx∶σ var ∆⊢QNQe∶σ ∆′, x∶σ⊢QNQe′∶τ ∆–∆′ ∆,∆′⊢QNQletx∶=ein e′∶τ let ∅ ⊢QNQ() ∶LUnit LUnit-I ∆⊢QNQe∶LUnit ∆′⊢QNQe′∶τ ∆–∆′ ∆,∆′⊢QNQlet() ∶=eine′∶τ LUnit-E ∆1 ⊢QNQe1∶σ1 ∆2⊢QNQe2∶σ2 ∆1–∆2 ∆1,∆2⊢QNQ(e1, e2) ∶σ1⊗σ2 ⊗-I ∆⊢QNQe∶σ1⊗σ2 ∆′, x1 ∶σ1, x2∶σ2⊢QNQe′∶τ ∆–∆′ ∆,∆′⊢QNQlet(x1, x2) ∶=ein e′∶τ ⊗-E a∶α α finite ∅ ⊢QNQputa∶Lowerα Lower-I ∆⊢QNQe∶Lowerα f∶α→LExpQNQ ∆ ′ τ –′ ∆,∆′⊢QNQe>!f ∶τ Lower-E

Figure 6.1: Multiplicative exponential fragment of QNQ

in a classical state, then η equivalence could be allowed. However, Chapter 7 violates that restriction by taking the type of qubits to be equal to Lower Bool, so traditional η equivalence will not be allowed. However, we still want >! bindings to be able to commute inside a term; we call thesecommuting conversions(Girardet al., 1989, Chapter 10). Thus, the following rule is admissible:

∆⊢QNQe∶Lower α f ∶α→LExpQNQ ∆

σ ′′, xσ

QNQe′∶τ ∆–∆′–∆′′

e′{(e>!f)/x} ∼cce>!λa.e′{f a/x}

Qubits. In addition to the standard linear types, QNQ has a typeQubitof qubits. Given a boolean b, we can initialize a qubit in the classical state b, written init b, and we can

measure qubits, resulting in a lowered boolean value.

b∶Bool ∅ ⊢QNQinit b∶Qubit

Qubit-I

∆⊢QNQe∶Qubit

∆⊢QNQmeas e∶Lower Bool

Qubit-E

The operational behavior states that measuring a newly initialized qubit will result in the original boolean value.

meas(init b) ∼βput b

Unitary transformations. Unitary transformations, writtenU ∶ U(σ, τ), can be applied to linear expressions as follows:

U ∶ U(σ, τ) ∆⊢QNQe∶σ

∆⊢QNQU #e∶τ

U

We expect several facts to hold about the behavior of unitary transformations, such as the fact thatU†#U #eis equivalent to e, and thatX#init bis equivalent to init(¬b). We defer the discussion of these equivalences until Chapter 7, when we explore them in depth.

Functions. What is a quantum function? At first approximation, a function is just a procedure—a sequence of instructions applied to quantum data. Such procedures should be duplicable, in that a procedure does not get consumed when it is applied to an argument, and modular, in that they can be combined to build up layers of abstraction. Selinger (2004) shows that a quantum language with first-order procedures can be interpreted in the category of density matrices and completely positive maps.

The type σ ⊸τ of (possibly higher-order) linear functions offers more than just sim- ple procedures, however, and treats higher-order functions as first class quantum data. The quantum lambda calculus demonstrates that higher-order functions are syntactically

sound—it is possible to give an operational semantics to a quantum lambda calculus with higher-order functions (van Tonder, 2004; Selinger and Valiron, 2009). However, the de- notational semantics (especially the quantum-mechanical interpretation) of higher-order quantum functions is not entirely settled. Selinger and Valiron (2008) show that higher- order functions are compatible with density matrices when bits and all other classical data is treated linearly in the program. Other denotational models have been proposed based on presheaves (Malherbe, 2010), geometry of interaction (Hasuo and Hoshino, 2011), and quantitative semantics (Paganiet al., 2014), but it is still unclear how such functions would be implementable on a quantum computer. Quipper, a quantum circuit language based on the quantum lambda calculus, compiles away its higher-order functions while generating the circuits to be executed on a quantum computer (Green et al., 2013b).

In the QNQ calculus, we choose to restrict functions to first-order procedures, which simplifies the meta-theory and is sufficient to express quantum algorithms (see Section 6.3). First-order procedures are also helpful when we adapt QNQ to a quantum circuit language in Chapter 8. However, QNQ could be extended with higher-order functions in the style of the quantum lambda calculus.

A first-order function in QNQ is called a box. A box of type Box σ τ holds a linear expression x ∶ σ ⊢QNQ e ∶ τ that uses exactly one linear variable of type σ. A box is a

generalization of the type Liftτ; whereas Lift τ holds a linear expression using no linear variables, Box σ τ holds a linear expression using exactly one linear variable. We write boxx⇒e for the constructor of typeBoxσ τ, and write f $efor function application.

x∶σ⊢QNQe∶τ boxx⇒e∶Boxσ τ box-I f ∶Boxσ τ ∆⊢QNQe∶σ ∆⊢QNQf $e∶τ box-E

The β- and η-equivalences for boxes are the same as those for ordinaryλabstraction.

(boxx⇒e′)$e∼β e′{e/x}

b∶Boxσ τ b∼ηbox x⇒b$x

Boxed functions are composable: for f ∶ Box σ τ and g ∶ Box τ ρ, we write g○f ∶ Box σ ρ for box x ⇒ g $ (f $ x), and for f1 ∶ Box σ1 τ1 and f2 ∶ Box σ2 τ2, we write

f1∥f2 ∶Box (σ1⊗σ2) τ1⊗τ2 for the parallel composition box(x1, x2) ⇒ (f1 $x1, f2 $x2).

Every unitary transformation U ∶ U(σ, τ) can be coerced into a box of type Box σ τ, and we sometimes just write U ∶Boxσ τ for the coercion boxx⇒U #x.

Note that we can deriveLiftτ asBox LUnitτ andsuspendeasboxx⇒let() ∶=xine.