import Mathlib import OAI.Computability.QuantumFactoring.BitStackProducts namespace OAI section namespace ExactQuantumFactoring.BitStackProgram namespace Procedure variable {α : Type} {ea : α→List Bool} {f : α→α} /-- Return the result to one fixed public register. Both word movements are implemented by two reversals; no output-to-input renaming is a TM operation. -/ noncomputable def inplace (p : Procedure ea ea f) : Procedure ea ea f where K:=p.K ⊕ Fin 2 finiteK:=inferInstance decideK:=inferInstance input:=Sum.inr 0 output:=Sum.inr 0 program:=.seq (moveWord (Sum.inr 0) (Sum.inl p.input) (Sum.inr 1)) (.seq (p.program.relabel Sum.inl) (moveWord (Sum.inl p.output) (Sum.inr 0) (Sum.inr 1))) bound:=Polynomial.C 8*Polynomial.X+Polynomial.C 5*p.bound+Polynomial.C 4 runs a:=by obtain ⟨cp,hcp,hp⟩:=p.runs a have h1:=moveWord_runs (Sum.inr 0 : p.K ⊕ Fin 2) (Sum.inl p.input) (Sum.inr 1) (by simp) (by simp) (by simp) (ea a) have h2:=hp.relabel (leftEmbed p.K (Fin 2)) (fun _=>[]) simp only [overlay_singleton] at h2 have h3:=moveWord_runs (Sum.inl p.output : p.K ⊕ Fin 2) (Sum.inr 0) (Sum.inr 1) (by simp) (by simp) (by simp) (ea (f a)) refine ⟨(4*(ea (f a)).length+2)+cp+(4*(ea a).length+2),?_, Runs.seq h1 (Runs.seq h2 h3)⟩ have hl:=p.output_length_le a simp only [Polynomial.eval_add,Polynomial.eval_mul,Polynomial.eval_C,Polynomial.eval_X] omega lemma inplace_register (p : Procedure ea ea f) : p.inplace.output=p.inplace.input := rfl /-- Literal repetition of an in-place subroutine by a unary counter. Each iteration consumes one control token; the bound charges every subroutine step. -/ lemma iterate_loop_runs {L : Type} [DecidableEq L] (p : Procedure ea ea f) (he : p.output=p.input) (k : L) (n : ℕ) (a : α) (B : ℕ) (hm : ∀ i≤n,(ea ((f^[i]) a)).length≤B) (s : Store L) (hs : s k=List.replicate n true) : ∃ c,c≤n*(p.bound.eval B+1)+1 ∧ Runs (.loop (Sum.inr k) (p.program.relabel Sum.inl) (p.program.relabel Sum.inl)) (sumStore (singletonStore p.input (ea a)) s) (sumStore (singletonStore p.input (ea ((f^[n]) a))) (Function.update s k [])) c := by induction n generalizing a s with | zero => have hk : Function.update s k []=s := by simp only [List.replicate_zero] at hs rw [←hs,Function.update_eq_self] refine ⟨1,by simp,?_⟩ rw [Function.iterate_zero_apply,hk] apply Runs.loop_nil exact hs | succ n ih => let t:=Function.update s k (List.replicate n true) have ht : t k=List.replicate n true:=by simp [t] have hm' : ∀ i≤n,(ea ((f^[i]) (f a))).length≤B := by intro i hi simpa only [Function.iterate_succ_apply] using hm (i+1) (by omega) obtain ⟨ct,hct,hrt⟩:=ih (f a) hm' t ht obtain ⟨cp,hcp,hrp⟩:=p.runs a rw [he] at hrp have hcall:=hrp.left t have hcpB : cp≤p.bound.eval B:= hcp.trans (eval_nat_mono p.bound (by simpa using hm 0 (by omega))) have hhead : sumStore (singletonStore p.input (ea a)) s (Sum.inr k)= true::List.replicate n true := by simpa only [sumStore_right,List.replicate_succ] using hs have hbody : Runs (p.program.relabel Sum.inl) (Function.update (sumStore (singletonStore p.input (ea a)) s) (Sum.inr k) (List.replicate n true)) (sumStore (singletonStore p.input (ea (f a))) t) cp := by simpa only [sumStore_update_right] using hcall have hh:=Runs.loop_true hhead hbody hrt refine ⟨ct+cp+1,?_,?_⟩ · nlinarith · simpa only [t,Function.update_idem,Function.iterate_succ_apply] using hh /-- Input-length-bounded iteration. The extra hypothesis is solely a size invariant along the already implemented iteration, not a computability oracle. -/ noncomputable def iterate (p : Procedure ea ea f) (q : Polynomial ℕ) (hgrowth : ∀ n a i,i≤n→(ea ((f^[i]) a)).length≤q.eval (n+(ea a).length)) : Procedure (prodCode (fun n=>List.replicate n true) ea) ea (fun x=>(f^[x.1]) x.2) where K:=p.inplace.K ⊕ Fin 4 finiteK:=inferInstance decideK:=inferInstance input:=Sum.inr 0 output:=Sum.inl p.inplace.input program:=.seq (unquoteMove (Sum.inr 0) (Sum.inr 1) (Sum.inr 2) (Sum.inr 3)) (.seq (moveWord (Sum.inr 0) (Sum.inl p.inplace.input) (Sum.inr 2)) (.loop (Sum.inr 1) (p.inplace.program.relabel Sum.inl) (p.inplace.program.relabel Sum.inl))) bound:=Polynomial.C 11*Polynomial.X+ Polynomial.X*(p.inplace.bound.comp q+1)+Polynomial.C 9 runs x:=by let r:=p.inplace let s : Store (r.K ⊕ Fin 4):=singletonStore (Sum.inr 0) (prodCode (fun n=>List.replicate n true) ea x) let u : Store (r.K ⊕ Fin 4):=Function.update (singletonStore (Sum.inr 0) (ea x.2)) (Sum.inr 1) (List.replicate x.1 true) let v : Store (r.K ⊕ Fin 4):=sumStore (singletonStore r.input (ea x.2)) (singletonStore 1 (List.replicate x.1 true)) have hparse : Runs (unquoteMove (Sum.inr 0) (Sum.inr 1) (Sum.inr 2) (Sum.inr 3)) s u (7*x.1+6) := by have hh:=unquoteMove_runs (Sum.inr 0 : r.K ⊕ Fin 4) (Sum.inr 1) (Sum.inr 2) (Sum.inr 3) (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) (List.replicate x.1 true) (ea x.2) s (by simp [s,singletonStore,prodCode,pairBits]) (by simp [s,singletonStore]) (by simp [s,singletonStore]) convert hh using 1 · funext k; cases k with | inl k => simp [s,u,singletonStore,Function.update] | inr k => fin_cases k <;> simp [s,u,singletonStore,Function.update] · simp have hmove : Runs (moveWord (Sum.inr 0) (Sum.inl r.input) (Sum.inr 2)) u v (4*(ea x.2).length+2) := by let emb : Fin 3 ↪ r.K ⊕ Fin 4 := ⟨fun k=>if k=0 then Sum.inr 0 else if k=1 then Sum.inl r.input else Sum.inr 2, by intro a b; fin_cases a <;> fin_cases b <;> simp⟩ have emb0 : emb 0=Sum.inr 0 := rfl have emb1 : emb 1=Sum.inl r.input := rfl have emb2 : emb 2=Sum.inr 2 := rfl have hh:=(moveWord_runs (0 : Fin 3) 1 2 (by decide) (by decide) (by decide) (ea x.2)).relabel emb (singletonStore (Sum.inr 1) (List.replicate x.1 true)) -- A framed move has the counter outside its three touched registers. have hs' : overlay emb (singletonStore (Sum.inr 1) (List.replicate x.1 true)) (singletonStore 0 (ea x.2))=u := by funext k cases k with | inl k => by_cases hk : k=r.input · subst k have h:=overlay_at emb (singletonStore (Sum.inr 1) (List.replicate x.1 true)) (singletonStore 0 (ea x.2)) 1 simpa [emb0,emb1,emb2,u,singletonStore] using h · rw [overlay_outside _ _ _ _ (by intro i; fin_cases i <;> simp [emb0,emb1,emb2,Ne.symm hk])] simp [u,singletonStore] | inr k => fin_cases k · simpa [emb0,emb1,emb2,u,singletonStore] using overlay_at emb (singletonStore (Sum.inr 1) (List.replicate x.1 true)) (singletonStore 0 (ea x.2)) 0 · rw [overlay_outside _ _ _ _ (by intro i; fin_cases i <;> simp [emb0,emb1,emb2])] simp [u,singletonStore] · simpa [emb0,emb1,emb2,u,singletonStore] using overlay_at emb (singletonStore (Sum.inr 1) (List.replicate x.1 true)) (singletonStore 0 (ea x.2)) 2 · rw [overlay_outside _ _ _ _ (by intro i; fin_cases i <;> simp [emb0,emb1,emb2])] simp [u,singletonStore] have ht' : overlay emb (singletonStore (Sum.inr 1) (List.replicate x.1 true)) (singletonStore 1 (ea x.2))=v := by funext k cases k with | inl k => by_cases hk : k=r.input · subst k simpa [emb0,emb1,emb2,v,singletonStore] using overlay_at emb (singletonStore (Sum.inr 1) (List.replicate x.1 true)) (singletonStore 1 (ea x.2)) 1 · rw [overlay_outside _ _ _ _ (by intro i; fin_cases i <;> simp [emb0,emb1,emb2,Ne.symm hk])] simp [v,singletonStore,hk] | inr k => fin_cases k · simpa [emb0,emb1,emb2,v,singletonStore] using overlay_at emb (singletonStore (Sum.inr 1) (List.replicate x.1 true)) (singletonStore 1 (ea x.2)) 0 · rw [overlay_outside _ _ _ _ (by intro i; fin_cases i <;> simp [emb0,emb1,emb2])] simp [v,singletonStore] · simpa [emb0,emb1,emb2,v,singletonStore] using overlay_at emb (singletonStore (Sum.inr 1) (List.replicate x.1 true)) (singletonStore 1 (ea x.2)) 2 · rw [overlay_outside _ _ _ _ (by intro i; fin_cases i <;> simp [emb0,emb1,emb2])] simp [v,singletonStore] rw [hs',ht'] at hh simpa [moveWord,reverseMove,Program.relabel,emb0,emb1,emb2] using hh obtain ⟨cl,hcl,hl⟩:=iterate_loop_runs r rfl (1 : Fin 4) x.1 x.2 (q.eval (x.1+(ea x.2).length)) (hgrowth x.1 x.2) (singletonStore 1 (List.replicate x.1 true)) (by simp [singletonStore]) have hfinal : sumStore (singletonStore r.input (ea ((f^[x.1]) x.2))) (Function.update (singletonStore (1 : Fin 4) (List.replicate x.1 true)) 1 [])= singletonStore (Sum.inl r.input) (ea ((f^[x.1]) x.2)) := by rw [sumStore_singleton_left] congr 1 funext k; fin_cases k <;> simp [singletonStore,Function.update] rw [hfinal] at hl refine ⟨cl+(4*(ea x.2).length+2)+(7*x.1+6),?_, Runs.seq hparse (Runs.seq hmove hl)⟩ let L:=(prodCode (fun n=>List.replicate n true) ea x).length have hx : x.1+(ea x.2).length≤L:=by simp only [L,prodCode,pairBits_length,List.length_replicate]; omega have hm:=eval_nat_mono r.bound (eval_nat_mono q hx) dsimp only at hm have hmul:=Nat.mul_le_mul (by omega : x.1≤L) (Nat.add_le_add_right hm 1) simp only [Polynomial.eval_add,Polynomial.eval_mul,Polynomial.eval_C,Polynomial.eval_X, Polynomial.eval_one,Polynomial.eval_comp] change cl+(4*(ea x.2).length+2)+(7*x.1+6)≤11*L+L*(r.bound.eval (q.eval L)+1)+9 omega end Procedure end ExactQuantumFactoring.BitStackProgram end end OAI