Systematic Approaches For Precise and Approximate Quantum State Runtime Assertion

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

2021 IEEE International Symposium on High-Performance Computer Architecture (HPCA)

Systematic Approaches for Precise and


Approximate Quantum State Runtime Assertion
Ji Liu Huiyang Zhou
North Carolina State University North Carolina State University
Raleigh, United States Raleigh, United States
[email protected] [email protected]

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

2378-203X/21/$31.00 ©2021 IEEE 191


DOI 10.1109/HPCA51647.2021.00025
circuits by Liu et al. [32] are actually special cases of our described with a vector. Instead, it is described with a density
systematic approaches. We analyze the efficiency and effec- matrix ρ = i Pi |ψi  ψi |, where Pi are the probabilities of
tiveness of both approaches and show that either design has each pure state |ψi  and i Pi = 1. The pure state |ψ can
its own advantages. also be represented with a density matrix: ρ = |ψ ψ|.
We further introduce the idea of approximate quantum state Multiple qubits are entangled if the state can not be rep-
assertion. It can be used for cases when the programmer has resented with a tensor product of individual qubit states. In
limited knowledge of the quantum states. For example, the general, suppose we have a system containing two subsystems
programmer expects that the quantum state needs to satisfy A and B, the state of the entire system AB is described by
some requirements but does not have the knowledge of the a state vector |ψAB  or a density matrix ρAB . When the
exact quantum state. We propose approximate assertion as state of the entire system can be represented as the tensor
a membership check w.r.t. to a set of quantum states {|ψ, product of individual subsystems, i.e., |ψAB  = |ψA  ⊗ |ψB ,
|φ, |θ,...}. An assertion failure means that the checked state subsystem A is not entangled with subsystem B or system AB
is not in the set while no assertion error means that the is separable. If system AB can’t be represented as the tensor
checked state is within the set but not sure which state. When product of individual subsystems, subsystem A is entangled
we construct the set to be a superset of the expected states, with subsystem B, and the system AB is not separable. The
approximate quantum state assertion is somewhat analogous state of subsystem A can be derived from the partial trace of
to the well-known Bloom filter [10] in classical computing. the whole system AB: ρA ≡ trB (ρAB ).
We also present the design methodology of approximate Mixed states are not necessarily entangled states. When de-
assertion circuits using both the SWAP-based and NDD-based termining the mixedness of a system, the entanglement within
approaches. the system doesn’t matter. For example, we have a 3-qubit sys-
The major contributions of our paper are as follows: tem with the state |ψ123  = √12 (|00+|11)⊗|0, which means
• We propose two systematic approaches for dynamic as- that the first qubit is entangled with the second qubit while
sertion, which essentially generalize the prior (ad hoc) the third qubit is not entangled with the first two. The density
assertion schemes. matrix of the system is ρ123 = 12 (|00 00|+|11 11|)⊗|0 0|.
• We expand the assertion types from classical, superposi- If we consider the system of the first two qubits, since they are
tion, and entanglement state to a wider range including not entangled with the third qubit, the two-qubit system is in
arbitrary pure states and many mixed states. a pure state, which can be represented with a vector |ψ12  =
• We analyze the efficiency and effectiveness of different √1 (|00 + |11). Note that the first two qubits are entangled,
2
assertion schemes. but the two-qubit state is a pure state. However, if we consider
• We introduce the idea of approximate assertion, which the system consisting of the second and third qubits, since this
performs membership checks on a set of states. two-qubit system is entangled with the first qubit, it is in the
The remainder of the paper is organized as follows. Sec- mixed state ρ23 = tr1 (ρ123 ) = 12 (|0 0| + |1 1|) ⊗ |0. In
tion II presents the background of quantum computing and this case, although the second and the third qubits are not
discusses the related work. Section III presents an overview entangled, the state of these two qubits is a mixed state. The
of our proposed precise and approximate assertions and prior dynamic assertion schemes [32] can assert the special
highlights how they overcome the limitations of the prior pure state |ψ12  as it is a Bell pair, but they can’t assert the
work. Sections IV and V detail SWAP-based and NDD-based mixed state ρ23 as well as some other pure states.
assertion circuit designs, respectively. Section VI compares our An n-qubit quantum operation/gate is represented by a 2n ×
n
proposed schemes with existing ones. Section VII presents our 2 unitary matrix U . A gate U operates on a pure state |ψ
experimental methodology. Section VIII discusses the general results in a state |ψ   = U |ψ. A gate U operates on a mixed
applicability of the assertions. Sections IX and X showcase state ρ leads to a state ρ = U ρU † [35].
the use of precise and approximate assertions for debugging, The state vector |ψ representing a pure state is a unit
respectively. Section XI summarizes the paper. vector [35], i.e., ψ|ψ = 1. Two pure states |ψ and |φ are
orthogonal if their inner product is zero [37], i.e., ψ|φ = 0. A
II. BACKGROUND AND R ELATED W ORK set of states |ψi  is orthonormal when each state is represented
A. Quantum Computing by a unit vector and distinct states in the set are orthogo-
In quantum computing, information is encoded in quantum nal [35]. In an n-qubit quantum system, a set of n orthonormal
bits (qubits). A quantum program consists of a sequence of states |ψi  forms an orthonormal basis of the system. Any
quantum gates operating on qubits. During program execution, quantum state |π can be nrepresented as a superposition of
qubits can be in pure or mixed states. Pure quantum states these states, i.e., |π = i=0 αi |ψi . An orthonormal basis
correspond to vectors in a Hilbert space, a complex vector {|ψi } has following properties: (1)ψi |ψj  = 0, for all i = j,
2n −1
space with an inner product, and can be represented with and ψi |ψj  = 1, for all i = j. (2) i=0 |ψi  ψi | = I,
vectors with norm one. For example, a single-qubit pure state where I is the identity matrix. We call the orthonormal basis,
2 2
can be represented as |ψ = a |0+b |1 where |a| +|b| = 1. which only consists of the classical state |0 and |1, the
Due to entanglement, qubits can be in mixed states [11], computational basis. For example, the computational basis
which are a mixture of pure states. A mixed state cannot be of a two qubit system is |00, |01, |10, |11. In the real

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 |φ

