quantrs2_ml/torchquantum/gates/single_qubit/
tqu2_traits.rs

1//! # TQU2 - Trait Implementations
2//!
3//! This module contains trait implementations for `TQU2`.
4//!
5//! ## Implemented Traits
6//!
7//! - `TQModule`
8//! - `TQOperator`
9//!
10//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
11
12use super::super::super::{
13    CType, NParamsEnum, OpHistoryEntry, TQDevice, TQModule, TQOperator, TQParameter, WiresEnum,
14};
15use crate::error::{MLError, Result};
16use scirs2_core::ndarray::{Array1, Array2, ArrayD, IxDyn};
17use std::f64::consts::PI;
18
19use super::types::TQU2;
20
21impl TQModule for TQU2 {
22    fn forward(&mut self, _qdev: &mut TQDevice) -> Result<()> {
23        Err(MLError::InvalidConfiguration(
24            "Use apply() instead of forward() for operators".to_string(),
25        ))
26    }
27    fn parameters(&self) -> Vec<TQParameter> {
28        self.params.iter().cloned().collect()
29    }
30    fn n_wires(&self) -> Option<usize> {
31        Some(1)
32    }
33    fn set_n_wires(&mut self, _n_wires: usize) {}
34    fn is_static_mode(&self) -> bool {
35        self.static_mode
36    }
37    fn static_on(&mut self) {
38        self.static_mode = true;
39    }
40    fn static_off(&mut self) {
41        self.static_mode = false;
42    }
43    fn name(&self) -> &str {
44        "U2"
45    }
46    fn zero_grad(&mut self) {
47        if let Some(ref mut p) = self.params {
48            p.zero_grad();
49        }
50    }
51}
52
53impl TQOperator for TQU2 {
54    fn num_wires(&self) -> WiresEnum {
55        WiresEnum::Fixed(1)
56    }
57    fn num_params(&self) -> NParamsEnum {
58        NParamsEnum::Fixed(2)
59    }
60    fn get_matrix(&self, params: Option<&[f64]>) -> Array2<CType> {
61        let (phi, lambda) = if let Some(p) = params {
62            (
63                p.first().copied().unwrap_or(0.0),
64                p.get(1).copied().unwrap_or(0.0),
65            )
66        } else if let Some(ref p) = self.params {
67            (p.data[[0, 0]], p.data[[0, 1]])
68        } else {
69            (0.0, 0.0)
70        };
71        let (phi, lambda) = if self.inverse {
72            (-lambda - PI, -phi + PI)
73        } else {
74            (phi, lambda)
75        };
76        let inv_sqrt2 = 1.0 / 2.0_f64.sqrt();
77        Array2::from_shape_vec(
78            (2, 2),
79            vec![
80                CType::new(inv_sqrt2, 0.0),
81                CType::from_polar(-inv_sqrt2, lambda),
82                CType::from_polar(inv_sqrt2, phi),
83                CType::from_polar(inv_sqrt2, phi + lambda),
84            ],
85        )
86        .unwrap_or_else(|_| Array2::eye(2).mapv(|x| CType::new(x, 0.0)))
87    }
88    fn apply(&mut self, qdev: &mut TQDevice, wires: &[usize]) -> Result<()> {
89        self.apply_with_params(qdev, wires, None)
90    }
91    fn apply_with_params(
92        &mut self,
93        qdev: &mut TQDevice,
94        wires: &[usize],
95        params: Option<&[f64]>,
96    ) -> Result<()> {
97        if wires.is_empty() {
98            return Err(MLError::InvalidConfiguration(
99                "U2 gate requires exactly 1 wire".to_string(),
100            ));
101        }
102        let matrix = self.get_matrix(params);
103        qdev.apply_single_qubit_gate(wires[0], &matrix)?;
104        if qdev.record_op {
105            qdev.record_operation(OpHistoryEntry {
106                name: "u2".to_string(),
107                wires: wires.to_vec(),
108                params: params.map(|p| p.to_vec()),
109                inverse: self.inverse,
110                trainable: self.trainable,
111            });
112        }
113        Ok(())
114    }
115    fn has_params(&self) -> bool {
116        self.has_params
117    }
118    fn trainable(&self) -> bool {
119        self.trainable
120    }
121    fn inverse(&self) -> bool {
122        self.inverse
123    }
124    fn set_inverse(&mut self, inverse: bool) {
125        self.inverse = inverse;
126    }
127}