quantrs2-tytan 0.1.3

High-level quantum annealing interface inspired by Tytan 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
//! Hierarchical solving methods for large-scale problems

use super::types::*;
use crate::sampler::{SampleResult, Sampler};
use scirs2_core::ndarray::Array2;
use std::collections::HashMap;

/// Hierarchical solver with multi-level approach
pub struct HierarchicalSolver<S: Sampler> {
    /// Base sampler for solving problems
    base_sampler: S,
    /// Hierarchical strategy
    strategy: HierarchicalStrategy,
    /// Coarsening approach
    coarsening: CoarseningStrategy,
    /// Minimum problem size for recursion
    min_problem_size: usize,
    /// Maximum number of levels
    max_levels: usize,
    /// Refinement iterations per level
    refinement_iterations: usize,
}

impl<S: Sampler> HierarchicalSolver<S> {
    /// Create new hierarchical solver
    pub const fn new(base_sampler: S) -> Self {
        Self {
            base_sampler,
            strategy: HierarchicalStrategy::CoarsenSolve,
            coarsening: CoarseningStrategy::VariableClustering,
            min_problem_size: 10,
            max_levels: 10,
            refinement_iterations: 5,
        }
    }

    /// Set hierarchical strategy
    pub const fn with_strategy(mut self, strategy: HierarchicalStrategy) -> Self {
        self.strategy = strategy;
        self
    }

    /// Set coarsening strategy
    pub const fn with_coarsening(mut self, coarsening: CoarseningStrategy) -> Self {
        self.coarsening = coarsening;
        self
    }

    /// Set minimum problem size
    pub const fn with_min_problem_size(mut self, size: usize) -> Self {
        self.min_problem_size = size;
        self
    }

    /// Solve using hierarchical approach
    pub fn solve(
        &mut self,
        qubo: &Array2<f64>,
        var_map: &HashMap<String, usize>,
        shots: usize,
    ) -> Result<SampleResult, String> {
        match self.strategy {
            HierarchicalStrategy::CoarsenSolve => self.coarsen_solve_approach(qubo, var_map, shots),
            HierarchicalStrategy::MultiGrid => self.multigrid_approach(qubo, var_map, shots),
            HierarchicalStrategy::VCycle => self.v_cycle_approach(qubo, var_map, shots),
        }
    }

    /// Coarsen-solve approach: coarsen problem, solve, then refine
    fn coarsen_solve_approach(
        &mut self,
        qubo: &Array2<f64>,
        var_map: &HashMap<String, usize>,
        shots: usize,
    ) -> Result<SampleResult, String> {
        // Build hierarchy
        let hierarchy = self.build_hierarchy(qubo, var_map)?;

        // Solve coarsest level
        let coarsest_level = hierarchy.levels.last().ok_or("Empty hierarchy")?;

        let coarse_results = self
            .base_sampler
            .run_qubo(
                &(coarsest_level.qubo.clone(), coarsest_level.var_map.clone()),
                shots,
            )
            .map_err(|e| format!("Sampler error: {e:?}"))?;

        // Take the best result (first one, since they're sorted by energy)
        let coarse_result = coarse_results
            .into_iter()
            .next()
            .ok_or_else(|| "No solutions found".to_string())?;

        // Refine solution back through levels
        self.refine_through_hierarchy(&hierarchy, coarse_result)
    }

    /// Multi-grid approach with multiple V-cycles
    fn multigrid_approach(
        &mut self,
        qubo: &Array2<f64>,
        var_map: &HashMap<String, usize>,
        shots: usize,
    ) -> Result<SampleResult, String> {
        let initial_results = self
            .base_sampler
            .run_qubo(&(qubo.clone(), var_map.clone()), shots / 4)
            .map_err(|e| format!("Initial sampler error: {e:?}"))?;

        // Take the best result (first one, since they're sorted by energy)
        let mut current_solution = initial_results
            .into_iter()
            .next()
            .ok_or_else(|| "No initial solutions found".to_string())?;

        // Perform multiple V-cycles for refinement
        for _cycle in 0..3 {
            current_solution =
                self.v_cycle_refinement(qubo, var_map, &current_solution, shots / 4)?;
        }

        Ok(current_solution)
    }

