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
#![allow(missing_docs)]

//! Gates and Gate Generation
//!
//! Matrices are in row major format, and
//! all gates use the `num_complex::Complex<f32>;`
//! datatype.

use num_complex::Complex32;
use std::fmt;
use std::f32::consts::{FRAC_1_SQRT_2, E};

/// 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)
    }
}

/// Identity gate
///
/// [1, 0]
///
/// [0, 1]
#[inline]
pub fn id() -> 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),
    }
}

/// 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),
    }
}

/// Negative Hadamard Gate
///
/// [-0.70710678118, -0.70710678118]
///
/// [-0.70710678118, 0.70710678118]
#[inline]
pub fn negh() -> 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),
    }
}

/// S / Phase Gate
///
/// [1, 0]
///
/// [0, i]
#[inline]
pub fn s() -> 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(0.0, 1.0),
    }
}

/// T Gate
///
/// [1, 0]
///
/// [0,(1/sqrt(2)) * 1 + 1i]
#[inline]
pub fn t() -> 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(
            0.707_106_781_186_547_524_400_844_362_104_849_039_3,
            0.707_106_781_186_547_524_400_844_362_104_849_039_3,
        ),
    }
}

/// The Phase Shift / R gate.
/// Note that this function has an argument of `angle`, which corresponds to the
/// rotation angle of the gate.
///
/// [1 ,0]
///
/// [0, e^i*angle]
pub fn r(angle: f32) -> 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(E, 0.0).powc(Complex32::new(0.0, angle)),
    }
}