use std::fmt::Debug;
use crate::action::Action;
use crate::condition::Condition;
use crate::config::AriaConfig;
use crate::error::AriaError;
use crate::gates::{GateMonitor, GateReport};
use crate::graph::Graph;
use crate::invariants;
use crate::invariants::InvariantReport;
use crate::policy::{DiffPolicy, MatchPolicy};
use crate::scheduler::Scheduler;
use crate::state::State;
use crate::trace::Trace;
pub trait OpticalBackend: Debug + Send + Sync {
fn unitary_step(&self, t: u64, psi: &[num_complex::Complex64]) -> Vec<num_complex::Complex64>;
fn energy(&self, psi: &[num_complex::Complex64]) -> f64;
}
pub trait Predictor: Debug + Send + Sync {
fn embed(&self, psi: &[num_complex::Complex64]) -> Vec<f64>;
fn predict(&self, z: &[f64], a: Condition) -> Vec<f64>;
fn dist(&self, a: &[f64], b: &[f64]) -> f64;
}
pub trait GraphBackend: Debug + Send + Sync {
fn edit(
&self,
g: &Graph,
z: &[f64],
policy: MatchPolicy,
target: Option<&Graph>,
) -> Graph;
fn ok(&self, g: &Graph) -> bool;
}
pub trait Diffuser: Debug + Send + Sync {
fn diffuse(&self, g: &Graph, z: &[f64], policy: DiffPolicy) -> Vec<f64>;
}
#[derive(Debug)]
pub struct Engine<O, P, G, D>
where
O: OpticalBackend,
P: Predictor,
G: GraphBackend,
D: Diffuser,
{
config: AriaConfig,
optical: O,
predictor: P,
graph_backend: G,
diffuser: D,
}
impl<O, P, GB, D> Engine<O, P, GB, D>
where
O: OpticalBackend,
P: Predictor,
GB: GraphBackend,
D: Diffuser,
{
pub fn new(config: AriaConfig, optical: O, predictor: P, graph_backend: GB, diffuser: D) -> Self {
Engine {
config,
optical,
predictor,
graph_backend,
diffuser,
}
}
pub fn init(
&self,
psi0: Vec<num_complex::Complex64>,
g0: Graph,
a0: Condition,
) -> Result<State, AriaError> {
if self.config.n_modes == 0 {
return Err(AriaError::Config("n_modes must be ≥ 1".into()));
}
if self.config.latent_dim == 0 {
return Err(AriaError::Config("latent_dim must be ≥ 1".into()));
}
if psi0.len() != self.config.n_modes {
return Err(AriaError::Config(format!(
"ψ₀ has {} modes but config.n_modes = {}",
psi0.len(),
self.config.n_modes
)));
}
for (i, c) in psi0.iter().enumerate() {
if !c.re.is_finite() || !c.im.is_finite() {
return Err(AriaError::Config(format!(
"ψ₀[{}] = {} is not finite",
i, c
)));
}
}
let energy_0 = self.optical.energy(&psi0);
let z0 = self.predictor.embed(&psi0);
if z0.len() != self.config.latent_dim {
return Err(AriaError::Config(format!(
"embed(ψ₀) has dim {} but config.latent_dim = {} — backend/config mismatch",
z0.len(),
self.config.latent_dim
)));
}
let p0 = self.predictor.predict(&z0, a0);
if p0.len() != self.config.latent_dim {
return Err(AriaError::Config(format!(
"P(z) has dim {} but config.latent_dim = {} — backend/config mismatch",
p0.len(),
self.config.latent_dim
)));
}
let prev_res = self.predictor.dist(&z0, &p0);
Ok(State {
psi: psi0,
z: z0,
g: g0,
t: 0,
prev_res,
energy_0,
})
}
pub fn apply(
&self,
mut state: State,
action: Action,
a: Condition,
) -> Result<State, AriaError> {
match action {
Action::OpticalStep => {
let prev_psi = state.psi.clone();
let prev_prev_res = state.prev_res;
let pre_residual = self.compute_residual(&state, a);
state.psi = self.optical.unitary_step(state.t, &state.psi);
state.prev_res = pre_residual;
let post_residual = self.compute_residual(&state, a);
let report = invariants::check_all(
&state,
post_residual,
self.config.eps,
self.config.n_modes,
self.config.latent_dim,
);
if self.config.strict && !report.all_ok() {
if let Some(v) = invariants::violation_from_report(
&report, action, state.energy(), state.energy_0,
post_residual, state.prev_res, self.config.eps,
) {
state.psi = prev_psi;
state.prev_res = prev_prev_res;
return Err(AriaError::InvariantViolation(v));
}
}
}
Action::Predict => {
let prev_z = state.z.clone();
let prev_prev_res = state.prev_res;
let pre_residual = self.compute_residual(&state, a);
state.z = self.predictor.predict(&self.predictor.embed(&state.psi), a);
state.prev_res = pre_residual;
let post_residual = self.compute_residual(&state, a);
let report = invariants::check_all(
&state,
post_residual,
self.config.eps,
self.config.n_modes,
self.config.latent_dim,
);
if self.config.strict && !report.all_ok() {
if let Some(v) = invariants::violation_from_report(
&report, action, state.energy(), state.energy_0,
post_residual, state.prev_res, self.config.eps,
) {
state.z = prev_z;
state.prev_res = prev_prev_res;
return Err(AriaError::InvariantViolation(v));
}
}
}
Action::Match => {
let prev_g = state.g.clone();
let prev_prev_res = state.prev_res;
let pre_residual = self.compute_residual(&state, a);
let mut g_with_z = state.g.clone();
g_with_z.add_node(
format!("z_{}", state.t),
state.z.clone(),
Some("latent".into()),
);
if g_with_z.size() > self.config.max_graph_size {
return Err(AriaError::Schedule(format!(
"graph size {} exceeds max {}",
g_with_z.size(),
self.config.max_graph_size
)));
}
state.g = self.graph_backend.edit(
&g_with_z,
&state.z,
self.config.match_policy,
None,
);
state.prev_res = pre_residual;
let post_residual = self.compute_residual(&state, a);
let report = invariants::check_all(
&state,
post_residual,
self.config.eps,
self.config.n_modes,
self.config.latent_dim,
);
if self.config.strict && !report.all_ok() {
if let Some(v) = invariants::violation_from_report(
&report, action, state.energy(), state.energy_0,
post_residual, state.prev_res, self.config.eps,
) {
state.g = prev_g;
state.prev_res = prev_prev_res;
return Err(AriaError::InvariantViolation(v));
}
}
}
Action::Diffuse => {
let prev_z = state.z.clone();
let prev_t = state.t;
let prev_prev_res = state.prev_res;
let pre_residual = self.compute_residual(&state, a);
state.z = self.diffuser.diffuse(&state.g, &state.z, self.config.diff_policy);
state.t = state
.t
.checked_add(1)
.ok_or_else(|| AriaError::Backend("t overflowed u64".into()))?;
state.prev_res = pre_residual;
let post_residual = self.compute_residual(&state, a);
let report = invariants::check_all(
&state,
post_residual,
self.config.eps,
self.config.n_modes,
self.config.latent_dim,
);
if self.config.strict && !report.all_ok() {
if let Some(v) = invariants::violation_from_report(
&report, action, state.energy(), state.energy_0,
post_residual, state.prev_res, self.config.eps,
) {
state.z = prev_z;
state.t = prev_t;
state.prev_res = prev_prev_res;
return Err(AriaError::InvariantViolation(v));
}
}
}
Action::Stutter => {
let residual = self.compute_residual(&state, a);
let report = invariants::check_all(
&state,
residual,
self.config.eps,
self.config.n_modes,
self.config.latent_dim,
);
if self.config.strict && !report.all_ok() {
if let Some(v) = invariants::violation_from_report(
&report, action, state.energy(), state.energy_0,
residual, state.prev_res, self.config.eps,
) {
return Err(AriaError::InvariantViolation(v));
}
}
}
}
Ok(state)
}
pub fn step_phi(&self, state: State, a: Condition) -> Result<State, AriaError> {
let s = state;
let s = self.apply(s, Action::OpticalStep, a)?;
let s = self.apply(s, Action::Predict, a)?;
let s = self.apply(s, Action::Match, a)?;
let s = self.apply(s, Action::Diffuse, a)?;
Ok(s)
}
pub fn run(
&self,
state: State,
scheduler: &mut Scheduler,
steps: u64,
a: Condition,
) -> Result<(State, Trace), AriaError> {
let (state, trace, _) = self.run_monitored(state, scheduler, steps, a)?;
Ok((state, trace))
}
pub fn run_monitored(
&self,
mut state: State,
scheduler: &mut Scheduler,
steps: u64,
a: Condition,
) -> Result<(State, Trace, GateReport), AriaError> {
let mut trace = Trace::new(self.config.n_modes, self.config.latent_dim, self.config.eps);
let mut monitor = GateMonitor::new(self.config.gates.clone());
for _ in 0..steps {
let action = scheduler.next_action_budgeted();
let t_before = state.t;
state = self.apply(state, action, a)?;
let residual = self.compute_residual(&state, a);
let energy = state.energy();
trace.push(
t_before,
action,
residual,
energy,
state.g.size(),
&format!("{:?}", a).to_lowercase(),
);
monitor.observe(action, &state, residual, self.config.eps);
}
Ok((state, trace, monitor.finish()))
}
pub fn check(&self, state: &State, a: Condition) -> InvariantReport {
let residual = self.compute_residual(state, a);
invariants::check_all(state, residual, self.config.eps, self.config.n_modes, self.config.latent_dim)
}
fn compute_residual(&self, state: &State, a: Condition) -> f64 {
let embedded = self.predictor.embed(&state.psi);
let predicted = self.predictor.predict(&embedded, a);
self.predictor.dist(&state.z, &predicted)
}
pub fn config(&self) -> &AriaConfig {
&self.config
}
}