    /// V-cycle approach: down to coarse, solve, up with refinement
    fn v_cycle_approach(
        &mut self,
        qubo: &Array2<f64>,
        var_map: &HashMap<String, usize>,
        shots: usize,
    ) -> Result<SampleResult, String> {
        self.v_cycle_refinement(qubo, var_map, &SampleResult::default(), shots)
    }

    /// Single V-cycle for solution refinement
    fn v_cycle_refinement(
        &mut self,
        qubo: &Array2<f64>,
        var_map: &HashMap<String, usize>,
        initial_solution: &SampleResult,
        shots: usize,
    ) -> Result<SampleResult, String> {
        // Build hierarchy
        let hierarchy = self.build_hierarchy(qubo, var_map)?;

        // Restrict solution to coarse levels
        let mut current_solution = initial_solution.clone();

        // Down cycle: restrict to coarser levels
        for level in 1..hierarchy.levels.len() {
            current_solution = self.restrict_solution(&hierarchy, level - 1, &current_solution)?;
        }

        // Solve at coarsest level
        if let Some(coarsest_level) = hierarchy.levels.last() {
            let coarse_results = self
                .base_sampler
                .run_qubo(
                    &(coarsest_level.qubo.clone(), coarsest_level.var_map.clone()),
                    shots,
                )
                .map_err(|e| format!("Coarse sampler error: {e:?}"))?;

            // Take the best result (first one, since they're sorted by energy)
            current_solution = coarse_results
                .into_iter()
                .next()
                .ok_or_else(|| "No coarse solutions found".to_string())?;
        }

        // Up cycle: interpolate and refine
        for level in (0..hierarchy.levels.len() - 1).rev() {
            current_solution =
                self.interpolate_and_refine(&hierarchy, level, &current_solution, shots / 4)?;
        }

        Ok(current_solution)
    }

    /// Build problem hierarchy through coarsening
    fn build_hierarchy(
        &self,
        qubo: &Array2<f64>,
        var_map: &HashMap<String, usize>,
    ) -> Result<Hierarchy, String> {
        let mut levels = Vec::new();
        let mut projections = Vec::new();

        let mut current_qubo = qubo.clone();
        let mut current_var_map = var_map.clone();
        let mut current_size = current_qubo.shape()[0];
        let mut level = 0;

        // Add finest level
        levels.push(HierarchyLevel {
            level,
            qubo: current_qubo.clone(),
            var_map: current_var_map.clone(),
            size: current_size,
        });

        // Build coarser levels
        while current_size > self.min_problem_size && level < self.max_levels {
            let (coarse_qubo, coarse_var_map, projection) =
                self.coarsen_problem(&current_qubo, &current_var_map)?;

            current_qubo = coarse_qubo;
            current_var_map = coarse_var_map;
            current_size = current_qubo.shape()[0];
            level += 1;

            levels.push(HierarchyLevel {
                level,
                qubo: current_qubo.clone(),
                var_map: current_var_map.clone(),
                size: current_size,
            });

            projections.push(projection);
        }

        Ok(Hierarchy {
            levels,
            projections,
        })
    }

    /// Coarsen problem to next level
    fn coarsen_problem(
        &self,
        qubo: &Array2<f64>,
        var_map: &HashMap<String, usize>,
    ) -> Result<(Array2<f64>, HashMap<String, usize>, Projection), String> {
        match self.coarsening {
            CoarseningStrategy::VariableClustering => {
                self.variable_clustering_coarsen(qubo, var_map)
            }
            _ => {
                // Default to variable clustering
                self.variable_clustering_coarsen(qubo, var_map)
            }
        }
    }

