| |
| |
| |
| |
| |
|
|
| 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) |
|
|
| |
| |
| |
|
|
| |
| record EulerContext : Set where |
| field |
| state : QuantumState |
| hamiltonian : Hamiltonian |
| dt : β |
| dim : β |
|
|
| |
| |
| |
|
|
| record EulerLoopState : Set where |
| field |
| ctx : EulerContext |
| i : β |
| |
| num_updated : β |
| |
| h_psi_computed : Bool |
| error_status : β |
|
|
| |
| |
| |
|
|
| record EulerInvariant (s : EulerLoopState) (i : β) : Set where |
| field |
| |
| h_i_in_range : (i β₯ 1 β§ i β€ EulerContext.dim (EulerLoopState.ctx s)) β¨ (i β‘ EulerContext.dim (EulerLoopState.ctx s) + 1) |
|
|
| |
| h_state_valid : isValidDim (EulerContext.state (EulerLoopState.ctx s)) |
|
|
| |
| h_ham_valid : isValidHamiltonian (EulerContext.hamiltonian (EulerLoopState.ctx s)) |
|
|
| |
| h_dt_pos : EulerContext.dt (EulerLoopState.ctx s) > 0 |
|
|
| |
| h_h_psi_ready : EulerLoopState.h_psi_computed s β‘ true |
|
|
| |
| |
| h_num_updated : EulerLoopState.num_updated s β‘ i - 1 |
|
|
| |
| h_error_clear : EulerLoopState.error_status s β‘ 0 |
|
|
| |
| h_ordered : β (j : β) β j < i β basisStateInRange j (EulerContext.dim (EulerLoopState.ctx s)) |
|
|
| |
| |
| |
|
|
| 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 () |
|
|
| |
| |
| |
|
|
| |
| record EulerIterationStep (s s' : EulerLoopState) : Set where |
| field |
| -- Same context |
| ctx_same : EulerLoopState.ctx s β‘ EulerLoopState.ctx s' |
|
|
| |
| 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_psi_still_ready : EulerLoopState.h_psi_computed s' β‘ true |
| |
| -- No errors during iteration |
| error_unchanged : EulerLoopState.error_status s' β‘ 0 |
|
|
| |
| 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 β¨ ? |
| , ? |
| β© |
| ; 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) β |
| |
| 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) β |
| |
| EulerInvariant.h_ordered inv_i j (β€-to-< h_j_le_i Β¬p) |
| } |
|
|
| |
| |
| |
|
|
| |
| euler_exit : |
| (s : EulerLoopState) (i : β) β |
| EulerInvariant s i β |
| i β‘ EulerContext.dim (EulerLoopState.ctx s) + 1 β |
| |
| |
| (EulerLoopState.num_updated s β‘ EulerContext.dim (EulerLoopState.ctx s)) β§ |
| |
| (loopCompleted i (EulerContext.dim (EulerLoopState.ctx s) + 1)) β§ |
| |
| (EulerLoopState.error_status s β‘ 0) β§ |
| |
| (isValidDim (EulerContext.state (EulerLoopState.ctx s))) |
|
|
| euler_exit s i inv_i h_done = |
| β¨ ? |
| , h_done |
| , EulerInvariant.h_error_clear inv_i |
| , EulerInvariant.h_state_valid inv_i |
| β© |
|
|