

Tutorial: Getting Started with Quantum Computing (Python)
source link: https://www.tuicool.com/articles/hit/iiAJjub
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Quantum computers might sound a bit exotic and far into the future, but in reality, they are now accessible in the cloud or through emulators for everyone to write quantum code. In this tutorial, we’ll go through how you can program a simple quantum computer to generate random numbers.
This example can be done on any emulator or quantum computer. For this blog post, the free and open source Python library ProjectQ is used.
ProjectQ can emulate a quantum computer on any CPU, or connect to IBMs quantum computer as a backend .
To get started, just install ProjectQ through pip or follow their installation guide
pip install projectq
Programming a quantum computer
Programming a quantum program is a bit different from what we are used to when creating classical programs, we have to dive down in the levels of computer abstractions and use logic gates to manipulate data, along the same mindset Alan Turing used when creating his famous Turing Machine, which describes a classical machine doing classical computations on classical bits.
A Quantum Turing machine describes a computer that can perform quantum computations on a Qubit, where quantum computations refers to applying quantum logic gates such as Pauli-X, CNOT etc to Qubits.
This means that any program possible to write on a classical computer is possible to run on a quantum computer, and vice versa, but this doesn’t mean any program will be more effective on a quantum computer , in fact, a range of programs will run slower on a quantum computer and a quantum computer will have to work in parallel with a classical computer to handle computations where the classical computer falls short, computations such as matrix multiplication or finding prime factors to break cryptography .
Creating a random generator with quantum gates
Creating a (pseudo) random number is one of the first things taught in computer science courses. Usually, it involves importing a pre-made library called something along the lines of ‘Random’ and then just call the appropriate function.
In quantum computing we’re not yet at this level of abstraction, but to create a random number is almost as easy by using quantum logic gates.
Quantum gates are similar to the logic gates we know from classical computing. Eg AND, OR, NAND, XOR etc
For those not familiar with the concept, logic gates are a set of input and output used to manipulate an input through boolean functions.
For example, if we feed the OR gate two numbers where one or both is one, the output will be True, if we feed the input two zeros the output will be false.
A B Q 0 0 0 0 1 1 1 0 1 1 1 1The table above shows the truth table of an OR gate, where A and B are inputs and Q is the output. Imagine that a door only opens when a lamp is lit and will stay closed when both lamps are turned off.
Logic gates can be used to compute any operation, and in quantum computing, we can use the logic gate called Hadamard to create a random number (1 or 0).
The Hadamard gate takes one input, and maps the output with a equal probability of being 1 or 0, i.e. create a superposition where the input can be either 1 or 0 at the same time.
The basis state |0⟩ is mapped to:
$$\frac{|0\rangle + |1\rangle}{\sqrt{2}}$$
The basis state |1⟩ is mapped to:
$$\frac{|0\rangle - |1\rangle}{\sqrt{2}}$$
The Hadamard gate is represented by the Hadamard matrix which shows that the rows are mutually orthogonal .
$$
H = \frac{1}{\sqrt{2}} \begin{bmatrix}1 & 1 \\ 1 & -1\end{bmatrix}
$$
Read more about the Hadamard matrix and other Quantum Logic gates on Wikipedia
Essentially, the Hadamard gate flips a coin and while the coin is in the air, it’s in a superposition in the sense that the coin can be both head and tail until it falls back down and we glance down at it - the human way of measuring the state of the coin.
Our quantum random generator outlined in a few simple steps together with the coin analogy
-
Create a new Qubit
- Fishing a coin out of our pocket
-
Applying a Hadamard gate to the Qubit to put it into a superposition of equal probability of being 0 and 1.
- Tossing our coin in the air, it can now be either heads or tail.
-
Measuring the Qubit
- The coin has finally landed and settled, its time to look at it to see if its head or tail.
Start by importing projectQ along with the Hadamard gate and the measuring function. We’re using projectQ in this tutorial, but the same approach can be followed in other libraries and systems as well, the code syntax will be a bit different, but the theory will be the same.
from projectq.ops import H, Measure from projectq import MainEngine
Initialise the backend, we’re using the emulator, but you can also use eg IBMs quantum computer.
Then Create a new Qubit to apply computations on.
quantum_engine = MainEngine() qubit = quantum_engine.allocate_qubit()
We now have a Qubit that initialised and ready to be turned into superposition. Remeber the coin analogy here, where we picked up a coin and now is ready to throw it in the air.
We’re then applying the Hadamard gate to the Qubit, this refers to the step where we toss the coin up in the air.
The syntax to do this will vary between each library and tool but in ProjectQ it’s simply done in the following way.
H | qubit
One interesting thing to pay attention to here is that we’re applying the gate directly to the Qubit and not creating a copy. This is because unlike classical bits, Qubits cannot be copied due to fundamental laws of physics.
However, its possible to teleport a quantum state from one location to another, but this is something for the next tutorial.
With the Qubit in a superposition, we can now measure it, this refers to the step where the coin has landed and settled on the table and its time to have a look whether its head or tail.
In projectQ the measuring is done with the following command.
Measure | qubit
The measured qubit can now be printed and will return either 0 or 1.
print(int(qubit))
Tidying this all up in a complete Python code along with a for loop that demonstrates the randomness of our coin toss.
from projectq.ops import H, Measure from projectq import MainEngine """ This Function creates a new qubit, applies a Hadamard gate to put it in superposition, and then measures the qubit to get a random 1 or 0. """ def get_random_number(quantum_engine): qubit = quantum_engine.allocate_qubit() H | qubit Measure | qubit random_number = int(qubit) return random_number # This list is used to store our random numbers random_numbers_list = [] # for loop to generate 10 random numbers for i in range(10): # initialises a new quantum backend quantum_engine = MainEngine() # calling the ranom number function and append the return to the list random_numbers_list.append(get_random_number(quantum_engine)) # Flushes the quantum engine from memory quantum_engine.flush() print('Random numbers', random_numbers_list)
Some outputs from the random generator.
Run 1: Random numbers [1, 1, 1, 1, 1, 0, 0, 1, 0, 0] Run 2: Random numbers [0, 0, 1, 0, 0, 0, 1, 0, 0, 1] Run 3: Random numbers [0, 0, 1, 0, 0, 0, 1, 0, 0, 1] Run 4: Random numbers [1, 0, 1, 1, 0, 0, 1, 1, 0, 0] Run 5: Random numbers [1, 1, 1, 1, 1, 1, 0, 1, 1, 0]
This was a simple introduction to creating a random generator with quantum gates in Python. Feel free to post any comments, concerns or questions in the comment field below.
Recommend
-
100
This is an excerpt taken from Chapter 1, “Getting Started”, of our book
-
78
Create ML is proof that Apple is committed to making it easier for you to use machine learning models in your apps. In this Create ML...
-
71
You’ve been working on iOS apps for a while now and you think you’re pretty slick. Think you’ve done it all, eh? Yeah I get it, you can probably do some basic networking? Maybe pull in some JSON and put together...
-
86
Note : This tutorial requires at least Xcode 10, Swift 4.2, and iOS 12.
-
49
Update note : Ehab Amer updated this tutorial for Xcode 10 and iOS 12. Bjørn Ruud wrote the original.
-
99
Note : This tutorial uses Xcode 10 and Swift 4.2. The libraries it depends upon are not yet updated for Swift 4.2 but can be used without issue. You’ll need to ignore the single warning telling you that Swift 4....
-
70
Update note : Pietro Rea updated this tutorial for Xcode 10, Swift 4.2 and iOS 11/12. Ray Wenderlich wrote the original.
-
50
Note : This tutorial is up to date for Swift 4.2, Xcode 10 and iOS 12. HomeKit is a library that allows users to u...
-
3
Getting started with Quantum ComputingAugust 3rd 2022 new story4...
-
8
Portfolio Optimization with Python and Quantum Computing TechniquesPortfolio Optimization with Python and Quantum Computing Techniques
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK