Quantum Computing relies heavily on principles of Quantum Mechanics such as Quantum Superposition and Quantum Entanglement. CNOT Gate (or sometimes also known as CX Gate) is can be used for entangling the state of 2 Qubits, which makes it a very important Quantum Gate. In this chapter of the OpenQASM Tutorial, you will be learning about how to apply CNOT Gate on Qubits.
CNOT(or CX) is a fundamental two-Qubit gate in the field of Quantum Computing. In OpenQASM, CNOT is a built-in gate and therefore OpenQASM provides the syntax to apply a CNOT Gate on two Qubits.
CNOT Gate
The CNOT Gate is a two-Qubit gate which applies a conditional NOT Gate on a target Qubit if and only if the control Qubit is in state |1>
The following syntax is used for applying the CNOT Gate with control Qubit qr1[index]
and target Qubit qr2[index]
–
CX qr1[index], qr2[index];
Note– CNOT is a built-in gate therefore CX
is a reserved keyword.
Example
Below is an example of applying the CNOT Gate with control Qubit as first Qubit of Quantum Register qubits
and target qubit as second Qubit of Quantum Register qubits
–
CX qubits[0], qubits[1];
Note– The control and target Qubits can both be in the same as well as different Quantum Register.
Below is a representation of the code in the above example
Example
Below is an example of applying the CNOT Gate with control Qubit as first Qubit of Quantum Register qubits1
and target qubit as first Qubit of another Quantum Register qubits2
–
CX qubits1[0], qubits2[0];
Below is a representation of the code in the above example
CNOT Gate on Quantum Registers
It is also possible to set all the Qubits of a Quantum Register as control qubit and all the corresponding Qubits of another Quantum Register as target Qubits for applying the CNOT gate.
The following syntax is used for applying the CNOT Gate where Qubits of Quantum Register qr1
are control Qubits and Qubits of Quantum Register qr2
are target Qubits-
CX qr1, qr2[index];
Example
Consider the following code-
CX qubits1, qubits2;
The above code applied CNOT gate with the following control and target Qubits-
Control Qubit | Target Qubit |
---|---|
qubits1[0] | qubits2[0] |
qubits1[1] | qubits2[1] |
… | … |
qubits1[n] | qubits2[n] |
Below is a representation of the code in the above example, consider both qubits1
and qubits2
are Quantum Registers with 4 Qubits each.
The following are also valid syntax for applying CNOT Gate-
Same Control Qubit, Different Target Qubits
The following syntax is used for applying the CNOT Gate where a Qubit(the first one in this case) of Quantum Register qubits1
is the control Qubit for all the Qubits of Quantum Register qubits2
which are its target Qubits-
CX qubits1[0], qubits2;
Below is a Circuit representation of the same-
Note– Notice that qubits1[0]
happens to be the control Qubit for all the CNOT Gates. While the Qubits of qubits2
are target Qubits for different CNOT Gates.
Same Target Qubit, Different Control Qubits
The following syntax is used for applying the CNOT Gate where a Qubit(the first one in this case) of Quantum Register qubits2
is the target Qubit for all the Qubits of Quantum Register qubits1
which are its control Qubits-
CX qubits1, qubits2[0];
Below is a Circuit representation of the same-
Note– Notice that qubits2[0]
happens to be the target Qubit for all the CNOT Gates. While the Qubits of qubits1
are control Qubits for different CNOT Gates.
Note– The control and target Qubits cannot be the same.
Example
Consider the following code in which control and target Qubit are the same, which is wrong–
// This is wrong
CX qubits1[0], qubits1[0];