quantrs2-tytan 0.2.0

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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
//! # Population Annealing (PA) Sampler
//!
//! Population Annealing (PA) maintains a population of R replicas that are
//! annealed simultaneously through a sequence of inverse temperatures β₁ < β₂ < … < β_K.
//! After each temperature step the replicas are resampled by importance weights,
//! concentrating computational effort on the lowest-energy region of the search space
//! while preserving the correct thermodynamic ensemble.
//!
//! ## Algorithm
//!
//! 1. **Initialise**: create R replicas with independent random binary assignments.
//! 2. **For each β_k in the schedule**, run `sweeps_per_step` Metropolis sweeps on
//!    every replica, then:
//!    - Compute importance weights `w_r = exp(-(β_{k+1} - β_k) * E_r)`.
//!    - Compute ESS = `(Σ w_r)² / Σ w_r²`.
//!    - If `ESS / R < resample_threshold`, multinomial-resample R replicas by weight.
//! 3. Return `shots` samples drawn from the final population.
//!
//! ## Mathematical Formulation
//!
//! Given a QUBO matrix Q, the objective is to minimise:
//!
//! ```text
//! E(x) = sum_{i,j} Q[i,j] * x[i] * x[j],   x[i] in {0, 1}
//! ```
//!
//! The Metropolis acceptance probability at inverse temperature β for a move
//! with energy change ΔE:
//!
//! ```text
//! P(accept) = min(1, exp(-β * ΔE))
//! ```
//!
//! ## Citation
//!
//! Hukushima, K., & Iba, Y. (2003).
//! Population Annealing and Its Application to a Spin Glass.
//! *AIP Conference Proceedings*, 690(1), 200–206.
//! <https://doi.org/10.1063/1.1632130>
//!
//! ## Parameters
//!
//! See [`PAParams`] for all tunable parameters (`population`, `beta_schedule`,
//! `sweeps_per_step`, `resample_threshold`).
//!
//! ## When to Use
//!
//! - **Best for**: problems where you need an ensemble of near-ground-state solutions
//!   or accurate estimates of low-temperature thermodynamic observables.
//! - **Strengths**: provides diversity across the low-energy landscape; ESS-based
//!   resampling avoids weight collapse.
//! - **Limitations**: higher memory usage (R replicas stored simultaneously);
//!   slower per-shot than SA for single-solution queries.
//!
//! ## Usage
//!
//! ```
//! use quantrs2_tytan::sampler::{PopulationAnnealingSampler, Sampler};
//! use scirs2_core::ndarray::Array;
//! use std::collections::HashMap;
//!
//! // K3 Max-Cut QUBO
//! let mut q = Array::<f64, _>::zeros((3, 3));
//! q[[0, 0]] = -1.0;
//! q[[1, 1]] = -1.0;
//! q[[2, 2]] = -1.0;
//! q[[0, 1]] = 2.0;
//! q[[0, 2]] = 2.0;
//! q[[1, 2]] = 2.0;
//!
//! let mut var_map = HashMap::new();
//! var_map.insert("x0".to_string(), 0);
//! var_map.insert("x1".to_string(), 1);
//! var_map.insert("x2".to_string(), 2);
//!
//! // Small schedule for a fast doc-test
//! let betas: Vec<f64> = (0..10).map(|i| 0.1 + 2.9 * i as f64 / 9.0).collect();
//! let sampler = PopulationAnnealingSampler::new()
//!     .with_seed(42)
//!     .with_population(20)
//!     .with_beta_schedule(betas);
//!
//! let results = sampler.run_qubo(&(q, var_map), 5).expect("PA sampler failed");
//! assert!(!results.is_empty());
//! println!("Best energy: {}", results[0].energy);
//! ```

use scirs2_core::ndarray::{Array, ArrayD, Ix2};
use scirs2_core::random::prelude::*;
use scirs2_core::random::rngs::StdRng;
use std::collections::HashMap;

use super::energy::{compute_influence_simd, energy_full_simd, update_influence_simd};
use super::{SampleResult, Sampler, SamplerError, SamplerResult};

/// Parameters for the Population Annealing algorithm
#[derive(Debug, Clone)]
pub struct PAParams {
    /// Number of replicas in the population
    pub population: usize,
    /// Inverse temperature schedule (ascending from low to high β)
    pub beta_schedule: Vec<f64>,
    /// Number of Metropolis sweeps per temperature step
    pub sweeps_per_step: usize,
    /// ESS/population threshold below which to resample
    pub resample_threshold: f64,
}

