Skip to main content

quantrs2_ml/torchquantum/layer/
tqop1qalllayer_traits.rs

1//! # TQOp1QAllLayer - Trait Implementations
2//!
3//! This module contains trait implementations for `TQOp1QAllLayer`.
4//!
5//! ## Implemented Traits
6//!
7//! - `TQModule`
8//!
9//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
10
11use 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::TQOp1QAllLayer;
16
17impl TQModule for TQOp1QAllLayer {
18    fn forward(&mut self, qdev: &mut TQDevice) -> Result<()> {
19        for (wire, gate) in self.gates.iter_mut().enumerate() {
20            gate.apply(qdev, &[wire])?;
21        }
22        Ok(())
23    }
24    fn parameters(&self) -> Vec<TQParameter> {
25        self.gates.iter().flat_map(|g| g.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    }
33    fn is_static_mode(&self) -> bool {
34        self.static_mode
35    }
36    fn static_on(&mut self) {
37        self.static_mode = true;
38        for gate in &mut self.gates {
39            gate.static_on();
40        }
41    }
42    fn static_off(&mut self) {
43        self.static_mode = false;
44        for gate in &mut self.gates {
45            gate.static_off();
46        }
47    }
48    fn name(&self) -> &str {
49        "Op1QAllLayer"
50    }
51    fn zero_grad(&mut self) {
52        for gate in &mut self.gates {
53            gate.zero_grad();
54        }
55    }
56}