0% found this document useful (0 votes)
79 views

Quantum Programming

Quantum Programming
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

Quantum Programming

Quantum Programming
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 130

Structured Quantum Programming

Bernhard Omer
rst version 26th May 2003 last revision 2nd September 2009

Institute for Theoretical Physics Vienna University of Technology

E-mail: Homepage:

[email protected] http://www.itp.tuwien.ac.at/~oemer/

Contents
Preface 1 Quantum Computing 1.1 The Way to Quantum Computing . . . . . . . . . . . . 1.1.1 From Huygens to Planck . . . . . . . . . . . . . 1.1.2 The Century of Quantum Physics . . . . . . . . 1.1.3 Beyond the Church-Turing Thesis . . . . . . . . 1.2 Quantum Mechanics . . . . . . . . . . . . . . . . . . . 1.2.1 Quantum Computation as Quantum Mechanical 1.2.2 Linear Algebra . . . . . . . . . . . . . . . . . . 1.2.3 The Postulates of Quantum Mechanics . . . . . 1.3 Classical Computing . . . . . . . . . . . . . . . . . . . 1.3.1 The Church-Turing Thesis . . . . . . . . . . . . 1.3.2 Machines . . . . . . . . . . . . . . . . . . . . . 1.3.3 Programs . . . . . . . . . . . . . . . . . . . . . 1.4 Elements of Quantum Computing . . . . . . . . . . . . 1.4.1 Quantum Memory . . . . . . . . . . . . . . . . 1.4.2 Quantum Operations . . . . . . . . . . . . . . . 1.4.3 Input and Output . . . . . . . . . . . . . . . . . 1.5 Concepts of Quantum Computation . . . . . . . . . . . 1.5.1 Models and Formalisms . . . . . . . . . . . . . 1.5.2 Quantum Algorithms . . . . . . . . . . . . . . . 2 Structured Quantum Programming 2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . 2.1.1 Motivation . . . . . . . . . . . . . . . . . . . . 2.1.2 Quantum Programming Languages . . . . . . 2.1.3 Classication of Programming Languages . . . 2.1.4 Goals . . . . . . . . . . . . . . . . . . . . . . . 2.1.5 State-of-the-Art . . . . . . . . . . . . . . . . . 2.2 The Computational Model of Quantum Programming i . . . . . . . . . . . . . . . . . . . . . . . . . . . Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iii 1 1 1 2 3 4 4 5 9 13 13 14 17 20 20 24 31 34 34 38 45 45 45 45 46 49 49 50

2.3

2.4

2.5

2.6

2.2.1 Quantum Circuits . . . . . 2.2.2 Finite Quantum Programs 2.2.3 Hybrid Architecture . . . Structured Programming . . . . . 2.3.1 Program Structure . . . . 2.3.2 Expressions and Variables 2.3.3 Subroutines . . . . . . . . 2.3.4 Statements . . . . . . . . Elementary Quantum Operations 2.4.1 Quantum Registers . . . . 2.4.2 Elementary Gates . . . . . 2.4.3 Measurements . . . . . . . Operators . . . . . . . . . . . . . 2.5.1 Quantum Subroutines . . 2.5.2 General Operators . . . . 2.5.3 Basis Permutations . . . . Quantum Flow Control . . . . . . 2.6.1 Conditional Operators . . 2.6.2 Conditional Branching . . 2.6.3 Quantum Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . .

51 51 56 59 59 60 61 62 64 65 69 73 74 74 78 85 92 92 97 102

A QCL Quick Reference A.1 Syntax . . . . . . . . . . . . . A.1.1 Expressions . . . . . . A.1.2 Statements . . . . . . A.1.3 Denitions . . . . . . . A.2 Expressions . . . . . . . . . . A.2.1 Data Types . . . . . . A.2.2 Operators . . . . . . . A.2.3 Elementary Functions A.3 Statements . . . . . . . . . . . A.3.1 Simple Statements . . A.3.2 Flow Control . . . . . A.3.3 Interactive Commands A.4 Interpreter Options . . . . . . References

108 . 108 . 109 . 110 . 110 . 111 . 111 . 113 . 114 . 114 . 114 . 115 . 116 . 118 119

ii

Preface
In contrast to quantum circuits, quantum Turing machines or the algebraic denition of unitary transformations, programming languages allow the complete and constructive description of quantum algorithms including their classical control structure for arbitrary input sizes and hardware architectures. This thesis investigates how the classical formalism of structured programming can be adapted to the eld of quantum computing, based on the machine model of a universal computer with a quantum oracle allowing the application of unitary gates and the measurement of single qubits. Starting with the abstract notion of programs as nite automatons (nite programs) and in analogy to classical programming languages, the concept of structured quantum programming languages (QPLs) is developed and illustrated by the experimental language QCL. A QPL is called imperative if it provides quantum registers (quantum variables), elementary gates and single qubit measurements. A procedural QPL additionally supports unitary subroutines and non-classical concepts like the reverse execution of code to derive the adjoint operator or the unitary uncomputing of temporary quantum registers (scratch space management). A procedural QPL is called structured it also allows the use of qubits and boolean expression of qubits (quantum conditions) in structured ow-control statements (quantum if-statement). A QCL interpreter for the Linux operating system as well as a numerical simulator for arbitrary quantum oracles are available as free software from http://www.itp.tuwien.ac.at/~oemer/qcl.html

Overview
Chapter 1 gives a general introduction to quantum computing and describes the key concepts and formalism necessary for the discussion of QPLs. After a short historical overview (1.1), the formalism and the postulates of quantum mechanics are presented in section 1.2. Section 1.3 introduces the key concepts of classical computation and develops a formal notion of iii

machines and programs which diers from the usual formalizations by treating machines and programs as separate entities. In section 1.4 the abstract machine concept is applied to quantum computing and the main components of a quantum computer, namely qubit-registers, unitary gates and qubit measurements, are discussed using a new formalism called register notation which allows the compact and abstract description of quantum circuits. Finally, section 1.5 discusses the formal description and the design of quantum algorithms. Chapter 2 presents the concept of structured quantum programming languages as a new formalism for quantum computing. After a general introduction to classical and quantum programming languages (2.1), section 2.2 discusses universal quantum computers and introduces the hybrid quantum architecture as the computational model of quantum programming. After an introduction to the key elements of classical structured programming languages (2.3), the remainder of chapter 2 demonstrates how these concepts can be adapted to quantum computing: Section 2.4 discusses the minimal requirements for a universal QPL (imperative quantum programming), section 2.5 introduces unitary subroutine (procedural quantum programming) and, nally, section 2.6 demonstrates how conditional operators can be used to realize the semantics of conditional branching on qubits and quantum conditions (structured quantum programming).

iv

Chapter 1 Quantum Computing


1.1
1.1.1

The Way to Quantum Computing


From Huygens to Planck

