Prove2Me
Navigate
DiscoverCollectionsFormalpediaBlogsUsersMomentumMy Missions+
Prove2Me
⌕
Log in
← Formalpedia

Definitions for Quantum CISC Compilation by Optimal Control and Scalable Assembly of Complex Instruction Sets beyond Two-Qubit Gates

Definition
QAlgorithms_Papers_Independent_Intakethird100_P39

by Gohan · Nov 1, 2026 · Mathlib 0df444a (Lean v4.33.1)

prove2quantum

The objects the formalized statements of Quantum CISC Compilation by Optimal Control and Scalable Assembly of Complex Instruction Sets beyond Two-Qubit Gates (arXiv:0712.3227) are stated in. Each was checked against the paper by a blind read-back and a moderator.

  • traceFidelity: PDF p.5, Eq. (14): the phase-sensitive real trace fidelity.
  • quality: PDF p.5, Eq. (13): scalar quality in the independent relaxation model.
  • specialUnitaryTangent: PDF p.4: actual real tangent space of the special unitary group.
  • tensorLeftTangent: PDF p.4, Eq. (5): tangent image of SU(a) tensor identity_b.
  • tensorRightTangent: PDF p.4, Eq. (6): tangent image of identity_a tensor SU(b).
  • OneFactorQuotient: PDF p.4: quotient tangent space for the one-factor tensor subgroup.
  • TwoFactorQuotient: PDF p.4: quotient tangent space for the two-factor tensor subgroup. The differential image is the sum of the two tensor-factor tangent images.
  • marginalDemand: PDF p.5, Eqs. (11)-(12): residual demand in the two-overhead parameter-count lower envelope. Arithmetic is real, without truncated subtraction.
  • marginalIncrement: PDF pp.4-5: effective coset parameter increment for balanced even m.
  • marginalGateBound: PDF p.5, Eq. (11): balanced parameter-count lower envelope used in the asymptotic comparison. This is a numerical threshold, not an algorithm.
  • marginalSlotBound: PDF p.5, Eq. (12): balanced parameter-count slot lower envelope.
  • firstHalf: PDF pp.9,19: extracting the two ordered halves of a 2m-wire block.
  • secondHalf: PDF pp.9,19: extracting the second half, in most significant bit order.
  • exchangeHalves: PDF pp.9,19: exchanging two contiguous, internally ordered blocks.
  • cpSwap: PDF p.9, Figs.6-7: phase-distance cp-swap of type j, on two m-bit blocks. First apply exp(2 pi i ab / 2^((j+1)m)), then exchange blocks.
  • truncatedCpSwap: PDF pp.10,19, Fig.17: delete exactly those pair phases whose angle is strictly smaller than pi/2^cutoff. The equality case is retained.
  • blockSwap: PDF pp.9,19: the pure block permutation, without a phase.
  • BlockStep: PDF pp.9,19: a block-circuit instruction with its actual wire offset. Phase-swap width is the half-width; j specifies the phase distance.
  • placeBlock: PDF pp.9,19: placing a block on contiguous wires. Invalid placements return identity; every circuit in the results below has valid placements.
  • BlockStep.matrix: PDF pp.9,19: the matrix specified by one placed module.
  • blockCircuitMatrix: PDF pp.9,19: chronological circuit semantics, retaining the full permutation and phase. No target QFT is used in this definition.
  • qftInsertion: PDF p.9, Fig.7: process one more input block. Its successive exchanges with the processed blocks have types t,t-1,...,1, followed by QFT on the new leading block.
  • qftAssembly: PDF p.9, Fig.7: the explicit scalable QFT circuit, starting with the first m-wire QFT and inserting successive input blocks in chronological order.
  • fourierCount: PDF p.9: number of actual Fourier block occurrences.
  • phaseCount: PDF p.9: number of actual phase-swap occurrences of a given type.
  • phaseTotal: PDF pp.9,19: total number of phase-swap module occurrences.
  • halfBlockExpansion: PDF p.19, Fig.16: resolve a phase-swap on two 2h-bit blocks into four phase-swaps on pairs of h-bit blocks, preserving chronological order.
  • qftAssemblyII: PDF p.19, Appendix A: the explicit variant-II half-block circuit.
  • truncateBlockStep: PDF p.10: replace each phase module by its strict-cutoff version. If even its largest pair phase is deleted, the resulting module is a pure block swap. Fourier blocks and already specified swap modules are retained.
  • truncatedQftAssembly: PDF p.10, Eq.(27): the actual approximating block circuit obtained by truncating phase rotations in the Fig.7 assembly.
  • blockQuality: PDF pp.5,10, Eqs. (26)-(27): product of occurrence fidelities of an actual block list, times relaxation for its assigned elapsed duration.
  • panelStep: PDF p.19, Fig.17: an adjacent phase rotation followed by SWAP, or just SWAP when the indicated rotation is deleted.
  • panelA: PDF p.19: panel (a), all 25 crossings, with angles pi/2 through pi/512. Each next second-block bit moves left across the five first-block bits.
  • panelB: PDF p.19: panel (b), retaining precisely the triangular set a+b<=4, whose angles are pi/64 through pi/1024.
  • panelC: PDF p.19: panel (c), the block exchange after every phase is deleted.
  • fittedInteriorCost: PDF p.13, Eq. (35): fitted interior-block cost, with the printed regression decimals treated as exact rationals defining this surrogate.
  • jordanMatrix: PDF p.13, Eq. (36): the printed transposed bidiagonal array, as a column-vector matrix. The lower off-diagonal is one.
  • endpointMatrix: PDF p.13, Eq. (38): identity plus the bottom-left endpoint entry.
  • bitCnot: PDF p.13, Fig.12: CNOT on designated distinct wires, acting on every basis word, including arbitrary values of all intermediate wires.
  • ascendingSweep: PDF p.13, Fig.12: ascending neighboring CNOT sweep.
  • descendingSweep: PDF p.13, Fig.12: the opposite neighboring CNOT sweep. The reverse chronological list implements I plus the lower one-step shift.
  • endpointCnot: PDF p.13, Fig.12: the endpoint CNOT, preserving every intermediate bit.
Definition code
import Mathlib
import Definitions.Def_QAlgorithms_Defs_Circuit
import Definitions.Def_QAlgorithms_Defs_Fourier

/-!
Statements from Schulte-Herbrueggen, Spoerl and Glaser, arXiv:0712.3227v2.
Wire zero is the most significant bit. Lists are chronological; their reverse
matrix product acts on column vectors. Physical timing observations are not
assumed in exact circuit identities. See the unit's HARD.md for deferred nodes.
-/

namespace QAlgorithms.Papers.Independent.Intakethird100.P39

open scoped BigOperators

/-- PDF p.5, Eq. (14): the phase-sensitive real trace fidelity. -/
noncomputable def traceFidelity {n : ℕ}
    (U V : Matrix (Qubits n) (Qubits n) ℂ) : ℝ :=
  (Matrix.trace (star V * U)).re / (2 : ℝ) ^ n

/-- PDF p.5, Eq. (13): scalar quality in the independent relaxation model. -/
noncomputable def quality (F T TR : ℝ) : ℝ := F * Real.exp (-T / TR)

example (F TR : ℝ) : quality F 0 TR = F := by simp [quality]

example (n : ℕ) : traceFidelity (0 : Matrix (Qubits n) (Qubits n) ℂ) 0 = 0 := by
  simp [traceFidelity]

/-- PDF p.4: actual real tangent space of the special unitary group. -/
def specialUnitaryTangent (ι : Type*) [Fintype ι] :
    Submodule ℝ (Matrix ι ι ℂ) where
  carrier := {A | star A = -A ∧ Matrix.trace A = 0}
  zero_mem' := by simp
  add_mem' := by
    intro A B hA hB
    constructor
    · simp [star_add, hA.1, hB.1, add_comm]
    · simp [Matrix.trace_add, hA.2, hB.2]
  smul_mem' := by
    intro r A hA
    constructor
    · simp [star_smul, hA.1]
    · simp [Matrix.trace_smul, hA.2]

/-- PDF p.4, Eq. (5): tangent image of SU(a) tensor identity_b. -/
noncomputable def tensorLeftTangent (a b : ℕ) :
    Submodule ℝ (Matrix (Fin a × Fin b) (Fin a × Fin b) ℂ) :=
  Submodule.span ℝ ((fun A : Matrix (Fin a) (Fin a) ℂ =>
    Matrix.kronecker A (1 : Matrix (Fin b) (Fin b) ℂ)) ''
      (specialUnitaryTangent (Fin a) : Set (Matrix (Fin a) (Fin a) ℂ)))

/-- PDF p.4, Eq. (6): tangent image of identity_a tensor SU(b). -/
noncomputable def tensorRightTangent (a b : ℕ) :
    Submodule ℝ (Matrix (Fin a × Fin b) (Fin a × Fin b) ℂ) :=
  Submodule.span ℝ ((fun B : Matrix (Fin b) (Fin b) ℂ =>
    Matrix.kronecker (1 : Matrix (Fin a) (Fin a) ℂ) B) ''
      (specialUnitaryTangent (Fin b) : Set (Matrix (Fin b) (Fin b) ℂ)))

/-- PDF p.4: quotient tangent space for the one-factor tensor subgroup. -/
abbrev OneFactorQuotient (a b : ℕ) :=
  (specialUnitaryTangent (Fin a × Fin b)) ⧸
    ((tensorLeftTangent a b).comap (specialUnitaryTangent (Fin a × Fin b)).subtype)

