Compare the Similarity between Two Quantum Images

Comparing the similarity between digital images is an important subroutine in various image processing algorithms. In this study, we present three quantum algorithms for comparing the similarity between two quantum images. These algorithms apply to binary, grey and color images respectively. Without considering the image preparation, the proposed algorithms achieve exponential acceleration than the existing quantum and classical methods in complexity. At the end of this paper, an experiment based on the real quantum computer of IBMQ and simulations verify the effectiveness of the algorithms.


Introduction
After Google realizing "quantum supremacy" in 2019 [1], quantum computing has aroused the great interest of the public. Exploiting quantum mechanics such as superposition and entanglement, quantum computing shows an overwhelming computing power advantage in some certain problems. Nonetheless, on account of the few quantum algorithms, these problems were extremely limited for more than a decade after the concept of the quantum computer was rst proposed by Richard Feynman in 1982 [2]. Nowadays, after decades of development of quantum computer hardware, quantum computing has entered a new era called "Noisy Intermediate-Scale Quantum (NISQ)" [3]. Researches on quantum algorithms also obtained some remarkable results [4][5][6][7]. Thanks to increasing demand for e cient image processing algorithms, quantum image processing (QImP) is one of the hot topics.
For QImP, the rst issue to be addressed is how to represent an image in a quantum computer. In recent years, a variety of quantum image representation (QImR) methods have been developed, such as quantum lattice [8], entangled image [9], exible representation of quantum images (FRQI) [10], a novel enhanced quantum representation of digital images (NEQR) [11] and their variants [12,13]. The former two QImR methods were proposed in an early stage and did not bene t from quantum speed-up [14]. Compared with FRQI, NEQR and its variants have advantages in both image preparation and color processing [11,15], which is vital in today's computer vision technology.
Based on different QImR methods, various QImP algorithms were proposed to solve problems in different elds of image processing, e.g. edge detection [14,16], image watermarking [17][18][19][20], image scaling [21,22], and etc. As for assessing the similarity between two quantum images, most researches were based on FRQI and its variants [23][24][25][26]. There was a research comparing images based on NEQR and its variant, a novel quantum representation of color digital images (NCQI), by using quantum amplitude ampli er and estimation in 2019 [15]. Nevertheless, the main component of quantum amplitude ampli er, an oracle which only ips the amplitude sign of the searched input, still needs to be given as a black box to the algorithm [27,28]. In addition, the binarization of grey and color images in [15] is slightly de cient due to the lack of available quantum circuits. In this paper, we present three algorithms on comparing the similarity between two quantum images based on NEQR and its variants. Our algorithms consist entirely of general quantum gate set, bene t from additional quantum accelerations, and are readily applicable on binary, grey, and color quantum images.
In the rest of this paper, we brie y described the representation methods based on NEQR and NCQI in Sect. 2.
The proposed algorithms were presented in Sect. 3. In Sect. 4, we analyzed the time complexity of our algorithms and gave a comparison with classical image processing and existing quantum methods. Finally, several experiment results were given in Sect. 5 to illustrate the validity of the algorithms.

Neqr And Ncqi
In this research, the representation of binary and grey images is based on NEQR, and the representation of color images is based on NCQI.

NEQR
Supposing an image is composed of 2 n × 2 n pixels and has a grey range of 2 q , it can be represented as [11]: where | C YX > encodes the grey-scale value and | YX > encodes the pixel positions. The length of | C YX > is q and binary images can be regarded as a special case taking q=1. After converting to binary format, the length of | YX > is 2n.

NCQI
Supposing a RGB color image is composed of 2 n × 2 n pixels and every RGB component has a range of 2 q , then it can be represented as [12]: where | YX > encodes the pixel positions and | R YX > , | G YX > , | B YX > encodes the red-scale, greenscale, and blue-scale value respectively. The length of | R YX G YX B YX > is 3q and after converting to binary format, the length of | YX > is 2n.

