Variational Quantum Algorithm based circuit that implements the Toffoli gate with multi inputs
Abstract
The prime objective of this study is to seek a circuit diagram for a multi-inputs Toffoli gate including only single qubit gates and CNOTs. In this regard, we have developed two variational quantum algorithms (VQAs) that can be used to implement a multi-inputs Toffoli gate. The cost functions of these two VQAs are derived by using the Hilbert–Schmidt inner product and the expected value of an observable that can capture the difference between the inputs and outputs of a Toffoli gate. We employ two ansatz circuit architectures and use the PennyLane package to execute the optimization.
I Introduction
Variational Quantum Algorithms (VQAs) have become one of the key tools that enable us to harness the quantum advantage from Noisy Intermediate Scale Quantum (NISQ) devices. VQAs can be viewed as the quantum analog of classical machine-learning methods. The core concept of the VQAs is the optimization of a parameterised quantum circuit, which is designed to be run on a quantum computer yielding a desired output. Moreover, VQAs adopt a leading strategy of outsourcing the parameter optimization to a classical optimizer in order to by pass the constraints imposed by NISQ devices that diminish the quantum advantage. As a result, in VQAs the depth of the quantum circuit can be reduced substantially and the noise mitigation becomes easier compared to that of quantum algorithms developed for the fault-tolerant era [1]. Variational Quantum Eigensolver (VQE) [2] and Quantum Approximate Optimization Algorithm (QAOA) [3] are the best known applications of the VQAs. The prior estimates the ground-state molecular energy for by combining a highly reconfigurable photonic quantum processor with a conventional computer and the latter approximately solves combinatorial problems such as Constrain-Satisfaction and Max-Cut problem.
In this project we intend to make use of a variational quantum algorithm to implement a Toffoli gate with input qubits that constitute a single target and control qubits. Such a Toffoli gate can be termed as a multi-controlled Toffoli (MCX) gate. In its internal structure, a MCX must include many quantum operations in order to yield the desired output. It can be stated with certainty that when the number of control qubits increases, the complexity of the internal structure of the MCX also increases proportionally. Hence, it is not straightforward to decompose a MCX gate with input lines into units of single qubit gates and CNOT gates. To resolve such a problem, VQAs can be considered as a potential candidate. Therefore, we intend to develop a reliable VQA with the aim of determining the optimum internal gate structure for a MCX with inputs under the constrain that a single unit of the internal structure comprises either a one qubit gate or a CNOT gate. We develop two separate VQAs and test them for Toffoli gates with 3 and 5 inputs.
II Basic concept and the components of VQAs
The variational method in quantum theory paves a way to find the lowest energy state or the ground state of a given quantum system [4]. In this method, a trial wave function that comprises one or more parameters is chosen at first as an approximation to the ground state of the system. Sometimes this trial wave function is termed as "ansatz". Then, using the variational principle, the optimal values for the parameters of the trial wave function are obtained in such a way that the expectation value of the energy becomes lowest as possible [5]. The aforementioned variational method lies at the heart of the VQAs. One can identify three main components that constitute a VQA. They are, the cost function, ansatzes and the optimization. Schematic diagram of a variational quantum algorithm that includes theses main three components is given in Figure 1.
II.1 Cost function
In the context of classical machine leaning, the cost function can be understood as the technique of evaluating how well the machine learning model performs for a given dataset [7]. In general, the cost function determines the difference between the expected and predicted values of the machine learning model and quantifies it in terms of a single real number. Similarly, in a VQA, encoding the problem of interest into a cost function is one of the important phases of its development. For a VQA, the cost function can be expressed in a more general way as follows
(1) |
where is some function, is a parameterized unitary, is composed of discrete/continuous parameters, are input states from a training set and are a set of observable [1]. For our study, we need to choose a suitable cost function that can compare the difference between the input and output states of the -inputs Toffoli gate. Since the matrix form of the -inputs Toffoli gate is well known, we employ the Hilbert Schmidt test to derive a suitable cost function to cater the demand of our problem. In addition, as a second method, we derive an observable combining a set of projectors that can capture the difference between the input and output states of a multi-input Toffloi gate. Then, by calculating the expected value of this special operator we determine the cost function.
II.2 Ansatz
Ansatz is a well-known term in mathematics that describe an educated guess made to help in solving a given problem. Since we work with quantum machine learning methods, it is crucial to define our ansatz in a particular way that is unknown at the beginning of our project. We test our code on two types of ansatz configurations named basic entangled layer and strongly entangled layer and determine what is the most suitable configurations for our study. The stated goal of our project is to determine the internal gate structure of a -inputs Tofolli gate that comprises only single qubit gates and CNOTs. Hence, when adopting the aforementioned two different ansatz configurations, we use the most general single qubit gate of the following form to represent any single qubit gates
(2) |
where and . In qiskit and Pennylane the most general single qubit gate is given by operator.
II.3 Gradients and Optimizers
Optimization process of a VQA makes use of the gradients and optimizers to speed up and to guarantee the convergence. For many optimization tasks, the information about the gradient of the cost function can be used to speed up the convergence of the optimization [1]. In this regard, the method of parameter-shifting rule is used to calculate the first and higher-order derivatives of the cost function. Optimizers are objects which can be used to automatically update the parameters of a quantum or hybrid machine learning model. For our study, we use GradientDescentOptimizer, one of the gradient-based optimizers available in the PennyLane package. All the gradient-based optimizers provided by PennyLane update the variational parameters moving along the direction indicated by the gradient at the specified point. GradientDescentOptimizer is the simplest version of the gradient-based optimizers available in PennyLane.
III Toffoli gate
The CNOT gate operates on a quantum register consisting of 2 qubits. The CNOT gate flips the second qubit (the target qubit) if and only if the first qubit (the control qubit) is . An expansion to the CNOT gate is the “Toffoli gate". Toffoli gate is an “AND” gate that contains control qubits and one target qubit. For an arbitrary quantum state , using the Toffoli gate the state will be transformed to the following state
(3) |
Thus, we will add one modulo 2(in dimensional 2 of Hilbert’s space, we can work in higher Hilbert’s spaces, which means that we will work with different groups ) to the target qubits if and only if all of the control qubits are 1. We can implement Toffoli gate with several configurations. However, in our project we want to do so only with single qubit gates and CNOT gates (see Figure 2)(it is possible to do so only with CNOT but it is not efficient in terms of quantum depth). Our goals is to create multi inputs Toffoli gate with quantum machines learning methods and find a way to minimize the circuits depth. The depth of a quantum circuits is the amount of operators in the longest path in the circuits.

