Skip to main content

qualia_core_db/specialized_libs/physics_simulation/
quantum.rs

1use super::*;
2
3impl PhysicsSimulationLibrary {
4    /// QuantumMechanics — 1D time-independent Schrödinger equation
5    /// `[-ħ²/(2m)·d²/dx² + V(x)]·ψ = E·ψ` discretised by second-order finite differences
6    /// (Dirichlet walls). The resulting symmetric tridiagonal Hamiltonian is diagonalised
7    /// by the tested `symmetric_eigen`; the lowest `num_levels` energies are returned.
8    ///
9    /// **Classical simulation** — no QPU required. This solves the Schrödinger equation
10    /// numerically on classical hardware (CPU). It does not use quantum processing
11    /// hardware. QPU offload is a separate path (`qpu_bridge` / `qpu_oracle`) reserved
12    /// for NP-hard tasks that cannot be approximated classically.
13    pub fn run_quantum_stationary_states_1d(
14        &self,
15        potential: Vec<f64>,
16        dx: f64,
17        mass: f64,
18        hbar: f64,
19        num_levels: usize,
20    ) -> Result<QuantumSpectrumResult, PhysicsError> {
21        let n = potential.len();
22        if n < 2 || !(dx > 0.0 && mass > 0.0 && hbar > 0.0) {
23            return Err(PhysicsError::InvalidConfiguration(
24                "require potential length >= 2, dx > 0, mass > 0, hbar > 0".to_string(),
25            ));
26        }
27        // Kinetic coupling t = ħ²/(2m·dx²). Diagonal 2t + V_i; off-diagonal -t.
28        let t = hbar * hbar / (2.0 * mass * dx * dx);
29        let mut a = vec![0.0f64; n * n];
30        for i in 0..n {
31            a[i * n + i] = 2.0 * t + potential[i];
32            if i + 1 < n {
33                a[i * n + (i + 1)] = -t;
34                a[(i + 1) * n + i] = -t;
35            }
36        }
37        let mut eigvecs = vec![0.0f64; n * n];
38        symmetric_eigen(n, &mut a, &mut eigvecs)
39            .map_err(|e| PhysicsError::SolverError(format!("symmetric_eigen: {:?}", e)))?;
40        let mut eigenvalues: Vec<f64> = (0..n).map(|i| a[i * n + i]).collect();
41        eigenvalues.sort_by(|x, y| x.partial_cmp(y).unwrap_or(std::cmp::Ordering::Equal));
42        eigenvalues.truncate(num_levels.min(n).max(1));
43        Ok(QuantumSpectrumResult {
44            eigenvalues,
45            num_grid_points: n,
46            dx,
47        })
48    }
49}