Machine Learning

Quantum Simulations with Python | About Data Science

Quantum Computing a field of technology that uses the principles of quantum mechanics (eg High standing again Being caught) to process information in a very different way than in the past. To put it in simple terms, instead of bits (0 or 1), quantum computers are used qubits solving complex, high-dimensional problems in chemistry, materials science, and optimization, which can happen in seconds rather than years.

In practice, problems are solved by building mathematical models called quantum states: a sequence of functions and instructions that take some input and return an output (such as linear regression and neural networks). In quantum computing, those functions are called the gates which changes the data (qubits) in a different way. Basically, a cycle is a sentence, and gates are the words that make up the sentence.

Circuits are used to conduct tests. Specifically, there are 2 types of quantum measurements:

  • Using a conventional computer to simulate a quantum computer. Like using Python to write a circuit, and a simulator to run it, while a real quantum computer would run the circuit.
  • Using a quantum computer to simulate a real quantum system (like atoms or electrons). In nature, quantum systems already exist, and primitive computers find it difficult to simulate them because the region of the region grows so large. On the other hand, quantum mechanics can model these systems effectively as they naturally follow the same rules.

In this tutorial, I will show you how to run a quantum simulation on your computer. This article is a sequel to “A Beginner's Guide to Quantum Computing with Python”.

Set up

First, we need to install Qiskit (pip install qiskit), an open source library for working with quantum computers developed by IBM that allows you to simulate a quantum device on your local machine.

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 to 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 that the probability that the qubit is 0 (the first element) is 100%, and the probability that the qubit is 1 (the second element) is 0%. Let's visualize the situation:

from qiskit.visualization import plot_bloch_multivector

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

Regions

A quantum gate is a single function that changes the quantum state. A quantum circuit is a sequence of gates applied to qubits over time.

Let's start building a simple circuit.

q = QuantumCircuit(1,0) #circuit with 1 quantum bit and 0 classic bit

q.draw(output="mpl", scale=0.7) #show circuit with matplotlib

We have one qubit, but to measure it, we need to add a classical bit to our circuit.

q = QuantumCircuit(1,1) #add 1 classic bit

q.draw(output="mpl", scale=0.7)

To build a circuit, you need to know what you want to achieve, or to put it another way, you need to know the gates and what they do. The approach is similar to Neural Networks: you just use one layer after another to get the desired output (ie Convolutions for images and Embeddings for text). The most common function is Hadamard's gate (H-gate), which operates the highest position in the qubit.

q = QuantumCircuit(1,1)
q.h(0) #Hadamard gate (Superposition)

q.draw(output="mpl", scale=0.7)

From the image, we can see that the red H gate is applied to the qubit, converting it from 0 to a 50/50 mix of 0 and 1. Let's add a quantization box, which wraps that Superposition to a real value (either 0 or 1), storing that result in the old bit.

q = QuantumCircuit(1,1)
q.h(0)
q.measure(qubit=0, cbit=0) #measure qubit with classic bit

q.draw(output="mpl", scale=0.7)

The circuit was designed mathematically on my old computer as written on paper, but never used.

Imitation

Quantum simulation is when you use a computer to model the behavior of a quantum system. When you write a circuit (as I did above), you just describe a mathematical model. To run it, you need a backend engine that uses a quantum circuit for simulation.

Qiskit-Aer (pip install qiskit-aer) is an engine that creates quantum circuits in simulation. Aer allows you to run quantum circuits on your computer, simulating different aspects of real quantum hardware (quantum state, measurement, noise system).

I will run the experiment with the pre-written circuit (classical bit + a qubit in Superposition) 1000 times.

from qiskit_aer import AerSimulator

sim = AerSimulator()
result = sim.run(q, shots=1000).result()
result.get_counts()

The qubit was measured 1000 times, resulting in 1 500 times and 0 the other 500 times. We can visualize it:

from qiskit.visualization import plot_histogram

plot_histogram(result.get_counts(), 
figsize=(5,4), color="black", title="1-qubit in Superposition")

The result is perfect because Aer can simulate complete quantum circuits, which would be impossible to have in real hardware. In the real world, quantum information is very fragile, and works under the assumption that the system is perfect and stable, allowing particles to exist in many states (Compatibility). But when the qubit comes into contact with anything, such as heat or vibration, the system loses its coherence and quantum properties (Dishonesty).

Therefore, you can visualize a qubit in Superposition (both 0 and 1 at the same time) only in the simulation, but not in the real world. Because when you see a qubit, you bring noise and the system collapses to a single number (0 or 1). In practice, real quantum computers are only for measuring results, while simulations are used to design quantum models.

To make the experiment more realistic, one can add sound to the simulation.

from qiskit_aer import noise

n = noise.NoiseModel()
error = noise.depolarizing_error(param=0.10, num_qubits=1) #10% error probability
n.add_all_qubit_quantum_error(error=error, instructions=['h'])

sim = AerSimulator(noise_model=n)
result = sim.run(q, shots=1000).result()
plot_histogram(result.get_counts(), 
figsize=(5,4), color="black", title="1-qubit in Superposition")

The conclusion

This article is for educational purposes introduces quantum simulations with Python and Qiskit. We learned what is the difference between real hardware and quantum experiments. We also learned how to design quantum circuits and use simulations on classical machines.

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