III.1 Implementation of Toffoli gate
The basic Toffoli gate (3 inputs) can be arrange according to [8] with depth of 10. When the Toffoli gate contains more inputs, the number of needed gates does not increase linearly and is very hard to create (this is why Classisq announced the Toffoli’s challenge). On the other hand, we noticed that the Toffoli gate contains two parts; the first part is the “AND" gate, and the second part is the phase fix (we want to affect only the target qubit). With the above prior knowledge, we can construct our layers in a certain way and improve the running time of our algorithm. e.g., We can for the first part, we can use a linear entangled pattern, and for the second part pattern we can use a maximally entangled pattern. [9]
IV First Method: Hilbert Schmidt test
The first step in our implementation is to try to copy the “generic” Toffoli gate (using an existing function that is not necessarily composed of just single qubit gates and CNOTS) with VQE using the Hilbert Schmidt test (HST) as a cost function. We succeeded in this task for small-scale operations (2-5 qubits). The HST was introduced in [10], it takes two operators and calculates a metric of their difference. To be more precise, it calculates the following quantity:
(4) |
where is the dimension of Hilbert space. In other words, by using the HST as the cost function of our learning process we’re able to acquire an approximation of the target circuit.

Adding rotations to each layer, each layer is composed of 3 operators. We used four layers but removed two of the cnot layers at the end. Thus we created a circuit with a depth of 10. We simulated the same optimizations with random initial parameters and succeeded in imitating the Toffoli gate according to the Hilbert Schmidt test. We should note that our circuit has a greater depth than the original Toffoli gates, but we can imitate unknown circuits with only rotations and CNOT.


IV.1 Testing our circuits
The learning algorithm stops when the condition is met. By choosing a smaller value for we could obtain a more accurate approximation but it would also increase the time it takes for the algorithm to complete. To check if our method is working we saved our weights from the HST and tested it on “qiskit” code and check all the state to be sure how good is our approximate. The next step was to transform our Pennylane results to a qiskit circuit and using a simulator in order to test the results of the circuits. Unfortunately, the initial is not good enough and not usable in real quantum circuits (Figure 5). From the results we can see that the state with the biggest amplitude is the correct state after the Toffoli gate ,but unfortunately it’s not good enough and can‘t be implemented in a real quantum circuit. We can take smaller but it will take more layers which would increase the depth of our circuits. We can conclude up that the Hilbert Schmidt test may be not the best method to create Toffoli gate because for insufficently small values of the approximation is not good. We will preform a second method in order to try and get better results.

