# Make a doubly controlled gates from existing operations

I'm trying to implement an modular adder mentioned here with qiskit. I have already built the $$\Phi ADD$$ gate. But in order to build a modular adder like in figure 5 in the paper, I need to build a doubly-controlled $$\Phi ADD$$ gate. Qiskit offers a method to transfer a circuit to a controlled gate, but now I need a doubly controlled gate. I know I can use something like this, but that need an additional qubit, which is undesired. I also know that I can use this method, but I don't know how to implement the square root of $$\Phi ADD$$ gate. Is there any other method to do this (creating a doubly-controlled gate from the $$\Phi ADD$$ gate I built without adding any additional qubits)?

• Hi, Frank :). One can apply Qiskit's get_controlled_circuit method twice to create the doubly controlled version of a given circuit. Will this help with your specific problem? Mar 4 '20 at 14:47
• @DavitKhachatryan Ok I just tried, but I don't quite understand what it does. I read the help message of the function, it said the function takes a circuit and a qubit as arguments and constructs the controlled version of circuit. But when I tried the function, the circuit looks entirely different to me and I can't tell if it does the same thing. So I'm guessing that the function modifies the circuit to add a control qubit, instead of creating a controlled version while using the original circuit as a whole. Am I correct? Mar 4 '20 at 16:55
• Here is the original code of the method github.com/Qiskit/qiskit-aqua/blob/master/qiskit/aqua/utils/…. How I understand 1) it takes your given circuit 2) changes your gates to Qiskit's basis gates (u1, u2, u3, cx). 3) for each given basis gate it modifies and replaces it with the controlled version of it (it has a "dictionary of methods" that implements the controlled circuits for all basis gates). I have used it for implementing phase estimation algo and it worked)) Mar 4 '20 at 17:06
• If you have used not the basis gates, then it will not be easy to understand the resulting circuit and even if you have used the basis gates I imagine that it will be still unreadable. Mar 4 '20 at 17:16
• @DavitKhachatryan thanks a lot! Mar 5 '20 at 0:45

Here is an example of creating a doubly controlled version of the simplest circuit with one gate (Qiskit's $$u1$$ gate).

from qiskit import *
from qiskit.aqua.utils.controlled_circuit import get_controlled_circuit
import numpy as np

q_reg = QuantumRegister(3, 'q')
qc_u1 = QuantumCircuit(q_reg)
qc_cu1 = QuantumCircuit(q_reg)
qc_ccu1 = QuantumCircuit(q_reg)

qc_u1.u1(np.pi/2, q_reg)

qc_cu1 = get_controlled_circuit(qc_u1, q_reg)

qc_ccu1 = get_controlled_circuit(qc_cu1, q_reg)

print(qc_cu1.qasm())
print(qc_ccu1.qasm())


And yes, even for this simplest gate, the printed result looks horrible XD (a lot of gates), because it uses generic procedures to create the controlled version of the given circuit. Maybe optimizing the circuit at the end will reduce the gate number and make it a more readable circuit.

• @thespaceman, what you mean by saying "equation"? Do you mean an "algorithm"? If yes then get_controlled_circuit implements an algorithm that is described in the comments of the question. Also, Figure 4.6. and Figure 4.8 in this textbook might be interesting. Oct 12 '20 at 9:20
• I am looking for a general equation which represents a multi-control U-gate for unitary U. For example, for a single-control gate I could use the equation $CU = I\bigotimes |0><0| + U\bigotimes |1><1|$. I am looking to extend this for a multi-control U-gate. Additionally, thank you for the references from Nielsen. I have already constructed a circuit from Figure 4.10 of Nielsen, but this is sub-optimal since it requires ancillary qubits. I am hoping that a general equation could help me to construct a multi-control gate without ancillary qubits. Oct 13 '20 at 16:54
• @thespaceman, this answer about doubly controlled $H$ gate and this answer about $CU$ for not adjacent qubits might be interesting. But I am not sure how this kind of equations can help to construct more optimal circuits. Oct 13 '20 at 17:08