quantrs2_ml/torchquantum/gates/single_qubit/
tqt_traits.rs

1//! # TQT - Trait Implementations
2//!
3//! This module contains trait implementations for `TQT`.
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};
18use std::f64::consts::PI;
19
20use super::types::TQT;
21
22impl Default for TQT {
23    fn default() -> Self {
24        Self::new()
25    }
26}
27
28impl TQModule for TQT {
29    fn forward(&mut self, _qdev: &mut TQDevice) -> Result<()> {
30        Err(MLError::InvalidConfiguration(
31            "Use apply() instead of forward() for operators".to_string(),
32        ))
33    }
34    fn parameters(&self) -> Vec<TQParameter> {
35        Vec::new()
36    }
37    fn n_wires(&self) -> Option<usize> {
38        Some(1)
39    }
40    fn set_n_wires(&mut self, _n_wires: usize) {}
41    fn is_static_mode(&self) -> bool {
42        self.static_mode
43    }
44    fn static_on(&mut self) {
45        self.static_mode = true;
46    }
47    fn static_off(&mut self) {
48        self.static_mode = false;
49    }
50    fn name(&self) -> &str {
51        "T"
52    }
53}
54
55impl TQOperator for TQT {
56    fn num_wires(&self) -> WiresEnum {
57        WiresEnum::Fixed(1)
58    }
59    fn num_params(&self) -> NParamsEnum {
60        NParamsEnum::Fixed(0)
61    }
62    fn get_matrix(&self, _params: Option<&[f64]>) -> Array2<CType> {
63        let phase = if self.inverse {
64            CType::from_polar(1.0, -PI / 4.0)
65        } else {
66            CType::from_polar(1.0, PI / 4.0)
67        };
68        Array2::from_shape_vec(
69            (2, 2),
70            vec![
71                CType::new(1.0, 0.0),
72                CType::new(0.0, 0.0),
73                CType::new(0.0, 0.0),
74                phase,
75            ],
76        )
77        .unwrap_or_else(|_| Array2::eye(2).mapv(|x| CType::new(x, 0.0)))
78    }
79    fn apply(&mut self, qdev: &mut TQDevice, wires: &[usize]) -> Result<()> {
80        self.apply_with_params(qdev, wires, None)
81    }
82    fn apply_with_params(
83        &mut self,
84        qdev: &mut TQDevice,
85        wires: &[usize],
86        _params: Option<&[f64]>,
87    ) -> Result<()> {
88        if wires.is_empty() {
89            return Err(MLError::InvalidConfiguration(
90                "T gate requires exactly 1 wire".to_string(),
91            ));
92        }
93        let matrix = self.get_matrix(None);
94        qdev.apply_single_qubit_gate(wires[0], &matrix)?;
95        if qdev.record_op {
96            qdev.record_operation(OpHistoryEntry {
97                name: "t".to_string(),
98                wires: wires.to_vec(),
99                params: None,
100                inverse: self.inverse,
101                trainable: false,
102            });
103        }
104        Ok(())
105    }
106    fn has_params(&self) -> bool {
107        false
108    }
109    fn trainable(&self) -> bool {
110        false
111    }
112    fn inverse(&self) -> bool {
113        self.inverse
114    }
115    fn set_inverse(&mut self, inverse: bool) {
116        self.inverse = inverse;
117    }
118}