This paper was converted on www.awesomepapers.org from LaTeX by an anonymous user.
Want to know more? Visit the Converter page.

{screen}

©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.

1st Masaomi Yamaguchi Quantum Laboratory
Fujitsu Limited
Kanagawa, Japan
y.masaomi@fujitsu.com
   2nd Nobukazu Yoshioka Waseda University / National Institute of Informatics
Tokyo, Japan
nobukazuy@acm.org
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 computing

I 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 UU for a state |ψ\left\lvert{\psi}\right\rangle, i.e., estimate ψ|U|ψ\left\langle{\psi}\middle|{U}\middle|{\psi}\right\rangle.

Refer to caption

Figure 1: The circuit for the Hadamard test

It changes the qubits’ state as follows:

|ψ|012{(I+U)|ψ|0+(IU)|ψ|1}\left\lvert{\psi}\right\rangle\otimes\left\lvert{0}\right\rangle\rightarrow\frac{1}{2}\{(I+U)\left\lvert{\psi}\right\rangle\otimes\left\lvert{0}\right\rangle+(I-U)\left\lvert{\psi}\right\rangle\otimes\left\lvert{1}\right\rangle\} (1)

Thus, the real part of ψ|U|ψ\left\langle{\psi}\middle|{U}\middle|{\psi}\right\rangle is obtained by:

Reψ|U|ψ=p0p1N0N1N0+N1\mathrm{Re}\left\langle{\psi}\middle|{U}\middle|{\psi}\right\rangle=p_{0}-p_{1}\approx\frac{N_{0}-N_{1}}{N_{0}+N_{1}} (2)

where pip_{i} and NiN_{i} are the probability and count of obtaining |i\left\lvert{i}\right\rangle when measuring the first qubit, respectively.

The circuit adopts UU as a parameter. Thus, we need to verify the procedure for building the circuit from the gate UU. 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

\start@align

Θ\st@rredtrue 𝐝𝐞𝐟\mathbf{def} 𝑚𝑎𝑘𝑒_𝑐𝑖𝑟𝑐_ℎ𝑎𝑑𝑎𝑚𝑎𝑟𝑑_𝑡𝑒𝑠𝑡\mathit{make\_circ\_hadamard\_test}(𝑈𝑔𝑎𝑡𝑒\mathit{Ugate},U\mathit{U})→𝖠𝗌𝗌𝖾𝗋𝗍𝖰𝗎𝖺𝗇𝗍𝗎𝗆𝖢𝗂𝗋𝖼𝗎𝗂𝗍\mathsf{AssertQuantumCircuit}:𝑐𝑖𝑟𝑐\mathit{circ} = 𝖠𝗌𝗌𝖾𝗋𝗍𝖰𝗎𝖺𝗇𝗍𝗎𝗆𝖢𝗂𝗋𝖼𝗎𝗂𝗍\mathsf{AssertQuantumCircuit}(𝑠𝑖𝑧𝑒\mathit{size}=𝑈𝑔𝑎𝑡𝑒.𝑛𝑢𝑚_𝑞𝑢𝑏𝑖𝑡𝑠\mathit{Ugate.num\_qubits} + 1)𝑐𝑖𝑟𝑐\mathit{circ}.𝑎𝑝𝑝𝑒𝑛𝑑\mathit{append}(𝖧𝖦𝖺𝗍𝖾\mathsf{HGate}(), [0])𝑐𝑡𝑟𝑙_U\mathit{ctrl\_U} = 𝑑𝑒𝑐𝑜𝑚𝑝𝑜𝑠𝑒\mathit{decompose}( 𝑈𝑔𝑎𝑡𝑒\mathit{Ugate}.𝑐𝑜𝑛𝑡𝑟𝑜𝑙\mathit{control}(),𝑏𝑎𝑠𝑖𝑠_𝑔𝑎𝑡𝑒𝑠\mathit{basis\_gates}=["𝚑"\mathtt{"h"},"𝚛𝚡"\mathtt{"rx"},"𝚛𝚣"\mathtt{"rz"},"𝚌𝚡"\mathtt{"cx"}])𝑐𝑖𝑟𝑐\mathit{circ}.𝑎𝑝𝑝𝑒𝑛𝑑\mathit{append}( 𝑐𝑡𝑟𝑙_U\mathit{ctrl\_U}, 𝑟𝑎𝑛𝑔𝑒\mathit{range}(𝑈𝑔𝑎𝑡𝑒\mathit{Ugate}.𝑛𝑢𝑚_𝑞𝑢𝑏𝑖𝑡𝑠\mathit{num\_qubits} + 1))𝑐𝑖𝑟𝑐\mathit{circ}.𝑎𝑝𝑝𝑒𝑛𝑑\mathit{append}(𝖧𝖦𝖺𝗍𝖾\mathsf{HGate}(), [0])𝐝𝐞𝐟\mathbf{def} 𝑐𝑜𝑛𝑑𝑖𝑡𝑖𝑜𝑛\mathit{condition}(𝑝𝑟𝑒_𝑠𝑡𝑎𝑡𝑒\mathit{pre\_state}, 𝑝𝑜𝑠𝑡_𝑠𝑡𝑎𝑡𝑒\mathit{post\_state}) →𝖻𝗈𝗈𝗅\mathsf{bool}:𝑝𝑠𝑖\mathit{psi} = 𝖲𝗍𝖺𝗍𝖾𝖥𝗇\mathsf{StateFn}(𝑝𝑎𝑟𝑡𝑖𝑎𝑙_𝑠𝑡𝑎𝑡𝑒\mathit{partial\_state}( 𝑝𝑟𝑒_𝑠𝑡𝑎𝑡𝑒\mathit{pre\_state},𝑟𝑎𝑛𝑔𝑒\mathit{range}(1, 𝑈𝑔𝑎𝑡𝑒\mathit{Ugate}.𝑛𝑢𝑚_𝑞𝑢𝑏𝑖𝑡𝑠\mathit{num\_qubits} + 1)))state0\mathit{state0} = ((𝑝𝑠𝑖\mathit{psi} + (U\mathit{U} @ 𝑝𝑠𝑖\mathit{psi})) / 2)^𝖹𝖾𝗋𝗈\mathsf{Zero}state1\mathit{state1} = ((𝑝𝑠𝑖\mathit{psi} - (U\mathit{U} @ 𝑝𝑠𝑖\mathit{psi})) / 2)^𝖮𝗇𝖾\mathsf{One}𝐫𝐞𝐭𝐮𝐫𝐧\mathbf{return} 𝑒𝑞_𝑠𝑡𝑎𝑡𝑒\mathit{eq\_state}(𝑝𝑜𝑠𝑡_𝑠𝑡𝑎𝑡𝑒\mathit{post\_state}, state0\mathit{state0} + state1\mathit{state1})𝑐𝑖𝑟𝑐\mathit{circ}.𝑎𝑑𝑑_𝑐𝑜𝑛𝑑𝑖𝑡𝑖𝑜𝑛\mathit{add\_condition}("𝚌𝚘𝚗𝚍𝚒𝚝𝚒𝚘𝚗𝟷"\mathtt{"condition1"}, 𝑐𝑜𝑛𝑑𝑖𝑡𝑖𝑜𝑛\mathit{condition})𝐫𝐞𝐭𝐮𝐫𝐧\mathbf{return} 𝑐𝑖𝑟𝑐\mathit{circ}

