#![allow(
clippy::pedantic,
clippy::unnecessary_wraps,
clippy::field_reassign_with_default,
clippy::module_inception
)]
use quantrs2_circuit::builder::{Circuit, Simulator};
use quantrs2_sim::stabilizer::{StabilizerGate, StabilizerSimulator};
use quantrs2_sim::statevector::StateVectorSimulator;
use scirs2_core::random::prelude::*;
use scirs2_core::random::rngs::StdRng;
use std::f64::consts::PI;
#[test]
fn test_15q_h_layer_superposition() {
const N: usize = 15;
let mut circuit = Circuit::<N>::new();
for q in 0..N {
circuit.h(q).expect("H gate failed");
}
let sim = StateVectorSimulator::new();
let register = sim.run(&circuit).expect("simulation failed");
let amps = register.amplitudes();
let dim = 1usize << N;
let expected_prob = 1.0 / dim as f64;
let expected_amp = (expected_prob).sqrt();
for (i, amp) in amps.iter().enumerate() {
let re_diff = (amp.re - expected_amp).abs();
let im_diff = amp.im.abs();
assert!(
re_diff < 1e-9,
"amplitude[{i}] real part {:.2e} differs from expected {:.2e} by {re_diff:.2e}",
amp.re,
expected_amp,
);
assert!(
im_diff < 1e-9,
"amplitude[{i}] imaginary part {:.2e} should be 0, got {im_diff:.2e}",
amp.im,
);
}
let prob_zero: f64 = amps[0].norm_sqr();
assert!(
(prob_zero - expected_prob).abs() < 1e-9,
"P(|0...0⟩) = {prob_zero:.2e}, expected {expected_prob:.2e} (diff {:.2e})",
(prob_zero - expected_prob).abs()
);
}
#[test]
#[ignore] fn test_18q_h_layer_norm_preservation() {
const N: usize = 18;
let mut circuit = Circuit::<N>::new();
for q in 0..N {
circuit.h(q).expect("H gate failed");
}
let sim = StateVectorSimulator::new();
let register = sim.run(&circuit).expect("simulation failed");
let amps = register.amplitudes();
let dim = 1usize << N; let expected_prob = 1.0 / dim as f64;
let total_norm: f64 = amps.iter().map(|a| a.norm_sqr()).sum();
assert!(
(total_norm - 1.0).abs() < 1e-6,
"Total norm {total_norm:.8} is not 1.0 (diff {:.2e})",
(total_norm - 1.0).abs()
);
let step = dim / 100;
for k in (0..dim).step_by(step) {
let p = amps[k].norm_sqr();
assert!(
(p - expected_prob).abs() < 1e-9,
"P(|{k}⟩) = {p:.2e}, expected {expected_prob:.2e}"
);
}
}
#[test]
fn test_20q_stabilizer_bell_chain() {
const NUM_QUBITS: usize = 20;
let num_pairs = NUM_QUBITS / 2;
let mut sim = StabilizerSimulator::new(NUM_QUBITS);
for k in 0..num_pairs {
sim.apply_gate(StabilizerGate::H(2 * k))
.expect("H gate failed");
sim.apply_gate(StabilizerGate::CNOT(2 * k, 2 * k + 1))
.expect("CNOT gate failed");
}
let stabilizers = sim.get_stabilizers();
assert_eq!(
stabilizers.len(),
NUM_QUBITS,
"expected {NUM_QUBITS} stabilizer generators, got {}",
stabilizers.len()
);
for (i, s) in stabilizers.iter().enumerate() {
assert!(!s.is_empty(), "stabilizer generator {i} is an empty string");
}
}
#[test]
#[ignore] fn test_12q_random_circuit_norm() {
const N: usize = 12;
const DEPTH: usize = 5;
let mut circuit = Circuit::<N>::new();
let mut rng = StdRng::seed_from_u64(42);
for layer in 0..DEPTH {
if layer % 2 == 0 {
for q in 0..N {
circuit.h(q).expect("H gate failed");
}
} else {
for pair_start in (0..N - 1).step_by(2) {
let control = pair_start;
let target = pair_start + 1;
circuit.cnot(control, target).expect("CNOT gate failed");
}
}
for _ in 0..N / 2 {
let q = rng.random_range(0..N);
let angle = rng.random_range(0.0..2.0 * PI);
circuit.rz(q, angle).expect("RZ gate failed");
}
}
let sim = StateVectorSimulator::new();
let register = sim.run(&circuit).expect("simulation failed");
let amps = register.amplitudes();
let total_norm: f64 = amps.iter().map(|a| a.norm_sqr()).sum();
assert!(
(total_norm - 1.0).abs() < 1e-9,
"Norm after depth-{DEPTH} random circuit on {N} qubits: {total_norm:.12} (diff {:.2e})",
(total_norm - 1.0).abs()
);
let nonzero_count = amps.iter().filter(|a| a.norm_sqr() > 1e-12).count();
assert!(
nonzero_count > 1,
"Expected a superposition state, but only {nonzero_count} basis state(s) have non-zero amplitude"
);
}
#[test]
fn test_15q_ghz_state_probability() {
const N: usize = 15;
let mut circuit = Circuit::<N>::new();
circuit.h(0).expect("H gate failed");
for k in 1..N {
circuit.cnot(0, k).expect("CNOT gate failed");
}
let sim = StateVectorSimulator::new();
let register = sim.run(&circuit).expect("simulation failed");
let probs = register.probabilities();
let dim = 1usize << N;
let p_zero = probs[0];
assert!(
(p_zero - 0.5).abs() < 1e-9,
"P(|0...0⟩) = {p_zero:.12}, expected 0.5 (diff {:.2e})",
(p_zero - 0.5).abs()
);
let p_all_ones = probs[dim - 1];
assert!(
(p_all_ones - 0.5).abs() < 1e-9,
"P(|1...1⟩) = {p_all_ones:.12}, expected 0.5 (diff {:.2e})",
(p_all_ones - 0.5).abs()
);
for (i, &p) in probs.iter().enumerate() {
if i == 0 || i == dim - 1 {
continue;
}
assert!(p < 1e-9, "P(|{i}⟩) = {p:.2e} in GHZ state — expected 0");
}
}