# Is Quantum Computing so different?

## Journey towards Classical Boolean Gates to Quantum Gates Using IBM Q

In this article, we discuss about Quantum gates equivalent to Classical Boolean gates.

Here is the combined truth table for classical logic gates:

To simulate the gates mentioned in the combined truth table(Fig1) in to Quantum reversible gates. We can observe the output of **AND**, **NAND**, **OR**, and **NOR** do not have equal number of 0’s and 1’s, they need an ancilla bit. For **XOR** and **XNOR**, no ancilla bit is needed

Note:The qubits referred in the circuit composer follows left to right where as qubits mentioned in the Mapping follows right to left

**Toffoli Gate:**

The CCNOT gate is a 3-qubit gate having the following properties:

- If both the 1st and 2nd inputs(A and B) are |1⟩, then it flips the 3rd input(C) while keeping the 1st and 2nd inputs(A and B) unchanged.
- Otherwise, it keeps all inputs unchanged.

The net effect of this gate is:

- It keeps the 1st qubit unchanged
- It keeps the 2nd qubit unchanged
- the 3rd qubit updates with the XORed operation. qubit3 XOR (qubit1 AND qubit2) as shown in Fig 2.

Note:The circuit’s mentioned in this article are created using IBM Quantum Composer, in which identity matrix is used to represent the previous state shall be forwarded and |0⟩(ket zero) reset gate is explicitly used to represent the qubit shall be initialize to |0⟩(ket zero) state even though IBMQ provides all qubits to default |0⟩ state.

**Simulate Classical AND gate using Toffoli gate also known as Controlled-Controlled-NOT gate or CCNOT gate:**

If we want to calculate |x⟩ AND |y⟩, we can:

- Prepare the 1st input to be |x⟩
- Prepare the 2nd input to be |y⟩
- Prepare the 3rd input to be |0⟩

Since we prepare the 3rd input to be |0⟩ on purpose, we can just ignore the cases where the 3rd input is |1⟩ from the Fig 2 truth table. By removing all the rows containing 1 in the 3rd input (i.e. the 2nd, 4th, 6th, 8th row) from the table above, it becomes as shown in Fig3.

The net effect of the gate in this setting is:

- It keeps the 1st qubit unchanged;
- It keeps the 2nd qubit unchanged;
- It outputs “qubit1 AND qubit2” as the result to the 3rd qubit, since “0 XOR x = x” for any “x”.

If we look at the 1st input, the 2nd input and the 3rd output as shown in Fig4. The table is exactly the same as the classical AND gate shown in Fig1.

Checking the result using IBM Quantum circuit composer:

**simulated using python and qiskit module:**

**2. Simulate Classical NAND gate using Toffoli gate also known as Controlled-Controlled-NOT gate or CCNOT gate**

from **Fig1** we can observe difference between AND and NAND gates, which depicts that 1’s flipped to 0’s and vice versa

We use the same Toffoli gate which is mentioned in the Fig2, as well use the X gate which flips the qubits from |0⟩ to |1⟩ and |1⟩ to |0⟩ and also Initializing the 3rd qubit to |0⟩ only which means removing all the rows containing 1 in the 3rd input (i.e. the 2nd, 4th, 6th, 8th row) from the Map 2 shown above.

If we want to calculate NOT (x AND y), we can:

- Prepare the 1st input to be |x⟩
- Prepare the 2nd input to be |y⟩
- Prepare the 3rd input to be |0⟩

The net effect of the gate in this setting is:

- It keeps the 1st qubit unchanged;
- It keeps the 2nd qubit unchanged;
- It outputs “NOT (qubit1 AND qubit2)” as the result to the 3rd qubit, since “0 XOR x = x” for any “x”.

If we look at the 1st input, the 2nd input and the 3rd output as shown in Fig5. The table is exactly the same as the classical NAND gate shown in Fig1.

Checking the result using IBM Quantum circuit composer:

**simulated using python and qiskit module:**

**3. Simulate Classical OR gate using Toffoli gate also known as Controlled-Controlled-NOT gate or CCNOT gate**

from the Fig1 we can observe the OR gate’s Truth table which depicts if any one input contains 1’s bit then the output bit shall be 1 other wise 0. The same can be observed in the below Map 3.

One can observe from NAND gate’s result. if we apply NOT gate to both the inputs the net truth table shall be equal to OR gate as shown in Fig 6

If we want to calculate |x⟩ OR |y⟩, we can:

- Prepare the 1st input to be NOT |x⟩
- Prepare the 2nd input to be NOT |y⟩
- Prepare the 3rd input to be |0⟩

