-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphase-estimation-qasm.js
97 lines (78 loc) · 1.81 KB
/
phase-estimation-qasm.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
const logger = require('../src/logger')()
const Bits = require('../src/bits')
estimate(1)
function estimate(value) {
input(value)
output(value)
}
function input(value) {
let size = 4
Circuit(`input for phase estimation of ${value} / ${Math.pow(2, size)}`, size + 1)
.unit(0, 4).h().circuit()
.encode(value)
.run()
}
function output(value) {
let size = 4
Circuit(`output for phase estimation of ${value} / ${Math.pow(2, size)}`, size + 1)
.unit(0, 4).h().circuit()
.encode(value)
.qft_inverse()
.run()
}
function Circuit(name, size) {
let circuit = require('../src/circuit.js')({
name: name,
size: size,
logger: logger,
engine: 'optimized',
order: ['targets', 'controls']
})
return Object.assign(circuit, {
encode: function(phase) {
let lambda = `${phase} * pi / 8`
return this
.cu(4, 3, lambda)
.cu(4, 2, lambda)
.cu(4, 2, lambda)
.cu(4, 1, lambda)
.cu(4, 1, lambda)
.cu(4, 1, lambda)
.cu(4, 1, lambda)
.cu(4, 0, lambda)
.cu(4, 0, lambda)
.cu(4, 0, lambda)
.cu(4, 0, lambda)
.cu(4, 0, lambda)
.cu(4, 0, lambda)
.cu(4, 0, lambda)
.cu(4, 0, lambda)
},
cu: function(target, control, lambda) {
return this
.u1(target, [], { lambda: `-${lambda}` })
.cx(target, control)
.u1(target, [], { lambda: `${lambda}` })
.cx(target, control)
},
qft_inverse: function(begin, length) {
return this
.h(0)
.cu1(1, 0, { lambda: '-1 * pi / 2' })
.h(1)
.cu1(2, 0, { lambda: '-1 * pi / 4' })
.cu1(2, 1, { lambda: '-1 * pi / 2' })
.h(2)
.cu1(3, 0, { lambda: '-1 * pi / 8' })
.cu1(3, 1, { lambda: '-1 * pi / 4' })
.cu1(3, 2, { lambda: '-1 * pi / 2' })
.h(3)
}
})
}
function repeat(number, fn) {
for (let i = 0; i < number; i++) {
let result = fn.apply(this, [i])
if (result === 'break') break
}
}