Compare The Similarity Between Two Quantum Images
After preparing two quantum images into qubits 1 (img-qubits1) and qubits 2 (img-qubits2) based on NEQR or NCQI, similarity assessment between these two images is summarized into three steps, as shown in Fig. 1.
In the following, we present the detailed information for the applications on binary, grey, and color images.

Binary images
Algorithm 1 Compare the similarity between two binary images.
Step 1: Compare all qubits in img-qubits1 and img-qubits2 one by one and generate auxiliary qubits 1 (AuxBit1); Step 2: Compress all-1 qubit strings | 11…11 > of AuxBit1 into | 1 > of a single qubit (AuxBit3); Step 3: Measure AuxBit3 and calculate the similarity s with s = p × 2 2n , where p is the probability of getting | 1 > and 2 2n is the size of images.
Binary images have only two pixel values, namely 0 and 1, which could be regarded as a special case of NEQR. Thus, when comparing two pixels, there are only two results, totally the same and completely different.
In this subsection, all discussions are based on this special case of taking q=1.
After preparing two binary images in a quantum computer, the quantum state for representing these two images could be written as: > has only four possible states, | 00 > , | 01 > , | 10 > , or | 11 > .
As pixel values are represented using binary qubits in NEQR, it's intuitive to think of comparing the pixel value qubits and the position qubits simultaneously. Only candidates having totally identical qubit strings have a contribution to the nal result. Therefore, the rst step is to compare all qubits in img-qubits1 and img-qubits2 one by one. Focusing on comparison between two qubits (one from imag-qubits1, and the other from the same site of imag-qubits2), an auxiliary qubit was used to store the result. If qubits for comparing are equal, the auxiliary qubit is set to 1; if different, the auxiliary qubit is set to 0, as shown in the truth table (Table 1). This operation is realized with two Control-Not (C-Not) gates and one NOT (X) gate, as shown in Fig. 2(a). > ) is generated based on the quantum state in (3) and Table 1: where the length of | A C > is 1 and the length of > has a state of | 00 > or | 11 > , which indicates the identical pixel values. | A Y 1 X 1 Y 2 X 2 > has a state of all-1 qubit string | 1…11 > only if Y 1 = Y 2 and X 1 = X 2 , which indicates the identical pixel positions. Therefore, all-1 qubit strings | 11…11 > (the following is written as | 11…11 > for simpli cation) of | A C A Y 1 X 1 Y 2 X 2 > indicate the identical pixel positions and values.
Supposing two quantum images composed of 2 n × 2 n pixels have j (j is an integer less than 2 2n ) identical (4), and the similarity equals j 2 2n , where 2 2n is the number of the total pixels.
Focusing on the subspace of AuxBit1, in measuring quantum state in (4), the probability of getting each basis > , the probability of getting | 11…11 > equals j 2 4n . That is to say, after measuring AuxBit1, we can calculate the similarity between two quantum images by multiplying the probability of getting | 11…11 > with 2 2n . However, because of the ineluctable errors included in a real quantum computer, there may exist 2 1 + 2n possible results maximally in measuring AuxBit1, and | 11…11 > is only one of them. Precise measurement of so numerous qubits is technically di cult and may introduce non-ignorable readout error [29].
The step 2 is to propagate the probability of getting | 11…11 > in measuring AuxBit1 to the probability of getting | 1 > in another single qubit measurement. As the only state we focus on is | 11…11 > in measuring AuxBit1, the other results just offer some redundant information. Thus, we could design a quantum circuit which compresses | 11…11 > to | 1 > and the other states to | 0 > . This operation can be achieved by using the Control-Not gate that takes multi qubits as its control condition, which reverses the target qubit only if all control qubits are in | 1 > . Construction of such multi-qubits controlled operation using general quantum gate set is introduced in [30]. Generally, de ning a controlled operation C a (U) acting on a+ 1 qubits, it can be written as: where U is a single qubit unitary operation. Taking that U is a Not gate as an example, construction of such controlled operation is shown in Fig. 2 (b), where auxiliary qubits 2 (AuxBits2) have a length of 2n and auxiliary qubit 3 (AuxBit3) is the target single qubit we nally wanted.
After step 2, AuxBit3 was set to | 1 > for all | 11…11 > strings of | A C A Y 1 X 1 Y 2 X 2 > . Therefore, in measuring AuxBit3, the probability of getting | 1 > is equal to the probability of getting | 11…11 > in measuring AuxBit1, which is j 2 4n . By multiplying this probability with 2 2n , we can get the similarity between two quantum images.
The factor of 2 2n comes from operations on quantum superposition states. Comparison between img-qubits1 and img-qubits2 will work on all bases in equation (3) simultaneously. Expanding the comparison to the whole Hilbert space means we compare each of the bases in img-qubits1 with all the bases in img-qubits2, which results in the numerator being the number of identical pixels, but the denominator being square of the number of total pixels.