, the state with the higher probability is the correct state after the Toffoli gate, but most of the time we will get incorrect state.
IV.2 Toffoli gate with 5 qubits
When approaching the limit of seven qubits we noticed and dealt with an exponential jump in the run time and the number of iterations and layers. We successfully managed to imitate Toffoli gates of 5 and 7 qubits perfectly with fidelity of 80 percent. We should note that imitating a Toffoli gate with more than 7 qubtis isn’t possible for our computers at a reasonable ammount of time (We used intel 12th Gen Intel(R) Core(TM) i7-12700KF 3.61 GHz). In addition, the number of layers that we need in order to imitate the Toffoli gate became inefficient as we can see in Figure 4(b), we couldn’t reach the global minima in reasonable running time.
V Second method: Defining cost function in terms of the expected value
In the previous approach, the cost function is derived from the Hilbert–Schmidt inner product as shown in (3). For this calculation, we take the trace of the product of parameterized unitary operator that represents the ansatz and the matrix form of the multi-input Toffoli gate. As an alternative method to the Hilbert–Schmidt test, in this section, we intend to derive another type of cost function in terms of the expected value of an observable that can capture the difference between input and output states of the ansatz circuit. Then, by minimizing the expected value of this observable we expect to obtain the desired optimization in the ansatz circuit. The schematic circuit diagram for the second method is given in Figure 6. According to the circuit diagram given in Figure 6, the first register is linked to the circuit ansatz represented by the parameterized unitary operator . The input and the output states of the ansatz are given by and respectively. The second register is initiated from the and the final state of the second register is represented by . Then, the state of the whole circuit can be written as
(5) |
Note that, initially, the first and second registers get entangled due to the configuration of CNOT gates. Thus . That is and give the input and the output states of the ansatz circuit respectively. Therefore, by comparing and we can identify whether the ansatz circuit gives the correct output state for the corresponding input state.
Our next task is to define a suitable operator in such a way that the expected value of this operator can be used to make the distinction between and efficiently. Let us define a set including the pairs of input and the corresponding output states of a Toffoli gate with input qubits as . Now define an operator of the following form
(6) |
Note that, since , the operator given in (6) is Hermitian. Thus, we can consider as an observable. The expected value of can be written as
(7) |
The proof of the expected value in (7) is given below. Write . Then we have . Suppose the circuit ansatz in Figure 6 yields a correct input and output pair. Then, for some unique , we have and . Hence, the expected value of operator can be written as . Thus we have . Now suppose that the input and output pair of the ansatz is not in . Then it is obvious that . Thus we have . This complete the proof. Let us now employ the second method to develop a VQA with the intention of implementing a Toffoli gate with 3 input qubits and 5 input qubits. For this, we need to make a slight modification to the schematic circuit diagram given in Figure 6 in order to generate all possible input states. We initiate the first register with and then apply Hadamard gates to each wire of the first register. Then, the state represents the superposition of all possible input states of a Toffoli gate with 3 or 5 inputs. We use the Pennylean package to develop the VQAs for 3 and 5 input Toffoli gates. Derivation of the observable and is given in Appendix B. Moreover, we consider two parameterized ansatz circuit layers. In the first case we build the circuit ansatz using the basic entangle layer architecture given in Figure 8. Next, the strongly entangled layers architecture given in Figure 9 is utilized. The graphs related to the minimization of the cost function in both architectures are given in Figure 7.