impl Default for PAParams {
    fn default() -> Self {
        // Linspace(0.1, 3.0, 50)
        let betas: Vec<f64> = (0..50)
            .map(|i| 0.1 + (3.0 - 0.1) * i as f64 / 49.0)
            .collect();
        Self {
            population: 100,
            beta_schedule: betas,
            sweeps_per_step: 5,
            resample_threshold: 0.5,
        }
    }
}

/// Population Annealing Sampler
///
/// Uses the Population Annealing algorithm to solve QUBO/HOBO problems.
/// Maintains a population of replicas that evolve through a temperature
/// schedule, with resampling to concentrate on low-energy regions.
///
/// # Example
///
/// ```
/// use quantrs2_tytan::sampler::{PopulationAnnealingSampler, Sampler};
/// use std::collections::HashMap;
/// use scirs2_core::ndarray::Array;
///
/// let mut q = Array::<f64, _>::zeros((3, 3));
/// q[[0, 0]] = -1.0;
/// q[[1, 1]] = -1.0;
/// q[[2, 2]] = -1.0;
/// q[[0, 1]] = 2.0;
/// q[[0, 2]] = 2.0;
/// q[[1, 2]] = 2.0;
///
/// let mut var_map = HashMap::new();
/// var_map.insert("x0".to_string(), 0);
/// var_map.insert("x1".to_string(), 1);
/// var_map.insert("x2".to_string(), 2);
///
/// // Small schedule for a fast doc-test
/// let betas: Vec<f64> = (0..10).map(|i| 0.1 + 2.9 * i as f64 / 9.0).collect();
/// let sampler = PopulationAnnealingSampler::new()
///     .with_seed(42)
///     .with_population(20)
///     .with_beta_schedule(betas);
///
/// let results = sampler.run_qubo(&(q, var_map), 5).expect("Population annealing failed");
/// assert!(!results.is_empty());
/// println!("Best energy: {}", results[0].energy);
/// ```
#[derive(Debug, Clone)]
pub struct PopulationAnnealingSampler {
    /// Random number generator seed
    pub seed: Option<u64>,
    /// PA algorithm parameters
    pub params: PAParams,
}

impl PopulationAnnealingSampler {
    /// Create a new Population Annealing sampler with default parameters
    #[must_use]
    pub fn new() -> Self {
        Self {
            seed: None,
            params: PAParams::default(),
        }
    }

    /// Set the random seed for reproducibility
    #[must_use]
    pub fn with_seed(mut self, seed: u64) -> Self {
        self.seed = Some(seed);
        self
    }

    /// Set the population size (number of replicas)
    #[must_use]
    pub fn with_population(mut self, population: usize) -> Self {
        self.params.population = population;
        self
    }

    /// Set the number of sweeps per temperature step
    #[must_use]
    pub fn with_sweeps_per_step(mut self, sweeps: usize) -> Self {
        self.params.sweeps_per_step = sweeps;
        self
    }

    /// Set the ESS/population resampling threshold
    #[must_use]
    pub fn with_resample_threshold(mut self, threshold: f64) -> Self {
        self.params.resample_threshold = threshold;
        self
    }

    /// Set a custom beta (inverse temperature) schedule
    #[must_use]
    pub fn with_beta_schedule(mut self, schedule: Vec<f64>) -> Self {
        self.params.beta_schedule = schedule;
        self
    }

    /// Compute QUBO energy: E(x) = sum_{i,j} Q[i,j] * x[i] * x[j]
    ///
    /// Delegates to [`energy_full_simd`] which uses 4-wide f64 SIMD for n >= 32.
    #[inline]
    fn compute_qubo_energy_flat(q_matrix: &[f64], state: &[bool], n: usize) -> f64 {
        energy_full_simd(state, q_matrix, n)
    }

    /// Compute influence vector g[i] = Q[i,i] + sum_{j!=i} (Q[i,j] + Q[j,i]) * x[j]
    ///
    /// ΔE from flipping bit i = (1 - 2*x[i]) * g[i]
    ///
    /// Delegates to [`compute_influence_simd`] which uses SIMD for n >= 32.
    #[inline]
    fn compute_influence_flat(q_matrix: &[f64], state: &[bool], n: usize) -> Vec<f64> {
        compute_influence_simd(state, q_matrix, n)
    }

