-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
440 lines (377 loc) · 15.2 KB
/
index.html
File metadata and controls
440 lines (377 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Kwantumpuzzelsimulator</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f5f5f5;
}
.container {
max-width: 1200px;
margin: 0 auto;
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 {
color: #333;
text-align: center;
}
.description {
margin-bottom: 20px;
padding: 10px;
background-color: #f0f8ff;
border-radius: 4px;
}
</style>
<!-- Load p5.js from CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.7.0/p5.min.js"></script>
</head>
<body>
<div class="container">
<h1>Kwantumpuzzelsimulator</h1>
<div class="description">
<p>Deze simulator demonstreert een 2x2-kwantumschuifpuzzel met zowel klassieke als kwantumoperaties.</p>
<p>Gebruik de knoppen om de kwantumtoestand van de puzzel te manipuleren.</p>
</div>
<!-- The canvas will be created by p5.js inside this div -->
<div id="canvas-container"></div>
</div>
<script>
// Quantum Rubik's Cube Simulator in p5.js
// Based on the original Python implementation
let currentState;
let represType = 'prob'; // 'prob' or 'math'
// Vector representations
const v0repr = "\n🟩🟩\n🟦🟦";
const v1repr = "\n🟦🟦\n🟩🟩";
const v2repr = "\n🟦🟩\n🟦🟩";
const v3repr = "\n🟩🟦\n🟩🟦";
const v4repr = "\n🟦🟩\n🟩🟦";
const v5repr = "\n🟩🟦\n🟦🟩";
// Complex number operations
class Complex {
constructor(real, imag = 0) {
this.real = real;
this.imag = imag;
}
static multiply(a, b) {
return new Complex(
a.real * b.real - a.imag * b.imag,
a.real * b.imag + a.imag * b.real
);
}
static add(a, b) {
return new Complex(
a.real + b.real,
a.imag + b.imag
);
}
multiply(scalar) {
if (scalar instanceof Complex) {
return Complex.multiply(this, scalar);
} else {
return new Complex(this.real * scalar, this.imag * scalar);
}
}
toString() {
if (Math.abs(this.imag) < 1e-10) {
return this.real.toFixed(4).replace(/\.?0+$/, '');
} else if (Math.abs(this.real) < 1e-10) {
return this.imag.toFixed(4).replace(/\.?0+$/, '') + 'i';
} else {
const sign = this.imag >= 0 ? '+' : '';
return this.real.toFixed(4).replace(/\.?0+$/, '') + sign + this.imag.toFixed(4).replace(/\.?0+$/, '') + 'i';
}
}
abs() {
return Math.sqrt(this.real * this.real + this.imag * this.imag);
}
}
// Define the i constant
const I = new Complex(0, 1);
// Vector class for quantum states
class Vector {
constructor(represent, coeff = new Complex(1), otherVecToSum = null) {
if (otherVecToSum === null) {
this._repres = [represent];
this._coefficients = [coeff];
} else {
if (otherVecToSum.getRepr().includes(represent)) {
const indx = otherVecToSum.getRepr().indexOf(represent);
this._repres = [...otherVecToSum.getRepr()];
this._coefficients = [...otherVecToSum.getCoeff()];
this._coefficients[indx] = Complex.add(this._coefficients[indx], coeff);
} else {
this._repres = [...otherVecToSum.getRepr()];
this._coefficients = [...otherVecToSum.getCoeff()];
this._repres.push(represent);
this._coefficients.push(coeff);
}
}
// Remove all zero coefficients
for (let i = this._coefficients.length - 1; i >= 0; i--) {
if (Math.abs(this._coefficients[i].real) < 1e-10 && Math.abs(this._coefficients[i].imag) < 1e-10) {
this._coefficients.splice(i, 1);
this._repres.splice(i, 1);
}
}
}
multiply(constant) {
if (this._repres.length === 0) return new Vector(v0repr, new Complex(0));
let newVec = new Vector(
this._repres[0],
this._coefficients[0] instanceof Complex
? this._coefficients[0].multiply(constant)
: new Complex(this._coefficients[0] * constant)
);
for (let i = 1; i < this._repres.length; i++) {
const newCoeff = this._coefficients[i] instanceof Complex
? this._coefficients[i].multiply(constant)
: new Complex(this._coefficients[i] * constant);
newVec = new Vector(this._repres[i], newCoeff, newVec);
}
return newVec;
}
toString() {
if (this._repres.length === 0) return "0";
if (represType === 'math') {
let result = "";
for (let i = 0; i < this._repres.length; i++) {
result += this._coefficients[i].toString() + " " + this._repres[i];
if (i < this._repres.length - 1) {
result += "\n+\n";
}
}
return result;
} else {
// Probability representation
const probabilities = this.getProbs();
let result = "";
for (let i = 0; i < this._repres.length; i++) {
result += (probabilities[i] * 100).toFixed(1) + "% " + this._repres[i];
if (i < this._repres.length - 1) {
result += "\n+\n";
}
}
return result;
}
}
copy() {
const representants = this.getRepr();
const coefficients = this.getCoeff();
if (representants.length === 0) return new Vector(v0repr, new Complex(0));
let copy = new Vector(representants[0], coefficients[0]);
for (let i = 1; i < representants.length; i++) {
copy = new Vector(representants[i], coefficients[i], copy);
}
return copy;
}
add(other) {
const otherRepr = other.getRepr();
const otherCoeff = other.getCoeff();
// Handle edge cases
if (otherRepr.length === 0) return this.copy();
if (this._repres.length === 0) return other.copy();
let newVec = this.copy();
for (let i = 0; i < otherRepr.length; i++) {
newVec = new Vector(otherRepr[i], otherCoeff[i], newVec);
}
return newVec;
}
getProbs() {
const probabilities = [];
for (const coef of this._coefficients) {
probabilities.push(coef.abs() * coef.abs());
}
return probabilities;
}
getRepr() {
return this._repres;
}
getCoeff() {
return this._coefficients;
}
equals(other) {
const diff = this.add(other.multiply(new Complex(-1)));
return diff.getCoeff().length === 0 || diff.getCoeff().every(c => Math.abs(c.real) < 1e-10 && Math.abs(c.imag) < 1e-10);
}
}
// Operator class for quantum operations
class Operator {
constructor(operations) {
this._berekening = {};
for (const operation of operations) {
this._berekening[operation[0].getRepr()[0]] = operation[1];
}
}
apply(vect) {
const vectRepr = vect.getRepr();
const vectCoeff = vect.getCoeff();
if (vectRepr.length === 0) return new Vector(v0repr, new Complex(0));
// Apply the operator to the first term
let newVect = this._berekening[vectRepr[0]].multiply(vectCoeff[0]);
// Apply to the remaining terms and sum
for (let i = 1; i < vectRepr.length; i++) {
newVect = newVect.add(this._berekening[vectRepr[i]].multiply(vectCoeff[i]));
}
return newVect;
}
multiply(constant) {
const newOperations = [];
for (const key in this._berekening) {
newOperations.push([new Vector(key), this._berekening[key].multiply(constant)]);
}
return new Operator(newOperations);
}
add(other) {
const newOperations = [];
for (const key in this._berekening) {
const sum = this._berekening[key].add(other.apply(new Vector(key)));
newOperations.push([new Vector(key), sum]);
}
return new Operator(newOperations);
}
}
// Define basic vectors
const v0 = new Vector(v0repr);
const v1 = new Vector(v1repr);
const v2 = new Vector(v2repr);
const v3 = new Vector(v3repr);
const v4 = new Vector(v4repr);
const v5 = new Vector(v5repr);
// Define classical operators
const Id = new Operator([
[v0, v0], [v1, v1], [v2, v2], [v3, v3], [v4, v4], [v5, v5]
]);
const SU = new Operator([
[v0, v0.multiply(new Complex(-1))],
[v1, v1.multiply(new Complex(-1))],
[v2, v5],
[v3, v4],
[v4, v3],
[v5, v2]
]);
const SD = new Operator([
[v0, v0.multiply(new Complex(-1))],
[v1, v1.multiply(new Complex(-1))],
[v2, v4],
[v3, v5],
[v4, v2],
[v5, v3]
]);
const SL = new Operator([
[v0, v4],
[v1, v5],
[v2, v2.multiply(new Complex(-1))],
[v3, v3.multiply(new Complex(-1))],
[v4, v0],
[v5, v1]
]);
const SR = new Operator([
[v0, v5],
[v1, v4],
[v2, v2.multiply(new Complex(-1))],
[v3, v3.multiply(new Complex(-1))],
[v4, v1],
[v5, v0]
]);
// Quantum operators
const sqrtSU = Id.add(SU.multiply(I)).multiply(1/Math.sqrt(2));
const sqrtSD = Id.add(SD.multiply(I)).multiply(1/Math.sqrt(2));
const sqrtSL = Id.add(SL.multiply(I)).multiply(1/Math.sqrt(2));
const sqrtSR = Id.add(SR.multiply(I)).multiply(1/Math.sqrt(2));
// Custom button creation function
function createCustomButton(label, x, y, callback) {
const button = document.createElement('button');
button.innerText = label;
button.style.position = 'absolute';
button.style.left = x + 'px';
button.style.top = y + 'px';
button.style.padding = '8px 12px';
button.style.margin = '5px';
button.style.backgroundColor = '#4CAF50';
button.style.color = 'white';
button.style.border = 'none';
button.style.borderRadius = '4px';
button.style.cursor = 'pointer';
button.addEventListener('mouseenter', () => {
button.style.backgroundColor = '#45a049';
});
button.addEventListener('mouseleave', () => {
button.style.backgroundColor = '#4CAF50';
});
button.addEventListener('click', callback);
document.body.appendChild(button);
return button;
}
// p5.js setup and draw functions
function setup() {
const canvas = createCanvas(800, 600);
canvas.parent('canvas-container');
textFont('monospace');
textSize(16);
// Initialize the current state
currentState = v0;
// Create buttons
createCustomButton('SU', 1000, 500, () => {
currentState = SU.apply(currentState);
});
createCustomButton('SD', 1060, 500, () => {
currentState = SD.apply(currentState);
});
createCustomButton('SL', 1000, 550, () => {
currentState = SL.apply(currentState);
});
createCustomButton('SR', 1060, 550, () => {
currentState = SR.apply(currentState);
});
createCustomButton('sqrtSU', 1120, 500, () => {
currentState = sqrtSU.apply(currentState);
});
createCustomButton('sqrtSD', 1190, 500, () => {
currentState = sqrtSD.apply(currentState);
});
createCustomButton('sqrtSL', 1120, 550, () => {
currentState = sqrtSL.apply(currentState);
});
createCustomButton('sqrtSR', 1190, 550, () => {
currentState = sqrtSR.apply(currentState);
});
createCustomButton('Reset Puzzle', 1190, 600, () => {
currentState = v0;
});
createCustomButton('Switch Representation', 1000, 600, () => {
represType = represType === 'math' ? 'prob' : 'math';
});
}
function draw() {
background(240);
// Display current state
fill(0);
text("Current State:", 50, 50);
// Format state for display
const stateLines = currentState.toString().split('\n');
for (let i = 0; i < stateLines.length; i++) {
text(stateLines[i], 50, 80 + i * 20);
}
// Display current representation type
text("Huidige representatie: " + represType, 425, 425);
// Display explanation
let explanation = "Dit is een simulator voor de 2x2-kwantumschuifpuzzel. De knoppen SU, SD, SL, SR zijn respectievelijk een gewone wissel (Swap) ";
explanation += " van de bovenste (Upper), onderste (Down), linkse (Left) en rechtse (Right) tegels.\n\nDe knoppen sqrtSU, etc. zijn de kwantumwissels.";
explanation += " Hierbij is er 50% kans dat de wissel doorgevoerd wordt en 50% kans dat dit niet gebeurt.\n\nDe knop 'Switch representatie' wisselt";
explanation += " de weergave tussen de kans dat de puzzel zich in deze staat bevindt (prob) en de wiskundige weergave (math) van de puzzel.";
text(explanation, 400, 50, 350, 400);
}
// Manually initialize p5.js
new p5();
</script>
</body>
</html>