#![allow(
clippy::pedantic,
clippy::unnecessary_wraps,
clippy::single_element_loop,
clippy::redundant_clone,
clippy::cloned_ref_to_slice_refs,
clippy::collection_is_never_read
)]
#![allow(unused_must_use, dead_code)]
use quantrs2_tytan::applications::finance::*;
use quantrs2_tytan::applications::logistics::*;
use quantrs2_tytan::coherent_ising_machine::*;
use quantrs2_tytan::performance_profiler::OutputFormat;
use quantrs2_tytan::performance_profiler::*;
use quantrs2_tytan::problem_decomposition::*;
use quantrs2_tytan::problem_dsl::*;
use quantrs2_tytan::sampler::Sampler;
use quantrs2_tytan::solution_debugger::*;
use quantrs2_tytan::testing_framework::*;
use quantrs2_tytan::*;
use scirs2_core::ndarray::distributions::Uniform;
use scirs2_core::ndarray::{array, Array, Array1, Array2};
use scirs2_core::RandomExt;
use std::collections::HashMap;
use std::time::Duration;
#[cfg(test)]
mod cim_tests {
use super::*;
#[test]
fn test_basic_cim() {
let mut cim = CIMSimulator::new(3)
.with_pump_parameter(1.5)
.with_evolution_time(5.0)
.with_seed(42);
let mut qubo = Array2::zeros((3, 3));
qubo[[0, 1]] = -1.0;
qubo[[1, 0]] = -1.0;
let mut var_map = HashMap::new();
for i in 0..3 {
var_map.insert(format!("x{i}"), i);
}
let results = cim.run_qubo(&(qubo, var_map), 10).unwrap();
assert!(!results.is_empty());
assert_eq!(results[0].assignments.len(), 3);
}
#[test]
fn test_advanced_cim_pulse_shaping() {
let mut cim = AdvancedCIM::new(4)
.with_pulse_shape(PulseShape::Gaussian {
width: 1.0,
amplitude: 2.0,
})
.with_num_rounds(2);
assert_eq!(cim.num_rounds, 2);
}
#[test]
fn test_cim_error_correction() {
let n = 4;
let mut check_matrix = Array2::from_elem((2, n), false);
check_matrix[[0, 0]] = true;
check_matrix[[0, 1]] = true;
check_matrix[[1, 2]] = true;
check_matrix[[1, 3]] = true;
let mut cim = AdvancedCIM::new(n)
.with_error_correction(ErrorCorrectionScheme::ParityCheck { check_matrix });
assert_eq!(cim.base_cim.n_spins, n);
}
#[test]
fn test_networked_cim() {
let mut net_cim = NetworkedCIM::new(4, 3, NetworkTopology::Ring);
assert_eq!(net_cim.modules.len(), 4);
let neighbors_0 = net_cim.get_neighbors(0);
assert_eq!(neighbors_0, vec![3, 1]);
let neighbors_2 = net_cim.get_neighbors(2);
assert_eq!(neighbors_2, vec![1, 3]);
}
#[test]
fn test_bifurcation_control() {
let mut cim = AdvancedCIM::new(3);
assert_eq!(cim.base_cim.n_spins, 3);
}
}
#[cfg(test)]
mod decomposition_tests {
use super::*;
use quantrs2_tytan::problem_decomposition::types::DecompositionStrategy;
#[test]
fn test_graph_partitioner() {
let size = 8;
let mut qubo = Array2::zeros((size, size));
for i in 0..size - 1 {
qubo[[i, i + 1]] = -1.0;
qubo[[i + 1, i]] = -1.0;
}
let mut partitioner = GraphPartitioner::new()
.with_num_partitions(2)
.with_algorithm(PartitioningAlgorithm::KernighanLin);
let partitions = partitioner.partition(&qubo).unwrap();
assert_eq!(partitions.len(), 2);
let total_vars: usize = partitions.iter().map(|p| p.variables.len()).sum();
assert_eq!(total_vars, size);
}
#[test]
fn test_hierarchical_solver() {
let size = 16;
let mut qubo = Array2::zeros((size, size));
for i in 0..size {
qubo[[i, i]] = -1.0;
if i < size - 1 {
qubo[[i, i + 1]] = -0.5;
}
}
let sampler = SASampler::new(None);
let _solver = HierarchicalSolver::new(sampler);
}
#[test]
fn test_domain_decomposer() {
let sampler = SASampler::new(None);
let _decomposer = DomainDecomposer::new(sampler);
}
#[test]
fn test_parallel_coordination() {
let subproblems: Vec<Array2<f64>> = vec![Array2::eye(3), Array2::eye(3), Array2::eye(3)];
assert_eq!(subproblems.len(), 3);
for subproblem in &subproblems {
assert_eq!(subproblem.shape(), &[3, 3]);
}
}
}
#[cfg(test)]
mod debugger_tests {
use super::*;
#[test]
fn test_solution_debugger() {
let problem_info = create_test_problem_info();
let config = DebuggerConfig {
detailed_analysis: true,
check_constraints: true,
analyze_energy: true,
compare_solutions: false,
generate_visuals: false,
output_format: DebugOutputFormat::Console,
verbosity: quantrs2_tytan::solution_debugger::VerbosityLevel::Normal,
};
let mut debugger = SolutionDebugger::new(problem_info, config);
let solution = Solution {
assignments: {
let mut map = HashMap::new();
map.insert("x".to_string(), true);
map.insert("y".to_string(), false);
map.insert("z".to_string(), true);
map
},
energy: -2.0,
quality_metrics: HashMap::new(),
metadata: {
let mut map = HashMap::new();
map.insert("solver".to_string(), "Test".to_string());
map
},
sampling_stats: None,
};
let report = debugger.debug_solution(&solution);
assert!(report.energy_analysis.is_some());
assert!(report.constraint_analysis.is_some());
assert_eq!(report.solution.energy, -2.0);
}
#[test]
fn test_interactive_debugger() {
let problem_info = create_test_problem_info();
let mut debugger = InteractiveDebugger::new(problem_info);
let solution = Solution {
assignments: {
let mut map = HashMap::new();
map.insert("x".to_string(), true);
map.insert("y".to_string(), true);
map.insert("z".to_string(), false);
map
},
energy: -1.0,
quality_metrics: HashMap::new(),
metadata: {
let mut map = HashMap::new();
map.insert("solver".to_string(), "Test".to_string());
map
},
sampling_stats: None,
};
debugger.load_solution(solution);
assert!(debugger.current_solution.is_some());
let output = debugger.execute_command("help");
assert!(output.contains("Available commands"));
debugger.add_watch("x".to_string());
assert_eq!(debugger.watch_variables.len(), 1);
}
#[test]
fn test_constraint_analyzer() {
let mut analyzer = ConstraintAnalyzer::new(1e-6);
let constraint = ConstraintInfo {
name: Some("test_one_hot".to_string()),
constraint_type: solution_debugger::ConstraintType::ExactlyOne,
variables: vec!["a".to_string(), "b".to_string(), "c".to_string()],
parameters: HashMap::new(),
penalty: 10.0,
description: Some("One hot constraint".to_string()),
};
let mut solution = HashMap::new();
solution.insert("a".to_string(), true);
solution.insert("b".to_string(), false);
solution.insert("c".to_string(), false);
let violations = analyzer.analyze(&[constraint.clone()], &solution);
assert_eq!(violations.len(), 0);
solution.insert("b".to_string(), true);
let violations = analyzer.analyze(&[constraint], &solution);
assert_eq!(violations.len(), 1);
assert!(!violations[0].suggested_fixes.is_empty());
}
fn create_test_problem_info() -> ProblemInfo {
let mut qubo = Array2::zeros((3, 3));
qubo[[0, 0]] = -1.0;
qubo[[1, 1]] = -1.0;
qubo[[0, 1]] = 2.0;
qubo[[1, 0]] = 2.0;
let mut var_map = HashMap::new();
var_map.insert("x".to_string(), 0);
var_map.insert("y".to_string(), 1);
var_map.insert("z".to_string(), 2);
ProblemInfo {
name: "Test Problem".to_string(),
problem_type: "QUBO".to_string(),
num_variables: 3,
var_map: var_map.clone(),
reverse_var_map: {
let mut rev = HashMap::new();
for (k, v) in &var_map {
rev.insert(*v, k.clone());
}
rev
},
qubo,
constraints: vec![ConstraintInfo {
name: Some("test_constraint".to_string()),
constraint_type: solution_debugger::ConstraintType::ExactlyOne,
variables: vec!["x".to_string(), "y".to_string()],
parameters: HashMap::new(),
penalty: 10.0,
description: Some("Test constraint".to_string()),
}],
optimal_solution: None,
metadata: HashMap::new(),
}
}
}
#[cfg(test)]
mod profiler_tests {
use super::*;
#[test]
fn test_performance_profiler() {
let config = ProfilerConfig {
enabled: true,
profile_memory: true,
profile_cpu: true,
profile_gpu: false,
sampling_interval: Duration::from_millis(10),
metrics: vec![],
detailed_timing: false,
output_format: OutputFormat::Json,
auto_save_interval: None,
};
let mut profiler = PerformanceProfiler::new(config);
profiler.start_profile("test_profile").unwrap();
{
let _guard = profiler.enter_function("test_function");
profiler.start_timer("computation");
std::thread::sleep(std::time::Duration::from_millis(10));
profiler.stop_timer("computation");
}
let profile = profiler.stop_profile().unwrap();
assert!(profile.id.contains("test_profile"));
assert!(!profile.events.is_empty());
assert!(profile.metrics.time_metrics.total_time > Duration::from_millis(0));
let analysis = profiler.analyze_profile(&profile);
assert!(analysis.summary.total_time > Duration::from_millis(0));
assert!(!analysis.bottlenecks.is_empty());
}
#[test]
fn test_profiler_macros() {
let mut profiler = PerformanceProfiler::new(ProfilerConfig::default());
profiler.start_profile("macro_test").unwrap();
profile!(profiler, "test_macro");
time_it!(profiler, "timed_operation", {
std::thread::sleep(std::time::Duration::from_millis(5));
});
let profile = profiler.stop_profile().unwrap();
assert!(profile.events.len() >= 2);
}
}
#[cfg(test)]
mod dsl_tests {
use super::*;
#[test]
fn test_dsl_parsing() {
let _dsl = ProblemDSL::new();
}
#[test]
fn test_dsl_macros() {
let _dsl = ProblemDSL::new();
}
#[test]
fn test_optimization_hints() {
let _dsl = ProblemDSL::new();
}
}
#[cfg(test)]
mod application_tests {
use super::*;
#[test]
fn test_portfolio_optimizer() {
let returns = array![0.05, 0.10, 0.15];
let covariance = array![
[0.01, 0.002, 0.001],
[0.002, 0.02, 0.003],
[0.001, 0.003, 0.03]
];
let mut optimizer = PortfolioOptimizer::new(returns, covariance, 2.0)
.unwrap()
.with_constraints(PortfolioConstraints::default());
let (qubo, mapping) = optimizer.build_qubo(8).unwrap();
assert!(!mapping.is_empty());
assert_eq!(qubo.shape()[0], qubo.shape()[1]);
}
#[test]
fn test_vrp_problem() {
let mut distances = Array2::from_shape_vec(
(3, 3),
vec![0.0, 10.0, 15.0, 10.0, 0.0, 12.0, 15.0, 12.0, 0.0],
)
.unwrap();
let mut demands = Array1::from(vec![0.0, 10.0, 15.0]);
let mut optimizer = VehicleRoutingOptimizer::new(
distances, 30.0, demands, 1, )
.unwrap();
let mut vrp = BinaryVehicleRoutingProblem::new(optimizer);
assert_eq!(vrp.num_variables(), 9);
let solution = vrp.random_solution();
let energy = vrp.evaluate(&solution);
assert!(energy >= 0.0);
}
}
#[cfg(test)]
mod testing_framework_tests {
use super::*;
#[test]
fn test_test_generator() {
let config = GeneratorConfig {
problem_type: ProblemType::MaxCut,
size: 10,
difficulty: Difficulty::Easy,
seed: Some(42),
parameters: HashMap::new(),
};
assert_eq!(config.size, 10);
assert_eq!(config.problem_type, ProblemType::MaxCut);
assert_eq!(config.size, 10);
}
#[test]
fn test_testing_framework() {
let config = TestConfig {
seed: Some(42),
cases_per_category: 5,
problem_sizes: vec![5, 10],
samplers: vec![SamplerConfig {
name: "SA".to_string(),
num_samples: 50,
parameters: HashMap::new(),
}],
timeout: Duration::from_secs(10),
validation: ValidationConfig {
check_constraints: true,
check_objective: true,
statistical_tests: false,
tolerance: 1e-6,
min_quality: 0.5,
},
output: OutputConfig {
generate_report: false,
format: ReportFormat::Json,
output_dir: "./test".to_string(),
verbosity: testing_framework::VerbosityLevel::Error,
},
};
let mut framework = TestingFramework::new(config);
framework.add_category(TestCategory {
name: "Test Category".to_string(),
description: "Test category for unit tests".to_string(),
problem_types: vec![ProblemType::MaxCut],
difficulties: vec![Difficulty::Easy],
tags: vec!["test".to_string()],
});
framework.generate_suite().unwrap();
}
#[test]
fn test_solution_validator() {
let mut validator = ConstraintAnalyzer::new(1e-6);
let mut solution = HashMap::new();
solution.insert("x".to_string(), true);
solution.insert("y".to_string(), false);
let constraints = [(
"x".to_string(),
"y".to_string(),
solution_debugger::ConstraintType::AtMostOne,
)];
}
}