-
April 2022
Multi-state Swap Test Algorithm
Abstract
Estimating the overlap between two states is an important task with several applications in quantum information. However, the typical swap test circuit can only measure a sole pair of quantum states at a time. In this study we designed a recursive quantum circuit to measure overlaps of multiple quantum states concurrently with controlled-swap (CSWAP) gates and ancillary qubits. This circuit enables us to get all pairwise overlaps among input quantum states . Compared with existing schemes for measuring the overlap of multiple quantum states, our scheme provides higher precision and less consumption of ancillary qubits. In addition, we performed simulation experiments on IBM quantum cloud platform to verify the superiority of the scheme.
Keywords:Quantum information, The Swap Test, Multiple Quantum States, Overlap
1 Introduction
The concept of quantum computing was proposed by Richard Feynman in the early 1980s[1] and David Deutsch later gave a quantum algorithm solution to a toy issue[2]. In recent years, Quantum algorithms and their applications in fields like encryption[3],database search[4], quantum simulation[5], optimization problems[6] and linear systems of equations[7] (refer to [8] for more information) have proved spectacular. For certain problems, these quantum algorithms outperform classical algorithms in terms of speed. Typically, quantum algorithms are described using quantum circuits, which are made up of a sequence of quantum gates that can handle quantum bits. When dealing with quantum circuits, it’s important to measure the similarity or overlap between two states and , which can be denoted by [9].
Swap Test is a well-known quantum circuit, which uses a controlled-swap (CSWAP) gate and an ancillary qubit to estimate the inner product of two states [10][11]. It has attracted interest as a fundamental primitive and its efficient implementation on near-term quantum computers is a hot research issue at the moment. In[12], L. Cincio et al. presented a machine learning approach for discovering short-depth algorithm to compute states overlap, whose performance is better than the linear scaling of Swap Test. In [13], M. Fanizza et al. studied the estimation of overlap between two unknown pure quantum states in a finite-dimensional system, given and copies of each type. They demonstrated that allowing for general collective measurements on all copies can yield a more precise estimate. In [14], U. Chabaud et al. proposed a strategy for a programmable projective measurement device, which may be interpreted as an optimal Swap Test when only one copy of one state and of the other is available. There are many applications that require state overlap computation thus making Swap Test appears as a subroutine in these applications. For instance, it is used to measure entanglement and indistinguishabiliy for quantum states[11][15], and also common in quantum supervised learning to measure the distances of quantum states[16][17][18]. What’s more, proofs-of-concept in implementations of quantum computers[19][20] call for this Swap Test as well.
Consider the case when there are quantum states and the Swap Test can only assess the overlap of two at a time. In order to obtain the overlap between arbitrary two states , there is a trivial solution which requires Swap Tests and ancillary qubits. In [21], X. Gitiaux et al. extended two-state Swap Test to quantum states . They built a unitary circuit with three ancillaries, three controlled-swap (CSWAP) gates, and one Swap Test to compute overlap for four states. Based on the circuit, they developed a recursive algorithm to generalize a circuit which can estimate overlaps between quantum states at a time. The circuit requires CSWAP gates and ancillary qubits.
In this paper, following the idea of [21], we design a new circuit with two ancillaries, two controlled-swap(CSWAP) gates , and two simple Swap Test. We also present two rules to swap four groups of quantum states. Based on the swap rules and our , a circuit to calculate the overlap between different pairs of arbitrary input states are presented. Our circuit requires CSWAP gates and ancillary qubits.
The paper is organized as follows: some correlative preliminaries are introduced in Section 2; a multi-state Swap Test algorithm is proposed in Section 3; In Section 4, the simulation of 8-state Swap Test algorithm on IBM Quantum Experience’s platform is described. And the correctness and performance of the algorithm are analyzed in section 5. A brief discussion and a summary are given in Section 6.
2 Preliminaries
2.1 Swap Test and Its Quantum Circuit
The Swap Test is used to estimate the overlap between two unknown quantum states, and it has a variety of applications. Given two unknown quantum states and , the overlap between them is defined as , i.e. the inner product between their state vectors. The Swap Test quantum circuit for estimating the overlap between and is shown in Fig.(1):

