File size: 6,690 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import Mathlib
import OAI.Computability.QuantumFactoring.BitStackBinaryBasics
import OAI.Computability.QuantumFactoring.BitStackUnary

namespace OAI



section

namespace ExactQuantumFactoring.BitStackProgram

/-- Literal full-adder truth tables, not an arithmetic oracle. -/
def sumBit (a b c : Bool) : Bool := xor a (xor b c)
def carryBit (a b c : Bool) : Bool := (a && b) || (a && c) || (b && c)

lemma sum_carry (a b c : Bool) :
    a.toNat+b.toNat+c.toNat=(sumBit a b c).toNat+2*(carryBit a b c).toNat := by
  cases a <;> cases b <;> cases c <;> decide

abbrev AddState := List Bool × List Bool × Bool × List Bool
abbrev addStateCode : AddState→List Bool :=
  prodCode id (prodCode id (prodCode Procedure.boolCode id))

def addStep (s : AddState) : AddState :=
  (s.1.tail,s.2.1.tail,
    carryBit (s.1.headD false) (s.2.1.headD false) s.2.2.1,
    sumBit (s.1.headD false) (s.2.1.headD false) s.2.2.1 :: s.2.2.2)

def addMeasure (s : AddState) : ℕ := s.1.length+s.2.1.length+s.2.2.1.toNat

def residual (s : AddState) : ℕ := binaryValue s.1+binaryValue s.2.1+s.2.2.1.toNat

def addInvariant (s : AddState) : ℕ :=
  binaryValue s.2.2.2.reverse+2^s.2.2.2.length*residual s

lemma binaryValue_head_tail (xs : List Bool) :
    binaryValue xs=(xs.headD false).toNat+2*binaryValue xs.tail := by
  cases xs <;> rfl

lemma addInvariant_step (s : AddState) : addInvariant (addStep s)=addInvariant s := by
  have hx:=binaryValue_head_tail s.1
  have hy:=binaryValue_head_tail s.2.1
  have hh:=sum_carry (s.1.headD false) (s.2.1.headD false) s.2.2.1
  simp only [addInvariant,addStep,List.reverse_cons,binaryValue_append,List.length_reverse,
    binaryValue,List.length_cons,pow_succ,residual]
  rw [hx,hy]
  nlinarith [congrArg (fun n : ℕ=>2^s.2.2.2.length*n) hh]

lemma addMeasure_step (s : AddState) : addMeasure (addStep s)≤addMeasure s-1 := by
  rcases s with ⟨xs,ys,c,zs⟩
  cases xs with
  | nil=>cases ys with
    | nil=>cases c <;> simp [addMeasure,addStep,carryBit]
    | cons b bs=>cases b <;> cases c <;> simp [addMeasure,addStep,carryBit]
  | cons a as=>cases ys with
    | nil=>cases a <;> cases c <;> simp [addMeasure,addStep,carryBit]
    | cons b bs=>cases a <;> cases b <;> cases c <;>
        simp [addMeasure,addStep,carryBit] <;> omega

