Skip to main content

01 - Qubits and Superposition: Quantum Foundations

A qubit is a pair of complex amplitudes: you check probabilities, the Bloch sphere, interference and no-cloning in NumPy, with no hardware.

What do you think about this article?

01 - Qubits and Superposition: Quantum Foundations
Quantum Computingfedericocalo.dev

At a Glance

A qubit is a pair of complex amplitudes: you check probabilities, the Bloch sphere, interference and no-cloning in NumPy, with no hardware.

You have heard that a qubit is "0 and 1 at the same time" and you do not know what to do with that: you cannot write it in a program, you cannot measure it, and it explains nothing. The definition you need is poorer and more useful. A qubit is a pair of complex numbers $(\alpha, \beta)$ with $|\alpha|^2 + |\beta|^2 = 1$. Measuring it gives 0 with probability $|\alpha|^2$ and 1 with probability $|\beta|^2$, and after the measurement the state is the one you read. Everything that separates a qubit from a coin lies in the fact that $\alpha$ and $\beta$ are complex: they have a phase, and phases add up and cancel before you measure.

This is the first lesson of Quantum Computing in Italiano, the UniAppunti series. You need no prerequisites beyond complex numbers and the product of matrices. Every numerical claim below comes out of a program you can rerun: two NumPy listings, executed with Python 3.14.4 and NumPy 2.5.4. The work is a simulation on your own computer, not access to a quantum device. I repeated the same calculations with Qiskit 2.6.2 (the current version on PyPI on 2026-11-13) and the results coincide.

The state of a qubit: two complex numbers and one constraint

You write the state as $|\psi\rangle = \alpha|0\rangle + \beta|1\rangle$. The symbols $|0\rangle$ and $|1\rangle$ are the two basis vectors: in NumPy they are [1, 0] and [0, 1]. The condition $|\alpha|^2 + |\beta|^2 = 1$ is not a formal detail: the two measurement probabilities must sum to one, and the Born rule defines them precisely as squared moduli. An example with numbers: if $\alpha = \sqrt{3}/2$ and $\beta = 1/2$ (multiplied by any phase), you get 0 with probability 0.75 and 1 with probability 0.25. This is the case the listing builds.

Two complex numbers are four real numbers, but the normalization constraint removes one and the global phase (which we discuss shortly) removes another. Two real parameters remain, which you read as latitude and longitude on a sphere:

$$|\psi\rangle = \cos\tfrac{\theta}{2}\,|0\rangle + e^{i\varphi}\sin\tfrac{\theta}{2}\,|1\rangle, \qquad \theta \in [0,\pi],\ \varphi \in [0, 2\pi)$$

This is the Bloch sphere: $|0\rangle$ at the north pole, $|1\rangle$ at the south pole, the states with 50/50 probability on the equator. The point on the sphere has coordinates $(\sin\theta\cos\varphi,\ \sin\theta\sin\varphi,\ \cos\theta)$, which are also the expectation values of the three Pauli matrices.

import numpy as np

# A qubit state from two angles: theta (latitude), phi (longitude)
theta, phi = np.pi / 3, np.pi / 4
psi = np.array([np.cos(theta / 2), np.exp(1j * phi) * np.sin(theta / 2)])

X = np.array([[0, 1], [1, 0]], dtype=complex)
Y = np.array([[0, -1j], [1j, 0]])
Z = np.array([[1, 0], [0, -1]], dtype=complex)

p = np.abs(psi) ** 2                      # Born rule: |amplitude|^2
print("sum of probabilities:", round(float(p.sum()), 12))
print("P(0), P(1):", np.round(p, 6))

# The Bloch vector comes from the expectation values <psi|sigma|psi>
r = np.array([np.vdot(psi, S @ psi).real for S in (X, Y, Z)])
expected = [np.sin(theta) * np.cos(phi), np.sin(theta) * np.sin(phi), np.cos(theta)]
print("Bloch vector :", np.round(r, 6))
print("closed formula:", np.round(expected, 6))
print("length |r|   :", round(float(np.linalg.norm(r)), 12))

# The global phase changes no statistics, the relative phase does
global_ph = np.exp(1j * 1.234) * psi
print("global phase, same probabilities:", np.allclose(np.abs(global_ph) ** 2, p))
r_g = np.array([np.vdot(global_ph, S @ global_ph).real for S in (X, Y, Z)])
print("global phase, same Bloch vector:", np.allclose(r_g, r))

