An interactive 3D visualization of the Bell State quantum circuit using Three.js. Watch quantum amplitudes evolve through Hadamard and CNOT gates with smooth animations.
- 3D Visualization: Amplitude magnitudes as bar heights, phases as colors
- Quantum Circuit: Implements H → CNOT to create Bell State (|00⟩ + |11⟩)/√2
- Animation System: Smooth interpolation of complex amplitudes over 1 second
- Interactive Controls: Reset, Step, and Play buttons
- Real-time Display: Shows magnitudes and phases for all basis states
- Orbiting Camera: Slowly rotates around the scene
- Clean Architecture: Vanilla JS + Three.js, no frameworks
cd bell-state-visualizer
python3 -m http.server 8000Then open: http://localhost:8000
cd bell-state-visualizer
npx http-server -p 8000Then open: http://localhost:8000
Simply open index.html directly in a modern browser (Chrome, Firefox, Edge).
Note: Some browsers may restrict ES modules from file:// protocol. Use HTTP server if issues occur.
The 2-qubit system is represented as a 4-dimensional complex vector:
|ψ⟩ = α₀₀|00⟩ + α₀₁|01⟩ + α₁₀|10⟩ + α₁₁|11⟩
- Initial State: |00⟩ =
[1, 0, 0, 0] - Hadamard on Q0: Creates superposition
(|00⟩ + |10⟩)/√2 - CNOT(Q0→Q1): Entangles qubits → Bell State
(|00⟩ + |11⟩)/√2
Hadamard Gate (Q0):
H = 1/√2 [1 1]
[1 -1]
CNOT Gate:
- Control: Qubit 0
- Target: Qubit 1
- If control=|1⟩, flip target bit
- Bar Height: Amplitude magnitude |α|
- Bar Color: Phase angle φ
- Red (0°) → Yellow (90°) → Green (180°) → Cyan (270°) → Red (360°)
-
Complex Class (
Complex)- Represents complex numbers with real and imaginary parts
- Operations: add, multiply, scale, magnitude, phase
- Conversion between rectangular and polar forms
-
QuantumState Class (
QuantumState)- Manages 4-amplitude state vector
- Implements quantum gates:
applyHadamard(),applyCNOT() - Handles normalization
-
StateAnimator Class (
StateAnimator)- Smoothly interpolates between quantum states
- Ease-in-out animation over 1 second
- Manages animation state machine
-
Visualizer Class (
Visualizer)- Three.js scene setup and rendering
- 3D bar creation and updates
- Camera orbiting and lighting
-
BellStateApp Class (
BellStateApp)- Main application controller
- Handles UI interactions
- Orchestrates quantum operations and animations
Complexity: Medium Implementation:
- Add
BlochSphereclass with Three.js sphere geometry - Map single-qubit states to (θ, φ) coordinates
- Add separate view for individual qubit reduced density matrices
- Animate transitions on the Bloch sphere surface
Code location: Add after Visualizer class
class BlochSphere {
constructor(scene, position) {
// Create sphere geometry
// Add X, Y, Z axes
// Add state vector arrow
}
updateFromQubit(densityMatrix) {
// Calculate Bloch vector
// Update arrow position
}
}Complexity: Low-Medium Implementation:
- Calculate von Neumann entropy:
S(ρ) = -Tr(ρ log ρ) - Compute entanglement entropy from reduced density matrix
- Add concurrence calculation for 2-qubit systems
- Display metrics in sidebar with real-time updates
Code location: Add to QuantumState class
calculateEntanglementEntropy() {
// Compute reduced density matrix for qubit 0
// Calculate eigenvalues
// Return -Σ λᵢ log(λᵢ)
}Complexity: Medium Implementation:
- Add "Measure" button to UI
- Sample from probability distribution |α|²
- Collapse state vector to measured basis state
- Show measurement statistics over multiple runs
- Add histogram of measurement outcomes
Code location: Add to BellStateApp class
measure() {
const probabilities = this.animator.displayState.amplitudes
.map(a => a.magnitude() ** 2);
const outcome = sampleFromDistribution(probabilities);
this.collapseState(outcome);
}Complexity: High Implementation:
- Create drag-and-drop gate palette (H, X, Y, Z, CNOT, Toffoli)
- Circuit timeline/track for gate placement
- Dynamic gate sequence execution
- Save/load circuit configurations
- Add more quantum gates (Pauli-X, Y, Z, Phase gates)
New components needed:
GateLibraryclassCircuitBuilderUI componentGatebase class with subclasses
Complexity: High Implementation:
- Generalize
QuantumStateto N qubits (2^N amplitudes) - Update gate application to work with arbitrary qubit indices
- Visualize as 2D grid of bars instead of 1D line
- Add qubit selector for gate targets
- Consider amplitude grouping/filtering for clarity
Performance considerations:
- State vector grows exponentially (4 qubits = 16 bars, 5 = 32...)
- May need LOD or selective visualization
- Consider density matrix representation for large N
Complexity: High Implementation:
- Simulate measurements in different bases (X, Y, Z)
- Reconstruct density matrix from measurement data
- Compare reconstructed state with actual state
- Visualize density matrix as heatmap
- Show fidelity metric
Complexity: Medium-High Implementation:
- Add noise channels (depolarizing, amplitude damping, phase damping)
- Animate transition from pure to mixed states
- Show purity decrease:
Tr(ρ²) - Density matrix representation (4×4 for 2 qubits)
- Compare ideal vs. noisy evolution
Complexity: Low Implementation:
- Add tooltips explaining each gate
- Show mathematical expressions for current state
- Display gate matrices when hovering
- Add tutorial mode with step-by-step explanations
- Include quantum mechanics glossary
Complexity: Medium Implementation:
- Use Web Workers for quantum calculations
- Implement instanced rendering for multiple bars
- Add level-of-detail (LOD) for complex scenes
- Optimize animation loop with RAF throttling
- Consider WebGL2 features
Complexity: Low-Medium Implementation:
- Export circuit as QASM (OpenQASM format)
- Save/load state snapshots
- Generate shareable URLs with circuit encoded
- Export animations as GIF/video
- Screenshot functionality (already possible with Three.js)
- Three.js v0.160.0: 3D rendering (loaded from CDN)
- ES6 Modules: Native browser support required
- Chrome 89+
- Firefox 88+
- Safari 15+
- Edge 89+
- Lightweight: ~400 polygons in scene
- Smooth 60 FPS animation
- No external dependencies beyond Three.js
- Self-contained single file
bell-state-visualizer/
├── index.html # Complete application (HTML + CSS + JS)
└── README.md # This file
⟨ψ|ψ⟩ = Σᵢ |αᵢ|² = 1
Applied to basis states |0⟩ and |1⟩:
H|0⟩ = (|0⟩ + |1⟩)/√2
H|1⟩ = (|0⟩ - |1⟩)/√2
Truth table for computational basis:
|00⟩ → |00⟩
|01⟩ → |01⟩
|10⟩ → |11⟩
|11⟩ → |10⟩
Linear interpolation in complex plane:
α(t) = α_start + (α_end - α_start) · ease(t)
Where ease(t) is a smooth interpolation function.
- Add more quantum gates (Toffoli, Fredkin, etc.)
- Implement multi-qubit circuits (3, 4, 5 qubits)
- Add quantum algorithms (Deutsch-Jozsa, Grover's, etc.)
- Create mobile-responsive layout
- Add VR support with WebXR
- Implement quantum error correction codes
- Add sound/audio feedback tied to phases
MIT License - Feel free to use, modify, and distribute.
- Three.js community for excellent 3D library
- Quantum computing educators for pedagogical insights
- Bell State as the "Hello World" of quantum entanglement
Built with precision. No hallucinated APIs. Pure quantum visualization. 🎯