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
//! Implementations of quantum gates, intended for consumer use.

use complex::Complex;

use gate::Gate;
use ket::Ket;
use matrix::Matrix;

/// The identity gate, not mutating the state at all.
#[allow(unused)]
pub fn identity(width: usize) -> Gate {
    let m = Matrix::identity(Ket::size(width));

    Gate::new(width, m)
}

/// The Hadamard gate.
///
/// See [Wikipedia](https://en.wikipedia.org/wiki/Hadamard_transform#Quantum_computing_applications)
/// for more information.
#[allow(unused, trivial_numeric_casts)]
pub fn hadamard(n: usize) -> Gate {
    let sqrt2inv = 2.0f64.sqrt().recip();

    let mut m = match n {
        0 => Matrix::identity(1),
        1 => {
            m_real![sqrt2inv,  sqrt2inv;
                     sqrt2inv, -sqrt2inv]
        }
        2 => {
            m_real![0.5,  0.5,  0.5,  0.5;
                     0.5, -0.5,  0.5, -0.5;
                     0.5,  0.5, -0.5, -0.5;
                     0.5, -0.5, -0.5,  0.5]
        }
        _ => panic!("Cannot compute Hadamard gate of dimension > 3!"),

    };

    Gate::new(n, m)
}

/// The Pauli-X gate.
///
/// See [Wikipedia](https://en.wikipedia.org/wiki/Quantum_gate#Pauli-X_gate)
/// for more information.
#[allow(unused)]
pub fn pauli_x() -> Gate {
    let m = m_real![0, 1;
                    1, 0];

    Gate::new(1, m)
}

/// The Pauli-Y gate.
///
/// See [Wikipedia](https://en.wikipedia.org/wiki/Quantum_gate#Pauli-Y_gate)
/// for more information.
#[allow(unused)]
pub fn pauli_y() -> Gate {
    let m = m![Complex::zero(),
               -Complex::i();
               Complex::i(),
               Complex::zero()];

    Gate::new(1, m)
}

/// The Pauli-Z gate.
///
/// See [Wikipedia](https://en.wikipedia.org/wiki/Quantum_gate#Pauli-Z_gate)
/// for more information.
#[allow(unused)]
pub fn pauli_z() -> Gate {
    let m = m_real![1,  0;
                    0, -1];

    Gate::new(1, m)
}

/// A single qubit phase-shift gate.
///
/// See [Wikipedia](https://en.wikipedia.org/wiki/Quantum_gate#Phase_shift_gates)
/// for more information.
#[allow(unused)]
pub fn phase_shift(phi: f64) -> Gate {
    let m = m![Complex::one(),
               Complex::zero();
               Complex::zero(),
               Complex::new_euler(1f64, phi)];

    Gate::new(1, m)
}

/// The two qubit swap gate.
///
/// This swaps the value of the first and second qubit.
///
/// See [Wikipedia](https://en.wikipedia.org/wiki/Quantum_gate#Swap_gate)
/// for more information.
#[allow(unused)]
pub fn swap() -> Gate {
    let m = m_real![1, 0, 0, 0;
                    0, 0, 1, 0;
                    0, 1, 0, 0;
                    0, 0, 0, 1];

    Gate::new(2, m)
}

/// The two qubit sqrt(swap) gate.
///
/// This performs half-way of a two-qubit swap.
///
/// It is universal such that any quantum many qubit gate can be constructed from
/// only sqrt(swap) and single qubit gates.
///
/// See [Wikipedia](https://en.wikipedia.org/wiki/Quantum_gate#Square_root_of_Swap_gate)
/// for more information.
#[allow(unused)]
pub fn sqrt_swap() -> Gate {
    let alpha_one = c!(0.5f64, 0.5f64);
    let alpha_two = c!(0.5f64, -0.5f64);

    let m = m![Complex::one(),  Complex::zero(), Complex::zero(), Complex::zero();
               Complex::zero(), alpha_one,       alpha_two,       Complex::zero();
               Complex::zero(), alpha_two,       alpha_one,       Complex::zero();
               Complex::zero(), Complex::zero(), Complex::zero(), Complex::one() ];

    Gate::new(2, m)
}

/// The two qubit controlled not gate.
///
/// See [Wikipedia](https://en.wikipedia.org/wiki/Quantum_gate#Controlled_gates)
/// for more information.
#[allow(unused)]
pub fn controlled_not() -> Gate {
    let m = m_real![1, 0, 0, 0;
                    0, 1, 0, 0;
                    0, 0, 0, 1;
                    0, 0, 1, 0];

    Gate::new(2, m)
}

