Skip to content

Commit

Permalink
Use consts ONE and ZERO in gate_matrix.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
jlapeyre committed May 9, 2024
1 parent 5e966b9 commit 6a9de79
Showing 1 changed file with 42 additions and 206 deletions.
248 changes: 42 additions & 206 deletions crates/circuit/src/gate_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
use num_complex::Complex64;
use std::f64::consts::FRAC_1_SQRT_2;

pub static ONE_QUBIT_IDENTITY: [[Complex64; 2]; 2] = [
[Complex64::new(1., 0.), Complex64::new(0., 0.)],
[Complex64::new(0., 0.), Complex64::new(1., 0.)],
];
const ZERO: Complex64 = Complex64::new(0., 0.);
const ONE: Complex64 = Complex64::new(1., 0.);

pub static ONE_QUBIT_IDENTITY: [[Complex64; 2]; 2] = [[ONE, ZERO], [ZERO, ONE]];

pub fn rx_gate(theta: f64) -> [[Complex64; 2]; 2] {
let half_theta = theta / 2.;
Expand All @@ -42,10 +42,7 @@ pub fn ry_gate(theta: f64) -> [[Complex64; 2]; 2] {

pub fn rz_gate(theta: f64) -> [[Complex64; 2]; 2] {
let ilam2 = Complex64::new(0., 0.5 * theta);
[
[(-ilam2).exp(), Complex64::new(0., 0.)],
[Complex64::new(0., 0.), ilam2.exp()],
]
[[(-ilam2).exp(), ZERO], [ZERO, ilam2.exp()]]
}

pub static HGATE: [[Complex64; 2]; 2] = [
Expand All @@ -60,250 +57,89 @@ pub static HGATE: [[Complex64; 2]; 2] = [
];

pub static CXGATE: [[Complex64; 4]; 4] = [
[
Complex64::new(1., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
],
[
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(1., 0.),
],
[
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(1., 0.),
Complex64::new(0., 0.),
],
[
Complex64::new(0., 0.),
Complex64::new(1., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
],
[ONE, ZERO, ZERO, ZERO],
[ZERO, ZERO, ZERO, ONE],
[ZERO, ZERO, ONE, ZERO],
[ZERO, ONE, ZERO, ZERO],
];

pub static SXGATE: [[Complex64; 2]; 2] = [
[Complex64::new(0.5, 0.5), Complex64::new(0.5, -0.5)],
[Complex64::new(0.5, -0.5), Complex64::new(0.5, 0.5)],
];

pub static XGATE: [[Complex64; 2]; 2] = [
[Complex64::new(0., 0.), Complex64::new(1., 0.)],
[Complex64::new(1., 0.), Complex64::new(0., 0.)],
];
pub static XGATE: [[Complex64; 2]; 2] = [[ZERO, ONE], [ONE, ZERO]];

pub static ZGATE: [[Complex64; 2]; 2] = [
[Complex64::new(1., 0.), Complex64::new(0., 0.)],
[Complex64::new(0., 0.), Complex64::new(-1., 0.)],
];
pub static ZGATE: [[Complex64; 2]; 2] = [[ONE, ZERO], [ZERO, Complex64::new(-1., 0.)]];

pub static YGATE: [[Complex64; 2]; 2] = [
[Complex64::new(0., -1.), Complex64::new(0., 0.)],
[Complex64::new(0., 1.), Complex64::new(0., 0.)],
[Complex64::new(0., -1.), ZERO],
[Complex64::new(0., 1.), ZERO],
];

pub static CZGATE: [[Complex64; 4]; 4] = [
[
Complex64::new(1., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
],
[
Complex64::new(0., 0.),
Complex64::new(1., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
],
[
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(1., 0.),
Complex64::new(0., 0.),
],
[
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(-1., 0.),
],
[ONE, ZERO, ZERO, ZERO],
[ZERO, ONE, ZERO, ZERO],
[ZERO, ZERO, ONE, ZERO],
[ZERO, ZERO, ZERO, Complex64::new(-1., 0.)],
];

pub static CYGATE: [[Complex64; 4]; 4] = [
[
Complex64::new(1., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
],
[
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., -1.),
],
[
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(1., 0.),
Complex64::new(0., 0.),
],
[
Complex64::new(0., 0.),
Complex64::new(0., 1.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
],
[ONE, ZERO, ZERO, ZERO],
[ZERO, ZERO, ZERO, Complex64::new(0., -1.)],
[ZERO, ZERO, ONE, ZERO],
[ZERO, Complex64::new(0., 1.), ZERO, ZERO],
];

pub static CCXGATE: [[Complex64; 8]; 8] = [
[
Complex64::new(1., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
],
[
Complex64::new(0., 0.),
Complex64::new(1., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
],
[
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(1., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
],
[
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(1., 0.),
],
[
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(1., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
],
[
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(1., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
],
[
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(1., 0.),
Complex64::new(0., 0.),
],
[
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(1., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
],
[ONE, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO],
[ZERO, ONE, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO],
[ZERO, ZERO, ONE, ZERO, ZERO, ZERO, ZERO, ZERO],
[ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ONE],
[ZERO, ZERO, ZERO, ZERO, ONE, ZERO, ZERO, ZERO],
[ZERO, ZERO, ZERO, ZERO, ZERO, ONE, ZERO, ZERO],
[ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ONE, ZERO],
[ZERO, ZERO, ZERO, ONE, ZERO, ZERO, ZERO, ZERO],
];

pub static ECRGATE: [[Complex64; 4]; 4] = [
[
Complex64::new(0., 0.),
ZERO,
Complex64::new(FRAC_1_SQRT_2, 0.),
Complex64::new(0., 0.),
ZERO,
Complex64::new(0., FRAC_1_SQRT_2),
],
[
Complex64::new(FRAC_1_SQRT_2, 0.),
Complex64::new(0., 0.),
ZERO,
Complex64::new(0., -FRAC_1_SQRT_2),
Complex64::new(0., 0.),
ZERO,
],
[
Complex64::new(0., 0.),
ZERO,
Complex64::new(0., FRAC_1_SQRT_2),
Complex64::new(0., 0.),
ZERO,
Complex64::new(FRAC_1_SQRT_2, 0.),
],
[
Complex64::new(0., -FRAC_1_SQRT_2),
Complex64::new(0., 0.),
ZERO,
Complex64::new(FRAC_1_SQRT_2, 0.),
Complex64::new(0., 0.),
ZERO,
],
];

pub static SWAPGATE: [[Complex64; 4]; 4] = [
[
Complex64::new(1., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
],
[
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(1., 0.),
Complex64::new(0., 0.),
],
[
Complex64::new(0., 0.),
Complex64::new(1., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
],
[
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(0., 0.),
Complex64::new(1., 0.),
],
[ONE, ZERO, ZERO, ZERO],
[ZERO, ZERO, ONE, ZERO],
[ZERO, ONE, ZERO, ZERO],
[ZERO, ZERO, ZERO, ONE],
];

pub fn global_phase_gate(theta: f64) -> [[Complex64; 1]; 1] {
[[Complex64::new(0., theta).exp()]]
}

pub fn phase_gate(lam: f64) -> [[Complex64; 2]; 2] {
[
[Complex64::new(1., 0.), Complex64::new(0., 0.)],
[Complex64::new(0., 0.), Complex64::new(0., lam).exp()],
]
[[ONE, ZERO], [ZERO, Complex64::new(0., lam).exp()]]
}

0 comments on commit 6a9de79

Please sign in to comment.