©2023 IEEE. Personal use of this material is permitted. Permission from IEEE must be obtained for all other uses, in any current or future media, including reprinting/republishing this material for advertising or promotional purposes, creating new collective works, for resale or redistribution to servers or lists, or reuse of any copyrighted component of this work in other works.
Design by Contract Framework for
Quantum Software
††thanks: This research is based on the discussion at Top SE education program.
Abstract
To realize reliable quantum software, techniques to automatically ensure the quantum software’s correctness have recently been investigated. However, they primarily focus on fixed quantum circuits rather than the procedure of building quantum circuits. Despite being a common approach, the correctness of building circuits using different parameters following the same procedure is not guaranteed. To this end, we propose a design-by-contract framework for quantum software. Our framework provides a python-embedded language to write assertions on the input and output states of all quantum circuits built by certain procedures. Additionally, it provides a method to write assertions about the statistical processing of measurement results to ensure the procedure’s correctness for obtaining the final result. These assertions are automatically checked using a quantum computer simulator. For evaluation, we implemented our framework and wrote assertions for some widely used quantum algorithms. Consequently, we found that our framework has sufficient expressive power to verify the whole procedure of quantum software.
Index Terms:
Programming by contract, Testing and Debugging, quantum computingI Introduction
Quantum computing is an emerging technology that accelerates many computing-heavy tasks. Recently, software development frameworks for quantum software, such as Qiskit [1], have been developed. However, realizing reliable quantum software is still challenging because of the property of quantum computing: superposition, entanglement and interference.
For those challenges, different approaches have been studied, such as formal verification [2, 3, 4], checking assertions on quantum computers [5, 6], equivalence checking of circuits [7], and automatic testing [8, 9]. However, these techniques mainly focus on fixed quantum circuits, so the correctness of building circuits using different parameters following the same procedure is not guaranteed despite being a common practice.
In this study, we propose a design-by-contract framework [10] for quantum software. Our contributions are as follows:
-
•
We present a python-embedded language to write assertions about the input states (termed pre-state) and output states (termed post-state) of all quantum circuits built by certain procedures. This enables us to verify the procedure of constructing quantum circuits. Additionally, one can write assertions about the statistical processing of measurement results for obtaining the final result. These assertions are checked on a quantum computer simulator.
-
•
We provide a module-like feature to construct a bigger circuit from smaller circuits verified by assertions.
II Motivating Example
As a motivating example, we use the Hadamard test. The circuit is shown in Fig. 1. It is a basic algorithm to estimate the expected value of a unitary matrix for a state , i.e., estimate .
It changes the qubits’ state as follows:
(1) |
Thus, the real part of is obtained by:
(2) |
where and are the probability and count of obtaining when measuring the first qubit, respectively.
The circuit adopts as a parameter. Thus, we need to verify the procedure for building the circuit from the gate . This includes complex steps such as the decomposition of gates to run on quantum computers. Moreover, we need statistical postprocessing according to (2) to obtain the final result. Unfortunately, existing works cannot verify these procedures.
III Proposed Approach
Our approach is based on the design-by-contract framework [10]. We provide a language leveraging Qiskit to write assertions about pre/post-states of all circuits built by certain procedures and the statistical processing of measurement results.
III-A Verifying the Procedure of Building Quantum Circuits
Θ\st@rredtrue (,)→: = (= + 1).((), [0]) = ( .(),=[,,,]).( , (. + 1)).((), [0]) (, ) →: = (( ,(1, . + 1))) = (( + ( @ )) / 2)^ = (( - ( @ )) / 2)^ (, + ).(, )
Fig. 2 is an example code for the Hadamard test written in our framework. It takes the gate and its unitary representation and returns a circuit with an assertion for (1). This code represents the procedure of building the circuit from the gate . It consists of three parts: define a circuit including a decomposing step, define an assertion, and return the circuit with the assertion tagged .
III-B Checking the Correctness of Statistical Postprocessing
In quantum computing, we must measure qubits and statistically process the measurement result to obtain the final result. Our framework can verify this process, as shown in Fig. 3.
Θ\st@rredtrue = (([[1, 0], [0, .(1j * . / 4)]]) ) = () = (, ) = = ( = 2).((), [1]).(, [0, 1]) () →: = .() ([] - []) / ([] + []): = .(=, =[0]) ( , , )→: = ((~) @ @ ).(). .(, , =0.01).(, )(.(=100_000))>> 0.85514 &
This code estimates . Here, we defined an assertion to check whether statistical processing follows (2). The assertion’s arguments are the qubits’ state before measurement, result of measurement, and result of post-processing. Note that we separated the class of the measurement and the other steps because the former contains classical functions. We built the circuit from smaller circuits by using our module-like system. The assertions in nested circuits are verified in runtime.
By running the code, it prints the estimated value without any assertion error, which means that the whole procedure of the Hadamard test is correct with respect to and .
If the circuit does not behave as expected, our framework raises an exception to notify the user, as shown in Fig. 4.
Cell In[8], line 22 ---> 22 print(circ_measure.run(shots=100_000)) ... StateConditionError: Condition Error occurred in ’condition1’
IV Evaluation
We evaluated our framework by writing three quantum algorithms that are widely used: the Hadamard test, quantum Fourier transform (QFT), and quantum phase estimation (QPE). The details of the evaluation are not provided for the sake of brevity. As explained in section III, our framework can express the whole procedure of quantum software and rapidly detects mistakes through the failure of the assertions.
The limitation of our framework is the lack of intermediate measurements. We only allow measurements in the last of the circuit, but some cases, such as error correction, need it.
V Conclusion and Future Direction
We proposed a design-by-contract framework for quantum software, whose novelty is that it can verify the procedure of building quantum circuits and statistical post-processing. We demonstrated the effectiveness of our framework and clarified its limitations. In future studies, we will expand our method to handle intermediate measurements. Another direction is to evaluate the effectiveness of our method using more examples.
References
- [1] IBM. Qiskit. [Online]. Available: https://qiskit.org/
- [2] K. Hietala, R. Rand, S.-H. Hung, L. Li, and M. Hicks, “Proving Quantum Programs Correct,” in 12th International Conference on Interactive Theorem Proving (ITP 2021), ser. Leibniz International Proceedings in Informatics (LIPIcs), L. Cohen and C. Kaliszyk, Eds., vol. 193. Dagstuhl, Germany: Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2021, pp. 21:1–21:19.
- [3] J. Paykin, R. Rand, and S. Zdancewic, “QWIRE: A core language for quantum circuits,” in Proceedings of the 44th ACM SIGPLAN Symposium on Principles of Programming Languages, ser. POPL ’17. NY, USA: Association for Computing Machinery, 2017, p. 846–858.
- [4] C. Chareton, S. Bardin, F. Bobot, V. Perrelle, and B. Valiron, “An automated deductive verification framework for circuit-building quantum programs,” in Programming Languages and Systems, ESOP 2021, ser. Lecture Notes in Computer Science, N. Yoshida, Ed., vol. 12648. Cham: Springer International Publishing, March 2021, pp. 148–177.
- [5] Y. Huang and M. Martonosi, “Statistical assertions for validating patterns and finding bugs in quantum programs,” in Proceedings of the 46th International Symposium on Computer Architecture, ser. ISCA ’19. New York, USA: Association for Computing Machinery, 2019, p. 541–553.
- [6] G. Li, L. Zhou, N. Yu, Y. Ding, M. Ying, and Y. Xie, “Projection-based runtime assertions for testing and debugging quantum programs,” Proc. ACM Program. Lang., vol. 4, no. OOPSLA, nov 2020.
- [7] L. Burgholzer and R. Wille, “Advanced equivalence checking for quantum circuits,” IEEE Transactions on Computer-Aided Design of Integrated Circuits and Systems, vol. 40, no. 9, pp. 1810–1824, 2021.
- [8] S. Honarvar, M. R. Mousavi, and R. Nagarajan, “Property-based testing of quantum programs in Q#,” in Proceedings of the IEEE/ACM 42nd International Conference on Software Engineering Workshops, ser. ICSEW’20. New York, NY, USA: Association for Computing Machinery, 2020, p. 430–435.
- [9] X. Wang, P. Arcaini, T. Yue, and S. Ali, “Generating failing test suites for quantum programs with search,” in Search-Based Software Engineering, U.-M. O’Reilly and X. Devroey, Eds. Cham: Springer International Publishing, 2021, pp. 9–25.
- [10] B. Meyer, “Design by contract,” Interactive Software Engineering Inc, Tech. Rep., 1986.