where the first qubit is an auxiliary qubit used for measurement and the other two qubits are used for input. According to the quantum circuit, the output denoted by can be calculated as:
(1) |
According to Equ.(1), the probability of measuring is .
Obviously, the overlap is related to the probability of measuring . So we can derive:
(2) |
In [15], Juan Carlos Garcia-Escartin et al. show that the Hong-Ou-Mandel effect from quantum optics is equivalent to the Swap Test. Then, they give a destructive Swap Test module that can provide better performance than the traditional Swap Test. The basic implementation of their Swap Test costs three quantum registers and a CSWAP gate to estimate the overlap between two unknown quantum states. The CSWAP circuit is inspired by classical XOR swapping, and its quantum implementation is shown in Fig.(2).

X gate can get with the help of Z gate. So we have the equivalent quantum circuit of Swap Test in Fig.(3).

Obviously, the ancillary qubit is not affected by the tested qubits after CCZ gate. We can just get rid of the last H and CNOT gates and perform measurements directly after the CCZ gate with no effect on the ancillary qubit and the result of the Swap Test. Moreover, with the help of a trick called ’phase kickback’, we can rewrite the circuit as in Fig.(4).

The output of ancillary qubit can be predicted according to the output of tested qubits in the classical method, which is sometimes called the principle of deferred measurement. Clearly, we can just ignore the ancillary qubit and perform a Swap Test with the circuit in Fig.(5), which is the improved Swap Test.

2.2 Gitiaux’s Swap Test for Multiple Quantum States
In [19], Xavier Gitiaux et al. gave a new quantum algorithm that is used to estimate the overlap between multiple quantum states based on the standard two-state Swap Test and CSWAP gate.
First, they define a unitary for a 4-state circuit as a building block to develop a recursive algorithm to produce an -state circuit.

As shown in Fig.(6), there are a total of four input states which can form 6 pairs of combinations. It requires at least 6 orthogonal states to label them. Therefore, the minimum number of ancillary qubits is three, and the minimum number of CSWAP gates is also three, each acting as control once.
Terms contained in the superposition state are listed in Table 1, where denote the basis states of the three ancillary qubits from bottom to top in the ancillary section in Fig.(6). The four state registers are labeled by . The input state is , and the order of the output states depends on the measured values of the auxiliary qubits. As shown in the table 1, the output state will be if the ancillary qubits are .The essential of this algorithm is that each possible overlap between two states can be generated by only measuring the first two qubits, which can be verified by Table.(1).

And the for 8 inputs can be constructed from which is shown in Fig.(7). The circuit is built into two sections of three ancillaries and three groups of three CSWAP gates, which amounts to six ancillaries and nine CSWAP gates. It is obvious that the quantum circuit is divided into two groups of four registers: and respectively. In the first step, the corresponding registers in the two sets are controlled by a set of three ancillaries in parallel which brings all six pairs in to and all six pairs in to respectively. Then another set of three ancillaries is introduced to control the pairing among which makes the pairing between all 8 registers possible.
Next we consider the case . As shown in Fig.(8), we build the by repeating the same thing we did in . Denoting the number of ancillaries in as and the number of CSWAP gate as , We have and where .

To clarify, the procedure is to use within different groups first, and then on the resultant registers of each group. acts each time to allow the pairing of two parts of the registers(these parts could be the simplest group or a group already combined by in the previous step.)
3 New Multi-state Swap Test algorithm
Suppose that there are quantum states . An algorithm to construct one circuit to take all input states at once and get the overlap of arbitrary two input states in is presented.
Two swap rules are designed as follows: quantum states can be divided into four groups . is to exchange the states of 2nd and 3rd group in turn and get ; is to exchange the states of 2nd and 4th group in turn and get . The multi-state Swap Test algorithm based on the two swap rules is as follows:
(1) Constructing a circuit to estimate the overlap of any two of the 4 states quantum states . In , there are two ancillary qubits and four input qubits . The initial states are and . are divided into four groups . There are also two CSWAP gates, where is the control qubit to swap two groups according to . Two simple Swap Test for two quantum states are places at the end of to measure the overlap for and . The is shown in Fig.(9).

The swap results between and are shown in Table.(2).
According to the Table.(2), if , the overlap of and can be obtained through two simple Swap Test; if , the overlap of and can be obtained through two simple Swap Test; if , the overlap of and can be obtained through two simple Swap Test.
(2) Constructing a circuit to estimate the overlap of any two quantum states in . In , there are four ancillary qubits and eight input qubits . The initial states are and . are divided into four groups . There are also eight CSWAP gates. is a control qubit on two CSWAP gates to swap according to . And can be seen as inputs of and can also be seen as inputs of . Four simple Swap Test for two quantum states are places at the end of to measure the overlap for and . The is shown in Fig.(10).

