Building Your Own Quantum Circuits in Python

This way, we can see the power of Superposition and how the final result is a completely random number without any one-sidedness.

Additionally, instead of running it on a local simulation that is classical in nature (cannot reflect the sheer awesomeness of a quantum computer), let’s hook up our circuit and run it on the actual IBM Quantum Computers located nearest to you!A simplified diagram of the processes involved in building a quantum circuit, running it on a Quantum Computer and getting back the simulated resultsBefore we perform any Quantum magic, we need to connect to the IBM Quantum Computer nearest to your region.

To do so, visit the IBM Q website and login or create an account if you’re new.

When logged in, click the profile icon located at the top right-hand corner.

Go to the My Account page and go to the Advanced tab.

There, you’ll see an area for your API Token.

If you don’t have one go ahead and generate one.

You should see a token inside.

If not, click Regenerate to generate one.

I’ve blocked mine for security reasons.

We can finally start writing a client-side circuit that interfaces with the IBM Q Machine closest to you, shown on the panel to the left.

The available machines closest to where I amFor this tutorial, I’ll be using the ibmqx4 system which can hold and manipulate a maximum of 4 Qubits.

You might see other available machines on your dashboard depending on your location so don’t worry if you don’t see mine.

Choose the one you like (pick the coolest sounding one ????).

Next up, let’s connect to the quantum computer using the following lines of code:Before we build a circuit, let’s create a record of Qubits and Classical Bits for our random number generator that generates numbers between 0 and 7 (n=3).

Now, we need to construct a circuit that returns a Superposition of a Qubit and collapses it to a bitstring that represents an integer between 0 and 2^n — 1.

To do that, we can apply the Hadamard Gate on the 3 Qubits to put them all in a Superposition with a 50–50 chance of being a 1 or 0.

If we visualize this circuit, it’ll look like this:The Quantum Circuit we’re sending up to the IBM Q Quantum ComputerNow, it’s time for the exciting part.

Let’s instantiate the Quantum Backend that’s going to run our circuit.

Remember you used the Qasm Simulator up above?.In the same way, my ibmqx4 machine is the simulator here (here, it’s the real deal!).

Let’s connect our backend simulator:Shots here refers to the number of times the circuit is being run.

Here we only want 1 randomly generated number, so we set it to 1.

Note: You can play around with the number and set it to something like 1000 to see a histogram of the generated numbers over the 1000 runs.

Next up is the main event function the runs a job on the ibmqx4 Quantum Computer and returns a completely random number between 0 and 7:Now, it’s the moment of truth.

Let’s run the complete circuit on our Quantum Computer allocated to us:The code here will take a few minutes to run and took 3 credits from my account (you’re given 15, to begin with).

When I ran the circuit, the final output was 6.

With that, congrats for coming this far!.You’ve just built a Quantum Circuit.

Let’s revisit the steps to successfully run a quantum program:The typical process of building a Quantum Circuit and running it on a Quantum backend.

Note: The full code for the Quantum Random Number Generator can be found here.

Wrapping upYou’ve just built a Quantum Circuit!.That’s awesome!.You are taking one step forward on your journey to becoming an expert.

By thinking of simple processes that require manipulation of bits and converting the problem into one that can be solved by a Quantum Computer, it’s good practice to getting a firm grasp over the concepts.

Congrats on building a Quantum Circuit!.The man himself wishes you well!In a nutshellLearning about the principles behind Quantum Computing was surely very demanding!.There’s so much theory floating around that having a grounded understanding of what’s going on is very important.

Learning about Qiskit was really fun and the documentation and resources provided by IBM Research went a long way in helping me understand what’s going on.

Of all the quantum processors including Google Cirq and QuTiP, Qiskit was the most enjoyable to learn and implement circuits with.

Quantum Computing is gaining pace at a promising rate.

Researchers are making progress in democratizing the usage of Quantum Computers to perform tasks and tackle problems that were once considered impossible to solve.

However, Quantum Computing theory has not reached a stage where it is fully accessible to everyone.

It may be due to the clunkiness of Quantum Computers, the colossal facilities and the enormous amount of resources that are needed to keep the field active.

Notwithstanding this, hopefully, this article has given some insight into what’s going on in the field and has sparked some interest in you!.If you have any questions or want to talk about anything, you can catch me blatantly procrastinating on Twitter or LinkedIn.

My next article will probably be focussed on the mathematics behind Quantum Computing and how the different gates and logic manipulate the Qubits.

We’ll be doing a rundown of the quantum algorithms and how they go about doing what they do!Till then I’ll see you in the next one!Original article by Rishabh AnandRelated articlesDo check out my other articles on technology and Machine Learning, and reviews of up-and-coming software innovations.

Crash Course in Quantum Computing Using Very Colorful DiagramsAlmost everything you need to know about Quantum Computing explained using very intuitive drawings…towardsdatascience.

comA Comprehensive Guide to Genetic Algorithms (and how to code them)On the Origin of Genetic Algorithmsmedium.

comCatGAN: cat face generation using GANsDetailed review of GANs and how to waste your time with them…hackernoon.

comIntroducing TensorFlow.

js ????Running ML models on the browser became so much more easier!hackernoon.


. More details

Leave a Reply