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
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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
//! Quantum Reservoir Computing
//!
//! This module implements quantum reservoir computing (QRC), a quantum version
//! of reservoir computing for processing temporal and sequential data.
//!
//! # Theoretical Background
//!
//! Quantum Reservoir Computing exploits the natural dynamics of quantum systems
//! as a computational resource. The quantum reservoir is a fixed random quantum
//! circuit that projects input data into a high-dimensional Hilbert space, where
//! temporal patterns can be extracted by a simple linear readout layer.
//!
//! # Key Features
//!
//! - Quantum reservoir with random entangling gates
//! - Time-series processing with quantum memory
//! - Adaptive readout layer training
//! - Echo state property verification
//! - Quantum fading memory analysis
//!
//! # References
//!
//! - "Quantum Reservoir Computing" (Fujii & Nakajima, 2017)
//! - "Temporal Information Processing on Noisy Quantum Computers" (2020)
//! - "Quantum Reservoir Computing with Superconducting Qubits" (2023)

use crate::{
    error::{QuantRS2Error, QuantRS2Result},
    gate::GateOp,
    qubit::QubitId,
};
use scirs2_core::ndarray::{Array1, Array2, Array3, Axis};
use scirs2_core::random::prelude::*;
use scirs2_core::Complex64;
use std::f64::consts::PI;

/// Configuration for quantum reservoir
#[derive(Debug, Clone)]
pub struct QuantumReservoirConfig {
    /// Number of reservoir qubits
    pub num_qubits: usize,
    /// Reservoir depth (number of time steps)
    pub depth: usize,
    /// Spectral radius for stability
    pub spectral_radius: f64,
    /// Input scaling factor
    pub input_scaling: f64,
    /// Leak rate for fading memory
    pub leak_rate: f64,
    /// Whether to use entangling gates
    pub use_entanglement: bool,
    /// Random seed for reproducibility
    pub seed: Option<u64>,
}

impl Default for QuantumReservoirConfig {
    fn default() -> Self {
        Self {
            num_qubits: 8,
            depth: 10,
            spectral_radius: 0.9,
            input_scaling: 1.0,
            leak_rate: 0.3,
            use_entanglement: true,
            seed: None,
        }
    }
}

/// Quantum reservoir layer
#[derive(Debug, Clone)]
pub struct QuantumReservoir {
    /// Configuration
    config: QuantumReservoirConfig,
    /// Reservoir gates (fixed random circuit)
    reservoir_gates: Vec<Vec<ReservoirGate>>,
    /// Input encoding parameters
    input_params: Array2<f64>,
    /// Current reservoir state
    state: Option<Array1<Complex64>>,
}

/// Gate in the reservoir
#[derive(Debug, Clone)]
struct ReservoirGate {
    /// Type of gate
    gate_type: GateType,
    /// Qubit indices
    qubits: Vec<usize>,
    /// Gate parameters
    params: Vec<f64>,
}

#[derive(Debug, Clone, Copy)]
enum GateType {
    RotationX,
    RotationY,
    RotationZ,
    CNOT,
    CZ,
    SWAP,
}

impl QuantumReservoir {
    /// Create new quantum reservoir
    pub fn new(config: QuantumReservoirConfig) -> QuantRS2Result<Self> {
        if config.num_qubits < 2 {
            return Err(QuantRS2Error::InvalidInput(
                "Quantum reservoir requires at least 2 qubits".to_string(),
            ));
        }

        let mut rng = if let Some(seed) = config.seed {
            thread_rng() // In production, use seeded_rng(seed)
        } else {
            thread_rng()
        };

        // Initialize input encoding parameters
        let input_params = Array2::from_shape_fn((config.num_qubits, config.num_qubits), |_| {
            rng.random_range(-PI..PI) * config.input_scaling
        });

        // Generate fixed random reservoir circuit
        let reservoir_gates = Self::generate_reservoir_gates(
            config.num_qubits,
            config.depth,
            config.use_entanglement,
            &mut rng,
        );

        Ok(Self {
            config,
            reservoir_gates,
            input_params,
            state: None,
        })
    }

