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
use num_complex::Complex32;
use std::f32::consts::FRAC_1_SQRT_2;
use std::fmt;

/// Representation of a gate
///
/// ```
///# extern crate qcgpu;
///# extern crate num_complex;
///# use qcgpu::Gate;
///# use num_complex::Complex32;
/// Gate {
///    a: Complex32::new(0.0, 0.0), b: Complex32::new(1.0, 0.0),
///    c: Complex32::new(1.0, 0.0), d: Complex32::new(0.0, 0.0)
/// };
///
///
#[derive(Debug, Clone, Copy)]
pub struct Gate {
    pub a: Complex32,
    pub b: Complex32,
    pub c: Complex32,
    pub d: Complex32,
}

impl fmt::Display for Gate {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "[[{}, {}], [{}, {}]]", self.a, self.b, self.c, self.d)
    }
}

/// Hadamard Gate
///
/// [0.70710678118, 0.70710678118]
///
/// [0.70710678118, -0.70710678118]
#[inline]
pub fn h() -> Gate {
    Gate {
        a: Complex32::new(FRAC_1_SQRT_2, 0.0),
        b: Complex32::new(FRAC_1_SQRT_2, 0.0),
        c: Complex32::new(FRAC_1_SQRT_2, 0.0),
        d: Complex32::new(-FRAC_1_SQRT_2, 0.0),
    }
}

/// Pauli X / NOT Gate
///
/// [0, 1]
///
/// [1, 0]
#[inline]
pub fn x() -> Gate {
    Gate {
        a: Complex32::new(0.0, 0.0),
        b: Complex32::new(1.0, 0.0),
        c: Complex32::new(1.0, 0.0),
        d: Complex32::new(0.0, 0.0),
    }
}

/// Pauli Y Gate
///
/// [0, -i]
///
/// [i, 0]
#[inline]
pub fn y() -> Gate {
    Gate {
        a: Complex32::new(0.0, 0.0),
        b: Complex32::new(0.0, -1.0),
        c: Complex32::new(0.0, 1.0),
        d: Complex32::new(0.0, 0.0),
    }
}

/// Pauli Z Gate
///
/// [1, 0]
///
/// [0, -1]
#[inline]
pub fn z() -> Gate {
    Gate {
        a: Complex32::new(1.0, 0.0),
        b: Complex32::new(0.0, 0.0),
        c: Complex32::new(0.0, 0.0),
        d: Complex32::new(-1.0, 0.0),
    }
}