plus = np.array([1, 1]) / np.sqrt(2)       # |+>
minus = np.array([1, -1]) / np.sqrt(2)     # |->  (relative phase pi)
print("P(0) in Z basis, |+> and |->:", np.round(np.abs(plus) ** 2, 3), np.round(np.abs(minus) ** 2, 3))
print("<X> on |+> and |->:", round(float(np.vdot(plus, X @ plus).real), 3), round(float(np.vdot(minus, X @ minus).real), 3))

# Repeated measurements: 10000 identical preparations, fixed seed
rng = np.random.default_rng(2026)
n = 10000
outcome1 = rng.random(n) < p[1]            # True = outcome 1
print("frequency of 1 over", n, "measurements:", outcome1.mean(), "(theoretical", round(float(p[1]), 4), ")")
print("expected deviation ~ sqrt(p(1-p)/n):", round(float(np.sqrt(p[1] * p[0] / n)), 4))

# Output:
# sum of probabilities: 1.0
# P(0), P(1): [0.75 0.25]
# Bloch vector : [0.612372 0.612372 0.5     ]
# closed formula: [0.612372 0.612372 0.5     ]
# length |r|   : 1.0
# global phase, same probabilities: True
# global phase, same Bloch vector: True
# P(0) in Z basis, |+> and |->: [0.5 0.5] [0.5 0.5]
# <X> on |+> and |->: 1.0 -1.0
# frequency of 1 over 10000 measurements: 0.2447 (theoretical 0.25 )
# expected deviation ~ sqrt(p(1-p)/n): 0.0043

The listing prepares the state with $\theta = \pi/3$ and $\varphi = \pi/4$ and reads its properties in three ways. The line np.abs(psi) ** 2 applies the Born rule and gives 0.75 and 0.25. The Bloch vector is obtained with np.vdot(psi, S @ psi).real, that is $\langle\psi|\sigma|\psi\rangle$ for each Pauli matrix (vdot conjugates its first argument, which is why you need exactly that function and not a naive dot product). The result matches the closed formula to the sixth digit, and its length is 1: for a pure state the point lies on the surface, not inside.

The lines on the phase show the distinction that supports everything else. Multiplying the state by np.exp(1j * 1.234) changes neither the probabilities nor the Bloch vector: a global phase is not observable, which is why the sphere has two parameters and not three. The relative phase between the two terms, on the other hand, changes the physical state. The states $|+\rangle = (|0\rangle+|1\rangle)/\sqrt2$ and $|-\rangle = (|0\rangle-|1\rangle)/\sqrt2$ both give 0.5 and 0.5 when measured in the $Z$ basis, but the expectation value of $X$ is +1 on the first and −1 on the second. If you only look at the probabilities in one basis, you do not see that difference: you have to change basis, and this is what the Hadamard gate will do in the lesson on gates.

The last part of the listing simulates 10,000 measurements with a fixed-seed generator: the frequency of outcome 1 is 0.2447 against a theoretical 0.25. The expected deviation of a frequency over $n$ trials is $\sqrt{p(1-p)/n} \approx 0.0043$, so a difference of 0.0053 is about 1.2 times that deviation: within the norm. With another seed the value changes by that order of magnitude, and this is the first practical thing to remember: you estimate an unknown state with many identical preparations, never with one.

Superposition is not ignorance: you see it through interference

The sentence "the qubit is in 0 or in 1, we do not know which" describes a coin hidden under a cup, and it makes wrong predictions. Take a state $|+\rangle$ and compare it with a classical mixture that is 0 or 1 with probability 1/2. If you measure immediately in the $Z$ basis, the two behave the same way. If instead you apply the Hadamard gate again and then measure, the mixture stays 50/50 and the state $|+\rangle$ returns to $|0\rangle$ with probability 1. The two amplitudes leading to "0" add up, those leading to "1" cancel: this is interference, and a classical probability distribution cannot produce it.

import numpy as np

H = np.array([[1, 1], [1, -1]]) / np.sqrt(2)
Z = np.array([[1, 0], [0, -1]])
zero = np.array([1.0, 0.0])

def prob0(v):
    return float(abs(v[0]) ** 2)

# Circuit A: H, then measure. Circuit B: H, H, then measure.
a = H @ zero
b = H @ H @ zero
print("A  P(0) after H      :", round(prob0(a), 6))
print("B  P(0) after H,H    :", round(prob0(b), 6))