Fig. 3: The idea of SWAP based assertion circuits.

|+ |+ |+ |+


Fig. 5: The unitary gate transformation for pure states.
H H
=
|0 H |0 H limitation, we can swap the qubits with the ancilla qubits and
measure the ancilla qubits. In order to reconstruct the qubit
Fig. 4: Circuits for asserting |+ state: our circuit design is on state |ψ0 , we need to prepare the state |ψ0  by applying the
the left side; the one in prior work [36] is on the right side. ⊗n ⊗n
gate U to the |0 state:|ψ0  = U |0 . The general scheme
of the SWAP based assertion circuit is illustrated in Figure 6.
Since we are swapping the qubits with ancilla qubits in |0
Note that the prior work [36] has proved that the state state, the SWAP gates can be optimized with only two CNOT
preparation circuit U has lower cost than the original quantum gates [31]. Depending on where we place the U gate and U −1
program, i.e., V1 ...Vn . Moreover, when used for debugging, gate w.r.t. the SWAP gate, there are four different designs. For
both U and U −1 are synthesized from the assertion, therefore example, the design in Figure 3 and Figure 6 are two different
they are assumed to be bug free. designs with U gate at different places. We prefer to use the
B. Precise Assertion for Pure States design in Figure 6 since the compiler has a better chance to
optimize the gates with the gates before and after the assertion
The key to our precise assertion for pure states is to generate
circuit.
the U or U −1 gate from the pure state to assert for. To assert
for an n-qubit pure state |ψ0 , from Figure 3, we know that
|ψ0  = U |0 or |0 = U −1 |ψ0 . Besides it, we also would like |ψ U −1 U |ψ
to signal an assertion error for other states. In other words, our
goal is that by applying the unitary gate U −1 , we transform ···
⊗n
the “correct” state |ψ0  to the zero state |0 , and other
“incorrect” states to the states in the computational basis, that ⊗n
|0
contain at least one |1. This way, when we measure the qubits
after U −1 , if all the qubits are in the |0 state, the circuit won’t
Fig. 6: General scheme of SWAP based assertion circuit for
raise an assertion error. If any of the qubit is in |1 state, the
pure state assertions.
pure state was not in the |ψ0  state before the transformation,
and the circuit will raise an assertion error. Figure 5 explains C. Precise Assertion for Mixed States
the idea. From the n-qubit pure state |ψ0 , we find an orthonor-
In this section, we generalize the assertion scheme to assert
mal basis {|ψi }i∈[0,2n −1] that includes state |ψ0  using the
for mixed states. The main idea is that for a mixed state ρ0
Gram-Schmidt process [22]. Then, based on the proposition in
to be asserted for, we can generate an unitary gate U −1 to
Appendix B, we generate U −1 to transform the states in this
transform the state ρ0 to the |0 states while keeping the
orthonormal basis to the states in the computational basis and
⊗n ⊗n−1 ⊗n entanglement with other qubits intact. A mixed state is a
U −1 = |ψ0  0| + |ψ1  0| 1| + ... + |ψ2n −1  1| .
1 probability mixture of pure  states, and can be defined with
For example, to assert for the Bell state 2 (|00 + |11),
√ t
a density matrix, i.e., ρ0 = i=0 Pi |ψi  ψi |, where Pi are
we can find the orthonormal basis that includes the state
the probabilities of each pure state |ψi  and these pure states
as:{ √12 (|00 + |11), √12 (|01 + |10), √12 (|00 − |11),
may not be orthogonal to each other.
√1 (− |01 + |10)}. Then we can calculate the two-qubit
2 In our design, the first step is to diagonalize the den-
unitary gate U −1 = √12 (|00 + |11) 00| + √12 (|01 + sity matrix with singular value decomposition (SVD) and
|10) 01|+ √12 (|00−|11) 10|+ √12 (− |01+|10) 11|. With find orthonormal eigenstates. Since the density matrix is a
gate decomposition, we find this gate consists of a CNOT gate square Hermitian matrix, it is always diagonalizable [26],
followed by a Hadamard gate on the control qubit. i.e., ρ0 = DΛD−1 , where D is a unitary matrix, and
On current quantum computers, we can only measure the Λ = diag(λ0 , λ1 , ...λn−1 ) is a diagonal matrix. The columns
qubits once at the end of the program. To overcome this vectors |ψ0  , |ψ1  , ..., |ψn  of D form an orthonormal basis

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