The swap results between and are shown in Table.(3).
(3) Constructing a circuit to estimate any two quantum states’ overlap for states . If , are added into states. In , there are ancillary qubits and input states . The initial states are and . are divided into four groups There are also CSWAP gates in . is a control qubit on CSWAP gates to swap the states in according to . Then the inputs of can be used to construct a circuit . After recursively constructing round by round, can be used as inputs of . simple Swap Test for two quantum states are places at the end of to measure the overlap for , ,…,.
4 Simulation of multi-state Swap Test algorithm
In this section, 8 random states are chosen as an example of a multi-state Swap Test algorithm and then the correctness of the algorithm on this example is verified by experiments on IBM quantum cloud platform.
For arbitrary two states , the exact overlap is denoted by , which can be calculated by the definition of overlap ; And the estimated overlap is denoted by , which can be obtained by executing simulated experiments.
The simulated quantum circuit on IBM quantum cloud platform for 8-state Swap Test algorithm is given in Fig.(11), where are the auxiliary control qubits that corresponds to , are the initial states that corresponds to and are the result qubits of simple two-state Swap Test. The initial states are . Pair of normalized random numbers are generated by Python random package and the initial states in the experiment are ,,,,,,,. And the measurement results of this implementation through IBM Quantum Experience (QISKIT) are given in Fig.(12),with 8192 runs. Meanwhile, the measurement times of is shown in Appendix A.


The estimated overlap of can be obtained according to the relation of and in Table.2 and Appendix A. For example, and , the measurement result of is related to estimate values of . After counting times of and the times of , . The exact overlap of arbitrary two states and the estimated overlap of arbitrary two states is shown in Table.(4).
0.3774 | 0.3959 | 0.9817 | 0.9880 | 0.7751 | 0.7590 | 0.9688 | 0.9610 |
0.8868 | 0.8949 | 0.0215 | 0.0009 | 0.8497 | 0.8658 | 0.5118 | 0.5697 |
0.8374 | 0.8476 | 0.5533 | 0.5393 | 0.7123 | 0.6913 | 0.7581 | 0.7714 |
0.7607 | 0.7562 | 0.8768 | 0.8774 | 0.9982 | 0.9982 | 0.9574 | 0.9611 |
0.0779 | 0.1093 | 0.9325 | 0.9516 | 0.9028 | 0.9104 | 0.9773 | 0.9739 |
0.3582 | 0.4323 | 0.9908 | 1.0 | 0.9727 | 0.9727 | 0.1017 | 0.0260 |
0.9519 | 0.9602 | 0.2218 | 0.1972 | 0.9970 | 0.9960 | 0.2691 | 0.2927 |
The scatter diagram is shown in Fig.(13), where the estimate overlap value is X-axis and the exact overlap value is Y-axis. It can be seen from Fig.(13) that most of the points fall near the diagonals of the picture, i.e. the line , indicating that the estimate overlap results are close to the exact overlap results.

9 data sets of the initial states are generated by Python random package to be simulated in this experiment and their initial values are shown in Appendix B. And the scatter diagrams of estimated overlaps and exact overlaps of these 9 sets are shown in Fig.(14), where the estimated overlaps is X-axis and the exact overlaps is Y-axis. It can be seen from Fig.6 that most of the points fall near the diagonals of the picture, i.e., the line , indicating that the estimated overlaps are close to the exact overlaps.









5 Analysis
In this section, we analyze the correctness and performance of our multi-state Swap Test algorithm in theory.
5.1 Correctness analysis
There is a transformation , which can take arbitrary unknown states as input states and as ancillaries. In , each pair can be labeled by ancillaries by Lemma 4.1.
Lemma 4.1 Let , there exists a unitary that maps
(3) |
where are arbitrary two input quantum states, and they are in adjacent quantum registers, i.e. in the quantum registers . are garbage states which is irrelevant to our target states, i.e. . represents the state in the first quantum registers. That means we can get all pairs by measuring the specific quantum registers in the quantum circuit.
Constructive proof of Lemma 4.1 According to scientific induction, we assume that Lemma 4.1 is correct at first. Let the quantum registers divided into 4 groups ,, ,. Furthermore, we get 4 quantum states ,, ,. Applying to and respectively, we can get all pairs of input states , i.e. , and the same is true for the other . If we apply a unitary on before the , we will get all pairs of input states . For example, we want to get a pair . will be stored in if the ancillary qubits of is , and the pair might be measured after .
Similarly, the correctness of can be provide by using and , and so on. Finally, the correctness of is shown in Table.1. So Lemma 4.1 is proved. For the case that cannot be written in the form of , we can add copy of any input state as the rest input.
So far, we have obtained all pairs of input quantum states, and can get each pair exactly according to the measurement results of auxiliary qubits. Therefore, we only need to apply the Swap Test in all specific registers to get the overlap between two input quantum states. As shown in Figure(16), the measurement results of auxiliaries determine the quantum state in each quantum register uniquely. Because it has been proved that all pairs of input quantum states have the opportunity to be measured in specific registers, all overlap estimates can be obtained after enough measurements.
5.2 Performance analysis
In this section, we compare our scheme and the scheme Swap Test for an arbitrary number of quantum states given by Gitiaux(referred to as SAN scheme later in this article).
5.2.1 Precision
The precision of estimation is determined by the occurrences of each overlap with the total number of runs given, thus we can use this value ,denoted by m to represent the precision. For the SAN scheme, only one measurement can be obtained each time the quantum circuit is run. Therefore, in the case of quantum state inputs, if the count of quantum circuit is run for times, each overlap is estimated to occur times in average, i.e., . For our scheme, one execution contains Swap Test operations. So in the same case, we have the precision ,which means higher precision.

