1 Introduction

Quantum image processing (QIMP) is an emerging area comprising the use of quantum computing and quantum information processing to design and work with quantum images. This field tries to answer aspects such as what is the best way to represent images on quantum computers and how one should prepare, process, and retrieve them  [1, 2]. The main idea is to take advantage of the powerful properties of quantum computation, like entanglement and parallelism, to achieve advances in the area of image processing [3].

Usually, the first step to work with quantum algorithms on images is to encode images in quantum states [4]. The most commonly two methods for encoding gray-level images in quantum states are the Flexible Representation of Quantum Images (FRQI) [5] and the Novel Enhanced Quantum Representation (NEQR) [6]. In FRQI, the color (gray level) information is encoded in the probability amplitude of one qubit state  [5]. On the contrary, in NEQR the color information is stored in the multi-qubit computational basis of the superposition state but not in the probability amplitude [7]. The latter method is the one considered in this paper, as it allows for advanced image transformations.

Once images are encoded, several transformations can be applied to them. One of the most useful transformations is image segmentation, which can be accomplished by various means, such as histogram calculation  [8, 9], quantum search algorithms [10], or dual-threshold image segmentation [7, 11]. Histogram calculation and quantum search methods use amplitude amplification and can achieve a significant speedup. However, these algorithms lack an explicit description of the oracle’s circuit or they require a high number of qubits to implement (more than 50) [7].

In this work, we focus our attention in the dual-threshold image segmentation method. It is based on identifying if a value of the image is contained between the range of values set by two thresholds. Threshold-based segmentation methods are computationally efficient, straightforward, and widely used in fields as diverse as medicine [12] or precision agriculture. For example, segmentation based in dual thresholding has been effectively used in maize leaf disease image segmentation [13] and estimation for mature rice crops [14].

In this work, a fault-tolerant quantum algorithm for dual-threshold image segmentation has been implemented. Quantum circuits are very sensitive to external and internal noise. Therefore, the proposed quantum algorithm has been built only with Clifford+T gates which have been shown to make fault-tolerant circuits thanks to the use of error detection and correction codes [15, 16]. Because the fault-tolerant implementations of T gates are more expensive than other gates in terms of space and time cost [17,18,19], we have focused on minimized these type of gates. The two state-of-the-art algorithms that compute dual-threshold image segmentation are those proposed by Yuan et al. [7] and Wang et al. [11]. The former is based on full comparators and the latter on half comparators and is, therefore, more limited. However, neither of them is implemented using Clifford+T gates. There are also some theoretical algorithms for quantum image segmentation, but either they do not provide a real circuit implementation for the oracle design, or they use a high number of qubits that make simulation not viable [5, 7,8,9]. Our proposed algorithm aims to provide a reversible, fault-tolerant and efficient alternative, since noise tolerance and fault tolerance are two of the most important objectives in quantum computing.

As discussed above, the dual-threshold algorithm involves two comparison operations. To perform these operations, two alternative fault-tolerant quantum circuit comparators have also been proposed. They have been built using only the group of gates called Clifford+T, making possible the use of error-correcting codes to ensure fault tolerance.

When designing quantum circuits, it is very important to specify the metrics used to assess the efficiency of the circuits. In this paper, we have focused our attention on improving two key performance metrics, T-count (the number of T gates a circuit has) and T-depth (the number of T gates which must be computed sequentially), using Clifford+T gates. We have also paid special attention to the number of ancilla qubits.

The main contributions of this paper are: (1) the implementation of an efficient, reversible and fault-tolerant quantum dual-threshold image segmentation algorithm, and (2) the evaluation of the dual-threshold segmentation algorithm with respect to other state-of-the-art algorithms. Additionally, we have proposed the design of two fault-tolerant quantum full comparators which outperform the state-of-the-art comparators in terms of T-count and T-depth.

The rest of the manuscript is organized as follows. Section 2 explains the QIMP encoding and quantum gate selection. Section 2.1 describes the NEQR model for quantum image representations based on gray levels, and Sect. 2.2 describes the selected gates to implement the algorithm. Section 3 describes the implementation of the proposed dual-threshold image segmentation algorithm and the design of two efficient quantum comparators. In Sect. 4, the dual-threshold image segmentation algorithm and the comparators designed for its implementation have been compared with their state-of-the-art counterparts. Finally, in Sect. 5 the main conclusions and ideas for future work can be found.

