quantrs2-anneal 0.1.3

Quantum annealing support for the QuantRS2 framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
//! Core active learning decomposer implementation

use scirs2_core::ndarray::Array1;
use scirs2_core::random::prelude::*;
use scirs2_core::random::{Rng, SeedableRng};
use std::collections::{HashMap, HashSet};
use std::time::Instant;

use super::{
    ActiveLearningConfig, BoundaryEdge, DecompositionKnowledgeBase, DecompositionMetadata,
    DecompositionResult, DecompositionStrategy, DecompositionStrategyLearner, EvaluationMetric,
    PerformanceEvaluator, PerformanceRecord, ProblemAnalysis, ProblemAnalyzer, QueryStrategy,
    Subproblem, SubproblemGenerator, SubproblemMetadata,
};
use crate::ising::IsingModel;

/// Active learning decomposer for optimization problems
#[derive(Debug, Clone)]
pub struct ActiveLearningDecomposer {
    /// Decomposition strategy learner
    pub strategy_learner: DecompositionStrategyLearner,
    /// Problem analyzer
    pub problem_analyzer: ProblemAnalyzer,
    /// Subproblem generator
    pub subproblem_generator: SubproblemGenerator,
    /// Performance evaluator
    pub performance_evaluator: PerformanceEvaluator,
    /// Knowledge base
    pub knowledge_base: DecompositionKnowledgeBase,
    /// Configuration
    pub config: ActiveLearningConfig,
}

impl ActiveLearningDecomposer {
    /// Create new active learning decomposer
    pub fn new(config: ActiveLearningConfig) -> Result<Self, String> {
        let strategy_learner = DecompositionStrategyLearner::new()?;
        let problem_analyzer = ProblemAnalyzer::new()?;
        let subproblem_generator = SubproblemGenerator::new()?;
        let performance_evaluator = PerformanceEvaluator::new()?;
        let knowledge_base = DecompositionKnowledgeBase::new()?;

        Ok(Self {
            strategy_learner,
            problem_analyzer,
            subproblem_generator,
            performance_evaluator,
            knowledge_base,
            config,
        })
    }

    /// Decompose problem using active learning
    pub fn decompose_problem(
        &mut self,
        problem: &IsingModel,
    ) -> Result<DecompositionResult, String> {
        // Analyze problem structure and characteristics
        let problem_analysis = self.analyze_problem(problem)?;

        // Select decomposition strategy using active learning
        let strategy = self.select_strategy(problem, &problem_analysis)?;

        // Generate subproblems
        let subproblems = self.generate_subproblems(problem, &strategy, &problem_analysis)?;

        // Validate decomposition quality
        let quality_score = self.validate_decomposition_quality(&subproblems, problem)?;

        // Update knowledge base if learning is enabled
        if self.config.enable_online_learning {
            self.update_knowledge_base(problem, &strategy, quality_score)?;
        }

        Ok(DecompositionResult {
            subproblems,
            strategy_used: strategy,
            quality_score,
            analysis: problem_analysis,
            metadata: DecompositionMetadata::new(),
        })
    }

    /// Analyze problem structure and characteristics
    pub fn analyze_problem(&mut self, problem: &IsingModel) -> Result<ProblemAnalysis, String> {
        // Calculate graph metrics
        let graph_metrics = self
            .problem_analyzer
            .graph_analyzer
            .calculate_metrics(problem)?;

        // Detect communities
        let communities = self
            .problem_analyzer
            .graph_analyzer
            .detect_communities(problem)?;

        // Detect structures
        let structures = self
            .problem_analyzer
            .structure_detector
            .detect_structures(problem)?;

        // Estimate complexity
        let complexity = self
            .problem_analyzer
            .complexity_estimator
            .estimate_complexity(problem)?;

        // Score decomposability
        let decomposability = self
            .problem_analyzer
            .decomposability_scorer
            .score_decomposability(problem)?;

        Ok(ProblemAnalysis {
            graph_metrics,
            communities,
            structures,
            complexity,
            decomposability,
            problem_features: self.extract_problem_features(problem)?,
        })
    }

