Skip to main content

quantrs2_ml/torchquantum/gates/two_qubit/special/
tqecr_traits.rs

1//! # TQECR - Trait Implementations
2//!
3//! This module contains trait implementations for `TQECR`.
4//!
5//! ## Implemented Traits
6//!
7//! - `Default`
8//! - `TQModule`
9//! - `TQOperator`
10//!
11//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
12
13use crate::error::{MLError, Result};
14use crate::torchquantum::{
15    CType, NParamsEnum, OpHistoryEntry, TQDevice, TQModule, TQOperator, TQParameter, WiresEnum,
16};
17use scirs2_core::ndarray::{Array2, ArrayD, IxDyn};
18
19use super::types::TQECR;
20
21impl Default for TQECR {
22    fn default() -> Self {
23        Self::new()
24    }
25}
26
27impl TQModule for TQECR {
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(2)
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        "ECR"
51    }
52}
53
54impl TQOperator for TQECR {
55    fn num_wires(&self) -> WiresEnum {
56        WiresEnum::Fixed(2)
57    }
58    fn num_params(&self) -> NParamsEnum {
59        NParamsEnum::Fixed(0)
60    }
61    fn get_matrix(&self, _params: Option<&[f64]>) -> Array2<CType> {
62        let scale = 1.0 / std::f64::consts::SQRT_2;
63        let (sign_i, sign_neg_i) = if self.inverse {
64            (CType::new(0.0, -scale), CType::new(0.0, scale))
65        } else {
66            (CType::new(0.0, scale), CType::new(0.0, -scale))
67        };
68        Array2::from_shape_vec(
69            (4, 4),
70            vec![
71                CType::new(0.0, 0.0),
72                CType::new(0.0, 0.0),
73                CType::new(scale, 0.0),
74                sign_i,
75                CType::new(0.0, 0.0),
76                CType::new(0.0, 0.0),
77                sign_i,
78                CType::new(scale, 0.0),
79                CType::new(scale, 0.0),
80                sign_neg_i,
81                CType::new(0.0, 0.0),
82                CType::new(0.0, 0.0),
83                sign_neg_i,
84                CType::new(scale, 0.0),
85                CType::new(0.0, 0.0),
86                CType::new(0.0, 0.0),
87            ],
88        )
89        .unwrap_or_else(|_| Array2::eye(4).mapv(|x| CType::new(x, 0.0)))
90    }
91    fn apply(&mut self, qdev: &mut TQDevice, wires: &[usize]) -> Result<()> {
92        self.apply_with_params(qdev, wires, None)
93    }
94    fn apply_with_params(
95        &mut self,
96        qdev: &mut TQDevice,
97        wires: &[usize],
98        _params: Option<&[f64]>,
99    ) -> Result<()> {
100        if wires.len() < 2 {
101            return Err(MLError::InvalidConfiguration(
102                "ECR gate requires exactly 2 wires".to_string(),
103            ));
104        }
105        let matrix = self.get_matrix(None);
106        qdev.apply_two_qubit_gate(wires[0], wires[1], &matrix)?;
107        if qdev.record_op {
108            qdev.record_operation(OpHistoryEntry {
109                name: "ecr".to_string(),
110                wires: wires.to_vec(),
111                params: None,
112                inverse: self.inverse,
113                trainable: false,
114            });
115        }
116        Ok(())
117    }
118    fn has_params(&self) -> bool {
119        false
120    }
121    fn trainable(&self) -> bool {
122        false
123    }
124    fn inverse(&self) -> bool {
125        self.inverse
126    }
127    fn set_inverse(&mut self, inverse: bool) {
128        self.inverse = inverse;
129    }
130}