# The same with a classical coin: after the first H the bit is 0 or 1
# with probability 1/2 (density matrix I/2), and H acts on each possibility.
rho_mix = np.eye(2) / 2
rho_pure = np.outer(a, a)
for name, rho in (("50/50 mixture", rho_mix), ("state |+>", rho_pure)):
    after = H @ rho @ H.T
    print(f"{name:14s} P(0) before H: {rho[0,0].real:.3f}   after another H: {after[0,0].real:.3f}")

# A Z in the middle changes the relative phase: H Z H |0> = |1>
c = H @ Z @ H @ zero
print("C  P(1) after H,Z,H  :", round(float(abs(c[1]) ** 2), 6))

# No-cloning: CNOT copies the basis states, but not |+>
CNOT = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]])
def kron(u, v):
    return np.kron(u, v)
one = np.array([0.0, 1.0])
plus = np.array([1.0, 1.0]) / np.sqrt(2)
for name, s in (("|0>", zero), ("|1>", one), ("|+>", plus)):
    out = CNOT @ kron(s, zero)
    fidelity = abs(np.vdot(kron(s, s), out)) ** 2
    print(f"copy of {name}: fidelity with |s>|s> = {fidelity:.3f}")
print("<0|+> =", round(float(np.vdot(zero, plus)), 4), " <0|+>^2 =", round(float(np.vdot(zero, plus)) ** 2, 4))

# Output:
# A  P(0) after H      : 0.5
# B  P(0) after H,H    : 1.0
# 50/50 mixture  P(0) before H: 0.500   after another H: 0.500
# state |+>      P(0) before H: 0.500   after another H: 1.000
# C  P(1) after H,Z,H  : 1.0
# copy of |0>: fidelity with |s>|s> = 1.000
# copy of |1>: fidelity with |s>|s> = 1.000
# copy of |+>: fidelity with |s>|s> = 0.500
# <0|+> = 0.7071  <0|+>^2 = 0.5

In the listing the gate H is the matrix $\tfrac{1}{\sqrt2}\begin{pmatrix}1&1\\1&-1\end{pmatrix}$. Circuit A (a single H) gives 0.5, circuit B (two H in a row) gives 1.0: no intermediate "random choice" would explain the certain return. The pair of lines with rho_mix and rho_pure uses density matrices to compare the two cases: $I/2$ describes the coin, np.outer(a, a) describes the pure state. Before the second gate both have $P(0)=0.5$, after the second one 0.5 against 1.0. Circuit C inserts a $Z$ gate between the two H and moves all the probability onto 1: changing the relative phase turns the interference from constructive into destructive.

The listing also shows why you need this scheme for every quantum algorithm: none of them "tries all the answers in parallel and picks the right one". A measurement returns a single outcome. What an algorithm can do is arrange the phases so that the amplitudes of the wrong answers cancel and that of the right answer is reinforced before the measurement. In the cases of Deutsch-Jozsa and Grover you will see it on precise numbers.

More qubits: 2n amplitudes, and why simulating costs

With $n$ qubits the state is a vector of $2^n$ complex numbers, one for each bit string, and it is built with the tensor product (np.kron). Two qubits have the four amplitudes of $|00\rangle, |01\rangle, |10\rangle, |11\rangle$. In the previous listing kron(s, zero) puts the first qubit on the left: this is the convention we use in this series. Qiskit uses the opposite order (qubit 0 is the least significant bit), as its documentation on bit ordering states: if you compare a vector of your own with one from Qiskit, always check the order before concluding that one of the two is wrong.

The number $2^n$ is also the reason why simulating a quantum computer on a classical computer, with the state-vector method used here, costs exponential memory. A double-precision complex number takes 16 bytes. With 30 qubits you need $2^{30} \cdot 16$ bytes, that is 16 GiB; with 50 qubits you need $2^{50} \cdot 16$ bytes, 16 PiB. The two calculations are arithmetic, not measurements. Adding one qubit doubles the vector: this is why the examples in this series stay on few qubits.

Not all two-qubit states can be written as a product of two single states. $(|00\rangle + |11\rangle)/\sqrt2$ does not factorize: it is the case of the lesson on entanglement. Here it is enough to know that "more qubits" is not "more bits", but a space of amplitudes in which the correlations between qubits have a place of their own.

Why you cannot copy an unknown qubit

