Download jacobian-formal/src/Invariants/EulerLoop.agda from Snapkitty/sov-kernel-monster: direct link, hf CLI and curl.
- Browser
- Download file 7.71 kB
-
https://huggingface.co/Snapkitty/sov-kernel-monster/resolve/main/jacobian-formal/src/Invariants/EulerLoop.agda
- Command line
-
hf download hf://Snapkitty/sov-kernel-monster/jacobian-formal/src/Invariants/EulerLoop.agda
-
curl -L -o EulerLoop.agda https://huggingface.co/Snapkitty/sov-kernel-monster/resolve/main/jacobian-formal/src/Invariants/EulerLoop.agda
7.71 kB
| -- Loop Invariant: step_euler | |
| -- BOB Quantum Kernel β Euler Method Integration | |
| -- Loop: do i = 1, state%dim | |
| -- Phase 2: Formal Structure (no proofs yet) | |
| -- WORM-sealed observable bookkeeping | |
| module Invariants.EulerLoop where | |
| open import Data.Nat using (β; _+_; _β€_; _<_; _β₯_; zero; suc) | |
| open import Data.Real using (β; _+_; _*_; _-_; _<_; _β€_; _β₯_) | |
| open import Data.Bool using (Bool; true; false) | |
| open import Relation.Binary.PropositionalEquality using (_β‘_; refl; cong) | |
| open import Core.ErrorCode using (ErrorCode; BOB_SUCCESS; isSuccess) | |
| open import Core.QuantumState using (QuantumState; Dimension; isValidDim) | |
| open import Core.Hamiltonian using (Hamiltonian; isValidHamiltonian) | |
| open import Core.Predicates using (basisStateInRange; dimensionPreserved; loopCompleted) | |
| -- ============================================================================ | |
| -- Loop Context: Euler step pre-conditions | |
| -- ============================================================================ | |
| -- Immutable parameters throughout the loop | |
| record EulerContext : Set where | |
| field | |
| state : QuantumState -- being mutated | |
| hamiltonian : Hamiltonian -- immutable operator | |
| dt : β -- immutable time step | |
| dim : β -- state dimension (β₯ 1) | |
| -- ============================================================================ | |
| -- Loop State at iteration i | |
| -- ============================================================================ | |
| record EulerLoopState : Set where | |
| field | |
| ctx : EulerContext | |
| i : β -- iteration counter [1, dim] | |
| -- Updated amplitudes accumulated so far | |
| num_updated : β -- how many amplitudes updated (β€ i-1) | |
| -- Tracking computation | |
| h_psi_computed : Bool -- H|Οβ© computed? | |
| error_status : β -- 0 = BOB_SUCCESS | |
| -- ============================================================================ | |
| -- Loop Invariant: What holds at each iteration i? | |
| -- ============================================================================ | |
| record EulerInvariant (s : EulerLoopState) (i : β) : Set where | |
| field | |
| -- 1. Loop iterator is in valid range: 1 β€ i β€ dim | |
| h_i_in_range : (i β₯ 1 β§ i β€ EulerContext.dim (EulerLoopState.ctx s)) β¨ (i β‘ EulerContext.dim (EulerLoopState.ctx s) + 1) | |
| -- 2. State remains valid | |
| h_state_valid : isValidDim (EulerContext.state (EulerLoopState.ctx s)) | |
| -- 3. Hamiltonian remains valid and unchanged | |
| h_ham_valid : isValidHamiltonian (EulerContext.hamiltonian (EulerLoopState.ctx s)) | |
| -- 4. Time step is positive | |
| h_dt_pos : EulerContext.dt (EulerLoopState.ctx s) > 0 | |
| -- 5. H|Οβ© has been computed (precondition before loop) | |
| h_h_psi_ready : EulerLoopState.h_psi_computed s β‘ true | |
| -- 6. Number of updated amplitudes = i - 1 | |
| -- (after iteration i, amplitude i has been updated) | |
| h_num_updated : EulerLoopState.num_updated s β‘ i - 1 | |
| -- 7. Error status remains success throughout | |
| h_error_clear : EulerLoopState.error_status s β‘ 0 | |
| -- 8. All predecessors updated in order | |
| h_ordered : β (j : β) β j < i β basisStateInRange j (EulerContext.dim (EulerLoopState.ctx s)) | |
| -- ============================================================================ | |
| -- Base Case: i = 1 (first iteration) | |
| -- ============================================================================ | |
| euler_base : | |
| (s : EulerLoopState) β | |
| EulerLoopState.i s β‘ 1 β | |
| isValidDim (EulerContext.state (EulerLoopState.ctx s)) β | |
| isValidHamiltonian (EulerContext.hamiltonian (EulerLoopState.ctx s)) β | |
| EulerContext.dt (EulerLoopState.ctx s) > 0 β | |
| EulerLoopState.h_psi_computed s β‘ true β | |
| EulerLoopState.num_updated s β‘ 0 β | |
| EulerLoopState.error_status s β‘ 0 β | |
| 1 β€ EulerContext.dim (EulerLoopState.ctx s) β | |
| EulerInvariant s 1 | |
| euler_base s h_i h_state_valid h_ham_valid h_dt_pos h_h_psi h_num_updated h_error h_dim_pos = | |
| record | |
| { h_i_in_range = inl β¨ refl , h_dim_pos β© | |
| ; h_state_valid = h_state_valid | |
| ; h_ham_valid = h_ham_valid | |
| ; h_dt_pos = h_dt_pos | |
| ; h_h_psi_ready = h_h_psi | |
| ; h_num_updated = h_num_updated | |
| ; h_error_clear = h_error | |
| ; h_ordered = Ξ» j h_j_lt_one β absurd (Β¬(<-one j) h_j_lt_one) | |
| } | |
| where | |
| Β¬(<-one : β n β Β¬(n < 1) | |
| Β¬(<-one 0 () | |
| -- ============================================================================ | |
| -- Inductive Step: i β i+1 | |
| -- ============================================================================ | |
| -- Represents one iteration of the loop body | |
| record EulerIterationStep (s s' : EulerLoopState) : Set where | |
| field | |
| -- Same context | |
| ctx_same : EulerLoopState.ctx s β‘ EulerLoopState.ctx s' | |
| -- Iterator increments | |
| i_increments : EulerLoopState.i s' β‘ EulerLoopState.i s + 1 | |
| -- Amplitude i was updated: new_amplitudes[i] = old[i] - CI * dt * h_psi[i] | |
| amplitude_updated : | |
| EulerLoopState.num_updated s' β‘ EulerLoopState.num_updated s + 1 | |
| -- H|Οβ© still available | |
| h_psi_still_ready : EulerLoopState.h_psi_computed s' β‘ true | |
| -- No errors during iteration | |
| error_unchanged : EulerLoopState.error_status s' β‘ 0 | |
| -- Inductive step | |
| euler_step : | |
| (s s' : EulerLoopState) (i : β) β | |
| EulerInvariant s i β | |
| EulerIterationStep s s' β | |
| EulerInvariant s' (i + 1) | |
| euler_step s s' i inv_i step = | |
| record | |
| { h_i_in_range = | |
| case (decide (i + 1 β‘ EulerContext.dim (EulerLoopState.ctx s) + 1)) of Ξ» where | |
| (yes p) β inr p | |
| (no Β¬p) β inl β¨ ? | |
| , ? -- i β₯ 1 implies i+1 β₯ 2; need to show i+1 β€ dim | |
| β© | |
| ; h_state_valid = EulerInvariant.h_state_valid inv_i | |
| ; h_ham_valid = EulerInvariant.h_ham_valid inv_i | |
| ; h_dt_pos = EulerInvariant.h_dt_pos inv_i | |
| ; h_h_psi_ready = EulerIterationStep.h_psi_still_ready step | |
| ; h_num_updated = cong suc (EulerInvariant.h_num_updated inv_i) | |
| ; h_error_clear = EulerIterationStep.error_unchanged step | |
| ; h_ordered = Ξ» j h_j_lt β | |
| let h_j_lt_suc_i = h_j_lt | |
| h_j_le_i = <-to-β€ h_j_lt_suc_i | |
| in case (decide (j β‘ i)) of Ξ» where | |
| (yes p) β | |
| -- j = i, so j is in range [1, dim] | |
| let h_i_in = EulerInvariant.h_i_in_range inv_i | |
| in case h_i_in of Ξ» where | |
| (inl β¨ h_i_ge_1 , h_i_le_dim β©) β | |
| basisStateInRange i (EulerContext.dim (EulerLoopState.ctx s)) | |
| (inr p_done) β absurd (Β¬-all-le-dim i (by-contradiction p_done)) | |
| (no Β¬p) β | |
| -- j < i, use previous invariant | |
| EulerInvariant.h_ordered inv_i j (β€-to-< h_j_le_i Β¬p) | |
| } | |
| -- ============================================================================ | |
| -- Exit Condition: Loop termination (i = dim + 1) | |
| -- ============================================================================ | |
| -- All amplitudes have been updated | |
| euler_exit : | |
| (s : EulerLoopState) (i : β) β | |
| EulerInvariant s i β | |
| i β‘ EulerContext.dim (EulerLoopState.ctx s) + 1 β | |
| -- Then: | |
| -- 1. All amplitudes updated | |
| (EulerLoopState.num_updated s β‘ EulerContext.dim (EulerLoopState.ctx s)) β§ | |
| -- 2. Loop completed | |
| (loopCompleted i (EulerContext.dim (EulerLoopState.ctx s) + 1)) β§ | |
| -- 3. No errors | |
| (EulerLoopState.error_status s β‘ 0) β§ | |
| -- 4. State still valid | |
| (isValidDim (EulerContext.state (EulerLoopState.ctx s))) | |
| euler_exit s i inv_i h_done = | |
| β¨ ? -- num_updated = i - 1 = (dim + 1) - 1 = dim | |
| , h_done | |
| , EulerInvariant.h_error_clear inv_i | |
| , EulerInvariant.h_state_valid inv_i | |
| β© | |