# 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? – Davit Khachatryan Mar 4 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? – Frank Wang Mar 4 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)) – Davit Khachatryan Mar 4 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. – Davit Khachatryan Mar 4 at 17:16
• @DavitKhachatryan thanks a lot! – Frank Wang Mar 5 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[0])

qc_cu1 = get_controlled_circuit(qc_u1, q_reg[1])

qc_ccu1 = get_controlled_circuit(qc_cu1, q_reg[2])

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.