Skip to main content

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

1//! # TQSSWAP - Trait Implementations
2//!
3//! This module contains trait implementations for `TQSSWAP`.
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::TQSSWAP;
20
21impl Default for TQSSWAP {
22    fn default() -> Self {
23        Self::new()
24    }
25}
26
27impl TQModule for TQSSWAP {
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        "SSWAP"
51    }
52}
53
54impl TQOperator for TQSSWAP {
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 (a, b) = if self.inverse {
63            (CType::new(0.5, -0.5), CType::new(0.5, 0.5))
64        } else {
65            (CType::new(0.5, 0.5), CType::new(0.5, -0.5))
66        };
67        Array2::from_shape_vec(
68            (4, 4),
69            vec![
70                CType::new(1.0, 0.0),
71                CType::new(0.0, 0.0),
72                CType::new(0.0, 0.0),
73                CType::new(0.0, 0.0),
74                CType::new(0.0, 0.0),
75                a,
76                b,
77                CType::new(0.0, 0.0),
78                CType::new(0.0, 0.0),
79                b,
80                a,
81                CType::new(0.0, 0.0),
82                CType::new(0.0, 0.0),
83                CType::new(0.0, 0.0),
84                CType::new(0.0, 0.0),
85                CType::new(1.0, 0.0),
86            ],
87        )
88        .unwrap_or_else(|_| Array2::eye(4).mapv(|x| CType::new(x, 0.0)))
89    }
90    fn apply(&mut self, qdev: &mut TQDevice, wires: &[usize]) -> Result<()> {
91        self.apply_with_params(qdev, wires, None)
92    }
93    fn apply_with_params(
94        &mut self,
95        qdev: &mut TQDevice,
96        wires: &[usize],
97        _params: Option<&[f64]>,
98    ) -> Result<()> {
99        if wires.len() < 2 {
100            return Err(MLError::InvalidConfiguration(
101                "SSWAP gate requires exactly 2 wires".to_string(),
102            ));
103        }
104        let matrix = self.get_matrix(None);
105        qdev.apply_two_qubit_gate(wires[0], wires[1], &matrix)?;
106        if qdev.record_op {
107            qdev.record_operation(OpHistoryEntry {
108                name: "sswap".to_string(),
109                wires: wires.to_vec(),
110                params: None,
111                inverse: self.inverse,
112                trainable: false,
113            });
114        }
115        Ok(())
116    }
117    fn has_params(&self) -> bool {
118        false
119    }
120    fn trainable(&self) -> bool {
121        false
122    }
123    fn inverse(&self) -> bool {
124        self.inverse
125    }
126    fn set_inverse(&mut self, inverse: bool) {
127        self.inverse = inverse;
128    }
129}