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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class QuantumSystem {
|
| 2 |
+
constructor() {
|
| 3 |
+
this.state = {
|
| 4 |
+
superposition: {
|
| 5 |
+
active: false,
|
| 6 |
+
states: new Array(8).fill(0).map(() => Math.random())
|
| 7 |
+
},
|
| 8 |
+
gates: {
|
| 9 |
+
hadamard: false,
|
| 10 |
+
phase: 0
|
| 11 |
+
},
|
| 12 |
+
search: {
|
| 13 |
+
iterations: 0,
|
| 14 |
+
target: Math.floor(Math.random() * 100),
|
| 15 |
+
probability: 0
|
| 16 |
+
},
|
| 17 |
+
walk: {
|
| 18 |
+
active: false,
|
| 19 |
+
position: 0,
|
| 20 |
+
history: []
|
| 21 |
+
}
|
| 22 |
+
};
|
| 23 |
+
|
| 24 |
+
this.metrics = {
|
| 25 |
+
coherence: 1.0,
|
| 26 |
+
entanglement: 0.0,
|
| 27 |
+
efficiency: 0.0
|
| 28 |
+
};
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
// Superposition methods
|
| 32 |
+
toggleSuperposition() {
|
| 33 |
+
this.state.superposition.active = !this.state.superposition.active;
|
| 34 |
+
if (this.state.superposition.active) {
|
| 35 |
+
this.state.superposition.states = this.state.superposition.states.map(
|
| 36 |
+
() => Math.random() * Math.exp(2 * Math.PI * Math.random())
|
| 37 |
+
);
|
| 38 |
+
}
|
| 39 |
+
this.updateMetrics();
|
| 40 |
+
return this.state.superposition;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
// Gate operations
|
| 44 |
+
applyHadamard() {
|
| 45 |
+
this.state.gates.hadamard = !this.state.gates.hadamard;
|
| 46 |
+
if (this.state.gates.hadamard) {
|
| 47 |
+
this.state.superposition.states = this.state.superposition.states.map(x =>
|
| 48 |
+
(x + (x * -1)) / Math.sqrt(2)
|
| 49 |
+
);
|
| 50 |
+
}
|
| 51 |
+
this.updateMetrics();
|
| 52 |
+
return this.state.gates;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
applyPhaseGate() {
|
| 56 |
+
this.state.gates.phase += Math.PI / 4;
|
| 57 |
+
// Using proper complex number math with real and imaginary parts
|
| 58 |
+
this.state.superposition.states = this.state.superposition.states.map(x => {
|
| 59 |
+
const phase = this.state.gates.phase;
|
| 60 |
+
// Multiply by e^(iφ) = cos(φ) + i*sin(φ)
|
| 61 |
+
return x * Math.cos(phase); // Simplified for visualization - only showing real part
|
| 62 |
+
});
|
| 63 |
+
this.updateMetrics();
|
| 64 |
+
return this.state.gates;
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
// Grover's search simulation
|
| 68 |
+
runGroverSearch() {
|
| 69 |
+
const N = 100;
|
| 70 |
+
const iterations = Math.floor(Math.PI/4 * Math.sqrt(N));
|
| 71 |
+
|
| 72 |
+
this.state.search.iterations++;
|
| 73 |
+
this.state.search.probability = Math.pow(
|
| 74 |
+
Math.sin((2 * this.state.search.iterations + 1) * Math.asin(1/Math.sqrt(N))),
|
| 75 |
+
2
|
| 76 |
+
);
|
| 77 |
+
|
| 78 |
+
this.updateMetrics();
|
| 79 |
+
return this.state.search;
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
// Quantum walk simulation
|
| 83 |
+
startQuantumWalk() {
|
| 84 |
+
if (this.state.walk.active) return;
|
| 85 |
+
|
| 86 |
+
this.state.walk.active = true;
|
| 87 |
+
this.state.walk.interval = setInterval(() => {
|
| 88 |
+
// Quantum walk uses superposition of left and right steps
|
| 89 |
+
const step = (Math.random() - 0.5) * 2 * Math.sqrt(2);
|
| 90 |
+
this.state.walk.position += step;
|
| 91 |
+
this.state.walk.history.push(this.state.walk.position);
|
| 92 |
+
this.updateMetrics();
|
| 93 |
+
// Trigger visualization update
|
| 94 |
+
if (typeof updateVisualizations === 'function') {
|
| 95 |
+
updateVisualizations();
|
| 96 |
+
}
|
| 97 |
+
}, 100);
|
| 98 |
+
|
| 99 |
+
return this.state.walk;
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
stopQuantumWalk() {
|
| 103 |
+
this.state.walk.active = false;
|
| 104 |
+
if (this.state.walk.interval) {
|
| 105 |
+
clearInterval(this.state.walk.interval);
|
| 106 |
+
}
|
| 107 |
+
return this.state.walk;
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
// Metrics calculation
|
| 111 |
+
updateMetrics() {
|
| 112 |
+
// Coherence decays with operations
|
| 113 |
+
this.metrics.coherence = Math.exp(-0.1 * this.state.search.iterations);
|
| 114 |
+
|
| 115 |
+
// Entanglement increases with superposition and operations
|
| 116 |
+
if (this.state.superposition.active) {
|
| 117 |
+
this.metrics.entanglement = Math.min(
|
| 118 |
+
1,
|
| 119 |
+
this.metrics.entanglement + 0.1
|
| 120 |
+
);
|
| 121 |
+
} else {
|
| 122 |
+
this.metrics.entanglement *= 0.9; // Decay when not in superposition
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
// Efficiency based on search progress and walk stability
|
| 126 |
+
const searchEfficiency = 1 - 1/(1 + this.state.search.iterations);
|
| 127 |
+
const walkEfficiency = this.state.walk.active ?
|
| 128 |
+
Math.exp(-Math.abs(this.state.walk.position) / 10) : 1;
|
| 129 |
+
this.metrics.efficiency = (searchEfficiency + walkEfficiency) / 2;
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
getState() {
|
| 133 |
+
return {
|
| 134 |
+
state: this.state,
|
| 135 |
+
metrics: this.metrics
|
| 136 |
+
};
|
| 137 |
+
}
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
// Initialize quantum system
|
| 141 |
+
const quantumSystem = new QuantumSystem();
|
| 142 |
+
|
| 143 |
+
// UI Event handlers
|
| 144 |
+
function toggleSuperposition() {
|
| 145 |
+
quantumSystem.toggleSuperposition();
|
| 146 |
+
updateVisualizations();
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
function applyHadamard() {
|
| 150 |
+
quantumSystem.applyHadamard();
|
| 151 |
+
updateVisualizations();
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
function applyPhaseGate() {
|
| 155 |
+
quantumSystem.applyPhaseGate();
|
| 156 |
+
updateVisualizations();
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
function runGroverSearch() {
|
| 160 |
+
quantumSystem.runGroverSearch();
|
| 161 |
+
updateVisualizations();
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
function startQuantumWalk() {
|
| 165 |
+
quantumSystem.startQuantumWalk();
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
function stopQuantumWalk() {
|
| 169 |
+
quantumSystem.stopQuantumWalk();
|
| 170 |
+
}
|