-
Notifications
You must be signed in to change notification settings - Fork 0
/
quantum-fourier-transform.js
179 lines (151 loc) · 4.03 KB
/
quantum-fourier-transform.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
const logger = require('../src/logger')()
const Bits = require('../src/bits')
// this is a workspace to create a QFT algorithm for n-qubits: a circuit with a dynamic number of qubits
frequency ({ period: 2, size: 4 })
frequency ({ period: 4, size: 4 })
frequency ({ period: 8, size: 4 })
frequency ({ period: 2, size: 5 })
frequency ({ period: 4, size: 5 })
frequency ({ period: 8, size: 5 })
frequency ({ period: 2, size: 6 })
frequency ({ period: 4, size: 6 })
frequency ({ period: 8, size: 6 })
frequency ({ period: 5, size: 8 })
frequency ({ period: 10, size: 6 })
function frequency(options) {
input(options.period, options.size)
output(options.period, options.size)
}
function input(period, size) {
Circuit(`input phases for a period of ${period} using ${size} qubits`, size)
.period(period)
.run()
}
function output(period, size) {
let circuit = Circuit(`output for a period of ${period} using ${size} qubits`, size)
.period(period)
.qft(size)
let shots = 1000, tally = 0
repeat(shots, function() {
circuit.run()
tally = tally + circuit.measure().invert().toNumber() + 1
})
let power = Math.pow(2, size)
logger.log(`The frequency is approximately ${tally / shots} from a period of ${period} in ${power}. (${power} / ${period} = ${power / period})\n`)
}
function Circuit(name, size, options) {
let circuit = require('../src/circuit.js')({
name: name,
size: size,
logger: logger,
engine: 'optimized',
order: ['targets', 'controls']
})
return Object.assign(circuit, {
period: function(period) {
return this
.unit('all').h().circuit()
.spread(function(index) {
this.u1(index, [], { lambda: 'pi / ' + period / 2 })
})
},
spread: function(fn) {
for (var i = 0; i < this.size; i++) {
for (var j = 0; j < 1 << i; j++) {
fn.apply(this, [i])
}
}
return this
},
qft: function(size) {
if (true) {
return this.qft_dynamic()
} else {
return this['qft_' + this.size]()
}
},
qft_4: function() {
return this
.h(3)
.cu1(3, 2, { lambda: "pi/2" })
.h(2)
.cu1(3, 1, { lambda: "pi/4" })
.cu1(2, 1, { lambda: "pi/2" })
.h(1)
.cu1(3, 0, { lambda: "pi/8" })
.cu1(2, 0, { lambda: "pi/4" })
.cu1(1, 0, { lambda: "pi/2" })
.h(0)
.swap(0, 3)
.swap(1, 2)
},
qft_5: function() {
return this
.h(4)
.cu1(4, 3, { lambda: "pi/2" })
.h(3)
.cu1(4, 2, { lambda: "pi/4" })
.cu1(3, 2, { lambda: "pi/2" })
.h(2)
.cu1(4, 1, { lambda: "pi/8" })
.cu1(3, 1, { lambda: "pi/4" })
.cu1(2, 1, { lambda: "pi/2" })
.h(1)
.cu1(4, 0, { lambda: "pi/16" })
.cu1(3, 0, { lambda: "pi/8" })
.cu1(2, 0, { lambda: "pi/4" })
.cu1(1, 0, { lambda: "pi/2" })
.h(0)
.swap(0, 4)
.swap(1, 3)
},
qft_6: function() {
return this
.h(5)
.cu1(5, 4, { lambda: "pi/2" })
.h(4)
.cu1(5, 3, { lambda: "pi/4" })
.cu1(4, 3, { lambda: "pi/2" })
.h(3)
.cu1(5, 2, { lambda: "pi/8" })
.cu1(4, 2, { lambda: "pi/4" })
.cu1(3, 2, { lambda: "pi/2" })
.h(2)
.cu1(5, 1, { lambda: "pi/16" })
.cu1(4, 1, { lambda: "pi/8" })
.cu1(3, 1, { lambda: "pi/4" })
.cu1(2, 1, { lambda: "pi/2" })
.h(1)
.cu1(5, 0, { lambda: "pi/32" })
.cu1(4, 0, { lambda: "pi/16" })
.cu1(3, 0, { lambda: "pi/8" })
.cu1(2, 0, { lambda: "pi/4" })
.cu1(1, 0, { lambda: "pi/2" })
.h(0)
.swap(0, 5)
.swap(1, 4)
.swap(2, 3)
},
qft_dynamic: function(begin, length) {
begin = begin || 0
length = length || this.size
repeat(length, function(index) {
let inverse = (begin + length) - 1 - (index)
this.h(inverse)
for (let j = inverse - 1; j >= begin; j--) {
this.cu1(inverse, j, { lambda: 'pi / ' + Math.pow(2, inverse - j) })
}
}.bind(this))
for (let i = begin, length_ = Math.floor((begin + length) / 2); i < length_; i++) {
this.swap(i, length - (i + 1))
}
return this
}
})
}
function repeat(number, fn) {
for (let i = 0; i < number; i++) {
let result = fn.apply(this, [i])
if (result === 'break') break
}
}