Is Quantum Computing so different?

Journey towards Classical Boolean Gates to Quantum Gates Using IBM Q

Anonymousket
10 min readJun 16, 2021

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

Here is the combined truth table for classical logic gates:

Fig 1: 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:

Fig 2: Toffoli Gate -> Matrix form and Truth Table
  • 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.

  1. Simulate Classical AND gate using Toffoli gate also known as Controlled-Controlled-NOT gate or CCNOT gate:
Map 1: AND Gate Mapping, Matrix

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⟩
Fig3: removing rows containing 1

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”.
Fig4: Achieved Classical AND gate

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:

Composer1 : Input |000⟩ -> output |000⟩
Composer2 : Input |010⟩ -> output |010⟩
Composer3 : Input |001⟩ -> output |001⟩
Composer4 : Input |011⟩ -> output |111⟩

simulated using python and qiskit module:

and gate using toffoli gate
circuit and measurement

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

Map 2: NAND Gate -> Mapping and Matrix

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:

Fig 5: NAND Truth Table
  • 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:

Composer1 : Input |000⟩ -> output |001⟩
Composer2 : Input |010⟩ -> output |110⟩
Composer3 : Input |001⟩ -> output |101⟩
Composer4 : Input |011⟩ -> output |011⟩

simulated using python and qiskit module:

nand gate using toffoli gate
circuit and measurement

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.

Map 3: OR Gate -> Mapping and Matrix

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

Fig 6: NAND gate to OR Gate

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⟩
Fig 7: removing rows containing 1

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”.
Fig8: Achieved Classical OR gate

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:

Composer1 : Input |000⟩ -> output |000⟩
Composer2 : Input |010⟩ -> output |110⟩
Composer3 : Input |001⟩ -> output |101⟩
Composer4 : Input |011⟩ -> output |111⟩

simulated using python and qiskit module:

or gate using toffoli gate
circuit and measurement

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.

Map 4: NOR Gate -> Mapping and Matrix

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:

Fig 8: Approach 1 -> OR gate to NOR gate

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⟩
Fig 9: removing rows containing 1

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”.
Fig10: Achieved Classical NOR gate

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:

Composer1 : Input |000⟩ -> output |100⟩
Composer2 : Input |010⟩ -> output |010⟩
Composer3 : Input |001⟩ -> output |001⟩
Composer4 : Input |011⟩ -> output |011⟩

simulate NOR gate using python and qiskit module:

nor gate using toffoli gate
circuit and measurement

Approach 2:

Fig 11: Approach 2 -> OR gate to NOR gate

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

nor gate using toffoli gate
circuit and measurement

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

img 1: comparison of classical and Quantum 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.

Map 5: XOR Gate -> Mapping and Matrix

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;
Fig: 12 Achieved classical XOR gate
  • It flips the 2nd qubit if 1st qubit is |1⟩ otherwise 2nd qubit is unchanged;

Checking the result using IBM Quantum circuit composer:

Composer1 : Input |00⟩ -> output |00⟩
Composer2 : Input |10⟩ -> output |10⟩
Composer3 : Input |01⟩ -> output |11⟩
Composer4 : Input |11⟩ -> output |01⟩

simulated XOR gate using python and qiskit module:

xor gate using controlled not gate
circuit and measurement

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.

Map 6: XNOR gate -> Mapping and Matrix
Fig 12: Achieved classical XNOR 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 |0⟩ otherwise 2nd qubit is unchanged;

Checking the result using IBM Quantum circuit composer:

Composer1 : Input |00⟩ -> output |10⟩
Composer2 : Input |10⟩ -> output |00⟩
Composer3 : Input |01⟩ -> output |01⟩
Composer4 : Input |00⟩ -> output |11⟩

simulated using python and qiskit module:

xnor gate using controlled not gate
circuit and measurement

update: 1/sep/2021 — “We can observe the output of AND, NAND, OR, and XOR 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.

--

--

Responses (1)