Skip to main content

quantrs2_ml/torchquantum/layer/
tqop2qalllayer_traits.rs

1//! # TQOp2QAllLayer - Trait Implementations
2//!
3//! This module contains trait implementations for `TQOp2QAllLayer`.
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::TQOp2QAllLayer;
16
17impl TQModule for TQOp2QAllLayer {
18    fn forward(&mut self, qdev: &mut TQDevice) -> Result<()> {
19        let n_pairs = if self.circular {
20            self.n_wires
21        } else {
22            self.n_wires.saturating_sub(self.jump)
23        };
24        for i in 0..n_pairs {
25            let wire0 = i;
26            let wire1 = (i + self.jump) % self.n_wires;
27            if i < self.gates.len() {
28                self.gates[i].apply(qdev, &[wire0, wire1])?;
29            }
30        }
31        Ok(())
32    }
33    fn parameters(&self) -> Vec<TQParameter> {
34        self.gates.iter().flat_map(|g| g.parameters()).collect()
35    }
36    fn n_wires(&self) -> Option<usize> {
37        Some(self.n_wires)
38    }
39    fn set_n_wires(&mut self, n_wires: usize) {
40        self.n_wires = n_wires;
41    }
42    fn is_static_mode(&self) -> bool {
43        self.static_mode
44    }
45    fn static_on(&mut self) {
46        self.static_mode = true;
47        for gate in &mut self.gates {
48            gate.static_on();
49        }
50    }
51    fn static_off(&mut self) {
52        self.static_mode = false;
53        for gate in &mut self.gates {
54            gate.static_off();
55        }
56    }
57    fn name(&self) -> &str {
58        "Op2QAllLayer"
59    }
60    fn zero_grad(&mut self) {
61        for gate in &mut self.gates {
62            gate.zero_grad();
63        }
64    }
65}