quantrs2-core 0.1.3

Core types and traits for the QuantRS2 quantum computing 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
//! Quantum Principal Component Analysis (qPCA)
//!
//! This module implements quantum principal component analysis for
//! efficient dimensionality reduction and eigenvalue estimation.

use crate::error::QuantRS2Error;
use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::Complex64;
use std::f64::consts::PI;

/// Parameters for quantum PCA
#[derive(Debug, Clone)]
pub struct QPCAParams {
    /// Number of precision qubits for phase estimation
    pub precision_qubits: usize,
    /// Number of samples for density matrix estimation
    pub num_samples: usize,
    /// Threshold for eigenvalue selection
    pub eigenvalue_threshold: f64,
    /// Maximum number of iterations for quantum phase estimation
    pub max_iterations: usize,
}

impl Default for QPCAParams {
    fn default() -> Self {
        Self {
            precision_qubits: 8,
            num_samples: 1000,
            eigenvalue_threshold: 0.01,
            max_iterations: 100,
        }
    }
}

/// Quantum Principal Component Analysis implementation
pub struct QuantumPCA {
    params: QPCAParams,
    data_matrix: Array2<f64>,
    density_matrix: Option<Array2<Complex64>>,
    eigenvalues: Option<Array1<f64>>,
    eigenvectors: Option<Array2<Complex64>>,
}

impl QuantumPCA {
    /// Create new qPCA instance with data matrix
    pub const fn new(data: Array2<f64>, params: QPCAParams) -> Self {
        Self {
            params,
            data_matrix: data,
            density_matrix: None,
            eigenvalues: None,
            eigenvectors: None,
        }
    }

    /// Compute the quantum density matrix from classical data
    pub fn compute_density_matrix(&mut self) -> Result<&Array2<Complex64>, QuantRS2Error> {
        let (n_samples, n_features) = (self.data_matrix.nrows(), self.data_matrix.ncols());

        // Center the data
        let mean = self
            .data_matrix
            .mean_axis(scirs2_core::ndarray::Axis(0))
            .ok_or_else(|| {
                QuantRS2Error::UnsupportedOperation("Failed to compute mean".to_string())
            })?;

        let centered_data = &self.data_matrix - &mean;

        // Compute covariance matrix
        let cov_matrix = centered_data.t().dot(&centered_data) / (n_samples - 1) as f64;

        // Convert to complex density matrix and normalize
        let mut density = Array2::<Complex64>::zeros((n_features, n_features));
        for i in 0..n_features {
            for j in 0..n_features {
                density[[i, j]] = Complex64::new(cov_matrix[[i, j]], 0.0);
            }
        }

        // Normalize to trace 1
        let trace: Complex64 = (0..n_features).map(|i| density[[i, i]]).sum();
        if trace.norm() > 1e-10 {
            density /= trace;
        }

        self.density_matrix = Some(density);
        self.density_matrix
            .as_ref()
            .ok_or(QuantRS2Error::UnsupportedOperation(
                "Failed to create density matrix".to_string(),
            ))
    }

    /// Quantum phase estimation for eigenvalue extraction
    pub fn quantum_phase_estimation(
        &self,
        unitary: &Array2<Complex64>,
        state: &Array1<Complex64>,
    ) -> Result<f64, QuantRS2Error> {
        let precision = self.params.precision_qubits;
        // let _n = 1 << precision;

        // Simulate quantum phase estimation
        // In a real quantum computer, this would use controlled-U operations
        let mut phase_estimate = 0.0;

        // Apply inverse QFT and measure
        for k in 0..precision {
            // Simplified simulation of controlled unitary application
            let controlled_phase = self.estimate_controlled_phase(unitary, state, 1 << k)?;
            phase_estimate += controlled_phase * (1.0 / (1 << (precision - k - 1)) as f64);
        }

        Ok(phase_estimate)
    }

    /// Estimate phase from controlled unitary (simplified simulation)
    fn estimate_controlled_phase(
        &self,
        unitary: &Array2<Complex64>,
        state: &Array1<Complex64>,
        power: usize,
    ) -> Result<f64, QuantRS2Error> {
        // Compute U^power
        let mut u_power = unitary.clone();
        for _ in 1..power {
            u_power = u_power.dot(unitary);
        }

        // Apply to state and extract phase
        let result = u_power.dot(state);
        let inner_product: Complex64 = state
            .iter()
            .zip(result.iter())
            .map(|(a, b)| a.conj() * b)
            .sum();

        Ok(inner_product.arg() / (2.0 * PI))
    }

