Quantum computing with Python

Quantum computers represent a fundamentally new form of hardware, and will require very different software to be written. However, the design of algorithms and handling of inputs and outputs will all be done via conventional programming. There are now a handful of frameworks being built to meet this need, and all but one are based on Python.

This session will serve as an introduction to quantum computing as a whole, and also to the most well-developed and well-used framework built to support it: Qiskit.

To introduce quantum computing I will explain what a quantum circuit is, and what it means to create programs at the level of a quantum circuit. This will be done with a presentation based on our Jupyter notebook based textbook. This presentation will give a simple demonstration of how the variables of quantum computing are fundamentally different than those of standard computing

A fun and simple suggested project is be provided, to give you your first taste of using Qiskit. To provide a simple sandbox for experimentation, this is based on the MicroPython version of Qiskit and for the PewPew (the microcontroller-based games console used for teaching Python). So if you have a PewPew from this year's EuroPython, bring it along!

Preparation

Attendees can just sit and watch, or they can run code as we go. For the latter, it is recommended to have Python 3, Jupyter notebooks and Qiskit installed, and to download the resources here.

This session took place in track PyData and was classified suitable for none domain / basic python by the speaker.

Transcript (auto)

Auto-generated from the recording utilizing Open-Source AI. Speaker labels (Speaker 1, Speaker 2) reflect diarization, not identity. Timestamps refer to the recording.

Speaker 1 [00:03]

