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
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
//! Domain decomposition methods for parallel optimization

use super::types::*;
use crate::sampler::{SampleResult, Sampler};
use scirs2_core::ndarray::Array2;
#[cfg(feature = "parallel")]
use scirs2_core::parallel_ops::*;
use std::collections::HashMap;

/// Domain decomposition solver
pub struct DomainDecomposer<S: Sampler> {
    /// Base sampler for subdomains
    base_sampler: S,
    /// Decomposition strategy
    strategy: DecompositionStrategy,
    /// Coordination method
    coordination: CoordinationStrategy,
    /// Maximum iterations for coordination
    max_coordination_iterations: usize,
    /// Convergence tolerance
    convergence_tolerance: f64,
    /// Overlap between domains
    overlap_size: usize,
}

impl<S: Sampler + Send + Sync + Clone> DomainDecomposer<S> {
    /// Create new domain decomposer
    pub const fn new(base_sampler: S) -> Self {
        Self {
            base_sampler,
            strategy: DecompositionStrategy::Schwarz,
            coordination: CoordinationStrategy::ADMM { rho: 1.0 },
            max_coordination_iterations: 50,
            convergence_tolerance: 1e-4,
            overlap_size: 2,
        }
    }

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

    /// Set coordination strategy
    pub const fn with_coordination(mut self, coordination: CoordinationStrategy) -> Self {
        self.coordination = coordination;
        self
    }

    /// Set maximum coordination iterations
    pub const fn with_max_iterations(mut self, max_iterations: usize) -> Self {
        self.max_coordination_iterations = max_iterations;
        self
    }

    /// Solve using domain decomposition
    pub fn solve(
        &mut self,
        qubo: &Array2<f64>,
        var_map: &HashMap<String, usize>,
        shots: usize,
    ) -> Result<SampleResult, String> {
        // Decompose problem into domains
        let domains = self.decompose_into_domains(qubo, var_map)?;

        match self.strategy {
            DecompositionStrategy::Schwarz => self.schwarz_solve(&domains, shots),
            DecompositionStrategy::BlockJacobi => self.block_jacobi_solve(&domains, shots),
            DecompositionStrategy::AdditiveSchwarz => self.additive_schwarz_solve(&domains, shots),
            DecompositionStrategy::MultiplicativeSchwarz => {
                self.multiplicative_schwarz_solve(&domains, shots)
            }
        }
    }

    /// Decompose QUBO into overlapping domains
    fn decompose_into_domains(
        &self,
        qubo: &Array2<f64>,
        var_map: &HashMap<String, usize>,
    ) -> Result<Vec<Domain>, String> {
        let n = qubo.shape()[0];
        let num_domains = (n as f64 / 10.0).ceil() as usize; // Target ~10 variables per domain
        let domain_size = n / num_domains + self.overlap_size;

        let mut domains = Vec::new();
        let reverse_var_map: HashMap<usize, String> =
            var_map.iter().map(|(k, v)| (*v, k.clone())).collect();

        for domain_id in 0..num_domains {
            let start_idx = domain_id * (domain_size - self.overlap_size);
            let end_idx =
                ((domain_id + 1) * (domain_size - self.overlap_size) + self.overlap_size).min(n);

            if start_idx >= n {
                break;
            }

            let domain_indices: Vec<usize> = (start_idx..end_idx).collect();
            let variables: Vec<String> = domain_indices
                .iter()
                .filter_map(|&i| reverse_var_map.get(&i))
                .cloned()
                .collect();

            // Extract domain QUBO
            let domain_size_actual = domain_indices.len();
            let mut domain_qubo = Array2::zeros((domain_size_actual, domain_size_actual));

            for (i, &idx_i) in domain_indices.iter().enumerate() {
                for (j, &idx_j) in domain_indices.iter().enumerate() {
                    domain_qubo[[i, j]] = qubo[[idx_i, idx_j]];
                }
            }

            // Build domain variable map
            let mut domain_var_map = HashMap::new();
            for (i, var) in variables.iter().enumerate() {
                domain_var_map.insert(var.clone(), i);
            }

            // Identify boundary and internal variables
            let mut boundary_vars = Vec::new();
            let mut internal_vars = Vec::new();

            for (i, &global_idx) in domain_indices.iter().enumerate() {
                let is_boundary = self.is_boundary_variable(qubo, global_idx, &domain_indices);
                if is_boundary {
                    boundary_vars.push(i);
                } else {
                    internal_vars.push(i);
                }
            }

            domains.push(Domain {
                id: domain_id,
                variables,
                qubo: domain_qubo,
                var_map: domain_var_map,
                boundary_vars,
                internal_vars,
            });
        }

        Ok(domains)
    }

