quantrs2-device 0.2.1

Quantum device connectors 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
//! Fallback implementations for SciRS2 functionality when the feature is not available
//!
//! This module provides basic implementations of SciRS2 functions that are used
//! in the performance dashboard module when the scirs2 feature is not enabled.

use scirs2_core::ndarray::{Array1, Array2};
use std::collections::HashMap;

/// Fallback error type for optimization
#[derive(Debug, Clone)]
pub struct OptimizeError {
    pub message: String,
}

impl std::fmt::Display for OptimizeError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Optimization error: {}", self.message)
    }
}

impl std::error::Error for OptimizeError {}

/// Fallback result type for optimization
pub type OptimizeResult<T> = Result<T, OptimizeError>;

/// Fallback linear algebra error
#[derive(Debug, Clone)]
pub struct LinalgError {
    pub message: String,
}

impl std::fmt::Display for LinalgError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Linear algebra error: {}", self.message)
    }
}

impl std::error::Error for LinalgError {}

/// Fallback result type for linear algebra
pub type LinalgResult<T> = Result<T, LinalgError>;

/// Alternative enum for statistical tests
#[derive(Debug, Clone, Copy)]
pub enum Alternative {
    TwoSided,
    Less,
    Greater,
}

/// Basic statistics functions
pub fn mean(data: &[f64]) -> f64 {
    if data.is_empty() {
        return 0.0;
    }
    data.iter().sum::<f64>() / data.len() as f64
}

pub fn std(data: &[f64]) -> f64 {
    if data.len() < 2 {
        return 0.0;
    }
    let m = mean(data);
    let variance = data.iter().map(|x| (x - m).powi(2)).sum::<f64>() / (data.len() - 1) as f64;
    variance.sqrt()
}

pub fn var(data: &[f64]) -> f64 {
    if data.len() < 2 {
        return 0.0;
    }
    let m = mean(data);
    data.iter().map(|x| (x - m).powi(2)).sum::<f64>() / (data.len() - 1) as f64
}

pub fn corrcoef(x: &[f64], y: &[f64]) -> f64 {
    pearsonr(x, y)
}

pub fn pearsonr(x: &[f64], y: &[f64]) -> f64 {
    if x.len() != y.len() || x.len() < 2 {
        return 0.0;
    }

    let mean_x = mean(x);
    let mean_y = mean(y);

    let numerator: f64 = x
        .iter()
        .zip(y.iter())
        .map(|(xi, yi)| (xi - mean_x) * (yi - mean_y))
        .sum();

    let sum_sq_x: f64 = x.iter().map(|xi| (xi - mean_x).powi(2)).sum();
    let sum_sq_y: f64 = y.iter().map(|yi| (yi - mean_y).powi(2)).sum();

    let denominator = (sum_sq_x * sum_sq_y).sqrt();

    if denominator == 0.0 {
        0.0
    } else {
        numerator / denominator
    }
}

pub fn spearmanr(x: &[f64], y: &[f64]) -> f64 {
    // Simplified Spearman correlation - just return Pearson for fallback
    pearsonr(x, y)
}

/// Fallback optimization function
pub fn minimize<F>(
    _objective: F,
    _initial_guess: &[f64],
    _bounds: Option<&[(f64, f64)]>,
) -> OptimizeResult<MinimizeResult>
where
    F: Fn(&[f64]) -> f64,
{
    // Basic fallback - return the initial guess as "optimal"
    Ok(MinimizeResult {
        x: _initial_guess.to_vec(),
        fun: 0.0,
        success: true,
        message: "Fallback optimization".to_string(),
        nit: 0,
        nfev: 0,
    })
}

/// Result type for minimize function
#[derive(Debug, Clone)]
pub struct MinimizeResult {
    pub x: Vec<f64>,
    pub fun: f64,
    pub success: bool,
    pub message: String,
    pub nit: usize,
    pub nfev: usize,
}

/// Real symmetric eigensolver using the cyclic Jacobi algorithm (pure Rust
/// fallback for when the `scirs2` feature is disabled).
///
/// Assumes the input is **symmetric**; it is defensively symmetrized as
/// `(A + Aáµ€) / 2`. Returns `(eigenvalues, eigenvectors)` with ascending
/// eigenvalues and orthonormal eigenvector columns. Non-square input is an error.
pub fn eig(matrix: &Array2<f64>) -> LinalgResult<(Array1<f64>, Array2<f64>)> {
    let (rows, cols) = matrix.dim();
    if rows != cols {
        return Err(LinalgError {
            message: format!("eig requires a square matrix, got {rows}x{cols}"),
        });
    }
    jacobi_symmetric_eig(matrix).map_err(|message| LinalgError { message })
}

