Skip to main content

quantrs2_ml/torchquantum/layer/
tquccsdlayer_traits.rs

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