neuroforge/
quantum_neuron.rs1use rand::Rng;
2use std::f64::consts::PI;
3
4pub struct QuantumNeuron {
5 phase: f64,
6 superposition: bool,
7}
8
9impl QuantumNeuron {
10 pub fn new() -> Self {
11 QuantumNeuron {
12 phase: 0.0,
13 superposition: false,
14 }
15 }
16
17 pub fn activate(&mut self, input: f64, emotional_state: f64) -> f64 {
18 let mut rng = rand::thread_rng();
19
20 self.phase += input * PI * 2.0;
21 self.phase %= 2.0 * PI;
22
23 if rng.gen::<f64>() < emotional_state {
24 self.superposition = !self.superposition;
25 }
26
27 if self.superposition {
28 (self.phase.sin() + self.phase.cos()) / 2.0
29 } else {
30 self.phase.sin()
31 }
32 }
33
34 pub fn calculate_gradient(&self, error: f64) -> f64 {
35 if self.superposition {
36 error * (self.phase.cos() - self.phase.sin()) / 2.0
37 } else {
38 error * self.phase.cos()
39 }
40 }
41}