File size: 2,758 Bytes
527dbb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39c2890
527dbb5
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import streamlit as st
from qiskit import QuantumRegister, QuantumCircuit, ClassicalRegister
from qiskit import Aer, execute
from math import pi

def createInputState(qc, reg, n, pie):
    qc.h(reg[n])
    for i in range(0, n):
        qc.cp(pie / float(2**(i + 1)), reg[n - (i + 1)], reg[n])

def evolveQFTState(qc, reg_a, reg_b, n, pie, factor):
    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 inverseQFT(qc, reg, n, pie):
    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):
    pie = pi
    n = len(reg_a) - 1

    for i in range(0, n + 1):
        createInputState(circ, reg_a, n - i, pie)

    for i in range(0, n + 1):
        evolveQFTState(circ, reg_a, reg_b, n - i, pie, factor)

    for i in range(0, n + 1):
        inverseQFT(circ, reg_a, i, pie)

def quantum_multiply(multiplicand_in, multiplier_in):
    multiplicand = QuantumRegister(len(multiplicand_in))
    multiplier = QuantumRegister(len(multiplier_in))
    accumulator = QuantumRegister(len(multiplicand_in) + len(multiplier_in))
    cl = ClassicalRegister(len(multiplicand_in) + len(multiplier_in))
    d = QuantumRegister(1)
    circ = QuantumCircuit(accumulator, multiplier, multiplicand, d, cl, name="qc")

    circ.x(d)

    for i in range(len(multiplicand_in)):
        if multiplicand_in[i] == '1':
            circ.x(multiplicand[len(multiplicand_in) - i - 1])

    for i in range(len(multiplier_in)):
        if multiplier_in[i] == '1':
            circ.x(multiplier[len(multiplicand_in) - i - 1])

    multiplier_str = '1'
    while int(multiplier_str) != 0:
        add(accumulator, multiplicand, circ, 1)
        add(multiplier, d, circ, -1)
        for i in range(len(multiplier)):
            circ.measure(multiplier[i], cl[i])
        result = execute(circ, backend=Aer.get_backend('qasm_simulator'), shots=2).result().get_counts(circ.name)
        multiplier_str = list(result.keys())[0]

    circ.measure(accumulator, cl)
    result = execute(circ, backend=Aer.get_backend('qasm_simulator'), shots=2).result().get_counts(circ.name)

    return result

def main():
    st.title("Quantum Multiplication App Using Fourier Transform")
    st.write("Enter two binary numbers to perform quantum multiplication.")

    multiplicand_in = st.text_input("Enter the multiplicand (binary):")
    multiplier_in = st.text_input("Enter the multiplier (binary):")

    if st.button("Multiply"):
        result = quantum_multiply(multiplicand_in, multiplier_in)
        st.write("Result of Quantum Multiplication:", result)

if __name__ == "__main__":
    main()