quantrs2_device/mid_circuit_measurements/
fallback.rs

1//! Fallback implementations when SciRS2 is not available
2
3use scirs2_core::ndarray::{Array1, Array2, ArrayView1};
4
5/// Fallback mean calculation
6pub fn mean(data: &ArrayView1<f64>) -> Result<f64, String> {
7    Ok(data.mean().unwrap_or(0.0))
8}
9
10/// Fallback standard deviation calculation
11pub fn std(data: &ArrayView1<f64>, _ddof: i32) -> Result<f64, String> {
12    Ok(data.std(1.0))
13}
14
15/// Fallback Pearson correlation calculation
16pub fn pearsonr(
17    x: &ArrayView1<f64>,
18    y: &ArrayView1<f64>,
19    _alternative: &str,
20) -> Result<(f64, f64), String> {
21    if x.len() != y.len() || x.len() < 2 {
22        return Ok((0.0, 0.5));
23    }
24
25    let x_mean = x.mean().unwrap_or(0.0);
26    let y_mean = y.mean().unwrap_or(0.0);
27
28    let mut num = 0.0;
29    let mut x_sum_sq = 0.0;
30    let mut y_sum_sq = 0.0;
31
32    for i in 0..x.len() {
33        let x_diff = x[i] - x_mean;
34        let y_diff = y[i] - y_mean;
35        num += x_diff * y_diff;
36        x_sum_sq += x_diff * x_diff;
37        y_sum_sq += y_diff * y_diff;
38    }
39
40    let denom = (x_sum_sq * y_sum_sq).sqrt();
41    let corr = if denom > 1e-10 { num / denom } else { 0.0 };
42
43    Ok((corr, 0.05)) // p-value placeholder
44}
45
46/// Fallback optimization function
47pub fn minimize(
48    _objective: fn(&[f64]) -> f64,
49    _x0: &[f64],
50    _bounds: Option<&[(f64, f64)]>,
51) -> Result<OptimizeResult, String> {
52    Ok(OptimizeResult {
53        x: vec![0.0; _x0.len()],
54        fun: 1.0,
55        success: true,
56        nit: 10,
57        message: "Fallback optimization".to_string(),
58    })
59}
60
61/// Fallback optimization result
62pub struct OptimizeResult {
63    pub x: Vec<f64>,
64    pub fun: f64,
65    pub success: bool,
66    pub nit: usize,
67    pub message: String,
68}
69
70/// Fallback linear regression implementation
71pub struct LinearRegression {
72    coefficients: Vec<f64>,
73    intercept: f64,
74}
75
76impl LinearRegression {
77    pub const fn new() -> Self {
78        Self {
79            coefficients: Vec::new(),
80            intercept: 0.0,
81        }
82    }
83
84    pub const fn fit(&mut self, _x: &Array2<f64>, _y: &Array1<f64>) -> Result<(), String> {
85        Ok(())
86    }
87
88    pub fn predict(&self, x: &Array2<f64>) -> Array1<f64> {
89        Array1::zeros(x.nrows())
90    }
91}
92
93impl Default for LinearRegression {
94    fn default() -> Self {
95        Self::new()
96    }
97}