File size: 2,977 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
import Mathlib
import OAI.Computability.QuantumFactoring.AIGCompiler
import OAI.Computability.QuantumFactoring.AIGBounds
import OAI.Computability.QuantumFactoring.AIGInputs
import OAI.Computability.QuantumFactoring.BitEncoding

namespace OAI

section
open scoped BigOperators


namespace ExactQuantumFactoring
namespace BitArithmetic
open Std.Sat Std.Tactic.BVDecide.BVExpr.bitblast
open AIGCompiler BooleanNetwork

/-- The two actual little-endian input words of the bitblaster. -/
def binaryInputs (w : ℕ) : AIG.BinaryRefVec (inputs (w+w)).1 w :=
  ⟨refVector (fun i => (inputs (w+w)).2 (i.castAdd w)),
   refVector (fun i => (inputs (w+w)).2 (Fin.natAdd w i))⟩

def leftWord {w : ℕ} (x : Fin (w+w) → Bool) : BitVec w :=
  bitsValue (fun i => x (i.castAdd w))
def rightWord {w : ℕ} (x : Fin (w+w) → Bool) : BitVec w :=
  bitsValue (fun i => x (Fin.natAdd w i))

lemma binaryInputs_left (w : ℕ) (x : Fin (w+w) → Bool) (i : ℕ) (hi : i < w) :
    AIG.denote x ⟨(inputs (w+w)).1, (binaryInputs w).lhs.get i hi⟩ =
      (leftWord x).getLsbD i := by
  rw [binaryInputs, refVector_get, inputs_correct]
  exact (bitsValue_bit (fun j => x (j.castAdd w)) ⟨i,hi⟩).symm

lemma binaryInputs_right (w : ℕ) (x : Fin (w+w) → Bool) (i : ℕ) (hi : i < w) :
    AIG.denote x ⟨(inputs (w+w)).1, (binaryInputs w).rhs.get i hi⟩ =
      (rightWord x).getLsbD i := by
  rw [binaryInputs, refVector_get, inputs_correct]
  exact (bitsValue_bit (fun j => x (Fin.natAdd w j)) ⟨i,hi⟩).symm

def add (w : ℕ) : BooleanNetwork (w+w) w :=
  let res := blastAdd (inputs (w+w)).1 (binaryInputs w)
  compileVec res.aig res.vec

lemma add_correct (w : ℕ) (x : Fin (w+w) → Bool) (i : Fin w) :
    (add w).eval x i = (leftWord x + rightWord x).getLsbD i.val := by
  rw [add, compileVec_correct]
  exact denote_blastAdd _ _ _ _ _ (binaryInputs_left w x) (binaryInputs_right w x) _ _

def mul (w : ℕ) : BooleanNetwork (w+w) w :=
  let res := blastMul (inputs (w+w)).1 (binaryInputs w)
  compileVec res.aig res.vec

lemma mul_correct (w : ℕ) (x : Fin (w+w) → Bool) (i : Fin w) :
    (mul w).eval x i = (leftWord x * rightWord x).getLsbD i.val := by
  rw [mul, compileVec_correct]
  exact denote_blastMul _ _ _ _ _ (binaryInputs_left w x) (binaryInputs_right w x) _ _

lemma add_count (w : ℕ) : (add w).net.count ≤ 86*w+6 := by
  have h := compileVec_count (blastAdd (inputs (w+w)).1 (binaryInputs w)).aig
    (blastAdd (inputs (w+w)).1 (binaryInputs w)).vec
  have hs := AIGBounds.add_size (inputs (w+w)).1 (binaryInputs w)
  rw [inputs_size] at hs
  dsimp only [add]
  omega

lemma mul_count (w : ℕ) : (mul w).net.count ≤ 90*w*w+14*w+6 := by
  have h := compileVec_count (blastMul (inputs (w+w)).1 (binaryInputs w)).aig
    (blastMul (inputs (w+w)).1 (binaryInputs w)).vec
  have hs := AIGBounds.mul_size (inputs (w+w)).1 (binaryInputs w)
  rw [inputs_size] at hs
  dsimp only [mul]
  nlinarith

end BitArithmetic
end ExactQuantumFactoring


end

end OAI