Quantum Image Processing Algorithm Using Line Detection Mask Based on NEQR

Line detection is a fundamental technique in image processing. It can extract the required information, while the information that does not need attention can be ignored, thus reducing the amount of data. At the same time, line detection is also the basis of image segmentation and plays an important role in this process. In this paper, we implement a quantum algorithm based on a line detection mask for novel enhanced quantum representation (NEQR). We build a quantum algorithm for line detection in different directions and design a quantum circuit for line detection. The detailed module designed is also provided. On a classical computer, we simulate the quantum method, and the simulation results prove the feasibility of the quantum method. By analyzing the complexity of quantum line detection, we find that the computation complexity of the proposed method is improved compared to some similar edge detection algorithms.


Introduction
Quantum technology has developed rapidly in recent years. Great progress has been made in various fields of quantum information science. In particular, some quantum computers have been developed, such as Google's quantum hegemony [1], which has brought quantum information research to worldwide attention. Due to the advantages of quantum parallel computation, it has the ability to surpass classical computation in many aspects. For example, the time complexity of quantum algorithms will not grow exponentially as the number of bits increases. Quantum information technology also shows some advantages in image processing; the new research frontier is named quantum image processing (QIP). Quantum information shows its prospects in almost all aspects of image processing. This includes the quantum representation of images for which different representations are already available. In other aspects, quantum versions of image processing have also been developed, including image scrambling [2,3], image scaling [4,5], image transformation [6][7][8][9], image filtering [10][11][12], edge detection [13][14][15][16][17][18][19][20][21][22], image segmentation [23,24], image encryption [25][26][27][28][29][30][31][32][33][34][35][36][37], feature extraction [38], etc. Compared with classical methods, these quantum image processing methods show great advantages. For example, the unique properties of quantum mechanics, such as entanglement, parallelism, and superposition, can be used in image processing, and this can process the pixels simultaneously and provide acceleration in the speed of image processing. The increasing demand for high-quality images today has also led to an increase in image storage space. Quantum image processing can conveniently store larger images and effectively process them in real time.
One class of problems in image segmentation techniques is image line detection. In the process, we are interested in a line in a certain direction, and other lines and other image content can be ignored. After the operation, the information of concern can be obtained and the sizes of image datasets are reduced. Thus, the line detection method is necessary in image processing. In addition, line detection also constitutes the basis of image segmentation, and it has also significant reference value for the construction of

The Preparation of Quantum Line Detection
In this section, we describe the preparatory work before implementing quantum line detection, including the implementation method of line detection in classical image processing, the representation method (NEQR) of quantum images used in the paper, and the quantum module used in quantum line detection.

The Classical Line Detection Method
Classical line detection is a method using masks [39]. The process is to apply the line detection mask to the gray-scale image of the original picture. In other words, using masks and the digitized gray-scale image for convolution or correlation calculation, a calculation result can be obtained; then, after setting an appropriate threshold, the new lines detected from the gray-scale image can be obtained. According to the detected lines in different directions, these lines correspond to four types of masks, which are the horizontal mask, vertical mask, +45 • mask, and −45 • mask [39]. These masks are as follows (Figure 1).
For example, using the horizontal mask, the horizontal line can be calculated by the following equation: where p is a digital image, and L H is the detected horizontal line.   where p is a digital image, and L H is the detected horizontal line.

Quantum Representation of Image
The quantum representation methods of images are categorized into different types. For example, there are the FRQI [40], MCRQI [41], NEQR [42], GQIR [43], and QIRHSI [44] methods, etc. It is indicated that the NEQR method is more suitable for gray-scale image operation. Since quantum line detection of images involves a large number of gray-scale pixel operations, we use the NEQR representation for line detection in this paper.
The NEQR model stores the gray values and pixel coordinates of an image in a quantum state. For a 2 n × 2 n quantum gray-scale image, its NEQR representation is where I is the quantum image, CYX are gray-scale values, The representation expression of the 2 × 2 image in NEQR is shown in Figure 2.

