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)?
-
$\begingroup$ 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? $\endgroup$– Davit KhachatryanCommented Mar 4, 2020 at 14:47
-
$\begingroup$ @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? $\endgroup$– Frank WangCommented Mar 4, 2020 at 16:55
-
$\begingroup$ 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)) $\endgroup$– Davit KhachatryanCommented Mar 4, 2020 at 17:06
-
$\begingroup$ 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. $\endgroup$– Davit KhachatryanCommented Mar 4, 2020 at 17:16
-
1$\begingroup$ @DavitKhachatryan thanks a lot! $\endgroup$– Frank WangCommented Mar 5, 2020 at 0:45
1 Answer
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.
Just an answer about the doubly controlled circuit, don't know much about the links and the problem that you have introduced.
-
$\begingroup$ Yes, this method is far from optimal. For example, to build a toffoli gate (CCNOT) with the double get_controlled_circuit method described above results in a circuit depth of 41! $\endgroup$ Commented Oct 9, 2020 at 19:04
-
$\begingroup$ Follow up question, is there an equation for a multi-controlled, multi-target arbitrary U gate? $\endgroup$ Commented Oct 9, 2020 at 19:06
-
1$\begingroup$ @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. $\endgroup$ Commented Oct 12, 2020 at 9:20 -
$\begingroup$ 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. $\endgroup$ Commented Oct 13, 2020 at 16:54
-
1