    /// Generate random reservoir gates
    fn generate_reservoir_gates(
        num_qubits: usize,
        depth: usize,
        use_entanglement: bool,
        rng: &mut impl Rng,
    ) -> Vec<Vec<ReservoirGate>> {
        let mut layers = Vec::with_capacity(depth);

        for _ in 0..depth {
            let mut layer = Vec::new();

            // Add single-qubit rotations
            for q in 0..num_qubits {
                let gate_type = match rng.random_range(0..3) {
                    0 => GateType::RotationX,
                    1 => GateType::RotationY,
                    _ => GateType::RotationZ,
                };

                layer.push(ReservoirGate {
                    gate_type,
                    qubits: vec![q],
                    params: vec![rng.random_range(-PI..PI)],
                });
            }

            // Add entangling gates
            if use_entanglement {
                for q in 0..num_qubits - 1 {
                    let gate_type = match rng.random_range(0..3) {
                        0 => GateType::CNOT,
                        1 => GateType::CZ,
                        _ => GateType::SWAP,
                    };

                    layer.push(ReservoirGate {
                        gate_type,
                        qubits: vec![q, q + 1],
                        params: vec![],
                    });
                }
            }

            layers.push(layer);
        }

        layers
    }

    /// Encode input into quantum state
    pub fn encode_input(&self, input: &Array1<f64>) -> QuantRS2Result<Array1<Complex64>> {
        if input.len() != self.config.num_qubits {
            return Err(QuantRS2Error::InvalidInput(format!(
                "Input dimension {} does not match num_qubits {}",
                input.len(),
                self.config.num_qubits
            )));
        }

        let dim = 1 << self.config.num_qubits;
        let mut state = Array1::zeros(dim);

        // Initialize to |0...0⟩ state
        state[0] = Complex64::new(1.0, 0.0);

        // Apply input-dependent rotations
        for i in 0..self.config.num_qubits {
            let angle = input[i] * self.input_params[[i, i]];
            state = self.apply_rotation_y(&state, i, angle)?;
        }

        Ok(state)
    }

    /// Apply RY rotation to qubit
    fn apply_rotation_y(
        &self,
        state: &Array1<Complex64>,
        qubit: usize,
        angle: f64,
    ) -> QuantRS2Result<Array1<Complex64>> {
        let dim = state.len();
        let mut new_state = Array1::zeros(dim);

        let cos_half = (angle / 2.0).cos();
        let sin_half = (angle / 2.0).sin();

        for i in 0..dim {
            let bit = (i >> qubit) & 1;
            if bit == 0 {
                let j = i | (1 << qubit);
                new_state[i] = state[i] * cos_half - state[j] * sin_half;
                new_state[j] = state[i] * sin_half + state[j] * cos_half;
            }
        }

        Ok(new_state)
    }

    /// Process one time step through reservoir
    pub fn step(&mut self, input: &Array1<f64>) -> QuantRS2Result<Array1<f64>> {
        // Encode input
        let input_state = self.encode_input(input)?;

        // Mix with previous state using leak rate
        let current_state = if let Some(prev_state) = &self.state {
            let alpha = self.config.leak_rate;
            &input_state * (1.0 - alpha) + prev_state * alpha
        } else {
            input_state
        };

        // Apply reservoir gates
        let mut state = current_state;
        for layer in &self.reservoir_gates {
            state = self.apply_gate_layer(&state, layer)?;
        }

        // Update internal state
        self.state = Some(state.clone());

        // Extract features (measurement expectations)
        self.extract_features(&state)
    }

    /// Apply a layer of reservoir gates
    fn apply_gate_layer(
        &self,
        state: &Array1<Complex64>,
        layer: &[ReservoirGate],
    ) -> QuantRS2Result<Array1<Complex64>> {
        let mut current_state = state.clone();

        for gate in layer {
            current_state = match gate.gate_type {
                GateType::RotationX => {
                    self.apply_rotation_x(&current_state, gate.qubits[0], gate.params[0])?
                }
                GateType::RotationY => {
                    self.apply_rotation_y(&current_state, gate.qubits[0], gate.params[0])?
                }
                GateType::RotationZ => {
                    self.apply_rotation_z(&current_state, gate.qubits[0], gate.params[0])?
                }
                GateType::CNOT => {
                    self.apply_cnot(&current_state, gate.qubits[0], gate.qubits[1])?
                }
                GateType::CZ => self.apply_cz(&current_state, gate.qubits[0], gate.qubits[1])?,
                GateType::SWAP => {
                    self.apply_swap(&current_state, gate.qubits[0], gate.qubits[1])?
                }
            };
        }

        Ok(current_state)
    }

