| |
| |
| |
| |
|
|
| 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) |
|
|
| |
| |
| |
|
|
| record EvolutionState : Set where |
| field |
| step : β |
| state : QuantumState |
| hamiltonian : Hamiltonian |
| dt : β |
| num_steps : β |
| error_status : β |
| normalization_log : β β Bool |
| accumulated_time : β |
|
|
| |
| |
| |
|
|
| |
| record EvolutionInvariant (s : EvolutionState) (k : β) : Set where |
| field |
| |
| h_step_eq : EvolutionState.step s β‘ k |
|
|
| |
| h_error : errorIsClear (EvolutionState.error_status s) |
|
|
| |
| h_state_valid : isValidDim (EvolutionState.state s) |
|
|
| |
| h_ham_valid : isValidHamiltonian (EvolutionState.hamiltonian s) |
|
|
| |
| h_dt_pos : EvolutionState.dt s > 0 |
|
|
| |
| h_in_range : k β€ EvolutionState.num_steps s |
|
|
| |
| h_accumulated_time : |
| EvolutionState.accumulated_time s β‘ (β.fromβ k) * (EvolutionState.dt s) |
|
|
| |
| h_norm_schedule : |
| β (m : β) β m < k β (m mod 100 β‘ 0) β (EvolutionState.normalization_log s m β‘ true) |
|
|
| |
| |
| |
|
|
| 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 _ () |
|
|
| |
| |
| |
|
|
| |
| 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 : 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_inv : (EvolutionState.error_status s' β‘ 0) β¨ |
| (EvolutionState.error_status s' β 0) |
|
|
| |
| 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 = ? |
| ; h_ham_valid = EvolutionInvariant.h_ham_valid inv_k |
| ; h_dt_pos = EvolutionInvariant.h_dt_pos inv_k |
| ; h_in_range = ? |
| ; 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) β |
| |
| if (k + 1) mod 100 β‘ 0 then true else ? |
| (no Β¬p) β |
| |
| EvolutionInvariant.h_norm_schedule inv_k m (β€-to-< h_le Β¬p) h_mod |
| } |
|
|
| |
| |
| |
|
|
| |
| evolution_exit : |
| (s : EvolutionState) (k : β) β |
| EvolutionInvariant s k β |
| k β‘ EvolutionState.num_steps s β |
| |
| |
| (loopCompleted k (EvolutionState.num_steps s)) β§ |
| |
| (errorIsClear (EvolutionState.error_status s)) β§ |
| |
| (isValidDim (EvolutionState.state s)) β§ |
| |
| (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 |
| β© |
|
|