File size: 2,061 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
import Mathlib
import OAI.Computability.QuantumFactoring.BitStackDivision

namespace OAI



section

namespace ExactQuantumFactoring.BitStackProgram

def unaryDecodeStep (s : ℕ×ℕ) : ℕ×ℕ := if s.1=0 then s else (s.1-1,s.2+1)
lemma unaryDecodeStep_iterate (k : ℕ) (s : ℕ×ℕ) :
    (unaryDecodeStep^[k]) s=(s.1-k,s.2+min k s.1) := by
  induction k generalizing s with
  | zero=>simp
  | succ k ih=>
    rw [Function.iterate_succ_apply,ih]
    unfold unaryDecodeStep
    split
    · rename_i hz
      apply Prod.ext <;> dsimp only <;> omega
    · rename_i hz
      apply Prod.ext <;> dsimp only <;> omega
namespace Procedure
noncomputable def unaryDecodeStepP : Procedure (prodCode Nat.bits unaryCode) (prodCode Nat.bits unaryCode)
    unaryDecodeStep := by
  let x:=first Nat.bits unaryCode
  let y:=second Nat.bits unaryCode
  let next:=(binarySub.comp (x.pair (Procedure.constant _ Nat.bits 1))).pair (unarySuccessor.comp y)
  exact (conditional (binaryZero.comp x) (identity _) next).congrFun (by
    intro s;unfold unaryDecodeStep;split <;> simp_all)
noncomputable def clippedUnary : Procedure (prodCode unaryCode Nat.bits) unaryCode (fun x=>min x.1 x.2) := by
  let rep:=unaryDecodeStepP.iterate Polynomial.X (by
    intro n s i hi
    rw [unaryDecodeStep_iterate]
    have hbits:=bits_length_mono (Nat.sub_le s.1 i)
    simp only [prodCode,pairBits_length,unaryCode,List.length_replicate,Polynomial.eval_X]
    omega)
  let inp:=(first unaryCode Nat.bits).pair
    ((second unaryCode Nat.bits).pair (Procedure.constant _ unaryCode 0))
  exact ((second Nat.bits unaryCode).comp (rep.comp inp)).congrFun (by
    intro x;dsimp only [Function.comp_def];rw [unaryDecodeStep_iterate];simp)
noncomputable def boundedUnary {α : Type} {ea : α→List Bool} {f B : α→ℕ}
    (p : Procedure ea Nat.bits f) (b : Procedure ea unaryCode B) (h : ∀a,f a ≤ B a) :
    Procedure ea unaryCode f :=
  (clippedUnary.comp (b.pair p)).congrFun (by intro a;exact min_eq_right (h a))
end Procedure
end ExactQuantumFactoring.BitStackProgram

end



end OAI