    /// Extract problem features for learning
    pub fn extract_problem_features(&self, problem: &IsingModel) -> Result<Array1<f64>, String> {
        let mut features = Vec::new();

        // Basic features
        features.push(problem.num_qubits as f64);

        // Count non-zero couplings
        let mut num_couplings = 0;
        for i in 0..problem.num_qubits {
            for j in (i + 1)..problem.num_qubits {
                if problem.get_coupling(i, j).unwrap_or(0.0).abs() > 1e-10 {
                    num_couplings += 1;
                }
            }
        }
        features.push(f64::from(num_couplings));

        // Density
        let max_couplings = problem.num_qubits * (problem.num_qubits - 1) / 2;
        let density = if max_couplings > 0 {
            f64::from(num_couplings) / max_couplings as f64
        } else {
            0.0
        };
        features.push(density);

        // Bias statistics
        let mut bias_sum = 0.0;
        let mut bias_var = 0.0;
        for i in 0..problem.num_qubits {
            let bias = problem.get_bias(i).unwrap_or(0.0);
            bias_sum += bias;
            bias_var += bias * bias;
        }
        let bias_mean = bias_sum / problem.num_qubits as f64;
        bias_var = bias_var / problem.num_qubits as f64 - bias_mean * bias_mean;
        features.extend_from_slice(&[bias_mean, bias_var.sqrt()]);

        // Coupling statistics
        let mut coupling_sum = 0.0;
        let mut coupling_var = 0.0;
        if num_couplings > 0 {
            for i in 0..problem.num_qubits {
                for j in (i + 1)..problem.num_qubits {
                    let coupling = problem.get_coupling(i, j).unwrap_or(0.0);
                    if coupling.abs() > 1e-10 {
                        coupling_sum += coupling;
                        coupling_var += coupling * coupling;
                    }
                }
            }
            let coupling_mean = coupling_sum / f64::from(num_couplings);
            coupling_var = coupling_var / f64::from(num_couplings) - coupling_mean * coupling_mean;
            features.extend_from_slice(&[coupling_mean, coupling_var.sqrt()]);
        } else {
            features.extend_from_slice(&[0.0, 0.0]);
        }

        // Pad to fixed size
        features.resize(20, 0.0);
        Ok(Array1::from_vec(features))
    }

    /// Select decomposition strategy using active learning
    fn select_strategy(
        &self,
        problem: &IsingModel,
        analysis: &ProblemAnalysis,
    ) -> Result<DecompositionStrategy, String> {
        // Get strategy recommendation from model
        let recommendation = self
            .strategy_learner
            .recommend_strategy(problem, analysis)?;

        // Decide whether to explore or exploit
        let should_explore = self.should_explore(&analysis.problem_features)?;

        if should_explore {
            // Exploration: try a different strategy or query for feedback
            self.explore_strategy(problem, analysis, &recommendation)
        } else {
            // Exploitation: use the recommended strategy
            Ok(recommendation)
        }
    }

    /// Decide whether to explore or exploit
    fn should_explore(&self, features: &Array1<f64>) -> Result<bool, String> {
        // Get uncertainty estimate for this problem
        let uncertainty = self
            .strategy_learner
            .selection_model
            .get_uncertainty(features)?;

        // Compare with threshold and exploration rate
        let explore_threshold = self.config.exploration_rate;
        let uncertainty_threshold = 0.5; // High uncertainty threshold

        Ok(uncertainty > uncertainty_threshold || thread_rng().random::<f64>() < explore_threshold)
    }

    /// Explore strategy selection
    fn explore_strategy(
        &self,
        problem: &IsingModel,
        analysis: &ProblemAnalysis,
        base_recommendation: &DecompositionStrategy,
    ) -> Result<DecompositionStrategy, String> {
        // Select query strategy
        match self.strategy_learner.query_selector.query_strategy {
            QueryStrategy::UncertaintySampling => self.uncertainty_sampling_exploration(analysis),
            QueryStrategy::DiversitySampling => self.diversity_sampling_exploration(analysis),
            _ => {
                // Default: slightly perturb the base recommendation
                self.perturb_strategy(base_recommendation)
            }
        }
    }

    /// Uncertainty sampling exploration
    fn uncertainty_sampling_exploration(
        &self,
        analysis: &ProblemAnalysis,
    ) -> Result<DecompositionStrategy, String> {
        // Find strategy with highest uncertainty
        let strategies = &self.knowledge_base.strategy_database.strategies;
        let mut best_strategy = DecompositionStrategy::GraphPartitioning;
        let mut highest_uncertainty = 0.0;

        for strategy in strategies {
            let uncertainty = self
                .strategy_learner
                .selection_model
                .get_strategy_uncertainty(strategy, &analysis.problem_features)?;

            if uncertainty > highest_uncertainty {
                highest_uncertainty = uncertainty;
                best_strategy = strategy.clone();
            }
        }

        Ok(best_strategy)
    }

    /// Diversity sampling exploration
    fn diversity_sampling_exploration(
        &self,
        analysis: &ProblemAnalysis,
    ) -> Result<DecompositionStrategy, String> {
        // Find strategy most different from recently used strategies
        let recent_strategies: Vec<_> = self
            .strategy_learner
            .query_selector
            .query_history
            .iter()
            .rev()
            .take(10)
            .map(|record| &record.recommended_strategy)
            .collect();

        let strategies = &self.knowledge_base.strategy_database.strategies;
        let mut best_strategy = DecompositionStrategy::GraphPartitioning;
        let mut max_diversity = 0.0;

        for strategy in strategies {
            let diversity = self.calculate_strategy_diversity(strategy, &recent_strategies)?;

            if diversity > max_diversity {
                max_diversity = diversity;
                best_strategy = strategy.clone();
            }
        }

        Ok(best_strategy)
    }

