Definitions for Quantum CISC Compilation by Optimal Control and Scalable Assembly of Complex Instruction Sets beyond Two-Qubit Gates
DefinitionQAlgorithms_Papers_Independent_Intakethird100_P39The 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.
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
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 , this assigns a complex matrix indexed by length- Boolean strings to each of four forms of step, whose parameters are arbitrary natural numbers: fourier , phaseSwap , swap , and truncatedPhaseSwap . The local matrix for a fourier step has row and column strings of length and entries
For the other three forms, the local strings have length ; let exchange their two halves, so for and for , and put
The local phaseSwap matrix has entry when , and zero otherwise; the local swap matrix has entry when , and zero otherwise; the local truncatedPhaseSwap matrix has entry when , and zero otherwise, where, writing ,
To obtain the assigned matrix, take the local width for fourier and for the other forms. If , its entry is the local matrix entry on the substrings and , provided and agree at every position outside that interval, and is zero otherwise. If , the assigned matrix is the identity. Empty sums are zero; width or half-width gives the identity matrix, and the truncated phase formula permits using .
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 , let be the real vector space of complex matrices indexed by satisfying and , and let
The abbreviation denotes the real vector space : two elements of represent the same class exactly when their difference belongs to . Here denotes conjugate transpose and denotes the Kronecker product. Zero values of or are allowed and give the zero quotient space.
QAlgorithms.Papers.Independent.Intakethird100.P39.TwoFactorQuotient
For arbitrary natural numbers , let be the real vector space of complex matrices indexed by satisfying and , and set
The abbreviation denotes the real vector space : two elements of represent the same class exactly when their difference belongs to the sum of subspaces . Here denotes conjugate transpose and denotes the Kronecker product. Zero values of or are allowed and give the zero quotient space.
QAlgorithms.Papers.Independent.Intakethird100.P39.ascendingSweep
For every natural number , including , and every Boolean string , this defines a Boolean string by starting with and, successively for , replacing by , leaving every other coordinate unchanged at that step. Here is Boolean exclusive-or, and each step uses the current values of , including changes made by previous steps. When , there are no steps, and the result is .
QAlgorithms.Papers.Independent.Intakethird100.P39.bitCnot
For every natural number , every control index and target index in , and every bit string , this defines the bit string by
where is exclusive-or and the control bit is read from the original string . The control and target need not be distinct: if , the target becomes false. Although 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 and finite list of block steps, this defines a complex matrix indexed by the length- bit strings as the reverse-ordered product , where is the matrix assigned to ; 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 positions, the assigned matrix is instead the identity. More explicitly, a local matrix on bits, placed at offset with , has entry when and agree outside that block, and entry zero otherwise. For a bit string of length , write , interpreting bits as or . A Fourier step of width uses the local matrix with entry
For a phase-swap step of half-width and phase index , write the input as , with each half of length ; its only nonzero entry in column is at row , and equals . A swap step has the same row position and entry . A truncated phase-swap step with half-width , phase index , and cutoff has the same row position and entry
Here uses natural-number subtraction, so it is zero when . All offsets, widths, phase indices, and cutoffs may be zero; a zero-width block has one empty bit string, empty sums are zero, and is included.
QAlgorithms.Papers.Independent.Intakethird100.P39.blockQuality
For arbitrary real numbers , an arbitrary function , and any finite list of block steps, this defines
The offsets, widths, and truncation cutoffs do not affect these weights. The product counts every occurrence in the list; an empty list has product . There are no sign, size, or positivity restrictions on the real parameters or weights. In particular, is allowed, and real division by zero is defined to be zero, making the exponential factor in that case.
QAlgorithms.Papers.Independent.Intakethird100.P39.blockSwap
For every natural number , this defines a complex matrix indexed by Boolean strings of length , with entry if
and entry otherwise. Thus each column has its at the row obtained by exchanging the two length- halves of the column string. For , the row and column index sets each contain only the empty string, and the matrix has the single entry .
QAlgorithms.Papers.Independent.Intakethird100.P39.cpSwap
For all natural numbers , this defines a complex matrix whose rows and columns are indexed by Boolean strings of length . For such a string , let
so these are the natural numbers encoded by the first and second halves, with their first coordinates having the largest binary weights. Let be the string with coordinates for and for . Then, for every row and column ,
There are no positivity assumptions on or ; the denominator is always nonzero. When , the sums are empty and the resulting matrix is the matrix with entry .
QAlgorithms.Papers.Independent.Intakethird100.P39.descendingSweep
For every natural number , including , and every Boolean string , this defines a Boolean string by starting with and performing the updates in decreasing order of , from down to , leaving every other coordinate unchanged at each step. Here is Boolean exclusive-or, and each step uses the current values of . When , there are no steps, and the result is .
QAlgorithms.Papers.Independent.Intakethird100.P39.endpointCnot
For every natural number , including , and every Boolean string , this defines the Boolean string by and for every , where denotes Boolean exclusive-or. In particular, when , the control and target coordinates coincide, so the sole output bit is .
QAlgorithms.Papers.Independent.Intakethird100.P39.endpointMatrix
For every natural number , this defines an matrix over , indexed by , as the identity matrix plus the matrix having entry at row , column , and entry elsewhere:
with addition taken modulo . When , the two contributions coincide, so the resulting one-entry matrix is .
QAlgorithms.Papers.Independent.Intakethird100.P39.exchangeHalves
For every natural number and every Boolean string of length , this defines a Boolean string of the same length by
Thus the last coordinates become the first coordinates, and the first become the last . The subtraction in the second case is natural-number subtraction, and its branch condition ensures that . For , the function sends the unique empty string to itself.
QAlgorithms.Papers.Independent.Intakethird100.P39.firstHalf
For every natural number and every Boolean string indexed by , this defines a Boolean string of length whose coordinate at each is . Thus its coordinates are . When , both the input and output are the unique empty Boolean string.
QAlgorithms.Papers.Independent.Intakethird100.P39.fittedInteriorCost
For any natural numbers , this defines the real number
Here and 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 , including no suppression when .
QAlgorithms.Papers.Independent.Intakethird100.P39.fourierCount
For every finite list of block-step records, this defines a natural number equal to the number of occurrences of Fourier-step records in , 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 , this defines a list as follows: if is a phase-swap record with offset , half-width , and phase index , put and return, in the displayed order, the four phase-swap records
where each triple specifies offset, half-width, and phase index. Subtraction is natural-number subtraction, so when . There is no requirement that be even or positive: odd widths are divided with rounding down, and or gives while still returning all four records. If is a Fourier, swap, or truncated phase-swap record, the result is the singleton list containing unchanged.
QAlgorithms.Papers.Independent.Intakethird100.P39.jordanMatrix
For any type equipped with a designated element and a designated element , and any natural number , this defines an matrix with entries in , whose rows and columns are indexed by , by
Thus the main diagonal and the immediately lower diagonal receive . No algebraic laws or distinction between and are assumed. The case is included and gives the one-entry matrix .
QAlgorithms.Papers.Independent.Intakethird100.P39.marginalDemand
For arbitrary natural numbers , define the real number
The quotient is computed in the natural numbers before being converted to a real number, and is natural-number subtraction, truncated at zero. Natural-number division by zero is zero, so the definition includes and gives . 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 , this defines an integer by
where is natural-number division : it equals when , and equals when . The fraction is evaluated in , 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 ; in particular, and , since the denominator is zero in both cases.
QAlgorithms.Papers.Independent.Intakethird100.P39.marginalIncrement
For every natural number , define the real number
Here 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 and , for both of which its value is zero.
QAlgorithms.Papers.Independent.Intakethird100.P39.marginalSlotBound
For every pair of natural numbers , this defines an integer by
where is natural-number division , with when . The fraction is evaluated in , division by zero gives zero, and the ceiling takes its value in . All natural , including zero, are allowed. In particular, the value is whenever or , so it is when and when .
QAlgorithms.Papers.Independent.Intakethird100.P39.panelA
This defines a list of phase-swap steps. In order, take , and for each fixed , take ; append the phase-swap step whose offset is , whose half-width is , and whose phase index is . 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 block steps, ordered by and, within each fixed , by . The entry for is a phase-swap step of offset , half-width , and phase index if , equivalently ; otherwise it is a swap step of offset and half-width . Thus the equality case 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 swap steps. In order, take , and for each fixed , take ; append the swap step with offset and half-width . Repeated offsets are retained as separate list entries.
QAlgorithms.Papers.Independent.Intakethird100.P39.panelStep
For any natural numbers , representing an offset, angle power, and cutoff, respectively, this defines a block-step record: it is a phase-swap step with offset , half-width , and phase index when , and a swap step with offset and half-width when . 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 and finite list of block-step records, this defines the number of occurrences in of phase-swap records whose phase index equals , 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 . Repeated occurrences are counted separately; and zero half-widths are allowed, and the empty list has count zero.
QAlgorithms.Papers.Independent.Intakethird100.P39.phaseTotal
For every finite list of block-step records, this defines the number of occurrences of phase-swap records in , 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 , every complex matrix indexed by Boolean strings of length , and all natural numbers , this defines a complex matrix indexed by Boolean strings of length . If , define for ; then
If , then is the identity matrix, with entry when and otherwise. No unitarity assumption is imposed on . Zero sizes are allowed: when and , the result is the identity matrix multiplied by the sole entry of ; when and , it is the identity regardless of that entry.
QAlgorithms.Papers.Independent.Intakethird100.P39.qftAssembly
For any natural numbers and , this defines the list obtained by concatenating, in increasing order of , the following lists: phase-swap steps with offset , half-width , and phase index , in decreasing order , followed by a Fourier step with offset and width . For , that constituent list contains just the Fourier step. When , the entire list is empty; when , it contains just one Fourier step. Zero is also allowed for , producing zero offsets and widths without removing any records.
QAlgorithms.Papers.Independent.Intakethird100.P39.qftAssemblyII
For any natural numbers , put . This defines a list by taking in increasing order and, for each , taking in decreasing order, appending the four phase-swap records
for each such , and then appending one Fourier record with offset and width . Each phase-swap triple specifies offset, half-width, and phase index. When , only the Fourier record is appended. No positivity or evenness assumption is imposed on : its division by two rounds down, and or gives zero half-widths for all generated phase-swap records. When , the list is empty; when , it consists solely of the Fourier record.
QAlgorithms.Papers.Independent.Intakethird100.P39.qftInsertion
For any natural numbers and , this defines a list of block-step records consisting, in order, of a phase-swap step with offset , half-width , and phase index for each , followed by one Fourier step with offset and width . When , there are no phase-swap steps and the list consists solely of that Fourier step. The value is allowed: all offsets and widths are then zero, while the phase indices remain .
QAlgorithms.Papers.Independent.Intakethird100.P39.quality
For arbitrary real numbers , define
There are no sign or nonzero assumptions on these arguments. Division is total, with division by zero equal to zero, so for every .
QAlgorithms.Papers.Independent.Intakethird100.P39.secondHalf
For every natural number and every Boolean string indexed by , this defines a Boolean string of length whose coordinate at each is . Thus its coordinates are . When , both the input and output are the unique empty Boolean string.
QAlgorithms.Papers.Independent.Intakethird100.P39.specialUnitaryTangent
For every type equipped with a finite enumeration, define the real vector subspace of complex matrices indexed by to be
where 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 , define a real vector subspace of complex matrices indexed by as
Here is the identity matrix, is the Kronecker product, and the real span consists of finite real linear combinations of the displayed matrices. Either or 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 , define a real vector subspace of complex matrices indexed by as
Here is the identity matrix, is the Kronecker product, and the real span consists of finite real linear combinations of the displayed matrices. Either or 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 and arbitrary complex matrices indexed by the set of length- bit strings, define
where is the conjugate transpose of . No unitarity assumption is imposed. The definition includes , when contains one empty bit string and the matrices are .
QAlgorithms.Papers.Independent.Intakethird100.P39.truncateBlockStep
For every natural-number cutoff and block-step record , this defines a new record as follows: if is a phase-swap record with offset , half-width , and phase index , it becomes a swap record with offset and half-width when , and otherwise becomes a truncated phase-swap record with offset , half-width , phase index , and cutoff . The subtraction is natural-number subtraction, so the threshold is when ; it is also whenever . 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 , where is the cutoff, this defines a complex matrix indexed by Boolean strings of length . Write , reflecting natural-number subtraction, and for set . For a column string , define the real number
Let have coordinates for and for . The matrix entries are
All natural values are allowed, including , for which . For , every phase summand vanishes, so the entries are exactly when . For , the matrix is the matrix with entry .
QAlgorithms.Papers.Independent.Intakethird100.P39.truncatedQftAssembly
For any natural numbers , this defines a list by taking in increasing order and, within each , taking in decreasing order: at each such , append a swap record with offset and half-width if , and otherwise append a truncated phase-swap record with offset , half-width , phase index , and cutoff ; after these records for each , append a Fourier record with offset and width . For , only the Fourier record is appended. When , the list is empty; when , it consists solely of the Fourier record. Cutoff zero selects the swap case at every phase position. The value is also allowed: all offsets and widths are then zero, and every phase position selects the swap case for and the truncated phase-swap case for .
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.