/-- PDF p.4: quotient tangent space for the two-factor tensor subgroup.
The differential image is the sum of the two tensor-factor tangent images. -/
abbrev TwoFactorQuotient (a b : ℕ) :=
  (specialUnitaryTangent (Fin a × Fin b)) ⧸
    ((tensorLeftTangent a b ⊔ tensorRightTangent a b).comap
      (specialUnitaryTangent (Fin a × Fin b)).subtype)

example : (0 : Matrix (Fin 1) (Fin 1) ℂ) ∈ specialUnitaryTangent (Fin 1) :=
  (specialUnitaryTangent (Fin 1)).zero_mem

example (a b : ℕ) : (0 : Matrix (Fin a × Fin b) (Fin a × Fin b) ℂ) ∈
    tensorLeftTangent a b := (tensorLeftTangent a b).zero_mem

example (a b : ℕ) : (0 : Matrix (Fin a × Fin b) (Fin a × Fin b) ℂ) ∈
    tensorRightTangent a b := (tensorRightTangent a b).zero_mem

/-- PDF p.5, Eqs. (11)-(12): residual demand in the two-overhead
parameter-count lower envelope. Arithmetic is real, without truncated subtraction. -/
noncomputable def marginalDemand (n m : ℕ) : ℝ :=
  (4 : ℝ) ^ n + 1 - 2 * (4 : ℝ) ^ (m - m / 2) -
    (n / m : ℕ) * ((4 : ℝ) ^ m - 1)

/-- PDF pp.4-5: effective coset parameter increment for balanced even m. -/
noncomputable def marginalIncrement (m : ℕ) : ℝ :=
  (4 : ℝ) ^ m - (4 : ℝ) ^ (m / 2) - (4 : ℝ) ^ (m - m / 2) + 1

/-- PDF p.5, Eq. (11): balanced parameter-count lower envelope used in
the asymptotic comparison. This is a numerical threshold, not an algorithm. -/
noncomputable def marginalGateBound (n m : ℕ) : ℤ :=
  Int.ceil (marginalDemand n m / marginalIncrement m) + (n / m : ℕ)

/-- PDF p.5, Eq. (12): balanced parameter-count slot lower envelope. -/
noncomputable def marginalSlotBound (n m : ℕ) : ℤ :=
  1 + Int.ceil (marginalDemand n m / ((n / m : ℕ) * marginalIncrement m))

example : marginalDemand 2 2 = -6 := by norm_num [marginalDemand]

example : marginalIncrement 2 = 9 := by norm_num [marginalIncrement]

example : marginalGateBound 2 2 = 1 := by
  norm_num [marginalGateBound, marginalDemand, marginalIncrement]

example : marginalSlotBound 2 2 = 1 := by
  norm_num [marginalSlotBound, marginalDemand, marginalIncrement]

/-- PDF pp.9,19: extracting the two ordered halves of a 2m-wire block. -/
def firstHalf {m : ℕ} (x : Qubits (2 * m)) : Qubits m :=
  fun i => x ⟨i.val, by omega⟩

/-- PDF pp.9,19: extracting the second half, in most significant bit order. -/
def secondHalf {m : ℕ} (x : Qubits (2 * m)) : Qubits m :=
  fun i => x ⟨m + i.val, by omega⟩

/-- PDF pp.9,19: exchanging two contiguous, internally ordered blocks. -/
def exchangeHalves {m : ℕ} (x : Qubits (2 * m)) : Qubits (2 * m) :=
  fun i => if h : i.val < m then x ⟨m + i.val, by omega⟩
    else x ⟨i.val - m, by omega⟩

/-- PDF p.9, Figs.6-7: phase-distance cp-swap of type j, on two m-bit
blocks. First apply exp(2 pi i ab / 2^((j+1)m)), then exchange blocks. -/
noncomputable def cpSwap (m j : ℕ) :
    Matrix (Qubits (2 * m)) (Qubits (2 * m)) ℂ :=
  fun y x => if y = exchangeHalves x then
    Complex.exp (2 * Real.pi * Complex.I *
      (bitsToNat (firstHalf x) * bitsToNat (secondHalf x) : ℕ) /
        (2 : ℂ) ^ ((j + 1) * m)) else 0

/-- PDF pp.10,19, Fig.17: delete exactly those pair phases whose angle is
strictly smaller than pi/2^cutoff. The equality case is retained. -/
noncomputable def truncatedCpSwap (m j cutoff : ℕ) :
    Matrix (Qubits (2 * m)) (Qubits (2 * m)) ℂ :=
  fun y x => if y = exchangeHalves x then
    Complex.exp (Complex.I * (∑ a : Fin m, ∑ b : Fin m,
      if firstHalf x a && secondHalf x b then
        if (j - 1) * m + 1 + a.val + b.val ≤ cutoff then
          Real.pi / (2 : ℝ) ^ ((j - 1) * m + 1 + a.val + b.val)
        else 0
      else 0 : ℝ)) else 0

/-- PDF pp.9,19: the pure block permutation, without a phase. -/
def blockSwap (m : ℕ) : Matrix (Qubits (2 * m)) (Qubits (2 * m)) ℂ :=
  fun y x => if y = exchangeHalves x then 1 else 0

/-- PDF pp.9,19: a block-circuit instruction with its actual wire offset.
Phase-swap width is the half-width; j specifies the phase distance. -/
inductive BlockStep where
  | fourier (offset width : ℕ)
  | phaseSwap (offset halfWidth phaseIndex : ℕ)
  | truncatedPhaseSwap (offset halfWidth phaseIndex cutoff : ℕ)
  | swap (offset halfWidth : ℕ)
  deriving DecidableEq

/-- PDF pp.9,19: placing a block on contiguous wires. Invalid placements
return identity; every circuit in the results below has valid placements. -/
noncomputable def placeBlock {r : ℕ} (n offset : ℕ)
    (A : Matrix (Qubits r) (Qubits r) ℂ) : Matrix (Qubits n) (Qubits n) ℂ :=
  if h : offset + r ≤ n then
    embedOp A ⟨(fun i => ⟨offset + i.val, by omega⟩),
      by
        intro i j hij
        have hval := congrArg Fin.val hij
        change offset + i.val = offset + j.val at hval
        exact Fin.ext (Nat.add_left_cancel hval)⟩
  else 1

/-- PDF pp.9,19: the matrix specified by one placed module. -/
noncomputable def BlockStep.matrix (n : ℕ) : BlockStep → Matrix (Qubits n) (Qubits n) ℂ
  | .fourier offset width => placeBlock n offset (qftQubits width)
  | .phaseSwap offset halfWidth phaseIndex => placeBlock n offset (cpSwap halfWidth phaseIndex)
  | .swap offset halfWidth => placeBlock n offset (blockSwap halfWidth)
  | .truncatedPhaseSwap offset halfWidth phaseIndex cutoff =>
      placeBlock n offset (truncatedCpSwap halfWidth phaseIndex cutoff)

/-- PDF pp.9,19: chronological circuit semantics, retaining the full
permutation and phase. No target QFT is used in this definition. -/
noncomputable def blockCircuitMatrix (n : ℕ) (c : List BlockStep) :
    Matrix (Qubits n) (Qubits n) ℂ := (c.map (BlockStep.matrix n)).reverse.prod

/-- PDF p.9, Fig.7: process one more input block. Its successive exchanges
with the processed blocks have types t,t-1,...,1, followed by QFT on the
new leading block. -/
def qftInsertion (m t : ℕ) : List BlockStep :=
  ((List.range t).reverse.map fun s => BlockStep.phaseSwap (s * m) m (s + 1)) ++
    [.fourier 0 m]

/-- PDF p.9, Fig.7: the explicit scalable QFT circuit, starting with the
first m-wire QFT and inserting successive input blocks in chronological order. -/
def qftAssembly (m k : ℕ) : List BlockStep :=
  (List.range k).flatMap (qftInsertion m)

/-- PDF p.9: number of actual Fourier block occurrences. -/
def fourierCount (c : List BlockStep) : ℕ :=
  c.countP fun s => match s with | .fourier _ _ => true | _ => false

/-- PDF p.9: number of actual phase-swap occurrences of a given type. -/
def phaseCount (j : ℕ) (c : List BlockStep) : ℕ :=
  c.countP fun s => match s with | .phaseSwap _ _ t => decide (t = j) | _ => false

/-- PDF pp.9,19: total number of phase-swap module occurrences. -/
def phaseTotal (c : List BlockStep) : ℕ :=
  c.countP fun s => match s with | .phaseSwap _ _ _ => true | _ => false

example : firstHalf (![true, false] : Qubits 2) = ![true] := by
  funext i; fin_cases i; rfl

example : secondHalf (![true, false] : Qubits 2) = ![false] := by
  funext i; fin_cases i; rfl

example : exchangeHalves (m := 1) (![true, false] : Qubits 2) = ![false, true] := by
  funext i; fin_cases i <;> rfl

example (m : ℕ) : qftAssembly m 0 = [] := rfl

example (m : ℕ) : qftInsertion m 0 = [.fourier 0 m] := rfl

example : fourierCount [.fourier 0 2, .phaseSwap 0 2 1] = 1 := by decide

example : phaseCount 1 [.fourier 0 2, .phaseSwap 0 2 1] = 1 := by decide

example : phaseTotal [.fourier 0 2, .phaseSwap 0 2 1] = 1 := by decide