    /// Calculate strategy diversity
    fn calculate_strategy_diversity(
        &self,
        strategy: &DecompositionStrategy,
        recent_strategies: &[&DecompositionStrategy],
    ) -> Result<f64, String> {
        if recent_strategies.is_empty() {
            return Ok(1.0);
        }

        let mut total_distance = 0.0;
        for &recent_strategy in recent_strategies {
            total_distance += self.strategy_distance(strategy, recent_strategy)?;
        }

        Ok(total_distance / recent_strategies.len() as f64)
    }

    /// Calculate distance between strategies
    fn strategy_distance(
        &self,
        strategy1: &DecompositionStrategy,
        strategy2: &DecompositionStrategy,
    ) -> Result<f64, String> {
        // Simplified strategy distance
        if strategy1 == strategy2 {
            Ok(0.0)
        } else {
            Ok(1.0)
        }
    }

    /// Perturb strategy selection
    fn perturb_strategy(
        &self,
        base_strategy: &DecompositionStrategy,
    ) -> Result<DecompositionStrategy, String> {
        let strategies = &self.knowledge_base.strategy_database.strategies;
        let mut rng = thread_rng();

        // Select a random strategy with some probability
        if rng.random::<f64>() < 0.3 {
            let random_idx = rng.random_range(0..strategies.len());
            Ok(strategies[random_idx].clone())
        } else {
            Ok(base_strategy.clone())
        }
    }

    /// Generate subproblems using selected strategy
    fn generate_subproblems(
        &self,
        problem: &IsingModel,
        strategy: &DecompositionStrategy,
        analysis: &ProblemAnalysis,
    ) -> Result<Vec<Subproblem>, String> {
        match strategy {
            DecompositionStrategy::GraphPartitioning => {
                self.graph_partitioning_decomposition(problem, analysis)
            }
            DecompositionStrategy::CommunityDetection => {
                self.community_detection_decomposition(problem, analysis)
            }
            DecompositionStrategy::SpectralClustering => {
                self.spectral_clustering_decomposition(problem, analysis)
            }
            DecompositionStrategy::NoDecomposition => {
                Ok(vec![Subproblem::from_full_problem(problem)])
            }
            _ => {
                // Default to graph partitioning
                self.graph_partitioning_decomposition(problem, analysis)
            }
        }
    }

    /// Graph partitioning decomposition
    fn graph_partitioning_decomposition(
        &self,
        problem: &IsingModel,
        analysis: &ProblemAnalysis,
    ) -> Result<Vec<Subproblem>, String> {
        // Simple bisection for demonstration
        let n = problem.num_qubits;
        let mid = n / 2;

        let partition1: Vec<usize> = (0..mid).collect();
        let partition2: Vec<usize> = (mid..n).collect();

        let subproblem1 = self.create_subproblem(problem, &partition1, 0)?;
        let subproblem2 = self.create_subproblem(problem, &partition2, 1)?;

        Ok(vec![subproblem1, subproblem2])
    }

    /// Community detection decomposition
    fn community_detection_decomposition(
        &self,
        problem: &IsingModel,
        analysis: &ProblemAnalysis,
    ) -> Result<Vec<Subproblem>, String> {
        // Use detected communities from analysis
        let communities = &analysis.communities;
        let mut subproblems = Vec::new();

        for (i, community) in communities.iter().enumerate() {
            if community.vertices.len() >= self.config.min_subproblem_size {
                let subproblem = self.create_subproblem(problem, &community.vertices, i)?;
                subproblems.push(subproblem);
            }
        }

        // If no valid communities, fall back to graph partitioning
        if subproblems.is_empty() {
            self.graph_partitioning_decomposition(problem, analysis)
        } else {
            Ok(subproblems)
        }
    }

    /// Spectral clustering decomposition
    fn spectral_clustering_decomposition(
        &self,
        problem: &IsingModel,
        analysis: &ProblemAnalysis,
    ) -> Result<Vec<Subproblem>, String> {
        // Simplified spectral clustering - in practice would use eigendecomposition
        self.graph_partitioning_decomposition(problem, analysis)
    }

