quantrs2-core 0.1.3

Core types and traits for the QuantRS2 quantum computing framework
Documentation
//! Auto-generated module
//!
//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)

use crate::error::{QuantRS2Error, QuantRS2Result};
use scirs2_core::ndarray::{Array1, Array2};
use std::collections::HashMap;
use std::time::{Duration, Instant};

use super::types::{
    AlgorithmParameters, AlgorithmSpecification, CircuitSynthesizer, GraphData,
    OracleSpecification, ProblemInstance, QuantumAlgorithmType, ResourceEstimates, SearchSpaceData,
    SynthesisConstraints, SynthesisObjective, SynthesizedCircuit, TemplateInfo, VQETemplate,
};

/// Algorithm template trait
pub trait AlgorithmTemplate: std::fmt::Debug + Send + Sync {
    /// Generate circuit from specification
    fn synthesize(&self, spec: &AlgorithmSpecification) -> QuantRS2Result<SynthesizedCircuit>;
    /// Estimate resources without full synthesis
    fn estimate_resources(
        &self,
        spec: &AlgorithmSpecification,
    ) -> QuantRS2Result<ResourceEstimates>;
    /// Get template information
    fn get_template_info(&self) -> TemplateInfo;
    /// Validate algorithm specification
    fn validate_specification(&self, spec: &AlgorithmSpecification) -> QuantRS2Result<()>;
}
#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_circuit_synthesizer_creation() {
        let synthesizer = CircuitSynthesizer::new();
        assert!(synthesizer.is_ok());
        let synthesizer =
            synthesizer.expect("Failed to create synthesizer in test_circuit_synthesizer_creation");
        let available_algorithms = synthesizer.get_available_algorithms();
        assert!(available_algorithms.contains(&QuantumAlgorithmType::VQE));
        assert!(available_algorithms.contains(&QuantumAlgorithmType::QAOA));
        assert!(available_algorithms.contains(&QuantumAlgorithmType::Grover));
    }
    #[test]
    fn test_vqe_synthesis() {
        let synthesizer =
            CircuitSynthesizer::new().expect("Failed to create synthesizer in test_vqe_synthesis");
        let spec = AlgorithmSpecification::vqe(4, vec![0.5, 0.3, 0.7, 0.1, 0.9, 0.2, 0.4, 0.8]);
        let circuit = synthesizer.synthesize_circuit(&spec);
        assert!(circuit.is_ok());
        let circuit = circuit.expect("Failed to synthesize VQE circuit in test_vqe_synthesis");
        assert_eq!(circuit.metadata.source_algorithm, QuantumAlgorithmType::VQE);
        assert_eq!(circuit.resource_estimates.qubit_count, 4);
        assert!(!circuit.gates.is_empty());
    }
    #[test]
    fn test_qaoa_synthesis() {
        let synthesizer =
            CircuitSynthesizer::new().expect("Failed to create synthesizer in test_qaoa_synthesis");
        let graph = GraphData {
            num_vertices: 4,
            adjacency_matrix: Array2::zeros((4, 4)),
            edge_weights: HashMap::new(),
            vertex_weights: vec![1.0; 4],
        };
        let spec = AlgorithmSpecification::qaoa(4, 2, graph);
        let circuit = synthesizer.synthesize_circuit(&spec);
        assert!(circuit.is_ok());
        let circuit = circuit.expect("Failed to synthesize QAOA circuit in test_qaoa_synthesis");
        assert_eq!(
            circuit.metadata.source_algorithm,
            QuantumAlgorithmType::QAOA
        );
        assert_eq!(circuit.resource_estimates.qubit_count, 4);
    }
    #[test]
    fn test_grover_synthesis() {
        let synthesizer = CircuitSynthesizer::new()
            .expect("Failed to create synthesizer in test_grover_synthesis");
        let search_space = SearchSpaceData {
            total_items: 16,
            marked_items: 1,
            oracle_specification: OracleSpecification::MarkedStates(vec![5]),
        };
        let spec = AlgorithmSpecification::grover(4, search_space);
        let circuit = synthesizer.synthesize_circuit(&spec);
        assert!(circuit.is_ok());
        let circuit =
            circuit.expect("Failed to synthesize Grover circuit in test_grover_synthesis");
        assert_eq!(
            circuit.metadata.source_algorithm,
            QuantumAlgorithmType::Grover
        );
        assert_eq!(circuit.resource_estimates.qubit_count, 4);
    }
    #[test]
    fn test_resource_estimation() {
        let synthesizer = CircuitSynthesizer::new()
            .expect("Failed to create synthesizer in test_resource_estimation");
        let spec = AlgorithmSpecification::vqe(6, vec![0.0; 12]);
        let estimates = synthesizer.estimate_resources(&spec);
        assert!(estimates.is_ok());
        let estimates =
            estimates.expect("Failed to estimate resources in test_resource_estimation");
        assert_eq!(estimates.qubit_count, 6);
        assert!(estimates.gate_count > 0);
        assert!(estimates.circuit_depth > 0);
    }
    #[test]
    fn test_synthesis_caching() {
        let synthesizer = CircuitSynthesizer::new()
            .expect("Failed to create synthesizer in test_synthesis_caching");
        let spec = AlgorithmSpecification::vqe(3, vec![0.1, 0.2, 0.3, 0.4, 0.5, 0.6]);
        let circuit1 = synthesizer
            .synthesize_circuit(&spec)
            .expect("Failed to synthesize circuit (first attempt) in test_synthesis_caching");
        let circuit2 = synthesizer
            .synthesize_circuit(&spec)
            .expect("Failed to synthesize circuit (second attempt) in test_synthesis_caching");
        assert_eq!(circuit1.gates.len(), circuit2.gates.len());
        assert_eq!(
            circuit1.resource_estimates.gate_count,
            circuit2.resource_estimates.gate_count
        );
        let stats = synthesizer.get_performance_stats();
        assert!(stats.cache_stats.cache_hits > 0);
    }
    #[test]
    fn test_custom_template_registration() {
        let synthesizer = CircuitSynthesizer::new()
            .expect("Failed to create synthesizer in test_custom_template_registration");
        let custom_template = Box::new(VQETemplate::new());
        let custom_algorithm = QuantumAlgorithmType::Custom("MyAlgorithm".to_string());
        assert!(synthesizer
            .register_template(custom_algorithm.clone(), custom_template)
            .is_ok());
        let available_algorithms = synthesizer.get_available_algorithms();
        assert!(available_algorithms.contains(&custom_algorithm));
    }
    #[test]
    fn test_optimization_objectives() {
        let synthesizer = CircuitSynthesizer::new()
            .expect("Failed to create synthesizer in test_optimization_objectives");
        let mut spec = AlgorithmSpecification::vqe(4, vec![0.0; 8]);
        spec.optimization_objectives = vec![
            SynthesisObjective::MinimizeGates,
            SynthesisObjective::MinimizeDepth,
        ];
        let circuit = synthesizer.synthesize_circuit(&spec);
        assert!(circuit.is_ok());
        let circuit =
            circuit.expect("Failed to synthesize circuit in test_optimization_objectives");
        assert!(!circuit.optimization_report.optimizations_applied.is_empty());
    }
    #[test]
    fn test_specification_validation() {
        let synthesizer = CircuitSynthesizer::new()
            .expect("Failed to create synthesizer in test_specification_validation");
        let invalid_spec = AlgorithmSpecification::vqe(0, vec![]);
        let result = synthesizer.synthesize_circuit(&invalid_spec);
        assert!(result.is_err());
    }
    #[test]
    fn test_performance_monitoring() {
        let synthesizer = CircuitSynthesizer::new()
            .expect("Failed to create synthesizer in test_performance_monitoring");
        for i in 2..5 {
            let spec = AlgorithmSpecification::vqe(i, vec![0.0; i * 2]);
            let _ = synthesizer.synthesize_circuit(&spec);
        }
        let stats = synthesizer.get_performance_stats();
        assert!(stats.total_syntheses >= 3);
        assert!(stats
            .average_synthesis_times
            .contains_key(&QuantumAlgorithmType::VQE));
    }
    #[test]
    fn test_different_algorithm_types() {
        let synthesizer = CircuitSynthesizer::new()
            .expect("Failed to create synthesizer in test_different_algorithm_types");
        let qft_spec = AlgorithmSpecification {
            algorithm_type: QuantumAlgorithmType::QFT,
            parameters: AlgorithmParameters {
                num_qubits: 3,
                max_depth: None,
                variational_params: vec![],
                algorithm_specific: HashMap::new(),
            },
            problem_instance: ProblemInstance {
                hamiltonian: None,
                graph: None,
                linear_system: None,
                search_space: None,
                factorization_target: None,
                custom_data: HashMap::new(),
            },
            constraints: SynthesisConstraints {
                max_qubits: None,
                max_depth: None,
                max_gates: None,
                hardware_constraints: None,
                min_fidelity: None,
                max_synthesis_time: None,
            },
            optimization_objectives: vec![SynthesisObjective::Balanced],
        };
        let qft_circuit = synthesizer.synthesize_circuit(&qft_spec);
        assert!(qft_circuit.is_ok());
        let qft_circuit = qft_circuit
            .expect("Failed to synthesize QFT circuit in test_different_algorithm_types");
        assert_eq!(
            qft_circuit.metadata.source_algorithm,
            QuantumAlgorithmType::QFT
        );
    }
    #[test]
    fn test_resource_estimation_scaling() {
        let synthesizer = CircuitSynthesizer::new()
            .expect("Failed to create synthesizer in test_resource_estimation_scaling");
        let small_spec = AlgorithmSpecification::vqe(3, vec![0.0; 6]);
        let large_spec = AlgorithmSpecification::vqe(6, vec![0.0; 12]);
        let small_estimates = synthesizer.estimate_resources(&small_spec).expect(
            "Failed to estimate resources for small spec in test_resource_estimation_scaling",
        );
        let large_estimates = synthesizer.estimate_resources(&large_spec).expect(
            "Failed to estimate resources for large spec in test_resource_estimation_scaling",
        );
        assert!(large_estimates.gate_count > small_estimates.gate_count);
        assert!(large_estimates.qubit_count > small_estimates.qubit_count);
        assert!(large_estimates.memory_requirements > small_estimates.memory_requirements);
    }
}