Skip to content

Instantly share code, notes, and snippets.

@kronaemmanuel
Created May 19, 2021 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kronaemmanuel/030dcd4914e536e9470a1109e23ec71d to your computer and use it in GitHub Desktop.
Save kronaemmanuel/030dcd4914e536e9470a1109e23ec71d to your computer and use it in GitHub Desktop.
Two Bit Adder Circuit With Qiskit
# We need 4 qubits for input, 7 for processing and 3 output bits
tba = QuantumCircuit(11, 3)
# Step 1: encode
# Here's how numbers are entered: E.g. 10 + 01 = 11
# q_0 = 0 and q_1 = 1 represent first number
# q_2 = 1 and q_3 = 0 represent second number
#tba.x(0) # For a=0, remove the this line. For a=1, leave it.
tba.x(1) # For b=0, remove the this line. For b=1, leave it.
tba.x(2) # For a=0, remove the this line. For a=1, leave it.
#tba.x(3) # For b=0, remove the this line. For b=1, leave it.
tba.barrier()
# Step 2: operations
# Adding 2^0 bit
# XOR q_0 and q_2 on q_4
tba.cx(0, 4)
tba.cx(2, 4)
tba.barrier()
# Adding 2^1 bit
# ADD q_0 and q_2 on q_5
tba.ccx(0, 2, 5)
# XOR q_1 and q_3 on q_6
tba.cx(1, 6)
tba.cx(3, 6)
# XOR q_5 and q_6 on q_7
tba.cx(5, 7)
tba.cx(6, 7)
tba.barrier()
# Adding 2^2 bit
# AND q_5 and q_6 on q_8
tba.ccx(5, 6, 8)
# AND q_1 and q_3 on q_9
tba.ccx(1, 3, 9)
tba.barrier()
# We need a OR gate between q_8 and q_9. We construct a OR gate by following method:
# 1. Invert q_8 and q_9 with NOT
# 2. ADD inverted q_8 and q_9
# 3. Invert the result of ADD with NOT
tba.x(8)
tba.x(9)
tba.ccx(8, 9, 10)
tba.x(10)
tba.barrier()
# Step 3: extract output
tba.measure(4, 0)
tba.measure(7, 1)
tba.measure(10, 2)
tba.draw()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment