use num_complex::Complex64;
type Gate = [[Complex64; 2]; 2];
#[inline]
fn c(re: f64, im: f64) -> Complex64 {
Complex64::new(re, im)
}
pub fn id() -> Gate {
[[c(1.0, 0.0), c(0.0, 0.0)], [c(0.0, 0.0), c(1.0, 0.0)]]
}
pub fn x() -> Gate {
[[c(0.0, 0.0), c(1.0, 0.0)], [c(1.0, 0.0), c(0.0, 0.0)]]
}
pub fn y() -> Gate {
[[c(0.0, 0.0), c(0.0, -1.0)], [c(0.0, 1.0), c(0.0, 0.0)]]
}
pub fn z() -> Gate {
[[c(1.0, 0.0), c(0.0, 0.0)], [c(0.0, 0.0), c(-1.0, 0.0)]]
}
pub fn h() -> Gate {
let s = std::f64::consts::FRAC_1_SQRT_2;
[[c(s, 0.0), c(s, 0.0)], [c(s, 0.0), c(-s, 0.0)]]
}
pub fn s() -> Gate {
[[c(1.0, 0.0), c(0.0, 0.0)], [c(0.0, 0.0), c(0.0, 1.0)]]
}
pub fn t() -> Gate {
let phase = std::f64::consts::FRAC_PI_4;
[
[c(1.0, 0.0), c(0.0, 0.0)],
[c(0.0, 0.0), c(phase.cos(), phase.sin())],
]
}
pub fn sdg() -> Gate {
[[c(1.0, 0.0), c(0.0, 0.0)], [c(0.0, 0.0), c(0.0, -1.0)]]
}
pub fn tdg() -> Gate {
let phase = -std::f64::consts::FRAC_PI_4;
[
[c(1.0, 0.0), c(0.0, 0.0)],
[c(0.0, 0.0), c(phase.cos(), phase.sin())],
]
}
pub fn p(lambda: f64) -> Gate {
let (s, co) = lambda.sin_cos();
[[c(1.0, 0.0), c(0.0, 0.0)], [c(0.0, 0.0), c(co, s)]]
}
pub fn u3(theta: f64, phi: f64, lambda: f64) -> Gate {
let (st, ct) = (theta / 2.0).sin_cos();
let m00 = c(ct, 0.0);
let m01 = c(-lambda.cos() * st, -lambda.sin() * st);
let m10 = c(phi.cos() * st, phi.sin() * st);
let (spl, cpl) = (phi + lambda).sin_cos();
let m11 = c(cpl * ct, spl * ct);
[[m00, m01], [m10, m11]]
}
pub fn u2(phi: f64, lambda: f64) -> Gate {
u3(std::f64::consts::FRAC_PI_2, phi, lambda)
}
pub(crate) fn u3_decompose(g: &Gate) -> (f64, f64, f64, f64) {
const EPS: f64 = 1e-12;
let cos_half = g[0][0].norm();
let sin_half = g[1][0].norm();
let theta = 2.0 * sin_half.atan2(cos_half);
if cos_half < EPS {
(0.0, theta, g[1][0].arg(), (-g[0][1]).arg())
} else if sin_half < EPS {
let gamma = g[0][0].arg();
(gamma, theta, 0.0, g[1][1].arg() - gamma)
} else {
let gamma = g[0][0].arg();
(
gamma,
theta,
g[1][0].arg() - gamma,
(-g[0][1]).arg() - gamma,
)
}
}
pub fn sx() -> Gate {
[[c(0.5, 0.5), c(0.5, -0.5)], [c(0.5, -0.5), c(0.5, 0.5)]]
}
pub fn sxdg() -> Gate {
[[c(0.5, -0.5), c(0.5, 0.5)], [c(0.5, 0.5), c(0.5, -0.5)]]
}
pub fn rx(theta: f64) -> Gate {
let (s, co) = (theta / 2.0).sin_cos();
[[c(co, 0.0), c(0.0, -s)], [c(0.0, -s), c(co, 0.0)]]
}
pub fn ry(theta: f64) -> Gate {
let (s, co) = (theta / 2.0).sin_cos();
[[c(co, 0.0), c(-s, 0.0)], [c(s, 0.0), c(co, 0.0)]]
}
pub fn rz(theta: f64) -> Gate {
let (s, co) = (theta / 2.0).sin_cos();
[[c(co, -s), c(0.0, 0.0)], [c(0.0, 0.0), c(co, s)]]
}
#[cfg(test)]
mod tests {
use super::*;
use std::f64::consts::PI;
fn assert_gate_eq(a: &Gate, b: &Gate) {
for i in 0..2 {
for j in 0..2 {
assert!(
(a[i][j] - b[i][j]).norm() < 1e-12,
"entry ({i},{j}) differs: {:?} vs {:?}",
a[i][j],
b[i][j]
);
}
}
}
fn neg_i_times(g: &Gate) -> Gate {
let ni = c(0.0, -1.0);
[[ni * g[0][0], ni * g[0][1]], [ni * g[1][0], ni * g[1][1]]]
}
#[test]
fn rotations_at_zero_are_identity() {
let id = [[c(1.0, 0.0), c(0.0, 0.0)], [c(0.0, 0.0), c(1.0, 0.0)]];
assert_gate_eq(&rx(0.0), &id);
assert_gate_eq(&ry(0.0), &id);
assert_gate_eq(&rz(0.0), &id);
}
#[test]
fn rx_pi_is_x_up_to_global_phase() {
assert_gate_eq(&rx(PI), &neg_i_times(&x()));
}
#[test]
fn ry_pi_is_y_up_to_global_phase() {
assert_gate_eq(&ry(PI), &neg_i_times(&y()));
}
#[test]
fn rz_pi_is_z_up_to_global_phase() {
assert_gate_eq(&rz(PI), &neg_i_times(&z()));
}
#[test]
fn rotations_are_unitary() {
for g in [rx(0.7), ry(-1.3), rz(2.1)] {
let dot = |i: usize, j: usize| g[i][0] * g[j][0].conj() + g[i][1] * g[j][1].conj();
let prod = [[dot(0, 0), dot(0, 1)], [dot(1, 0), dot(1, 1)]];
let id = [[c(1.0, 0.0), c(0.0, 0.0)], [c(0.0, 0.0), c(1.0, 0.0)]];
assert_gate_eq(&prod, &id);
}
}
fn mul(a: &Gate, b: &Gate) -> Gate {
let e = |i: usize, j: usize| a[i][0] * b[0][j] + a[i][1] * b[1][j];
[[e(0, 0), e(0, 1)], [e(1, 0), e(1, 1)]]
}
#[test]
fn daggers_invert_their_gates() {
let id = [[c(1.0, 0.0), c(0.0, 0.0)], [c(0.0, 0.0), c(1.0, 0.0)]];
assert_gate_eq(&mul(&s(), &sdg()), &id);
assert_gate_eq(&mul(&t(), &tdg()), &id);
}
#[test]
fn phase_gate_specializes_to_z_s_t() {
assert_gate_eq(&p(PI), &z());
assert_gate_eq(&p(PI / 2.0), &s());
assert_gate_eq(&p(PI / 4.0), &t());
}
#[test]
fn u3_specializes_to_known_gates() {
let id = [[c(1.0, 0.0), c(0.0, 0.0)], [c(0.0, 0.0), c(1.0, 0.0)]];
assert_gate_eq(&u3(0.0, 0.0, 0.0), &id);
assert_gate_eq(&u3(PI, 0.0, PI), &x());
assert_gate_eq(&u3(0.0, 0.0, 0.9), &p(0.9));
}
#[test]
fn u2_zero_pi_is_hadamard() {
assert_gate_eq(&u2(0.0, PI), &h());
}
fn recompose(gamma: f64, theta: f64, phi: f64, lambda: f64) -> Gate {
let phase = c(gamma.cos(), gamma.sin());
let g = u3(theta, phi, lambda);
[
[phase * g[0][0], phase * g[0][1]],
[phase * g[1][0], phase * g[1][1]],
]
}
#[test]
fn u3_decompose_reconstructs_every_gate() {
let cases = [
x(),
y(),
z(),
h(),
s(),
t(),
sdg(),
tdg(),
rz(0.7),
p(1.3),
u3(0.5, -0.6, 0.7),
u3(PI, 0.4, -0.9), u3(0.0, 0.2, 1.1), [[c(1.0, 0.0), c(0.0, 0.0)], [c(0.0, 0.0), c(1.0, 0.0)]], ];
for g in cases {
let (gamma, theta, phi, lambda) = u3_decompose(&g);
assert_gate_eq(&recompose(gamma, theta, phi, lambda), &g);
}
}
#[test]
fn sx_squares_to_x_and_inverts() {
let id = [[c(1.0, 0.0), c(0.0, 0.0)], [c(0.0, 0.0), c(1.0, 0.0)]];
assert_gate_eq(&mul(&sx(), &sx()), &x());
assert_gate_eq(&mul(&sxdg(), &sx()), &id);
}
#[test]
fn u3_is_unitary() {
let g = u3(0.7, -1.1, 2.3);
let dot = |i: usize, j: usize| g[i][0] * g[j][0].conj() + g[i][1] * g[j][1].conj();
let prod = [[dot(0, 0), dot(0, 1)], [dot(1, 0), dot(1, 1)]];
let id = [[c(1.0, 0.0), c(0.0, 0.0)], [c(0.0, 0.0), c(1.0, 0.0)]];
assert_gate_eq(&prod, &id);
}
}