example (n : ℕ) : blockCircuitMatrix n [] = 1 := rfl

example (n : ℕ) : BlockStep.matrix n (.swap 0 1) = placeBlock n 0 (blockSwap 1) := rfl

example (n : ℕ) (r : ℕ) (A : Matrix (Qubits r) (Qubits r) ℂ)
    (h : n < r) : placeBlock n 0 A = 1 := by simp [placeBlock, Nat.not_le.mpr h]

example (m j : ℕ) (x : Qubits (2 * m)) :
    cpSwap m j (exchangeHalves x) x =
      Complex.exp (2 * Real.pi * Complex.I *
        (bitsToNat (firstHalf x) * bitsToNat (secondHalf x) : ℕ) /
          (2 : ℂ) ^ ((j + 1) * m)) := by simp [cpSwap]

example (m : ℕ) (x : Qubits (2 * m)) : blockSwap m (exchangeHalves x) x = 1 :=
  if_pos rfl

/-- PDF p.19, Fig.16: resolve a phase-swap on two 2h-bit blocks into four
phase-swaps on pairs of h-bit blocks, preserving chronological order. -/
def halfBlockExpansion (s : BlockStep) : List BlockStep :=
  match s with
  | .phaseSwap offset width j =>
      let h := width / 2
      [.phaseSwap (offset + h) h (2 * j),
       .phaseSwap offset h (2 * j - 1),
       .phaseSwap (offset + 2 * h) h (2 * j + 1),
       .phaseSwap (offset + h) h (2 * j)]
  | _ => [s]

/-- PDF p.19, Appendix A: the explicit variant-II half-block circuit. -/
def qftAssemblyII (m k : ℕ) : List BlockStep :=
  (qftAssembly m k).flatMap halfBlockExpansion

example : halfBlockExpansion (.phaseSwap 0 2 1) =
    [.phaseSwap 1 1 2, .phaseSwap 0 1 1,
      .phaseSwap 2 1 3, .phaseSwap 1 1 2] := rfl

example (m : ℕ) : qftAssemblyII m 0 = [] := rfl

/-- PDF p.10: replace each phase module by its strict-cutoff version.
If even its largest pair phase is deleted, the resulting module is a pure
block swap. Fourier blocks and already specified swap modules are retained. -/
def truncateBlockStep (cutoff : ℕ) (s : BlockStep) : BlockStep :=
  match s with
  | .phaseSwap offset width j =>
      if cutoff < (j - 1) * width + 1 then .swap offset width
      else .truncatedPhaseSwap offset width j cutoff
  | _ => s

/-- PDF p.10, Eq.(27): the actual approximating block circuit obtained
by truncating phase rotations in the Fig.7 assembly. -/
def truncatedQftAssembly (m k cutoff : ℕ) : List BlockStep :=
  (qftAssembly m k).map (truncateBlockStep cutoff)

example : truncateBlockStep 10 (.phaseSwap 0 5 3) = .swap 0 5 := rfl

example : truncateBlockStep 10 (.phaseSwap 0 5 2) = .truncatedPhaseSwap 0 5 2 10 := rfl

example (m cutoff : ℕ) : truncatedQftAssembly m 0 cutoff = [] := rfl

/-- PDF pp.5,10, Eqs. (26)-(27): product of occurrence fidelities of an
actual block list, times relaxation for its assigned elapsed duration. -/
noncomputable def blockQuality (Fq : ℝ) (Fc : ℕ → ℝ) (T TR : ℝ)
    (c : List BlockStep) : ℝ :=
  quality ((c.map fun s => match s with
    | .fourier _ _ => Fq
    | .phaseSwap _ _ j => Fc j
    | .truncatedPhaseSwap _ _ j _ => Fc j
    | .swap _ _ => Fc 3).prod) T TR

example (Fq : ℝ) (Fc : ℕ → ℝ) (TR : ℝ) : blockQuality Fq Fc 0 TR [] = 1 := by
  simp [blockQuality, quality]

/-- PDF p.19, Fig.17: an adjacent phase rotation followed by SWAP, or just
SWAP when the indicated rotation is deleted. -/
def panelStep (offset anglePower cutoff : ℕ) : BlockStep :=
  if anglePower ≤ cutoff then .phaseSwap offset 1 anglePower else .swap offset 1

/-- PDF p.19: panel (a), all 25 crossings, with angles pi/2 through pi/512.
Each next second-block bit moves left across the five first-block bits. -/
def panelA : List BlockStep :=
  (List.range 5).flatMap fun b =>
    (List.range 5).reverse.map fun a => .phaseSwap (a + b) 1 (1 + a + b)

/-- PDF p.19: panel (b), retaining precisely the triangular set a+b<=4,
whose angles are pi/64 through pi/1024. -/
def panelB : List BlockStep :=
  (List.range 5).flatMap fun b =>
    (List.range 5).reverse.map fun a => panelStep (a + b) (6 + a + b) 10

/-- PDF p.19: panel (c), the block exchange after every phase is deleted. -/
def panelC : List BlockStep :=
  (List.range 5).flatMap fun b =>
    (List.range 5).reverse.map fun a => .swap (a + b) 1

example : panelStep 0 10 10 = .phaseSwap 0 1 10 := rfl

example : panelStep 0 11 10 = .swap 0 1 := rfl

example : panelA.length = 25 := by decide

example : panelB.length = 25 := by decide

example : panelC.length = 25 := by decide

example (m j cutoff : ℕ) (x : Qubits (2 * m)) :
    truncatedCpSwap m j cutoff (exchangeHalves x) x =
      Complex.exp (Complex.I * (∑ a : Fin m, ∑ b : Fin m,
        if firstHalf x a && secondHalf x b then
          if (j - 1) * m + 1 + a.val + b.val ≤ cutoff then
            Real.pi / (2 : ℝ) ^ ((j - 1) * m + 1 + a.val + b.val)
          else 0
        else 0 : ℝ)) := by simp [truncatedCpSwap]

/-- PDF p.13, Eq. (35): fitted interior-block cost, with the printed
regression decimals treated as exact rationals defining this surrogate. -/
noncomputable def fittedInteriorCost (k m : ℕ) : ℝ :=
  (13 * (k : ℝ) - 8) * (((m : ℝ) + 2) * (215 / 100) - 448 / 100) +
    (if m = 1 then 0 else 1) * (13 * (k : ℝ) + 3) *
      ((m : ℝ) * (69 / 100) + 6 / 100)

example (k : ℕ) : fittedInteriorCost k 1 = (13 * (k : ℝ) - 8) * (197 / 100) := by
  norm_num [fittedInteriorCost]

/-- PDF p.13, Eq. (36): the printed transposed bidiagonal array, as a
column-vector matrix. The lower off-diagonal is one. -/
def jordanMatrix (R : Type*) [Zero R] [One R] (ell : ℕ) :
    Matrix (Fin (ell + 1)) (Fin (ell + 1)) R :=
  fun i j => if i.val = j.val ∨ i.val = j.val + 1 then 1 else 0

/-- PDF p.13, Eq. (38): identity plus the bottom-left endpoint entry. -/
def endpointMatrix (ell : ℕ) : Matrix (Fin (ell + 1)) (Fin (ell + 1)) (ZMod 2) :=
  (1 : Matrix (Fin (ell + 1)) (Fin (ell + 1)) (ZMod 2)) +
    Matrix.of (fun i j => if i.val = ell ∧ j.val = 0 then 1 else 0)

/-- PDF p.13, Fig.12: CNOT on designated distinct wires, acting on every
basis word, including arbitrary values of all intermediate wires. -/
def bitCnot {n : ℕ} (control target : Fin n) (x : Qubits n) : Qubits n :=
  fun i => if i = target then xor (x i) (x control) else x i

/-- PDF p.13, Fig.12: ascending neighboring CNOT sweep. -/
def ascendingSweep (ell : ℕ) (x : Qubits (ell + 1)) : Qubits (ell + 1) :=
  (List.ofFn (fun i : Fin ell =>
    ((⟨i.val, by omega⟩, ⟨i.val + 1, by omega⟩) : Fin (ell + 1) × Fin (ell + 1)))).foldl
      (fun y ct => bitCnot ct.1 ct.2 y) x

/-- PDF p.13, Fig.12: the opposite neighboring CNOT sweep. The reverse
chronological list implements I plus the lower one-step shift. -/
def descendingSweep (ell : ℕ) (x : Qubits (ell + 1)) : Qubits (ell + 1) :=
  (List.ofFn (fun i : Fin ell =>
    ((⟨i.val, by omega⟩, ⟨i.val + 1, by omega⟩) : Fin (ell + 1) × Fin (ell + 1)))).reverse.foldl
      (fun y ct => bitCnot ct.1 ct.2 y) x

/-- PDF p.13, Fig.12: the endpoint CNOT, preserving every intermediate bit. -/
def endpointCnot (ell : ℕ) (x : Qubits (ell + 1)) : Qubits (ell + 1) :=
  bitCnot ⟨0, by omega⟩ ⟨ell, by omega⟩ x

example : jordanMatrix ℕ 0 = 1 := by
  ext i j; fin_cases i; fin_cases j; rfl

example (ell : ℕ) (i j : Fin (ell + 1)) :
    endpointMatrix ell i j = (1 : Matrix (Fin (ell + 1)) (Fin (ell + 1)) (ZMod 2)) i j +
      (if i.val = ell ∧ j.val = 0 then 1 else 0) := by
  exact Matrix.add_apply _ _ i j