Before the adoption of quantum theory, one of the main problems of what now is referred to as classical physics was the dual nature of light. While its linear propagation and the lack of a physical medium seemed to suggest a particle-like behavior, phenomena like interference and diraction are well known properties of waves. In 1690, Christiaan Huygens explained optical birefringence in his Trait e de la lumi` ere where he developed a comprehensive wave-theory of light. [35] 14 years later, Isaac Newton published his Opticks in which he explained phenomena like reection, dispersion, color and polarization by interpreting light as a stream of dierently sized particles. The corpuscular-theory of light dominated the scientic discussion until the beginning of the 19th century when Young and Fresnel demonstrated several shortcomings of the theory which can be resolved assuming transversal light-waves in a universal medium called Ether. In 1873 in his Treatise on Electricity and Magnetism, Maxwell published a set of 4 partial dierential equations which lay the foundation to classical electrodynamics and elegantly explains light as electromagnetic waves. Maxwells theory however was still unable to explain the radiation of black bodies as well the the discrete energy spectra of atoms. Both shortcomings would prove crucial in the development of quantum theory.

CHAPTER 1. QUANTUM COMPUTING

1.1.2

The Century of Quantum Physics

Classical electodynamics predict that the energy distribution in a cavity and therefore the spectrum of a black body is proportional to the number of vibrational modes, which leads to an energy density of U (T ) = 8kT 4 , known as Rayleigh-Jeans Law, which is not integrable. [14] In the year 1900, Max Planck found a way to avoid this contradiction, which is also known as ultraviolet catastrophe, by the ad-hoc assumption, that the possible energy states are restricted to E = nh , where n is an integer, the frequency and h the Planck constant, the fundamental constant of quantum physics, with a value of h = 2 h = 6.626075 1034 Js This restriction causes the probability of frequencies crease exponentially and leads to the integrable distribution U (T ) = 8 2 h 3 h/kT c e 1 kT /h to de-

which is also in exact accordance with the experimental data. Five years later, Albert Einstein explained the photo-electric eect by postulating the existence of light particles, later called photons, with the energy E = h . In 1913, Niels Bohr calculated the value of the Rydberg constant, by assuming that the angular momentum of electrons orbiting the nucleus satises the quantization condition L = nh = nh/2 . This restriction could be justied by attributing wave properties to the electron and demanding that their corresponding wave functions form a standing wave; however this kind of hybrid theory remained unsatisfactory. A complete solution for the problem came in 1923 from Werner Heisenberg who used a matrix-based formalism; two years later Erwin Schr odinger published an equivalent solution using complex wave functions. In 1927, Heisenberg formulated the uncertainty principle, which formalized the complementarity of the wave and the particle picture, claimed by Bohr which, while being mutually exclusive, are both essential for a complete description of quantum events. Together with a statistical interpretation of Schr odingers wave function, they lay the theoretical foundation for the Copenhagen interpretation of quantum mechanics. [17]

CHAPTER 1. QUANTUM COMPUTING We regard quantum mechanics as a complete theory for which the fundamental physical and mathematical hypotheses are no longer susceptible of modication.
Werner Heisenberg and Max Born, Solvay Congress of 1927

While its explanation of quantum phenomena like entanglement or measurement still seems somewhat unsatisfactory, even after 75 years, the Copenhagen interpretation can still be regarded as the mainstream in quantum physics. Apparent contradictions like the famous EPR paradox [29] have not only been veried by experiment, but also serve as fundamental principles for new elds of research like quantum cryptography and quantum computing.

1.1.3

Beyond the Church-Turing Thesis

The basic idea of modern computing science is the view of computation as a mechanical, rather than a purely mental process. In 1936, Alan Turing formalized this concept by constructing an abstract device, now called Turing-Machine, which he proved to be capable of performing any eective (i.e. mechanical, algorithmic) computation. At about the same time, Alonzo Church showed that any function of positive integers is eectively calculable only if recursive. Both ndings are, in fact, equivalent and are commonly referred to as the Church-Turing Thesis. In its strong form, it can be summarized as Any algorithmic process can be simulated eciently using a Turing machine This means that, no matter what type of machine is actually used for a certain computation, an equivalent Turing Machine can be found which solves the same problem with only polynomial overhead. The strong Church-Turing Thesis came under attack when in 1977 Robert Solovay and Volker Strassen published a fast Monte-Carlo test for primality [55, 43], a problem for which no ecient deterministic algorithm was known at that time.1 While this challenge could easily be resolved by using a probabilistic Turing Machine, it raises the question whether even more powerful models of computation exists. In 1985, David Deutsch adopted a more general approach and tried to develop an abstract machine, the Universal Quantum Computer, which is not targeted at some formal notion of computability, but should be capable
In 2002, Manindra Agrawal, Neeraj Kayal and Nitin Saxena eventually found a deterministic primality test [40] with a worst case time complexity of O(n12 ).
1

CHAPTER 1. QUANTUM COMPUTING

of eectively simulating an arbitrary physical system and consequently any realizable computational device [24, 56]. Deutsch also described a simple quantum algorithm which would be capable of determining in a single step whether a given one-bit oracle function f : B B is either constant or balanced. The algorithm was later generalized for n-bit functions f : Bn B (Deutsch-Jozsa problem [26]) and demonstrates that a quantum computer is indeed more powerful than a probabilistic Turing machine. At the same time, Richard Feynman showed how local Hamiltonians can be constructed to perform arbitrary classical computations [31]. In 1994, Peter Shor demonstrated how prime factorization and the calculation of the discrete logarithm could be eciently performed on a quantum computer [54]. The immense practical importance of these problems for cryptography made Shors algorithm the killer-application of quantum computing. One year later, Lov Grover designed a quantum algorithm for nding a unique solution to Q(x) = 1 in an unstructured search space of size n, requiring only O( n) evaluations of the black-box oracle function Q [32]. At this time, Peter Zoller and Ignacio Cirac demonstrated how a linear ion trap can be used to store qubits and perform quantum computations [19]. In 2001, a team at IBM succeeded to implement Shors algorithm on an NMR based 7-qubit quantum computer to factorize the number 15 [18].

1.2
1.2.1

Quantum Mechanics
Quantum Computation as Quantum Mechanical Theory

Strictly spoken, the algebraic formulation of quantum mechanics, which shall be introduced in this section, is not a physical theory in its own right, but rather provides a framework to formulate physical theories within. Depending on how exactly the Hilbert spaces and Hamiltonians are constructed, dierent theories emerge, from non-relativistic quantum electrodynamics, which still maintains many formal analogies to classical physics, to quantum chromodynamics which introduces entities like quarks and gluons which are completely meaningless outside the scope of quantum mechanics. Quantum computing is yet another theory on top of the abstract quantum mechanical formalism. It is, however, not a physical theory in the sense that it tries to accurately describe natural processes, but is built on abstract concepts like qubits and quantum gates, without regard to the underlying physical quantum-dynamical model.

CHAPTER 1. QUANTUM COMPUTING

This top-down approach is at the same time the greatest strength and the greatest weakness of quantum computation. While it guarantees that its computational model is in fact the most general which is physically realizable in a quantum mechanical universe, the lack of a concrete and scalable reference implementation, like the Turing machine is for classical computing, leaves open the question whether quantum computers with more than a handful of qubits are in fact possible, under realistic assumptions for noise and experimental accuracy.

1.2.2
1.2.2.1

Linear Algebra
Braket Notation

The braket notation is a very compact formalism for linear algebra and was introduced by Dirac. Table 1.1 lists the most commonly used expressions. Notation | | |n |n | | | | | |i, j M |M | Description general ket vector, e.g. | = (c0 , c1 , . . .)T dual bra vector to | , e.g. | = (c 0 , c 1 , . . .) th n basis vector of some standard basis N = {|0 , |1 , . . .} = {| basis vector of an alternate basis N 0 , | 1 , . . .} inner product of | and | tensor product of | and | abbreviated tensor product | | abbreviated tensor product of the basis vectors |i and |j adjoint operator (matrix) M = (M T ) inner product of | and M | abbreviated norm | Table 1.1: Dirac Notation

1.2.2.2

Hilbert Space

Denition 1 A set V is called vector space over a scalar eld F i the operations + : V V V (vector addition) and : F V V (scalar multiplication) are dened, and (i) (V, +) is a commutative group, (ii) (iii) | = | , (| ) = ()| ,

CHAPTER 1. QUANTUM COMPUTING (iv) (v) ( + )| = | + | , (| + | ) = | + | . From now on, we will only consider complex vector spaces, i.e. F = C.

Denition 2 Let V be a complex vector space. A function | : V V C is called inner product i (i) |(| + | ) = | + | , (ii) (iii) | = | , | R, | 0, | = 0 | = o. = | (also written as (1.1) (1.2)

An inner product also denes the norm | ). The following inequalities apply: | | | | + | +

(Schwarz inequality) (triangle inequality)

Denition 3 (Completeness) Let V be a vector space with the norm and |n V a sequence of vectors. (i) |n is a Cauchy sequence i > 0 N > 0 such that n, m > N, |n |m (ii) <

(1.3)

|n is convergent i there is a | V such that > 0 N > 0 n > N, |n | < (1.4)

V is complete i every Cauchy sequence converges. Denition 4 A complete vector space H with an inner product | and the corresponding norm = | is called Hilbert space. A Hilbert space H is separable if there exists an enumerable set S H which is dense in H, i.e. for any | H and > 0 there exists a | S with | | < . From now on, we will only consider separable Hilbert spaces. A vector | H is normalized or a unit-vector i = 1. An enumerable set of normalized vectors B = {|0 , |0 , . . .} is called orthonormal

CHAPTER 1. QUANTUM COMPUTING

system i i |j = ij . An orthonormal system B is an (orthonormal) basis of H i any vector | H can be written as | =


i

i | i

with |i B

Since Hilbert spaces are complete by denition, any separable, complex Hilbert space H with some basis B is algebraically isomorphic and isometric to either (i) Cn with the basis {|0 , |1 , . . . |n 1 } with |k = (0k , 1k , . . . n1,k )T if dim H = |B | = n or (ii) l2 (i.e. the space of complex sequences n for which k |k |2 is dened) with the basis vectors |k = (0k , 1k , . . .)T if dim H = |B | = 0

In quantum computing we usually deal with nite dimensional Hilbert spaces, so unless otherwise noted we will always assume H = Cn . In Cn , a ket vector | can be written as column vector and the dual bra vector | can be written as row vector | = (| )T , which allows the inner product | to be formally expressed as ordinary matrix multiplication. Denition 5 Let H1 and H2 be Hilbert spaces with the bases B1 and B2 , then the tensor product H = H1 H2 =
|i B1 |j B2

cij |i, j

cij C

(1.5)

is also a Hilbert space with the basis B = B1 B2 and the inner product i, j |i , j = i|i . 1.2.2.3 Linear Operators j |j = ii jj with |i , |i B 1 and |i , |i B2

Denition 6 Let V be a vector space and A be function A : V V. A is called linear operator on V i A(| + | ) = A(| ) + A(| ) = A| + A| . (1.6)

In Cn , a linear operator A can be written as a n n matrix with the matrix elements aij = i|A|j a0,0 a0,n1 . . ... = . . A= . . an1,0 an1,n1

aij |i j |
i,j

(1.7)

CHAPTER 1. QUANTUM COMPUTING

Because of (1.6), a linear operator on a vector space V with the basis B is completely dened by its eect on the basis vectors, so the above operator A could also be written as A : |n
k

akn |k

with |k B
i,j

(1.8)

Denition 7 The operator A = (AT ) = operator of A. Denition 8 A linear operator A is called (i) normal i A A = AA , (ii) (iii) (iv) (v) (vi) self-adjoint or Hermitian i A = A, positive i |A| R+ 0 | H ,

aji |i j | is called adjoint

unitary i A A = I , with I being the identity operator, idempotent i A2 = A, self-inverse i A2 = I ,

(vii) an (orthogonal) projection i A is self-adjoint and idempotent. SU (n) is the group of unitary operators on Cn with determinant 1. Since for each unitary U on Cn there exists a physically equivalent U = ei U SU (n) (see 1.2.3.1), we will also use SU (n) to denote any set of operators physically equivalent to SU (n). Denition 9 An a C with at least one non-zero solution |a of the equation A|a = a|a is called eigenvalue of A, with |a being an eigenvector for a. The set {| H|A| = a| } is known as the eigenspace of A for the eigenvalue a. Any linear operator A can be written in terms of its eigenvectors as A=
i

ai | | with

| = ij .

(1.9)

This form is called spectral decomposition of A. Denition 10 Let A be a linear operator on H1 and B a linear operator on H2 , then the tensor product AB =
i,j i ,j

|i, j i|A|i

j |B |j

i ,j |

(1.10)

is a linear operator on H1 H2 . Denition 11 Let A and B be linear operators on H. The operator [A, B ] = AB BA is called commutator and {A, B } = AB + BA is called anticommutator of A and B .

CHAPTER 1. QUANTUM COMPUTING

1.2.3
1.2.3.1

The Postulates of Quantum Mechanics


Quantum States

Postulate 1 Associated to any physical system S is a complex Hilbert space H known as the state space of S . The state of S is completely described by a unit vector | H with = 1, which is called the state vector of S . Two state vectors | and | are equivalent (| | ) i | = ei | with real . How exactly the state space of for a given physical system is constructed, is beyond the scope of this postulate. Qubits The simplest non-trivial quantum mechanical system is the quantum bit or qubit with a state space B = C2 . The state | of a qubit can be described by a linear combination (also called superposition) of just two basis states labeled |0 and |1 | = |0 + |1 1.2.3.2 Evolution with , C and ||2 + | |2 = 1 (1.11)

Postulate 2 The temporal evolution of the state of a closed quantum system is described by the Schr odinger equation i h | = H | t (1.12)

with the (experimental) Planck constant h 1.05457 1034 Js and a xed self-adjoint operator H on the state space H known as the Hamiltonian of the system. In quantum physics, it is common to use a system of measurement where = H | . h = 1, so that (1.12) can be written in the dimensionless form i| The Hamiltonian H completely describes the dynamics of a closed quantum system. As with the state space, the concrete form of H (or an approximation thereof) must be determined by the physical theory used to describe the system. Unitary Evolution If we know the system to be in some initial state |0 at the time t = 0, we can dene an operator U (t) such that HU (t) |0 = i U (t) |0 t and U (0)| = | (1.13)

CHAPTER 1. QUANTUM COMPUTING and get the operator equation H U (t) = i with the solution U (t) = eiHt =

10

U (t) t

(1.14)

1 (i)n tn H n n ! n=0

(1.15)

U (t) is the operator of temporal evolution and satises the criterion U (t) | (t0 ) = | (t0 + t) U (t) is unitary because H = H and therefore U (t)U (t) = eiHt e+iHt = I, (1.17) (1.16)

In fact, U (t) and the Hamiltonian are equivalent descriptions of a systems dynamics. Since any unitary operator U can be expressed as the exponential of a self-adjoint operator H such that U = eiH , we can reformulate the 2nd postulate in a non-continuous, discrete-time version: The temporal evolution of a closed quantum system from the state | at time t1 to state | at time t2 can be described by a unitary operator U = U (t2 t1 ) such that | = U | . In either formulation, the postulate only applies to closed systems, so H or U (t) are xed operators. It is however often possible to interact with a quantum system in such a manner that it can still be treated as isolated, while the eect of the interaction can mathematically be described by a timevarying Hamiltonian. Even in that case, the discrete evolution of the system between two points in time t1 and t2 can still be described by a unitary operator U = U (t1 , t2 ). In this context, we would speak of applying the operator U to a quantum state | . 1.2.3.3 Measurements

Postulate 3 A (projective) measurement2 is described by a self-adjoint operator M , called observable, with the spectral decomposition M = m mPm , where Pm is the projector onto the eigenspace of the eigenvalue m. The eigenvalues m of M correspond to the possible outcomes of the measurement. Measuring | will give the result m with probability p(m) =
2 There is also a more general formulation of quantum measurement allowing nonprojective measurement operators. See [43] for details.

CHAPTER 1. QUANTUM COMPUTING |Pm | , thereby reducing | to the post-measurement state | = 1 p(m) Pm |

11

(1.18)

For a qubit state, the self-adjoint operator N N= 0 0 0 1 = 0 |0 0| + 1 |1 1| (1.19)

is known as the standard observable. Generally, for a system with the state space H = Cn , the standard observable N can be dened as N = i i|i i|. Denition 12 The weighted average M over all possible outcomes of a measurement of M is called expectation value and is dened as M =
m

p(m)m =
m

|mPm | = |M |

(1.20)

Denition 13 The standard deviation M of all possible outcomes of a measurement is dened as M = (M M )2 = M2 M


2

(1.21)

The Heisenberg Uncertainty Principle The destructive nature of measurement raises the question whether 2 observables A and B can be measured simultaneously. This can only be the case if the post-measurement state | is an eigenvector of A and B A| = a| and B | = b| (1.22)

This is equivalent to the condition [A, B ] = 0. If A and B do not commute, then the uncertainty product (A)(B ) > 0. To nd a lower limit for (A)(B ) we introduce the operators A = A A and B = B B and can express the squared uncertainty product as (A)2 (B )2 = (A)2 (B )2 = |(A)(A)| |(B )(B )| Since A and B are self adjoint, we can express the above as (A)2 (B )2 = A| Using (1.1) and [A, B ] = [A, B ] we get (A)(B ) 1 [A, B ] 2 (1.25)
2

(1.23)

B |

(1.24)

CHAPTER 1. QUANTUM COMPUTING 1.2.3.4 Composite Systems

12

Postulate 4 The state space H of a composite physical system is the tensor product of the state spaces Hi of its components. Moreover, if the subsystems are in the states |i Hi , then the joint state | H of the composite system is | = |1 |2 . . . |n . Let S be a composite system of S1 and S2 with the state space H = H1 H2 . A measurement of the observable M : H1 H1 in S1 , is equivalent to measuring the observable M (1) = M I in S with I being the identity operator on H2 . Equivalently, a unitary transformation U : H1 H1 of S1 is described by the padded operator U (1) = U I on H. A joint state of the form | = |1 |2 is called product state, which can be expanded to | =
i j

ai bj |i, j

with
i

|ai |2 = 1 and
j

|bj |2 = 1

(1.26)

In product states, unitary transformations or measurements performed on one system, do not aect the state of the other system. Entanglement Not any joint state is a product state. A state | =
i j

cij |i, j

with
i,j

|cij |2 = 1

(1.27)

where the coecients cij cannot be written as cij = ai bj is called entangled. Consider the following joint states of two qubits 1 1 1 1 (1.28) |A = |0, 0 + |1, 0 + |0, 1 + |1, 1 and 2 2 2 2 1 1 |B = |0, 0 + |1, 1 (1.29) 2 2 A single measurement of either qubit (using the standard observable as dened in (1.19)) will give 0 or 1 with equal probability p = 1/2. Assuming that a measurement of the rst qubit gave the result m, the respective post measurement states are 1 1 (1.30) |A = |m, 0 + |m, 1 and 2 2 |B = |m, m (1.31) A measurement of the second qubit of |A will still give a random result, while in the case of |B , the outcome is correlated to the previous measurement and will always produce m. |B is also known as Bell state.

CHAPTER 1. QUANTUM COMPUTING

13

1.3
1.3.1

Classical Computing
The Church-Turing Thesis

As already mentioned in 1.1.3, computing science is based on the paradigm of computation being a mechanical, rather than a purely mental process. A method, or procedure P for achieving some desired result is called eective or mechanical if [21] 1. P is set out in terms of a nite number of exact instructions (each instruction being expressed by means of a nite number of symbols); 2. P will, if carried out without error, always produce the desired result in a nite number of steps; 3. P can (in practice or in principle) be carried out by a human being unaided by any machinery save paper and pencil; 4. P demands no insight or ingenuity on the part of the human being carrying it out. Alan Turing and Alonzo Church both formalized the above denition by introducing the concept of computability by Turing machine and the mathematically equivalent concept of recursive functions with the following conclusions: Turings Thesis LCMs [logical computing machines i.e. Turing machines] can do anything that could be described as rule of thumb or purely mechanical. [58] Churchs Thesis A function of positive integers is eectively calculable only if recursive. [50] As the above statements are equivalent, they are commonly referred to as the Church-Turing Thesis which denes the scope of classical computing. 1.3.1.1 Partial Recursive Functions

The class P of partial recursive functions mathematically captures the concept of eective functions f : Nn Nm . P can be constructed from simpler classes in the following way: [39]

CHAPTER 1. QUANTUM COMPUTING

14

1. A basic function f : Nn Nm is a function f : x y where yi is either a constant yi = ci , ci N or an element of the input vector yi = x(i) . The class BF of basic function is closed under the basic operators BO = {, }, where denotes function composition and the usual cross-product. 2. The class PR of primitive recursive functions is generated from BF{S } by closure under BO {Pr} where (i) (ii) S : N N is the successor function S (n) = n + 1 and Pr denotes the primitive recursion h = Pr[f, g ] h(x, 0) = f (x), h(x, n + 1) = g (x, n, h(x, n)) (1.32)

3. P is generated from PR by closure under BO {0 }. The operator 0 is called 0 -recursion (minimization) and dened as 0 [f ] : x min [f (x, k ) = 0]
k N

with f PR

(1.33)

As 0 [f ](x) is only dened if k N, f (x, k ) = 0, P is a class of partial functions. The class R P of total functions in P is called recursive functions.

1.3.2
1.3.2.1

Machines
General Machines

Denition 14 A machine M is a 5-tuple (S, O, T, , ) where [39] (i) S is a set of of computational states (ii) (iii) (iv) (v) O = {fi : S S} is an enumerable set of operations on S (memory commands) T = {ti : S B} is an enumerable set of predicates on S (test commands) : I S is an input function for the enumerable input set I : S O is an output function for the enumerable output set O

By providing a set of (simple) elementary operations and predicates, a machine denes a framework for the description of eective procedures. The enumerability of O and T guarantees that such a description, called program, can be nite and represented as a string over a nite set of symbols.

CHAPTER 1. QUANTUM COMPUTING 1.3.2.2 Discrete Machines

15

A more rigid interpretation of eectivity also requires S to be enumerable, so that not only the program but also the computational state can be expressed by nite means and the whole computation can in fact be carried out by manipulating symbols on paper. Such machines are known as discrete. For any machine M = (S, O, T, , ) with O = {f1 , f2 , . . .} and the input set I = {x1 , x2 , . . .} an equivalent discrete machine M = (S , O, T, , ) can be constructed by the diagonalization

S =
n=1

n {(g1 g2 . . . gn )( (x)) | g On , x In }

(1.34)

where O0 = {I }, On+1 = On {fn } and In = {x0 , . . . xn }. Turing Machines A Turing Machine (TM) consists of a head operating on an innite tape of memory cells. In the simplest case, each cell can only adopt one of two possible states labeled 0 (also called blank) and 1. If we index the cells by their relative position to the head, we can describe the content of the tape content as a function s : Z B and write s = (. . . s2 s1 |s0 s1 s2 . . .). (1.35)

The state space T is the set of all tapes containing only a nite number of 1s.3 T= s:ZB
i=

si <

(1.36)

We can now dene a TM as a machine M = (T, {S, E, L, R}, {T}, , ) with the commands (i) S(. . . s1 |s0 s1 s2 . . .) = (. . . s1 |1s1 s2 . . .) (set) (ii) (iii) (iv) (v) E(. . . s1 |s0 s1 s2 . . .) = (. . . s1 |0s1 s2 . . .) (erase) L(s) = s where si = si+1 (move left) R(s) = s where si = si1 (move right)

T(s) = s0 (test) For I = Nm and O = Nn we can dene a Turing machine TMm n = (T, {S, E, L, R}, {T}, m , n ) with the unary encoding m (x1 , x2 , . . . xn ) = (0 |1x1 01x2 0 . . . 01xm 0 ) and n (. . . |1y1 01y2 0 . . . 01yn 0 . . .) = (y1 , y2 , . . . yn )
3

(1.37) (1.38)

This zero tails state condition is necessary as the set T = {s : Z B} would not be enumerable.

CHAPTER 1. QUANTUM COMPUTING 1.3.2.3 Finite Machines

16

For a discrete machine M with an innite S, the number of symbols to represent a computational state s S can get arbitrarily large so any realization of M would require unlimited memory. If the amount of memory is limited, so is the number of computational states. A discrete machine M with limited memory is called nite. The memory capacity of a nite machine M with the nite state space S is S = log2 |S| bit. 1.3.2.4 Oracles

If a machine M1 = (S1 , O1 , T1 , 1 , 2 ) is extended to allow computations on another machine M2 = (S2 , O2 , T2 , 2 , 2 ), then the resulting machine M = M1 M2 is referred to as an M1 -machine with an M2 -oracle. The interaction between M1 and M2 is described by oracle commands of the form fO : S1 S2 S1 S2 and M can be written as M = (S1 S2 , O1 {I1 } {fO }, T1 {tO }, , ) (1.40) or tO : S1 S2 B (1.39)

with I2 being the identity on S2 . Depending on the denition of fO and tO , oracle calls can correspond to single M2 -commands up to the execution of complete nite programs (see 1.3.3.2) on M2 . Still, with regard to time complexity, an oracle-call counts as a single computational step. 1.3.2.5 Probabilistic Machines

A machine M = (S, O, T, , ) is probabilistic if it provides at least one random test command c T . A random predicate c : S B can be mathematically described by the probability distribution p(c|s) where s S, so true with p = p(c|s) c:s (1.41) false with p = 1 p(c|s) We can generalize this denition by also allowing for random memory commands f : S S f :ss with p = pf (s |s) where s S,
s S

pf (s |s) = 1

(1.42)

CHAPTER 1. QUANTUM COMPUTING

17

and output functions : S O. In the latter case, p (y |s) with y O is also called (probability) spectrum of s. For any probabilistic machine M it is possible to formally construct a operating on the distribution space corresponding deterministic machine M = ps S : S [0, 1]
sS

ps (s) = 1

(1.43)

has the (Shannon) entropy A state s S H ( s) =


sS

p(s|s ) log2 p(s|s ),

p(s|s ) = ps (s).

(1.44)

Probabilistic Turing Machine A probabilistic Turing Machine (PTM) can be constructed from a deterministic TM by adding a stateless random oracle which provides a fair coin toss test command C with p(C) = p(C) = 1/2.

1.3.3

Programs

A program for some machine M = (S, O, T, , ) is a nite set of instructions (also called statements) that determines how to iteratively transform an input state s0 = (x) using the machines memory and test commands until some halting condition is met. If the program halts, the resulting state sh is called output state.
input
memory commands

test commands

output

Figure 1.1: A program controlling a machine M = (S, O, T, , ) The transfer function : s0 sh is a partial function on S dened for all input states s0 S for which halts. F (, M) denotes the partial function x ( ( (x))) implemented by on M = (S, O, T, , ).

CHAPTER 1. QUANTUM COMPUTING

18

The interpretation of a program of a program class is specied by a step function : P S P S. P = P( ) is the set of possible control-states with a unique p0 P called initial state and a subset Ph P called halting states. A pair (p, s) P S is called a conguration. has the general form (p, s) = (, p, t(,p) (s)), s (p, s)

(, p), f(,p) (s)

if p Pf if p Pt if p Ph (1.45)

where P = Pf Pt Ph , f(,p) O and t(,p) T . For an input state s0 = (x), (p0 , s0 ) = (p0 , (x)) is called initial conguration. The transfer function is dened as (ph , (s0 )) = n (p0 , s0 ) with n = min k (p0 , s0 ) Ph S
k

(1.46)

If halts for a given input x I , n is called the (time) complexity of the computation. 1.3.3.1 Sequences

Denition 15 A program On for a machine M = (S, O, T, , ) consisting of a static list of n memory commands is called a sequence. The transfer function : s (1 2 . . . n )(s) is a total function. Denition 16 Let S be composed of identical indexed memory cells Mi with the state space S , such that S = S1 S2 . . . for some suitable composition and g a function g : S n S n . The class of functions (g ) = {gi1 i2 ...in : S S} where gi1 i2 ...in denotes the application of g to a permutation of n mutually dierent cells Mi1 , Mi2 . . . Min is called an n-ary gate. If O is a union of gates, then a sequence O can be interpreted as a feed forward network and is also called a circuit. A set of operations O is universal if for any function f : I O dened for a nite subset I I there exists a sequence O such that f (x) = ( ( (x))) for all x I . While generally, more powerful programming concepts are required to fully exploit the computational potential of a machine M = (S, O, T, , ), sequences are sucient to implement any function that can possibly be implemented if either (i) O is universal and (a) M is nite or (b) I is a nite set (ii) M provides no test-commands, i.e. T =

CHAPTER 1. QUANTUM COMPUTING 1.3.3.2 Finite Programs

19

Denition 17 Let M be a machine (S, O, T, , ) and L an enumerable set of labels with an element l0 L called start-label. (i) A triple (l, f, p) L O L is called function-statement (ii) A 4-tuple (l, t, p, q ) L T L L is called test-statement The labels l are called statement-, p and q are called jump-labels. A nite program for M is a nite set of statements with unique labels l. Function- and test-statements are also written as l: l: f then p if t then p else q

A program describes a nite automaton operating on M with the step function


(p, f (s)) if l = (l, f, p) (p, s) if l = (l, t, p, q ) t(s) (l, s) = ( q, s ) if l = (l, t, p, q ) t(s)

(1.47)

(l, s)

if l / L

where L denotes the set of statement-labels in and l : L the statement with the label l. According to the Church-Turing thesis, any eective function f : Nn Nm can be implemented as a nite program for TMm n and the set of Turingcomputable functions
m F (TM) = {F (, T Mn ) | , n, m N}

(1.48)

is identical to P. Denition 18 (Universal Computer) A machine M = (S, O, T, , ) with the encodings : N S and : S N is a universal computer i for any f P, there exists a nite program for M such that f = F (, M). 1.3.3.3 Programming Languages

Denition 19 A programming language L for a machine M is a class of algorithm descriptions p over some alphabet , which can be eciently translated into a nite program for M by another program L for a machine ML = (S, O, T, : S, : S ) (1.49)

CHAPTER 1. QUANTUM COMPUTING

20

The pair (L , ML ) is called a compiler for L. It is usually required that the compilation is ecient, i.e. has polynomial time and space complexity. A programming language L is universal, if M is a universal computer and for any f P there exists a program p L such that f = F (p, M) = F (F (L , ML ), M).

1.4

Elements of Quantum Computing

Just like a classical machine, a quantum computer, essentially consists of three parts: a memory, which holds the current machine state, a processor, which performs elementary operations on the machine state, and some sort of input/output which allows to set the initial state and extract the nal state of the computation. Formally, we can describe a quantum computer as a probabilistic machine M = (H, O, T, , ) where H is the state space of the quantum system operated on, O a set of (deterministic) unitary transformations, T a set of (probabilistic) measurement commands, is the initialization operator and describes the nal measurement.

1.4.1
1.4.1.1

Quantum Memory
Qubits

The quantum analogue to the classical bit is the quantum bit or qubit (see 1.2.3.1). Denition 20 A qubit or quantum bit is a quantum system whose state can be fully described by a superposition of two orthonormal basis states labeled |0 and |1 . The state space of a qubit is the Hilbert space B = C2 . The orthonormal system {|0 , |1 } is called computational basis. The classical value of a qubit is described by the standard observable N = |1 1| (1.19). N gives the probability to nd the system in state |1 if a measurement is performed on the qubit.

CHAPTER 1. QUANTUM COMPUTING

21

The Bloch Sphere Ignoring an irrelevant overall phase factor, the general state of a qubit can be written as | = cos |0 + ei sin |1 2 2 By interpreting and as polar coordinates r = (cos sin , sin sin , cos ), (1.51) (1.50)

every qubit state has a unique representation as a point on the three-dimensional unit sphere, also known as Bloch sphere.

|0
z

|
x y

|1
Figure 1.2: Bloch sphere representation of the qubit state | The unit-vector r =r is called Bloch vector of | . Bloch vectors have the property that r = r | = 0. (1.52) 1.4.1.2 Machine State

Denition 21 The state space of a quantum computer is a separable complex Hilbert space H with a designated enumerable orthonormal system B = {|i } called computational basis. The state of a quantum computer is a unit-vector | H known as machine state.

CHAPTER 1. QUANTUM COMPUTING

22

Classically, the common state space S of a composite system consisting of n memory cells with the state spaces Si is given by the cross-product S = S1 S2 . . . Sn . In quantum mechanics, this is only true for product states (see 1.2.3.4). The state space H of a quantum computer composed of n identical subsystems with the state space S is given as the tensor product H=S
n

n times = S ... S

(1.53)

The machine state | of an n-qubit quantum computer is therefore a n unit vector in H = B n = C2 | =


(d0 ...dn1 )Bn

cd0 ...dn1 |d0 . . . dn1

with

|cd0 ...dn1 |2 = 1

(1.54)

The basis vectors |d0 . . . dn1 can be interpreted as binary numbers and 1 i relabeled as |k with k = n i=0 2 di so B can be written as B = {|k |k Z2n }. A quantum computer M = (H, O, T, , ) is nite if dim H < . The memory capacity of a nite quantum computer is log2 dim H qubit. Unlimited Memory As the state space of a quantum computer is a separable Hilbert space, it is isomorphic to either Cn or l2 (see 1.2.2.2). The Hilbert space B resulting from a composition of an innite number of qubits is non-separable. We can however construct a separable subspace B B which is isomorphic to l2 by introducing a zero tail state condition. B = | | 0 1.4.1.3 Quantum Registers

| B n , n N

(1.55)

Denition 22 Let H be the state space of a quantum computer M with the computational basis B = {|i }. A quantum register s is a sub-system of M with a nite dimensional state space Hs and a basis Bs = {|i s } such that H = Hs H s is known as the s and B = Bs B s . If M is nite, then complimentary register to s. A register s denes a decomposition for the computational basis B , so any basis vector |k B can be written as the product state |k = |ik s |jk s with |ik s Bs and |jk B . s s In H the classical value of a register s is described by the register observable N (s) N (s) Ns I i |i s |j (1.56) s = s i|s j | s
i,j

CHAPTER 1. QUANTUM COMPUTING

23

where Ns is the standard observable on Hs and I s the identity operator on H s. Likewise, a unitary transformation U on Hs can be expressed as a register operator U (s) on H U (s) U I s =
i,i ,j

ui,i |i s |j

i |s j | s

(1.57)

where ui,i is the matrix element ui,i = i|U |i . Qubit Registers When H is the state space of a composition of the qubits qi , i.e. H = B m or H = B , then any qi denes a register qi with the decomposition |d0 . . . di1 di di+1 . . . = |di
qi |d0

. . . di1 di+1 . . .

i q

(1.58)

Likewise, any permutation (s0 s1 . . . sn1 ) = (qk0 qk1 . . . qkn1 ) of mutually dierent qubits sj {qi } denes an n-qubit register s = qk0 qk1 . . . qkn1 with the decomposition
n1

|d0 d1 . . . =
i=0

|dki

qki

|...

= |dk0 dk1 . . . dkn1 s | . . .

(1.59)

The order of qubits is important, so while a b and b a refer to the same 2-qubit subsystem, they are two dierent registers. If s is a n-qubit register and U an operator on B n , then the register operator U (s) is also referred to as a quantum gate. Register States Strictly speaking, the state of a register s is only dened if the machine state | is of the form | = | s | s . In that case, we say that s is in the pure state | Hs . Alternatively, the state of s can be described by the (reduced) density operator s . Denition 23 Let H = Ha Hb be the state space of a composite system a b. For a machine state | =
i,j

cij |i a |j

with
i,j

|cij |2 = 1

(1.60)

the reduced density operator a : Ha Ha is dened as a = trb (| |) =


i,i

|i

i |a
j

cij c ij

(1.61)

CHAPTER 1. QUANTUM COMPUTING

24

If a register s is in a pure state then s = | | and tr(2 s ) = trs = 1. If s is entangled then s is a positive operator with the spectral decomposition s =
k

pk |k k | with pk [0, 1) and


k

pk = 1

(1.62)

4 and tr(2 s ) < 1. In that case, s is also said to be in the mixed state s . The density operator allows to treat s as an isolated system with regard to unitary evolution and measurements as long as no operation on s is performed: (i) Let U be a unitary operator on Hs , then

| = U (s)| (ii)

= s = U s U .

(1.63)

Let M be a Hermitian operator on Hs with the spectral decomposition M = m mPm , then p(m) = |Pm (s)| = tr(Pm s ) and | = Pm | p(m) = s =
Pm s Pm . p(m)

(1.64)

Schmidt Decomposition If s and s are entangled, the machine state can always be written as | =
i

i |i s |i

with i R+

and
i

2 = 1

(1.65)

such that |i Hs and |i H s are orthonormal states i.e. i |j = ij and i |j = ij . This representation is known as Schmidt decomposition.

1.4.2
1.4.2.1

Quantum Operations
Unitary Operators

According to the 2nd postulate of quantum mechanics (see 1.2.3.2), the evolution of a closed quantum system is unitary and can be described by the operator U (t) = eiHt . Therefore, the memory commands O of a quantum computer M = (H, O, T, , ) are unitary transformations on the state space H. A unitary transformations U is a linear operator of the form U U = U . From a computational I and describes a basis transformation B B point of view, these mathematical properties account for three fundamental dierences between classical and quantum computing:
The term mixed state refers to the fact that exhibits the same measurement statistics as a system which is known to be in the state |k with probability pk .
4

CHAPTER 1. QUANTUM COMPUTING

25

Reversibility: Since unitary operators, by denition, match the condition U U = I , for every transformation U there exists the inverse transformation U (1) = U . As a consequence, quantum computation is restricted to reversible functions.5 Superposition: A classical state | = |k B can be transformed into a superposition of several basis vectors = | = U |k = |k
k

uk k |k

(1.66)

and vice versa. Parallelism: If the machine state | already is a superposition of several basis vectors, then a transformation U is applied to all basis states simultaneously. U
k

c k |k =
k

c k U |k

(1.67)

This feature of quantum computing is called quantum parallelism and is a consequence of the linearity of unitary transformations. 1.4.2.2 Qubit Operators

The simplest case of unitary transformations are operators which work on a single qubit. A general 2-dimensional complex unitary matrix U SU (2) can be written as U =e
i e 2 ( ) cos 2 e 2 (+ ) sin 2 i i e 2 (+ ) cos 2 e 2 ( ) sin 2
i i

(1.68)

As we have shown in 1.4.1.1, every qubit state | B can be represented by a Bloch vector r . Rotations about the x , y and z -axes in the Bloch sphere correspond to the operators Rx ( ) = Ry ( ) = Rz ( ) =
5

i sin 2 cos 2 i sin 2 cos 2 cos 2 sin 2 cos 2 sin 2

(1.69) (1.70) (1.71)

ei/2 0 0 ei/2

A classical analogue would be the class of bijective functions on Bn .

CHAPTER 1. QUANTUM COMPUTING

26

on B . It is easy to verify that Ri () = Ri (), so all Ri are unitary operators. Using the self-inverse Pauli matrices

x =

0 1 1 0

y =

0 i i 0

z =

1 0 0 1

(1.72)

a general rotation6 about a unit vector n can be written as i n Rn = cos I i sin (nx x + ny y + nz z ) ( ) = e 2 2 2 (1.73)

Let U SU (2) have the orthonormal eigenvectors |u and |v and the eigenvalues u and v , then U can be written as U = u|u u| + v |v v | = ei Rn ( ) (1.74)

where n is the Bloch vector of |u and is the phase dierence between u and v , i.e. v = ei u. 1.4.2.3 Universal Qubit Operations

As qubit operators correspond to rotations in the Bloch sphere, any unitary U on B can implemented as a composition of three rotations about two orthogonal axes and a (physically irrelevant) overall phase factor.7 e.g. U = ei Rz ()Ry ( )Rz ( ) U = ei Rx ()Ry ( )Rx ( ) (Z-Y decomposition) (X-Y decomposition) or (1.75) (1.76)

This means, for symmetry reasons, that for any two orthogonal unit vectors u and v , the operator set Ouv = {Ru ( ) | R} {Rv ( ) | R} is universal for single qubit operations. Denition 24 (Universality of operator sets) Let H be a separable Hilbert space and O a set of unitary operators on H. O is universal on H if for any unitary operator U : H H and for any R+ , there exists a composition = U1 U2 . . . Uk with Ui O and an overall phase ei such that |(U ei )| < | , | H . (1.77)

6 Note that despite the mathematical period of 4 , Rn ( ) and Rn ( + 2 ) = Rn ( ) describe the same physical operation. 7 Note that expanding (1.75) directly leads to (1.68).

CHAPTER 1. QUANTUM COMPUTING

27

Let O = {Rn ( ) | R} be the class of rotations about some axis vector n . Since
k Rn ( + ) = Rn ()Rn ( ) and Rn ( + 2k ) = (1) Rn ( ),

(1.78)

k the set {Rn ( ) | k N} O is dense in O i = 0 and / is irrational. So any pair {Ru v and (q ), Rv (p )} of orthogonal qubit rotations where u p, q I+ already constitutes a universal set of qubit operators. The above result can be generalized to non-orthogonal rotations: Let V = Rv v, w )| < 1 ( ) and W = Rw ( ) be unitary qubit operators where 0 < |( and let u be an axis vector orthonormal to v . Since W can be written as W = Rv (1 )Ru ()Rv (2 ), we can construct a qubit rotation

U = Ru () = Rv (1 ) W Rv (2 )

(1.79)

orthogonal to V . Provided that /, / I+ , there exist k1 , k2 N such ki that Rv to arbitrary precision and the operator pair {V, W } is (i ) V universal. 1.4.2.4 Quantum Gates

Denition 25 Let H = B n or H = B be the state space of a composite system of the qubits S = {qi } and Rk (S ) = {s S | |s| = k } denote the ordered k -qubit subsets of S i.e. the set of k -qubit registers. The class (U ) = {U (s) | s Rk (S )} (1.80)

of register operators on H for some unitary k -qubit operator on B k is called a k -qubit quantum gate. Informally, we can describe a k -qubit gate as a unitary operator which can be equally applied to any k -qubit register of a quantum computer. In that case, the term gate is also used to refer to a single register operator U (s) (U ) as well as to the operator U itself. Common Elementary Gates Single Qubit Gates Pauli gates X x = 0 1 1 0 , Y y = 0 i i 0 , Z z = 1 0 0 1 (1.81)

CHAPTER 1. QUANTUM COMPUTING Hadamard gate 1 H 2 Phase- and /8-gate8 S Z= 1 0 0 i , T S= 1 0 0 ei/4 1 1 1 1

28

(1.82)

(1.83)

Two Qubit Gates controlled-not gate CNot : |x, y |x y, y swap-gate Swap : |x, y |y, x controlled-phase-gate CPhase : |x, y ixy |x, y Three Qubit Gates Tooli-gate (controlled-controlled-not) CCNot : |x, y, z |x (y z ), y, z Fredkin-gate (controlled-swap) CSwap : |x, y, z 1.4.2.5 Controlled Gates |y, x, z |x, y, z if z = 1 if z = 0 (1.88) (1.87) (1.86) (1.85) (1.84)

The single most important 2-qubit gate is the controlled-not-gate. The CNotgate operates on a target qubit t and a control (or enable) qubit e and can be dened using the X -gate, as matrix

CNot = C [X ] =

I 0 0 X

1 0 0 0

0 1 0 0

0 0 0 1

0 0 1 0

(1.89)

T is named /8-gate as T

eix /8 .

CHAPTER 1. QUANTUM COMPUTING or as the register operator CNot(t, e) : |d t |c


e

29

(X c |d t ) |c

(1.90)

Informally, we can describe the CNot-gate as conditionally applying the operator X (single bit not) to the target qubit t in dependence of the control qubit e. This can be generalized to arbitrary gates and multiple control-bits: Denition 26 (Controlled Gate) Let U be a unitary m-qubit gate. A controlled U -gate with n control qubits is dened as I . . . . .. C n [U ] = 0 0 0 0 on B n+m or in register notation U[[e]] (t) C n [U ](t, e) : |k t |c
e

0 0

0 0 I 0 0 U

(1.91)

(U |k t )|c |k t |c e

if c = 111 . . . otherwise

(1.92)

For any single qubit gate U , C [U ] can be implemented using single qubit operations and CNot: Let U = ei Rz ()Ry ( )Rz ( ) be the ZY-decomposition (1.75) of U , A = Rz ()Ry (/2), B = Ry (/2)Rz (( + )/2) and C = Rz (( )/2), then U[[e]] (t) = ei Rz ()(e)A(t)CNot(t, e)B (t)CNot(t, e)C (t) (1.93)

Phase Gates Operators of the form V () = C n [ei ] are referred to as (controlled) phase gates, and are an interesting special case as the operator U = ei is a physically irrelevant overall phase and can technically be considered as a zero-qubit gate, so only controlled versions of U have a non-trivial physical eect. Examples for controlled phase gates are Rz () C [ei ] (see 1.4.2.2), Z = C [1], S = C [i], T = C [ei/4 ] and CPhase = C 2 [i] (see 1.4.2.4). 1.4.2.6 Universal Gates

A well known result from classical boolean logic is that any possible function f : Bn Bm can be constructed as a composition from a small universal set of operators if we can wire the inputs and outputs to arbitrary bits in a feed-forward network. Examples for universal sets of logical gates are }. {, }, {, } or {

CHAPTER 1. QUANTUM COMPUTING

30

In 1.4.2.3 we have already demonstrated how almost any pair of single qubit rotations can be used to approximate an arbitrary unitary operator on B. It can be shown that any n-dimensional unitary matrix can be decomposed into a product of at most n = n(n 1)/2 two-layer unitary matrices 2 9 [42, 52], i.e.
n1 j 1

U=
j =1 i=0

Uij

where Uij : |k

aij |i + bij |j

cij |i + dij |j |k

if k = i if k = j otherwise

(1.94)

If H = B n is a composition of qubits, then for any single qubit gate U , C n1 [U ] is also a two-layer matrix. Using suitable basis permutations ij : |k |k with i = 2n 2 and j = 2n1 the two-layer matrices Uij from (1.94) can be written as
n1 Uij = [Vij ] ij , ij C

Vij =

aij bij cij dij

(1.95)

Since basis permutations essentially implement bijective functions over B (see 2.5.3), any quantum gate which implements a universal reversible boolean gate, together with controlled single qubit operations, is enough to implement arbitrary unitary operators on B n . An example for a universal reversible boolean gate is the classical Tooli gate T : (x, y, z ) (x (y z )) [57]. Unlike T , its quantum counterpart (1.87) can be factorized into 2-qubit operators, e.g.
n

CCNot(x, y, z) = V[[z]] (x)CNot(y, z)V[[y]] (x)CNot(y, z)V[[y]] (x) X = ei/4 Rx ( ) (1.96) 2 By choosing V such that U = V 2 the above factorization can be used to construct C 2 [U ] for arbitrary arbitrary single qubit-gates. Moreover, similar decompositions can be found for any number of control qubits, so CNot and single qubit operations are universal [28]. Further examples for universal sets of quantum gates are with V = the standard set [43, 13]10 {H, S, T, CNot}
9

(1.97)
n 2

This is similar to the fact that a general rotation in Rn can be decomposed into simple rotations in the coordinate planes. 10 Since S = T 2 , the phase gate (1.83) is merely included for convenience.

CHAPTER 1. QUANTUM COMPUTING

31

The standard set is universal despite H and T being and /4 rotations in the Bloch sphere, because the rotation angle of Rn ( ) = T H and 2 Rm ( ) = HT , which is given by cos = cos , can be shown to be an 8 irrational multiple of [13]. the Deutsch gate [25] D = C 2 [iRx ()] for I (1.98)

The universality proof involves the construction of the Tooli gate which can be used to implement arbitrary basis-permutations : |i |i . Those can then be used to construct 3-qubit two-layer rotations between any two basis-vectors, which can be shown to be sucient to construct arbitrary unitary transformations [41].

1.4.3
1.4.3.1

Input and Output


Quantum Computing and Information Processing

As already mentioned in 1.2.1, the ultimate claim of quantum computing is that the interpretation of computing as a physical process, rather than the abstract manipulation of symbols, leads to an extended notion of computability. In accordance with the postulates of quantum mechanics (see 1.2.3), we also identied the concept of unitary transformations as the most general paradigm for physical computability. Unitary transformations describe the transition between machine states and thereby the temporal evolution of a quantum system. The very notion of a (quantum) computer as a computing machine requires, however, that the evolution of the physical system corresponds to a processing of information. Classical information theory requires that any reasonable information can be expressed as a series of answers to yes-no questions, i.e. a string of bits. But unlike classical symbolic computation, where every single step of a computation can be mapped onto a bit-string, physical computation requires such a labeling only for the initial and the nal machine state, the labels of which make up the input and output of the computation.11 If we regard a quantum computer as a probabilistic machine M (see 1.3.2.5 and 1.4), the above requirements are equivalent to the enumerability of the input and output sets I and O.
This is in accordance with the Copenhagen interpretation of quantum physics, which states that the setup and the outcome of any experiment has to be described in classical terms.
11

CHAPTER 1. QUANTUM COMPUTING 1.4.3.2 Measurement Operators

32

In the classical machine denition (see 1.3.2), a test command t T is a function t : S B. In the case of a quantum computer M = (H, O, T, , ), however, the machine state | H is not directly accessible and any physically realizable test-command will have to amount to the measurement of some observable M . According to the 3rd postulate of quantum mechanics (see 1.2.3.3), the measurement of M on | is only deterministic and invariant to | i | happens to be an eigenstate of M , so the test commands are no longer boolean predicates on S but probabilistic measurement operators, i.e. T = {i : H H B} (1.99)

Denition 27 Let M be a self-adjoint operator on H with the spectral decomposition M = m m Pm , then the measurement operator [M ] is a probabilistic mapping [M ] : H H R dened as [M ] : | 1 Pm | , m pm with probability pm = |Pm | (1.100) Since test commands are supposed to deliver boolean results, each i corresponds to a projection operator Pi i.e. a Hermitian with the eigenvalues 0 and 1. So i = i [Pi ] and
1 P | , 1) ( p [P ] : | 1 ( (I P )| , 0) p

with p = |P | with p = |I P |

(1.101)

If P = N (s) we also write (s) [N (s)]. Also, we will occasionally ignore one of the function values if this is convenient and can be done without ambiguity. Single Qubit Measurements If H = B n , then a natural choice for Pi are the standard observable N (qi ) = I i |1 1| I ni1 (1.102)

for each qubit qi . If M provides a universal set O of unitary operators, single qubit measurements in the computational basis are sucient to measure an arbitrary P :

CHAPTER 1. QUANTUM COMPUTING A general projection P has the form P =


n

33

n |n n | with n B and |n B

(1.103)

is an arbitrary orthonormal basis of H. If O is universal, then it where B | and P can be is possible to implement the unitary operator U = n |n n expressed as P = U P U with P = n n |n n|. To measure P , we can use an additional scratch qubit s in state |0 s and use the unitary operator U : |k s |n
s

| k n s |n

(1.104)

to prepare the entangled machine state | =


n

cn |n s |n

where cn = n|

(1.105)

A measurement of N (s) on | is now equivalent to to measuring P on | . If the result is 1 and therefore s is left in the state |1 s , the previous state can be restored by applying X (s). Output Function To retrieve the classical result of the computation, a nal measurement is required. Using the standard observable N on H, we can dene the probabilistic output function : H N as = [N ].12 1.4.3.3 State Preparation

To set or reset a quantum computer M to desired initial state |0 , no additional operations besides unitary transformations and measurements are necessary. Assuming H = B n , it suces to measure all qubits to bring M into a known state | = |m and then to apply an arbitrary unitary operator Um which satises the condition 0 |Um |m = 1. If |0 = |d0 d1 . . . dn1 then at most n X -gates are required for the preparation.13 It is also convenient to include a special non-unitary memory command reset : | |0 (1.106) for the initialization of the machine state.
12 13

This notation is actually a shorthand for (| ) = m [N ]| = (|m , m). For arbitrary |0 , the number of necessary gates generally increases exponentially.

CHAPTER 1. QUANTUM COMPUTING

34

Input Function To allow the preparation of classical input states we can dene the input function (s) : N H as (s) = |s . For quantum algorithms which take their input in the form of oracle operators14 and consequently do not require any classical input, it is common to assume an initial state |0 = (s) = |0 .

1.5
1.5.1

Concepts of Quantum Computation


Models and Formalisms

As we demonstrated in 1.3 the concept of the universal computer can be represented by several equivalent models, corresponding to dierent scientic approaches. From a mathematical point of view, a universal computer is a machine capable of calculating partial recursive functions (1.3.1.1), computer scientists often use the Turing machine (1.3.2.2) as their favorite model, an electronic engineer would possibly speak of logic circuits while a programmer probably will prefer a universal programming language (1.3.3.2). As for quantum computation, each of these classical concepts has a quantum counterpart: [47, 48] Model Mathematical Machine Circuit Algorithmic classical partial recursive funct. Turing Machine logical circuit univ. programming language quantum unitary operators QTM quantum gates QPLs

Table 1.2: Classical and quantum computational models

1.5.1.1

The Mathematical Model

The paradigm of computation as a physical process requires that algorithms can in principle be described by the same means as any other physical system, which, for the eld of quantum physics, is the mathematical formalism of Hilbert space algebra. The basics of this formalism, were introduced in 1.2. The quantum equivalent of partial recursive functions is unitary operators. Just as every classically computable problem can be reformulated as
An oracle function or operator is a special black-box memory or test command which can be used either as problem description or to extend the functionality of a machine.
14

CHAPTER 1. QUANTUM COMPUTING

35

calculating the value of a partial recursive function, every quantum computation can be described by a unitary operator.15 The mathematical description of an operator is inherently declarative; the actual implementation for a certain quantum architecture i.e. the algorithmic decomposition into elementary operations, is beyond the scope of this formalism. Also, since the mathematical model treats unitary operators as black boxes, no complexity measure is provided. Register Notation To simplify the discussion of operators applied to permutations of qubits on H = B n and H = B we introduced the concept of quantum registers (see 1.4.1.3). Table 1.3 summarizes all important register expressions. Notation s s |... s |s| Hs ab U (s) (s) s U (a, b) U[[b]] (a) Description general register i.e. an ordered set of qubits complementary register to s ket-vector of register s length of s i.e. number of qubits in s |s|-qubit sub-space {| s } of H, Hs H s = H concatenation of registers a and b, a b = b a register operator U I on Hs H s stochastic measurement operator [N (s)] : H H B|s| register density operator s = tr s (| |) multi-register operator U (a b) conditional register operator C |b| [U ](a b) Table 1.3: Register Notation

1.5.1.2

Quantum Turing Machines

In analogy to the classical Turing Machine (TM) several types of Quantum Turing Machines (QTM) have been proposed as a model of a universal quantum computer [5, 24, 6, 9]. The complete machine-state | H of a QTM is given by a superposition of basis states |l, j, s , where l Zn is the state of the head, j N the head position and s = (. . . s2 s1 |s0 s1 s2 . . .) (1.107)
This assumes that any measurements are performed at the end of the computation. There are however algorithms, which take advantage of the state-reduction inherent to quantum measurement so the analogy is not universal.
15

CHAPTER 1. QUANTUM COMPUTING

36

the binary representation of the tape-content. To keep H separable, s has to meet the zero tail state condition (see 1.3.2.2) i.e. only a nite number of bits with sm = 0 are allowed, so s B and H = Cn l2 B The quantum analogue of the transition function of a classical probabilistic TM is the unitary step operator T , which has to meet locality conditions for the aected tape-qubit, as well as for head movement.16 QTMs provide a measure for execution times, but as with the classical TM nding an appropriate step operator can be very hard. 1.5.1.3 Quantum Circuits

Quantum circuits are the quantum equivalent to classical boolean feed-forward networks, with one major dierence: since all quantum computations have to be unitary, quantum circuits can be evaluated in both directions (as in classical reversible logic). Quantum circuits are composed of elementary gates and operate on qubits.17 The wiring between the gates denes the register on which the gates operate, so an m-qubit gate U in an n-qubit circuit can n! describe up to (n dierent unitary transformations U (s). m)! As opposed to the mathematical formalism, the gate-notation is an inherently constructive method and the complexity of the problem is directly reected in the number of gates necessary to implement it. However, since quantum circuits describe static sequences, the size of the input as well as the number of qubits is xed, so without additional assumptions, quantum circuits cannot be used to analyze the complexity depending on the size of the problem. For the same reason, quantum circuits are also inadequate for machines with unlimited memory. Restrictions In comparison with classical boolean feed-forward networks, this imposes the following restrictions: Only n-to-n networks are allowed i.e. the total number of inputs has to match the total number of outputs. Only n-to-n gates are allowed.
Instead of a unitary step-operator T , it is also possible to directly construct a (local) Hamiltonian H (see 1.2.3.2) [5, 6]. In this case, the computation does not need to be discrete and T = U (t0 ) = eiHt0 is not required to conform to locality conditions. 17 There also exist extentions to cover measurements and classical bits [43]. In that case, quantum circuits can also be used to describe irreversible computations.
16

CHAPTER 1. QUANTUM COMPUTING

37

No forking of inputs is allowed. This is directly related to the fact that qubits cannot be copied, i.e. that there exists no unitary operation Copy | |0 | | with | C2 (1.108)

which can turn a general qubit-state into a product state of itself. No dead ends are allowed. Again, this is because the erasure of a qubit Erase | |0 with | C2 (1.109) is not a unitary operation. Notation Some common gates (see 1.4.2.4) have special symbols. The circuit in g. 1.3 implements the operator18 C [ei ](a) CSwap(a, b, c) Swap(a, b) CCNot(a, b, c) CNot(a, b) U[[b]] (a) U (a) (1.110)
c b a c b

Figure 1.3: Circuit notation for common gates

1.5.1.4

Quantum Programming Languages

A possible way to generalize quantum circuits for arbitrary input sizes is to use a classical computer with unlimited memory (such as a TM) to generate the circuits depending on the size of the input. So instead of directly specifying a single circuit in terms of wires and gates, a whole class of quantum circuits is specied by means of a classical program. Quantum programming languages (QPLs) take this abstraction even further by directly using a quantum computer Mq as a oracle (see 1.3.2.4) for a classical machine Mc . This not only avoids the need for an intermediate circuit-description, but also allows the computation to depend on previous measurements so quantum programs can describe complete algorithms and not merely unitary transformations.
18

Note that the order of the operators is inverted.

CHAPTER 1. QUANTUM COMPUTING

38

1.5.2
1.5.2.1

Quantum Algorithms
Classical and Quantum Computability

If we consider a nite quantum computer with the Tooli gate (see 1.4.2.4) as the only available instruction, then any transformation of the machine state has to be of the form | = |i |g (i) = | with g : Bn Bn (1.111)

Since the Tooli gate is universal for reversible boolean logic, any bijective binary function g can directly be implemented on a quantum computer. A general binary function f on Bn , can be implemented by an arbitrary unitary operator F which satises the condition F |i, 0 = |i, f (i) . So any function f computable on a (nite) classical machine can also be implemented on a quantum computer with a universal set of gates. Moreover, C. H. Bennet has shown that a reversible implementation of f can be made with a maximal overhead of O(2) in time and O( n) in space complexity [7]. On the other hand, a general n-qubit quantum state consists of maximally n 2 basis-vectors with a non-zero amplitude and can consequently be described by an array of 2n complex numbers. Also, any m-qubit quantum gate can be described by a complex 2n 2n matrix with the elements uij = i|U |j . By encoding the complex amplitudes as a pair of oating point binary numbers a classical computer can simulate any unitary operator to arbitrary precision.19 This will generally require an overhead of O(en ) in time as well as in space complexity. Due to the stochastic nature of quantum measurements, the emulating computer will also need a source of true randomness (like e.g. the probabilistic Turing machine). So classical and quantum computers are computationally equivalent, but while it is possible to eciently simulate a classical computer on a quantum computer, the opposite case can involve an exponential overhead. Therefore, while not extending our notion of computability, for certain tasks quantum algorithms might provide a more ecient solutions than classical implementations. 1.5.2.2 Deutschs Algorithm

In 1985, Deutsch proposed a probabilistic algorithm [24] which for some oracle function g : B B allows to compute g (0) g (1) with a probability of 1/2 using only 1 application of G:
19

The linearity of unitary transformations assures that small errors will not escalate.

CHAPTER 1. QUANTUM COMPUTING

39

Let G : |x, y |x, y g (x) be a 2-qubit oracle-operator implementing the boolean oracle function g : B B. 1. Prepare an empty initial state |0 = |0 x |0 y . 2. Apply H (x) |1 = (H |0 x ) |0
y

1 = |0 x |0 2

+ |1 x |0

(1.112)

3. Apply the oracle operator G, giving 1 |2 = |0 x |g (0) 2 4. Apply H (x y), resulting in 1 (1)yg(0) + (1)x+yg(1) |x x |y |3 = 2 2 xB yB 5. Measure x and y. As (1.114) can be simplied to 1 | 3 = |0 x |0 2
y y y

+ |1 x |g (1)

(1.113)

(1.114)

+ (1)g(0) |g (0) g (1) x |1

(1.115)

the register x will contain the value g (0) g (1) whenever 1 has been measured in y which will happen in 50% of the cases. While strictly speaking, this does not provide any speedup over the classical case, if we take into account that, on average, two tries are required to actually measure g (0) g (1), Deutschs algorithm was the rst proof that quantum computers are capable of performing computations in ways that are impossible on a classical computer.20 Generally, in order to achieve any speedup over classical algorithms, it is necessary to take advantage of the unique features of quantum computing, namely Superposition (step 2) Quantum Parallelism (step 3) Interference (step 4)
There exist several improvements and generalizations to the original version of Deutschs algorithm [26, 20], one of which the Deutsch-Jozsa Algorithm is described in 1.5.2.6.
20

CHAPTER 1. QUANTUM COMPUTING 1.5.2.3 Superposition

40

A key element in any universal programming language is conditional branching. Any classical program (see 1.3.3) can be modeled as a decision tree where each node corresponds to a binary state sn and leads to one or more succes(i) sor states sn+1 . On a deterministic Turing machine (TM), only one of those (k) transitions sn sn+1 is possible, so the computational path s0 , s1 , . . . sn is predetermined. On a probabilistic TM (see 1.3.2.5), the transitions are characterized by (i) probabilities pi with i pi = 1 and one of the possible successor states sn+1 is chosen accordingly at random. Since the basis-vectors |i directly correspond to classical binary states, we might interpret a unitary transformation U : |s
s

uss |s

with s, s Bn

and uss C

(1.116)

as a probabilistic transition form the classical state s to the successor states s with the transition probabilities ps = |uss |2 , but unless we perform a measurement, the resulting machine state remains in a superposition of all possible classical successor states | = |sn | =
i U

usn s(i) |sn+1


n+1

(i)

(1.117)

So from a classical point of view, we can consider a unitary operator which transforms an eigenstate into a superposition of n eigenstates with nonzero amplitudes as a 1n fork-operation, which enables a quantum computer to follow several classical computational paths at once. Hadamard-Transform Most non-classical algorithms take advantage of this feature by bringing a register into a even superposition of all basis-states to serve as search space. In Deutschs algorithm, this is achieved by applying the Hadamard-gate on the rst qubit-register. The H -gate can be generalized to n-qubit registers, by applying H to each individual qubit. The resulting unitary operator is called Hadamard transform and dened as: H : |x 2 2 where x y = basis
i
n

(1)xy |y
y Bn

(1.118)

xi yi denotes the binary inner product. The transformed 1 with | = (|0 |1 ) (1.119) 2

= {H |x | x Bn } = {|+ , | }n B

CHAPTER 1. QUANTUM COMPUTING

41

is sometimes referred to as the dual basis. Classically, the Hadamard transform of | = |0 can be viewed as a binary decision tree with a 50% chance for each bit to ip. For an n-qubit register, this leads to 2n classical computational paths all of which are followed simultaneously resulting in a superposition of 2n eigenvectors. 1.5.2.4 Quantum Parallelism

If we restrict unitary transformations to basis-permutations (i.e. operators of the form |i |i then the classical decision tree degenerates into a list and we end up with the functionality of a classical deterministic reversible computer i.e. for any bijective binary function f : Bn Bn there is a corresponding unitary operator F : |s |f (s) with s Bn . (1.120)

The restriction to bijective functions is not a severe as it seems, since for any general binary function g : Bn Bm a corresponding quantum function (1.121) can be constructed, which implements g with a maximum overhead of O( n) in space- and O(2) time-complexity. However, if we use a quantum function on an superposition of eigenstates, the same classical computation is performed on all bit-strings simultaneously. G
s

G : |s, 0 |s, g (s)

with s Bn

cs |s, 0 =
s

cs G|s, 0 =
s

cs |s, g (s)

(1.122)

In classical terms, this can be described as a SIMD (single instruction, multiple data) vector operation, in quantum terms this feature is referred to as quantum parallelism. In Deutschs algorithms, quantum parallelism is exploited by applying G on the superposition |1 = (|00 + |10 )/ 2. 1.5.2.5 Interference

While superpositions and quantum parallelism allow us to perform an exponentially large number of classical computations in parallel, the only way to read out any results is by performing a measurement whereby all but one of the superpositioned eigenstates get discarded. Since it does not make any dierence if the computational path is determined during the calculation (as with the probabilistic TM) or a-posteriori (by quantum measurement), the

CHAPTER 1. QUANTUM COMPUTING

42

use of quantum computers would not provide any advantage over probabilistic classical computers. Quantum states, however, are not merely a probability distribution of binary values but complex vectors i.e. each basis-state in a superposition is not characterized by a real probability, but a complex amplitude, so 1 1 |1 = (|0 + |1 ) and |2 = (|0 |1 ) 2 2 (1.123)

describe dierent states, even if they have the same probability spectrum. So, while on a probabilistic TM, the probabilities of two dierent computational paths leading to the same nal state s simply add up, this is not necessarily the case on a quantum computer since generally | + |2 = | |2 + | |2 for , C. (1.124)

To illustrate this concept, consider the three states |1 = |0 , | 2 = |1 1 and |3 = (|0 + |1 ). 2 (1.125)

If we apply the Hadamard-transform H to the basis states |1 and |2 we get 1 1 |1 = H |1 = (|0 + |1 ) and |2 = H |2 = (|0 |1 ). 2 2 (1.126) Since |1 and |2 have the same probability distribution and |3 is merely a superposition of |1 and |2 , classically we would assume that |3 also shows the same probability spectrum, however 1 |3 = H |3 = (|1 + |2 ) = |0 2 (1.127)

so in case of |0 the probabilities added up while in case of |1 , the complex amplitudes had opposing signs leading to a partial probability of 0. This phenomenon is referred to as positive or negative interference. So while the computational paths on a probabilistic TM are independent, interference allows computations on superposition states to interact and it is this interaction which allows a quantum computer to solve certain problems more eciently than classical computers. The foremost design principle for any quantum algorithm therefore is to use interference to increase the probability of interesting basis states while trying to reduce the probability of uninteresting states, in order to improve the chance that a measurement will pick one of the former.

CHAPTER 1. QUANTUM COMPUTING

43

Basis Transformations Since any unitary operator U can also be regarded as a basis transformation, the above problem can also be reformulated as nding an appropriate observable for the measurement, thereby eectively replacing the standard observable N by the Hermitian operator = U N U = M =N
n

n|n n |.

(1.128)

This view is especially useful, if global properties of classical functions such as periodicy are of interest for the problem. In Deutschs algorithms, the nal measurement is performed in the dual basis (1.119), which allows g (0) g (1) to be extracted in a single measurement. 1.5.2.6 Deutsch-Jozsa Algorithm

The Deutsch-Jozsa algorithm [26] is an improved and generalized version of the original algorithm described in (see 1.5.2.2). For n = 1 it gives a deterministic solution to Deutschs problem: Let F : |x, y |x, y f (x) be an (n + 1)-qubit oracle-operator implementing a boolean function f : Bn B. Let us further assume that we know that f is either constant ((x)f (x) = b where b B) or balanced ( f (x) = 2n1 ). Classically, 2n1 + 1 evaluations of f would be necessary to nd out which of the possibilities applies. The following quantum algorithm can solve this decision problem with a single application of F : 1. Prepare initial state |0 = |0 x |1 y . 2. Apply H (x y) to set up the search space |1 = 2
n+1 2

superposition
y

|x
xBn

|0

|1

(1.129)

3. Apply the oracle operator, giving |2 = 2


n+1 2

quantum parallelism
x

(1)f (x) |x
xBn

|0

|1

(1.130) interference

4. Apply H (x), which results in |3 =


z Bn

c z |z

1 |0 2

|1

c z = 2 n
xBn

(1)xz+f (x) (1.131)

CHAPTER 1. QUANTUM COMPUTING 5. Measure x. The probability to measure 0 in x is |c0 |2 where c0 = 2n


xBn

44

(1)f (x)

(1.132)

If f is constant c0 = 1, if f is balanced, the summands cancel out and c0 = 0, so (x)|3 = 0 f is constant.

Chapter 2 Structured Quantum Programming


2.1
2.1.1

Introduction
Motivation

As quantum computing is on its way to becoming an established discipline of computing science, much eort is being put into the development of new quantum algorithms. This research has usually not got much in common with the experimental work on quantum computers and is rarely tied to a specic hardware, but instead employs an abstract notion of a quantum computer with qubits, registers, and a small set of suitable elementary operations [3], which allows one to concentrate on the problem at hand. A programming language which integrates these abstractions by design should be a useful tool in situations where the much more general physical formalism of Hilbert space algebra gets unnecessarily complex. The possibility to formulate and simulate an algorithm in a programming language should also make it easier to optimize the implementation with regard to dierent quantum architectures and allow for more accurate estimates of time and memory complexity.

2.1.2

Quantum Programming Languages

From a software engineering point of view, we can regard the algebraic formalism as a specication language, as the mathematical description of a quantum algorithm is inherently declarative and provides no means to derive a unique decomposition into elementary operations for a given quantum hardware. 45

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

46

Low level formalisms as e.g. quantum circuits [25], on the other hand, are usually restricted to specic tasks, such as the description of unitary transformations, and thus lack the generality to express all aspects of nonclassical algorithms. The purpose of programming languages is therefore twofold, as they allow the expression of a computations semantics in an abstract manner, as well as the automated generation of a sequence of elementary operations to control the computing device. Any useful quantum programming language (QPL) therefore needs to be constructive hardware independent provide arbitrary levels of abstraction integrate non-classical features at a semantic level While the rst three requirements equally apply to classical and quantum programming languages, QPLs also have to reect the peculiarities of quantum computing, as e.g. reversibility of unitary operations non-locality of qubits non-observability of states destructive nature of measurement lack of an erase operation at a design level and consequently have to provide the means to take advantage of these features (e.g. by allowing to run code in reverse).1

2.1.3

Classication of Programming Languages

In traditional CS, programming languages can be categorized as either logical (e.g. Prolog), functional (e.g. LISP) or imperative (e.g. Assembler, Fortran, Pascal, C), the latter being the most widely used, for the description of algorithms, as well as for the actual implementation of real world programs.
Classical languages with additional quantum directives (like e.g.. a C++ simulation or device driver API) are therefore not considered to be QPLs, as they do not generically support these non-classical concepts [61].
1

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING 2.1.3.1 Imperative Programming Languages

47

Imperative programming is centered around the concept of a computational state, and commands modifying the state [1]. The state is an abstraction for the modiable storage of the underlying machine model and is semantically expressed in terms of symbolic variables of various data types and the current state of control (instruction counter, stack pointer, etc.), which may or may not have a direct representation within the language. During its execution, a program generates a sequence of states. The transition from one state to the next is determined by assignment commands, which modify the state of variables, and sequencing commands, which modify the state of control. The direct correspondence between state chances and program statements leads to the concept of ow of control, i.e. a computation can be characterized by the actual sequence of commands. This allows an imperative program to be traced by following the current state of control and introduces a notion of locality of execution. Examples of imperative programming languages are assembly language or BASIC. Concepts of imperative programming are reected in the hybrid quantum architecture (2.2.3) and the concept of symbolic quantum registers (2.4.1). 2.1.3.2 Procedural Programming Languages

Imperative programming combined with parameterized subroutines (procedures, functions) it is called procedural programming. The availability of subroutines allows for Functional Abstraction: Code of similar or identical functionality can be generalized to reusable parameterized procedures. Hierarchical Program Structure: Subroutines can contain calls to other subroutines which provides arbitrary levels of abstraction and supports a top-down approach in software design. Recursion: Subroutines can also contain calls to themselves which allows for an elegant and ecient implementation of otherwise complicated control structures and directly supports the classical divide and conquer approach in software design.

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

48

Private Scopes: Subroutines provide their own namespace which reduces interdependencies within the program. Local Variables: Local namespaces also allow the use of temporary variables of limited lifetime which leads to more ecient usage of the available storage and allows details of the implementation to be hidden from the procedures calling interface. Examples of procedural programming languages are Fortran and C. Concepts of procedural programming are reected in quantum data types (2.4.1.3, 2.5.2.2), quantum subroutines (2.5.1) and scratch space management (2.5.3.5) [47]. 2.1.3.3 Structured Programming Languages

An mentioned in 2.1.3.1, imperative programs consist of assignment and sequencing commands. A simple way to provide ow-control is by means of a goto command which explicitly transfers control to a labeled command which is to be executed next. goto label ; if condition then goto label ; This approach is problematic as it implies a at control structure where any labeled command can be jumped to from anywhere within the program (or, in the case of procedural languages, the current subroutine). This tends to make larger programs unreadable (spaghetti code). In 1966, Corrado Bohm and Guiseppe Jacopini showed [11] that the goto command can be replaced by the nesting and stacking (sequencing) of three basic control statements sequence (block-statements), selection (if-statements) and iteration (conditional loops), with well dened entry- and exit-points. This approach, called structured programming [27, 23], implies a strictly hierarchical control-structure which not only makes programs easier to understand, but also provides useful metainformation for the compiler. Examples of structured programming languages are Pascal and Modula, but almost any procedural language supports the necessary control statements, even if their exclusive use is not enforced. Concepts of structured programming are reected in quantum conditions (2.6.3), conditional operators (2.6.1) and quantum if-statements (2.6.2.1).

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

49

2.1.4

Goals

Functional abstraction and hierarchical control-structure, together with the very intuitive notion of a ow of control, absent in merely declarative formalisms, seem to t most peoples way of reasoning about computational tasks. This makes structured programming a powerful formalism, not only for the actual task of coding, but also for the description of algorithms in general (ow-charts, pseudo-code). In the remainder of this chapter we will show how familiar concepts of classical structured programming can be adopted to the eld of quantum computing. The programming language QCL [45, 49] will serve as an example to illustrate the principles of structured quantum programming. Table 2.1 gives an overview of quantum language elements along with their classical semantic counterparts. Classical concept classical machine model variables variable assignments classical input subroutines argument and return types local variables dynamic memory boolean expressions conditional execution selection conditional loops Quantum analogue hybrid quantum architecture quantum registers elementary gates quantum measurement operators quantum data types scratch registers scratch space management quantum conditions conditional operators quantum if-statement quantum forking

Table 2.1: Classical and quantum programming concepts

2.1.5
2.1.5.1

State-of-the-Art
QC Software and Simulators

As the general interest in quantum computing increased during the last few years, so did the number of available simulators and computational tools. J. Wallace ran a detailed survey [61] on 21 quantum computer simulators. A more recent list naming 36 projects can be found online [62].

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING The available software can be roughly categorized as class libraries for existing classical languages packages for computer algebra systems quantum circuit simulators simulations of specic quantum hardware simulations of specic algorithms other quantum simulations (QTMs, quantum Bayesian nets, etc.) 2.1.5.2 Quantum Programming Languages

50

Besides QCL [49], there have been at least 3 attempts to design a quantum programming language 1996 Q-gol by Greg Baker [2] 2000 qGCL by Paolo Zuliani [63] 2002 Quantum C Language by Stephen Blaha [10] From those, the most evolved project is probably Zulianis qGCL. In his thesis [63] Zuliani proposes an abstract formalism with rigorous semantics and an associated renement calculus which allows for program derivation and strict proof of correctness. There is, however, no interpreter or compiler available.

2.2

The Computational Model of Quantum Programming

In this section we will demonstrate why quantum circuits and nite programs are insucient for universal quantum computation and introduce the hybrid quantum architecture as the computational model of quantum programming.

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

51

2.2.1
2.2.1.1

Quantum Circuits
Deterministic Sequential Algorithms

In its simplest form, a quantum algorithm P merely consists of a unitary transformation and a subsequent measurement of the resulting state | in the computational basis B . If | = |y is a basis-state, then the outcome of the measurement is predetermined and the algorithm P is deterministic. An example for this class is the Deutsch-Jozsa algorithm presented in 1.5.2.6. For xed problem sizes2 , deterministic sequential algorithms can be completely described as quantum circuits (see 1.5.1.3) or, equivalently, as sequences (see 1.3.3.1) for a quantum computer M. The Deutsch-Jozsa algorithm e.g. can be implemented by the following commands (assuming an initial state of |0, 0 ). DJ = X (y) H (x) H (y) F (x, y) H (x) (x) 2.2.1.2 General Probabilistic Algorithms (2.1)

For many computational problems, ecient quantum implementations have the form of probabilistic algorithms. Fig. 2.1 shows the basic outline of a probabilistic quantum algorithm with a single evaluation loop. An example for this simple case is Deutschs algorithm presented in 1.5.2.2. More complex quantum algorithms, as e.g. Shors algorithm for quantum factoring [54, 30], can also include classical random numbers, partial measurements, nested evaluation loops and multiple termination conditions; thus the actual quantum operations are embedded into a classical control structure As quantum circuits are feed-forward networks and lack internal owcontrol, they cannot provide a complete description of probabilistic algorithms. Even in the simple case depicted in g. 2.1, the decision whether a measurement value is good i.e. provides a solution of the computational problem, is outside the scope of the formalism and requires additional assumptions.

2.2.2

Finite Quantum Programs

In 1.3.3.2 we introduced the concept of nite programs which dene nite automata to determine the ow of control of a classical machine M. The same concept can also be used to describe probabilistic quantum algorithms.
This not only refers to the number of classical input bits, but, in the case of blackbox algorithms, also to the number of qubit-parameters for any oracle-operators.
2

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

52

START

reset machine state

classical control structure

quantum operations

unitary transformation

evaluate measurement

measure machine state

no

solution found?
yes

STOP

Figure 2.1: A simple probabilistic quantum algorithm

For a quantum computer M = B 2 , {H (s), G(s), reset}, {(q)}, |0 , [N ] , (2.2)

Deutschs algorithm (see 1.5.2.2) can e.g. be implemented by the following nite program: 1: 2: 3: 4: 5: 6: 7: reset then 2 H(q0) then 3 G(q0,q1) then 4 H(q0) then 5 H(q1) then 6 if (q1) then 7 else 1 (q0) then 0

Generally, for xed problem sizes, any probabilistic quantum algorithm can be implemented as a nite program for a quantum computer with a universal set of gates.3
This is possible as for xed problem sizes, there always exists an upper bound for the number of scratch qubits required to implement the classical control structure.
3

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING 2.2.2.1 Unlimited Memory Machines

53

In classical computing, a machine M in universal, if for any partial recursive function f P , there exists a nite which implements f on M (see 1.3.3.2). Since there exists no upper bound in input size, any classical universal machine must have unlimited memory. Denition 28 Let O SU (2) be an enumerable set of single qubit gates which is dense in SU (2). A quantum computer UGM = (B , {U (q) | U O} {CNot(q, p)}, {(q)}, n |n , [N ]) (2.3) is called unlimited gate machine. Since SU (2) and CNot are a universal set of gates (see 1.4.2.6), any n-qubit unitary operator can be implemented as a sequence for a UGM. However, the UGM is not a universal computer in the classical sense. While this might seem surprising at rst glance, the non-universality of the UGM (and any other quantum computer operating by means of nite gates) becomes obvious if we consider the following simple decision problem: The parity of a bit-string s B is odd if i si = 1 and even if i si = 0. If the length of s is known to be at most n bits, then the parity of s can be computed by an n-qubit quantum circuit using n 1 CNot-gates. Fig. 2.2 shows the circuit for n = 4.
s3 s2 s1 s0 s3 s2 s1 s0

Figure 2.2: Parity of a bit string of length 4 Assume that is a nite quantum program for UGM to compute the parity of arbitrary bit-strings. If contains n = | | instructions, then no matter how complex the computation turns out to be for any given input the transfer function can only result in a composition of at most n dierent gates or measurements. Since any memory or test command aects at most 2 qubits, can only aect a 2n-qubit register of UGM. Therefore only 2n qubits can contribute to the result and there exist at least 22n pairs of bit-strings a, b B2n+1 which dier in a single bit and therefore have opposite parities, for which F (, UGM)(a) = F (, UGM)(b).

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING 2.2.2.2 Universal Quantum Computers

54

The above results shows that for nite programs to be universal, a quantum computer M = (H, O, T, , ) has to provide at least one global operator U O which acts non-trivially on l2 subspaces of H.4 Let H = T = B B be the Hilbert space of a double innite tape of qubits with zero tail states i.e. T = |0

| |0

| B n , n N

(2.4)

and B be the computational basis of T with the single qubit registers l, h and r

B = | . . . s3 s2 |s1 l |s0 h |s1 r |s2 s3 . . .

s : Z B,
i=

si <

(2.5)

We can now dene a unitary shift operator S on T as S : | . . . s2 s1 |s0 h |s1 s2 s3 . . . | . . . s2 s1 s0 |s1 h |s2 s3 . . . (2.6)

Classical Quantum Turing Machine Lets consider the following quantum computer M = T , {X (h), S, S }, {(h)}, s |0 |s , [N ] . (2.7)

It is obvious that M does not provide a complete set of unitary operators as each of the three memory commands operates within B , so no superposition can be created. However, M is a universal computer; in fact M is a Turing Machine (see 1.3.2.2) in disguise. Table 2.2 gives the homomorphisms to translate nite programs between M and a TM. Because of this equivalence, we call M a classical quantum Turing machine or CQTM. Semi-classical Quantum Turing Machine The CQTM can be extended by adding a universal set of unitary transformations. Because of the availability of the shift operator, those operations can be localized and may be restricted to a small number of xed qubit positions.5
An example would be the step-operator of a QTM (see 1.5.1.2). This is an important feature and several proposed ion-trap based hardware architectures for quantum computers take advantage of this [37, 53].
5 4

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING TM L (left) R (right) T (test) S (set) E (erase) 1: 2: 3: if T then 2 else 3 E then 0 S then 0 CQTM S S (h) 1: if (h) then 0 else 2 2: X (h) then 0 1: if (h) then 2 else 0 2: X (h) then 0 X (h)

55

Table 2.2: Equivalent TM and CQTM commands

Denition 29 The quantum computer SQTM = (T , O, {(h)}, s |0 |s , [N ]) with the memory commands O = {H (h), T (h), CNot(h, l), CNot(h, r), S, S } is called semi-classical (or simple) quantum Turing machine. Since X = HT 4 H , the SQTM is capable of emulating a CQTM and is also a universal computer. Let qk denote the k th qubit relative to h and let U be an arbitrary single qubit operator U SU (2). Since H and T are universal for single qubit operations (see 1.4.2.6), there exists a composition U = H i T ni H to approximate U to arbitrary precision. 6 The register operator U (qk ) can then be realized by U (qk ) (S )k H (h)
i

(2.8) (2.9)

T ni (h)H (h) S k .

(2.10)

Moreover, since Swap(qk , qk+1 ) = (S )k CNot(h, r)CNot(r, h)CNot(h, r)S k and (2.11) Swap(qk , qk+l+1 ) = Swap(qk , qk+l )Swap(qk + l, qk+l+1 )Swap(qk , qk+l ) (2.12) any permutation of qubits can be realized and the controlled-not can be applied to arbitrary registers. This means that a SQTM can emulate a UGM and allows to implement arbitrary unitary transformations.
Note, that H is self-inverse, so the factorization of U does not contain higher powers of H as H k+2 = H k . By the same reasoning ni < 8 as T k+8 = T k .
6

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

56

General Quantum Algorithms Non-classical algorithms usually require the creation of quantum circuits depending on classical parameters such as the problem-size. In the most general case, this description is given by a recursive function (see 1.3.1.1), so the actual gate-sequence can be determined by a nite-program on a universal computer. To demonstrate that the SQTM is able to run general quantum algorithms, we need to show how tape-qubits can be addressed as the result of a classical computation. A possible way to achieve this, is to treat all even tape-qubits as classical bit and use them to emulate a TM while using all odd qubits to carry out the actual quantum computation (memory interleaving). The example below shows how to apply a Hadamard gate to the k th odd qubit (q2k+1 ), whose position k is unary encoded (see 1.3.2.2) into the even qubits which are used classically. 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: if (h) then 2 else 4 S then 3 S then 1 S then 5 H (h) then 6 S then 7 S then 8 S then 9 if (h) then 7 else 10 S then 11 S then 0 end mark (0) reached? move to the next classical bit and reiterate move head over qubit apply Hadamard gate move back to end mark move to the previous classical bit begin mark (0) reached? move head to original position

2.2.3

Hybrid Architecture

In 2.2.2.1 we showed that gate-based quantum computers are not universal. On the other hand, global unitary operations like the shift operator (see 2.2.2.2) cannot be expressed within the circuit model, cannot be equally applied to machines with unlimited and limited memory and cannot be assumed to be equally available on dierent quantum hardware architectures. To overcome the above restrictions, quantum programming uses a classical universal language to dene the actual sequence of elementary instructions for a quantum computer, so a program is not intended to run on a quantum computer itself, but on a (probabilistic) classical computer, which in turn controls a quantum computer and processes the results of measurements. From the perspective of the user, a quantum program behaves exactly like any other classical program, in the sense that it takes classical input, such as startup parameters or interactive data, and produces classical output.

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

57

quantum program

classical input

classical output

0000 1111 1111111 0000000 0000 1111 0000000 1111111


binary program state

1 0 0 1 0 1 0 1

quantum operations

measurement values

quantum machine state

Figure 2.3: Hybrid quantum architecture

The state of the controlling computer (i.e. variable values, execution stack, but also the mapping of quantum registers) is referred to as program state. The quantum computer itself does not require any control logic, its computational state can therefore be fully described by the common quantum state | of its qubits (machine state). 2.2.3.1 Machine Model

Formally, the above architecture can be described as a universal computer with a quantum oracle (see 1.3.2.4): Let Mc = (S, Oc , Tc , , ) be a universal classical computer with an encoding : S N and Mq = (H, Oq , Tq , |0 , [N ]) a quantum computer with unlimited memory H = B , a universal set of nite gates Oq =
k

(Gk ) =
k

{Gk (s)} = {U0 , U1 , . . .}

(2.13)

and single qubit-measurements Tq = {(qi )} = {0 , 1 , . . .}. (2.14)

As the number of qubits in Mq is unlimited, Oc and Tc are innite sets. Since any Ui Oc and i Tc only operates on a nite number of qubits, we need a method to allow Mc to address arbitrary commands of Mq . Using the encoding 0 (s) = y0 if (s) = (y0 , . . .) 0 if (s) = ( ) with s S (2.15)

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING we dene the oracle commands U : (s, | ) (s, U0 (s) | ) M : (s, | ) 0 (s) | and

58

(2.16) (2.17)

With U and M as interface, we can now dene the hybrid machine M = Mc Mq as M = (S H, Oc {I } {U }, Oc {I } {M }, , ) (2.18)

I O
Ui

Mc
i

Mq
H

Figure 2.4: Classical computer with quantum oracle

2.2.3.2

The Quantum Oracle

For our formal machine model, we assumed Mq to have unlimited memory. However, the restriction to nite gates and single qubit measurements also allows for quantum oracles with a nite number of qubits. The concept of quantum programming is intended to be hardware independent and applicable to any qubit-based quantum architecture, so we will not assume a specic set of elementary gates, as long as the following requirements are met: The set of gates is universal. Each gate can be equally applied to arbitrary qubits. For any non self-inverse gate U , the gate U is also available.

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

59

Elementary gates may take any number of classical parameters and may or may not be restricted in the number of qubits they operate on. Some concepts of structured quantum programming will require certain operators to be available either as elementary gates or as user-dened operators i.e. procedural compositions of gates which can be dened in the programming language itself. Those operators are a Fanout operation i.e. an arbitrary unitary operator which matches the condition Fanout : |n x |0
y

| n x |n

where |x| = |y|

(2.19)

Fanout is usually implemented as |a, b |a, a b using CNot-gates and is required for the transparent use of local scratch registers (see 2.5.3.5). the Not- (or X -) gate and the controlled-not gate C n [X ] with an arbitrary number of control qubits. The implementation of C n [X ] may use scratch qubits. X and C n [X ] are required for quantum conditions (see 2.6.3) and the quantum if-statement (see 2.6.2.1).

2.3

Structured Programming

Since the computational model of QPLs is that of a classical computer with a quantum oracle, any QPL is also a universal classical programming language. In this section, we will summarize the key elements of classical structured programming languages.

2.3.1

Program Structure

A structured program is a sequence (block) of statements and denitions, which are processed top-down and may contain blocks themselves (controlstatements, subroutine-denitions). 2.3.1.1 Statements

Statements range from simple commands, through subroutine calls to nested control-statements and are executed when they are encountered.
qcl> if random()>=0.5 { print "red"; } else { print "black"; } : red

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING 2.3.1.2 Denitions

60

Denitions are not executed but bind a value (variable- or constant-denition) or a block of code (subroutine-denition) to a symbol (identier).
qcl> int counter=5; qcl> int fac(int n) { if n<=0 {return 1;} else {return n*fac(n-1);} }

Each symbol has an associated type, which can either be a data type or a subroutine type. 2.3.1.3 Expressions

Statements and subroutine-calls can take arguments of certain data types. Expressions can be composed of literals, variables and constants combined by operators and function calls.
qcl> print "5 out of 10:",fac(10)/fac(5)^2,"combinations." : 5 out of 10: 252 combinations.

2.3.2

Expressions and Variables

Unlike untyped formalisms like nite programs or circuits, in programming languages, an expression is usually associated with a data type T . A value v T is called an instance of T . 2.3.2.1 Atomic Expressions

The direct symbolic representation of an instance v T is called a literal. Type int real complex Description integer real number complex number Examples 1234, -1 3.14, -0.001 (0,-1), (0.5, 0.866)

Table 2.3: Scalar arithmetic types and literals in QCL A symbol which is permanently bound to a value v T is called a (symbolic) constant of type T . A symbol which can be bound to arbitrary instances of T is called a variable of type T .

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING 2.3.2.2 Denitions

61

Constants and variables have to be dened (i.e. declared and initialized) before they can be used. To assure deterministic behaviour (see 2.5.1.2) we require that a variable is bound to a type dependent default value if no initial value is provided.
qcl> qcl> qcl> qcl> qcl> const pi = 3.141592653589793238462643383279502884197; const I = (0,1); complex z=exp(I*pi/4); string msg="Hello World"; real vector v[3]; // v is initialized with [0,0,0]

After its denition, a constant or variable is visible and can be used in any subsequent statements and denitions until the end of the current subroutine (local symbol) or the end of the program (global symbol).7 2.3.2.3 Composite Expressions

Literals, constants and variables are atomic expressions. General expressions can be recursively constructed from atomic expression by operators and function calls. expr atomic-expression function ( expr , . . .) unary-operator expr expr binary-operator expr n-ary-operator ( expr , . . .)

So general expressions can be described as trees with literals, constants and variables as leaf-nodes and operators and functions as inner nodes.
qcl> print (3^2+4^2)*sin(log(z)/I); : 17.6777

2.3.3

Subroutines

A subroutine S is a named block (also referred to as body of S ) preceded by a list of parameter-declarations. The body of a subroutine may contain calls to itself (recursion). Classical procedural languages usually provide two types of subroutines:
There also exist languages with ner grained scoping rules, e.g. C++, where variables can be local to a block.
7

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING 2.3.3.1 Procedures

62

A procedure is a general subroutine with arbitrary dependencies and sideeects on the program state. This means that besides its declared arguments, a procedure can also depend on hidden parameters, external input and random events. Moreover, procedures can also change the binding of global variables.
int cash; procedure roulette(int bet) { int n; input "pick a number:",n; cash=cash-bet; if n==floor(37*random()) { cash=cash+36*bet; }; }

In procedural quantum programming, procedures, being the most general subroutine type (see 2.5), are used to implement the classical control structure of quantum algorithms (see 2.2.1.2). 2.3.3.2 Functions

A function is a subroutine which returns a value v T of a certain data-type T. In procedural quantum programming, functions have strict mathematical semantics, which means that v must exclusively and deterministically depend on the declared arguments and that the computation of v must not exhibit any side-eects on the program state.8 This implies that neither global variables nor calls of less restricted subroutine types may appear within the body of a function.
int fibonacci(int n) { if n<2 { return 1; } else { return fibonacci(n-1)+fibonacci(n-2); } }

2.3.4
2.3.4.1

Statements
Assignment

An assignment binds a variable (or an element of a data structure such as an array) of type T to a value specied by an expression of the same type.
Many classical procedural languages (e.g. C) are less strict and treat functions as procedures with a return value.
8

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING


qcl> z=z^2; qcl> v=vector(cos(pi/6),sin(pi/6),0); qcl> v[2]=1; qcl> print z,v; : (0,1) [0.866025,0.5,1]

63

Assignments and atomic expressions are sucient to implement the class BF of basic functions (see 1.3.1.1). 2.3.4.2 Control-Statements

According to the principles of structured programming, ow-control is executed in terms of control-statements which execute blocks according to the value of a boolean expression and have well dened entry- and exit-points. The two basic control-structures are if-statements and (conditional) loops.9 Conditional loops dier in whether they evaluate the loop condition before (while-loop) or after (until-loop) the body. In the latter case, the body is executed at least once.

true cond

false

false cond true body

ifblock

elseblock

body

cond false true

ifstatement

whileloop

untilloop

Figure 2.5: Basic control structures

As conditional-loops allow the implementation of 0 -recursion, a programming language with while-loops, assignments, atomic expressions and the operators + and = is universal (see 1.3.1.1). Most structured languages also provide a special statement for counting loops over a known range of integers (for-loops).
Conceptually, a single block (sequence) can be regarded as a third control-structure [11].
9

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING


qcl> for i=a to b { body(i); }; qcl> i=a; while i<=b { body(i); i=i+1; } // for-loop and equivalent // while-loop

64

As counting loops allow the implementation of primitive recursion, a programming language with for-loops, assignments, atomic expressions and addition is sucient to implement the class PR of primitive recursive functions (see 1.3.1.1). Break Statements A break-statement allows the innermost loop to be immediately exited, regardless of the loop condition. While the possibility to exit a loop from any point within the body can be considered to be against the spirit of structured programming as it compromises the concept of well dened exit points, most structured languages do provide a break-statement (as does QCL). 2.3.4.3 Calls

A call of a procedure S binds a list of arguments to the parameters of S and executes the body of S . The parameter bindings as well as any symbol denitions within S are local. 2.3.4.4 Input and Output

Any programming language has to provide means to communicate with the outside world. This can be realized by designated input- and output-variables at the start and termination of the program or at runtime via I/O-commands.
qcl> input "length in cm:",x; ? length in cm: 192 qcl> print x,"cm =",x/2.54,"inches"; : 192 cm = 75.5906 inches

2.4

Elementary Quantum Operations

As introduced in 2.2.3, the computational model of quantum programming is a universal computer with a quantum oracle. Formally, the interface between the classical front-end and the quantum backend can be described by oracle commands which allow elementary gates to be applied to and measurements to be performed on arbitrary target qubits. In this section, we will show how this interface can be represented within the scope of an imperative (see 2.1.3.1) programming language (imperative quantum programming).10
10

Even if the classical language provides subroutines and structured ow-control, the

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

65

2.4.1

Quantum Registers

In 1.4.1.3, we dened a quantum register s as an arbitrary sub-system with a nite dimensional state space Hs and a well-dened computational basis Bs . We will also make the following assumptions about the quantum oracle Mq (see 2.2.3.2): The state space H of Mq is a composition of identical qubit subsystems, i.e. H = B n or H = B . Elementary gates and measurements operate on a nite number of qubits.11 All operations of Mq can be equally applied to arbitrary qubits. The qubits of Mq are numbered in ascending order starting with zero. The restriction to qubits implies that any register s is either a single qubit (s = qi ), a composition of single qubit registers (s = qk0 qk1 . . . qkn1 ) or the null-register (s = o). An n-qubit quantum computer thus allows for
n 1 n! = n! = e n! k=0 k ! k=0 (n k )! n

(2.20)

dierent registers.
n Notation We write Rk to denote the set of k -qubit registers for an n-qubit n quantum computer. As Rk is equivalent to the set of possible k -permutations of n elements, n! k n |R n | = Pk = . (2.21) (n k )!

Further, we dene Rk = Rn = R=
n n=1 Rk n n k=0 Rk n n=1 R

(set of k -qubit registers) (set of registers over n qubits) (set of general registers)

(2.22) (2.23) (2.24)

resulting QPL would still be regarded as imperative as it lacks semantic support for operators and quantum if-statements. 11 For convenience, QCL also supports a (global) reset-command (see 1.4.3.3).

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING 2.4.1.1 Language Representation of Registers

66

Within a structured QPL, quantum registers are instances of a quantum data type (see 2.4.1.3) Q.12 Semantically, an n-qubit register s Q is a nite sequence of mutually dierent qubit positions s = (s0 , s1 . . . sn1 ), so s does not denote the physical register itself, but serves as a pointer (logical register) to the actual n-qubit sub-system qs0 . . . qsn1 of Mq (physical register). This distinction allows registers to be treated just like any other classical data type, so register variables can be declared, printed and combined to expressions, just like classical variables.13
qcl> qureg q[1]; // qcl> qureg p[4]; // qcl> qureg qp = q & p; // qcl> print q,p,qp; // : <0> <1,2,3,4> <0,1,2,3,4> qcl> print p[0..2] & q; // : <1,2,3,0> allocate single qubit register allocate 4-qubit register declare combined register qp print register mappings registers can be combined to expressions

Registers are the formal interface between the classical front-end and the quantum oracle. All operations on the machine state take quantum registers as operands14 and are restricted to their corresponding sub-spaces.
qcl> H(q); // apply Hadamard gate to register q [5/32] 0.70711 |0,0> + 0.70711 |1,0> qcl> Not(p); // invert qubits in register p [5/32] 0.70711 |0,15> + 0.70711 |1,15> qcl> measure q; // measure register q [5/32] 1 |1,15>

2.4.1.2

The Quantum Heap

The mapping between logical registers and physical qubits is handled transparently by allocation and deallocation of registers from the set of all available qubits, also referred to as the quantum heap [46].15 Quantum registers are explicitely allocated, when a register variable is dened, but also implicitly
Since quantum data types are only used to restrict the possible operations on registers, mathematically, any quantum data type Q denotes the same set of qubit permutations, i.e. Q = R. 13 To allow for the static allocation of qubits, QCL does not allow register assignments, so in QCL, registers variables are treated as symbolic constants. 14 Depending on our point of view, registers can be thought of being passed by value (logical registers) or reference (physical registers). 15 In classical programming, the section of memory reserved for dynamic variables is called the heap.
12

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

67

for scratch space management (see 2.5.3.5) and for the evaluation of quantum conditions (see 2.6.3). Just as classical variables are initialized with type-dependent default values, newly allocated registers and consequently all free (i.e. unallocated) quantum memory has to be empty. Denition 30 (Empty Registers) A quantum register e is empty i the machine state | is of the form | = |0 e | e or, equivalently, e = |0 0|. Also, temporary registers must be emptied before deallocation, which, in the case of local register variables, takes place when the symbol leaves scope. This can be achieved either by measurement or by uncomputing (see 2.5.3.5). At startup, the whole machine state is empty, thus | = |0 .
qcl> dump; : STATE: 0 / 32 qubits allocated, 32 / 32 qubits free 1 |0>

The concept of the quantum heap allows for two important abstractions: Since the allocation of registers is transparent, no qubit positions need to be specied and no register literals need to be dened. This also leaves room for architecture-dependent optimizations. Since allocated and unallocated qubits are in a product state, the definition of quantum algorithms is independent of the total number of qubits. 2.4.1.3 Quantum Data Types

Dierent quantum data-types can be used to restrict the way unitary operators may aect quantum registers. This is not only done to prevent programming errors and to make the code more readable, but also to provide information to the compiler or interpreter to allow for more ecient optimizations and to designate the argument-, target- and scratch-registers of quantum functions (see 2.5.3.3), to allow for transparent scratch space management. General Registers impose no restrictions and can be used as arguments to arbitrary operators.

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING Type qureg quconst quvoid quscratch Restriction none invariant to all suboperators has to be empty when the uninverted operator is called has to be empty before and after the call Table 2.4: Quantum data types in QCL

68

Constant Registers must be invariant to all operators. They are used to designate argument registers of quantum functions (see 2.5.3.3). The enable registers of controlled gates (see 1.4.2.5) and conditional operators (see 2.6.1) are also of this data type. Denition 31 (Invariance of Registers) A quantum register c is invariant to a register operator U (s, c) i U |i s |j
c

= (Uj |i s ) |j

(2.25)

where Uj are arbitrary |s|-qubit unitary operators, so U can be written as


U =

U0 0 . . . 0

U1 .. .. . . 0 0 Un1

...

0 . . .

with

n = 2|c| .

(2.26)

An operator U of the above form is also called selection operator (see 2.5.2.2). Target Registers are used to designate the result register t for quantum functions (see 2.5.3.3) of the form F : |n c |0
t

|n c |f (n) t .

(2.27)

Since F |x, y is undened16 for y = 0, target registers need to be empty when F is called or else the eect of the operator is undened.
This is done in order to allow for dierent ways to accumulate the result, so F : |x, y |x, y f (x) and F : |x, y |x, (y + f (x)) mod 2n are merely considered to be dierent implementations of the same quantum function.
16

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

69

Scratch Registers are temporary registers which can be used within the implementation of an operator but are required to be empty before and after the call.17 2.4.1.4 Register Expressions

On an n-qubit quantum computer, a single k -qubit gate U can be used to n! implement up to (n dierent unitary transformation U (s) by applying U k)! to dierent registers. In order to realize all possible qubit permutations, a QPL has to provide means to extract subregisters and to combine disjoint registers. Denition 32 A register a = (a0 . . . an1 ) Rn is a subregister a b of b = (b0 . . . bm1 ) Rm i
n1 m1 i=0 j =0

ai = bj .

(2.28)

Denition 33 Two registers a = (a0 . . . an1 ) Rn and b = (b0 . . . bm1 ) Rm are disjoint i
n1 m1 i=0 j =0

ai = bj .

(2.29)

Table 2.5 shows the available operators in QCL to manipulate quantum registers. Expr. a a[i] a[i..j] a[i::l] a & b Description reference qubit sub-register sub-register concatenation Register a0 , a1 . . . an ai ai , ai+1 . . . aj ai , ai+1 . . . ai+l1 a0 , a1 . . . an , b0 , b1 . . . bm

Table 2.5: Register expressions in QCL

2.4.2

Elementary Gates

Just as assignments (see 2.3.4.1) represent atomic changes of the program state, elementary gates are the fundamental primitives to manipulate the quantum machine state.
In QCL, the type quscratch, when used for a local register, denotes managed scratch registers (see 2.5.3.5) which are uncomputed automatically, while a local qureg is an unmanaged scratch (see 2.5.2.4) register which has to be emptied by the operator itself.
17

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING 2.4.2.1 Register Operators

70

A k -qubit (elementary) gate (see 1.4.2.4) is an atomic unitary transformation U which can be applied to arbitrary k -qubit registers s Rk of the quantum oracle (see 2.2.3.2). The resulting transformation of the machine state is formally described by the register operator (see 1.4.1.3) U (s) = U I s =
i,j,k

uij |i s |k

j |s k | s

with uij = i|U |j .

(2.30)

In the circuit model, a register operator describes a gate which is wired to operate on certain qubits. In (2.30), the wiring is hidden in the basis decomposition | . . . s | . . . s , however it can be made explicit, by the introduction of a register-dependent reordering operator. Denition 34 (Reordering Operator) Let H = B n (or H = B ) and n s = (s0 . . . sk1 ) Rk (or Rk ) be a k -qubit register of H. A qubit permutation s |d0 , d1 . . . dn1 = |d0 , d1 . . . dn1 (2.31)

on H with being a bijective function on Zn (or N) and i = si for i < k is called reordering operator for s. The denition of s is not unique; for H = B n , there exist (n k )! i) dierent ( s and innitely many for H = B . Functionally, the reordering operator can be compared to the shift- and swap-operations used to address arbitrary tape-qubits on the SQT M (see 2.2.2.2). Denition 35 (Register Operator) The register operator U (s) for a k n qubit operator U and a k -qubit quantum register s Rk of H = B n (or s Rk of H = B ) is dened as U (s) = s (U I ) s on H. (2.32)

Fig. 2.6 shows a quantum circuit for U (s) on a 5-qubit machine with U being a 3-qubit gate and s = (0, 2, 3). 2.4.2.2 Language Representation of Gates

Since quantum programming is supposed to be a hardware independent formalism, besides the three basic requirements named in 2.2.3.2 (universality, functional equivalence of qubits, availability of adjoint gates), no assumptions can be made on the operators provided by the quantum oracle and there is no specic set of elementary gates.

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

71

s0

s0

U
s1 s2 s1 s2

Figure 2.6: Register operator U (s)

Consequently, any QPL has to allow the declaration of arbitrary elementary gates and the transparent replacement of gates by user-dened operators i.e. non-classical subroutines (see 2.5). Therefore, within a procedural QPL (see 2.5), elementary gates are treated as external subroutines with at least one parameter of a quantum data-type to indicate the register to operate upon.
extern operator H(qureg q); extern operator RotX(real theta,qureg q); extern qufunct CNOT(qureg q,quconst c); // Hadamard gate // X-Rotation // controlled-not

The calling syntax of gates is identical to procedures, except that calls can be inverted as for any gate U there also exists the gate U . In QCL, this is done by preceding the name of the gate the with the adjungation prex !.
qcl> qureg a[1]; qureg b[1]; // allocate 2 qubits qcl> H(a); // apply H to 1st qubit [2/32] 0.7071 |0,0> + 0.7071 |1,0> qcl> CNOT(b,a); // controlled-not [2/32] 0.7071 |0,0> + 0.7071 |1,1> qcl> RotX(pi/3,b); // rotate 2nd qubit by pi/3 [2/32] 0.6123 |0,0> - 0.3535i |1,0> - 0.3535i |0,1> + 0.6137 |1,1> qcl> !RotX(pi/3,b); // undo last operation [2/32] 0.7071 |0,0> + 0.7071 |1,1> // equiv. to RotX(-pi/3,b)

2.4.2.3

Operator Types

Besides quantum data types, procedural QPLs also provide dierent operator types which can be used for gates as well as non-classical subroutines (see 2.5). Generally, for any given gate U , the most restrictive operator type and the most restrictive register types which match the denition of U should be used, as restricted gates can be called by less restricted subroutines but not the other way around (see 2.5.1.1).

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

72

General Operators allow arbitrary unitary transformations on the argument registers.


extern operator RotX(real theta,qureg q); // X-Rotation

Basis Permutations are operators of the form F : |n |f (n) with f being a bijective boolean function.
extern qufunct CNOT(qureg q,quconst c); // controlled-not

Conditional Operators are a generalization of controlled gates (see 1.4.2.5) for enable registers of arbitrary size (including the null-register i.e. the uncontrolled operator). Denition 36 Let s, e R be disjoint registers and U (s) be a register operator, then the operator U[[e]] (s) : |k s |c
e

(U |k s )|c |k s |c e

if c = 111 . . . otherwise

(2.33)

is called conditional operator with the enable (or control) register e General operators as well as basis permutations can be declared conditional.
extern cond operator Phase(real phi); extern cond qufunct Not(qureg q); // conditional phase gate // conditional not gate

The enable register is passed as an implicit parameter if the operator is used within the body of a quantum if-statement (see 2.6.2.1), so operators without an explicit argument register can be useful (conditional phase gates).
qcl> qureg s[1]; qureg e[2]; // allocate 2 registers qcl> H(e); // create test state in e [3/32] 0.5 |0,0> + 0.5 |0,1> + 0.5 |0,2> + 0.5 |0,3> qcl> if e[0] { Phase(pi); } // flip sign if LSB of e is set [3/32] 0.5 |0,0> - 0.5 |0,1> + 0.5 |0,2> - 0.5 |0,3> qcl> if e { Not(s); } // invert target qubit s for e=11 [3/32] 0.5 |0,0> - 0.5 |0,1> + 0.5 |0,2> - 0.5 |1,3>

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

73

2.4.3
2.4.3.1

Measurements
Register Observable

In quantum mechanics, classical physical quantities (observables) are described by Hermitian operators (see 1.2.3.3). In quantum programming, we restrict measurements to the computational basis B = {|n }, expressed by the standard observable N = n n|n n|. Using the register basis Bs = {|k s } the register observable N (s) can be dened as (see 1.4.1.3) N (s) = Ns I s =
i,j

i |i s |j

i|s j | s.

(2.34)

Again, we can avoid the use of the register basis Bs by dening N (s) by means of a reordering operator s (see 2.4.2.1). Denition 37 (Register Observable) The register observable N (s) for a n k -qubit quantum register s Rk on H = B n (or s Rk on H = B ) is dened as N (s) = s
2k 1

(N I ) s

with

N=
i=0

i |i i |.

(2.35)

Further, we assume the basis-vectors are labeled as little-endian binary numbers (i.e. LSB rst), so for two disjoint registers a Rn and b Rm N (a b) = N (a) + 2n N (b). 2.4.3.2 Language Representation of Measurements (2.36)

Since QPLs are intended to control a classical computer which serves as a front-end between the user and the quantum oracle (see 2.2.3), quantum measurements are treated analogously to external classical input (see 2.3.4.4) and are implemented as a quantum input command with the side eect that the machine state gets reduced (see 1.2.3.3).
qcl> qureg q[8]; // allocate an 8-qubit register q qcl> int m; // declare a classical input variable qcl> H(q); // prepare an even superposition in q [8/32] 0.0625 |0> + ... + 0.0625 |255> (256 terms) qcl> measure q[0..5],m; // measure the first 6 qubits of q [8/32] 0.5 |50> + 0.5 |114> + 0.5 |178> + 0.5 |242> qcl> print m; // print measurement result : 50

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING 2.4.3.3 Initialization

74

Since many quantum algorithms are probabilistic (see 2.2.1.2) and involve iterating over the same computation until a solution is found, it is convenient to provide a command to reset the machine state | to the initial state |0 and empty all allocated registers, while not aecting the program state, so that variable bindings and register mappings remain valid.
[8/32] 0.5 |50> + 0.5 |114> + 0.5 |178> + 0.5 |242> qcl> reset; // reset quantum state as generated above [8/32] 1 |0> qcl> print q,m; // variables q and m are not affected : <0,1,2,3,4,5,6,7> 50

Since the initialization of | can be implemented by measuring all allocated registers and inverting qubits qi found in state qi = |1 1| to zero by applying the not-gate X (qi ), it is not necessary for the quantum oracle to provide a generic reset-command (see 1.4.3.3).
procedure resetregister(qureg int i; int m; for i=0 to #q-1 { measure q[i],m; if m==1 { Not(q[i]); } } } q) {

// iterate over qubits // measure i-th qubit // invert if measured 1

2.5

Operators

While registers, elementary gates and measurements are already sucient to implement arbitrary quantum algorithms, a programming language restricted to those concepts would not be much dierent from a classical language with a quantum device driver as it would lack a semantic representation for unitary operators, which are the essence of all quantum algorithms. In this section, we will demonstrate how unitary operators can be integrated into the framework of a procedural programming language (procedural quantum programming).

2.5.1

Quantum Subroutines

Procedural programming languages provide arbitrary levels of abstraction by allowing to group simple computational tasks into parameterized subroutines which can be used recursively as primitives for the denition of more complex subroutines.

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

75

Within a procedural QPL, unitary operators are represented as quantum subroutines which allow the recursive construction of complex quantum circuits from elementary gates. 2.5.1.1 Hierarchy of Subroutines

In addition to classical procedures and functions (see 2.3.3), we provide two quantum subroutine types (see 2.4.2.3) for general unitary operators and basis permutations. Both quantum subroutine types (which together we will refer to as operators) have mathematical semantics (see 2.5.1.2) and can be inverted to produce the adjoint operator. Quantum functions additionally allow the transparent use of (managed) scratch registers (see 2.5.3.5). Subroutine procedure general unitarian basis permutation functions QCL procedure operator qufunct return type S all none none none H all unitary rev. boolean none inv. no yes yes no scratch no no yes no

Table 2.6: Hierarchy of subroutines The 4 subroutine types form a call hierarchy, which means that a routine may only invoke subroutines of the same or a lower level. Table 2.6 lists the subroutines together with their QCL type, their allowed classical (S) and quantum (H) side eects, their invertibility and support for scratch space management. Deutschs Algorithm To illustrate the above concept, the following QCL implementation of Deutschs algorithm (see 1.5.2.2) uses all 4 subroutine types:
/* Define Oracle */ const coin1=(random()>=0.5); const coin2=(random()>=0.5); boolean g(boolean x) { if coin1 { return coin2; } else { return x xor coin2; } } // Define two random boolean // constants // Oracle function g // coin1=true -> g is constant // coin1=false -> g is balanced

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING


qufunct G(quconst x,quvoid y) { // Construct oracle op. G from g if g(false) xor g(true) { CNot(y,x); } if g(false) { Not(y); } } /* Deutschs Algorithm */ operator U(qureg x,qureg y) { H(x); G(x,y); H(x & y); } procedure deutsch() { qureg x[1]; qureg y[1]; int m; { reset; U(x,y); measure y,m; } until m==1; measure x,m; print "g(0) xor g(1) =",m; reset; } // Bundle all unitary operations // of the algorithm into one // operator U

76

// Classical control structure // allocate 2 qubits

// // // // // // // //

evaluation loop initialize machine state do unitary computation measure 2nd register value in 1st register valid? measure 1st register which contains g(0) xor g(1) clean up

2.5.1.2

Mathematical Semantics

In classical routines, subroutines are executed when they are called (linear execution) i.e. when control reaches the corresponding call-statement (see 2.3.4.3). Operators, however, support non-classical concepts like invertibility (see 2.5.2.3), scratch space management (see 2.5.3.5) and quantum if-statements (see 2.6.2.1) with the result that neither the number nor the execution order of suboperators necessarily corresponds to the classical ow of control (non-linear execution). Therefore operators have mathematical semantics i.e. their eect is completely described by the unitary transformation they implement as a function of their declared parameters; so they must be reproduceable and neither depend on nor exhibit side-eects on the program state. This specically excludes the use of global variables user input and classical random numbers measurements and resetting of the machine state calls of procedures

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING 2.5.1.3 Language Representation of Operators

77

Formally, just like a classical procedure, an operator is a named block (see 2.3.1) with a list of symbolic parameters of classical or quantum types. The latter are used to indicate the registers the operator is applied to. The QCL operator below implements the basis permutation xregb (a) : |x
qufunct xreg(qureg a,int b) { int i; for i=0 to #a-1 { if bit(b,i) { Not(a[i]); } } }
a

|x b

(2.37)

// xor reg. a with binary number b // iterate over qubits of a // invert i-th qubit of a if // i-th bit of b is set

The calling syntax of operators is identical to gates, including the ability to call the adjoint operator. Since operators have mathematical semantics, an operator is completely equivalent to an elementary gate with the same declaration and functionality. So it makes e.g. no dierence whether the Z -gate (see 1.4.2.4) is provided by the quantum oracle
extern operator Z(qureg q); // pi-rotation about the Z-axis

or implemented as the sequence Z = HXH by the operator


operator Z(qureg q) { H(q); Not(q); H(q); }

2.5.1.4

Polymorphism

On an n-qubit quantum computer, an k -qubit gate can implement up to n! n unitary transformations U (s) with s Rk . In procedural quantum (nk)! programming, this polymorphism is further extended by additional abstractions: Register Size: Argument registers can be of dierent sizes, so for every quantum parameter s, the register size |s| is passed as an implicit classical parameter. In QCL, the size of a register is given by the sizeoperator #, so #s = |s|. An operator with a single argument register s Rn can implement up to e n! dierent unitary transformations. Multiple Registers: Operators can take multiple argument regisq 1 ters. Since for any s Rk , there exist k+ possible decompositions q 1 s = s1 . . . sq (including null-registers), an operator with p register arguments si with the total size k = q i=1 |si | can implement up to k +q 1 dierent unitary gates. q 1

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

78

Classical Parameters: Besides argument registers, operators can also take an arbitrary number of classical parameters. Any classical parameter of type T increases the number of possible unitary operations by the factor |T |. Generally, on an n-qubit quantum computer, an operator with q register arguments and p classical parameters of the datatypes Ti can implement up to n n! k+q1 p 1+ |T i | (2.38) q1 i=1 k=1 (n k )! dierent unitary transformations.

2.5.2
2.5.2.1

General Operators
Quantum Circuits

General operators can be regarded as procedural descriptions of quantum circuits depending on the size of their argument registers as well as classical parameters. Fig. 2.7 shows the 4-qubit quantum circuit which is generated by the following QCL implementation of the discrete Fourier transform DFT : |x
q

1 N

N 1 y =0

eN

2 i

xy

|y

with N = 2|q|

(2.39)

using an FFT-like algorithm suggested by Coppersmith [22].


operator dft(qureg q) { // Discrete Fourier transform const n=#q; // set n to length of input int i; int j; for i=1 to n { for j=1 to i-1 { // apply conditional phase gates V(pi/2^(i-j),q[n-i] & q[n-j]); } H(q[n-i]); // Hadamard gate } flip(q); // swap qubit order of the output }

Since an operator may call other suboperators, this descriptions can be nested. The above implementation of the DFT e.g. uses the suboperator flip : |d0 d1 . . . dn1 |dn1 . . . d1 d0 to generate the last two Swap-gates in g. 2.7. (2.40)

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

79

/8 /4 /2 /2

/4

/2

Figure 2.7: Quantum Fourier Transform for a 4-qubit register

qufunct flip(qureg q) { int i; for i=0 to #q/2-1 { Swap(q[i],q[#q-i-1]); } }

// Invert order of qubits

// swap 2 opposite qubits

The implementation of an operator may also contain calls to itself, which results in a recursive denition of the resulting circuit. The QCL operator below is a recursive implementation of the phase transformation P (s) : |k
s

eik |k s .

(2.41)

using the conditional phase gate V () = C n [ei ], which for n = 1 is equivalent to Rz () = ei/2 |0 0| + ei/2 |1 1|.
operator P(qureg q,real phi) { V(phi,q[0]); if #q>1 { P(q[1..#q-1],2*phi); } } // // // // Phase transformation rotate LSB if there are higher qubits call P with phase 2*phi

Fig. 2.8 shows how the resulting circuit is constructed recursively for a 4-qubit register q = q0 q1 q2 q3 . 2.5.2.2 Parameter Types

Besides general registers (QCL type qureg) which allow arbitrary quantum circuits, more restrictive quantum data types (see 2.4.1.3) can be used as parameter registers to indicate that an operator U belongs to a certain class of unitary transformations.

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

80

q0 q1 q2 q3

q0 q1 q2 q3

()

(2)

2 4

2 4 8

(4)

(8)

Figure 2.8: Recursive phase transformation

Constant Parameters A constant register c (QCL type quconst) indicates that c is invariant to the operator, i.e. that U is a selection operator of the form (|k c k |c ) ukij |i j |. (2.42) U=
k ij

Denition 38 An (n + m)-qubit operator U of the form U = diag (U0 , U1 . . . U2m 1 ) with Uk SU (2n ) and m > 0 is called selection operator. To enforce that restriction, within the implementation of U , c may only be used as constant argument to any suboperators. So the following implementation of the Z -gate is illegal, despite the fact that Z = HXH = |0 is of the form (2.42).
qcl> operator z(quconst c) { H(c); X(c); H(c); } ! in operator z(quconst c) at "H(c); ": ! parameter mismatch: quconst used as non-const argument to H
c

(2.43)

0 |c | 1

1|c

(2.44)

In order to allow the declaration of a constant parameter register c even when c is used as general register argument to suboperators, in QCL, the above restriction can be circumvented by redeclaring c as qureg. It is then in the responsibility of the programmer to ensure proper quconst-semantics (see also 2.5.3.2).
operator z(quconst c) { qureg q = c; H(q); X(q); H(q); } // // // // // // Correct implementation of the Z-gate redeclare c as qureg transform into dual basis X-rotation in dual basis transform back into computational basis

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

81

Target Parameters A target register t (QCL type quvoid) is expected to be empty when the un-inverted operator is called, so U |k t | is undened for k = 0.18 Therefore, two operators U (1) and U (2) with a target register t are considered to be equivalent i U (1) |0 t |
t

= U (2) |0 t |

for all | H t.

(2.45)

While target registers are usually used as result registers for quantum functions (see 2.5.3.3), they can also be used for general operators:
operator prepare(quvoid t) { H(t); P(t,2*pi/2^#t); } // Prepare test state for empty t // produce even superposition // phase transformation

The QCL operator above expects an empty register t to prepare a test state of the form

| = 2

|t|/2

2|t| 1 k=0

21|t| ik

|k t |

(2.46)

using the phase transformation P () (2.41) from 2.5.2.1.


qcl> qureg q[5]; // allocate empty 5-qubit register qcl> prepare(q); // prepare test state [5/32] 0.17678 |0> + ... + (0.17338-0.034487i) |31> (32 terms) qcl> plot; // plot simulated state

Fig. 2.9 shows the 5-qubit test state


31

| =
k=0

c k |k

1 with ck = eik/16 32

(2.47)

as prepared above. Dots () and crosses () denote the real and imaginary parts ( (ck ) and (ck )) and the length of the vertical lines the absolute value (|ck |) of the complex amplitudes ck .
Of course, U (t, s) does have a deterministic eect for t = |0 0|, just that this eect is not part of the operators declared semantics and is considered to be an implementation detail.
18

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

82

5 used qubits, 32 basevectors

Figure 2.9: 5-qubit test state

Scratch Parameters A scratch register s (QCL type quscratch) is expected to be empty before and after the operator is called, so U has has to be of the form
2|s| 1

U = ( |0

0|s )
ij

uij |i j | +
k,l=1 ij

uklij |k s |i l|s j |

(2.48)

Scratch parameters (also called explicit scratch registers), have the same semantics as unmanaged (see 2.5.2.4) local scratch variables (QCL-type qureg), except that they are provided as parameters instead of getting allocated from the quantum heap (see 2.4.1.2). Explicit scratch parameters can be useful for suboperators as they allow to temporarily use target registers, or other registers that are known to be empty at some point during a computation, as scratch to save quantum memory. 2.5.2.3 Inverse Operators

Unlike procedures, operator calls can be inverted. In QCL, this is done by preceding the operator name with the adjungation prex !. The adjoint operator of a composition of unitary operators is19
n n

Ui
i=1
19 n i=1

=
i=1

Un +1i

(2.49)

To avoid ambiguities with non-commutative matrix products, we use the convention fi = fn fn1 . . . f1

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

83

In the circuit model, the inverse operator is simply the execution of the circuit from right to left, whereby gates are replaced by their adjoint gates.
qcl> qureg q[3]; qcl> H(q[1]); [3/32] 0.70711 |0> + 0.70711 |2> qcl> set log 1; qcl> dft(q);
@ @ @ @ @ @ @ H(qureg q=<2>) V(real phi=1.5708,quconst q=<1,2>) H(qureg q=<1>) V(real phi=0.785398,quconst q=<0,2>) V(real phi=1.5708,quconst q=<0,1>) H(qureg q=<0>) Swap(qureg a=<0>,qureg b=<2>)
0 1 2

// allocate 3-qubit register // prepare test state // show gate sequence // quantum Fourier transform
/4 /2 /2

0 1 2

[3/32] 0.5 |0> + (0.25+0.25i) |1> + (0.25-0.25i) |3> + 0.5 |4> + (0.25+0.25i) |5> + (0.25-0.25i) |7> qcl> !dft(q); // inverse transform
@ @ @ @ @ @ @ !Swap(qureg a=<0>,qureg b=<2>) !H(qureg q=<0>) !V(real phi=1.5708,quconst q=<0,1>) !V(real phi=0.785398,quconst q=<0,2>) !H(qureg q=<1>) !V(real phi=1.5708,quconst q=<1,2>) !H(qureg q=<2>)
0 1 2

/2 /4

/2

[3/32] 0.70711 |0> + 0.70711 |2>

Delayed Execution Since the sequence of applied suboperators is specied using a procedural language which cannot be executed in reverse, the adjungation is is achieved by the delayed execution of suboperator calls. Whenever a suboperator call is encountered during the execution of an inverted operator, the name of the suboperator and its evaluated arguments are pushed on the execution stack. Afterwards, the stacked suboperator calls are processed in reverse order. Thereby, normal calls are replaced by inverted calls and vice-versa. 2.5.2.4 Scratch Registers

Let U be an arbitrary k -qubit unitary operator on B k . While any universal set of gates G = {G1 , G2 . . .} allows the direct implementation of U as U (1) =
i

Gni (si ) = U

with si Rk ,

(2.50)

the implementation of a (k + s)-qubit operator U (2) =


j

Gnj (sj ) with sj Rk+s

(2.51)

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING such that U (2) | |0 = (U | ) |0 for all | B k

84

(2.52)

may be considerably more ecient. In quantum programming, we consider U (1) and U (2) as equivalent and refer to U (2) as an implementation of U with s scratch qubits. Formally, a scratch register is a local quantum variable dened within the body of an operator denition. As all other quantum variables, local registers are empty on allocation (see 2.4.1.2). While, in contrast to quantum functions (see 2.5.3.3), for general operators there is no way to automatically cleanup scratch registers, the implementation itself has to assure that a local register s is empty (i.e. in the state s = |0 0|) after the call (unmanaged scratch space). In QCL, unmanaged scratch registers are local variables of type qureg. The operator below uses a scratch qubit to implement the conditional phase gate ei |k q if k = 2|q| 1 (2.53) V () : |k q |k q otherwise using the single qubit Z-rotation Rz (see 1.4.2.2) and the generalized CNotgate CNot(q, p) = Not[[p]] (q) = C |p| [X |q| ](q, p) (2.54)
operator cphase(real phi,quconst q) { qureg s[1]; CNot(s,q); RotZ(phi,s); CNot(s,q); } // // // // // Conditional phase gate single scratch qubit s=1 if q=111... add phase if s=1 restore scratch qubit

For an n-qubit argument register cphase() can be written as the (n +1)qubit matrix cphase() = ei 2 C n [ei ] 0 i 2 0 e C n [ei ]

(2.55)

on B n+1 and implements V (, q) up to an irrelevant global phase as cphase() | q |0


s

= ei/2 V ()|

|0 s .

(2.56)

qcl> qureg q[2]; // allocate 2 qubits qcl> H(q); // prepare test state [2/32] 0.5 |0> + 0.5 |1> + 0.5 |2> + 0.5 |3> qcl> cphase(pi,q); // rotate phase for |3> [2/32] -0.5i |0> - 0.5i |1> - 0.5i |2> + 0.5i |3>

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

85

2.5.3

Basis Permutations

Because of the linearity of unitary transformations, an operator applied to a superposition state | is simultaneously applied to all basis vectors that constitute | since U c i |i = ci (U |i ). (2.57)
i i

This feature is called quantum parallelism and is exploited in most nonclassical algorithms (see 1.5.2.4). Often U implements a reversible boolean, or, equivalently, a bijective integer function, by treating the basis vectors merely as bitstrings or binary numbers. Denition 39 An n-qubit basis permutation is a unitary operator of the form F : |k |f (k ) with f being a bijective function (i.e. a permutation) on Z2n (or Bn ). 2.5.3.1 Reversible Boolean Networks

While general operators implement quantum circuits, basis permutations are procedural descriptions of reversible boolean networks operating on qubits. This allows us to discuss computations on qubits analogously to classical bits, so we can e.g. describe the eect of the controlled-not operation C [X ](a, b) as if qubit b is set then invert qubit a. The set L = {X, C [X ], C 2 [X ]} is universal for basis permutations.20 The gates in L can be generalized to the CNot-gate (2.54) which operates on argument and target registers of arbitrary sizes. In QCL, basis permutations are represented by the subroutine type qufunct. Fig. 2.10 shows the 4-qubit circuit generated by the operator
qufunct inc(qureg x) { int i; for i = #x-1 to 0 step -1 { CNot(x[i],x[0::i]); } } // Increment register

// apply controlled-not // from MSB to LSB

which implements the basis vector incrementation inc : |k


20

|(k + 1) mod 2|x| x .

(2.58)

In fact, the Tooli gate T = C 2 [X ] alone would be universal if two additional constant qubits in the state |1 are available.

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

86

0 1 2 3

0 1 2 3

Figure 2.10: Increment operator


qcl> qureg q[8]; qcl> H(q[2]&q[5]); CNot(q[0],q[2]); [6/32] 0.5 |0> + 0.5 |5> + 0.5 |32> qcl> inc(q); [6/32] 0.5 |1> + 0.5 |6> + 0.5 |33> qcl> inc(q); [6/32] 0.5 |2> + 0.5 |7> + 0.5 |34> qcl> !inc(q); [6/32] 0.5 |1> + 0.5 |6> + 0.5 |33> // allocate quantum byte // prepare test state + 0.5 |37> // increment basis vectors + 0.5 |38> + 0.5 |39> // decrement basis vectors + 0.5 |38>

2.5.3.2

Non-Boolean Factorizations

According to the hierarchy of subroutines (see 2.5.1.1), any routine can only call subroutines of the same or a more restricted type. This especially means that basis permutations may not call general operators or gates within their denition. However, there are universal sets of gates G where L G. One example would be the standard set (see 1.4.2.6) {H, S, T, C [X ]} which lacks the Not- and the Tooli-gate. A procedural QPL therefore has to provide a way to circumvent the hierarchy of subroutines in order to dene non-boolean implementations for elementary basis permutations. In QCL this can be achieved by a double declaration, e.g.
qufunct operator Not(qureg q) { int i; for i=0 to #q-1 { H(q[i]); S(q[i]); S(q[i]); H(q[i]); } } // Standard set implementation // of the generalized Not-gate // for all qubits: // transform into dual basis // S^2 = Z = |0><0|-|1><1| // transform back into // computational basis

2.5.3.3

Quantum Functions

One obvious problem in quantum computing is its restriction to reversible computations. Consider a simple operation like counting the number of set

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING qubits in a register q bitcount : |k


q

87

|b(k )

with

(2.59) (2.60)

b(n) = n mod 2 + b( n/2 ) and b(0) = 0.

Clearly, this operation is non-reversible since bitcount |2n = 1 for all n < |q|, so bitcount is not an unitary operator. However, if we use an additional register p with 2|p| > |q|, then we can always nd a unitary operator bitcount such that bitcount : |k q |0
p

|k q |b(k )

(2.61)

Denition 40 Let c Rn and t Rm be disjoint registers and f be an arbitrary function f : Z2n Z2m (or f : Bn Bm ). A unitary operator F of the form F : |x c |y
t

|x c |g (f (x), y )

with

g (z, 0) = z

(2.62)

is referred to as quantum function implementing f with the accumulation function g . Since F is required to be unitary, each hz : y g (z, y ) must be a 2m permutation with hz (0) = z . So for any given function f : Z2n Z2m there exist 2n (2m 1)! dierent quantum functions F (k) which implement f . In procedural quantum programming, two quantum functions F (1) and F (2) are considered equivalent when they implement the same function, so F |x t |y t is undened for y = 0 which makes the actual denition of g an irrelevant implementation detail.21 The above notion of equivalence implies that t is a target register (see 2.5.2.2). Also, from the form of (2.62) it follows that c is a constant register. Using the QCL register types quconst and quvoid, we can implement bitcount as the quantum function
qufunct bitcount(quconst q,quvoid p) { int i; int j; if #q>=2^#p { exit "target register too small"; } for i=0 to #q-1 { for j=#p-1 to 0 step -1 { CNot(p[j],p[0::j] & q[i]); } } }
21

// count set bits in q // make sure that p is wide // enough // iterate over qubits in q // increment p if q[i] // is set

This abstraction is necessary as the use of scratch registers (see 2.5.3.5) can aect g .

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING with the accumulation function g (z, y ) = (z + y ) mod 2|p| , so bitcount : |x q |y
qcl> qureg q[3]; qureg p[2]; qcl> H(q); [5/32] 0.3535 |0,0> + 0.3535 0.3535 |4,0> + 0.3535 qcl> bitcount(q,p); [5/32] 0.3535 |0,0> + 0.3535 0.3535 |3,2> + 0.3535
p

88

|x q |(y + b(k )) mod 2|p| p .

(2.63)

// allocate argument and target reg. // prepare superposition |1,0> + 0.3535 |2,0> + 0.3535 |3,0> + |5,0> + 0.3535 |6,0> + 0.3535 |7,0> // count set qubits |1,1> + 0.3535 |2,1> + 0.3535 |4,1> + |5,2> + 0.3535 |6,2> + 0.3535 |7,3>

2.5.3.4

Junk Registers

While quantum functions can be used to work around the reversible nature of quantum computing, the necessity to keep a copy of the argument is a problem, as longer computations will leave registers lled with intermediate results. Suppose we want to compare the number of set qubits in two registers a and b, i.e. nd a quantum function to implement the predicate c(x, y ) = 1 if b(x) = b(y ) 0 otherwise . (2.64)

Using an auxiliary register s and B = bitcount from (2.63), the operator bitcmp0(a, b, t, s) = CNot(t, s) Not(s) B (b, s) B (a, s) implements c but leaves the register s in a dirty state: |x, y, 0, 0 |x, y, b(x), 0 |x, y, 11...1 (b(x) b(y )), 0
B (a,s) B (b,s)

(2.65)

|x, y, b(x) b(y ), 0

Not(s)

(2.66)

CNot(t,s)

|x, y, 11...1 (b(x) b(y )), c(x, y )

qufunct bitcmp0(quconst a,quconst b,quvoid t,quvoid s) { bitcount(a,s); // write #bits(a) to s !bitcount(b,s); // subtract b(b) from s Not(s); // invert s CNot(t,s); // t=1 if #bits(a)-#bits(b)=0 }

Since s does not contain useful information and is usually entangled (i.e. tr(2 s ) < 1) so that it cannot be reset by measurement (see 2.4.3.3) without aecting other registers, s is called a junk register. A quantum function F : |x, 0 |0
s

|x, f (x) |j (x)

(2.67)

with a junk parameter s is called a dirty implementation of f .

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING


qcl> qureg a[3];qureg b[3]; // allocate registers a,b,s,t qcl> qureg s[2];qureg t[1]; qcl> H(a[0]); Not(a[2]); // prepare test state for a [9/32] 0.70711 |4,0,0,0> + 0.70711 |5,0,0,0> qcl> H(b[1]); Not(b[0]); // prepare test state for b [9/32] 0.5 |4,1,0,0> + 0.5 |5,1,0,0> + 0.5 |4,3,0,0> + 0.5 |5,3,0,0> qcl> bitcmp(a,b,t,s); // compare number of bits [9/32] 0.5 |4,3,0,0> + 0.5 |5,1,2,0> + 0.5 |4,1,3,1> + 0.5 |5,3,3,1>

89

2.5.3.5

Scratch Space Management

Let F be a a dirty implementation of a classical function f with the argument register x, the target register y and the junk register s F : |k x |0 y |0
s

|k x |f (k ) y |j (k )

(2.68)

While computing f (k ), F also lls s with the temporary junk bits j (k ). To reclaim s, Bennett proposed the following method [7], which is known as uncomputing: 1. Allocate an (empty) auxiliary register t of the same size as y. 2. Replace F (x, y, s) by the operator F (x, y, s, t) = F (x, t, s) Fanout(t, y) F (x, t, s). (2.69)

The above procedure restores both the junk and the auxiliary register so s and t are scratch parameters (see 2.5.2.2) of F so F is a clean implementation of f with |s| + |t| scratch qubits (see 2.5.2.4): |k, 0, 0, 0
F (x,t,s)

|k, 0, j (k ), f (k )
F (x,t,s)

Fanout(t,y)

|k, f (k ), j (k ), f (k )

|k, f (k ), 0, 0

(2.70)

The Fanout Operator The Fanout operator is a quantum function implementing the identity i.e. Fanout : |x a |0
b

| x a |x

with |a| = |b|

(2.71)

and is usually realized using |x| controlled-not gates with the accumulation function gF (x, y ) = x y .
cond qufunct Fanout(quconst a,quvoid b) { int i; if #a!=#b { exit "fanout arguments must be of equal size"; } for i=0 to #a-1 { CNot(b[i],a[i]); } }

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

90

Let F be a dirty implementation of f with the accumulation function g and a junk register s, so F : |x x |y y |0
s

|x x |g (f (x), y ) y |j (x, y ) s .

(2.72)

The clean version F according to (2.69) with the scratch registers s and t is given as F : |x x |y y |0 s |0
t

|x x |gF (f (x), y ) y |0 s |0 t .

(2.73)

So in F the original accumulation function g is replaced by the accumulation function gF of the Fanout operator. Scratch Registers In procedural quantum programming, the method of uncomputing (2.69) allows the automatically reclaiming of local registers which are left in a non-empty state after the body of the operator has been executed (managed scratch registers). In QCL managed scratch registers are local variables of type quscratch. Since the method of uncomputing only works for quantum functions, managed scratch registers are restricted to basis permutations with constant and target registers as quantum parameters. For bitcmp0 from (see 2.5.3.4), a clean version can be constructed by making s a managed local scratch register
qufunct bitcmp(quconst a,quconst b,quvoid t) { quscratch s[ceil(log(max(#a,#b)+0.5,2))]; bitcmp0 (a,b,t,s); }

which is not only equivalent to but also implements the same unitary transformation22 as the following operator using two unmanaged scratch registers (see 2.5.2.4).
qufunct bitcmp(quconst a,quconst b,quvoid t) { qureg s[ceil(log(max(#a,#b)+0.5,2))]; qureg u[#t]; bitcmp0 (a,b,u,s); CNot(t,u); !bitcmp0 (a,b,u,s); } Note that for single qubits, every possible implementation of the Fanout operator is identical to a CNot gate.
22

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING 2.5.3.6 Calling Semantics

91

The potential use of managed scratch registers extends the calling semantics of basis permutations as compared to general operators. Consider the quantum functions
qufunct U1(quconst x,quvoid y,quvoid s) { A(x,y,s); B(x,y,s) } qufunct U2(quconst x,quvoid y) { quscratch s; A(x,y,s); B(x,y,s) } // s is junk register

// s is scratch register

While the calls U1(x,y,s) and !U1(x,y,s) are analogous to general operator calls i.e.
(x, y, s) = A (x, y, s) B (x, y, s) U1 (x, y, s) = B (x, y, s) A(x, y, s) and U1 (2.74) the calls U2(x,y,s) and !U2(x,y,s) cause the transparent allocation of a register t, with |t| = |y| and results in the transformations (x, t, s) Fanout(t, y) U1 (x, t, s) and U2 (x, y, s, t) = U1 U2 (x, y, s, t) = U1 (x, t, s) Fanout (t, y) U1 (x, t, s).

(2.75) (2.76)

if a Hermitian are almost identical and that U2 = U2 Note that U2 and U2 implementation of the Fanout operator is used.

Double Execution When a general operator or basis permutation without managed scratch registers is executed, calls of suboperators are either processed immediately (linear execution) or pushed on the execution stack (delayed execution), depending on whether the normal or the inverse operator has been called (see 2.5.2.3). In the case of a quantum function with managed scratch registers, after the initial remapping of the target registers, suboperator calls always get executed and pushed on the execution stack (double execution). After the body of the subroutine has been processed, the appropriate Fanout (normal call) or Fanout (inverted call) operation is executed and then the stacked calls get executed again in reverse order with their adjungation ags inverted.

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

92

2.6

Quantum Flow Control

All classical programming languages, in one way or another, allow the conditional execution of code depending on a boolean variable or logical expression (conditional branching). In structured languages conditional branching is realized by if-statements with well dened entry and exit points. While the unobservability of qubits forbids a direct implementation, we will show how conditional operators can be used to emulate conditional branching on qubits (quantum if-statement) as well as (bounded) conditional loops with quantum termination conditions to the eect that, with regard to ow-control, qubits can be treated almost equivalently to classical bits (structured quantum programming). We will further demonstrate how qubits and registers can be combined to arbitrary boolean expressions and complex predicates (quantum conditions).

2.6.1

Conditional Operators

As already mentioned in 2.4.2.3, for a unitary register operator U (s) and an enable (or control) register e R disjoint to s, the conditional operator U[[e]] (s) is dened as U[[e]] (s) : |k s |c
e

(U |k s )|c |k s |c e

if c = 111 . . . otherwise .

(2.77)

If e is a qubit so |e| = 1, then informally, we can describe the eect of U[[e]] (s) as if e is set then apply U (s) which relates to the fact that
2|s| 1 2|s| 1 2|s| 1

U[[e]]
k=0 bB

ckb |k s |b

=
k=0

ckb |k s |0

+
k=0

ckb (U |k s ) |1 e .

(2.78)

2.6.1.1

Properties of Conditional Operators

Orthogonal Enable Registers Let U, V SU (2n ) be n-qubit unitary operators, s Rn a n-qubit register and q R1 a qubit disjoint to s (i.e. q s), then U[[q]] (s) X (q), V[[q]] (s) X (q) = 0. (2.79) For arbitrary register operators U (x) and V (y), (2.79) can be generalized to U[[e]] (x) X n (c), V[[e]] (y) X n (c) = 0 . with c Rn being an non-empty subregister c e and x, y e (2.80)

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

93

Conditional Decomposition Its easy to verify that for an arbitrary U (s) and a qubit q R1 , q s U (s) = U[[q]] (s) X (q) U[[q]] (s) X (q). (2.81)

Denition 41 An n-qubit basis permutation P : |k |p(k ) is called cyclic i it generates the computational basis B such that for any |k B
2n 1 i=0

P i |k = {|j | j Z2n } = B

(2.82)

p is therefore a permutation on Z2n with the cycle length 2n . If P is cyclic, then A P A is also cyclic for an arbitrary basis permutations A. An example for a cyclic basis permutation is the operator inc (2.58) from 2.5.3.1. We can now generalize the identity (2.81) for enable registers e Rn , e s to 2n U (s) = U[[e]] (s) P (e) (2.83) with P being an arbitrary n-qubit cyclic basis permutation. Conditional Composition Let V, W SU (2n ) be n-qubit unitary operators, then the selection operator U= can be implemented as U (s, c) = X (c) V[[c]] (s) X (c) W[[c]] (s) Likewise, for any (n + m)-qubit selection operator U = diag (U0 , U1 . . . U2m 1 ) there exists an cyclic m-qubit basis permutation P such that
2m 1

V 0 0 W

(2.84)

(2.85)

(2.86)

U (s, c) =
i=0

Ui [[c]] (s) P (c)

(2.87)

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING 2.6.1.2 Conditional Subroutines

94

As conditional operators are just a special case of selection operators (see 2.5.2.2), they can be implemented as quantum subroutines with a constant enable register. The operator below implements a conditional version cinc of the register increment operator (2.58) dened as cinc : |k x |c
e

|(k + 1) mod 2|x| x |c |(k x |c e

if c = 11...1 otherwise

(2.88)

qufunct cinc(qureg x,quconst e) { int i; for i = #x-1 to 0 step -1 { CNot(x[i],x[0::i] & e); } }

// Conditional increment // as selection // operator

The above operator is a conditional version of (2.58) cinc : |k x |c


e

|(k + 1) mod 2|x| x |c |(k x |c e

if c = 11...1 otherwise

(2.89)

Structured QPLs directly support conditional execution, i.e. the automatic construction of U[[e]] from a given implementation of U , by allowing the explicit declaration of conditional subroutines. In QCL, conditional operators can be dened by prexing the subroutine declaration with the keyword cond.
cond qufunct inc(qureg x) { int i; for i = #x-1 to 0 step -1 { CNot(x[i],x[0::i]); } } // Conditional increment register // as conditional // subroutine

Conditional Calls The enable register e is an implicit constant parameter and not part of the parameter declaration of a conditional subroutine. Instead, e is set by a quantum if-statement (see 2.6.2.1) and transparently passed on to all suboperators, which therefore are also required to be either conditional operators or conditional elementary gates (see 2.4.2.3).

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING


qcl> qureg q[4];qureg e[1]; qcl> H(q[3] & e); [5/32] 0.5 |0,0> + 0.5 |8,0> qcl> cinc(q,e); [5/32] 0.5 |0,0> + 0.5 |8,0> qcl> if e { inc(q); } [5/32] 0.5 |0,0> + 0.5 |8,0> qcl> !cinc(q,e); [5/32] 0.5 |0,0> + 0.5 |8,0> qcl> if e { !inc(q); } [5/32] 0.5 |0,0> + 0.5 |8,0> // allocate counting and control reg. // prepare test state + 0.5 |0,1> + 0.5 |8,1> // conditional increment + 0.5 |1,1> + 0.5 |9,1> // equivalent to cinc(q,e) + 0.5 |2,1> + 0.5 |10,1> // conditional decrement + 0.5 |1,1> + 0.5 |9,1> // equivalent to cinc(q,e); + 0.5 |0,1> + 0.5 |8,1>

95

In the example above, the if-statement if e { inc(q); } causes the control qubit e to be appended to the enable registers of the CNot-gates generated by the call inc(q), so if e { inc(q); } and cinc(q,e) describe the same quantum circuit (g. 2.11).
e q0 q1 q2 q3 e q0 q1 q2 q3

Figure 2.11: Conditional increment operator

Unconditional Calls If a conditional subroutine is called outside of a quantum if-statement23 then e is empty (unconditional call) and call semantics are identical to unconditional subroutines.
qcl> inc(q); // unconditional increment [4/32] 0.5 |1,0> + 0.5 |9,0> + 0.5 |1,1> + 0.5 |9,1>

Since the declaration of an operator as conditional does not incur any overhead on unconditional calls, it is reasonable to always declare routines as conditional if all required suboperators are available as conditional subroutines or gates.
23

In the case of a suboperator, this also includes all parent operators

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING 2.6.1.3 Call Graph of Subroutines

96

The hierarchy of subroutines (see 2.5.1.1) implies that routines may only call subroutines of the same or a more restricted type. If, in addition to that, we also take into account that conditional operators cannot call unconditional suboperators, the hierarchy becomes a lattice, which can be represented by the call graph depicted in g. 2.12.

procedure

operator

cond operator

qufunct

cond qufunct

functions

Figure 2.12: Call graph of QCL subroutines

2.6.1.4

Explicit Enable Registers

Conditional operators, by denition, must be able to handle enable registers of arbitrary sizes. As structured quantum programming is supposed to be a hardware independent formalism, it cannot be assumed that the quantum oracle provides a universal set of conditional gates. In fact, most standard gates do not operate on more than three qubits (see 1.4.2.4). A structured QPL therefore has to allow for the implementation of basic conditional operators using unconditional suboperators and gates and consequently has to provide a means to make the enable register available as a symbolic quantum constant (explicit enable register). In QCL this is achieved by redeclaring the enable register as local register of type quconst. The example below is the QCL default implementation of the Not-gate:

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING


extern qufunct NOT(qureg q); extern qufunct CNOT(qureg q,quconst c); cond qufunct Not(qureg q) { quconst e = cond; if #e>0 { CNOT(q,e); } else { NOT(q); } } // unconditional Not // unconditional CNot // conditional Not // e=enable register //

97

2.6.2
2.6.2.1

Conditional Branching
Quantum If-Statements

In classical structured programming languages, the conditional execution of a statement sequence (block) depending on the value of a boolean variable p is expressed by an if-statement S of the form if p then 1 , 2 . . . n endif or, if one of two blocks and should be executed, if p then 1 , 2 . . . n else 1 , 2 . . . m endif If the instead of a classical boolean variable, the if-condition is a qubit p if p then 1 , 2 . . . n else 1 , 2 . . . m endif then S is referred to as quantum if-statement (QIS). S is called (i) (ii) (iii) (iv) (v) invalid i or contain24 procedure calls, measurements, reset or input commands or use random numbers. nested i or contain other quantum if-statements. dirty i or contain assignments or break-statements. clean i S is valid and not dirty. simple i S is clean and = () i.e. no else-branch is dened.

Block and Tail Operators Like the body of an operator subroutine, the if- and else-branches and are basically procedural descriptions of quantum circuits, or more precisely, of sequences of suboperators which get generated by linear execution. However, unlike a subroutine, ( ) has no declared interface, so its parameters are all classical and quantum variables that are visible in the current scope. Also, ( ) is not guaranteed to have mathematical semantics (dirty QIS).
24

This also includes any sub-statements in control structures.

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

98

We will write ( ) to denote the composition of suboperators (blockoperator ) which would be generated if ( ) is executed in place of S 25 and ( ) to denote the circuit that is generated when the execution is continued until the end of the current subroutine (tail operator).26 In global scope and are undened. In the following example , , and would evaluate to = B (x), = C (x), = D(x, y) B (x) and = D(x, y) C (x).
operator U(qureg x,qureg y) { A(x); if y { B(x); } else { C(x); } D(x,y); }

(2.90)

// quantum if-statement

2.6.2.2

Conditional Execution

A simple QIS S is of the form if p then 1 , 2 . . . n endif and implements the transformation US = [[p]] . So simple QISs can be used to set the enable registers of conditional operators (conditional calls, see 2.6.1.2). Since p is already passed as an implicit parameter must not operate on p i.e. = ( p), so the QIS below is not a miraculous implementation of a unitary erase operation, but simply triggers an error
qcl> if q { Not(q); } ! at "Not(q); ": ! runtime error: arguments overlap with quantum condition

Global Control Register Internally, the passing of enable registers to conditional subroutines is handled by a global control register g. Whenever a simple QIS is encountered, the following happens: 1. The appropriate enable register p is appended to g. 2. The if-branch is executed. 3. p is removed from g. Whenever a call to a conditional gate is encountered, the current value of g is passed as enable register to the quantum oracle.
i.e. the circuit that would be generated if S were a classical if-statement with p = true (p = false) 26 Note that therefore, even if no else-branch is dened and consequently = (), this does not imply that = I.
25

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING 2.6.2.3 Quantum Selection

99

A clean QIS S with = () is of the form if p then 1 , 2 . . . n else 1 , 2 . . . m endif The semantics of S (i.e. the corresponding unitary transformation) are dened as the selection operator U S : |k | c
p

( |k ) |c ( |k )|c

p p

if c = 1 if c = 0.

(2.91)

As US contains and as suboperators, this means that, unlike a classical if-statement, both branches of S get executed. As we assumed that neither nor contain assignments or other statements which would change the program state, both branches cannot exhibit mutual side eects and this can be done without being inconsistent with classical semantics.27 US is realized as the conditional composition (2.85) US = Not(p) [[p]] Not(p) [[p]] = 0 0 (2.92)

so the QIS on the left corresponds to the right sequence (see 2.6.1.2)
if p { inc(x); } else { !inc(x); } // // // // cinc(x,p); Not(p); !cinc(x,p); Not(p); conditional increment invert enable qubit conditional decrement restore enable qubit

2.6.2.4

Quantum Forking

If the body of a classical if-statement S contains changes to the program state (e.g. assignments to local variables), then subsequent operator calls may dier, depending on whether the if- or the else-branch has been executed. If however, S is a (dirty) QIS, then both paths need to be executed in order to determine the corresponding conditional operator US , which allows for non-classical side-eects and can lead to classically inconsistent states. To illustrate the problem, consider the operator
27

An example for inconsistent code would be e.g. boolean v := true if p then v := v else v := v endif if v then error classically unreachable code endif

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING


operator U(qureg s,quconst p) { int n; if p { n=1; } else { n=0; } V(n,s); }

100

// dirty QIS // n=0 or n=1?

At rst glance it seems impossible to come up with semantics U (s, p) that are both, classically consistent and also provide a reasonable interpretation as quantum control statement because there is no reason to prefer one of the obvious candidates V0 (s) and V1 (s) over the other. As a quantum analogy, we might say that the QIS brought p and the classical variable n into a correlated state, which would suggest the interpretation that V0 (s) is applied to all basis vectors |k s |0 e and V1 (s) to all basis vectors |k s |1 e i.e. U : |k s |n p (Vn |k s )|n p (2.93) While these semantics are symmetric28 and have a reasonable quantum interpretation, they are not classically consistent as they leave the binding of n undened. But here, the mathematical semantics of operators (see 2.5.1.2) come to the rescue, as they state that operators are dened by their unitary eect and may not change the program state. Since n is a local variable about to leave scope, we might as well leave it undened. However, these semantics also imply that dirty QISs cannot be used in global scope or within procedures. The above interpretation could also be described as extending the QIS to include the remainder of the operator, so U is considered to be equivalent to
operator U(qureg s,quconst p) { int n; if p { n=1; V(n,s); } else { n=0; V(n,s); } }

As this can only be done explicitly if the QIS in question is not part of a control statement (like, e.g. a while-loop), a more general notion of what exactly constitutes the remainder of a subroutine is necessary. This is provided by the the tail operators and (see 2.6.2.1). Using and , the above method can be formalized so that for a dirty QIS S of the form if p then 1 , 2 . . . n else 1 , 2 . . . m endif the transformation US implemented by S can be dened as U S : |k | c
28

( |k )|c ( |k )|c

p p

if c = 11..1 otherwise .

(2.94)

i.e. treat both branches of the QIS equally.

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING and realized as US = Not(p) [[p]] Not(p) [[p]] = 0 0

101

(2.95)

Threaded Execution Since the evaluation of and involves not only the execution of the corresponding block of S but of the complete subroutine, a dirty QIS splits the classical ow of control into two separate threads (quantum forking). Each thread is executed independently until the end of the subroutine is reached (threaded execution). Because of (2.80), the generated quantum circuits and can then be recombined to US = Not(p) [[p]] Not(p) [[p]] = 0 0 (2.96)

Restrictions While the concept of threaded execution allows classical code to be conditionally executed depending on qubits, dirty QISs also impose several restrictions: Since dierent threads will generally result in dierent program states, dirty QISs may only appear in subroutines with mathematical semantics and are therefore restricted to operators. As the eect of a dirty QIS extends over the remainder of the routine, all reachable suboperators have to be conditional, even when the routine is not declared conditional and they are not part of a QIS. When a dirty QIS is part of a loop, then the enable registers have to be disjoint for each iteration. 2.6.2.5 Pseudo Measurements

A possible use of QISs is to formally accumulate the content of (unobservable) quantum registers into a classical variable (pseudo measurements). The QCL operator below implements the selection operator mux : |k q |i
s

Not(qi )|k

|i s .

(2.97)

by conditional composition (2.87).


cond qufunct mux(qureg q,quconst s) { // Quantum Multiplexer int i; int n = 0; for i=0 to #s-1 { // accumulate content of if s[i] { n=n+2^i; } // selection register in a } // classical variable Not(q[n]); // flip selected output qubit }

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

102

Figure 2.13 shows the quantum circuit generated by mux(s,q) in the case of a 2-qubit selection register s.

q0 q1 q2 q3 s0 s1

q0 q1 q2 q3

X X X X

X X

s0 s1

Figure 2.13: A quantum multiplexer

2.6.2.6

Conditional Loops

By conditionally executing a break statement, QISs can be used to implement (bounded) conditional loops with quantum termination conditions. The QCL operator below implements the phase transformation pdlog() = diag(1, , 2 , 2 , 3 , 3 , 3 , 3 , 4 . . .) with = ei which rotates basis vectors according to the highest set qubit.
operator pdlog(real phi,qureg q) { int i; for i=#q-1 to 0 step -1 { // iterate from MSB to LSB if q[i] { break; } // exit if qubit is set Phase(-phi); // rotate by -phi } }

(2.98)

Figure 2.14 shows the 4-qubit quantum circuit generated by pdlog.

2.6.3

Quantum Conditions

Denition 42 (Boolean Formulas) Let S be a set of symbols, then B0 (S ) = S {true, false} is the set of atomic boolean formulas on S . The set B (S ) of boolean formulas on S is recursively constructed in the following way (i) if a B0 (S ) then a, inB (S ) (ii) if a B (S ) then a B (S )

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

103

q0 q1 q2 q3

X X X X

X X X X

q0 q1 q2 q3

Figure 2.14: Dual log phase transform

(iii)

if a, b B (S ) then a b, a b, a b B (S )

Denition 43 (Quantum Condition) A boolean formula C B (R1 ) of qubits is called quantum condition. A quantum register e = e0 . . . en1 Rn corresponds to the quantum condition e0 . . . en1 . Two registers p and q are equivalent (p q) if they correspond to the same quantum condition (i.e. consist of the same qubits) 2.6.3.1 Exclusive Disjunctive Normal Form

Denition 44 Let S be a set of symbols. A boolean formula f B (S ) of the form


n mi

f=
i=1 j =1

pij

with

pij S

and {pkj } = {plj } k = l

(2.99)

is in exclusive disjunctive normal form (XNF). Also we declare


0 0

false
i=1

and

true
j =1

(2.100)

Any boolean formula f can be transformed into the XFN by recursively applying the following rules. (i) a a false (ii) (iii) (iv) (v) aaa a true a abaabb a
i bi

a bi

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

104

From now on, we will assume that quantum conditions are provided in XNF. Further, we will use the notation
|pi |1

C = {pi } C =
i j =0

pij .

(2.101)

Denition 45 Let p, q R be registers and P = {pi }, Q = {qi } be the set of qubits in p, q. The registers p q and p q are dened as p q = r0 . . . rk1 , p q = s0 . . . sl1 , ri P Q, ri < rj i < j ri P Q, ri < rj i < j pi
i

(2.102) (2.103) (2.104)

Denition 46 Let C = {pi } be a quantum condition. The register c = reg C = is called condition register of C. 2.6.3.2 Quantum Predicates

Denition 47 Let C = {pi } be a quantum condition with the condition register c = reg C and t R1 . The operator PC (c, t) = X[[C]] (t) =
i

CNot(t, pi )

(2.105)

is called quantum predicate of C with the target register t. Denition 48 Let C = {pi } be a quantum condition with the condition register c = reg C, c : B|c| B such that PC |x c |0 = |x c |c(x) and U be a unitary operator. The (extended) conditional operator U[[C]] is dened as U[[C]] : |k |x
c

(U |k )|x |k | x c

if c(k ) . if c(k )

(2.106)

By using an empty scratch qubit s, U[[C]] can be implemented as U[[C]] = PC (c, s) U[[s]] PC (c, s) (2.107)

Boolean Operations Let p, q be qubits, s be an empty scratch qubit and U a unitary operator. The extended conditional operators for the boolean operations not, and, xor and or can be implemented as U[[p]] U[[pq]] U[[pq]] U[[pq]] = = = = X (p) U[[p]] X (p) U[[pq]] X[[p]] (s) X[[q]] (s) U[[s]] X[[q]] (s) X[[p]] (s) X[[pq]] (s) U[[pq]] X[[pq]] (s) (2.108) (2.109) (2.110) (2.111)

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING 2.6.3.3 Language Representation of Quantum Conditions

105

In structured quantum programming, quantum conditions are represented as a special datatype (QCL type qucond) that represents a list of constant quantum registers. Boolean operators can be used to combine registers, quantum conditions and even classical boolean expressions:
qcl> qureg a[1]; qureg b[1]; qcl> print a and b, a or b, a xor b; : <0,1> <0; 1; 0,1> <0; 1> qcl> qucond c; qcl> c=not (a or b); qcl> print c, #c, c[3]; : <*; 0; 1; 0,1> 4 <0,1> qcl> print c xor true, c and (1==2); : <0; 1; 0,1> <> qcl> c=(pi > 3); qcl> print c; : <*> // allocate 2 qubits // basic boolean operators // // // // // // // // // qucond variable qucond assignment print c, the number of clauses, the last register mixed quantum/boolean expressions boolean expressions get cast to qucond if necessary

QCL even denes a comparison operator to compare registers to other registers or integers.
qcl> qureg q[4]; qcl> print q==15,q==7; : <0,1,2,3> <0,1,2; 0,1,2,3>

2.6.3.4

Quantum Conditions and If-Statements

The main use of quantum conditions is as arguments to quantum if-statements. if C then 1 , 2 . . . n else 1 , 2 . . . m endif Depending on C, there are 5 possible cases: 1. If C = {} = false, the else-branch is executed. 2. If C = {o} = true, the if-branch is executed. 3. If C = {p} and |p| = 1 then the QIS is executed normally. 4. If C = {q} and |q| > 1 and the QIS is simple, then the QIS is executed normally. 5. Otherwise, (a) an empty scratch qubit s is allocated,

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING (b) the quantum predicate PC (c, s) is applied to s, (c) C is substituted by s and the QIS is executed, (d) s is uncomputed by PC (c, s) and deallocated.
qcl> qureg q[3]; qcl> qureg a[1]; qureg b[1]; qcl> H(a & b); [5/32] 0.5 |0,0,0> + 0.5 |0,1,0> qcl> if a { inc(q); } [5/32] 0.5 |0,0,0> + 0.5 |1,1,0> qcl> if a and b { inc(q); } [5/32] 0.5 |0,0,0> + 0.5 |1,1,0> qcl> if a or b { inc(q); } [5/32] 0.5 |0,0,0> + 0.5 |2,1,0> qcl> if not a or b { inc(q); } [5/32] 0.5 |1,0,0> + 0.5 |2,1,0>

106

+ 0.5 |0,0,1> + 0.5 |0,1,1> + 0.5 |0,0,1> + 0.5 |1,1,1> + 0.5 |0,0,1> + 0.5 |2,1,1> + 0.5 |1,0,1> + 0.5 |3,1,1> + 0.5 |2,0,1> + 0.5 |4,1,1>

2.6.3.5

Quantum Condition Functions

Auxiliary functions can be used to compute complex conditions. The following function computes a quantum condition to test for primality:
qucond isprime(quconst q) { int i; qucond c; for i = 0 to 2^#q-1 { if testprime(i) { c = c or q==i; } } return c; } // Primality test for register q // // // // qucond variable c={}=false iterate of possible numbers if prime then add qucond(q==i) to c

Fig. 2.15 shows the quantum circuit of the quantum predicate Pisprime (q, p) for |q| = 4.
q0 q1 q2 q3 p q0 q1 q2 q3 p

Figure 2.15: 4-qubit XNF primality test

CHAPTER 2. STRUCTURED QUANTUM PROGRAMMING

107

qcl> qureg q[4]; qcl> H(q); [4/32] 0.25 |0> + 0.25 |1> + 0.25 |2> + 0.25 |3> + 0.25 |4> + 0.25 |5> + 0.25 |6> + 0.25 |7> + 0.25 |8> + 0.25 |9> + 0.25 |10> + 0.25 |11> + 0.25 |12> + 0.25 |13> + 0.25 |14> + 0.25 |15> qcl> if isprime(q) { Phase(pi); } // swap sign if prime [4/32] 0.25 |0> + 0.25 |1> - 0.25 |2> - 0.25 |3> + 0.25 |4> 0.25 |5> + 0.25 |6> - 0.25 |7> + 0.25 |8> + 0.25 |9> + 0.25 |10> 0.25 |11> + 0.25 |12> - 0.25 |13> + 0.25 |14> + 0.25 |15>

Appendix A QCL Quick Reference


A.1 Syntax

The syntactic structure of a QCL program is described by a context free LALR(1) grammar. For the formal syntax denition the following notation is used: expression-name expression-def 1 expression-def 2

Within syntax denitions, the following conventions apply Keywords and other literal text is set in courier Subexpressions are set in italic Optional expressions are put in [ square brackets ]. Optional expressions can be repeated 0 or 1 times. Multiple expressions are put in { braces }. Multiple expression can be repeated 0, 1 or n times. Alternatives are written as alt 1 | alt 2 | . . . Exactly one alternative has to be chosen. Grouping of expressions can be forced by ( round brackets ). To simplify the notation of literals, the following character classes are dened: 108

APPENDIX A. QCL QUICK REFERENCE digit decimal digit from 0 to 9.

109

letter alphabetic letter form a to z or A to Z. Case is signicant. char printable character except ". A QCL Program is a sequence of statements and denitions so qcl-input { stmt | def } Comments Like C++, QCL supports two ways of commenting code. All comments are simply discarded by the scanner. Line comments are started with // and last until the end of the current line C-style comments are started with /* and terminated with */ and may continue over several lines. C-style comments may not be nested.

A.1.1

Expressions

complex-coord [ + | - ] digit { digit } [ . { digit }] const digit { digit } [ . { digit }] ( complex-coord , complex-coord ) true | false " { char } " subscript identier [ expr { , expr } ] expr const | subscript identier [ [ expr [( : | .. ) expr ] ] ] identier ( [ expr { , expr }] ) ( expr ) # expr expr ^ expr - expr expr ( * | / ) expr expr mod expr expr ( + | - | & ) expr

APPENDIX A. QCL QUICK REFERENCE expr ( == | != | < | <= | > | >= ) expr not expr expr and expr expr ( or | xor ) expr

110

A.1.2

Statements
block option stmt { stmt { stmt } } letter { letter | - } [ ! ] identier ( [ expr { , expr }] ) ; ( identier | subscript ) = expr ; expr ( -> | <- | <-> ) expr ; for identier = expr to expr [ step expr ] block while expr block block until expr ; if expr block [ else block ] return expr ; input [ expr , ] identier ; print expr [ , expr ] ; exit [ expr ] ; measure expr [ , identier ] ; reset ; dump [ expr ] ; list [ identier { , identier }] ; ( load | save ) [ expr ] ; shell ; set option [ , expr ] ; stmt ;

A.1.3

Denitions
int | real | complex qureg | quvoid | quconst | quscratch ( vector | matrix | tensor digit ) scalartype

scalartype quantumtype tensortype

APPENDIX A. QCL QUICK REFERENCE othertype type const-def var-def arg-def arg-list body def

111

( qucond | boolean | string scalartype | tensortype | quantumtype | othertype const identier = expr ; type identier [ expr ] ; type identier [ = expr ] ; type identier ( [ arg-def { , arg-def }] ) { { const-def | var-def } { stmt } } const-def | var-def type identier arg-list body procedure identier arg-list body [ cond ] operator identier arg-list body [ cond ] qufunct [ operator ] identier arg-list body extern operator identier arg-list ; extern qufunct identier arg-list ;

A.2
A.2.1
A.2.1.1

Expressions
Data Types
Classical Scalar Types

The classical scalar data-types of QCL are the arithmetic types int, real and complex, as well as boolean and string. Type int real complex boolean string A.2.1.2 Tensors Description integer real number complex number logical value character string Examples 1234, -1 3.14, -0.001 (0,-1), (0.5, 0.866) true, false "hello world", ""

Since v0.5, QCL supports vectors, (square) matrices and higher tensors up to order 9.

APPENDIX A. QCL QUICK REFERENCE Type vector matrix tensorn Description vector square matrix tensor of order n Examples vector(0,0.5,0.866) matrix(0,(0,-1),(0,1),0) tensor3(1,0,0,0,0,0,0,1)

112

Tensors can be dened for the arithmetic scalar types int, real and complex. A tensor variable v of order n and dimension dim is declared by the syntax scalartype tensorn v [dim ]; For tensors of order 1 and 2 the keywords vector and matrix can be used. The subscript operator v [coord ] is used to access tensor elements with coord being a comma separated list of n zero-based integer indices. There are no tensor literals; instead a tensor object of order n and dimension d is created by the constructor function tensorn (elem ), with elem being a comma separated list of dn scalar expressions ordered in ascending order of their indices with the leftmost index being the most signicant. Tensors of equal order and dimension can be added, subtracted and assigned to. Tensors can be multiplied by scalars or by tensors of equal dimension. In the latter case, multiplication is dened as generalized dot-product, i.e. contraction by summing over the innermost indices ai1 ...in1 in bj1 j2 ...jm =
k

ai1 ...in1 k bkj2 ...jm

A.2.1.3

Register Types

For local variables or parameters of the types quvoid and quscratch let U be the corresponding unitary operator. Type qureg quconst quvoid quscratch A.2.1.4 Function general register quantum constant target register scratch register Restriction none invariant to all operators empty when U is called empty when U or U are called

Quantum Conditions

Boolean expressions of qubits are represented by the QCL type qucond. Let C be a variable of type qucond and
n1 |pi |1

C = { pi } =
i=0 j =0

pij

APPENDIX A. QCL QUICK REFERENCE

113

the XNF of C . The size operator #C returns the number n of clauses in C and C [k ] gives the quconst register pk .

A.2.2

Operators

The following table lists all QCL operators ordered by precedence. arith stands for all arithmetic scalar types, tensor for all tensor types and quantum for all register types. Op [ ] Description qubit subregister XNF clause vector subscript subregister by range subregister by oset and length tensor subscript register size number of XNF clauses dimension power integer power unary minus multiplication division integer division integer modulus addition subtraction concatenation equal unequal less less or equal greater greater or equal logical not logical and logical inclusive or logical exclusive or Argument type quantum variable qucond variable vector variable quantum variable quantum variable tensor variable quantum qucond tensor arith int arith , tensor arith , tensor arith int int arith , tensor arith , tensor string, quantum arith , string arith , string int, real int, real int, real int, real boolean, qucond boolean, qucond boolean, qucond boolean, qucond

[ .. ] [ :: ] [ , ] #

^ * / mod + & == != < <= > >= not and or xor

APPENDIX A. QCL QUICK REFERENCE

114

A.2.3

Elementary Functions

Elementary functions are part of QCL and need not be declared. Unlike function subroutines, elementary functions do not need to have a xed number of arguments may take arguments of dierent types and may have varying return types depending on the types of the arguments Function sin, cos, tan, cot sinh, cosh, tanh, coth exp(x) log(x) log(x,n) sqrt(x) abs(x) Re, Im conj(z) floor, ceil gcd(n,...) lcm(n,...) min, max not, and, or, xor bit(n,k) int, real, complex, string vector, matrix, tensorn random() Description trigonometric functions hyperbolic functions e raised to the power of x natural logarithm of x base-n logarithm of x square root of x absolute value of x real and imaginary part complex conjugate of z next higher and lower integer greatest common divisor least common multiple minimum and maximum binary functions logical value of the k th bit of n explicit typecasts tensor constructors random value from [0, 1) Arg. arith arith arith arith arith arith arith complex complex real int int arith int int scalar arith none

A.3

Statements

Unless otherwise stated, parameters in slanted courier denote expressions (see A.1.1).

A.3.1
A.3.1.1

Simple Statements
Assignments

lvalue = rvalue ;

APPENDIX A. QCL QUICK REFERENCE

115

lvalue can either be a variable or a subscript expression. Quantum variables (i.e. symbolic registers) cannot be assigned to. Implicit typecasting is performed from int and real to real or complex and from boolean and all register types to qucond. In all other cases, lvalue and rvalue have to be of the same type. A.3.1.2 Subroutine Calls

[!]sub (args ); args is a comma separated list of arguments and can be empty. The number of expressions in args has to match the declaration of the subroutine sub which can be a procedure, operator or qufunct. If sub is a quantum subroutine, then the inverse operator can be called by prexing the name with !. Procedures cannot be inverted. A.3.1.3 Input and Output

input [ prompt , ] var ; reads classical input from the user as assigns it to the scalar variable var . An optional prompt of type string can be specied. print list ; prints a comma separated list of expressions. A.3.1.4 Measurement and Initialization

measure q [,var ]; measures the register q and assigns the measured value to the integer variable var if specied. reset; initializes the machine state.

A.3.2
A.3.2.1

Flow Control
Loops

The body of a loop is a list of statements. Braces are obligatory even if body is a single statement. The break statement break; can be used within the body to immediately exit the innermost loop.

APPENDIX A. QCL QUICK REFERENCE Conditional Loops while cond { body } { body } until cond ;

116

cond is a boolean expression. The body of an until-loop is executed at least once. Counting Loops for i = a to b [ step s ] { body } The range a , b and the optional step size s are integer expressions. The loop variable i is also of type int and is redeclared as symbolic constant within the body. A.3.2.2 If-statement

if cond { sigma } [ else { tau }] The if-branch sigma and the optional else-branch tau are list of statements; braces are obligatory even for single statements. The if-condition cond is either a boolean expression (classical if-statement) or a quantum condition (quantum if-statement). A.3.2.3 Abnormal Termination

exit msg ; prints out the error message msg and terminates the QCL program.

A.3.3

Interactive Commands

The QCL interpreter qcl denes additional commands which are mainly intended for interactive use and debugging and not considered to be coreparts of QCL. A.3.3.1 Simulation Commands

The following commands are only available if QCL is used together with a numerical simulator. dump [ q ];

APPENDIX A. QCL QUICK REFERENCE

117

prints the probability spectrum of the register q . If no register is specied, the current machine state is printed. plot [ q ]; plots the probability spectrum of the register q . If no register is specied, the current machine state is plotted. plot q , p ; plots the probability spectrum of q p as two-dimensional density graph. save file ; saves the current machine state to file . load file ; loads the current machine state from file . A.3.3.2 Other Commands

set opt val ; sets the interpreter option opt (without the leading --) to the value val . Please refer to appendix A.4 for a complete list of options. list [sym ]; lists the denition of the symbol sym in the current scope. If no symbol is specied, all currently dened symbols are listed. shell; opens a subshell which can be closed again with the exit command. Subshells can also be opened in response to keyboard interrupts and runtime errors (see A.4). Symbols dened in a subshell are local and leave scope when the shell is left again. exit; closes the current shell. Closing the top-level shell terminates the session.

APPENDIX A. QCL QUICK REFERENCE

118

A.4

Interpreter Options

The QCL interpreter qcl has the following options: Startup Options Those can only be set on the command line.
-h, --help -V, --version -i, --interactive -x, --exec<commands> -q, --quiet --color --texmacs -n, --no-default-include -o, --logfile -b, --bits=n: display this message display version information force interactive mode execute commands on startup skip startup message color xterm interface TeXmacs interface (experimental) dont read default.qcl on startup specify a logle set number of qubits (32)

Dynamic Options Those can be set on the command line or via the set command (see above). Default values are given in brackets.
-s, --seed=<seed-value> -I, --include-path=<path> --library=<y|n> -d, --dump-file=<file> -p, --plot-file=<file> -f, --dump-format=x,d,b -r, --show-regs=<y|n> -D, --dump-precision=<d> -P, --precision=<digits> -Z, --trunc-zeros=<y|n> -T, --truc-states=<y|n> --plot-paper=<format> --plot-size=<pixel> -Q, --qureg-mask=<y|n> -g, --debug=<y|n> -a, --auto-dump=<max> -l, --log==<y|n> -L, --log-state==<y|n> -c, --check==<y|n> --trace==<y|n> -S, --syntax=<y|n> -E, --echo=<y|n> -t, --test=<y|n> -e, --shell-escape=<y|n> --irq=<y|n> set random seed value (system time) QCL include path (/usr/local/lib/qcl) ignore redenitions of existing symbols (n) send output of dump-command to le (none) Postscript le created by plot-command (none) list base vectors as hex, decimal or binary (d) show global registers in dumped states (y) shown d digits in dumped states (5) shown digits for real and complex values (6) truncate zeros for real and complex values (y) truncate non-allocated qubits (y) Set paper-format for Postscript output (b5) Set maximum window size for X11 plots (600) list registers as masks instead of lists (n) open debug-shell on error (n) dump states up to max terms in shell mode (8) log external operator calls (n) log state after each transformation (n) check consistency of quantum heap (n) trace mode (very verbose) (n) check only the syntax, no interpretation (n) echo parsed input (n) test program, ignore quantum operations (n) honor shell-escapes (y) allow user interrupts if supported (y)

Bibliography
[1] Anthony A. Aaby: Introduction to Programming Languages (1996). http://cs.wwc.edu/~aabyan/221_2/PLBOOK/ [2] G. Baker: Qgol (1996). project-thesis, Macquarie University, http://www.ifost.org.au/~gregb/q-gol/ [3] A. Barenco et al.: Elementary Gates for Quantum Computation (1995). Phys. Rev. A 52, pp. 3457-3467 [4] D. Beckman et al.: Ecient networks for quantum factoring (1996). Phys. Rev. A 54, pp. 1034-1063 [5] P.A. Benio: The computer as a physical system: A microscopic quantum mechanical Hamiltonian model of computers as represented by Turing machines (1980). J. Stat. Phys., 22(5), pp. 563-591 [6] P.A. Benio: Models of Quantum Turing Machines (1997). LANL quant-ph/9708054 [7] C.H. Bennet: Logical Reversibility of Computation (1973). IBM J. Res. Develop. 17, 525 [8] C.H. Bennet: Time-space trade-os for reversible computation (1989). SIAM J. Comput. 18, p. 766 [9] E. Bernstein, U. Vazirani: Quantum complexity theory (1997). SIAM J. Comput., 26(5), pp. 1411-1473 [10] S. Blaha: Quantum Computers and Quantum Computer Languages: Quantum Assembly Language and Quantum C Language (2002). LANL quant-ph/0201082 [11] C. B ohm and G. Jacopini: Flow Diagrams, Turing Machines, and Languages with Only Two Formation Rules (1966). Communications of the ACM, Vol. 9, No. 5, pp. 336-371 119

BIBLIOGRAPHY

120

[12] M. Boyer, G. Brassard, P. Hoyer, A. Tapp: Tight bounds on quantum searching (1998). Fortschritte der Physik, vol. 46(4-5), pp. 493-505 [13] P.O, Boykin et al.: On universal and fault-tolerant quantum computing (1999). LANL quant-ph/9906054 [14] J. Branson: Quantum Physics 130A (2001). lecture notes, http://heppc16.ucsd.edu/ph130a/130a_notes/node15.html [15] J. Buchmann: Faktorisierung groer Zahlen (1996). Spektrum der Wissenschaft 9/96, pp. 80-88 [16] S.L. Braunstein: Quantum computation: a tutorial (1995). http://www.informatics.bangor.ac.uk/~schmuel/comp/comp.html [17] D.C. Cassidy: Exhibit on Werner Heisenberg (1998). American Institute of Physics http://www.aip.org/history/heisenberg/p09_text.htm [18] I.L. Chuang et al.: NMR quantum computing: Realizing Shors algorithm (2001). Nature 414, pp. 883-887 [19] J.I. Cirac, P. Zoller: Quantum Computations with Cold trapped Ions (1995). Phys. Rev. Lett. 74, p. 4091 [20] R. Cleve, A.Ekert, C. Macchiavello, M. Mosca: Quantum algorithms revisited (1998). Proc. R. Soc., London A 454, pp. 339-354 [21] B.J. Copeland: The Church-Turing Thesis (1996). Stanford Encyclopedia of Philosophy ISSN, pp. 1095-5054 [22] D. Coppersmith: An Approximate Fourier Transform Useful in Quantum Factoring (1994). IBM Research Report No. RC19642 [23] O.J. Dahl, E.W. Dijkstra, C.A.R. Hoare: Structured Programming (1972). Academic Press, London, UK [24] D. Deutsch,: Quantum theory, the Church-Turing principle and the universal quantum computer (1985). Proc. R. Soc., London, A 400, pp. 97-117 [25] D. Deutsch: Quantum computational networks (1989). Proc. R. Soc., London, A 439, pp. 553-558

BIBLIOGRAPHY [26] D. Deutsch and R. Jozsa: Rapid solution of problems by quantum computer (1992). Proc. Roy. Soc. London, Ser. A, vol. 439, pp. 553-558

121

[27] E.W. Dijkstra: Structured Programming (1969). Software Engineering Techniques, Buxton, J. N., and Randell, B., eds. Brussels, Belgium, NATO Science Committee [28] D.P. DiVincenzo: Two bit gates are universal for quantum computation (1995). Phys. Rev. A, 51(2), pp. 1015-1022 [29] A. Einstein, B. Podolsky, N. Rosen: Can quantum-mechanical description of physical reality be considered complete? (1935). Physical Review 47, pp. 777-780 [30] A. Ekert, R. Jozsa: Shors Quantum Algorithm for Factoring Numbers (1996). Rev. Modern Physics 68 (3), pp. 733-753 [31] R.P. Feynman: Quantum mechanical computers (1985). Optics News 11, pp 11-20 [32] Lov K. Grover: A fast quantum mechanical algorithm for database search (1996). Proceeding of the 28th Annual ACM Symposium on Theory of Computing [33] J. Gruska,: Foundations of Computing (1998). chap. 12: Frontiers Quantum Computing [34] G.H. Hardy, E.M. Wright: An Introduction to the Theory of Numbers Oxford (1938). U.K.: Oxford Univ. Press [35] A. Hermann: Lexikon - Geschichte der Physik A-Z (1978). Aulis-Verlag Deubner & Co KG [36] R.W. Keyes: Miniaturization of electronics and its limits (1988). IBM J. Res. Develop. 32(1), pp. 24-28 [37] D. Kielpinski, C. Monroe and D.J. Wineland: Architecture for a large-scale ion-trap quantum computer (2002). Nature 417, pp. 709-711 [38] W. Kummer and R. Trausmuth: Quantentheorie (1988). Skriptum zur Vorlesung 131.869 [39] A. Leitsch: Skriptum zur Vorlesung Algorithmen-, Rekursions und Komplexit atstheorie (1994). Technical University Vienna

BIBLIOGRAPHY

122

[40] M. Agrawal, N. Kayal, N. Saxena: PRIMES is in P (2002). preprint, http://www.cse.iitk.ac.in/users/manindra/primality.ps [41] Z. Meglicki: Introduction to Quantum Computing (M743) (2002). http://beige.ucs.indiana.edu/B679/M743.html [42] F.D. Murnaghan: The Unitary and Rotation Groups (1962). Spartan Books, Washington [43] M.A. Nielsen and I.L. Chuang: Quantum Computation and Quantum Information (2000). Cambridge University Press [44] B. Omer: Simulation of Quantum Computers (1996). http://www.itp.tuwien.ac.at/~oemer/papers.html [45] B. Omer: A Procedural Formalism for Quantum Computing (1998). master-thesis, Technical University of Vienna [46] B. Omer: Quantum Programming in QCL (2000). master-thesis, Technical University of Vienna [47] B. Omer: Procedural Quantum Programming (2002). Proc. of the 5th Int. Conference CASYS 2001, D. M. Dubois (Ed.), AIP Conference Proceedings 627, pp. 276-285 [48] B. Omer: Classical Concepts in Quantum Programming (2003). accepted for publication in the QS2002 Conference Proceedings, LANL quant-ph/0211100 [49] B. Omer: QCL homepage (2003). http://www.itp.tuwien.ac.at/~oemer/qcl.html [50] E.L. Post: Finite Combinatory Processes - Formulation 1 (1936). Journal of Symbolic Logic, 1, pp. 103-105 [51] H. Putnam: A philosopher looks at quantum mechanics (1965). R. Colodny (ed.), Beyond the Edge of Certainty: Essays in Contemporary Science and Philosophy, Englewood Clis, New Jersey: Prentice Hall, pp. 130-158. [52] M. Reck, A. Zeilinger, H.J. Bernstein, P. Bertani: Experimental realization of any discrete unitary operator (1994). Phys. Rev. Lett., 73(1), pp. 58-61

BIBLIOGRAPHY

123

[53] M.A. Rowe et al.: Transport of quantum states and separation of ions in a dual rf ion trap (2002). Quantum Information and Computation 2, pp. 257-271 [54] P.W. Shor.: Algorithms for quantum computation: Discrete logarithms and factoring (1994). Proc. 35th Annual Symposium on Foundations of Computer Science, IEEE Press, Los Alamitos, CA [55] R. Solovay and V. Strassen: A Fast Monte-Carlo Test for Primality (1977). SIAM Journal on Computing, 1977, pp. 84-85 [56] K. Svozil: Quantum algorithmic information theory (1996). Journal of Universal Computer Science 2, pp. 311-346 [57] T. Tooli: Bicontinuous extension of reversible combinatorial functions (1981). Math. Syst. Theory 14, pp. 13-23 [58] A.M. Turing: On computable numbers, with an application to the Entscheidungsproblem (1936). Proc. of the London Mathematical Society, ser. 2. vol. 42, pp. 230-265, http://www.abelard.org/turpap2/tp2-ie.asp [59] A.M. Turing: Intelligent Machinery (1948). National Physical Laboratory Report. In Meltzer, B., Michie, D. (eds) 1969. Machine Intelligence 5. Edinburgh: Edinburgh University Press., 7 [60] B. VanHoy: Structured Programming, Control Structures, if-else Statements, Pseudocode (1998). APCS-C++, Lesson 8, ICT http://www.mvhs.fuhsd.org/bob_vanhoy/pdfs/lesson08.pdf [61] J. Wallace: Quantum Computer Simulators (2001). Partial Proc. of the 4th Int. Conference CASYS 2000, D. M. Dubois (Ed.), International Journal of Computing Anticipatory Systems, volume 10, 2001, pp. 230-245 [62] J. Wallace: Quantum Computer Simulators (2002). online survey, http://www.dcs.ex.ac.uk/~jwallace/simtable.htm [63] P. Zuliani: Quantum Programming (2001). PhD thesis, University of Oxford,
http://web.comlab.ox.ac.uk/oucl/work/paolo.zuliani/pzthesis.ps.gz

List of Figures
1.1 1.2 1.3 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 2.10 2.11 2.12 2.13 2.14 2.15 A program controlling a machine M = (S, O, T, , ) . . . . 17 Bloch sphere representation of the qubit state | . . . . . . . . 21 Circuit notation for common gates . . . . . . . . . . . . . . . . 37 A simple probabilistic quantum algorithm . . . . . Parity of a bit string of length 4 . . . . . . . . . . Hybrid quantum architecture . . . . . . . . . . . . Classical computer with quantum oracle . . . . . . Basic control structures . . . . . . . . . . . . . . . Register operator U (s) . . . . . . . . . . . . . . . Quantum Fourier Transform for a 4-qubit register Recursive phase transformation . . . . . . . . . . 5-qubit test state . . . . . . . . . . . . . . . . . . Increment operator . . . . . . . . . . . . . . . . . Conditional increment operator . . . . . . . . . . Call graph of QCL subroutines . . . . . . . . . . . A quantum multiplexer . . . . . . . . . . . . . . . Dual log phase transform . . . . . . . . . . . . . . 4-qubit XNF primality test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 53 57 58 63 71 79 80 82 86 95 96 102 103 106

124

List of Tables
1.1 1.2 1.3 2.1 2.2 2.3 2.4 2.5 2.6 Dirac Notation . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Classical and quantum computational models . . . . . . . . . . 34 Register Notation . . . . . . . . . . . . . . . . . . . . . . . . . 35 Classical and quantum programming concepts Equivalent TM and CQTM commands . . . Scalar arithmetic types and literals in QCL . Quantum data types in QCL . . . . . . . . . Register expressions in QCL . . . . . . . . . Hierarchy of subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 55 60 68 69 75

125

You might also like