2 Quantum representation and quantum gates

2.1 NEQR image representation

The Novel Enhanced Quantum Representation (NEQR) is one of the earlier forms of quantum image representation, and it is broadly used in QIMP [6]. Quantum image representation models can be classified under various categories based on the requirements defined to store the content of an image in a quantum system, i.e., image color model, image coordinate model, and image color information encoding model [1]. The color model for NEQR is based on grayscale, and its coordinate model is Cartesian. In terms of image color information encoding, this model uses the basis state of a qubit sequence to store the grayscale value of every pixel. NEQR offers some advantages over other early representation models such as FRQI: there is a quadratic speedup of the time complexity to prepare the NEQR quantum image (a time complexity of \(O(2^{4n})\) in FRQI versus \(O(qn2^{2n})\) for NEQR, where n is the \(log_{2}\) of the size for a \(2^n\times 2^n\) size image and q represents the color range as [0, \(2^{q-1}\)]. However, there are some variants of FRQI such as IFRQI that address this and reduce their time complexity), optimized image compression ratio of up to 1.5 \(\times\), its image retrieval is accurate (not probabilistic as FRQI), and advanced color and segmentation operations can be achieved [6].

In the NEQR representation, a \(2^n\times 2^n\) image is represented according to the following formula:

$$\begin{aligned} \vert C_{YX}\rangle = \frac{1}{2^n} \sum \limits _{Y=0}^{2^n-1} \sum \limits _{X=0}^{2^n-1} \vert C^{q-1}_{YX} C^{q-2}_{YX} ... C^{1}_{YX} C^{0}_{YX}\rangle \otimes \vert YX\rangle \end{aligned}$$
(1)

where \(\vert C^{q-1}_{YX} C^{q-2}_{YX}... C^{1}_{YX} C^{0}_{YX}\rangle\) encodes the color value of the pixel (Y, X), n is related to the size of the image, and q defines the color range as [0, \(2^{q-1}\)]. YX encodes the spatial location of the pixel in a cartesian coordinate model.

NEQR requires \(2n + q + 2n - 2\) qubits to prepare a quantum image, for a \(2^n \times 2^n\) size image and the grayscale range being [0, \(2^{q-1}\)], where 2n qubits are used for position information, q qubits for the gray-scale information and \(2n - 2\) are auxiliary qubits [6]. Yuan et al. [7] proposed a strategy to reduce the required number of qubits to \(2n + q + 2\) by reusing qubits with reset operation, which is achievable to reset qubit to \(\vert 0\rangle\) state in the middle of computation [20]. Both approaches are used to obtain a NEQR-encoded image to perform the quantum double-threshold operation, but the latter has the disadvantage of using the reset operation. This operation might not be optimal in some cases since is not available in all quantum devices and it involves non-Clifford gates.

2.2 Quantum gates

Number of qubits is an important metric in quantum circuit design due to the scarcity of quantum computational resources. Further, scalable and reliable quantum circuits are based on fault-tolerant implementations of quantum gates such as Clifford+T gates [17]. This is due to the fact that physical quantum computers are prone to noise errors [21,22,23,24], but fault-tolerant implementations of quantum gates and quantum error-correcting codes can be used to overcome the limits imposed by noise errors [24, 25]. Since quantum circuits are very sensitive to external and internal noise, the proposed quantum algorithm has been built only with Clifford+T gates. The symbols and matrices associated with these quantum gates are observed in Fig. 1.

Fig. 1
figure 1

Symbols and matrices of the Clifford+T gate set used in the proposed dual-threshold image segmentation algorithm

The chosen gates have been selected taking into account the best T-count and T-depth optimization strategies. For example, to implement an AND operation, the proposed circuits use the temporary logical-AND gate [26] in order to reduce the number of involved T gates. This gate performs an AND operation of two inputs (qubits), saving the result in an ancilla qubit. Temporary logical-AND and Toffoli gates are similar, but temporary logical-AND has a better T-count (4 vs. 7 for the Toffoli gate) and T-depth (2 vs. 3 for the Toffoli gate). Uncomputation of temporary logical-AND adds another level of cost efficiency, since it does not require T gates as opposed to uncomputation of Toffoli gate that requires another Toffoli gate. The temporary logical-AND gate and its uncomputation gate are shown in Fig. 2 [18].

Fig. 2
figure 2

Temporary logical-AND gate and its uncomputation. Z gate represents Pauli-Z gate [18]