and |ψi  are the corresponding eigenvectors. The rank t of the


density matrix should be less than or equal to the total number Fig. 8: General scheme of SWAP based assertion circuit for
of basis, 2n . The mixed state ρ0 can fall into these t basis mixed state assertion when t = 2m .
states |ψi i∈[0,t−1] , but would never fall into the other 2n − t
states |ψi i∈[t,2n −1] . Therefore, these t states, from |ψ0  to 2) 2m < t < 2m+1 and t < 2n−1 . In this case, we
|ψt−1 , are considered as the “correct” states and the assertion can take advantage of the assertion design for the case when
circuit should not raise an assertion error. However, the other t = 2m by finding two supersets of states. Either superset
2n −t states from |ψt  to |ψ2n −1  are considered as “incorrect” of states contains 2m+1 states and the t correct states are
states and the assertion circuit should raise an assertion error the intersection of these two supersets of states. By asserting
for them. The unitary gate U −1 is generated to transform the for these two supersets of states, when there is no assertion
states in the basis formed by |ψi  to the computational basis. error for both assertions, we can guarantee the state is within
It is worth noting that by checking the “correct” states, we “correct” states. For example, we want to assert for a mixed
can guarantee the state under test is a probability mixture of state ρ = 0.5 |000 000| + 0.25 |001 001| + 0.25 |010 010|.
t “correct” states when there is no assertion error. However, Here, t = 3 and 21 < t < 21+1 , the “correct” states
we can’t guarantee the probability of each “correct” state is are |000, |001, and |010. We can first assert for the
the same as the desired state. The prior work [30] also has superset with 22 states:{|000,|001,|010,|011}. If there is
the same limitation. In section IX-A2, we will show that no assertion error, we assert for another superset with 22
our proposed mixed state assertion is capable of identifying states:{|000,|001,|010,|100}. When both assertion don’t
program bugs. raise an assertion error, the mixed state is a probability mixture
In the second step, we discuss assertion circuit designs for of the “correct” states.
different rank values, t:
1) t = 2m and t ≤ 2n−1 , where m is an integer.
Figure 7 shows the main idea of gate transformation and
Figure 8 shows the assertion circuit. As shown in Figure 7,
we can generate a unitary gate U −1 , which transforms the 2m
“correct” states to the states with the leading n − m qubits
in |0 state. Therefore, when we measure the leading n − m
qubits after unitary gate transformation, if the n − m qubits
are all in |0 state, the circuit won’t raise an assertion error.
If any of the leading n − m qubits is in |1 state, the circuit
will raise an assertion error.
As indicated in Figure 8, although the qubits under test
are entangled with other qubits (therefore they are in a mixed
state), after U −1 , the leading n − m qubits are not entangled
with any other qubits (as they are in |0 state). Therefore, they Fig. 9: The unitary gate transformation for mixed states when
can be measured without affecting the existing entanglement. 2n−1 < t < 2n .
3) 2n−1 < t < 2n . As t increases, the number of “incorrect”
states decreases and the number of qubits that we need
to measure also decreases. When t > 2n−1 , the ratio of
“incorrect” states is less than half and we do not have enough
qubit to measure. Therefore, we need one extra ancilla qubits
to enlarge the number of “incorrect” states. The ancilla qubit
is initialized in |0 state. As shown in Figure 9, we pick some
states as the “virtually correct” states. These “virtually correct”
states won’t raise an assertion error, but we also know they
won’t happen since the ancilla qubit is prepared in |0 state.
Fig. 7: The unitary gate transformation for mixed states when The unitary gate transforms the union of “virtually correct”
t = 2m . and “correct” states to the computational basis states with

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.

