-
Notifications
You must be signed in to change notification settings - Fork 0
/
qubit.js
74 lines (64 loc) · 1.97 KB
/
qubit.js
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
export class Qubit {
constructor(coefficients) {
this.coefficients = coefficients;
}
measure() {
const probabilities = this.coefficients.map(
(coefficient) => Math.pow(Math.abs(coefficient), 2)
);
const randomNum = Math.random();
let cumulativeProb = 0;
for (let i = 0; i < probabilities.length; i++) {
cumulativeProb += probabilities[i];
if (randomNum < cumulativeProb) {
return i; // Índice do estado medido
}
}
}
// Rotação de qubit em torno do eixo X
rotateX(angle) {
const rotationMatrix = [
[Math.cos(angle / 2), -1 * Math.sin(angle / 2)],
[-1 * Math.sin(angle / 2), Math.cos(angle / 2)],
];
this.coefficients = this.applyMatrix(rotationMatrix, this.coefficients);
}
// Rotação de qubit em torno do eixo Y
rotateY(angle) {
const rotationMatrix = [
[Math.cos(angle / 2), -Math.sin(angle / 2)],
[Math.sin(angle / 2), Math.cos(angle / 2)],
];
this.coefficients = this.applyMatrix(rotationMatrix, this.coefficients);
}
// Rotação de qubit em torno do eixo Z (inversão de fase)
rotateZ(angle) {
const rotationMatrix = [
[Math.exp(-1 * angle / 2), 0],
[0, Math.exp(1 * angle / 2)],
];
this.coefficients = this.applyMatrix(rotationMatrix, this.coefficients);
}
// Aplicar uma matriz unitária ao vetor de coeficientes do qubit
applyMatrix(matrix, coefficients) {
const result = [];
for (let i = 0; i < matrix.length; i++) {
let sum = 0;
for (let j = 0; j < matrix[i].length; j++) {
sum += matrix[i][j] * coefficients[j];
}
result.push(sum);
}
return result;
}
// Entrelaçar dois qubits
entangle(qubit) {
const entangledCoefficients = [];
for (const coefficient1 of this.coefficients) {
for (const coefficient2 of qubit.coefficients) {
entangledCoefficients.push(coefficient1 * coefficient2);
}
}
this.coefficients = entangledCoefficients;
}
}