In order to replace a Toffoli gate by a Temporary logical-AND operation, the AND operation must be performed on an auxiliary qubit and not on quantum states already initialized. So, not all Toffoli gates can be directly replaced by a Temporary logical-AND gate, and these changes will require some additional ancilla qubits for reducing the T-count.

3 Fault-tolerant implementation of dual-threshold image segmentation

3.1 State-of-the-art comparator circuits for dual-threshold image segmentation

The threshold segmentation algorithm proposed by Yuan et al. [7] sets all values in an image to 0 except those between the two given thresholds. Suppose the original image is f(xy), T1 is the lower threshold, T2 is the upper threshold and g(xy) is the output image. The double-threshold segmentation algorithm can be described as shown:

$$\begin{aligned} \left\{ \begin{array}{ll} g (x, y) = f (x, y) &{} \mathrm {if\ } T1 \le f (x, y) \le T2 \\ g (x, y) = 0 &{} \mathrm {{otherwise}\ } \end{array} \right. \end{aligned}$$
(2)

The fundamental operation of the algorithm is a comparator, as can be deduced from Eq. 2. As part of their algorithm, Yuan et al. [7] presented a comparator optimized in terms of ancilla qubits. It involves \(5N-3\) Toffoli gates (being N the size of the numbers to be compared, that is, the number of bits), so it has a T-count of \(35N-21\). This comparator wisely uses the reset operation presented by Shende et al. in  [20] to reduce the required number of qubits in the circuit. Thanks to this reset operation, the same qubits can be reused in different parts of the circuit. However, the quantum gates that compute the reset operation do not belong to the Clifford+T group. Moreover, this operation is not trivial to perform since it requires customizing the angles to each specific quantum state to be reset, something that currently available quantum devices cannot do without programmer intervention. For instance, on the IBM Quantum platform, the reset operation is only available directly as a non-reversible operation, so it is not a valid option if the reversibility of the circuit must be preserved. Reversibility of the circuit is essential if it is to be used as part of a complete quantum algorithm, which is precisely the case with this comparator.

The comparator of Yuan et al. [7] is shown in Fig. 3. To avoid the 2N reset operations, the uncomputation of the ancilla qubits must be done using Clifford+T gates. Since all the circuit except the reset operations is made exclusively of Clifford+T gates, the Bennett’s garbage removal scheme can be used for the uncomputation [27]. Following this scheme, it is obtained a new circuit (Fig. 4), which is now composed exclusively of Clifford+T gates and is therefore compatible with the error detection and correction codes associated with such gates. However, the price to pay is high: the depth of the circuit and the number of ancilla qubits have been doubled. Worse, its T-count has been also doubled: from \(35N-21\) to \(70N-42\).

Fig. 3
figure 3

Comparator circuit proposed by Yuan et al. [7], for the case \(N=3\). It is optimized in terms of ancilla qubits. However, it involves non-Clifford+T gates to perform the reset operation. Moreover, the reset operation is not available in all quantum devices. The use of the reset operation makes the circuit non-reversible

Fig. 4
figure 4

Comparator obtained from replacing the reset operations in the circuit of Yuan et al. [7]. The uncomputation part consists of applying the same circuit, but in reverse order. This circuit has only Clifford+T gates, so it is fault-tolerant. However, it is twice as deep and has twice as many ancilla qubits as the original circuit

3.2 Proposed comparator circuits for dual-threshold image segmentation

As mentioned above, the circuit in Fig. 4 has a prohibitive T-count, but several improvements can be proposed to implement fault tolerance and to reduce T-count and T-depth metrics. T-count can easily be reduced by replacing some Toffoli gates with Temporary logical-AND gates. Most of the Toffoli gates in the Yuan et al. circuit (and in the Clifford+T version obtained from it) act on an auxiliary qubit and not on quantum states already initialized, so they can be directly replaced by Temporary-logical-AND operations. In particular, \(3N-1\) Toffoli gates can be substituted, while \(2N-2\) Toffoli gates act on existing states and cannot be replaced (at least not directly). Considering the uncomputation part, \(3N-1\) uncomputation gates of the Temporary logical-AND operation, and \(2N-2\) extra Toffoli gates will be necessary. The final T-count of this circuit is \(40N-32\). An example of this new version, for the \(N=3\) case, is shown in Fig. 5.

Fig. 5
figure 5

First proposal for quantum comparator circuit focused on reducing the T-count, for the \(N=3\) case. \(a_{i}\) and \(b_{i}\) are the qubits of the two number A and B to be compared. Outputs marked as x and y are the outputs of the comparison

The T-count of the obtained circuit is high due to the \(2N-2\) remaining Toffoli gates. As mentioned, such gates cannot be directly replaced since they act over non-auxiliary qubits. However, if the goal is to reduce the T-count as much as possible, these gates can be replaced at the cost of increasing the number of ancilla qubits. Thus, a Toffoli gate that involves three qubits \(\vert A\rangle , \vert B\rangle\) (control qubits) and \(\vert C\rangle\) (target qubit) can be replaced by a temporary logical-AND gate and a CNOT gate. The temporary gate will use \(\vert A\rangle\) and \(\vert B\rangle\) as control qubits, and an ancilla qubit as target one, whereas the CNOT gate will use the previous ancilla qubit as control qubit, and \(\vert C\rangle\) as target one. If the operations are rearranged so that the original result is maintained, the remaining Toffoli gates can be replaced. For instance, a Toffoli gate applied to a \(\vert C\rangle\) qubit which then acts as a control in a CNOT operation \(\vert C\rangle\) over \(\vert 0\rangle\) is equivalent to an AND operation over an auxiliary qubit (instead of acting on \(\vert C\rangle\)) and applying a CNOT gate between the obtained result and \(\vert C\rangle\). The obtained circuit by replacing all the Toffoli gates (for the \(N=3\) case) is shown in Fig. 6. Its T-count is only 16N.

Fig. 6
figure 6

Second proposal for quantum comparator circuit focused on reducing the T-count, for the \(N=3\) case. \(a_{i}\) and \(b_{i}\) are the bits of the two number A and B to be compared. Outputs marked as x and y are the outputs of the comparison

3.3 Proposed dual-threshold image segmentation algorithm

In this subsection, we will discuss the implementations of dual-threshold image segmentation, both those made by other authors and the one we have proposed in this paper.

Once we have fault-tolerant and fully reversible comparators, the full dual-threshold segmentation algorithm can be implemented. Figure 7 shows the algorithm proposed by Yuan et al. [7], for an example in which the high and low threshold limits are set to 6 and 2, respectively. This circuit does not need extra ancilla qubits, since it reuses those of the comparator. This algorithm is intended to be used with the comparator proposed by Yuan et al., so it is not compatible with the comparators we propose. In addition, it also has reset operations. Such reset operations can be easily removed by simply applying the inverse operations. Moreover, the Toffoli gates that do not act on the \(a_i\) qubits can be replaced by Temporary logical-AND gates, thus reducing the T-count. Since our comparators have no garbage outputs, their auxiliary qubits can be used in this circuit, requiring no extra ancilla qubits. An example of this new version of the algorithm is shown in Fig. 8.

Fig. 7
figure 7

Circuit of the dual-threshold segmentation algorithm proposed by Yuan et al. [7]. In this example, the low threshold is set to 2, and the high threshold to 6. The use of the reset operation makes the circuit non-reversible

Fig. 8
figure 8

Proposed implementation of the dual-threshold segmentation algorithm. In this example, the low threshold is set to 2, ad the high threshold to 6. It is implemented using only Clifford+T gates, and it reduces the T-count by 6 with respect to the circuit shown in Fig. 7. The comparators will need extra auxiliary inputs, as shown in Figs. 5 and 6

4 Analysis and comparison

In this section, both the dual-threshold image segmentation algorithm and the comparators designed for its implementation have been compared with their state-of-the-art counterparts.

A comparative study between the full comparator circuits we have designed (a generalization of Figs. 5 and 6) and the most advanced comparator circuits in the literature is observed in Table 1. This table shows the values of the metrics: T-count (obtained from the number of Toffoli gates and Temporary logical-AND gates), T-depth, number of ancilla qubits and, in addition, it indicates whether the circuit contains only Clifford+T type gates and whether it is a full comparator or not. It is important to indicate whether a comparator is complete or not, because a full comparator provides more information than a half comparator and, therefore, has more applications. The full comparator identifies whether A is less than, greater than, or equal to B, while the half comparator only identifies whether A is less than or equal to B, or not (in which case it is obviously greater). Focusing now on the specific case of the segmentation operation, our proposed algorithm only works with full comparators. As explained in Sect. 3, our algorithm is based on that of Yuan et al. [7]. In [7], specifically in the Subsection 4.1, they defend the reasons why it is better to use a full comparator instead of a half comparator. Without going into details, we can cite the most important of these reasons: to use two thresholds (the upper and the lower), a half comparator would not be enough.

If we observe Table 1, it is important to mention that the results for the circuits of Yuan et al. [7] and Wang et al. [11] have required two rows to indicate their metrics: the first one for the original values and the another one to show the values in the case of replacing the reset operations with Clifford+T gates, making them fault-tolerant.

If we analyze Table 1, we can extract the following information. The best values of T-count, T-depth, and number of ancilla qubits have been obtained by Xia et al. [28] and Li et al. [29] circuits. However, these circuits have the limitation that they are not full comparators.

If we now turn our attention to the full comparators, the comparable versions are those of Yuan et al. [7] and our two proposed circuits. In this case, we observe how our proposed comparator circuit 2 improves in T-count and T-depth values the two versions of Yuan et al. (with and without reset operations). In the case of T-count, the value of our second proposal is 16N versus the values of Yuan et al., \(35N - 21\) and \(70N - 42\). In terms of T-depth, the same conclusion can be drawn. A better T-depth value is obtained by our second proposal in comparison with the original version and the version without resets of Yuan et al.: \(15N - 9\) for the original version and \(30N - 18\) for the modified version, versus \(10N - 6\) in our proposed comparator circuit 2. Regarding the ancilla qubits and full comparators, the best values have been achieved by Yuan et al. without resets and our proposed comparator circuit 1, both with a value of \(3N +1\). However, our proposal presents the advantage of being fault-tolerant.

Table 1 Comparison, in terms of T-count, T-depth and number of ancilla qubits, between the proposed comparators and the best designs found in the literature. Circuits that are built using only Clifford+T gates are also indicated

If we focus on the quantum algorithm for dual-threshold image segmentation, a comparative analysis is carried out in Table 2. The considered approaches have been: the proposed by Yuan et al. [7] (Fig. 7), the proposed by Wang et al. [11] and the implemented in this work (Fig. 8). The table shows the values of T-count, T-depth, and number of ancilla qubits and also indicates whether the circuit is noise tolerant or not. Notice that the T-count value does not include the cost of the comparators of Table 1, which are called twice in each segmentation. As can be observed from Table 2, the proposed dual-threshold segmentation algorithm improves both T-count and T-depth with regard to the implementations of Yuan et al. [7] and Wang et al. [11]. With respect to the number of ancilla qubits, none of the circuits involve new ancilla qubits as they can reuse the ancilla qubits of the comparators they include. Finally, the proposed dual threshold does not require the use of the reset operation unlike the other two alternatives. This can be considered an improvement, as the reset operation is not available in all quantum devices.

Table 2 Comparison, in terms of T-count, T-depth and number of ancilla qubits, between the original dual-threshold image segmentation circuit proposed by Yuan et al. [7], Wang et al. [11] and the proposed in this work, which is fault-tolerant

5 Conclusions and future work

In this paper, we have improved an existing quantum algorithm for dual-threshold image segmentation by incorporating fault-tolerance. This algorithm is feasible to use (on a small scale) in simulators.

The contributions of this work are the following improvements on the algorithm:

  1. 1.

    Two new full-comparators have been designed for their integration into the algorithm. These comparators improve the current ones in three aspects. The first is that they are built exclusively using Clifford+T gates, making them fault-tolerant. Second, they have a lower T-count and T-depth, so they are cheaper in terms of T gates and have less overhead. Third, they are fully reversible and easily customizable.

  2. 2.

    The main dual-threshold image segmentation algorithm has been redefined to reduce the T-count and the T-depth parameters and to make it fully reversible and easier to implement in currently available quantum devices.

Some future lines of work related to this paper can be the following: (1) the use of this type of comparator on other algorithms (for instance, binarization, or sorting); (2) the implementation of fault-tolerant dual-threshold algorithms and comparator circuits for newer encodings, like IFRQI, INEQR, MCRQI, INCQI, or QPIXL; and (3) to explore the execution of the proposed algorithm to solve real dual-threshold problems, such as segmentation. Segmentation problems typically use high-resolution images, which implies the need for a high number of qubits to be encoded. An analysis of the state of the art of quantum computers is necessary to establish a limit to the resolution of the images (or the size of the image fragments) that this algorithm could segment.