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.
Quantum Computing Stack Exchange is a question and answer site for engineers, scientists, programmers, and computing professionals interested in quantum computing. It only takes a minute to sign up.Sign up to join this community
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, qb, qb, qb, qb) circuit = cirq.Circuit() circuit.append(cnX(qb))
For doing CnX (N-CNOT or N-Tofolli) gate,
The following is the correct way to do using
from cirq import X, Circuit NUM_QBITS = 3 qbits = cirq.LineQubit.range(NUM_QBITS) qa = cirq.NamedQubit("ansilla") cnX = X.controlled(NUM_QBITS).on(*qbits[:NUM_QBITS], qa) print(Circuit(cnX))