-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqengine.ts
257 lines (227 loc) · 8.48 KB
/
qengine.ts
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
// define some constants/types to use in the APP.
const r2 = Math.SQRT1_2
const pi = Math.PI
class Complex {
constructor(
public re: f64 = 0,
public im: f64 = 0,
) {}
squaredAbs(): f64 {
return this.re * this.re + this.im * this.im
}
}
enum GateType { X, H, Rx, Cx }
class Gate {
constructor(
public type: GateType,
public qubit: i32,
public target: i32 = 0,
public theta: f64 = 0,
) {}
toString(): string {
let name = ''
switch (this.type) {
case GateType.X: name = 'x'; break;
case GateType.H: name = 'h'; break;
case GateType.Rx: name = 'rx'; break;
case GateType.Cx: name = 'cx'; break;
}
return `[${name},${this.qubit},${this.target},${this.theta}]`
}
}
class QuantumCircuit {
Bits: i32;
circuit: Array<Gate>;
constructor(public Qubits: i32) {
if (Qubits <= 0) console.error("Number of Qubits need to be more than 0")
if (Qubits > 32) console.error("Number of Qubits need to less than 32")
this.Bits = Qubits
this.circuit = []
}
addGate(gate: Gate): void { this.circuit.push(gate) }
x(qubit: i32): void { this.addGate(new Gate(GateType.X, qubit)) }
h(qubit: i32): void { this.addGate(new Gate(GateType.H, qubit)) }
rx(qubit: i32, theta: f64): void { this.addGate(new Gate(GateType.Rx, qubit, 0, theta)) }
cx(qubit: i32, target: i32): void { this.addGate(new Gate(GateType.Cx, qubit, target, 0)) }
ry(qubit: i32, theta: f64): void {
this.rx(qubit, pi / 2);
this.h(qubit);
this.rx(qubit, theta);
this.h(qubit);
this.rx(qubit,-pi / 2);
}
rz(qubit: i32, theta: f64): void {
this.h(qubit);
this.rx(qubit, theta);
this.h(qubit);
}
z(qubit: i32): void { this.rz(qubit,pi) }
y(qubit: i32): void { this.rz(qubit,pi); this.x(qubit) }
toString(): string {
return this.circuit.toString()
}
}
class QuantumSimulator {
circuit: Array<Gate>
Qubits: i32
Bits: i32
stateVector: Array<Complex>
constructor(quantumCircuit: QuantumCircuit) {
const qubits = quantumCircuit.Qubits
this.circuit = quantumCircuit.circuit
this.Qubits = qubits
this.Bits = qubits
this.stateVector = new Array(2 ** qubits)
this.initializeStateVector()
}
initializeStateVector(): void {
this.stateVector[0] = new Complex(1, 0);
for (let i = 1, len = this.stateVector.length; i < len; ++i) {
this.stateVector[i] = new Complex()
}
}
superpose(x: Complex,y: Complex): StaticArray<Complex> {
const
xr = x.re, yr = y.re,
xi = x.im, yi = y.im
return [
new Complex(r2 * (xr + yr), r2 * (xi + yi)),
new Complex(r2 * (xr - yr), r2 * (xi - yi))
]
}
turn(x: Complex, y: Complex, theta: f64): StaticArray<Complex> {
const
xr = x.re, yr = y.re,
xi = x.im, yi = y.im,
tc = Math.cos(theta / 2),
ts = Math.sin(theta / 2)
return [
new Complex(xr * tc + yi * ts, xi * tc - yr * ts),
new Complex(yr * tc + xi * ts, yi * tc - xr * ts)
]
}
probability(shots: i32): string[] {
let probabilities = this.stateVector.map<f64>(value => value.squaredAbs())
let probLength = probabilities.length
let output: string[] = []
for (let shotsCount = 0; shotsCount < shots; shotsCount++) {
let cumu = 0.0
let un = true
let r = Math.random()
for (let i = 0; i < probLength; i++) {
cumu += probabilities[i]
if (r < cumu && un) {
let raw_output = i.toString(2).padStart(this.Qubits, '0')
output.push(raw_output)
un = false
}
}
}
return output
}
statevector(): string {
let output = ""
for (let i = 0, len = this.stateVector.length; i < len; ++i) {
let value = this.stateVector[i]
let bits = i.toString(2).padStart(this.Qubits, '0')
output += `${bits} ${value.re}+${value.im}j\n`
}
return output
}
memory(shots: i32 = 1024): string[] {
return this.probability(shots)
}
counts(shots: i32 = 1024): Map<string,i32> {
let probabilities = this.probability(shots);
let counts = new Map<string,i32>()
for (let i = 0, len = probabilities.length; i < len; i++) {
let value = probabilities[i]
if (counts.has(value)) {
counts.set(value, counts.get(value) + 1)
} else {
counts.set(value, 1)
}
}
return counts
}
toString(): string {
return this.counts().keys().toString()
}
run(shots: i32 = 1024): void {
for (let i = 0, len = this.circuit.length; i < len; ++i) {
let gate = this.circuit[i]
let type = gate.type
// one Gate instructions
if (type == GateType.X || type == GateType.H || type == GateType.Rx) {
const contQubitLimit = 2 ** gate.qubit
const contStateLimit = 2 ** (this.Qubits - gate.qubit - 1)
const contQubitMax = 2 ** (gate.qubit + 1)
const contStateMax = 2 ** (gate.qubit + 0)
for (let contQubit = 0; contQubit < contQubitLimit; ++contQubit) {
for (let contState = 0; contState < contStateLimit; ++contState) {
let b0 = contQubit + contQubitMax * contState
let b1 = b0 + contStateMax
if (gate.type == GateType.X) {
let temp = this.stateVector[b0]
this.stateVector[b0] = this.stateVector[b1]
this.stateVector[b1] = temp
}
if (gate.type == GateType.H) {
let superpositionResult = this.superpose(
this.stateVector[b0],
this.stateVector[b1]
);
this.stateVector[b0] = superpositionResult[0]
this.stateVector[b1] = superpositionResult[1]
}
if (gate.type == GateType.Rx) {
let turn = this.turn(
this.stateVector[b0],
this.stateVector[b1],
gate.theta
);
this.stateVector[b0] = turn[0]
this.stateVector[b1] = turn[1]
}
}
}
} else {
// two Gates Instructions
if (type == GateType.Cx) {
let loopLimit0 = gate.qubit
let loopLimit1 = gate.target
if (gate.target < gate.qubit) {
loopLimit0 = gate.target
loopLimit1 = gate.qubit
}
const cx0Limit = 2 ** loopLimit0
const cx1Limit = 2 ** (loopLimit0 - loopLimit1 - 1)
const cx2Limit = 2 ** (this.Qubits - loopLimit1 - 1)
const cx0Max = 2 ** (loopLimit0 + 1)
const cx1Max = 2 ** (loopLimit1 + 1)
const cx2Max = 2 ** gate.qubit
const b0Max = 2 ** gate.target
for (let cx0 = 0; cx0 < cx0Limit; cx0++) {
for (let cx1 = 0; cx1 < cx1Limit; cx1++) {
for (let cx2 = 0; cx2 < cx2Limit; cx2++) {
let b0 = cx0 + cx0Max * cx1 + cx1Max * cx2 + cx2Max
let b1 = b0 + b0Max
let temp = this.stateVector[b0]
this.stateVector[b0] = this.stateVector[b1]
this.stateVector[b1] = temp
}
}
}
}
}
}
}
}
export function runQuantumSimulator(qubits: i32): string {
let qc = new QuantumCircuit(qubits)
qc.h(0)
qc.cx(0, 1)
let qs = new QuantumSimulator(qc)
qs.run()
return qs.statevector()
}