Figure 2: Code with assertions for Hadamard test written in our framework

Fig. 2 is an example code for the Hadamard test written in our framework. It takes the gate 𝑈𝑔𝑎𝑡𝑒\mathit{Ugate} and its unitary representation UU and returns a circuit with an assertion for (1). This code represents the procedure of building the circuit from the gate UgateUgate. It consists of three parts: define a circuit including a decomposing step, define an assertion, and return the circuit with the assertion tagged "𝚌𝚘𝚗𝚍𝚒𝚝𝚒𝚘𝚗𝟷"\mathtt{"condition1"}.

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.

\start@align

Θ\st@rredtrue U\mathit{U} = 𝖯𝗋𝗂𝗆𝗂𝗍𝗂𝗏𝖾𝖮𝗉\mathsf{PrimitiveOp}(𝖮𝗉𝖾𝗋𝖺𝗍𝗈𝗋\mathsf{Operator}([[1, 0], [0, 𝑐𝑚𝑎𝑡ℎ\mathit{cmath}.𝑒𝑥𝑝\mathit{exp}(1j * 𝑚𝑎𝑡ℎ\mathit{math}.𝑝𝑖\mathit{pi} / 4)]]) )𝑈𝑔𝑎𝑡𝑒\mathit{Ugate} = 𝖳𝖦𝖺𝗍𝖾\mathsf{TGate}()𝑐𝑖𝑟𝑐_ℎ𝑎𝑑𝑎𝑚𝑎𝑟𝑑_𝑡𝑒𝑠𝑡\mathit{circ\_hadamard\_test} = 𝑚𝑎𝑘𝑒_𝑐𝑖𝑟𝑐_ℎ𝑎𝑑𝑎𝑚𝑎𝑟𝑑_𝑡𝑒𝑠𝑡\mathit{make\_circ\_hadamard\_test}(𝑈𝑔𝑎𝑡𝑒\mathit{Ugate}, U\mathit{U})𝑝𝑠𝑖\mathit{psi} = 𝖯𝗅𝗎𝗌\mathsf{Plus}𝑐𝑖𝑟𝑐\mathit{circ} = 𝖠𝗌𝗌𝖾𝗋𝗍𝖰𝗎𝖺𝗇𝗍𝗎𝗆𝖢𝗂𝗋𝖼𝗎𝗂𝗍\mathsf{AssertQuantumCircuit}(𝑠𝑖𝑧𝑒\mathit{size} = 2)𝑐𝑖𝑟𝑐\mathit{circ}.𝑎𝑝𝑝𝑒𝑛𝑑\mathit{append}(𝖧𝖦𝖺𝗍𝖾\mathsf{HGate}(), [1])𝑐𝑖𝑟𝑐\mathit{circ}.𝑎𝑝𝑝𝑒𝑛𝑑\mathit{append}(𝑐𝑖𝑟𝑐_ℎ𝑎𝑑𝑎𝑚𝑎𝑟𝑑_𝑡𝑒𝑠𝑡\mathit{circ\_hadamard\_test}, [0, 1])𝐝𝐞𝐟\mathbf{def} 𝑒𝑠𝑡𝑖𝑚𝑎𝑡𝑒_𝑒𝑥𝑝\mathit{estimate\_exp}(𝑟𝑒𝑠𝑢𝑙𝑡\mathit{result}) →𝖿𝗅𝗈𝖺𝗍\mathsf{float}:c\mathit{c} = 𝑟𝑒𝑠𝑢𝑙𝑡\mathit{result}.𝑔𝑒𝑡_𝑐𝑜𝑢𝑛𝑡𝑠\mathit{get\_counts}()𝐫𝐞𝐭𝐮𝐫𝐧\mathbf{return} (c\mathit{c}["𝟶"\mathtt{"0"}] - c\mathit{c}["𝟷"\mathtt{"1"}]) / (c\mathit{c}["𝟶"\mathtt{"0"}] + c\mathit{c}["𝟷"\mathtt{"1"}])𝑐𝑖𝑟𝑐_𝑚𝑒𝑎𝑠𝑢𝑟𝑒\mathit{circ\_measure}: 𝖠𝗌𝗌𝖾𝗋𝗍𝖰𝗎𝖺𝗇𝗍𝗎𝗆𝖢𝗂𝗋𝖼𝗎𝗂𝗍𝖬𝖾𝖺𝗌𝗎𝗋𝖾\mathsf{AssertQuantumCircuitMeasure}= 𝑐𝑖𝑟𝑐\mathit{circ}.𝑚𝑒𝑎𝑠𝑢𝑟𝑒\mathit{measure}(𝑝𝑜𝑠𝑡𝑝𝑟𝑜𝑐𝑒𝑠𝑠\mathit{postprocess}=𝑒𝑠𝑡𝑖𝑚𝑎𝑡𝑒_𝑒𝑥𝑝\mathit{estimate\_exp}, 𝑞𝑢𝑏𝑖𝑡\mathit{qubit}=[0])𝐝𝐞𝐟\mathbf{def} 𝑚𝑒𝑎𝑠𝑢𝑟𝑒_𝑐𝑜𝑛𝑑𝑖𝑡𝑖𝑜𝑛\mathit{measure\_condition}( 𝑝𝑟𝑒_𝑚𝑒𝑎𝑠𝑢𝑟𝑒_𝑠𝑡𝑎𝑡𝑒\mathit{pre\_measure\_state}, 𝑟𝑒𝑠𝑢𝑙𝑡\mathit{result}, 𝑒𝑠𝑡_𝑒𝑥𝑝\mathit{est\_exp} )→𝖻𝗈𝗈𝗅\mathsf{bool}:𝑎𝑐𝑡𝑢𝑎𝑙_𝑒𝑥𝑝\mathit{actual\_exp} = ((~𝑝𝑠𝑖\mathit{psi}) @ U\mathit{U} @ 𝑝𝑠𝑖\mathit{psi}).𝑒𝑣𝑎𝑙\mathit{eval}().𝑟𝑒𝑎𝑙\mathit{real}𝐫𝐞𝐭𝐮𝐫𝐧\mathbf{return} 𝑚𝑎𝑡ℎ\mathit{math}.𝑖𝑠𝑐𝑙𝑜𝑠𝑒\mathit{isclose}(𝑎𝑐𝑡𝑢𝑎𝑙_𝑒𝑥𝑝\mathit{actual\_exp}, 𝑒𝑠𝑡_𝑒𝑥𝑝\mathit{est\_exp}, 𝑎𝑏𝑠_𝑡𝑜𝑙\mathit{abs\_tol}=0.01)𝑐𝑖𝑟𝑐_𝑚𝑒𝑎𝑠𝑢𝑟𝑒\mathit{circ\_measure}.𝑎𝑑𝑑_𝑐𝑜𝑛𝑑𝑖𝑡𝑖𝑜𝑛\mathit{add\_condition}("𝚌𝚘𝚗𝚍𝚒𝚝𝚒𝚘𝚗𝟸"\mathtt{"condition2"}, 𝑚𝑒𝑎𝑠𝑢𝑟𝑒_𝑐𝑜𝑛𝑑𝑖𝑡𝑖𝑜𝑛\mathit{measure\_condition})𝑝𝑟𝑖𝑛𝑡\mathit{print}(𝑐𝑖𝑟𝑐_𝑚𝑒𝑎𝑠𝑢𝑟𝑒\mathit{circ\_measure}.𝑟𝑢𝑛\mathit{run}(𝑠ℎ𝑜𝑡𝑠\mathit{shots}=100_000))>> 0.85514 &

Figure 3: Code to check the post process of the Hadamard test

This code estimates Re+|T|+\mathrm{Re}\left\langle{+}\middle|{T}\middle|{+}\right\rangle. 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 TT and |+\left\lvert{+}\right\rangle.

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’
Figure 4: Exception when the assertion of the Hadamard test fails

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.