diff options
Diffstat (limited to 'libpsi-core/src/core/gates.rs')
| -rw-r--r-- | libpsi-core/src/core/gates.rs | 126 |
1 files changed, 125 insertions, 1 deletions
diff --git a/libpsi-core/src/core/gates.rs b/libpsi-core/src/core/gates.rs index 4f922a8..cee4ebc 100644 --- a/libpsi-core/src/core/gates.rs +++ b/libpsi-core/src/core/gates.rs @@ -1,4 +1,100 @@ -use crate::{complex, matrix, QuantumGate}; +use crate::{complex, matrix, Complex, Matrix, QuantumGate}; +use std::f64::consts::FRAC_1_SQRT_2; + +pub fn rx_matrix(theta: f64) -> Matrix<Complex<f64>> { + let cos = (theta / 2.0).cos(); + let sin = (theta / 2.0).sin(); + matrix!( + [complex!(cos, 0.0), complex!(0.0, -sin)]; + [complex!(0.0, -sin), complex!(cos, 0.0)] + ) +} + +pub fn ry_matrix(theta: f64) -> Matrix<Complex<f64>> { + let cos = (theta / 2.0).cos(); + let sin = (theta / 2.0).sin(); + matrix!( + [complex!(cos, 0.0), complex!(-sin, 0.0)]; + [complex!(sin, 0.0), complex!(cos, 0.0)] + ) +} + +pub fn rz_matrix(theta: f64) -> Matrix<Complex<f64>> { + let half = theta / 2.0; + matrix!( + [complex!(half.cos(), -half.sin()), complex!(0.0, 0.0)]; + [complex!(0.0, 0.0), complex!(half.cos(), half.sin())] + ) +} + +pub fn p_matrix(theta: f64) -> Matrix<Complex<f64>> { + matrix!( + [complex!(1.0, 0.0), complex!(0.0, 0.0)]; + [complex!(0.0, 0.0), complex!(theta.cos(), theta.sin())] + ) +} + +pub fn u1_matrix(lambda: f64) -> Matrix<Complex<f64>> { + p_matrix(lambda) +} + +pub fn u2_matrix(phi: f64, lambda: f64) -> Matrix<Complex<f64>> { + let inv_sqrt2 = FRAC_1_SQRT_2; + matrix!( + [complex!(inv_sqrt2, 0.0), complex!(-inv_sqrt2 * lambda.cos(), -inv_sqrt2 * lambda.sin())]; + [complex!(inv_sqrt2 * phi.cos(), inv_sqrt2 * phi.sin()), complex!((phi + lambda).cos() * inv_sqrt2, (phi + lambda).sin() * inv_sqrt2)] + ) +} + +pub fn u3_matrix(theta: f64, phi: f64, lambda: f64) -> Matrix<Complex<f64>> { + let cos = (theta / 2.0).cos(); + let sin = (theta / 2.0).sin(); + matrix!( + [complex!(cos, 0.0), complex!(-sin * lambda.cos(), -sin * lambda.sin())]; + [complex!(sin * phi.cos(), sin * phi.sin()), complex!(cos * (phi + lambda).cos(), cos * (phi + lambda).sin())] + ) +} + +pub fn crx_matrix(theta: f64) -> Matrix<Complex<f64>> { + let cos = (theta / 2.0).cos(); + let sin = (theta / 2.0).sin(); + matrix!( + [complex!(1.0, 0.0), complex!(0.0, 0.0), complex!(0.0, 0.0), complex!(0.0, 0.0)]; + [complex!(0.0, 0.0), complex!(1.0, 0.0), complex!(0.0, 0.0), complex!(0.0, 0.0)]; + [complex!(0.0, 0.0), complex!(0.0, 0.0), complex!(cos, 0.0), complex!(0.0, -sin)]; + [complex!(0.0, 0.0), complex!(0.0, 0.0), complex!(0.0, -sin), complex!(cos, 0.0)] + ) +} + +pub fn cry_matrix(theta: f64) -> Matrix<Complex<f64>> { + let cos = (theta / 2.0).cos(); + let sin = (theta / 2.0).sin(); + matrix!( + [complex!(1.0, 0.0), complex!(0.0, 0.0), complex!(0.0, 0.0), complex!(0.0, 0.0)]; + [complex!(0.0, 0.0), complex!(1.0, 0.0), complex!(0.0, 0.0), complex!(0.0, 0.0)]; + [complex!(0.0, 0.0), complex!(0.0, 0.0), complex!(cos, 0.0), complex!(-sin, 0.0)]; + [complex!(0.0, 0.0), complex!(0.0, 0.0), complex!(sin, 0.0), complex!(cos, 0.0)] + ) +} + +pub fn crz_matrix(theta: f64) -> Matrix<Complex<f64>> { + let half = theta / 2.0; + matrix!( + [complex!(1.0, 0.0), complex!(0.0, 0.0), complex!(0.0, 0.0), complex!(0.0, 0.0)]; + [complex!(0.0, 0.0), complex!(1.0, 0.0), complex!(0.0, 0.0), complex!(0.0, 0.0)]; + [complex!(0.0, 0.0), complex!(0.0, 0.0), complex!(half.cos(), -half.sin()), complex!(0.0, 0.0)]; + [complex!(0.0, 0.0), complex!(0.0, 0.0), complex!(0.0, 0.0), complex!(half.cos(), half.sin())] + ) +} + +pub fn cp_matrix(theta: f64) -> Matrix<Complex<f64>> { + matrix!( + [complex!(1.0, 0.0), complex!(0.0, 0.0), complex!(0.0, 0.0), complex!(0.0, 0.0)]; + [complex!(0.0, 0.0), complex!(1.0, 0.0), complex!(0.0, 0.0), complex!(0.0, 0.0)]; + [complex!(0.0, 0.0), complex!(0.0, 0.0), complex!(1.0, 0.0), complex!(0.0, 0.0)]; + [complex!(0.0, 0.0), complex!(0.0, 0.0), complex!(0.0, 0.0), complex!(theta.cos(), theta.sin())] + ) +} #[rustfmt::skip] lazy_static::lazy_static! { @@ -45,6 +141,34 @@ lazy_static::lazy_static! { num_qubits: 1, }; + pub static ref SDG_GATE: QuantumGate<'static> = QuantumGate { + name: "S†", + matrix: matrix!([complex!(1.0, 0.0), complex!(0.0, 0.0)]; + [complex!(0.0, 0.0), complex!(0.0, -1.0)]), + num_qubits: 1, + }; + + pub static ref TDG_GATE: QuantumGate<'static> = QuantumGate { + name: "T†", + matrix: matrix!([complex!(1.0, 0.0), complex!(0.0, 0.0)]; + [complex!(0.0, 0.0), complex!(core::f64::consts::FRAC_1_SQRT_2, -core::f64::consts::FRAC_1_SQRT_2)]), + num_qubits: 1, + }; + + pub static ref SX_GATE: QuantumGate<'static> = QuantumGate { + name: "√X", + matrix: matrix!([complex!(0.5, 0.5), complex!(0.5, -0.5)]; + [complex!(0.5, -0.5), complex!(0.5, 0.5)]), + num_qubits: 1, + }; + + pub static ref SXDG_GATE: QuantumGate<'static> = QuantumGate { + name: "√X†", + matrix: matrix!([complex!(0.5, -0.5), complex!(0.5, 0.5)]; + [complex!(0.5, 0.5), complex!(0.5, -0.5)]), + num_qubits: 1, + }; + pub static ref IDENTITY: QuantumGate<'static> = QuantumGate { name: "I", matrix: matrix!([complex!(1.0, 0.0), complex!(0.0, 0.0)]; |
