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
//! Samplers for solving QUBO/HOBO problems.
//!
//! QuantRS2 Tytan provides five pure-Rust metaheuristic samplers for solving
//! QUBO (Quadratic Unconstrained Binary Optimization) and HOBO (Higher-Order
//! Binary Optimization) problems. All samplers implement the [`Sampler`] trait,
//! which exposes a uniform `run_qubo` / `run_hobo` interface.
//!
//! The [`energy`] submodule provides SIMD-accelerated QUBO energy evaluation
//! functions that serve as the shared inner loop for all samplers.
//!
//! # Sampler Comparison
//!
//! | Sampler | Type | Strengths | Typical problem size | Citation |
//! |---------|------|-----------|----------------------|----------|
//! | [`SASampler`] | Local search | Versatile, easy to tune | Small–large | Kirkpatrick 1983 |
//! | [`GASampler`] | Evolutionary | Population diversity | Medium | Holland 1975 |
//! | [`TabuSampler`] | Tabu search | Structured combinatorial | Small–medium | Glover 1989 |
//! | [`SBSampler`] | Bifurcation dynamics | Large dense QUBO | Large | Goto 2019 |
//! | [`PopulationAnnealingSampler`] | Population annealing | Ensemble, near-ground-state | Medium–large | Hukushima 2003 |
//!
//! # Choosing a Sampler
//!
//! - **Start with [`SASampler`]** for most problems. It is robust, has sensible
//! defaults, and handles both small and large instances.
//! - **Use [`TabuSampler`]** for scheduling, routing, or other problems that have
//! a structured neighbourhood where recently-visited moves should be forbidden.
//! - **Use [`SBSampler`]** for large, dense QUBO matrices (n ≥ 200) where SA is
//! too slow. The ballistic (`SBVariant::Ballistic`) variant converges faster;
//! discrete (`SBVariant::Discrete`) gives crisper spin assignments.
//! - **Use [`PopulationAnnealingSampler`]** when you need an ensemble of
//! near-ground-state solutions or when the problem has a complex low-energy
//! landscape and you want thermodynamic statistics.
//! - **Use [`GASampler`]** when the problem structure maps naturally to bitstring
//! chromosomes and crossover is likely to be productive (e.g., scheduling
//! problems with independent sub-problems).
//!
//! # Quick Example
//!
//! The following example solves a 3-variable Max-Cut QUBO (K3 graph) with the
//! Simulated Annealing sampler:
//!
//! ```
//! use quantrs2_tytan::sampler::{SASampler, Sampler};
//! use scirs2_core::ndarray::Array;
//! use std::collections::HashMap;
//!
//! // K3 Max-Cut: minimise -x0 - x1 - x2 + 2*x0*x1 + 2*x0*x2 + 2*x1*x2
//! 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);
//!
//! let sampler = SASampler::new(Some(42));
//! let samples = sampler.run_qubo(&(q, var_map), 5).expect("SA sampler failed");
//! assert!(!samples.is_empty());
//! // Best solution is at index 0 (sorted by energy ascending)
//! println!("Best energy: {}", samples[0].energy);
//! ```
use Array;
use HashMap;
pub use ;
/// A sample result from a QUBO/HOBO problem
///
/// This struct represents a single sample result from a QUBO/HOBO
/// problem, including the variable assignments, energy, and occurrence count.
/// Trait for samplers that can solve QUBO/HOBO problems
/// Evaluate energy for a QUBO problem given a binary state vector
///
/// # Arguments
///
/// * `state` - Binary state vector (solution)
/// * `h_vector` - Linear coefficients (diagonal)
/// * `j_matrix` - Quadratic coefficients (n_vars x n_vars matrix in flattened form)
/// * `n_vars` - Number of variables
///
/// # Returns
///
/// The energy (objective function value) for the given state
pub
// Re-export main sampler implementations
pub use GASampler;
pub use ArminSampler;
pub use ;
pub use PopulationAnnealingSampler;
pub use SASampler;
pub use ;
pub use TabuSampler;
// Re-export energy module public interface
pub use ;