Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Verify Lumiris
on:
push:
pull_request:
permissions:
contents: read
jobs:
executable:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- run: cargo fmt --manifest-path Lean-Semantics/rust/Cargo.toml -- --check
- run: scripts/test.sh
- run: scripts/demo.sh artifacts/mzi-demo
- uses: actions/upload-artifact@v4
with:
name: mzi-demonstration
path: artifacts/mzi-demo
formal:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: leanprover/lean-action@v1
with:
lake-package-directory: Lean-Semantics/lean
- uses: dtolnay/rust-toolchain@stable
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- run: scripts/demo.sh artifacts/mzi-demo --lean
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Toolchains, dependencies, generated proofs, and reports
**/target/
**/.lake/
**/*.olean
**/*.ilean
**/__pycache__/
.venv/
artifacts/
10 changes: 9 additions & 1 deletion Lean-Semantics/lean/Lumiris.lean
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
import Lumiris.Core.Units
import Lumiris.Core.Range
import Lumiris.Core.Range
import Lumiris.Core.Encoding
import Lumiris.Core.Approx
import Lumiris.Core.Semantics
import Lumiris.Core.Bounded
import Lumiris.Physics.Laws
import Lumiris.Physics.Photonic
import Lumiris.Physics.Control
import Lumiris.Tests
32 changes: 16 additions & 16 deletions Lean-Semantics/lean/Lumiris/Core/Approx.lean
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
import Lumiris.Core.Range

/-!
# Approximate operations and error composition

Analog hardware never computes exactly. Each of the four core operations is realized by a
physical process whose result is within `ε` of the exact result. These lemmas say how errors
propagate through `+ − × ÷`; they are the foundation of the certified error budget.
-/

namespace Lumiris

/-- An approximate binary operation with a certified error bound on the given input ranges. -/
structure ApproxOp (op : ℝ → ℝ → ℝ) (A B : Range) where
realize : ℝ → ℝ → ℝ
err : ℝ
err_nonneg : 0 ≤ err
spec : ∀ a b, A.contains a → B.contains b → |realize a b - op a b| ≤ err

namespace ErrProp

theorem add (a a' b b' : ℝ) : |(a' + b') - (a + b)| ≤ |a' - a| + |b' - b| := by
have h : (a' + b') - (a + b) = (a' - a) + (b' - b) := by ring
rw [h]; exact abs_add_le _ _

theorem sub (a a' b b' : ℝ) : |(a' - b') - (a - b)| ≤ |a' - a| + |b' - b| := by
have h : (a' - b') - (a - b) = (a' - a) - (b' - b) := by ring
rw [h]; exact abs_sub _ _

theorem mul (a a' b b' : ℝ) :
|a' * b' - a * b| ≤ |a' - a| * |b| + |a| * |b' - b| + |a' - a| * |b' - b| := by
have h : a' * b' - a * b = (a' - a) * b + a * (b' - b) + (a' - a) * (b' - b) := by ring
rw [h]
calc |(a' - a) * b + a * (b' - b) + (a' - a) * (b' - b)|
≤ |(a' - a) * b| + |a * (b' - b)| + |(a' - a) * (b' - b)| := abs_add_three _ _ _
≤ |(a' - a) * b| + |a * (b' - b)| + |(a' - a) * (b' - b)| := le_trans (abs_add_le _ _) (add_le_add_right (abs_add_le _ _) _)
_ = _ := by rw [abs_mul, abs_mul, abs_mul]