    /// Create subproblem from vertex subset
    pub fn create_subproblem(
        &self,
        problem: &IsingModel,
        vertices: &[usize],
        subproblem_id: usize,
    ) -> Result<Subproblem, String> {
        // Create Ising model for subproblem
        let mut subproblem_model = IsingModel::new(vertices.len());

        // Map vertex indices
        let vertex_map: HashMap<usize, usize> = vertices
            .iter()
            .enumerate()
            .map(|(new_idx, &old_idx)| (old_idx, new_idx))
            .collect();

        // Copy biases
        for (new_idx, &old_idx) in vertices.iter().enumerate() {
            let bias = problem.get_bias(old_idx).unwrap_or(0.0);
            subproblem_model
                .set_bias(new_idx, bias)
                .map_err(|e| e.to_string())?;
        }

        // Copy couplings within subproblem
        for (i, &old_i) in vertices.iter().enumerate() {
            for (j, &old_j) in vertices.iter().enumerate().skip(i + 1) {
                let coupling = problem.get_coupling(old_i, old_j).unwrap_or(0.0);
                if coupling.abs() > 1e-10 {
                    subproblem_model
                        .set_coupling(i, j, coupling)
                        .map_err(|e| e.to_string())?;
                }
            }
        }

        // Identify boundary edges (edges connecting to other subproblems)
        let mut boundary_edges = Vec::new();
        for &vertex in vertices {
            for other_vertex in 0..problem.num_qubits {
                if !vertices.contains(&other_vertex) {
                    let coupling = problem.get_coupling(vertex, other_vertex).unwrap_or(0.0);
                    if coupling.abs() > 1e-10 {
                        boundary_edges.push(BoundaryEdge {
                            internal_vertex: vertex_map[&vertex],
                            external_vertex: other_vertex,
                            coupling_strength: coupling,
                        });
                    }
                }
            }
        }

        Ok(Subproblem {
            id: subproblem_id,
            model: subproblem_model,
            vertices: vertices.to_vec(),
            boundary_edges,
            metadata: SubproblemMetadata::new(),
        })
    }

    /// Validate decomposition quality
    pub fn validate_decomposition_quality(
        &self,
        subproblems: &[Subproblem],
        original_problem: &IsingModel,
    ) -> Result<f64, String> {
        let mut total_score = 0.0;
        let mut num_criteria = 0;

        // Size balance
        let sizes: Vec<usize> = subproblems.iter().map(|sp| sp.vertices.len()).collect();
        let avg_size = sizes.iter().sum::<usize>() as f64 / sizes.len() as f64;
        let size_variance = sizes
            .iter()
            .map(|&size| (size as f64 - avg_size).powi(2))
            .sum::<f64>()
            / sizes.len() as f64;
        let size_balance_score = 1.0 / (1.0 + size_variance / avg_size);
        total_score += size_balance_score;
        num_criteria += 1;

        // Cut quality (minimize boundary edges)
        let total_boundary_edges: usize =
            subproblems.iter().map(|sp| sp.boundary_edges.len()).sum();
        let total_edges = original_problem.num_qubits * (original_problem.num_qubits - 1) / 2;
        let cut_quality_score = 1.0 - (total_boundary_edges as f64 / total_edges as f64);
        total_score += cut_quality_score;
        num_criteria += 1;

        // Coverage (all vertices included)
        let covered_vertices: HashSet<usize> = subproblems
            .iter()
            .flat_map(|sp| sp.vertices.iter())
            .copied()
            .collect();
        let coverage_score = covered_vertices.len() as f64 / original_problem.num_qubits as f64;
        total_score += coverage_score;
        num_criteria += 1;

        Ok(total_score / f64::from(num_criteria))
    }

    /// Update knowledge base with new experience
    fn update_knowledge_base(
        &mut self,
        problem: &IsingModel,
        strategy: &DecompositionStrategy,
        quality_score: f64,
    ) -> Result<(), String> {
        // Update strategy performance history
        let performance_record = PerformanceRecord {
            timestamp: Instant::now(),
            problem_id: format!("problem_{}", problem.num_qubits),
            strategy_used: strategy.clone(),
            metrics: {
                let mut metrics = HashMap::new();
                metrics.insert(EvaluationMetric::SolutionQuality, quality_score);
                metrics
            },
            overall_score: quality_score,
        };

        self.strategy_learner
            .performance_history
            .entry(format!("{strategy:?}"))
            .or_insert_with(Vec::new)
            .push(performance_record.clone());

        self.performance_evaluator
            .performance_history
            .push(performance_record);

        // Update learning statistics
        self.strategy_learner.learning_stats.total_queries += 1;
        if quality_score > self.config.performance_threshold {
            self.strategy_learner.learning_stats.successful_predictions += 1;
        }

        // Update success rates
        let strategy_key = format!("{strategy:?}");
        let history = &self.strategy_learner.performance_history[&strategy_key];
        let success_count = history
            .iter()
            .filter(|record| record.overall_score > self.config.performance_threshold)
            .count();
        let success_rate = success_count as f64 / history.len() as f64;

        self.knowledge_base
            .strategy_database
            .success_rates
            .insert(strategy.clone(), success_rate);

        Ok(())
    }
}