quantrs2_ml/torchquantum/gates/single_qubit/
tqu3_traits.rs1use 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::TQU3;
19
20impl TQModule for TQU3 {
21 fn forward(&mut self, _qdev: &mut TQDevice) -> Result<()> {
22 Err(MLError::InvalidConfiguration(
23 "Use apply() instead of forward() for operators".to_string(),
24 ))
25 }
26 fn parameters(&self) -> Vec<TQParameter> {
27 self.params.iter().cloned().collect()
28 }
29 fn n_wires(&self) -> Option<usize> {
30 Some(1)
31 }
32 fn set_n_wires(&mut self, _n_wires: usize) {}
33 fn is_static_mode(&self) -> bool {
34 self.static_mode
35 }
36 fn static_on(&mut self) {
37 self.static_mode = true;
38 }
39 fn static_off(&mut self) {
40 self.static_mode = false;
41 }
42 fn name(&self) -> &str {
43 "U3"
44 }
45 fn zero_grad(&mut self) {
46 if let Some(ref mut p) = self.params {
47 p.zero_grad();
48 }
49 }
50}
51
52impl TQOperator for TQU3 {
53 fn num_wires(&self) -> WiresEnum {
54 WiresEnum::Fixed(1)
55 }
56 fn num_params(&self) -> NParamsEnum {
57 NParamsEnum::Fixed(3)
58 }
59 fn get_matrix(&self, params: Option<&[f64]>) -> Array2<CType> {
60 let (theta, phi, lambda) = if let Some(p) = params {
61 (
62 p.first().copied().unwrap_or(0.0),
63 p.get(1).copied().unwrap_or(0.0),
64 p.get(2).copied().unwrap_or(0.0),
65 )
66 } else if let Some(ref p) = self.params {
67 (p.data[[0, 0]], p.data[[0, 1]], p.data[[0, 2]])
68 } else {
69 (0.0, 0.0, 0.0)
70 };
71 let (theta, phi, lambda) = if self.inverse {
72 (-theta, -lambda, -phi)
73 } else {
74 (theta, phi, lambda)
75 };
76 let cos_half = (theta / 2.0).cos();
77 let sin_half = (theta / 2.0).sin();
78 Array2::from_shape_vec(
79 (2, 2),
80 vec![
81 CType::new(cos_half, 0.0),
82 CType::from_polar(-sin_half, lambda),
83 CType::from_polar(sin_half, phi),
84 CType::from_polar(cos_half, phi + lambda),
85 ],
86 )
87 .unwrap_or_else(|_| Array2::eye(2).mapv(|x| CType::new(x, 0.0)))
88 }
89 fn apply(&mut self, qdev: &mut TQDevice, wires: &[usize]) -> Result<()> {
90 self.apply_with_params(qdev, wires, None)
91 }
92 fn apply_with_params(
93 &mut self,
94 qdev: &mut TQDevice,
95 wires: &[usize],
96 params: Option<&[f64]>,
97 ) -> Result<()> {
98 if wires.is_empty() {
99 return Err(MLError::InvalidConfiguration(
100 "U3 gate requires exactly 1 wire".to_string(),
101 ));
102 }
103 let matrix = self.get_matrix(params);
104 qdev.apply_single_qubit_gate(wires[0], &matrix)?;
105 if qdev.record_op {
106 qdev.record_operation(OpHistoryEntry {
107 name: "u3".to_string(),
108 wires: wires.to_vec(),
109 params: params.map(|p| p.to_vec()),
110 inverse: self.inverse,
111 trainable: self.trainable,
112 });
113 }
114 Ok(())
115 }
116 fn has_params(&self) -> bool {
117 self.has_params
118 }
119 fn trainable(&self) -> bool {
120 self.trainable
121 }
122 fn inverse(&self) -> bool {
123 self.inverse
124 }
125 fn set_inverse(&mut self, inverse: bool) {
126 self.inverse = inverse;
127 }
128}