quantrs2_ml/torchquantum/gates/single_qubit/
tqglobalphase_traits.rs

1//! # TQGlobalPhase - Trait Implementations
2//!
3//! This module contains trait implementations for `TQGlobalPhase`.
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};
17
18use super::types::TQGlobalPhase;
19
20impl TQModule for TQGlobalPhase {
21    fn forward(&mut self, _qdev: &mut TQDevice) -> Result<()> {
22        Ok(())
23    }
24    fn parameters(&self) -> Vec<TQParameter> {
25        self.params.iter().cloned().collect()
26    }
27    fn n_wires(&self) -> Option<usize> {
28        Some(1)
29    }
30    fn set_n_wires(&mut self, _n_wires: usize) {}
31    fn is_static_mode(&self) -> bool {
32        self.static_mode
33    }
34    fn static_on(&mut self) {
35        self.static_mode = true;
36    }
37    fn static_off(&mut self) {
38        self.static_mode = false;
39    }
40    fn name(&self) -> &str {
41        "GlobalPhase"
42    }
43}
44
45impl TQOperator for TQGlobalPhase {
46    fn num_wires(&self) -> WiresEnum {
47        WiresEnum::Fixed(1)
48    }
49    fn num_params(&self) -> NParamsEnum {
50        NParamsEnum::Fixed(1)
51    }
52    fn get_matrix(&self, params: Option<&[f64]>) -> Array2<CType> {
53        let phi = params.and_then(|p| p.first().copied()).unwrap_or(0.0);
54        let sign = if self.inverse { -1.0 } else { 1.0 };
55        let phase = CType::new(0.0, sign * phi).exp();
56        Array2::from_shape_vec(
57            (2, 2),
58            vec![phase, CType::new(0.0, 0.0), CType::new(0.0, 0.0), phase],
59        )
60        .unwrap_or_else(|_| Array2::eye(2).mapv(|x| CType::new(x, 0.0)))
61    }
62    fn apply(&mut self, qdev: &mut TQDevice, wires: &[usize]) -> Result<()> {
63        let params: Vec<f64> = self
64            .params
65            .as_ref()
66            .map(|p| p.data.iter().copied().collect())
67            .unwrap_or_default();
68        self.apply_with_params(qdev, wires, Some(&params))
69    }
70    fn apply_with_params(
71        &mut self,
72        qdev: &mut TQDevice,
73        wires: &[usize],
74        params: Option<&[f64]>,
75    ) -> Result<()> {
76        let matrix = self.get_matrix(params);
77        qdev.apply_single_qubit_gate(wires[0], &matrix)?;
78        Ok(())
79    }
80    fn has_params(&self) -> bool {
81        self.has_params
82    }
83    fn trainable(&self) -> bool {
84        self.trainable
85    }
86    fn inverse(&self) -> bool {
87        self.inverse
88    }
89    fn set_inverse(&mut self, inverse: bool) {
90        self.inverse = inverse;
91    }
92}