AFTD Auto-Formalizing Theoretical Domains

recursive_nae_two_eight_clean_phase_queries

theoremverifiedformalization

There exists an eight-query clean phase circuit for recursive ternary not-all-equal at depth two.

Statement

theorem recursive_nae_two_eight_clean_phase_queries : phase_query_computes_cleanly (recursive_nae 2) 8
Formalizes
arXiv:1211.0721 Section 3.4, Construction 1: an eight-query (-1)-computation of the depth-two function, expressed in the present phase-query circuit model.
Verified
18 Nov 2026
Axioms
Classical.choiceQuot.soundpropext
Built on 2
  • phase_query_computes_cleanlyThere is a normalized finite-dimensional q-query unitary phase-query circuit that returns its initial state with phase (-1) to the power…
  • recursive_naeThe k-th recursive block composition F_k = NAE₃^{∘ k} of the ternary not-all-equal Boolean function on 3^k bits.

Read back from the Lean

The 2-level recursive ternary Not-All-Equal function on 9 bits can be cleanly computed by an 8-query quantum phase algorithm. That is, there exist a finite basis type $B$ with decidable equality, an assignment $reg : B \to \text{Option}(\text{Fin } 9)$ of basis states to input indices (or none), a normalized initial state $\psi_0 \in \mathbb{C}^B$, an initial unitary matrix $U_0$ on $B$, and a sequence of 8 unitary matrices $U_1, \dots, U_8$ on $B$, such that for every input $x \in \{\text{false}, \text{true}\}^9$, starting from $U_0 \psi_0$ and alternately applying the phase query oracle (which negates the amplitude of basis state $b$ if $reg(b) = \text{some } i$ and $x(i) = \text{true}$) and the unitary matrices $U_t$ results in the exact state $(-1)^{f(x)} \psi_0$ without garbage, where $f = \text{recursive\_nae } 2$ is the ternary Not-All-Equal function applied to the results of ternary Not-All-Equal on three length-3 blocks of $x$.

Written by a model that saw only the Lean, never the English above. If the two disagree, that is worth a challenge.

The proof, explained