/// A two qubit controlled gate.
///
/// See [Wikipedia](https://en.wikipedia.org/wiki/Quantum_gate#Controlled_gates)
/// for more information.
///
/// # Panics
///
/// We panic if this supplied matrix isn't of size 2x2.
#[allow(unused)]
pub fn controlled(u: &Matrix) -> Gate {
    assert_eq!(2, u.size());

    let mut m = m_real![1, 0, 0, 0;
                        0, 1, 0, 0;
                        0, 0, 0, 0;
                        0, 0, 0, 0];

    m.embed(&u, 2, 2);

    Gate::new(2, m)
}

/// The two qubit controlled-X gate.
///
/// See [Wikipedia](https://en.wikipedia.org/wiki/Quantum_gate#Controlled_gates)
/// for more information.
#[allow(unused)]
pub fn controlled_x() -> Gate {
    controlled(pauli_x().matrix())
}

/// The two qubit controlled-Y gate.
///
/// See [Wikipedia](https://en.wikipedia.org/wiki/Quantum_gate#Controlled_gates)
/// for more information.
#[allow(unused)]
pub fn controlled_y() -> Gate {
    controlled(pauli_y().matrix())
}

/// The two qubit controlled-Z gate.
///
/// See [Wikipedia](https://en.wikipedia.org/wiki/Quantum_gate#Controlled_gates)
/// for more information.
#[allow(unused)]
pub fn controlled_z() -> Gate {
    controlled(pauli_z().matrix())
}

/// The three qubit Toffoli gate.
///
/// If the first two bits are in the state |1> , it applies a Pauli-X on the third bit,
/// else it does nothing.
///
/// See [Wikipedia](https://en.wikipedia.org/wiki/Quantum_gate#Toffoli_gate)
/// for more information.
#[allow(unused)]
pub fn toffoli() -> Gate {
    let mut m = Matrix::identity(8);

    let mut exchange = m_real![0, 1;
                               1, 0];

    m.embed(&exchange, 6, 6);

    Gate::new(3, m)
}

/// The three qubit Fredkin gate.
///
/// performs a controlled swap.
///
/// See [Wikipedia](https://en.wikipedia.org/wiki/Quantum_gate#Fredkin_gate)
/// for more information.
#[allow(unused)]
pub fn fredkin() -> Gate {
    let mut m = Matrix::identity(8);

    let mut exchange = m_real![0, 1;
                               1, 0];

    m.embed(&exchange, 5, 5);

    Gate::new(3, m)
}

/// The Quantum Fourier Transform on n qubits.
///
/// See [Wikipedia](https://en.wikipedia.org/wiki/Quantum_Fourier_transform)
/// for more information.
#[allow(unused)]
pub fn quantum_fourier_transform(n: usize) -> Gate {
    let d = Ket::size(n);
    let c = (d as f64).sqrt().recip();
    let r = Complex::nth_root_of_unity(d as u32);

    let mut m = Matrix::new(d);

    for i in 0..d {
        for j in 0..i + 1 {
            let v = c![c, 0f64] * r.pow((i * j) as u32);

            m.set(i, j, v);
            m.set(j, i, v);
        }
    }

    Gate::new(n, m)
}

/// Convenience macro for testing a quantum gate.
macro_rules! test_gate {
    ($computer:expr, $gate:expr, $from:expr, $to:expr) => {
        $computer.initialize($from);
        $computer.apply($gate);
        $computer.collapse();
        assert_eq!($to, $computer.value());
        $computer.reset();
    };
}

#[test]
fn identity_test() {
    use complex::Complex;

    let id_gate = identity(3);
    let mut ket = Ket::new(8);
    ket.elements[5] = c![99f64, 0f64];

    let expected = ket.clone();

    ket.apply(id_gate);

    assert_eq!(expected, ket);
}

#[test]
fn hadamard_test() {
    use float_cmp::ApproxEqUlps;

    use computer::QuantumComputer;

    let mut c = QuantumComputer::new(1);

    c.initialize(0);
    c.apply(hadamard(1));

    assert!(0.5f64.approx_eq_ulps(&c.probabilities()[0], 10));
    assert!(0.5f64.approx_eq_ulps(&c.probabilities()[1], 10));
}