/// Real SVD via the eigendecomposition of `Aáµ€A` (pure Rust fallback).
///
/// Computes right singular vectors `V` and singular values
/// `σ = sqrt(eig(AᵀA))` (clamped non-negative, sorted descending), then
/// recovers `U[:, i] = A V[:, i] / σ_i` with an orthonormal completion for
/// numerically-zero singular values. Returns `(U, S, Vt)`.
pub fn svd(matrix: &Array2<f64>) -> LinalgResult<(Array2<f64>, Array1<f64>, Array2<f64>)> {
    let (m, n) = matrix.dim();
    if m == 0 || n == 0 {
        return Err(LinalgError {
            message: "svd requires a non-empty matrix".to_string(),
        });
    }

    let mut ata = Array2::<f64>::zeros((n, n));
    for i in 0..n {
        for j in i..n {
            let mut acc = 0.0;
            for k in 0..m {
                acc += matrix[(k, i)] * matrix[(k, j)];
            }
            ata[(i, j)] = acc;
            ata[(j, i)] = acc;
        }
    }

    let (eigenvalues, eigenvectors) =
        jacobi_symmetric_eig(&ata).map_err(|message| LinalgError { message })?;

    let mut order: Vec<usize> = (0..n).collect();
    order.sort_by(|&a, &b| eigenvalues[b].total_cmp(&eigenvalues[a]));

    let mut singular_values = Array1::<f64>::zeros(n);
    let mut v = Array2::<f64>::zeros((n, n));
    for (new_idx, &old_idx) in order.iter().enumerate() {
        singular_values[new_idx] = eigenvalues[old_idx].max(0.0).sqrt();
        for row in 0..n {
            v[(row, new_idx)] = eigenvectors[(row, old_idx)];
        }
    }

    let mut u = Array2::<f64>::zeros((m, m));
    let k = m.min(n);
    let max_sigma = singular_values.iter().cloned().fold(0.0_f64, f64::max);
    let tol = max_sigma * (m.max(n) as f64) * f64::EPSILON;

    let mut filled = vec![false; m];
    for i in 0..k {
        let sigma = singular_values[i];
        if sigma > tol {
            for r in 0..m {
                let mut acc = 0.0;
                for c in 0..n {
                    acc += matrix[(r, c)] * v[(c, i)];
                }
                u[(r, i)] = acc / sigma;
            }
            filled[i] = true;
        }
    }
    complete_orthonormal_basis(&mut u, &filled);

    let vt = v.t().to_owned();
    Ok((u, singular_values, vt))
}

pub fn matrix_norm(matrix: &Array2<f64>) -> f64 {
    // Frobenius norm
    matrix.iter().map(|x| x * x).sum::<f64>().sqrt()
}

/// Real matrix inverse via Gauss-Jordan elimination with partial pivoting
/// (pure Rust fallback). Returns an honest error on non-square or singular
/// matrices.
pub fn inv(matrix: &Array2<f64>) -> LinalgResult<Array2<f64>> {
    let n = matrix.nrows();
    if n != matrix.ncols() {
        return Err(LinalgError {
            message: "Matrix must be square".to_string(),
        });
    }
    gauss_jordan_inverse(matrix).map_err(|message| LinalgError { message })
}

/// Determinant via LU decomposition with partial pivoting (pure Rust fallback).
/// Returns 0.0 for singular or non-square matrices.
pub fn det(matrix: &Array2<f64>) -> f64 {
    let n = matrix.nrows();
    if n != matrix.ncols() || n == 0 {
        return 0.0;
    }

    let mut a = matrix.clone();
    let mut determinant = 1.0;

    for col in 0..n {
        // Partial pivot: largest magnitude entry in this column.
        let mut pivot_row = col;
        let mut pivot_val = a[(col, col)].abs();
        for r in (col + 1)..n {
            let v = a[(r, col)].abs();
            if v > pivot_val {
                pivot_val = v;
                pivot_row = r;
            }
        }
        if pivot_val <= f64::MIN_POSITIVE {
            return 0.0;
        }
        if pivot_row != col {
            for c in 0..n {
                a.swap((col, c), (pivot_row, c));
            }
            determinant = -determinant;
        }

        determinant *= a[(col, col)];
        let pivot = a[(col, col)];
        for r in (col + 1)..n {
            let factor = a[(r, col)] / pivot;
            for c in col..n {
                let v = a[(col, c)];
                a[(r, c)] -= factor * v;
            }
        }
    }

    determinant
}

