Quantum Computing
A two-qubit Bell state simulator in Python
A reproducible experiment in phase and measurement basis, using only the Python standard library.
Research question
Can two simulated states with identical Z-basis probabilities be distinguished by a change of measurement basis?
Hypothesis
The plus and minus Bell states agree in Z and give opposite correlation patterns in X.
Method
Run a deterministic four-amplitude simulation. Compare probabilities before and after applying Hadamard gates to both qubits.
Results
Both states give 00 and 11 at 50% each in Z. In X, the plus state gives 00 and 11 while the minus state gives 01 and 10, at 50% each.
Limitations
This is a noiseless classical simulation. It does not measure a physical quantum system or constitute a Bell inequality experiment.
Reproduce the run
Save the script as bell.py and run it with Python 3. The first bit controls the second in the controlled-NOT. Applying a Hadamard to each qubit before reading Z-basis probabilities gives the X-basis distribution.
from math import sqrt
# Basis order: 00, 01, 10, 11. Left bit is qubit 0.
def hadamard(state, bit):
result = state.copy()
stride = 1 << (1 - bit)
for i in range(4):
if i & stride:
continue
j = i | stride
result[i] = (state[i] + state[j]) / sqrt(2)
result[j] = (state[i] - state[j]) / sqrt(2)
return result
def cnot(state):
return [state[0], state[1], state[3], state[2]]
def probabilities(state):
return {f"{i:02b}": round(abs(a) ** 2, 6)
for i, a in enumerate(state)}
plus = cnot(hadamard([1, 0, 0, 0], 0))
minus = [plus[0], plus[1], plus[2], -plus[3]]
for label, state in [("plus", plus), ("minus", minus)]:
print(label, "Z", probabilities(state))
print(label, "X", probabilities(hadamard(hadamard(state, 0), 1)))Recorded output
| State and basis | 00 | 01 | 10 | 11 |
|---|---|---|---|---|
| Plus, Z | 0.5 | 0 | 0 | 0.5 |
| Plus, X | 0.5 | 0 | 0 | 0.5 |
| Minus, Z | 0.5 | 0 | 0 | 0.5 |
| Minus, X | 0 | 0.5 | 0.5 | 0 |
The pattern changes when we change the basis. That is the observable effect of relative phase in this experiment. Extend the simulator with explicit density matrices if you want to model mixtures or channels.