V. NDD-BASED A SSERTION C IRCUIT


D. Approximate Assertion
Liu et.al. [32] proposed quantum assertion circuits for
In Section III we discussed the idea of approximate assertion superposition states based on Non Destructive Discrimination
which asserts a set of states. We can see that asserting a (NDD). Here, we extend the idea to assert for pure and mixed
set of states is actually similar to asserting mixed states. A states.
mixed state is a probability mixture of pure states, which is
analogous to a set of pure states. Therefore, we can reuse A. Precise Assertion for Pure States
the same methodology as asserting mixed states. Given a set The general scheme for asserting a pure state is shown in
of quantum states {|ψ , |φ , ...}, since the states may not be Figure 12. The design consists of a controlled-U gate and two
orthogonal, we can form a density matrix from these states Hadamard gates and is a phase-kickback circuit. It has the
with equal probability and use the same process to diagonalize following properties: (i) when the quantum state |ψ is the
it and calculate the orthonormal basis states. After finding the t eigenstate of the unitary matrix U with the eigenvalue of 1,
orthonormal basis states, we can treat this problem as asserting i.e., |ψ = U |ψ, the ancilla qubit’s output state is |0. (ii)
for the mixed state composed of these t basis states. We mark When the quantum state |ψ is the eigenstate of the unitary
these t basis states as “correct” states and follow the assertion matrix U with the eigenvalue of -1, the ancilla qubit’s output
process as described for mixed states. state is |1. Based on these two properties, we can design the
assertion circuit for asserting a pure state |ψ0 .
E. Alternative Design: Logical OR Based Assertion Circuit
Similar to the SWAP based design, the first step of our
The circuit for asserting an n-qubit pure state requires n design is to find an orthonormal basis {|ψi }i∈[0,2n −1] that
SWAP gates and n ancilla qubits. When the number n is includes state |ψ0 . Since we want to distinguish between the
large, the design becomes inefficient. Here, we present another “correct” state |ψ0  and the other “incorrect” states |ψi i=0 , we
design, which only requires one ancilla qubit. After the U −1 can find an unitary matrix U such that the “correct” state |ψ0 
gate, the state of the qubits under test should be transformed is its eigenstate with the eigenvalue being 1 and the “incorrect”
to all |0 state. If any of them is in |1 state, the circuit should states |ψi i=0 are its eigenstates with eigenvalues being -1.
raise an assertion error. Therefore, we can apply logical OR Based on this property, when the qubits under test are in |ψ0 
gate to the qubits and store the result in one ancilla qubit. state, we will get |0 state when measuring the ancilla qubit,
When the ancilla qubit is in |0 state, the circuit won’t raise i.e., the assertion circuit won’t raise an assertion error. When
an assertion error. When the ancilla qubit is in |1 state, the qubits under test are in any other state |ψi i=0 , we will
some of the qubits are in |1 state after U −1 gate and the get |1 when measuring the ancilla qubit, i.e., the assertion
circuit will raise an assertion error. The circuit design is shown circuit will raise an assertion error. Based on the spectral
in Figure 11. An n-qubit logical OR gate can be linearly decomposition [20], the unitary matrix U can be calculated

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