    /// Check if variable is on domain boundary
    fn is_boundary_variable(
        &self,
        qubo: &Array2<f64>,
        var_idx: usize,
        domain_indices: &[usize],
    ) -> bool {
        let n = qubo.shape()[0];

        // Check if variable has connections outside the domain
        for j in 0..n {
            if !domain_indices.contains(&j) && qubo[[var_idx, j]].abs() > 1e-10 {
                return true;
            }
        }

        false
    }

    /// Schwarz alternating method
    fn schwarz_solve(&mut self, domains: &[Domain], shots: usize) -> Result<SampleResult, String> {
        let mut coordination_state = CoordinationState {
            iteration: 0,
            lagrange_multipliers: None,
            consensus_variables: None,
            convergence_tolerance: self.convergence_tolerance,
            max_iterations: self.max_coordination_iterations,
        };

        // Initialize coordination state
        self.initialize_coordination_state(&mut coordination_state, domains)?;

        for iteration in 0..self.max_coordination_iterations {
            coordination_state.iteration = iteration;

            // Solve all subdomains in parallel
            let subdomain_solutions =
                self.solve_subdomains_parallel(domains, &coordination_state, shots)?;

            // Update coordination variables
            let converged =
                self.update_coordination(&mut coordination_state, &subdomain_solutions, domains)?;

            if converged {
                break;
            }
        }

        // Integrate final solution
        self.integrate_solutions(domains, &coordination_state)
    }

    /// Block Jacobi method (parallel subdomain solving)
    fn block_jacobi_solve(
        &mut self,
        domains: &[Domain],
        shots: usize,
    ) -> Result<SampleResult, String> {
        let mut coordination_state = CoordinationState {
            iteration: 0,
            lagrange_multipliers: None,
            consensus_variables: None,
            convergence_tolerance: self.convergence_tolerance,
            max_iterations: self.max_coordination_iterations,
        };

        self.initialize_coordination_state(&mut coordination_state, domains)?;

        for iteration in 0..self.max_coordination_iterations {
            coordination_state.iteration = iteration;

            // Solve all subdomains simultaneously
            let subdomain_solutions =
                self.solve_subdomains_parallel(domains, &coordination_state, shots)?;

            // Update all coordination variables simultaneously
            let converged =
                self.update_coordination(&mut coordination_state, &subdomain_solutions, domains)?;

            if converged {
                break;
            }
        }

        self.integrate_solutions(domains, &coordination_state)
    }

    /// Additive Schwarz method
    fn additive_schwarz_solve(
        &mut self,
        domains: &[Domain],
        shots: usize,
    ) -> Result<SampleResult, String> {
        // Similar to block Jacobi but with additive updates
        self.block_jacobi_solve(domains, shots)
    }

    /// Multiplicative Schwarz method
    fn multiplicative_schwarz_solve(
        &mut self,
        domains: &[Domain],
        shots: usize,
    ) -> Result<SampleResult, String> {
        let mut coordination_state = CoordinationState {
            iteration: 0,
            lagrange_multipliers: None,
            consensus_variables: None,
            convergence_tolerance: self.convergence_tolerance,
            max_iterations: self.max_coordination_iterations,
        };

        self.initialize_coordination_state(&mut coordination_state, domains)?;

        for iteration in 0..self.max_coordination_iterations {
            coordination_state.iteration = iteration;

            // Solve subdomains sequentially with immediate updates
            for domain in domains {
                let solution = self.solve_single_subdomain(domain, &coordination_state, shots)?;

                // Update coordination immediately after each subdomain
                self.update_coordination_single(&mut coordination_state, &solution, domain)?;
            }

            // Check convergence
            if self.check_convergence(&coordination_state) {
                break;
            }
        }

        self.integrate_solutions(domains, &coordination_state)
    }

    /// Initialize coordination state
    fn initialize_coordination_state(
        &self,
        state: &mut CoordinationState,
        domains: &[Domain],
    ) -> Result<(), String> {
        if let CoordinationStrategy::ADMM { .. } = &self.coordination {
            let mut lagrange_multipliers = HashMap::new();
            let mut consensus_variables = HashMap::new();

            // Initialize Lagrange multipliers and consensus variables
            for domain in domains {
                for &boundary_var in &domain.boundary_vars {
                    lagrange_multipliers.insert((domain.id, boundary_var), 0.0);
                    consensus_variables.insert(boundary_var, false);
                }
            }

            state.lagrange_multipliers = Some(lagrange_multipliers);
            state.consensus_variables = Some(consensus_variables);
        } else {
            // Other coordination strategies would be initialized here
        }

        Ok(())
    }