Fig.(15) shows the ratio of the average precision of our scheme with respect to SAN. Taking the number of the SAN scheme as the standard, it can be seen that with the increase of the circuits depth, the ratio between the new scheme and SAN scheme increases gradually, indicating that our precision advantage becomes gradually more pronounced as the circuit depth increases.The stepwise growth is due to the fact that we must apply such that . It should be noted that if the probability of occurrence of each quantum state is taken into account, the estimation of different overlaps will have different precision.This is because the number of recurrences of different pairs varies according to the table, which means that some pairs may occur more frequently. However, this only depends on the artificial choices we make when coding, and the total effect does not have a large impact as the number increases.
5.2.2 Space complexity
The space complexity of consists of the count of CSWAP gates and the number of ancillaries, where and . First of all, we notice that the count of CSWAP gates follows the recursive relation:
(4) |
where the first term comes from two and the second term comes from a . The general term formula can be obtained by simplifying the sequence relation. If considering the CSWAP gates come from the following Swap Test operation, we have. Similarly, for SAN scheme, we can get the general term formula for the number of CSWAP gates , which is shown in the right panel of Fig.(16). Furthermore, if we concentrate on the count of CSWAP gates, the complexity of in our scheme is and in SAN scheme is . Obviously, our scheme uses more CSWAP gates and has higher complexity than the SAN scheme.
As for the number of ancillaries, we have the relation:
(5) |
which means each time the number of input quantum states is doubled, 2 additional auxiliaries are required. It can be rewritten as , and the general term formula for SAN scheme the number of ancillaries is . Obviously, our scheme uses fewer ancillaries than the SAN scheme, and both complexities are . The relationship between the number of auxiliaries and the number of input quantum states (assuming that all input quantum states are single quantum states) is shown in Figure(18). As shown in the left panel. the red line and the green line represent our scheme and SAN scheme respectively. It can be seen that the number of auxiliaries required by our new scheme is less than that of the SAN scheme.

