File size: 4,272 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
import Mathlib
import OAI.Computability.QuantumFactoring.EmissionCombinators

namespace OAI



section

namespace ExactQuantumFactoring.BitStackProgram.Procedure
variable {α β : Type} {ea : α→List Bool} {eb : β→List Bool} {f : α→β}
noncomputable def chooseBitD (c : α→Prop) [DecidablePred c]
    (p : Procedure (fun x:{a // ¬c a}=>ea x.val) eb (fun x=>f x.val))
    (q : Procedure (fun x:{a // c a}=>ea x.val) eb (fun x=>f x.val)) :
    Procedure (fun a=>decide (c a)::ea a) eb f where
  K:=(p.K ⊕ q.K) ⊕ Fin 2
  finiteK:=inferInstance
  decideK:=inferInstance
  input:=Sum.inr 0
  output:=Sum.inr 0
  program:=.cases (Sum.inr 0) .skip
    (p.shared.program.relabel (leftPublic p.K q.K))
    (q.shared.program.relabel (rightPublic p.K q.K))
  bound:=p.shared.bound+q.shared.bound+1
  runs a:=by
    have hpmono:=eval_nat_mono p.shared.bound (Nat.le_succ (ea a).length)
    have hqmono:=eval_nat_mono q.shared.bound (Nat.le_succ (ea a).length)
    by_cases h:c a
    · obtain ⟨t,ht,hd⟩:=q.shared.runs ⟨a,h⟩
      have hh:=hd.relabel (rightPublic p.K q.K) (fun _=>[])
      simp only [overlay_singleton] at hh
      have he : rightPublic p.K q.K q.shared.input=Sum.inr 0:=rfl
      rw [he] at hh
      refine ⟨t+1,?_,?_⟩
      · simpa only [List.length_cons,Polynomial.eval_add,Polynomial.eval_one] using
          Nat.add_le_add_right ((ht.trans hqmono).trans (Nat.le_add_left
            (q.shared.bound.eval ((ea a).length+1)) (p.shared.bound.eval ((ea a).length+1)))) 1
      · simp only [h,decide_true]
        have hu : Function.update (singletonStore (Sum.inr 0 : (p.K⊕q.K)⊕Fin 2)
            (true::ea a)) (Sum.inr 0) (ea a)=singletonStore (Sum.inr 0) (ea a):=by
          simp [singletonStore]
        simpa only [hu] using Runs.cases_true (p:=Program.skip)
          (q:=p.shared.program.relabel (leftPublic p.K q.K))
          (xs:=ea a) (s:=singletonStore (Sum.inr 0) (true::ea a)) (by simp [singletonStore])
          (by rw [hu];exact hh)
    · obtain ⟨t,ht,hd⟩:=p.shared.runs ⟨a,h⟩
      have hh:=hd.relabel (leftPublic p.K q.K) (fun _=>[])
      simp only [overlay_singleton] at hh
      have he : leftPublic p.K q.K p.shared.input=Sum.inr 0:=rfl
      rw [he] at hh
      refine ⟨t+1,?_,?_⟩
      · simpa only [List.length_cons,Polynomial.eval_add,Polynomial.eval_one] using
          Nat.add_le_add_right ((ht.trans hpmono).trans (Nat.le_add_right
            (p.shared.bound.eval ((ea a).length+1)) (q.shared.bound.eval ((ea a).length+1)))) 1
      · simp only [h,decide_false]
        have hu : Function.update (singletonStore (Sum.inr 0 : (p.K⊕q.K)⊕Fin 2)
            (false::ea a)) (Sum.inr 0) (ea a)=singletonStore (Sum.inr 0) (ea a):=by
          simp [singletonStore]
        simpa only [hu] using Runs.cases_false (p:=Program.skip)
          (r:=q.shared.program.relabel (rightPublic p.K q.K))
          (xs:=ea a) (s:=singletonStore (Sum.inr 0) (false::ea a)) (by simp [singletonStore])
          (by rw [hu];exact hh)
noncomputable def splitOn (c : α→Prop) [DecidablePred c]
    (test : Procedure ea boolCode (fun a=>decide (c a)))
    (yes : Procedure (fun x:{a // c a}=>ea x.val) eb (fun x=>f x.val))
    (no : Procedure (fun x:{a // ¬c a}=>ea x.val) eb (fun x=>f x.val)) : Procedure ea eb f:=by
  let p:=no.comp (unprefix (fun x:{a // ¬c a}=>ea x.val) false)
  let q:=yes.comp (unprefix (fun x:{a // c a}=>ea x.val) false)
  let choice:=(chooseBitD c p q).comp (unprefix (fun a=>decide (c a)::false::ea a) true)
  let input:= (test.pair (identity ea)).result (ec:=fun a=>true::decide (c a)::false::ea a)
    (h:=id) (by intro a;rfl)
  exact choice.comp input
end ExactQuantumFactoring.BitStackProgram.Procedure

namespace ExactQuantumFactoring.BitStackProgram.Emits
variable {α β : Type} {ea : α→List Bool} {eb : β→List Bool} {f : α→β}
lemma splitOn (c : α→Prop) [DecidablePred c]
    (test : Emits ea Procedure.boolCode (fun a=>decide (c a)))
    (yes : Emits (fun x:{a // c a}=>ea x.val) eb (fun x=>f x.val))
    (no : Emits (fun x:{a // ¬c a}=>ea x.val) eb (fun x=>f x.val)) : Emits ea eb f:=by
  obtain ⟨p⟩:=test;obtain ⟨q⟩:=yes;obtain ⟨r⟩:=no
  exact ⟨Procedure.splitOn c p q r⟩
end ExactQuantumFactoring.BitStackProgram.Emits

end



end OAI