Grey images
Algorithm 2 Compare the similarity between two grey quantum images with a grey range of .
Step 1: Compare the former b pixel value qubits and all pixel position qubits of img-qubits1 and img-qubits2 one by one, where b is an integer less than q, and generate auxiliary qubits 4 (AuxBit4); Step 2: Compress all-1 qubit strings | 11…11 > of AuxBit4 into | 1 > of a single qubit (AuxBit5); Step 3: Measure AuxBit5 and calculate the similarity s with s = p × 2 2n , where p is the probability of getting | 1 > and 2 2n is the size of images.
Grey images have a much wider application than binary images in classical image processing. In this part, an algorithm for comparing the similarity between two grey quantum images is given. The algorithm is similar to the processing of binary images, except that pixel value qubits compared in step 1 need to be selected.
For illustration, we will take q = 4 at rst, and then move on to a more general case. After preparing two grey images in a quantum computer, the quantum state for representing these two images could be written as: > and | C Y 2 X 2 > has 16 possible states respectively for q = 4, as shown in Fig. 3 A qubits selection strategy is presented to avoid these drawbacks. The strategy is to select the former b qubits of | C Y 1 X 1 > and | C Y 2 X 2 > for comparison. As shown in Fig. 3  , where j is the number of pixels considered identical in two grey quantum images. After step 2, we propagate this intermediate result to the probability of getting | 1 > in measuring AuxBit5, and the similarity can be calculated by multiplying this probability with 2 2n .

Color images
Algorithm 3 Compare the similarity between two color quantum images with the RGB component range of .
Step 1: Compare the former b R , b G , b B (b R , b G , b B are integers less than q) RGB component qubits and all pixel position qubits of img-qubits1 and img-qubits2 one by one, and generate auxiliary qubits 6 (AuxBit6); Step 2: Compress all-1 qubit strings | 11…11 > of AuxBit6 into | 1 > of a single qubit (AuxBit7); Step 3: Measure AuxBit7 and calculate the similarity s with s = p × 2 2n , where p is the probability of getting | 1 > and 2 2n is the size of images.
Color images could convey more information and are much more common in our daily life than binary and grey images. In this subsection, an algorithm for comparing the similarity between two color images is given. The algorithm is similar to Algorithm 2, except that instead of selecting the former b pixel value qubits of After step 1, the AuxBit6 was generated based on the quantum state in (7). AuxBit6 has an all-1 qubit string only if the pixel positions are the same and the RGB component values are in the same group respectively.
The probability of getting all-1 qubit string in measuring AuxBit6 is equal to \frac{j}{{2}^{4n}}, where j is the number of pixels considered identical in two color quantum images. After step 2, we propagate this intermediate result to the probability of getting |1> in measuring AuxBit7, and the similarity can be calculated by multiplying this probability with {2}^{2n}.

