# Coding The Half Adder Quantum Circuit

Recently, I learned about half adder quantum circuits and decided to replicate one. In quantum computing, we use tools called quantum logic gates, which are quantum circuits that is operating on a small number of qubits. In this algorithm, we will use Qiskit to encode our input, perform our desired operations on each bit using logic gates, and extract our output! I will be adding 1+1 on a 4-qubit system.

## Understanding Logic Gates

A logic gate is an idealized computation model or physical electronic device which utilizes **boolean functions **(logical operations performed on one or more binary inputs)to produce a signal binary output.

# The Process

First, I used the CNOT or Controlled NOT gate which performs a **Pauli-X gate** on the target qubit when the control qubit is in state ∣1⟩; leaves the target qubit unchanged when the control qubit is in state ∣0⟩ while leaving the control qubit unchanged.

While using Jupyter Notebook, I begun by encoding the inputs.

`qc = QuantumCircuit(4,2)`

# inputs encoded in 0 and 1 qubits

qc.x(0) #1st qubit flipped to 1

qc.x(1) #2nd qubit flipped to 1 in order to perform the addition 1+1

Then, I used our CNOT gate to XOR input of qubit 2.

`# cnots used to write the XOR of the inputs on qubit 2`

qc.cx(0,2)

qc.cx(1,2)

Here, I extracted the outputs from this gate.

# outputs extracted

qc.measure(2,0) # extract XOR value

qc.measure(3,1)qc.draw(output='mpl')

Addition of the ccx Tofolli gate.

qc = QuantumCircuit(4,2)

# inputs encoded in qubits 0 and 1

qc.x(0)

qc.x(1)

# cnots used to write the XOR of the qubit 2 inputs

qc.cx(0,2)

qc.cx(1,2)

# ccx is used to write the AND of the qubit 3 inputs

qc.ccx(0,1,3)

# outputs extracted

qc.measure(2,0) # extract XOR value

qc.measure(3,1) # extract AND valueqc.draw(output='mpl')

Finally, I ran the circuit on a quantum simulator.

`counts = execute(qc,Aer.get_backend('qasm_simulator')).result().get_counts()`

plot_histogram(counts)

# TL;DR

- A logic gate is an idealized computation model or physical electronic device which utilizes
**boolean functions.** - The CNOT gate leaves the control qubit untouched.
- Our result probability is in binary and our inputs can be changed with this algorithm

Hi there! My name’s Laura and I’m a curious 17-year-old hoping to impact the world using emerging technologies.

If you would like to follow along with my journey, you can connect with me on LinkedIn and subscribe to my monthly newsletter.