 After my talk Yeah, I was the same yesterday I got back to my hotel at three and I just slept Yeah, hey, I'd like to introduce Josh Josh holds a PhD in quantum information from the University of Western Australia and is now working for the quantum full stack startup Xanadu in Toronto where he's part of the quantum machine learning software team And quantum open-source project that he's actively working on includes strawberry fields and penny lane and today He's going to talk about quantum machine learning with penny lane. So please welcome Josh Hi, everyone. So as Mark mentioned, I'm a my name is Josh I'm a quantum software developer at Xanadu and Xanadu were a full-stack quantum confusing company So we're working on both the continuous variable hardware as well as open-source quantum software development so as mentioned one of our software platforms of strawberry fields, this is for Integration and submitting jobs to our quantum hardware I encourage you to check it out strawberry fields to AI for the purposes of this talk I'll be focusing on one of our other libraries penny lane, which is a open-source quantum machine learning framework So for this talk, so I gave this a similar talk last year introducing penny lane this year I'll be taking going back a bit Discussing where the ideas for penny lane came from and also moving forward a year So discussing what's changed in penny lane over the last year since I last talked at plus them so starting off Penny Lane was conceived about July 2018 we were noticing that a Quantum machine learning had become quite a buzzword the number of papers in quantum machine learning the archive had started to increase significantly and as we were in this noisy era of quantum computation where we have these small-scale devices People were wondering what could we do to combine machine learning with quantum computing and what could we get out of it? So we sat down and we asked ourselves. Why was deep learning so successful over the last Decade or so and it basically came down to three main points Hardware advancements, so we'd seen GPUs to become immensely important in machine learning the ability to scale up these Thousands of small computations that people have been using in graphics for decades for machine learning caused a seismic shift in what we could do and This is in addition to tensor processing units. So TPUs as well have So revolutionized what we can do in machine learning In addition the abundance of workhorse algorithm, so things like back propagation gradient descent We were able to suddenly train massive neural networks using Exacted differentiation techniques that were much more stable than what we were doing previously and Finally, and this is probably a really large one is specialized user-friendly software So things like TensorFlow, PyTorch, Jax These machine learning frameworks that people most of you in the room probably familiar with these have done a huge amount to increase the accessibility of machine learning Yeah, so this is kind of just Leaning more into that so you can see that the number of projects using TensorFlow have increased almost exponentially over the last 10 years or so and As the industry is matured these software frameworks are becoming more accessible more easy to use and there's more choice as well So TensorFlow, PyTorch, Jax the question is Can we train quantum circuits? so It was a question people had and people start playing around with in the quantum computing field It turns out quantum computing is also differentiable So you have a small quantum circuit which is controlled by quantum gates These quantum gates take classical parameters You perform a quantum computation on the hardware and then you perform a quantum measurement at the end and from these quantum measurements You can compute measurement statistics So you can measure certain quantum observables and take expectation values or variances and these things are deterministic If you put in the same parameters to this quantum circuit you get the same output So it turns out that these quantum circuits are differentiable The outputs depend smoothly on gate parameters Can we practically train quantum circuits though? It turns out we can and Quantum machine learning methods called variational quantum algorithms have become one of the main near-term quantum algorithms for near-term devices So this happened around 2011-2012-2013 Ideas start forming in the quantum community things like the variational quantum eigensolver Which was a really neat idea at the time saying that these quant variational quantum devices They give us access to this enormous state space that we can't normally access with classical techniques Can we use can we train the classical parameters of these gates? to generate a state that outputs a measurement statistic that we are looking for that we want to find So this is kind of this variational quantum algorithms described on the right-hand side here So we have a quantum sub routine. This is a quantum black box We just have quantum gates on our quantum hardware We prepare a quantum state and we have some circuit parameters that drive these quantum gates We run the quantum circuit we get a measurement output and then we perform some classical processing and classical optimization so we use techniques such as optimization and Gradient free optimization to update these parameters via some classical optimization loop So this was first these two algorithms ventured up here VQE and QAOA These were sort of the first two major variational quantum algorithms, but since then the fields really grown and There'd be natural extensions to other circumstances that we've seen from standard machine learning so things like Quantum classifies can we distinguish different quantum states by using the source of variational quantum algorithm? Nowadays there are many many proposals just because of how well the scales to noisy devices We don't have fault tolerance yet in quantum computing But these algorithms seem to work so far very very well on the noisy devices that we have So what we want to do is we want to be able to train Quantum variational quantum algorithms, but we want to do them using some of the workhorse algorithms They've been developed over the last 10 20 30 years of machine learning So one technique we have of doing this. It's called the parameter shift method so Initially we were using gradient free methods to train quantum circuits This shifted a bit and we started using something called the parameter shift method So this is an analytic method of computing Gradients coming out of quantum devices So the main insight here is we're able to use the same quantum hardware to evaluate its own gradients And this is a significant insight Because if we weren't able to do this We'd have to classically simulate the circuit to get the gradients and that's intractable is the size of the quantum devices We have increases You'd no longer be able to classically compute the gradient to perform this classical optimization loop So you're constrained to gradient free optimization techniques. So this is just a Very short diagram that sort of demonstrates this parameter shift method So on the left we have our variational quantum circuit It's an arbitrary circuit We have a couple of gates running on the quantum device and we have our input gate parameters and our output measurement statistics All the parameters shift rule says is that if we Take the exact same quantum circuit that we want to train We shift the parameters forward by some value s backward by some value s and take the difference The quantum device will actually output the gradient Of the quantum expectation value at that parameter value And the crucial thing here is that this shift is macroscopic So It's not finite differences. We're not doing numerical differentiation. We're doing analytic differentiation And we're able to get the output of the gradient with noisy devices even when Even outside of regimes or we might be drowned by noise So the quantum variational algorithms we've had so far have been quite simple in scope They've been mostly maybe one or a couple of quantum devices And we're getting the output of that quantum device and we've got that classical optimization loop directly training this quantum circuit So Back in july 2018 or so we were thinking whatever we want to do more than that Whatever we want to do something like this So we have an arbitrary computational graph On this diagram here the green uh boxes the green nodes. Those are quantum circuits The yellow nodes are just arbitrary classical processing And we want it to be as arbitrary as we want Why can't we construct something like this say we have a machine learning model Involving lots of classical layers. We want to replace a couple of them with quantum layers We want to do something like that in open source quantum software and we want to see what happens So that's kind of where we were. We knew what we wanted to do. We wanted an open source framework That could interface with the accessible machine learning framework such as pi torch tends to flow jacks We wanted it to drive real quantum hardware devices using these techniques for analytic gradient computation on real devices And we wanted it to be accessible intuitive And allow you to perform arbitrary computations mixing classical and quantum however you want So we knew what we wanted to do. We weren't really sure what it should look like So I for this presentation actually scrolled Up quite high into the deep dark depths of my slack history To see what we were discussing when it came to the UI of penny lane So I've got a couple of the messages here before I actually show you what the UI looks like This is the very first one the coach would look as much as like numpy as possible This was a very popular statement in our slack channel when we were discussing the architecture of penny lane So there was lots of agreement there We kind of got a bit distracted though and we started looking at context managers We were like maybe we want to train things using context managers in python We spent I'm a bit surprised looking at slack history, but we spent about two to three days trying to make this work Before realizing it's absolutely atrocious idea. People don't want to train things using context managers But hey a quantum node is kind of like a function. You have input classical parameters you have output Classical data that's come from taking measurement statistics of your quantum device You want to define it once and then you want to evaluate it multiple times To be able to train it. So we started thinking hey Quantum devices in this framework should just be functions that you call And compose with your classical functions So that's kind of where we ended up. So that's our prototype on the left of what this quantum machine learning framework should look like And that's what we ended up with so In penny lane all quantum circuits are functions within these functions. You define your quantum operations The function state classical parameters and they output Measurement statistics So this is just a quick overview of what A quantum machine learning task looks like in penny lane the main Things we want to do with penny lane. I've mentioned them slightly before but just to reiterate It's open sourced and developed openly So not only is the code available on github But we also develop it openly on github So I think that's quite important and that anyone who wants to contribute can Look at our github issues Look at our pull requests and see the roadmap for where we want to take penny lane And this goes back all the way to the very beginning as well If you go back through the history of the github repo you'll see all those ideas In terms of the ui and what we want penny lane to do as pull requests and issues So we want penny lane to be functional Penny lane is functional. It has a python interface. It's designed to integrate Seamlessly with the machine learning frameworks that we have right now. So numpy pie torch and TensorFlow You can train the quantum computer the same way as you can within your own network And crucially it's designed to scale as quantum computers grow in power So in penny lane All quantum functions Are actually usable on the quantum hardware we have available. So we have quantum hardware available over the cloud from regetti from ibm There are plugins available to connect penny lane to that And by just changing a couple of lines you can switch What devices you're running your quantum functions on? So the main abstraction layer in penny lane is I've discussed this previously by a quantum node so the quantum node is what encapsulates your quantum computation and That's the diagram there on the left. So you have your quantum circuit input parameters input state preparation Output expectation values or variances or samples or probabilities. These are all things you can train on analytically in penny lane so the Goal is that a computation is end-to-end differentiable We want you to be able to use a workhorse algorithms like gradient descent across your quantum classical hybrid model So all quantum nodes in penny lane will always be Differentiable end-to-end you can combine it with your other models in Classical machine learning and the entire computational graph will be end-to-end differentiable So how do we do this? So with penny lane we support three main interfaces. We support numpy py torch and tensorflow And the way this works is we simply we accept the tensor objects from these three frameworks So numpy if you're using autograd and array box the other two a torch tensor or tensorflow tensor When it comes time to evaluate the quantum portion of your computation What we do is we just unwrap and extract the numerical value We evaluate it we use our formulas and internal architecture to evaluate the gradient Converse it back to a tensor object and as well pass on the gradient information to the machine learning framework So this is how we're able to make the entire thing end-to-end differentiable Even though we're using a mixture of quantum and classical hardware So you can almost think of the Quantum hardware as accelerators in a sense similar to gpus and tpus Penny lane is framework and hardware agnostic So I mentioned previously that one of our framework hard one of our hardware frameworks is strawberry field This is what we use to connect to our photonic hardware that we're developing in our photonic labs In addition We want to be able to mix and match so as we're in the noisy era there are advantages to our photonic technology There are also disadvantages to our photonic technology This is similar to superconducting cubits. This is similar to d waves quantum annealer All these noisy Devices have their pros and cons. They have applications. They're best suited for and applications that may be a better in a different architecture So we want to be able to combine these different frameworks these different quantum devices Um, so you can take any of these there are plugins available for kiskit forest Cirque and strawberry fields and combine them with either tensor flow pure non-py for scikit-learn or pytorch The cool thing is you can even In one computation have devices from different providers. So You can have a for instance This circuit down here in the bottom left. That's a photonic circuit. It's going into a classical processing node doing the cosine and square root And that's going up to a qubit circuit So you can take a provider from regetti or take a device from regetti or ibm combine this with a photonic device Train over the entire architecture So for those familiar with machine learning, but maybe not quantum machine learning Here's a very quick example of what you can do. So we have a quantum circuit. We're rotating Uh We have one qubit in this quantum circuit We're rotating the qubit around the x-axis Controlled by a classical parameter doing the same thing around the y-axis controlled by another classical parameter And just measuring where this uh The state the qubit ends up in measuring the z the poly z expectation value So this is pretty simple in penny lane. You just uh Create your device so the device is just an abstraction for the qpu that you want to run and train your computation on in this case i'm using a qpu device from regetti first You define your quantum function. So this uh, this is a q-node. It corresponds exactly to that quantum circuit in the previous slide I've got my two rotations. I'm returning expectation value of the poly z measurement And the last part is just pure classical optimization exactly like you'd see in the pi torch or a tensor flow Uh machine learning example in this case. I'm just using pi torch to um optimize So i'm defining my cost function which takes In the uh the q-node and i'm just optimizing that to reduce the expectation value of that q-node So the key thing I guess is that The penny lane q-node is It makes pi torch tensor uh quantum aware in this example You can take a q-node And use it within pi torsion from pi torches point of view or tensor flows point of view This function is a classical function that's end to end differentiable So these examples are available on our website as a tutorials Just on the right hand side is the animation of this Short script actually running on a qpu. So we're training the qubit to To enter the state that's defined by the cross on the block sphere and you can see the qubit being trained and approaching The required state So over the last year we've worked hard on making penny lane more accessible So we want it to be a high level abstraction. A lot of the other frameworks are They're object oriented. They're low level and they're very hardware focused. So you can access things that Maybe specific to a particular hardware and things that we want to do sometimes so sometimes We need to think about the underlying hardware that we're running our quantum programming on Other times we don't we want to think in a high level abstraction We want to maybe do something assuming that The hardware will do the best it's can based on the underlying framework So that's kind of where we're positioning penny lane and sources the num pi interface Wrapping will pack underneath So a couple of things we've added to the library of the last year to make it more accessible to provide an Easier way of constructing your quantum machine learning models Built a library of circuit ansatz from the qml literature So it's kind of an unanswered question about what quantum circuits ansatz are easiest to train and best for particular machine learning models and quantum algorithms Papers are continuously coming out with new ansatz that provide benefits for different different use cases So these are easily accessible in penny lane. We have a qml.templates library and you can just include these in your quantum node We heard feedback from a lot of users that said that they had Codes already using kiss kit or already using pi call that they wanted to Um interface with their machine learning models, but They didn't want to have to rewriting it using penny lanes interface So we added the ability to import from these quantum libraries So you can import your code from kiss kits from chasm from pi quill from quill Um, it runs the same circuit. It gets converted into a q node and it just becomes end-to-end differentiable So you can take your existing code And then interface it with pi torch or TensorFlow straight away So automatically adds analysis differentiation to existing pi quill and kiss kit frameworks Cool thing is you can take these programs loaded from regessie forest convert it into q nodes and then deploy it on other quantum hardware if you'd like We're also working on adding optimization that's it may be better suited for a quantum landscape so things like The quantum natural gradient So taking into account what the cost landscape of your variational circuit looks like In order to ensure better convergence. So this is internal research that we've been working on at zanidu. Um, this is just a Outline of what this algorithm looks like. It's a kind of a quantum equivalent to the natural gradient if you've heard of that Which uh takes into account classical euclidean space when you're doing gradient descent Quantum natural gradient takes into account the quantum geometry when doing gradient descent And in general can lead to much better convergence with variational algorithms So this is just a plot comparing for a couple of circuits Uh gradient descent some blue versus Adam and green and then quantum natural gradient descent in uh reds and black for different approximations Um, we've also heard feedback that people want their simulations to be faster So with pinning lane, we took the approach that it was hardware first If you defined a q-node it should always be executable on hardware But there are shortcuts we can take with classical simulators One of those is if we write the simulator using tensorflow or pie torch, for example We can do classical back propagation techniques that scale better insertion regimes So penny lane now includes a tensor network simulator Uh built using google's tensor network software and tensorflow Uh, so this uses classical back propagation inside the q-node To compute the gradient which if you have a lot of parameters can be faster. So enables routines where maybe you're Training your quantum circuit on a simulator you get your Trains parameters and then you deploy using test data on the quantum device We also have a new quantum chemistry plugin. So Right in the beginning of the talk I mentioned that one of the First variational quantum algorithms to really create this qml second 2.0 wave Was the vqe algorithm the variational quantum eigen solver So this algorithm allows us to use quantum computers to Probe the electronic molecular Energies of various molecules So penny lane now interfaces with another open source quantum library open fermion by the quantum team at google So using open fermion you can Converse at any electronic molecular structure To a quantum observe a set of quantum observables and these are now importable into penny lane for training on quantum hardware So the other thing we really want to do is we I mentioned at the beginning We want to make it accessible and part of that is the ui and what frameworks we interface with Another part of it is having good documentation stable api and Collection of tutorials for people to explore how to use penny lane So if you go to penny lane dot ai slash qml, please check it out Where we have a constantly growing collection of quantum machine learning tutorials Using penny lane across a mixture of different hardware devices hardware frameworks and also machine learning frameworks And these are things from just looking at variational quantum eigen solver looking at quantum state preparation To things such as quantum transfer learning doubly stochastic gradient descent in quantum systems Quantum structure learning etc So please have a look this not only is penny lane completely open source and developed in the open But the penny lane dot ami dot ai slash qml website is open sourcing on github So any of these true tutorials that are available there can be downloaded Can be edited with pull requests and we also accept pull requests for new tutorials We also have a twitter account penny lane ai so please follow and we'll We try every week or every two weeks to tweet out new qml results We see as well as new tutorials that are available using penny lane So we wanted to see where we could go with penny lane once we had this base ui the space abstraction We noticed that The core abstraction penny lane is this q node concept So a quantum device corresponds to a quantum function in python that is an end-to-end differentiable and can be used with any framework But we started to notice that it wasn't enough for us as internal researchers at zandu We needed The ability to process these q nodes At a higher level of traction we found that we would have groups of q nodes And we wanted to work with these groups of q nodes without thinking about the low level quantum circuit within each q node And this is something that happened when we were thinking about vqe Essentially the vqe module was two things. It was a mapping operation We were taking a quantum circuit And mapping it across different return values different expectation values. We wanted to measure And then a reduction operation Once we evaluated all these different quantum nodes We wanted to just take the dot product between these quantum nodes And coefficients that defined the problem we were solving So we wanted a better abstraction for working with groups of q nodes that was algorithm independent We also noticed that we were getting to the point where multiple q p's were available right now on the cloud So if you're using ibm, there are I think five or six ibm devices available Uh for the public. So if you sign up to their accounts, um, you have access to multiple devices right now um The same thing is available on riggedi forest. There are I think two chips now available on the qcs aspen 4 and aspen 7 so Maybe a year ago we were getting excited by having access to one device and that was fine now we have access to couple of devices And it can be frustrating at sometimes now that we know we can have more than one. We want a thousand so We started thinking what can we do with rather than have a single big q pu which is what a lot of hardware providers are Working really hard to get towards What can we do in the interim if we had a thousand small q p's? So we recently introduced the concept of a q node collection So a q node collection is a group of q nodes that are evaluated that can be evaluated independently Um, so they can evaluated uh at one go asynchronously Um at the same time We wanted to be in the abstraction that you don't have to think about too much And a common way for creating q node collections and various, uh quantum algorithms is you map a ansatz of template Over measurements, so we wanted to extend that as well. Not only can you map a Ansatz or a quantum template over measurements, but you can also map it over devices So I have a small example of what this looks like here So here I have a quantum circuit ansatz Um, it just does two x-axis rotations Uh, both of these parameterized and then it does a c-naught which is a quantum entangling gate Then I have a list of uh observables. I want to measure So these are just two qubit tensor observables. I want to measure poly x cross poly z And I also want to measure poly z cross poly x And then say I have access to two devices. So in this example here, I'm just using the regetti forest qvm simulator Uh with Two devices that are currently available on the simulator aspen 4 and aspen 7 So previously you would create two q nodes. You would be forced to evaluate them sequentially as you normally are in python We introduced a way to do this that makes it slightly simpler So you can treat you can use this new qml dot map operation to map your ansatz over both your observable list and your list of devices Say what you want your measurement to be And the output of that is not just a single q node, but a collection or a group of q nodes And these group of q nodes are treated as one single object in penny lane So in this example here, you have some parameters. You pass it to your q nodes Both of the quantum devices are evaluated At the same time and you get your results back as a single numpy array And the really cool thing about this is because we have access to these devices that we can access At the same time It's embarrassingly paralyzable. So we've added this asynchronous dispatch support to penny lane So this is experimental. It's only recently been added Uh, but if you call your q nodes normally in this case, it takes about five seconds If I pass the parallel equals true arguments, penny lanes dispatching both of these quantum circuits to be evaluated at the same time Uh, you get your results back in almost half the time So we added this ability to create q node collections, but we also want them to be easy to use So we introduce some high level, uh, quantum function high level processing functions They allow you to impose your q node collections With other, uh, classical processing functions from your machine learning libraries So things like q ml dot sum q ml dot dot q ml dot apply So in this very simple three line example here I'm creating a uh, so i'm using pie torch in this case creating a tensor of observables a tensor of coefficients rather And then you can see in the second line I've got my q nodes and i'm just composing it with a whole bunch of different classical processing functions uh to manipulate the output of these q nodes and With the ability to manipulate q node collections in this way, we've gone from something that might take Uh 10 or 15 lines in python to something that only becomes a single one liner in python So in this particular case, I'm applying the torch sign function to my q nodes And then performing the dot product between The sign of those q nodes and the coefficients I just defined All this happens lazily in penny lanes So the quantum evaluation only ever happens when you actually Evaluate your q node collection your cost function So the third line here when i'm calling my new cost function with the parameters That's when the dispatching happens and penny lane starts Dispatching the quantum computations to the external quantum devices and Performs the classical processing so I hope this talk is kind of given uh You guys an idea of our vision for quantum machine learning at zanidu We want uh quantum machine learning to be intuitive accessible Um be easily explored and we want anyone to have the ability to run the quantum machine learning algorithms Whether it's on simulators or actually on hardware that's available in the cloud We want quantum machine learning models to be widely widely shared and re-implemented by others So that's sort of the impetus behind the penny lane dot ai slash q ml collection Um circuits should be reusable embeddings pre and post processing should be easy We want that to be something that you don't have to think about We want the documentation to be able to To be designed so that you can find these things easily and that maybe you don't even have to look at the documentation Rather than searching throughout what methods are available uh You just find a function in penny lane that applies to that thing for you and you can do function Compositioning on your quantum uh devices Um penny lane is open source development is open source and uh, I think as uh Mark mentioned in tomas mentioned in his first talk a lot of the physicists working on quantum open software at the moment A lot of the developers are physicists. I'm a physicist a lot of my team are physicists um And sometimes It helps to get feedback and contributions from people outside of physics So something we'd love is for people in this room to have a look at our open source code Have a look at the issues that are opened and see if there's anything they feel they can contribute because maybe From someone outside of physics you guys can Add features or implement things in a way that we would never have thought of doing ourselves Uh, so thank you. Uh, I've got some links at the top. So this is our twitter link and our qml tutorials page