Introduction

Modern day researchers have shown a startling interest for implementing quantum algorithms [2,3,4], which give a potential speedup over many of their classical counterparts as the advancement of quantum computer has achieved a phenomenal success in recent years [5]. There is an immense urge for the implementation of NP-complete problems on quantum computers with the thriving quantum wave [6]. According to the seminal work on computational complexity by Karp [7], if a solution to any of the NP-complete problems can be obtained then any other NP-complete problem can be polynomially reducible to that problem. In this paper, we have focused on one of the well-known NP-complete problems i.e., k-coloring problem. Our main focus is to provide an end-to-end framework that automatically implements an NP-complete problem i.e., k-coloring problem in any dimensional quantum system, so that if anyone can map their computational problem to the k-coloring problem in polynomial time, will be able to implement further, without prior knowledge of gate-based quantum circuit implementation.

An automatic circuit synthesis for k-coloring problem with the help of Grover’s algorithm [8] is presented in the context of multi-valued quantum system in this paper. Although physical systems in classical hardware are typically binary, but, common quantum hardware, such as in superconducting [9] and trapped ion computers [10], has an infinite spectrum of discrete energy levels. Quantum hardware may be configured to manipulate the d energy levels by operating on d-ary quantum system. Qudit gates have already been successfully implemented [11,12,13,14,15,16,17] indicating it is possible to consider higher-level systems apart from qubit only systems. Thus, the question of higher states, beyond the standard two being implemented and performed, no longer stands strong [18]. Therefore, we graduate to multi-valued quantum systems or qudits [19], which in the course reduce the circuit complexity and commend the efficiency of quantum algorithms [20] to provide larger state space with simultaneous multiple control operations [21,22,23]. For instance, N qubits can be formulated as \(\frac{N}{{\log _{2} d}}\) qudits, which immediately gives \({\log _2 {d}}\)-factor in run-time [24,25,26] for solving any computational problem using quantum algorithm.

k-coloring problem is an NP-complete problem that assigns colors to every nodes or vertices of a given graph with the available k colors in such a way that every adjacent vertices connected by an edge have distinct colors. Suppose n is the number of nodes of a given graph, k is the number of colors, then to find an appropriate solution using a classical algorithm requires \(O(d^{n \cdot {\log _{d}} k})\) number of steps in d-dimensional system [27, 28]. On the other hand, by using oracle and the diffusion operator of multi-valued Grover’s algorithm [29, 30], finding the exact solution needs \(O{\sqrt{N}}\) number of iterations where N is \(d^{n \cdot {\log _{d}} k}\). Many researchers have already addressed the graph coloring problem for binary and ternary quantum systems. Earlier in [27, 31], graph coloring problem with the help of Grover’s algorithm is talked about with respect to the binary quantum systems. Again, in [32], SAT reduction technique, the state-of-the-art approach, is used for solving 3-coloring problem and it gives an end-to-end framework for the implementation of it in the IBMQ quantum processor [33]. But, SAT reduction technique generates an immense qubit cost, resulting in inefficient circuit cost. Previously in [34] and [28], circuit synthesis for graph coloring problem using Grover’s algorithm has been presented with respect to ternary quantum systems with the help of ternary comparators [35]. Albeit, in these works, the gate cost remains colossal. A space-efficient solution of graph coloring problem [36, 37] using QAOA (Quantum Approximate Optimization Algorithm) is also proposed in the literature, but we stick to Grover’s algorithm in this work.

In [1], we proposed a comparator-based approach for implementing the k-coloring problem in the binary quantum structure using Grover’s search, which has less qubit cost as compared to the state-of-the-art approach [32] i.e., based on Grover’s algorithm. In this paper, we have generalized the comparator for d-ary quantum systems, which helps to overcome the engineering challenge of the implementation of k-coloring problem in d-ary quantum systems. We have proposed an automated end-to-end framework for any dimensional quantum system to implement k-coloring problem using a newly proposed generalized comparator to map the high-level description of the proposed circuit to any hardware-level quantum operations with an abstraction with better quantum cost in terms of quantum gate cost relative to the state-of-the-art works. In addition to this, we have claimed the following, for further establishing the novelty of the propose research work:

  • We propose an automated end-to-end framework for k-coloring problem using quantum search algorithm in any dimensional quantum system for the first time, to the best of our knowledge.

  • The design of the proposed framework is such that, the quantum solution of k-coloring problem can be mapped into any available near-term quantum devices or multi-valued quantum technology, which makes our approach generalized in nature.

  • We show that our newly proposed comparator helps to implement k-coloring problem with reduced quantum cost with respect to quantum gates as compared to the state-of-the-art approaches in ternary quantum domain.

  • A generalized comparator for d-ary system is elaborated in this paper, which is a first of its kind approach.

The paper is structured as follows. In “Background”, the brief description of quantum circuits and graph coloring using Grover’s algorithm are described. The proposed methodology is vividly explained in “General Flow of Proposed Automated Framework for Mapping k-coloring Problem to Quantum Computers ”. In “Proposed Methodology of Circuit Design for k-coloring Problem in d-dimensional Quantum Systems ”, implementation of k-coloring problem has been shown. Experimental results are captured in “Experimental Results” followed by concluding remarks.

Background

In this section, a few preliminaries of the quantum circuit and Grover’s algorithm are presented. We have also explicitly described the existing comparator-based k-coloring problem using Grover’s algorithm in binary quantum systems.

Quantum Circuit

Any quantum algorithm can be expressed or visualized in the form of a quantum circuit. Commonly for binary quantum systems, logical qubits and quantum gates comprise these quantum circuits [38]. The number of gates present in a circuit is called gate count and the number of qubits present in a circuit is known as qubit cost. In this work, we mainly deal with qudits and generalized quantum gates.

