How I can implement this CNOT gate with control activated when input is 0 instead of 1 in Qiskit. Refer circuit

I'm adding 8 to the input with the CNOT and X gates. I want to set a flag when the sum adds up to 8. When the sum is 8, the 2nd and 3rd qubit outputs 0. In which case I want to apply the CNOT with control activated when input is 0 rather than 1.

I do not want to apply X gate before the controlled-gate to achieve this.


2 Answers 2


A CNOT that controls on a 0 is usually called "open-controlled" and it can be implemented in Qiskit like this:

from qiskit import QuantumCircuit
circuit = QuantumCircuit(2)
circuit.cx(0, 1, ctrl_state=0)
q_0: ──o──
q_1: ┤ X ├

Your circuit requires a multi-open-controlled Toffoli, that is an instance of MCXGate (multi controlled X gate):

from qiskit import QuantumCircuit
from qiskit.circuit.library import MCXGate

circuit = QuantumCircuit(4)
for _ in range(4):
    circuit.cx(1, 2)
circuit.append(MCXGate(3, ctrl_state="000"), [0, 1, 2, 3])
q_0: ──────────────────────────────────────────o──
          ┌───┐     ┌───┐     ┌───┐     ┌───┐  │  
q_1: ──■──┤ X ├──■──┤ X ├──■──┤ X ├──■──┤ X ├──o──
     ┌─┴─┐└───┘┌─┴─┐└───┘┌─┴─┐└───┘┌─┴─┐└───┘  │  
q_2: ┤ X ├─────┤ X ├─────┤ X ├─────┤ X ├───────o──
     └───┘     └───┘     └───┘     └───┘     ┌─┴─┐
q_3: ────────────────────────────────────────┤ X ├

In Qiskit, you can use the method Gate.control or ControlledGate.control to create a controlled version of any given Gate or ControlledGate, respectively. This method takes an argument num_ctrl_qubits to specify the number of control qubits you want to add (=3 in you case) and an argument ctrl_state which is a bitstring matching the number of controls and representing the control state (='000' in your case).

from qiskit import QuantumCircuit
from qiskit.circuit.library import XGate

cccx = XGate().control(num_ctrl_qubits=3, ctrl_state='000')

qc = QuantumCircuit(4)
qc.append(cccx, range(4))


enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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