    /// Solve subdomains in parallel
    fn solve_subdomains_parallel(
        &self,
        domains: &[Domain],
        coordination: &CoordinationState,
        shots: usize,
    ) -> Result<Vec<SubdomainSolution>, String> {
        let solutions: Vec<_> = {
            #[cfg(feature = "parallel")]
            {
                domains
                    .par_iter()
                    .map(|domain| self.solve_single_subdomain(domain, coordination, shots))
                    .collect::<Result<Vec<_>, _>>()?
            }
            #[cfg(not(feature = "parallel"))]
            {
                domains
                    .iter()
                    .map(|domain| self.solve_single_subdomain(domain, coordination, shots))
                    .collect::<Result<Vec<_>, _>>()?
            }
        };

        Ok(solutions)
    }

    /// Solve single subdomain
    fn solve_single_subdomain(
        &self,
        domain: &Domain,
        coordination: &CoordinationState,
        shots: usize,
    ) -> Result<SubdomainSolution, String> {
        // Modify QUBO with coordination terms
        let modified_qubo = self.add_coordination_terms(domain, coordination)?;

        // Solve
        let results_vec = self
            .base_sampler
            .run_qubo(&(modified_qubo, domain.var_map.clone()), shots)
            .map_err(|e| format!("Sampler error: {e:?}"))?;

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

        Ok(SubdomainSolution {
            domain_id: domain.id,
            results,
        })
    }

    /// Add coordination terms to subdomain QUBO
    fn add_coordination_terms(
        &self,
        domain: &Domain,
        coordination: &CoordinationState,
    ) -> Result<Array2<f64>, String> {
        let mut modified_qubo = domain.qubo.clone();

        if let CoordinationStrategy::ADMM { rho } = &self.coordination {
            if let (Some(lagrange), Some(consensus)) = (
                &coordination.lagrange_multipliers,
                &coordination.consensus_variables,
            ) {
                // Add augmented Lagrangian terms
                for &boundary_var in &domain.boundary_vars {
                    if let Some(local_idx) = domain.var_map.values().find(|&&v| v == boundary_var) {
                        let lambda = lagrange.get(&(domain.id, boundary_var)).unwrap_or(&0.0);
                        let z = if *consensus.get(&boundary_var).unwrap_or(&false) {
                            1.0
                        } else {
                            0.0
                        };

                        // Add (rho/2)||x - z||^2 + lambda^T(x - z)
                        modified_qubo[[*local_idx, *local_idx]] += rho + 2.0 * lambda * (1.0 - z);
                    }
                }
            }
        }

        Ok(modified_qubo)
    }

    /// Update coordination variables
    fn update_coordination(
        &self,
        state: &mut CoordinationState,
        solutions: &[SubdomainSolution],
        domains: &[Domain],
    ) -> Result<bool, String> {
        match &self.coordination {
            CoordinationStrategy::ADMM { rho } => {
                self.update_admm_coordination(state, solutions, domains, *rho)
            }
            _ => Ok(false),
        }
    }

    /// Update ADMM coordination
    fn update_admm_coordination(
        &self,
        state: &mut CoordinationState,
        solutions: &[SubdomainSolution],
        domains: &[Domain],
        rho: f64,
    ) -> Result<bool, String> {
        if let (Some(lagrange), Some(consensus)) = (
            &mut state.lagrange_multipliers,
            &mut state.consensus_variables,
        ) {
            let mut max_residual = 0.0_f64;

            // Update consensus variables (z-update)
            for domain in domains {
                for &boundary_var in &domain.boundary_vars {
                    let mut total_value = 0.0;
                    let mut count = 0;

                    // Collect values from all domains containing this variable
                    for solution in solutions {
                        if let Some(sample) = solution.results.best_sample() {
                            if let Some(var_name) = domain
                                .var_map
                                .iter()
                                .find(|(_, &idx)| idx == boundary_var)
                                .map(|(name, _)| name)
                            {
                                if let Some(&value) = sample.get(var_name) {
                                    total_value += if value { 1.0 } else { 0.0 };
                                    count += 1;
                                }
                            }
                        }
                    }

                    // Consensus as majority vote
                    let new_consensus = if count > 0 {
                        total_value / count as f64 > 0.5
                    } else {
                        false
                    };

                    let old_consensus = *consensus.get(&boundary_var).unwrap_or(&false);
                    consensus.insert(boundary_var, new_consensus);

                    // Track residual
                    let residual = if new_consensus == old_consensus {
                        0.0
                    } else {
                        1.0
                    };
                    max_residual = max_residual.max(residual);
                }
            }

            // Update Lagrange multipliers (dual update)
            for domain in domains {
                for &boundary_var in &domain.boundary_vars {
                    if let Some(solution) = solutions.iter().find(|s| s.domain_id == domain.id) {
                        if let Some(sample) = solution.results.best_sample() {
                            if let Some(var_name) = domain
                                .var_map
                                .iter()
                                .find(|(_, &idx)| idx == boundary_var)
                                .map(|(name, _)| name)
                            {
                                if let Some(&x_value) = sample.get(var_name) {
                                    let z_value = *consensus.get(&boundary_var).unwrap_or(&false);
                                    let x_val = if x_value { 1.0 } else { 0.0 };
                                    let z_val = if z_value { 1.0 } else { 0.0 };

                                    let lambda_key = (domain.id, boundary_var);
                                    let old_lambda = *lagrange.get(&lambda_key).unwrap_or(&0.0);
                                    let new_lambda = rho.mul_add(x_val - z_val, old_lambda);
                                    lagrange.insert(lambda_key, new_lambda);
                                }
                            }
                        }
                    }
                }
            }

            // Check convergence
            Ok(max_residual < state.convergence_tolerance)
        } else {
            Ok(false)
        }
    }