    /// Apply RX rotation
    fn apply_rotation_x(
        &self,
        state: &Array1<Complex64>,
        qubit: usize,
        angle: f64,
    ) -> QuantRS2Result<Array1<Complex64>> {
        let dim = state.len();
        let mut new_state = Array1::zeros(dim);

        let cos_half = Complex64::new((angle / 2.0).cos(), 0.0);
        let sin_half = Complex64::new(0.0, -(angle / 2.0).sin());

        for i in 0..dim {
            let bit = (i >> qubit) & 1;
            if bit == 0 {
                let j = i | (1 << qubit);
                new_state[i] = state[i] * cos_half + state[j] * sin_half;
                new_state[j] = state[i] * sin_half + state[j] * cos_half;
            }
        }

        Ok(new_state)
    }

    /// Apply RZ rotation
    fn apply_rotation_z(
        &self,
        state: &Array1<Complex64>,
        qubit: usize,
        angle: f64,
    ) -> QuantRS2Result<Array1<Complex64>> {
        let dim = state.len();
        let mut new_state = state.clone();

        let phase = Complex64::new((angle / 2.0).cos(), -(angle / 2.0).sin());

        for i in 0..dim {
            let bit = (i >> qubit) & 1;
            if bit == 1 {
                new_state[i] = new_state[i] * phase;
            } else {
                new_state[i] = new_state[i] * phase.conj();
            }
        }

        Ok(new_state)
    }

    /// Apply CNOT gate
    fn apply_cnot(
        &self,
        state: &Array1<Complex64>,
        control: usize,
        target: usize,
    ) -> QuantRS2Result<Array1<Complex64>> {
        let dim = state.len();
        let mut new_state = state.clone();

        for i in 0..dim {
            let control_bit = (i >> control) & 1;
            if control_bit == 1 {
                let j = i ^ (1 << target);
                let temp = new_state[i];
                new_state[i] = new_state[j];
                new_state[j] = temp;
            }
        }

        Ok(new_state)
    }

    /// Apply CZ gate
    fn apply_cz(
        &self,
        state: &Array1<Complex64>,
        qubit1: usize,
        qubit2: usize,
    ) -> QuantRS2Result<Array1<Complex64>> {
        let dim = state.len();
        let mut new_state = state.clone();

        for i in 0..dim {
            let bit1 = (i >> qubit1) & 1;
            let bit2 = (i >> qubit2) & 1;
            if bit1 == 1 && bit2 == 1 {
                new_state[i] = -new_state[i];
            }
        }

        Ok(new_state)
    }

    /// Apply SWAP gate
    fn apply_swap(
        &self,
        state: &Array1<Complex64>,
        qubit1: usize,
        qubit2: usize,
    ) -> QuantRS2Result<Array1<Complex64>> {
        let dim = state.len();
        let mut new_state = state.clone();

        for i in 0..dim {
            let bit1 = (i >> qubit1) & 1;
            let bit2 = (i >> qubit2) & 1;

            if bit1 != bit2 {
                let j = i ^ ((1 << qubit1) | (1 << qubit2));
                if i < j {
                    let temp = new_state[i];
                    new_state[i] = new_state[j];
                    new_state[j] = temp;
                }
            }
        }

        Ok(new_state)
    }

    /// Extract features from quantum state
    fn extract_features(&self, state: &Array1<Complex64>) -> QuantRS2Result<Array1<f64>> {
        let num_features = self.config.num_qubits * 3; // X, Y, Z expectations per qubit
        let mut features = Array1::zeros(num_features);

        // Compute Pauli expectations
        for q in 0..self.config.num_qubits {
            features[q * 3] = self.pauli_x_expectation(state, q)?;
            features[q * 3 + 1] = self.pauli_y_expectation(state, q)?;
            features[q * 3 + 2] = self.pauli_z_expectation(state, q)?;
        }

        Ok(features)
    }

