| |
| |
| |
|
|
| #[cfg(test)] |
| mod tests { |
| use qataaum_algorithms::*; |
| use std::f64::consts::PI; |
|
|
| #[test] |
| fn test_h2_vqe_convergence() { |
| |
| let hamiltonian = hamiltonian::h2_hamiltonian(); |
| assert_eq!(hamiltonian.n_qubits, 2); |
| assert!(hamiltonian.n_terms() > 0); |
|
|
| |
| let circuit = vqe::ParametrizedCircuit::simple_ansatz(2, 2); |
| assert_eq!(circuit.n_qubits, 2); |
|
|
| |
| let optimizer = vqe::VQEOptimizer::new(); |
|
|
| |
| |
| assert!(optimizer.learning_rate > 0.0); |
| } |
|
|
| #[test] |
| fn test_maxcut_qaoa_small_graph() { |
| |
| let edges = vec![(0, 1), (1, 2), (0, 2)]; |
| let qaoa = qaoa::MaxCutQAOA::new(3, edges, 1); |
|
|
| assert!(qaoa.is_ok()); |
| let qaoa = qaoa.unwrap(); |
| assert_eq!(qaoa.edge_count(), 3); |
| assert_eq!(qaoa.max_cut(), 3); |
| } |
|
|
| #[test] |
| fn test_hamiltonian_simulation_trotter() { |
| |
| let hamiltonian = hamiltonian::h2_hamiltonian(); |
| let config = hamiltonian_sim::HamiltonianSimConfig::new(0.1, 5) |
| .with_second_order(); |
|
|
| let mut sim = hamiltonian_sim::TrotterSimulator::new(hamiltonian, config); |
| let gates = sim.simulate(); |
|
|
| assert!(gates.is_ok()); |
| let gate_seq = gates.unwrap(); |
| assert!(!gate_seq.is_empty()); |
|
|
| |
| let conservation = sim.energy_conservation(); |
| assert!(conservation > 0.99); |
| } |
|
|
| #[test] |
| fn test_amplitude_estimation_simple() { |
| |
| let register = amplitude_est::AmplitudeRegister::uniform_marked(2, 0.5); |
| assert!(register.is_ok()); |
|
|
| let mut estimator = amplitude_est::AmplitudeEstimator::new(5); |
| assert!(estimator.is_ok()); |
| } |
|
|
| #[test] |
| fn test_quantum_walk_mixing() { |
| |
| let walk = walks::CycleQuantumWalk::new(4); |
| assert!(walk.is_ok()); |
|
|
| let walk = walk.unwrap(); |
| let gap = walk.spectral_gap(); |
| assert!(gap > 0.0 && gap < 4.0); |
| } |
|
|
| #[test] |
| fn test_shor_factor_15() { |
| |
| let mut shor = shor::ShorFactoring::new(15); |
| assert!(shor.is_ok()); |
|
|
| let mut shor = shor.unwrap(); |
| let factors = shor.factor(); |
|
|
| assert!(factors.is_ok()); |
| let factors = factors.unwrap(); |
| assert!(!factors.is_empty()); |
| } |
|
|
| |
|
|
| #[test] |
| fn test_vqe_h2_pipeline() { |
| |
| let hamiltonian = hamiltonian::h2_hamiltonian(); |
| let (e_min, e_max) = hamiltonian.eigenvalue_bounds(); |
|
|
| |
| let ground_truth = vqe::molecules::h2_ground_state_energy(); |
| assert!(ground_truth >= e_min && ground_truth <= e_max); |
| } |
|
|
| #[test] |
| fn test_qaoa_approximation_ratio_scaling() { |
| |
| let ratio_p1 = qaoa::MaxCutQAOA::expected_approx_ratio(1); |
| let ratio_p2 = qaoa::MaxCutQAOA::expected_approx_ratio(2); |
| let ratio_p3 = qaoa::MaxCutQAOA::expected_approx_ratio(3); |
|
|
| assert!(ratio_p2 >= ratio_p1); |
| assert!(ratio_p3 >= ratio_p2); |
| assert!(ratio_p1 > 0.6 && ratio_p1 < 0.8); |
| } |
|
|
| #[test] |
| fn test_trotter_error_convergence() { |
| |
| let config1 = hamiltonian_sim::HamiltonianSimConfig::new(1.0, 5); |
| let config2 = hamiltonian_sim::HamiltonianSimConfig::new(1.0, 10); |
| let config4 = hamiltonian_sim::HamiltonianSimConfig::new(1.0, 20); |
|
|
| let err1 = config1.error_bound(); |
| let err2 = config2.error_bound(); |
| let err4 = config4.error_bound(); |
|
|
| assert!(err2 < err1); |
| assert!(err4 < err2); |
| } |
|
|
| #[test] |
| fn test_amplitude_grover_amplification() { |
| |
| let initial = 0.25; |
| let amplified = amplitude_est::AmplitudeEstimator::grover_amplification(initial, 1); |
|
|
| assert!(amplified.is_ok()); |
| let amplified = amplified.unwrap(); |
| assert!(amplified > initial); |
| } |
|
|
| #[test] |
| fn test_walks_line_probability_distribution() { |
| |
| let walk = walks::LineQuantumWalk::new(5); |
| let dist = walk.distribution(); |
|
|
| |
| let sum: f64 = dist.iter().sum(); |
| assert!((sum - 1.0).abs() < 1e-10); |
| } |
|
|
| #[test] |
| fn test_shor_modpow_correctness() { |
| |
| |
| let exp = shor::ModularExponentiation::new(2, 1000).unwrap(); |
| assert_eq!(exp.compute(10), 24); |
| } |
|
|
| |
|
|
| #[test] |
| fn test_pauli_hamiltonian_consistency() { |
| |
| let p1 = hamiltonian::PauliString::new(vec![hamiltonian::PauliOp::X]); |
| let p2 = hamiltonian::PauliString::new(vec![hamiltonian::PauliOp::X]); |
|
|
| let result = p1.multiply(&p2).unwrap(); |
| assert_eq!(result.ops[0], hamiltonian::PauliOp::I); |
| } |
|
|
| #[test] |
| fn test_vqe_optimizer_structure() { |
| |
| let opt = vqe::VQEOptimizer::new(); |
| assert!(opt.learning_rate > 0.0); |
| assert!(opt.max_iterations > 0); |
| assert!(opt.convergence_threshold > 0.0); |
| } |
|
|
| #[test] |
| fn test_qaoa_circuit_parameters() { |
| |
| let params = qaoa::QAOAParams::new(2); |
| assert_eq!(params.n_params(), 4); |
|
|
| let vec = params.to_vec(); |
| let params2 = qaoa::QAOAParams::from_vec(&vec).unwrap(); |
| assert_eq!(params2.p, 2); |
| } |
|
|
| #[test] |
| fn test_hamiltonian_simulation_config_scaling() { |
| |
| let steps_opt = hamiltonian_sim::HamiltonianSimConfig::optimal_steps(1.0, 1e-3); |
| assert!(steps_opt > 0); |
|
|
| let bound = hamiltonian_sim::HamiltonianSimConfig::new(1.0, steps_opt) |
| .error_bound(); |
| assert!(bound < 1e-2); |
| } |
|
|
| #[test] |
| fn test_amplitude_precision_scaling() { |
| |
| let shots = amplitude_est::AmplitudeEstimator::precision_scaling(0.5, 0.01); |
| assert!(shots.is_ok()); |
| assert!(shots.unwrap() > 0); |
| } |
|
|
| #[test] |
| fn test_walk_cycle_regularity() { |
| |
| let walk = walks::CycleQuantumWalk::new(6).unwrap(); |
| let gap = walk.spectral_gap(); |
| assert!(gap > 0.0); |
| } |
|
|
| #[test] |
| fn test_shor_success_rate() { |
| |
| let prob = shor::ShorFactoring::success_probability(); |
| assert!(prob > 0.4 && prob < 0.42); |
| } |
| } |
|
|
| |
|
|