    /// Update coordination for single domain (multiplicative Schwarz)
    fn update_coordination_single(
        &self,
        state: &mut CoordinationState,
        solution: &SubdomainSolution,
        domain: &Domain,
    ) -> Result<(), String> {
        // Simplified single domain update
        if let Some(consensus) = &mut state.consensus_variables {
            for &boundary_var in &domain.boundary_vars {
                if let Some(sample) = solution.results.best_sample() {
                    if let Some(var_name) = domain
                        .var_map
                        .iter()
                        .find(|(_, &idx)| idx == boundary_var)
                        .map(|(name, _)| name)
                    {
                        if let Some(&value) = sample.get(var_name) {
                            consensus.insert(boundary_var, value);
                        }
                    }
                }
            }
        }
        Ok(())
    }

    /// Check coordination convergence
    const fn check_convergence(&self, state: &CoordinationState) -> bool {
        // Simplified convergence check
        state.iteration >= self.max_coordination_iterations / 2
    }

    /// Integrate solutions from all subdomains
    fn integrate_solutions(
        &self,
        domains: &[Domain],
        coordination_state: &CoordinationState,
    ) -> Result<SampleResult, String> {
        // Use consensus variables as final solution
        if let Some(consensus) = &coordination_state.consensus_variables {
            let mut final_sample = HashMap::new();

            // Collect all variables from all domains
            for domain in domains {
                for (var_name, &local_idx) in &domain.var_map {
                    if let Some(&value) = consensus.get(&local_idx) {
                        final_sample.insert(var_name.clone(), value);
                    }
                }
            }

            // Create SampleResult with integrated solution
            // This is simplified - in practice would compute proper energy
            Ok(SampleResult {
                assignments: final_sample,
                energy: 0.0,
                occurrences: 1,
            })
        } else {
            Err("No consensus variables available".to_string())
        }
    }
}

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

    #[test]
    fn test_domain_decomposer_creation() {
        let base_sampler = SASampler::new(None);
        let decomposer = DomainDecomposer::new(base_sampler);

        assert_eq!(decomposer.max_coordination_iterations, 50);
        assert_eq!(decomposer.overlap_size, 2);
    }

    #[test]
    fn test_domain_decomposition() {
        let base_sampler = SASampler::new(None);
        let decomposer = DomainDecomposer::new(base_sampler);

        // Create test QUBO
        let qubo = Array2::from_shape_vec((6, 6), (0..36).map(|x| x as f64 * 0.1).collect())
            .expect("6x6 QUBO matrix construction should succeed");

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

        let domains = decomposer.decompose_into_domains(&qubo, &var_map);
        assert!(domains.is_ok());

        let domains = domains.expect("Domain decomposition should succeed");
        assert!(!domains.is_empty());

        // Check that domains cover all variables
        let mut all_vars = std::collections::HashSet::new();
        for domain in &domains {
            for var in &domain.variables {
                all_vars.insert(var.clone());
            }
        }
        assert_eq!(all_vars.len(), 6);
    }

    #[test]
    fn test_boundary_variable_detection() {
        let base_sampler = SASampler::new(None);
        let decomposer = DomainDecomposer::new(base_sampler);

        // Create QUBO with clear structure
        let mut qubo = Array2::zeros((4, 4));
        qubo[[0, 1]] = 1.0; // Connection within domain
        qubo[[2, 3]] = 1.0; // Connection within domain
        qubo[[1, 2]] = 1.0; // Cross-domain connection

        let mut domain_indices = vec![0, 1];

        // Variable 1 should be boundary (connects to variable 2 outside domain)
        assert!(decomposer.is_boundary_variable(&qubo, 1, &domain_indices));
        // Variable 0 should not be boundary
        assert!(!decomposer.is_boundary_variable(&qubo, 0, &domain_indices));
    }
}