introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
5.97 kB
import Mathlib
namespace OAI
namespace ExactQuantumFactoring
open scoped BigOperators
/-- The five primitive gates exactly as in Section 2. -/
inductive Primitive where
| not | cnot | toffoli | hadamard | phase
deriving DecidableEq
def Primitive.arity : Primitive β†’ β„•
| .not | .hadamard | .phase => 1
| .cnot => 2
| .toffoli => 3
def Primitive.code : Primitive β†’ β„•
| .not => 0 | .cnot => 1 | .toffoli => 2 | .hadamard => 3 | .phase => 4
/-- An inverse and/or one added control. This is a fixed set of 20 gate names,
not an unbounded-control or arbitrary-angle gate model. -/
structure Gate where
primitive : Primitive
inverse : Bool
controlled : Bool
deriving DecidableEq
def Gate.arity (g : Gate) : β„• := g.primitive.arity + if g.controlled then 1 else 0
/-- Matrix entries are indexed by lists in wire order (row first, column second).
Entries on ill-sized lists are irrelevant and set to zero. -/
noncomputable def Primitive.matrix : Primitive β†’ List Bool β†’ List Bool β†’ β„‚
| .not, [a], [b] => if a = !b then 1 else 0
| .cnot, [a,b], [c,d] => if a = c ∧ b = Bool.xor c d then 1 else 0
| .toffoli, [a,b,c], [d,e,f] =>
if a = d ∧ b = e ∧ c = Bool.xor f (d && e) then 1 else 0
| .hadamard, [a], [b] =>
(if a && b then -1 else 1) / (Real.sqrt 2 : β„‚)
| .phase, [a], [b] => if a = b then (if b then Complex.I else 1) else 0
| _, _, _ => 0
noncomputable def Gate.baseMatrix (g : Gate) (a b : List Bool) : β„‚ :=
if g.inverse then star (g.primitive.matrix b a) else g.primitive.matrix a b
noncomputable def Gate.matrix (g : Gate) (a b : List Bool) : β„‚ :=
if g.controlled then
match a, b with
| false :: a', false :: b' => if a' = b' then 1 else 0
| true :: a', true :: b' => g.baseMatrix a' b'
| _, _ => 0
else g.baseMatrix a b
/-- A primitive is placed on distinct actual wires, with no operation of
unbounded arity hidden in a circuit instruction. -/
structure Instruction (q : β„•) where
gate : Gate
wire : Fin gate.arity β†’ Fin q
distinct : Function.Injective wire
abbrev Basis (q : β„•) := Fin q β†’ Bool
abbrev State (q : β„•) := Basis q β†’ β„‚
noncomputable def Instruction.matrix {q : β„•} (o : Instruction q) :
Matrix (Basis q) (Basis q) β„‚ := by
classical
exact fun a b =>
if βˆ€ i : Fin q, (βˆ€ j, o.wire j β‰  i) β†’ a i = b i then
o.gate.matrix (List.ofFn (a ∘ o.wire)) (List.ofFn (b ∘ o.wire))
else 0
structure Circuit where
qubits : β„•
instructions : List (Instruction qubits)
noncomputable def Circuit.apply (c : Circuit) (ψ : State c.qubits) : State c.qubits :=
c.instructions.foldl (fun v o => o.matrix.mulVec v) ψ
/-- Input wires are first, least significant bit first; all other wires start zero. -/
def inputBasis (q β„“ N : β„•) : Basis q :=
fun i => if i.val < β„“ then N.testBit i.val else false
noncomputable def Circuit.outputState (c : Circuit) (β„“ N : β„•) : State c.qubits := by
classical
exact c.apply (fun x => if x = inputBasis c.qubits β„“ N then 1 else 0)
/-- The cutoff and padding are the standing parameters in Section 2. -/
def paddedLength (β„“ : β„•) : β„• := max 128 β„“
def readBit {q : β„•} (x : Basis q) (i : β„•) : Bool :=
if h : i < q then x ⟨i,h⟩ else false
/-- n entries of n bits each, in the nΒ² wires immediately after the input. -/
def outputList {q : β„•} (β„“ : β„•) (x : Basis q) : List β„• :=
let n := paddedLength β„“
List.ofFn (fun a : Fin n =>
βˆ‘ j ∈ Finset.range n, if readBit x (β„“ + a.val * n + j) then 2^j else 0)
/-- Complete nondecreasing prime factorization, multiplicities included,
padded by zeros, as in Section 6. There is no primality oracle in the circuit. -/
def CorrectEncoding (N n : β„•) (xs : List β„•) : Prop :=
βˆƒ ps : List β„•, (βˆ€ p ∈ ps, Nat.Prime p) ∧ ps.Pairwise (Β· ≀ Β·) ∧
ps.prod = N ∧ ps.length ≀ n ∧ xs = ps ++ List.replicate (n - ps.length) 0
/-- Unnormalized Born sum on correct outputs; success is exactly one,
not positivity, postselection, or a limiting high probability. -/
noncomputable def Circuit.correctProbability (c : Circuit) (β„“ N : β„•) : ℝ := by
classical
exact βˆ‘ x : Basis c.qubits,
if CorrectEncoding N (paddedLength β„“) (outputList β„“ x) then
Complex.normSq (c.outputState β„“ N x) else 0
/-- Unary natural words with delimiters make the circuit description explicit.
Unary wire addresses cause at most polynomial overhead when width is polynomial. -/
def natWord (k : β„•) : List Bool := List.replicate k true ++ [false]
def Instruction.encode {q : β„•} (o : Instruction q) : List Bool :=
natWord o.gate.primitive.code ++ [o.gate.inverse, o.gate.controlled] ++
(List.ofFn (fun i => o.wire i |>.val)).flatMap natWord
def Circuit.encode (c : Circuit) : List Bool :=
natWord c.qubits ++ natWord c.instructions.length ++
c.instructions.flatMap Instruction.encode
/-- A single finite-alphabet classical generator receives only the INPUT LENGTH.
All stack alphabets, states and labels are finite: no large-integer unit-cost
oracle is hidden in TM2 push/branch functions. -/
def Uniform (family : β„• β†’ Circuit) : Prop :=
βˆƒ generator : Turing.TM2ComputableInPolyTime
(fun β„“ => List.replicate β„“ true) Circuit.encode family,
βˆ€ k, Finite (generator.tm.Ξ“ k)
def PolynomialResources (family : β„• β†’ Circuit) : Prop :=
βˆƒ bound : Polynomial β„•, βˆ€ β„“,
(family β„“).qubits ≀ bound.eval β„“ ∧
(family β„“).instructions.length ≀ bound.eval β„“
/-- Literal main endpoint: one length-uniform family, exact output for EVERY
binary integer Nβ‰₯2, and worst-case polynomial gates AND qubits. -/
def MainTheorem : Prop :=
βˆƒ family : β„• β†’ Circuit, Uniform family ∧ PolynomialResources family ∧
βˆ€ (β„“ N : β„•), 2 ≀ N β†’ N.size = β„“ β†’
β„“ + (paddedLength β„“)^2 ≀ (family β„“).qubits ∧
(family β„“).correctProbability β„“ N = 1
end ExactQuantumFactoring
end OAI