dreamwell-intelligence 1.0.0

QuantumGPT (The Loom) — Quantum Information Pretrained Transformer. Density matrix attention with intrinsic thermodynamic loss, φ-scaled causal dephasing, and parameter shift gradient.
Documentation
// Quantum Token Embedding — maps tokens to density matrix states.
//
// Each token is embedded as a parameterized pure state on the Bloch hypersphere.
// Similar tokens → nearby quantum states (high fidelity).
// Dissimilar tokens → orthogonal states (zero fidelity).

use crate::complex::Complex;
use crate::density_matrix::DensityMatrixN;

/// Quantum embedding: maps vocab indices to density matrices.
#[derive(Clone)]
pub struct QuantumEmbedding {
    pub vocab_size: usize,
    pub dim: usize,
    /// Per-token rotation angles [vocab_size × dim]. Learnable.
    /// Each token gets `dim` angles that parameterize a unitary rotation.
    pub angles: Vec<f32>,
}

impl QuantumEmbedding {
    pub fn new(vocab_size: usize, dim: usize, seed: u64) -> Self {
        let mut angles = Vec::with_capacity(vocab_size * dim);
        for i in 0..(vocab_size * dim) {
            let s = seed.wrapping_add(i as u64).wrapping_mul(0x517cc1b727220a95);
            angles.push(std::f32::consts::PI * ((s % 2000) as f32 / 1000.0 - 1.0));
        }
        Self {
            vocab_size,
            dim,
            angles,
        }
    }

    /// Embed a single token as a density matrix.
    /// Start with |k mod dim⟩, then apply parameterized rotations to spread
    /// the state across modes. The rotations are the learnable embedding.
    pub fn embed(&self, token: usize) -> DensityMatrixN {
        let d = self.dim;
        let base = token % d;

        // Build amplitudes from angles via parameterized rotation
        let offset = token * d;
        let mut amplitudes = vec![Complex::ZERO; d];

        // Start with base state
        amplitudes[base] = Complex::ONE;

        // Apply rotation: each angle mixes the base with another mode
        for k in 0..d {
            if k == base {
                continue;
            }
            let angle = self.angles.get(offset + k).copied().unwrap_or(0.0);
            let c = angle.cos();
            let s = angle.sin();
            let old_base = amplitudes[base];
            let old_k = amplitudes[k];
            amplitudes[base] = Complex::new(c * old_base.re - s * old_k.re, c * old_base.im - s * old_k.im);
            amplitudes[k] = Complex::new(s * old_base.re + c * old_k.re, s * old_base.im + c * old_k.im);
        }

        // Build density matrix ρ = |ψ⟩⟨ψ|
        let mut entries = vec![Complex::ZERO; d * d];
        for i in 0..d {
            for j in 0..d {
                entries[i * d + j] = amplitudes[i].mul(amplitudes[j].conj());
            }
        }

        let n2 = d * d;
        DensityMatrixN {
            dim: d,
            entries,
            scratch_a: vec![Complex::ZERO; n2],
            scratch_b: vec![Complex::ZERO; n2],
        }
    }

    /// Embed a single token as an amplitude vector (Fock space representation).
    ///
    /// Returns the dim-dimensional complex state vector |ψ⟩ WITHOUT forming
    /// the density matrix ρ = |ψ⟩⟨ψ|. This is the Fock space representation:
    /// dim amplitudes instead of dim² matrix entries.
    ///
    /// BA-62: At dim=86, this returns 688 bytes instead of 59,168 bytes (86× smaller).
    /// The populations |ψ_k|² are identical to the diagonal of ρ = |ψ⟩⟨ψ|.
    pub fn embed_amplitude(&self, token: usize) -> Vec<Complex> {
        let d = self.dim;
        let base = token % d;
        let offset = token * d;
        let mut amplitudes = vec![Complex::ZERO; d];

        amplitudes[base] = Complex::ONE;

        for k in 0..d {
            if k == base {
                continue;
            }
            let angle = self.angles.get(offset + k).copied().unwrap_or(0.0);
            let c = angle.cos();
            let s = angle.sin();
            let old_base = amplitudes[base];
            let old_k = amplitudes[k];
            amplitudes[base] = Complex::new(c * old_base.re - s * old_k.re, c * old_base.im - s * old_k.im);
            amplitudes[k] = Complex::new(s * old_base.re + c * old_k.re, s * old_base.im + c * old_k.im);
        }

        amplitudes
    }

    /// Number of learnable parameters.
    pub fn num_params(&self) -> usize {
        self.angles.len()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn embedding_produces_valid_state() {
        let emb = QuantumEmbedding::new(65, 5, 42);
        for token in 0..65 {
            let rho = emb.embed(token);
            assert!(
                (rho.trace() - 1.0).abs() < 1e-4,
                "token {token}: trace = {}",
                rho.trace()
            );
            assert!(
                rho.purity() > 0.9,
                "token {token}: embedded state should be nearly pure, got {}",
                rho.purity()
            );
        }
    }

    #[test]
    fn different_tokens_different_states() {
        let emb = QuantumEmbedding::new(65, 5, 42);
        let rho_a = emb.embed(0);
        let rho_b = emb.embed(1);
        // Check that populations differ
        let pops_a = rho_a.populations();
        let pops_b = rho_b.populations();
        let diff: f32 = pops_a.iter().zip(pops_b.iter()).map(|(a, b)| (a - b).abs()).sum();
        assert!(diff > 0.01, "Different tokens should produce different states");
    }
}