Skip to main content

aria_engine_core/
state.rs

1use num_complex::Complex64;
2use serde::{Deserialize, Serialize};
3
4use crate::graph::Graph;
5
6/// Full Aria state — the discrete Spec variables.
7///
8/// Observable core: ⟨ψ, z, G, t⟩.
9/// prev_res is auxiliary history for Inv2 only (TLA prevRes).
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct State {
12    /// Optical field amplitudes ψ ∈ C^N — 𝔸1
13    pub psi: Vec<Complex64>,
14    /// JEPA latent z ∈ Z — 𝔸2
15    pub z: Vec<f64>,
16    /// Experience/thought graph G — 𝔸3
17    pub g: Graph,
18    /// Discrete step counter t ∈ N
19    pub t: u64,
20    /// Auxiliary: previous residual for Inv2
21    pub prev_res: f64,
22    /// Initial field energy ‖ψ₀‖₂ — cached for Inv1
23    pub energy_0: f64,
24}
25
26impl State {
27    /// Compute the current field energy ‖ψ‖₂.
28    pub fn energy(&self) -> f64 {
29        self.psi
30            .iter()
31            .map(|c| c.norm_sqr())
32            .sum::<f64>()
33            .sqrt()
34    }
35
36    /// Compute the current JEPA residual:
37    /// Res(ψ, z, t) = d(z, P(I(ψ), a_t))
38    ///
39    /// This is computed by the Predictor backend; see engine for usage.
40    /// Here we provide the distance function used in Inv2 comparison.
41    pub fn residual(&self, predicted_z: &[f64]) -> f64 {
42        euclidean_distance(&self.z, predicted_z)
43    }
44}
45
46/// Euclidean distance in Z.
47pub fn euclidean_distance(a: &[f64], b: &[f64]) -> f64 {
48    a.iter()
49        .zip(b.iter())
50        .map(|(x, y)| (x - y).powi(2))
51        .sum::<f64>()
52        .sqrt()
53}
54
55/// Compute ‖ψ‖₂ energy of a complex vector.
56pub fn field_energy(psi: &[Complex64]) -> f64 {
57    psi.iter()
58        .map(|c| c.norm_sqr())
59        .sum::<f64>()
60        .sqrt()
61}