quantrs2_ml/torchquantum/layer/
tqtwolocallayer_traits.rs1use crate::error::{MLError, Result};
12use crate::torchquantum::{TQDevice, TQModule, TQOperator, TQParameter};
13
14use super::functions::{create_single_qubit_gate, create_two_qubit_gate};
15use super::types::TQTwoLocalLayer;
16
17impl TQModule for TQTwoLocalLayer {
18 fn forward(&mut self, qdev: &mut TQDevice) -> Result<()> {
19 for layer in &mut self.layers {
20 layer.forward(qdev)?;
21 }
22 Ok(())
23 }
24 fn parameters(&self) -> Vec<TQParameter> {
25 self.layers.iter().flat_map(|l| l.parameters()).collect()
26 }
27 fn n_wires(&self) -> Option<usize> {
28 Some(self.n_wires)
29 }
30 fn set_n_wires(&mut self, n_wires: usize) {
31 self.n_wires = n_wires;
32 self.layers = Self::build_layers(
33 n_wires,
34 &self.rotation_ops,
35 &self.entanglement_ops,
36 self.entanglement_pattern,
37 self.reps,
38 self.skip_final_rotation,
39 );
40 }
41 fn is_static_mode(&self) -> bool {
42 self.static_mode
43 }
44 fn static_on(&mut self) {
45 self.static_mode = true;
46 for layer in &mut self.layers {
47 layer.static_on();
48 }
49 }
50 fn static_off(&mut self) {
51 self.static_mode = false;
52 for layer in &mut self.layers {
53 layer.static_off();
54 }
55 }
56 fn name(&self) -> &str {
57 "TwoLocalLayer"
58 }
59 fn zero_grad(&mut self) {
60 for layer in &mut self.layers {
61 layer.zero_grad();
62 }
63 }
64}