Quantum Representation of Image
The quantum representation methods of images are categorized into different types. For example, there are the FRQI [40], MCRQI [41], NEQR [42], GQIR [43], and QIRHSI [44] methods, etc. It is indicated that the NEQR method is more suitable for gray-scale image operation. Since quantum line detection of images involves a large number of gray-scale pixel operations, we use the NEQR representation for line detection in this paper.
The NEQR model stores the gray values and pixel coordinates of an image in a quantum state. For a 2 n × 2 n quantum gray-scale image, its NEQR representation is The representation expression of the 2 × 2 image in NEQR is shown in Figure 2.  Using this module, we can copy a known quantum image. Specifically, given an  Using this module, we can copy a known quantum image. Specifically, given an empty image, the known image is copied to the empty image. The CNOT gate can perform this operation [20]. It is worth noting that the quantum no-cloning principle is not violated here. The quantum no-cloning principle means that the unknown quantum state cannot be copied exactly. Here, the copied quantum state is a known state. The copy module and its simplified diagram are represented in Figure 3.

Quantum Copy Module
Using this module, we can copy a known quantum image. Specifically, given an empty image, the known image is copied to the empty image. The CNOT gate can perform this operation [20]. It is worth noting that the quantum no-cloning principle is not violated here. The quantum no-cloning principle means that the unknown quantum state cannot be copied exactly. Here, the copied quantum state is a known state. The copy module and its simplified diagram are represented in Figure 3.

Quantum Double (DO) Module
In quantum circuit design for line detection, quantum bits need to be multiplied by two. This can be achieved by adding 0 to the last position of the binary bit string. For example, The operation can be realized by the auxiliary 0 qubit and the flip gates [15]. The quantum double module is shown in Figure 4.

Quantum Double (DO) Module
In quantum circuit design for line detection, quantum bits need to be multiplied by two. This can be achieved by adding 0 to the last position of the binary bit string. For example, c q−1 c q−2 . . . c 0 0 = 2c q−1 c q−2 . . . c 0 . The operation can be realized by the auxiliary 0 qubit and the flip gates [15]. The quantum double module is shown in Figure 4.  Using this module, we can copy a known quantum image. Specifically, given an empty image, the known image is copied to the empty image. The CNOT gate can perform this operation [20]. It is worth noting that the quantum no-cloning principle is not violated here. The quantum no-cloning principle means that the unknown quantum state cannot be copied exactly. Here, the copied quantum state is a known state. The copy module and its simplified diagram are represented in Figure 3.

Quantum Double (DO) Module
In quantum circuit design for line detection, quantum bits need to be multiplied by two. This can be achieved by adding 0 to the last position of the binary bit string. For example, The operation can be realized by the auxiliary 0 qubit and the flip gates [15]. The quantum double module is shown in Figure 4.

Cycle Shift Transformation Module
The purpose of the cyclic shift transform is to move the whole image. With the transformation operation, the pixel values of the whole image move to the adjacent positions at the same time [19,38]. For example, the right shift operation means that all pixels of the image are shifted to the right by one unit. The right or left and upward or downward shift can be expressed by Equations (4) and (5), respectively. The corresponding circuit diagram is represented and simplified in Figure 5.

Reversible Parallel Adder (PA) Module
The reversible parallel adder module is used to calculate the sum of two n bit numbers. For example, if can be calculated through this circuit. This can be achieved through the MAJ (Majority) gate and UMA (Un Majority and Add) gate. In reference [19], more detailed explanations are provided. The circuit diagram is shown in Figure 6.  If X is used to replace X + 1 and Y is used to replace Y + 1, the above Equations (4) and (5) can be changed to where X = (X ∓ 1)mod2 n , and Y = (Y ∓ 1)mod2 n .

Reversible Parallel Adder (PA) Module
The reversible parallel adder module is used to calculate the sum of two n bit numbers. For example, if |A = |a n−1 a n−2 . . . a 1 a 0 , |B = |b n−1 b n−2 . . . b 1 b 0 , |C = |A + |B can be calculated through this circuit. This can be achieved through the MAJ (Majority) gate and UMA (Un Majority and Add) gate. In reference [19], more detailed explanations are provided. The circuit diagram is shown in Figure 6.

