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