+ All Categories
Home > Documents > Hynek Mlnarık- Quantum Programming Language LanQ

Hynek Mlnarık- Quantum Programming Language LanQ

Date post: 06-Apr-2018
Category:
Upload: gholsas
View: 226 times
Download: 0 times
Share this document with a friend

of 155

Transcript
  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    1/155

    Masaryk University, Faculty of Informatics

    } w!"#$%&12 3456789@ACDEFGHIPQRS`ye|

    Quantum Programming LanguageLanQ

    Hynek Mlnark

    PhD Thesis

    Brno, Czech Republic September 2007

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    2/155

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    3/155

    Acknowledgments

    This work would not be realized without support of many people who helped me both proffes-sionally and personally.

    I would like to thank my supervisor, Jozef Gruska, for his guidance, support and advicesduring my PhD studies.

    I would like also to express my thanks to Lukas Bohac, Jan Bouda, Simon Gay, PhilippeJorrand, Rajagopal Nagarajan, Nick Papanikolaou, Simon Perdrix, Igor Peterlk, Libor Skar-vada, Josef Sprojcar and Tomoyuki Yamakami for their enthusiasm and invaluable commentson LanQ and related discussions. I would like to particularly thank to Rajagopal Nagarajanand Nick Papanikolaou for their hospitality, encouragement and fruitful discussions not onlyduring my visit to the University of Warwick.

    Finally, I would like to express my deep gratitude to my wife Lucie and to my parentsfor their endless support and love without which I would never have been able to write thisthesis.

    This work was realized under financial support of grants MSM0021622419, GACR201/04/1153 and GACR 201/07/0603.

    i

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    4/155

    ii

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    5/155

    Contents

    I Introduction 1

    1 Introduction 3

    1.1 The history of quantum information processing . . . . . . . . . . . . . . . . . 3

    1.2 Introduction to quantum computing . . . . . . . . . . . . . . . . . . . . . . . 4

    1.2.1 Basics of quantum mechanics . . . . . . . . . . . . . . . . . . . . . . . 4

    1.3 Models of quantum computation . . . . . . . . . . . . . . . . . . . . . . . . . 12

    1.3.1 Models based on classical models . . . . . . . . . . . . . . . . . . . . . 12

    1.3.2 Measurement-based computation . . . . . . . . . . . . . . . . . . . . . 13

    1.4 Introduction to programming languages . . . . . . . . . . . . . . . . . . . . . 14

    1.4.1 Quantum programming languages . . . . . . . . . . . . . . . . . . . . 16

    1.5 Introduction to process algebras . . . . . . . . . . . . . . . . . . . . . . . . . . 17

    1.5.1 Quantum process algebras . . . . . . . . . . . . . . . . . . . . . . . . . 18

    2 Current state of the art 192.1 Functional languages and -calculi . . . . . . . . . . . . . . . . . . . . . . . . 19

    2.1.1 Quantum q-calculus van Tonder . . . . . . . . . . . . . . . . . . . . 19

    2.1.2 QPL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

    2.1.3 QML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

    2.1.4 Quantum -calculus Selinger & Valiron . . . . . . . . . . . . . . . . 21

    2.1.5 nQML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

    2.2 Other languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

    2.2.1 qGCL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

    2.2.2 QCL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

    2.2.3 Q language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

    2.2.4 QMC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

    2.3 Quantum process algebras . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

    2.3.1 CQP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

    2.3.2 QPAlg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

    2.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

    II LanQ programming language 27

    3 Introduction 29

    3.1 Informal introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

    iii

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    6/155

    CONTENTS

    4 Syntax 33

    4.1 Concrete syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

    4.2 Internal syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

    4.3 Configuration syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

    5 Typing 39

    5.1 Typing rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

    6 Basic concepts 45

    6.1 Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

    6.2 Reference-related concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

    6.3 Variable-related concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

    7 Operational semantics 497.1 Memory model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

    7.2 Variable properties storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

    7.3 Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

    7.4 Variable properties handling functions . . . . . . . . . . . . . . . . . . . . . . 56

    7.5 Local memory handling functions . . . . . . . . . . . . . . . . . . . . . . . . . 58

    7.6 Functions for handling aliasfor constructs . . . . . . . . . . . . . . . . . . . . 60

    7.7 Internal values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

    7.8 Transitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

    7.9 Runtime errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64

    7.10 Processes and configurations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

    7.10.1 Structural congruence . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

    7.10.2 Nondeterminism and parallelism . . . . . . . . . . . . . . . . . . . . . 66

    7.11 Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66

    7.11.1 Basic rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66

    7.11.2 Promotable expressions . . . . . . . . . . . . . . . . . . . . . . . . . . 67

    7.11.3 Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

    7.11.4 Variable declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

    7.11.5 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68

    7.11.6 Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

    7.11.7 Conditional statement if . . . . . . . . . . . . . . . . . . . . . . . . . 70

    7.11.8 Conditional cycle while . . . . . . . . . . . . . . . . . . . . . . . . . 707.11.9 Method call . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

    7.11.10Returning from a method . . . . . . . . . . . . . . . . . . . . . . . . . 72

    7.11.11Forking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

    7.11.12Measurement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

    7.11.13Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

    8 Unique resource ownership 77

    8.1 Unique quantum system ownership . . . . . . . . . . . . . . . . . . . . . . . . 77

    8.2 Unique channel end ownership . . . . . . . . . . . . . . . . . . . . . . . . . . 79

    8.3 Unique channel ownership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

    iv

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    7/155

    CONTENTS

    9 Type soundness 839.1 Typing of configurations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839.2 Progress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849.3 Type preservation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

    9.3.1 Evaluation theorems . . . . . . . . . . . . . . . . . . . . . . . . . . . . 889.3.2 Type preservation lemmata . . . . . . . . . . . . . . . . . . . . . . . . 97

    9.4 Type soundness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

    10 Comparison with related work 11110.1 LanQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11110.2 Omer: QCL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11110.3 Gay, Nagarajan: CQP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11410.4 Jorrand, Lalire: QPAlg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

    11 Conclusion 119

    References 121

    III Appendix 131

    A Program execution example 133

    B Implementation 139B.1 LanQ simulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139B.2 Reference manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140

    B.2.1 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140B.2.2 Quantum operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141B.2.3 Variable declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143B.2.4 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143B.2.5 Operators and methods . . . . . . . . . . . . . . . . . . . . . . . . . . 144

    v

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    8/155

    CONTENTS

    vi

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    9/155

    Preface

    The original motivation for development of LanQ was the need of an imperative quantumprogramming language with the syntax similar to some well-known classical language, well-defined semantics, and an existing simulator. Moreover, this language was requested tosupport implementation of multiparty protocols.

    Such a language can be used by the scientists working in the quantum information process-ing theory field to implement and test their ideas of new quantum algorithms and protocols.They would learn the language easily because it is similar to something they already know.The existing semantics enables them to formally verify various program properties. More-over, the simulator could be connected to existing verification tools thus supporting machine-assisted verification. Further, it could benefit from existing optimization techniques to helpin semi- or fully automatic optimizations of the implemented algorithms and protocols.

    In the thesis, we define a typed imperative quantum programming language LanQ thatallows programmers to implement any algorithm that is based on either classical or quantumcomputation or a combination of both. It has well-defined operational semantics, and typesoundness of its part lacking communication capabilities is proved. It allows programmers to

    assign one quantum system to several variables in such a way that all quantum principles,namely no-cloning and no-deleting, are obeyed. The language syntax is similar to that of Clanguage.

    The developed language has several process algebraic features such as creation a newprocess (this is done by forking a new process from a running one) and interprocess com-munication. It therefore supports implementation of multiparty protocols. The quantumresources are proved to be always accessible to at most one process and the channel resourcesto at most two processes at a time. We prove type soundness for unrestricted languageprovided the programs are guaranteed to use only a well-formed communication.

    The first part of the thesis offers an introduction to the fields used in the thesis in Chapter 1and provides a list of related work in Chapter 2.

    The second part presents the developed quantum programming language LanQ. We for-mally describe the language syntax in Chapter 4, its type system in Chapter 5, definitionsof auxiliary functions and structures in Chapter 6, and its memory model and operationalsemantics in Chapter 7. Further, we prove the unique resource ownership property in Chap-ter 8, and type soundness in Chapter 9. Detailed comparison with several related formalismsis given in Chapter 10.

    The appendices form the last part of the thesis. We show an example run of a randomnumber generator implemented in the language in Appendix A, and provide a description ofthe implementation of LanQ simulator in Appendix B. The simulator is publicly available athttp://lanq.sf.net/

    vii

    http://lanq.sf.net/http://lanq.sf.net/
  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    10/155

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    11/155

    Part I

    Introduction

    1

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    12/155

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    13/155

    Chapter 1

    Introduction

    1.1 The history of quantum information processing

    Quantum information processing is a relatively young branch of computer science and physics.It combines paradigms of classical computer science and methods of information processingwith the phenomena exhibited in the quantum world on the basis of the laws of quantummechanics.

    The birth of quantum information processing was motivated by several things. It wasFeynman in 1982 who observed that there cannot exist any efficient classical algorithm for sim-ulating quantum mechanics [Feynman, 1982]. It was then suggested that building a computerbased on the quantum mechanical principles could help in this simulation. It was conjecturedthat a quantum computer could use less resources and/or solve several types of problems

    faster than classical computers. Another motivation came from the field of microelectronics.Current design of processors leads to so small sizes that in only a few decades, according toMoores law [Moore, 1965], circuit elements would reach atomic level where quantum phe-nomena inevitably appear. Then anyone designing such small circuits would necessarily needto know how to deal with the quantum phenomena. The third big motivation for develop-ment of quantum information processing is a better understanding of the quantum world andcreation of formalisms and tools that help in handling quantum phenomena.

    The first contribution to quantum information processing field was published in 1984 byBennett and Brassard a quantum key distribution protocol known nowadays as BB84 proto-col [Bennett and Brassard, 1984] which is proved to be unconditionally secure [Mayers, 1996].This was the first published work showing the relation of quantum information processing tocryptography. The next contribution to a family of quantum secure key distribution protocolswas given by Ekert in 1991 [Ekert, 1991]. Ekerts protocol uses quantum entanglement aspecific correlation of several quantum particles not exhibited in the classical world. Bennett,Brassard and Mermin in 1992 created a simplier scheme for quantum key distribution whichis also based on entanglement [Bennett, Brassard, and Mermin, 1992].

    Teleportation of a quantum bit [Bennett, Brassard, Crepeau, Jozsa, Peres, and Woot-ters, 1993] is a protocol which illustrates another feature of the quantum world which is notobserved in the classical world. Classically, when we want to move information from onepart of a system to another, we must send the physical medium containing the data to thedestination. Surprisingly, this is not necessary in the quantum world. Quantum state can beteleported to the destination without physically moving the particle whose state is teleported,

    3

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    14/155

    1.2. INTRODUCTION TO QUANTUM COMPUTING

    only two auxiliary classical bits are needed in the protocol.In 1994, Shor invented a famous polynomial-time quantum algorithms for integer factor-

    ization and discrete logaritm [Shor, 1994]. This was a big improvement over the classicalalgorithms which run at best in exponential time. The existence of Shors algorithms gavea great impetus to the development of quantum information processing. They represent aserious threat to currently publicly used cryptography: The security of many algorithms usednowadays (eg. RSA, ElGamal) depends on the computational hardness of factorization of bigintegers and discrete logaritm computation. If a quantum computer existed and the Shor al-gorithms were implemented, this would endanger the current secure internet communicationwhich heavily uses these algorithms eg. for securing internet banking and digital signatures.

    Another algorithm which is an example that quantum algorithms can be exponentiallyquicker than the classical ones is Deutsch-Jozsa algorithm for discriminating balanced func-tions from the constant ones [Deutsch and Jozsa, 1992]. Computational complexity of theDeutsch-Jozsa algorithm is constant while complexity of the algorithm solving the same prob-lem using only classical deterministic computation is exponential (in number of bits necessaryto encode the function input).

    Quantum information processing also contributed to other fields of computer science. Weshould mention mainly the contributions of quantum computer science to both computationaland communication complexity theories. The most general proofs of impossibility of uncon-ditional security of coin tossing and bit commitment employ quantum information theoreticresults.

    But quantum information processing is not only a theoretical field. The results fromquantum cryptography are practically used even nowadays. Devices for secure key estab-lishment (on distance of more than 100 km over an optical fibre) and truly random numbergenerators (a USB device or a PCI card) are already on the market. There is a runningSECOQC project which aims to develop European Quantum Key Distribution network [Al-leaume, Bouda, Branciard, Debuisschert, Dianati, Gisin, Godfrey, Grangier, Langer, Lever-rier, Lutkenhaus, Painchault, Peev, Poppe, Pornin, Rarity, Renner, Ribordy, Riguidel, Salvail,Shields, Weinfurter, and Zeilinger, 2007]. However, quantum computers are still in experi-mental phase and can be only found in laboratories.

    In the following few sections, we give a brief introduction to the fields used in this thesis,namely quantum computing, programming languages and process calculi.

    1.2 Introduction to quantum computing

    Quantum computing combines results from classical computer science and quantum mechan-ics. In this section, we give a brief overview of quantum computing basics. For an in-depthoverview of the employed sciences we refer the reader to many existing books, eg. [Gruska,1997, Knuth, 1998, Sipser, 2005] for computer science, [Feynman, 2006, Peres, 1995] for quan-tum mechanics, and [Gruska, 1999, Nielsen and Chuang, 2000, Preskill, 2000] for quantumcomputer science.

    1.2.1 Basics of quantum mechanics

    Although we do not intend to describe quantum mechanics theory in detail, in this section wegive a brief overview of quantum mechanics principles used for computation. In the followingtext, we assume that the reader is familiar with the basic notions from linear algebra.

    4

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    15/155

    CHAPTER 1. INTRODUCTION

    Pure states

    From classical world we are used to the fact that a smallest unit of information a bit can

    take only value 0 or 1, ie. it is a discrete two-level system. The value of the system is exactlygiven by either one of those levels.

    A quantum analogue of a bit is a qubit a quantum bit. Like the classical case, a qubitcan be in one of two basis states which we denote |0 and |1. Contrary to classical bits, aqubit q state can also be in a superpositionof these states:

    q = |0 + |1

    where , C and we require that ||2 + ||2 = 1. Informally, a quantum bit can behave asif it was in both states |0 and |1 simultaneously.

    Let us formalize the previous intuition of a quantum system. We will not restrict ourselves

    to qubits, ie. two-level systems, but define the formalism for general finite quantum states.A finite quantum system is represented by a complete finite-dimensional Hilbert space1:

    Definition 1.2.1. A metric vector space is called complete if a limit of any Cauchy sequenceof vectors is an element of this vector space.

    Definition 1.2.2. LetH be a complete finite-dimensional complex vector space and | :H H C be an inner product on this space. A finite-dimensional Hilbert space is thecomplex vector space H together with an inner product |.

    The inner product defines a vector norm and orthogonality of two vectors:

    Definition 1.2.3. For a vector , we define its norm |||| to be:|||| =

    |.

    Two vectors , are said to be orthogonal if | = 0. They are called orthonormal if theyare orthogonal and their norms equal to one. A (orthogonal, orthonormal) basis of a vectorspace H is a set of (orthogonal, orthonormal) linearly independent vectors (so called basisvectors) such that any vector from H can be expressed as a linear combination of the basisvectors.

    Remark 1.2.4. In the thesis, we work with orthonormal bases only, therefore in the rest ofthe thesis, by basis we always understand orthonormal basis.

    A qubit is represented by a two-dimensional Hilbert space, hence its basis consists ofexactly two orthonormal vectors. But even in the qubit case, there are uncountably manychoices of them. To be able to perform any computation we pick one pair of orthonormalvectors:

    |0 =

    10

    and |1 =

    01

    .

    These vectors form an orthonormal basis which we call a standard basis (also called computati-onal basis).

    1We only consider the finite-dimensional Hilbert spaces as we will not use infinite quantum systems in thisthesis. For further information regarding infinite states see eg. [Peres, 1995].

    5

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    16/155

    1.2. INTRODUCTION TO QUANTUM COMPUTING

    In the real world, we assign these two vectors to distinguishable states of some physicaltwo-valued observable quantity of a quantum particle, eg. photon polarization (horizontal:

    |0

    =|

    , and vertical:|1

    =|

    ) or a spin of an electron (|0

    =|

    ,|1

    =|

    ).Besides the standard basis, there is one other very important basis. It is a dual basis (also

    called diagonal or Hadamard basis) {|+, |} where:

    |+ = 12

    (|0 + |1) = 12

    11

    , and | = 1

    2(|0 |1) = 1

    2

    1

    1

    .

    This basis is used eg. in the BB84 protocol [Bennett and Brassard, 1984] (together with thestandard basis).

    After stating how a quantum system is represented, we now formalize its state:

    Definition 1.2.5. LetH be an n-dimensional Hilbert space and let 0, . . . , n1 be a basis ofH. A (pure) state of a quantum system H is described by a (unit) vector:

    =n1i=0

    ii H

    where i C andn1

    i=0 |i|2 = 1. The numbers i are called probability amplitudes.Dirac introduced a very useful bra-ket notation. Let be a unit column vector from a

    Hilbert space H. Then: Bra vector | = where () = ()T is an operation of complex conjugation and

    transposition. Mathematically,

    |is a linear functional, it is an element of a dual space

    of H. It is represented by a row vector, Ket vector | = is the original column vector.

    Closed quantum system evolution

    A closed quantum system is a quantum system which does not in any way interact withother systems. Although in the physical reality such a system does not exist (possibly up tothe Universe itself), it provides a good approximation for many experiments. Evolution of aclosed quantum system from a state | to a state | is represented by a linear operation Uacting on states. This operation is required to be unitary. This requirement comes from the

    requirement on quantum theory consistency: The operation has to map valid quantum states(ie. unit vectors) to valid quantum states (ie. to unit vectors).

    Definition 1.2.6. We call a complex matrixU unitary iffU U = UU = I, where U denotesan operation of complex conjugation and transposition, and I is an identity matrix.

    The evolution of a closed quantum system from a state | is represented by multiplicationof | by a complex-valued unitary matrix U which describes the operation applied onto thesystem:

    | = U|.The unitarity of the matrix has an important implication to the quantum system evolution:

    Any closed quantum system evolution is reversible. This is because the unitary matrices

    6

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    17/155

    CHAPTER 1. INTRODUCTION

    represent reversible operations on state vectors. The inverse operation to a unitary matrix Uis the operation represented by a (unitary) matrix U.

    Examples of unitary operations are an identity operator I and Pauli matrices x, y andz . These operations play quite important role in various areas of both quantum mechanicsand quantum information processing (see eg. [Feynman, 2006]). Among other things, theyare necessary components in several quantum computational models (see Subsection 1.3.2).Their matrix representation in the standard basis is the following:

    I =

    1 00 1

    , x =

    0 11 0

    , y =

    0 ii 0

    , z =

    1 00 1

    .

    Another useful operation is so called Hadamard transformation. It is often used in quan-tum algorithms, for instance in quantum Fourier transform. The transformation is given bythe following actions on basis vectors: |0 |+ and |1 |. Its matrix representation isthen the following:

    H =1

    2

    1 11 1

    .

    Obtaining classical information about quantum states

    In the classical case, we can read a bit value as many times as we wish without disturbing theinformation it carries. Obtaining classical information about quantum state however affectsthe state in an irreversible way.

    Classical information about a quantum state is obtained by a measurement of a quantumstate.2 A measurement performs stochastic projection of the state vector to some subspaceof the state space making the state collapse to the corresponding subspace. Any possible

    superposition of the measured quantum system state is destroyed. Given a quantum stateand a description of a measurement, the probabilities of individual outcomes are predictable.

    Definition 1.2.7. For a Hilbert space H, a projector is a linear operation P : H H suchthat P2 = P and P = P.

    Quantum projective measurement (also called von Neumann measurement) is given by aset of projectors {Pi}i such that

    i

    Pi = I, PiPj = 0 for i = j.

    In the process of a measurement of a state |, one of the projectors Pi is randomly appliedto the state. The probability of choosing the projector Pi is given by the third postulateof quantum mechanics and equals to |Pi||2. After the measurement, the quantum statecollapses to the state:

    =Pi|

    |Pi||2 .

    Note that if | H is a state then || is a projector. This allows us to specifya projective measurement by a Hilbert space basis: If {|i}i is an orthonormal basis of aHilbert space H then i |ii| = I. Therefore the measurement is given by the projectors

    {|ii|}i.2We will not describe the most general form of the measurement. For information on this account see eg.

    [Nielsen and Chuang, 2000].

    7

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    18/155

    1.2. INTRODUCTION TO QUANTUM COMPUTING

    We usually specify the measurement with respect to physical quantities by observables.A physical observable is a self-adjoint operator. We can decompose an observable O using itseigenvalues i and eigenvectors

    |i

    as follows:

    O =

    i

    i|ii|.

    Note that the |ii| part of the decomposition specifies a projector Pi. We call this form ofthe observable O a spectral decomposition of O. Measurement with respect to an observableresults into one of its eigenvalues and makes the state collapse to the subspace spanned bythe corresponding eigenvectors.

    Mixed states

    Imagine a source of photons that produces a vertically polarized photon with probability pand a horizontally polarized one with probability 1 p. When we obtain a photon out of it,we want to describe our a priori knowledge about its state before any measurement. Can wedo so?

    In this case, our knowledge about the photon state is incomplete for we only know theprobabilities of individual outcomes. Nevertheless, even such incomplete information can berepresented in the quantum formalism:

    Definition 1.2.8. A mixed state is a probability distribution{pi, |i}i over pure states |i.

    Therefore we can represent our photon state as:

    {(p, |), (1 p, |)}.

    Mixed states are represented by density matrices:

    Definition 1.2.9. Let A = (aij) be a square matrix in a finite complex Hilbert space H. Atrace of a matrix A is defined as Tr A =

    i aii. A square matrix A such that A = A

    iscalled hermitian. A matrix A is said to be positive if it is hermitian and |A| 0 for all| H. A density matrix (also called a density operator) is a positive matrix A such thatTr A = 1.

    To any mixed state {(pi, i)}i we assign a density matrix : =

    i

    pi|ii|.

    Note that a pure state | is represented by a density matrix = ||.

    Remark 1.2.10. The trace of a density matrix can be thought of as a probability thata quantum system is in the state . In the definition, we require that the state exists withprobability 1 (the condition Tr A = 1). However, there is an alternate definition of a densitymatrix [Selinger, 2004] which allows the trace to be also strictly less than one. In the thesis,we work with density matrices in the sense of Definition 1.2.9.

    8

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    19/155

    CHAPTER 1. INTRODUCTION

    One density matrix can represent many different mixed states. For example, the mixedstates:

    ={

    (1

    2,|0), (

    1

    2,|1)}

    and ={

    (1

    2,|+

    ), (

    1

    2,|

    )}

    have the same density matrix:1

    2

    1 00 1

    .

    However, as mixed states described by the same density matrix are physically indistinguish-able, this correspondence of one density matrix to many mixed states does not cause harmto us.

    We follow the usual convention and write a state instead of a mixed state representedby a density matrix .

    A prominent place among the mixed states belongs to the maximally mixed state amixed state of an n-dimensional quantum system whose eigenvalues are all equal to 1

    n

    . Sucha state can be interpretted as follows: For any basis, the system is in any of the basis stateswith equal probability 1

    n. The density matrix of this state is:

    1

    nI where I is an identity operator of order n.

    Compound systems

    In the classical case, we can compose bits to create larger data types, eg. a byte is composedof eight bits. It is now a natural question how we can combine smaller quantum systemsto get larger ones. Imagine the situation when we have two particles in states H and

    H, respectively. We want to describe the state of the system composed of these two

    particles.In contrast to the classical case, it is not enough to describe the state of the individual

    subsystems (what suffices in the case of bits). The reason is that in the quantum world, thetwo systems might be quantum-correlated. The overall state of the system is therefore anelement of a tensor product of the subsystem spaces:

    H H.In the case when the states and were prepared separately and did not interact, theiroverall state is given by the tensor product of the subsystem states:

    =

    .

    Definition 1.2.11. Let us have two matrices A and B:

    A =

    a00 a0m... . . . ...am0 amm

    , B = b00 b0n... . . . ...

    bn0 bnn

    .Their tensor product is defined as:

    A B =

    a00B a0mB

    .... . .

    ...am0B

    ammB

    .

    9

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    20/155

    1.2. INTRODUCTION TO QUANTUM COMPUTING

    Note that this way we have also defined the tensor product for vectors because vectorscan be regarded as matrices with only one column.

    We use the following standard notation for qubits: in place of|b0

    |b1

    |bn

    for

    basis vectors |bi, we often write |b0b1 . . . bn.There exist quantum states of a compound system that cannot be written as a tensor

    product of states of two separate systems. We call such states entangled. Entanglementis a very specific feature of the quantum world. Due to presence of entanglement, nonlocalphenomena can occur in the quantum world actions performed on one part of the compoundsystem can immediatelly affect the other part of the system.

    Consider the following important state of two-qubit compound system:

    =1

    2(|00 + |11).

    The state is called EPR state, named after Einstein, Podolsky and Rosen who first showedthe occurence of nonlocal phenomena in quantum mechanics using a very similar state [Ein-stein, Podolsky, and Rosen, 1935], and the pair of qubits is called EPR pair. This state hasa nice property: If one qubit is measured (in the standard basis), we obtain a result b whichis either 0 or 1, and the whole state collapses either to |00 or |11. If we then measurethe second qubit in the same basis, we always obtain the same result b. This holds even ifthe quantum particles are separated by a large distance. The EPR state is used in variousquantum protocols, among others in teleportation [Bennett et al., 1993] or several quantumkey distribution protocols [Ekert, 1991, Bennett et al., 1992].

    Now we know how to represent a state of a quantum compound system. Indeed, aninteresting question is: Given a state AB HA HB of a compound system, what is thestate of its subsystems? To answer this question, we define a reduced density operator:

    Definition 1.2.12. Let AB HA HB be a density operator representing the state of atwo-particle quantum system AB. Further, let{i}i be a basis of the system B. The stateof system A is represented by a reduced density operator A that is given by partial traceoperation:

    A = TrBAB =

    i

    i|AB |i.

    The partial trace operation is a generalization of the trace operation.Next we turn our attention to the operations acting on a compound system H = HAHB.

    These operations are represented by matrices from H. An operation U which can be written asa tensor product U = UA

    UB acts on the individual subsystems separately. The operations

    UA HA, UB HB are then called local operations. Similarly to the quantum states, there arealso quantum operations that cannot be written as a tensor product of two local operations.An example of such an operation is a CNOT(Controlled-NOT3) operation which operates ontwo qubits, performing a NOT operation on the second qubit if the first qubit is in state |1:

    CNOT :

    |00 |00|01 |01|10 |11|11 |10

    , its matrix representationin standard basis is

    CNOT =

    1 0 0 00 1 0 00 0 0 10 0 1 0

    .3Controlled operations are such operations which are executed on a set of target quantum systems only if

    some other controlquantum systems are in a specific state.

    10

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    21/155

    CHAPTER 1. INTRODUCTION

    Mixed state evolution

    We now describe the formalism used to describe evolution of a quantum system represented

    by the density matrix: =

    i

    pi|ii|.

    The evolution of a state is described by an operator E: which satisfies:

    Eis linear: = E() =

    i

    piE(|ii|),

    If E is applied on a density matrix it yields again a density matrix. Informally thismeans that if an operation described by E is applied onto a valid system, we obtainagain a valid system. We hence require that E maps a positive matrix to a positivematrix, and preserves trace of the matrix as both Tr and Tr are equal to one (bydefinition of a density matrix),

    If E is applied to a part A of a compound system AB then the compound systemremains in a valid state. In particular, it can be described by a density matrix. Thiscondition is called complete positivity.

    Now we formalize the previous requirements.

    Definition 1.2.13. LetHB be a Hilbert space. We call a linear operator F : HB HB:

    trace-preserving if for for any positive matrix

    B HB, Tr

    B= Tr F(

    B),

    positive if for any positive matrix B HB, F(B) yields a positive matrix, completely positive if F is positive and for any positive matrix B HB, Hilbert space

    HA and a positive matrix AB HA HB such that TrA AB = B, (I F)AB is apositive matrix.

    A trace-preserving completely positive operator is called a superoperator.

    The name superoperator was given because it transforms density operators to densityoperators.

    Remark 1.2.14. Like for density matrices, the superoperator can be alternatively defined as

    a completely positive operator E such that for a density matrix A in the sense of Remark1.2.10, Eis a completely positive operator where Tr E(A) Tr A.

    The evolution of a closed quantum system is like in the case of pure states represented byan application of a unitary operator on its density matrix. However, as we do not apply thematrix to the vector but to a matrix, there is a difference: Should such a unitary operationU be applied to a density operator , we represent it as a superoperator EU:

    = EU() = U U.

    Superoperators can also represent evolution of open quantum systems, ie. quantum sys-tems which interact with other systems (environment) whose state is unknown. Such an

    11

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    22/155

    1.3. MODELS OF QUANTUM COMPUTATION

    evolution is not necessary unitary. Let us have a pure state and let the evolution be repre-sented by a superoperator E. Then the state E() can be mixed.

    This property is used to represent eg. noisy channels. Consider a channel where a qubitpasses without any change with probability p and the qubit value is flipped from 0 to 1 andvice versa with probability 1 p. This channel is called a bit-flip channel. Operation Ebfwhich represents this channel acting on a state is given as:

    Ebf() = pII + (1 p)xx = p

    1 00 1

    1 00 1

    + (1 p)

    0 11 0

    0 11 0

    .

    1.3 Models of quantum computation

    In this section, we describe several existing models of quantum computation. We start with

    models whose predecessors were successful models of classical computation.

    1.3.1 Models based on classical models

    There have been many successful attempts to adapt classical computational models to thequantum world, for example quantum finite automata [Kondacs and Watrous, 1997], quantumcellular automata [Watrous, 1995], quantum Turing machine [Deutsch, 1985, Bernstein andVazirani, 1997]. We describe two widely-used models based on classical predecessors whichwe use later in the thesis, namely quantum circuits and QRAM model, in more detail.

    Quantum circuits

    Classical circuits are well known and widely used model for design of variety of electronicaldevices televisions as well as computer processors. It is a well-readable formal (albeit graph-ical) specification of the device. Logic circuits realize Boolean-logic functions. Its primitivesare wires and gates, eg. gates for realizing NOT, NAND, OR or XOR operations.

    Quantum circuit model introduced by Deutsch in [Deutsch, 1989] is based on classicallogic circuit model. Quantum bits passing through the circuit gates are represented by wires.The gates labelled boxes drawn on these wires represent unitary operations acting onthe qubits. Because of the unitarity of the operations, the number of wires cannot changein the circuit. By convention, the operations are applied to the qubits from left to right.Conventionally, the standard basis is used for computation.

    Quantum circuits cannot contain feedback loops, ie. no wire can return from right to left.Therefore no part of a circuit can be repeatedly used. It also does not support conditionalexecution of any part of the circuit, ie. we cannot implement any analogue of a classical ifstatement known eg. from C language.

    An example of a quantum circuit is shown in Figure 1.1. The circuit operates on twoqubits, first applying the Hadamard transformation to the first qubit (the H gate) and identityto the second qubit, and next applying Control-NOT operation to both qubits (the CNOTgate).

    In the algebraic formalism, we start with two qubits in states | and | and assumethey are not entangled. Therefore their overall state is | |. Application of the first gatein the first step means matrix multiplication of the state | | by the unitary operationH

    I where I is an identity matrix (for we perform Hadamard transformation on the first

    12

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    23/155

    CHAPTER 1. INTRODUCTION

    qubit | but do nothing on the second qubit |). It is followed by multiplication of theresult by CNOT matrix:

    CNOT (H I) (| |) =

    1 0 0 00 1 0 00 0 0 10 0 1 0

    1 0 1 00 1 0 11 0 1 00 1 0 1

    (| |).This circuit is used to create an EPR pair. If it is given two qubits in the initial state |0|0,the state of these qubits gets transformed to the EPR state.

    QRAM model

    Quantum Random Access Machine (QRAM) is a classical random access machine in the

    usual sense able to perform several quantum operations quantum state preparation, unitaryoperation and measurement on quantum registers [Knill, 1996]. The registers are allocatedas needed during a program run. This model supports manipulation of both classical andquantum data, the control of programs is classical.

    Classical program control enables QRAM to support loops and conditional statements.It makes a QRAM model a universal model that can express any classical and/or quantumalgorithm in its pseudocode which is also described in [Knill, 1996]. This model is thereforestronger than quantum circuits.

    For its features, the QRAM model is much more interesting for quantum algorithm de-signers than quantum circuits. Although semantics of the language is not specified formally,it suffices to standardize algorithm notation what was the original intention of the Knills

    paper.The LanQ language described in this thesis can be described by this model.

    1.3.2 Measurement-based computation

    Measurements are used to obtain classical information about a quantum state. The com-putational models mentioned in previous subsection usually use measurements to obtain theclassical information only about the result of the computation. Surprisingly, one can usemeasurements also to perform computation.

    One-way quantum computer

    The one-way quantum computation [Raussendorf and Briegel, 2001] is based on the work withso called cluster states a multipartite quantum state organized into a rectangular latticein which the particles are entangled in a specific way. The preparation of this state hasconstant time complexity: initially all qubits up to the input ones are in the |+ state, then

    | H

    CNOT

    |

    Figure 1.1: Example of a quantum circuit.

    13

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    24/155

    1.4. INTRODUCTION TO PROGRAMMING LANGUAGES

    the Control-Z operation is applied to the qubits neighbouring in the lattice. As the Control-Zoperation commutes, it can be applied onto all qubits at the same time.

    The computation itself consists of one-qubit measurements performed in certain orderand basis. Results of individual measurements are uniformly random, introducing an errorinto the computation. To correct this error, the basis of the next measurement is adaptedaccordingly.

    The result qubits also suffer from this type of error. However, it was shown in the originalpaper that this error is correctable and always takes the form ax

    bz where the x, z are

    Pauli operators and coefficients a and b are either 0 or 1 and are known from the previousmeasurement outcomes.

    Teleportation-based quantum computer

    Another model developed by Nielsen [Nielsen, 2003] and improved by Leung [Leung, 2004] is

    based on the following observation: The teleportation protocol [Bennett et al., 1993] transfersa state of one qubit from one place to another up to a known and simply correctable error,thus performing an identity operation (up to the error) on the given state. The teleportationemploys quantum projective measurement in Bell basis {| + +, | + , | +, |}. Usingother bases, we can perform operations other than the identity, again up to a known error.

    In this model, the connection between the operations and the bases is shown. The originalNielsens formulation uses 4-qubit measurements to simulate any 2-qubit quantum gate whatwas improved by Leung who showed that 2-qubit measurements are sufficient. This modeluses four auxiliary qubits for simulation of any n-qubit quantum gate.

    The original model was further developed by Perdrix [Perdrix, 2005] who used state trans-ferin place of teleporation as the primitive for computation. This way the number of auxiliary

    qubits needed for the simulation of any n-qubit gate is reduced only to one qubit, hence reach-ing the optimum.

    1.4 Introduction to programming languages

    In this section, we briefly introduce the field of programming languages. For further reading,we refer the reader eg. to [Knuth, 1998, Slonneger and Kurtz, 1995].

    A programming language is a medium used for communication between a human anda machine. A programming language must provide rules for an unambiguous notation ofprograms. For this purpose, we define syntax and semantics of a programming language.

    Syntax defines symbols of a language and rules how these symbols can be put together toform phrases. Semantics defines meaning of individual.

    The semantics of a programming language is usually defined in one of the following ways:

    Operational semantics the meaning of a phrase is a sequence of abstract machinetransitions. To define operational semantics, one has to rigorously define a set of states(this can be eg. a -term or a state of some formally specified abstract machine) and atransition function which acts on the states. This type of semantics was pioneered byMcCarthy [McCarthy, 1963],

    Denotational semantics the meaning of a phrase is a phrase (denotation) in other lan-guage, usually in a mathematical formalism. For instance, each phrase of the language

    14

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    25/155

    CHAPTER 1. INTRODUCTION

    is associated a number or a function. The phrases can be combined to form biggerphrases according to the language syntax. The denotation of these bigger phrases isgiven by composing the denotations of their subphrases, therefore denotational seman-tics is compositional. This type of semantics was developed by Scott and Strachey [Scottand Strachey, 1971],

    Categorical semantics the semantics of a phrase is given by a morphism in a categorywith prescribed properties. Categorical semantics is hence more abstract than the de-notational. The actual shape of the category can vary a lot for example Abramskyand Coecke showed that quantum mechanics can be described by a strongly compactclosed category with biproducts [Abramsky and Coecke, 2004].4 As the category of finitevector spaces with unitary transformations, category of sets and relations and a planepicture calculus [Coecke, 2005] are such categories, they all can be used to interpretquantum theory.

    There are also other approaches to semantics, among others: action semantics, algebraicsemantics, axiomatic semantics, game semantics. For further reading on semantics of pro-gramming languages, we refer the reader to [Slonneger and Kurtz, 1995] and to [Abramskyand McCusker, 1999] specifically for game semantics.

    A paradigm of a programming language determines a way of expressing programs in thelanguage. The main paradigms are the following:

    Functional a program is an expression written in a declarative form. The programresult is given by reduction of the expression,

    Logical a program is a theory written in a declarative form. The program result is ananswer to some question. This answer has to satisfy the given theory,

    Imperative a program is a description of a way to obtain a result. During a programrun, a global variable specifying a program state is updated.

    An imperative programming language that supports definition of methods (or functions,routines, procedures) is called procedural. Methods are parametrized program fragments. Theyallow programmers to structure a program into smaller pieces of code. This improves read-ability of program code and makes it easier to maintain the program code. The program canthen invoke the methods from within a caller method. After a method finishes its execution,it eventually returns a return value to its caller.

    A higher-orderprogramming language allows one to manipulate functions as if they weredata. In such languages it is possible to create functions during a program run, functionscan return other functions as their return value etc. A language which is not higher-order iscalled first-order.

    A variable in imperative languages is a specification of a memory location where a valueis stored. In functional languages, a variable denotes a variable in the usual mathematicalmeaning.

    In general, it is possible to create syntactically correct terms that have no meaning. Toavoid this unwanted freedom, we add a type system to a language. A type of a variablespecifies a range of the valid values of the variable. Languages where variables are given

    4Similar result was independently obtained by Selinger [Selinger, 2005].

    15

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    26/155

    1.4. INTRODUCTION TO PROGRAMMING LANGUAGES

    a type are called typed [Cardelli, 2004]. To give an example of what typing is useful for,consider a type letter of letters of English alphabet. Let c denote a variable of type letter.Then c can stand for a letter g but it cannot represent a number 4. The usage of typesallows a compiler to detect such errors at compile-time. Therefore, taking c as in the previousexample, assignment of a value 4 to the variable c can be detected at compile-time and thiserror can be reported to the programmer.

    Types are particularly useful when developing a programming language that allows theprogrammer to express combination of both quantum and classical computations. For exam-ple, quantum data cannot be in general duplicated (due to no-cloning theorem, [Wootters andZurek, 1982]), while classical data can. This means that classical and quantum data mustbe handled differently what can be achieved eg. by using different types for quantum andclassical variables.

    Type soundness

    A useful propery of typed languages is type soundness. A language with sound type systemguarantees that if a term in the language is typable (ie. we can infer a type of the term), thenthe evaluation of this term never terminates because of a type error5 (weak type soundness)[Milner, 1978].

    We can often prove strong type soundness of a language which guarantees that the evalu-ation of a typable term t either diverges or yields an answer6 of the same type as the term t.Note that what several other authors, eg. [Cardelli, 2004], understand by type soundness isin fact strong type soundness.

    Type soundness of a language is nowadays most usually proved using a technique by

    Wright and Feleisen [Wright and Felleisen, 1994]. This technique is based on similar proofs ofsubject reduction from combinatory logic and on term rewriting as specified by operationalsemantics.

    1.4.1 Quantum programming languages

    A quantum programming language is a language that allows a programmer to describe com-putations in the quantum world and possibly also in the classical world. We can characterizeprogramming languages by quantumness of their control state and manipulated data.

    Classical programs can be characterized by the slogan classical control, classical data:both control state of a program and data are classical.

    Quantum programs can be characterized either by slogan quantum control, quantum dataor classical control, quantum data. Control state of a program in the former style is quantummeaning that it can be in a superposition of multiple basis states. In this case all the data arequantum. Programs in the latter style support manipulation of both quantum and classicaldata while employing a classical program control state. Note that this behaviour is exactlymodelled by a QRAM.

    Several existing quantum programming languages are discussed in Chapter 2.

    5A type error is a language-dependent concept usually meaning an error arising from passing such argumentsto a function which do not belong to the function domain, eg. attempt to add a number 7 to a boolean valuefalse.

    6Specification ofwhat the answer lo oks like is again language-dependent.

    16

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    27/155

    CHAPTER 1. INTRODUCTION

    1.5 Introduction to process algebras

    In this section, we briefly introduce the field of process algebras. For further reading, we

    refer the reader to [Fokkink, 2000] and [Bergstra, Ponse, and Smolka, 2001] for an in-depthintroduction to this field, and to [Baeten, 2004] for a historical overview.

    A process describes behaviour of a system, ie. a sequence of observable actions andevents that are performed in the system. A process algebra is the study of behaviour ofparallel or distributed systems by algebraic means [Baeten, 2004]. It is a mathematical toolfor describing or specifying processes and their interactions and reasoning about them bymeans of equations. Hence, process algebras are crucial for proving different kinds of processequivalences and properties of multiparty protocols.

    A process description consists of actions and their compositions. The set of actions isdependent on the system settings. An action can be for instance a keypress on a keyboardor an allocation of a resource. These actions can be composed together to form processes by

    many different operators. The most common composition operators are:

    Sequential composition P1.P2 of two processes P1 and P2 the process P2 is run afterthe process P1 is successfully completed,

    Alternative compositionP1 + P2 of two processes P1 and P2 the system nondetermin-istically executes either process P1 or P2

    7,

    Parallel composition P1 P2 (or P1 | P2) of two processes P1 and P2 the systemexecutes the processes P1 and P2 simultaneously,

    Probabilistic compositionP1p P2 of two processes P1 and P2 the system executes theprocesses P1 with probability p or P2 with probability (1 p).

    The most influential process algebras are Milners CCS (Calculus of Communicating Sys-tems) [Milner, 1980], CSP (Communicating Sequential Processes) by Hoare [Hoare, 1983] andACP (Algebra of Communicating Processes) by Bergstra and Klop [Bergstra and Klop, 1984].For further reading on these algebras, we refer the reader to [Baeten and Weijland, 1990] and[Bergstra et al., 2001].

    The work of Milner, Parrow and Walker led to the development of -calculus, a calculuswhere not only data but also communication channels can be passed over a channel from aprocess to another process [Milner, Parrow, and Walker, 1992]. This feature is used eg. tomodel a situation when a person A is travelling with a mobile phone while speaking to person

    B while travelling, the phone changes its base stations during the call but the communicationchannel between A and B remains the same.Any process algebra containing both nondeterministic and probabilistic choices has to

    specify which choice is solved first. For more information on this account see [Cazorla, Cuar-tero, Ruiz, Pelayo, and Pardo, 2003].

    Communication

    The interaction between two or more processes is done by communication. Process algebrasoffer tools for describing communication among processes, usually by means of abstraction of

    7Note that process algebras are inherently nondeterministic the choice of order in which the concurrentprocesses evolve is nondeterministic.

    17

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    28/155

    1.5. INTRODUCTION TO PROCESS ALGEBRAS

    communication links called channels. We distinguish two types of communication:

    Synchronous (blocking) communication when a sender attempts to send a message,

    its execution is delayed until there is another process which can receive. Similarly,when a receiver attempts to receive a message, its execution is delayed until there isanother process which sends a message over the corresponding channel. This type ofcommunication can indeed lead to a deadlock when there is no sender/receiver to areceiving/sending process,

    Asynchronous (nonblocking) communication a sent message waits in a channel untilthere is a receiver which can receive the message, the sender continues its executionwithout waiting for the receiver.

    It has been proved that asynchronous communication can be simulated using the syn-

    chronous one but synchronous cannot be simulated using the asynchronous [Palamidessi,1997].

    Note that communication can be a source of nondeterminism. Consider the following fourprocesses (we use the notation of -calculus):

    A(g) = g3.0 B(g) = g(x).0 C(g) = g(x).0 System = (g)(A(g)|B(g)|C(g))

    The process A sends a number 3 over the communication channel g and terminates. Bothprocesses B and C behave identically: After a successful reception of the number from thechannel g the process terminates. The process System allocates a channel g and executes theprocesses A, B and C in parallel. Now, which of the processes B or C receives the sent value3, is left to the nondeterminism.

    This type of nondeterminism was recognized earlier and there were several approaches tohandle it. Kobayashi, Pierce and Turner suggest in [Kobayashi, Pierce, and Turner, 1996]using a linear type system over the -calculus. A channel is assigned a polarity (whether itcan be used for sending or receiving) and multiplicity (how many times it can be used forcommunication, either 1 or ). Linear channels are those channels whose multiplicity is 1regardless of their polarity. Type system then uses polarities to guarantee that linear channelsare used by at most two processes at a time, one sender and one receiver. For a solution for amore complex formalism where channel types for structured communication (so called sessiontypes) can be specified see eg. [Gay and Hole, 2005, Yoshida and Vasconcelos, 2007]. Thesolution is also based on channel polarities checked by the type system.

    1.5.1 Quantum process algebras

    In quantum process algebras, the communication and the manipulated data can be classicalor quantum. This has a large impact on quantum resource handling: a process calculus mustguarantee unique ownership of quantum systems among processes.

    Moreover, if a quantum process algebra supports a combination of quantum and classicalcomputations, expression of quantum measurements must be allowed. Because the measure-ments are probabilistic by nature, it has to deal with probabilistic choice. As mentionedearlier, if the process algebra also supports nondeterministic choice, it must specify whichchoice the probabilistic or the nondeterministic is solved first when they occur simultane-ously.

    18

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    29/155

    Chapter 2

    Current state of the art

    In this chapter, we give a brief characterization of several quantum programming languagesand process algebras that have been developed so far. For the currently most complete surveyon quantum programming languages we refer the reader to [Gay, 2006] and to [Glendinning,2005] for an online list of them.

    Some of the languages will be discussed in more detail in Chapter 10 where they will bealso compared with LanQ.

    2.1 Functional languages and -calculi

    We can view programs as descriptions of functions which transform input values to someoutput. This is one reason why many of proposed quantum programming languages arefunctional. Another reason why functional languages are so popular, is that their operationalsemantics can be straightforwardly defined.

    -calculus is a mathematical formal system introduced by Church and Kleene in 1930s.It has a very simple and well-known syntax for writing expressions. Operational semanticsis straightforwardly given by reduction rules. Nevertheless, it is powerful enough to expressany computable function. Any functional language can be viewed as a syntactic variation ofthe -calculus [Slonneger and Kurtz, 1995]. This justifies the decision to put the functionallanguages and -calculi to the same section.

    2.1.1 Quantum q-calculus van Tonder

    The first successful attempt to create a functional quantum programming language was doneby Andre van Tonder in 2003 [van Tonder, 2003a]. He created untyped -calculus for quantumcomputing. He proved the model of this calculus to be computationally equivalent to quantumTuring machines. This -calculus was later extended to a typed -calculus in [van Tonder,2003b] where denotational semantics of the latter calculus was also developed.

    The language supports higher-order computations to be expressed.

    To deal with quantum resources, techniques from a linear -calculus were employed. Theresources are divided into two groups:

    Linear these resources can be neither copied nor discarded. Quantum states in the

    q-calculus are considered to be linear resources,

    19

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    30/155

    2.1. FUNCTIONAL LANGUAGES AND -CALCULI

    Nonlinear these resources can be both copied and discarded.A disadvantage of the quantum q-calculus is the nonexistence of measurements in the lan-

    guage. This constraint makes it impossible to combine classical and quantum computations.Therefore, this language can be used for describing purely quantum algorithms.

    The simulator is available from http://www.het.brown.edu/people/andre/qlambda/ .

    2.1.2 QPL

    Selingers QPL [Selinger, 2004] is an important typed functional language. This language canbe described by a slogan quantum data, classical control. It can be used to express anyalgorithm manipulating quantum and classical data or their combination.

    The syntax of the language has two interchangeable forms. A program can be describedby a graphical flow chart. This form is intuitive and has direct connection to the semantics

    but it is difficult to manipulate and discourages structured programming. For this reason, astructured textual form of the language exists.The linearity of quantum data is enforced by the syntax which ensures that syntactically

    distinct variables will refer to physically distinct ob jects. The language is very restrictive inhandling variables: eg. it denies to assign an expression or even a value of another variableto a variable. For example, if a and b are of type bit, the only way to set a to the value of bis the following: if b then a := 1 else a := 0. Indeed, this nonconformity can be overcomeby using syntactic abbreviations for frequently used statements.

    QPL is the first quantum language with well-defined denotational semantics. The se-mantics is given by assigning any program fragment a generalized superoperator acting ongeneralized density matrix tuples: A generalized density matrix D is a positive matrix such

    that Tr D 1. A generalized density matrix tuple (D0, . . . , Dn) is a tuple of generalized den-sity matrices Di such that

    ni=0 1. A generalized superoperator that acts on generalized

    density matrix tuples is then defined similarly to the usual definition of superoperator (see[Selinger, 2004]).

    The generalization of the density matrix formalism is used to represent of finite classicaldata. The i-th element of a generalized density matrix tuple Di represents the state ofquantum world if the overall state of classical memory is represented by the number i. Theprobability that the classical memory is in overall state i is given by the trace Tr Di.

    The fact that generalized superoperators form a complete partial order enables semanticsof loops and recursion to be defined naturally as a limit of a sequence of superoperators.

    Selinger also defines a categorical semantics of the language. Later he showed in [Selinger,

    2005] the connection between the categorical semantics of [Abramsky and Coecke, 2004] andthe semantics of QPL.

    2.1.3 QML

    QML is a typed language developed by Altenkirch and Grattage [Altenkirch and Grattage,2004]. It supports both classical and quantum computations to be expressed. Both data andcontrol of the program flow may be quantum.

    Even though the language is functional, it only supports first-order computations to beexpressed; all data types are finite. It is therefore not possible to treat functions as data.

    The technique used for developing the language semantics is interesting. First, they lookat classical computations and show them to be reversible in a closed system. Then they

    20

    http://www.het.brown.edu/people/andre/qlambda/http://www.het.brown.edu/people/andre/qlambda/
  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    31/155

    CHAPTER 2. CURRENT STATE OF THE ART

    explain irreversible classical computations based on reversible mechanics. To show it theyutilize a heap. Heap is an auxiliary space which can be used during computation.

    Let A, B be two finite sets. Reversible finite computation of a function f : A

    B isrepresented by a bijection on finite sets: f : A H B G where G is a set of possibleheap states after computation and H is a set of possible heap states before computation. Suchtwo bijections f, g are considered extensionally equivalent if the functions f and g, whosereversible computations they represent, are equal (f = g). Any computable function f on afinite domain can be represented by such a mapping f.

    The authors justify usage of superoperators for definition of semantics of quantum com-putation in the following way: A superoperator can be represented by a space expansion,application of a unitary transformation in the larger space and finally tracing out the aux-iliary space. This gives a picture similar to that of the previous paragraph. Therefore, anyquantum computation should be in general represented by superoperators.

    The authors put emphasis on the control of decoherence. For this purpose, their languageoffers two different ifthenelse constructs. One of them measures a qubit in the condition,the second one does not. However, the second if-construct can be used only in certain cases.

    Another tool used to control the decoherence is minimalisation of weakenings (ie. dis-carding of quantum states). This is achieved by the type system based on strict logic. Min-imalisation of weakenings is a difference to other quantum programming languages whichprevent contraction (duplication of a quantum system) to occur. Indeed, the language doesnot support duplication of quantum data, but two or more variables are allowed to refer tothe same quantum system.

    The language compiler is available from http://sneezy.cs.nott.ac.uk/qml/compiler/ .

    2.1.4 Quantum -calculus Selinger & Valiron

    The calculus developed by Selinger and Valiron in [Selinger and Valiron, 2006] is a typedquantum data, classical control -calculus. Higher-order computations can be expressed inthis language. Contrary to van Tonders language, combination of both classical and quantumcomputations is supported, measurement of a quantum system is a primitive in the language.

    To support handling of both quantum and classical data, the type system distinguishesbetween the types whose values are duplicable and whose not. This type system is used toguarantee that no-cloning and no-deleting of quantum data is not violated. Interestingly, thisnot only holds for the ground data types but also for the higher-order types. It is thereforepossible to have a function of type qbit qbit that can be called only once and anotherthat can be invoked multiple times.

    It is remarkable that the type system does not satisfy principal type property, ie. thatevery untyped expression has the most general type. Therefore the authors had to develop atype-inference algorithm which can determine whether a given term is typable in the lineartype system, and find its type.

    The authors define operational semantics of this language in terms of probabilistic reduc-tion rules. They prove type preservation and progress lemmata for well-typed programs.

    2.1.5 nQML

    nQML [Lampis, Ginis, and Papaspyrou, 2006] is a language based on the previously describedQML language by Grattage and Altenkirch. This language is characterized by the slogan

    21

    http://sneezy.cs.nott.ac.uk/qml/compiler/http://sneezy.cs.nott.ac.uk/qml/compiler/http://sneezy.cs.nott.ac.uk/qml/compiler/
  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    32/155

    2.2. OTHER LANGUAGES

    quantum data and control, allowing to perform a measurement at any point during aprogram run.

    The language supports variable aliasing hence several quantum variables can point to thesame qubit. To prevent a situation when one qubit is used more than once in one quantumoperation, the authors employ the type system. The type of quantum expressions containsspecification of the place of the qubits in which the expression value is stored. Then it is easyto check the requirement of distinctness of quantum variables without introducing linear typesystem.

    Denotational semantics of the language is defined using density matrices in terms of uni-tary operations (for terms not containing measurements) and general functions from densitymatrices to density matrices (for those containing measurements).

    A publicly available implementation exists and is available from ftp://ftp.softlab.ntua.gr/pub/users/nickie/papers/nqml06.code.tar.gz .

    2.2 Other languages

    2.2.1 qGCL

    Quantum Guarded Command Language (qGCL) was developed by Sanders and Zuliani in[Sanders and Zuliani, 2000, Zuliani, 2001]. Both quantum and classical computations andtheir combination can be expressed. qGCL was developed from the probabilistic languagepGCL which, in turn, was created using Dijkstras GCL as a basis. pGCL was chosen as abase of qGCL because of its rigorous semantics.

    A probabilistic guarded-command language program is a sequence of assignments and

    statements skip and abort. The program is manipulated by standard constructors of se-quential composition with conditionals, loops and probabilistic and nondeterministic choice.A quantum program in qGCL is a pGCL program which uses quantum procedures. Thesequantum procedures can be of three types: initialisation of a quantum state, evolution and

    finalisation (a measurement).

    This language can be used both as a specification language and a programming language.It has well defined operational semantics. The refinement calculus associated with qGCL wasinherited from pGCL and extended. It allows systematic program derivation and algebraicreasoning about qGCL programs. The refinement calculus also allows to simplify programsalgebraically. Simplification is seen as a particular program refinement which preserves se-mantics.

    Even though recursion is available in pGCL, the language qGCL does not support it[Zuliani, 2001, Sec. 2.4]. This was a decision during design of this language because of thecompilation technique used.

    2.2.2 QCL

    Imperative language QCL was designed by Omer in [Omer, 2000, 2003]. It supports bothquantum and classical computations to be expressed and freely interleaved. Its syntax isbased on that of C language what makes it easy to learn for ordinary programmers.

    This language is interesting for many reasons. The language offers tools for simple defini-tion of a quantum operator in the language. It supports creation of reversible quantum gates

    22

    ftp://ftp.softlab.ntua.gr/pub/users/nickie/papers/nqml06.code.tar.gzftp://ftp.softlab.ntua.gr/pub/users/nickie/papers/nqml06.code.tar.gzftp://ftp.softlab.ntua.gr/pub/users/nickie/papers/nqml06.code.tar.gzftp://ftp.softlab.ntua.gr/pub/users/nickie/papers/nqml06.code.tar.gzftp://ftp.softlab.ntua.gr/pub/users/nickie/papers/nqml06.code.tar.gz
  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    33/155

    CHAPTER 2. CURRENT STATE OF THE ART

    from classical-like code (so called quantum functions). This allows programmers with only arough knowledge of quantum mechanics to create quantum operators.

    QCL is not a higher-order programming language, but it retains some features of higher-order languages. For example, it is possible to automatically compute an inverse of a quantumoperator.

    The semantics of this language is not defined. Therefore, it is impossible to prove aprogram written in QCL to satisfy some property.

    Omer developed a QCL simulator that is publicly available from http://tph.tuwien.ac.at/~oemer/qcl.html .

    This language is further discussed in Section 10.2.

    2.2.3 Q language

    Betteli, Calarco and Serafinis Q language [Bettelli, Calarco, and Serafini, 2001] is not a

    language written from scratch. It is an extension of the existing C++ language. Like QCL,it supports description of both quantum and classical computations and their combination.

    Q philosophy is to extend existing standard language instead of creating a new one.This way programmers need not learn a new language, they just learn the extension.1 Thisapproach also allows the developers to use existing compilers for compilation of programswritten in Q. No new compilers or simulators need to be developed, it is possible to onlyextend the existing ones.

    This language supports combination of quantum and classical computation in the sameway as in QRAM model. This means that classical computations are performed on a clas-sical computer; quantum device, that performs quantum computations, is controlled by theclassical computer.

    Unitary operators are represented by objects in the sense of object-oriented programming.This representation allows programmers to create operators during a program run usingoperator composition, conjugation etc. Created operators can be optimised before they areused at the quantum device. If the quantum device does not support execution of arbitraryquantum operations, this approach allows the control machine to compute a decompositionof an operator to such primitives that the quantum device can perform.

    The semantics of this language is not defined formally. A publicly available implementa-tion exists and is available from http://sra.itc.it/people/serafini/qlang/ .

    2.2.4 QMC

    Quantum Model Checker by Gay, Nagarajan and Papanikolaou [Gay, Nagarajan, and Pa-panikolaou, 2007] is a single-process quantum model checker with a formally defined syntaxand semantics. It uses a subset of exogenous quantum propositional logic (EQPL) [Baltazar,Mateus, and Sernadas, 2007] to check the properties of the resulting state. The tool scansall possible paths which the execution of the checked program can take due to quantummeasurements and checks whether the EQPL formula is satisfied for all possible programruns.

    The model-checker uses the stabilizer formalism for quantum system state representation.Note that using this formalism, instead of one state, a set of quantum states satisfying givenstabilizer equations is represented. Stabilizer formalism only allows us to use Clifford group

    1This is the same idea that was used for development of the LanQ syntax.

    23

    http://tph.tuwien.ac.at/~oemer/qcl.htmlhttp://tph.tuwien.ac.at/~oemer/qcl.htmlhttp://tph.tuwien.ac.at/~oemer/qcl.htmlhttp://sra.itc.it/people/serafini/qlang/http://sra.itc.it/people/serafini/qlang/http://sra.itc.it/people/serafini/qlang/http://tph.tuwien.ac.at/~oemer/qcl.htmlhttp://tph.tuwien.ac.at/~oemer/qcl.html
  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    34/155

    2.3. QUANTUM PROCESS ALGEBRAS

    operations, 1-qubit measurements, and Clifford group operations conditioned on classical bitsto be applied onto a quantum state. Although the set of operations is not universal and notall quantum states can be expressed in this formalism, this model is rich enough to expressmany practically usable algorithms and protocols, eg. teleportation protocol [Bennett et al.,1993] or quantum dense coding [Bennett and Wiesner, 1992]. Moreover, it is known to beefficiently simulable on classical computers (Gottesman-Knill Theorem, [Gottesman, 1998])and an algorithm for its simulation exists [Aaronson and Gottesman, 2004].

    Both EQPL and stabilizer formalism limit the dimension of used quantum systems to two,hence only qubits can be used in the programs. Although it is an insuperable obstacle in awork with higher-dimensional quantum systems, this design decision has important impacton speed making the implementation very fast.

    Currently, the number of qubits manipulated by the program must be specified before theprogram is run and cannot be changed during the execution. Although this decision of theauthors does not usually cause a problem, the algorithms which allocate qubits dynamicallyin a loop can be limited by this restriction.

    The model checker implementation is under development.

    2.3 Quantum process algebras

    There have been developed several quantum process algebras. We describe two of them,QPAlg and CQP.

    2.3.1 CQP

    Communicating Quantum Processes developed in [Gay and Nagarajan, 2004, 2005, 2006] isanother quantum process algebra. It can describe both quantum and classical interactions andevolution of processes. The syntax and semantics of this algebra are based on the -calculus.

    In this algebra, the channels are dynamically allocated. Therefore, they are only availableto the allocating process and its subprocesses. However, the number of subprocesses that canuse an allocated channel for communication is not limited. This way three or more processescan use one channel hence introducing nondeterminism as described in Section 1.5.

    Communication channels are typed. The type of values that can be sent over individualchannels is given by the type of the channel. For example, sending a value of type char overchannel that can send only values of type bit, is impossible.

    The algebra guarantees unique quantum system ownership among processes. Contrary to

    QPAlg, type soundness is proved for the language. A typechecking algorithm exists and isproved to be sound and complete [Gay and Nagarajan, 2006].This algebra is further discussed in Section 10.3.

    2.3.2 QPAlg

    The quantum process algebra QPAlg by Lalire and Jorrand [Lalire and Jorrand, 2004, Jor-rand and Lalire, 2004] can describe both classical and quantum interaction and evolution ofprocesses. The syntax and operational semantics of this algebra are based on that of CCS andLotos. Recently [Lalire, 2006a], it was enhanced by a fixpoint operator and typed channels.

    Names of quantum variables are stored together with their state in a context. The stateof quantum variables used in any process in the system is represented by a density matrix.

    24

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    35/155

    CHAPTER 2. CURRENT STATE OF THE ART

    The context is also used for storing a mapping between classical variables names and theirvalues. The context is formed in a style of a cactus stackwhat allows one to declare variablesshared among several processes.

    Processes can communicate over named gates. The gates that processes can use forcommunication are static, ie. they are given before a process run. Hence, they need not beallocated before their usage. This means that any process has access to any channel until thechannels are hidden by a special handling construct.

    The algebra defines a bisimilarity relation on processes (which is an equivalence relationon processes) in the sense of probabilistic branching bisimulation [Lalire, 2006b]. This type ofbisimilarity is weaker than strong bisimilarity because it abstracts from silent transitions.

    This algebra is further discussed in Section 10.4.

    2.4 Summary

    All the programming languages presented in this chapter can be used for description of quan-tum algorithms. Some of the languages cannot describe combination of quantum and classi-cal computations (van Tonders q-calculus). But a practically-usable programming languageshould support such a combination to be expressed. The reason is that in practice, users passclassicalinput to a program and require classicaloutput to be returned. Therefore, a classicalpart of the program should process the input and invoke quantum computation. After thequantum computation is finished, it should present its output to the user in a classical form.

    Functional languages regard programs as mathematical expressions. Functional programsare manipulated according to mathematical rules. Two of the described functional languagessupport quantum data processing, but the flow control is classical Selinger and Valirons

    -calculus and Selingers QPL. Both languages require a quantum system to be referreduniquely, ie. at most one variable should refer to one quantum system. This is a very strongconstraint. In real programming, it is quite useful when two or more variables can refer toone quantum system. This constraint is overcome in the QML language by Altenkirch andGrattage and nQML by Lampis, Ginis and Papaspyrou. In their languages, it is allowed tohave two or more variables referring to a single quantum system.

    Contrary to functional languages, imperative languages rather reflect underlying machinearchitecture. Even though functional languages have better formal basis, practical program-mers prefer imperative programming languages to functional ones.

    The qGCL language can be used both as a specification language and a programminglanguage. Its well-defined semantics and refinement calculus make this program a good basefor optimisation tasks. However, this language lacks recursion.

    QCL is quite a usable language. It allows programmer to encode nearly any algorithm,quantum or classical, in the language. It contains many usable features like scratch-spacemanagement. However, the semantics of the language is not fully defined. Therefore formalproving of program properties is impossible. This is also true for the Q language.

    QMC is an imperative language that supports checking of EQPL formulas over stabilizerstates. Its speed makes it a good choice for verification of properties of quantum states evenin complex programs if these states and operations over them can be described in terms ofstabilizer formalism.

    A programming language LanQ developed in the dissertation is imperative and has well-defined operational semantics. The semantics is an improvement over two of the mentioned

    25

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    36/155

    2.4. SUMMARY

    imperative languages (QCL, Q-language). Moreover, it exhibits several features of processalgebras, being able to run several processes in parallel and supporting interprocess commu-nication. Its simulator has been implemented and is shortly described in Appendix B

    26

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    37/155

    Part II

    LanQ programming language

    27

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    38/155

  • 8/3/2019 Hynek Mlnark- Quantum Programming Language LanQ

    39/155

    Chapter 3

    Introduction

    In this part of the thesis, we formally define the language LanQ and prove its properties unique resource ownership and type soundness. The presentation of the language is based onthe articles [Mlnark, 2006a,b, 2007]. The part is structured as follows:

    In this chapter, we show an example of a LanQ program,

    The syntax is presented in Chapter 4,

    The typing system is introduced in Chapter 5,

    The concepts used in the language semantics definition are formalized in Chapter 6,

    The language operational semantics is defined in Chapter 7, The unique resource ownership property is proved in Chapter 8,

    Type soundness of LanQ is proved in Chapter 9,

    We provide a comparison of LanQ to other existing programming languages and processalgebras in Chapter 10.

    3.1 Informal introduction

    We begin our description of LanQ by an example of implementation of a well-known mul-tiparty quantum protocol a teleportation of an unknown quantum state [Bennett et al.,1993].

    First, we declare a method prep0 that prepares a new qubit initialized in the |0 stateand returns it as its result. This method is shown in Figure 3.1. It returns a qubit what isseen from the type qbit written in front of the method name. On the first line of the methodbody, a qubit variable q is declared. We then assign a newly allocated qubit initialized i


Recommended