quantrs2_ml/torchquantum/gates/single_qubit/
tqi_traits.rs

1//! # TQI - Trait Implementations
2//!
3//! This module contains trait implementations for `TQI`.
4//!
5//! ## Implemented Traits
6//!
7//! - `Default`
8//! - `TQModule`
9//! - `TQOperator`
10//!
11//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
12
13use super::super::super::{
14    CType, NParamsEnum, OpHistoryEntry, TQDevice, TQModule, TQOperator, TQParameter, WiresEnum,
15};
16use crate::error::{MLError, Result};
17use scirs2_core::ndarray::{Array1, Array2, ArrayD, IxDyn};
18
19use super::types::TQI;
20
21impl Default for TQI {
22    fn default() -> Self {
23        Self::new()
24    }
25}
26
27impl TQModule for TQI {
28    fn forward(&mut self, _qdev: &mut TQDevice) -> Result<()> {
29        Ok(())
30    }
31    fn parameters(&self) -> Vec<TQParameter> {
32        Vec::new()
33    }
34    fn n_wires(&self) -> Option<usize> {
35        Some(1)
36    }
37    fn set_n_wires(&mut self, _n_wires: usize) {}
38    fn is_static_mode(&self) -> bool {
39        self.static_mode
40    }
41    fn static_on(&mut self) {
42        self.static_mode = true;
43    }
44    fn static_off(&mut self) {
45        self.static_mode = false;
46    }
47    fn name(&self) -> &str {
48        "I"
49    }
50}
51
52impl TQOperator for TQI {
53    fn num_wires(&self) -> WiresEnum {
54        WiresEnum::Fixed(1)
55    }
56    fn num_params(&self) -> NParamsEnum {
57        NParamsEnum::Fixed(0)
58    }
59    fn get_matrix(&self, _params: Option<&[f64]>) -> Array2<CType> {
60        Array2::eye(2).mapv(|x| CType::new(x, 0.0))
61    }
62    fn apply(&mut self, qdev: &mut TQDevice, wires: &[usize]) -> Result<()> {
63        let matrix = self.get_matrix(None);
64        qdev.apply_single_qubit_gate(wires[0], &matrix)?;
65        Ok(())
66    }
67    fn apply_with_params(
68        &mut self,
69        qdev: &mut TQDevice,
70        wires: &[usize],
71        _params: Option<&[f64]>,
72    ) -> Result<()> {
73        self.apply(qdev, wires)
74    }
75    fn has_params(&self) -> bool {
76        false
77    }
78    fn trainable(&self) -> bool {
79        false
80    }
81    fn inverse(&self) -> bool {
82        self.inverse
83    }
84    fn set_inverse(&mut self, inverse: bool) {
85        self.inverse = inverse;
86    }
87}