    /// Update influence vector after flipping bit k.
    ///
    /// Delegates to [`update_influence_simd`] which uses SIMD for n >= 32.
    #[inline]
    fn update_influence_flat(g: &mut [f64], q_matrix: &[f64], k: usize, new_val: bool, n: usize) {
        update_influence_simd(g, q_matrix, n, k, new_val);
    }

    /// Evaluate HOBO energy for a generic tensor
    fn evaluate_hobo_energy<D>(tensor: &Array<f64, D>, state: &[bool], n_vars: usize) -> f64
    where
        D: scirs2_core::ndarray::Dimension + 'static,
    {
        // Convert to dynamic dimensionality to allow slice-based indexing
        let dyn_tensor: ArrayD<f64> = tensor.to_owned().into_dyn();
        Self::evaluate_hobo_energy_dyn(&dyn_tensor, state, n_vars)
    }

    /// Evaluate HOBO energy for a dynamic-dimension tensor
    fn evaluate_hobo_energy_dyn(tensor: &ArrayD<f64>, state: &[bool], _n_vars: usize) -> f64 {
        super::energy::hobo_energy_full_dispatch(state, tensor)
    }

    /// Perform multinomial resampling of the population
    ///
    /// Given a population and weights, draw `n` samples with replacement.
    fn multinomial_resample(
        population: &[Vec<bool>],
        weights: &[f64],
        n: usize,
        rng: &mut StdRng,
    ) -> Vec<Vec<bool>> {
        let total_weight: f64 = weights.iter().sum();
        if total_weight <= 0.0 || population.is_empty() {
            // Fallback: uniform resampling
            return (0..n)
                .map(|_| {
                    let idx = rng.random_range(0..population.len());
                    population[idx].clone()
                })
                .collect();
        }

        // Build cumulative weight distribution
        let mut cumulative = Vec::with_capacity(weights.len());
        let mut running = 0.0;
        for &w in weights {
            running += w / total_weight;
            cumulative.push(running);
        }
        // Ensure last entry is exactly 1.0
        if let Some(last) = cumulative.last_mut() {
            *last = 1.0;
        }

        // Sample n indices
        (0..n)
            .map(|_| {
                let u: f64 = rng.random_range(0.0f64..1.0f64);
                let idx = cumulative
                    .iter()
                    .position(|&c| u <= c)
                    .unwrap_or(cumulative.len().saturating_sub(1));
                population[idx.min(population.len() - 1)].clone()
            })
            .collect()
    }

    /// Run population annealing on a QUBO problem (flat matrix)
    fn run_pa_qubo(
        &self,
        q_flat: &[f64],
        n: usize,
        shots: usize,
        seed: u64,
    ) -> Vec<(Vec<bool>, f64)> {
        let pop_size = self.params.population;
        let beta_schedule = &self.params.beta_schedule;
        let sweeps_per_step = self.params.sweeps_per_step;
        let resample_threshold = self.params.resample_threshold;

        let mut rng = StdRng::seed_from_u64(seed);

        if beta_schedule.is_empty() || pop_size == 0 || n == 0 {
            return vec![];
        }

        // Initialize population: each replica is a random binary state
        let mut population: Vec<Vec<bool>> = (0..pop_size)
            .map(|_| (0..n).map(|_| rng.random_bool(0.5)).collect())
            .collect();

        // Initialize energies for each replica
        let mut energies: Vec<f64> = population
            .iter()
            .map(|s| Self::compute_qubo_energy_flat(q_flat, s, n))
            .collect();

        // Track current beta (initialized at 0 before the first step)
        let mut beta_prev = 0.0;

        for &beta_new in beta_schedule {
            let delta_beta = beta_new - beta_prev;

            // MC sweeps on each replica
            for r in 0..pop_size {
                let state = &mut population[r];
                let mut energy = energies[r];
                let mut g = Self::compute_influence_flat(q_flat, state, n);

                for _ in 0..sweeps_per_step {
                    for i in 0..n {
                        let delta_e = (1.0 - 2.0 * if state[i] { 1.0 } else { 0.0 }) * g[i];
                        // Metropolis acceptance: flip with probability 1 / (1 + exp(beta * delta_e))
                        let accept = if delta_e <= 0.0 {
                            true
                        } else {
                            let threshold = 1.0 / (1.0 + (beta_new * delta_e).exp());
                            rng.random_range(0.0f64..1.0f64) < threshold
                        };

                        if accept {
                            let new_val = !state[i];
                            Self::update_influence_flat(&mut g, q_flat, i, new_val, n);
                            state[i] = new_val;
                            energy += delta_e;
                        }
                    }
                }
                energies[r] = energy;
            }

            // Compute importance weights w_r = exp(-delta_beta * E_r)
            // Use log-sum-exp trick for numerical stability
            let log_weights: Vec<f64> = energies.iter().map(|&e| -delta_beta * e).collect();
            let max_log_w = log_weights
                .iter()
                .cloned()
                .fold(f64::NEG_INFINITY, f64::max);

            let weights: Vec<f64> = log_weights
                .iter()
                .map(|&lw| (lw - max_log_w).exp())
                .collect();

            // Compute ESS = (Σw_r)² / Σw_r²
            let sum_w: f64 = weights.iter().sum();
            let sum_w2: f64 = weights.iter().map(|&w| w * w).sum();

            let ess = if sum_w2 > 0.0 {
                sum_w * sum_w / sum_w2
            } else {
                0.0
            };

            // Resample if ESS/N < threshold
            if (ess / pop_size as f64) < resample_threshold {
                let new_population =
                    Self::multinomial_resample(&population, &weights, pop_size, &mut rng);
                // Recompute energies after resampling
                let new_energies: Vec<f64> = new_population
                    .iter()
                    .map(|s| Self::compute_qubo_energy_flat(q_flat, s, n))
                    .collect();
                population = new_population;
                energies = new_energies;
            }

            beta_prev = beta_new;
        }

        // Return shots samples from final population
        let pop_size_actual = population.len();
        (0..shots)
            .map(|i| {
                let idx = i % pop_size_actual;
                (population[idx].clone(), energies[idx])
            })
            .collect()
    }

