File size: 5,689 Bytes
2f887db | 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 | import Mathlib
import OAI.Computability.QuantumFactoring.BitStackLists
namespace OAI
section
namespace ExactQuantumFactoring.BitStackProgram
/-- Little-endian evaluation; leading zeroes at the high end are allowed. -/
def binaryValue : List Bool → ℕ
| [] => 0
| b::xs => b.toNat+2*binaryValue xs
def incrementWord : List Bool → List Bool
| [] => [true]
| false::xs => true::xs
| true::xs => false::incrementWord xs
def carryTail : List Bool → List Bool
| [] => [true]
| false::xs => true::xs
| true::xs => carryTail xs
def carryCount : List Bool → ℕ
| [] => 0
| false::_ => 0
| true::xs => carryCount xs+1
lemma incrementWord_value (xs : List Bool) : binaryValue (incrementWord xs)=binaryValue xs+1 := by
induction xs with
| nil => rfl
| cons x xs ih => cases x <;> simp [incrementWord,binaryValue,ih] <;> omega
lemma carryCount_le_length (xs : List Bool) : carryCount xs≤xs.length := by
induction xs with
| nil => rfl
| cons x xs ih => cases x <;> simp [carryCount,List.length_cons]; omega
lemma incrementWord_eq (xs : List Bool) :
incrementWord xs=List.replicate (carryCount xs) false++carryTail xs := by
induction xs with
| nil => rfl
| cons x xs ih => cases x <;> simp [incrementWord,carryCount,carryTail,ih,List.replicate_succ]
lemma incrementWord_length (xs : List Bool) : (incrementWord xs).length≤xs.length+1 := by
induction xs with
| nil => rfl
| cons x xs ih => cases x <;> simp [incrementWord,List.length_cons]; omega
variable {K : Type} [DecidableEq K]
def rippleBody (a t c : K) : Program K :=
.cases a (.push a true) (.push a true) (.seq (.push t false) (.push c true))
def ripple (a t c : K) : Program K := .loop c (rippleBody a t c) (rippleBody a t c)
def increment (a t c : K) : Program K :=
.seq (.push c true) (.seq (ripple a t c) (reverseMove t a))
lemma ripple_runs (a t c : K) (hat : a≠t) (hac : a≠c) (htc : t≠c)
(s : Store K) (hc : s c=[true]) :
Runs (ripple a t c) s
(Function.update (Function.update (Function.update s c []) a (carryTail (s a)))
t (List.replicate (carryCount (s a)) false++s t))
(4*(carryCount (s a)+1)) := by
generalize hs : s a=xs at *
induction xs generalizing s with
| nil =>
let u := Function.update s c []
let v := Function.update u a [true]
have hua : u a=[] := by simp [u,hac,hs]
have hvc : v c=[] := by simp [v,u,hac.symm]
have body : Runs (rippleBody a t c) u v 2 :=
Runs.cases_nil hua (by simpa only [hua] using Runs.push u a true)
have hh := Runs.loop_true (p:=rippleBody a t c) (a:=2) (b:=1) hc body (Runs.loop_nil hvc)
change Runs (.loop _ _ _) _ _ _
convert hh using 1
· simp only [carryCount,carryTail,List.replicate_zero,List.nil_append]
change Function.update v t (s t)=v
rw [← show v t=s t by simp [v,u,hat.symm,htc],Function.update_eq_self]
· rfl
| cons x xs ih =>
cases x
· let u := Function.update s c []
let w := Function.update u a xs
let v := Function.update w a (true::xs)
have hua : u a=false::xs := by simp [u,hac,hs]
have hvc : v c=[] := by simp [v,w,u,hac.symm]
have body : Runs (rippleBody a t c) u v 2 :=
Runs.cases_false hua (by simpa only [w,Function.update_self] using Runs.push w a true)
have hh := Runs.loop_true (p:=rippleBody a t c) (a:=2) (b:=1) hc body (Runs.loop_nil hvc)
change Runs (.loop _ _ _) _ _ _
convert hh using 1
· funext k
by_cases ha : k=a <;> by_cases ht : k=t <;> by_cases hcc : k=c <;>
simp_all [v,w,u,Function.update,carryCount,carryTail]
· rfl
· let u := Function.update s c []
let w := Function.update u a xs
let z := Function.update w t (false::w t)
let v := Function.update z c (true::z c)
have hua : u a=true::xs := by simp [u,hac,hs]
have hva : v a=xs := by simp [v,z,w,hac,hat]
have hvc : v c=[true] := by simp [v,z,w,u,htc.symm,hac.symm]
have body : Runs (rippleBody a t c) u v 3 :=
Runs.cases_true (a:=2) hua (Runs.seq (Runs.push w t false) (Runs.push z c true))
have hh := Runs.loop_true (p:=rippleBody a t c) (a:=3) (b:=4*(carryCount xs+1)) hc body (ih v hvc hva)
change Runs (.loop _ _ _) _ _ _
convert hh using 1
· funext k
by_cases ha : k=a <;> by_cases ht : k=t <;> by_cases hcc : k=c <;>
simp_all [v,z,w,u,Function.update,carryCount,carryTail,List.replicate_succ]
rw [← List.cons_append, ← List.replicate_succ, List.replicate_succ',List.append_assoc]
rfl
· simp only [carryCount]; omega
lemma increment_runs (a t c : K) (hat : a≠t) (hac : a≠c) (htc : t≠c)
(s : Store K) (ht : s t=[]) (hc : s c=[]) :
Runs (increment a t c) s (Function.update s a (incrementWord (s a)))
(6*carryCount (s a)+6) := by
let u := Function.update s c [true]
have hua : u a=s a := by simp [u,hac]
have hut : u t=[] := by simp [u,ht,htc]
have huc : u c=[true] := by simp [u]
let v := Function.update (Function.update (Function.update u c []) a (carryTail (u a)))
t (List.replicate (carryCount (u a)) false++u t)
have hp : Runs (.push c true) s u 1 := by simpa only [hc] using Runs.push s c true
have hh := Runs.seq hp
(Runs.seq (ripple_runs a t c hat hac htc u huc) (reverseMove_runs t a hat.symm v))
change Runs (.seq _ _) _ _ _
convert hh using 1
· funext k
by_cases ha : k=a <;> by_cases htk : k=t <;> by_cases hck : k=c <;>
simp_all [v,u,Function.update,incrementWord_eq]
· simp only [v,Function.update_self,hut,List.append_nil,List.length_replicate,hua]
omega
end ExactQuantumFactoring.BitStackProgram
end
end OAI
|