/// Cyclic Jacobi eigenvalue algorithm for real symmetric matrices. Returns
/// `(eigenvalues, eigenvectors)` ascending with orthonormal eigenvector
/// columns. The input is defensively symmetrized as `(A + Aáµ€) / 2`.
fn jacobi_symmetric_eig(matrix: &Array2<f64>) -> Result<(Array1<f64>, Array2<f64>), String> {
    let n = matrix.nrows();
    if n != matrix.ncols() {
        return Err("jacobi_symmetric_eig requires a square matrix".to_string());
    }
    if n == 0 {
        return Ok((Array1::zeros(0), Array2::zeros((0, 0))));
    }

    let mut a = Array2::<f64>::zeros((n, n));
    for i in 0..n {
        for j in 0..n {
            a[(i, j)] = 0.5 * (matrix[(i, j)] + matrix[(j, i)]);
        }
    }

    let mut eigenvectors = Array2::<f64>::eye(n);
    if n == 1 {
        return Ok((Array1::from_vec(vec![a[(0, 0)]]), eigenvectors));
    }

    let max_sweeps = 100;
    for _ in 0..max_sweeps {
        let mut off = 0.0;
        for p in 0..n {
            for q in (p + 1)..n {
                off += a[(p, q)] * a[(p, q)];
            }
        }
        if off <= f64::EPSILON * f64::EPSILON {
            break;
        }

        for p in 0..n {
            for q in (p + 1)..n {
                let apq = a[(p, q)];
                if apq.abs() <= f64::MIN_POSITIVE {
                    continue;
                }
                let app = a[(p, p)];
                let aqq = a[(q, q)];
                let tau = (aqq - app) / (2.0 * apq);
                let t = if tau >= 0.0 {
                    1.0 / (tau + (1.0 + tau * tau).sqrt())
                } else {
                    -1.0 / (-tau + (1.0 + tau * tau).sqrt())
                };
                let c = 1.0 / (1.0 + t * t).sqrt();
                let s = t * c;

                for k in 0..n {
                    let akp = a[(k, p)];
                    let akq = a[(k, q)];
                    a[(k, p)] = c * akp - s * akq;
                    a[(k, q)] = s * akp + c * akq;
                }
                for k in 0..n {
                    let apk = a[(p, k)];
                    let aqk = a[(q, k)];
                    a[(p, k)] = c * apk - s * aqk;
                    a[(q, k)] = s * apk + c * aqk;
                }
                for k in 0..n {
                    let vkp = eigenvectors[(k, p)];
                    let vkq = eigenvectors[(k, q)];
                    eigenvectors[(k, p)] = c * vkp - s * vkq;
                    eigenvectors[(k, q)] = s * vkp + c * vkq;
                }
            }
        }
    }

    let mut eigenvalues = Array1::<f64>::zeros(n);
    for i in 0..n {
        eigenvalues[i] = a[(i, i)];
    }

    let mut order: Vec<usize> = (0..n).collect();
    order.sort_by(|&a_idx, &b_idx| eigenvalues[a_idx].total_cmp(&eigenvalues[b_idx]));

    let mut sorted_values = Array1::<f64>::zeros(n);
    let mut sorted_vectors = Array2::<f64>::zeros((n, n));
    for (new_idx, &old_idx) in order.iter().enumerate() {
        sorted_values[new_idx] = eigenvalues[old_idx];
        for row in 0..n {
            sorted_vectors[(row, new_idx)] = eigenvectors[(row, old_idx)];
        }
    }

    Ok((sorted_values, sorted_vectors))
}

