-
Notifications
You must be signed in to change notification settings - Fork 27
/
q.go
447 lines (364 loc) · 8.92 KB
/
q.go
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
package q
import (
"github.com/itsubaki/q/math/matrix"
"github.com/itsubaki/q/math/number"
"github.com/itsubaki/q/math/rand"
"github.com/itsubaki/q/quantum/gate"
"github.com/itsubaki/q/quantum/qubit"
)
// Qubit is a quantum bit.
type Qubit int
// Index returns the index of qubit.
func (q Qubit) Index() int {
return int(q)
}
// Index returns the index list of qubits.
func Index(qb ...Qubit) []int {
idx := make([]int, len(qb))
for i := range qb {
idx[i] = qb[i].Index()
}
return idx
}
// Theta returns 2 * pi / 2**k
func Theta(k int) float64 {
return gate.Theta(k)
}
// Q is a quantum computation simulator.
type Q struct {
qb *qubit.Qubit
Rand func() float64
}
// New returns a new quantum computation simulator.
func New() *Q {
return &Q{
qb: nil,
Rand: rand.Float64,
}
}
// New returns a new qubit.
func (q *Q) New(v ...complex128) Qubit {
if q.qb == nil {
q.qb = qubit.New(v...)
q.qb.Rand = q.Rand
return Qubit(0)
}
q.qb.TensorProduct(qubit.New(v...))
return Qubit(q.NumberOfBit() - 1)
}
// NewOf returns a new qubit from binary string.
func (q *Q) NewOf(binary string) []Qubit {
qb := make([]Qubit, len(binary))
for i, b := range binary {
if b == '0' {
qb[i] = q.Zero()
continue
}
qb[i] = q.One()
}
return qb
}
// Zero returns a qubit in the zero state.
func (q *Q) Zero() Qubit {
return q.New(1, 0)
}
// One returns a qubit in the one state.
func (q *Q) One() Qubit {
return q.New(0, 1)
}
// ZeroWith returns n qubits in the zero state.
func (q *Q) ZeroWith(n int) []Qubit {
qb := make([]Qubit, n)
for i := 0; i < n; i++ {
qb[i] = q.Zero()
}
return qb
}
// One returns n qubits in the one state.
func (q *Q) OneWith(n int) []Qubit {
qb := make([]Qubit, n)
for i := 0; i < n; i++ {
qb[i] = q.One()
}
return qb
}
// ZeroLog2 returns n qubits in the zero state.
// n is greater than or equal to log2(N).
func (q *Q) ZeroLog2(N int) []Qubit {
return q.ZeroWith(number.Log2(N) + 1)
}
// NumberOfBit returns the number of qubits.
func (q *Q) NumberOfBit() int {
return q.qb.NumberOfBit()
}
// Amplitude returns the amplitude of qubits.
func (q *Q) Amplitude() []complex128 {
return q.qb.Amplitude()
}
// Probability returns the probability of qubits.
func (q *Q) Probability() []float64 {
return q.qb.Probability()
}
// Reset sets qubits to the zero state.
func (q *Q) Reset(qb ...Qubit) {
for i := range qb {
if q.Measure(qb[i]).IsOne() {
q.X(qb[i])
}
}
}
// U applies U gate.
func (q *Q) U(theta, phi, lambda float64, qb ...Qubit) *Q {
return q.Apply(gate.U(theta, phi, lambda), qb...)
}
// I applies I gate.
func (q *Q) I(qb ...Qubit) *Q {
return q.Apply(gate.I(), qb...)
}
// X applies X gate.
func (q *Q) X(qb ...Qubit) *Q {
return q.Apply(gate.X(), qb...)
}
// Y applies Y gate.
func (q *Q) Y(qb ...Qubit) *Q {
return q.Apply(gate.Y(), qb...)
}
// Z applies Z gate.
func (q *Q) Z(qb ...Qubit) *Q {
return q.Apply(gate.Z(), qb...)
}
// H applies H gate.
func (q *Q) H(qb ...Qubit) *Q {
return q.Apply(gate.H(), qb...)
}
// S applies S gate.
func (q *Q) S(qb ...Qubit) *Q {
return q.Apply(gate.S(), qb...)
}
// T applies T gate.
func (q *Q) T(qb ...Qubit) *Q {
return q.Apply(gate.T(), qb...)
}
// R applies R gate with theta.
func (q *Q) R(theta float64, qb ...Qubit) *Q {
return q.Apply(gate.R(theta), qb...)
}
// RX applies RX gate with theta.
func (q *Q) RX(theta float64, qb ...Qubit) *Q {
return q.Apply(gate.RX(theta), qb...)
}
// RY applies RY gate with theta.
func (q *Q) RY(theta float64, qb ...Qubit) *Q {
return q.Apply(gate.RY(theta), qb...)
}
// RZ applies RZ gate with theta.
func (q *Q) RZ(theta float64, qb ...Qubit) *Q {
return q.Apply(gate.RZ(theta), qb...)
}
// Apply applies matrix to qubits.
func (q *Q) Apply(m matrix.Matrix, qb ...Qubit) *Q {
if len(qb) < 1 {
q.qb.Apply(m)
return q
}
idx := make(map[int]bool)
for _, i := range Index(qb...) {
idx[i] = true
}
g := gate.I()
if _, ok := idx[0]; ok {
g = m
}
for i := 1; i < q.NumberOfBit(); i++ {
if _, ok := idx[i]; ok {
g = g.TensorProduct(m)
continue
}
g = g.TensorProduct(gate.I())
}
q.qb.Apply(g)
return q
}
func (q *Q) Controlled(m matrix.Matrix, control []Qubit, target Qubit) *Q {
n := q.NumberOfBit()
g := gate.Controlled(m, n, Index(control...), target.Index())
q.qb.Apply(g)
return q
}
func (q *Q) C(m matrix.Matrix, control, target Qubit) *Q {
return q.Controlled(m, []Qubit{control}, target)
}
// ControlledNot applies CNOT gate.
func (q *Q) ControlledNot(control []Qubit, target Qubit) *Q {
n := q.NumberOfBit()
g := gate.ControlledNot(n, Index(control...), target.Index())
q.qb.Apply(g)
return q
}
// CNOT applies CNOT gate.
func (q *Q) CNOT(control, target Qubit) *Q {
return q.ControlledNot([]Qubit{control}, target)
}
// CCNOT applies CCNOT gate.
func (q *Q) CCNOT(control0, control1, target Qubit) *Q {
return q.ControlledNot([]Qubit{control0, control1}, target)
}
// CCCNOT applies CCCNOT gate.
func (q *Q) CCCNOT(control0, control1, control2, target Qubit) *Q {
return q.ControlledNot([]Qubit{control0, control1, control2}, target)
}
// Toffoli applies Toffoli gate.
func (q *Q) Toffoli(control0, control1, target Qubit) *Q {
return q.CCNOT(control0, control1, target)
}
// ControlledZ applies Controlled-Z gate.
func (q *Q) ControlledZ(control []Qubit, target Qubit) *Q {
n := q.NumberOfBit()
g := gate.ControlledZ(n, Index(control...), target.Index())
q.qb.Apply(g)
return q
}
func (q *Q) CZ(control, target Qubit) *Q {
return q.ControlledZ([]Qubit{control}, target)
}
func (q *Q) CCZ(control0, control1, target Qubit) *Q {
return q.ControlledZ([]Qubit{control0, control1}, target)
}
func (q *Q) ControlledR(theta float64, control []Qubit, target Qubit) *Q {
n := q.NumberOfBit()
g := gate.ControlledR(theta, n, Index(control...), target.Index())
q.qb.Apply(g)
return q
}
// CR applies Controlled-R gate.
func (q *Q) CR(theta float64, control, target Qubit) *Q {
return q.ControlledR(theta, []Qubit{control}, target)
}
// ControlledModExp2 applies Controlled-ModExp2 gate.
func (q *Q) ControlledModExp2(a, j, N int, control Qubit, target []Qubit) *Q {
n := q.NumberOfBit()
g := gate.ControlledModExp2(n, a, j, N, control.Index(), Index(target...))
q.qb.Apply(g)
return q
}
// CModExp2 applies Controlled-ModExp2 gate.
func (q *Q) CModExp2(a, N int, control []Qubit, target []Qubit) *Q {
for i, c := range control {
q.ControlledModExp2(a, i, N, c, target)
}
return q
}
// Cond applies m if condition is true.
func (q *Q) Cond(condition bool, m matrix.Matrix, qb ...Qubit) *Q {
if condition {
return q.Apply(m, qb...)
}
return q
}
// CondX applies X gate if condition is true.
func (q *Q) CondX(condition bool, qb ...Qubit) *Q {
return q.Cond(condition, gate.X(), qb...)
}
// CondZ applies Z gate if condition is true.
func (q *Q) CondZ(condition bool, qb ...Qubit) *Q {
return q.Cond(condition, gate.Z(), qb...)
}
// Swap applies Swap gate.
func (q *Q) Swap(qb ...Qubit) *Q {
n := q.NumberOfBit()
l := len(qb)
for i := range l / 2 {
q0, q1 := qb[i], qb[(l-1)-i]
g := gate.Swap(n, q0.Index(), q1.Index())
q.qb.Apply(g)
}
return q
}
// QFT applies Quantum Fourier Transform.
func (q *Q) QFT(qb ...Qubit) *Q {
l := len(qb)
for i := range l {
q.H(qb[i])
k := 2
for j := i + 1; j < l; j++ {
q.CR(Theta(k), qb[j], qb[i])
k++
}
}
return q
}
// InverseQFT applies Inverse Quantum Fourier Transform.
func (q *Q) InverseQFT(qb ...Qubit) *Q {
l := len(qb)
for i := l - 1; i > -1; i-- {
k := l - i
for j := l - 1; j > i; j-- {
q.CR(-1*Theta(k), qb[j], qb[i])
k--
}
q.H(qb[i])
}
return q
}
// InvQFT applies Inverse Quantum Fourier Transform.
func (q *Q) InvQFT(qb ...Qubit) *Q {
return q.InverseQFT(qb...)
}
// IQFT applies Inverse Quantum Fourier Transform.
func (q *Q) IQFT(qb ...Qubit) *Q {
return q.InverseQFT(qb...)
}
// M returns the measured state of qubits.
func (q *Q) M(qb ...Qubit) *qubit.Qubit {
return q.Measure(qb...)
}
// Measure returns the measured state of qubits.
func (q *Q) Measure(qb ...Qubit) *qubit.Qubit {
if len(qb) < 1 {
n := q.NumberOfBit()
m := make([]*qubit.Qubit, n)
for i := range n {
m[i] = q.qb.Measure(i)
}
return qubit.TensorProduct(m...)
}
m := make([]*qubit.Qubit, len(qb))
for i := range qb {
m[i] = q.qb.Measure(qb[i].Index())
}
return qubit.TensorProduct(m...)
}
// Clone returns a clone of a quantum computation simulator.
func (q *Q) Clone() *Q {
if q.qb == nil {
return &Q{
qb: nil,
Rand: q.Rand,
}
}
return &Q{
qb: q.qb.Clone(),
Rand: q.qb.Rand,
}
}
// Raw returns the internal qubit.
func (q *Q) Raw() *qubit.Qubit {
return q.qb
}
// String returns the string representation of a quantum computation simulator.
func (q *Q) String() string {
return q.qb.String()
}
// State returns the state of qubits.
func (q *Q) State(reg ...any) []qubit.State {
idx := make([][]int, 0)
for _, r := range reg {
switch r := r.(type) {
case Qubit:
idx = append(idx, []int{r.Index()})
case []Qubit:
idx = append(idx, Index(r...))
}
}
return q.qb.State(idx...)
}