theorem div (a a' b b' : ℝ) (hb : b ≠ 0) (hb' : b' ≠ 0) :
|a' / b' - a / b| ≤ (|a' - a| * |b| + |a| * |b' - b|) / (|b| * |b'|) := by
have h : a' / b' - a / b = ((a' - a) * b - a * (b' - b)) / (b * b') := by
field_simp; ring
field_simp [hb, hb']; ring
rw [h, abs_div, abs_mul b b']
apply div_le_div_of_nonneg_right _ (by positivity)
calc |(a' - a) * b - a * (b' - b)| ≤ |(a' - a) * b| + |a * (b' - b)| := abs_sub _ _
_ = _ := by rw [abs_mul, abs_mul]

/-- A perturbed divisor stays away from zero: `|b| ≥ m`, `|b' − b| ≤ e`, `e < m` ⇒ `|b'| ≥ m − e`. -/
theorem abs_perturbed_ge {b b' m e : ℝ} (hm : m ≤ |b|) (he : |b' - b| ≤ e) : m - e ≤ |b'| := by
have := abs_sub_abs_le_abs_sub b b'
have h2 : |b - b'| = |b' - b| := abs_sub_comm _ _
linarith

/-- Monotone bound used in the division case of the budget theorem. -/
theorem div_bound {a a' b b' Ma Mb ea eb m : ℝ}
(hMa : |a| ≤ Ma) (hMb : |b| ≤ Mb) (hea : |a' - a| ≤ ea) (heb : |b' - b| ≤ eb)
Expand All @@ -73,7 +73,7 @@ theorem div_bound {a a' b b' Ma Mb ea eb m : ℝ}
· have : 0 < m - eb := by linarith
positivity
· exact mul_le_mul hm hb'abs (by linarith) (abs_nonneg _)

end ErrProp
end Lumiris

end Lumiris
90 changes: 90 additions & 0 deletions Lean-Semantics/lean/Lumiris/Core/Bounded.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import Lumiris.Core.Approx

/-!
# Bounded, per-device contracts

Unlike the legacy globally quantified `Realization`, these contracts only hold on
specified physical domains. Each operand's *actual*, not merely ideal, value must
be in the domain. Constants and inputs may both have error. No stochastic claim
is made. Model calibration remains an explicit external assumption.
-/
noncomputable section
namespace Lumiris.Bounded

structure Value where
ideal : ℝ
actual : ℝ
range : Range
error : ℝ
error_nonneg : 0 ≤ error
ideal_mem : range.contains ideal
close : |actual - ideal| ≤ error

def Value.actualRange (v : Value) : Range :=
⟨v.range.lo - v.error, v.range.hi + v.error, by linarith [v.range.le, v.error_nonneg]⟩

theorem Value.actual_mem (v : Value) : v.actualRange.contains v.actual := by
obtain ⟨hlo, hhi⟩ := v.ideal_mem
obtain ⟨he0, he1⟩ := abs_le.mp v.close
constructor <;> dsimp [Value.actualRange, Range.contains] <;> linarith

structure Contract (op : ℝ → ℝ → ℝ) where
inputLeft : Range
inputRight : Range
output : Range
transfer : ℝ → ℝ → ℝ
localError : ℝ
localError_nonneg : 0 ≤ localError
spec : ∀ x y, inputLeft.contains x → inputRight.contains y →
|transfer x y - op x y| ≤ localError
safe : ∀ x y, inputLeft.contains x → inputRight.contains y → output.contains (transfer x y)

/-- The common composition step. Physical-domain membership is an explicit obligation. -/
theorem compose {op : ℝ → ℝ → ℝ} (c : Contract op) (a b : Value)
(ha : c.inputLeft.contains a.actual) (hb : c.inputRight.contains b.actual)
{propagated : ℝ}
(hp : |op a.actual b.actual - op a.ideal b.ideal| ≤ propagated) :
|c.transfer a.actual b.actual - op a.ideal b.ideal| ≤ propagated + c.localError := by
calc
_ ≤ |c.transfer a.actual b.actual - op a.actual b.actual| +
|op a.actual b.actual - op a.ideal b.ideal| := abs_sub_le _ _ _
_ ≤ c.localError + propagated := add_le_add (c.spec _ _ ha hb) hp
_ = _ := add_comm _ _

theorem add (c : Contract (· + ·)) (a b : Value)
(ha : c.inputLeft.contains a.actual) (hb : c.inputRight.contains b.actual) :
|c.transfer a.actual b.actual - (a.ideal + b.ideal)| ≤ a.error + b.error + c.localError := by
apply compose c a b ha hb
exact le_trans (ErrProp.add _ _ _ _) (add_le_add a.close b.close)

theorem sub (c : Contract (· - ·)) (a b : Value)
(ha : c.inputLeft.contains a.actual) (hb : c.inputRight.contains b.actual) :
|c.transfer a.actual b.actual - (a.ideal - b.ideal)| ≤ a.error + b.error + c.localError := by
apply compose c a b ha hb
exact le_trans (ErrProp.sub _ _ _ _) (add_le_add a.close b.close)

theorem mul (c : Contract (· * ·)) (a b : Value)
(ha : c.inputLeft.contains a.actual) (hb : c.inputRight.contains b.actual) :
|c.transfer a.actual b.actual - a.ideal * b.ideal| ≤
a.error * b.range.mag + a.range.mag * b.error + a.error * b.error + c.localError := by
apply compose c a b ha hb
have hMa := Range.abs_le_mag a.ideal_mem
have hMb := Range.abs_le_mag b.ideal_mem
have h1 := mul_le_mul a.close hMb (abs_nonneg _) a.error_nonneg
have h2 := mul_le_mul hMa b.close (abs_nonneg _) a.range.mag_nonneg
have h3 := mul_le_mul a.close b.close (abs_nonneg _) a.error_nonneg
have h := ErrProp.mul a.ideal a.actual b.ideal b.actual
linarith

/-- Division requires separation of the *perturbed* denominator from zero. -/
theorem div (c : Contract (· / ·)) (a b : Value)
(ha : c.inputLeft.contains a.actual) (hb : c.inputRight.contains b.actual)
(hsafe : b.error < b.range.minMag) :
|c.transfer a.actual b.actual - a.ideal / b.ideal| ≤
(a.error * b.range.mag + a.range.mag * b.error) /
(b.range.minMag * (b.range.minMag - b.error)) + c.localError := by
apply compose c a b ha hb
exact ErrProp.div_bound (Range.abs_le_mag a.ideal_mem) (Range.abs_le_mag b.ideal_mem)
a.close b.close (Range.minMag_le_abs b.ideal_mem) hsafe a.error_nonneg b.error_nonneg

end Lumiris.Bounded
66 changes: 43 additions & 23 deletions Lean-Semantics/lean/Lumiris/Core/Encoding.lean
Original file line number Diff line number Diff line change
@@ -1,42 +1,60 @@
import Lumiris.Core.Range

import Mathlib.Analysis.SpecialFunctions.Trigonometric.Basic

/-!
# Number encodings

A real number is carried by a physical quantity (optical power, field amplitude, voltage,
conductance, ...). An `Encoding` is a pair `enc/dec` that round-trips exactly on its domain,
together with the physical constraints of the carrier (e.g. power is non-negative).
-/


noncomputable section

namespace Lumiris

/-- An encoding of reals (on `domain`) into a physical carrier type `Q`. -/
structure Encoding (Q : Type) where
domain : Range
enc : ℝ → Q
dec : Q → ℝ
dec_enc : ∀ x, domain.contains x → dec (enc x) = x

namespace Encoding

/-- Linear scaling into a carrier with full-scale value `fs` (e.g. `1.0 ↦ 1 mW`). -/
noncomputable def scaled (fs : ℝ) (hfs : 0 < fs) (domain : Range) : Encoding ℝ where
domain := domain
enc x := x * fs
dec q := q / fs
dec_enc x _ := by field_simp
dec_enc x _ := by field_simp [ne_of_gt hfs]

/-- Unipolar optical power: domain `[0, 1]`, carrier `P ∈ [0, Pmax]`. The carrier is never
negative on the domain. -/
noncomputable def optPow (Pmax : ℝ) (h : 0 < Pmax) : Encoding ℝ :=
scaled Pmax h ⟨0, 1, by norm_num⟩

theorem optPow_nonneg (Pmax : ℝ) (h : 0 < Pmax) {x : ℝ}
(hx : (optPow Pmax h).domain.contains x) : 0 ≤ (optPow Pmax h).enc x := by
obtain ⟨h0, _⟩ := hx
simp only [optPow, scaled]
exact mul_nonneg h0 h.le


theorem optPow_le_max (Pmax : ℝ) (h : 0 < Pmax) {x : ℝ}
(hx : (optPow Pmax h).domain.contains x) : (optPow Pmax h).enc x ≤ Pmax := by
have hx1 : x ≤ 1 := hx.2
change x * Pmax ≤ Pmax
nlinarith

/-- Finite-resolution carriers need an approximate round trip, not an impossible exact inverse. -/
structure ApproxEncoding (Q : Type) where
domain : Range
enc : ℝ → Q
dec : Q → ℝ
error : ℝ
error_nonneg : 0 ≤ error
roundtrip : ∀ x, domain.contains x → |dec (enc x) - x| ≤ error

/-- Differential (signed) encoding into a pair of non-negative carriers,
`x = x⁺ − x⁻` (balanced photodetection, `G⁺ − G⁻` crossbar columns). -/
noncomputable def differential (m : ℝ) (hm : 0 ≤ m) : Encoding (ℝ × ℝ) where
Expand All @@ -48,32 +66,34 @@ noncomputable def differential (m : ℝ) (hm : 0 ≤ m) : Encoding (ℝ × ℝ)
rcases le_total 0 x with hx | hx
· rw [max_eq_left hx, max_eq_right (by linarith)]; ring
· rw [max_eq_right hx, max_eq_left (by linarith)]; ring

theorem differential_nonneg (m : ℝ) (hm : 0 ≤ m) (x : ℝ) :
0 ≤ ((differential m hm).enc x).1 ∧ 0 ≤ ((differential m hm).enc x).2 :=
⟨le_max_right _ _, le_max_right _ _⟩

/-- Conductance encoding: a weight `w ∈ [0, 1]` is stored as `G = Gmin + w·(Gmax − Gmin)`,
which always lies in the physically allowed window `[Gmin, Gmax]`. -/
noncomputable def conductance (Gmin Gmax : ℝ) (h : Gmin < Gmax) : Encoding ℝ where
noncomputable def conductance (Gmin Gmax : ℝ) (_h0 : 0 ≤ Gmin) (h : Gmin < Gmax) : Encoding ℝ where
domain := ⟨0, 1, by norm_num⟩
enc w := Gmin + w * (Gmax - Gmin)
dec G := (G - Gmin) / (Gmax - Gmin)
dec_enc w _ := by
have : Gmax - Gmin ≠ 0 := by linarith
field_simp
theorem conductance_in_window (Gmin Gmax : ℝ) (h : Gmin < Gmax) {w : ℝ}
(hw : (conductance Gmin Gmax h).domain.contains w) :
Gmin ≤ (conductance Gmin Gmax h).enc w ∧ (conductance Gmin Gmax h).enc w ≤ Gmax := by

theorem conductance_in_window (Gmin Gmax : ℝ) (h0 : 0 ≤ Gmin) (h : Gmin < Gmax) {w : ℝ}
(hw : (conductance Gmin Gmax h0 h).domain.contains w) :
Gmin ≤ (conductance Gmin Gmax h0 h).enc w ∧ (conductance Gmin Gmax h0 h).enc w ≤ Gmax := by
obtain ⟨h0, h1⟩ := hw
simp only [conductance]
constructor <;> nlinarith

/-- Phase encoding of an angle: `φ ↦ e^{iφ}` is only invertible modulo `2π`, so the domain
is `[0, 2π)`-shaped (we use the closed `[0, 2π]` range and the branch `[0, 2π)` in control). -/

/-- A closed analysis envelope, NOT an invertible phase encoding: 0 and 2π coincide. -/
def phaseDomain : Range := ⟨0, 2 * Real.pi, by positivity⟩


/-- The half-open branch used when a unique phase representative is required. -/
def PhaseAngle := {φ : ℝ // 0 ≤ φ ∧ φ < 2 * Real.pi}

end Encoding
end Lumiris

end Lumiris
Loading
Loading