According to the graphs given in Figure 7, after the steps, the value of the cost function tends to oscillate around . Hence, in order to test the efficiency of the second method, we stopped the optimization process whenever the cost function hits and then saved the set of optimized parameters corresponding to the of the cost function. Since the case of 5-inputs anzatz circuit is time consuming, we have followed this testing procedure only for the 3-inputs ansatz circuits. Moreover, we have considered only the basic entangled architecture with 8 layers. After picking up the set of optimized parameters corresponding to of the cost function, we have run the 3-inputs ansatz circuits in IBM quantum platform by inserting this set of optimized parameters. Then, for all possible inputs, we have compared the outputs of the 3-inputs ansatz circuits with that of a 3-inputs Toffoli gate. Our optimized circuit gives the results of 3-inputs Toffoli gates for all the possible input states with more than accuracy. Figure 11 shows the optimized circuit diagrams with 8 layers that mimics the 3-inputs Toffoli gates. Some of the output results of this optimized circuit is given in Figure 12.
VI Conclusion
In this study we have developed two separate VQAs that can be used to determine a quantum circuit for a multi-input Tofolli gate consisting of single qubit gates and CNOTS. The main distinction between these two VQAs arises from the derivation of their cost functions. The VQAs of the first and second methods use the Hilbert–Schmidt inner product and the expected value of an observable to derive efficient cost functions respectively. From the perspective of run time efficiency, the first method surpasses the second method substantially. However, when it comes to the accuracy level of the optimized circuit, second method yields more favourable results. By analysing the convergence rate of the graphs given in Figures 4 and 10, it can be stated that the basic entangled layer is the most suitable architecture for fast convergences than the strongly entangled layer. Due to the run time constrains imposed by the PennyLane package, it is difficulty to run the ansatz circuits with higher number of input qubits. However, we anticipate that the outcome of this study may pave a way to develop more efficient VQAs for implementing Toffoli gates with more inputs. We experience the computational power of quantum computer by trying to implement more then 3 qubit circuit(The run time become unbearable) and hope that in the future we could use a real quantum computer to test our results.
References
- Cerezo et al. [2021] M. Cerezo, A. Arrasmith, R. Babbush, S. C. Benjamin, S. Endo, K. Fujii, J. R. McClean, K. Mitarai, X. Yuan, L. Cincio, et al., Variational quantum algorithms, Nature Reviews Physics 3, 625 (2021).
- Peruzzo et al. [2014] A. Peruzzo, J. McClean, P. Shadbolt, M.-H. Yung, X.-Q. Zhou, P. J. Love, A. Aspuru-Guzik, and J. L. O’brien, A variational eigenvalue solver on a photonic quantum processor, Nature communications 5, 1 (2014).
- Farhi et al. [2014] E. Farhi, J. Goldstone, and S. Gutmann, A quantum approximate optimization algorithm, arXiv preprint arXiv:1411.4028 (2014).
- Sommerfeld [2011] T. Sommerfeld, Lorentz trial function for the hydrogen atom: a simple, elegant exercise, Journal of Chemical Education 88, 1521 (2011).
- Griffiths and Schroeter [2018] D. J. Griffiths and D. F. Schroeter, Introduction to quantum mechanics (Cambridge university press, 2018).
- Macaluso et al. [2020] A. Macaluso, L. Clissa, S. Lodi, and C. Sartori, A variational algorithm for quantum neural networks, in International Conference on Computational Science (Springer, 2020) pp. 591–604.
- Raschka and Mirjalili [2019] S. Raschka and V. Mirjalili, Python machine learning: Machine learning and deep learning with Python, scikit-learn, and TensorFlow 2 (Packt Publishing Ltd, 2019).
- Nielsen and Chuang [2002] M. A. Nielsen and I. Chuang, Quantum computation and quantum information (2002).
- Toffoli [1980] T. Toffoli, Reversible computing, in International colloquium on automata, languages, and programming (Springer, 1980) pp. 632–644.
- Khatri et al. [2019] S. Khatri, R. LaRose, A. Poremba, L. Cincio, A. T. Sornborger, and P. J. Coles, Quantum-assisted quantum compiling, Quantum 3, 140 (2019).
Appendix A Layers
We compared several methods of layering our ansatz. As we mentioned before, the first method is the “basic entangled layers” along with “ strongly entangled layers” and “random layers”. The “basic entangled layers” are composed of a repeating pattern of unitary rotation (U3) and CNOTS that operate with neighboring(the first with the second and go on.. and the last CNOT act on the first qubit). This configuration is similar to the implementation of the Toffoli gate in that each qubit is entangled with the other and resembles the repetition of the Toffoli(Fig 2). This pattern had the best performance. We can assume that it is because of the similarities with the three qubits Toffoli gate(The architecture look vert similar). The “ strongly entangled layers” is similar to the “basic entangled layers” but in each layer the CNOT act with other pair of qubits which entaneled directly each qubit with the another. We simulate method one with strongly entangled layers and got the following results:


As we expected, the results of the “ strongly entangled layers” were less effective than the “basic entangled layers” as we can see in the Figure 10.

Appendix B Derivation of the observable and
In this section, we present the detailed procedure followed to implement the Toffoli gates with 3 and 5 inputs. The set that includes all the pairs of inputs and their corresponding outputs of a Toffoli gate with 3 qubits can be given as
(8) |
Now, let us write the explicit form of the observable that can capture the difference between the input and output states of a Toffoli gate with 3 qubits as
(9) |
Note that, the outter products marked in red and black colours correspond to input and output states respectively. From the properties of tensor products, we can rewrite the operator in (9) as follows
(10) |
When building-up observable in PennyLean, we use the qml.Projector function. Consider an arbitrary state of where are binary variables which can take either 0 or 1. The function qml.Projector allows us to build the projector of . Hence, by deriving a customized Hamiltonian using the projectors in (10), we can build-up the operator. Similarly, for the case of Toffoli gates with 5 inputs, the set can be written as follows
(11) |
Note that, for the sake of convenience, we have employed the decimal representation to write the input output pairs. The explicit form of the observable that can capture the difference between the input and output states of a Toffoli gate with 5 qubits can be written as
(12) |
Following the same fashion described above, we can derive a customized Hamiltonian for the operator in (12) using PennyLean package and then calculate the expected value of it.