    /// Extract principal components using quantum algorithm
    pub fn extract_components(&mut self) -> Result<(), QuantRS2Error> {
        // Ensure density matrix is computed
        if self.density_matrix.is_none() {
            self.compute_density_matrix()?;
        }

        let density = self.density_matrix.as_ref().ok_or_else(|| {
            QuantRS2Error::UnsupportedOperation("Density matrix not computed".to_string())
        })?;
        let dim = density.nrows();

        // Use quantum phase estimation to find eigenvalues
        // In practice, this would be done on a quantum computer
        let (eigenvalues, eigenvectors) = self.quantum_eigendecomposition(density)?;

        // Filter by threshold
        let mut filtered_indices: Vec<usize> = eigenvalues
            .iter()
            .enumerate()
            .filter(|(_, &val)| val.abs() > self.params.eigenvalue_threshold)
            .map(|(idx, _)| idx)
            .collect();

        // Sort by eigenvalue magnitude (descending)
        filtered_indices.sort_by(|&a, &b| {
            eigenvalues[b]
                .abs()
                .partial_cmp(&eigenvalues[a].abs())
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Extract filtered eigenvalues and eigenvectors
        let n_components = filtered_indices.len();
        let mut filtered_eigenvalues = Array1::zeros(n_components);
        let mut filtered_eigenvectors = Array2::zeros((dim, n_components));

        for (new_idx, &old_idx) in filtered_indices.iter().enumerate() {
            filtered_eigenvalues[new_idx] = eigenvalues[old_idx];
            for i in 0..dim {
                filtered_eigenvectors[[i, new_idx]] = eigenvectors[[i, old_idx]];
            }
        }

        self.eigenvalues = Some(filtered_eigenvalues);
        self.eigenvectors = Some(filtered_eigenvectors);

        Ok(())
    }

    /// Quantum eigendecomposition (simulated)
    fn quantum_eigendecomposition(
        &self,
        matrix: &Array2<Complex64>,
    ) -> Result<(Array1<f64>, Array2<Complex64>), QuantRS2Error> {
        // In a real implementation, this would use quantum phase estimation
        // For now, we use a simplified eigendecomposition approach

        // Ensure matrix is Hermitian
        let hermitian = (matrix + &matrix.t().mapv(|x| x.conj())) / Complex64::new(2.0, 0.0);

        // Convert to real symmetric matrix if possible
        let is_real = hermitian.iter().all(|x| x.im.abs() < 1e-10);

        if is_real {
            let n = hermitian.nrows();
            let real_matrix = hermitian.mapv(|x| x.re);

            // Use power iteration method for finding dominant eigenvalues
            // This is a simplified approach - in practice use proper eigensolvers
            let mut eigenvalues = Vec::with_capacity(n);
            let mut eigenvectors = Array2::<Complex64>::zeros((n, n));

            // For demonstration, we'll just extract a few principal components
            let num_components = n.min(self.params.precision_qubits);

            for comp in 0..num_components {
                // Power iteration for finding dominant eigenvector
                let mut v = Array1::from_vec(vec![1.0 / (n as f64).sqrt(); n]);
                let mut eigenvalue = 0.0;

                for _ in 0..self.params.max_iterations {
                    let av = real_matrix.dot(&v);
                    eigenvalue = v.dot(&av);
                    let norm = av.dot(&av).sqrt();
                    if norm > 1e-10 {
                        v = av / norm;
                    }
                }

                eigenvalues.push(eigenvalue);
                for i in 0..n {
                    eigenvectors[[i, comp]] = Complex64::new(v[i], 0.0);
                }
            }

            // Fill remaining with zeros
            eigenvalues.extend(vec![0.0; n - num_components]);

            Ok((Array1::from_vec(eigenvalues), eigenvectors))
        } else {
            // For complex Hermitian matrices, we need specialized algorithms
            // This is a placeholder - in practice, use quantum phase estimation
            Err(QuantRS2Error::UnsupportedOperation(
                "Complex eigendecomposition not yet implemented".to_string(),
            ))
        }
    }

    /// Project data onto principal components
    pub fn transform(&self, data: &Array2<f64>) -> Result<Array2<f64>, QuantRS2Error> {
        let eigenvectors =
            self.eigenvectors
                .as_ref()
                .ok_or(QuantRS2Error::UnsupportedOperation(
                    "Components not yet extracted".to_string(),
                ))?;

        // Center the data
        let mean = self
            .data_matrix
            .mean_axis(scirs2_core::ndarray::Axis(0))
            .ok_or_else(|| {
                QuantRS2Error::UnsupportedOperation("Failed to compute mean".to_string())
            })?;

        let centered_data = data - &mean;

        // Project onto principal components
        let n_components = eigenvectors.ncols();
        let n_samples = centered_data.nrows();
        let mut transformed = Array2::zeros((n_samples, n_components));

        for i in 0..n_samples {
            for j in 0..n_components {
                let mut sum = 0.0;
                for k in 0..centered_data.ncols() {
                    sum += centered_data[[i, k]] * eigenvectors[[k, j]].re;
                }
                transformed[[i, j]] = sum;
            }
        }

        Ok(transformed)
    }

    /// Get explained variance ratio for each component
    pub fn explained_variance_ratio(&self) -> Result<Array1<f64>, QuantRS2Error> {
        let eigenvalues = self
            .eigenvalues
            .as_ref()
            .ok_or(QuantRS2Error::UnsupportedOperation(
                "Components not yet extracted".to_string(),
            ))?;

        let total_variance: f64 = eigenvalues.sum();
        if total_variance.abs() < 1e-10 {
            return Err(QuantRS2Error::UnsupportedOperation(
                "Total variance is zero".to_string(),
            ));
        }

        Ok(eigenvalues / total_variance)
    }

    /// Get the number of components
    pub fn n_components(&self) -> Option<usize> {
        self.eigenvalues.as_ref().map(|e| e.len())
    }

    /// Get eigenvalues
    pub const fn eigenvalues(&self) -> Option<&Array1<f64>> {
        self.eigenvalues.as_ref()
    }

    /// Get eigenvectors
    pub const fn eigenvectors(&self) -> Option<&Array2<Complex64>> {
        self.eigenvectors.as_ref()
    }
}

/// Quantum-inspired PCA using density matrix formulation
pub struct DensityMatrixPCA {
    params: QPCAParams,
    pub trace_threshold: f64,
}

impl DensityMatrixPCA {
    /// Create new density matrix PCA
    pub const fn new(params: QPCAParams) -> Self {
        Self {
            params,
            trace_threshold: 0.95, // Capture 95% of trace
        }
    }

    /// Compute low-rank approximation using quantum-inspired method
    pub fn fit_transform(
        &self,
        data: &Array2<f64>,
    ) -> Result<(Array2<f64>, Array1<f64>), QuantRS2Error> {
        let mut qpca = QuantumPCA::new(data.clone(), self.params.clone());

        // Compute density matrix and extract components
        qpca.compute_density_matrix()?;
        qpca.extract_components()?;

        // Find number of components to retain based on trace threshold
        let explained_variance = qpca.explained_variance_ratio()?;
        let mut cumsum = 0.0;
        let mut n_components_retain = 0;

        for (i, &var) in explained_variance.iter().enumerate() {
            cumsum += var;
            n_components_retain = i + 1;
            if cumsum >= self.trace_threshold {
                break;
            }
        }

        // Transform data
        let transformed = qpca.transform(data)?;

        // Return only the retained components
        let retained_transform = transformed
            .slice(scirs2_core::ndarray::s![.., ..n_components_retain])
            .to_owned();
        let retained_variance = explained_variance
            .slice(scirs2_core::ndarray::s![..n_components_retain])
            .to_owned();

        Ok((retained_transform, retained_variance))
    }
}

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

    #[test]
    fn test_qpca_basic() {
        // Create simple test data
        let data = Array2::from_shape_vec(
            (5, 3),
            vec![
                1.0, 2.0, 3.0, 2.0, 4.0, 6.0, 3.0, 6.0, 9.0, 4.0, 8.0, 12.0, 5.0, 10.0, 15.0,
            ],
        )
        .expect("Failed to create test data array");

        let params = QPCAParams::default();
        let mut qpca = QuantumPCA::new(data.clone(), params);

        // Compute density matrix
        let density = qpca
            .compute_density_matrix()
            .expect("Failed to compute density matrix");
        assert_eq!(density.shape(), &[3, 3]);

        // Extract components
        qpca.extract_components()
            .expect("Failed to extract components");

        let eigenvalues = qpca.eigenvalues().expect("No eigenvalues computed");
        assert!(!eigenvalues.is_empty());

        // Check that eigenvalues are sorted in descending order
        for i in 1..eigenvalues.len() {
            assert!(eigenvalues[i - 1] >= eigenvalues[i]);
        }
    }

    #[test]
    fn test_density_matrix_pca() {
        // Create test data with clear principal components
        let mut data = Array2::zeros((10, 4));
        for i in 0..10 {
            data[[i, 0]] = i as f64;
            data[[i, 1]] = 2.0 * i as f64;
            data[[i, 2]] = 0.1 * ((i * 7) % 10) as f64 / 10.0;
            data[[i, 3]] = 0.1 * ((i * 13) % 10) as f64 / 10.0;
        }

        let params = QPCAParams::default();
        let pca = DensityMatrixPCA::new(params);

        let (transformed, variance) = pca.fit_transform(&data).expect("Failed to fit transform");

        // Should retain at least one component
        assert!(transformed.ncols() >= 1);
        assert!(transformed.ncols() <= data.ncols());
        // Variance should be normalized
        assert!(variance.sum() <= 1.0 + 1e-6);
    }
}