-
Notifications
You must be signed in to change notification settings - Fork 8
/
qubit.js
584 lines (523 loc) · 16.3 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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
const math = require('mathjs')
class QuantumComputing {
constructor() {
}
boot(qubitRegsNum, classicRegNum) {
if (typeof(classicRegNum) === 'undefined') classicRegNum = qubitRegsNum
this.qasm = []
this.qasm.push(`qreg q[${qubitRegsNum}];`)
this.qasm.push(`creg c[${classicRegNum}];\n`)
this.qubitRegsNum = qubitRegsNum
this.classicRegNum = classicRegNum
this.twoBitsEntanglements = {}
this.amplitudes = []
const n = Math.pow(2, qubitRegsNum)
for (let i = 0; i < n; i++) {
this.amplitudes.push(0)
}
this.amplitudes[0] = 1 // pr |00....000> = 1
this.measures = [] // offset is bits offset, value is bits qubit offset
for (let i = 0; i < classicRegNum; i++) {
// TODO
this.measures.push(null)
}
return this
}
/**
*
* eg this.amplitudes = [1/2, 1/2, 1/2, 1/2]
* 00 01 10 11
*
* getSingleQubitPairs(0) =>
* [
* [{bitString: '00', i: 0, amplitudes: 1/2}, {bitString: '01', i: 1, amplitudes: 1/2}],
* [{bitString: '10', i: 0, amplitudes: 1/2}, {bitString: '11', i: 1, amplitudes: 1/2}],
* ]
*
*/
getSingleQubitPairs(offset) {
if (offset >= this.qubitRegsNum) throw `Error: invalid offset ${offset}`
const memory = {}
const pairs = []
const shift = Math.pow(2, offset)
for (let i = 0; i < this.amplitudes.length; i++) {
if (i in memory) continue
const n = Math.floor(i / shift)
const isOne = n % 2 === 1
let a = null,
b = null
if (isOne) {
a = i - shift
b = i
} else {
a = i
b = i + shift
}
memory[a] = true
memory[b] = true
pairs.push([
{i: a, amplitude: this.amplitudes[a]},
{i: b, amplitude: this.amplitudes[b]}
])
}
return pairs
}
/**
*
* like getSingleQubitPairs, but return for two qubits
* |control, target>
* |00> |01> |10> |11>
* a b c d
*
*/
getTwoQubitsPairs(control, target) {
if (control >= this.qubitRegsNum) throw `Error: invalid offset ${control}`
if (target >= this.qubitRegsNum) throw `Error: invalid offset ${target}`
const memory = {}
const pairs = []
const shiftControl = Math.pow(2, control)
const shiftTarget = Math.pow(2, target)
for (let i = 0; i < this.amplitudes.length; i++) {
if (i in memory) continue
const nControl = Math.floor(i / shiftControl)
const controlIsOne = nControl%2 === 1
const nTarget = Math.floor(i / shiftTarget)
const targetIsOne = nTarget%2 === 1
let a = null,
b = null,
c = null,
d = null
if (controlIsOne) {
if (targetIsOne) {
d = i
c = i - shiftTarget
b = d - shiftControl
a = b - shiftTarget
} else {
c = i
d = c + shiftTarget
b = d - shiftControl
a = b - shiftTarget
}
} else {
if (targetIsOne) {
b = i
a = b - shiftTarget
c = a + shiftControl
d = c + shiftTarget
} else {
a = i
b = a + shiftTarget
c = a + shiftControl
d = c + shiftTarget
}
}
memory[a] = true
memory[b] = true
memory[c] = true
memory[d] = true
pairs.push([
{i: a, amplitude: this.amplitudes[a]},
{i: b, amplitude: this.amplitudes[b]},
{i: c, amplitude: this.amplitudes[c]},
{i: d, amplitude: this.amplitudes[d]}
])
}
return pairs
}
// TODO: universal getPairs([offsets])
// TODO: universal applyMatrix([offsets], matrix)
/**
* get probability of qubits[offset]
*/
getQubitProbability(offset) {
if (offset >= this.qubitRegsNum) throw `Error: invalid offset ${offset}`
let probability0 = 0,
probability1 = 0
const shift = Math.pow(2, offset)
for (let i = 0; i < this.amplitudes.length; i++) {
const amplitude = this.amplitudes[i]
if (math.equal(amplitude, 0)) continue
const n = Math.floor(i / shift)
const isOne = n % 2 === 1
if (isOne) {
probability1 += math.abs(math.pow(amplitude, 2))
} else {
probability0 += math.abs(math.pow(amplitude, 2))
}
}
return [probability0, probability1]
}
/**
* eg this.getQubitsProbability([0, 1], [0, 1])
* get probability of qubit[0] = 0 and qubit[1] = 1
*/
getQubitsProbability(offsets, targets) {
let probability = 0
for (let i = 0; i < this.amplitudes.length; i++) {
const amplitude = this.amplitudes[i]
if (math.equal(amplitude, 0)) continue
let found = true
for (let j = 0; j < offsets.length; j++) {
const offset = offsets[j],
target = targets[j]
const shift = Math.pow(2, offset),
n = Math.floor(i / shift)
if (n % 2 !== target) {
found = false
break
}
}
if (found) {
probability += math.abs(math.pow(amplitude, 2))
}
}
// console.log('getQubitsProbability:', offsets, targets, probability)
return probability
}
/**
* eg this.applySingleBitMatrix(0, [[1, 0], [0, 1]])
* apply identity matrix
*
*/
applySingleBitMatrix(offset, matrix) {
const pairs = this.getSingleQubitPairs(offset)
for (let i = 0; i < pairs.length; i++) {
const pair = pairs[i]
const a = pair[0].amplitude,
b = pair[1].amplitude
const r = math.multiply(matrix, math.matrix([[a], [b]]))
this.amplitudes[pair[0].i] = r.subset(math.index(0, 0))
this.amplitudes[pair[1].i] = r.subset(math.index(1, 0))
}
}
applyTwoBitsMatrix(control, target, matrix) {
const pairs = this.getTwoQubitsPairs(control, target)
let entangled = null
for (let i = 0; i < pairs.length; i++) {
const pair = pairs[i]
const a = pair[0].amplitude,
b = pair[1].amplitude,
c = pair[2].amplitude,
d = pair[3].amplitude
const r = math.multiply(matrix, math.matrix([[a], [b], [c], [d]]))
this.amplitudes[pair[0].i] = r.subset(math.index(0, 0))
this.amplitudes[pair[1].i] = r.subset(math.index(1, 0))
this.amplitudes[pair[2].i] = r.subset(math.index(2, 0))
this.amplitudes[pair[3].i] = r.subset(math.index(3, 0))
if (entangled !== null) continue
const a2 = math.pow(r.subset(math.index(0, 0)), 2),
b2 = math.pow(r.subset(math.index(1, 0)), 2),
c2 = math.pow(r.subset(math.index(2, 0)), 2),
d2 = math.pow(r.subset(math.index(3, 0)), 2)
// check entanglement
// 00 01 10 11
const pc_0 = math.add(a2, b2),
pc_1 = math.add(c2, d2)
const pt_0 = math.add(a2, c2),
pt_1 = math.add(b2, d2)
if (!(math.equal(pc_0 * pt_0, a2) &&
math.equal(pc_0 * pt_1, b2) &&
math.equal(pc_1 * pt_0, c2) &&
math.equal(pc_1 * pt_1, d2))) {
entangled = true
this.twoBitsEntanglements[control] = target
this.twoBitsEntanglements[target] = control
} else {
entangled = false
delete this.twoBitsEntanglements[control]
delete this.twoBitsEntanglements[target]
}
}
// console.log('entangled: ', entangled)
}
applyAll(gate) {
for (let i = 0; i < this.qubitRegsNum; i++) {
this[gate](i)
}
return this
}
/**
* Single bit gate
*/
id(offset) { // identity matrix, do nothing
if (offset === 'all') return this.applyAll('id')
this.qasm.push(`id q[${offset}];`)
// this.qubits[offset].applySingleBitMatrix(offset, [[1, 0], [0, 1]])
return this
}
/**
* Pauli X Gate
* 𝛑-rotation around the X axis and has a property that
* X -> X, Z -> -Z.
* Also refered as a bit-flip
*/
x(offset) { // not gate
if (offset === 'all') return this.applyAll('x')
this.qasm.push(`x q[${offset}];`)
this.applySingleBitMatrix(offset, [[0, 1], [1, 0]])
return this
}
not(offset) {
return this.x(offset)
}
/**
* Pauli Y Gate
* 𝛑-rotation around the Y axis and has a property that
* X -> -X, Z -> -Z.
* This is both a bit-flip and a phase-flip, that satisfies Y = XZ.
*/
y(offset) {
if (offset === 'all') return this.applyAll('y')
this.qasm.push(`y q[${offset}];`)
this.applySingleBitMatrix(offset, [[0, math.complex(0, -1)], [math.complex(0, 1), 0]])
return this
}
/**
* Pauli Z Gate
* 𝛑-rotation around the Z axis and has a property that
* X -> -X, Z -> Z.
* Also refered as a phase-flip
*/
z(offset) {
if (offset === 'all') return this.applyAll('z')
this.qasm.push(`z q[${offset}];`)
this.applySingleBitMatrix(offset, [[1, 0], [0, -1]])
return this
}
/**
* The Phase gate that is sqrt(S), which is a pi/4
* rotation around Z axis. This gate is required for
* universal control.
*/
t(offset) {
if (offset === 'all') return this.applyAll('t')
this.qasm.push(`t q[${offset}];`)
this.applySingleBitMatrix(offset, [[1, 0], [0, math.divide(math.complex(1, 1), math.sqrt(2))]])
return this
}
/**
* The Phase gate is the transposed
* conjugate of T
*/
tdg(offset) {
if (offset === 'all') return this.applyAll('tdg')
this.qasm.push(`tdg q[${offset}];`)
this.applySingleBitMatrix(offset, [[1, 0], [0, math.divide(math.complex(1, -1), math.sqrt(2))]])
return this
}
/**
* Phase Gate
* The Phase Gate is sqrt(Z) and has the property that
* it maps X -> Y and Z -> Z. This gate extends H to
* make complex superpositions.
* [[1, 0], [0, i]]
*/
s(offset) {
if (offset === 'all') return this.applyAll('s')
this.qasm.push(`s q[${offset}];`)
this.applySingleBitMatrix(offset, [[1, 0], [0, math.complex(0, 1)]])
return this
}
/**
* The Phase Gate that is transposed conjugate of S and
* has the property that it maps X -> -Y and Z -> Z.
*/
sdg(offset) {
if (offset === 'all') return this.applyAll('sdg')
this.qasm.push(`sdg q[${offset}];`)
this.applySingleBitMatrix(offset, [[1, 0], [0, math.complex(0, -1)]])
return this
}
/**
* Hadamard gate
* 1/sqrt(2) [[1, 1], [1, -1]]
*
*/
h(offset) {
if (offset === 'all') return this.applyAll('h')
this.qasm.push(`h q[${offset}];`)
const t = 1 / Math.sqrt(2)
this.applySingleBitMatrix(offset, [[t, t], [t, -t]])
return this
}
/**
* CNot Gate
* A two-qubit gate that flips the target qubit(i.e. applies Pauli X)
* if control is in state 1. This gate is required to generate entanglement
* and is the physical two qubit gate.
* https://en.wikipedia.org/wiki/Controlled_NOT_gate
*/
cnot(control, target) {
/**
* |ct> c: control, t: target
*
* a|00> + b|01> + c|10> + d|11>
* => transform into
* a|00> + b|01> + c|11> + d|10>
* where a^2 + b^2 + c^2 + d^2 = 1
*
*/
if (typeof(target) === 'undefined') throw('target has to be defined')
this.qasm.push(`cx q[${control}],q[${target}];`)
this.applyTwoBitsMatrix(control, target, [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]])
return this
}
/**
* The barrier prevents transformations accross this source line.
*/
barrier(offsets) {
if (typeof(offsets) === 'undefined' || offsets === 'all') {
offsets = []
for (let i = 0; i < this.qubitRegsNum; i++) {
offsets.push(i)
}
return this.barrier(offsets)
}
this.qasm.push(`barrier q[${offsets.join('],q[')}];`)
// TODO: what does this function do?
return this
}
cx(control, target) {
return this.cnot(control, target)
}
/**
*
* Select the bit on this classical register where the measure will write its value.
* @param {Number} q: qubit offset
* @param {Number} c: classic bit offset. If undefined, then set to q
*/
measure(q, c) {
if (typeof(c) === 'undefined') c = q
if (typeof(q) === 'undefined' || q === 'all') { // measure all
for (let i = 0; i < this.qubitRegsNum; i++) {
this.measure(i, i)
}
return this
}
this.qasm.push(`measure q[${q}] -> c[${c}];`)
this.measures[c] = q
return this
}
toBitString(num, bitsNum) {
let output = ''
for (let i = 0; i < bitsNum; i++) {
if (num % 2 === 0) {
output = '0' + output
} else {
output = '1' + output
}
num = Math.floor(num / 2)
}
return output
}
getResult() {
// n = this.measures.length
// there should be 2^(n)
const output = [] // { bitString, probability }
const n = this.measures.length
let oneToOne = true
for (let i = 0; i < this.measures.length; i++) {
if (this.measures[i] !== i) {
oneToOne = false
break
}
}
if (oneToOne) {
const output = []
for (let i = 0; i < this.amplitudes.length; i++) {
output.push({bitString: this.toBitString(i, this.qubitRegsNum), probability: math.abs(math.pow(this.amplitudes[i], 2))})
}
this.result = output
.map((x)=> {return {bitString: x.bitString, probability: parseFloat(math.format(x.probability))}})
.filter((x)=> x.probability)
return this.result
}
/**
* @param {[type]} offset classical register offset.
* @param {[type]} bitString [description]
* @param {[type]} probability [description]
* @param {Object} [meausred={}] key is qubit offset, value is its state 0 | 1
* @return {[type]} nothing
*/
const helper = (offset, bitString, probability, measured={})=> {
if (math.equal(probability, 0)) return
if (offset >= n) { // end
return output.push({bitString, probability})
}
const qubitOffset = this.measures[offset]
if (qubitOffset === null) { // not initialized
return helper(offset + 1,
'0' + bitString,
probability,
measured)
}
if (qubitOffset.toString() in measured) { // already measured, so have same state.
return helper(offset + 1,
measured[qubitOffset] + bitString,
probability,
measured)
}
// TODO entanglement
const keys = Object.keys(measured)
for (let i = 0; i < keys.length; i++) {
if (this.twoBitsEntanglements[qubitOffset] == keys[i]) {
// console.log('find entanglement: ', keys[i], qubitOffset)
const pr = this.getQubitProbability(parseInt(keys[i]))
helper(offset+1,
'0'+bitString,
math.multiply(
math.divide(probability, pr[0]), // restore probability
this.getQubitsProbability(
[qubitOffset, parseInt(keys[i])],
[0, measured[keys[i]]]
)),
Object.assign({}, measured, {[qubitOffset]: 0}))
helper(offset+1,
'1'+bitString,
math.multiply(
math.divide(probability, pr[1]), // restore probability
this.getQubitsProbability(
[qubitOffset, parseInt(keys[i])],
[1, measured[keys[i]]]
)),
Object.assign({}, measured, {[qubitOffset]: 1}))
return
}
}
const pr = this.getQubitProbability(this.measures[offset])
helper(offset+1,
'0'+bitString,
math.multiply(
probability,
pr[0]),
Object.assign({}, measured, {[qubitOffset]: 0}))
helper(offset+1,
'1'+bitString,
math.multiply(
probability,
pr[1]),
Object.assign({}, measured, {[qubitOffset]: 1}))
}
helper(0, '', 1, {})
this.result = output
.map((x)=> {return {bitString: x.bitString, probability: parseFloat(math.format(x.probability))}})
.filter((x)=> x.probability)
return this.result
}
executeQASM(c) {
throw "Sorry, this function is not implemented yet."
// need to write an interpreter here
const code = c.split('\n').filter(x => x.length > 0)
}
toQASM() {
return this.qasm.join('\n').trim()
}
}
if (typeof(window) !== 'undefined') {
window.QuantumComputing = QuantumComputing
window.math = math
}
if (typeof(module) !== 'undefined') {
module.exports = {QuantumComputing, math}
}