1
$\begingroup$

How can we code in Qiskit, a ccz gate ?

In python (in https://qiskit.org/textbook/ch-algorithms/grover.html), we see three instructions : qc.h(nqubits-1) qc.mct(list(range(nqubits-1)), nqubits-1) # multi-controlled-toffoli qc.h(nqubits-1)

In qiskit , a Hadamard gate can be coded as : circuit.h(qreg[i]). But what is it this "mct" gate ? i've not seen it in Qiskit documentation.

$\endgroup$
1
$\begingroup$

You can use the MCMT (multi-controlled multi-target gate) class to do something like this. To execute CCZ gate, I can do it as follow:

from qiskit.circuit.library import MCMT
qr= QuantumRegister(3)
circ=QuantumCircuit(qr)
circ = MCMT('cz',2,1)
print(circ)
q_0: ─■─
      │ 
q_1: ─■─
      │ 
q_2: ─■─
        

And if you want to see how the circuit would look using more elementary gates, you can decompose it:

print(circ.decompose().decompose())

                                               ┌───┐    ┌─────────────┐     »
q_0: ──────────────────────────────────────────┤ X ├────┤ U(0,0,-π/4) ├──■──»
     ┌────────────┐                            └─┬─┘    └─────────────┘  │  »
q_1: ┤ U(0,0,π/4) ├──■───────────────────■───────■───────────────────────┼──»
     ├────────────┤┌─┴─┐┌─────────────┐┌─┴─┐┌──────────┐ ┌────────────┐┌─┴─┐»
q_2: ┤ U(0,0,π/4) ├┤ X ├┤ U(0,0,-π/4) ├┤ X ├┤ U(0,0,0) ├─┤ U(0,0,π/4) ├┤ X ├»
     └────────────┘└───┘└─────────────┘└───┘└──────────┘ └────────────┘└───┘»
«                             ┌───┐     ┌────────────┐                         »
«q_0: ────────────────■───────┤ X ├─────┤ U(0,0,π/4) ├──■───────────────────■──»
«                     │       └─┬─┘     └────────────┘  │                   │  »
«q_1: ────────────────┼─────────■───────────────────────┼───────────────────┼──»
«     ┌────────────┐┌─┴─┐┌─────────────┐┌────────────┐┌─┴─┐┌─────────────┐┌─┴─┐»
«q_2: ┤ U(0,0,π/4) ├┤ X ├┤ U(0,-π/2,0) ├┤ U(0,0,π/4) ├┤ X ├┤ U(0,0,-π/4) ├┤ X ├»
«     └────────────┘└───┘└─────────────┘└────────────┘└───┘└─────────────┘└───┘»
«                 
«q_0: ────────────
«                 
«q_1: ────────────
«     ┌──────────┐
«q_2: ┤ U(0,0,0) ├
«     └──────────┘

$\endgroup$
6
  • $\begingroup$ +1 Minor comments: I think QuantumRegister(3) is sufficient. Also, the numpy import is probably not needed for the snippet to work. $\endgroup$ – Adam Zalcman Feb 28 at 2:46
  • $\begingroup$ @AdamZalcman Yes, that is right. :) $\endgroup$ – KAJ226 Feb 28 at 4:30
  • 1
    $\begingroup$ OK, Thank you for your help. $\endgroup$ – Bertrand Mercier Mar 1 at 17:23
  • $\begingroup$ In the below instruction "circ = MCMT('cz',2,1)", how can we know the numbers of the list of control qubits and the number of the target qubit ? $\endgroup$ – Bertrand Mercier Mar 2 at 9:07
  • $\begingroup$ In the doc of MCMT class, we see the methods of the class, and among them, there is "mcrz", that is to apply a multiple controlled Z rotation gate , with : q_controls (list(Qubit)) – The list of control qubits & q_target (Qubit) – The target qubit. But there is a lambda angle to set. What's this lambda angle ? $\endgroup$ – Bertrand Mercier Mar 2 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.