pub mod sealed_identity_shard; pub mod sealed_identity_catalog; pub mod bipartite_identity_lift; pub mod train_source_revision; pub mod row_padding_masks; pub mod packed_row_decode; pub mod random_stream; pub mod native_compiler; pub mod poisson_stream; pub mod guarded_exact; pub mod saturated_frame; pub mod pair_exact; pub mod bipartite_exact; pub mod zero_excess; pub mod word_bracket; pub mod exact_records; pub mod records; pub mod cuda_gaussian; pub mod holdout_views; pub mod compact_bank; pub mod training_records; pub mod native_store; pub mod windows_path_policy; pub mod cuda_bank; pub mod exact_batch_reuse; pub mod native_epoch_engine; pub mod native_epoch_owner; pub const SIZES:[usize;14]=[8,10,13,25,26,34,45,50,58,72,90,108,122,144]; pub const EPOCHS:u32=1000; pub const ROOTS_PER_EPOCH:u32=10000; #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum Split {Train,Dev,Test} #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum Family {WalkMixture,GuardedExact,BasisShear,Composition} #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum WalkKind {Ordinary,Poisson} #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct RootPlan {pub split:Split,pub epoch:u32,pub position:u32,pub attempt:u32,pub seed:u64,pub n:usize,pub family:Family,pub walk:WalkKind} impl RootPlan { pub fn new(split:Split,epoch:u32,position:u32,attempt:u32,seed_base:u64)->Result { if epoch>=EPOCHS||position>=ROOTS_PER_EPOCH||attempt>=100{return Err("invalid root identity");} let offset=match split {Split::Train=>1_000_000_000,Split::Dev=>2_000_000_000,Split::Test=>3_000_000_000}; let seed=seed_base.checked_add(offset).and_then(|s|s.checked_add(epoch as u64*1_000_000+position as u64*100+attempt as u64)).ok_or("seed overflow")?; let family=match position/14%4 {0=>Family::WalkMixture,1=>Family::GuardedExact,2=>Family::BasisShear,_=>Family::Composition}; let walk=if (position/56+epoch)%2==0 {WalkKind::Ordinary} else {WalkKind::Poisson}; Ok(Self{split,epoch,position,attempt,seed,n:SIZES[position as usize%14],family,walk}) } } #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Gate {pub control:usize,pub target:usize} #[derive(Clone,Debug,PartialEq,Eq,Hash)] pub struct Matrix {pub n:usize,pub rows:Vec<[u64;3]>} impl Matrix { pub fn bit(&self,row:usize,col:usize)->bool {self.rows[row][col/64]&(1u64<<(col%64))!=0} pub fn inverse(&self)->Result{ let mut left=self.clone();let mut right=Self::identity(self.n)?; for col in 0..self.n { let pivot=(col..self.n).find(|&i|left.bit(i,col)).ok_or("singular matrix")?; left.rows.swap(col,pivot);right.rows.swap(col,pivot); for row in 0..self.n {if row!=col&&left.bit(row,col){for k in 0..3 {left.rows[row][k]^=left.rows[col][k];right.rows[row][k]^=right.rows[col][k];}}} }Ok(right) } pub fn identity(n:usize)->Result { if !(1..=144).contains(&n){return Err("unsupported matrix width");} let mut rows=vec![[0;3];n];for i in 0..n {rows[i][i/64]=1u64<<(i%64);} Ok(Self{n,rows}) } pub fn apply(&mut self,gate:Gate)->Result<(),&'static str> { if gate.control>=self.n||gate.target>=self.n||gate.control==gate.target{return Err("invalid CNOT");} for limb in 0..3 {self.rows[gate.target][limb]^=self.rows[gate.control][limb];}Ok(()) } } pub fn forest_encoder(matrix:&Matrix)->Result>,&'static str>{ let inverse=matrix.inverse()?;let mut parents=vec![None;matrix.n]; for (row,parent) in parents.iter_mut().enumerate(){ if !inverse.bit(row,row){return Ok(None);} for col in 0..matrix.n {if col!=row&&inverse.bit(row,col){if parent.is_some(){return Ok(None);}*parent=Some(col);}} } fn visit(v:usize,parents:&[Option],state:&mut [u8],gates:&mut Vec)->Result<(),()>{ if state[v]==1{return Err(());}if state[v]==2{return Ok(());}state[v]=1; if let Some(p)=parents[v]{visit(p,parents,state,gates)?;gates.push(Gate{control:p,target:v});}state[v]=2;Ok(()) } let mut state=vec![0;matrix.n];let mut gates=Vec::new(); for v in 0..matrix.n {if visit(v,&parents,&mut state,&mut gates).is_err(){return Ok(None);}} let mut replay=Matrix::identity(matrix.n)?;for &g in &gates {replay.apply(g)?;} if replay!=*matrix{return Ok(None);}Ok(Some(gates)) } pub struct WalkRoot {pub matrix:Matrix,pub inverse_word:Vec} pub fn ordinary_walk(plan:RootPlan)->Result { if plan.family!=Family::WalkMixture||plan.walk!=WalkKind::Ordinary{return Err("ordinary walk schedule required; no alternate-family fallback");} let length=plan.n*(1+(plan.seed%3) as usize); walk_from_seed(plan.seed,plan.n,length) } pub fn poisson_walk(plan:RootPlan)->Result { if plan.family!=Family::WalkMixture||plan.walk!=WalkKind::Poisson{return Err("Poisson walk schedule required");} let mean=plan.n*(1+(plan.seed%3) as usize); let length=poisson_stream::PoissonStream::new(plan.seed).poisson(mean as f64)?; walk_from_seed(plan.seed,plan.n,length) } pub fn walk_from_seed(seed:u64,n:usize,length:usize)->Result { let mut rng=random_stream::RandomStream::new(seed); let mut matrix=Matrix::identity(n)?;let mut word=Vec::with_capacity(length); for _ in 0..length { let index=rng.randbelow((n*(n-1)) as u64) as usize; let control=index/(n-1);let t=index%(n-1); let gate=Gate{control,target:if t>=control {t+1} else {t}}; matrix.apply(gate)?;word.push(gate); } word.reverse();Ok(WalkRoot{matrix,inverse_word:word}) } pub fn basis_shear(plan:RootPlan)->Result { if plan.family!=Family::BasisShear||plan.n<4{return Err("basis shear schedule required");} Ok(basis_parts(plan.seed,plan.n)?.root) } pub struct BasisParts {pub root:WalkRoot,pub block:Vec,pub sources:Vec,pub targets:Vec} pub fn basis_parts(seed:u64,n:usize)->Result { if !(4..=144).contains(&n){return Err("basis width unsupported");} let mut rng=random_stream::RandomStream::new(seed); let wires=rng.sample_range(n,n);let width=n/2; let sources=&wires[..width];let targets=&wires[width..]; let mut word=Vec::new();let mut block=Vec::with_capacity(sources.len()*targets.len()); for &target in targets {for &control in sources {let bit=(rng.random()<0.35) as u8;block.push(bit);if bit!=0 {word.push(Gate{control,target});}}} if word.is_empty(){block[0]=1;word.push(Gate{control:sources[0],target:targets[0]});} let mut matrix=Matrix::identity(n)?;for &gate in &word {matrix.apply(gate)?;} word.reverse();Ok(BasisParts{root:WalkRoot{matrix,inverse_word:word},block,sources:sources.iter().map(|&i|i as u16).collect(),targets:targets.iter().map(|&i|i as u16).collect()}) } /// The source retains the last occurrence of a state, then sorts those offsets. pub fn solution_offsets(matrix:&Matrix,word:&[Gate])->Result,&'static str> { let mut state=matrix.clone();let mut seen=std::collections::HashMap::new(); for (offset,&gate) in word.iter().enumerate(){seen.insert(state.clone(),offset);state.apply(gate)?;} if state!=Matrix::identity(matrix.n)?{return Err("selected solution endpoint mismatch");} let mut offsets:Vec<_>=seen.into_values().collect();offsets.sort_unstable();Ok(offsets) } #[cfg(test)] mod tests { use super::*; #[test] fn normative_seed_boundaries_and_schedule(){ let first=RootPlan::new(Split::Train,0,0,0,3700).unwrap();assert_eq!(first.seed,1_000_003_700); assert_eq!(RootPlan::new(Split::Train,999,9999,99,3700).unwrap().seed,2_000_003_699); assert_eq!(RootPlan::new(Split::Dev,0,0,0,3700).unwrap().seed,2_000_003_700); assert_eq!(RootPlan::new(Split::Test,0,0,0,3700).unwrap().seed,3_000_003_700); assert_eq!(RootPlan::new(Split::Train,0,14,0,3700).unwrap().family,Family::GuardedExact); assert_eq!(RootPlan::new(Split::Train,0,28,0,3700).unwrap().family,Family::BasisShear); assert_eq!(RootPlan::new(Split::Train,0,42,0,3700).unwrap().family,Family::Composition); assert_eq!(RootPlan::new(Split::Train,0,56,0,3700).unwrap().walk,WalkKind::Poisson); assert_eq!(RootPlan::new(Split::Train,1,0,0,3700).unwrap().walk,WalkKind::Poisson); for (e,p,a) in [(1000,0,0),(0,10000,0),(0,0,100)] {assert!(RootPlan::new(Split::Train,e,p,a,3700).is_err());} } #[test] fn ordinary_walk_all_widths_reverse_endpoint(){ for split in [Split::Train,Split::Dev,Split::Test] {for position in 0..14 {for attempt in [0,1,99] { let plan=RootPlan::new(split,0,position,attempt,3700).unwrap();let root=ordinary_walk(plan).unwrap(); assert_eq!(root.inverse_word.len(),plan.n*(1+(plan.seed%3) as usize)); let mut state=root.matrix.clone();for &gate in &root.inverse_word {state.apply(gate).unwrap();} assert_eq!(state,Matrix::identity(plan.n).unwrap());solution_offsets(&root.matrix,&root.inverse_word).unwrap(); }}} } #[test] fn suffix_last_occurrence(){ let mut state=Matrix::identity(8).unwrap();let g=Gate{control:7,target:6};state.apply(g).unwrap(); assert_eq!(solution_offsets(&state,&[g,g,g]).unwrap(),vec![1,2]); assert!(solution_offsets(&state,&[]).is_err()); } #[test] fn sample_pool_and_set_are_unique(){ for (n,k) in [(144,144),(1000,4),(30,6),(0,0)] { let result=random_stream::RandomStream::new(3700).sample_range(n,k); assert_eq!(result.len(),k);assert_eq!(result.iter().copied().collect::>().len(),k); } } } pub mod mapped_publication; pub mod packed_row_encode; pub mod owned_stream_controller; pub mod cuda_event_profile; pub mod native_stage_profile; pub mod independent_returned_batch_reference; pub mod tiered_identity_payload_reader; pub mod train_admission_policy; mod validated_trajectory; #[cfg(test)] mod baseline_trajectory_policy; pub mod correctness_epoch_owner;