Sei sulla pagina 1di 46

Quantum Programming Languages A Survey

Roland Rudiger
r.ruediger@fh-wolfenbuettel.de

Seminar talks given at Institut fur Mathematische Physik , TU Braunschweig February 26/27, 2003

Outline

Simulation of Quantum Computers Classical Programming Languages Concepts and Terminology QPLs: Design Considerations QPLs: Detailed Examples

Simulation of Quantum Computers review of quantum computer simulators by J. Wallace [home page] 37 simulators (Jan. 2002) covering a wide range of QM some features: implementation languages: Pascal, C++, Java, . . . , Haskell, Mathematica, . . . , Perl, . . . ; also: implementation on a parallel machine simulation of single algorithms vs generic simulator visualization of quantum physical processes / algorithms new language vs library support for implementing algorithms in conventional languages specic new data structures (entanglement of variables) dierent computing models: quantum Turing machine vs quantum circuit model

Classical Programming LanguagesConcepts and Terminology remark on terminology: programming pragmatic vs theoretical approach programming in the small vs programming in the large classes of programming languages: imperative programming languages Computation is seen as a sequence of state transitions on a set of state variables. language elements: e. g. assignment statement rened classication (based on programming methodology): structured (e. g. Pascal) modular (e. g. Modula-2)

object-oriented (e. g. C++, Java, Eiel, . . . ) also: mixtures modular / object-oriented (e. g. Oberon) functional programming languages program describes computation of a function: no variables, no assignment statements (in a purely functional language) formal basis: -calculus (Church) examples: Lisp, ML, Scheme, Haskell, . . . logic programming languages also: declarative programming languages, rule-based example: Prolog syntax: context free grammar Context free means: non-terminal symbols can be substituted independently of their context (lhs of production rule: exactly one non-terminal symbol)

semantics (adopted from [Mey90, ch. 4]): translational semantics meaning of a language is expressed by a translational schema: program in this language is translated into a program in another (known) language operational semantics semantics of a language is dened by an interpreting automaton: a formal device capable of formally executing a program denotational semantics meaning of a program is a mathematical object, a function (whereas in translational semantics its another program)

axiomatic semantics basis: program formulae {P } a {Q} P , Q are assertions, specically: a precondition, P a postcondition, Q a: instruction meaning of a program formula: If a is executed in a state in which assertion P is satised, then it will result in a state in which assertion Q is satised. [Mey90, p. 111]

abstraction / abstract datatypes (ADTs) good examples include: First-In-First-Out (FIFO) queue Stack Priority Queue data and operations acting on these data are combined to form objects implementation details remain invisible excessively large example based on the idea of abstraction: Java library (API) (Application Programming Interface) (estimate of 20.000 - 30.000 names (methods, attributes))

First-In-First-Out (FIFO) queue (ADT formalized):


SPECIFICATION ... LIBRARY Boolean, Integer ENDLIB (* library types *) ... TYPE fifo IS ... OPNS (* operations *) empty: -> fifo put: int, fifo -> fifo head: fifo -> int tail: fifo -> fifo isempty: fifo -> bool ...

( 1) ( 2) ( 3) ( 4) ( 5) ...

continued ...

...

( 6) ( 7)

... EQNS (* equations or axioms *) FORALL f: fifo, d1, d2: int OFSORT bool (* boolean equations *) isempty (empty) = true ; isempty (put (d1, f)) = false ; OFSORT tail tail tail fifo (* fifo equations *) (empty) = empty ; (put (d1, empty)) = empty ; (put (d1, put (d2, f))) = put (d1, tail (put (d2, f))) ; int (* integer equations *) (put (d1, put (d2, f))) = head (put (d2, f)) ; (put (d1, empty)) = d1 ; (empty) = 0 ;

( 8) ( 9) (10)

OFSORT (11) head (12) head (13) head ENDTYPE ENDSPEC

QPLs: Design Considerations talking about and specifying QPLs?? answers for questions (never) to come? What can (or cannot) be expected? (pros and cons) new fundamental insights into the oddities of quantum theory?? QPLs might serve as a catalyst for asking new questions helpful in nding new algorithms?? cf learning Pascal and re-inventing heapsort but: exact notations of algorithms require some sort of programming language notation one goal could be: formalized description of algorithms by means of a universal language fresh look at quantum systems

