# Quantum Pattern Matching in Generalised Degenerate Strings (`arXiv:2603.16297`) This directory contains a Lean 4 (`v4.36.0`) formalization of Massimo Equi, Md Rabiul Islam Khan, and Veli Mäkinen, *Quantum Pattern Matching in Generalised Degenerate Strings* (`arXiv:2603.16297`). Every definition, problem statement, subroutine, lemma, and theorem in the compiled paper is formalized and proved unconditionally under standard Lean axioms (`propext`, `Classical.choice`, `Quot.sound`), directly reusing `CslibFrontier`'s unitary quantum query complexity and measurement infrastructure (`ValueQuery`, `UnknownSearch`, `Composition`, `Measurement`). Four minor statement and initialization discrepancies in Lemma 3 and Section 4 are documented below with compiling counterexamples and repaired statements. ## Source & Attribution - **Title**: *Quantum Pattern Matching in Generalised Degenerate Strings* - **Author(s)**: Massimo Equi, Md Rabiul Islam Khan, Veli Mäkinen - **Copyright**: Copyright © 2026 Massimo Equi, Md Rabiul Islam Khan, Veli Mäkinen (retained under CC BY 4.0). - **Source**: https://arxiv.org/abs/2603.16297 (`arXiv:2603.16297`) - **Source Paper License**: Creative Commons Attribution 4.0 International (CC BY 4.0) (https://creativecommons.org/licenses/by/4.0/, legal code: https://creativecommons.org/licenses/by/4.0/legalcode) - **Disclaimer of Warranties**: Provided as-is without warranties of any kind (Section 5 of CC BY 4.0; see https://creativecommons.org/licenses/by/4.0/legalcode). - **Modifications / Adaptation Notice**: These Lean 4 modules and companion `README.md` are an AI-generated mathematical formalization adapted from the source preprint. No endorsement by or affiliation with the original authors or copyright holders is implied. ## Source Correspondence All declarations live in `namespace Papers.arxiv_2603_16297`. | Paper Item (`main.tex`) | Lean Declaration(s) | Module | | :--- | :--- | :--- | | Generalised Degenerate (GD) string, language, width `W`, size `N` (`main.tex:244-246`) | `Papers.arxiv_2603_16297.GDSegment`, `Papers.arxiv_2603_16297.GDString`, `Papers.arxiv_2603_16297.GDShape`, `Papers.arxiv_2603_16297.GDInstance`, `Papers.arxiv_2603_16297.GDString.language`, `Papers.arxiv_2603_16297.GDString.width`, `Papers.arxiv_2603_16297.GDString.size` | `Defs.lean` | | Problem 1 (`main.tex:210-214`) | `Papers.arxiv_2603_16297.HasMatch`, `Papers.arxiv_2603_16297.hasMatch_iff_exists_language_infix`, `Papers.arxiv_2603_16297.hasMatch_iff_inSegment_or_crossSegment` | `Defs.lean` | | Theorem 2 (`main.tex:260-264`) | `Papers.arxiv_2603_16297.amplitude_amplification`, `Papers.arxiv_2603_16297.amplitude_amplification_of_decomp`, `Papers.arxiv_2603_16297.amplitude_amplification_witness_fin4` | `AmplitudeAmplification.lean` | | Oracle functions `f₁`, `ext`, `sm`, `pm` and Subroutine 1 (`main.tex:286-306,368-380`) | `Papers.arxiv_2603_16297.f1Oracle`, `Papers.arxiv_2603_16297.extPred`, `Papers.arxiv_2603_16297.smPred`, `Papers.arxiv_2603_16297.pmPred`, `Papers.arxiv_2603_16297.ThreadState`, `Papers.arxiv_2603_16297.initThreadState`, `Papers.arxiv_2603_16297.threadStep`, `Papers.arxiv_2603_16297.runThreads` | `Defs.lean` | | Lemma 3 (`main.tex:417-436`, repaired) | `Papers.arxiv_2603_16297.thread_invariant`, `Papers.arxiv_2603_16297.thread_invariant_shift`, `Papers.arxiv_2603_16297.runThreads_matched_iff_f1`, `Papers.arxiv_2603_16297.hasMatch_iff_inSegment_or_exists_thread` | `ThreadInvariant.lean` | | Lemma 4 (`main.tex:438-446`) | `Papers.arxiv_2603_16297.lemma_f1_computation`, `Papers.arxiv_2603_16297.lemma_f1_computation_general`, `Papers.arxiv_2603_16297.f1LoopProg`, `Papers.arxiv_2603_16297.f1LoopProg_computesAmp` | `F1Algorithm.lean` | | Theorem 1 (`main.tex:220-223`, general case via Section 6, `main.tex:461-466`) | `Papers.arxiv_2603_16297.quantum_smgd_algorithm`, `Papers.arxiv_2603_16297.quantum_smgd_algorithm_short_segments`, `Papers.arxiv_2603_16297.inSegSMGDProg`, `Papers.arxiv_2603_16297.crossSegSMGDProg` | `QuantumAlgorithm.lean` | | Appendix A, Lemma 5 (`main.tex:475-504`) | `Papers.arxiv_2603_16297.sum_sqrt_segment_size_le`, `Papers.arxiv_2603_16297.sum_sqrt_segment_size_le_shape`, `Papers.arxiv_2603_16297.sqrt_mul_sum_sqrt_segment_size_le` | `Complexity.lean` | | Figure 1 DNA example (`main.tex:204,246`) & long-segment witness (`k_i ≥ m`) | `Papers.arxiv_2603_16297.figGD_witness`, `Papers.arxiv_2603_16297.quantum_smgd_fig1_witness`, `Papers.arxiv_2603_16297.quantum_smgd_long_segment_witness` | `ThreadInvariant.lean`, `QuantumAlgorithm.lean` | ### Upstream Quantum Library Reuse The quantum query algorithms and probability guarantees build directly on `CslibFrontier`: - `CslibFrontier.Foundations.Quantum.Measurement`: `Cslib.Quantum.ket`, `Cslib.Quantum.measureProb`, `Cslib.Quantum.norm_ket`, `Cslib.Quantum.measureProb_univ`, `Cslib.Quantum.measureProb_compl`. - `CslibFrontier.Computability.QueryComplexity.Quantum.ValueQuery`: `Cslib.Quantum.ValueState`, `Cslib.Quantum.valueOracle`, `Cslib.Quantum.addStep`, `Cslib.Quantum.runValue`, `Cslib.Quantum.norm_runValue`. - `CslibFrontier.Computability.QueryComplexity.Quantum.UnknownSearch`: `Cslib.Quantum.orRange`, `Cslib.Quantum.orStage`, `Cslib.Quantum.randomGrover`, `Cslib.Quantum.measureProb_run_orStage`, `Cslib.Quantum.le_sum_regProb_run_randomGrover`. - `CslibFrontier.Computability.QueryComplexity.Quantum.Composition`: `Cslib.Quantum.QueryBasis`, `Cslib.Quantum.twice`, `Cslib.Quantum.twiceInit`, `Cslib.Quantum.measureProb_run_twice`. ## Module Structure - `Papers/arxiv_2603_16297.lean`: Public umbrella re-exporting all eight submodules. - `Papers/arxiv_2603_16297/Defs.lean`: GD strings, structural shapes `GDShape`, Problem 1 (`HasMatch`), in-segment/cross-segment decomposition, oracle predicates (`f1Oracle`, `extPred`, `smPred`, `pmPred`), and Subroutine 1 (`threadStep`, `runThreads`). - `Papers/arxiv_2603_16297/Complexity.lean`: Appendix A, Lemma 5 (`sum_sqrt_segment_size_le`), its Cauchy–Schwarz corollary for Theorem 1, and geometric/logarithmic amplification bounds. - `Papers/arxiv_2603_16297/ThreadInvariant.lean`: Repaired Lemma 3 (`thread_invariant`, `thread_invariant_shift`), Section 6 completeness (`hasMatch_iff_inSegment_or_exists_thread`), compiling counterexamples for the four literal Lemma 3 / Section 4 discrepancies, and the Figure 1 DNA witness (`figGD_witness`). - `Papers/arxiv_2603_16297/AmplitudeAmplification.lean`: Theorem 2 (`amplitude_amplification`, `amplitude_amplification_of_decomp`) and a 2-qubit Walsh–Hadamard witness (`amplitude_amplification_witness_fin4`). - `Papers/arxiv_2603_16297/QuantumCombinators.lean`: Compositional unitary `QueryProg` algebra over `Cslib.Quantum.runValue Cslib.Quantum.addStep`, exact 4-query QRAM character comparison (`charMismatchOracle`), and cleanly uncomputed phase and bit oracles (`ctrlCleanPhaseProg`, `cleanBitProg`). - `Papers/arxiv_2603_16297/QuantumSearch.lean`: Unitary hybrid perturbation lemma (`norm_liftSearchProg_sub_ideal_le`), `2^d`-fold amplified `Cslib.Quantum.orStage` search (`groverOrProg`), multiplexed clean phase oracles (`muxCtrlPhaseProg`), and sequential OR composition (`orCombineProg`). - `Papers/arxiv_2603_16297/F1Algorithm.lean`: Word-QRAM input encoding (`InputAddr`, `CharVal`, `inputMem`), nested Grover subroutines `G₃` (`stringEqProg`) and `G₂` (`segSearchProg`, `extProg`, `smProg`, `pmProg`), the reversible `f₁` for-loop (`f1LoopProg`), and Lemma 4 (`lemma_f1_computation`, `lemma_f1_computation_general`). - `Papers/arxiv_2603_16297/QuantumAlgorithm.lean`: Section 6 in-segment preprocessing search (`inSegSMGDProg`), outer search `G₁` (`crossSegSMGDProg`), Theorem 1 (`quantum_smgd_algorithm`, `quantum_smgd_algorithm_short_segments`), and concrete joint non-vacuity witnesses (`quantum_smgd_fig1_witness`, `quantum_smgd_long_segment_witness`). ## Formalization Notes ### Clarified Statements in Lemma 3 and Section 4 (`arXiv:2603.16297`) Four minor statement and initialization discrepancies occur in the literal formulation of Lemma 3 (`main.tex:417-436`) and Section 4 (`main.tex:338`). All four are repairable without changing the algorithm's architecture, nested Grover structure, or asymptotic complexity; each discrepancy is paired with a compiling counterexample in `ThreadInvariant.lean` under standard Lean axioms: 1. **Base-case activation (`main.tex:433`)**: The proof of Lemma 3 writes "When the system is initialized with all threads active we have `j_{i,h} = h`, `a_{1,h} = 0`, `m_{1,h} = 0` for every `h`." Initializing `a_{1,h} = 0` for every thread prevents any thread from detecting a match starting at column `0` (`Papers.arxiv_2603_16297.counterexample_zero_init_misses_col_zero`), whereas initializing `a_{1,h} = 1` for every thread admits false positives on non-matching instances (`Papers.arxiv_2603_16297.counterexample_all_active_false_positive`). Activating exactly the thread with prefix offset `0` (`a_{1,h} = true ↔ j_{1,h} = 0`, implemented in `initThreadState`) satisfies the invariant at `i = 0` and preserves soundness and completeness. 2. **Thread offset in Bullet 1 (`main.tex:423-424`)**: Bullet 1 of Lemma 3 writes `j_{i,h} = ∑_{t=1}^{i-1} k_t`, omitting the thread shift and reduction modulo `m`. For `m ≥ 2`, a value independent of `h` cannot simultaneously satisfy Bullet 2 (`j_{i,h+1} = j_{i,h} + 1 mod m`) (`Papers.arxiv_2603_16297.counterexample_literal_invariant_j_contradiction`). Including the modular thread shift (`j_{i,h} = (∑_{t=1}^{i-1} k_t + (m - h)) mod m`) reconciles both bullets (`Papers.arxiv_2603_16297.thread_invariant` and `Papers.arxiv_2603_16297.thread_invariant_shift`). 3. **Thread-phase restriction in Bullet 4 (`main.tex:426`)**: Bullet 4 of Lemma 3 states `m_{i,h} = 1` iff `P` has a match in `T[1..i-1]`, omitting the thread-phase congruence `c ≡ h (mod m)` specified earlier in Section 4 (`main.tex:349`). When a match occurs at a column `c ≢ h (mod m)`, thread `h` still finishes with `m_{i,h} = 0` (`Papers.arxiv_2603_16297.counterexample_literal_invariant_m_phase`). 4. **Initialization sign (`main.tex:338`)**: Initializing `j_{1,h} = h` aligns thread `h` with matches starting at columns `c ≡ -h (mod m)` rather than `c ≡ h (mod m)` (`Papers.arxiv_2603_16297.counterexample_positive_h_init_phase_sign`). Initializing `j_{1,h} = (m - h) mod m` keeps `j_{i,h}` increasing by `k_i` per segment while matching the paper's definition of `f₁(h)` (`c = h + r * m`) identically for every thread `h` (`Papers.arxiv_2603_16297.runThreads_matched_iff_f1`). ### Model and Complexity Notes - **Word-QRAM model**: Characters of any finite alphabet `α` are encoded injectively into the additive cyclic group `CharVal α = ZMod (Fintype.card α + 1)`, and every quantum subroutine runs directly under `Cslib.Quantum.runValue Cslib.Quantum.addStep` (`|i⟩|x⟩ ↦ |i⟩|x + A[i]⟩`, matching `main.tex:255`). All algorithm unitaries, initial states, and acceptance projectors depend only on the structural shape `(m, sh)` (never on the input characters `P` or `inst`). - **Explicit polylogarithmic bounds**: Rather than using an informal `Õ` notation, `lemma_f1_computation` proves an explicit query upper bound of `14000000 * (Nat.log 2 (n * N + 2) + 1)^2 * ∑ i, √(|T[i]| * k_i)`, and `quantum_smgd_algorithm` proves an explicit query upper bound of `4000000000 * (Nat.log 2 (m * n * N + 2) + 1)^2 * √(m * n * N)` with two-sided error `< 1 / 3` (success probability `> 2 / 3`), matching `Õ(∑ i, √(|T[i]| * k_i))` and `Õ(√(m n N))` respectively.