    /// Variable clustering coarsening
    fn variable_clustering_coarsen(
        &self,
        qubo: &Array2<f64>,
        _var_map: &HashMap<String, usize>,
    ) -> Result<(Array2<f64>, HashMap<String, usize>, Projection), String> {
        let n = qubo.shape()[0];

        // Cluster strongly connected variables
        let mut clusters = Vec::new();
        let mut assigned = vec![false; n];

        for i in 0..n {
            if !assigned[i] {
                let mut cluster = vec![i];
                assigned[i] = true;

                // Find strongly connected variables
                for j in i + 1..n {
                    if !assigned[j] && qubo[[i, j]].abs() > 0.5 {
                        cluster.push(j);
                        assigned[j] = true;
                    }
                }

                clusters.push(cluster);
            }
        }

        // Build coarse problem
        let num_clusters = clusters.len();
        let mut coarse_qubo = Array2::zeros((num_clusters, num_clusters));

        for (ci, cluster_i) in clusters.iter().enumerate() {
            for (cj, cluster_j) in clusters.iter().enumerate() {
                let mut weight = 0.0;

                for &i in cluster_i {
                    for &j in cluster_j {
                        weight += qubo[[i, j]];
                    }
                }

                coarse_qubo[[ci, cj]] = weight;
            }
        }

        // Build variable mapping
        let mut coarse_var_map = HashMap::new();
        for (ci, _cluster) in clusters.iter().enumerate() {
            let var_name = format!("cluster_{ci}");
            coarse_var_map.insert(var_name, ci);
        }

        // Build projection
        let projection = Projection {
            fine_to_coarse: (0..n)
                .map(|i| clusters.iter().position(|c| c.contains(&i)).unwrap_or(0))
                .collect(),
            coarse_to_fine: clusters,
        };

        Ok((coarse_qubo, coarse_var_map, projection))
    }

    /// Refine solution through hierarchy levels
    fn refine_through_hierarchy(
        &mut self,
        hierarchy: &Hierarchy,
        coarse_solution: SampleResult,
    ) -> Result<SampleResult, String> {
        let mut current_solution = coarse_solution;

        // Interpolate solution from coarse to fine levels
        for level in (0..hierarchy.levels.len() - 1).rev() {
            current_solution = self.interpolate_solution(hierarchy, level, &current_solution)?;

            // Refine at this level
            for _iter in 0..self.refinement_iterations {
                current_solution = self.refine_solution(
                    &hierarchy.levels[level].qubo,
                    &hierarchy.levels[level].var_map,
                    &current_solution,
                    10, // Small number of shots for refinement
                )?;
            }
        }

        Ok(current_solution)
    }

    /// Restrict solution to coarser level
    fn restrict_solution(
        &self,
        hierarchy: &Hierarchy,
        level: usize,
        solution: &SampleResult,
    ) -> Result<SampleResult, String> {
        if level >= hierarchy.projections.len() {
            return Ok(solution.clone());
        }

        let projection = &hierarchy.projections[level];
        let coarse_level = &hierarchy.levels[level + 1];

        // Create restricted solution
        let restricted_solution = SampleResult::default();

        for (var_name, &coarse_idx) in &coarse_level.var_map {
            // Find corresponding fine variables
            let fine_vars = &projection.coarse_to_fine[coarse_idx];

            // Simple majority vote for binary variables
            let mut votes = 0i32;
            for &fine_idx in fine_vars {
                if let Some(fine_var_name) = hierarchy.levels[level]
                    .var_map
                    .iter()
                    .find(|(_, &idx)| idx == fine_idx)
                    .map(|(name, _)| name)
                {
                    if let Some(sample) = solution.best_sample() {
                        if let Some(&value) = sample.get(fine_var_name) {
                            votes += if value { 1 } else { -1 };
                        }
                    }
                }
            }

            // Set coarse variable value
            if let Some(mut best_sample) = restricted_solution.best_sample().cloned() {
                best_sample.insert(var_name.clone(), votes > 0);
            } else {
                let mut new_sample = HashMap::new();
                new_sample.insert(var_name.clone(), votes > 0);
                // Create new SampleResult with this sample
                // This is simplified - in practice would need proper SampleResult construction
            }
        }

        Ok(restricted_solution)
    }

