Download runtime/quantum/QuantumSpec.agda from Snapkitty/carry-agent: direct link, hf CLI and curl.
- Browser
- Download file 5.94 kB
-
https://huggingface.co/Snapkitty/carry-agent/resolve/main/runtime/quantum/QuantumSpec.agda
- Command line
-
hf download hf://Snapkitty/carry-agent/runtime/quantum/QuantumSpec.agda
-
curl -L -o QuantumSpec.agda https://huggingface.co/Snapkitty/carry-agent/resolve/main/runtime/quantum/QuantumSpec.agda
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 | |