carry-agent / runtime /quantum /QuantumSpec.agda
SNAPKITTYWEST's picture
push from SNAPKITTYWEST/carry-agent
80d7559 verified
Raw
History Blame Contribute Delete
5.94 kB
-- CarryQuantum.QuantumSpec
-- Agda dependent-type specification.
-- Illegal FSM transitions are unrepresentable at the type level.
-- Compile: agda QuantumSpec.agda
module QuantumSpec where
open import Data.Nat using (β„•; _<_; suc; zero; _+_)
open import Data.Bool using (Bool; true; false)
open import Data.Product using (_Γ—_; _,_; βˆƒ)
open import Data.Sum using (_⊎_; inj₁; injβ‚‚)
open import Relation.Binary.PropositionalEquality using (_≑_; refl)
open import Data.Empty using (βŠ₯; βŠ₯-elim)
-- ============================================================
-- FSM States
-- ============================================================
data FSMState : Set where
Init : FSMState
Prepare : FSMState
Entangle : FSMState
Compute : FSMState
Measure : FSMState
Verify : FSMState
Commit : FSMState
Halted : FSMState
CycleLimit : FSMState
-- ============================================================
-- Allowed Transition β€” encoded as a type index.
-- A value of type `Transition s s'` is a proof that the edge exists.
-- An illegal (s, s') pair has no constructor β†’ it is unrepresentable.
-- ============================================================
data Transition : FSMState β†’ FSMState β†’ Set where
T-Init-Prepare : Transition Init Prepare
T-Prep-Entangle : Transition Prepare Entangle
T-Ent-Compute : Transition Entangle Compute
T-Comp-Measure : Transition Compute Measure
T-Meas-Verify : Transition Measure Verify
T-Veri-Commit : Transition Verify Commit
T-Comm-Prepare : Transition Commit Prepare -- loop back
T-Comm-Commit : Transition Commit Commit -- terminal commit
T-Any-Halted : βˆ€ {s} β†’ Transition s Halted -- emergency halt
-- ============================================================
-- Terminal states
-- ============================================================
data Terminal : FSMState β†’ Set where
T-Halted : Terminal Halted
T-Cycle : Terminal CycleLimit
-- INV-5 by construction:
-- There is no constructor of type `Transition Halted s'` for s' β‰  Halted.
-- Proof: inspect every constructor β€” T-Any-Halted is the only one where
-- Halted appears, and it appears on the RIGHT. So Halted as a source
-- produces no constructors at all (except via T-Any-Halted arriving at Halted).
-- The type Transition Halted (non-Halted) is uninhabited.
-- We can prove this:
halted-absorbing : βˆ€ {s'} β†’ Transition Halted s' β†’ s' ≑ Halted
halted-absorbing T-Any-Halted = refl
-- No other constructor has Halted as its first argument β†’ proof is total.
cyclelimit-absorbing : βˆ€ {s'} β†’ Transition CycleLimit s' β†’ s' ≑ Halted
cyclelimit-absorbing T-Any-Halted = refl
-- ============================================================
-- FSM record with cycle budget
-- ============================================================
record FSM : Set where
field
state : FSMState
cycle : β„•
maxCycle : β„•
bounded : cycle < suc maxCycle -- cycle ≀ maxCycle
-- ============================================================
-- Step function β€” INV-4 guaranteed by type signature.
-- `step` only accepts a `Transition (fsm .state) s'` witness.
-- If you cannot construct that witness, you cannot call step.
-- ============================================================
data StepResult : Set where
Ok : FSM β†’ StepResult
Err : StepResult
step : (fsm : FSM) β†’ (s' : FSMState) β†’ Transition (FSM.state fsm) s' β†’ StepResult
step fsm s' t with FSM.cycle fsm < FSM.maxCycle
... | false = Ok (record fsm { state = CycleLimit })
... | true = Ok (record fsm
{ state = s'
; cycle = suc (FSM.cycle fsm)
; bounded = {!!} -- obligation: suc cycle ≀ suc maxCycle
})
-- step never returns Err for allowed transitions β€” that is the type guarantee.
-- For terminal states the caller cannot construct a Transition witness at all,
-- so they cannot call step. No runtime check needed.
-- ============================================================
-- Normalisation
-- ============================================================
postulate
Amp : Set
normSq : Amp β†’ β„• -- simplified; use rationals or reals in full version
-- A statevector of dimension 2^n
StateVec : β„• β†’ Set
StateVec n = (i : β„•) β†’ Amp -- domain is Fin(2^n) in a full version
postulate
sumNormSq : βˆ€ {n} β†’ StateVec n β†’ β„•
-- DEF-1: Normalised
Normalised : βˆ€ {n} β†’ StateVec n β†’ Set
Normalised {n} ψ = sumNormSq ψ ≑ 1
-- DEF-2: Unitary (norm-preserving)
IsUnitary : βˆ€ {n} β†’ (StateVec n β†’ StateVec n) β†’ Set
IsUnitary {n} U = βˆ€ ψ β†’ Normalised ψ β†’ Normalised (U ψ)
-- INV-1: Unitary gates preserve normalisation β€” direct from the definition
inv1 : βˆ€ {n} (ψ : StateVec n) (U : StateVec n β†’ StateVec n)
β†’ IsUnitary U β†’ Normalised ψ β†’ Normalised (U ψ)
inv1 ψ U hU hN = hU ψ hN
-- ============================================================
-- Agent ownership
-- ============================================================
postulate
QubitSet : Set
βˆ… : QubitSet
_∩_ : QubitSet β†’ QubitSet β†’ QubitSet
emptyInter : βˆ€ (a b : QubitSet) β†’ Set -- a ∩ b ≑ βˆ…
-- DEF-7: Disjoint ownership
OwnershipDisjoint : QubitSet β†’ QubitSet β†’ Set
OwnershipDisjoint a b = emptyInter a b
-- INV-7: If ownership is disjoint, agent A's qubits are not in agent B's set
postulate
inv7 : βˆ€ (a b : QubitSet) β†’ OwnershipDisjoint a b
β†’ βˆ€ q β†’ q ∈ a β†’ q βˆ‰ b -- ∈/βˆ‰ defined over QubitSet
where
postulate
_∈_ : β„• β†’ QubitSet β†’ Set
_βˆ‰_ : β„• β†’ QubitSet β†’ Set