Since we prepare the 3rd input to be |0⟩ on purpose, we can just ignore the cases where the 3rd input is |1⟩ from the Map 3 Mapping. By removing all the rows containing 1 in the 3rd input (i.e. the 2nd, 4th, 6th, 8th row) from the table above, it becomes as shown in Fig 7.

To achieve the net effect of inputs gates unchanged shall apply NOT gates again after Toffoli gate.

The net effect of the gate in this setting is:

- It keeps the 1st qubit unchanged;
- It keeps the 2nd qubit unchanged;
- It outputs “qubit1 OR qubit2” as the result to the 3rd qubit, since “0 XOR x = x” for any “x”.

If we look at the 1st input, the 2nd input and the 3rd output as shown in Fig8. The table is exactly the same as the classical OR gate shown in Fig1.

Checking the result using IBM Quantum circuit composer:

**simulated using python and qiskit module:**

**4. Simulating Classical NOR gate using Toffoli gate also known as Controlled-Controlled-NOT gate or CCNOT gate**

from the Fig1 we can observe the NOR gate’s Truth table which depicts if any one input contains 1’s bit then the output bit shall be 0 other wise 1. The same can be observed in the below Map 4.

We can achieve this mapping in two different approaches.

- Applying NOT operation to OR gate(using IBMQ Circuit Composer).
- Applying NOT operation to both inputs of AND gate(using qiskit module).

**Approach 1:**

We can observe from OR gate’s result. if we apply NOT gate to output register the net truth table shall be equal to NOR gate as shown in Fig 8

If we want to calculate |x⟩ NOR |y⟩, we can:

- Prepare the 1st input to be |x⟩
- Prepare the 2nd input to be |y⟩
- Prepare the 3rd input to be |0⟩

Since we prepare the 3rd input to be |0⟩ on purpose, we can just ignore the cases where the 3rd input is |1⟩ from the Map 4 Mapping. By removing all the rows containing 1 in the 3rd input (i.e. the 2nd, 4th, 6th, 8th row) from the table above, it becomes as shown in Fig 9.

To achieve the net effect of inputs gates unchanged shall apply NOT gates again after Toffoli gate.

The net effect of the gate in this setting is:

- It keeps the 1st qubit unchanged;
- It keeps the 2nd qubit unchanged;
- It outputs “qubit1 NOR qubit2” as the result to the 3rd qubit, since “0 XOR x = x” for any “x”.

If we look at the 1st input, the 2nd input and the 3rd output as shown in Fig10. The table is exactly the same as the classical NOR gate shown in Fig1.

**Checking the result using IBM Quantum circuit composer:**

**simulate NOR gate using python and qiskit module:**

**Approach 2:**

We can observe from AND gate’s result. if we apply NOT gate to both the inputs the net truth table shall be equal to NOR gate as shown in Fig 11

**5. Simulate Classical XOR gate using Controlled NOT gate also known as CNOT gate or CX gate**

from the Fig1 we can observe the XOR gate’s Truth table which depicts if first input is 1’s bit then the second input shall be flipped other wise remains unchanged. The same can be observed in the below Map 5.

XOR gate having equal number of 0’s and 1’s, no ancilla bit is needed.

IBM Qiskit is having a special gate for this purpose. which is known as CNOT or CX gate.

The net effect of the gate in this setting is:

- It keeps the 1st qubit unchanged;

- It flips the 2nd qubit if 1st qubit is |1⟩ otherwise 2nd qubit is unchanged;

**Checking the result using IBM Quantum circuit composer:**

**simulated XOR gate using python and qiskit module:**

**6. Simulate Classical XNOR gate using Controlled NOT gate also known as CNOT gate or CX gate**

from the Fig1 we can observe the XNOR gate’s Truth table which depicts if first input is 0’s bit then the second input shall be flipped other wise remains unchanged. The same can be observed in the below Map 6.

XNOR gate having equal number of 0’s and 1’s, no ancilla bit is needed.

The net effect of the gate in this setting is:

- It keeps the 1st qubit unchanged;
- It flips the 2nd qubit if 1st qubit is |0⟩ otherwise 2nd qubit is unchanged;

**Checking the result using IBM Quantum circuit composer:**

**simulated using python and qiskit module:**

update: 1/sep/2021 — “We can observe the output of **AND**, **NAND**, **OR**, and X**OR** do not have equal number of 0’s and 1’s” modified as “ We can observe the output of **AND**, **NAND**, **OR**, and **NOR** do not have equal number of 0’s and 1’s” — identified by Dr. Alessandro Crimi

**References:** Qiskit Textbook, StackExchange, IITM course by @Prabha M and @Anil P, Qubit x Qubit(The Coding School) course.