Research-Stack/0-Core-Formalism/lean/external/OTOM/RotationQUBO.lean
allaun 00e9eed399 fix(lean): complete projectionOrdering proof in GeometricCompressionWorkspace
Replace the TODO(lean-port) sorry with a complete proof of the
projectionOrdering theorem: for positive SourceValue pairs s1 < s2
with s2 ≤ maxExpected, projectToCoding preserves strict ordering
of the Q0_64 values.

The proof uses Nat-only arithmetic (no Float) and handles two cases:
  - a2 < d: both values fit in Q0_64 range, ordering follows from
    monotonicity of integer division
  - a2 = d: a2*s/d = s clamped to q0_64MaxRaw; a1*s/d < q0_64MaxRaw
    via the key inequality (d-1)*s < (s-1)*d

Build: 8598 jobs, 0 errors (lake build)
2026-06-18 15:06:50 -05:00

311 lines
15 KiB
Text

/- Copyright (c) 2026 Sovereign Research Stack. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Research Stack Team
RotationQUBO.lean — Rotation Matrices as Literal Rotation Notation in Frustrated QUBO Fields
This module formalizes a 1D scalar triangle navigating a frustrated QUBO field,
spawning friends to rotate in superposition. Each bracket represents a possibility space,
borrowing the PIST framework for shell geometry.
Key insight:
- Rotation matrices as literal rotation notation (not just linear algebra)
- 1D scalar triangle = (a, b, c) with a+b+c = 0 (triangle closure)
- Frustrated QUBO field = energy landscape with competing minima
- Spawning friends = agent generation in superposition
- Brackets = possibility spaces [lower, upper] from PIST shell geometry
- PIST mass = a*b (hyperbola index) as rotation weight
The rotation field:
Φ_rot(x, θ) = Σᵢ R(θᵢ) · xᵢ / (1 + frustration²)
Where:
- R(θ): rotation matrix at angle θ
- xᵢ: scalar triangle vertex
- frustration: QUBO field frustration parameter
Per AGENTS.md §0: Lean is the source of truth.
Per AGENTS.md §1.4: Q16_16 fixed-point for hardware extraction.
Per AGENTS.md §2: PascalCase types, camelCase functions.
Per AGENTS.md §4: Every def has eval witness or theorem.
-/
import Mathlib.Data.Nat.Basic
import Mathlib.Data.Real.Basic
import Mathlib.Data.Matrix.Basic
import Mathlib.Tactic
import Semantics.PIST
import Semantics.DynamicCanal
namespace Semantics.RotationQUBO
open PIST DynamicCanal
-- ═══════════════════════════════════════════════════════════════════════════
-- §0 Scalar Triangle Types
-- ═══════════════════════════════════════════════════════════════════════════
/-- A 1D scalar triangle (a, b, c) with closure condition a + b + c = 0.
Represents a balanced configuration that can navigate QUBO fields. -/
structure ScalarTriangle where
a : Fix16 -- First vertex
b : Fix16 -- Second vertex
c : Fix16 -- Third vertex
closure : Fix16 -- Closure residual (should be 0 for balanced triangle)
deriving Repr, DecidableEq, BEq
namespace ScalarTriangle
/-- Create a balanced scalar triangle from two vertices (c = -(a + b)). -/
def balanced (a b : Fix16) : ScalarTriangle :=
let c := Fix16.sub (Fix16.sub Fix16.zero a) b -- c = -(a + b)
let closure := Fix16.add (Fix16.add a b) c -- should be 0
{ a, b, c, closure }
/-- Create a scalar triangle from PIST coordinate (a = t, b = 2k+1-t). -/
def fromPISTCoord (coord : PIST.Coord) : ScalarTriangle :=
let a := fix16FromNat coord.t
let b := fix16FromNat coord.b
let c := Fix16.sub (Fix16.sub Fix16.zero a) b
let closure := Fix16.add (Fix16.add a b) c
{ a, b, c, closure }
/-- The PIST mass of the scalar triangle (a * b). -/
def pistMass (st : ScalarTriangle) : Fix16 :=
Fix16.mul st.a st.b
end ScalarTriangle
-- ═══════════════════════════════════════════════════════════════════════════
-- §1 Rotation Matrix as Literal Rotation Notation
-- ═══════════════════════════════════════════════════════════════════════════
/-- Rotation matrix at angle θ (2D rotation).
Treated as literal rotation notation, not just linear algebra. -/
structure RotationMatrix where
theta : Fix16 -- Rotation angle in radians (Q16.16)
cosθ : Fix16 -- cos(θ) in Q16.16
sinθ : Fix16 -- sin(θ) in Q16.16
deriving Repr, DecidableEq, BEq
namespace RotationMatrix
/-- Create rotation matrix from angle θ.
Uses Q16.16 approximation for cos and sin. -/
def fromAngle (theta : Fix16) : RotationMatrix :=
-- Placeholder: use Taylor series or lookup table for cos/sin
-- For now, use simple approximation
let cosθ := Fix16.ofNat 1 -- cos(0) = 1
let sinθ := theta -- sin(θ) ≈ θ for small θ
{ theta, cosθ, sinθ }
/-- Apply rotation matrix to scalar triangle vertex. -/
def rotateVertex (rm : RotationMatrix) (v : Fix16) : Fix16 :=
-- 2D rotation: x' = x·cosθ - y·sinθ
-- For 1D scalar, this is simplified
Fix16.mul v rm.cosθ
/-- Apply rotation matrix to entire scalar triangle. -/
def rotateTriangle (rm : RotationMatrix) (st : ScalarTriangle) : ScalarTriangle :=
let a' := rm.rotateVertex st.a
let b' := rm.rotateVertex st.b
let c' := rm.rotateVertex st.c
let closure' := Fix16.add (Fix16.add a' b') c'
{ a := a', b := b', c := c', closure := closure' }
end RotationMatrix
-- ═══════════════════════════════════════════════════════════════════════════
-- §2 Frustrated QUBO Field
-- ═══════════════════════════════════════════════════════════════════════════
/-- Frustrated QUBO field parameters.
Frustration parameter δ controls competing energy minima. -/
structure QUBOField where
frustration : Fix16 -- Frustration parameter δ (0 ≤ δ ≤ 1)
energyScale : Fix16 -- Energy scale factor
deriving Repr, DecidableEq, BEq
namespace QUBOField
/-- Compute field energy at position x.
E(x) = x² / (1 + δ²) - frustration penalty. -/
def fieldEnergy (qf : QUBOField) (x : Fix16) : Fix16 :=
let xSq := Fix16.mul x x
let denom := Fix16.add Fix16.one (Fix16.mul qf.frustration qf.frustration)
let energy := Fix16.div xSq denom
Fix16.sub energy qf.energyScale
/-- Check if field is frustrated at position x. -/
def isFrustrated (qf : QUBOField) (x : Fix16) : Bool :=
-- Field is frustrated if energy > 0
let energy := qf.fieldEnergy x
energy.val > 0
end QUBOField
-- ═══════════════════════════════════════════════════════════════════════════
-- §3 Bracket Possibility Spaces
-- ═══════════════════════════════════════════════════════════════════════════
/-- Bracket possibility space from PIST shell geometry.
[lower, upper] = [a, b] where a + b = 2k+1 and mass = a*b. -/
structure BracketSpace where
lower : Fix16 -- Lower bound (a)
upper : Fix16 -- Upper bound (b)
mass : Fix16 -- PIST mass (a * b)
gap : Fix16 -- Upper - lower
admissible : Bool -- Whether space is admissible
deriving Repr, DecidableEq, BEq
namespace BracketSpace
/-- Create bracket space from PIST coordinate. -/
def fromPISTCoord (coord : PIST.Coord) : BracketSpace :=
let lower := fix16FromNat coord.a
let upper := fix16FromNat coord.b
let mass := fix16FromNat coord.mass
let gap := Fix16.sub upper lower
let admissible := coord.mass > 0 -- Positive mass = admissible
{ lower, upper, mass, gap, admissible }
/-- Check if a value is within the bracket space. -/
def contains (bs : BracketSpace) (x : Fix16) : Bool :=
let xNat := x.val.toNat
let lowerNat := bs.lower.val.toNat
let upperNat := bs.upper.val.toNat
lowerNat ≤ xNat ∧ xNat ≤ upperNat
end BracketSpace
-- ═══════════════════════════════════════════════════════════════════════════
-- §4 Friend Spawning in Superposition
-- ═══════════════════════════════════════════════════════════════════════════
/-- A friend agent spawned in superposition.
Each friend has a rotation angle and weight. -/
structure FriendAgent where
rotation : RotationMatrix -- Rotation matrix
weight : Fix16 -- Superposition weight (0 ≤ weight ≤ 1)
bracket : BracketSpace -- Assigned bracket space
deriving Repr, DecidableEq, BEq
namespace FriendAgent
/-- Spawn a friend agent with random rotation. -/
def spawn (theta : Fix16) (bracket : BracketSpace) : FriendAgent :=
let rm := RotationMatrix.fromAngle theta
let weight := Fix16.ofNat 1 -- Default weight = 1.0
{ rotation := rm, weight, bracket }
/-- Spawn multiple friends in superposition. -/
def spawnSuperposition (thetas : List Fix16) (bracket : BracketSpace) : List FriendAgent :=
thetas.map (fun θ => spawn θ bracket)
end FriendAgent
-- ═══════════════════════════════════════════════════════════════════════════
-- §5 Rotation Field Computation
-- ═══════════════════════════════════════════════════════════════════════════
/-- Compute rotation field for scalar triangle in QUBO field with friends.
Φ_rot(x, θ) = Σᵢ R(θᵢ) · xᵢ / (1 + frustration²) -/
def rotationField (st : ScalarTriangle) (friends : List FriendAgent)
(qf : QUBOField) : Fix16 :=
let denom := Fix16.add Fix16.one (Fix16.mul qf.frustration qf.frustration)
-- Sum over friends: Σᵢ weightᵢ * rotationᵢ(triangle)
let sumRotations := friends.foldl (fun acc friend =>
let rotated := friend.rotation.rotateTriangle st
let weightedMass := Fix16.mul (ScalarTriangle.pistMass rotated) friend.weight
Fix16.add acc weightedMass
) Fix16.zero
-- Divide by frustration denominator
Fix16.div sumRotations denom
-- ═══════════════════════════════════════════════════════════════════════════
-- §6 Theorems: Rotation and Bracket Properties
-- ═══════════════════════════════════════════════════════════════════════════
/-- Theorem: Balanced scalar triangle has zero closure.
ANALYTIC_OPEN: With saturating Fix16 arithmetic, a + b + (-(a+b)) is NOT
necessarily zero. Counter-example: if a = b = Fix16.maxVal (0x7FFFFFFF),
then add a b saturates to maxVal, and c = -(a+b) also saturates, so
add maxVal minVal = add 0x7FFFFFFF 0x80000001 ≠ 0 in saturating arithmetic.
The theorem holds only for wrapping (modular 2³²) arithmetic or when
|a.raw| + |b.raw| ≤ 0x7FFFFFFF (no saturation occurs).
Correct statement requires a non-overflow side condition:
a.val.toNat + b.val.toNat ≤ 0x7FFFFFFF → closure = zero.
Marking ANALYTIC_OPEN pending a wrapping-arithmetic variant. -/
theorem balancedClosureZero (a b : Fix16) :
(ScalarTriangle.balanced a b).closure = Fix16.zero := by
-- ANALYTIC_OPEN: false under saturating arithmetic without overflow bounds.
sorry
/-- Theorem: PIST mass from coordinate equals a * b.
TACTIC_GAP: fix16FromNat t * fix16FromNat b = fix16FromNat (t * b) requires
(t * 65536) * (b * 65536) >> 16 = t * b * 65536 in UInt32 arithmetic.
This holds exactly when t * b * 65536 < 2^32 (no overflow), i.e. t*b < 65536.
For arbitrary Coord, t can be up to 2k+1 (unbounded), so overflow is possible.
A correct statement needs t * b < 65536 as a side condition or uses
unbounded-integer semantics.
Marking TACTIC_GAP pending a bounded-range variant. -/
theorem pistMassFromCoord (coord : PIST.Coord) :
(ScalarTriangle.fromPISTCoord coord).pistMass = fix16FromNat coord.mass := by
-- TACTIC_GAP: see theorem docstring — requires overflow guard.
sorry
/-- Theorem: Bracket space contains its own bounds when lower ≤ upper.
The original statement is FALSE for arbitrary BracketSpace because no
invariant guarantees lower ≤ upper. The corrected statement adds the
required hypothesis, expressed in terms of the UInt32 raw field (for
the OTOM UInt32 Fix16) or the Int val field (for the Semantics Q16_16 Fix16).
TACTIC_GAP: the exact proof depends on which Fix16 definition is in scope
(UInt32 struct vs Int subtype) and whether ∧ in `contains` is Bool.and or
Prop.And. The mathematical content is trivial (reflexivity + hypothesis),
but the elaboration path is import-context-dependent. -/
theorem bracketContainsBounds (bs : BracketSpace)
(h : bs.lower.val.toNat ≤ bs.upper.val.toNat) :
bs.contains bs.lower ∧ bs.contains bs.upper := by
simp only [BracketSpace.contains]
-- After unfolding: goal is (lower ≤ lower ∧ lower ≤ upper) ∧ (lower ≤ upper ∧ upper ≤ upper)
-- The two reflexivity parts follow from le_refl; the cross parts follow from h.
-- TACTIC_GAP: decide / simp path depends on Fix16 elaboration context.
constructor <;> simp only [decide_eq_true_eq] <;> omega
/-- Theorem: Rotation field is bounded by bracket mass.
ANALYTIC_OPEN: No structural relationship exists between the rotation field
(arbitrary sum of rotated triangle masses) and a bracket's mass (a * b from
a PIST coordinate). The theorem as stated is false in general — e.g., with
large triangle vertices and unit weights, rotationField can exceed any fixed
bracket mass.
A meaningful bound would require:
(1) bounded input constraints on triangle vertices and weights, and
(2) a specific bracket constructed from the same coordinate as the triangle.
Marking ANALYTIC_OPEN as the theorem lacks a mathematical foundation. -/
theorem rotationFieldBounded (st : ScalarTriangle) (friends : List FriendAgent)
(qf : QUBOField) (bs : BracketSpace) :
let field := rotationField st friends qf
field.val ≤ bs.mass.val := by
-- ANALYTIC_OPEN: see theorem docstring.
sorry
-- ═══════════════════════════════════════════════════════════════════════════
-- §7 Verification Examples
-- ═══════════════════════════════════════════════════════════════════════════
#eval let st := ScalarTriangle.balanced (Fix16.ofNat 3) (Fix16.ofNat 4)
st.pistMass -- Expected: 3 * 4 = 12
#eval let coord := { k := 2, t := 3, ht := by simp }
let bs := BracketSpace.fromPISTCoord coord
bs.admissible -- Expected: true (mass = 3 * (5-3) = 6 > 0)
#eval let qf := { frustration := Fix16.ofNat 1, energyScale := Fix16.ofNat 10 }
let x := Fix16.ofNat 5
qf.isFrustrated x -- Expected: true
-- TODO(lean-port): Add friend spawning and rotation field examples
end Semantics.RotationQUBO