Machine Learning

A Beginner's Guide to Quantum Computing with Python

Quantum Mechanics is a fundamental theory in physics that explains phenomena on a small scale (such as atoms and subatomic particles). This “new” field (1900) differs from Classical Physics, which describes nature on a large scale (such as bodies and machines) and does not work at the quantum level.

Quantum Computing the exploitation of the properties of Quantum Mechanics to perform calculations and solve problems that a primitive computer cannot and cannot solve.

Conventional computers speak a binary code language: they assign a pattern of binary digits (1s and 0s) to each letter and instruction, and store and process that information pieces. Even your Python code is translated to binary digits when running on your laptop. For example:

The word “Hello” → “h”: 01001000 me too”: 0110100101001000 01101001

On the other hand, quantum computers process information with qubits (quantum bits), which can be both 0 and 1 at the same time. That makes quantum machines much faster than conventional ones for certain problems (ie, probabilistic computation).

Quantum computers

Quantum computers use atoms and electrons, instead of silicon-based chips. As a result, they can use Quantum Mechanics to perform calculations much faster than conventional machines. For example, 8 bits are enough for a classical computer to represent any number between 0 and 255, but 8 qubits are enough for a quantum computer to represent every number between 0 and 255. at the same time. A few hundred qubits are not enough to represent more numbers than atoms in the universe.

The brain of a quantum computer is small a small chip made of metals or sapphires.

Photo by Niek Doup on Unsplash

However, the most notable part is the large gold channel-like cooling hardware that hangs inside the metal cylinder: dilution in the refrigerator. It cools the chip to a colder temperature than the outside as heat kills the quantum states (basically the colder, the more accurate).

Image via Planet Volumes on Unsplash

That is the leading type of structures, and they are called superconducting-qubits: artificial atoms created from circuits using superconductors (such as aluminum) that show zero electrical resistance at very low temperatures. Another architecture is ion-traps (charged atoms trapped in magnetic fields in an ultra-high vacuum), which are more precise but slower than the former.

There is no exact public count of how many quantum computers exist, but estimates are around 200 worldwide. As of today, the most advanced are:

  1. IBM The Condorthe largest number of qubits built so far (1000 qubits), although that alone does not amount to a useful calculation as error rates are still significant.
  2. Google Willow (105 qubits), which has a good error rate but is still far from a fault-tolerant supercomputer.
  3. The ionQ of Tempo (100 qubits), an ion-traps quantum computer that is more powerful but still slower than superconducting machines.
  4. Quantinuum's Helios (98 qubits), uses the highest precision ion-trap structures reported today.
  5. Rigetti Computing's Ankaa (80 kits).
  6. Intel's Tunnel Falls (12 kits).
  7. Canada Xanadu Aurora (12 qubits), the first photonic quantum computer, which uses light instead of electrons to process information.
  8. Microsoft Marjoramthe first computer designed to scale a million qubits on a single chip (but has 8 qubits at the moment).
  9. Chinese SpinQ A littlethe first portable quantum computer (2 qubits).
  10. NVIDIA QPU (Quantum Processing Unit), the first GPU-accelerated quantum system.

Currently, it is not possible for the average person to have a large quantum computer, but you can access it through the cloud.

Set up

In Python, there are many libraries for working with quantum computers around the world:

  • Qiskit by IBM is the most complete high-level ecosystem for running quantum programs on IBM quantum computers, ideal for beginners.
  • Circ by Google, dedicated to low-level control in their hardware, is ideal for research.
  • Penny Lane by Xanadu specializes in Quantum Machine Learning, it runs on their own graphics computers but can be connected to other providers as well.
  • ProjectQ is ETH Zurich University, an open source project that tries to be the main package for general purpose quantum computing.

In this tutorial, I will use IBM Qiskit as the industry leader (pip install qiskit).

The most basic code we can write is to build i quantum circuit (the nature of quantum computation) with only one qubit and initialize it as 0. To measure the state of a qubit, we need statevectorwhich basically tells you the current quantum reality of your cycle.

from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector

q = QuantumCircuit(1,0) #circuit with 1 quantum bit and 0 classic bit
state = Statevector.from_instruction(q) #measure state
state.probabilities() #print prob%

It means: the probability that the qubit is 0 (the first object) is 100%, and the probability that the qubit is 1 (the second object) is 0%. You can print it like this:

print(f"[q=0 {round(state.probabilities()[0]*100)}%, 
         q=1 {round(state.probabilities()[1]*100)}%]")

Let's visualize the situation:

from qiskit.visualization import plot_bloch_multivector

plot_bloch_multivector(state, figsize=(3,3))

As you can see in the 3D representation of the quantum state, the qubit is 100% of 0. That was the quantum equivalent of “Hello World“, and now we can move on to the quantum equilibrium “1+1=2“.

Qubits

Qubits have two important properties of Quantum Mechanics: Superposition and Entanglement.

High standing – primitive bits can be either 1 or 0, but never both. In contrast, a qubit can be binary (technically it is a linear combination of an infinite number of states between 1 and 0), and only when measured, the superposition collapses to 1 or 0 and remains so forever. This is because the act of observing a quantum particle forces it to assume the classical binary state of 1 or 0 (the Schrödinger's cat story we all know and love). Therefore, a qubit has a certain probability of falling to either 1 or 0.

q = QuantumCircuit(1,0)
q.h(0) #add Superposition
state = Statevector.from_instruction(q) 
print(f"[q=0 {round(state.probabilities()[0]*100)}%, 
         q=1 {round(state.probabilities()[1]*100)}%]")
plot_bloch_multivector(state, figsize=(3,3))

With superposition, we introduce “random”, so the state of the vector is between 0 and 1. Instead of representing a vector, we can use a q-sphere where the size of the points is equal to the probability of the corresponding term in the state.

from qiskit.visualization import plot_state_qsphere

plot_state_qsphere(state, figsize=(4,4))

A qubit is both 0 and 1, with 50% probability respectively. But what happens if we average it? Sometimes it will be 1 programmed, and sometimes it will be 0 programmed.

result, collapsed = state.measure() #Superposition disappears
print("measured:", result)
plot_state_qsphere(collapsed, figsize=(4,4)) #plot collapsed state

Being caught – primitive bits are independent of each other, while qubits can collide. When that happens, the qubits are linked forever, regardless of the distance (which is often used as a mathematical metaphor for love).

q = QuantumCircuit(2,0) #circuit with 2 quantum bits and 0 classic bit
q.h([0]) #add Superposition on the 1st qubit
state = Statevector.from_circuit(q) 
plot_bloch_multivector(state, figsize=(3,3))

We have the first qubit in Superposition between 0-1, the second at 0, and now I'm going to combine them. So, if the first particle is measured and rolled to 1 or 0, the second particle will change as well (not necessarily to the same effect, one can be 0 while the other is 1).

q.cx(control_qubit=0, target_qubit=1) #Entanglement
state = Statevector.from_circuit(q)
result, collapsed = state.measure([0]) #measure the 1st qubit
plot_bloch_multivector(collapsed, figsize=(3,3))

As you can see, the first qubit that was in Superposition was scaled and folded to 1. At the same time, the second qubit is entangled with the first, so it has also changed.

The conclusion

This article is for educational purposes introduces the basics of Quantum Computing with Python and Qiskit. We learned how to work with qubits and their 2 key properties: Superposition and Entanglement. In the next lesson, we will use qubits to build quantum models.

Full code for this article: GitHub

I hope you enjoyed it! Feel free to contact me for questions and feedback or just to share your interesting projects.

👉 Let's connect 👈

(All images are by the author unless otherwise stated)

Source link

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button