(What can physics learn from classical concepts of computer science?) feasibility of computer science concepts (of classical origin) (What can computer science learn from physics?) What can be formulated using existing language concepts? example modularization: composing a larger system from its parts allocated memory is additive QPL-design isnt merely numerics of matrix operations | = U | main dierence: program structure operational view to quantum systems one important aspect of programming languages: handling of a system / machine by non-specialists didactical aspects

hardware model: QRAM model (Knill [Kni96], Bettelli et al. [BCS01])

bytecode


quantum quantum  device device driver

measure

Classical Computer (Master)

Quantum Processor (Slave)

basic idea [BCS01, p. 15]: What is really needed is a mechanism for generating the whole circuital description before the quantum device is even red up, applying algebraic simplications once for all.

alternatives pragmatic vs theoretical approach new language vs conventional language with library extension historical analogy: transputer one way of designing an ADT based high level language: QFT ... ... ...

QPL based on basic functions ...

Create

CNot U(, , , ) Reset Measure

Niels Bohr: However far the quantum phenomena transcend the scope of classical physical explanation, the account of all evidence must be expressed in classical terms. The argument is simply that by the word experiment we refer to a situation where we can tell others what we have done and what we have learned and that, therefore, the account of the experimental arrangement and the results of the observations must be expressed in unambiguous language with suitable application of the terminology of classical physics.

QPLs: Detailed Examples


Survey QCL Q language qGCL (Block-)QPL new language standard lang. / library imperative language functional language simulator available pragmatic approach theoretical approach formal semantics universal language B. Omer S. Bettelli, T. Calarco, L. Serani qGCL (Quant. Guarded Command Lang.) J.W. Sanders, P. Zuliani QPL (Quant. Programming Language) P. Selinger QCL (Quant. Computation Language) Q language

Q-gol (G. Baker)

Greg Bakers Thesis on Q-gol Additional remarks in thesis-points general comments: Q-gol: one of the rst attempts of designing and implementing a quantum progamming language graphical tool for building and simulation of quantum circuits; uses the gate formalism is not a concise programming language not able to implement and simulate all known quantum algorithms

QCL (B. Omer) General properties

The relevant papers on QCL: Classical Concepts in Quantum Programming, 2002 [Om02] abs Quantum Programming in QCL, 2000 [Om00] (B. Omers master thesis in computing science) A Procedural Formalism for Quantum Computing, 1998 [Om98] (B. Omers master thesis in theoretical physics) general comments: rst actual quantum programming language (rudimentary) procedural language, classical sublanguage not on top of a standard classical language

automatic scratch space management syntactic reversibility of user-dened quantum operations syntax like C / Pascal universal language: can implement and simulate all known quantum algorithms no program renement (no renement calculus) no (formal) semantics non-trivial unitary operations are functions (function call syntax)

QCL as a classical language