    /// Run population annealing on a HOBO tensor
    fn run_pa_hobo<D>(
        &self,
        tensor: &Array<f64, D>,
        n_vars: usize,
        shots: usize,
        seed: u64,
    ) -> Vec<(Vec<bool>, f64)>
    where
        D: scirs2_core::ndarray::Dimension + 'static,
    {
        // Convert once to dynamic dimensionality for repeated indexing
        let dyn_tensor: ArrayD<f64> = tensor.to_owned().into_dyn();
        self.run_pa_hobo_dyn(&dyn_tensor, n_vars, shots, seed)
    }

    /// Run population annealing on a dynamic-dimension HOBO tensor
    fn run_pa_hobo_dyn(
        &self,
        tensor: &ArrayD<f64>,
        n_vars: usize,
        shots: usize,
        seed: u64,
    ) -> Vec<(Vec<bool>, f64)> {
        let pop_size = self.params.population;
        let beta_schedule = &self.params.beta_schedule;
        let sweeps_per_step = self.params.sweeps_per_step;
        let resample_threshold = self.params.resample_threshold;

        let mut rng = StdRng::seed_from_u64(seed);

        if beta_schedule.is_empty() || pop_size == 0 || n_vars == 0 {
            return vec![];
        }

        let mut population: Vec<Vec<bool>> = (0..pop_size)
            .map(|_| (0..n_vars).map(|_| rng.random_bool(0.5)).collect())
            .collect();

        let mut energies: Vec<f64> = population
            .iter()
            .map(|s| Self::evaluate_hobo_energy_dyn(tensor, s, n_vars))
            .collect();

        let mut beta_prev = 0.0;

        for &beta_new in beta_schedule {
            let delta_beta = beta_new - beta_prev;

            for r in 0..pop_size {
                let state = &mut population[r];
                let mut energy = energies[r];

                for _ in 0..sweeps_per_step {
                    for i in 0..n_vars {
                        state[i] = !state[i];
                        let new_energy = Self::evaluate_hobo_energy_dyn(tensor, state, n_vars);
                        let delta_e = new_energy - energy;
                        state[i] = !state[i]; // Restore for now

                        let accept = if delta_e <= 0.0 {
                            true
                        } else {
                            let threshold = 1.0 / (1.0 + (beta_new * delta_e).exp());
                            rng.random_range(0.0f64..1.0f64) < threshold
                        };

                        if accept {
                            state[i] = !state[i];
                            energy = new_energy;
                        }
                    }
                }
                energies[r] = energy;
            }

            let log_weights: Vec<f64> = energies.iter().map(|&e| -delta_beta * e).collect();
            let max_log_w = log_weights
                .iter()
                .cloned()
                .fold(f64::NEG_INFINITY, f64::max);

            let weights: Vec<f64> = log_weights
                .iter()
                .map(|&lw| (lw - max_log_w).exp())
                .collect();

            let sum_w: f64 = weights.iter().sum();
            let sum_w2: f64 = weights.iter().map(|&w| w * w).sum();

            let ess = if sum_w2 > 0.0 {
                sum_w * sum_w / sum_w2
            } else {
                0.0
            };

            if (ess / pop_size as f64) < resample_threshold {
                let new_population =
                    Self::multinomial_resample(&population, &weights, pop_size, &mut rng);
                let new_energies: Vec<f64> = new_population
                    .iter()
                    .map(|s| Self::evaluate_hobo_energy_dyn(tensor, s, n_vars))
                    .collect();
                population = new_population;
                energies = new_energies;
            }

            beta_prev = beta_new;
        }

        let pop_size_actual = population.len();
        (0..shots)
            .map(|i| {
                let idx = i % pop_size_actual;
                (population[idx].clone(), energies[idx])
            })
            .collect()
    }