Qudits

Logical qudit that encodes input/output of a quantum algorithm in multi-valued quantum systems is often referred to as data qudit. Ancilla qudit are another type of qudit used to store temporary results. In d-dimensional quantum systems qudit is the unit of quantum information. Qudit states can be manifested as a vector in the d dimensional Hilbert space \({{\mathscr {H}}_d}\). The span of orthonormal basis vectors \(\{{|0\rangle ,|1\rangle ,|2\rangle ,\dots |d-1\rangle \}}\) is the vector space. In qudit system, the general form of quantum state can be expressed as

$$\begin{aligned} |\psi \rangle ={\alpha _0} |0\rangle +{\alpha _1} |1\rangle +{\alpha _2} |2\rangle +\cdots +{\alpha _{d-1}} |d-1\rangle = \begin{pmatrix} {\alpha _0} \\ {\alpha _1} \\ {\alpha _2} \\ \vdots \\ {\alpha _{d-1}} \\ \end{pmatrix} \end{aligned}$$
(1)

where \({|\alpha _0|^2}+{|\alpha _1|^2}+{|\alpha _2|^2}+\cdots +{|\alpha _{d-1}|^2}=1\) and \({\alpha _0}, {\alpha _1}, \dots, {\alpha _{d-1}} \in {{\mathbb {C}}}^d.\)

Generalized Quantum Gates

In this subsection, an outline of generalized qudit gates [39, 40] is conferred. The generalisation can be delineated as discrete quantum states of any arity. In a quantum algorithm, for modification of the quamtum state, unitary qudit gates are applied. For logic synthesis of Grover’s algorithm in d-dimensional quantum systems, it is necessary to take into account one-qudit generalized gates namely NOT gate (\(X_d\)), phase-shift gate (\(Z_d\)), Hadamard gate (\(F_d\)), two-qudit generalized CNOT gate (\(C_{X,d}\)) and Generalized multi-controlled Toffoli gate (\(C^{n}_{X,d}\)). These gates are expressed in detail for a better understanding in the Appendix 1.

Generalized Grover’s Algorithm in d-dimensional quantum systems

The generalized Grover’s algorithm in d-dimensional quantum systems is presented here. There exists two sub-parts of the algorithm namely oracle and diffusion [30]. As per convention, Grover’s algorithm for searching in an unstructured database can be defined as follows: given a collection of unstructured database elements \(x=1, 2 ,\ldots , N\), and an oracle function f(x) that acts on a marked element s as follows [29],

