Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Rust representation for CHGate, CPhaseGate, CSGate, CSdgGate, CSXGate, CSwapGate #12639

Merged
merged 11 commits into from
Jul 1, 2024
149 changes: 112 additions & 37 deletions crates/circuit/src/gate_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,43 +53,6 @@ pub fn rz_gate(theta: f64) -> GateArray1Q {
[[(-ilam2).exp(), C_ZERO], [C_ZERO, ilam2.exp()]]
}

#[inline]
pub fn crx_gate(theta: f64) -> GateArray2Q {
let half_theta = theta / 2.;
let cos = c64(half_theta.cos(), 0.);
let isin = c64(0., half_theta.sin());
[
[C_ONE, C_ZERO, C_ZERO, C_ZERO],
[C_ZERO, cos, C_ZERO, -isin],
[C_ZERO, C_ZERO, C_ONE, C_ZERO],
[C_ZERO, -isin, C_ZERO, cos],
]
}

#[inline]
pub fn cry_gate(theta: f64) -> GateArray2Q {
let half_theta = theta / 2.;
let cos = c64(half_theta.cos(), 0.);
let sin = c64(half_theta.sin(), 0.);
[
[C_ONE, C_ZERO, C_ZERO, C_ZERO],
[C_ZERO, cos, C_ZERO, -sin],
[C_ZERO, C_ZERO, C_ONE, C_ZERO],
[C_ZERO, sin, C_ZERO, cos],
]
}

#[inline]
pub fn crz_gate(theta: f64) -> GateArray2Q {
let i_half_theta = c64(0., theta / 2.);
[
[C_ONE, C_ZERO, C_ZERO, C_ZERO],
[C_ZERO, (-i_half_theta).exp(), C_ZERO, C_ZERO],
[C_ZERO, C_ZERO, C_ONE, C_ZERO],
[C_ZERO, C_ZERO, C_ZERO, i_half_theta.exp()],
]
}

