NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Testing Quantum Programs with Multiple Subroutines

Peixun Long, Jianjun ZhaoarXiv 2022

Abstract

Errors in quantum programs are challenging to track down due to the uncertainty of quantum programs. Testing is, therefore, an indispensable method for assuring the quality of quantum software. Existing testing methods focus only on testing quantum programs with quantum circuits or single subroutines and, therefore, cannot effectively test quantum programs with multi-subroutines. In this paper, we first discuss several critical issues that must be considered when testing multi-subroutine quantum programs and point out the limitations and problems with existing testing methods. We then present a novel framework for testing multi-subroutine quantum programs that allow for both unit and integration testing. Our framework includes two novel test coverage criteria for the equivalent class partition of quantum variables to guide our testing tasks and techniques to test quantum programs with several common patterns. We also discuss how to generate test cases based on our framework. To evaluate the effectiveness of our testing framework, we implemented a tool called QSharpTester for testing Q# programs with multiple subroutines. We used it to conduct experiments on hundreds of mutation programs deriving from seven original Q# programs. The experimental results show that our testing methods can deal with broader types of quantum programs than existing ones and perform well on almost all faulty mutation programs.

Introduction

As quantum computing transitions from theoretical circuits to practical software, programs are becoming increasingly complex. Modern quantum software often consists of multiple nested subroutines and a mix of classical and quantum logic. However, existing testing methods suffer from "circuit-centricity," often ignoring classical control logic and failing to address how different program components interact. Furthermore, traditional testing often relies on measurements that collapse the quantum state, making it difficult to test subroutines intended to pass coherent superpositions to subsequent parts of the program.

A Novel Testing Framework

To address these limitations, we propose a systematic testing process that mirrors classical software engineering while respecting the unique nature of quantum mechanics. The framework distinguishes between unit testing (testing individual subroutines in isolation) and integration testing (testing the interaction between subroutines).

The proposed framework for unit and integration testing of quantum programs.

IO Analysis and "IO Marks"

A core component of our framework is the use of IO marks to formally define inputs and outputs for a subroutine. This distinguishes between qubits that are actual inputs and those initialized to an "all-zero" state ($|0\rangle$) within the routine. This distinction is vital for accurately partitioning the input space and generating effective test cases.

Handling Subroutine Complexity

Quantum programs often use patterns where one subroutine calls another, such as in Quantum Phase Estimation (QPE), which utilizes the Inverse Quantum Fourier Transform (IQFT) and controlled-U operations. Our framework is designed to handle these hierarchical structures by treating subroutines as modular units.

A Quantum Phase Estimation (QPE) circuit illustrating the interaction between multiple subroutines like IQFT and controlled-U operations.

Methodology and Coverage Criteria

To ensure thorough testing, we introduced three new criteria for partitioning the input space:

  • Classical-Superposition Partition (CSP): Ensures testing with both classical bitstrings and quantum superpositions.
  • Classical-Superposition-Mixed Partition (CSMP): Incorporates mixed states (statistical ensembles) into the test suite.
  • Entanglement Coverage (EntC): Specifically checks if test cases generate entanglement across qubits, targeting elusive quantum-specific bugs.

For unit testing, we utilize Transform-Based Detection (TBD). By applying the inverse of an expected output state, a correct program should always return the $|0\rangle$ state, allowing for verification without complex state tomography. For integration testing, we employ Test Doubles (similar to mocks in classical programming) to substitute complex or unfinished subroutines with simplified mathematical models.

Experimental Evaluation

We evaluated our framework using QSharpTester, a tool designed for Q# programs. We tested seven original programs, including the Quantum Fourier Transform and Grover’s Search, and generated 244 faulty "mutant" programs by intentionally introducing gate, subroutine, classical, and measurement mutations.

Key Results

  • High Bug Detection: QSharpTester identified almost all 244 faulty mutations. The few remaining mutations were functionally equivalent to the original code.
  • Efficiency: Testing tasks were completed in acceptable timeframes on standard hardware. For example, the Reverse program took only seconds, while more complex programs like QFT remained performant despite the statistical nature of quantum detection.
  • Versatility: The framework successfully handled programs with oracles and programs with no quantum inputs, demonstrating broader applicability than previous methods.

Conclusion

Our framework provides a robust solution for testing complex, multi-subroutine quantum programs. By combining unit and integration testing with novel coverage criteria and transform-based detection, we can identify bugs that were previously invisible to circuit-centric tools. Future work will focus on automating program specifications and exploring metamorphic testing to handle cases where the exact output state is unknown.

Summary updated

BibTeX
@misc{long2022testing,
  author = {Peixun Long and Jianjun Zhao},
  title = {{Testing Quantum Programs with Multiple Subroutines}},
  year = {2022},
  eprint = {2208.09206},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2208.09206},
}
Figure