The theorem establishes that depth-two recursive ternary Not-All-Equal on nine input bits has an eight-query clean phase circuit in a thirteen-dimensional state space. The quantum algorithm is designed over the integers, with four auxiliary basis states and unitary operators scaled from integer matrices. The proof establishes the equivalence between the matrix circuit and an arithmetic update function, verifies correctness over all 512 Boolean inputs via kernel computation, and transfers the integer dynamics to complex unitary evolution via list induction.

  1. The matrix-vector multiplications for the integer operators Dz, T2z, Rz, and Iz agree with their explicit componentwise formulas.

    For each 13×13 integer matrix, equality is shown by extensionality on vectors. The 13 basis coordinates are examined by finite case analysis (fin_cases i). In each case, the dot product sum is unfolded across Fin 13 using Fin.sum_univ_succ, the matrix entries are evaluated, and ring arithmetic confirms that the sum matches the fast closed-form expression.

    lines 50–68

    uses Fin.sum_univ_succ

    theorem rnae8_Dz_fast (w : Fin 13 → ℤ) : rnae8_Dz.mulVec w = rnae8_fastD w := by
      funext i
      fin_cases i <;>
        simp [Matrix.mulVec, dotProduct, Fin.sum_univ_succ, rnae8_Dz, rnae8_kd, rnae8_blk, rnae8_fastD, rnae8_bsum] <;> ring
    
    theorem rnae8_T2z_fast (w : Fin 13 → ℤ) : rnae8_T2z.mulVec w = rnae8_fastT w := by
      funext i
      fin_cases i <;>
        simp [Matrix.mulVec, dotProduct, Fin.sum_univ_succ, rnae8_T2z, rnae8_kd, rnae8_blk, rnae8_fastT, rnae8_bsum, rnae8_tot] <;> ring
    
    theorem rnae8_Rz_fast (w : Fin 13 → ℤ) : rnae8_Rz.mulVec w = rnae8_fastR w := by
      funext i
      fin_cases i <;>
        simp [Matrix.mulVec, dotProduct, Fin.sum_univ_succ, rnae8_Rz, rnae8_kd, rnae8_nz, rnae8_fastR, rnae8_dotn, rnae8_bsum, rnae8_tot] <;> ring
    
    theorem rnae8_Iz_fast (w : Fin 13 → ℤ) : rnae8_Iz.mulVec w = w := by
      funext i
      fin_cases i <;> simp [Matrix.mulVec, dotProduct, Fin.sum_univ_succ, rnae8_Iz, rnae8_kd]
    
  2. The sequential matrix simulation of the eight-step circuit equals the fast arithmetic function composition rnae8_evalF.

    The definition rnae8_evalZ folds the eight circuit steps starting from rnae8_nz. Unrolling the eight-element list (List.ofFn_succ, List.ofFn_zero, List.foldl_cons, List.foldl_nil) and substituting the fast matrix multiplication lemmas (rnae8_Dz_fast, rnae8_T2z_fast, rnae8_Rz_fast, rnae8_Iz_fast) reduces the matrix fold to the direct composition of arithmetic updates in rnae8_evalF.

    lines 69–83

    uses List.ofFn_succ, List.ofFn_zero, rnae8_Dz_fast, rnae8_T2z_fast, rnae8_Rz_fast, rnae8_Iz_fast

    def rnae8_evalF (x : Fin (3 ^ 2) → Bool) : Fin 13 → ℤ :=
      let q := fun (w : Fin 13 → ℤ) (b : Fin 13) => rnae8_phz x b * w b
      let v1 := rnae8_fastD (q rnae8_nz)
      let v2 := rnae8_fastT (q v1)
      let v3 := rnae8_fastD (q v2)
      let v4 := rnae8_fastR (q v3)
      let v5 := rnae8_fastD (q v4)
      let v6 := rnae8_fastT (q v5)
      let v7 := rnae8_fastD (q v6)
      q v7
    
    theorem rnae8_evalZ_eq (x : Fin (3 ^ 2) → Bool) : rnae8_evalZ x = rnae8_evalF x := by
      simp only [rnae8_evalZ, rnae8_evalF, List.ofFn_succ, List.ofFn_zero, List.foldl_cons, List.foldl_nil, rnae8_stepZ, rnae8_stepsZ]
      simp [rnae8_Dz_fast, rnae8_T2z_fast, rnae8_Rz_fast, rnae8_Iz_fast]
    
  3. For all 512 nine-bit Boolean inputs, the fast circuit evaluation rnae8_evalF correctly computes the depth-two recursive NAE value scaled by 3359232 · rnae8_nz. automation

    Because the input consists of 9 Boolean values and the target is finite, kernel reduction evaluates the circuit function on all 2⁹ = 512 possible assignments and all 13 basis indices, verifying the exact identity for every case.

    lines 84–90

    uses rnae8_allbF

    set_option maxRecDepth 100000 in
    theorem rnae8_allbF : ∀ b0 b1 b2 b3 b4 b5 b6 b7 b8 : Bool, ∀ b,
        rnae8_evalF (![b0, b1, b2, b3, b4, b5, b6, b7, b8] : Fin (3 ^ 2) → Bool) b
        = (if recursive_nae 2 (![b0, b1, b2, b3, b4, b5, b6, b7, b8] : Fin (3 ^ 2) → Bool) then -1 else 1)
          * 3359232 * rnae8_nz b := by
      decide +kernel
    
  4. For every Boolean input function on nine bits, the circuit fold rnae8_evalZ computes the scaled depth-two recursive NAE value.

    Any function x : Fin 9 → Bool is extensional to the explicit vector ![x 0, ..., x 8]. Rewriting with this vector representation and replacing rnae8_evalZ by rnae8_evalF via rnae8_evalZ_eq allows applying the exhaustive verification lemma rnae8_allbF.

    lines 91–99

    uses rnae8_evalZ_eq, rnae8_allbF

    theorem rnae8_allx : ∀ x : Fin (3 ^ 2) → Bool, ∀ b, rnae8_evalZ x b
        = (if recursive_nae 2 x then -1 else 1) * 3359232 * rnae8_nz b := by
      intro x
      have hx : x = (![x 0, x 1, x 2, x 3, x 4, x 5, x 6, x 7, x 8] : Fin (3 ^ 2) → Bool) := by
        funext i; fin_cases i <;> rfl
      rw [hx]
      rw [rnae8_evalZ_eq]
      exact rnae8_allbF _ _ _ _ _ _ _ _ _
    
  5. Each integer operator satisfies Mᵀ M = c² I, the initial vector has squared norm 1024, and the product of the step factors is 3359232. automation

    The scaled orthogonality Mᵀ M = c² I for each of the eight steps and the vector squared norm ∑ nz(b)² = 1024 are verified by kernel evaluation. The product of the step scaling factors 3 · 9 · 3 · 512 · 3 · 9 · 3 · 1 is computed by list expansion and arithmetic.

    lines 100–109

    uses List.ofFn_succ

    set_option maxRecDepth 100000 in
    theorem rnae8_unitZ : ∀ t, Matrix.transpose (rnae8_stepsZ t).2 * (rnae8_stepsZ t).2 = ((rnae8_stepsZ t).1 ^ 2 : ℤ) • (1 : Matrix (Fin 13) (Fin 13) ℤ) := by
      decide +kernel
    
    theorem rnae8_normZ : ∑ b, rnae8_nz b * rnae8_nz b = 1024 := by decide +kernel
    
    theorem rnae8_prodZ : ((List.ofFn rnae8_stepsZ).map (fun p => (p.1 : ℂ))).prod = 3359232 := by
      simp [List.ofFn_succ, rnae8_stepsZ]
      norm_num
    
  6. Folding the complex unitaries over phase queries equals the integer simulation divided by the product of scaling factors.

    First, rnae8_step_cast proves that a single step scales linearly when casting from ℤ to ℂ, moving scalar factors and casts through the sum using Int.cast_sum, Int.cast_mul, and Finset.mul_sum. Then rnae8_foldl_cast proves the list fold identity by induction on the list of circuit steps (List.map_cons), combining each step via rnae8_step_cast and rewriting division through scalar inverses.

    lines 110–133

    uses Int.castRingHom, Int.cast_sum, Int.cast_mul, Finset.mul_sum, Finset.sum_congr, List.map_cons, rnae8_step_cast, rnae8_foldl_cast, div_mul_eq_div_div, div_eq_mul_inv

    noncomputable def rnae8_toC (p : ℕ × Matrix (Fin 13) (Fin 13) ℤ) : Matrix (Fin 13) (Fin 13) ℂ :=
      ((p.1 : ℂ)⁻¹) • p.2.map (Int.castRingHom ℂ)
    
    theorem rnae8_step_cast (ph : Fin 13 → ℤ) {phC : Fin 13 → ℂ} (hC : ∀ b, phC b = ((ph b : ℤ) : ℂ))
        (p : ℕ × Matrix (Fin 13) (Fin 13) ℤ) (c : ℂ) (v : Fin 13 → ℤ) :
        (rnae8_toC p).mulVec (fun b => phC b * (c * ((v b : ℤ) : ℂ)))
          = fun b => (c * (p.1 : ℂ)⁻¹) * ((rnae8_stepZ ph v p b : ℤ) : ℂ) := by
      funext i
      simp only [rnae8_toC, rnae8_stepZ, Matrix.mulVec, dotProduct, Matrix.smul_apply, Matrix.map_apply,
        Int.coe_castRingHom, smul_eq_mul, Int.cast_sum, Int.cast_mul, Finset.mul_sum, hC]
      refine Finset.sum_congr rfl fun j _ => ?_
      ring
    
    theorem rnae8_foldl_cast (ph : Fin 13 → ℤ) {phC : Fin 13 → ℂ} (hC : ∀ b, phC b = ((ph b : ℤ) : ℂ)) :
        ∀ (L : List (ℕ × Matrix (Fin 13) (Fin 13) ℤ)) (c : ℂ) (v : Fin 13 → ℤ),
        (L.map rnae8_toC).foldl (fun w U => U.mulVec (fun b => phC b * w b)) (fun b => c * ((v b : ℤ) : ℂ))
          = fun b => (c / (L.map (fun p => (p.1 : ℂ))).prod) * (((L.foldl (rnae8_stepZ ph) v) b : ℤ) : ℂ)
      | [], c, v => by simp
      | p :: L, c, v => by
        simp only [List.map_cons, List.foldl_cons, List.prod_cons]
        rw [rnae8_step_cast ph hC, rnae8_foldl_cast ph hC L]
        funext b
        rw [div_mul_eq_div_div, div_eq_mul_inv c (p.1 : ℂ)]
    
  7. The initial quantum state ψ₀ = (1/32) · rnae8_nz is normalized to unit norm.

    The clean phase query witness is instantiated with state space Fin 13, register map rnae8_reg, initial state ψ₀(b) = (1/32) · rnae8_nz(b), initial unitary U₀ = 1, and step unitaries Ut. For normalization, the squared norm of ψ₀(b) is rewritten as nz(b)² / 1024. Summing over Fin 13 and substituting ∑ nz(b)² = 1024 from rnae8_normZ confirms the total norm is 1.

    lines 134–147

    uses Complex.normSq, inferInstance, Submonoid.one_mem, rnae8_normZ

    /-- The known eight-query clean phase circuit at depth two. -/
    theorem recursive_nae_two_eight_clean_phase_queries : phase_query_computes_cleanly (recursive_nae 2) 8 := by
      refine ⟨Fin 13, inferInstance, inferInstance, rnae8_reg, fun b => (32 : ℂ)⁻¹ * ((rnae8_nz b : ℤ) : ℂ), ?_, 1,
        Submonoid.one_mem _, fun t => rnae8_toC (rnae8_stepsZ t), ?_, ?_⟩
      · have h1 : ∀ b, Complex.normSq ((32 : ℂ)⁻¹ * ((rnae8_nz b : ℤ) : ℂ)) = ((rnae8_nz b * rnae8_nz b : ℤ) : ℝ) / 1024 := by
          intro b
          rw [Complex.normSq_mul]
          have : ((rnae8_nz b : ℤ) : ℂ) = (((rnae8_nz b : ℤ) : ℝ) : ℂ) := by simp
          rw [this, Complex.normSq_ofReal]
          simp [Complex.normSq_apply]
          ring
        simp only [h1]
        rw [← Finset.sum_div, ← Int.cast_sum, rnae8_normZ]
        norm_num
  8. Each complex operator Ut in the circuit is unitary.

    Unitarity requires Ut* Ut = 1. Since Ut = c⁻¹ M for an integer matrix M, the adjoint is Ut* = c⁻¹ Mᵀ. Unfolding the definition and using rnae8_unitZ gives Ut* Ut = c⁻² (Mᵀ M) = c⁻² (c² I) = I.

    lines 148–163

    uses Int.castRingHom, rnae8_unitZ

      · intro t
        rw [Matrix.mem_unitaryGroup_iff']
        have hd : ((rnae8_stepsZ t).1 : ℂ) ≠ 0 := by
          fin_cases t <;> simp [rnae8_stepsZ]
        have hs : star (rnae8_toC (rnae8_stepsZ t)) = ((rnae8_stepsZ t).1 : ℂ)⁻¹ • (Matrix.transpose (rnae8_stepsZ t).2).map (Int.castRingHom ℂ) := by
          ext i j
          simp [rnae8_toC, Matrix.star_apply]
        show star (rnae8_toC (rnae8_stepsZ t)) * rnae8_toC (rnae8_stepsZ t) = 1
        rw [hs]
        unfold rnae8_toC
        rw [Matrix.smul_mul, Matrix.mul_smul, ← Matrix.map_mul, rnae8_unitZ t]
        ext i j
        simp only [Matrix.smul_apply, Matrix.map_apply, Matrix.one_apply, smul_eq_mul]
        split_ifs <;> simp [hd]
        field_simp
      · intro x b
  9. The circuit cleanly computes depth-two recursive ternary NAE on every input.

    The circuit output state is rewritten using rnae8_foldl_cast and rnae8_prodZ into (1 / 3359232) · rnae8_evalZ(x, b). Applying rnae8_allx replaces this with (-1)^(recursive_nae 2 x) · ψ₀(b), showing that the initial state is mapped to (-1)^(f(x)) ψ₀ without residual garbage. The condition that phase queries cast correctly between ℤ and ℂ is verified by case analysis on whether the basis element queries an input bit or is an ancilla.

    lines 164–174

    uses rnae8_foldl_cast, rnae8_prodZ, rnae8_allx

        rw [Matrix.one_mulVec,
          show (List.ofFn fun t => rnae8_toC (rnae8_stepsZ t)) = (List.ofFn rnae8_stepsZ).map rnae8_toC by rw [List.map_ofFn]; rfl,
          rnae8_foldl_cast (rnae8_phz x) ?hC (List.ofFn rnae8_stepsZ) _ rnae8_nz, rnae8_prodZ]
        · show _ * ((rnae8_evalZ x b : ℤ) : ℂ) = _
          rw [rnae8_allx x b]
          by_cases hf : recursive_nae 2 x <;> simp [hf] <;> ring
        · intro c
          unfold rnae8_phz
          cases rnae8_reg c with
          | none => simp
          | some i => by_cases hx : x i <;> simp [hx]
Fin.sum_univ_succ
splits a summation over Fin (n + 1) into the first term and the sum over the remaining terms
rnae8_Dz_fast
the integer matrix rnae8_Dz acts on vectors via the block-sum formula rnae8_fastD
rnae8_T2z_fast
the integer matrix rnae8_T2z acts on vectors via the formula rnae8_fastT
rnae8_Rz_fast
the integer reflection matrix rnae8_Rz acts on vectors via the rank-one update rnae8_fastR
rnae8_Iz_fast
the integer identity matrix rnae8_Iz acts as the identity on vectors
List.ofFn_succ
converting a function on Fin (n + 1) to a list prepends the value at zero to the list of remaining values
List.ofFn_zero
converting a function on Fin 0 to a list yields the empty list
rnae8_evalZ_eq
the circuit simulation by matrix folding equals the composition of fast updates rnae8_evalF
rnae8_allbF
on all 512 concrete nine-bit Boolean inputs, the fast evaluation equals the depth-two recursive NAE value times 3359232 times rnae8_nz
rnae8_allx
for every Boolean input function on nine bits, the circuit fold equals the depth-two recursive NAE value times 3359232 times rnae8_nz
rnae8_unitZ
each integer step matrix satisfies Mᵀ M = c² I for its associated scaling factor c
rnae8_normZ
the sum of squared components of the integer vector rnae8_nz equals 1024
rnae8_prodZ
the product of the scaling factors across the eight circuit steps equals 3359232
Int.castRingHom
the canonical ring homomorphism embedding integers into a target ring
Int.cast_sum
embedding an integer sum into a ring equals the sum of the embedded integers
Int.cast_mul
embedding an integer product into a ring equals the product of the embedded integers
Finset.mul_sum
multiplication distributes over a finite sum
Finset.sum_congr
two finite sums are equal if their index sets and summands agree
List.map_cons
mapping a function over a non-empty list applies the function to the head and recurses on the tail
rnae8_step_cast
a single step of the complex circuit corresponds to the integer step scaled by the reciprocal of the step factor
rnae8_foldl_cast
folding the complex unitaries over phase queries equals the integer fold divided by the product of scaling factors
div_mul_eq_div_div
dividing by a product is equivalent to dividing sequentially by each factor
div_eq_mul_inv
division by b is equivalent to multiplication by b⁻¹
inferInstance
synthesizes a typeclass instance from the current context
Submonoid.one_mem
the identity element belongs to any submonoid
Complex.normSq
the squared complex absolute value, mapping a complex number to the sum of the squares of its real and imaginary parts

Written by a model (gemini-3.8-flash) from the Lean proof and the goal states Lean reported at each step. Lean verified the proof; this explanation is a reading aid and is not itself machine-checked.

Lean source view module on GitHub

def rnae8_blk (a : Fin 13) : ℕ := a.val / 3def rnae8_kd (a b : Fin 13) : ℤ := if a = b then 1 else 0def rnae8_Dz : Matrix (Fin 13) (Fin 13) ℤ := Matrix.of fun a b =>  if a.val < 9 ∧ b.val < 9 then (if rnae8_blk a = rnae8_blk b then 2 else 0) - 3 * rnae8_kd a b else 3 * rnae8_kd a bdef rnae8_T2z : Matrix (Fin 13) (Fin 13) ℤ := Matrix.of fun a b =>  if a.val < 9 ∧ b.val < 9 then 9 * rnae8_kd a b - (if rnae8_blk a = rnae8_blk b then 6 else 0) + 2 else 9 * rnae8_kd a bdef rnae8_nz (a : Fin 13) : ℤ :=  if a.val < 9 then 9 else if a.val = 9 then 17 else if a.val = 10 then 2 else 1def rnae8_Rz : Matrix (Fin 13) (Fin 13) ℤ := Matrix.of fun a b => rnae8_nz a * rnae8_nz b - 512 * rnae8_kd a bdef rnae8_Iz : Matrix (Fin 13) (Fin 13) ℤ := Matrix.of rnae8_kddef rnae8_stepsZ : Fin 8 → ℕ × Matrix (Fin 13) (Fin 13) ℤ :=  ![(3, rnae8_Dz), (9, rnae8_T2z), (3, rnae8_Dz), (512, rnae8_Rz), (3, rnae8_Dz), (9, rnae8_T2z), (3, rnae8_Dz), (1, rnae8_Iz)]def rnae8_reg (b : Fin 13) : Option (Fin (3 ^ 2)) := if h : b.val < 9 then some ⟨b.val, h⟩ else nonedef rnae8_phz (x : Fin (3 ^ 2) → Bool) (b : Fin 13) : ℤ :=  match rnae8_reg b with | none => 1 | some i => if x i then -1 else 1def rnae8_stepZ (ph : Fin 13 → ℤ) (w : Fin 13 → ℤ) (p : ℕ × Matrix (Fin 13) (Fin 13) ℤ) : Fin 13 → ℤ :=  p.2.mulVec (fun b => ph b * w b)def rnae8_evalZ (x : Fin (3 ^ 2) → Bool) : Fin 13 → ℤ :=  (List.ofFn rnae8_stepsZ).foldl (rnae8_stepZ (rnae8_phz x)) rnae8_nzdef rnae8_bsum (w : Fin 13 → ℤ) (k : ℕ) : ℤ :=  match k with  | 0 => w 0 + w 1 + w 2  | 1 => w 3 + w 4 + w 5  | _ => w 6 + w 7 + w 8def rnae8_tot (w : Fin 13 → ℤ) : ℤ := rnae8_bsum w 0 + rnae8_bsum w 1 + rnae8_bsum w 2def rnae8_fastD (w : Fin 13 → ℤ) (i : Fin 13) : ℤ :=  if i.val < 9 then 2 * rnae8_bsum w (i.val / 3) - 3 * w i else 3 * w idef rnae8_fastT (w : Fin 13 → ℤ) (i : Fin 13) : ℤ :=  if i.val < 9 then 9 * w i - 6 * rnae8_bsum w (i.val / 3) + 2 * rnae8_tot w else 9 * w idef rnae8_dotn (w : Fin 13 → ℤ) : ℤ := 9 * rnae8_tot w + 17 * w 9 + 2 * w 10 + w 11 + w 12def rnae8_fastR (w : Fin 13 → ℤ) (i : Fin 13) : ℤ := rnae8_nz i * rnae8_dotn w - 512 * w itheorem rnae8_Dz_fast (w : Fin 13 → ℤ) : rnae8_Dz.mulVec w = rnae8_fastD w := by  funext i  fin_cases i <;>    simp [Matrix.mulVec, dotProduct, Fin.sum_univ_succ, rnae8_Dz, rnae8_kd, rnae8_blk, rnae8_fastD, rnae8_bsum] <;> ringtheorem rnae8_T2z_fast (w : Fin 13 → ℤ) : rnae8_T2z.mulVec w = rnae8_fastT w := by  funext i  fin_cases i <;>    simp [Matrix.mulVec, dotProduct, Fin.sum_univ_succ, rnae8_T2z, rnae8_kd, rnae8_blk, rnae8_fastT, rnae8_bsum, rnae8_tot] <;> ringtheorem rnae8_Rz_fast (w : Fin 13 → ℤ) : rnae8_Rz.mulVec w = rnae8_fastR w := by  funext i  fin_cases i <;>    simp [Matrix.mulVec, dotProduct, Fin.sum_univ_succ, rnae8_Rz, rnae8_kd, rnae8_nz, rnae8_fastR, rnae8_dotn, rnae8_bsum, rnae8_tot] <;> ringtheorem rnae8_Iz_fast (w : Fin 13 → ℤ) : rnae8_Iz.mulVec w = w := by  funext i  fin_cases i <;> simp [Matrix.mulVec, dotProduct, Fin.sum_univ_succ, rnae8_Iz, rnae8_kd]def rnae8_evalF (x : Fin (3 ^ 2) → Bool) : Fin 13 → ℤ :=  let q := fun (w : Fin 13 → ℤ) (b : Fin 13) => rnae8_phz x b * w b  let v1 := rnae8_fastD (q rnae8_nz)  let v2 := rnae8_fastT (q v1)  let v3 := rnae8_fastD (q v2)  let v4 := rnae8_fastR (q v3)  let v5 := rnae8_fastD (q v4)  let v6 := rnae8_fastT (q v5)  let v7 := rnae8_fastD (q v6)  q v7theorem rnae8_evalZ_eq (x : Fin (3 ^ 2) → Bool) : rnae8_evalZ x = rnae8_evalF x := by  simp only [rnae8_evalZ, rnae8_evalF, List.ofFn_succ, List.ofFn_zero, List.foldl_cons, List.foldl_nil, rnae8_stepZ, rnae8_stepsZ]  simp [rnae8_Dz_fast, rnae8_T2z_fast, rnae8_Rz_fast, rnae8_Iz_fast]set_option maxRecDepth 100000 intheorem rnae8_allbF : ∀ b0 b1 b2 b3 b4 b5 b6 b7 b8 : Bool, ∀ b,    rnae8_evalF (![b0, b1, b2, b3, b4, b5, b6, b7, b8] : Fin (3 ^ 2) → Bool) b    = (if recursive_nae 2 (![b0, b1, b2, b3, b4, b5, b6, b7, b8] : Fin (3 ^ 2) → Bool) then -1 else 1)      * 3359232 * rnae8_nz b := by  decide +kerneltheorem rnae8_allx : ∀ x : Fin (3 ^ 2) → Bool, ∀ b, rnae8_evalZ x b    = (if recursive_nae 2 x then -1 else 1) * 3359232 * rnae8_nz b := by  intro x  have hx : x = (![x 0, x 1, x 2, x 3, x 4, x 5, x 6, x 7, x 8] : Fin (3 ^ 2) → Bool) := by    funext i; fin_cases i <;> rfl  rw [hx]  rw [rnae8_evalZ_eq]  exact rnae8_allbF _ _ _ _ _ _ _ _ _set_option maxRecDepth 100000 intheorem rnae8_unitZ : ∀ t, Matrix.transpose (rnae8_stepsZ t).2 * (rnae8_stepsZ t).2 = ((rnae8_stepsZ t).1 ^ 2 : ℤ) • (1 : Matrix (Fin 13) (Fin 13) ℤ) := by  decide +kerneltheorem rnae8_normZ : ∑ b, rnae8_nz b * rnae8_nz b = 1024 := by decide +kerneltheorem rnae8_prodZ : ((List.ofFn rnae8_stepsZ).map (fun p => (p.1 : ℂ))).prod = 3359232 := by  simp [List.ofFn_succ, rnae8_stepsZ]  norm_numnoncomputable def rnae8_toC (p : ℕ × Matrix (Fin 13) (Fin 13) ℤ) : Matrix (Fin 13) (Fin 13) ℂ :=  ((p.1 : ℂ)⁻¹) • p.2.map (Int.castRingHom ℂ)theorem rnae8_step_cast (ph : Fin 13 → ℤ) {phC : Fin 13 → ℂ} (hC : ∀ b, phC b = ((ph b : ℤ) : ℂ))    (p : ℕ × Matrix (Fin 13) (Fin 13) ℤ) (c : ℂ) (v : Fin 13 → ℤ) :    (rnae8_toC p).mulVec (fun b => phC b * (c * ((v b : ℤ) : ℂ)))      = fun b => (c * (p.1 : ℂ)⁻¹) * ((rnae8_stepZ ph v p b : ℤ) : ℂ) := by  funext i  simp only [rnae8_toC, rnae8_stepZ, Matrix.mulVec, dotProduct, Matrix.smul_apply, Matrix.map_apply,    Int.coe_castRingHom, smul_eq_mul, Int.cast_sum, Int.cast_mul, Finset.mul_sum, hC]  refine Finset.sum_congr rfl fun j _ => ?_  ringtheorem rnae8_foldl_cast (ph : Fin 13 → ℤ) {phC : Fin 13 → ℂ} (hC : ∀ b, phC b = ((ph b : ℤ) : ℂ)) :    ∀ (L : List (ℕ × Matrix (Fin 13) (Fin 13) ℤ)) (c : ℂ) (v : Fin 13 → ℤ),    (L.map rnae8_toC).foldl (fun w U => U.mulVec (fun b => phC b * w b)) (fun b => c * ((v b : ℤ) : ℂ))      = fun b => (c / (L.map (fun p => (p.1 : ℂ))).prod) * (((L.foldl (rnae8_stepZ ph) v) b : ℤ) : ℂ)  | [], c, v => by simp  | p :: L, c, v => by    simp only [List.map_cons, List.foldl_cons, List.prod_cons]    rw [rnae8_step_cast ph hC, rnae8_foldl_cast ph hC L]    funext b    rw [div_mul_eq_div_div, div_eq_mul_inv c (p.1 : ℂ)]/-- The known eight-query clean phase circuit at depth two. -/theorem recursive_nae_two_eight_clean_phase_queries : phase_query_computes_cleanly (recursive_nae 2) 8 := by  refine ⟨Fin 13, inferInstance, inferInstance, rnae8_reg, fun b => (32 : ℂ)⁻¹ * ((rnae8_nz b : ℤ) : ℂ), ?_, 1,    Submonoid.one_mem _, fun t => rnae8_toC (rnae8_stepsZ t), ?_, ?_⟩  · have h1 : ∀ b, Complex.normSq ((32 : ℂ)⁻¹ * ((rnae8_nz b : ℤ) : ℂ)) = ((rnae8_nz b * rnae8_nz b : ℤ) : ℝ) / 1024 := by      intro b      rw [Complex.normSq_mul]      have : ((rnae8_nz b : ℤ) : ℂ) = (((rnae8_nz b : ℤ) : ℝ) : ℂ) := by simp      rw [this, Complex.normSq_ofReal]      simp [Complex.normSq_apply]      ring    simp only [h1]    rw [← Finset.sum_div, ← Int.cast_sum, rnae8_normZ]    norm_num  · intro t    rw [Matrix.mem_unitaryGroup_iff']    have hd : ((rnae8_stepsZ t).1 : ℂ) ≠ 0 := by      fin_cases t <;> simp [rnae8_stepsZ]    have hs : star (rnae8_toC (rnae8_stepsZ t)) = ((rnae8_stepsZ t).1 : ℂ)⁻¹ • (Matrix.transpose (rnae8_stepsZ t).2).map (Int.castRingHom ℂ) := by      ext i j      simp [rnae8_toC, Matrix.star_apply]    show star (rnae8_toC (rnae8_stepsZ t)) * rnae8_toC (rnae8_stepsZ t) = 1    rw [hs]    unfold rnae8_toC    rw [Matrix.smul_mul, Matrix.mul_smul, ← Matrix.map_mul, rnae8_unitZ t]    ext i j    simp only [Matrix.smul_apply, Matrix.map_apply, Matrix.one_apply, smul_eq_mul]    split_ifs <;> simp [hd]    field_simp  · intro x b    rw [Matrix.one_mulVec,      show (List.ofFn fun t => rnae8_toC (rnae8_stepsZ t)) = (List.ofFn rnae8_stepsZ).map rnae8_toC by rw [List.map_ofFn]; rfl,      rnae8_foldl_cast (rnae8_phz x) ?hC (List.ofFn rnae8_stepsZ) _ rnae8_nz, rnae8_prodZ]    · show _ * ((rnae8_evalZ x b : ℤ) : ℂ) = _      rw [rnae8_allx x b]      by_cases hf : recursive_nae 2 x <;> simp [hf] <;> ring    · intro c      unfold rnae8_phz      cases rnae8_reg c with      | none => simp      | some i => by_cases hx : x i <;> simp [hx]
See it in the graphDiscuss this resultChallenge it