DEV Community

Kshitiz Sharma
Kshitiz Sharma

Posted on

You have tried Maths on Computer, now try it on Quantum computer

Simulating Classical Arithmetic on Quantum Circuits with Qiskit

Quantum computing is often introduced through complex concepts like superposition and entanglement. However, a practical way to understand quantum logic gates is to rebuild familiar classical components—like adders and multiplexers—using quantum hardware.

This article demonstrates how to implement Half Adders and Full Adders using IBM’s Qiskit SDK, based on the implementations found in the QuantumProgramming repository.

The Prerequisite: Qubits vs. Bits

In this implementation, we utilize Qiskit, an open-source SDK for working with IBM quantum processors.

While classical bits are binary (0 or 1), qubits are defined by statevectors and can exist in superpositions. However, to simulate classical arithmetic deterministic logic, we initialize these qubits to specific states (0 or 1) and use quantum gates that mimic classical logic tables:

  • Pauli-X Gate (.x): Analogous to a classical NOT gate.
  • CNOT Gate (.cx): Analogous to an XOR gate (used for Sum).
  • Toffoli Gate (.ccx): Analogous to an AND gate (used for Carry).

1. Implementing the Half Adder

A Half Adder accepts two binary inputs and generates two outputs: a Sum and a Carry.

Circuit Architecture

The circuit requires 4 qubits and 2 classical bits for measurement. We initialize the input qubits to 1 using the Pauli-X gate to test the "1 + 1" case.

The Code

from qiskit import *
from qiskit.providers.aer import AerSimulator

# Initialize Quantum Circuit: 4 Qubits, 2 Classical Bits
qc = QuantumCircuit(4, 2)

# Initialize inputs to '1' using the Pauli-X gate
qc.x() 

# Logic Implementation
qc.cx(0, 2)       # XOR gate (Sum calculation)
qc.cx(1, 2)       # XOR gate (Sum calculation)
qc.ccx(0, 1, 3)   # Toffoli gate (Carry calculation)

# Measure results to classical bits
qc.measure(,)
Enter fullscreen mode Exit fullscreen mode

Execution and Output

When running this circuit on a simulator (like AerSimulator), the output for inputs 1 and 1 results in a count of { '10': 1024 }.

  • Binary 10 corresponds to decimal 2.
  • This confirms the Half Adder logic: 1 + 1 = 0 (Sum) with a Carry of 1.

2. Scaling to a Full Adder

To handle a carry-in bit, we scale up to a Full Adder. This requires a larger circuit setup to manage three inputs (q0, q1, q2).

Circuit Architecture

The implementation uses 5 qubits and 4 classical bits. In the repository example, all three inputs are initialized to 1 to test the maximum addition case (1 + 1 + 1).

The Code

qfa = QuantumCircuit(5, 4)

# Initialize all three inputs to '1'
qfa.x()

# XOR gates for Sum logic (CNOT)
qfa.cx(,)

# Toffoli gates for Carry logic
qfa.ccx(0, 2, 4)
qfa.ccx(0, 1, 4)
qfa.ccx(1, 2, 4)

# Measure the results
qfa.measure(,)
Enter fullscreen mode Exit fullscreen mode

Execution and Output

The simulator returns { '0011': 1024 }.

  • The binary result 11 equals decimal 3.
  • This validates the logic: 1 + 1 + 1 = 1 (Sum) with a Carry of 1.

Conclusion and Resources

Translating classical logic into quantum circuits is an excellent way to familiarize yourself with Qiskit's syntax and gate operations. The repository also explores control logic, including a 2x1 Multiplexer implementation.

For the full source code and Jupyter Notebooks, you can view the repository here:
GitHub: clunkiersalt817/QuantumProgramming.

Top comments (0)