pub static H_GATE: GateArray1Q = [
[c64(FRAC_1_SQRT_2, 0.), c64(FRAC_1_SQRT_2, 0.)],
[c64(FRAC_1_SQRT_2, 0.), c64(-FRAC_1_SQRT_2, 0.)],
Expand Down Expand Up @@ -210,13 +173,115 @@ pub static TDG_GATE: GateArray1Q = [
[C_ZERO, c64(FRAC_1_SQRT_2, -FRAC_1_SQRT_2)],
];

pub static CH_GATE: GateArray2Q = [
[C_ONE, C_ZERO, C_ZERO, C_ZERO],
[
C_ZERO,
c64(FRAC_1_SQRT_2, 0.),
C_ZERO,
c64(FRAC_1_SQRT_2, 0.),
],
[C_ZERO, C_ZERO, C_ONE, C_ZERO],
[
C_ZERO,
c64(FRAC_1_SQRT_2, 0.),
C_ZERO,
c64(-FRAC_1_SQRT_2, 0.),
],
];

pub static CS_GATE: GateArray2Q = [
[C_ONE, C_ZERO, C_ZERO, C_ZERO],
[C_ZERO, C_ONE, C_ZERO, C_ZERO],
[C_ZERO, C_ZERO, C_ONE, C_ZERO],
[C_ZERO, C_ZERO, C_ZERO, IM],
];

pub static CSDG_GATE: GateArray2Q = [
[C_ONE, C_ZERO, C_ZERO, C_ZERO],
[C_ZERO, C_ONE, C_ZERO, C_ZERO],
[C_ZERO, C_ZERO, C_ONE, C_ZERO],
[C_ZERO, C_ZERO, C_ZERO, M_IM],
];

pub static CSX_GATE: GateArray2Q = [
[C_ONE, C_ZERO, C_ZERO, C_ZERO],
[C_ZERO, c64(0.5, 0.5), C_ZERO, c64(0.5, -0.5)],
[C_ZERO, C_ZERO, C_ONE, C_ZERO],
[C_ZERO, c64(0.5, -0.5), C_ZERO, c64(0.5, 0.5)],
];

pub static CSWAP_GATE: GateArray3Q = [
[
C_ONE, C_ZERO, C_ZERO, C_ZERO, C_ZERO, C_ZERO, C_ZERO, C_ZERO,
],
[
C_ZERO, C_ONE, C_ZERO, C_ZERO, C_ZERO, C_ZERO, C_ZERO, C_ZERO,
],
[
C_ZERO, C_ZERO, C_ONE, C_ZERO, C_ZERO, C_ZERO, C_ZERO, C_ZERO,
],
[
C_ZERO, C_ZERO, C_ZERO, C_ZERO, C_ZERO, C_ONE, C_ZERO, C_ZERO,
],
[
C_ZERO, C_ZERO, C_ZERO, C_ZERO, C_ONE, C_ZERO, C_ZERO, C_ZERO,
],
[
C_ZERO, C_ZERO, C_ZERO, C_ONE, C_ZERO, C_ZERO, C_ZERO, C_ZERO,
],
[
C_ZERO, C_ZERO, C_ZERO, C_ZERO, C_ZERO, C_ZERO, C_ONE, C_ZERO,
],
[
C_ZERO, C_ZERO, C_ZERO, C_ZERO, C_ZERO, C_ZERO, C_ZERO, C_ONE,
],
];

pub static DCX_GATE: GateArray2Q = [
[C_ONE, C_ZERO, C_ZERO, C_ZERO],
[C_ZERO, C_ZERO, C_ZERO, C_ONE],
[C_ZERO, C_ONE, C_ZERO, C_ZERO],
[C_ZERO, C_ZERO, C_ONE, C_ZERO],
];

#[inline]
pub fn crx_gate(theta: f64) -> GateArray2Q {
let half_theta = theta / 2.;
let cos = c64(half_theta.cos(), 0.);
let isin = c64(0., half_theta.sin());
[
[C_ONE, C_ZERO, C_ZERO, C_ZERO],
[C_ZERO, cos, C_ZERO, -isin],
[C_ZERO, C_ZERO, C_ONE, C_ZERO],
[C_ZERO, -isin, C_ZERO, cos],
]
}

#[inline]
pub fn cry_gate(theta: f64) -> GateArray2Q {
let half_theta = theta / 2.;
let cos = c64(half_theta.cos(), 0.);
let sin = c64(half_theta.sin(), 0.);
[
[C_ONE, C_ZERO, C_ZERO, C_ZERO],
[C_ZERO, cos, C_ZERO, -sin],
[C_ZERO, C_ZERO, C_ONE, C_ZERO],
[C_ZERO, sin, C_ZERO, cos],
]
}

#[inline]
pub fn crz_gate(theta: f64) -> GateArray2Q {
let i_half_theta = c64(0., theta / 2.);
[
[C_ONE, C_ZERO, C_ZERO, C_ZERO],
[C_ZERO, (-i_half_theta).exp(), C_ZERO, C_ZERO],
[C_ZERO, C_ZERO, C_ONE, C_ZERO],
[C_ZERO, C_ZERO, C_ZERO, i_half_theta.exp()],
]
}

#[inline]
pub fn global_phase_gate(theta: f64) -> GateArray0Q {
[[c64(0., theta).exp()]]
Expand Down Expand Up @@ -309,3 +374,13 @@ pub fn xx_plus_yy_gate(theta: f64, beta: f64) -> GateArray2Q {
[C_ZERO, C_ZERO, C_ZERO, C_ONE],
]
}

#[inline]
pub fn cp_gate(lam: f64) -> GateArray2Q {
[
[C_ONE, C_ZERO, C_ZERO, C_ZERO],
[C_ZERO, C_ONE, C_ZERO, C_ZERO],
[C_ZERO, C_ZERO, C_ONE, C_ZERO],
[C_ZERO, C_ZERO, C_ZERO, c64(0., lam).exp()],
]
}
Loading
Loading