Programming with classical quantum datatypes

Similar documents
THE UNIVERSITY OF CALGARY. Programming with a Quantum Stack. Brett Gordon Giles A THESIS SUBMITTED TO THE FACULTY OF GRADUATE STUDIES

Programming Language Concepts, CS2104 Lecture 3

Roy L. Crole. Operational Semantics Abstract Machines and Correctness. University of Leicester, UK

Quantum Functional Programming Language & Its Denotational Semantics

An Introduction to Z3

Towards a High Level Quantum Programming Language

4th year Project demo presentation

Declarative Computation Model. Conditional. Case statement. Procedure values (2) Procedure values. Sequential declarative computation model

A High Level Programming Language for Quantum Computing

Reasoning About Imperative Programs. COS 441 Slides 10b

Models of Computation. by Costas Busch, LSU

Foundations of Computer Science Lecture 23 Languages: What is Computation?

Quantum Effects. Juliana K. Vizzotto 1 Thorsten Altenkirch 2. Amr Sabry January Yale CS Colloquium Page 1

Quantum data and control made easier

Orthogonality and Algebraic Lambda-Calculus

Register machines L2 18

Operational Semantics

THE UNIVERSITY OF CALGARY FACULTY OF SCIENCE FINAL EXAMINATION COMPUTER SCIENCE 521

Quantum Recursion and Second Quantisation

- Why aren t there more quantum algorithms? - Quantum Programming Languages. By : Amanda Cieslak and Ahmana Tarin

Solutions to EoPL3 Exercises

Normalization by Evaluation

Quantum Computing Virtual Machine. Author: Alexandru Gheorghiu Scientific advisor: PhD. Lorina Negreanu

THE UNIVERSITY OF CALGARY FACULTY OF SCIENCE FINAL EXAMINATION COMPUTER SCIENCE 521

Parallelism and Machine Models

Part I: Definitions and Properties

Depending on equations

Towards a Quantum Programming Language

1 Introduction. 2 Recap The Typed λ-calculus λ. 3 Simple Data Structures

Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL. C. Varela; Adapted w. permission from S. Haridi and P. Van Roy 1

Static Program Analysis

System F. Proofs and Types. Bow-Yaw Wang. Academia Sinica. Spring 2012

Simulating and compiling code for the Sequential Quantum Random Access Machine

Quantum Phase Estimation using Multivalued Logic

Models of Computation, Recall Register Machines. A register machine (sometimes abbreviated to RM) is specified by:

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing

BDD Based Upon Shannon Expansion

Accumulators. A Trivial Example in Oz. A Trivial Example in Prolog. MergeSort Example. Accumulators. Declarative Programming Techniques

COMPUTER SCIENCE TRIPOS

