File size: 7,622 Bytes
9425aed | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | -- Loop Invariant: integrator_evolve
-- BOB Quantum Kernel β Time Evolution Main Loop
-- Phase 2: Formal Structure (no proofs yet)
-- WORM-sealed observable bookkeeping
module Invariants.EvolutionLoop 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; canApplyGate)
open import Core.Hamiltonian using (Hamiltonian; isValidHamiltonian; hamiltonianImmutable)
open import Core.Predicates using (stepInRange; errorIsClear; needsNormalization; canContinueLoop; loopCompleted)
-- ============================================================================
-- Loop State: All variables captured at step k
-- ============================================================================
record EvolutionState : Set where
field
step : β -- iteration counter k β [0, num_steps]
state : QuantumState -- quantum state (mutable, evolving)
hamiltonian : Hamiltonian -- time-independent operator (immutable)
dt : β -- time step (immutable, positive)
num_steps : β -- total iteration count (immutable)
error_status : β -- error code (0=BOB_SUCCESS, etc.)
normalization_log : β β Bool -- which steps were normalized?
accumulated_time : β -- β(dt for steps taken)
-- ============================================================================
-- Loop Invariant: Predicate over state and step count
-- ============================================================================
-- At each iteration k, what must be true?
record EvolutionInvariant (s : EvolutionState) (k : β) : Set where
field
-- 1. Step counter matches loop variable
h_step_eq : EvolutionState.step s β‘ k
-- 2. Error status is success (loop hasn't aborted)
h_error : errorIsClear (EvolutionState.error_status s)
-- 3. Quantum state is dimensionally valid
h_state_valid : isValidDim (EvolutionState.state s)
-- 4. Hamiltonian is valid
h_ham_valid : isValidHamiltonian (EvolutionState.hamiltonian s)
-- 5. Time step is positive
h_dt_pos : EvolutionState.dt s > 0
-- 6. Current step β€ num_steps (loop guard)
h_in_range : k β€ EvolutionState.num_steps s
-- 7. Accumulated time = k * dt
h_accumulated_time :
EvolutionState.accumulated_time s β‘ (β.fromβ k) * (EvolutionState.dt s)
-- 8. Normalization log: for each step m β€ k, if m β‘ 0 (mod 100), then normalized
h_norm_schedule :
β (m : β) β m < k β (m mod 100 β‘ 0) β (EvolutionState.normalization_log s m β‘ true)
-- ============================================================================
-- Base Case: k = 0 (before loop starts)
-- ============================================================================
evolution_base :
(s : EvolutionState) β
EvolutionState.step s β‘ 0 β
EvolutionState.error_status s β‘ 0 β
isValidDim (EvolutionState.state s) β
isValidHamiltonian (EvolutionState.hamiltonian s) β
EvolutionState.dt s > 0 β
EvolutionState.accumulated_time s β‘ 0 β
EvolutionInvariant s 0
evolution_base s h_step h_error h_state_valid h_ham_valid h_dt_pos h_acc_time =
record
{ h_step_eq = h_step
; h_error = h_error
; h_state_valid = h_state_valid
; h_ham_valid = h_ham_valid
; h_dt_pos = h_dt_pos
; h_in_range = Ξ» where
0 β zero
; h_accumulated_time = h_acc_time
; h_norm_schedule = Ξ» m h_lt_zero _ β absurd (Β¬(<-zero m) h_lt_zero)
}
where
Β¬(<-zero : β n β Β¬(n < 0)
Β¬(<-zero _ ()
-- ============================================================================
-- Inductive Step: k β k+1
-- ============================================================================
-- Represents one call to integrator_step(state, hamiltonian)
record StepTransition (s s' : EvolutionState) : Set where
field
-- Same invariant on input state
pre_inv : EvolutionInvariant s (EvolutionState.step s)
-- Step counter increments by 1
step_increments : EvolutionState.step s' β‘ EvolutionState.step s + 1
-- State changed (evolved one step)
state_changed : EvolutionState.state s β EvolutionState.state s'
-- Hamiltonian unchanged
ham_unchanged :
(EvolutionState.hamiltonian s β‘ EvolutionState.hamiltonian s') β¨
(hamiltonianImmutable (EvolutionState.hamiltonian s) (EvolutionState.hamiltonian s'))
-- Quantum state remains valid-dimensioned after evolution (physics invariant)
state_valid_preserved :
isValidDim (EvolutionState.state s) β
isValidDim (EvolutionState.state s')
-- Error status either remains success or becomes error (and loop exits)
error_inv : (EvolutionState.error_status s' β‘ 0) β¨
(EvolutionState.error_status s' β 0)
-- Accumulated time increases by dt
time_advances :
EvolutionState.accumulated_time s' β‘
EvolutionState.accumulated_time s + EvolutionState.dt s
-- Inductive step: if invariant holds at k, then after one step it holds at k+1
-- (OR the loop exits due to error)
evolution_step :
(s s' : EvolutionState) (k : β) β
EvolutionInvariant s k β
StepTransition s s' β
-- If no error, then invariant holds at k+1
(EvolutionState.error_status s' β‘ 0) β
EvolutionInvariant s' (k + 1)
evolution_step s s' k inv_k trans h_no_error =
record
{ h_step_eq = StepTransition.step_increments trans
; h_error = h_no_error
; h_state_valid = ? -- state remains valid-dimensioned after step
; h_ham_valid = EvolutionInvariant.h_ham_valid inv_k
; h_dt_pos = EvolutionInvariant.h_dt_pos inv_k
; h_in_range = ? -- (k+1) β€ num_steps follows from k β€ num_steps and loop guard
; h_accumulated_time = StepTransition.time_advances trans
; h_norm_schedule = Ξ» m h_lt h_mod β
let h_le = <-to-β€ h_lt
in case (decide (m β‘ k + 1)) of Ξ» where
(yes p) β
-- m = k+1, check if normalization_log updated
if (k + 1) mod 100 β‘ 0 then true else ?
(no Β¬p) β
-- m < k+1 and m β k+1, so m β€ k, use old log
EvolutionInvariant.h_norm_schedule inv_k m (β€-to-< h_le Β¬p) h_mod
}
-- ============================================================================
-- Exit Condition: Loop termination at k = num_steps
-- ============================================================================
-- When loop exits (k β‘ num_steps), postcondition is satisfied
evolution_exit :
(s : EvolutionState) (k : β) β
EvolutionInvariant s k β
k β‘ EvolutionState.num_steps s β
-- Then:
-- 1. Loop has completed all iterations
(loopCompleted k (EvolutionState.num_steps s)) β§
-- 2. Error status is success
(errorIsClear (EvolutionState.error_status s)) β§
-- 3. State is valid (ready for measurement/output)
(isValidDim (EvolutionState.state s)) β§
-- 4. Total time accumulated matches intended duration
(EvolutionState.accumulated_time s β‘
(β.fromβ k) * (EvolutionState.dt s))
evolution_exit s k inv_k h_done =
β¨ h_done
, EvolutionInvariant.h_error inv_k
, EvolutionInvariant.h_state_valid inv_k
, EvolutionInvariant.h_accumulated_time inv_k
β©
|