• No results found

Establishing the category theory background of embedded LNL type systems does two things. First, it acts as a sanity check that our type system has a sound semantics and matches the semantics of other linear type systems. Second, it shows that the embedded language approach is strong enough to reason about its own type system.

The remainder of the dissertation will focus less on the high-level semantics and theory of embedded LNL type systems, and more on a particular case study—quantum computing.

We will see how the needs of a particular DSL and the tools available from the host language can shape the structure and meta-theory of the embedded linear language, building on the foundation of linear/non-linear type theory.

CHAPTER 6

A quantum/non-quantum type system

Quantum computing is an exciting upcoming area of computer science and physics. By harnessing the power of quantum mechanics, quantum computers have the potential to solve problems for which there is no known effective classical (non-quantum) algorithm. Such computers are still in their infancy, but the theory of quantum computing is in full swing. Shor’s algorithm describes how to factor large numbers in polynomial time (Shor, 1999), Grover’s algorithm describes how to search databases in logarithmic time (Grover, 1996), and several algorithms describe how to simulate other quantum systems, such as those found in chemistry and high-energy physics (Georgescuet al., 2014).

Instead of bits, quantum computers operate onqubits—superpositions of classical bits of the formc0∣0⟩+c1∣1⟩wherec0, c1∶Cand∣c0∣2+∣c1∣2=1. A qubit can be measured, resulting

in the bit 0 with probability ∣c0∣2, or the bit 1 with probability ∣c1∣2. Qubits e can also

be manipulated by applying unitary transformations U, which we writeU #e. Quantum algorithms, therefore, need three domain-specific features: to initialize qubits, to apply unitary gates, and to measure qubits. However, they also need to be able to process the probabilistic, classical results of measurements, which means they also need good support for classical computing.

Programming with qubits presents many challenges, and programming languages are in a unique position to address them. The design of domain-specific quantum programming languages fall mainly into two camps.

Formal semantics and meta-theory. From the start of the field, researchers in the area of quantum programming languages have prioritized their formal semantics and meta- theory. The correctness of quantum algorithms can be subtle, and specifications of cor- rectness are closely tied to the mathematical foundations of quantum computing. Without a clear semantics, it is impossible to reliably reason about the correctness of a quantum program.

Additionally, quantum languages use formal semantics to justify new programming ab- stractions when it might not be clear whether they are sound with respect to quantum computing. This has been the case for higher-order functions (Selinger and Valiron, 2009), recursion (Ying, 2014), and quantum conditionals (Yinget al., 2014). Such computational features, though common for classical programming domains, are quite subtle quantum- mechanically, so special care must be taken to make sure they are valid in quantum lan- guages.

Other innovative abstractions in quantum computing can be expressed with radically new programming abstractions. The ZX calculus is a graphical calculus with an elegant equational theory that is sound and complete with respect to quantum computing (Backens, 2015). However, the graphical nature of the calculus makes it somewhat alien from the perspective of classical programming. The measurement calculus is based on a computing model where only measurement is allowed, and not unitary transformations (Danos et al., 2007). Algebraic quantum calculi allow algebraic reasoning about quantum programs which line up with their semantics (Altenkirch and Green, 2010; Vizzottoet al., 2013).

The most widely accepted programming model, however, is the quantum circuit model, where quantum programs consist of initialization, unitary transformations, and measure- ment, as well as classical features for manipulating the results of measurement.

For many of the languages following the quantum circuit model, linear type systems are a key factor that enables reasoning about the language’s semantics. The need for linear types is closely tied to the mathematics of quantum computing in terms of linear transformations and linear algebra. More specifically, quantum physics abides by the so-called “no-cloning”

theorem, which says that an arbitrary qubit’s state cannot be duplicated. Naturally, linear types can be used to reject programs that would otherwise try to copy quantum data.

The quantum lambda calculus is a simple linear lambda calculus with a type of qubits, multiplicative products, and types for classical/non-linear data !σ(van Tonder, 2004; Selinger and Valiron, 2009). As the name would suggest, it also provides higher-order linear func- tions. As we have seen throughout this dissertation, however, languages with non-linear data marked with the type !α are often impractical or difficult to use.

The QRAM model. Implementations of quantum programming languages have had to balance sound language abstractions like linear types against usability and accessibility for the wide range of computer scientists, mathematicians, and physicists who are developing quantum algorithms and building quantum computers. Languages like Quipper (Green et al., 2013b), LIQU i∣⟩(Wecker and Svore, 2014), and Q# (Svoreet al., 2018) provide high- level, modular programming abstractions for working with both quantum and classical data, though they lack the same degree of theory as found ine.g., the quantum lambda calculus. The relationship between quantum and classical data used in these high-level languages is known as theQRAM model, which describes how a quantum computer could work in tan- dem with a classical computer (Knill, 1996). In the QRAM model, the classical computer handles the majority of ordinary tasks, while the quantum computer performs specialized quantum operations. To communicate, the classical computer sends instructions to the quantum machine in the form of quantum circuits, and the quantum computer sends mea- surement results back to the classical computer as needed.

Classical Computer Quantum Computer circuits measurement results

The philosophy of the QRAM model clearly has a lot in common with the embedded LNL framework described in this dissertation. Indeed, several state-of-the-art quantum circuit languages are implemented as embedded domain-specific languages.

Quipper is one such language embedded in Haskell (Green et al., 2013b), and it takes advantage of many features from its host language, including monads, meta-programming using Template Haskell (Sheard and Jones, 2002), type classes, and more. Quipper has been used to develop real-world quantum algorithms, and provides tools to visualize, simulate, and optimize quantum circuits. Other languages and toolkits follow a similar pattern: LIQU i∣⟩ is embedded in F# (Wecker and Svore, 2014); the Q language is embedded in C++ (Bettelli et al., 2003); and Project Q11, QISKit12, and pyQuil13 are all embedded in Python.

Unfortunately, these languages mostly lack formal meta-theory. For example, because Quipper is embedded in Haskell, it does not provide linear type checking and so is not type safe. In addition, Quipper does not have a formal semantics because giving such a semantics would require reasoning about all of Haskell, which itself does not have a formal semantics. The Proto-Quipper project is working to formalize the semantics of Quipper, but its target is limited to a standalone variant of Quipper, not the actual implementation in Haskell (Ross, 2015; Rios and Selinger, 2018).

The embedded linear/non-linear methodology developed in this dissertation gives us a way to retain linear types and a formal semantics while working inside a classical host lan- guage. In the next few chapters, we present a few variations of a quantum linear type system and its meta-theory developed in a dependently-typed host language. This case study shows how the linear/non-linear framework can be used to develop sound and expressive EDSLs in the domain of quantum computing.

In the remainder of this chapter we present a first-order embedded quantum lambda calculus that we call thequantum/non-quantum (QNQ) calculus. In Section 6.1 we discuss some relevant background on the mathematics of quantum computing, and in Section 6.2 we presents the calculus itself. We illustrate the calculus with a number of examples in Section 6.3, and give a denotational semantics in Section 6.4.

11 https://projectq.ch/ 12 https://github.com/QISKit 13 https://github.com/rigetticomputing/pyQuil