Skip to main content

quantrs2_core/circuit_synthesis/
qaoatemplate_traits.rs

1//! # QAOATemplate - Trait Implementations
2//!
3//! This module contains trait implementations for `QAOATemplate`.
4//!
5//! ## Implemented Traits
6//!
7//! - `AlgorithmTemplate`
8//!
9//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
10
11use super::functions::AlgorithmTemplate;
12use super::types::{
13    AlgorithmSpecification, GateMetadata, ParameterValue, QAOATemplate, QuantumAlgorithmType,
14    ResourceEstimates, SynthesizedCircuit, SynthesizedGate, TemplateInfo,
15};
16use crate::error::{QuantRS2Error, QuantRS2Result};
17use crate::QubitId;
18use std::collections::HashMap;
19use std::time::Duration;
20
21impl AlgorithmTemplate for QAOATemplate {
22    fn synthesize(&self, spec: &AlgorithmSpecification) -> QuantRS2Result<SynthesizedCircuit> {
23        let num_qubits = spec.parameters.num_qubits;
24        let mut gates = Vec::new();
25        for i in 0..num_qubits {
26            gates.push(SynthesizedGate {
27                name: "H".to_string(),
28                qubits: vec![QubitId::new(i as u32)],
29                parameters: vec![],
30                matrix: None,
31                metadata: GateMetadata {
32                    layer: 0,
33                    purpose: "Initial superposition".to_string(),
34                    hints: vec!["single_qubit".to_string()],
35                    hardware_preferences: vec!["any".to_string()],
36                },
37            });
38        }
39        self.create_circuit_from_gates(gates, num_qubits, QuantumAlgorithmType::QAOA)
40    }
41    fn estimate_resources(
42        &self,
43        spec: &AlgorithmSpecification,
44    ) -> QuantRS2Result<ResourceEstimates> {
45        let num_qubits = spec.parameters.num_qubits;
46        let p_layers = spec
47            .parameters
48            .algorithm_specific
49            .get("p_layers")
50            .and_then(|v| {
51                if let ParameterValue::Integer(i) = v {
52                    Some(*i as usize)
53                } else {
54                    None
55                }
56            })
57            .unwrap_or(1);
58        let gate_count = num_qubits + 2 * p_layers * num_qubits;
59        Ok(ResourceEstimates {
60            gate_count,
61            circuit_depth: 1 + 2 * p_layers,
62            qubit_count: num_qubits,
63            gate_breakdown: HashMap::new(),
64            estimated_execution_time: Duration::from_micros((gate_count * 100) as u64),
65            memory_requirements: 1 << num_qubits,
66            parallelization_factor: 0.7,
67        })
68    }
69    fn get_template_info(&self) -> TemplateInfo {
70        TemplateInfo {
71            name: "QAOA".to_string(),
72            supported_parameters: vec![
73                "num_qubits".to_string(),
74                "p_layers".to_string(),
75                "graph".to_string(),
76            ],
77            required_parameters: vec!["num_qubits".to_string()],
78            complexity_scaling: "O(p*m)".to_string(),
79            hardware_compatibility: vec!["all".to_string()],
80        }
81    }
82    fn validate_specification(&self, spec: &AlgorithmSpecification) -> QuantRS2Result<()> {
83        if spec.parameters.num_qubits == 0 {
84            return Err(QuantRS2Error::InvalidParameter(
85                "num_qubits must be > 0".to_string(),
86            ));
87        }
88        Ok(())
89    }
90}