Reversible Parallel Adder (PA) Module
The reversible parallel adder module is used to calculate the sum bers. For example, if calculated through this circuit. This can be achieved through the MAJ ( UMA (Un Majority and Add) gate. In reference [19], more detailed exp vided. The circuit diagram is shown in Figure 6.

Quantum Absolute Value Module for Subtraction of Two Positive Integers
To calculate the subtraction of two positive integers, the binary complement is introduced [45]. The subtraction of two positive integers can be regarded as a positive integer plus a negative integer. Here, the binary sequence of integers will become the complement with sign bits. For the representation of the complement, the first bit is the sign bit, where 0 represents a positive number and 1 represents a negative number. The other bits represent the numeric value. For a positive integer, the complement representation is the same as the normal binary representation except for the sign bit. For a negative integer, its numeric bits need to be negated and 1 added. For example, if there are 6 bit representations, for the decimal numbers 5 and −3, their complements are 0 00101, 1 11101, respectively. Generally, for the integer x with n bits, its complement is where |x|= x n−1 x n−2 · · · x 2 x 1 , x l = 1 − x l , l = 1, 2, . . . n − 1.
After representation as a complement, the difference between a positive integer and another positive integer can be directly calculated as the complement of the positive integer plus the complement of negative integer. Therefore, the sum of their complements can be directly calculated with the PA module [45]. After this operation, we obtain the result of subtraction of two positive integers. The result is a complement representation with a signed bit. After obtaining the result, we need to calculate the absolute value. As we know, the absolute value is the part of a signed number where the sign bit has been removed. At this time, the number is still represented by a complement and needs to be restored to the original code. There are two cases: if the sign number is 0, it is a positive integer, and its original code is the same as the complement except for the sign bit. In this case, we simply need to remove the sign number to obtain the absolute value. In our complement module (CA) circuit, the sign bit is used as a control bit for complement operation. For positive numbers, the sign bit is 0, so no operation is applied to other bits except for the sign bit. Thus, the positive number will not change after passing through the CA module. As soon as the sign bit is removed, it becomes the absolute value of the positive number. If the sign bit is 1, it is a negative integer. It needs to be changed into the original code, and then the sign bit is removed to obtain the absolute value. The process of changing to the original code is to remove the sign bit firstly, and then invert it (0 convert to 1, 1 convert to 0), and add 1. The process is the same as the operation in which the source code transforms into a complement. For example, for the decimal number −3, its complement is 1 11101, and its original code with a sign bit is 1 00011. In our CA circuit, the sign bit is 1; it is a control bit to perform the conversion operation. At this time, the absolute value can be obtained by removing the sign bit. The circuit diagrams for the complement module and absolute value (abs) module are shown in Figure 7. tive integer, its numeric bits need to be negated and 1 added. For example, if there are 6 bit representations, for the decimal numbers 5 and −3, their complements are 0 00101, 1 11101, respectively. Generally, for the integer x with n bits, its complement is After representation as a complement, the difference between a positive integer and another positive integer can be directly calculated as the complement of the positive integer plus the complement of negative integer. Therefore, the sum of their complements can be directly calculated with the PA module [45]. After this operation, we obtain the result of subtraction of two positive integers. The result is a complement representation with a signed bit. After obtaining the result, we need to calculate the absolute value. As we know, the absolute value is the part of a signed number where the sign bit has been removed. At this time, the number is still represented by a complement and needs to be restored to the original code. There are two cases: if the sign number is 0, it is a positive integer, and its original code is the same as the complement except for the sign bit. In this case, we simply need to remove the sign number to obtain the absolute value. In our complement module (CA) circuit, the sign bit is used as a control bit for complement operation. For positive numbers, the sign bit is 0, so no operation is applied to other bits except for the sign bit. Thus, the positive number will not change after passing through the CA module. As soon as the sign bit is removed, it becomes the absolute value of the positive number. If the sign bit is 1, it is a negative integer. It needs to be changed into the original code, and then the sign bit is removed to obtain the absolute value. The process of changing to the original code is to remove the sign bit firstly, and then invert it (0 convert to 1, 1 convert to 0), and add 1. The process is the same as the operation in which the source code transforms into a complement. For example, for the decimal number −3, its complement is 1 11101, and its original code with a sign bit is 1 00011. In our CA circuit, the sign bit is 1; it is a control bit to perform the conversion operation. At this time, the absolute value can be obtained by removing the sign bit. The circuit diagrams for the complement module and absolute value (abs) module are shown in Figure 7.

Threshold Classification (TC) Module
In the process of quantum line detection, it is necessary to classify the gray-scale values calculated. This requires us to determine a threshold value. The gray-scale values that are smaller than the threshold value become 0 (black), and those that are larger than the threshold value become 255 (white). This process uses the comparison module, and we use the comparison module in Ref. [18] as part of our circuit. The specific process is as follows. If there are two numbers x, y, their binary representations are

Threshold Classification (TC) Module
In the process of quantum line detection, it is necessary to classify the gray-scale values calculated. This requires us to determine a threshold value. The gray-scale values that are smaller than the threshold value become 0 (black), and those that are larger than the threshold value become 255 (white). This process uses the comparison module, and we use the comparison module in Ref. [18] as part of our circuit. The specific process is as follows. If there are two numbers x, y, their binary representations are x = x n−1 x n−2 · · · x 1 x 0 , y = y n−1 y n−2 · · · y 1 y 0 , respectively. In a quantum circuit, |x , |y are inputs, and qubits e 1 and e 0 are outputs. If e 1 e 0 = 10, then x > y; if e 1 e 0 = 01, then x < y; and if e 1 e 0 = 00, then x = y. In order to compare it with the threshold value, we take x as the gray-scale value and y as the threshold value, which can be recorded as |y = T . In order to segment the threshold output, it is necessary to introduce the auxiliary bits, which are |0 ⊗n bits. Then, the gray-scale values can be classified by the CNOT gate. After this operation, the image is converted into a binary image by threshold value classification. The circuit is shown in Figure 8.
In order to compare it with the threshold value, we take x as the gray-scal and y as the threshold value, which can be recorded as y T  . In order to segm threshold output, it is necessary to introduce the auxiliary bits, which are 0 Then, the gray-scale values can be classified by the CNOT gate. After this operati image is converted into a binary image by threshold value classification. The ci shown in Figure 8.

Quantum Line Detection Algorithm
In this section, we introduce the specific algorithm for the quantum line dete images. It is based on the modules from the previous section. The algorithm con five steps: (1) Quantum image representation; (2) Quantum image shift transformation; (3) Calculation of lines in different directions; (4) Threshold value classification; (5) Quantum measurement operation.

Step 1: Quantum Image Representation
As introduced in Section 2.2, we use the NEQR model to represent quantu tures. For a 2 n × 2 n image, 2n position bits are required to represent the pixel pos the gray-scale range for the image is 1 0~2 q , q bits are needed to repres

Quantum Line Detection Algorithm
In this section, we introduce the specific algorithm for the quantum line detection of images. It is based on the modules from the previous section. The algorithm consists of five steps: (1) Quantum image representation; (2) Quantum image shift transformation; (3) Calculation of lines in different directions; (4) Threshold value classification; (5) Quantum measurement operation.

Step 1: Quantum Image Representation
As introduced in Section 2.2, we use the NEQR model to represent quantum pictures. For a 2 n × 2 n image, 2n position bits are required to represent the pixel position. If the gray-scale range for the image is 0 ∼ 2 q−1 , q bits are needed to represent the gray-scale values. For example, if there are 256 gray-scale levels, 8 bits are required to represent the gray-scales.

Step 2: Quantum Image Shift Transformation
In classical line detection for an image, the line detection masks (Figure 1) are used to act on the gray-scale image (through convolution or correlation calculation), and desired lines can be detected. However, quantum convolution is very difficult, and even considered unachievable in some works [18]. Here, we first move the original image in eight directions around the pixel coordinate XY. By adding moving images to the original image, we can obtain nine images. The gray-scale values in the same position of the nine images are multiplied by the coefficients of the corresponding nine positions in the mask, and the products are added to obtain the same effects, such as classical convolution or correlation between the mask and original image. The moving operators are expressed in Figure 9.
Here, the original image needs to be copied into 8 identical images, which can be done by the quantum copy module.
After the image is copied into eight identical images, we need to use the shift transformation operation to place the eight images at a 3 × 3 vicinity of pixel XY. In a quantum circuit, this is done using cycle shift transformation modules. Transformations in eight directions are listed in Algorithm 1. Through the transferring operation, the images become the following quantum state: (9) mask, and the products are added to obtain the same effects, such as classical convolution or correlation between the mask and original image. The moving operators are expressed in Figure 9.
Here, the original image needs to be copied into 8 identical images, which can be done by the quantum copy module.
After the image is copied into eight identical images, we need to use the shift transformation operation to place the eight images at a 3 × 3 vicinity of pixel XY. In a quantum circuit, this is done using cycle shift transformation modules. Transformations in eight directions are listed in Algorithm 1. Through the transferring operation, the images become the following quantum state:

Step 3: Quantum Line Calculation in Different Directions
After the transfer operation, the eight gray-scale values adjacent to the XY pixel position of the original image will be moved to the XY position of the eight new images. Now, by calculating the products of the gray-scale values and the coefficients in the mask, the results, such as convolution or correlation calculation in the classical method, can be obtained. Then, the lines in different directions of the image |I YX can be calculated using Equations (10)- (13). Lines in four directions are horizontal, vertical, +45 • , and −45 • lines.
In quantum circuit design, the calculation of the above lines can be performed by the quantum double module, PA module, and quantum absolute value module (the subtraction of converted into complements). The calculation circuits are shown in Figures 10 and 11.

Step 4: Quantum Threshold Value Classification
After calculating the lines in different directions, the obtained images can be classified by the threshold. In other words, the gray-scales below a certain threshold are set to 0, and the gray-scales above the threshold are set to 2 q − 1. It can be expressed by Equation

Step 4: Quantum Threshold Value Classification
After calculating the lines in different directions, the obtained images can be classified by the threshold. In other words, the gray-scales below a certain threshold are set to 0, and the gray-scales above the threshold are set to 2 q − 1. It can be expressed by Equation

Step 4: Quantum Threshold Value Classification
After calculating the lines in different directions, the obtained images can be classified by the threshold. In other words, the gray-scales below a certain threshold are set to 0, and the gray-scales above the threshold are set to 2 q − 1. It can be expressed by Equation (14). In this way, the image becomes a binary image. The gray-scales that become 2 q − 1 constitute the detected lines. The threshold classification (TC) module can be used to design the quantum circuits. (14) where U T indicates the threshold classification operation. Through the threshold classification operation, the line detected is represented by a quantum superposition state.

Step 5: Quantum Measurement Operation
After the threshold classification operation, the quantum image needs to be restored to the classical image by quantum measurement. Finally, the quantum line detection is completed. However, line detection is only one step of quantum image preprocessing, and we do not consider the quantum measurement. Its time complexity is also very large (O(2 2n )).
The quantum circuit diagrams for the horizontal line detection and +45 • line detection are shown in Figures 10 and 11, respectively. The vertical line detection and −45 • line detection are not given, because they are similar to the above-mentioned two detections.

Complexity Analysis
In this section, we analyze the complexity of the quantum line detection circuit. The complexity of the circuit is analyzed according to the gates used in circuits, such as the CNOT gate, Toffoli gate, Hadamard gate, etc. We analyze the complexity based on several steps of line detection.
Firstly, we use the NEQR method to represent images. For a 2 n × 2 n image, there are 2 q − 1 gray-scale levels, and its complexity is no more than O(qn2 2n ) [42].
Secondly, we perform the cycle shift transformation operation. Before cycle transformation, we need to copy the original image into eight identical copies, and the copy uses the CONT gate. For the CONT gate, its complexity is O(q) [15]. For the cycle shift transformation operations, its complexity is O(n 2 ) [19]. Therefore, the complexity of the whole process is O(n 2 + q).
Again, we perform the calculation of lines in different directions. This step uses the quantum double (DO) module, reversible parallel adder (PA) module, and absolute value module (abs). For the quantum double module, its complexity is O(q) [20]. The complexity of the reversible parallel adder (PA) is also O(q) [19]. However, for the absolute value module (abs), its circuit complexity is O(q 2 ) [20]. Therefore, the complexity for this step is O(q 2 ).
Finally, in the threshold classification step, the comparison operation and the CNOT gates are used. For the comparison operation, which is the comparison of the gray-scale values and threshold value, the complexity is O(q 2 ) [18]. For the CNOT gate, its complexity is O(q). In this step, the total complexity is O(q 2 + q).
In summary, according to the above analysis, the overall complexity is O[qn2 2n + (n 2 + q) + q 2 + (q 2 + q)] = O(qn2 2n + n 2 + q 2 ) in the whole circuit. It is O(n 2 + q 2 ) except for the NEQR representation. For the completeness of the paper, we give the quantum state representation of the image, line detection processes, and measurement processes for image restoration. However, for general considerations, the NEQR representation for the image and the quantum measurement to restore the image are not considered part of quantum image processing and are not included in the complexity analysis [15,17,19,20]. Thus, for a 2 n × 2 n image, the complexity of quantum line detection processes is O(n 2 + q 2 ). For the classical line detection process of such an image, its complexity is no less than O(2 2n ). The complexity of our method is only a second-order polynomial function, which provides exponential acceleration. The time complexity of our scheme, classical line detection, and some similar edge detection algorithms is listed in Table 1. The first three rows are classic processing methods, and the following rows are the quantum method. It can be seen that our scheme has some advantages compared with other quantum algorithms. Table 1. Comparison of the complexity between our scheme and other quantum edge detection algorithms.

Algorithm
QIR Model

Simulation Analysis
In order to test the performance of our quantum algorithm, we simulated the quantum algorithm with MATLAB on a classical computer. MATLAB2014 was used. Our computer simulation used the same steps as the quantum algorithm. It proves the feasibility of the quantum algorithm to some extent. We selected three common test images for the line detection simulation, such as the wiring diagram of a circuit board, a house, and rice ( Figure 12a) [39]. In Figure 12b-e, the line detection results for the horizontal, vertical, +45 • , and −45 • masks are shown, respectively.
For the simulation results, it can be clearly seen that the horizontal and vertical masks mainly detect horizontal and vertical lines, respectively. The +45 • and −45 • masks are mainly used to detect the lines in the 45 • and −45 • directions, and oblique lines in other directions can also be detected. The horizontal and vertical masks can partly detect the oblique lines, because the oblique lines are also composed of horizontal and vertical lines. It can be seen from the detection of the wiring diagram of the circuit board, for the +45 • mask, that there is more clarity for the diagonal lines close to +45 • . In the −45 • direction, the lines detected become double lines. The phenomenon is similar for the −45 • mask. For the detection of rice, the masks for different directions can detect the corresponding rice closer to the direction. For example, the vertical mask detects more rice in the vertical direction. During the computer simulations, we set the gray-scale threshold to 128 in the wiring diagram and rice detections and 140 for the house detection.
the oblique lines, because the oblique lines are also composed of horizontal and vertical lines. It can be seen from the detection of the wiring diagram of the circuit board, for the +45° mask, that there is more clarity for the diagonal lines close to +45°. In the −45° direction, the lines detected become double lines. The phenomenon is similar for the −45° mask. For the detection of rice, the masks for different directions can detect the corresponding rice closer to the direction. For example, the vertical mask detects more rice in the vertical direction. During the computer simulations, we set the gray-scale threshold to 128 in the wiring diagram and rice detections and 140 for the house detection.

Conclusions
In this paper, we have developed a quantum line detection method. Based on the NEQR model of representing images, firstly, we introduced the quantum modules for line detection. Based on these quantum modules, we then established the quantum line