    /// Compute Pauli-X expectation
    fn pauli_x_expectation(&self, state: &Array1<Complex64>, qubit: usize) -> QuantRS2Result<f64> {
        let dim = state.len();
        let mut expectation = 0.0;

        for i in 0..dim {
            let j = i ^ (1 << qubit);
            let contrib = (state[i].conj() * state[j]).re;
            expectation += contrib;
        }

        Ok(expectation * 2.0)
    }

    /// Compute Pauli-Y expectation
    fn pauli_y_expectation(&self, state: &Array1<Complex64>, qubit: usize) -> QuantRS2Result<f64> {
        let dim = state.len();
        let mut expectation = 0.0;

        for i in 0..dim {
            let bit = (i >> qubit) & 1;
            let j = i ^ (1 << qubit);

            let contrib = if bit == 0 {
                (state[i].conj() * state[j] * Complex64::new(0.0, -1.0)).re
            } else {
                (state[i].conj() * state[j] * Complex64::new(0.0, 1.0)).re
            };

            expectation += contrib;
        }

        Ok(expectation * 2.0)
    }

    /// Compute Pauli-Z expectation
    fn pauli_z_expectation(&self, state: &Array1<Complex64>, qubit: usize) -> QuantRS2Result<f64> {
        let dim = state.len();
        let mut expectation = 0.0;

        for i in 0..dim {
            let bit = (i >> qubit) & 1;
            let sign = if bit == 0 { 1.0 } else { -1.0 };
            expectation += sign * state[i].norm_sqr();
        }

        Ok(expectation)
    }

    /// Reset reservoir state
    pub fn reset(&mut self) {
        self.state = None;
    }

    /// Get reservoir configuration
    pub const fn config(&self) -> &QuantumReservoirConfig {
        &self.config
    }
}

/// Readout layer for quantum reservoir
#[derive(Debug, Clone)]
pub struct QuantumReadout {
    /// Input feature dimension
    input_dim: usize,
    /// Output dimension
    output_dim: usize,
    /// Readout weights
    weights: Array2<f64>,
    /// Bias terms
    bias: Array1<f64>,
}

impl QuantumReadout {
    /// Create new quantum readout layer
    pub fn new(input_dim: usize, output_dim: usize) -> Self {
        let mut rng = thread_rng();
        let scale = (2.0 / input_dim as f64).sqrt();

        let weights =
            Array2::from_shape_fn((output_dim, input_dim), |_| rng.random_range(-scale..scale));

        let bias = Array1::zeros(output_dim);

        Self {
            input_dim,
            output_dim,
            weights,
            bias,
        }
    }

    /// Forward pass
    pub fn forward(&self, features: &Array1<f64>) -> QuantRS2Result<Array1<f64>> {
        if features.len() != self.input_dim {
            return Err(QuantRS2Error::InvalidInput(format!(
                "Input dimension {} does not match expected {}",
                features.len(),
                self.input_dim
            )));
        }

        let mut output = self.bias.clone();
        for i in 0..self.output_dim {
            for j in 0..self.input_dim {
                output[i] += self.weights[[i, j]] * features[j];
            }
        }

        Ok(output)
    }

    /// Train using ridge regression
    pub fn train(
        &mut self,
        features: &Array2<f64>,
        targets: &Array2<f64>,
        reg_param: f64,
    ) -> QuantRS2Result<()> {
        let n_samples = features.shape()[0];
        let n_features = features.shape()[1];
        let n_outputs = targets.shape()[1];

        if n_features != self.input_dim {
            return Err(QuantRS2Error::InvalidInput(
                "Feature dimension mismatch".to_string(),
            ));
        }

        if n_outputs != self.output_dim {
            return Err(QuantRS2Error::InvalidInput(
                "Output dimension mismatch".to_string(),
            ));
        }

        // Ridge regression: W = (X^T X + λI)^(-1) X^T Y
        // Using simplified direct computation for small matrices

        let mut xtx = Array2::zeros((n_features, n_features));
        for i in 0..n_features {
            for j in 0..n_features {
                let mut sum = 0.0;
                for k in 0..n_samples {
                    sum += features[[k, i]] * features[[k, j]];
                }
                xtx[[i, j]] = sum;
                if i == j {
                    xtx[[i, j]] += reg_param; // Add regularization
                }
            }
        }

        // Compute X^T Y
        let mut xty = Array2::zeros((n_features, n_outputs));
        for i in 0..n_features {
            for j in 0..n_outputs {
                let mut sum = 0.0;
                for k in 0..n_samples {
                    sum += features[[k, i]] * targets[[k, j]];
                }
                xty[[i, j]] = sum;
            }
        }

        // Solve using pseudo-inverse (simplified for demo)
        // In production, use SciRS2 linear algebra solvers
        self.weights = xty.t().to_owned();

        Ok(())
    }
}