With a classical bit you copy the value and move on. For a qubit in an unknown state, the no-cloning theorem, proved by Wootters and Zurek in 1982, says there is no unitary transformation that produces $|\psi\rangle|\psi\rangle$ from $|\psi\rangle|0\rangle$ for every $|\psi\rangle$. The proof is short. If a copier $U$ worked for two states $|\psi\rangle$ and $|\varphi\rangle$, the inner product between the two results, which a unitary transformation preserves, would give $\langle\psi|\varphi\rangle = \langle\psi|\varphi\rangle^2$. The equation $x = x^2$ has only the solutions 0 and 1: the states are either orthogonal or identical. For any other pair the copier does not exist.

The second listing checks it on the concrete case. The CNOT gate (CNOT @ kron(s, zero)) is a perfect copier for the basis states: the fidelity with $|s\rangle|s\rangle$ is 1.000 for $|0\rangle$ and for $|1\rangle$. On $|+\rangle$ it drops to 0.500, because the output is the entangled state $(|00\rangle+|11\rangle)/\sqrt2$ and not $|+\rangle|+\rangle$. The last two lines show the cause: $\langle 0|+\rangle = 0.7071$ and its square is 0.5, two different numbers that cannot be equal as $x = x^2$ would require.

The practical consequence is twofold. You do not make a backup of an unknown quantum state. The flip side is that you cannot even read a qubit twice to check the result: the first measurement has already modified it.

The mistakes made most often

  • Treating the global phase as information: it is not, and comparing two states "equal up to a phase" with == fails.
  • Forgetting to renormalize after writing an amplitude by hand: the probabilities do not sum to one and every later result is wrong.
  • Mixing up the order of the qubits in the tensor product: $|01\rangle$ and $|10\rangle$ are different states, and Qiskit and the textbooks do not agree on the order.
  • Believing that a repeated measurement gives independent results: after the first one the state has collapsed and the following ones return the same outcome.
  • Using the Bloch sphere for several qubits: it holds for a pure state of a single qubit, and already an entangled state of two qubits has no point of its own on the sphere.

These five mistakes have a common cause: they carry a classical intuition into the quantum world (information is copied, reread, indexed like bits). The list is read in this order only for convenience, there is no hierarchy. A check that often saves you is computing np.linalg.norm(psi) after every step done by hand: if it is not 1, there is an error before any physical conclusion.

What you can do now

You can write the state of a qubit, compute its measurement probabilities and its point on the Bloch sphere, and recognize why a global phase does not count and a relative one does. You can check with two matrices and a product that superposition is not ignorance, and that no unitary transformation copies an unknown state. You also have a practical limit: a state-vector simulator doubles its memory with every qubit. In the next article you build the operations that transform these states: quantum gates. An honest limit on all of this: physical qubits are noisy, and how large a circuit can be run reliably today is a subject for the literature on the NISQ era (Preskill, 2018), not for this article.

Frequently asked questions

Is a qubit really "in 0 and 1 at the same time"?

No, it is a misleading description. A superposition state is a new state, distinct from $|0\rangle$ and from $|1\rangle$: interference proves it, because applying the Hadamard gate twice to $|0\rangle$ brings you back to $|0\rangle$ with probability 1, while a classical coin stays at 50%.

Can I copy a qubit the way I copy a bit?

Not if its state is unknown. The no-cloning theorem rules it out: a unitary transformation that copies two non-orthogonal states would have to satisfy $x = x^2$ on their inner product, and this holds only for 0 and 1. You can, instead, copy known basis states.

How many measurements do you need to know the state of a qubit?

Many identical preparations, never just one. Each measurement gives one bit and destroys the state. The observed frequency has a typical deviation of $\sqrt{p(1-p)/n}$: with 10,000 measurements and $p = 0.25$ it is about 0.0043, as in the listing.

Why does simulating many qubits become impossible?

With the state-vector method the memory doubles with every qubit: you need $2^n$ complex numbers, 16 bytes each in double precision. With 30 qubits that is 16 GiB, with 50 qubits 16 PiB.

Later in the series you will also find quantum machine learning (article 9) and quantum hardware (article 10).

Sources

The two listings were run twice with Python 3.14.4 and NumPy 2.5.4, with identical output, and report the real output at the end. No access to quantum hardware or cloud services: everything is local simulation.

Continue from here

To do ittools that do it

Commenti

Caricamento commenti...

Sign in to comment

New articles and tools on Telegram too

The Telegram channel flags the site's new articles and tools.

Did you like this article?

Explore other content on the blog or discover my projects