Abstract
Quantum computing technology may soon deliver revolutionary improvements in algorithmic performance, but these are only useful if computed answers are correct. While hardware-level decoherence errors have garnered significant attention, a less recognized obstacle to correctness is that of human programming errors -- "bugs". Techniques familiar to most programmers from the classical domain for avoiding, discovering, and diagnosing bugs do not easily transfer, at scale, to the quantum domain because of its unique characteristics. To address this problem, we have been working to adapt formal methods to quantum programming. With such methods, a programmer writes a mathematical specification alongside their program, and semi-automatically proves the program correct with respect to it. The proof's validity is automatically confirmed -- certified -- by a "proof assistant". Formal methods have successfully yielded high-assurance classical software artifacts, and the underlying technology has produced certified proofs of major mathematical theorems. As a demonstration of the feasibility of applying formal methods to quantum programming, we present the first formally certified end-to-end implementation of Shor's prime factorization algorithm, developed as part of a novel framework for applying the certified approach to general applications. By leveraging our framework, one can significantly reduce the effects of human errors and obtain a high-assurance implementation of large-scale quantum applications in a principled way.
Introduction: The Correctness Gap in Quantum Programming
Quantum programming is notoriously difficult. Unlike classical software development, where developers rely on "print" statements and debuggers, quantum states cannot be observed without causing them to collapse. This fundamental physical constraint makes traditional testing and debugging strategies largely ineffective at scale. While much of the field's focus remains on hardware-level decoherence, human-written "bugs" in quantum software represent a massive, under-addressed risk.
As shown in Figure 1, traditional testing (a) faces significant bottlenecks: classical simulation requires exponential resources, runtime assertions cause state collapse, and hardware noise complicates output analysis. In contrast, formal methods (b) utilize proof assistants to provide guaranteed correctness on all inputs before the code ever touches hardware.
A Certified Framework for Hybrid Algorithms
Shor’s factorization algorithm is a "hybrid" algorithm, combining complex classical pre-processing, a quantum "order-finding" core, and classical post-processing via Continued Fraction Expansion (CFE). To verify such a system end-to-end, we utilized the Coq proof assistant and developed a multi-layered framework:
- SQIR (Small Quantum Intermediate Representation): A language for expressing quantum circuits and reasoning about their mathematical semantics using matrices and vectors.
- RCIR (Reversible Circuit Intermediate Representation): A novel intermediate language designed to handle classical reversible logic (e.g., modular multiplication) more efficiently than standard quantum representations.
- Formalized Number Theory: Proving Shor’s algorithm required formalizing significant mathematical foundations in Coq, including Legendre’s theorem on continued fractions and lower bounds for Euler’s totient function.
Implementation of Shor's Algorithm
The workflow is truly end-to-end: a programmer writes both the algorithm and its proof in Coq. The system then "extracts" this into executable OCaml code for classical components and OpenQASM for quantum circuits.
Figure 2 illustrates the complete flow, from the initial classical checks (primality, parity) to the quantum order-finding core—featuring Quantum Phase Estimation (QPE) and In-place Modular Multipliers (IMM)—and finally the classical post-processing that extracts the non-trivial factors.
Key Results and Validation
The certified implementation achieves a gate count complexity of $O(\log^3 N)$, matching the theoretical efficiency of Shor’s original 1994 proposal. Crucially, we proved that the algorithm succeeds with a probability of at least $1/\text{polylog}(N)$, a bound that can be boosted through repetition.
Empirical validation using the DDSIM simulator for integers $N$ up to 255 confirmed these theoretical guarantees. For $N=15$, the empirical success probability was approximately 43.77%, significantly higher than the certified lower bound of 0.17%. This discrepancy highlights that while our certified bounds are "pessimistic" (guaranteed for the worst case), the actual performance is robust.
Limitations and Future Outlook
While this work represents a major milestone, several challenges remain. The "trusted base"—the Coq-to-OCaml compiler and the OpenQASM-to-hardware compiler—remains unverified. Additionally, current extraction assumes OCaml’s floating-point numbers behave like mathematical Real numbers; future iterations will need to formalize floating-point errors to ensure perfect precision in quantum gate parameters. By refining these proofs to be "tighter," we can provide even stronger guarantees for the next generation of quantum hardware.
Summary updated