Start Writing Quantum Code with Abby Mitchell: Qiskit Summer School 2024

6.36k views8805 WordsCopy TextShare
Qiskit
This lecture is a broad overview of Qiskit's quantum computing software capabilities, including the ...
Video Transcript:
hello kiss kitters and welcome to lecture one of the kiss kit Global summer school 2024 my name is Abby Mitchell I am the developer advocacy lead here at IB on Quantum and I am going to be taking you through an introduction to kiss kit 1.0 okay let's Dive Right In before we get started I do want to explain that this lecture is going to be a little bit different from other introduction lectures from previous summer schools so if you've attended previous Summer Schools um this format might be a little bit different um I am mostly
going to be focusing on software and what we've normally done in the past is focus more in the first lectures on uh quantum theory so what I really recommend doing if you haven't done so already is um as a prerequisite go and check out John's course on understanding Quantum information and computation this will get you up to speed on a lot of the uh Quantum information principles that are behind a lot of the software Concepts that I'm going to be taking you through today and you can check out that course either on learning. Quantum ibm.com
or on our kkit YouTube channel okay now diving into the anatomy of a Quantum Computing service how does a you you know a Quantum program go from being on your laptop with you as the end user through to actually being run on a real Quantum device well the first thing that you will need to do as a user is you will need to use some sort of quantum programming language in this case it's going to be kiss kit to construct a Quantum program then what you will be doing is using a cloudbased service in this
case it will be kkit runtime to package up that program and send it through the cloud until it hits a server in a laboratory somewhere then that program will go through some control Electronics you don't need to worry too much about this piece but essentially the outcome of of this step is that you will your program will be converted into microwave pulse sequences that can then be run on a real Quantum processor once your program has been run it will we'll get some results back and that will kind of get prop ated all the way
back up the chain back to you uh the end user on your laptop so this sort of gives you an overview of a very high level of of the full process and kind of indicates there's a lot of stuff going on behind the scenes in the cloud that maybe you as the end user aren't aware of um for the purposes of this lecture we are just going to be focusing on this section the stuff that you the user will be directly interacting with and an example of what a final result might look like from a
Quantum program is something like this you will get a series of bit strings many zeros and ones um these in a classical format um and you will typically have run the program multiple times on the quantum device and so you will get probabilities back of how you know did you get a certain bit string more so others so you can see in this graph here we're sort of visually representing how the result from a Quantum program gave a lot of results that were 00 0 and a lot of results that were 111 and this is
probably the the answer to the experiment that you are trying to run but then we also get a lot of much smaller counts of different bit strings here and this uh shows that there are some on uh the quantum device um we're going to talk about this a little bit more later but Quantum devices are by are not perfect you will often get you know small numbers of incorrect bit strings from the outcome of your um experiment and this is why you have to run your program multiple times so that you can start to see
okay these are these are where the errors are and you will we'll also talk a little bit about different things you can do to prevent or mitigate um these kinds of Errors ideally we would only want to see two bars in this result image now let's talk about what is kiss kit the tools that we are going to be spending the majority of the time today talking about there are many different things that sort of come under the name of Kiss kit um what we are going to be focusing on today is the kiss kit
SDK which is an open-source SDK for working with quantum computers at the level of extended Quantum circuits operators and Primitives we will cover those terms in a little bit um if you want to check out the open source repository for kiss kit you can find that on GitHub and there's also plenty of documentation um on our Quantum platform that you can check out as well we're also going to be talking a lot about kiss kit runtime this is the cloud-based service for executing your Quantum computations on IBM Quantum Hardware specifically um kiss kit runtime the
service is not open source but the client that you use to interact with that service is and you can check out that repo on GitHub as well and again there's also plenty of documentation on our documentation platform too next up you may have heard the term kiss kit patterns before we are going to be using this quite a lot in this lecture today uh kiss kit patterns is a sort of a general framework for how you can break down a problem um into different stages to build a Quantum program around so it's it's you can
think of it as sort of a um some guidelines for how you might construct a a full Quantum program including um everything from building your initial circuit through to running it on real hardware and getting the results at the end lastly there are many other packages out there in the software um ecosystem that uh are not kiss kit itself but they maybe extend kis kit they use kiss kit a lot and they can be useful additions to your toolbox of Kiss kit SDK and kiss kit runtime and we generally refer to these packages as part
of the kiss kit ecosystem uh we if you go to kit. github.io ecosystem you can see a catalog of some of um or quite a few of the projects that are in uh the broader kisy ecosystem some maintained by IBM some maintained by members of the open source community and the reason that I have called this lecture um introduction to kkit 1.0 is that quite excitingly earlier this year in February the kis kit SDK reached version 1.0 this is a big milestone in the Journey of a Quantum software package and it normally sort of brings
in a new era of stability and performance for a given package so it's really exciting that we'll be using um sort of the really the latest and greatest um package that the kiss kit SDK is today and sort of on the other side of that coin uh kis kit runtime was also updated with kis kit 1.0 so we now have a version two of the runtime Primitives we're going to explain what Primitives are in a minute don't worry just remember that there is a thing called Primitives and we are now using version two of them
and I will point this out in the code when we get to it later okay so now that we've given a little bit of an overview of what it means to run a Quantum program what do the results kind of look like when you get them back uh what kis kit is now we're going to start diving into some actual code starting with how do we actually get started setting up kiss kit and installing it the first thing you're going to want to do is install the kiss kit SDK which you can do with Pip
install kiss kit what I've also highlighted here is you I definitely recommend um also installing kis kit with the additional visualization uh capabilities this is uh functionality that you need especially um when you're um doing the type of work that I'm going to show today for visualizing circuits visualizing results um I recommend inst installing this additional add-on uh we're also going to need to install uh kiss kit runtime client which you can do by pip install kkit IBM runtime and if you need a little bit more in-depth instructions on how to do these installations how
to get your python environment set up definitely go and check out docs. quantum.com for/ start SL install that will give you a more detailed sort of step-by-step run through there's also a video there to help you um if you are not familiar with the installation process as well as installing those python packages onto your local machine you will also need to make sure that you have access to IBM Hardware which you can do by logging into the IBM Quantum platform or through IBM Cloud um and again I recommend if you haven't done this already go
and check out the documentation in the setup Channel section we'll kind of give you step-by-step uh instru on how to do that and also how to choose if you should use the IBM Quantum platform or IBM Cloud to access IBM Hardware okay now that we have our package installed hope hopefully we have our local environment setup now let's talk about how do we actually construct a Quantum program uh and this is the point where we want to introduce kis kit patterns in more depth uh the four steps you see here are the four four main
steps that make up the kis kit patterns framework um so a single kis kit pattern is made up of uh firstly mapping your problem from whatever sort of domain it's in to Quantum circuits and operators which will actually be um sort of understood by the hardware step two is then taking that circuit and optimizing it for the specific back end that you wish to run your program on step three is then sending it to the quantum computer letting it do it do its thing and then getting some results back and then step four is all
about now that you have these results what sort of post-processing do you need to do to sort of analyze them and get valuable insights so we're going to start off with the first step mapping your problem to Quantum circuits and operators this is a whole lecture Series in it s and I think you're going to hear a lot more about this later on in the summer school so I'm just going to talk about it at a very high level here but essentially what this step focuses on is taking the the real world problem you're trying
to solve typically it is a um a very complicated mathematical problem could be you know simulating a molecule in chemistry it could be figuring out some sort of um optimization calculation generally you start with a with a very complicated equation um or some kind of numerical representation and then what you need to do is translate that numerical representation into a Quantum Computing representation which normally takes the form of a Quantum circuit and what you also will need to do as part of that process is there's typically a few different Quantum algorithms that you can select
to help you with that um with that process so building a circuit with kiss kit is a key part of this phase and I'm just going to take a little bit of time now to sort of talk you through what a Quantum circuit actually looks like if you are familiar with classical programming maybe this imagery is somewhat similar to you um but this image that I have here in the center of the slide is this is Quantum circuit it's a very simple one it only has two cubits in it each black horizontal line here represents
a cubit on the device and each block here you have different colors um different they're doing different things these represent operations that can be done on that Cubit so here this big red square this is a hamod gate uh this is a really fundamental gate in Quantum Computing it is the gate that will put your Cubit into a super position the next one here this sort of um blue line and and plus sign this is a two Cubit gate so it doesn't just act on a single Cubit it acts on two cubits this is called
a controlled knot gate what a controlled knot gate does is um it sort of it flips the second Cubit from 0 to 1 depending on what the uh value is of the Cubit then these sort of gray boxes here these represent measurement Gates we somehow need to be able to convert um you know Quantum Computing data into Data that a classical computer can understand and send to us back at home on our real laptops so to do this uh we have to do a measurement on the Cubit and at the point of measurement the Cubit
will then collapse to either a classical zero or classical one and that value will be recorded on a classical bit which is represented by this double Gray Line so here we have um two classical bits and it's recording one value from Cubit one cubit Zer sorry and another value from Cubit one so how do we actually code the circuit that I just showed you before now we're going to dive into the kkit SDK code syntax itself um as you can see it's based on um py the Python programming language which is great especially if you
are maybe not coming from a uh classical software domain maybe you're coming more from a scientific domain python is a one of the easier coding languages to get started with so the fact that kis kit is written in Python makes it more quite intuitive and and not too difficult to pick up um the first thing that we have to do is we have to import a Quantum circuit and a Quantum register class from kkit and then we have to initialize them a Quantum register is a collection of cubits so for that circuit that we just
saw it had two cubits so I'm initializing a Quantum register with with two and then we need to put that Quantum those cubits into a Quantum circuit so we are initializing a Quantum circuit like this and then we are passing the cubits as an argument to it then once we have our circuit we want to apply those Gates that hadam mod gate that c not those measurement Gates as well to do that we call circuit Doh for a hadam mod gate and then we pass in the label of the uh Cubit that we want to
apply that gate onto and we do the same for the controlled not gate this time we uh call that uh function using CX and this this time because it's a two Cubit gate we're passing in the two cubits that we want to apply that gate onto lastly we want to add a measurement gate onto each Cubit um luckily we don't have to put in if we know we want to measure all of the cubits we don't have to put in you know every single Cubit there uh we can just call this measure all function which
will do it all for us that becomes really helpful if you have a 100 Cubit circuit and you don't want to list out all 100 cubits lastly um uh in this code example we're calling circuit. draw this is um why I mentioned earlier it's important for you to install kiss kit with the additional visualization um component because that will enable you to call this draw function which is what will um sort of render this nice visual here for you so if you want to visualize how your circuit actually looks you're going to need to use
this draw function to do that and I'm just passing an MPL um uh argument here this is just for a styling um thing there's different types of styles that you can render your circuit in uh MPL is just the prettiest okay so what I showed you was you know a very very minimal example of what a Quantum circuit um of how you can actually code a Quantum circuit with kis kit but kkit itself is a lot more powerful than just that small little snippet that I showed you and we're just going through an introdu ction
here today but you know at some point especially through the course of the next two weeks and through the rest of your Quantum Journey you will want to start scaling up and start using some of these much more complex uh functionality and kiss kit itself has um a bunch of different operations gates in its uh uh you know in the codebase to help you with this we have uh what's called standard Gates these are your very typical um sort of collection of gates like the hadam mod gate is here the C not gate is here
a couple of other Classics um and then there's also um variational uh parameterized Gates as well these are Gates where you can submit um sort of custom rotations to them I'm not going to go into a lot of detail about that um but if you're familiar with um variational algorithms these types of gates are really important for that okay now that we have um gone through the step of mapping a problem to a Quantum circuit creating that Quantum circuit using kis kit next up we are going to uh move on to the second step in
a kis kit pattern which is optimizing that circuit there are typically two different ways for you to do this sort of optimization step the first one is absolutely essential um and that is transpilation where you have to take the circuit that you created and convert it into something that can run on the specific Hardware that you have selected for your computation the other part of optimization which is more optional is that some people um find that it's helpful to use some kind of simulation techniques to just verify that their circuit has been constructed in the
right way uh before they make the jump to running it on a real device so to start off with let's talk about transpilation to understand why transpilation is important we need to understand um that real Quantum devices are subject to various constraints the first one is this uh thing called a basis gate set the circuit that you can create using the kiss kit circuit library or building your own custom Gates isn't necessarily the exact same uh Gates that you can use on a real device a real device typically maybe only has four or five core
kind of basis Gates that can actually be run so any circuit that you create if you if you created an abstract circuit which doesn't which uses a different gate that isn't part of that basis gate set that gate needs to be converted into um some combination of those basis Gates and this can you know turn in your abstract circuit something that looks like only one gate might actually be made up of three basis Gates so that can extend the length of your circuit which can have implications for for how long it takes to run and
how good the results are that you're going to get back another constraint is Cubit connectivity this little diagram here shows sort of the layout of what the how cubits are actually connected on a real device and you can sort of see here some each dot represents a cubit and not all cubits are connected to every other Cubit and not all Cubit has cubits have multiple connections some cubits can only be connected to one other Cubit some cubits can be connected to two other cubits and some cubits can be connected to three um so it's really
important that you need to be able to convert your circuit into something that has the correct connectivity make sure that you have any cubits that need to be connected to two other cubits are actually mapped to the correct Cubit here so that you can facilitate those connections and the last thing we've touched on it a little bit before is this idea of Errors Quantum devices are not perfect every operation that you add every gate that you add to your circuit has a chance of of of not going not going um correctly and it can generate
an error so there are other optimizations you can do to reduce the chance of these errors occurring for example reducing the number of CN not Gates that you have um two cubic Gates tend to have a higher chance of erroring than single cubic gates for example um or just reducing the depth of your circuit so if you have a circuit with only 10 operations deep it's going to probably give you better results than if a circuit that is 20 uh Gates deep so all of these things sort of you know mix together to um you
know form the the the things that you want to be considering when you are trying to optimize your circuit and this kind of presents this challenge right where you want to be able to run an abstract circuit on a specific Quantum device which has a specific connectivity a specific basis gate set um and the way to kind of do this is through transpilation um this is when you are essentially converting that abstract circuit into what is called an Isa circuit an instruction set architecture circuit you'll see this term come up a lot so burn it
into your brain an Isa circuit is a circuit that is um optimized and and specifically runnable on the device that you have have chosen digging into these this idea of Transportation a bit more there are some other terms that you should be familiar with and that is the idea of a past the idea of a past manager and the idea of a staged past manager um it's also helpful sometimes to think of this in terms of an analogy I quite like this Orchestra um way of of this Orchestra metaphor um starting with a pass this
is a single transformation that you might make on a circuit and you can think of that as maybe a single instrument in an orchestra like a violin for example a past manager is a list of those transpiler passes that are grouped into a single logical unit so if a pass is a violin then an instrument section might be the string section of an orchestra and finally a staged pass manager is a list of pass managers um each one representing sort of a discrete stage in your transpilation pipeline so you can sort of think of this
as the conductor of the orchestra the one that pulls all those different sections together and helps them play in harmony so how do we actually do transpilation with kkit um luckily there is a lot of built-in tooling that can do a lot of this um for you out of the box but uh what you should also know is the example I'm showing you here might just be a very sort of simple example what's really powerful about the transpiler and kiss kit is that it is very customizable so you can do the things that I'm showing
you here um from scratch yourself and make your own tweet tweaks and your own decisions if you um feel like you know better than what the trans pilot could uh could do for you so let's dig into this code a little bit more you should be familiar with this section here it's the same as before where we just created a sing a simple um Quantum circuit this this next section here is the important bits that's showing you the transpilation first of all we need to initialize the kiss kit runtime service because remember we are going
to be sending uh this program eventually to um an IBN Quantum device through this kis kit runtime cloud service um and so we need to initialize this service and we want to be able to select the back end remember transpilation is um is converting your circuit into something that is specific for your backend so you need to have chosen your back end ahead of time to be able to do this transpilation so here what we're doing is we are calling service. backend and we're passing the name of the backend that we've chosen for this example
it's just IBM IBM Brisbane Brisbane um then what we're going to do is create a past manager remember a past manager is a list of transpiler passes and uh kkit has a very helpful um sort of beginner um pass manager function for you so if you aren't yet yet confident enough with transpilation to construct your own combination of passes uh kkit has this generate generate preset past manager for you which will sort of do it automatically and the important things that you want to pass to this pass generate preset past manager function is first of
all the back end so that we know that we're transpiling for this specific device that we're going to be using and next is the optimization level this is a sort of a a blunt way of us saying how how how good do you want your your circuit to be optimized through this transpilation process you can start with um I think there's three optimization levels and the higher your optimization level with level three being the highest the more optimized your circuit is but that does come with a caveat of it taking a lot longer for the
transpiler to actually generate that optimization for you so if you have a really big circuit it's probably going to take you while to do that transpilation process if you're going for that higher optimization level three and then finally to actually generate this Isa circuit that we can send to our device we need to call Pass manager. Run and then pass our original circuit that we created to it um and here I've just drawn for you what that might look like as you can see this circuit looks quite different from that initial um you know hadamard
and scene not gate that we had at the start so what are the different types of transpilation pass within a uh any form of transpilation um there are actually six stages to transpilation 2 3 four five six um don't worry about memorizing this all right now there is plenty of documentation that you can have in your back pocket just have it on hand to remind yourself um um I'm just going to go through them very briefly um the first stage is um initialization this is quite a you know a a smaller stage it doesn't do
an awful lot um mostly what this stage is for is converting multi Cubit Gates into two Cubit one or two Cubit Gates um when you're creating an abstract circuit you might be able to create add a gate which has um that that you know spans four or five cubits multiple cubits but in reality the device can only handle two cubic Gates so that multi cubic gate needs to be converted to two cubic Gates next up is the layout stage so what happens here is the cubits in your abstract circuit are mapped onto physical cubits on
the device and then this then moves on to the roting stage where sometimes when we've laid out the cubits you know the ones that are need to interact aren't next to each other so we need to insert swap Gates into the circuit to enable interactions between cubits that need to interact with each other but maybe aren't physically located next to each other on the device step four is translation this is where every gate in your circuit is converted into that into a basis gate from that basis gate set that I mentioned before and each device
might have a different basis gate set so this is something that's very specific to the device the fifth step is um some further optimizations things um and different things can be done here for example you might rewrite the circuit to make it shorter um remember we said that the you know the shorter the depth of your circuit probably the less chance that is of Errors happening because the cubits have a finite lifetime before they decohere so you want to try and get your your U operations done as quickly as possible so you have the greatest
chance of that Cubit um you know sort of staying coherent within that time um and then lastly we have scheduling which is just when you add certain delay instructions here so you need to when you're when you're running the circuit sometimes you need to make sure that um you know certain operations are done um you know in a certain sequence between the different cubits so sometimes you need to add delay instructions to say okay you have to wait uh before we can run this operation on the circuit apart from the transpilation um code that I
showed you earlier that just uses the kiss kit SDK there are some additional um tools out there that are useful for uh transpilation that you might want to explore in your own time um the first one is quite new it's the kiss kit transpiler service this is a cloud-based service for doing transpilation and it also includes some AI powered transpiler passes as well which is pretty cool um then within the broader kkit ecosystem there are many different packages or projects out there that might optimize certain bits of the transportation pipeline um circuit knitting toolbox is
one example of some optimization that you can do as a package that you might want to go look at um and lastly you can also write your own transpiler plugins I mentioned earlier that the example I showed you was just a really basic example um but what you can do is if you learn more about transpilation techniques you might decide to uh create a transpiler pass that you think um is better or more customized to your use case than something that kiss kit provides and you can create that yourself and kiss kits interface is um
very plug inable so you can turn your pass into a plug-in that you can then use directly in your kis kit transpilation pipeline okay now that we've um covered the basics of transpilation let's move on to the other type of optimization that you might do in that step of a kis kit pattern and that is verifying your circuit with uh simulation there are a few different simulation tools that um You probably want to be aware of um the first one is kiss the kiss kit SDK has reference Primitives we're going to talk about Primitives a
little bit later um but in kis kit itself it's just good to be aware that kis kit has its own version of of Primitives that can do exact simulation but really only for very small circuits and you can't simulate any type of Noise with them so this is really just for very basic testing out if you youve you know initialized your primitive properly that type of thing um the next option that you have is the kis kit runtime local testing mode um this is a useful tool if you want to test out how to interact
with the um with the runtime cloud service um and it provides a number of fake backends that sort of mimic the real devices that IBM has as obviously not to the same quality level um but it's useful for for example when you're doing transpilation maybe you want to use those fake backends just to like test out how you're doing transpilation and things and sometimes it's useful to be able to mimic a specific backend um including the type of noise that is um you know present in that device and here's a code example of of how
the same um the the same kind of insation process we showed earlier might work this time with a a fake back end instead lastly we have kkit air which is a kkit ecosystem project this is maybe a slightly more powerful um simulator than the other two options you can do slightly larger circuits also supports stabilizer circuits and noise models as well but it is important to note that there is an inherent problem with um using simulators um and that is you know the reason why quantum computers are exist in the first place is because there
a certain size of computation um you know classical devices just don't work anymore um you know maybe if you have a pretty good Mac and a pretty simple program you could simulate maybe 50 cubits um really depends on on the specifics of the the the circuit you're trying to to simulate um but this is where you know simulation really just becomes a stepping stone to get you to Hardware um some things that you need to think about is like if you're trying to scale up to utility scale circuits when you're using 100 plus cubits you
know simulation is not going to be able to get you the the answers that that you need um so then the question becomes how do you you know test out that your you know 100 plus cubits circuit is actually going to work properly when you send it to the real device um and there's sort of two different ways that you can generally approach this problem during that optimized step of a kis kit pattern the first way is maybe you want to test smaller versions of your circuit um kind of break it up into chunks or
or simplify it so that it's something you can you can test out just to kind of give you an intuition for is did you construct your circuit you know in the right way um another option is changing the circuit to something that becomes uh classically simulatable um this is um what's known as a stabilizer circuit or a Clifford circuit it's a circuit that's made up of a very specific combination of gates that are can be efficiently run on a simulator but obviously if you're modifying that circuit it's not it's not a one to one you
know it's not going to be exactly like the original circuit you had so your the answer you get at the end end is it going to be uh is going to be an approximation of the correct answer but only when you actually run it on the real device will you get the correct one the one that you're looking for so that's just some some caveats with simulation they can be a great tool for debugging your Quantum program but they are not ever going to be you know a really good replacement for utility scale um Quantum
computation okay now we're going to move on to the third step in a kiss kit pattern and that is finally we are executing our Quantum program on a real device this is where Primitives becomes an important concept to understand um and what A Primitive really does is it encapsulates the output of a Quantum circuit and there are two different types of Primitives that are available to you um through kiss kit and kiss kit runtime the first one is a sampler primitive this is the one this is the the Primitive you want to use if the
output that you want from your computation is um a series of bit strings mapped to how many counts you got so if you ran the circuit 100 times how many times did you get a zero bit string how many times you get a one bit string the sampler primitive will produce the output in that format for you and then next up we have the estimator primitive the output of an estimator primitive is an expectation value of an observable so this really depends on the specific of the the problem that you're trying to solve with your
Quantum program and this can be very domain specific so for example if you're looking if you're trying to solve some um numerical problem in high energy pH physics maybe the observable that you are looking for is um the spin of of an electron or a system um and so your expectation value um is what is you're looking for here it's just a single result that you get um an important uh distinction when you are calling a sampler primitive versus an estimator primitive a sampler primitive you want to make sure that your circuits include measurement Gates
whereas for an estimator Primitives you don't want measurement gates in that circuit so what does it like in code to run a Quantum program using um The Primitives the first example I have here is of the sampler primitive so here we have started we haven't used the same we're not using the same circuit we used earlier instead we are um we just chose a standard circuit from the kiss kit circuit Library um so again a really useful feature of Kiss kit is it has its library of pre-built circuits which is perfect for for for us
in this small example we're just um sort of generating an iqp circuit with um you know this this selected number random number of um with uh sorry with 127 cubits and this sort of random um configuration here um we have also initialized the kkit runtime service I showed you an example of that earlier as well um and this time when we're selecting uh back end um we're going to use this helpful function from the runtime service to select which of the quantum devices is the least busy um and is not a simulator and has to
have 127 cubits for for this example um this is a great function to use especially because you know there are a limited number of quantum devices out there in the world and everyone wants to use them so normally um there can be um you might have to wait in a cube before your UH before your program actually gets run so by using this least busy uh function you will select the device that has the shortest queue that you have access to um so it could save you a bit of time so we have this circuit
we have done our transpilation um just with the basic um generate preset pass manager option with optimization level one we saw that earlier in the transpile section and then this final step here is where we are actually um initializing the sampler and we are passing the back into it and then to actually run the execution we're going to call sampler. run and then we're passing our transpiled circuit directly to it and then once it's done its thing it's finished running then we might uh want to retrieve the results to do some postprocessing on it later
and we can call that um by calling this job. result function here when you um send a single um program with the Primitive um we call that a job so youve sent a single circuit uh to be um to be run um you maybe running it multiple times but it's just one one instance we call that a job um another thing to call out here remember I mentioned that we are um we have a new version of The Primitives uh Primitives uh version two um you want to make sure when you are importing from kkit
IVM runtime that you are importing sampler V2 um as sampler if you just want to change the name so you're not having to type out sampler V2 every time in the in the code so just watch out for that if you might come across older code examples online always check the Imports and make sure it's using that you are using the latest version for your for your code okay now we've just seen how to do things with the sampler now we are going to um see the same thing but for the estimator the way we've
constructed our circuit is a little bit different we're still using this um iqp uh circuit with this random Amion and 127 cubits but um the difference here is we're also specifying an observable that we want to um um that that we want to generate an expectation value for in this case um we're using the PO Zed operation we're doing transpilation the same as uh before but we have an extra step here for the observable we need to call this apply layout function um and then lastly we want to call estimator do run and we're this
time we're going to pass not only the ISA circuit but also the ISA the transpiled observable as well um this input is called A Primitive unified block I'm kind of glossing over it a bit now that's because your next in your next lecture in lecture two you should be going into a lot more detail around um the structure of this Pub object and then once you've run it you can get the results the same as you as you did before and just note here we don't add any measurements to this circuit as well okay so
in addition to um the kind of the basic primitive structure that I just showed you you can also customize certain um behavior of your runtime Primitives as well the first thing is the number of shots um I think as I've mentioned a couple of times now when you actually run your circuit on a device you probably want to run it multiple times so that you can see at the end where there might be some errors that you can you can get rid of um something to be aware of is that the more shots that you
have the you know the more you can reduce the statistical error but it does mean it takes you longer to run it on the real device so you want to be conscious of that depending on how much qpu time you have the more time you're using obviously the more expensive it is um another technique you can do is some error suppression there are some built in um error suppression techniques within kuit runtime that you can toggle um one example is dynamical decoupling and what this does is it reduces the um chance of do decoherence on
the um on the Cubit itself um the caveat is that it's not always possible because it requires some delay between different um gate operations so if you need to run an operation directly after and you don't have much time between them then it's not possible to use dynamical decoupling and lastly we have some Er mitigation techniques um these are techniques that reduce the effect of noise after you've gotten a result from the from the device so it's not changing how things are run on the device itself um a couple of examples of error mitigation are
T-Rex and zene um these uh I'm not going to go into a lot of detail about them because you're going to have quite um a lot of depth on error mitigation in later cures um just for now be generally aware of the idea of error mitigation and some of these techniques that we will explore in more detail later um error mitigation is a great technique but the one downside of it is it normally has additional computational overhead you normally have to end up running additional circuits or um doing using more classical resources as well um
so there's always a tradeoff as well as um the sort of just the basic primitive jobs that you can run there are a couple of different execution modes that you can use with runtime and these are really helpful when you start scaling up to to much bigger um computations um as I mentioned when you're just sending off you know sort of like a single circuit to be run um we call that a job but you can also um um combine multiple jobs into a batch so if you need to run multiple jobs in a row
it's um more efficient for you to use this batch mode and sessions is similar if you have multiple jobs you need to run but it's more iterative so going into a little bit more detail on that um some of the benefits of batch mode as opposed to just running a single job back and forth is that um runtime is sort of clever enough to do some kind of additional classical processing with batching that helps you get your individual results sooner so it's slightly quicker than if you just were like running single jobs back to back
if you run those jobs as part of a batch um runtime can do some some clever things behind the scene to kind of get you the results back quicker and to initialize batch mode with um A Primitive here we just have an example of um maybe we have 100 Cubit sorry 100 circuits um that we want to and each is a single job and we want to you know get results for for all of them um what we can do is use with batch and then pass the back end and then um call our initialize
our sampler and then call um sampler. run with those circuits for every single circuit in our array of um of of jobs of circuits um so it's a little if you're kind of familiar with this with python already this sort of language might be um something you're already familiar with um it's you just want to remember that you know everything that's happening within this this batch context is um is what is going to all be sent together as part of that batch mode sessions simp similarly allow you to send multiple jobs in sort of a
single um kind of workload um but the difference with sessions is that um it will allow you to with certain um algorithms especially variational ones what you might need to do is send a send a job get the result back and then based on that result send another job so in that instance batching wouldn't work because sometimes you know there's not enough time to wait to get the result and then send off the next job batching just like sends it as quickly as it can for sessions it you know it's it's tweaked a little bit
so that um you can do that classical postprocessing before the next job gets um get sent off and this is still um you know slightly faster than if you were going to um run it run your batches just a sorry run your jobs just a single single job session sessions does it a little bit more efficiently and to initialize a session very similar to uh batching you just call with session pass the back end initialize your primitive and then run it um as you normally would okay we uh have completed uh the third step of
the kis pattern and now we're on the home stretch we've gotten our results back back what sort of things can we do for postprocessing um the first thing you probably are going to want to do is visualize your results somehow you can do that with um a bunch of different features within uh the kiss kit visualizations module for example there's a nice plot histogram function you can call which is what will um sort of group your results and display them nicely in that uh image that we showed you know right at the start another post-processing
technique is something called post selection this is where maybe you can filter out some of your results that you know are going to be incorrect based on some prior intuition you have so let's say maybe you get um sort of like in this diagram we can see here like oh there's like these bit strings you know were pretty short at errors so we some a post-processing steps would be to like filter out these results that you know for sure are not the correct results and just leave you with the results that you know are the
correct ones that's called post selection another post-processing technique uh is circuit knitting this is a a really cool technique that you can go and explore with that ecosystem project called the circuit knitting toolbox um this is where during your optimized uh part of your kid Capac pattern you can decompose your circuit into smaller chunks that's called circuit cutting then you you execute those circuits as you normally would with in uh the third step of a kisk pattern and then lastly in this final postprocessing step you can then stick that the circuit kind of back together
um to reconstruct what the original circus outcome would have been that is a very gross simplification of the process but it's all that I have the time to explain in a single slide for you I really recommend going and and looking up the Circ itting toolbox and they have more tutorials and examples of of how that process works and so there we have it we have gone through all four steps of a kiss kit pattern um please don't worry if a lot of this went over your head this is just the first lecture in the
summer school you will have plenty more lectures to go through where you'll kind of be exploring the same Concepts and going into a lot more depth than what I've just shown you here today today was just sort of getting you exposed to um the an overview of what it means to to run a Quantum program using kiss kit using the latest versions um and I hope uh you have I enjoyed this lecture and um are excited for the rest of um the summer school if you want to go for a deep dive on any of
the things that I have talked about today I really recommend checking out these websites here docs. quantum.com gives you a lot of technical documentation on kiss kit and IBM Quantum Services learning. quantum.com gives you full on courses and um tutorials um that's probably a lot more like deeper dive type of content and then lastly we have YouTube um kiss kit channel uh where there's all sorts of stuff there there's lectures there's funny videos there's Community updates um and yeah I I really encourage you to go and look at these in more depth and enjoy the
rest of the summer school
Copyright © 2024. Made with ♥ in London by YTScribe.com