Skip to content

Instantly share code, notes, and snippets.

@PatrickChoDev
Created December 12, 2024 14:01
Show Gist options
  • Save PatrickChoDev/596b313bbc06d806a61517963ade57ea to your computer and use it in GitHub Desktop.
Save PatrickChoDev/596b313bbc06d806a61517963ade57ea to your computer and use it in GitHub Desktop.
Binary multiplication on circuit based Quantum computer (Qiskit)
#Please run this inside the Jupyter notebook, It'll be quite straight forward.
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, transpile
from qiskit_aer import AerSimulator
from math import pi
from matplotlib import pyplot as plt
%matplotlib inline
def create_input_state(qc, reg, n, pie):
"""
Computes the quantum Fourier transform of reg, one qubit at a time.
Apply one Hadamard gate to the nth qubit of the quantum register reg, and
then apply repeated phase rotations with parameters being pi divided by
increasing powers of two.
"""
qc.h(reg[n])
for i in range(0, n):
qc.cp(pie / float(2 ** (i + 1)), reg[n - (i + 1)], reg[n]) # Controlled phase
def evolve_qft_state(qc, reg_a, reg_b, n, pie, factor):
"""
Evolves the state |F(ψ(reg_a))> to |F(ψ(reg_a+reg_b))> using the quantum
Fourier transform conditioned on the qubits of the reg_b.
"""
l = len(reg_b)
for i in range(0, n + 1):
if (n - i) > l - 1:
pass
else:
qc.cp(factor * pie / float(2 ** (i)), reg_b[n - i], reg_a[n])
def inverse_qft(qc, reg, n, pie):
"""
Performs the inverse quantum Fourier transform on a register reg.
Apply repeated phase rotations with parameters being pi divided by
decreasing powers of two, and then apply a Hadamard gate to the nth qubit.
"""
for i in range(0, n):
qc.cp(-1 * pie / float(2 ** (n - i)), reg[i], reg[n])
qc.h(reg[n])
def add(reg_a, reg_b, circ, factor):
"""
Add two quantum registers reg_a and reg_b, and store the result in reg_a.
"""
pie = pi
n = len(reg_a) - 1
# Compute the Fourier transform of register a
for i in range(0, n + 1):
create_input_state(circ, reg_a, n - i, pie)
# Add the two numbers by evolving the Fourier transform
for i in range(0, n + 1):
evolve_qft_state(circ, reg_a, reg_b, n - i, pie, factor)
# Compute the inverse Fourier transform of register a
for i in range(0, n + 1):
inverse_qft(circ, reg_a, i, pie)
# Take two numbers as user input in binary form
multiplicand_in = input("Enter the multiplicand: ")
l1 = len(multiplicand_in)
multiplier_in = input("Enter the multiplier: ")
l2 = len(multiplier_in)
# Ensure multiplicand_in holds the larger number
if l2 > l1:
multiplier_in, multiplicand_in = multiplicand_in, multiplier_in
l2, l1 = l1, l2
multiplicand = QuantumRegister(l1, "multiplicand")
multiplier = QuantumRegister(l2, "multiplier")
accumulator = QuantumRegister(l1 + l2, "accumulator")
cl = ClassicalRegister(l1 + l2, "cl")
d = QuantumRegister(1, "decrement")
circ = QuantumCircuit(accumulator, multiplier, multiplicand, d, cl, name="qc")
circ.x(d) # Initialize decrement qubit
# Store bit strings in quantum registers
for i in range(l1):
if multiplicand_in[i] == "1":
circ.x(multiplicand[l1 - i - 1])
for i in range(l2):
if multiplier_in[i] == "1":
circ.x(multiplier[l2 - i - 1])
multiplier_str = "1"
# Perform repeated addition until the multiplier is zero
while int(multiplier_str, 2) != 0:
add(accumulator, multiplicand, circ, 1) # Add multiplicand to accumulator
add(multiplier, d, circ, -1) # Decrement multiplier by 1
circ.measure(multiplier, cl[:l2])
simulator = AerSimulator()
transpiled_circ = transpile(circ, simulator)
result = simulator.run(transpiled_circ, shots=1).result()
counts = result.get_counts()
multiplier_str = max(counts, key=counts.get)
circ.measure(accumulator, cl)
circ.draw(output='mpl')
plt.show()
simulator = AerSimulator()
transpiled_circ = transpile(circ, simulator)
result = simulator.run(transpiled_circ, shots=1024).result()
counts = result.get_counts()
print("Final result in binary:", counts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment