Skip to main content

qualia_core_db/solvers/
mod.rs

1//! Zero-Allocation Solver Library
2//!
3//! This module provides mathematical solvers designed for the #![no_std]
4//! zero-allocation environment of Qualia-DB. All solvers operate on
5//! fixed-size stack arrays and maintain strict memory constraints.
6//!
7//! Enabled:
8//! - All native and WASM solver kernels
9//! - `qpu` — QPU problem formulation + in-process job queue (non-WASM only)
10//!
11//! (Note: this module is **not** actually `#![no_std]` — the `qpu` submodule pulls in
12//! std + tokio. The individual solver kernels are written to be no-std-compatible, but the
13//! attribute only has effect at the crate root, so it is not applied here.)
14
15// QPU integration — uses std + tokio; gated to non-WASM targets.
16#[cfg(not(target_arch = "wasm32"))]
17pub mod qpu;
18
19pub mod activation;
20pub mod attention;
21pub mod calculus;
22pub mod exact;
23pub mod feed_forward;
24pub mod fuzzy_query;
25pub mod geometric_algebra;
26pub mod graph_match;
27pub mod graph_opt;
28pub mod grounding;
29pub mod interpolation;
30pub mod learning;
31pub mod linear_algebra;
32pub mod number_theory;
33pub mod ontology_align;
34pub mod optimization;
35pub mod polynomial;
36pub mod quantum_optimizers;
37pub mod rope;
38pub mod special_functions;
39pub mod statistics;
40pub mod symbolic_logic;
41pub mod transforms;
42pub mod units;
43pub mod vector_calculus;
44
45pub use calculus::{
46    BVPState, IntegralChunk, ODEState, RungeKutta4Static, ShootingMethodBVP,
47    SimpsonsIntegratorChunked,
48};
49pub use linear_algebra::{
50    ConstTensorContractor, FixedLanczosEigensolver, Matrix4x4, StaticLuDecomposition, Tensor3x3x3,
51    Vector4,
52};
53pub use optimization::{
54    BoundedNewtonRaphson, CurveFitState, LevenbergMarquardtStack, NelderMeadSimplex,
55    OptimizationState, RootFindingState,
56};
57pub use quantum_optimizers::{
58    QAOAAngleOptimizer, QAOAAngles, QuantumOptimizerState, SpsaGradient, SpsaOptimizer,
59};
60pub use symbolic_logic::{BoundedSatSolver, DefeasibleState, ForwardChainingDefeasible, SatState};
61
62/// Unified error type for solver operations.
63#[derive(Debug, Clone, PartialEq)]
64pub enum SolversError {
65    CapacityExceeded,
66    SingularMatrix,
67    InvalidParameters,
68    ConvergenceFailed,
69    InvalidDimension,
70    ComputationError,
71    QuantumError(u32),
72    OutOfMemory,
73    Unsatisfiable,
74    BacktrackFailed,
75}
76
77/// Result type for solver operations
78pub type SolverResult<T> = Result<T, SolversError>;
79
80/// Common solver configuration
81#[repr(C)]
82#[derive(Clone, Copy)]
83pub struct SolverConfig {
84    pub max_iterations: u32,
85    pub tolerance: f64,
86    pub step_size: f64,
87    pub verbose: bool,
88}
89
90impl Default for SolverConfig {
91    fn default() -> Self {
92        Self {
93            max_iterations: 1000,
94            tolerance: 1e-6,
95            step_size: 0.01,
96            verbose: false,
97        }
98    }
99}
100
101/// Common solver state — includes all fields referenced by disabled sub-modules
102/// so they can be re-enabled without structural changes.
103#[repr(C)]
104#[derive(Clone, Copy)]
105pub struct SolverState {
106    pub iteration: u32,
107    pub error: f64,
108    pub converged: bool,
109    /// Solver-specific packed data:
110    /// - solver_data[0]: cost_value (f64 bits)
111    /// - solver_data[1]: satisfiable (u64 boolean)
112    /// - solver_data[2]: quantum_calls (u32 cast to u64)
113    pub solver_data: [u64; 4],
114}
115
116impl SolverState {
117    pub fn cost_value(&self) -> f64 {
118        f64::from_bits(self.solver_data[0])
119    }
120    pub fn set_cost_value(&mut self, val: f64) {
121        self.solver_data[0] = val.to_bits();
122    }
123    pub fn satisfiable(&self) -> Option<bool> {
124        match self.solver_data[1] {
125            0 => None,
126            1 => Some(false),
127            _ => Some(true),
128        }
129    }
130    pub fn set_satisfiable(&mut self, val: Option<bool>) {
131        self.solver_data[1] = match val {
132            None => 0,
133            Some(false) => 1,
134            Some(true) => 2,
135        };
136    }
137    pub fn quantum_calls(&self) -> u32 {
138        self.solver_data[2] as u32
139    }
140    pub fn set_quantum_calls(&mut self, val: u32) {
141        self.solver_data[2] = val as u64;
142    }
143    pub fn add_quantum_calls(&mut self, val: u32) {
144        self.solver_data[2] += val as u64;
145    }
146}
147
148impl Default for SolverState {
149    fn default() -> Self {
150        Self {
151            iteration: 0,
152            error: f64::MAX,
153            converged: false,
154            solver_data: [f64::MAX.to_bits(), 0, 0, 0],
155        }
156    }
157}