File size: 7,294 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | import Mathlib
import OAI.Computability.QuantumFactoring.BooleanVector
namespace OAI
section
open scoped BigOperators
namespace ExactQuantumFactoring
open BooleanNetwork Std.Sat
namespace AIGCompiler
variable {n : ℕ} (g : AIG (Fin n))
def graphWire (i : Fin g.decls.size) : Fin (n + g.decls.size) := Fin.natAdd n i
def faninNet (f : AIG.Fanin) : BooleanNetwork (n + g.decls.size) 1 :=
let v := if h : f.gate < g.decls.size then bit (graphWire g ⟨f.gate,h⟩) else constant false
if f.invert then v.bnot else v
lemma fanin_count (f : AIG.Fanin) : (faninNet g f).net.count ≤ 2 := by
have hv : (if h : f.gate < g.decls.size then bit (graphWire g ⟨f.gate,h⟩)
else constant false : BooleanNetwork (n+g.decls.size) 1).net.count ≤ 1 := by
by_cases h : f.gate < g.decls.size
· rw [dite_eq_left h]; simp
· rw [dite_eq_right h]; simp
unfold faninNet
generalize (if h : f.gate < g.decls.size then bit (graphWire g ⟨f.gate,h⟩)
else constant false : BooleanNetwork (n+g.decls.size) 1) = v at *
cases hf : f.invert
· change v.net.count ≤ 2
omega
· change v.bnot.net.count ≤ 2
rw [count_bnot]
omega
lemma fanin_eval (f : AIG.Fanin) (h : f.gate < g.decls.size) (x : Fin (n+g.decls.size) → Bool) :
(faninNet g f).eval x 0 = Bool.xor (x (graphWire g ⟨f.gate,h⟩)) f.invert := by
simp only [faninNet, dite_eq_left h]
cases f.invert <;> simp
def nodeNet (i : Fin g.decls.size) : BooleanNetwork (n+g.decls.size) 1 :=
match g.decls[i.val] with
| .false => constant false
| .atom v => bit (Fin.castAdd g.decls.size v)
| .gate a b => (faninNet g a).band (faninNet g b)
lemma node_count (i : Fin g.decls.size) : (nodeNet g i).net.count ≤ 5 := by
unfold nodeNet
cases g.decls[i.val] with
| false => simp
| atom v => simp
| gate a b =>
rw [count_band]
have := fanin_count g a
have := fanin_count g b
omega
def nodeDenote (x : Fin n → Bool) (i : Fin g.decls.size) : Bool :=
AIG.denote x ⟨g, ⟨i.val, false, i.isLt⟩⟩
lemma node_eval (i : Fin g.decls.size) (x : Fin n → Bool)
(y : Fin (n+g.decls.size) → Bool)
(hi : ∀ j, y (j.castAdd g.decls.size) = x j)
(hg : ∀ j : Fin g.decls.size, j.val < i.val → y (graphWire g j) = nodeDenote g x j) :
(nodeNet g i).eval y 0 = nodeDenote g x i := by
unfold nodeNet nodeDenote
cases h : g.decls[i.val] with
| false => simpa only [eval_constant] using (AIG.denote_idx_false (assign := x) (invert := false) h).symm
| atom v => simpa only [eval_bit, hi, Bool.xor_false] using (AIG.denote_idx_atom (assign := x) (invert := false) h).symm
| gate a b =>
have hab := g.hdag i.isLt h
dsimp only
rw [eval_band, fanin_eval g a (by omega), fanin_eval g b (by omega),
hg ⟨a.gate, by omega⟩ hab.1, hg ⟨b.gate, by omega⟩ hab.2]
have he := (AIG.denote_idx_gate (assign := x) (invert := false) h).symm
simpa only [AIG.denote, nodeDenote, Bool.xor_false] using he
def nodeStep (i : Fin g.decls.size) : BooleanNetwork (n+g.decls.size) (n+g.decls.size) :=
assign (graphWire g i) (nodeNet g i)
def partialGraph (g : AIG (Fin n)) : (k : ℕ) → k ≤ g.decls.size →
BooleanNetwork (n+g.decls.size) (n+g.decls.size)
| 0, _ => select id
| k+1, h => (partialGraph g k (by omega)).comp (nodeStep g ⟨k, by omega⟩)
lemma graph_count (k : ℕ) (h : k ≤ g.decls.size) : (partialGraph g k h).net.count ≤ 5*k := by
induction k with
| zero => simp [partialGraph]
| succ k ih =>
rw [partialGraph, count_comp, nodeStep, count_assign]
have := node_count g ⟨k, by omega⟩
have := ih (by omega)
omega
lemma graph_input (k : ℕ) (h : k ≤ g.decls.size) (x : Fin (n+g.decls.size) → Bool) (j : Fin n) :
(partialGraph g k h).eval x (j.castAdd g.decls.size) = x (j.castAdd g.decls.size) := by
induction k with
| zero => rfl
| succ k ih =>
rw [partialGraph, eval_comp, nodeStep, eval_assign, Function.update_of_ne, ih]
intro he
have := congrArg Fin.val he
simp [graphWire] at this
omega
lemma graph_value (k : ℕ) (h : k ≤ g.decls.size) (x : Fin (n+g.decls.size) → Bool)
(j : Fin g.decls.size) (hj : j.val < k) :
(partialGraph g k h).eval x (graphWire g j) =
nodeDenote g (fun i => x (i.castAdd g.decls.size)) j := by
induction k generalizing j with
| zero => omega
| succ k ih =>
rw [partialGraph, eval_comp, nodeStep, eval_assign]
by_cases hjk : j.val = k
· have hj' : j = ⟨k, by omega⟩ := Fin.ext hjk
rw [hj', Function.update_self]
apply node_eval g
· exact fun i => graph_input g k (by omega) x i
· exact fun i hi => ih (by omega) i (by simpa using hi)
· rw [Function.update_of_ne]
· exact ih (by omega) j (by omega)
· intro he
have := congrArg Fin.val he
simp [graphWire] at this
omega
/-- The circuit interpreter's own, already verified, AIG semantics is preserved
by a retained-trace network of at most five nodes per AIG declaration. -/
theorem graph_correct (x : Fin (n+g.decls.size) → Bool) (j : Fin g.decls.size) :
(partialGraph g g.decls.size le_rfl).eval x (graphWire g j) =
nodeDenote g (fun i => x (i.castAdd g.decls.size)) j :=
graph_value g _ le_rfl x j j.isLt
/-- Compile designated references of an AIG, starting only with its input bits.
All graph slots and their retained evaluation traces are genuine ancillas. -/
def compile {m : ℕ} (refs : Fin m → AIG.Ref g) : BooleanNetwork n m :=
((padRight n g.decls.size).comp (partialGraph g g.decls.size le_rfl)).comp
(vector (fun i => faninNet g (AIG.Fanin.mk (refs i).gate (refs i).invert)))
lemma compile_count {m : ℕ} (refs : Fin m → AIG.Ref g) :
(compile g refs).net.count ≤ 6*g.decls.size + 2*m := by
rw [compile, count_comp, count_comp, count_padRight]
have hg := graph_count g g.decls.size le_rfl
have hv := count_vector_le (fun i => faninNet g (AIG.Fanin.mk (refs i).gate (refs i).invert))
(fun i => fanin_count g (AIG.Fanin.mk (refs i).gate (refs i).invert))
omega
lemma compile_correct {m : ℕ} (refs : Fin m → AIG.Ref g) (x : Fin n → Bool) (i : Fin m) :
(compile g refs).eval x i = AIG.denote x ⟨g, refs i⟩ := by
rw [compile, eval_comp, eval_comp, eval_vector, fanin_eval g _ (by simpa using (refs i).hgate),
graph_correct, eval_padRight]
have he : (fun j : Fin n => Fin.append x (fun _ : Fin g.decls.size => false)
(j.castAdd g.decls.size)) = x := by funext j; exact Fin.append_left ..
rw [he]
simp only [nodeDenote, AIG.denote, Bool.xor_false, AIG.Fanin.gate_mk, AIG.Fanin.invert_mk]
/-- The whole output BitVec of a native, already verified, arithmetic bitblaster
is now a Boolean network with a linear overhead in its AIG graph size. -/
def compileVec {w : ℕ} (refs : AIG.RefVec g w) : BooleanNetwork n w :=
compile g (fun i => refs.get i.val i.isLt)
lemma compileVec_correct {w : ℕ} (refs : AIG.RefVec g w) (x : Fin n → Bool) (i : Fin w) :
(compileVec g refs).eval x i = AIG.denote x ⟨g, refs.get i.val i.isLt⟩ :=
compile_correct ..
lemma compileVec_count {w : ℕ} (refs : AIG.RefVec g w) :
(compileVec g refs).net.count ≤ 6*g.decls.size + 2*w := compile_count ..
end AIGCompiler
end ExactQuantumFactoring
end
end OAI
|