quantrs2_anneal/active_learning_decomposition/
mod.rs

1//! Active Learning for Problem Decomposition
2//!
3//! This module implements active learning techniques for intelligent decomposition
4//! of complex optimization problems into smaller, more manageable subproblems.
5//! It uses machine learning to guide the decomposition process and adaptively
6//! improve decomposition strategies based on performance feedback.
7//!
8//! Key features:
9//! - Intelligent problem decomposition using graph analysis
10//! - Active learning for decomposition strategy selection
11//! - Hierarchical decomposition with adaptive granularity
12//! - Performance-guided decomposition refinement
13//! - Multi-objective decomposition optimization
14//! - Transfer learning across problem domains
15
16pub mod config;
17pub mod core;
18pub mod knowledge_base;
19pub mod performance_evaluation;
20pub mod problem_analysis;
21pub mod strategy_learning;
22pub mod subproblem_generation;
23pub mod types;
24pub mod utils;
25
26pub use config::*;
27pub use core::*;
28pub use knowledge_base::*;
29pub use performance_evaluation::*;
30pub use problem_analysis::*;
31pub use strategy_learning::*;
32pub use subproblem_generation::*;
33pub use types::*;
34pub use utils::*;
35
36use crate::ising::IsingModel;
37use crate::simulator::AnnealingResult;
38use scirs2_core::ndarray::Array1;
39use std::time::{Duration, Instant};
40
41/// Decomposition strategies
42#[derive(Debug, Clone, PartialEq, Eq, Hash)]
43pub enum DecompositionStrategy {
44    /// Graph partitioning based
45    GraphPartitioning,
46    /// Community detection based
47    CommunityDetection,
48    /// Spectral clustering
49    SpectralClustering,
50    /// Hierarchical decomposition
51    Hierarchical,
52    /// Random decomposition
53    Random,
54    /// Greedy decomposition
55    Greedy,
56    /// No decomposition
57    NoDecomposition,
58    /// Custom strategy
59    Custom(String),
60}
61
62/// Problem analysis result
63#[derive(Debug, Clone)]
64pub struct ProblemAnalysis {
65    /// Graph metrics
66    pub graph_metrics: GraphMetrics,
67    /// Detected communities
68    pub communities: Vec<DetectedCommunity>,
69    /// Detected structures
70    pub structures: Vec<DetectedStructure>,
71    /// Complexity estimate
72    pub complexity: ComplexityEstimate,
73    /// Decomposability score
74    pub decomposability: DecomposabilityScore,
75    /// Extracted features
76    pub problem_features: Array1<f64>,
77}
78
79/// Detected community
80#[derive(Debug, Clone)]
81pub struct DetectedCommunity {
82    /// Community ID
83    pub id: usize,
84    /// Vertices in community
85    pub vertices: Vec<usize>,
86    /// Community modularity
87    pub modularity: f64,
88    /// Internal density
89    pub internal_density: f64,
90    /// External density
91    pub external_density: f64,
92}
93
94/// Subproblem representation
95#[derive(Debug, Clone)]
96pub struct Subproblem {
97    /// Subproblem ID
98    pub id: usize,
99    /// Ising model for subproblem
100    pub model: IsingModel,
101    /// Original vertex indices
102    pub vertices: Vec<usize>,
103    /// Boundary edges to other subproblems
104    pub boundary_edges: Vec<BoundaryEdge>,
105    /// Subproblem metadata
106    pub metadata: SubproblemMetadata,
107}
108
109impl Subproblem {
110    #[must_use]
111    pub fn from_full_problem(problem: &IsingModel) -> Self {
112        Self {
113            id: 0,
114            model: problem.clone(),
115            vertices: (0..problem.num_qubits).collect(),
116            boundary_edges: Vec::new(),
117            metadata: SubproblemMetadata::new(),
118        }
119    }
120}
121
122/// Boundary edge connecting subproblems
123#[derive(Debug, Clone)]
124pub struct BoundaryEdge {
125    /// Vertex index within this subproblem
126    pub internal_vertex: usize,
127    /// Vertex index in original problem
128    pub external_vertex: usize,
129    /// Coupling strength
130    pub coupling_strength: f64,
131}
132
133/// Subproblem metadata
134#[derive(Debug, Clone)]
135pub struct SubproblemMetadata {
136    /// Creation timestamp
137    pub creation_time: Instant,
138    /// Subproblem size
139    pub size: usize,
140    /// Complexity estimate
141    pub complexity_estimate: f64,
142    /// Expected solution time
143    pub expected_solution_time: Duration,
144}
145
146impl SubproblemMetadata {
147    #[must_use]
148    pub fn new() -> Self {
149        Self {
150            creation_time: Instant::now(),
151            size: 0,
152            complexity_estimate: 0.0,
153            expected_solution_time: Duration::from_secs(1),
154        }
155    }
156}
157
158/// Decomposition result
159#[derive(Debug, Clone)]
160pub struct DecompositionResult {
161    /// Generated subproblems
162    pub subproblems: Vec<Subproblem>,
163    /// Strategy used
164    pub strategy_used: DecompositionStrategy,
165    /// Quality score
166    pub quality_score: f64,
167    /// Problem analysis
168    pub analysis: ProblemAnalysis,
169    /// Decomposition metadata
170    pub metadata: DecompositionMetadata,
171}
172
173/// Decomposition metadata
174#[derive(Debug, Clone)]
175pub struct DecompositionMetadata {
176    /// Time spent on decomposition
177    pub decomposition_time: Duration,
178    /// Time spent on strategy selection
179    pub strategy_selection_time: Duration,
180    /// Total number of subproblems
181    pub total_subproblems: usize,
182    /// Decomposition depth
183    pub decomposition_depth: usize,
184}
185
186impl DecompositionMetadata {
187    #[must_use]
188    pub const fn new() -> Self {
189        Self {
190            decomposition_time: Duration::from_secs(0),
191            strategy_selection_time: Duration::from_secs(0),
192            total_subproblems: 0,
193            decomposition_depth: 1,
194        }
195    }
196}