    /// Run generic sampler
    fn run_generic<D>(
        &self,
        matrix_or_tensor: &Array<f64, D>,
        var_map: &HashMap<String, usize>,
        shots: usize,
    ) -> SamplerResult<Vec<SampleResult>>
    where
        D: scirs2_core::ndarray::Dimension + 'static,
    {
        let shots = shots.max(1);
        let n_vars = var_map.len();
        if n_vars == 0 {
            return Err(SamplerError::InvalidParameter(
                "Variable map is empty".to_string(),
            ));
        }

        if self.params.beta_schedule.is_empty() {
            return Err(SamplerError::InvalidParameter(
                "Beta schedule is empty".to_string(),
            ));
        }

        let idx_to_var: HashMap<usize, String> = var_map
            .iter()
            .map(|(var, &idx)| (idx, var.clone()))
            .collect();

        // Determine seed for this run
        let run_seed = match self.seed {
            Some(s) => s,
            None => {
                let mut rng_tmp = thread_rng();
                rng_tmp.random()
            }
        };

        // Run population annealing
        let raw_results = if matrix_or_tensor.ndim() == 2 {
            let q2 = matrix_or_tensor
                .to_owned()
                .into_dimensionality::<Ix2>()
                .map_err(|e| SamplerError::InvalidParameter(format!("Array cast error: {e}")))?;

            let n = q2.dim().0;
            if n != q2.dim().1 {
                return Err(SamplerError::InvalidParameter(
                    "QUBO matrix must be square".to_string(),
                ));
            }

            let q_flat: Vec<f64> = q2
                .as_slice()
                .ok_or_else(|| {
                    SamplerError::InvalidParameter("Non-contiguous QUBO matrix".to_string())
                })?
                .to_vec();

            self.run_pa_qubo(&q_flat, n, shots, run_seed)
        } else {
            self.run_pa_hobo(matrix_or_tensor, n_vars, shots, run_seed)
        };

        if raw_results.is_empty() {
            return Ok(vec![]);
        }

        // Aggregate results: count occurrences of identical states
        let mut solution_counts: HashMap<Vec<bool>, (f64, usize)> = HashMap::new();
        for (state, energy) in raw_results {
            let entry = solution_counts.entry(state).or_insert((energy, 0));
            entry.1 += 1;
        }

        // Sort by (energy, state) for deterministic ordering when energies are equal
        let mut pairs: Vec<(Vec<bool>, SampleResult)> = solution_counts
            .into_iter()
            .map(|(state, (energy, count))| {
                let assignments: HashMap<String, bool> = state
                    .iter()
                    .enumerate()
                    .filter_map(|(idx, &value)| {
                        idx_to_var.get(&idx).map(|name| (name.clone(), value))
                    })
                    .collect();
                let result = SampleResult {
                    assignments,
                    energy,
                    occurrences: count,
                };
                (state, result)
            })
            .collect();

        pairs.sort_by(|(state_a, a), (state_b, b)| {
            a.energy
                .partial_cmp(&b.energy)
                .unwrap_or(std::cmp::Ordering::Equal)
                .then_with(|| state_a.cmp(state_b))
        });

        let results: Vec<SampleResult> = pairs.into_iter().map(|(_, r)| r).collect();
        Ok(results)
    }
}

impl Sampler for PopulationAnnealingSampler {
    fn run_qubo(
        &self,
        qubo: &(Array<f64, Ix2>, HashMap<String, usize>),
        shots: usize,
    ) -> SamplerResult<Vec<SampleResult>> {
        self.run_generic(&qubo.0, &qubo.1, shots)
    }