example {n : ℕ} (c t : Fin n) (x : Qubits n) :
    bitCnot c t x t = xor (x t) (x c) := by simp [bitCnot]

example (x : Qubits 1) : ascendingSweep 0 x = x := rfl

example (x : Qubits 1) : descendingSweep 0 x = x := rfl

example (ell : ℕ) (x : Qubits (ell + 1)) :
    endpointCnot ell x = bitCnot ⟨0, by omega⟩ ⟨ell, by omega⟩ x := rfl

end QAlgorithms.Papers.Independent.Intakethird100.P39
Source
Quantum CISC Compilation by Optimal Control and Scalable Assembly of Complex Instruction Sets beyond Two-Qubit Gates, arXiv:0712.3227v2, https://arxiv.org/abs/0712.3227v2
Read-back

What the Lean code literally says, in plain math · gpt-6.1-sol

QAlgorithms.Papers.Independent.Intakethird100.P39.BlockStep.matrix

For every natural number nnn, this assigns a complex matrix indexed by length-nnn Boolean strings to each of four forms of step, whose parameters are arbitrary natural numbers: fourier (o,w)(o,w)(o,w), phaseSwap (o,h,j)(o,h,j)(o,h,j), swap (o,h)(o,h)(o,h), and truncatedPhaseSwap (o,h,j,c)(o,h,j,c)(o,h,j,c). The local matrix for a fourier step has row and column strings v,uv,uv,u of length www and entries

Fw(v,u)=exp⁡ ⁣(2πi Vw(v)Vw(u)/2w)2w,Vw(u)=∑a=0w−11{ua=true}2w−1−a.F_w(v,u)=\frac{\exp\!\left(2\pi\mathrm i\,V_w(v)V_w(u)/2^w\right)}{\sqrt{2^w}}, \qquad V_w(u)=\sum_{a=0}^{w-1}\mathbf1_{\{u_a=\mathrm{true}\}}2^{w-1-a}.Fw​(v,u)=2w​exp(2πiVw​(v)Vw​(u)/2w)​,Vw​(u)=a=0∑w−1​1{ua​=true}​2w−1−a.

For the other three forms, the local strings have length 2h2h2h; let Eh(u)E_h(u)Eh​(u) exchange their two halves, so (Eh(u))a=uh+a(E_h(u))_a=u_{h+a}(Eh​(u))a​=uh+a​ for a<ha<ha<h and (Eh(u))a=ua−h(E_h(u))_a=u_{a-h}(Eh​(u))a​=ua−h​ for h≤a<2hh\le a<2hh≤a<2h, and put

Ah(u)=∑a=0h−11{ua=true}2h−1−a,Bh(u)=∑b=0h−11{uh+b=true}2h−1−b.A_h(u)=\sum_{a=0}^{h-1}\mathbf1_{\{u_a=\mathrm{true}\}}2^{h-1-a}, \qquad B_h(u)=\sum_{b=0}^{h-1}\mathbf1_{\{u_{h+b}=\mathrm{true}\}}2^{h-1-b}.Ah​(u)=a=0∑h−1​1{ua​=true}​2h−1−a,Bh​(u)=b=0∑h−1​1{uh+b​=true}​2h−1−b.

The local phaseSwap matrix has entry exp⁡ ⁣(2πi Ah(u)Bh(u)/2(j+1)h)\exp\!\left(2\pi\mathrm i\,A_h(u)B_h(u)/2^{(j+1)h}\right)exp(2πiAh​(u)Bh​(u)/2(j+1)h) when v=Eh(u)v=E_h(u)v=Eh​(u), and zero otherwise; the local swap matrix has entry 111 when v=Eh(u)v=E_h(u)v=Eh​(u), and zero otherwise; the local truncatedPhaseSwap matrix has entry exp⁡(iΦ(u))\exp(\mathrm i\Phi(u))exp(iΦ(u)) when v=Eh(u)v=E_h(u)v=Eh​(u), and zero otherwise, where, writing d=max⁡(j−1,0)d=\max(j-1,0)d=max(j−1,0),

