Research-Stack/0-Core-Formalism/lean/Semantics/Semantics/WSMConcrete.lean

235 lines
5 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Mathlib.Data.Complex.Basic
import Mathlib.Data.Real.Basic
import Mathlib.Data.Matrix.Basic
import Mathlib.Data.Matrix.Diagonal
import Mathlib.Data.Fin.Basic
import Mathlib.Analysis.SpecialFunctions.Sqrt
import Mathlib.Analysis.SpecialFunctions.Trigonometric.Basic
import Mathlib.Tactic
open Complex
namespace WSMConcrete
noncomputable section
abbrev Time :=
abbrev State4 := Fin 4 →
abbrev Op4 := Matrix (Fin 4) (Fin 4)
abbrev Signal := Time →
abbrev Feature2 := Fin 2 →
abbrev Probe2 := Fin 2 →
abbrev Coarse1 := Fin 1 →
def sigAdd (s₁ s₂ : Signal) : Signal := fun t => s₁ t + s₂ t
def sigScale (a : ) (s : Signal) : Signal := fun t => a * s t
infixl:65 " ⊞ " => sigAdd
def applyOp (A : Op4) (ψ : State4) : State4 :=
A.mulVec ψ
def cInner (x y : State4) : :=
star (x 0) * y 0 + star (x 1) * y 1 + star (x 2) * y 2 + star (x 3) * y 3
def expect (ψ : State4) (A : Op4) : :=
Complex.re (cInner ψ (applyOp A ψ))
def finiteDiff (dt : ) (s : Signal) : Signal :=
fun t => (s (t + dt) - s t) / dt
/-
Concrete wavefunction ψ(t) ∈ ℂ⁴.
Only the first two amplitudes vary with time.
-/
def ψ : Time → State4 :=
fun t =>
![
(Real.cos t : ),
(Real.sin t : ),
0,
0
]
/-
Explicit Hamiltonian:
diag(1,2,3,4)
-/
def H : Op4 :=
!![
(1 : ), 0, 0, 0;
0, (2 : ), 0, 0;
0, 0, (3 : ), 0;
0, 0, 0, (4 : )
]
/-
Two observable channels:
O₀ projects onto coordinate 0
O₁ projects onto coordinate 1
-/
def O0 : Op4 :=
!![
(1 : ), 0, 0, 0;
0, 0, 0, 0;
0, 0, 0, 0;
0, 0, 0, 0
]
def O1 : Op4 :=
!![
0, 0, 0, 0;
0, (1 : ), 0, 0;
0, 0, 0, 0;
0, 0, 0, 0
]
def Obs : Fin 2 → Op4
| 0 => O0
| 1 => O1
/-
Weights for the two channels.
-/
def w : Fin 2 →
| 0 => 1.0
| 1 => 0.5
def recordingChannel (O : Op4) : Signal :=
fun t => expect (ψ t) O
def shapeWaveform : Signal :=
fun t => w 0 * recordingChannel (Obs 0) t + w 1 * recordingChannel (Obs 1) t
def energySignal : Signal :=
fun t => expect (ψ t) H
def temporalEnergyGradient (dt : ) : Signal :=
finiteDiff dt energySignal
/-
Toy spatial gradient channel.
You can replace this with anything more physical later.
-/
def gSpatial : Signal :=
fun t => |Real.sin t|
def energyGradientMagnitude (dt : ) : Signal :=
fun t =>
Real.sqrt ((temporalEnergyGradient dt t)^2 + (gSpatial t)^2)
def energyIncrease (dt : ) : Signal :=
fun t => max (temporalEnergyGradient dt t) 0
def energyDecrease (dt : ) : Signal :=
fun t => max (- temporalEnergyGradient dt t) 0
/-
Toy shape-energy coupling channel.
-/
def ΓSE : Signal :=
fun t => Real.cos t * Real.sin t
/-
Toy noise channel.
-/
def η : Signal :=
fun _ => 0.0
def totalSignal (dt : ) (lambdaE : ) (lambdaC : ) : Signal :=
shapeWaveform
⊞ sigScale lambdaE (energyGradientMagnitude dt)
⊞ sigScale lambdaC ΓSE
⊞ η
/-
Toy feature extractor:
sample the total signal at t=0 and t=1.
So F[S] = [S(0), S(1)] ∈ ℝ².
-/
def F (S : Signal) : Feature2 :=
![
S 0,
S 1
]
/-
Explicit waveprobe matrix W : ℝ² → ℝ².
-/
def W : Matrix (Fin 2) (Fin 2) :=
!![
(1 : ), 2;
(-1 : ), 1
]
/-
Explicit coarse-graining matrix C : ℝ² → ℝ¹.
-/
def C : Matrix (Fin 1) (Fin 2) :=
!![
(0.5 : ), 0.5
]
def probeState (dt : ) (lambdaE : ) (lambdaC : ) : Probe2 :=
W.mulVec (F (totalSignal dt lambdaE lambdaC))
def coarseState (dt : ) (lambdaE : ) (lambdaC : ) : Coarse1 :=
C.mulVec (probeState dt lambdaE lambdaC)
/-
A few concrete evaluation helpers.
-/
def example_dt : := 0.01
def example_lambdaE : := 0.2
def example_lambdaC : := 0.1
def exampleFeature : Feature2 :=
F (totalSignal example_dt example_lambdaE example_lambdaC)
def exampleProbe : Probe2 :=
probeState example_dt example_lambdaE example_lambdaC
def exampleCoarse : Coarse1 :=
coarseState example_dt example_lambdaE example_lambdaC
/-
Sanity theorems
-/
theorem shapeWaveform_def :
shapeWaveform = fun t => w 0 * recordingChannel (Obs 0) t + w 1 * recordingChannel (Obs 1) t := by
rfl
theorem energySignal_def :
energySignal = fun t => expect (ψ t) H := by
rfl
theorem totalSignal_pointwise (dt lambdaE lambdaC t : ) :
totalSignal dt lambdaE lambdaC t
=
shapeWaveform t
+ lambdaE * energyGradientMagnitude dt t
+ lambdaC * ΓSE t
+ η t := by
simp [totalSignal, sigAdd, sigScale]
theorem probeState_def (dt lambdaE lambdaC : ) :
probeState dt lambdaE lambdaC = W.mulVec (F (totalSignal dt lambdaE lambdaC)) := by
rfl
theorem coarseState_def (dt lambdaE lambdaC : ) :
coarseState dt lambdaE lambdaC = C.mulVec (W.mulVec (F (totalSignal dt lambdaE lambdaC))) := by
rfl
/-
The full concrete composition theorem.
-/
theorem full_pipeline_is_composition (dt lambdaE lambdaC : ) :
coarseState dt lambdaE lambdaC
=
C.mulVec (W.mulVec (F (totalSignal dt lambdaE lambdaC))) := by
rfl
end
end WSMConcrete