quantrs2_ml/torchquantum/gates/two_qubit/special/
tqxxminusyy_traits.rs1use 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::TQXXMinusYY;
20
21impl Default for TQXXMinusYY {
22 fn default() -> Self {
23 Self::new(true, true)
24 }
25}
26
27impl TQModule for TQXXMinusYY {
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 self.params.iter().cloned().collect()
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 "XXMinusYY"
51 }
52 fn zero_grad(&mut self) {
53 if let Some(ref mut p) = self.params {
54 p.zero_grad();
55 }
56 }
57}
58
59impl TQOperator for TQXXMinusYY {
60 fn num_wires(&self) -> WiresEnum {
61 WiresEnum::Fixed(2)
62 }
63 fn num_params(&self) -> NParamsEnum {
64 NParamsEnum::Fixed(2)
65 }
66 fn get_matrix(&self, params: Option<&[f64]>) -> Array2<CType> {
67 let theta = params
68 .and_then(|p| p.first().copied())
69 .or_else(|| self.params.as_ref().map(|p| p.data[[0, 0]]))
70 .unwrap_or(0.0);
71 let beta = params
72 .and_then(|p| p.get(1).copied())
73 .or_else(|| self.params.as_ref().map(|p| p.data[[0, 1]]))
74 .unwrap_or(0.0);
75 let theta = if self.inverse { -theta } else { theta };
76 let half_theta = theta / 2.0;
77 let c = half_theta.cos();
78 let s = half_theta.sin();
79 let exp_pos = CType::from_polar(1.0, beta);
80 let exp_neg = CType::from_polar(1.0, -beta);
81 Array2::from_shape_vec(
82 (4, 4),
83 vec![
84 CType::new(c, 0.0),
85 CType::new(0.0, 0.0),
86 CType::new(0.0, 0.0),
87 CType::new(0.0, -s) * exp_neg,
88 CType::new(0.0, 0.0),
89 CType::new(1.0, 0.0),
90 CType::new(0.0, 0.0),
91 CType::new(0.0, 0.0),
92 CType::new(0.0, 0.0),
93 CType::new(0.0, 0.0),
94 CType::new(1.0, 0.0),
95 CType::new(0.0, 0.0),
96 CType::new(0.0, -s) * exp_pos,
97 CType::new(0.0, 0.0),
98 CType::new(0.0, 0.0),
99 CType::new(c, 0.0),
100 ],
101 )
102 .unwrap_or_else(|_| Array2::eye(4).mapv(|x| CType::new(x, 0.0)))
103 }
104 fn apply(&mut self, qdev: &mut TQDevice, wires: &[usize]) -> Result<()> {
105 self.apply_with_params(qdev, wires, None)
106 }
107 fn apply_with_params(
108 &mut self,
109 qdev: &mut TQDevice,
110 wires: &[usize],
111 params: Option<&[f64]>,
112 ) -> Result<()> {
113 if wires.len() < 2 {
114 return Err(MLError::InvalidConfiguration(
115 "XXMinusYY gate requires exactly 2 wires".to_string(),
116 ));
117 }
118 let matrix = self.get_matrix(params);
119 qdev.apply_two_qubit_gate(wires[0], wires[1], &matrix)?;
120 if qdev.record_op {
121 qdev.record_operation(OpHistoryEntry {
122 name: "xxmyy".to_string(),
123 wires: wires.to_vec(),
124 params: params.map(|p| p.to_vec()),
125 inverse: self.inverse,
126 trainable: self.trainable,
127 });
128 }
129 Ok(())
130 }
131 fn has_params(&self) -> bool {
132 self.has_params
133 }
134 fn trainable(&self) -> bool {
135 self.trainable
136 }
137 fn inverse(&self) -> bool {
138 self.inverse
139 }
140 fn set_inverse(&mut self, inverse: bool) {
141 self.inverse = inverse;
142 }
143}