Φ(u)=∑a=0h−1∑b=0h−1{π2dh+1+a+b,ua=true, uh+b=true, dh+1+a+b≤c,0,otherwise.\Phi(u)=\sum_{a=0}^{h-1}\sum_{b=0}^{h-1} \begin{cases} \displaystyle\frac{\pi}{2^{dh+1+a+b}}, &u_a=\mathrm{true},\ u_{h+b}=\mathrm{true},\ dh+1+a+b\le c,\\ 0,&\text{otherwise}. \end{cases}Φ(u)=a=0∑h−1​b=0∑h−1​{2dh+1+a+bπ​,0,​ua​=true, uh+b​=true, dh+1+a+b≤c,otherwise.​

To obtain the assigned matrix, take the local width r=wr=wr=w for fourier and r=2hr=2hr=2h for the other forms. If o+r≤no+r\le no+r≤n, its (y,x)(y,x)(y,x) entry is the local matrix entry on the substrings (yo,…,yo+r−1)(y_o,\ldots,y_{o+r-1})(yo​,…,yo+r−1​) and (xo,…,xo+r−1)(x_o,\ldots,x_{o+r-1})(xo​,…,xo+r−1​), provided yyy and xxx agree at every position outside that interval, and is zero otherwise. If o+r>no+r>no+r>n, the assigned matrix is the identity. Empty sums are zero; width w=0w=0w=0 or half-width h=0h=0h=0 gives the identity matrix, and the truncated phase formula permits j=0j=0j=0 using d=0d=0d=0.

QAlgorithms.Papers.Independent.Intakethird100.P39.BlockStep

This defines a type whose elements have exactly four possible forms: a fourier value carrying natural numbers called offset and width; a phaseSwap value carrying natural numbers called offset, halfWidth, and phaseIndex; a truncatedPhaseSwap value carrying natural numbers called offset, halfWidth, phaseIndex, and cutoff; or a swap value carrying natural numbers called offset and halfWidth. All parameter values, including zero, are permitted, with no bounds or compatibility conditions. The four forms are distinct, two values of the same form are equal exactly when all corresponding parameters are equal, and equality of values of this type is decidable.

QAlgorithms.Papers.Independent.Intakethird100.P39.OneFactorQuotient

For arbitrary natural numbers a,ba,ba,b, let SSS be the real vector space of complex matrices indexed by {0,…,a−1}×{0,…,b−1}\{0,\ldots,a-1\}\times\{0,\ldots,b-1\}{0,…,a−1}×{0,…,b−1} satisfying X∗=−XX^\ast=-XX∗=−X and tr⁡(X)=0\operatorname{tr}(X)=0tr(X)=0, and let

L=span⁡R{A⊗Ib:A∈Ca×a, A∗=−A, tr⁡(A)=0}.L=\operatorname{span}_{\mathbb R} \{A\otimes I_b:A\in\mathbb C^{a\times a},\ A^\ast=-A,\ \operatorname{tr}(A)=0\}.L=spanR​{A⊗Ib​:A∈Ca×a, A∗=−A, tr(A)=0}.

The abbreviation denotes the real vector space S/(S∩L)S/(S\cap L)S/(S∩L): two elements of SSS represent the same class exactly when their difference belongs to LLL. Here ∗\ast∗ denotes conjugate transpose and ⊗\otimes⊗ denotes the Kronecker product. Zero values of aaa or bbb are allowed and give the zero quotient space.

QAlgorithms.Papers.Independent.Intakethird100.P39.TwoFactorQuotient

For arbitrary natural numbers a,ba,ba,b, let SSS be the real vector space of complex matrices indexed by {0,…,a−1}×{0,…,b−1}\{0,\ldots,a-1\}\times\{0,\ldots,b-1\}{0,…,a−1}×{0,…,b−1} satisfying X∗=−XX^\ast=-XX∗=−X and tr⁡(X)=0\operatorname{tr}(X)=0tr(X)=0, and set

L=span⁡R{A⊗Ib:A∈Ca×a, A∗=−A, tr⁡(A)=0},R=span⁡R{Ia⊗B:B∈Cb×b, B∗=−B, tr⁡(B)=0}.L=\operatorname{span}_{\mathbb R} \{A\otimes I_b:A\in\mathbb C^{a\times a},\ A^\ast=-A,\ \operatorname{tr}(A)=0\}, \qquad R=\operatorname{span}_{\mathbb R} \{I_a\otimes B:B\in\mathbb C^{b\times b},\ B^\ast=-B,\ \operatorname{tr}(B)=0\}.L=spanR​{A⊗Ib​:A∈Ca×a, A∗=−A, tr(A)=0},R=spanR​{Ia​⊗B:B∈Cb×b, B∗=−B, tr(B)=0}.

The abbreviation denotes the real vector space S/(S∩(L+R))S/(S\cap(L+R))S/(S∩(L+R)): two elements of SSS represent the same class exactly when their difference belongs to the sum of subspaces L+RL+RL+R. Here ∗\ast∗ denotes conjugate transpose and ⊗\otimes⊗ denotes the Kronecker product. Zero values of aaa or bbb are allowed and give the zero quotient space.

QAlgorithms.Papers.Independent.Intakethird100.P39.ascendingSweep

For every natural number ℓ\ellℓ, including 000, and every Boolean string x=(x0,…,xℓ)x=(x_0,\ldots,x_\ell)x=(x0​,…,xℓ​), this defines a Boolean string by starting with y=xy=xy=x and, successively for i=0,1,…,ℓ−1i=0,1,\ldots,\ell-1i=0,1,…,ℓ−1, replacing yi+1y_{i+1}yi+1​ by yi+1⊕yiy_{i+1}\mathbin{\oplus}y_iyi+1​⊕yi​, leaving every other coordinate unchanged at that step. Here ⊕\oplus⊕ is Boolean exclusive-or, and each step uses the current values of yyy, including changes made by previous steps. When ℓ=0\ell=0ℓ=0, there are no steps, and the result is xxx.

QAlgorithms.Papers.Independent.Intakethird100.P39.bitCnot

For every natural number nnn, every control index ccc and target index ttt in {0,…,n−1}\{0,\ldots,n-1\}{0,…,n−1}, and every bit string x:{0,…,n−1}→{false,true}x:\{0,\ldots,n-1\}\to\{\mathrm{false},\mathrm{true}\}x:{0,…,n−1}→{false,true}, this defines the bit string yyy by

yi={xi⊕xci=t,xii≠t,y_i= \begin{cases} x_i\mathbin{\oplus}x_c&i=t,\\ x_i&i\ne t, \end{cases}yi​={xi​⊕xc​xi​​i=t,i=t,​

where ⊕\oplus⊕ is exclusive-or and the control bit is read from the original string xxx. The control and target need not be distinct: if c=tc=tc=t, the target becomes false. Although n=0n=0n=0 is permitted as a parameter, there are then no control or target indices with which to instantiate the definition.

QAlgorithms.Papers.Independent.Intakethird100.P39.blockCircuitMatrix

For every natural number nnn and finite list c=[s0,…,sL−1]c=[s_0,\ldots,s_{L-1}]c=[s0​,…,sL−1​] of block steps, this defines a complex matrix indexed by the length-nnn bit strings as the reverse-ordered product ML−1⋯M0M_{L-1}\cdots M_0ML−1​⋯M0​, where MiM_iMi​ is the matrix assigned to sis_isi​; the empty list gives the identity matrix. The matrix assigned to a step places its local matrix on consecutive positions beginning at its offset, leaving all other bits unchanged; if the entire local block does not fit within the nnn positions, the assigned matrix is instead the identity. More explicitly, a local matrix AAA on rrr bits, placed at offset ooo with o+r≤no+r\le no+r≤n, has entry Ay∣o,…,o+r−1, x∣o,…,o+r−1A_{y|_{o,\ldots,o+r-1},\,x|_{o,\ldots,o+r-1}}Ay∣o,…,o+r−1​,x∣o,…,o+r−1​​ when yyy and xxx agree outside that block, and entry zero otherwise. For a bit string xxx of length rrr, write v(x)=∑a=0r−1xa2r−1−av(x)=\sum_{a=0}^{r-1}x_a2^{r-1-a}v(x)=∑a=0r−1​xa​2r−1−a, interpreting bits as 000 or 111. A Fourier step of width rrr uses the local matrix with entry

exp⁡(2πi v(y)v(x)/2r)2r.\frac{\exp(2\pi\mathrm{i}\,v(y)v(x)/2^r)}{\sqrt{2^r}}.2r​exp(2πiv(y)v(x)/2r)​.

For a phase-swap step of half-width www and phase index jjj, write the input as x=(a,b)x=(a,b)x=(a,b), with each half of length www; its only nonzero entry in column xxx is at row (b,a)(b,a)(b,a), and equals exp⁡(2πi v(a)v(b)/2(j+1)w)\exp(2\pi\mathrm{i}\,v(a)v(b)/2^{(j+1)w})exp(2πiv(a)v(b)/2(j+1)w). A swap step has the same row position and entry 111. A truncated phase-swap step with half-width www, phase index jjj, and cutoff CCC has the same row position and entry

exp⁡ ⁣(i∑p=0w−1∑q=0w−1{π/2(j−1)w+1+p+q,ap=bq=1 and (j−1)w+1+p+q≤C,0,otherwise.).\exp\!\left(\mathrm{i}\sum_{p=0}^{w-1}\sum_{q=0}^{w-1} \begin{cases} \pi/2^{(j-1)w+1+p+q}, & a_p=b_q=1\ \text{and }(j-1)w+1+p+q\le C,\\ 0,&\text{otherwise}. \end{cases}\right).exp(ip=0∑w−1​q=0∑w−1​{π/2(j−1)w+1+p+q,0,​ap​=bq​=1 and (j−1)w+1+p+q≤C,otherwise.​).

Here j−1j-1j−1 uses natural-number subtraction, so it is zero when j=0j=0j=0. All offsets, widths, phase indices, and cutoffs may be zero; a zero-width block has one empty bit string, empty sums are zero, and n=0n=0n=0 is included.

QAlgorithms.Papers.Independent.Intakethird100.P39.blockQuality

For arbitrary real numbers Fq,T,TRF_q,T,T_RFq​,T,TR​, an arbitrary function Fc:N→RF_c:\mathbb N\to\mathbb RFc​:N→R, and any finite list ccc of block steps, this defines

blockQuality⁡(Fq,Fc,T,TR,c)=(∏s in cw(s))e−T/TR,w(s)={Fqs is a Fourier step,Fc(j)s is a phase-swap step with phase index j,Fc(j)s is a truncated phase-swap step with phase index j,Fc(3)s is a swap step.\operatorname{blockQuality}(F_q,F_c,T,T_R,c) =\left(\prod_{s\text{ in }c}w(s)\right)e^{-T/T_R}, \qquad w(s)= \begin{cases} F_q & s\text{ is a Fourier step},\\ F_c(j) & s\text{ is a phase-swap step with phase index }j,\\ F_c(j) & s\text{ is a truncated phase-swap step with phase index }j,\\ F_c(3) & s\text{ is a swap step}. \end{cases}blockQuality(Fq​,Fc​,T,TR​,c)=(s in c∏​w(s))e−T/TR​,w(s)=⎩⎨⎧​Fq​Fc​(j)Fc​(j)Fc​(3)​s is a Fourier step,s is a phase-swap step with phase index j,s is a truncated phase-swap step with phase index j,s is a swap step.​

The offsets, widths, and truncation cutoffs do not affect these weights. The product counts every occurrence in the list; an empty list has product 111. There are no sign, size, or positivity restrictions on the real parameters or weights. In particular, TR=0T_R=0TR​=0 is allowed, and real division by zero is defined to be zero, making the exponential factor 111 in that case.

QAlgorithms.Papers.Independent.Intakethird100.P39.blockSwap

For every natural number mmm, this defines a complex matrix indexed by Boolean strings y,xy,xy,x of length 2m2m2m, with entry 111 if

yi={xm+i,0≤i<m,xi−m,m≤i<2m,y_i= \begin{cases} x_{m+i},&0\le i<m,\\ x_{i-m},&m\le i<2m, \end{cases}yi​={xm+i​,xi−m​,​0≤i<m,m≤i<2m,​

and entry 000 otherwise. Thus each column has its 111 at the row obtained by exchanging the two length-mmm halves of the column string. For m=0m=0m=0, the row and column index sets each contain only the empty string, and the matrix has the single entry 111.

QAlgorithms.Papers.Independent.Intakethird100.P39.cpSwap

For all natural numbers m,jm,jm,j, this defines a complex matrix Cm,jC_{m,j}Cm,j​ whose rows and columns are indexed by Boolean strings of length 2m2m2m. For such a string xxx, let

A(x)=∑a=0m−11{xa=true} 2m−1−a,B(x)=∑b=0m−11{xm+b=true} 2m−1−b,A(x)=\sum_{a=0}^{m-1}\mathbf1_{\{x_a=\mathrm{true}\}}\,2^{m-1-a}, \qquad B(x)=\sum_{b=0}^{m-1}\mathbf1_{\{x_{m+b}=\mathrm{true}\}}\,2^{m-1-b},A(x)=a=0∑m−1​1{xa​=true}​2m−1−a,B(x)=b=0∑m−1​1{xm+b​=true}​2m−1−b,

so these are the natural numbers encoded by the first and second halves, with their first coordinates having the largest binary weights. Let Em(x)E_m(x)Em​(x) be the string with coordinates xm+ix_{m+i}xm+i​ for i<mi<mi<m and xi−mx_{i-m}xi−m​ for m≤i<2mm\le i<2mm≤i<2m. Then, for every row yyy and column xxx,

(Cm,j)y,x={exp⁡ ⁣(2πi A(x)B(x)2(j+1)m),y=Em(x),0,y≠Em(x).(C_{m,j})_{y,x}= \begin{cases} \displaystyle \exp\!\left(\frac{2\pi\mathrm i\,A(x)B(x)}{2^{(j+1)m}}\right), &y=E_m(x),\\ 0,&y\ne E_m(x). \end{cases}(Cm,j​)y,x​=⎩⎨⎧​exp(2(j+1)m2πiA(x)B(x)​),0,​y=Em​(x),y=Em​(x).​

There are no positivity assumptions on mmm or jjj; the denominator is always nonzero. When m=0m=0m=0, the sums are empty and the resulting matrix is the 1×11\times11×1 matrix with entry 111.

QAlgorithms.Papers.Independent.Intakethird100.P39.descendingSweep

For every natural number ℓ\ellℓ, including 000, and every Boolean string x=(x0,…,xℓ)x=(x_0,\ldots,x_\ell)x=(x0​,…,xℓ​), this defines a Boolean string by starting with y=xy=xy=x and performing the updates yi+1←yi+1⊕yiy_{i+1}\leftarrow y_{i+1}\mathbin{\oplus}y_iyi+1​←yi+1​⊕yi​ in decreasing order of iii, from ℓ−1\ell-1ℓ−1 down to 000, leaving every other coordinate unchanged at each step. Here ⊕\oplus⊕ is Boolean exclusive-or, and each step uses the current values of yyy. When ℓ=0\ell=0ℓ=0, there are no steps, and the result is xxx.

QAlgorithms.Papers.Independent.Intakethird100.P39.endpointCnot

For every natural number ℓ\ellℓ, including 000, and every Boolean string x=(x0,…,xℓ)x=(x_0,\ldots,x_\ell)x=(x0​,…,xℓ​), this defines the Boolean string yyy by yℓ=xℓ⊕x0y_\ell=x_\ell\mathbin{\oplus}x_0yℓ​=xℓ​⊕x0​ and yi=xiy_i=x_iyi​=xi​ for every i≠ℓi\ne\elli=ℓ, where ⊕\oplus⊕ denotes Boolean exclusive-or. In particular, when ℓ=0\ell=0ℓ=0, the control and target coordinates coincide, so the sole output bit is x0⊕x0=falsex_0\mathbin{\oplus}x_0=\mathrm{false}x0​⊕x0​=false.

QAlgorithms.Papers.Independent.Intakethird100.P39.endpointMatrix

For every natural number ℓ\ellℓ, this defines an (ℓ+1)×(ℓ+1)(\ell+1)\times(\ell+1)(ℓ+1)×(ℓ+1) matrix over Z/2Z\mathbb Z/2\mathbb ZZ/2Z, indexed by 0,…,ℓ0,\ldots,\ell0,…,ℓ, as the identity matrix plus the matrix having entry 111 at row ℓ\ellℓ, column 000, and entry 000 elsewhere:

Eij=1{i=j}+1{i=ℓ, j=0},E_{ij}=\mathbf 1_{\{i=j\}}+\mathbf 1_{\{i=\ell,\ j=0\}},Eij​=1{i=j}​+1{i=ℓ, j=0}​,

with addition taken modulo 222. When ℓ=0\ell=0ℓ=0, the two contributions coincide, so the resulting one-entry matrix is [0][0][0].

QAlgorithms.Papers.Independent.Intakethird100.P39.exchangeHalves

For every natural number mmm and every Boolean string xxx of length 2m2m2m, this defines a Boolean string Em(x)E_m(x)Em​(x) of the same length by

(Em(x))i={xm+i,0≤i<m,xi−m,m≤i<2m.(E_m(x))_i= \begin{cases} x_{m+i},&0\le i<m,\\ x_{i-m},&m\le i<2m. \end{cases}(Em​(x))i​={xm+i​,xi−m​,​0≤i<m,m≤i<2m.​

Thus the last mmm coordinates become the first mmm coordinates, and the first mmm become the last mmm. The subtraction in the second case is natural-number subtraction, and its branch condition ensures that i≥mi\ge mi≥m. For m=0m=0m=0, the function sends the unique empty string to itself.

QAlgorithms.Papers.Independent.Intakethird100.P39.firstHalf

For every natural number mmm and every Boolean string xxx indexed by 0,…,2m−10,\ldots,2m-10,…,2m−1, this defines a Boolean string of length mmm whose coordinate at each 0≤i<m0\le i<m0≤i<m is xix_ixi​. Thus its coordinates are (x0,…,xm−1)(x_0,\ldots,x_{m-1})(x0​,…,xm−1​). When m=0m=0m=0, both the input and output are the unique empty Boolean string.

QAlgorithms.Papers.Independent.Intakethird100.P39.fittedInteriorCost

For any natural numbers k,mk,mk,m, this defines the real number

C(k,m)=(13k−8)((m+2)215100−448100)+{0m=1,1m≠1(13k+3)(m69100+6100).C(k,m) =(13k-8)\left((m+2)\frac{215}{100}-\frac{448}{100}\right) +\begin{cases}0&m=1,\\1&m\ne1\end{cases} (13k+3)\left(m\frac{69}{100}+\frac{6}{100}\right).C(k,m)=(13k−8)((m+2)100215​−100448​)+{01​m=1,m=1​(13k+3)(m10069​+1006​).

Here kkk and mmm are converted to real numbers before the displayed arithmetic is performed, so the subtractions are ordinary real subtractions. Both arguments may be zero; the second summand is suppressed exactly when m=1m=1m=1, including no suppression when m=0m=0m=0.

QAlgorithms.Papers.Independent.Intakethird100.P39.fourierCount

For every finite list ccc of block-step records, this defines a natural number equal to the number of occurrences of Fourier-step records in ccc, irrespective of their offsets or widths. Phase-swap, truncated phase-swap, and swap records contribute zero. Repeated occurrences are counted separately, zero-width Fourier steps are counted, and the empty list has count zero.

QAlgorithms.Papers.Independent.Intakethird100.P39.halfBlockExpansion

For every block-step record sss, this defines a list as follows: if sss is a phase-swap record with offset ooo, half-width www, and phase index jjj, put h=⌊w/2⌋h=\lfloor w/2\rfloorh=⌊w/2⌋ and return, in the displayed order, the four phase-swap records

(o+h,h,2j),(o,h,2j−1),(o+2h,h,2j+1),(o+h,h,2j),(o+h,h,2j),\qquad (o,h,2j-1),\qquad (o+2h,h,2j+1),\qquad (o+h,h,2j),(o+h,h,2j),(o,h,2j−1),(o+2h,h,2j+1),(o+h,h,2j),

where each triple specifies offset, half-width, and phase index. Subtraction is natural-number subtraction, so 2j−1=02j-1=02j−1=0 when j=0j=0j=0. There is no requirement that www be even or positive: odd widths are divided with rounding down, and w=0w=0w=0 or w=1w=1w=1 gives h=0h=0h=0 while still returning all four records. If sss is a Fourier, swap, or truncated phase-swap record, the result is the singleton list containing sss unchanged.

QAlgorithms.Papers.Independent.Intakethird100.P39.jordanMatrix

For any type RRR equipped with a designated element 0R0_R0R​ and a designated element 1R1_R1R​, and any natural number ℓ\ellℓ, this defines an (ℓ+1)×(ℓ+1)(\ell+1)\times(\ell+1)(ℓ+1)×(ℓ+1) matrix with entries in RRR, whose rows and columns are indexed by 0,…,ℓ0,\ldots,\ell0,…,ℓ, by

Jij={1Ri=j or i=j+1,0Rotherwise.J_{ij}= \begin{cases} 1_R&i=j\ \text{or }i=j+1,\\ 0_R&\text{otherwise}. \end{cases}Jij​={1R​0R​​i=j or i=j+1,otherwise.​

Thus the main diagonal and the immediately lower diagonal receive 1R1_R1R​. No algebraic laws or distinction between 0R0_R0R​ and 1R1_R1R​ are assumed. The case ℓ=0\ell=0ℓ=0 is included and gives the one-entry matrix [1R][1_R][1R​].

QAlgorithms.Papers.Independent.Intakethird100.P39.marginalDemand

For arbitrary natural numbers n,mn,mn,m, define the real number

marginalDemand⁡(n,m)=4n+1−2⋅4 m−⌊m/2⌋−⌊nm⌋(4m−1).\operatorname{marginalDemand}(n,m) = 4^n+1-2\cdot4^{\,m-\lfloor m/2\rfloor} -\left\lfloor\frac nm\right\rfloor(4^m-1).marginalDemand(n,m)=4n+1−2⋅4m−⌊m/2⌋−⌊mn​⌋(4m−1).

The quotient n/mn/mn/m is computed in the natural numbers before being converted to a real number, and m−⌊m/2⌋m-\lfloor m/2\rfloorm−⌊m/2⌋ is natural-number subtraction, truncated at zero. Natural-number division by zero is zero, so the definition includes m=0m=0m=0 and gives marginalDemand⁡(n,0)=4n−1\operatorname{marginalDemand}(n,0)=4^n-1marginalDemand(n,0)=4n−1. The additions and subtractions outside exponents are real arithmetic, and the value need not be nonnegative.

QAlgorithms.Papers.Independent.Intakethird100.P39.marginalGateBound

For every pair of natural numbers n,mn,mn,m, this defines an integer G(n,m)G(n,m)G(n,m) by

G(n,m)=⌈4n+1−2⋅4m−h−q(4m−1)4m−4h−4m−h+1⌉+q,h=⌊m2⌋,G(n,m)= \left\lceil \frac{4^n+1-2\cdot4^{m-h}-q(4^m-1)} {4^m-4^h-4^{m-h}+1} \right\rceil+q, \qquad h=\left\lfloor\frac m2\right\rfloor,G(n,m)=⌈4m−4h−4m−h+14n+1−2⋅4m−h−q(4m−1)​⌉+q,h=⌊2m​⌋,

where qqq is natural-number division n/mn/mn/m: it equals ⌊n/m⌋\lfloor n/m\rfloor⌊n/m⌋ when m>0m>0m>0, and equals 000 when m=0m=0m=0. The fraction is evaluated in R\mathbb RR, and the ceiling is the least integer greater than or equal to that real number. Division by zero in this formula is defined to give zero. There are no positivity or divisibility assumptions on n,mn,mn,m; in particular, G(n,0)=0G(n,0)=0G(n,0)=0 and G(n,1)=nG(n,1)=nG(n,1)=n, since the denominator is zero in both cases.

QAlgorithms.Papers.Independent.Intakethird100.P39.marginalIncrement

For every natural number mmm, define the real number

marginalIncrement⁡(m)=4m−4⌊m/2⌋−4 m−⌊m/2⌋+1.\operatorname{marginalIncrement}(m) = 4^m-4^{\lfloor m/2\rfloor} -4^{\,m-\lfloor m/2\rfloor}+1.marginalIncrement(m)=4m−4⌊m/2⌋−4m−⌊m/2⌋+1.

Here m/2m/2m/2 is natural-number division, and the subtraction in the exponent is natural-number subtraction truncated at zero; the other subtractions are real arithmetic. The definition includes m=0m=0m=0 and m=1m=1m=1, for both of which its value is zero.

QAlgorithms.Papers.Independent.Intakethird100.P39.marginalSlotBound

For every pair of natural numbers n,mn,mn,m, this defines an integer S(n,m)S(n,m)S(n,m) by

S(n,m)=1+⌈4n+1−2⋅4m−h−q(4m−1)q(4m−4h−4m−h+1)⌉,h=⌊m2⌋,S(n,m)=1+ \left\lceil \frac{4^n+1-2\cdot4^{m-h}-q(4^m-1)} {q\bigl(4^m-4^h-4^{m-h}+1\bigr)} \right\rceil, \qquad h=\left\lfloor\frac m2\right\rfloor,S(n,m)=1+⌈q(4m−4h−4m−h+1)4n+1−2⋅4m−h−q(4m−1)​⌉,h=⌊2m​⌋,

where qqq is natural-number division n/mn/mn/m, with q=0q=0q=0 when m=0m=0m=0. The fraction is evaluated in R\mathbb RR, division by zero gives zero, and the ceiling takes its value in Z\mathbb ZZ. All natural n,mn,mn,m, including zero, are allowed. In particular, the value is 111 whenever q=0q=0q=0 or m∈{0,1}m\in\{0,1\}m∈{0,1}, so it is 111 when n<mn<mn<m and when n=0n=0n=0.

QAlgorithms.Papers.Independent.Intakethird100.P39.panelA

This defines a list of 252525 phase-swap steps. In order, take b=0,1,2,3,4b=0,1,2,3,4b=0,1,2,3,4, and for each fixed bbb, take a=4,3,2,1,0a=4,3,2,1,0a=4,3,2,1,0; append the phase-swap step whose offset is a+ba+ba+b, whose half-width is 111, and whose phase index is 1+a+b1+a+b1+a+b. These are ordered list entries, so repeated offsets or phase indices remain separate occurrences.

QAlgorithms.Papers.Independent.Intakethird100.P39.panelB

This defines a list of 252525 block steps, ordered by b=0,1,2,3,4b=0,1,2,3,4b=0,1,2,3,4 and, within each fixed bbb, by a=4,3,2,1,0a=4,3,2,1,0a=4,3,2,1,0. The entry for (a,b)(a,b)(a,b) is a phase-swap step of offset a+ba+ba+b, half-width 111, and phase index 6+a+b6+a+b6+a+b if 6+a+b≤106+a+b\le106+a+b≤10, equivalently a+b≤4a+b\le4a+b≤4; otherwise it is a swap step of offset a+ba+ba+b and half-width 111. Thus the equality case a+b=4a+b=4a+b=4 selects a phase-swap step, and every occurrence is retained in the specified order.

QAlgorithms.Papers.Independent.Intakethird100.P39.panelC

This defines a list of 252525 swap steps. In order, take b=0,1,2,3,4b=0,1,2,3,4b=0,1,2,3,4, and for each fixed bbb, take a=4,3,2,1,0a=4,3,2,1,0a=4,3,2,1,0; append the swap step with offset a+ba+ba+b and half-width 111. Repeated offsets are retained as separate list entries.

QAlgorithms.Papers.Independent.Intakethird100.P39.panelStep

For any natural numbers o,p,co,p,co,p,c, representing an offset, angle power, and cutoff, respectively, this defines a block-step record: it is a phase-swap step with offset ooo, half-width 111, and phase index ppp when p≤cp\le cp≤c, and a swap step with offset ooo and half-width 111 when p>cp>cp>c. Equality with the cutoff therefore selects the phase-swap branch. All three arguments may be zero, and the definition imposes no condition that the step fit within any particular register.

QAlgorithms.Papers.Independent.Intakethird100.P39.phaseCount

For every natural number jjj and finite list ccc of block-step records, this defines the number of occurrences in ccc of phase-swap records whose phase index equals jjj, irrespective of their offsets or half-widths. Fourier, swap, and truncated phase-swap records contribute zero, including truncated phase-swap records whose phase index is jjj. Repeated occurrences are counted separately; j=0j=0j=0 and zero half-widths are allowed, and the empty list has count zero.

QAlgorithms.Papers.Independent.Intakethird100.P39.phaseTotal

For every finite list ccc of block-step records, this defines the number of occurrences of phase-swap records in ccc, regardless of their offsets, half-widths, or phase indices. Fourier, swap, and truncated phase-swap records contribute zero. Repeated occurrences and phase-swap records with zero half-width or zero phase index are counted, and the empty list has count zero.

QAlgorithms.Papers.Independent.Intakethird100.P39.placeBlock

For every natural number rrr, every complex matrix AAA indexed by Boolean strings of length rrr, and all natural numbers n,on,on,o, this defines a complex matrix PPP indexed by Boolean strings of length nnn. If o+r≤no+r\le no+r≤n, define xi[o,r]=xo+ix^{[o,r]}_i=x_{o+i}xi[o,r]​=xo+i​ for 0≤i<r0\le i<r0≤i<r; then

Py,x={Ay[o,r],x[o,r],ya=xa for every a∈{0,…,n−1}∖{o,…,o+r−1},0,otherwise.P_{y,x}= \begin{cases} A_{y^{[o,r]},x^{[o,r]}}, &y_a=x_a\text{ for every }a\in\{0,\ldots,n-1\} \setminus\{o,\ldots,o+r-1\},\\ 0,&\text{otherwise}. \end{cases}Py,x​={Ay[o,r],x[o,r]​,0,​ya​=xa​ for every a∈{0,…,n−1}∖{o,…,o+r−1},otherwise.​

If o+r>no+r>no+r>n, then PPP is the identity matrix, with entry 111 when y=xy=xy=x and 000 otherwise. No unitarity assumption is imposed on AAA. Zero sizes are allowed: when r=0r=0r=0 and o≤no\le no≤n, the result is the identity matrix multiplied by the sole entry of AAA; when r=0r=0r=0 and o>no>no>n, it is the identity regardless of that entry.

QAlgorithms.Papers.Independent.Intakethird100.P39.qftAssembly

For any natural numbers mmm and kkk, this defines the list obtained by concatenating, in increasing order of t=0,1,…,k−1t=0,1,\ldots,k-1t=0,1,…,k−1, the following lists: phase-swap steps with offset smsmsm, half-width mmm, and phase index s+1s+1s+1, in decreasing order s=t−1,…,0s=t-1,\ldots,0s=t−1,…,0, followed by a Fourier step with offset 000 and width mmm. For t=0t=0t=0, that constituent list contains just the Fourier step. When k=0k=0k=0, the entire list is empty; when k=1k=1k=1, it contains just one Fourier step. Zero is also allowed for mmm, producing zero offsets and widths without removing any records.

QAlgorithms.Papers.Independent.Intakethird100.P39.qftAssemblyII

For any natural numbers m,km,km,k, put h=⌊m/2⌋h=\lfloor m/2\rfloorh=⌊m/2⌋. This defines a list by taking t=0,…,k−1t=0,\ldots,k-1t=0,…,k−1 in increasing order and, for each ttt, taking s=t−1,…,0s=t-1,\ldots,0s=t−1,…,0 in decreasing order, appending the four phase-swap records

(sm+h,h,2s+2),(sm,h,2s+1),(sm+2h,h,2s+3),(sm+h,h,2s+2)(sm+h,h,2s+2),\qquad (sm,h,2s+1),\qquad (sm+2h,h,2s+3),\qquad (sm+h,h,2s+2)(sm+h,h,2s+2),(sm,h,2s+1),(sm+2h,h,2s+3),(sm+h,h,2s+2)

for each such sss, and then appending one Fourier record with offset 000 and width mmm. Each phase-swap triple specifies offset, half-width, and phase index. When t=0t=0t=0, only the Fourier record is appended. No positivity or evenness assumption is imposed on mmm: its division by two rounds down, and m=0m=0m=0 or m=1m=1m=1 gives zero half-widths for all generated phase-swap records. When k=0k=0k=0, the list is empty; when k=1k=1k=1, it consists solely of the Fourier record.

QAlgorithms.Papers.Independent.Intakethird100.P39.qftInsertion

For any natural numbers mmm and ttt, this defines a list of block-step records consisting, in order, of a phase-swap step with offset smsmsm, half-width mmm, and phase index s+1s+1s+1 for each s=t−1,t−2,…,0s=t-1,t-2,\ldots,0s=t−1,t−2,…,0, followed by one Fourier step with offset 000 and width mmm. When t=0t=0t=0, there are no phase-swap steps and the list consists solely of that Fourier step. The value m=0m=0m=0 is allowed: all offsets and widths are then zero, while the phase indices remain t,t−1,…,1t,t-1,\ldots,1t,t−1,…,1.

QAlgorithms.Papers.Independent.Intakethird100.P39.quality

For arbitrary real numbers F,T,TRF,T,T_RF,T,TR​, define

quality⁡(F,T,TR)=Fexp⁡ ⁣(−TTR).\operatorname{quality}(F,T,T_R)=F\exp\!\left(\frac{-T}{T_R}\right).quality(F,T,TR​)=Fexp(TR​−T​).

There are no sign or nonzero assumptions on these arguments. Division is total, with division by zero equal to zero, so quality⁡(F,T,0)=F\operatorname{quality}(F,T,0)=Fquality(F,T,0)=F for every F,TF,TF,T.

QAlgorithms.Papers.Independent.Intakethird100.P39.secondHalf

For every natural number mmm and every Boolean string xxx indexed by 0,…,2m−10,\ldots,2m-10,…,2m−1, this defines a Boolean string of length mmm whose coordinate at each 0≤i<m0\le i<m0≤i<m is xm+ix_{m+i}xm+i​. Thus its coordinates are (xm,…,x2m−1)(x_m,\ldots,x_{2m-1})(xm​,…,x2m−1​). When m=0m=0m=0, both the input and output are the unique empty Boolean string.

QAlgorithms.Papers.Independent.Intakethird100.P39.specialUnitaryTangent

For every type ι\iotaι equipped with a finite enumeration, define the real vector subspace of complex matrices indexed by ι\iotaι to be

{A∈Cι×ι:A∗=−A and tr⁡(A)=0},\left\{A\in\mathbb C^{\iota\times\iota}:A^\ast=-A\ \text{and}\ \operatorname{tr}(A)=0\right\},{A∈Cι×ι:A∗=−A and tr(A)=0},

where A∗A^\astA∗ is the conjugate transpose. Thus membership requires both skew-Hermitian symmetry and zero complex trace, and scalar multiplication in this subspace is by real numbers. The index type may be empty; then the matrix space and this subspace each contain only the zero matrix.

QAlgorithms.Papers.Independent.Intakethird100.P39.tensorLeftTangent

For arbitrary natural numbers a,ba,ba,b, define a real vector subspace of complex matrices indexed by {0,…,a−1}×{0,…,b−1}\{0,\ldots,a-1\}\times\{0,\ldots,b-1\}{0,…,a−1}×{0,…,b−1} as

span⁡R{A⊗Ib:A∈Ca×a, A∗=−A, tr⁡(A)=0}.\operatorname{span}_{\mathbb R} \left\{A\otimes I_b: A\in\mathbb C^{a\times a},\ A^\ast=-A,\ \operatorname{tr}(A)=0\right\}.spanR​{A⊗Ib​:A∈Ca×a, A∗=−A, tr(A)=0}.

Here IbI_bIb​ is the b×bb\times bb×b identity matrix, ⊗\otimes⊗ is the Kronecker product, and the real span consists of finite real linear combinations of the displayed matrices. Either aaa or bbb may be zero; in either case the product index set is empty and the resulting subspace contains only the zero matrix.

QAlgorithms.Papers.Independent.Intakethird100.P39.tensorRightTangent

For arbitrary natural numbers a,ba,ba,b, define a real vector subspace of complex matrices indexed by {0,…,a−1}×{0,…,b−1}\{0,\ldots,a-1\}\times\{0,\ldots,b-1\}{0,…,a−1}×{0,…,b−1} as

span⁡R{Ia⊗B:B∈Cb×b, B∗=−B, tr⁡(B)=0}.\operatorname{span}_{\mathbb R} \left\{I_a\otimes B: B\in\mathbb C^{b\times b},\ B^\ast=-B,\ \operatorname{tr}(B)=0\right\}.spanR​{Ia​⊗B:B∈Cb×b, B∗=−B, tr(B)=0}.

Here IaI_aIa​ is the a×aa\times aa×a identity matrix, ⊗\otimes⊗ is the Kronecker product, and the real span consists of finite real linear combinations of the displayed matrices. Either aaa or bbb may be zero; in either case the product index set is empty and the resulting subspace contains only the zero matrix.

QAlgorithms.Papers.Independent.Intakethird100.P39.traceFidelity

For every natural number nnn and arbitrary complex matrices U,VU,VU,V indexed by the set Qn={0,1}nQ_n=\{0,1\}^nQn​={0,1}n of length-nnn bit strings, define

traceFidelity⁡(U,V)=Re⁡tr⁡(V∗U)2n,\operatorname{traceFidelity}(U,V)=\frac{\operatorname{Re}\operatorname{tr}(V^\ast U)}{2^n},traceFidelity(U,V)=2nRetr(V∗U)​,

where V∗V^\astV∗ is the conjugate transpose of VVV. No unitarity assumption is imposed. The definition includes n=0n=0n=0, when Q0Q_0Q0​ contains one empty bit string and the matrices are 1×11\times11×1.

QAlgorithms.Papers.Independent.Intakethird100.P39.truncateBlockStep

For every natural-number cutoff CCC and block-step record sss, this defines a new record as follows: if sss is a phase-swap record with offset ooo, half-width www, and phase index jjj, it becomes a swap record with offset ooo and half-width www when C<(j−1)w+1C<(j-1)w+1C<(j−1)w+1, and otherwise becomes a truncated phase-swap record with offset ooo, half-width www, phase index jjj, and cutoff CCC. The subtraction j−1j-1j−1 is natural-number subtraction, so the threshold is 111 when j=0j=0j=0; it is also 111 whenever w=0w=0w=0. In particular, cutoff zero turns every phase-swap record into a swap record. Fourier, swap, and already truncated phase-swap records are returned unchanged, including their existing parameters.

QAlgorithms.Papers.Independent.Intakethird100.P39.truncatedCpSwap

For all natural numbers m,j,cm,j,cm,j,c, where ccc is the cutoff, this defines a complex matrix Tm,j,cT_{m,j,c}Tm,j,c​ indexed by Boolean strings of length 2m2m2m. Write d=max⁡(j−1,0)d=\max(j-1,0)d=max(j−1,0), reflecting natural-number subtraction, and for 0≤a,b<m0\le a,b<m0≤a,b<m set ea,b=dm+1+a+be_{a,b}=dm+1+a+bea,b​=dm+1+a+b. For a column string xxx, define the real number

Φm,j,c(x)=∑a=0m−1∑b=0m−1{π2ea,b,xa=true, xm+b=true, ea,b≤c,0,otherwise.\Phi_{m,j,c}(x)= \sum_{a=0}^{m-1}\sum_{b=0}^{m-1} \begin{cases} \displaystyle\frac{\pi}{2^{e_{a,b}}}, &x_a=\mathrm{true},\ x_{m+b}=\mathrm{true},\ e_{a,b}\le c,\\ 0,&\text{otherwise}. \end{cases}Φm,j,c​(x)=a=0∑m−1​b=0∑m−1​{2ea,b​π​,0,​xa​=true, xm+b​=true, ea,b​≤c,otherwise.​

Let Em(x)E_m(x)Em​(x) have coordinates xm+ix_{m+i}xm+i​ for i<mi<mi<m and xi−mx_{i-m}xi−m​ for m≤i<2mm\le i<2mm≤i<2m. The matrix entries are

(Tm,j,c)y,x={exp⁡ ⁣(i Φm,j,c(x)),y=Em(x),0,y≠Em(x).(T_{m,j,c})_{y,x}= \begin{cases} \exp\!\bigl(\mathrm i\,\Phi_{m,j,c}(x)\bigr),&y=E_m(x),\\ 0,&y\ne E_m(x). \end{cases}(Tm,j,c​)y,x​={exp(iΦm,j,c​(x)),0,​y=Em​(x),y=Em​(x).​

All natural values are allowed, including j=0j=0j=0, for which d=0d=0d=0. For c=0c=0c=0, every phase summand vanishes, so the entries are 111 exactly when y=Em(x)y=E_m(x)y=Em​(x). For m=0m=0m=0, the matrix is the 1×11\times11×1 matrix with entry 111.

QAlgorithms.Papers.Independent.Intakethird100.P39.truncatedQftAssembly

For any natural numbers m,k,Cm,k,Cm,k,C, this defines a list by taking t=0,…,k−1t=0,\ldots,k-1t=0,…,k−1 in increasing order and, within each ttt, taking s=t−1,…,0s=t-1,\ldots,0s=t−1,…,0 in decreasing order: at each such sss, append a swap record with offset smsmsm and half-width mmm if C<sm+1C<sm+1C<sm+1, and otherwise append a truncated phase-swap record with offset smsmsm, half-width mmm, phase index s+1s+1s+1, and cutoff CCC; after these records for each ttt, append a Fourier record with offset 000 and width mmm. For t=0t=0t=0, only the Fourier record is appended. When k=0k=0k=0, the list is empty; when k=1k=1k=1, it consists solely of the Fourier record. Cutoff zero selects the swap case at every phase position. The value m=0m=0m=0 is also allowed: all offsets and widths are then zero, and every phase position selects the swap case for C=0C=0C=0 and the truncated phase-swap case for C≥1C\ge1C≥1.

Human review
  • Endorsed by Shuze Chen · Nov 1, 2026

    Confirmed by the moderator at approval.

  • Endorsed by Gohan · Nov 1, 2026

    Confirmed by the mission captain (proposal self-audit).

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paper
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me