1use crate::error::{MLError, Result};
8use scirs2_core::ndarray::{Array, Array1, Array2, Array3, ArrayD, ArrayViewD, Dimension, IxDyn};
9use std::collections::HashMap;
10
11pub trait SciRS2Tensor {
13 fn shape(&self) -> &[usize];
15
16 fn view(&self) -> ArrayViewD<f64>;
18
19 fn to_scirs2(&self) -> Result<SciRS2Array>;
21
22 fn matmul(&self, other: &dyn SciRS2Tensor) -> Result<SciRS2Array>;
24
25 fn add(&self, other: &dyn SciRS2Tensor) -> Result<SciRS2Array>;
27 fn mul(&self, other: &dyn SciRS2Tensor) -> Result<SciRS2Array>;
28 fn sub(&self, other: &dyn SciRS2Tensor) -> Result<SciRS2Array>;
29
30 fn sum(&self, axis: Option<usize>) -> Result<SciRS2Array>;
32 fn mean(&self, axis: Option<usize>) -> Result<SciRS2Array>;
33 fn max(&self, axis: Option<usize>) -> Result<SciRS2Array>;
34 fn min(&self, axis: Option<usize>) -> Result<SciRS2Array>;
35}
36
37pub struct SciRS2Array {
39 pub data: ArrayD<f64>,
41 pub requires_grad: bool,
43 pub grad: Option<ArrayD<f64>>,
45 pub grad_fn: Option<Box<dyn GradFunction>>,
47}
48
49impl std::fmt::Debug for SciRS2Array {
50 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51 f.debug_struct("SciRS2Array")
52 .field("data", &self.data)
53 .field("requires_grad", &self.requires_grad)
54 .field("grad", &self.grad)
55 .field("grad_fn", &"<gradient_function>")
56 .finish()
57 }
58}
59
60impl Clone for SciRS2Array {
61 fn clone(&self) -> Self {
62 Self {
63 data: self.data.clone(),
64 requires_grad: self.requires_grad,
65 grad: self.grad.clone(),
66 grad_fn: None, }
68 }
69}
70
71impl SciRS2Array {
72 pub fn new(data: ArrayD<f64>, requires_grad: bool) -> Self {
74 let grad = if requires_grad {
75 Some(ArrayD::zeros(data.raw_dim()))
76 } else {
77 None
78 };
79 Self {
80 data,
81 requires_grad,
82 grad,
83 grad_fn: None,
84 }
85 }
86
87 pub fn from_array<D: Dimension>(arr: Array<f64, D>) -> Self {
89 let data = arr.into_dyn();
90 Self::new(data, false)
91 }
92
93 pub fn with_grad<D: Dimension>(arr: Array<f64, D>) -> Self {
95 let data = arr.into_dyn();
96 Self::new(data, true)
97 }
98
99 pub fn zero_grad(&mut self) {
101 if let Some(ref mut grad) = self.grad {
102 grad.fill(0.0);
103 }
104 }
105
106 pub fn backward(&mut self) -> Result<()> {
108 if let Some(grad_fn) = self.grad_fn.take() {
110 grad_fn.backward(self)?;
111 self.grad_fn = Some(grad_fn);
112 }
113 Ok(())
114 }
115
116 pub fn matmul(&self, other: &SciRS2Array) -> Result<SciRS2Array> {
118 let result_data = if self.data.ndim() == 2 && other.data.ndim() == 2 {
120 let self_2d = self
121 .data
122 .view()
123 .into_dimensionality::<scirs2_core::ndarray::Ix2>()
124 .map_err(|e| MLError::ComputationError(format!("Shape error: {}", e)))?;
125 let other_2d = other
126 .data
127 .view()
128 .into_dimensionality::<scirs2_core::ndarray::Ix2>()
129 .map_err(|e| MLError::ComputationError(format!("Shape error: {}", e)))?;
130 self_2d.dot(&other_2d).into_dyn()
131 } else {
132 return Err(MLError::InvalidConfiguration(
133 "Matrix multiplication requires 2D arrays".to_string(),
134 ));
135 };
136
137 let requires_grad = self.requires_grad || other.requires_grad;
138 let mut result = SciRS2Array::new(result_data, requires_grad);
139
140 if requires_grad {
141 result.grad_fn = Some(Box::new(MatmulGradFn {
142 left_shape: self.data.raw_dim(),
143 right_shape: other.data.raw_dim(),
144 }));
145 }
146
147 Ok(result)
148 }
149
150 pub fn add(&self, other: &SciRS2Array) -> Result<SciRS2Array> {
152 let result_data = &self.data + &other.data;
153 let requires_grad = self.requires_grad || other.requires_grad;
154 let mut result = SciRS2Array::new(result_data, requires_grad);
155
156 if requires_grad {
157 result.grad_fn = Some(Box::new(AddGradFn));
158 }
159
160 Ok(result)
161 }
162
163 pub fn mul(&self, other: &SciRS2Array) -> Result<SciRS2Array> {
165 let result_data = &self.data * &other.data;
166 let requires_grad = self.requires_grad || other.requires_grad;
167 let mut result = SciRS2Array::new(result_data, requires_grad);
168
169 if requires_grad {
170 result.grad_fn = Some(Box::new(MulGradFn {
171 left_data: self.data.clone(),
172 right_data: other.data.clone(),
173 }));
174 }
175
176 Ok(result)
177 }
178
179 pub fn sum(&self, axis: Option<usize>) -> Result<SciRS2Array> {
181 let result_data = match axis {
182 Some(ax) => self
183 .data
184 .sum_axis(scirs2_core::ndarray::Axis(ax))
185 .into_dyn(),
186 None => {
187 let sum_val = self.data.sum();
188 ArrayD::from_elem(IxDyn(&[]), sum_val)
189 }
190 };
191
192 let mut result = SciRS2Array::new(result_data, self.requires_grad);
193
194 if self.requires_grad {
195 result.grad_fn = Some(Box::new(SumGradFn { axis }));
196 }
197
198 Ok(result)
199 }
200}
201
202impl SciRS2Tensor for SciRS2Array {
203 fn shape(&self) -> &[usize] {
204 self.data.shape()
205 }
206
207 fn view(&self) -> ArrayViewD<f64> {
208 self.data.view()
209 }
210
211 fn to_scirs2(&self) -> Result<SciRS2Array> {
212 Ok(self.clone())
213 }
214
215 fn matmul(&self, other: &dyn SciRS2Tensor) -> Result<SciRS2Array> {
216 let other_array = other.to_scirs2()?;
218 self.matmul(&other_array)
219 }
220
221 fn add(&self, other: &dyn SciRS2Tensor) -> Result<SciRS2Array> {
222 let other_array = other.to_scirs2()?;
223 self.add(&other_array)
224 }
225
226 fn mul(&self, other: &dyn SciRS2Tensor) -> Result<SciRS2Array> {
227 let other_array = other.to_scirs2()?;
228 self.mul(&other_array)
229 }
230
231 fn sub(&self, other: &dyn SciRS2Tensor) -> Result<SciRS2Array> {
232 let result_data = &self.data - &other.to_scirs2()?.data;
233 let requires_grad = self.requires_grad || other.to_scirs2()?.requires_grad;
234 Ok(SciRS2Array::new(result_data, requires_grad))
235 }
236
237 fn sum(&self, axis: Option<usize>) -> Result<SciRS2Array> {
238 self.sum(axis)
239 }
240
241 fn mean(&self, axis: Option<usize>) -> Result<SciRS2Array> {
242 let result_data = match axis {
243 Some(ax) => self
244 .data
245 .mean_axis(scirs2_core::ndarray::Axis(ax))
246 .unwrap()
247 .into_dyn(),
248 None => {
249 let mean_val = self.data.mean().unwrap();
250 ArrayD::from_elem(IxDyn(&[]), mean_val)
251 }
252 };
253 Ok(SciRS2Array::new(result_data, self.requires_grad))
254 }
255
256 fn max(&self, axis: Option<usize>) -> Result<SciRS2Array> {
257 let result_data = match axis {
258 Some(ax) => self
259 .data
260 .map_axis(scirs2_core::ndarray::Axis(ax), |view| {
261 *view
262 .iter()
263 .max_by(|a, b| a.partial_cmp(b).unwrap())
264 .unwrap()
265 })
266 .into_dyn(),
267 None => {
268 let max_val = *self
269 .data
270 .iter()
271 .max_by(|a, b| a.partial_cmp(b).unwrap())
272 .unwrap();
273 ArrayD::from_elem(IxDyn(&[]), max_val)
274 }
275 };
276 Ok(SciRS2Array::new(result_data, self.requires_grad))
277 }
278
279 fn min(&self, axis: Option<usize>) -> Result<SciRS2Array> {
280 let result_data = match axis {
281 Some(ax) => self
282 .data
283 .map_axis(scirs2_core::ndarray::Axis(ax), |view| {
284 *view
285 .iter()
286 .min_by(|a, b| a.partial_cmp(b).unwrap())
287 .unwrap()
288 })
289 .into_dyn(),
290 None => {
291 let min_val = *self
292 .data
293 .iter()
294 .min_by(|a, b| a.partial_cmp(b).unwrap())
295 .unwrap();
296 ArrayD::from_elem(IxDyn(&[]), min_val)
297 }
298 };
299 Ok(SciRS2Array::new(result_data, self.requires_grad))
300 }
301}
302
303pub trait GradFunction: Send + Sync {
305 fn backward(&self, output: &mut SciRS2Array) -> Result<()>;
306}
307
308#[derive(Debug)]
310struct MatmulGradFn {
311 left_shape: IxDyn,
312 right_shape: IxDyn,
313}
314
315impl GradFunction for MatmulGradFn {
316 fn backward(&self, _output: &mut SciRS2Array) -> Result<()> {
317 Ok(())
319 }
320}
321
322#[derive(Debug)]
324struct AddGradFn;
325
326impl GradFunction for AddGradFn {
327 fn backward(&self, _output: &mut SciRS2Array) -> Result<()> {
328 Ok(())
330 }
331}
332
333#[derive(Debug)]
335struct MulGradFn {
336 left_data: ArrayD<f64>,
337 right_data: ArrayD<f64>,
338}
339
340impl GradFunction for MulGradFn {
341 fn backward(&self, _output: &mut SciRS2Array) -> Result<()> {
342 Ok(())
344 }
345}
346
347#[derive(Debug)]
349struct SumGradFn {
350 axis: Option<usize>,
351}
352
353impl GradFunction for SumGradFn {
354 fn backward(&self, _output: &mut SciRS2Array) -> Result<()> {
355 Ok(())
357 }
358}
359
360pub struct SciRS2Optimizer {
362 pub optimizer_type: String,
364 pub config: HashMap<String, f64>,
366 pub state: HashMap<String, ArrayD<f64>>,
368}
369
370impl SciRS2Optimizer {
371 pub fn new(optimizer_type: impl Into<String>) -> Self {
373 Self {
374 optimizer_type: optimizer_type.into(),
375 config: HashMap::new(),
376 state: HashMap::new(),
377 }
378 }
379
380 pub fn with_config(mut self, key: impl Into<String>, value: f64) -> Self {
382 self.config.insert(key.into(), value);
383 self
384 }
385
386 pub fn step(&mut self, params: &mut HashMap<String, SciRS2Array>) -> Result<()> {
388 match self.optimizer_type.as_str() {
389 "adam" => self.adam_step(params),
390 "sgd" => self.sgd_step(params),
391 "lbfgs" => self.lbfgs_step(params),
392 _ => Err(MLError::InvalidConfiguration(format!(
393 "Unknown optimizer type: {}",
394 self.optimizer_type
395 ))),
396 }
397 }
398
399 fn adam_step(&mut self, params: &mut HashMap<String, SciRS2Array>) -> Result<()> {
401 let learning_rate = self.config.get("learning_rate").unwrap_or(&0.001);
402 let beta1 = self.config.get("beta1").unwrap_or(&0.9);
403 let beta2 = self.config.get("beta2").unwrap_or(&0.999);
404 let epsilon = self.config.get("epsilon").unwrap_or(&1e-8);
405
406 for (name, param) in params.iter_mut() {
407 if let Some(ref grad) = param.grad {
408 let m_key = format!("{}_m", name);
410 let v_key = format!("{}_v", name);
411
412 if !self.state.contains_key(&m_key) {
413 self.state
414 .insert(m_key.clone(), ArrayD::zeros(grad.raw_dim()));
415 self.state
416 .insert(v_key.clone(), ArrayD::zeros(grad.raw_dim()));
417 }
418
419 {
421 let m = self.state.get_mut(&m_key).unwrap();
422 *m = *beta1 * &*m + (1.0 - *beta1) * grad;
423 }
424
425 {
427 let v = self.state.get_mut(&v_key).unwrap();
428 *v = *beta2 * &*v + (1.0 - *beta2) * grad * grad;
429 }
430
431 let m_hat = self.state.get(&m_key).unwrap().clone();
433 let v_hat = self.state.get(&v_key).unwrap().clone();
434
435 param.data =
437 ¶m.data - *learning_rate * &m_hat / (v_hat.mapv(|x| x.sqrt()) + *epsilon);
438 }
439 }
440
441 Ok(())
442 }
443
444 fn sgd_step(&mut self, params: &mut HashMap<String, SciRS2Array>) -> Result<()> {
446 let learning_rate = self.config.get("learning_rate").unwrap_or(&0.01);
447 let momentum = self.config.get("momentum").unwrap_or(&0.0);
448
449 for (name, param) in params.iter_mut() {
450 if let Some(ref grad) = param.grad {
451 if *momentum > 0.0 {
452 let v_key = format!("{}_v", name);
453 if !self.state.contains_key(&v_key) {
454 self.state
455 .insert(v_key.clone(), ArrayD::zeros(grad.raw_dim()));
456 }
457
458 let v = self.state.get_mut(&v_key).unwrap();
459 *v = *momentum * &*v + *learning_rate * grad;
460 param.data = ¶m.data - &*v;
461 } else {
462 param.data = ¶m.data - *learning_rate * grad;
463 }
464 }
465 }
466
467 Ok(())
468 }
469
470 fn lbfgs_step(&mut self, _params: &mut HashMap<String, SciRS2Array>) -> Result<()> {
472 Ok(())
474 }
475}
476
477pub struct SciRS2DistributedTrainer {
479 pub world_size: usize,
481 pub rank: usize,
483 pub backend: String,
485}
486
487impl SciRS2DistributedTrainer {
488 pub fn new(world_size: usize, rank: usize) -> Self {
490 Self {
491 world_size,
492 rank,
493 backend: "nccl".to_string(),
494 }
495 }
496
497 pub fn all_reduce(&self, tensor: &mut SciRS2Array) -> Result<()> {
499 Ok(())
501 }
502
503 pub fn all_reduce_scalar(&self, value: f64) -> Result<f64> {
505 Ok(value)
508 }
509
510 pub fn broadcast(&self, tensor: &mut SciRS2Array, root: usize) -> Result<()> {
512 Ok(())
514 }
515
516 pub fn all_gather(&self, tensor: &SciRS2Array) -> Result<Vec<SciRS2Array>> {
518 Ok(vec![tensor.clone(); self.world_size])
520 }
521
522 pub fn wrap_model<T>(&self, model: T) -> Result<T> {
524 Ok(model)
527 }
528}
529
530pub struct SciRS2Serializer;
532
533impl SciRS2Serializer {
534 pub fn save_model(params: &HashMap<String, SciRS2Array>, path: &str) -> Result<()> {
536 Ok(())
538 }
539
540 pub fn load_model(path: &str) -> Result<HashMap<String, SciRS2Array>> {
542 Ok(HashMap::new())
544 }
545
546 pub fn save_checkpoint(
548 params: &HashMap<String, SciRS2Array>,
549 optimizer: &SciRS2Optimizer,
550 epoch: usize,
551 path: &str,
552 ) -> Result<()> {
553 Ok(())
555 }
556
557 pub fn load_checkpoint(
559 path: &str,
560 ) -> Result<(HashMap<String, SciRS2Array>, SciRS2Optimizer, usize)> {
561 Ok((HashMap::new(), SciRS2Optimizer::new("adam"), 0))
563 }
564}
565
566pub struct SciRS2Dataset {
568 pub data: ArrayD<f64>,
570 pub labels: ArrayD<f64>,
572 pub size: usize,
574}
575
576impl SciRS2Dataset {
577 pub fn new(data: ArrayD<f64>, labels: ArrayD<f64>) -> Result<Self> {
579 let size = data.shape()[0];
580 if labels.shape()[0] != size {
581 return Err(MLError::InvalidConfiguration(
582 "Data and labels must have same number of samples".to_string(),
583 ));
584 }
585
586 Ok(Self { data, labels, size })
587 }
588}
589
590pub struct SciRS2DataLoader {
592 pub dataset: SciRS2Dataset,
594 pub batch_size: usize,
596 pub current_index: usize,
598}
599
600impl SciRS2DataLoader {
601 pub fn new(dataset: SciRS2Dataset, batch_size: usize) -> Self {
603 Self {
604 dataset,
605 batch_size,
606 current_index: 0,
607 }
608 }
609
610 pub fn enumerate(&mut self) -> DataLoaderIterator {
612 DataLoaderIterator {
613 loader: self,
614 batch_idx: 0,
615 }
616 }
617}
618
619pub struct DataLoaderIterator<'a> {
621 loader: &'a mut SciRS2DataLoader,
622 batch_idx: usize,
623}
624
625impl<'a> Iterator for DataLoaderIterator<'a> {
626 type Item = (usize, (SciRS2Array, SciRS2Array));
627
628 fn next(&mut self) -> Option<Self::Item> {
629 if self.loader.current_index >= self.loader.dataset.size {
630 return None;
631 }
632
633 let start = self.loader.current_index;
634 let end = (start + self.loader.batch_size).min(self.loader.dataset.size);
635
636 let batch_data = self
638 .loader
639 .dataset
640 .data
641 .slice(scirs2_core::ndarray::s![start..end, ..])
642 .to_owned();
643 let batch_labels = self
644 .loader
645 .dataset
646 .labels
647 .slice(scirs2_core::ndarray::s![start..end, ..])
648 .to_owned();
649
650 let data_array = SciRS2Array::from_array(batch_data);
651 let label_array = SciRS2Array::from_array(batch_labels);
652
653 self.loader.current_index = end;
654 let batch_idx = self.batch_idx;
655 self.batch_idx += 1;
656
657 Some((batch_idx, (data_array, label_array)))
658 }
659}
660
661#[derive(Debug, Clone, Copy)]
663pub enum SciRS2Device {
664 CPU,
665 GPU,
666 Quantum,
667}
668
669impl SciRS2Array {
671 pub fn randn(shape: Vec<usize>, device: SciRS2Device) -> Result<Self> {
673 use scirs2_core::random::prelude::*;
674 let total_size = shape.iter().product();
675 let mut rng = thread_rng();
676 let data: Vec<f64> = (0..total_size).map(|_| rng.gen_range(-1.0..1.0)).collect();
677 let array = ArrayD::from_shape_vec(IxDyn(&shape), data)
678 .map_err(|e| MLError::ComputationError(format!("Shape error: {}", e)))?;
679 Ok(Self::new(array, false))
680 }
681
682 pub fn ones_like(&self) -> Result<Self> {
684 let ones = ArrayD::ones(self.data.raw_dim());
685 Ok(Self::new(ones, false))
686 }
687
688 pub fn randint(low: i32, high: i32, shape: Vec<usize>, device: SciRS2Device) -> Result<Self> {
690 use scirs2_core::random::prelude::*;
691 let total_size = shape.iter().product();
692 let mut rng = thread_rng();
693 let data: Vec<f64> = (0..total_size)
694 .map(|_| rng.gen_range(low..high) as f64)
695 .collect();
696 let array = ArrayD::from_shape_vec(IxDyn(&shape), data)
697 .map_err(|e| MLError::ComputationError(format!("Shape error: {}", e)))?;
698 Ok(Self::new(array, false))
699 }
700
701 pub fn quantum_observable(name: &str, num_qubits: usize) -> Result<Self> {
703 match name {
704 "pauli_z_all" => {
705 let size = 1 << num_qubits;
706 let mut data = ArrayD::zeros(IxDyn(&[size, size]));
707 for i in 0..size {
708 let parity = i.count_ones() % 2;
709 data[[i, i]] = if parity == 0 { 1.0 } else { -1.0 };
710 }
711 Ok(Self::new(data, false))
712 }
713 _ => Err(MLError::InvalidConfiguration(format!(
714 "Unknown observable: {}",
715 name
716 ))),
717 }
718 }
719}
720
721pub mod integration {
723 use super::*;
724
725 pub fn from_ndarray<D: Dimension>(arr: Array<f64, D>) -> SciRS2Array {
727 SciRS2Array::from_array(arr)
728 }
729
730 pub fn to_ndarray<D: Dimension>(arr: &SciRS2Array) -> Result<Array<f64, D>> {
732 arr.data
733 .view()
734 .into_dimensionality::<D>()
735 .map(|v| v.to_owned())
736 .map_err(|e| MLError::ComputationError(format!("Dimension error: {}", e)))
737 }
738
739 pub fn create_optimizer(optimizer_type: &str, config: HashMap<String, f64>) -> SciRS2Optimizer {
741 let mut optimizer = SciRS2Optimizer::new(optimizer_type);
742 for (key, value) in config {
743 optimizer = optimizer.with_config(key, value);
744 }
745 optimizer
746 }
747
748 pub fn setup_distributed(world_size: usize, rank: usize) -> SciRS2DistributedTrainer {
750 SciRS2DistributedTrainer::new(world_size, rank)
751 }
752}
753
754#[cfg(test)]
755mod tests {
756 use super::*;
757 use scirs2_core::ndarray::Array2;
758
759 #[test]
760 fn test_scirs2_array_creation() {
761 let arr = Array2::from_shape_vec((2, 2), vec![1.0, 2.0, 3.0, 4.0]).unwrap();
762 let scirs2_arr = SciRS2Array::from_array(arr);
763
764 assert_eq!(scirs2_arr.data.shape(), &[2, 2]);
765 assert!(!scirs2_arr.requires_grad);
766 }
767
768 #[test]
769 fn test_scirs2_array_with_grad() {
770 let arr = Array2::from_shape_vec((2, 2), vec![1.0, 2.0, 3.0, 4.0]).unwrap();
771 let scirs2_arr = SciRS2Array::with_grad(arr);
772
773 assert!(scirs2_arr.requires_grad);
774 assert!(scirs2_arr.grad.is_some());
775 }
776
777 #[test]
778 fn test_scirs2_matmul() {
779 let arr1 = Array2::from_shape_vec((2, 3), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).unwrap();
780 let arr2 = Array2::from_shape_vec((3, 2), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).unwrap();
781
782 let scirs2_arr1 = SciRS2Array::from_array(arr1);
783 let scirs2_arr2 = SciRS2Array::from_array(arr2);
784
785 let result = scirs2_arr1.matmul(&scirs2_arr2).unwrap();
786 assert_eq!(result.data.shape(), &[2, 2]);
787 }
788
789 #[test]
790 fn test_scirs2_optimizer() {
791 let mut optimizer = SciRS2Optimizer::new("adam")
792 .with_config("learning_rate", 0.001)
793 .with_config("beta1", 0.9);
794
795 let mut params = HashMap::new();
796 let param_arr = SciRS2Array::with_grad(Array1::from_vec(vec![1.0, 2.0, 3.0]));
797 params.insert("weight".to_string(), param_arr);
798
799 let result = optimizer.step(&mut params);
800 assert!(result.is_ok());
801 }
802
803 #[test]
804 fn test_integration_helpers() {
805 let arr = Array2::from_shape_vec((2, 2), vec![1.0, 2.0, 3.0, 4.0]).unwrap();
806 let scirs2_arr = integration::from_ndarray(arr.clone());
807
808 let back_to_ndarray: Array2<f64> = integration::to_ndarray(&scirs2_arr).unwrap();
809 assert_eq!(arr, back_to_ndarray);
810 }
811}