ZX-Quantum-Py A beginner’s guide to Quantum Computing and ZX-Calculus using PyZX Mahnoor Fatima Subhojit Halder Maria Fernanda Vel´asquez Jyoti Faujdar Contents Acknowledgement v 1 Introduction to Vectors 1 1.1 Description of Vector 2 1.1.1 Polar Representation 2 1.1.2 Rectangular Representation 2 1.2 Further Insights Into Vectors 3 1.2.1 Unit Vector 3 1.2.2 Conversion of Vector Representation 3 1.3 Operations on Vectors 5 1.3.1 Addition 5 1.3.2 Subtraction 5 1.3.3 Scaling 5 1.3.4 Multiplication 5 1.4 More Ways of Describing Vectors 6 1.4.1 Matrix Notation 6 2 Introduction to Python 9 2.1 So, What is Python? 9 2.2 Programming Platform Installation 9 2.3 Words and Sentences: Hello, World! 11 2.4 The building blocks of programs 12 2.5 Variables, expressions, and statements 12 2.5.1 Values and types 12 2.5.2 Variables 13 2.5.3 Variable names and keywords 13 2.5.4 Statements 14 2.5.5 Operators and Operands 14 2.5.6 Expressions 14 2.5.7 String operations 15 2.5.8 Asking the user for input 15 2.5.9 Comments 15 2.6 Conditionals 16 2.6.1 Boolean expressions 16 2.6.2 Logical operators 16 2.6.3 Conditional execution 17 2.7 Alternative execution (if, else) 17 2.7.1 Chained conditionals (if, elif, else) 17 2.7.2 Nested conditionals 18 2.8 Functions 18 2.8.1 Function calls 18 2.8.2 Built-in functions 18 i CONTENTS ii 2.8.3 Type conversion functions 18 2.8.4 Math functions 19 2.8.5 Random numbers 19 2.8.6 Adding new functions (def) 20 2.8.7 Why functions? 20 2.9 Iteration 20 2.9.1 Updating variables 20 2.9.2 The while statement 21 2.9.3 Definite for loops 21 2.10 Strings 22 2.10.1 What is a string? 22 2.10.2 Length of a string (len) 22 2.10.3 Slicing strings 22 2.10.4 The in operator 23 2.10.5 String comparison (==) 23 2.11 Lists 23 2.11.1 What is a list? 23 2.11.2 List operations 24 2.12 Dictionaries 24 2.13 Your learning journey 25 2.14 Exercises 25 2.15 Further resources 27 3 ZX-Calculus: First Contact 29 3.1 PyZX: Python Library for ZX-Calculus 29 3.2 Building Blocks 30 3.2.1 Wires 30 3.2.2 Z(α) Vertices 30 3.2.3 X(α) Vertices 31 3.2.4 H Vertices 32 √ 3.2.5 D Vertices 32 3.3 Circuits Notation 32 3.3.1 Vertices 34 4 PyZX: ZX-Calculus Using Python 37 4.1 Overview 37 4.1.1 Circuit class 37 4.1.2 Graph class 38 4.2 TikZiT 39 4.2.1 Setting up the Environment in LATEX 39 4.3 Examples 41 5 Quantum Circuit Optimisation 45 5.1 Optimisation with spiders 45 5.2 Equational Rules 47 5.2.1 T-Rule 47 5.2.2 S-Rule 47 5.2.3 B-Rule 48 5.2.4 K-Rule 48 5.2.5 C-Rule 48 5.3 Circuit Optimisation with PyZX 49 5.3.1 Graph-Level Optimisation 49 CONTENTS iii 5.3.2 Gate-Level Optimisation 49 Author Biography 55 Acknowledgements We would like to extend our heartiest gratitude to our supervisors, Srinjoy Ganguly and Gopika Rao Chaganti, for providing us the opportunity to work on this project and for their constant mentorship, support, and guidance throughout the process. We would also like to thank the QResearch Department, QWorld, for organising QIntern and for sponsoring our research project. Special thanks to Adam Glos and Zolt´an Zimbor´a for coordinating this program. v Chapter 1 Introduction to Vectors Thischapterisabasicintroductiontovectorstheirmathematicaloperations. Ifyouhavealreadystudied them, feel free to proceed to the next chapter! Whenever we want to define a quantity, we use numbers. For instance, if someone asks you how many candies you have, you can answer them in forms of numbers, say 1, 5, or 100. Numbers let you have a concrete idea about the amount of things. Thus, if you know that you have 5 candies and your sibling has 2, you can be sure that you have more candies than your sibling (yay!). The number which describe the ’quantity’ of something is called its magnitude. In science, we call all such things scalar. Because of this, we can say that a scalar is a quantity that can be described alone by its magnitude. Let’s take another example now. Suppose someone is asking you how far your school is from your home. You will make a nice guess and again answer in some sort of number, say 1 mile or 2 kilometers. That’s all good – no one will want you to measure the whole distance on your own anyway. But there’s still a problem. Even if we know that the school is 1 mile away from your home, this does not completely describe the location of the school, because the school could either be in the north, south, east, or west of your home – or anywhere in between. Hence, magnitude alone is not enough to describe the position of your school. 1 mile Figure 1.1: The problem faced with describing the position of the school from home. Every point on the circle is 1 mile away from the home, so saying ’My school is 1 mile away from home’ is not enough; you need to tell the direction as well. In this figure, the direction is north. This problem demands that we add direction to our description as well. Hence, if you say 1 CHAPTER 1. INTRODUCTION TO VECTORS 2 that your school is 1 mile north from your home, this will completely describe the location of your school from your home. That is, we can say that position is completely described by both magnitude and direction. These quantities are called vectors. Thus, a vector is a physical quantity that is completely described by its magnitude and direction (and please keep this in mind; vectors will be your best bud to understand the next chapters!). All the physical quantities are either scalars or vectors, about which you will study in your high school physics. But for now, this much description will be sufficient for the rest of the book. 1.1 Description of Vector Just like we use numbers to describe scalars, we have different systems to describe vectors. 1.1.1 Polar Representation I’ll be referring back to Fig. 1.1. Let’s look at it from a different perspective. In the image, the magnitude of the position is given as the radius of the circle (in our case: 1 mile). So, all we need to do is describe the direction. For that, we will use angles. Suppose we take east as 0°. In that case, north will be described as 90° away from it. Thus, in polar representation, our position vector can be written as 1|[phase,,label = [phaselabel,]90°]| mile. This is shown in Fig. 1.2. 1 mile 90° Figure 1.2: Redefining Fig. 1.1 to show that the position vector can be represented with a radius and an angle, otherwise known as the polar representation of vectors. The images of house and school have been removed for convenience Generally speaking, the polar representation of a vector comprises a magnitude r and an angle θ; the vector itself is written as r|[phase,,label = [phaselabel,]θ]|. 1.1.2 Rectangular Representation Let’s look back at Fig. 1.1. In fact, let’s bring it on an x-y plane as shown in Fig. 1.3. Here, we can see that the coordinates of the school are (0,10) if we consider one big box (marked by a slightly bolder grey line on the grid) as 0.1 mile. We can write this in vector notation as 10 yˆ miles. Here, yˆ simply means that this is the y-component of the vector. The meaning of the hat will be described shortly. Similarly the coordinates of the red vector are (8, 6) and we can write this as 8xˆ + 6yˆ miles.