quantrs2_ml/torchquantum/gates/two_qubit/special/
tqfsimgate_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::TQFSimGate;
20
21impl Default for TQFSimGate {
22 fn default() -> Self {
23 Self::new(true, true)
24 }
25}
26
27impl TQModule for TQFSimGate {
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 "fSim"
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 TQFSimGate {
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 phi = 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 phi = if self.inverse { -phi } else { phi };
77 let c = theta.cos();
78 let s = theta.sin();
79 let exp_neg_i_phi = CType::from_polar(1.0, -phi);
80 Array2::from_shape_vec(
81 (4, 4),
82 vec![
83 CType::new(1.0, 0.0),
84 CType::new(0.0, 0.0),
85 CType::new(0.0, 0.0),
86 CType::new(0.0, 0.0),
87 CType::new(0.0, 0.0),
88 CType::new(c, 0.0),
89 CType::new(0.0, -s),
90 CType::new(0.0, 0.0),
91 CType::new(0.0, 0.0),
92 CType::new(0.0, -s),
93 CType::new(c, 0.0),
94 CType::new(0.0, 0.0),
95 CType::new(0.0, 0.0),
96 CType::new(0.0, 0.0),
97 CType::new(0.0, 0.0),
98 exp_neg_i_phi,
99 ],
100 )
101 .unwrap_or_else(|_| Array2::eye(4).mapv(|x| CType::new(x, 0.0)))
102 }
103 fn apply(&mut self, qdev: &mut TQDevice, wires: &[usize]) -> Result<()> {
104 self.apply_with_params(qdev, wires, None)
105 }
106 fn apply_with_params(
107 &mut self,
108 qdev: &mut TQDevice,
109 wires: &[usize],
110 params: Option<&[f64]>,
111 ) -> Result<()> {
112 if wires.len() < 2 {
113 return Err(MLError::InvalidConfiguration(
114 "fSim gate requires exactly 2 wires".to_string(),
115 ));
116 }
117 let matrix = self.get_matrix(params);
118 qdev.apply_two_qubit_gate(wires[0], wires[1], &matrix)?;
119 if qdev.record_op {
120 qdev.record_operation(OpHistoryEntry {
121 name: "fsim".to_string(),
122 wires: wires.to_vec(),
123 params: params.map(|p| p.to_vec()),
124 inverse: self.inverse,
125 trainable: self.trainable,
126 });
127 }
128 Ok(())
129 }
130 fn has_params(&self) -> bool {
131 self.has_params
132 }
133 fn trainable(&self) -> bool {
134 self.trainable
135 }
136 fn inverse(&self) -> bool {
137 self.inverse
138 }
139 fn set_inverse(&mut self, inverse: bool) {
140 self.inverse = inverse;
141 }
142}