TABLE III: Circuit cost for different assertion circuit designs.

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

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

Based on the circuit equivalence, we can transform our


assertion circuit step by step as shown in Figure 19. In the
last step, since the first CNOT gate is controlling over the |0
state, it can be substituted with a quantum wire, the resulting
circuit is equivalent to the assertion circuit proposed by prior
work [32].

|+ H |+ |+ H |+


=
|0 H |0 H
|+ H |+ |+ H |+
= =
|0 H |0 H

|+ H |+
=
|0 H

Fig. 19: Circuit transformation of the assertion circuit for |+


state

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

Fig. 20: SWAP based circuit for asserting constant set

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.

1 def controlled adder(num ctrl, qr ctrl0, qr ctrl1, width, a, qr):


2 for i in range(width−1, −1, −1):
3 for j in range(i, −1, −1):
4 if (a >> j) & 1: //shift out bits in constant a
5 angle = np.pi/ pow(2, i−j) //rotation angle
6 if num ctrl is 0:
7 circuit.rz(qr[i], angle)
8 elif num ctrl is 1:
9 circuit.crz(qr ctrl0, qr[i], angle)
10 elif num ctrl is 2:
11 circuit.ccrz(qr ctrl0, qr ctrl1, qr[i], angle)
12 return circuit

Fig. 21: The pseudo code for controlled adder using QFT

Since quantum algorithms need varying control and target


qubits, the same subroutine might occur multiple times with
different numbers of controlled qubits. For example, line 7, 9,
and 11 in Figure 21 are the codes for rz gate with different
control qubits. Now, let’s consider a bug happens in line 11,
where the programmer used variable j instead of i. This bug
leads to an incorrect entangled state and it can’t be asserted by
the prior primitives. We can insert precise pure state assertions
after each rz, crz or ccrz gate to capture this bug. Since i and
j are the same for the first rz gate and they start to differ from
the second rz gate, asserting the state after the second rz gate
should be sufficient to capture the bug. The bug also leads to
changes in the mixed states of a subset of qubits, thus it is also
detectable with mixed state assertions. Similarly, the incorrect

205

You might also like