# How can we code a ccz (control control z, equivalent of Toffoli on Z and not on X) gate in Qiskit language?

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.

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) ├
«     └──────────┘


• +1 Minor comments: I think QuantumRegister(3) is sufficient. Also, the numpy import is probably not needed for the snippet to work. Commented Feb 28, 2021 at 2:46
• @AdamZalcman Yes, that is right. :) Commented Feb 28, 2021 at 4:30
• OK, Thank you for your help. Commented Mar 1, 2021 at 17:23
• 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 ? Commented Mar 2, 2021 at 9:07
• 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 ? Commented Mar 2, 2021 at 9:07