Quantum Computing is the sphere of technology that uses principles of quantum mechanics (i.e. Superposition and Entanglement) to process information in a fundamentally different way than classical computers. To place it in easy words, as an alternative of bits (0 or 1), quantum computers use qubits to unravel complex, high-dimensional problems in chemistry, material science, and optimization, potentially in seconds fairly than years.
In practice, problems are solved by constructing mathematical models called quantum circuits: sequences of operations and directions that take some inputs and return an output (similarly to linear regression and neural networks). In quantum computing, those operations are called gates that modify data (qubits) differently. Mainly, a circuit is a sentence, and the gates are the words composing the sentence.
Circuits are used to run experiments. Specifically, there are 2 varieties of quantum simulations:
- Using a standard computer to simulate a quantum computer. Like using Python to put in writing a circuit, and a simulator to run it, while an actual quantum computer would physically implement the circuit.
- Using a quantum computer to simulate an actual quantum system (like atoms or electrons). In nature quantum systems exist already, and classical computers struggle to simulate them since the state space grows exponentially. However, quantum machines can model these systems more efficiently as they naturally follow the identical rules.
On this tutorial, I’ll show you the right way to run a quantum simulation in your computer. This text is the sequel to “A Beginner’s Guide to Quantum Computing with Python“.
Setup
Initially, we’d like to put in (pip install qiskit), an open-source library for working with quantum computers developed by IBM that permits you to simulate a quantum device in your local machine.
Probably the most basic code we will write is to create a quantum circuit (environment for quantum computation) with just one qubit and initialize it to 0. To measure the state of the qubit, we’d like a statevector, which principally tells you the present quantum reality of your circuit.
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 implies that the probability that the qubit is 0 (first element) is 100%, and the probability that the qubit is 1 (second element) is 0%. Let’s visualize the state:
from qiskit.visualization import plot_bloch_multivector
plot_bloch_multivector(state, figsize=(3,3))

Circuits
A quantum gate is a single operation that changes the quantum state. A quantum circuit is a sequence of gates applied to qubits over time.
Let’s start constructing a straightforward 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’ve one qubit, but with a purpose to measure it, we’d like so as to add a classical bit to our circuit.
q = QuantumCircuit(1,1) #add 1 classic bit
q.draw(output="mpl", scale=0.7)

To be able to construct a circuit, it’s best to know what you would like to achieve, or to place it one other way, you must know the gates and what they do. The approach is analogous to Neural Networks: you only use one layer after one other to get the specified output (i.e. Convolutions on images and Embeddings on text). Probably the most common operation is the Hadamard Gate (H-gate), which applies Superposition to a qubit.
q = QuantumCircuit(1,1)
q.h(0) #Hadamard gate (Superposition)
q.draw(output="mpl", scale=0.7)

From the image, we see that the red H-gate is applied to the qubit, turning it from a definite 0 right into a 50/50 mixture of 0 and 1. Let’s add a measurement box, which collapses that Superposition right into a real value (either 0 or 1), by storing that result into the classical 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 has been mathematically designed by my classical computer because it was written on paper, but it surely hasn’t been executed yet.
Simulation
A quantum simulation is if you use a pc to model the behavior of a quantum system. If you happen to write a circuit (like I did above), you might be just describing the mathematical model. To run it, you wish a backend engine that executes the quantum circuit in simulation.
Qiskit-Aer (pip install qiskit-aer) is the engine that executes quantum circuits in simulation. Aer permits you to run quantum circuits in your computer, simulating different facets of real quantum hardware (quantum state, measurement, noisy system).
I’m going to run the experiment with the circuit written earlier (a 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, leading to 1 for 500 times and 0 for the opposite 500 times. We are able to 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’s perfectly even because Aer can simulate perfect quantum states, which could be inconceivable to have on real hardware. In the actual world, quantum information is amazingly fragile, and it really works under the belief that the system is ideal and stable, allowing particles to exist in multiple states (Coherence). However the moment the qubit interacts with anything, like heat or vibrations, the system loses its harmony and quantum properties (Decoherence).
Subsequently, you may visualize a qubit in Superposition (each 0 and 1 at the identical time) only in a simulation, but never in the actual world. Since the moment you observe the qubit, you bring noise and the system collapses to a single number (0 or 1). In practice, real quantum computers are just for results measurement, while simulations are used for designing quantum models.
To make the experiment more realistic, one can add noise 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")

Conclusion
This text has been a tutorial to introduce quantum simulations with Python and . We learned what’s the difference between an actual hardware and a quantum experiment. We also learned the right way to design quantum circuits and to run a simulation on a classical machine.
Full code for this text:Â GitHub
I hope you enjoyed it! Be at liberty to contact me for questions and feedback or simply to share your interesting projects.
👉 Let’s Connect 👈

(All images are by the writer unless otherwise noted)