#[test]
fn pauli_x_test() {
    use computer::QuantumComputer;

    let mut c = QuantumComputer::new(1);

    // |0> goes to |1>
    test_gate!(c, pauli_x(), 0, 1);

    // |1> goes to |0>
    test_gate!(c, pauli_x(), 1, 0);
}

#[test]
fn pauli_y_test() {
    use computer::QuantumComputer;

    let mut c = QuantumComputer::new(1);

    // |0> goes to i|1>
    test_gate!(c, pauli_y(), 0, 1);

    // |1> goes to -i|0>
    test_gate!(c, pauli_y(), 1, 0);
}

#[test]
fn pauli_z_test() {
    use computer::QuantumComputer;

    let mut c = QuantumComputer::new(1);

    // |0> goes to |0>
    test_gate!(c, pauli_z(), 0, 0);

    // |1> goes to -|1>
    test_gate!(c, pauli_z(), 1, 1);
}

#[test]
fn phase_shift_test() {
    use computer::QuantumComputer;

    let phi = 0.3f64;
    let mut c = QuantumComputer::new(1);

    // |0> goes to |0>
    test_gate!(c, phase_shift(phi), 0, 0);

    // |1> goes to exp(i * phi)|1>
    test_gate!(c, phase_shift(phi), 1, 1);
}

#[test]
fn swap_test() {
    use computer::QuantumComputer;

    let mut c = QuantumComputer::new(2);

    // |00> goes to |00>
    test_gate!(c, swap(), 0, 0);

    // |01> goes to |10>
    test_gate!(c, swap(), 2, 1);
}

#[test]
fn sqrt_swap_test() {
    use computer::QuantumComputer;

    let mut c = QuantumComputer::new(2);

    // |00> goes to |00>
    test_gate!(c, sqrt_swap(), 0, 0);

    // |11> goes to |11>
    test_gate!(c, sqrt_swap(), 3, 3);
}

#[test]
fn controlled_not_test() {
    use computer::QuantumComputer;

    let mut c = QuantumComputer::new(2);

    // |00> goes to |00>
    test_gate!(c, controlled_not(), 0, 0);

    // |01> goes to |01>
    test_gate!(c, controlled_not(), 1, 1);

    // |10> goes to |11>
    test_gate!(c, controlled_not(), 2, 3);

    // |11> goes to |10>
    test_gate!(c, controlled_not(), 3, 2);
}

#[test]
fn controlled_test() {
    let g = controlled(&m_real![0, 1; 1, 0]);

    assert_eq!(controlled_not(), g);
}

#[test]
fn toffoli_test() {
    use computer::QuantumComputer;

    let mut c = QuantumComputer::new(3);

    // |000> goes to |000>
    test_gate!(c, toffoli(), 0, 0);

    // |010> goes to |010>
    test_gate!(c, toffoli(), 2, 2);

    // |011> goes to |111>
    test_gate!(c, toffoli(), 6, 7);

    // |111> goes to |011>
    test_gate!(c, toffoli(), 7, 6);
}

#[test]
fn fredkin_test() {
    use computer::QuantumComputer;

    let mut c = QuantumComputer::new(3);

    // |000> goes to |000>
    test_gate!(c, fredkin(), 0, 0);

    // |101> goes to |011>
    test_gate!(c, fredkin(), 5, 6);

    // |011> goes to |101>
    test_gate!(c, fredkin(), 6, 5);

    // |111> goes to |111>
    test_gate!(c, fredkin(), 7, 7);
}

#[test]
fn quantum_fourier_transform_test() {
    let qft = quantum_fourier_transform(2);

    // qft(2) = (1/2) * |1  1  1  1|
    //                  |1  i -1 -i|
    //                  |1 -1  1 -1|
    //                  |1 -i -1  i|
    //
    assert!(c![0.5f64, 0.0f64].approx_eq(&qft.matrix().get(3, 0)));
    assert!(c![0.0f64, -0.5f64].approx_eq(&qft.matrix().get(3, 1)));
    assert!(c![-0.5f64, 0.0f64].approx_eq(&qft.matrix().get(3, 2)));
    assert!(c![0.0f64, 0.5f64].approx_eq(&qft.matrix().get(3, 3)));
}

#[test]
fn permutation_test() {
    use computer::QuantumComputer;

    let mut c = QuantumComputer::new(2);

    let flipped_controlled_not = controlled_not().permute(vec![2, 3, 0, 1]);

    // |01> goes to |11>
    test_gate!(c, flipped_controlled_not, 1, 3);
}