#![allow(missing_docs)]
use num_complex::Complex32;
use std::fmt;
use std::f32::consts::{FRAC_1_SQRT_2, E};
#[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)
}
}
#[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),
}
}
#[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),
}
}
#[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),
}
}
#[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),
}
}
#[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),
}
}
#[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),
}
}
#[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),
}
}
#[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,
),
}
}
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)),
}
}