Hello, I'm James Wotton. I'm a person who doesn't get on very well with mics, so if there's anything wrong with this mic, let me know. I'm also from IBM Research in Zurich, and I work on quantum computing, and I work on our open source project called Qiskit for doing quantum computing. So in this session, I'll take you through some very basics of quantum computing and Qiskit. And it's a workshop, so you can run code along with me. There's free plans for how you can do this. So the repository you can find at the other end of this shortened link. So go there and you'll get to this repository. Obviously, you can see the URL at the top as well. So yeah, you can download or you can clone this repository. and it has some Jupyter Notebooks in that I'll be using as a basis for this presentation. So if you have the ability to use Jupyter Notebooks and you have Python 3, then all is well with the world. There are a few packages you need. Mostly it's like NumPy, but there's also Matplotlib, which for me is very normal, but maybe not everybody uses it. Plan B is we have this cloud service that you can use, and it hosts Jupyter Notebooks. and then everything is already installed there. It's a nice environment where everything is already set up. Obviously, it needs the internet. And so that's why it's not plan A, because it doesn't go without its hitches. Also, not absolutely everything that I'm going to be presenting is able to run on that cloud service. But plan C is not to run anything. You can just sit and watch and learn about stuff and then go on to do things later. Okay, so what I'm first going to do is give a little bit of an introduction to what quantum computing is and do so with, you know, fancy pictures and buzzwords just to motivate what this is all about. And at the bottom there, which hopefully people can see the bottom, is this shortened link to the repository. so at any point you can always go and have a look at that so yeah I'll go through this well also while you try plan a and plan B and resign yourself to plan C and then we'll see what's going on at the end okay so the first thing to note is well quantum computing has the word quantum in it and if you watch films then well for me sci-fi films often seem like fantasy films except you replace the word magic with quantum and that doesn't really help quantum computing because we're an actual technology able of doing able to do stuff but people have this misconception that it's somehow strange or chaotic or unpredictable so it's it's not it's a science and we can use it for things and And the motivation behind quantum computing, and this is something that has been known since the 80s, is that there are some problems that are just too hard for current computers. And a classic example of this is simulating quantum systems. If you have a relatively large system, like a molecule, then the amount of memory it takes to store the possible states for a large quantum system is huge and then the number of operations you'd have to perform in order to simulate a large quantum system is also huge so you get to the point where you have the kind of molecules that you might be interested in from the perspective of drug design or or creating fuels or trying to understand how plants create fertilizers so much more efficiently than we do and these are just It's too hard to run in the lifespan of a human, even if you run it on a fancy supercomputer. So this is a caffeine molecule. It's very hard for us to know how caffeine does what it does because it's too big. But somehow it does what it does, and probably all of you are living proof of that. It doesn't need to run a program on a supercomputer to tell it what to do. It just does it. quantum systems are good at knowing what quantum systems can do and so if we want to know what a molecule can do well the most naive approach might be to you might think you just get that molecule and look at it and then if you want to know what happens if you prod it in a certain way then you prod it and then you see what happens but trapping and dissecting a molecule is not like dissecting a frog it's a much smaller and harder to reach scale so instead what we can do is make quantum systems that are easy to put information in to read information out and to manipulate so specifically quantum systems that we want to manipulate and then we can use those to simulate other kinds of quantum systems So we do this using little loops of superconductors, and they're so small that you can induce quantum effects on them, and you can control them via microwave pulses. So this is a well-controlled quantum system that we can use to do quantum simulation. But a very important point is something we've known since the 90s, is that there are other kinds of problems that you can do on a quantum computer. so quantum computers are universal you can compile any computation onto a quantum computer I would advise that you don't if you want to play Mario Kart quantum computers are not the place to do that if you want to add numbers then use a normal computer quantum computers are not going to give you any advantages in things like adding numbers but there are various problems such with one of the most famous being factoring. So if I have a number and I wanna know what prime numbers I need to multiply together to get that number, this is something where the computational effort you have to put into that increases exponentially with the number of digits and the number until you get to a point where numbers are so big that you just cannot conceivably factor that number on any device that exists in the lifetime of a human. And that principle is actually used behind a lot of cryptography, that it's essentially impossible to factor numbers and therefore we can use this as part of cryptography. Although quantum computers, there is an algorithm that can factor things much more efficiently than that. So things like factoring, but also optimization algorithms, graph theoretic problems, all kinds of problems that are used in all kinds of areas can be done on a quantum computer so I would I see it like like a graphics card you know you don't run everything on a graphics card but some things are better on a graphics card so you you have the ability to to target a graphics card when you're running a certain operation when you're running a certain problem it's going to be like this with quantum computers you're not gonna it's not going to replace your normal computers but sometimes as part of whatever use case you have you need to solve a certain problem and it's easier just to send that out on the cloud to a quantum computer than it would be to do it on a non-quantum device so we have a an open source framework now to to do quantum computing to set up jobs to send those jobs to get the results It's called Qiskit. There are others also available. There are a few of these available now. All but one of them are based in Python, so Python seems to be taking off as the language of choice for quantum computing. But the most important one is Qiskit, which is the one which was founded by IBM and which is mostly developed by IBM, although we also have a lot of external contributors. So far I've told you how great quantum computing is, but it is very much a technology that's on a journey. In the 80s people started to come up with the idea of it. So in 1981, the first conference on physics of computation co-hosted by MIT and IBM was kind of when quantum computing was born. And IBM was even involved all the way back then. And this is a very IBM centric timeline. timeline so almost every other milestone is IBM other perspectives might write different timelines but the company has been involved quite a lot over the last few decades so we've got to a point now where we have hard prototype hardware that's on the cloud so it's in the hands of everyone but it's not yet okay I i would normally say i've got to adapt to the new era now i was going to say it's not yet possible to do anything that a normal computer can't do but a certain company have done some certain results that have been leaked recently that may challenge that view but nothing you can't do anything useful on a quantum computer that you can't do otherwise we haven't yet reached that goal which is i would say the the more important one but we're very much at a point when you can we can start actually using the technology to do stuff. So what kind of stuff? Well, one thing is to plan for your future use cases. You can look at the, there's something called the Quantum Algorithm Zoo. You can look at this and you can, that link shouldn't be there. You can look at this, you can look at the Quantum Algorithm Zoo and you can see the kind of things that are spread out by a quantum computer and you can try and work out how that might impact you in the future and maybe look at proof of principle applications you can do to prepare yourself for that. So we would say that a revolution is coming in say about 10 years in terms of the usefulness of technology and you should probably start preparing for that now. The cryptography is one case where it's gonna be significantly disrupted by quantum computing and they have already been preparing for a while but also everyone else can get going. So, for example, some of my colleagues at IBM Research in Zurich have done some proof-of-principle results for quantum risk analysis, so improving risk analysis using quantum computation. So probably the most important use over the next few years is that it will be a platform for science. That's actually how I came to use IBM's technology before I joined IBM. had a experiment that I wanted to do and convincing experimental physics groups to do your experiment is very hard but then IBM just put it on the cloud and I could do my experiment in an afternoon which was quite revolutionary so there's a lot of people have been using this for science so if you have a scientific background in the field then it's kind of obvious what you might do if not you can still have a go at the science. I like to liken this to the controls of CERN being put on the internet for anyone to fiddle around with. This is similarly one of the most advanced pieces of experimental physics equipment in the world and yet you can just log into a website and drag and drop some things and press run and run something on a device which is sitting at one of most cold one of the coldest points in the universe this is pretty cool um and i'm plugging our awards throughout these next few slides so for all of the things that we we like and we're trying to encourage uh we have some awards that we've just announced um so uh for people who who do those things they can submit their work to the awards so there's a best paper award if you are are doing science. Another thing is it's a platform for education. So this is also something, which is also one of the use cases for Python. Python is this easy programming language, so it's great for teaching people about programming. Well, quantum computing brings quantum physics to a point where people can easily manipulate it and use it and get hands on with it. So quantum computing in the next few years be a great platform for education and we have various awards for that we also have a textbook and i'll be taking you through some of that textbook today uh and it's also an open source project so if you're looking for an open source project to uh to spend your precious time on then kiss kit is one of them uh so we have an award where you could specifically uh do a circuit do a certain optimization problem which is suitable for people who don't have a quantum background but we also we're on github and you know there's good first issues and there's plenty of ways to get involved and there's plenty of people who haven't got a quantum background that have that have started contributing to this project uh and also you can just have some fun um so uh if you're a hobbyist programmer then you might be always on the lookout for something new and fun to to have a go with and you can try out quantum computing so this is kind of the way i do things with quantum computing a lot as well i look at how quantum computing and games might intersect so this is an example of a procedural generation project that i've done using using qiskit and if you make a quantum game then there's an award for that as well so that is the basics of why quantum computing exists and now we can switch over to this again so um now I'm going to go through some Jupiter notebooks so who a quick show of hands who's got plan a working Wow so this is definitely a Pythonic audience I've tried this before with less pythonic audiences less yeah and who's on plan B well your signups are great for our metric so thank you and then plans plan C okay fair enough so is there any specific issues that people want to do want to ask maybe who are in the boat of plan C or to get themselves that over or you happy yeah okay so the first thing I'm gonna do is one of the first well we have a textbook one of our first sections of introduction to Python and I'm sure you'll hate that section because you're thinking this is not how you should introduce people to Python what the hell kind of syntax is that so we're not going to do that. Then there's also one that's an introduction to Qiskit, but I'm going to do that via this one at the moment. So this is called the Atoms of Computation. And really, when people, sometimes programmers ask me, how does quantum computing differ from normal computing? And I think one of the main differences is you have to start thinking at the machine code level. So if someone asks you what's the, what's kind of the simplest thing you can do in programming, You might say, well, print hello world or add a couple of numbers, but those are quite complicated things. Adding numbers, you've got to represent a number as a bit string, and then another number as another bit string, and then you've got to do all these bitwise operations between them to generate another number? My God, and the number of bits you need for hello world. even worse so you have to kind of change your perspective a bit and come down to the machine code level so that means I'm going to import some stuff here come on so that means getting to the level of of circuit diagrams so who here is who hair looks at that and knows what's going on not many people so yeah circuit diagrams is definitely somewhere to to start so this is this is a way you can express computation in a very simple form in that you have these lines along which bits travel and and then they could go into these gates and these gates are operations so gates are kind of like a function this is a function that takes two bits as an input and gives one bit as an output and this is an alternative kind of function that does the same thing and so this is a computation well this is the whole thing then that takes two bits as an input uh a and b and then gives two bits of an output s and c and then that's your computation so this is this is a certain type of adding that's going this is the way you can add two single bit numbers to get one two bit number so zero zero zero add zero will come out of zero zero zero add one or one add zero would come out as zero one and one add one would come out as, hang on, how does that thing work again? Well, you know, we'll do it throughout. So this is, so if you know what this looks like, then this is the kind of language you'll be taking with quantum computing. But this is the classical version. This is what's happening in your normal computers. These things here, these are what transistors do. Okay, so I've imported my stuff. that's always good. Now the very heart of quantum computing is the quantum circuit and so it it's kind of the quantum version of this so for this you might if you were to make a python object and in fact there's I'm sure there are python packages based around doing circuits but if you were to make a quantum or if you wanted to make a python object which contained this sort of structure then you might declare it in terms of the number of inputs it has and the number of outputs it has and then you'll have some methods that you can use to stick these gates in there they call gates and hook them up so this is basically what we need in our quantum computing so the so we're thinking at the machine code level that means thinking at the level of bits so in the classical circuit we might say how many bits we want as input how many bits we wanted output. In quantum computing, our basic fundamental unit is called the qubit, the quantum bit. And we'll look into what they are over the course of this. And this is what we build out of some sort of physics. At IBM, we build them out of superconductors. There are also other approaches. So when we define this fundamental object, we have to say how many qubits we want in it but also a qubit is this weird quantum thing at the end we want a proper output that we can read as as as non-quantum animals and so at the end we get an output which is bits so when we set up a circuit we ask we tell it how many qubits we want and how many bits of output we want to get out at the end so we define the number of qubits we define the number of bits and then we define our quantum circuit object using the number of qubits in the number of bits so over here I did from Kim put from kids kit import star which is why it took so long but so this quantum circuit object is something that I imported from here so from Chris kit import quantum circuit here's what was going on there so now I've got a quantum circuit I can execute this and yeah if I already use the draw methods of the quantum circuit to show us what's happening in that quantum circuit I get this not much is happening we haven't added anything to this circuit yet so what we can do is add in some quantum gates so these are the basic operations so let's add in something called something called X and we've got two qubits and in this particular example and those two qubit we number qubits from zero so they're called zero and one if we had three qubits they would be called zero one and two and so on and so forth so it's a little bit of a confused confusing case actually having two qubits because they're called zero and one but they're also kind of like bits, which are 0 and 1, but they're not the same 0 and 1. So let's do an x on qubit 1. Did we already do that, actually? Anyway, so we add these gates in, but one thing that's very important is at the end of a circuit, we have to actually remember to extract the output. So what that means is that we use this measure command, and we have two arguments to that. one is the qubit that we are extracting the output from and one is the bit where we are putting the output. So we have this register of bits where we place an output but we also have some qubits from which we extract an output. So here I'm just saying I want qubit zero to give me an output and I want to put that output on bit zero and then I want qubit one to give me an output and then I to put that output on bit one so uh when we we do this then this is what we get as our circuit diagram as our as our visual depiction of what's going on in this fundamental element of a quantum process so here we just we've got this qubit one it's straight away taking an output so qubit zero sorry is straight away getting an output extracted from it qubit one is having this x applied to it and then an output is being extracted so what does that mean well uh of course we'll go through some of this in more detail but let's get to the point where we actually run a circuit so in qiskit to run a circuit you have to first decide where you're going to run it so you set up a back end object so we have different ways to set up a a back-end object but this air is where all of our simulators live because you can either simulate it on your actual device because if you've not if your circuit isn't composed of many qubits you can simulate it quite easily if it's composed of more qubits then it will take more time it will take exponentially an exponential amount of time with the number of qubits you have but for something like this a two qubit circuit is very easily to simulate so this is the the local simulator the easiest way to simulate things and then we run it using the execute command so we tell the execute command what circuit we want to run where we want to run it and typically we run it for many repetitions to get some statistics out and then stuff happens in Qiskit execute gives you a job object from the job object you you run the result method to get a results object and from the results object you run the method to get the exact kind of results that you want which if you're running it all in the simulator then you can just kind of combine it all into one line and forget about all that crazy shit so one so the typical way to get results is this thing called accounts dictionary and the counts dictionary is basically a histogram in the form of a Python dictionary I'm just going to zoom in a bit so it tells you the result you get out and the number of times you get that particular result so how do we interpret this result well it's giving us a bit string so we have two qubits we have a circuit with two qubits and two bits as output so that is because we've got two bits of output is giving us as output a two bit string so that is what we're getting here this is the output from our quantum computation it is a 2 bit string and we ran it a thousand times and every one of those thousand times it gave us the same 2 bit string so we've got a pretty boring circuit here which always gives us the same output and why did it give us that particular output well let's run it again but let's this time not include any gates so this time we are initializing two qubits and straightaway extracting an output from them if we do this then we always get the result 0 0 so what this is telling us is that a qubit is always initialized in such a way that it gives us the output 0 so if we initialize a qubit and straightaway way say give me an output it will give me an output zero and we've got two qubits and so they're both keeping giving us zero and what this X gate is doing is it's it's flipping one of those bits from zero to one so now instead of getting zero zero we get one zero because this bit is flipped from zero to one. Now herein lies one of the most annoying pieces of Qiskit, so concentrate. If you were to take that string and ask say for the first or the element zero of this string, what element would it give you? It would give you the one. So the element of this string labeled zero here is 1, but where did we do the x? We did the x on the qubit labeled 1, and the output from the qubit labeled 1 was put on the bit that Qiskit labels 1. So the bit that Qiskit labels 1 is placed in the string where Python labels that element 0. Basically, it's the wrong way round. Let's call it what it is. It's the wrong way round. So if we added some more in here, for example, just to make it so that we've got more than just two, it's flipping this, bit one, it's flipping output, the output bit one. And so this is, if we asked what is the output bit one of this it wouldn't give us that one that would give us a zero so we have to go to uh hang on two to get that one so uh it's basically it's labeled the it labels the bits the other way around so um yeah just be aware of that if you're if you get confused ever get confused with where your outputs have gone then remember that i told you that something weird is happening and you'll find them uh so also we have this h gate in here on qubit zero so let's see what that does okay well i didn't reinitialize the object so let's do it again once we use this h gate then we start seeing some randomness so we did it on on qubit uh zero which we we output to the bit zero and here we see that half the time it comes out 0 and half of the time it comes out 1. So at this point, this H gate just seems like a coin flip. It adds in some randomness. But it's not just randomness. It's more nuanced than that, as we'll see later. So yeah, this is one particular way of getting results as effectively a histogram and a dictionary form. Another way to get results is to use, instead of get counts here, to use get memory and you also need the keyword memory equals true just to make things complicated and um and then this will give us a list an ordered list of the results so i'll run it the first time and then it will see what the output is and it'll give us that and it'll run it a second time and so on so if we just want a single output we can just do one we can ask for the first element of that list and you will just get a single output every time and as you can see because of the randomness it changes randomly okay so let's do something with this so I'm going to show you how to make a two-bit adder with Qiskit so this is a two-bit adder made with normal computing I'm going to show you the equivalent in kids kit so just to motivate a two-bit adder I'll remind you how to add so I'll take you back to your primary school days I won't take too long about it but I'm sure you you remember but just to get just so we're all on the same page of the terminology I'm using so for example let's let's Let's talk about a number, 9,213. So when we write down 9,213 we write a 9, then a 2, then a 1, then a 3, and why do we do that? Well, it's because we are saying that it's 9,000 plus 200 plus 10 plus 3, but why do we do that? Well, it's because we are used to expressing numbers in terms of powers of 10. So 10 to the 3 is 1,000, so we're saying how many 10 to the 3s we have. 10 to the 2 is 100, so then we're saying how many 10 to the 2s we have, and then we say how many 10 to the 1s we have, and then we say how many 10 to the 0s we have, and so on. So we're expressing a number in terms of its powers of 10. But we could equivalently express a number in terms of its powers of whatever, and in binary we, of course, use powers of 2. So 9,213 could be expressed as 1, 10 to the 13, 0, 10 to the 12, 0, 10 to the 11, 0, 10 to the 10s, 1, 10 to the 9, and so on and so forth, which means that the binary representation of this number is 1, 0, 0, well, no one benefits from me reading that out, does it? And this is actually why Qiskit numbers things the way they do, because this, so if you asked python about the zeroth element of this string it would give you the one on this end but it's actually the one on this end which corresponds to two to the zero and then this one corresponds to two to the one so this is where the the kizkit numbering of um the bit strings comes from it's not the kizkit numbering it's well okay maybe this is the right numbering and Python's got it wrong but it's certainly annoying that there's a different way around sometimes anyway so of course binary strings can be represented used to represent more than just numbers they can be used to represent everything and then also any computation is basically the manipulation of binary strings and and you can you can represent all computation as in terms of fundamental elements so just take two bits and output a single bit so once you have that element then you have everything so one of the a universal logic gate is the nand gate so this is just a way of taking two bits as input and extracting giving one bit of output and everything can be compiled down to nand gates and there's even a course called NAND to Tetris which takes you from just NAND gates to building your own operating system to implementing games in that so if you think NAND gates sound fun who wouldn't? then you can have a look at that okay so let's just look at some binary strings in Qiskit. So now we're going to do some bit strings with eight bits. We're going to set these up in Qiskit. So here's where I'm going to say how many bits there are. And then I'm going to initialize a quantum circuit, which has n qubits and n output bits. Often when people initialize quantum circuits, then they do just use as many qubits as they do output bits. But In general, in quantum computing, these numbers can be different, but it'll always be the same in what we do today. And then I want to extract an output from each of those qubits, so I have to loop over all of my qubits and add one of these measure operations. I should maybe point out, why do we have to have this measure operation? Well, in quantum systems, which is what qubits are, well, how to say it without sounding like a pop science article? Quantum systems change when you observe them, so a measure is not a passive act. You're not just having a little glimpse of what it's doing and then it continues to do what it did forevermore. When you measure something, you are coupling yourself to it and you are changing its behavior from evermore. So the moment at which you extract an output is an important moment in the life of a qubit. And that's why we have to have explicitly these measure operations, which is where the The qubit sends a message down into our normal, classical world. And that's why they're represented in this way. So this is meant to be a very simple representation of kind of like a needle on a measuring device. That's where the symbol comes from. And then it's sending a message down into our normal, everyday world. I should zoom out a little bit now. so this is this is what's happening now I get eight qubits and I immediately extract an output from all of them and then I I want to look at what that output is so if I go down here what I'm going to do is is visualize this I'm visualizing the counts as a histogram so if I if I just put counts in here then it would be one of these dictionaries if I move that up which is the the bit string we get out and then a number of times it came out but we can use one of the tools from kids kit just to make that into a histogram because everyone loves histograms right this is a this is actually this this talk is on the PI data stream so you can't say you don't love histograms so basically we're getting zero zero zero zero well eight zeros out all of the time and this is because, as I said earlier, a qubit, by default, outputs zero. When you initialize a qubit, it always outputs zero. And we're not doing anything to these qubits. We're just extracting an output, so they're all outputting a zero. Now, if we wanted to do something more exciting, then we could put a gate in. So here I'm putting an X gate. So here I'm doing something slightly fancy. so let's look at exactly what I'm doing I'm setting up a quantum circuit here and I'm calling it QC encode and if you look at how I'm initializing that quantum circuit I'm initializing it we have only one argument so I'm saying here is a quantum circuit which has n qubits how many outputs does it have I don't know I'm not including the prospect of extracting an output from this circuit which is a bit weird but you'll see how so okay I have no output so it's completely cut off from from us but we'll see how we get around that later so now I'm putting a gate into this and this is this X gate which is effectively a not gate it just flips a zero to a one so on qubit seven I'm flipping that zero into a one now how do I see that I've done that well actually if you look up here i i called this qc output and i'm basically and this is just consisting of measures this is just consisting of operations which extract an output from a quantum circuit so i'm going to use this to extract outputs from the next few circuits so basically i'm going to take this where i do something actually interesting i do a gate and i'm going to add it onto this which is where i extract outputs and it's going to allow me to to get an output at the end so I I make a circuit which is me adding my encoding circuit which is where I did the gate with my output circuit which is where I'm taking these measurement where I'm doing these measurements and the resulting circuit just takes the first circuit I made and then adds on the second circuit so there was the same number of qubits in both of these circuits so it's very well defined what happens when this when I add I just basically take one of these circuits and the other one and put them next to each other there are many other cases where you can you can add circuits to do different kinds of effects but I won't go for all of those this is just what I'm gonna the kind of syntax I'm going be using here. And now if we run this combined circuit, then we get a result that to me is unexpected. Ah, because I haven't executed this cell yet, so it's still remembering a QC from earlier. So here, always I get out the output 1 and then 7 zeros, just because I flipped one of these bits from 0 to 1 and the rest of them remain 0 so with this I can encode a number and in this case I've encoded the number which is 2 to the 7 and then I can if I choose to I can encode other numbers so if you want to know how old I was when I created this notebook I was that old which I yeah so you see where have I gone so and if you add the output circuit to this you can extract the actual output and you'll get the bit string which in this case will be zero one zero zero zero one zero zero okay so this is just me encoding binary strings in kids kit we have not really gone anywhere quantum yet or done anything well yeah let's say I've done anything interesting so but is everyone fine with how I'm encoding bit strings using qubits for no apparent reason now we can get on to actually doing something so I've as I said earlier if you want to add there's no better computational device to add on than the ones we already have available to us but let's look well adding is a very simple algorithm so if we're trying to look at how to actually do something with quantum computing then seeing how to add it's kind of like a very simple hello world and then from that we can go on to do more complicated stuff so how do how does one add well at primary school what I was taught to do at least was to to write the numbers that I was adding one on top of the other and then you can go down through the columns three plus four is seven one plus five is six two plus eight is ten which means I put a zero down here and I carried a one and then so on and so forth so this this process of adding and two numbers of arbitrary length is compiled down to adding just single digit numbers mostly we just add two single digit numbers at a time but in the case of a carry we add three single digit numbers because here I have to do nine plus one but also carry the one so if we have the ability to add at most three single digit numbers then we have the ability to add anything and if we have the ability to add two single digit numbers we can also use that to add three single digit numbers so this This operation of adding two single-digit numbers is kind of the most fundamental aspect of addition. If we have the ability to do that, then we can take whatever we've used to do that and wire it up many times to make something that can add anything. Now, how does this look in binary? Well, you just do the same thing, but in binary. So 1 add 0 is 1. 0 add 1 is 1. 1 add 1 is 2. and 2 is is 1 0 so in so in the case that we have 1 add 1 is 2 we have to express this as 1 0 and carry the 1 and then we have 1 as 1 as 1 is 1 1 and so on and so forth so the important thing to note about this is that there's basically only once we get it into binary it becomes a lot simpler that if you want to add two single digit numbers which is now which is the the fundamental process of addition there's only four cases you have to be able to fulfill so if you can make some sort of device be it made out of transistors or or the flow of water through a channel or whatever if you can make some sort of device which can encode and perform this operation then you can build any adder you want and so what it has to do is for the case of 0 plus 0 it has to give you 0 0 for the case of 0 plus 1 it has to give you 0 1 for the case of 1 plus 0 it has to also give you 0 1 because this is 1 in binary expressed as a 2-bit string and in the case of 1 plus 1 it has to give you 1 0 because this is 2 in binary so this is a half adder and what we saw at the top with a half adder using boolean logic gates which is basically what happens inside these devices what we're going to do is is to implement a half adder in kids kit so the way we're going to do this is but using a circuit which looks something like this so we're going to have four qubits in the circuit we're going to have two output bits so this is actually an exception to the rule I said earlier. And so what we're going to do is we're going to use two of these circuits to encode our input. We're going to, sorry, do two of these qubits to encode our input. We're going to use the other two qubits to extract our output. And then in the middle, magic's going to happen. So why don't we just use two qubits? And the reason is that quantum computations always have to be reversible, which means that so if you think of normal reversible compute reversible operations means that whatever whatever bit string whatever input you give has to lead to a unique output such that it is possible in principle given an output to infer exactly what the input was now with with with adding this is not reversible in itself because zero plus one and one plus zero these are two separate inputs but they both lead to the same output so if you do this with a report reversible computation you need some sort of somewhere where you're scribbling notes throughout the process so this is why we need more qubits than we have then there are bits in the problem we just need an extra little bit space to ensure reversibility. So we're going to encode our two input bits and we encode these two input bits using x gates so if we want zero plus zero then we have nothing over on the left hand side there so that they just go in in their natural zero states. If we want zero plus one we put an x on one of those to flip it to a one but leave the other one zero and if we want one plus one we put an x on both which is uh what we've depicted here and then um at the end we extract outputs so what do we need to put in the middle well let's look again at what our half adder does in the case so if we look at the output we need to get from a half adder then if we look on the the left hand side of okay let's look first actually on the right hand side of these bit strings. In two cases we get a 0 and in two cases we get a 1. The two cases where we get a 0 on the left hand side are 0 plus 0 and 1 plus 1. So they're the two cases where you add the same thing. You're adding 0 to 0 or 1 to 1. And the two cases where we get a 1 are 0 plus 1 and 1 plus 0. So that bit is basically telling us whether our two input bits are the same or different so that is a piece of information that that is telling us so we need two components to making a half adder one is to work out whether our two inputs are the same or different and then if we look at the left side it is one only in one case which is one plus one so that part tells us are both of our inputs one so to make a half adder we have to answer two questions one is are our inputs the same or different and give a binary output output and the other is our inputs both one or not and those two questions are what are answered in the in normal computing by these two things they are basically the things that we use to answer those two questions this is the AND gate and this is the exclusive or the exclusive or works out whether they're the same or different the and gate works out whether they're both one um but we are not this is i'm not telling you about classical so well i am telling you about classical circuits because i just did but that is not the point of uh this exercise uh so now we need some other operations and um so what i'm gonna so the The operation that's very important for working out whether they're the same or different, which replicates the exclusive OR in some sense, is this thing called the controlled NOT. So, so far, the only gate you've really seen is X, and X just flips from 0 to 1 and 1 to 0. It's kind of boring. This is the controlled NOT, and what this does is it acts on two qubits, and it basically looks at one qubit. Well, okay, what does it do? it does well it applies some quantum stuff but we have to as humans come up with some sort of story to explain so i'm going to tell you the story that everyone uses but really it does some quantum stuff there are other stories that sound completely completely contradictory which also explain what c naught is doing but what one usually says is a c naught has two it involves two qubits we call one of these the control qubit and we call the other one the target qubit and it basically looks at the control qubit to see whether it's a zero or a one if it's a zero the gate itself does nothing as if it wasn't there if the control qubit is zero it is as if this c naught was not even there if the control qubit was a one it is as if you had you applied an x to the target so it's like conditional version of the X it doesn't X on the target which is in this case qubit 1 if the control qubit is in state 1 and if it is in the state 0 else if it's in the state 0 it does nothing and the end effect of this is to perform an exclusible and we can see this by having it in a circuit yeah now why if they're not an execution of this anywhere nearby so let's execute this execute QC we also have already have a back-end setup we'll use the default value of shots don't resolve can't type don't get counts so in this specific case we're getting one one so let's look at a few a few cases as to whether we have this X here or not so in this case our qubits are initialized 0 0 it's given that this one is initialized to be 0 this is this see not does nothing we extract outputs and what do we get at the end we get 0 0 because we initialize it at 0 0 because the controllers in state 0 we effectively do nothing and then we just extract an output and we get 0 0 Similarly, let's apply an x to qubit 1, and in this case, we start off 0, 0, and here, this is still in state 0, this is in state 1, but because this is in state 0, the controlled knot effectively does nothing. We extract an output, this gives us 0, this gives us 1, which means that the result we get is 1, 0, which is 1 from this qubit and 0 from this one. So again, the CNOT isn't doing anything. So let's now have a case where the CNOT actually does something. So here it starts off 0, 0. We do this to flip this 0 to a 1. And then here the control not sees that this is 1, it springs into action by effectively doing another x down here which means that then when we extract the output both of them are in state one so we see the controlled not doing something and one thing that we can note if we if we look at different out different implementations of this is that the output we get on qubit one is actually going to be the exclusive or of the inputs that we give so if these are 0 and 0, the CNOT does nothing, so this remains 0. If this is 1 and 1, then the control NOT flips this 1 into a 0. So in both cases where the inputs here are the same, the output on qubit 1 is 0. But for both cases where the inputs are different, then the output here on qubit 1 is 1. So we get the exclusive OR. We get the same or different information that we need on qubit one by just doing a controlled knot so the controlled knot is what we can use to get the same or different information it tells us whether these two inputs are the same or different unfortunately that's not all we are doing at the moment because it's part of a an addition circuit and as i said the form that we want at the end is for us to have two inputs and two outputs and these the outputs are on different qubits than the inputs so we can't use exactly what we've got here so what we can instead do is form a circuit of this type so here we have two inputs we have two outputs and our exclusive or information is going to be down here so we're doing two controlled knots they're both controlled on the same place so if think about what's happening here if these are both zero neither of these controlled knots do anything this one remains zero if these are both one then both of these controlled knots do something this does an x to flip the zero to a one this does another x to flip the one to a zero and then hence we've got two flips and we're back to our initial zero state and if these are different then one of them flips it and the other one doesn't so we get a one here so our exclusive more information our same or different information about the inputs is now here and it's provided by these so-called controlled knots which are effectively like conditional versions of the x operation now all we need is our next bit of the output which is going to tell us the other bit the carry bit of the output which tells us whether both of our inputs were one so how do we do that Well, we do it using something called a toffoli. So a toffoli, the easiest way to explain it is it's basically like a controlled, controlled not. So this is a controlled not. It does an X here if this is in state one, but a toffoli does an X here if both of these are in state one. And then it's basically already doing the thing we want. So in this one particular case where these are both 1, it flips this. Otherwise, it does nothing, and this remains 0. So then using these X gates just to flip between 0s and 1s, using these controlled NOT gates to get the XOR information, using this to do the job of the NAND, this controlled form of the X, then we have the ability to add numbers so here is where we encode our input if we want to do if we want to add zero to zero we don't do any x's here so zero plus zero equals drum roll zero 0 plus 1 which we are encoding by having this as a 0 and flipping this to a 1 gives us the output 1 and now I know let's get to the really fun bit 1 plus 1 is 2 there are only this many kinds of people in the world those who understand binary and those who don't so this is the very basic level of doing something with Qiskit we're already doing something that we know how to do on normal computers which is to add binary strings but it hopefully gets us into the right mindset it. It gets us thinking at the right level, which is at the level of these bitwise operations. We have to think how we are going to encode our information in the form of a binary string. Then we encode that information. Then we have to think of how we are going to manipulate our information in the form of binary. And in this case, we've done it in a way which is exactly the same as how a normal computer does it. So we'll get no advantage over normal computation doing doing this but in in general in quantum computing we take more complicated problems and we try and do them in a way that is that is better so we can get we can get an output faster than we would on a normal computation so this is the first of the notebooks that I was going through today and so far we have just done we have just looked at qubits being bits so we have completely demystified these qubits hopefully we've taken all of the magic out of qubits we've just used them as bits so now we can start looking at some of what are you yeah So I'll deal more with an H gate in this notebook, but yeah, that's an important point about the CNOT. I told you earlier that the measurement was this point where you extract an output from a qubit and it's the most important point in a qubit's life that it has this measurement um and then i was i was talking about the qubit the the c knot the controlled knot and i was talking about it in this this way that we usually do that the c knot looks at a qubit and based on the state does something now this what does it mean for a c knot to look at a qubit well now Now we're getting into philosophy. It means a different thing for us to look at it. It means a different thing from us doing a measurement to determine its state, and then based on the result of that measurement, doing an x. That would be called a classically controlled x, and it is different. So a CNOT, well, so a CNOT can work even when the state of a qubit is not deterministic. So even when there is some randomness to the zero or one state of the qubit the c knot also works and it serves to um basically spread out that randomness so if you have one qubit and it's random randomly in state zero or one and you do a c knot controlled on that you'll get two qubits that are randomly in state zero zero or one one although this randomness is not the normal kind of randomness that we uh we usually experience it's uh it's um it's it's kind of the tip of a of an iceberg and i'm going to try and explain the randomness inside a qubit including the h gate um in this next notebook and uh in this notebook i actually this notebook is made so it can run on a pew pew just uh because that's more fun than not running it on a pew pew uh i'm not actually going to run oh i saw you there i'm not actually going to run it on a pew pew here so if you don't know what a pew pew is there was one in very large form on the screen uh in the keynote this morning uh but it's it's this it's a microprocessor um based now microcontroller based games console it's got an 8x8 pixel screen. It's got some buttons and you can use Python to make games for it. It's a great fun way of using Python to do something. It's a great way of teaching people how to use Python. So I decided to get a quantum simulator running on this microcontroller so that I could use, so that people could easily make quantum games. And also it helps sort of visualize what's going on here. So I'm going to, instead of using histograms, I'm going to pretend that we've got a pew-pew up on the big screen here. So let's see if that works. And my emulator uses matplotlib because how better to emulate a games console than using matplotlib, eh? Right. Let's try and get this to actually fit on the screen. Okay, I think it is fitting on the screen. It just goes big when it stops running, or maybe not. Okay, I'll have to flip between the different screens for the controller and the screen. So this is a pew-pew, and in this program I've set all of the pixels to a kind of dim value except for that one over there which is bright and we're going to be using actually that pixel down there to display the output of a qubit and we're going to use the controllers to manipulate this qubit so I won't go through how pewpews work because we don't have that much time but here is so here is um i'm using a qubit uh initially and it is initially in state zero and i'm going to represent the binary output of this qubit as the brightness of that pixel down there so if i press up on my gamepad come on work work if I press up, nothing works ok, change of plan that would be the the funnest way to do it if it worked but let's not spend too long making it work on this big screen it is a tiny bit flaky so instead we'll go through this notebook and I'm just going to do ecstatically this is from the web so the important thing about qubits is that they are they are quantum objects and as quantum objects they obey Heisenberg's uncertainty principle so here we here we go with with the last portion of this workshop which is a lecture in quantum mechanics and we begin with Heisenberg's uncertainty principle so if you know if If you've ever heard about quantum, you might have heard about this notion of the uncertainty principle, which means that for a quantum object, for example, you cannot precisely know both its position and its momentum. In theory, if you know its position with absolute precision, then its momentum is completely uncertain. And if you know its momentum with absolute precision, then its position is completely uncertain. and it's randomly anywhere in the universe. So qubits are quantum objects, and they obey this uncertainty principle, but I don't have position and momentum. The only quantities related to a qubit are binary outputs. It gives you zeros and ones. So the way that we get the uncertainty principle with qubits is by noting that this operation that we've been performing, this measure operation, is not the only way of extracting an output from a qubit. So it is a way of extracting an output from a qubit. It is a way of inducing a qubit to tell us whether it is a 0 or a 1. But it's not the only way. And if you go to our cloud-based service and you use the GUI there, instead of on our matplotlib we're visualizing things it looks like now even though I've given up on this it's still running in the background and it's causing my fan to start going a little bit mad so I'll just shut that down a moment the symbol is this so it's the same except it has this Z here so the reason for this Z this Z if you look on this version is a hint that it's not the only way to extract an output so this is this is one way to extract an output but there is also another way and the other way is called so the the normal way which you get by just doing a measure gate is called a Zed measurement so this way of extracting an output is called a Zed measurement this alternative way is called an X measurement so you did and you can implement it like this you could by just doing an H whatever that is we don't I haven't yet explained that and then a measure now why does this work well it does and to be honest if I had more power in Qiskit there wouldn't be a measure operation it would be measure underscore Z or something like that so you're explicitly saying it's a Zed measurement And you would do this by using measure underscore x. But I don't, so we're stuck with it. The physics of the device means that one of these types of measurements is very easy to actually do, actually implement. So with our actual qubits, we always extract a measurement using a Z measurement, and we have to kind of use hacky ways to do the other measurements. But in terms of the actual, the theory behind qubits, this is a distinct way of extracting a measurement, extracting an output, sorry. So this is another way we can extract an output from a qubit. And so there's different ways we can extract an output, but the important thing about a qubit is it can only store a bit of information. So you can't use these two ways of getting an output to extract two different bits. You can't fool it into storing more information than it really can. It can only store a bit's worth of information. So to see this, let's create a circuit QC0, which is just an empty circuit. Let's then do an X measurement of this circuit and run it. And what does it give us? Well, we've been doing this the whole session so far. If you take a qubit and you straight away extract an output, it will give you a zero. So this gives us zero the whole time. But if we instead, instead of extracting this output using the VED measurement, if we instead extract it using the X measurement and we look at the output, then it will randomly be either zero or one. So this is an important principle of qubits. unlike bits which are zero or one and there's basically there's only one way to ask a qubit sorry there's only one way to ask a bit whether it's zero or one and that's just to look at whether it's zero or one but with a qubit there's different ways to extract the output if you use and if you prepare something which is definitely going to give you zero for a z measurement which is what we had here and you instead do an x measurement the result is completely random so you're getting a random output for the x measurement even though the result would have been definite for a z measurement I know this is a little bit abstract but this is a quantum computing session so it does have to come in at some point now we can try doing this h gate which I've kind of introduced a little bit before but we've not really looked too much at it So if I take a circuit and I make it an empty circuit and the only thing I do to it is an H gate, we can look at what effect that has. So now let's extract an output just from that. And we've got two ways of extracting an output. So one of those ways is to do a Z measurement, which is our normal way of extracting an output, and we get a random result. So what is this H gate? Is it like a coin flip, which just makes stuff random? well no because if we instead take that same circuit and do an x measurement then we get a definite result it always comes out zero so what the h has effectively done is just moved the certainty we had a qubit which was very certain about the output it would give for a z measurement and we made it instead very certain about the output it would give for an x measurement and the other one is always random. So these are two different ways of extracting an output and they can't simultaneously be certain about what they're going to give but we can move the certainty around between them. One of them can be certain, the other one not. We can also look at other gates. So there's something called a Y rotation which you can do by doing RY. You can specify the qubit that you're acting on And then you put in an angle in radians, which I'm just putting minus pi by 4. And for this particular example, if I take now this circuit and I measure it in the user Z measurement of that, then the output I get is 87% of the time it comes out 0 and 13% of the time it comes out 1. So it's mostly certain that it's going to be a 1, but not entirely certain. If we instead do an x measurement, in this case, it's mostly certain it's going to be a 1. It's not entirely certain. So we can also kind of share out this certainty between it, but the certainty that a qubit holds is a conserved quantity. It can only have so much certainty. And we can either make the output of a z measurement certain, or we can make the output of an x measurement certain, or we can have a compromise between the two but there is a conserved amount of certainty in there. So that is basically one way in which qubits are different from normal bits. And usually this will be explained in terms of superposition. I don't really like the term superposition because it refers to what's going on in the mathematics, in the linear algebra. So if you want to get into the linear algebra then you'll see a superposition. But if we do it without the linear algebra, we just look at the behavior of a qubit, it's like this. We have multiple ways of extracting outputs, and we have a conserved amount of certainty. And basically, making quantum algorithms is about moving this certainty around in certain ways that we get our input that we've encoded in there, it's certain about our input and then moving it around in certain ways to get our output and this is a completely different approach to creating algorithms than we usually have in a normal computing so I've just been told that we're coming to the end how long do I have how long do I have five minutes Okay, so, I can't explain 50 years of progress in theoretical physics in 5 minutes. 10 minutes, maybe I could have done it. But, okay, 10 minutes I could, well 10 minutes, I say 10 minutes maybe I could have done it to make myself seem like a great teacher, but maybe 10 minutes is also a little bit. but basically if we use the controlled knot let's go back to somewhere where we can actually execute code so now let's do a 2 qubit circuit, quantum circuit 2, 2 and then we do a halomard so this H gate and then we do a controlled knot and well I've just done the ASCII version there rather than the matplotlib so it doesn't look quite as nice but what is this going to output so this is if we think about it in terms of outputs for z measurements then this is randomly either zero or one so this is going to if it sees a random zero it will do nothing here and keep this zero if it sees a one it will flip this to a one so what we would then expect and i can't be bothered to write out the execute command so let's just uh steal it from somewhere else oh i will need to put measurement gates in of course if we put some measurement gates in here qc.measure so i'm just going to put some Zed measurements in QC dot measure and then I run that then it gives us what I just predicted it's either randomly zero zero or one one so it's created something which is random but also correlated if we instead were to do X measurements here which we can do by just having QC dot H zero and QC dot H one when I'll start that from the beginning so in this case I'm so before I was taking this circuit and doing Z outputs and it gave me randomly either zero zero or one one here I do X output I use X measurements instead I extract the output in this other way. It also gives me randomly 0, 0, or 1, 1. And it's kind of weird. So one thing that's telling us is if you look at each qubit individually, it doesn't matter if you're using a Z measurement or an X measurement. It's giving you a random result. And the reason for this is now that, again, it's certainty. There's a certain amount of certainty contained within qubits. And by using operations, we move that certainty around. It started off that they were both certain that if you did a Z measurement, you would get the output zero. And now we find that if we do Z measurements of both or X measurements of both, in both cases, it's correlated. So here we have a certainty that the outputs are going to be the same. The outputs themselves are random, but we have the certainty that they're going to be the same. So we have instead made ourselves certain about certain factors of the combination of the two qubits instead of the qubits on their own so if we start using controlled knots when we don't have certainty on each of the qubits then we run into these interesting effects and actually these effects have things that are beyond what any normal variables in normal computers can do, so if you look in that section of the KitKit textbook there's an example because you might think well, a qubit is like a bit, but from what I've told you about a qubit, it's kind of like a bit, but with a random number generator attached. You could probably very easily write something that reproduces the kind of properties of qubits that I've told you about today. And it tempts you to think that maybe qubits are just equivalent to some pile of bits, just like dicts are, or lists are, or ints are, or anything are. But qubits are a type of variable that are fundamentally different from any kind of variable that we have in normal computers. and it's when we start getting a little bit more into involved into how information it moves around inside these things that we can start seeing now so I'll have to refer you to that to look into afterwards because we don't have enough time for now so one thing I will just touch upon before I stop is if you want to do something interesting with a qubit with with quantum computing then to do something that a normal computer can't you need about 50 qubits and you need maybe a fancy algorithm if you want to do something interesting fun then one qubit is enough so if you look if you experiment with one qubit then with that you can you can do some I've got a suggestion for some procedural generation that you can do so if you want to look into that afterwards as your first example of doing something in Qiskit then that's something you only need one qubit you don't need any physics knowledge but you can do something that's kind of fun and in fact this last weekend was Ludum Dare which is a game jam so if I made a game for that game jam which was based on my procedural generation project so as my final thing I'm gonna I'm gonna make that run. So it's a game. The theme for the game jam was start with nothing. So it's procedural generation, which initially has no seed. So you start, you create the world as you move around on it. And the procedural generation for this is very simple, but it was done using Qiskit, using just one qubit. So this is the kind of thing you can do with only one qubit. It's not going to cure cancer but uh it might be fun and then that can be a first step if you want to start looking at the kind of things that you might be able to do with qiskit over the next decade and also when the revolution comes uh so that's that's basically all i've got to tell you thank you for listening any questions what the yeah so let me think well yes yes is the answer So I think the gates that I had were a universal gate set, which means that they could on their own do everything, but there are much more gates. And in fact, maybe one thing to leave you with is I have this sort of gamified and notebook-based game that you can use to get an introduction to all of the gates, and it's called Hello Quantum. at ibm.biz slash helloquantum and that's a way of finding out more about all of the gates but yeah there are more but they take that same sort of general form any more? okay well hopefully you are motivated to look into some of the other resources that I provided and thank you for coming

James Wootton

About — in the speaker's own words

I'm a scientist and quantum programmer at IBM Research - Zurich. I wrote the much of the early chapters of the Qiskit textbook, and am one of the main contributors to the tutorials for Qiskit, the IBM-led Python-based framework for quantum computing.

I was previously a researcher at the University of Basel, and have lectured there on quantum computing for at the last 7 years.

Social card for talk: Quantum computing with Python