    /// Interpolate solution from coarse to fine level
    fn interpolate_solution(
        &self,
        hierarchy: &Hierarchy,
        level: usize,
        coarse_solution: &SampleResult,
    ) -> Result<SampleResult, String> {
        if level >= hierarchy.projections.len() {
            return Ok(coarse_solution.clone());
        }

        let projection = &hierarchy.projections[level];
        let fine_level = &hierarchy.levels[level];
        let coarse_level = &hierarchy.levels[level + 1];

        let interpolated_solution = SampleResult::default();

        // Interpolate from coarse to fine
        for (fine_var_name, &fine_idx) in &fine_level.var_map {
            let coarse_idx = projection.fine_to_coarse[fine_idx];

            // Find coarse variable name
            if let Some((coarse_var_name, _)) = coarse_level
                .var_map
                .iter()
                .find(|(_, &idx)| idx == coarse_idx)
            {
                if let Some(coarse_sample) = coarse_solution.best_sample() {
                    if let Some(&coarse_value) = coarse_sample.get(coarse_var_name) {
                        // Simple interpolation: copy coarse value to fine
                        if let Some(mut fine_sample) = interpolated_solution.best_sample().cloned()
                        {
                            fine_sample.insert(fine_var_name.clone(), coarse_value);
                        } else {
                            let mut new_sample = HashMap::new();
                            new_sample.insert(fine_var_name.clone(), coarse_value);
                            // Create new SampleResult - simplified
                        }
                    }
                }
            }
        }

        Ok(interpolated_solution)
    }

    /// Interpolate and refine solution at given level
    fn interpolate_and_refine(
        &mut self,
        hierarchy: &Hierarchy,
        level: usize,
        coarse_solution: &SampleResult,
        shots: usize,
    ) -> Result<SampleResult, String> {
        // First interpolate
        let mut interpolated = self.interpolate_solution(hierarchy, level, coarse_solution)?;

        // Then refine
        for _iter in 0..self.refinement_iterations {
            interpolated = self.refine_solution(
                &hierarchy.levels[level].qubo,
                &hierarchy.levels[level].var_map,
                &interpolated,
                shots,
            )?;
        }

        Ok(interpolated)
    }

    /// Refine solution at current level
    fn refine_solution(
        &mut self,
        qubo: &Array2<f64>,
        var_map: &HashMap<String, usize>,
        _current_solution: &SampleResult,
        shots: usize,
    ) -> Result<SampleResult, String> {
        // Use base sampler with warm start from current solution
        // This is simplified - in practice would implement warm starting
        let results = self
            .base_sampler
            .run_qubo(&(qubo.clone(), var_map.clone()), shots)
            .map_err(|e| format!("Refinement sampler error: {e:?}"))?;

        // Take the best result (first one, since they're sorted by energy)
        results
            .into_iter()
            .next()
            .ok_or_else(|| "No refinement results found".to_string())
    }
}

impl Default for SampleResult {
    fn default() -> Self {
        // Simplified default implementation
        // In practice, this would create a proper empty SampleResult
        Self {
            assignments: HashMap::new(),
            energy: 0.0,
            occurrences: 0,
        }
    }
}

impl SampleResult {
    /// Get best sample (simplified implementation)
    pub const fn best_sample(&self) -> Option<&HashMap<String, bool>> {
        Some(&self.assignments)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sampler::simulated_annealing::SASampler;
    use scirs2_core::ndarray::Array2;
    use std::collections::HashMap;

    #[test]
    fn test_hierarchical_solver_creation() {
        let base_sampler = SASampler::new(None);
        let solver = HierarchicalSolver::new(base_sampler);

        // Test that solver is created with default parameters
        assert_eq!(solver.min_problem_size, 10);
        assert_eq!(solver.max_levels, 10);
    }

    #[test]
    fn test_hierarchy_building() {
        let base_sampler = SASampler::new(None);
        let solver = HierarchicalSolver::new(base_sampler);

        // Create simple QUBO
        let mut qubo = Array2::from_shape_vec(
            (4, 4),
            vec![
                1.0, 0.5, 0.1, 0.0, 0.5, 1.0, 0.0, 0.1, 0.1, 0.0, 1.0, 0.5, 0.0, 0.1, 0.5, 1.0,
            ],
        )
        .expect("QUBO matrix construction should succeed");

        let mut var_map = HashMap::new();
        for i in 0..4 {
            var_map.insert(format!("x{i}"), i);
        }

        let hierarchy = solver.build_hierarchy(&qubo, &var_map);
        assert!(hierarchy.is_ok());

        let h = hierarchy.expect("Hierarchy building should succeed");
        assert!(!h.levels.is_empty());
        assert_eq!(h.levels[0].size, 4); // Finest level should have 4 variables
    }
}