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
//! Probability distributions over computational basis states.
//!
//! [`Probabilities`] serves dense vectors and lazily factored per-block
//! marginals through one interface.
/// Each block represents the marginal probabilities for one independent
/// subsystem. The `mask` indicates which global qubit positions belong
/// to this block, and `probs` holds the 2^k marginal distribution.
#[derive(Debug, Clone)]
pub struct FactoredBlock {
pub probs: Vec<f64>,
pub mask: u64,
}
/// For monolithic simulations this wraps a dense `Vec<f64>` of length 2^n.
/// For decomposed simulations with independent subsystems, this stores
/// per-block marginal distributions that are multiplied on demand,
/// avoiding the O(2^N) Kronecker product unless explicitly requested.
#[derive(Debug, Clone)]
pub enum Probabilities {
/// Full probability vector of length 2^n.
Dense(Vec<f64>),
/// Lazy Kronecker product of independent block distributions.
Factored {
blocks: Vec<FactoredBlock>,
total_qubits: usize,
},
}
impl Probabilities {
/// Number of basis states (2^n).
pub fn len(&self) -> usize {
match self {
Probabilities::Dense(v) => v.len(),
Probabilities::Factored { total_qubits, .. } => 1 << total_qubits,
}
}
/// Always false, a probability distribution has at least one state.
pub fn is_empty(&self) -> bool {
false
}
/// Compute per-qubit marginal probabilities from an existing joint distribution.
///
/// Returns `(P(0), P(1))` for each qubit. This is a view over already
/// materialized probability data. Query APIs can still choose faster direct
/// marginal algorithms before producing a joint distribution.
pub fn marginals(&self) -> Vec<(f64, f64)> {
match self {
Probabilities::Dense(v) => {
let n = v.len().ilog2() as usize;
let mut marginals = vec![(0.0f64, 0.0f64); n];
for (idx, &p) in v.iter().enumerate() {
for (q, m) in marginals.iter_mut().enumerate() {
if (idx >> q) & 1 == 0 {
m.0 += p;
} else {
m.1 += p;
}
}
}
marginals
}
Probabilities::Factored {
blocks,
total_qubits,
} => {
let mut marginals = vec![(0.0f64, 0.0f64); *total_qubits];
for block in blocks {
let mut qubits = Vec::new();
let mut mask = block.mask;
while mask != 0 {
qubits.push(mask.trailing_zeros() as usize);
mask &= mask.wrapping_sub(1);
}
for (idx, &p) in block.probs.iter().enumerate() {
for (local_bit, &qubit) in qubits.iter().enumerate() {
if (idx >> local_bit) & 1 == 0 {
marginals[qubit].0 += p;
} else {
marginals[qubit].1 += p;
}
}
}
}
marginals
}
}
}
/// Probability of a single computational basis state. O(1) for dense,
/// O(K) for factored where K is the number of independent blocks.
///
/// # Panics
/// Panics if `index >= self.len()`.
pub fn get(&self, index: usize) -> f64 {
match self {
Probabilities::Dense(v) => v[index],
Probabilities::Factored { blocks, .. } => {
let mut p = 1.0;
for block in blocks {
let local = extract_block_bits(index, block.mask);
p *= block.probs[local];
}
p
}
}
}
/// Iterate over all basis-state probabilities in order.
///
/// For `Dense` this is a direct slice iteration. For `Factored` each
/// probability is computed on the fly in O(K) per element.
pub fn iter(&self) -> ProbabilitiesIter<'_> {
match self {
Probabilities::Dense(v) => ProbabilitiesIter {
inner: ProbabilitiesIterInner::Dense(v.iter().copied()),
},
Probabilities::Factored {
blocks,
total_qubits,
} => ProbabilitiesIter {
inner: ProbabilitiesIterInner::Factored {
blocks,
next: 0,
len: 1usize << total_qubits,
},
},
}
}
/// Materialize the full probability vector. O(1) clone for dense,
/// O(K x 2^N) for factored. Prefer [`Probabilities::get`] for spot-checking.
pub fn to_vec(&self) -> Vec<f64> {
match self {
Probabilities::Dense(v) => v.clone(),
Probabilities::Factored {
blocks,
total_qubits,
} => {
let n = 1usize << total_qubits;
let mut result = vec![0.0f64; n];
#[cfg(feature = "parallel")]
{
const MIN_PAR_STATES: usize = 1 << 14;
if n >= MIN_PAR_STATES {
use rayon::prelude::*;
crate::backend::init_thread_pool();
result.par_iter_mut().enumerate().for_each(|(i, slot)| {
let mut p = 1.0;
for block in blocks {
let local = extract_block_bits(i, block.mask);
p *= block.probs[local];
}
*slot = p;
});
return result;
}
}
for (i, slot) in result.iter_mut().enumerate() {
let mut p = 1.0;
for block in blocks {
let local = extract_block_bits(i, block.mask);
p *= block.probs[local];
}
*slot = p;
}
result
}
}
}
}
impl std::ops::Index<usize> for Probabilities {
type Output = f64;
/// Index into a dense probability vector.
///
/// Only works for `Dense`. Panics on `Factored` because `Index` must
/// return `&f64` and factored values are computed, not stored.
/// Use [`Probabilities::get`] or [`Probabilities::iter`] instead.
fn index(&self, index: usize) -> &f64 {
match self {
Probabilities::Dense(v) => &v[index],
Probabilities::Factored { .. } => {
panic!("cannot index Factored probabilities; use .get(i) or .to_vec()")
}
}
}
}
/// Concrete iterator for [`Probabilities::iter`].
pub struct ProbabilitiesIter<'a> {
inner: ProbabilitiesIterInner<'a>,
}
enum ProbabilitiesIterInner<'a> {
Dense(std::iter::Copied<std::slice::Iter<'a, f64>>),
Factored {
blocks: &'a [FactoredBlock],
next: usize,
len: usize,
},
}
impl Iterator for ProbabilitiesIter<'_> {
type Item = f64;
fn next(&mut self) -> Option<Self::Item> {
match &mut self.inner {
ProbabilitiesIterInner::Dense(iter) => iter.next(),
ProbabilitiesIterInner::Factored { blocks, next, len } => {
if *next >= *len {
return None;
}
let index = *next;
*next += 1;
let mut p = 1.0;
for block in *blocks {
let local = extract_block_bits(index, block.mask);
p *= block.probs[local];
}
Some(p)
}
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
match &self.inner {
ProbabilitiesIterInner::Dense(iter) => iter.size_hint(),
ProbabilitiesIterInner::Factored { next, len, .. } => {
let remaining = len.saturating_sub(*next);
(remaining, Some(remaining))
}
}
}
}
impl ExactSizeIterator for ProbabilitiesIter<'_> {}
/// Extract the bits of `global_index` at positions set in `mask`,
/// packing them into contiguous low bits.
#[inline]
fn extract_block_bits(global_index: usize, mask: u64) -> usize {
#[cfg(target_arch = "x86_64")]
{
if is_x86_feature_detected!("bmi2") {
// SAFETY: BMI2 availability is checked immediately before this call.
return unsafe { core::arch::x86_64::_pext_u64(global_index as u64, mask) as usize };
}
}
let mut result = 0usize;
let mut bit = 0;
let mut m = mask;
while m != 0 {
let pos = m.trailing_zeros() as usize;
if global_index & (1 << pos) != 0 {
result |= 1 << bit;
}
bit += 1;
m &= m.wrapping_sub(1);
}
result
}
#[cfg(test)]
mod tests {
use super::*;
fn factored_2x3() -> Probabilities {
Probabilities::Factored {
blocks: vec![
FactoredBlock {
probs: vec![0.25, 0.75],
mask: 0b001,
},
FactoredBlock {
probs: vec![0.1, 0.2, 0.3, 0.4],
mask: 0b110,
},
],
total_qubits: 3,
}
}
#[test]
fn dense_basic_accessors() {
let p = Probabilities::Dense(vec![0.1, 0.2, 0.3, 0.4]);
assert_eq!(p.len(), 4);
assert!(!p.is_empty());
assert_eq!(p.get(2), 0.3);
assert_eq!(p[3], 0.4);
assert_eq!(p.to_vec(), vec![0.1, 0.2, 0.3, 0.4]);
let collected: Vec<f64> = p.iter().collect();
assert_eq!(collected, vec![0.1, 0.2, 0.3, 0.4]);
}
#[test]
fn factored_get_matches_to_vec() {
let p = factored_2x3();
assert_eq!(p.len(), 8);
let dense = p.to_vec();
for (i, d) in dense.iter().enumerate() {
assert!((p.get(i) - d).abs() < 1e-12);
}
let sum: f64 = dense.iter().sum();
assert!((sum - 1.0).abs() < 1e-12);
}
#[test]
fn factored_iter_matches_to_vec() {
let p = factored_2x3();
let dense = p.to_vec();
let iter_vec: Vec<f64> = p.iter().collect();
assert_eq!(iter_vec.len(), dense.len());
for (a, b) in iter_vec.iter().zip(dense.iter()) {
assert!((a - b).abs() < 1e-12);
}
}
#[test]
fn factored_iter_size_hint_exact() {
let p = factored_2x3();
let mut it = p.iter();
assert_eq!(it.size_hint(), (8, Some(8)));
assert_eq!(it.len(), 8);
it.next();
assert_eq!(it.size_hint(), (7, Some(7)));
for _ in 0..7 {
it.next();
}
assert_eq!(it.size_hint(), (0, Some(0)));
assert!(it.next().is_none());
}
#[test]
fn dense_iter_size_hint_exact() {
let p = Probabilities::Dense(vec![0.5, 0.5]);
let it = p.iter();
assert_eq!(it.size_hint(), (2, Some(2)));
}
#[test]
#[should_panic(expected = "cannot index Factored")]
fn factored_index_panics() {
let p = factored_2x3();
let _ = p[0];
}
#[test]
fn extract_block_bits_scalar_via_get() {
let blocks = vec![FactoredBlock {
probs: vec![0.0, 0.0, 0.0, 1.0],
mask: 0b1010,
}];
let p = Probabilities::Factored {
blocks,
total_qubits: 4,
};
assert!((p.get(0b1010) - 1.0).abs() < 1e-12);
assert!(p.get(0b0010).abs() < 1e-12);
assert!(p.get(0b1000).abs() < 1e-12);
}
#[cfg(feature = "parallel")]
#[test]
fn factored_to_vec_parallel_path() {
let mut blocks = Vec::new();
for i in 0..5 {
blocks.push(FactoredBlock {
probs: vec![0.125; 8],
mask: 0b111u64 << (3 * i),
});
}
let p = Probabilities::Factored {
blocks,
total_qubits: 15,
};
let v = p.to_vec();
assert_eq!(v.len(), 1 << 15);
let sum: f64 = v.iter().sum();
assert!((sum - 1.0).abs() < 1e-9);
for (i, val) in v.iter().enumerate() {
assert!((p.get(i) - val).abs() < 1e-12);
}
}
}