I Built a Quantum Physics Simulator in JavaScript — And It Broke My Brain
I'm going to tell you something strange.
An electron can pass through two holes at the same time.
Not metaphorically. Not "as if it does." Physically, literally, through both holes simultaneously.
And I wrote a simulation to prove it.
You've heard of this experiment. You probably got it wrong.
The double-slit experiment is usually described like this: "Electrons behave like waves."
Okay. But what does that actually mean?
It means: fire an electron. Let it pass through two slits. Look at the screen.
Expected: two bands.
Reality: dozens of thin stripes. An interference pattern.
Fine. Wave behavior. Got it.
But here's the part that actually matters.
Fire electrons one at a time. One electron. Wait. Another. Wait.
After hundreds of electrons — look at the screen.
The interference pattern is still there.
A single electron interfered with itself.
Then turn on the detector — just to see which slit it went through.
The interference pattern vanishes. Instantly.
The act of looking changed the result.
Coding Made It Click
To compute the interference pattern, you need this formula:
I(y) = (ψ1 + ψ2)^2
Add first, then square. The order matters.
- Add first → both waves interact → interference happens
- Square first → each slit is independent → no interference
In the simulation, for each pixel we compute:
ψ = A · e^(-(Δy²) / (2σ²)) · e^(i · 2πr / λ)
When physicists say "the electron passes through both slits at once," this is exactly what they mean mathematically. ψ₁ and ψ₂ exist simultaneously and interact.
I only truly understood this when I had to write it in code.
How I Built It
For each electron, I do two things.
1. Compute where it will land:
function computeScreenIntensity(y, slits) {
let re = 0, im = 0;
slits.forEach(slit => {
const r = Math.sqrt(L*L + (y - slit.y)**2);
const phase = 2 * Math.PI * r / lambda;
const envelope = Math.exp(-(dy*dy) / (2*sigma*sigma));
re += envelope * Math.cos(phase);
im += envelope * Math.sin(phase);
});
return re*re + im*im; // |ψ|² — probability density
}
ψ₁ + ψ₂ interaction produces interference. High value = electron lands here more often.
2. Sample from that probability (rejection sampling):
do {
y = random point
prob = computeIntensity(y) / maxIntensity
} while (Math.random() > prob)
The electron doesn't land randomly. It lands probabilistically — guided by the wave function.
The pattern doesn't appear instantly. Each electron adds one dot. After hundreds of hits, the interference pattern slowly emerges — exactly like the real experiment.
One Line of Code Kills the Pattern
When the detector is on, the code does one thing:
activeSlits = [slits[Math.random() < 0.5 ? 0 : 1]];
Only one slit is active. ψ₁ + ψ₂ becomes just ψ₁. No interaction. No interference.
The pattern disappears — not because of some mysterious quantum magic, but because the math changes.
Both amplitudes need to be present at the same time for interference to exist. Remove one. Pattern gone.
"Left: detector off (wave). Right: detector on (particle)."
The Question That Has No Answer
Okay. The detector turns on, the pattern disappears. Why?
Physics says: "The wave function collapsed."
But why did it collapse?
This is where physicists still disagree — loudly.
Copenhagen: Before measurement, position didn't exist. Now it does. Don't ask why.
Bohm: The electron always had a position. An invisible pilot wave was guiding it.
Many Worlds: The wave function never collapsed. The universe split in two. You're living in one branch.
Which one is correct?
Nobody knows.
And I think the absence of an answer is more interesting than any answer could be.
The Wave Interference Simulation
To make the physics behind the double-slit visible, I built a second simulation.
Two point sources. Waves spreading outward. They meet and interfere.
const phase = (2 * Math.PI * r) / lambda - time;
total += Math.cos(phase); // superposition of two waves
timeincreases every frame — waves animate. Green = constructive. Pink = destructive.
Each slit in the double-slit experiment is a wave source. The fringes you see on the screen are exactly what two interfering waves produce.
"Wave interference simulation — two sources, infinite patterns."
The wave interference simulation makes this intuitive in a way that equations never could.
Try It Yourself
🔗 Live demo: https://emineugurlu.github.io/double-slit-simulation/
💻 GitHub: https://github.com/emineugurlu/double-slit-simulation
Toggle the detector. Watch the pattern collapse.
Drag the detector strength slider slowly — watch the pattern fade gradually. That's decoherence in real time.
What's Next
Quantum tunneling is coming — a particle passing through a barrier it classically has no right to cross.
The phenomenon that makes modern electronics possible.
If you want to see it when it drops — follow me here. I'll be building it the same way: physics first, then code, then simulation.
If this made you think, or if you want to argue about interpretations of quantum mechanics — drop a comment. I'm here for it.



Top comments (0)