$$\begin{aligned} f(x) =\left\{ \begin{array}{ll} 1,&{} x=s, \\ 0,&{} x\ne s, \end{array}\right. \end{aligned}$$
(2)

perceive the marked element with as few calls to f(x) as possible [29, 30]. The database is encoded into a superposition of quantum states where each element is assigned to a corresponding basis state. Grover’s algorithm searches over every possible outcome, which is put forward as a basis vector \(|x\rangle\) in an n-dimensional Hilbert space in d-dimensional quantum systems. Likewise, the marked element is encoded as \(|s\rangle\). Thus, the search can be done in parallel, after application of unitary operations as an oracle function to the superposition of the different possible outcomes. The generalized diffusion operator, also known as inversion about the average operator, amplifies the amplitude of the marked state to increase its measurement probability using constructive interference, with simultaneous enfeeblement of all other amplitudes, and searches the marked element in \(O({\sqrt{N}})\) steps, where \(N=d^n\) [29].

The circuit diagram for the generalized Grover’s algorithm in a d-dimensional quantum systems is presented in Fig. 1, where at least \(n + 1\) qudits are required. More elaborately, the steps of the Grover’s algorithm are as follows:

Fig. 1
figure 1

Generalized circuit for Grover’s algorithm in d-dimensional quantum systems

Initialization: The algorithm starts with the uniform superposition of all the basis states on the n input qudits in \(|0\rangle\) by assimilating generalized Hadamard or quantum DFT gate. The last ancilla qudit is used as an output qudit, which is initialized to \(F_d |d-1\rangle\). Thus, we obtain the d-dimensional quantum state \(|a\rangle\):

$$\begin{aligned} \vert a\rangle =F_d^{\otimes {n}}\vert 0_d\rangle =\,{1\over {\sqrt{d^{n}}}}\sum _{x=1}^{d^{n}}\vert x\rangle \end{aligned}$$

Oracle query: The oracle (\(U_f\)) of Grover’s search marks the marked state \(|s\rangle\) while keeping all the other states unchanged, and can be expressed as:

$$\begin{aligned} \vert x\rangle \xrightarrow {U_f}(-1)^{f(x)}\vert x\rangle \end{aligned}$$

The oracle block \(U_f\) as shown in Fig. 1 is dependant on the problem instance. The oracle using Unitary transformation is needed to be designed as per requirement.

Diffusion: The diffusion operator of Grover’s search is generic, it doesn’t depend on specific problem . As shown in Fig. 1, the diffusion operator is initially assigned with generalized Hadamard \((F_d^{\otimes {n}})\), then by \(2|0^n\rangle \langle 0^n|-I_{d^n}\) and generalized Hadamard \((F_d^{\otimes {n}})\) again. The diffusion operator (D) can be expressed as:

$$\begin{aligned} D=F_d^{\otimes {n}} [2|0^n\rangle \langle 0^n|-I_{d^n}] F_d^{\otimes {n}} \end{aligned}$$

The matrix representation of generalized diffusion operator [29] for d-dimensional quantum systems is shown below:

$$\begin{aligned} D = \left( \begin{array}{ccccc} \frac{2}{d^n}-1 &{} \frac{2}{d^n} &{} \frac{2}{d^n} &{} \ldots &{} \frac{2}{d^n} \\ \frac{2}{d^n} &{} \frac{2}{d^n}-1 &{} \frac{2}{d^n} &{} \ldots &{} \frac{2}{d^n} \\ \frac{2}{d^n} &{} \frac{2}{d^n} &{} \frac{2}{d^n}-1 &{} \ldots &{} \frac{2}{d^n} \\ \vdots &{} \vdots &{} \vdots &{} \ddots &{} \vdots \\ \frac{2}{d^n} &{} \frac{2}{d^n} &{} \frac{2}{d^n} &{} \ldots &{} \frac{2}{d^n}-1 \\ \end{array} \right) \end{aligned}$$

Figure 2 shows the generalized circuit for Grover’s diffusion operator in d-dimensional quantum systems. It can be constructed using generalized Hadamard gate, generalized NOT gate and generalized multi-controlled Toffoli gate.

Fig. 2
figure 2

Generalized circuit for Grover’s diffusion operator in d-dimensional quantum systems [29]

We have already discussed that the diffusion operator is predefined for all problem instances, the oracle is specific to the given search problem. The combination of the oracle and the diffusion gives generalized Grover operator G,

$$\begin{aligned} G=D U_f \end{aligned}$$

We need to din the Grover’s operator \(O({\sqrt{N}})\) times to get the coefficient of the marked state \(|s\rangle\) large enough so that it can be obtained from measurement with probability close to 1. As we have considered every gate to be ideal in our proposed circuit design, and therefore, the success rate of the Grover’s algorithm remains same as for any other circuit design of a specific problem. In this paper, we start the algorithm with the equal superposition of all the basis states, and hence the success probability is \(\sim 1\) after \({\sqrt{N}}\) steps and thus round off the Grover’s algorithm.

Circuit Design and Implementation of k-coloring Problem using Grover’s Algorithm in Qubit System

Graph coloring problem, as discussed in the introduction, Given a graph, we need to color it with the constraint that no two adjacent nodes will have the same color. In [32], a SAT reduction-based technique was introduced for solving 3-coloring problem as an example of k-coloring problem in binary quantum systems. A SAT problem is defined as a function of OR and AND logical operations on input variables so as to reach to a solution. In [32], if n is the number of vertices in a given graph and k is the given number of color to solve the k-coloring problem, then it is shown that k-coloring problem can be solved as a Boolean satisfiability problem using Grover’s algorithm. For that, all possible input variables of \(n \cdot k\) are needed to be encoded as input data to implement Grover’s algorithm. Hence \(n \cdot k\) data qubits are required to implement k-coloring problem by SAT reduction-based technique. In [1], we proposed a comparator-based approach for implementing the k-coloring problem in binary quantum structure using Grover’s search, which has less qubit cost i.e., \(n \cdot \lceil {\log _{2}} k \rceil\) as compared to [32]. All possible combination of k colors to color the nodes of the given graph can be implemented with \(n \cdot \lceil {\log _{2}} k \rceil\) input qudits. Instead of using OR and AND logical operation, our proposed comaparator helped us in achieving logarithmic reduction with respect to the input qubit cost. As we know in Grover’s algorithm, diffusion operator uses an MCT gate of the size of the number data qubits, thus as since we reduce the number of data qubits by our propose approach, hence the circuit depth will also be reduced [41].

Now, we have discussed about the proposed comparator in [1]. In Fig. 3, circuit synthesis for 2-qubit and 4-qubit comparator is shown, the functional description of which is given in Equation 3. The complete circuit synthesis for the binary comparator is designed using CNOT, NOT, Toffoli/MCT gates.

$$\begin{aligned} {\text {Binary}}\_{\rm {Comparator}}(a, b, f)= \left\{ \begin{array}{ll}{f=0,} &{} \text{ if }~ a = b;\\ {f=1,} &{} {a \ne b}.\\ \end{array}\right. \end{aligned}$$
(3)
Fig. 3
figure 3

Example binary comparator: a 2-qubit; b 4-qubit

Our proposed comparator-based oracle gives better result with respect to the data qubit and ancilla qubit as \(n \cdot \lceil {\log _{2}} k \rceil\) and O(n) respectively, as compared to [32]. The comparative analysis is shown in Table 1.

Table 1 Comparative analysis of binary oracle

In [1], we further showed that the mapping of generated comparator-based oracular circuit using Grover’s search to NISQ devices through MCT realization and qubit mapping algorithm. For mapping the logical synthesized circuit to quantum hardware or NISQ devices [5], the logical quantum gates are needed to be realized to hardware-specific one and two-qubit gates for making it hardware compatible for implementation. The process of decomposition of MCT gate to NISQ compatible one-qubit and two-qubit gates is presented in [42]. At first, the MCT gate is to be decomposed to MCZ gate. After that, MCZ gate is realized to MC\(R_{x}(\pi )\). Lastly, without using any ancilla qubit, MC\(R_{x}(\pi )\) is reduced to one-qubit and two-qubit gates. As the quantum circuit is logical, there is no constraint of qubit connectivity. There is a specific qubit topology or coupling graph for NISQ devices. The interaction between two physical qubits is defined by coupling graph. This is different for different NISQ devices. Hence, obviously mapping the logical circuit to the physical one is challenging. The solution to this problem is to insert SWAP gates between the two qubits to comply to the hardware constraint without compromising on the logic of the quantum circuit. The concept behind a good qubit mapping problem is to minimize the number of SWAP insertion gates as well as the depth of the circuit. Li et. al. proposed SWAP-based BidiREctional heuristic search algorithm (SABRE) in [43], which deals with any arbitrary qubit topology for any NISQ devices. Three main features make SABRE noticeable. Firstly, an exhaustive search is not performed on the entire circuit, but a SWAP-based heuristic search is performed keeping in mind the qubit dependency. Then, initial mapping is optimized with the use of a novel reverse traversal technique. Lastly, the decay effect being introduced to enable the trade-off between the depth and the number of gates of the entire algorithm. Tan et. al. formulated layout synthesis for quantum computing, which is a benchmark, as optimization problems [44]. They handed over two synthesizers: an exact layout synthesizer (OLSQ) and an approximate, transition-based synthesizer (TB-OLSQ). OLSQ is the first, that guarantees optimality and efficiency both in time and space for general quantum processors, as compared to previous exact approaches. This approach shows some promises of being beneficial for realistic applications for near-term quantum computers. The proposed circuit can easily be mapped to any arbitrary qubit topology by using one of these protocols. Apart from these protocols, we performed all our experiments on IBM quantum computer [33], specifically ibmq_16_melbourne (quantum volume is 8), which has a transpiler that automatically maps the decomposed logical circuit into physical IBM hardware. The above-discussed methodology of mapping k-coloring problem to binary quantum computer is further extended to any dimensional quantum system in this paper.

General Flow of Proposed Automated Framework for Mapping k-Coloring Problem to Quantum Computers

The flowchart in Fig. 4 depicts the complete flow of our proposed automated end-to-end framework. Mainly three algorithms: AutoGenOracle_K-color, MCT_Realization and Qubit Mapping (binary) are the basis of our framework. At first, AutoGenOracle_K-color algorithm, which is based on the proposed comparator, is implemented with inputs: graph information i.e, adjacency matrix of the given graph and the number of colors (k) to get the output, quantum circuit netlist in the form QASM. Now, MCT_Realization algorithm takes as input, generated circuit netlist and MCT gates to NISQ hardware compatible 1-qubit and 2-qubit gates [42] and multi-valued quantum technology compatible 1-qudit and 2-qudit gates are realised. These MCT_Realization and qubit mapping algorithm are well-defined in the literature. We have just adopted them as a support in our framework.

Due to multi-valued quantum technology constraint, implementation of multi-valued circuit netlist on multi-valued quantum hardware is technologically infeasible till date, rather we have simulated and verified the multi-valued circuit netlist on multi-valued simulator. As the proposed framework works for any dimensional quantum system, hence it must work for binary quantum systems as well. We have already discussed about that in the previous subsection, therefore, we stick to multi-valued quantum systems from here on.

Fig. 4
figure 4

General flow of proposed automated framework for mapping k-coloring problem to any dimensional quantum system

Proposed Methodology of Circuit Design for k-Coloring Problem in d-dimensional Quantum Systems

This section outlines the proposed methodology for the circuit synthesis of the k-coloring problem using Grover’s algorithm. k-coloring problem takes an adjacency matrix of a graph and k number of colors as input and determines that the graph is properly colored or not. As mentioned in the previous section, there are two parts of the Grover’s search algorithm, where only the oracle is specific to the given search problem. Hence, this paper proposes the design of an oracle circuit that takes a graph and k number of colors as input. The oracle then marks those states that determine the properly colored nodes of the given graph before the diffusion operator is applied to them. Let’s start with the proposed oracle for k-coloring problem.

Proposed Oracle for k-Coloring Problem in d-Dimensional Quantum Systems

The proposed oracle of the k-coloring problem in any dimensional quantum system as an application of the Grover’s search algorithm is sketched in this subsection. Figure 5 shows the quantum circuit block of oracle for the k-coloring problem in d-dimensional quantum systems. The construction of oracle for k-coloring problem is divided into four parts. It starts with initialization, which is essentially required in Grover’s Algorithm.

Fig. 5
figure 5

Block diagram of generalized oracular circuit for k-coloring problem in any dimensional quantum system

Initialization

Let there be a graph which has n vertices and e edges, k is the given number of colors, then the total number of data qudits that are required for representing all the colored vertices are \(n \cdot {\lceil }{\log _d} k{\rceil }\). The oracle performs a check to find all possible right combination of properly colored vertices with k/fewer colors from a combination of all possible colored vertices. A superposition of \(m=n\cdot {\lceil }{\log _d} k{\rceil }\) qudits thus generates all possible combination of colored vertices. The initial data qudits in Fig. 5 include m qudits prepared in the ground state \(\vert \psi \rangle =|0\rangle ^{\otimes m}\), due to the re-usability property of ancilla qudits, \(r= n\) ancilla qudits in the exited state \(|\theta \rangle =|d-1\rangle ^{\otimes r}\) (These r ancilla qudits are required to prepare Invalid_Color detector block and comparator block that are thoroughly explained in the next subsection), one ancilla qudit in the ground state \(|\zeta \rangle =|0\rangle\) (1 ancilla is required if invalid color exists), and one output qudit in the excited state \(|\phi \rangle =|d-1\rangle\) is required to perform generalized CNOT/Toffoli/MCT operation of the oracle. This entire initialization can be mathematically depicted as:

$$\begin{aligned} \vert \psi \rangle \otimes \vert \theta \rangle \otimes \vert \zeta \rangle \otimes \vert \phi \rangle =\vert 0\rangle ^{\otimes m}\otimes \vert d-1\rangle ^{\otimes r}\otimes \vert 0\rangle \otimes \vert d-1\rangle \end{aligned}$$

Generalized Hadamard Transformation

The next step after the initialization is Hadamard transformation. The genralized Hadamard transform \({F_{d}}^{\otimes m}\) on data qudits and \(F_d\) on output qudit is performed, hence all possible states are superposed as \(|\psi _{0}\rangle \otimes |\theta _{0}\rangle \otimes |\zeta _{0}\rangle \otimes |\phi _{0}\rangle\), where

$$\begin{aligned} \vert \psi _{0}\rangle= & {} \,{1\over {\sqrt{d^{m}}}}\sum _{i=0}^{d^{m}-1}\vert i\rangle \\ |\theta _{0}\rangle= & {} |(d-1)(d-1)(d-1)(d-1) ..... r(times)\rangle \\ |\zeta _{0}\rangle= & {} |0\rangle \\ \vert \phi _{0}\rangle= & {} \,{1\over {\sqrt{d}}}\left( \vert 0\rangle +\omega ^{d-1}\vert 1\rangle +\omega ^{2(d-1)}\vert 2\rangle + ..... \right. \\&\left. +\omega ^{(d-1)(d-1)}\vert d-1\rangle \right) \end{aligned}$$

Proposed \(U_f\) Transformation for d-dimensional quantum systems:

The proposed unitary \(U_f\) transformation is distinctly divided into two parts.

(1) Reduction of Invalid Colors: As color, \(c={\lceil }{\log _d} k{\rceil }\), so maximum \(d^c\) colors are considered. If \(d^c=k\), then all colors are valid colors, else there exists a set of \(d^c -k\) invalid colors. The valid colors are used to optimize the search space. Using the following steps this can be executed:

Qudit Activation: Colors should be numbered sequentially as \(\{0,1,2 .... d^{c}-1\}\). After the generalized Hadamard transformation, the input data qudit lines act as the d-dimensional representation of combination of all possible colored vertices. The oracle performs a check for only k, which is the combination of valid colors. All the input qudit lines should be in the excited state \(|d-1\rangle\) for those particular combinations of invalid colors by making input qudit lines suitable as control lines for Generalized CNOT/Toffoli/MCT operation to be assured that the oracle is checking only the k-colored combination of vertices. A number of generalized NOT gates must be imposed on the input qudit lines, that are in the ground state \(|0\rangle\) accompanied by the application of ’Invalid Color Detector’. Again after ’Invalid Color Detector’, this ’Qudit Activation’ is to be applied to roll back to the initial superposed quantum state.

Invalid Color Detector: In any combination of colored vertices if any invalid color is noticed then that combination is rejected with the use of the function ICD (Invalid Color Detector) as:

$$\begin{aligned}&{\text {Generalized}}\_{\text {ICD}}(I_1, I_2, \dots , I_n, f) \nonumber \\&\quad = \left\{ \begin{array}{ll} {f=0,} &{} \text{ if }~I_1 \, \text{or}\, I_2\, \hbox {or} \dots I_n = \text{ Invalid } \text{ color};\\ {f=d-1,} &{} \text{ No } \text{ invalid } \text{ color }.\\ \end{array}\right. \end{aligned}$$
(4)

As an instance, the circuit synthesis of ’Invalid Color Detector’ for ternary quantum systems is described in Fig. 6. This is functionally shown in Equation 5 for n vertices, where \(I_1, I_2,\ldots, I_n\) are the data qutrits.

$$\begin{aligned}&{\rm {Ternary}}\_{\text {ICD}}(I_1, I_2, \dots , I_n, f) \nonumber \\&\quad = \left\{ \begin{array}{ll}{f=0,} &{} \text{ if }~I_1~\text{ or }~ I_2\text{ or }~\dots I_n = \text{ Invalid } \rm{ color };\\ f = 2, &{} \text{ No } \rm{ invalid } \text{ color }.\\ \end{array}\right. \end{aligned}$$
(5)
Fig. 6
figure 6

Ternary invalid color detector

(2) Comparator: A newly proposed generalized comparator circuit for d-dimensional quantum systems can be defined as:

$$\begin{aligned}&{\text {Generalized}}\_{\rm {Comparator}}(a, b, f) \nonumber \\&\quad = \left\{ \begin{array}{ll}{f=0,} &{} \text{ if }~ a = b;\\ {f=d-1,} &{} {a \ne b}.\\ \end{array}\right. \end{aligned}$$
(6)

where a and b are the comparing inputs representing the colored vertices of the given graph and the ancilla qudit is f .

Induced from the above, a newly proposed ternary comparator can be defined as:

$$\begin{aligned}&{\text {Ternary}}\_{\rm {Comparator}}(a, b, f) \nonumber & = \left\{ \begin{array}{ll}{f=0,} &{} \text{ if }~ a = b;\\ {f=2,} &{} {a \ne b}.\\ \end{array}\right. \end{aligned}$$
(7)

Circuit synthesis for 2-qutrit ternary comparator is shown in Fig. 7. 1-qutrit and 2-qutrit permutative gate are used to design the complete circuit synthesis for a ternary comparator. Initially, the total number of gate count of our newly proposed comparator is reduced by one as compared to the comparator proposed in [35]. But, gate count can be reduced further at the time of oracular circuit synthesis as one of the outputs of our comparator is \('2'\) instead of \('1'\) [35].

Fig. 7
figure 7

Ternary comaparator

As everything has to be mirrored at the time of circuit synthesis in order to eliminate the cost of wires, there is a need to design an inverse comparator. Our newly proposed 2-qutrit ternary inverse comparator is shown in Fig. 8. This newly proposed ternary inverse comparator has a reduced gate count compared to the work in [35].

Fig. 8
figure 8

Ternary inverse comaparator

Generalized MCT Operation

The \(\vert \phi _{0}\rangle\), output qudit state, is initialized as:

$$\begin{aligned} {1\over {\sqrt{d}}}\left( \vert 0\rangle +\omega ^{d-1}\vert 1\rangle +\omega ^{2(d-1)}\vert 2\rangle + ..... +\omega ^{(d-1)(d-1)}\vert d-1\rangle \right) \end{aligned}$$

Applying a Generalized MCT gate on the output line with ancilla qudits as control, an eigenvalue kickback \(\omega ^{(d-1)(d-1)}\) occurs as a result. This causes a phase shift for the respective input state(s), which in turn helps in finding out all the combinations of a properly colored set of vertices. The algorithm that causes the gate level synthesis of the proposed method is discussed in the next subsection.

Proposed Generalized Algorithm for Oracle Circuit Synthesis of k-Coloring Problem in Any Dimensional Quantum System

The proposed algorithm Algorithm 1 (AutoGenOracle_K-Coloring) of automated oracular circuit synthesis for the k-coloring problem in any dimensional quantum system is illustrated in this subsection. The algorithm accepts the adjacency matrix of the given graph and the number of colors k as input parameters. A circuit netlist in the form of QASM is the output of the algorithm.

figure a

The total number of qudit lines that are required for the generation of the oracle circuit can be easily evaluated from the details of the adjacency matrix and the number of given colors. All the input data qudits are initialized with \(|0\rangle\) followed by generalized Hadamard, ancilla lines (\(A_{r}\)) are initialized with \(|d-1\rangle\), ancilla line \(A_{r+1}\) is initialized with \(|0\rangle\) and the output line is initialized with \(|d-1\rangle\) followed by generalized Hadamard. First of all, ’Invalid Color Detector’ is applied with suitable Qudit Activation (if invalid color exists) with \(I_r, A_r\) as control and \(A_{r+1}\) as the target. After that, between two adjacent vertices(ij), a generalized comparator circuit is used with two input lines(ij) as control and the ancilla line(\(A_{r}\)) as output. They perform the same task for all the adjacent vertices. Following this, a generalized MCT gate is used with all the ancilla lines \(A_{r}\) and \(A_{r+1}\) as control and the output line as output for the flip operation of the Grover’s oracle. To achieve the mirror of the oracle circuit, we have repeated the previous steps as shown in Algorithm 1.

Generation of Proposed Oracle Circuit for k-Coloring Problem for an Exemplified Graph

As an example, the circuit synthesis of the 3-coloring problem for a graph of three vertices with three connected edges (\(K_{3}\)) in ternary quantum systems is illustrated in Fig. 9. For a 3-vertex graph, adjacency matrix is represented by adj(3, 3) and vertices are indexed as 0, 1, 2. As we are solving 3-coloring problem, the value of k is 3. Hence, the total \(3 \cdot {\lceil }{\log _3} 3{\rceil }=3\) qudit lines are required for inputs. As per Algorithm 1, input qudit lines are represented as \(I_{r}\) , where \(1\le r \le 3\). These input qudit lines are initialized with \(|0\rangle\) followed by Chrestenson gate (Generalized Hadamard gate in ternary quantum systems) as we have considered ternary quantum systems. The above three input qudit lines represent three vertices of the given graph. In order to store comparator information, three ancilla lines \(A_{r}\) are initialized with \(|2\rangle\), where \(1\le r \le 3\). One ancilla line \(A_{r+1}\) is not required as there is no invalid color in this case. One output line O initialized with \(|2\rangle\) followed by Chrestenson gate is required to store the output of the circuit. Hence, a total of 7 qudit lines are required for the simulation.

Fig. 9
figure 9

Oracle circuit for k-coloring problem of a graph with three vertices in ternary quantum systems

  • At first the algorithm considers to apply ’Invalid Color Detector’, if it is required. As the number of color is 3 in this example, So the color c becomes \(c={\lceil }{\log _d} k{\rceil }={\lceil }{\log _3} 3{\rceil }=1\) and \(d^c=3^1=3\). Therefore, we can conclude that \(d^c=k\), hence, all the given colors are valid colors. So, there is no need of ’Invalid Color Detector’. Let’s assume the number of color be given 2, then we had to apply ’Invalid Color Detector’ as \(d^c \ne k\).

  • The algorithm now initializes variable l as 3 and variable f as 1.

  • Thereafter, for every possible vertex pair (0, 1), (0, 2), and (1, 2), the algorithm checks that they are connected by an edge or not by accessing the information of adjacency matrix.

  • At first for vertex pair (0, 1), \(adj(0,1)=1\), insert a ternary comparator circuit with \(I_{1}\), \(I_{2}\) and \(A_{1}\) qudit lines.

  • In case of vertex pair (0, 2), \(adj(0,2)=1\), insert an ternary comparator circuit with \(I_{1}\), \(I_{3}\) and \(A_{2}\) qudit lines.

  • Again to make two \(A_{1}\) and \(A_{2}\) ancilla lines reusable, insert a ternary Toffoli gate with \(A_{1}\) and \(A_{2}\) as control and \(A_{3}\) as target by accessing the value of the variables r and f followed by inserting two inverse comparator circuit for vertex pairs (0, 1) and (0, 2) by accessing the value of the variables l and m.

  • Similarly, for vertex pair (1, 2), the entry for the adjacency matrix adj(1, 2) is 1, so insert a ternary comparator circuit with \(I_{2}\), \(I_{3}\) and \(A_{2}\) qudit lines after re-initializing the value of the variables r and m to f.

  • Thereafter, Insert a ternary three-controlled Toffoli gate with each of the three \(A_{1}\), \(A_{2}\), and \(A_{3}\) ancilla lines as control and O ancilla as target.

  • The remaining steps of the algorithm generate the mirror for the original circuit in order to keep the overall circuit netlist reversible.

After optimizing the gate-cost, the circuit synthesis of the 3-coloring problem for above-mentioned example graph is presented in Fig. 10.

Fig. 10
figure 10

Gate-optimized oracle circuit for k-coloring problem of a graph with three vertices in ternary quantum systems

Circuit Cost Estimation

The design of generalized oracle for our proposed algorithm has already been described. Now, in this subsection, we furnish the circuit cost analysis of the oracular circuit in Table 2.

Table 2 Circuit cost analysis of oracle

\(n\cdot \lceil {log_{d}} k \rceil\) data qudits are required for n-vertices graph and k given color. At most \(n+1\) number of ancilla and at most \(O(n^2 \cdot {\log_{d}} k)\) gates are required to design the oracular circuit for n-vertices graph.

Comparative Analysis

The new ternary oracle circuit for 3-vertex graph has achieved 41% reduction in the quantum gate cost as compared to the most recent related work [28] and 85% reduction in the quantum gate cost as compared to the work [34]. Table 3 shows that the comparative analysis for higher vertices graphs as well. We show that for 4-vertex and 5-vertex graph, it can reach to 43% reduction of gate cost.

Table 3 Comparative analysis of ternary oracle

Experimental Results

In this section, we have vividly discuss about the implementation results of k-coloring problem in multi-valued quantum systems.

Simulation Results of k-Coloring Problem in Ternary Quantum Domain

Our ternary circuit instance is verified through simulation with the help of MATLAB simulator [45]. Unfortunately, MATLAB has some limitations of memory constraints. For that, the verification of our circuit needs to be restricted with at most seven qutrits. Therefore, a complete graph of three vertices i.e., a triangle is taken as an example case for simulating the k-coloring problem. The number of color k is given as 3. As per Algorithm 1, three qutrits (\(n\cdot \lceil {\log _{3}} k \rceil\)) are required to represent the vertices of the given graph. As the example graph have three vertices, three ancilla qutrits are required to perform the comparator circuit synthesis and one final qutrit as output. Three colors are given, which are encoded as \(|0\rangle , |1\rangle\) and \(|2\rangle\). Generally, a graph with three vertices can be colored with the help of three colors in \(3^3=27\) different ways, that can be represented in the ternary quantum system as \(|000\rangle \dots |222\rangle\). Thus, our database contains 27 elements. In Fig. 11, the gate level representation of the Grover’s circuit is shown.

Fig. 11
figure 11

Complete circuit for k-coloring problem of a graph with three vertices in ternary quantum systems

The simulation steps of Fig. 11 are:

Step 1: At first, all the qutrits of the 3-qutrit data register are initialized with \(|0\rangle\).

Step 2: Then, the CH gate is applied to create the all possible 27 states \((|000\rangle\) \(\dots\) \(|222\rangle )\), which is portrayed in Fig. 12a.

Step 3: The ternary oracle compares the colors of every adjacent vertices and inverts the amplitude of the solution elements, which are \(|012\rangle , |021\rangle , |102\rangle , |120\rangle , |201\rangle ,\) \(|210\rangle\), illustrated in Fig. 12b.

Step 4: The output of ternary oracle is acted upon by the ternary diffusion operator. This diffusion operator amplifies the amplitudes of the marked states of step 3.

Step 5: Steps 3 and 4 are repeated for \({\sqrt{N/M}}\) times (For the multiple solution of Grover’s operator having \(N=\) number of elements in the database and \(M=\) number of marked states), where the value of N is \(d^{n*log_{d} k}\) for this k-coloring problem.

The probability distribution of quantum states after two iterations of Grover’s operator is shown in Fig. 12c, where the amplitude amplification is performed using diffusion operator to amplify the amplitudes of marked states. It can be verified that the location of the searched states (output of step 5) by converting its index values to its equivalent ternary value, having higher probability from all the index values of Fig. 12c. For example, if index value is taken as five form Fig. 12c, after converting it to ternary, we get the string \(|012\rangle\), which is one of the searched states. Analysis of the simulation result confirms that our oracle successfully verifies that any set of vertices of a graph, is properly colored by the given set of colors.

Fig. 12
figure 12

Simulation results: (a) the amplitude of the quantum states after applying CH gate to input qutrits (b) The amplitude of the quantum states after applying oracle (c) The probability distribution of the quantum states after two iterations of Grover’s operator

Mapping of k-Coloring Problem to Multi-valued Quantum Systems

Multi-valued quantum hardware is not presently publicly available for common users. But, in this section, we discuss about a few works which guide us to map a multi-valued quantum algorithm to quantum hardware in future. As in binary quantum systems, for this mapping, generalized multi-controlled Toffoli gates are needed to be decomposed into one and two-qudit gates followed by a qudit mapping algorithm. In [41], Saha et. al. have shown the decomposition of multi-controlled Toffoli gate in d-dimensional quantum systems. A generalized Toffoli decomposition in d-dimensional system using \(|d\rangle\) state is shown in Fig. 13. An akin construction for the Toffoli gate in binary using qutrit is evident from a previous work [46]; Saha et. al. have extended it for d-dimensional quantum systems. The idea is to execute an \(X_d\) operation on the target qudit (third qudit) if and only if the two control qudits, are both \(|d-1\rangle\). Firstly, a \(|d-1\rangle\)-controlled \(X^{+1}_{d+1}\), where \(+1\) and \(d+1\) are used to denote that the target qudit is incremented by \(1 \ (\text {mod } d+1)\), is implemented on the first and the second qudits. This eventually upgrades the second qudit to \(|d\rangle\) as long as the first and the second qudits were both \(|d-1\rangle\). Then, a \(|d\rangle\)-controlled \(X_d\) gate is applied to the target qudit. Therefore, \(X_d\) is executed only when both the first and the second qudits were \(|d-1\rangle\), as expected. The controls are rolled back to their original states by a \(|d-1\rangle\)-controlled \(X^{-1}_{d+1}\) gate, which reverses the effect of the first gate. The most important aspect in this decomposition is that the \(|d\rangle\) state from \(d+1\)-dimensional quantum systems can be used instead of ancilla to store temporary information.

Fig. 13
figure 13

Generalized Toffoli in d-dimensional quantum systems [41]

For further decomposition of the Toffoli for simulation purpose, the \(d+1\)-dimensional Toffoli gate has been decomposed into \(d+2\)-dimensional CNOT gates. A generalized CNOT gate for \(d+2\)-dimensional quantum system as \(C^{+1}_{X,d+2}\), where \(+1\) and \(d+2\) denote that the target qudit is incremented by \(1 \ (\text {mod } d+2)\) only when the control qudit value is \(d+1\). The \(((d+2)^2 \times (d+2)^2)\) matrix representation of the \(C^{+1}_{X,d+2}\) gate is as follows:

$$\begin{aligned} C^{+1}_{X,d+2} = \left( \begin{matrix} I_{d+2} &{} 0_{d+2} &{} 0_{d+2} &{} \ldots &{} 0_{d+2} \\ 0_{d+2} &{} I_{d+2} &{} 0_{d+2} &{} \ldots &{} 0_{d+2} \\ 0_{d+2} &{} 0_{d+2} &{} I_{d+2} &{} \ldots &{} 0_{d+2} \\ \vdots &{} \vdots &{} \vdots &{} \ddots &{} \vdots \\ 0_{d+2} &{} 0_{d+2} &{} 0_{d+2} &{} \ldots &{} X^{+1}_{d+2} \\ \end{matrix} \right) \end{aligned}$$

where \(X^{+1}_{d+2}\) and \(0_{d+2}\) are both \((d+2) \times (d+2)\) matrices as shown below:

$$\begin{aligned} X^{+1}_{d+2} = \left( \begin{matrix} 0 &{} 0 &{} \ldots &{} 0 &{} 1 \\ 1 &{} 0 &{} \ldots &{} 0 &{} 0 \\ 0 &{} 1 &{} \ldots &{} 0 &{} 0 \\ \vdots &{} \vdots &{} \ddots &{} \vdots &{} \vdots \\ 0 &{} 0 &{} \ldots &{} 1 &{} 0 \end{matrix}\right) \quad \text {and,}\quad 0_{d+2} = \left( \begin{matrix} 0 &{} 0 &{} \ldots &{} 0 &{} 0 \\ 0 &{} 0 &{} \ldots &{} 0 &{} 0 \\ 0 &{} 0 &{} \ldots &{} 0 &{} 0 \\ \vdots &{} \vdots &{} \ddots &{} \vdots &{} \vdots \\ 0 &{} 0 &{} \ldots &{} 0 &{} 0 \end{matrix}\right) \end{aligned}$$
Fig. 14
figure 14

Decomposition of 8-qudit Toffoli gate : (a) 8-qudit Toffoli gate (b) decomposed into generalized Toffoli gates (c) decomposed into one and two-qudit Generalized CNOT gates [41]

As an example, a 8-qudit Toffoli gate is shown in Fig. 14a. First, we need to decompose it as in [46] as shown in Fig. 14b. Further, we need to decompose all the \(d+1\)-dimensional Toffoli gates into \((d+2)\)-dimensional CNOT gates as shown in Fig. 14c with the help of the Saha et. al. decomposition of the generalized Toffoli in any dimensional quantum system [41]. All the \(d-1\)-controlled Toffoli gates are decomposed into \(d-1\)-controlled and d-controlled CNOT gates as shown in Fig. 14c. Likewise, all the d-controlled Toffoli gates are decomposed into d-controlled and \(d+1\)-controlled CNOT gates. Consequently, with the help of \(|d\rangle\) and \(|d+1\rangle\) quantum state of \((d+2)\)-dimensional system, \(X_d\) is carried out effectively if all the controlled qudits are in \(|d-1\rangle\) state. In this way, any dimensional multi-controlled Toffoli gate can be decomposed. In [18], the authors have exhibited that how quantum states could be moved without using SWAP gate in any dimensional quantum system. Thus, it paves the path for future qudit mapping problem to be implemented, in a cost-effective manner.

Conclusion

Here, in this paper, we have proposed an end-to-end framework, which includes mapping of k-coloring problem to any NISQ devices/multi-valued quantum technology through automatic generation of oracle circuit using Grover’s search followed by MCT realization for any dimensional quantum system. This proposed approach for any dimensional quantum system is applicable for any undirected and unweighted given graph, which makes our approach generalized. We exhibited that our comparator-based approach requires \(n \cdot \lceil {\log _d} {k} \rceil\) data qudits, which leads to a query complexity \(O({\sqrt{n \cdot {\log _d {k}}}})\) to solve k-coloring problem using Grover’s algorithm. Further, we have shown that our newly proposed ternary comparator is a key component in designing the circuit for the k-coloring problem using quantum search algorithm. Our new ternary oracle circuit achieved an at least 41% reduction in the quantum gate cost as compared to the most recent related work [28] and an at least 85% reduction in the quantum gate cost as compared to the work [34]. In the future, with the evolution of qudit-supported quantum hardware, we would like to validate our designs.