    fn run_hobo(
        &self,
        hobo: &(ArrayD<f64>, HashMap<String, usize>),
        shots: usize,
    ) -> SamplerResult<Vec<SampleResult>> {
        self.run_generic(&hobo.0, &hobo.1, shots)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::ndarray::Array2;

    /// Build K3 Max-Cut QUBO matrix.
    /// Optimal energy: -2.0
    fn build_k3_maxcut_qubo() -> (Array2<f64>, HashMap<String, usize>) {
        let mut q = Array2::<f64>::zeros((3, 3));
        q[[0, 0]] = -2.0;
        q[[1, 1]] = -2.0;
        q[[2, 2]] = -2.0;
        q[[0, 1]] = 2.0;
        q[[0, 2]] = 2.0;
        q[[1, 2]] = 2.0;

        let mut var_map = HashMap::new();
        var_map.insert("x0".to_string(), 0);
        var_map.insert("x1".to_string(), 1);
        var_map.insert("x2".to_string(), 2);

        (q, var_map)
    }

    #[test]
    fn test_pa_3var_maxcut() {
        let (q, var_map) = build_k3_maxcut_qubo();
        let sampler = PopulationAnnealingSampler::new()
            .with_seed(42)
            .with_population(50)
            .with_sweeps_per_step(3);

        let results = sampler
            .run_qubo(&(q, var_map), 50)
            .expect("PA run_qubo failed");

        assert!(!results.is_empty(), "Expected non-empty results");
        let best_energy = results[0].energy;
        assert!(
            best_energy <= -2.0 + 1e-9,
            "Expected optimal energy <= -2.0, got {best_energy}"
        );
    }

    #[test]
    fn test_pa_determinism() {
        let (q, var_map) = build_k3_maxcut_qubo();

        let s1 = PopulationAnnealingSampler::new()
            .with_seed(42)
            .with_population(20);
        let s2 = PopulationAnnealingSampler::new()
            .with_seed(42)
            .with_population(20);

        let r1 = s1
            .run_qubo(&(q.clone(), var_map.clone()), 10)
            .expect("Run 1 failed");
        let r2 = s2.run_qubo(&(q, var_map), 10).expect("Run 2 failed");

        assert_eq!(r1.len(), r2.len(), "Result lengths differ");
        for (a, b) in r1.iter().zip(r2.iter()) {
            assert!(
                (a.energy - b.energy).abs() < 1e-12,
                "Energies differ: {} vs {}",
                a.energy,
                b.energy
            );
            assert_eq!(
                a.assignments, b.assignments,
                "Assignments differ for same seed"
            );
        }
    }

    #[test]
    fn test_pa_hobo_smoke() {
        // Simple 2D HOBO smoke test
        let mut q = Array2::<f64>::zeros((2, 2));
        q[[0, 0]] = -1.0;
        q[[1, 1]] = -1.0;

        let mut var_map = HashMap::new();
        var_map.insert("a".to_string(), 0);
        var_map.insert("b".to_string(), 1);

        let sampler = PopulationAnnealingSampler::new()
            .with_seed(7)
            .with_population(20);
        let q_dyn = q.into_dyn();
        let results = sampler
            .run_hobo(&(q_dyn, var_map), 10)
            .expect("HOBO PA run failed");

        assert!(!results.is_empty());
        assert!(results[0].energy <= -2.0 + 1e-9);
    }

    #[test]
    fn test_pa_results_sorted_ascending() {
        let (q, var_map) = build_k3_maxcut_qubo();
        let sampler = PopulationAnnealingSampler::new()
            .with_seed(5)
            .with_population(30);

        let results = sampler.run_qubo(&(q, var_map), 30).expect("PA run failed");

        // Verify ascending energy order
        for window in results.windows(2) {
            assert!(
                window[0].energy <= window[1].energy + 1e-12,
                "Results not sorted: {} > {}",
                window[0].energy,
                window[1].energy
            );
        }
    }

    #[test]
    fn test_pa_custom_schedule() {
        let (q, var_map) = build_k3_maxcut_qubo();
        // Coarser beta schedule
        let betas: Vec<f64> = (0..10).map(|i| 0.2 + 2.8 * i as f64 / 9.0).collect();
        let sampler = PopulationAnnealingSampler::new()
            .with_seed(42)
            .with_population(30)
            .with_beta_schedule(betas);

        let results = sampler
            .run_qubo(&(q, var_map), 20)
            .expect("PA custom schedule failed");

        assert!(!results.is_empty());
        // Should still find a reasonable solution
        assert!(results[0].energy <= 0.0 + 1e-9);
    }
}