quantrs2_ml/torchquantum/layer/
tqop2qdenselayer_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::TQOp2QDenseLayer;
16
17impl TQModule for TQOp2QDenseLayer {
18 fn forward(&mut self, qdev: &mut TQDevice) -> Result<()> {
19 let mut gate_idx = 0;
20 for i in 0..self.n_wires {
21 for j in (i + 1)..self.n_wires {
22 if gate_idx < self.gates.len() {
23 self.gates[gate_idx].apply(qdev, &[i, j])?;
24 gate_idx += 1;
25 }
26 }
27 }
28 Ok(())
29 }
30 fn parameters(&self) -> Vec<TQParameter> {
31 self.gates.iter().flat_map(|g| g.parameters()).collect()
32 }
33 fn n_wires(&self) -> Option<usize> {
34 Some(self.n_wires)
35 }
36 fn set_n_wires(&mut self, n_wires: usize) {
37 self.n_wires = n_wires;
38 }
39 fn is_static_mode(&self) -> bool {
40 self.static_mode
41 }
42 fn static_on(&mut self) {
43 self.static_mode = true;
44 for gate in &mut self.gates {
45 gate.static_on();
46 }
47 }
48 fn static_off(&mut self) {
49 self.static_mode = false;
50 for gate in &mut self.gates {
51 gate.static_off();
52 }
53 }
54 fn name(&self) -> &str {
55 "Op2QDenseLayer"
56 }
57 fn zero_grad(&mut self) {
58 for gate in &mut self.gates {
59 gate.zero_grad();
60 }
61 }
62}