lemma addMeasure_iterate (i : ℕ) (s : AddState) :
    addMeasure (addStep^[i] s)≤addMeasure s-i := by
  induction i with
  | zero=>simp
  | succ i ih=>
    rw [Function.iterate_succ_apply']
    exact (addMeasure_step _).trans (by omega)

lemma addInvariant_iterate (i : ℕ) (s : AddState) :
    addInvariant (addStep^[i] s)=addInvariant s := by
  induction i with
  | zero=>rfl
  | succ i ih=>rw [Function.iterate_succ_apply',addInvariant_step,ih]

lemma residual_zero {s : AddState} (h : addMeasure s=0) : residual s=0 := by
  have hx : s.1=[]:=List.length_eq_zero_iff.mp (by unfold addMeasure at h;omega)
  have hy : s.2.1=[]:=List.length_eq_zero_iff.mp (by unfold addMeasure at h;omega)
  have hc : s.2.2.1.toNat=0:=by unfold addMeasure at h;omega
  simp [residual,hx,hy,hc,binaryValue]

def wordAdd (x : List Bool×List Bool) : List Bool :=
  (addStep^[x.1.length+x.2.length+1] (x.1,x.2,false,[])).2.2.2.reverse

lemma wordAdd_value (x : List Bool×List Bool) :
    binaryValue (wordAdd x)=binaryValue x.1+binaryValue x.2 := by
  let s : AddState:=(x.1,x.2,false,[])
  let i:=x.1.length+x.2.length+1
  have hz : addMeasure (addStep^[i] s)=0:=by
    have hh:=addMeasure_iterate i s
    simp only [s,i,addMeasure,Bool.toNat_false,Nat.add_zero] at hh
    dsimp only [s,i,addMeasure]
    omega
  have hh:=addInvariant_iterate i s
  simp only [addInvariant,residual_zero hz,Nat.mul_zero,Nat.add_zero] at hh
  simpa [s,i,wordAdd,residual,binaryValue] using hh

lemma addStep_size (s : AddState) :
    (addStateCode (addStep s)).length≤(addStateCode s).length+1 := by
  simp only [addStateCode,addStep,prodCode,pairBits_length,id_eq,Procedure.boolCode,
    List.length_cons,List.length_tail]
  omega

namespace Procedure
variable {α : Type} {ea : α→List Bool} {f : α→α}

noncomputable def iterateLinear (p : Procedure ea ea f) (c : ℕ)
    (hg : ∀a,(ea (f a)).length≤(ea a).length+c) :
    Procedure (prodCode unaryCode ea) ea (fun x=>f^[x.1] x.2) := by
  have hh : ∀ i a,(ea (f^[i] a)).length≤(ea a).length+i*c:=by
    intro i a
    induction i with
    | zero=>simp
    | succ i ih=>
      rw [Function.iterate_succ_apply']
      have h:=hg (f^[i] a)
      nlinarith
  exact p.iterate (Polynomial.C (c+1)*Polynomial.X) (by
    intro n a i hi
    have h:=hh i a
    simp only [Polynomial.eval_mul,Polynomial.eval_C,Polynomial.eval_X]
    nlinarith)

noncomputable def addStepP : Procedure addStateCode addStateCode addStep := by
  let x:=first (id : List Bool→List Bool) (prodCode id (prodCode boolCode id))
  let rem:=second (id : List Bool→List Bool) (prodCode id (prodCode boolCode id))
  let y:=(first (id : List Bool→List Bool) (prodCode boolCode id)).comp rem
  let co:=(second (id : List Bool→List Bool) (prodCode boolCode id)).comp rem
  let c:=(first boolCode (id : List Bool→List Bool)).comp co
  let zs:=(second boolCode (id : List Bool→List Bool)).comp co
  let bits:=(head.comp x).pair ((head.comp y).pair c)
  let newc:=(ofBool₃ boolCode carryBit).comp bits
  let newb:=(ofBool₃ boolCode sumBit).comp bits
  exact ((tail.comp x).pair ((tail.comp y).pair
    (newc.pair (cons.comp (newb.pair zs))))).congrFun (by intro s;rfl)

noncomputable def wordAddP : Procedure (prodCode (id : List Bool→List Bool) id) id wordAdd := by
  let x:=first (id : List Bool→List Bool) (id : List Bool→List Bool)
  let y:=second (id : List Bool→List Bool) (id : List Bool→List Bool)
  let len:=unarySuccessor.comp (unaryAdd.comp ((length.comp x).pair (length.comp y)))
  let start:=x.pair (y.pair ((constant (prodCode id id) boolCode false).pair
    (constant (prodCode id id) (id : List Bool→List Bool) [])))
  let rep:=addStepP.iterateLinear 1 addStep_size
  let co:=(second (id : List Bool→List Bool) (prodCode boolCode id)).comp
    (second (id : List Bool→List Bool) (prodCode id (prodCode boolCode id)))
  let zs:=(second boolCode (id : List Bool→List Bool)).comp co
  exact (reverse.comp (zs.comp (rep.comp (len.pair start)))).congrFun (by intro x;rfl)

noncomputable def binaryAdd : Procedure (prodCode Nat.bits Nat.bits) Nat.bits
    (fun x=>x.1+x.2) :=
  ((normalize.comp wordAddP).precompose (fun x : ℕ×ℕ=>(x.1.bits,x.2.bits))).congrFun
    (by
      intro x
      change binaryValue (wordAdd (x.1.bits,x.2.bits))=x.1+x.2
      rw [wordAdd_value,binaryValue_bits,binaryValue_bits])

end Procedure
end ExactQuantumFactoring.BitStackProgram

end


end OAI