Algorithm Complexity
The following discussion of complexity is based on two {2}^{n}\times {2}^{n} images. Intuitively, the classical way to calculate the number of identical pixels is to compare every pixel at the same position. As the comparison must be taken one pixel by one pixel, it would take no more than \text{O}\left({2}^{2n}\right) complexity to complete the calculation for two classical images.
The complexity of a quantum algorithm is usually de ned as the number of universal gates used to accomplish the function [15,31]. As shown in Fig. 2(a), comparing two single qubits in step 1 costs two CNOT gates and one X gate. Since the comparison is acted on 2+4\text{n} qubits for Algorithm 1, the number of basic gates costed in step 1 is no more than 3\times \frac{(2+4\text{n})}{2}=3+6\text{n}. As shown in Fig. 2(b), step 2 needs 2\text{n} Toffoli gates and one CNOT gate. Since Toffoli gate can be constructed with six CNOT gates and ten single gates [30], the number of basic gates costed in step 2 is no more than 1+32\text{n}. Thus, we need \left(3+6\text{n}\right)+\left(1+32\text{n}\right)=4+38\text{n} gates maximally to complete Algorithm 1. Taking into account the complexity of two binary images preparation [15], the complexity of comparing the similarity between two binary quantum images is \text{O}\left({2n2}^{2n}\right)+\text{O}\left(38\text{n}\right).
The time complexity of previous quantum algorithms that compare the similarity between two quantum images based on NEQR or NCQI is given in Table 2. In order to re ect the advantage of the proposed algorithms, the data in Table 2 don't contain the complexity of image preparation as this process is constant in almost all QImP algorithms. The complexity of SAB_PV, SAG_SAB_PV, SAC_SAB_PV depends on the expectation of the precision of results and the data in Table 2 are from an instance given in [15]. In addition, because b, {b}_{R}, {b}_{G}, and {b}_{B} are integers less than q, the complexity of Algorithms 2 and 3 in Table 2 re ects the maximum complexity taking {b,b}_{R},{b}_{G},{b}_{B}=q. Even in this worst case (all pixel value qubits are compared), an exponential acceleration is still achieved than the previous algorithms.

Calculation Results
In the following, some calculation results of binary, grey, and color images are given based on either real quantum computer or simulations.

Experiment result of binary images
The best method for validating a quantum algorithm is to run it on a real quantum computer. IBMQ Experience offers several real quantum computers which can be accessed by public via cloud, and there have been demonstrations of various quantum algorithms based on these quantum computers [33][34][35]. The similarity between three binary images is calculated using IBM_Manila. IBM_Manila is a real quantum computer with ve qubits and 32 quantum volume [36]. The average CNOT gate error of IBM_Manila is 0.8106% and the average single qubit readout error of IBM_Manila is 2.102% (updated aperiodically) [37].
Due to the limited qubits of IBM_Manila, we have to make some simpli cations in order to apply Algorithm 1.
The simpli cations include: the binary images for comparison have only 2 pixels, a pixel value qubit is reused as part of Auxbit1, and the Auxbit1 is measured directly for calculating the similarity. The binary images used for experiments are shown in Fig. 4.
A quantum circuit example is given in Fig. 5. Q0 and q1 are used for representing image a. Q2 and q3 are used for representing image b. After images were prepared, q4 is used for comparing pixel value qubits of image a and b. Due to the lack of more qubits, q0 is reused for comparing pixel position qubits of image a and b. For the same reason, q0 and q4 are measured directly for calculating the similarity between image a and b.
For IBM_Manila, the number of runs of circuits can be selected from 1 to 8192. In more runs, we can get results closer to expectations. The circuit in Fig. 5 was run 8192 times on IBM_Manila, and the numbers of getting |11> in measuring q0 and q4 were counted. In an instance, the numbers of getting |11> were 1810.
Thus, the similarity between image a and b was calculated as \frac{1810}{8192}\times 2=44.19\%. The similarity between image a and c, image b and c was calculated similarly, and the results were 8.40% and 46.44%. There is a notable margin between the calculation results and expectations. This is mainly because of the readout error and imperfect quantum gates. In addition, the limitation of physical links between qubits on a real quantum chip also introduces additional SWAP gates when compiling quantum circuits [38].

