use crate::expr::Expression;
#[must_use]
pub fn ket_0() -> Expression {
Expression::new("Matrix([[1], [0]])")
}
#[must_use]
pub fn ket_1() -> Expression {
Expression::new("Matrix([[0], [1]])")
}
#[must_use]
pub fn ket_plus() -> Expression {
Expression::new("1/sqrt(2) * Matrix([[1], [1]])")
}
#[must_use]
pub fn ket_minus() -> Expression {
Expression::new("1/sqrt(2) * Matrix([[1], [-1]])")
}
#[must_use]
pub fn ket_i() -> Expression {
Expression::new("1/sqrt(2) * Matrix([[1], [I]])")
}
#[must_use]
pub fn ket_minus_i() -> Expression {
Expression::new("1/sqrt(2) * Matrix([[1], [-I]])")
}
#[must_use]
pub fn bell_phi_plus() -> Expression {
Expression::new("1/sqrt(2) * Matrix([[1], [0], [0], [1]])")
}
#[must_use]
pub fn bell_phi_minus() -> Expression {
Expression::new("1/sqrt(2) * Matrix([[1], [0], [0], [-1]])")
}
#[must_use]
pub fn bell_psi_plus() -> Expression {
Expression::new("1/sqrt(2) * Matrix([[0], [1], [1], [0]])")
}
#[must_use]
pub fn bell_psi_minus() -> Expression {
Expression::new("1/sqrt(2) * Matrix([[0], [1], [-1], [0]])")
}
#[must_use]
pub fn bloch_state(theta: &Expression, phi: &Expression) -> Expression {
Expression::new(format!(
"Matrix([[cos({theta}/2)], [exp(I*{phi})*sin({theta}/2)]])"
))
}
#[must_use]
pub fn ghz_state(n: usize) -> Expression {
if n == 0 {
return Expression::one();
}
let dim = 1 << n; let mut elements = vec!["0".to_string(); dim];
elements[0] = "1".to_string();
elements[dim - 1] = "1".to_string();
let matrix_str = elements
.iter()
.map(|e| format!("[{e}]"))
.collect::<Vec<_>>()
.join(", ");
Expression::new(format!("1/sqrt(2) * Matrix([{matrix_str}])"))
}
#[must_use]
pub fn w_state(n: usize) -> Expression {
if n == 0 {
return Expression::one();
}
let dim = 1 << n; let mut elements = vec!["0".to_string(); dim];
for i in 0..n {
let idx = 1 << (n - 1 - i); elements[idx] = "1".to_string();
}
let matrix_str = elements
.iter()
.map(|e| format!("[{e}]"))
.collect::<Vec<_>>()
.join(", ");
Expression::new(format!("1/sqrt({n}) * Matrix([{matrix_str}])"))
}
#[must_use]
pub fn coherent_state(alpha: &Expression) -> Expression {
Expression::new(format!("CoherentState({alpha})"))
}
#[must_use]
pub fn squeezed_vacuum(zeta: &Expression) -> Expression {
Expression::new(format!("SqueezedVacuum({zeta})"))
}
#[must_use]
pub fn thermal_state(n_thermal: &Expression) -> Expression {
Expression::new(format!("ThermalState({n_thermal})"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_computational_basis() {
let ket0 = ket_0();
let ket1 = ket_1();
assert!(!ket0.to_string().is_empty());
assert!(!ket1.to_string().is_empty());
}
#[test]
fn test_bell_states() {
let phi_plus = bell_phi_plus();
let psi_minus = bell_psi_minus();
assert!(!phi_plus.to_string().is_empty());
assert!(!psi_minus.to_string().is_empty());
}
#[test]
fn test_ghz_state() {
let ghz3 = ghz_state(3);
assert!(ghz3.to_string().contains("Matrix"));
}
#[test]
fn test_w_state() {
let w3 = w_state(3);
assert!(w3.to_string().contains("Matrix"));
}
}