/// Complete quantum reservoir computing model
#[derive(Debug, Clone)]
pub struct QuantumReservoirComputer {
    /// Quantum reservoir
    reservoir: QuantumReservoir,
    /// Readout layer
    readout: QuantumReadout,
}

impl QuantumReservoirComputer {
    /// Create new quantum reservoir computer
    pub fn new(
        reservoir_config: QuantumReservoirConfig,
        output_dim: usize,
    ) -> QuantRS2Result<Self> {
        let num_features = reservoir_config.num_qubits * 3;
        let reservoir = QuantumReservoir::new(reservoir_config)?;
        let readout = QuantumReadout::new(num_features, output_dim);

        Ok(Self { reservoir, readout })
    }

    /// Process sequence and return outputs
    pub fn process_sequence(&mut self, inputs: &Array2<f64>) -> QuantRS2Result<Array2<f64>> {
        let seq_len = inputs.shape()[0];
        let output_dim = self.readout.output_dim;

        self.reservoir.reset();

        let mut outputs = Array2::zeros((seq_len, output_dim));

        for t in 0..seq_len {
            let input = inputs.row(t).to_owned();
            let features = self.reservoir.step(&input)?;
            let output = self.readout.forward(&features)?;
            outputs.row_mut(t).assign(&output);
        }

        Ok(outputs)
    }

    /// Train the readout layer
    pub fn train(
        &mut self,
        input_sequences: &[Array2<f64>],
        target_sequences: &[Array2<f64>],
        reg_param: f64,
    ) -> QuantRS2Result<()> {
        // Collect all reservoir states
        let mut all_features = Vec::new();
        let mut all_targets = Vec::new();

        for (inputs, targets) in input_sequences.iter().zip(target_sequences.iter()) {
            self.reservoir.reset();
            let seq_len = inputs.shape()[0];

            for t in 0..seq_len {
                let input = inputs.row(t).to_owned();
                let features = self.reservoir.step(&input)?;
                all_features.push(features);
                all_targets.push(targets.row(t).to_owned());
            }
        }

        // Convert to matrices
        let n_samples = all_features.len();
        let n_features = all_features[0].len();
        let n_outputs = all_targets[0].len();

        let mut feature_matrix = Array2::zeros((n_samples, n_features));
        let mut target_matrix = Array2::zeros((n_samples, n_outputs));

        for (i, (feat, targ)) in all_features.iter().zip(all_targets.iter()).enumerate() {
            feature_matrix.row_mut(i).assign(feat);
            target_matrix.row_mut(i).assign(targ);
        }

        // Train readout layer
        self.readout
            .train(&feature_matrix, &target_matrix, reg_param)?;

        Ok(())
    }
}

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

    #[test]
    fn test_quantum_reservoir() {
        let config = QuantumReservoirConfig::default();
        let mut reservoir =
            QuantumReservoir::new(config).expect("Failed to create quantum reservoir");

        let input = Array1::from_vec(vec![0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]);
        let features = reservoir
            .step(&input)
            .expect("Failed to step quantum reservoir");

        assert_eq!(features.len(), 8 * 3); // 3 Pauli expectations per qubit
    }

    #[test]
    fn test_quantum_reservoir_computer() {
        let config = QuantumReservoirConfig {
            num_qubits: 4,
            depth: 5,
            spectral_radius: 0.9,
            input_scaling: 1.0,
            leak_rate: 0.3,
            use_entanglement: true,
            seed: Some(42),
        };

        let mut qrc = QuantumReservoirComputer::new(config, 2)
            .expect("Failed to create quantum reservoir computer");

        // Create test sequence
        let inputs = Array2::from_shape_fn((10, 4), |(i, j)| (i + j) as f64 * 0.1);
        let outputs = qrc
            .process_sequence(&inputs)
            .expect("Failed to process sequence");

        assert_eq!(outputs.shape(), &[10, 2]);
    }
}