# N-Toffoli on Cirq

I am looking for guidance in more generally how to developed n-bit gates in Cirq.

I am working on a QNN paper and I need to develop a n-controlled gate to be able to measure the cost function of the circuit.

This is actually very easy in Cirq. The controlled_by method can be used to automatically make any given gate controlled by an arbitrary number of control qubits. Here is a simple example for creating an X gate with 5 controls:

import cirq

qb = [cirq.LineQubit(i) for i in range(6)]

cnX = cirq.X.controlled_by(qb[0], qb[1], qb[2], qb[3], qb[4]);

circuit = cirq.Circuit();
circuit.append(cnX(qb[5]));

• Thank you. I am wondering though: how can this solution work when you have n-qubits and you cannot hardcode it like the example above? – Enrique Segura Jun 26 at 19:04
• Use an unpacking asterisk, e.g. cnX = cirq.X.controlled_by(*qb[:-1]) – Joseph Geipel Jun 27 at 2:47
• Thank you very much ! – Enrique Segura Jun 27 at 3:57