The quantum computing "Hello World"

Published on May 10, 2026 in Quantum Computing  

If you are new to quantum computing, I advise you to start by reading my previous article which explains what a qubit is.

Now it’s time to apply operations on our qubit. Just as a classical computer works with logic gates, a quantum computer works with quantum gates.

We will start with the Hadamard gate.

This gate takes one input qubit, and allows us to switch from base state, |0⟩ or |1⟩, to a state of equal superposition. More generally, the Hadamard gate applies a π/2 rotation on the Y axis followed by a π rotation on the X axis.

The transformations performed by quantum gates can be represented by matrices. We then just need to multiply our qubit (a vector made up of the complex values α and β) by the matrix of the gate to obtain the output of the gate.

Here’s what it looks like for the Hadamard gate:

H = 1/√2 * [[1, 1], [1, -1]]

One of the particularities of the Hadamard gate is that it is self-reversible, If you apply it twice, you return to the initial state.

Since we don’t have a quantum computer, we’re going to simulate it in python. Here is a program that simulates a qubit and a Hadamard gate:

python
import math
import random
from typing import Tuple

class Qubit:
    def __init__(self, alpha: complex = 1+0j, beta: complex = 0+0j):
        # |ψ> = alpha*|0> + beta*|1>
        self.alpha = complex(alpha)
        self.beta = complex(beta)
        self._normalize()

    def _normalize(self):
        norm = math.sqrt(abs(self.alpha)**2 + abs(self.beta)**2)
        if norm == 0:
            raise ValueError("State vector zero.")
        self.alpha /= norm
        self.beta /= norm

    def state(self) -> Tuple[complex, complex]:
        return (self.alpha, self.beta)

    def apply_gate(self, matrix):
        a = matrix[0][0]*self.alpha + matrix[0][1]*self.beta
        b = matrix[1][0]*self.alpha + matrix[1][1]*self.beta
        self.alpha, self.beta = a, b
        self._normalize()

    def measure(self) -> int:
        # returns 0 or 1 with correct probabilities
        p0 = abs(self.alpha)**2
        if random.random() < p0:
            # collapse to |0>
            self.alpha, self.beta = 1+0j, 0+0j
            return 0
        else:
            # collapse to |1>
            self.alpha, self.beta = 0+0j, 1+0j
            return 1

# Hadamard gate H = (1/sqrt(2)) * [[1, 1], [1, -1]]
H = [[1/math.sqrt(2), 1/math.sqrt(2)],
     [1/math.sqrt(2), -1/math.sqrt(2)]]

if __name__ == "__main__":
    # Example: prepare |0>, apply H, display amplitude and measure multiple times
    q = Qubit(1, 0) # |0>
    q.apply_gate(H) # H|0> = (|0> + |1>)/sqrt(2)
    alpha, beta = q.state()
    print("State after H :", alpha, beta) # amplitudes ~ 0.707..., 0.707...
    # statistical sampling to see frequencies
    counts = {0: 0, 1: 0}
    for _ in range(1000):
        qq = Qubit(alpha, beta) # copy of the state before measurement
        m = qq.measure()
        counts[m] += 1
    print("Counts after 1000 measurements :", counts)

If you have understood everything I have explained so far about the qubits and the Hadamard gate, nothing very complicated.

Normalization ensures that |α|^2 + |β|^2 = 1. If the starting qubit is valid (on the Bloch sphere) this should not be necessary. But when many gates are applied, rounding errors can appear and it is better to normalize.

You are also probably wondering what collapse means when measuring. It simply simulates the quantum reality of superposition where when one state is observed, the other disappears.

To test our qubit and our Hadamard gate, we will start from |0⟩, apply H, then run 1000 measurements and find that we are in an equal superposition state (each counter should be close to 500 after 1000 measurements).

There are also existing quantum simulators that you can use, for example Qiskit (IBM), Cirq (Google) or Q# (Microsoft). The most popular is Qiskit.

Qiskit has several components, Terra, Aer, Ingnis, and Aqua. Aer allows you to simulate quantum circuits. To use it, you have to install the necessary library:

terminal
pip install qiskit qiskit-aer

Here is the same program with Qiskit:

python
from qiskit import QuantumCircuit, transpile
from qiskit_aer import Aer
from qiskit.quantum_info import Statevector

# Circuit: 1 qubit, 1 classical bit
qc = QuantumCircuit(1, 1)
qc.h(0) # Hadamard on qubit 0
state = Statevector.from_instruction(qc) # get the statevector after applying gates but before measurement
qc.measure(0, 0) # measure and store in classical bit 0

print("Statevector amplitudes (|0>,|1>):")
print(state.data) # should be ~ [1/sqrt(2), 1/sqrt(2)]

# Simulator
sim = Aer.get_backend("aer_simulator")
compiled = transpile(qc, sim)
job = sim.run(compiled, shots=1000)
result = job.result()
counts = result.get_counts(qc)

print("Counts :", counts)
print(qc.draw()) # display the circuit diagram

Don’t miss my upcoming posts — hit the follow button on my LinkedIn profile