/// Gauss-Jordan matrix inversion with partial pivoting. Returns an error if the
/// matrix is singular.
fn gauss_jordan_inverse(matrix: &Array2<f64>) -> Result<Array2<f64>, String> {
    let n = matrix.nrows();
    if n == 0 {
        return Ok(Array2::zeros((0, 0)));
    }

    // Augmented working copy [A | I].
    let mut a = matrix.clone();
    let mut inv = Array2::<f64>::eye(n);

    for col in 0..n {
        // Partial pivot.
        let mut pivot_row = col;
        let mut pivot_val = a[(col, col)].abs();
        for r in (col + 1)..n {
            let v = a[(r, col)].abs();
            if v > pivot_val {
                pivot_val = v;
                pivot_row = r;
            }
        }
        if pivot_val <= f64::MIN_POSITIVE {
            return Err("singular matrix: cannot invert".to_string());
        }
        if pivot_row != col {
            for c in 0..n {
                a.swap((col, c), (pivot_row, c));
                inv.swap((col, c), (pivot_row, c));
            }
        }

        // Normalize the pivot row.
        let pivot = a[(col, col)];
        for c in 0..n {
            a[(col, c)] /= pivot;
            inv[(col, c)] /= pivot;
        }

        // Eliminate the pivot column from all other rows.
        for r in 0..n {
            if r == col {
                continue;
            }
            let factor = a[(r, col)];
            if factor == 0.0 {
                continue;
            }
            for c in 0..n {
                let a_val = a[(col, c)];
                let inv_val = inv[(col, c)];
                a[(r, c)] -= factor * a_val;
                inv[(r, c)] -= factor * inv_val;
            }
        }
    }

    Ok(inv)
}

/// Fill unfilled columns of `u` with an orthonormal completion via modified
/// Gram-Schmidt against the canonical basis.
fn complete_orthonormal_basis(u: &mut Array2<f64>, filled: &[bool]) {
    let m = u.nrows();
    let ncols = u.ncols();
    let mut next_canonical = 0usize;
    for col in 0..ncols {
        if col < filled.len() && filled[col] {
            continue;
        }
        loop {
            if next_canonical >= m {
                break;
            }
            let mut candidate = Array1::<f64>::zeros(m);
            candidate[next_canonical] = 1.0;
            next_canonical += 1;

            for prev in 0..col {
                let mut dot = 0.0;
                for r in 0..m {
                    dot += candidate[r] * u[(r, prev)];
                }
                for r in 0..m {
                    candidate[r] -= dot * u[(r, prev)];
                }
            }

            let norm = candidate.iter().map(|x| x * x).sum::<f64>().sqrt();
            if norm > 1e-12 {
                for r in 0..m {
                    u[(r, col)] = candidate[r] / norm;
                }
                break;
            }
        }
    }
}

/// Distribution modules
pub mod distributions {
    use super::*;

    pub struct Normal {
        pub mean: f64,
        pub std: f64,
    }

    impl Normal {
        pub fn new(mean: f64, std: f64) -> Self {
            Self { mean, std }
        }

        pub fn pdf(&self, x: f64) -> f64 {
            let z = (x - self.mean) / self.std;
            (-0.5 * z * z).exp() / (self.std * (2.0 * std::f64::consts::PI).sqrt())
        }

        pub fn cdf(&self, x: f64) -> f64 {
            // Simplified CDF approximation
            0.5 * (1.0 + ((x - self.mean) / (self.std * 2.0_f64.sqrt())).tanh())
        }
    }

    pub fn norm(mean: f64, std: f64) -> Normal {
        Normal::new(mean, std)
    }

    pub fn gamma(_shape: f64, _scale: f64) -> Normal {
        Normal::new(1.0, 1.0) // Fallback to normal
    }

    pub fn chi2(_df: f64) -> Normal {
        Normal::new(1.0, 1.0) // Fallback to normal
    }

    pub fn beta(_a: f64, _b: f64) -> Normal {
        Normal::new(0.5, 0.1) // Fallback to normal
    }

    pub fn uniform(_low: f64, _high: f64) -> Normal {
        Normal::new(0.0, 1.0) // Fallback to standard normal
    }
}

/// Graph-related fallback functions
#[derive(Debug, Clone)]
pub struct Graph<N, E> {
    nodes: Vec<N>,
    edges: Vec<(usize, usize, E)>,
}

impl<N, E> Graph<N, E> {
    pub fn new() -> Self {
        Self {
            nodes: Vec::new(),
            edges: Vec::new(),
        }
    }

    pub fn add_node(&mut self, node: N) -> usize {
        self.nodes.push(node);
        self.nodes.len() - 1
    }