Simulation result of grey images
On account of the limited qubits and delities, it is unlikely to run a complex quantum circuit on the real quantum computer and obtain a satis ed result presently. IBMQ Experience also offers several simulators that have been frequently used for demonstration of quantum algorithms [39][40][41]. IBMQ_qasm_simulator is a general-purpose simulator for simulating quantum circuits both ideally and subject to noise modeling [37].
Similarities between three grey quantum images (Fig. 6) are calculated using IBMQ_qasm_simulator. The example of preparing image d and e is given in Fig. 7. The other part of the algorithm can be generated with reference to Fig. 2 and is omitted in Fig. 7 for clarity. For IBMQ_qasm_simulator, the number of runs of circuits can be selected from 1 to 8192. In order to get results closer to expectations, the quantum circuits were run 8192 times. The similarity results are shown in Table 3. If all pixel value qubits are compared, there is not so signi cant difference between the three similarity results. By selecting the rst pixel value qubit for comparison, we can get an obvious maximum similarity when comparing image d and e, which is consistent with expectation as they both look like number "0" but image f looks like number "2". Other similarity results also have an increment by transforming the comparison on all pixel value qubits to on the rst pixel value qubit. In order to simulate the evolution of quantum states in the algorithm, some simpli cations have to be made from Algorithm 3. Firstly, in step1, instead of multiplying the bases with unitary matrices which have the enormous dimensions, the Auxbit6 was generated directly according to its truth table (Table 1). Secondly, the number of Auxbit6 being |11\dots 11> was counted for statistical analysis of the expected probability of getting |11\dots 11> in measuring Auxbit6. Finally, instead of step3, this probability was used for calculating the similarities. Similar simpli cations are also used in [15].
The simulation was carried out on a laptop with Core i7-8565U CPU and 16GB RAM. The pixel values and positions of sample images were extracted using the open-source software OpenCv. The subsequent calculations were based on the array computing package NumPy with python. The calculation results for three different selections of {b}_{R}, {b}_{G}, and {b}_{B} are given in Table 4. In all cases, image g and h have the largest similarity, which is consistent with prediction as they look the most similar visually. Image i and j also have a similarity more than 50% when {b}_{R}, {b}_{G}, {b}_{B}=1. This similarity has a rapid decrease when {b}_{B} is increased to 2. However, it doesn't fall so quickly when {b}_{G} is increased to 2. We suppose that this is due to the large proportion of blue part in image i and j resulting in the great impact of blue components on similarity.

Conclusion
In this study, three algorithms are proposed to compare the similarity between two quantum images. The algorithms are suitable for binary, grey, and color images respectively. Compared with existing methods, especially with the algorithms also based on NEQR and NCQI [15,32], our work has the following contributions.
Firstly, under the condition that the image preparation is not included, the proposed algorithms can achieve exponential acceleration than the previous and classical methods.
Secondly, the binarization of grey and color images in [15] is slightly de cient due to the lack of available quantum circuits. Our study makes a progress in assessing the similarity of grey and color images based on NEQR and NCQI.
Finally, although it is a primitive demonstration with binary images that have only two pixels, this is the rst time to compare the similarity between two quantum images on a real quantum computer to the best of our knowledge.
This study is a preliminary attempt of the practical application of quantum computing in the image processing eld. Experiment and simulation results have indicated the effectiveness of the algorithms. However, as shown in Table 4, an inappropriate selection of pixel value qubits in comparing color images may cause a signi cant decrease of results. A qubits selection strategy remains to be developed for future studies. Three steps in comparing the similarity between two quantum images. Different colors represent qubits carrying different functions. The dot on endpoint indicates a multi qubits string. M represents measurement operation. Figure 2 a: The quantum circuit for comparing qubits from img-qubits1 and img-qubits2. This circuit de nes the mapping between two NEQR image strings and AuxBit1. The output is XNOR of two input. b: The quantum circuit that compresses |11…11> to |1> and the other states to |0>. This circuit equals the Control-Not gate conditioning on the whole AuxBit1. The qubits with the same color as in Fig. 1 have the same function.   Three binary images used for calculating similarity. The image representation states are shown below the images. The pixel value qubits are underlined. Probability amplitudes are not given for simpli cation.  Three grey images used for calculating similarities. The image representation states are shown below the images. The pixel value qubits are underlined. Probability amplitudes are not given for simpli cation.