quantrs2_ml/torchquantum/gates/single_qubit/
tqhadamard_traits.rs

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