    pub fn add_edge(&mut self, a: usize, b: usize, edge: E) {
        self.edges.push((a, b, edge));
    }

    pub fn nodes(&self) -> impl Iterator<Item = &N> {
        self.nodes.iter()
    }

    pub fn node_count(&self) -> usize {
        self.nodes.len()
    }

    pub fn edge_count(&self) -> usize {
        self.edges.len()
    }
}

pub fn shortest_path<N, E>(_graph: &Graph<N, E>, _start: usize, _end: usize) -> Option<Vec<usize>> {
    None // Fallback - no path found
}

pub fn betweenness_centrality<N, E>(
    _graph: &Graph<N, E>,
    _normalized: bool,
) -> HashMap<usize, f64> {
    HashMap::new() // Fallback - empty centrality
}

pub fn closeness_centrality<N, E>(_graph: &Graph<N, E>, _normalized: bool) -> HashMap<usize, f64> {
    HashMap::new() // Fallback - empty centrality
}

pub fn minimum_spanning_tree<N, E>(_graph: &Graph<N, E>) -> Vec<(usize, usize)> {
    Vec::new() // Fallback - empty MST
}

pub fn strongly_connected_components<N, E>(_graph: &Graph<N, E>) -> Vec<Vec<usize>> {
    Vec::new() // Fallback - no components
}

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

    fn approx(a: f64, b: f64, tol: f64) -> bool {
        (a - b).abs() <= tol
    }

    #[test]
    fn test_eig_diagonal() {
        let m = array![[3.0, 0.0], [0.0, 1.0]];
        let (vals, _vecs) = eig(&m).expect("eig should succeed");
        assert!(approx(vals[0], 1.0, 1e-9));
        assert!(approx(vals[1], 3.0, 1e-9));
    }

    #[test]
    fn test_eig_non_square_errors() {
        let m = array![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
        assert!(eig(&m).is_err());
    }

    #[test]
    fn test_inv_identity_property_2x2() {
        let a = array![[4.0, 7.0], [2.0, 6.0]];
        let a_inv = inv(&a).expect("inverse should succeed");
        // A * A^-1 == I.
        for r in 0..2 {
            for c in 0..2 {
                let mut acc = 0.0;
                for k in 0..2 {
                    acc += a[(r, k)] * a_inv[(k, c)];
                }
                let expected = if r == c { 1.0 } else { 0.0 };
                assert!(approx(acc, expected, 1e-6), "[{r},{c}]={acc}");
            }
        }
    }

    #[test]
    fn test_inv_identity_property_3x3() {
        let a = array![[2.0, 1.0, 1.0], [1.0, 3.0, 2.0], [1.0, 0.0, 0.0]];
        let a_inv = inv(&a).expect("inverse should succeed");
        for r in 0..3 {
            for c in 0..3 {
                let mut acc = 0.0;
                for k in 0..3 {
                    acc += a[(r, k)] * a_inv[(k, c)];
                }
                let expected = if r == c { 1.0 } else { 0.0 };
                assert!(approx(acc, expected, 1e-6), "[{r},{c}]={acc}");
            }
        }
    }

    #[test]
    fn test_inv_singular_errors() {
        // Rank-deficient matrix has no inverse.
        let a = array![[1.0, 2.0], [2.0, 4.0]];
        assert!(inv(&a).is_err());
    }

    #[test]
    fn test_inv_non_square_errors() {
        let a = array![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
        assert!(inv(&a).is_err());
    }

    #[test]
    fn test_det_known() {
        let a = array![[4.0, 7.0], [2.0, 6.0]];
        // det = 4*6 - 7*2 = 10.
        assert!(approx(det(&a), 10.0, 1e-9));
        let singular = array![[1.0, 2.0], [2.0, 4.0]];
        assert!(approx(det(&singular), 0.0, 1e-9));
    }

    #[test]
    fn test_svd_reconstruction() {
        let a = array![[3.0, 1.0], [1.0, 3.0], [0.0, 2.0]];
        let (u, s, vt) = svd(&a).expect("svd should succeed");
        let (m, n) = a.dim();
        for r in 0..m {
            for c in 0..n {
                let mut recon = 0.0;
                for k in 0..n.min(m) {
                    recon += u[(r, k)] * s[k] * vt[(k, c)];
                }
                assert!(approx(recon, a[(r, c)], 1e-6), "[{r},{c}]={recon}");
            }
        }
    }
}