Theorem 1.7 [Bayes' Law]: Assume that,,, are mutually disjoint events in the sample space s.t.. Then Pr( )

THEORY OF COMPILATION

Declarative Programming Techniques

Lecture 11: Data Flow Analysis III

A Logic for Formal Verification of Quantum Programs

Combined Satisfiability Modulo Parametric Theories

Declarative Programming Techniques

Undecidable Problems. Z. Sawa (TU Ostrava) Introd. to Theoretical Computer Science May 12, / 65

Multicore Semantics and Programming

Quantum Recursion and Second Quantisation

Automata Theory CS S-12 Turing Machine Modifications

A Principled approach to Ornamentation in ML

The L Machines are very high-level, in two senses:

Lecture 17: Language Recognition

NICTA Advanced Course. Theorem Proving Principles, Techniques, Applications

CPSC 421: Tutorial #1

Conservation of Information

EXTRACTING COST RECURRENCES FROM SEQUENTIAL AND PARALLEL FUNCTIONAL PROGRAMS

QWIRE Practice: Formal Verification of Quantum Circuits in Coq

EDA045F: Program Analysis LECTURE 10: TYPES 1. Christoph Reichenbach

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

Game semantics for quantum stores

Lambda Calculus! Gunnar Gotshalks! LC-1

Lecture 21: Algebraic Computation Models

1 Introduction (January 21)

Trace semantics: towards a unification of parallel paradigms Stephen Brookes. Department of Computer Science Carnegie Mellon University

Lower and Upper Bound Theory

Advanced Cryptography Quantum Algorithms Christophe Petit

Handout 8: Computation & Hierarchical parsing II. Compute initial state set S 0 Compute initial state set S 0

MP 5 Program Transition Systems and Linear Temporal Logic

Lecture 22: Quantum computational complexity

Type Systems for Quantum Computation

Propositional and Predicate Logic - V

A call-by-name lambda-calculus machine

Single qubit + CNOT gates

Analyse et Conception Formelle. Lesson 4. Proofs with a proof assistant

A Short Introduction to Hoare Logic

SEMANTICS OF PROGRAMMING LANGUAGES Course Notes MC 308

1 The Quantum IO Monad

LECTURE NOTES ON QUANTUM COMPUTATION. Cornell University, Physics , CS 483; Spring, 2005 c 2006, N. David Mermin

The Assignment Axiom (Hoare)

Theory of Computation

Limitations of OCAML records

1 Trees. Listing 1: Node with two child reference. public class ptwochildnode { protected Object data ; protected ptwochildnode l e f t, r i g h t ;

Functional Programming with F# Overview and Basic Concepts

Types and Programming Languages (15-814), Fall 2018 Assignment 4: Data Representation (Sample Solutions)

CA Compiler Construction

Remainders. We learned how to multiply and divide in elementary

Lecture 12: Core State Machines II

Please give details of your answer. A direct answer without explanation is not counted.

Exam 1. March 12th, CS525 - Midterm Exam Solutions

Representing Multidimensional Trees. David Brown, Colin Kern, Alex Lemann, Greg Sandstrom Earlham College

Syntax Analysis Part I. Position of a Parser in the Compiler Model. The Parser. Chapter 4

Principles of Program Analysis: A Sampler of Approaches

COSE312: Compilers. Lecture 17 Intermediate Representation (2)

Extending the Lambda Calculus: An Eager Functional Language

Introduction to Computing II (ITI1121) FINAL EXAMINATION

Compute the Fourier transform on the first register to get x {0,1} n x 0.

(a) Definition of TMs. First Problem of URMs

Since CFL is closed under intersection with regular languages, we can do

Compiling Techniques

Transcription:

Programming with classical quantum datatypes Robin Cockett & Brett Giles (robin,gilesb)@cpsc.ucalgary.ca University of Calgary FMCS 2006, Programming with classical quantum datatypes p. 1/49

Yet another Quantum Programming Language (QPL)... Linear type system with (classically controlled) datatypes No higher-order constructs... Only built-in types are Qbit and Int. Built-in unitary transformations. A QPL program consists of function definitions and main instructions. Compiles to Quantum Stack Machine (QSM). Quantum assembler calculates density matrix. FMCS 2006, Programming with classical quantum datatypes p. 2/49

Semantics... Countably infinite bi-products of completely positive matrices with trace less than or equal to 1. fhilb CPM(fHilb) Mat (CPM(fHilb)) Similar to the semantics of Peter Selinger s QPL. FMCS 2006, Programming with classical quantum datatypes p. 3/49

This QPL... Can declare (classically controlled) datatypes (e.g. lists of Quantum bits). Linear variables are unscoped: live from introduction to first use (cannot be used twice). Classical data extracted from linear variables by use command. Classical variables are scoped: live to end of a block (multiple uses permitted). Quantum bits can be measured, Integers have usual arithmetic functions... FMCS 2006, Programming with classical quantum datatypes p. 4/49

Example datatype declarations type Bool = { True False } type L i s t a = { N i l Cons ( a, ( L i s t a ) ) } type E i t h e r a b = { L e f t ( a ) Right ( b ) } type Maybe a = { Nothing Just ( a ) } type BinTree a = { Leaf ( a ) Br ( ( BinTree a ), ( BinTree a ) ) } FMCS 2006, Programming with classical quantum datatypes p. 5/49

Language constructs continued... Function declaration rotate :: (n : Int,x 1 : Qbit,x 2 : List(Qbit); y 1 : Qbit,y 2 : List(Qbit)) = {statements} Function calls: rotate(n,x,y;x,y); (x,y) = rotate(n,x,y); rotate(n) x y; Had q;not q;not q 1 <=q 2 ;... FMCS 2006, Programming with classical quantum datatypes p. 6/49

Example Program: coin flip # Import Prelude. qpl c f l i p : : ( ; b : Bool ) = { q = 0 > ; Had q ; measure q of 0 > => { b = False } 1 > => { b = True } ; } main : : ( ) = { b = c f l i p ( ) ; } FMCS 2006, Programming with classical quantum datatypes p. 7/49

Language construct: measurement measure q of 0 {...} 1 {...};... measurement is a control construct... FMCS 2006, Programming with classical quantum datatypes p. 8/49

Example Program: Teleport prepare : : ( ; a : Qbit, b : Qbit ) = { a = 0 > ; b = 0 > ; Had a ; Not b <= a } / / C o n t r o l l e d Not t e l e p o r t : : ( x : Qbit ; b : Qbit ) = { ( a, b ) = prepare ( ) ; Not a <= x ; Had x ; measure a of 0 > => { } 1 > => { Not b } ; measure x of 0 > => { } 1 > => {RhoZ b } } FMCS 2006, Programming with classical quantum datatypes p. 9/49

Language constructs continued... Loops:... handled by recursion. Pattern matching: case q of nil {...} cons(x,xs) {...}; FMCS 2006, Programming with classical quantum datatypes p. 10/49

Example Program: append # Import Prelude. qpl append : : ( x : L i s t ( a ), y : L i s t ( a ) ; z : L i s t ( a ) ) = { case x of N i l => { z = y } Cons ( v, vs ) => { z = Cons ( v, append ( vs, y ) ) } } FMCS 2006, Programming with classical quantum datatypes p. 11/49

Language constructs continued... Discarding: discard q 1,...,q n ; implicit discarding of variables on joining of control paths (with warning). Linear assignment is basic: q = expression; q is a linear variable... There is syntactic sugar for classical assignment (see below). FMCS 2006, Programming with classical quantum datatypes p. 12/49

Using classical data... Syntactic sugar: use x 1,...,x n in {statements} x := expr;...; x = expr; use x in {...;} use x 1,...,x n ;...; use x 1,...,x n in {...;} FMCS 2006, Programming with classical quantum datatypes p. 13/49

Example Program: rotate r o t a t e : : ( n : Int, h : Qbit, z : L i s t ( Qbit ) ; h : Qbit, z : L i s t ( Qbit ) ) = { case z of N i l => { discard n ; z = N i l } Cons ( x, y ) => { use n in { R( n ) x h ; m = n+1 } ; r o t a t e (m) h y ; z = Cons ( x, y ) } } FMCS 2006, Programming with classical quantum datatypes p. 14/49

Example Program: Quantum Fourier Transform q f t : : ( qf : L i s t ( Qbit ) ; qf : L i s t ( Qbit ) ) = { case qf of N i l => { qf = N i l } Cons ( head, t a i l ) => { { Had head ; r o t a t e ( 2 ) head t a i l ; q f t t a i l ; qf = Cons ( head, t a i l ) } } FMCS 2006, Programming with classical quantum datatypes p. 15/49

A Quantum stack machine The machine state consists of the quantum stack, a classical stack, and a dump. The quantum stack is represented as a tree, with three types of nodes (Qbit, Datatype, Int) and values at the leaves (zero probability branches are not represented). Operations are done primarily on the top node or top few nodes. Rotation of the tree allows us to bring a node to the top. The dump is used to hold intermediate data during instruction execution. FMCS 2006, Programming with classical quantum datatypes p. 16/49

Qbit Nodes A Qbit is represented by a node with four branches, labeled by 00, 01, 10 and 11 (equivalent to the density matrix notation of a Qbit). Note that physically, we only store those branches with non-zero values at the leaves. FMCS 2006, Programming with classical quantum datatypes p. 17/49

Int nodes Classical data is represented by a node with an arbitrary (finite) number of branches labeled by classical values (e.g. Int values): FMCS 2006, Programming with classical quantum datatypes p. 18/49

Datatype nodes A node with a branch for each constructor which is labeled by the constructor name and the variables the construction binds. FMCS 2006, Programming with classical quantum datatypes p. 19/49

Binding... Variables bound along a branch of a node cannot be rotated above their point of binding... so can not be manipulated directly in the Quantum stack. FMCS 2006, Programming with classical quantum datatypes p. 20/49

Example program evaluation: coin flip... # Import Prelude. qpl c f l i p : : ( ; q : Qbit, r : Bool ) = { q = 0 > ; Had q ; measure q of 0 > => { q = 0 > ; r = True } 1 > => { q = 1 > ; r = False } ; } main : : ( ) = { ( q, b ) = c f l i p ( ) ; } FMCS 2006, Programming with classical quantum datatypes p. 21/49

Empty stack FMCS 2006, Programming with classical quantum datatypes p. 22/49

Introduce Qbit 0 FMCS 2006, Programming with classical quantum datatypes p. 23/49

Hadamard FMCS 2006, Programming with classical quantum datatypes p. 24/49

Measure FMCS 2006, Programming with classical quantum datatypes p. 25/49

Left branch FMCS 2006, Programming with classical quantum datatypes p. 26/49

Left branch: introduce Qbit 0 FMCS 2006, Programming with classical quantum datatypes p. 27/49

Left branch: introduce True FMCS 2006, Programming with classical quantum datatypes p. 28/49

Swap to right branch FMCS 2006, Programming with classical quantum datatypes p. 29/49

Right branch: introduce Qbit 1 FMCS 2006, Programming with classical quantum datatypes p. 30/49

Right branch: introduce False FMCS 2006, Programming with classical quantum datatypes p. 31/49

Merge FMCS 2006, Programming with classical quantum datatypes p. 32/49

Quantum Stack Machine Instructions QPL programs compile to Quantum Stack Machine (QSM) code. QPL QSM QSM code is run by the QSM-assembler. The design of an efficient yet basic instruction set for this machine has been an issue... FMCS 2006, Programming with classical quantum datatypes p. 33/49

QSM Instructions: node construction QLoad x k : creates a node called x with a single branch labeled k on top of the Quantum stack. QCons x cons: creates a node called x with a single branch labeled cons (the specific constructor) on top of the Quantum stack. QMove x: removes the top value of the classical stack and creates, on top of the Quantum stack, a node called x with a single branch labeled by that classical value. QBind z: expects a node with a single branch and binds the variable z down that branch. FMCS 2006, Programming with classical quantum datatypes p. 34/49

Construction Transitions (QLoad x k :C,S,Q,D) = (C,S,x:[ k Q],D) (QCons x c:c,s,q,d) = (C,S,x:[c{} Q],D) (QBind z 0 :C,S,x:[c{z 1,...,z n} Q],D) = (C,S,x:[c{z 0,z 1,...,z n} Q[z 0/z 0 ]],D) (QMove x:c,v:s,q,d) = (C,S,x:[ v Q],D) FMCS 2006, Programming with classical quantum datatypes p. 35/49

QSM Instructions: node destruction QUnbind x: Expects the stack have a node with a single branch on top with a list of bound variables. The first bound variable is removed and renamed to x to make it free. QDiscard: Discards the top node by merging its classical branches. FMCS 2006, Programming with classical quantum datatypes p. 36/49

Destruction Transitions (QDiscard:C,S,x:[ k Q],D) = (C,S,Q,D) (QDiscard:C,S,x:[c{} Q],D) = (C,S,Q,D) (QDiscard:C,S,x:[ v Q],D) = (C,v:S,Q,D) (QUnbind y:c,s,x:[c{z 1,...,z n} Q],D) = (C,S,x:[c{z 2,...,z n} Q[y/z 1]],D) ( k { 0, 1 }) FMCS 2006, Programming with classical quantum datatypes p. 37/49

QSM Instructions: stack manipulation QPullup x: Rotates the node labeled x to the top of the Quantum stack. QApply 0 Had: Expects 1 Qbit on top of the Quantum stack and applies the Hadamard transformation to it. QApply 1 Rotate: Parametrizes the Rotate transform by the top element of the classical stack and then applies it to the 2 Qbits on the top of the stack. QName x y: Renames the (visible) node x to y. FMCS 2006, Programming with classical quantum datatypes p. 38/49

Manipulation Transitions (QPullup x:c,s,q,d) = (C,S, pull(x,q),d (QApply n t:c,v 1 : :v n :S,Q,D) = (C,v 1 : :v n :S, transform([v 1,...,v n ],t,q),d) (QName x y:c,s,q,d) = (C,S,Q[y/x],D) FMCS 2006, Programming with classical quantum datatypes p. 39/49

QSM Instructions: Quantum control; Use; Split; Measure Use label: Expects top of the Quantum stack to be classical. For each branch put this value on the classical stack, perform the computation at label, which is ended by EndQC, and merge the results. Split [Nil label 1,Cons label 2 ]: Expects a datatype node on top of the Quantum stack. Executes the code at label i, which is ended with EndQC, on each branch with a matching label and merges the results. Meas label 1 label 2 : Expects a Qbit node on top of the Quantum stack. Executes the code at label 1, which is ended with EndQC, on the 0 branch and label 2 on the 1 branch and merges the results. FMCS 2006, Programming with classical quantum datatypes p. 40/49

Transitions for Quantum Control - Use (Use C U :C,S,x:[ v i Q i ],D) = (EndQC,S, 0, Qc(S, [(x i :v i Q i, C U )], C, 0):D) (EndQC,S,Q, Qc(S, [(x i :v i Q i, C U )] i=j,...,m, C,Q ):D) = (C U,S,x j, Qc(S, [(x i :v i Q i, C U )] i=j+1,...,m, C,Q + Q ):D) (EndQC,S,Q, Qc(S, [], C,Q ):D) = (C,S,Q + Q,D) FMCS 2006, Programming with classical quantum datatypes p. 41/49

Transitions for Quantum Control - Split (Split [(c i, C i )]:C,S,x:[c i {V i } Q i ],D) = (EndQC,S, 0, Qc(S, [(x i :c i {V i } Q i, C i )], C, 0):D) (EndQC,S,Q, Qc(S, [(x i :c i {V i } Q i, C i )] i=j,...,m, C,Q ):D) = (C j,s,x j, Qc(S, [(x i :c i {V i } Q i, C i )] i=j+1,...,m, C,Q + Q ):D) (EndQC,S,Q, Qc(S, [], C,Q ):D) = (C,S,Q + Q,D) FMCS 2006, Programming with classical quantum datatypes p. 42/49

Transitions for Quantum Control - Measure (Meas C 0 C 1 :C,S,x:[ 0 Q 0, 1 Q 1, Q],D) = (EndQC,S, 0, Qc(S, [(x k : k Q k, C k )] k {0,1}, C, 0):D) (EndQC,S,Q, Qc(S, [(x k : k Q k, C k )] k {0,1}, C,Q ):D) = (C 0,S,x 0, Qc(S, [(x 1 : 1 Q 1, C 1 )], C,Q + Q ):D) (EndQC,S,Q, Qc(S, [(x 1 : 1 Q 1, C 1 )], C,Q ):D) = (C 1,S,x 1, Qc(S, [], C,Q + Q ):D) (EndQC,S,Q, Qc(S, [], C,Q ):D) = (C,S,Q + Q,D) FMCS 2006, Programming with classical quantum datatypes p. 43/49

QSM Instructions: classical control Jump label: Jumps to the code at label. CJump label: Pops the top element of the classical stack if it is true jumps to the code at label. Call label: Pushes the return code pointer onto the dump and jumps to the subroutine code. Return: Pops the code label off the dump and continues execution of the code to which it points. FMCS 2006, Programming with classical quantum datatypes p. 44/49

Transitions for Classical Control (Jump C J :C,S,Q,D) = (C J,S,Q,D) (CJump C J :C, False:S,Q,D) = (C J,S,Q,D) (CJump C J :C, True:S,Q,D) = (C,S,Q,D) (Call n C C :C,v 1 : :v n :S,Q,D) = (C C, [v 1,...,v n ],Q,R(S, C):D) (Return n,v 1 : :v n :S,Q,R(S, C):D) = (C, [v 1,...,v n ]:S,Q,D) FMCS 2006, Programming with classical quantum datatypes p. 45/49

QSM Instructions: classical CGet n: Puts the n th Classical stack value on top of the Classical stack. CApply ADD: Pops the top values off the classical stack, applies the operation and pushes the result back onto the stack. CLoad n: Pushes the constant value n onto the Classical stack. CPop: Pops the top element off the Classical stack. FMCS 2006, Programming with classical quantum datatypes p. 46/49

Transitions for Classical Operations (CPop:C,v:S,Q,D) = (C,S,Q,D) (CGet n:c,v 1 : :v n :S,Q,D) = (c,v n :v 1 : :v n :S,Q,D) (CApply op n :C,v 1 : :v n :S,Q,D) = (C, op n (v 1,...,v n ):S,Q,D) (CLoad n :C,S,Q,D) = (C,n:S,Q,D) FMCS 2006, Programming with classical quantum datatypes p. 47/49

Recursion The machine stack is actually a stream of stacks, where stacks further down the stream correspond to progressive unfoldings of the recursion. The limit of the stream is the computation. Instructions are lifted by the standard Kliesli lifting to produce a function between streams of Quantum stacks. FMCS 2006, Programming with classical quantum datatypes p. 48/49

Conclusion Where are we... State of QPL compiler: Working, generating code! State of quantum stack machine: Working with this instruction set, revising for controlled transforms. State of QPL programs: order finding for Shor s factoring algorithm compiling, and quantum search not done. Where are we going... Expect to complete implementation this fall. Plan to make web runnable version available. Executable compiler and QPL programs will be down-loadable. FMCS 2006, Programming with classical quantum datatypes p. 49/49