If we consider the additional auxiliaries brought by the subsequent Swap Test of the two Swap Test schemes, we can reduce it by using the Swap Test circuit optimization scheme mentioned in section 2 to reduce the auxiliaries through classical calculation. At this time, the total number of qubits required by our scheme will be less than that required by the SAN scheme, which means less resource consumption.
To sum up, our scheme provides a better estimation under the same count of circuit runs. At the same time, the number of auxiliaries required in our scheme is less than that of the SAN scheme. Although the complexity of the count of CSWAP gates in the SAN scheme is which is less than that in our scheme , When the circuits run, they are processed in parallel and do not affect the total time, so we trade a certain space cost for the desired time cost.
6 Summary
In this paper, we propose a new scheme for estimating overlap between all pair of input quantum states. Given arbitrary number of input quantum states . the scheme provide a way to calculate the overlap for all pair of input quantum states where .
Our scheme is based on the scheme given by [19]. The basic principle is we concentrate a unitary according to input quantum states , which is used to obtain a specific superposition state, so that we can get all pairs of input quantum states at a specific position . In order to decode the measurement results, we use auxiliary qubits, their measurement results reveal what the quantum state in the register is. Finally, the results are obtained by performing Swap Test on these positions to estimate the value of overlap between each pair. A total of CSWAP gates are used in the quantum circuit. We carry out experiments on IBM cloud platform, and give a exact analysis to verify the correctness of our scheme. At the same time, we analyze the complexity and accuracy of our scheme. Compared with the original scheme [19], our scheme uses fewer auxiliary qubits and has higher accuracy, but the line depth (the number of CSWAP gates) will be higher than the original scheme. Therefore, if it is possible to find a way to reduce the use of cswap gates, the performance of the scheme will be further improved.
References
References
- [1] Shor P W 1982 International Journal of exact Physics 21 467-488
- [2] Deutsch D 1985 Proceedings of the Royal Society A Mathematical 400 97-117
- [3] Shor, Peter W 1999 SIAM Review 26 1484-1509
- [4] Grover L K 1996 Proceedings of the twenty-eighth annual ACM symposium on Theory of computing (STOC96) 212-219
- [5] Lloyd S 1996 Science 273 1073-1078
- [6] Farhi E , Goldstone J , Gutmann S , et al, 2000 Physics
- [7] Harrow A W , Hassidim A , Lloyd S 2009 Physical Review Letters 103 150502
- [8] Jordan S (https://quantumalgorithmzoo.org/ (Accessed: 2020-11-07))
- [9] Aimeur E , Zekrifa D M S , Gambs S 2006 Proceedings of the 19th international conference on Advances in Artificial Intelligence: Canadian Society for Computational Studies of Intelligence(Springer-Verlag) 431-42
- [10] Chuang I , Gottesman D 2001 SIAM Review (arXiv:quant-ph/0105032)
- [11] Garcia-Escartin J C , Chamorro-Posada P 2013 Physical Review A 87 239-257
- [12] Cincio L , Y Subaşı, Sornborger A T , et al, 2018 New Journal of Physics 20 113022
- [13] Fanizza M , Rosati M , Skotiniotis M , et al, 2020 Physical review letters 124 060503
- [14] Chabaud U , Diamanti E , Markham D , et al, 2018 Physical Review A 98 062318
- [15] Linke N M , Johri S , Figgatt C , et al, 2017 APS March Meeting(American Physical Society)
- [16] Lloyd S , Mohseni M , Rebentrost P 2013 (arXiv:1307.0411)
- [17] Wiebe N , Kapoor A , Svore K 2014 Quantum Information Computation 15 318-358
- [18] V Havlíček, AD Córcoles, Temme K , et al, 2019 Nature 567 209-212
- [19] Schuld Maria,Sinayskiy Ilya ,et al, 2015 (arXiv:1409.3097)
- [20] Kang Min-Sung, Heo Jino,et al 2019 Scientific Reports9 (1) 6167
- [21] Gitiaux X, Morris I , Emelianenko M , et al, 2021 (arXiv:2110.13261)
Appendix
A
counts | counts | counts | |||
---|---|---|---|---|---|
398 | 38 | 38 | |||
235 | 207 | 7 | |||
245 | 30 | 1 | |||
60 | 3 | 2 | |||
83 | 55 | 4 | |||
221 | 31 | 22 | |||
36 | 148 | 9 | |||
10 | 34 | 4 | |||
389 | 9 | 29 | |||
256 | 32 | 6 | |||
388 | 4 | 5 | |||
116 | 19 | 1 | |||
45 | 3 | 19 | |||
114 | 4 | 18 | |||
238 | 18 | 6 | |||
29 | 62 | 2 | |||
220 | 35 | 4 | |||
73 | 16 | 3 | |||
43 | 20 | 10 | |||
26 | 57 | 1 | |||
44 | 49 | 9 | |||
189 | 4 | 4 | |||
4 | 88 | 8 | |||
32 | 19 | 2 | |||
200 | 33 | 1 | |||
1 | 4 | 6 | |||
238 | 18 | 6 | |||
131 | 30 | 4 | |||
218 | 5 | 3 | |||
93 | 18 | 1 | |||
189 | 2 | 1 | |||
54 | 69 | 1 | |||
126 | 2 | 3 | |||
121 | 2 | 6 | |||
195 | 12 | 1 | |||
99 | 2 | 2 | |||
413 | 18 | 4 | |||
104 | 26 | 3 | |||
109 | 6 | 1 | |||
49 | 7 | 2 | |||
231 | 7 | 1 | |||
234 | 5 | 1 | |||
252 | 8 | 1 | |||
56 | 3 | 4 | |||
13 | 2 | 1 | |||
3 | 2 | 1 | |||
1 | 4 | 1 | |||
1 | 1 | 2 | |||
2 | 1 | 2 | |||
2 | 1 | 1 | |||
1 | 3 | 1 | |||
1 | 1 | 2 | |||
1 | 2 | 1 | |||
1 | 1 | 1 |