if (y == 0) or (y == 1) { return 1; } else { // "else" for better readability // here: y >= 2 k = y; while y >= 3 { y = y - 1; k = k*y; } return k; } } x = 7; print x, "! = ", factorialNR(x); print "The End."; /* result: : 5 ! = 120 : 7 ! = 5040 : The End. */

// program example: recursive // and non-recursive function call: int factorialR(int x) { // recursive implementation // precondition x >= 0 if x == 0 { return 1; } return x*factorialR(x-1); } int x = 5; print x, "! = ", factorialR(x); //------------------------------int factorialNR(int x) { // iterative implementation // precondition x >= 0 int k; int y = x;

quantum memory management


/* QCL, p 53 */ // start QCL with 16 qubits qureg a[4]; qureg b[3]; print "a: ", a, " b: ", b; /* : a: */

// allocate a 4-qubit register // allocate another 3-qubit register // show actual qubit mappings

|............3210>

b:

|.........210....>

/* QCL, p. 53 */ qureg a[3]; procedure test() { qureg b[2]; print a, b; } test(); qureg c[3]; print a, c; // allocate 3 qubits

// local register b gets allocated // allocate another 3 qubits // qubits from b have been reclaimed

/* : |.............210> |...........10...> : |.............210> |..........210...> */

/* QCL, p. 54 */ qureg q[4]; Mix(q); print "dump q: "; dump; /* : dump q: : STATE: 4 / 6 qubits allocated, 2 / 0.25 |000000> + 0.25 |001000> + 0.25 + 0.25 |001010> + 0.25 |000110> + 0.25 + 0.25 |000101> + 0.25 |001101> + 0.25 + 0.25 |001111> */

6 qubits |000100> |001110> |000011>

free + 0.25 |001100> + 0.25 |000010> + 0.25 |000001> + 0.25 |001001> + 0.25 |001011> + 0.25 |000111>

quantum variables

see [Om02, p. 6]: type qureg quconst quvoid quscratch restriction none invariant to all sub-operators has to be empty when the un-inverted operator is called has to be empty before and after the call

but: proper type checking is a problem: the knowledge of the quantum device state is required

quantum expressions [Om00, p. 57]

Expression a a[i] a[i:j] a[i\l] a & b

Description Register reference a0, a1, . . . , an qubit ai substring ai, ai+1, . . . , aj substring ai, ai+1, . . . , ai+l1 concatenation a0, a1, . . . , an, b0, b1, . . . , bm

non-unitary operations

reset measure q, m

// example: initialize a qureg qureg a[6]; qufunct inc(qureg p) { // QCL, 2002, p. 7 int i; for i=#p-1 to 1 step -1 { CNot(p[i], p[0:i-1]); } Not(p[0]); } procedure setto(qureg p, int x) { // initialize p with int val x int i; for i = 0 to x-1 { inc(p); } } reset; dump a; setto(a, 19); dump a; /* result: : SPECTRUM a: |543210> 1 |000000> : SPECTRUM a: |543210> 1 |010011> */

typical evaluation loop:


// QCL, p. 58 { reset; myoperator(q); measure q, m; } until ok(m);

subroutines [Om00, p. 59]

allowed side-eects: routine procedure operator qufunct functions type program state machine state recursion classical all all yes quantum none unitary no quantum none pseudo-classic no classical none none yes

general operators
// QCL operator inversion, p. 60 qureg a[1]; dump; Mix(a); dump; !Mix(a); dump; /* result: : STATE: 1 |0000> : STATE: 0.707107 : STATE: 1 |0000> */

1 / 4 qubits allocated, 3 / 4 qubits free 1 / 4 qubits allocated, 3 / 4 qubits free |0000> + 0.707107 |0001> 1 / 4 qubits allocated, 3 / 4 qubits free

// example: statistical experiment int m; int count0; int count1; const total = 30000; qureg q[4]; count0 = 0; count1 = 0; { reset; Mix(q); measure q[0], m; if m == 0 { count0 = count0 + 1; } else { count1 = count1 + 1; } } until count0 + count1 >= total; print "prob0: ", (count0+0.0)/(count0+count1); print "prob1: ", (count1+0.0)/(count0+count1); /* result is: : prob0: 0.503367 : prob1: 0.496633 */

unitary gates

unitary matrices: e. g. extern operator Matrix2x2(complex u00, complex u10, complex u10, complex u11, qureg q); qubit rotation: extern operator Rot(real theta, qureg q); Hadamard gate: extern operator Mix(qureg q); conditional phase gate: extern operator CPhase(real phi, qureg q);

quantum functions

F : |x x|0

|x x|f (x)

with f : Bn Bm,

n, m N

scratch space management: Bennet-style un-computation [NC00, p. 158] |x, 0, 0, y reversible computation of f (x): circuit C |x, f (x), g(x), y apply CNots |x, f (x), g(x), y f (x) apply C 1: no eect on 4th register |x, 0, 0, y f (x)

pseudo-classic gates

base permutation: e .g. extern qufunct Perm2(int p0, int p1, qureg q); fanout: extern qufunc Fanout(quconst a, quvoid b); swap: extern qufunct Swap(qureg a, qureg b); not, controlled not: extern qufunct Not(qureg q); extern qufunct CNot(qureg q, quconst c);

example: teleportation
operator entangle(qureg p, qureg q){ // entangle qubits p and q Mix(p); CNot(q, p); } reset; x = random(); prepare(x, a[0]); print "prepared state: "; dump a[0]; entangle(a[1], b[0]); !entangle(a[0], a[1]); measure a[0], m0; measure a[1], m1; /* Alice to Bob: -----+ / / +-----> */

/* Alice: a[0] --------- ... a[1] --------- ... Bob: b[0]

--------- ...

Nielsen / Chuang, p. 27: teleportation a[0] -> b[0] */ const i = (0,1); int m0; int m1; qureg a[2]; // Alices 2-qubits register qureg b[1]; // Bobs 1-qubit register real x; // random values, 0 <= x < 1.0 procedure prepare(real x, qureg p){ // prepare some example state complex u00 = cos(2*pi*x) + i*0; complex u01 = -sin(2*pi*x) + i*0; complex u10 = sin(2*pi*x) + i*0; complex u11 = cos(2*pi*x) + i*0; Matrix2x2(u00, u01, u10, u11, p); // unitary operation }

continued ...

if m1 == 1 { Matrix2x2((0,0), (1,0), (1,0), (0,0), b[0]); //sigma_x }; if m0 == 1 { Matrix2x2((1,0), (0,0), (0,0), (-1,0), b[0]);//sigma_z }; print "teleported state: "; dump b[0];

/* result: : prepared state: : SPECTRUM a[0]: |..0> 0.370046 |0> + 0.629954 |1> : teleported state: : SPECTRUM b[0]: |0..> 0.370046 |0> + 0.629954 |1> */

Shors algorithm (quantum kernel) [Om00, p. 99]


... { { // generate random base x=floor(random() * (number-3)) + 2; } until gcd(x,number)==1; print "chosen random x =",x; //-------------------------------------------------Mix(reg1); // Hadamard transform expn(x, number, reg1, reg2); // modular exponentiation measure reg2; // measure 2nd register dft(reg1); // Fourier transform measure reg1, m; // measure 1st register reset; // clear local registers //-------------------------------------------------if m==0 { // failed if measured 0 print "measured zero in 1st register. trying again ..."; } else { ...; } ...; } ...

Q language (S. Bettelli, L. Serani, T. Calarco) General properties

Relevant papers: Q language library S. Bettelli, L. Serani, T. Calarco: Toward an architecture for quantum programming, 2001 [cs.PL.0103009.ps.gz] general comments: Q language: library extension of C++ quantum operators as objects: can be constructed and manipulated at run-time run-time optimizations are possible

Desiderata for a quantum programming language

completeness: it must be possible to code every quantum algorithm classical extension: the (quantum) language must include a high level classical computing paradigm separability: keep classical and quantum programming separated expressivity: language must provide high level constructs hardware independence

Language primitives

Register handling 1. Register allocation, e. g. Qreg a_register(5); 2. Register addressing and concatenation, e. g. Qreg a_qubit = a_register[3] 3. Register resizing 4. Register deallocation Quantum Operators and their manipulation 1. Identity operator, e. g. Qop my_op; 2. Fixed arity quantum operators, e. g. Qop my_op = QHadamard(7); 3. Macro quantum operators, e. g. Qop my_op = QFourier(7); 4. Qubit line reordering, e. g. Qop a_swap = QSwap(5);

5. Controlled operators, e. g. Qop a_controlled_op(U, 5); 6. Operators for classical functions, e. g. Qop an_oracle = Qop(f, 3, 5); 7. Operator composition, e. g. Qop composed = part_1 & part_2; 8. Operator conjunction, e. g. Qop adj_operator = !an_op; 9. Operator permutations, e. g. Qop split = an_op(2, 3, SPLIT); 10. Application of an operator, e. g. an_operator(a_register); Low level primitives 1. Register initialization and assignment, e. g. Qreg a_register(5, 3); a_register = 7; 2. Register measurement, e. g. int val = a_register.measure(); 3. Low level unitary gates, cf [NC00]

qGCL (J.W. Sanders and P. Zuliani) General properties

Relevant papers: [SZ00]: Quantum programming see also: Paolo Zulianis dissertation: Quantum Programming general comments: extension of Edsger Dijkstras guarded command language, GCL qGCL: expressed in terms of pGCL (probabilistic guarded command language) qGCL: imperative language suitable as a specication language (high level mathematical notation)

mechanism for stepwise renement for program derivation and verication (correctness proof) formal (operational) semantics: pre- and post-conditions are extended to pre- and post-expectations includes three high level quantum primitives: state preparation, evolution, observation is universal language

QPL, Block QPL (P. Selinger) General properties

Relevant paper: see P. Selingers home page [Sel]: Towards a Quantum Programming Language general comments: slogan: quantum data, classical control: no quantum branching language is functional statically typed: program free of run-time errors

two alternative representations of quantum programs: graphical, ow charts textual language admits a denotational semantics by assigning a superoperator to each program fragment: semantics of a quantum ow chart is given as a function from mixed states to mixed states not addressed in the paper: quantum communication
Some additional references related to quantum programming

[Bla00] (Quantum Computers and Quantum Computer Languages. Quantum Assembly and Quantum C ) [Fra02] (Quantum software) [Sam02] (Practical State-charts in C/C++)

References
[Bak96a] G. Baker. Qgol. A system for simulating quantum computations: Theory, Implementations and Insights. PhD thesis, Macquarie University, 1996. www. [Bak96b] Greg Baker. Points from my hounors thesis, 1996. www. [BCS01] S. Bettelli, T. Calarco, and L. Serani. Toward an architecture for quantum programming, 2001. abs. [Bla00] S. Blaha. Cosmos and Consciousness. 1stBooks Library, Bloomington, IN, 2000. excerpt: Quantum Computers and Quantum Computer Languages. Quantum Assembly and Quantum C. www. [Di 00] David P. Di Vincenzo. The physical implementation of quantum computation. Fortschritte der Physik, 48:771783, 2000. abs. [EWZ] Matthias Eck, Pawel Wocjan, and Robert Michael Zeier. Quasi. User Documentation. www. [Fra02] Jaroslaw Francik. Quantum software. Studia Informatica, 23(2A(48)), 2002. www. [KN01] E.H. Knill and M.A. Nielsen. Theory of quantum computation. Supplement III, Encyclopaedia of Mathematics, 2001. abs. [Kni96] Emanuel H. Knill. Conventions for quantum pseudocode, 1996. www. [Mey90] B. Meyer. Introduction to the Theory of Programming Languages. Prentice-Hall, 1990.

[NC00] Michael A. Nielsen and Isaac L Chuang. Quantum Computation and Quantum Information. Cambridge University Press, 2000. [Om98] B. Omer. A procedural formalism for quantum computing. Masters thesis, TU Wien, 1998. www. [Om00] B. Omer. Quantum programming in QCL. Masters thesis, TU Wien, 2000. www. [Om02] B. Omer. Classical concepts in quantum programming, 2002. arXiv: abs. [Sam02] M. Samek. Practical Statecharts in C/C++. CMPBooks, Lawrence, Kansas, 2002. www. [Sel] Peter Selinger. Towards a quantum programming language. www.

[Sma] Eric Smalley. Programming goes quantum. www. [SZ00] J.W. Sanders and P. Zuliani. Quantum programming. In Mathematics of Program Construction, volume 1837 of LNCS, pages 8099. Springer Verlag, 2000. also: Technical Report TR-5-99. [Wal99] J. Wallace. Quantum computer simulators a review (version 2.1), 1999. www. [Zul00] P. Zuliani. Logical reversibility. Technical report, Univ. of Oxford, 2000. Technical Report TR-11-00. [Zul01a] P. Zuliani. Formal reasoning for quantum mechanical nonlocality. Technical report, Univ. of Oxford, 2001. Research Report RR-01-05. [Zul01b] P. Zuliani. Quantum Programming. PhD thesis, University of Oxford, 2001. www.

END OF TALK
and thank you for your interest

Potrebbero piacerti anche