Systematic Approaches For Precise and Approximate Quantum State Runtime Assertion
Systematic Approaches For Precise and Approximate Quantum State Runtime Assertion
Systematic Approaches For Precise and Approximate Quantum State Runtime Assertion
Abstract—With the rapid growth of quantum computing tech- Assertion is an important technique for program debugging.
nology, programmers need new tools for debugging quantum It is a predicate that should always be evaluated to be true
programs. Recent works show that assertions are a promising during program execution. Huang et al. proposed a statistical
way for debugging quantum programs. However, there are two
main drawbacks with the existing schemes. First, the existing assertion scheme [28] which essentially creates breakpoints
schemes, including both statistical and dynamic assertions are in the program and measures the qubits at breakpoints. Due
only capable of asserting limited types of states, namely classical, to its destructive measurement, a statistical assertion cannot
superposition, and specific entanglement states. Second, the use be implemented dynamically at runtime. To overcome this,
cases of these assertions are limited, since the programmer has Liu et al. proposed quantum circuits for dynamic runtime
to know the exact/precise state to assert.
In this work, we propose two systematic approaches for
assertions [32], which do not interrupt program execution
dynamic quantum state assertion and they can assert a much if there are no assertion errors. In addition to debugging
broader range of quantum states including both pure states quantum programs, it is shown that assertion can also be
and mixed states. We also introduce the idea of approximate used to improve program reliability by filtering out erroneous
quantum state assertion for the cases where the programmers results. However, the scope of their supported assertions
only have limited knowledge of the quantum states. Approximate
assertion is capable of checking membership in a set of states
is limited. The assertion primitives only support assertions
{|ψ, |φ, ...}. While precise quantum state assertion can check for classical, superposition, and specific entanglement states.
a specific quantum state, approximate assertion enables a way Li et al. proposed a runtime assertion scheme [30] based
to check whether the qubits of interest are in a super-set of on projective measurements. The projection-based assertions
some expected states, which is analogous to the well-known can cover a broader range of quantum states. However, the
Bloom filter for membership checking in classical computing.
Our experiments demonstrate that our systematic approaches
projection-based assertions require architectural support for
can assert many more quantum states and can be used in various measuring the same qubits repeatedly and performing gate
assertion locations for qubit state checking. operations after measurement. The state-of-the-art publicly
Index Terms—quantum computing, runtime assertion available quantum computers [41], [42] only provide measure-
ments at the end of the program and cannot take advantage
I. I NTRODUCTION of the projection-based assertion scheme. Besides such limi-
tations, another key issue is that all these dynamic assertions
Quantum computing has become an active research area require precise state information before debugging. However,
due to its remarkable potential in chemistry simulation [25], in practical cases, the programmer may not know the exact
cryptography [18], machine learning [9], and many more ap- state to be asserted beforehand. If the programmer selects an
plications. The fast pace of development in quantum software incorrect assertion state, the quantum state would be destroyed
platforms and programming languages has necessitated the after assertion. In short, there are four limitations of the prior
development of program debugging tools. However, quantum assertion schemes: (a) destructive measurement stops program
computing features a number of unique properties including execution, (b) existing dynamic assertion schemes support
superposition and entanglement. These features increase the limited assertion states, (c) repeated measurements require
difficulty of writing correct quantum programs; even quantum architectural support, and (d) asserting quantum states requires
experts may write them incorrectly. Li et al. [30] reported precise assertion state information.
that bugs have been found in the example programs in IBM’s In this paper, we focus on dynamic assertion as it over-
OpenQASM project [16] and Rigetti’s PyQuil project [38]. comes the limitation of destructive measurement. We propose
Previous works by Huang et al. [27], [28] have discussed two systematic approaches: SWAP-based assertion and Non-
several types of bugs in quantum programs. Based on their destructive-discrimination(NDD)-based assertion, for assertion
study, many bugs in the quantum programs are different from circuit design. Both approaches can assert a broad range of
the ones in classical programs. Nevertheless, they showed that quantum states and both can be performed on the existing
similar to debugging classical programs, assertions are very quantum computers (i.e., no need for additional hardware
useful primitives for debugging quantum programs. support). We show that the prior proposed dynamic assertion
192
quantum devices, we usually measure the qubit states in the assertEquals(|ψ , |φ). Approximate assertion, on the other
computational basis. hand, provides a membership check for a set of states.
Compared to prior works, our precise assertion can assert
B. Related work for a wider range of states. First, our precise assertion for
A few approaches have been proposed to provide assertions pure states ensures that any changes in the states, including
for quantum programs. Huang et al. [28] proposed a statis- the coefficients, will raise an assertion error. In the prior
tical approach by measuring the qubits multiple times and entanglement state assertion [32], the state in the form of
compare the probability distribution with that of the desired a |00 + b |11 is asserted using parity checks. As a re-
state. However, directly measuring the qubits may change the sult, it won’t raise an assertion error for an entangled state
quantum state after measurement. Therefore, the statistical with incorrect coefficients. Furthermore, for the GHZ state
assertion cannot be applied at runtime. SWAP-test [13] is |GHZ = √12 |000+ √12 |111 that is widely used in quantum
another candidate for assertions. However, when the ancilla communication [19] and cryptography, the prior primitives
qubit in the swap test is measured as 0, i.e., no assertion error, for asserting entangled state can’t check for it since not all
there is no guarantee that the qubit under test is in the desired the states have even number of ones. Our proposed precise
state. For example, when the qubit under test and the desired assertion overcomes the limitations and can check all such
state are orthogonal (e.g., |0 and |1), the ancilla qubit has cases. Second, we propose assertion schemes for mixed states
a probability of 50% being 0. Therefore, it is not suitable for to further increase the range of states that can be checked.
dynamic runtime assertions. Approximate assertion checks whether a state |ψ is within
a set of states {|φ , |θ , ...}. An assertion error means that the
Liu et al. [32] proposed dynamic runtime assertion primi-
state |ψ is not in the set. It extends the usage of assertions
tives for three different types of states: classical, superposition,
when the programmer only has limited knowledge of the
and specific entanglement states. These assertion primitives
program. For example, in the Deutsch-Jozsa algorithm [17]
are designed such that they can measure the qubits non-
we are given a black-box function f (x). The function is
destructively through the ancilla qubits. The three types of
guaranteed to be either constant (output is always 0 or always
states are represented by state vectors, which means they are
1) or balanced (returns 0 for half of the inputs and 1 for
pure states. However, asserting these three types of state are
the rest). The Deutsch-Jozsa algorithm determines whether
not sufficient, and might lead to an inefficient design. For
the function f (x) is constant or balanced. Since the f (x)
example, the highly entangled states such as Bell states [6],
is a black-box function, the programmer can’t predict the
GHZ states [23] and cluster states [12] are widely used in
output of f (x). If a program bug happens in the black-box
quantum teleportation [7], [14], quantum secret sharing [29],
function f (x), which makes the function neither constant nor
and super dense coding [34]. The prior work only describes
balanced, the existing dynamic assertion schemes can’t assert
assertions for entangled states with even or odd numbers of
for such type of bugs. However, since the constant output states
ones, leaving many types of entanglement states unaddressed.
together with the inputs form a set of states, we can resort to
Moreover, we might want to assert only a few qubits in a large
approximate assertion. If the function is neither constant nor
entangled system. These qubits are usually in a mixed state
balanced, the program will raise an assertion error. We will
since they are likely to be entangled with the other qubits. The
discuss this example in detail in Section X.
lack of a wide range of assertion types necessitates finding a
Our proposed mixed-state assertion and approximate asser-
more generic dynamic assertion scheme.
tion also enable a new trade-off between assertion accuracy
Li et al. [30] proposed assertion schemes based on
and circuit complexity. For example, to check the GHZ state,
projective-measurement. Their assertion schemes are capable
we can use three different assertions as shown in Figure 1.
of asserting a broader type of quantum states including mixed
With our SWAP-based circuit design, 10 CNOT gates are
states. However, their approach inserts measurements at each
needed for the precise assertion for the GHZ state. We can
assertion location and requires the system to operate gates after
use two ways to reduce the circuit cost. First, we assert
measurement. Such an assertion scheme requires architectural
for fewer qubits. Instead of asserting the three-qubit state,
support for measuring the qubits repeatedly and operating
we can assert the last two qubits since our approach allows
gates after measurement. The state-of-the-art publicly available
assertion for such a two-qubit mixed state. With our SWAP-
quantum computers [41], [42] only provide measurement at
based mixed-state assertion, the circuit requires 4 CNOT gates.
the end of the program and cannot take advantage of this
Second, we can disregard the coefficients and assert for a
projection-based assertion scheme.
set of states {|000 , |111} using approximate assertion. Our
SWAP-based approximate assertion circuit requires 8 CNOT
III. P RECISE AND A PPROXIMATE A SSERTION
gates for such approximate assertion. We can expand the
In this work, we propose systematic approaches for both set of states to further reduce the circuit cost. When we
precise and approximate assertions. We consider the assertions extend the set to {|000 , |011 , |100 , |111}, the CNOT gate
that require a quantum state vector |ψ or a density matrix ρ count can be reduced to 4. We can also use the NDD-
as precise quantum state assertion as the state to be asserted based approximate assertion circuit along with a different set
for is precise. The precise assertions can be considered as of states { |000+|111
√
2
, |001+|110
√
2
, |011+|100
√
2
, |010+|101
√
2
}, the
193
cannot capture the bugs with incorrect coefficients (Bug1)
since the phase changes are not directly measurable in the
computational basis. The prior assertion primitives [32] failed
to provide assertion for the GHZ state as discussed before. The
projection-based assertions (Proq) can capture these two bugs
but it requires additional architectural support. In comparison,
our proposed assertions are applicable to the state-of-the-art
quantum computers and our approaches provide the flexibility
for identifying the bug with different circuit costs.
In the next two sections, we will present two systematic
assertion circuit designs: SWAP-based assertion circuit and
NDD-based assertion circuit. We also propose logical OR
based assertion design as an alternative for SWAP-based
Fig. 1: Three different assertions for the GHZ state. The assertion design. For each circuit design approach, we will
precise pure state assertion asserts for the exact three-qubit introduce the designs for precise and approximate assertion.
state, including coefficients and states. The precise two-qubit For all of the designs, the ancilla qubit being |0 means no
mixed state assertion asserts for the mixed state of the last two assertion error, being |1 means assertion error. The reason is
qubits. The approximate three-qubit state assertion asserts for that |1 has higher measurement error and may decay into |0.
a set of states {|000, |111}.
IV. SWAP-BASED A SSERTION C IRCUIT
A. Motivation
1 circuit.u2(0, np.pi, qr[0])
2 circuit.cx(qr[0], qr[1]) To achieve assertEquals(|ψ , |φ), we can use a SWAP-
3 circuit.cx(qr[1], qr[2]) based design as shown in Figure 3. We leverage the fact that
any n-qubit pure state can be generated by applying a unitary
⊗n
Fig. 2: The pseudo code for GHZ state preparation. gate U to the n-qubit ground state |0 . For asserting an n-
⊗n
qubit pure state created by the program |ψ = V1 ...Vn |0
⊗n
equals to the desired state |φ = U |0 , we can apply
resulting circuit only requires 3 CNOT gates. the inverse gate U −1 to transfer the pure state |ψ back
⊗n
Next, let us use the GHZ example to illustrate the capa- to n-qubit state |0 . Then we can measure the qubits to
bilities and the cost of different assertion schemes. Figure 2 verify whether all the qubits are in |0 state. If the n-qubit
⊗n
shows the code for preparing the GHZ state. The code contains state is not |0 , the pure state |ψ was not in the |φ
a single qubit u2 gate on qubit qr[0] and two CNOT gates state before the inverse transformation, and the circuit will
between qubit qr[0], qr[1], and qr[2]. Considering two types raise an assertion error. However, the state-of-the-art quantum
of bugs in the code. Bug1: In line 1, the programmer specifies computers do not support operations after measurement which
incorrect u2 gate parameter order circuit.u2(np.pi, 0, qr[0]). means the program cannot proceed after we measure the
This bug would lead to incorrect coefficients in the output state qubits. A solution to this problem is introducing SWAP gates
√1 |000 − √1 |111. Bug2: The programmer reorders line 2 and ancilla qubits. We can SWAP the qubits under test with
2 2
and 3. This bug will lead to incorrect entanglement in the ancilla qubits and measure the ancilla qubits, as shown in
output state √12 |000 + √12 |011. Table I compares existing Figure 3. The first n-qubits are the qubits under test, and the
assertion schemes with our proposed ones in terms debugging pure state is generated by a sequence of gates V1 ...Vn . We
⊗n
capability and the circuit cost in the number of CNOT (CX) can apply the U −1 gate to convert it to |0 state. Since we
gates and single-qubit gates (SG), the number of ancilla qubits, can’t measure it directly, we introduce SWAP gates to swap the
and the number of measurements. qubits under test with the ancilla qubits. When we measure the
ancilla qubits, all of the ancilla qubits should be in |0 state. In
Assertion type Bug1 Bug2 #CX #SG #ancilla #measure
Stat [28] False True N/A N/A N/A N/A order to regenerate the desired state |φ, we apply the unitary
Primitive [32] N/A N/A N/A N/A N/A N/A gate U to the ancilla qubits before swapping and then swap
Proq [30] True True 4 2 0 3
SWAP-based precise assertion True True 10 2 3 3 this prepared state to the qubits under test.
SWAP-based mixed state assertion
NDD-based approximate assertion
False
True
True
True
4
3
0
2
1
1
1
1
For example, the |+ state is generated by applying the
Hadamard gate H to the |0 state. The Hadamard gate H is
TABLE I: Assertion coverage and circuit cost for different the inverse gate of itself. Therefore, if we want to assert for
assertion schemes. True/False means the assertion can/can’t state |+, the unitary gate U in the assertion circuit should be
detect the corresponding bug. the Hadamard gate. The assertion circuit is shown on the left
side of Figure 4. The assertion circuit proposed by the prior
The statistical assertion [28] measures the probability dis- work [32] is shown on the right side of Figure 4. We can prove
tribution of quantum states. Therefore, it is capable of de- these two circuits are equivalent, and the proof outline is in
tecting incorrect entanglement states (Bug2). However, it Appendix A.
194
Quantum Program Assertion circuit
...
⊗n
|0 V1 Vn |ψ U −1 |φ
...
···
⊗n
|0 U |φ
195
and are eigenvectors of ρ0 with eigenvalues λ0 , λ1 , ..., λn−1 ,
respectively. We use the orthonormal basis formed by the
eigenvectors |ψi . Since some of the eigenvalues might be ···
|ψ U −1 U |ψ
zero, we reorder the eigenvalues and the eigenvectors such that
the first t eigenvalues are non-zero, where t is the rank of the
densitymatrix. Now the density matrix can be represented as
t−1
ρ0 = i=0 λi |ψi ψi |, where λi are the non-zero eigenvalues |0
n−m
196
decomposed into basic operators with one ancilla qubit [5],
[24]
OR gate
|ψ ··· |ψ
U −1 U
|ψ U −1 U |ψ
|0 |0
···
|0
|1
Fig. 10: General scheme of SWAP based assertion circuit for
mixed state assertion when 2n−1 < t < 2n . Fig. 11: Logical OR based assertion circuits.
leading |0. Therefore, we can measure the leading qubit to Although both SWAP-based and logical OR-based designs
assert for the union of “virtually correct” and “correct” states, can detect the incorrect states, their functionalities are slightly
which is functionally equivalent to asserting only the “correct” different. In the SWAP based assertion circuit, we prepare the
states. desired state for the ancilla qubits and swap the qubits under
Similarly, due to the measurement limitation, we need to test with the ancilla qubits. When the qubits under test are not
swap the qubits with the ancilla qubits. When t ≤ 2n−1 , the in the desired state, after the assertion circuit, the state of the
circuit design would be similar to the design in Figure 8. When qubits will be “corrected” to the desired state. However, the
2n−1 < t < 2n we need an extra qubit and the design is shown logical OR based design doesn’t have this property. When the
in Figure 10. ancilla qubit is in |1 state and an assertion error is raised, the
One corner case worth discussion is t = 2n , where the qubits under test are not “corrected” by the assertion circuit.
mixed state is the probability mixture of all the 2n basis states. In the following discussion, we will find that the NDD based
In this case, all the basis states are marked as “correct” states, assertion circuit has the same property as this logical OR based
which means that we can’t assert for this special mixed state. design.
197
2n −1
as U = |ψ0 ψ0 | − i=1 |ψi ψi |. The controlled-U gate of U with eigenvalue of 1, the existing entanglement between
can be generated by adding controls [43] to the unitary gate the qubits under test and others are not affected.
U.
C. Approximate Assertion
Similar to the discussion in Section IV-D, to assert a set of
|ψ U |ψ quantum states {|ψ , |φ , ...}, we can form a density matrix
and use the same process to diagonalize it and calculate the
··· orthonormal basis states. After finding the t orthonormal basis
states, we can treat this problem as asserting for the mixed
|0 H H
state composed of these t basis states. We mark these t basis
Fig. 12: General scheme of NDD-Based assertion circuits. states as “correct” states and follow the assertion process as
Except the assertion circuit shown in Figure 4, all the described for mixed states.
assertion circuits proposed in the prior work [32] can be Based on the idea of parity check, the prior work [32]
categorized as NDD based assertion circuits. For example, in described the circuit for asserting entanglement state with even
order to assert for classical state |0, we can find orthonormal numbers of ones: a |00+b |11. Since coefficients a and b are
basis {|0 , |1} that includes state |0. The matrix U can be not fixed, this can be considered as an approximate assertion
calculated as U = |0 0| − |1 1| = Z. Here Z is the matrix for a set of states with even numbers of ones:{|00 , |11}. We
of the Pauli Z operator. Therefore, the controlled-U gate is will show that our approximate assertion circuit and the circuit
indeed a controlled-Z gate. The corresponding assertion circuit proposed in prior work are equivalent.
is shown on the left side of Figure 13. The assertion circuit First, we form a density matrix from these states with equal
proposed by the prior work [32] is shown on the right side probability: ρ = 12 |00 00| + 12 |11 11|. After performing
of Figure 13. It can be proven that these two circuits are SVD on the density matrix ρ, we can find an orthonormal
equivalent based on the quantum circuit equivalence rules [21]. basis {|00 , |01 , |10 , |11}. Then, we mark |00 and |11
as “correct” states and |01 and |10 as “incorrect” states. The
unitary matrix U is calculated as U = |00 00| + |11 11| −
|0 Z |0 |0 |0 |01 01|−|10 10| = Z ⊗Z. Here Z is the matrix of the Pauli
= Z operator. The controlled-U gate is indeed two controlled-Z
|0 gates. The corresponding assertion circuit is shown on the left
|0 H H
side of Figure 14. The assertion circuit proposed in the prior
Fig. 13: Circuits for asserting |0 state: our circuit is on the work [32] is shown on the right side of Figure 14. It can be
left side; the one in prior work is on the right side. proven that these two circuits are equivalent.
Z
B. Precise Assertion for Mixed States |ψ |ψ |ψ |ψ
The assertion scheme in Figure 12 can be generalized to Z =
assert for mixed states ρ0 . After performing SVD on the |0
density matrix of the mixed state, we can find an orthonormal |0 H H
basis {|ψi }i∈[0,2n −1] and represent the mixed state as a
t−1 Fig. 14: Circuits for asserting |ψ = a |00 + b |11: our circuit
mixture of the basis states: ρ0 = i=0 λi |ψi ψi |. The is on the left side; the one in prior work is on the right side.
number of basis states t should be less than or equal to the total
number of basis 2n . Similar to the discussion in Section IV-C,
the circuit should not raise an assertion error when the qubits VI. D ESIGN COMPARISON
under test are in the t “correct” states from |ψ0 to |ψt−1 . In this section, we compare the assertion coverage and
When the state belongs to the 2n − t “incorrect” states from circuit cost of different designs. We propose three different
|ψt to |ψ2n −1 , the circuit should raise an assertion error. The assertion circuit designs, namely the SWAP based assertion
assertion circuit structure is the same as the design for pure circuit, logical OR based assertion circuit, and NDD based
states shown in Figure 12. We can find a unitary matrix U for assertion circuit. We compare them with the statistical asser-
which the “correct” states are its eigenstate with the eigenvalue tion (Stat) [28], runtime assertion primitives (Primitive) [32],
being 1, and the “incorrect” states are its eigenstates with the and projection-based assertion (Proq) [30].
eigenvalues
t−1 being -1. Then unitary matrix U is calculated as
2 −1 A. Assertion Coverage
U = i=0 |ψi ψi | − i=t |ψi ψi |. The ancilla qubit will
stay in |0, i.e., no assertion error, when the qubits under test The statistical assertions and runtime assertion primitives
is in a probability mixture of the “correct” states. The ancilla support assertion for classical, superposition, and entangle-
qubit will stay in |1, i.e., an assertion error, when the qubits ment states. Nevertheless, the discussions of these three types
under test is in a probability mixture containing “incorrect” of states are not complete. The superposition states with dif-
states. Note that since the expected states are the eigenstates ferent relative phase have not been discussed in the statistical
198
assertion. Beyond that, some types of entanglement states
π
have based assertion circuit consists of two single-qubit gates and
|00+ei 4 |11 a CNOT gate. Therefore, we choose logical OR based design
not been discussed either, for example, state √ . All
2
these three types of quantum states can be represented by for asserting single-qubit pure states.
a state vector, which means that they are pure states. Proq Then, we discuss the cost for asserting arbitrary n-qubit
proposed assertions for both pure states and mixed states but separable state. The qubits in an n-qubit separable state are not
the assertion for a set of states has not been discussed. The entangled and the state can be expressed as the tensor product
coverages of different assertion designs are shown in Table II. of individual single-qubit states:|ψ = |ψ0 ⊗ |ψ1 ... |ψn−1 .
Since our check for the mixed states cannot evaluate the This kind of state often appears as the initial state or the
probabilities, we mark the assertions for mixed states and for output state of quantum programs. Since the qubits in this
a set of states as “Part”. The three designs proposed in this state are not entangled, asserting an n-qubit separable state is
paper have the broadest coverage. equivalent to asserting each qubit individually. For the logical
OR based assertion circuit, the multi-qubit OR gate is indeed a
Assertion state type Stat [28] Primitive [32] Proq [30] SWAP based logical OR based NDD based
Classical ALL ALL ALL ALL ALL ALL multiple open-controlled Toffoli gate. There are multiple ways
Superposition Part ALL ALL ALL ALL ALL
Pure states
Entanglement Part Part ALL ALL ALL ALL to decompose multi-controlled Toffoli gates [4], [33]. Hereby,
Other N/A N/A ALL ALL ALL ALL
Mixed states N/A N/A Part Part Part Part we choose a simple design [35] which consists of 12n + 1
Set of states N/A N/A N/A Part Part Part
CNOT gates and 16n single-qubit gates. For the NDD based
TABLE II: Assertion coverage for different designs. AL- circuit, the number of CNOT gates in the circuit is not fixed,
L/Part/NA means the scheme supports all/part/none of the depending on the actual assertion state.
corresponding quantum states. When asserting for the n-qubit entangled states with even
numbers of ones, for example Bell state |00+|11
√
2
and n-qubit
⊗n ⊗n
B. Assertion Circuit Cost GHZ state |0 √+|1
2
, the NDD based design has the least
gate count among the assertion circuits we propose. The NDD
Since the runtime assertion circuits are inserted in the
based circuit only consists of n CNOT gates. When asserting
quantum programs, the circuit cost is an important factor.
for the entangled gates, the decomposition of unitary gate U in
Intuitively, the SWAP-based assertion circuits may be costly
the SWAP based and logical OR based design will include at
since we are reverting the quantum state to the ground state
least one CNOT gates. Therefore, the gate count for asserting
and recomputing it and the cost of the matrix U may be
entanglement state should be greater than that of the separable
similar to the quantum program itself. This, however, is not
state.
the case. For a quantum program, we don’t know the answer
and need to run the costly circuit to find the answer. In We will introduce the circuit design for asserting constant
comparison, for asserting a known state, the circuit could be functions in Section X. In that case, the SWAP based design
much smaller. As shown in the prior work [36], an n-qubit has the least number of gates.
gate decomposition reaching the lower bound of O(4n ) CNOT Since each design performs the best for their special cases,
gates can lead to state preparation with O(2n ) CNOT gates. none of the designs outperforms the rest for every situation.
Since the cost of the SWAP gates and the logical OR gates In our assertion function, the programmer can specify the
with one ancilla qubit both scale linearly O(n), the resulting assertion circuit design. The programmer can also let the
SWAP-based assertion circuit will have O(2n ) CNOT gates assertion function to estimate the circuit cost of each design
which is much smaller than the original circuit. In addition, and select the design with the lowest cost.
quantum machine learning [3], [39] has been used to prepare
VII. M ETHODOLOGY
high fidelity quantum states with low depth circuits.
Given that the unitary gates in the designs need to be Qiskit [2] is an open-source quantum computing framework.
decomposed to the basis gates and different decomposition We augmented the Qiskit version 0.18.0 with the function
will result in different quantum gate counts, it is difficult to to insert assertion circuits. We also provided several tools
judge the circuit cost for an arbitrary state. Here, we present for finding orthonormal basis and calculating the number of
the circuit cost comparison for several common cases. We use basis states. The augmented version of Qiskit is publicly
four metrics: number of CNOT gates (#CX), number of single- available [1]. The assertion function is in the form:
qubit gates (#SG), number of ancilla qubits (#ancilla), number assert(circuit,qubitList,stateSet,design)
of measurements (#measure). The resulting cost of each circuit The assertion function takes four arguments. The first ar-
design is shown in Table III. gument “circuit” specifies the quantum circuit. The second
First, we discuss the cost for asserting arbitrary single-qubit argument “qubitList” specifies a list of qubits under test. The
pure states. The single-qubit state assertion is like the basic “stateSet” argument represents a set of desired state variables.
unit of assertions. The circuit cost for each design is shown The state variable can be either a vector representing a pure
in Table III. Even though the proq design has the least cost, it state or a matrix representing a mixed state. If the size of
is not applicable to the recent quantum computers.When there the set is one, the assertion function will assert for that
is only one qubit under test, the logical OR gate in Figure 11 particular state. If the size of the set is greater than one,
can be simplified to an open CNOT gate. The logical OR the assertion function will use approximate assertion to assert
199
Primitive [32] Proq [30] SWAP based Logical OR based NDD based
State single separable even single separable even single separable even single separable even single separable even
#CX 2 2n n 0 0 >0 3 3n > 3n 1 12n+1 > 12n + 1 2 State dependent n
#SG 6 12n 0 2 2n ≥ 2n 2 2n ≥ 2n 2 16n ≥ 16n 6 State dependent 0
#ancilla 1 n 1 0 0 0 1 n n 1 1 1 1 1 1
#measure 1 n 1 1 n n 1 n n 1 1 1 1 1 1
for the set of states. The “design” argument specifies the gates with recursion, and de-allocation of ancillary qubits.
circuit design among the three different designs we proposed. As another example, we illustrate debugging the gates with
If the “design” argument is set to NONE, the function will recursion in Appendix D.
estimate the circuit cost of each design and select the design
with the least CNOT gate count. We use the UnitaryGate IX. D EBUGGING C ASE S TUDY: Q UANTUM P HASE
function from Qiskit to generate gates from unitary matrices. E STIMATION
We perform our experiments on a 15-qubit quantum computer In this section, we use quantum phase estimation (QPE)
ibmq-melbourne and the qasm simulator from Qiskit Aer. Each to showcase that our proposed circuits significantly improve
experiment is executed for 8192 shots. the effectiveness of assertions. In subsection IX-A, we use
several case studies to demonstrate the debugging process. In
1 2 3 4 5 6 subsection IX-B, we run the experiments on a 15-qubit real
H quantum device to show our work is practical for near-term
quantum computers.
H
⊗4
|0 QF T †
H 1 #n−qubit quantum phase estimation
2 qr = QuantumRegister(4)
H 3 #ancilla qubit
4 ar = QuantumRegister(1)
|ψ U2
0
U2
1
U2
2
U2
3
|ψ 5 cr = ClassicalRegister(4)
6 circuit = QuantumCircuit(qr, ar, cr)
7
Fig. 15: Quantum phase estimation circuit.
8 # Precalculated state vectors for each assertion
9 stateVectorList = [V1, V2, ..., V6]
VIII. G ENERAL A PPLICABILITY 10
Quantum programs usually operate on a finite number of 11 # Initialize the n−qubits to |+> state
12 circuit.h(qr[0:4])
qubits. These qubits only entangle with the other qubits within
13
the system, thus the system stays in a pure state for every 14 # Assertion slot 1, we choose the logical OR based design:
instruction. As a result, our systematic assertion scheme can 15 qubitList = [qr[0],qr[1]...,qr[3], ar[0]]
essentially assert the state after every instruction. Using n- 16 assert(circuit, qubitList, set(V1), ”ORbased”)
qubit QPE as an example, there are n+2 slots available for 17
3
assertion as illustrated in Figure 15, which shows the 4- 18 # Controlled U 2 gate
qubit QPE circuit. The corresponding code for the 4-qubit 19 for j in range(4):
20 circuit.cu3(0, 0, 2j × π8 , qr[j], ar[0])
QPE algorithm is shown in Figure 16. In this example, all 21 #Assertion slot 2 to slot 5, the function choose best design
the assertions are precise assertions since we can calculate 22 assert(circuit, qubitList, set(Vj+2), NONE)
qubit state vectors beforehand. With the capability to assert 23
for mixed states or a set of states, we also have the flexibility 24 # 4−qubit inverse QFT
to choose different assertions, from single-qubit state assertion 25 iQFT(circuit, qr, 4)
26
to multi-qubit state assertion, and from precise state assertion 27 # Assertion slot 6:
to approximate assertion. 28 assert(circuit, qubitList, set(V6), NONE)
While there is a variety of quantum programs, the quantum 29
programs usually share common subroutines or program pat- 30 circuit.measure(qr, cr)
terns. For example, the phase-kickback circuit is widely used
in quantum algorithms such as Shor’s algorithm [40], phase Fig. 16: The pseudo code for 4-qubit QPE with assertions.
estimation algorithm [15], Deutsch algorithm [17], Bernstein-
Vazirani algorithm [8], etc. In the next section, we will use the
QPE circuit as a case study to introduce the debugging process A. Assertions for Program Debugging
for the phase-kickback subroutine. The debugging scheme Given a unitary operator U and its eigenstate |ψ, U |ψ =
is applicable to the other algorithms that contain the same eiθ |ψ, QPE estimates the phase θ of the corresponding
subroutine. Similarly, many quantum programs also share eigenvalue eiθ . The circuit consists of the superposition pre-
the same program pattern. The prior paper [28] points out condition (line 12), a phase-kickback subroutine (line 19-
common program patterns such as superposition precondition, 20) and an inverse QFT subroutine (line 25). Let’s consider
200
the 4-qubit QPE program for which the unitary operator We can also assert mixed states with even fewer qubits
U is u3(0, 0, π8 ) gate operator with the eigenstates |0 and to reduce the circuit cost. There’s a tradeoff in using mixed
|1. The state |ψ is in the superposition of the eigenstates: state assertions: the fewer qubits we assert, the lower assertion
|ψ = √12 |0 + √12 |1. Assuming the following two types of circuit cost, but the higher chance to miss program bugs.
bugs at line 20. Bug1: the programmer didn’t include the loop 3) Approximate Assertion: We can use approximate asser-
index at line 20: circuit.cu3(0, 0, π8 ). Due to this mistake in tion to reduce the circuit cost. For example, the assertion at slot
rotation angles, slot3, 4, and 5 in Figure 15 will have incorrect 5 asserts for pure state |φ5 = √12 |+ + ++ |0 + √12 |θ4 |1.
quantum states. Bug2: the programmer missed the word “c” We can use approximate assertion to assert for a set of two
when coding for “cu3” gate, which makes all the controlled- states: {|+ + ++ |0 , |θ4 |1}. Both of the bugs will lead
u3 gates become single qubit gate u3. This is a bug due to to states outside this set, therefore, we can capture both bugs
choosing incorrect gates, and slots 2 to 5 will have incorrect with less circuit cost using approximate assertion.
quantum states. These two bugs can’t be captured by the prior
primitives since they do not support the assertion for the states B. Experiment on Real Quantum Computer
from slots 2 to 5.
We run our experiment on a 15-qubit quantum computer
1) Pure State Assertion: We can assert for all the five ibmq-melbourne. When running the assertion circuit on real
qubits. For example, the qubit state at slot 5 in Figure 15 is quantum computers, we actually check for the errors caused
a five qubit pure state:|φ5 = √12 |+ + ++ |0 + √12 |θ4 |1, by both system noise and program bugs. To compare with the
iπ iπ
where |θ4 = (|0 + eiπ |1)(|0 + e 2 |1)(|0 + e 4 |1)(|0 + prior work [32], we assert for single-qubit pure state |ψ at
iπ
e 8 |1). For bug1, the assertion at slot 1, and 2 will not slot 6. We set the gate cu3(2j × π8 , 0, 0) and the eigenstate
raise an assertion error, but the assertion at other slots will. |ψ = √12 |0 + √i2 |1. The swap-based design only requires
Therefore, we can check the gates between slot 2 and slot 3 two CNOTs and two single-qubit gates. On the other hand,
to locate the bug. For bug2, only the assertion at slot 1 will the assertion primitives in the prior work requires two CNOTs
not raise an assertion error. Therefore, we can check the gates and six single-qubit gates. Hereby, we reduced the number of
between slot 1 and slot 2 to pinpoint the bug. We verified the single-qubit gates by four. When we don’t introduce any bug,
results on both a noise-free simulator and a noisy simulator to due to the system noise, 36% of the output states have assertion
simulate the ideal and real cases. errors. When we insert a bug (changing the controlled-u3
2) Mixed State Assertion: The assertion at slot 5 asserts gate to circuit.cu3(0, 2j × π8 , 0, qr[j], ar[0])), 45% of the
for a five-qubit pure state:|φ5 . The circuit for asserting five output states have assertion errors. Therefore, we can detect
qubits can be costly. For example, the SWAP based assertion the program bug based on the increment in the number of
circuit requires 26 CNOT gates. In this case, our mixed state assertion errors. In comparison, the circuit using the assertion
assertion offers flexibility to assert for fewer qubits. primitives in the prior work [32] has 42% assertion errors and
The first four qubits’ state is a mixed state, and we can 50% assertion errors after inserted a bug.
calculate it by taking the partial trace: ρ1234 = tr5 (|φ5 φ5 |) The prior work highlights that the assertion circuit can also
= 12 |+ + ++ + + ++| + 12 |θ4 θ4 |. Since the state |θ4 is be used to improve the success rate. The success rate of the
orthogonal to state |+ + ++, they are eigenvectors of the original circuit is 19%. After filtering out the erroneous results
mixed state. We can diagonalize the density matrix ρ1234 to using the assertion primitives, the success rate improves to
find the orthonormal basis that includes |θ4 and |+ + ++ 33%. In comparison, our lower-cost assertion circuit further
as basis states. We mark state |+ + ++ and |θ4 as “correct” improves the success rate to 36%.
states, which also means that the number of decomposed basis
states is 2. The circuit for asserting this four-qubit mixed state X. D EBUGGING C ASE S TUDY: P ROGRAMS WITH LIMITED
requires 20 CNOTs, less costly than asserting all five qubits. KNOWLEDGE OF STATES
Similar to our pure state discussion, we assume two types In this section, we use the Deutsch-Jozsa algorithm to
of bugs at line 20. When the loop index is not included show the effectiveness of approximate assertion for debugging
(Bug1), the five qubit state at slot 5 would be a different state programs with limited knowledge of states. None of the
|φ5 = √12 |+ + ++ |0 + √12 |θ4 |1, where |θ4 = (|0 + existing works supports this kind of assertion.
iπ iπ iπ iπ
e 8 |1)(|0 + e 8 |1)(|0 + e 8 |1)(|0 + e 8 |1). The four In the Deutsch-Jozsa algorithm [17], we are given a black-
qubit mixed state will be ρ1234 = 12 |+ + ++ + + ++| + box function f (x), which takes n-bit binary values as input and
1
2 |θ4 θ4 |. When we represent the state with the basis states, produces either 0 or 1 as output for each value. The function
we can find that besides basis state |+ + ++ and |θ4 , the is guaranteed to be constant (output is always 0 or always 1)
state has other components, which means the mixed state has or balanced (returns 0 for half of the inputs and 1 for the rest
“incorrect” basis states. Our mixed state assertion circuit will of the inputs). When we try to debug the program, we can’t
raise an assertion error as a result. For bug2, the first four predict the output of the black-box function f (x). If a program
qubits are not entangled with the fifth qubit, and the state is bug happens in the black-box function f (x), which makes the
|+ + ++. Since it is the “correct” basis state, our mixed state function neither constant nor balanced, the existing dynamic
assertion would not report an assertion error. assertion schemes can’t detect such a bug.
201
(a) Constant function (b) Inconstant function
Fig. 17: The results for asserting the constant function and inconstant function.
First two qubit state |00 |01 |10 |11
We can assert the function in two steps: 1) initialize all Constant
|0
|1
|0
|1
|0
|1
|0
|1
the input qubits in |+ state, which is the superposition of all |0
|0
|0
|1
|1
|0
|1
|1
Third qubit state
possible inputs, and 2) assert for the joint output state of the Balanced
|1
|0
|0
|1
|0
|1
|1
|0
function: |ψ = |x |f (x). When the function is constant, the |1
|1
|0
|1
|1
|0
|0
|0
joint output state is either |x |0 or |x |1, and these states
form a set of states. Similarly, the balanced functions form TABLE IV: Set of constant and balanced output states.
another set of states. We can use approximate assertion to
assert for the joint state |ψ is in the constant state set, balanced
state set, or the combined constant and balanced set. and the result is shown in Figure 17b. In comparison, the result
For example, we have a hidden function f (x) with two input when f (x) is a constant function is shown in Figure 17a. In
qubits. We set the input state x in state |++, which is the the figure, the first two qubits are the ancilla qubits. When
superposition of all possible inputs: |++ = 12 (|00 + |01 + the function is not a constant function, the ancilla qubit has a
|10 + |11). If the function is a constant function, the output chance to be |1 state, and the circuit will raise assertion error.
qubit is either all |0 or all |1 for any possible input. The joint The reason that it does not raise an assertion error 100% of
output state |x |f (x) has two possibilities: 12 (|000+|010 + the time is that the state 12 (|000 + |010 + |100 + |111) is
|100 + |110) or 12 (|001 + |011 + |101 + |111). These two not orthogonal to the constant states meaning that it still has
possible states are shown in the first two rows in Table IV. constant components.
We can use the approximate assertion to assert for this set of XI. C ONCLUSIONS
constant output states. If the function is a balanced function, In this paper, we proposed two systematic approaches,
the possible output states are shown in Table IV from row 3 to namely SWAP-based and NDD-based designs for quantum
row 8. We can use the approximate assertion function to assert state runtime assertion. The systematic approaches are capable
for constant state set, balanced state set, or both of them. of asserting a broader range of quantum states than existing
We use the constant state set as an example to illustrate works. We present detailed analysis of the efficiency and
our assertion circuit designs. The first design is SWAP based effectiveness of different assertion schemes. We also introduce
assertion circuit. Our assertion function calculates that the the idea of approximate assertion, which performs membership
unitary gate U in the SWAP based design consists of two check on a set of states. We showcase that both the precise
Hadamard gates on the first two qubits U = H ⊗ H ⊗ I. The and approximate assertions can help with debugging quantum
resulting assertion circuit is shown in Appendix C. It consists programs.
of four CNOT gates and four single-qubit gates. The second
design is logical OR based assertion circuit. The unitary gate ACKNOWLEDGMENT
U is the same as the gate in the SWAP based design. The We thank the anonymous reviewers for their valuable com-
assertion circuit consists of six CNOT gates and twelve single- ments. This work is funded in part by NSF grants 1717550
qubit gates. The third design is NDD based assertion circuit. and 1908406.
After decomposing the controlled U gate, the assertion circuit
consists of fourteen CNOT gates and twenty single-qubit gates. R EFERENCES
Among these three circuits, the SWAP based assertion circuit [1] https://github.com/revilooliver/Systematic-Approaches-for-Precise-and-
has the least number of gates. Therefore, we prefer to use the Approximate-Quantum-State-Runtime-Assertion, 2020.
[2] H. Abraham, I. Y. Akhalwaya, G. Aleksandrowicz, T. Alexander,
SWAP based assertion circuit for asserting n-qubit constant G. Alexandrowics, E. Arbel, A. Asfaw, C. Azaustre, AzizNgoueya,
function. P. Barkoutsos, G. Barron, L. Bello, Y. Ben-Haim, D. Bevenius, L. S.
If the function f (x) is not a constant function due to Bishop, S. Bolos, S. Bosch, S. Bravyi, D. Bucher, F. Cabrera, P. Calpin,
L. Capelluto, J. Carballo, G. Carrascal, A. Chen, C.-F. Chen, R. Chen,
program bugs, our assertion circuit will raise assertion errors. J. M. Chow, C. Claus, C. Clauss, A. J. Cross, A. W. Cross, S. Cross,
For example, when the output state is 12 (|000+|010+|100+ J. Cruz-Benito, C. Culver, A. D. Córcoles-Gonzales, S. Dague, T. E.
|111), f (x) is zero for three input states and 1 for one input Dandachi, M. Dartiailh, DavideFrr, A. R. Davila, D. Ding, J. Doi,
E. Drechsler, Drew, E. Dumitrescu, K. Dumon, I. Duran, K. EL-Safty,
state. The function is neither constant nor balanced. We run E. Eastman, P. Eendebak, D. Egger, M. Everitt, P. M. Fernández,
the program with assertion for constant set on Qiskit simulator, A. H. Ferrera, A. Frisch, A. Fuhrer, M. GEORGE, J. Gacon, Gadi,
202
B. G. Gago, J. M. Gambetta, A. Gammanpila, L. Garcia, S. Garion, Journal of Physics A: Mathematical and Theoretical, vol. 42, no. 11, p.
J. Gomez-Mosquera, S. de la Puente González, J. Gorzinski, I. Gould, 115303, 2009.
D. Greenberg, D. Grinko, W. Guan, J. A. Gunnels, M. Haglund, I. Haide, [15] R. Cleve, A. Ekert, C. Macchiavello, and M. Mosca, “Quantum algo-
I. Hamamura, V. Havlicek, J. Hellmers, Ł. Herok, S. Hillmich, H. Horii, rithms revisited,” Proceedings of the Royal Society of London. Series A:
C. Howington, S. Hu, W. Hu, H. Imai, T. Imamichi, K. Ishizaki, Mathematical, Physical and Engineering Sciences, vol. 454, no. 1969,
R. Iten, T. Itoko, A. Javadi, A. Javadi-Abhari, Jessica, K. Johns, pp. 339–354, 1998.
T. Kachmann, N. Kanazawa, Kang-Bae, A. Karazeev, P. Kassebaum, [16] I. Corporation, “Gate and operation specification for quantum circuits,”
S. King, Knabberjoe, A. Kovyrshin, R. Krishnakumar, V. Krishnan, https://github.com/Qiskit/openqasm, 2019, [Online; accessed 3-9-2020].
K. Krsulich, G. Kus, R. LaRose, R. Lambert, J. Latone, S. Lawrence, [17] D. Deutsch and R. Jozsa, “Rapid solution of problems by quantum
D. Liu, P. Liu, Y. Maeng, A. Malyshev, J. Marecek, M. Marques, computation,” Proceedings of the Royal Society of London. Series A:
D. Mathews, A. Matsuo, D. T. McClure, C. McGarry, D. McKay, Mathematical and Physical Sciences, vol. 439, no. 1907, pp. 553–558,
D. McPherson, S. Meesala, M. Mevissen, A. Mezzacapo, R. Midha, 1992.
Z. Minev, A. Mitchell, N. Moll, M. D. Mooring, R. Morales, N. Moran, [18] M. Ekerå and J. Håstad, “Quantum algorithms for computing short dis-
P. Murali, J. Müggenburg, D. Nadlinger, K. Nakanishi, G. Nannicini, crete logarithms and factoring rsa integers,” in International Workshop
P. Nation, Y. Naveh, P. Neuweiler, P. Niroula, H. Norlen, L. J. O’Riordan, on Post-Quantum Cryptography. Springer, 2017, pp. 347–363.
O. Ogunbayo, P. Ollitrault, S. Oud, D. Padilha, H. Paik, S. Per- [19] A. Farouk, J. Batle, M. Elhoseny, M. Naseri, M. Lone, A. Fedorov,
riello, A. Phan, F. Piro, M. Pistoia, A. Pozas-iKerstjens, V. Prutyanov, M. Alkhambashi, S. H. Ahmed, and M. Abdel-Aty, “Robust general
D. Puzzuoli, J. Pérez, Quintiii, R. Raymond, R. M.-C. Redondo, n user authentication scheme in a centralized quantum communication
M. Reuter, J. Rice, D. M. Rodrı́guez, RohithKarur, M. Rossmannek, network via generalized ghz states,” Frontiers of Physics, vol. 13, no. 2,
M. Ryu, T. SAPV, SamFerracin, M. Sandberg, H. Sargsyan, N. Sathaye, p. 130306, 2018.
B. Schmitt, C. Schnabel, Z. Schoenfeld, T. L. Scholten, E. Schoute, [20] J. N. Franklin, Matrix theory. Courier Corporation, 2012.
J. Schwarm, I. F. Sertage, K. Setia, N. Shammah, Y. Shi, A. Silva, [21] J. C. Garcia-Escartin and P. Chamorro-Posada, “Equivalent quantum
A. Simonetto, N. Singstock, Y. Siraichi, I. Sitdikov, S. Sivarajah, M. B. circuits,” arXiv preprint arXiv:1110.2998, 2011.
Sletfjerding, J. A. Smolin, M. Soeken, I. O. Sokolov, SooluThomas,
[22] G. H. Golub and C. F. Van Loan, Matrix computations. JHU press,
D. Steenken, M. Stypulkoski, J. Suen, K. J. Sung, H. Takahashi,
2012, vol. 3.
I. Tavernelli, C. Taylor, P. Taylour, S. Thomas, M. Tillet, M. Tod,
[23] D. M. Greenberger, M. A. Horne, and A. Zeilinger, “Going beyond
E. de la Torre, K. Trabing, M. Treinish, TrishaPe, W. Turner, Y. Vaknin,
bell’s theorem,” in Bell’s theorem, quantum theory and conceptions of
C. R. Valcarce, F. Varchon, A. C. Vazquez, D. Vogt-Lee, C. Vuillot,
the universe. Springer, 1989, pp. 69–72.
J. Weaver, R. Wieczorek, J. A. Wildstrom, R. Wille, E. Winston, J. J.
Woehr, S. Woerner, R. Woo, C. J. Wood, R. Wood, S. Wood, J. Wootton, [24] Y. He, M.-X. Luo, E. Zhang, H.-K. Wang, and X.-F. Wang, “Decomposi-
D. Yeralin, R. Young, J. Yu, C. Zachow, L. Zdanski, C. Zoufal, tions of n-qubit toffoli gates with linear circuit complexity,” International
Zoufalc, a matsuo, azulehner, bcamorrison, brandhsn, chlorophyll zz, Journal of Theoretical Physics, vol. 56, no. 7, pp. 2350–2361, 2017.
dan1pal, dime10, drholmie, elfrocampeador, enavarro51, faisaldebouni, [25] C. Hempel, C. Maier, J. Romero, J. McClean, T. Monz, H. Shen,
fanizzamarco, gadial, gruu, kanejess, klinvill, kurarrr, lerongil, ma5x, P. Jurcevic, B. P. Lanyon, P. Love, R. Babbush et al., “Quantum
merav aharoni, michelle4654, ordmoj, sethmerkel, strickroman, sumit- chemistry calculations on a trapped-ion quantum simulator,” Physical
puri, tigerjack, toural, vvilpas, welien, willhbang, yang.luh, yelojakit, Review X, vol. 8, no. 3, p. 031022, 2018.
and yotamvakninibm, “Qiskit: An open-source framework for quantum [26] R. A. Horn and C. R. Johnson, “Norms for vectors and matrices,” Matrix
computing,” 2019. analysis, pp. 313–386, 1990.
[3] J. M. Arrazola, T. R. Bromley, J. Izaac, C. R. Myers, K. Brádler, [27] Y. Huang and M. Martonosi, “Qdb: From quantum algorithms towards
and N. Killoran, “Machine learning method for state preparation and correct quantum programs,” arXiv preprint arXiv:1811.05447, 2018.
gate synthesis on photonic quantum computers,” Quantum Science and [28] Y. Huang and M. Martonosi, “Statistical assertions for validating patterns
Technology, vol. 4, no. 2, p. 024004, 2019. and finding bugs in quantum programs,” in Proceedings of the 46th
[4] J. M. Baker, C. Duckering, A. Hoover, and F. T. Chong, “Decomposing International Symposium on Computer Architecture, 2019, pp. 541–553.
quantum generalized toffoli with an arbitrary number of ancilla,” arXiv [29] S. Jain, S. Muralidharan, and P. K. Panigrahi, “Secure quantum con-
preprint arXiv:1904.01671, 2019. versation through non-destructive discrimination of highly entangled
multipartite states,” EPL (Europhysics Letters), vol. 87, no. 6, p. 60008,
[5] A. Barenco, C. H. Bennett, R. Cleve, D. P. DiVincenzo, N. Margolus,
2009.
P. Shor, T. Sleator, J. A. Smolin, and H. Weinfurter, “Elementary gates
for quantum computation,” Physical review A, vol. 52, no. 5, p. 3457, [30] G. Li, L. Zhou, N. Yu, Y. Ding, M. Ying, and Y. Xie, “Proq: Projection-
1995. based runtime assertions for debugging on a quantum computer,” arXiv
preprint arXiv:1911.12855, 2019.
[6] J. S. Bell, “On the einstein podolsky rosen paradox,” Physics Physique
[31] J. Liu, L. Bello, and H. Zhou, “Relaxed peephole optimization: A novel
Fizika, vol. 1, no. 3, p. 195, 1964.
compiler optimization for quantum circuits,” in Proceedings of the 2021
[7] C. H. Bennett, G. Brassard, C. Crépeau, R. Jozsa, A. Peres, and W. K. International Symposium on Code Generation and Optimization, 2021.
Wootters, “Teleporting an unknown quantum state via dual classical
[32] J. Liu, G. T. Byrd, and H. Zhou, “Quantum circuits for dynamic runtime
and einstein-podolsky-rosen channels,” Physical review letters, vol. 70,
assertions in quantum computation,” in Proceedings of the Twenty-Fifth
no. 13, p. 1895, 1993.
International Conference on Architectural Support for Programming
[8] E. Bernstein and U. Vazirani, “Quantum complexity theory,” SIAM Languages and Operating Systems, 2020, pp. 1017–1030.
Journal on computing, vol. 26, no. 5, pp. 1411–1473, 1997. [33] D. M. Miller, R. Wille, and Z. Sasanian, “Elementary quantum gate
[9] J. Biamonte, P. Wittek, N. Pancotti, P. Rebentrost, N. Wiebe, and realizations for multiple-control toffoli gates,” in 2011 41st IEEE In-
S. Lloyd, “Quantum machine learning,” Nature, vol. 549, no. 7671, pp. ternational Symposium on Multiple-Valued Logic. IEEE, 2011, pp.
195–202, 2017. 288–293.
[10] B. H. Bloom, “Space/time trade-offs in hash coding with allowable [34] S. Muralidharan and P. K. Panigrahi, “Perfect teleportation, quantum-
errors,” Commun. ACM, vol. 13, no. 7, pp. 422–426, 1970. [Online]. state sharing, and superdense coding through a genuinely entangled five-
Available: https://doi.org/10.1145/362686.362692 qubit state,” Physical Review A, vol. 77, no. 3, p. 032321, 2008.
[11] K. Blum, Density matrix theory and applications. Springer Science & [35] M. A. Nielsen and I. Chuang, “Quantum computation and quantum
Business Media, 2012, vol. 64. information,” 2002.
[12] H. J. Briegel and R. Raussendorf, “Persistent entanglement in arrays of [36] M. Plesch and Č. Brukner, “Quantum-state preparation with universal
interacting particles,” Physical Review Letters, vol. 86, no. 5, p. 910, gate decompositions,” Physical Review A, vol. 83, no. 3, p. 032302,
2001. 2011.
[13] H. Buhrman, R. Cleve, J. Watrous, and R. de Wolf, “Quantum finger- [37] E. Rieffel and W. Polak, “An introduction to quantum computing for
printing,” Physical Review Letters, vol. 87, no. 16, Sep 2001. [Online]. non-physicists,” ACM Computing Surveys (CSUR), vol. 32, no. 3, pp.
Available: http://dx.doi.org/10.1103/PhysRevLett.87.167902 300–335, 2000.
[14] S. Choudhury, S. Muralidharan, and P. K. Panigrahi, “Quantum tele- [38] Rigetti, “A Python library for quantum programming using Quil,”
portation and state sharing using a genuinely entangled six-qubit state,” https://github.com/rigetti/pyquil, 2019, [Online; accessed 3-9-2020].
203
[39] K. K. Sabapathy, H. Qi, J. Izaac, and C. Weedbrook, “Production A PPENDIX A
of photonic universal quantum gates enhanced by machine learning,” A SSERTION CIRCUIT EQUIVALENCE
Physical Review A, vol. 100, no. 1, p. 012326, 2019.
[40] P. W. Shor, “Polynomial-time algorithms for prime factorization and The prior work [21] has proved the two circuits shown in
discrete logarithms on a quantum computer,” SIAM review, vol. 41, no. 2, Figure 18 are equivalent.
pp. 303–332, 1999.
[41] I. Q. team, “IBM Q 16 Melbourne V1.1.0 backend specification V1.1.0,”
https://quantum-computing.ibm.com, 2019, [Online; accessed 3-9-2020].
[42] R. F. team, “Forest SDK,” https://www.rigetti.com/forest, 2019, [Online; H H
accessed 3-9-2020].
[43] X.-Q. Zhou, T. C. Ralph, P. Kalasuwan, M. Zhang, A. Peruzzo, B. P. =
Lanyon, and J. L. O’brien, “Adding control to arbitrary unknown
quantum operations,” Nature communications, vol. 2, no. 1, pp. 1–8, H H
2011.
Fig. 18: H gates mirroring through CNOT inversion
|+ H |+
=
|0 H
A PPENDIX B
O RTHONORMAL BASIS T RANSFORMATION
Proposition: In an n-qubit quantum system, given
two orthonormal quantum bases {|ψi }i∈[0,2n −1] and
{|ψi }i∈[0,2n −1] , we can always find a unitary gate U that
transforms each basis state |ψi to the corresponding state
|ψi in the other basis. In other words, |ψi = U |ψi for
every i in [0, 2n − 1]. The unitary gate U can be calculated
2n −1
as U = i=0 |ψi ψi |.
Proof: An orthonormal basis {|ψi } have following proper-
ties: (1)ψi |ψj = 1 for all i = j, and ψi |ψj = 0 for all i =
2n −1
j. (2) i=0 |ψi ψi | = I, here I is the identity matrix. First,
we need to prove |ψ = U |ψi for every i ∈n [0, 2n −1]. Based
2 −1
on the first property,
we have U |ψi = j=0 |ψj ψj |ψi =
|ψi ψi |ψi + i=j |ψj ψj |ψi = |ψi . Then, we need to
prove that the matrix U is a unitary matrix such that we
can find a corresponding gate. Based 2non the first and the
−1
second property, we have U U † = |ψ
i ψi | |ψi ψi |
2n −1 i=0
= i=0 |ψi ψi | = I. Similarly, we can prove U † U = I.
Therefore, U is a unitary matrix.
204
The computational basis is indeed an orthonormal basis. entanglement will lead to states out of the expected set of
In our assertion circuit, we transform an orthonormal basis states and the bug is detectable with approximate assertions
that includes the assertion state |ψ to the computational as well. We confirmed such analysis with experiments using
basis. Note that we can change the order of basis states in a quantum simulator.
computational basis to form a new correspondence, the unitary
gate U will change accordingly.
A PPENDIX C
SWAP BASED CIRCUIT FOR ASSERTING CONSTANT SET
H H
|ψconst H H |ψconst
⊗2
|0
A PPENDIX D
D EBUGGING C ASE S TUDY: C ONTROLLED A DDER
S UBROUTINE
In quantum computing, it is a common practice to use
recursion of quantum operations. Bugs may happen when
coding for such recursive patterns. In Figure 21, we show
the pseudo code for controlled adder using QFT. The program
computes qr = a + qr, where a is a constant integer with
length “width” and qr is an integer encoded in Fourier space
with “width” numbers of qubits.
Fig. 21: The pseudo code for controlled adder using QFT
205