Spaces:
Sleeping
Sleeping
Commit
•
527dbb5
1
Parent(s):
3863ae1
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from qiskit import QuantumRegister, QuantumCircuit, ClassicalRegister
|
3 |
+
from qiskit import Aer, execute
|
4 |
+
from math import pi
|
5 |
+
|
6 |
+
def createInputState(qc, reg, n, pie):
|
7 |
+
qc.h(reg[n])
|
8 |
+
for i in range(0, n):
|
9 |
+
qc.cp(pie / float(2**(i + 1)), reg[n - (i + 1)], reg[n])
|
10 |
+
|
11 |
+
def evolveQFTState(qc, reg_a, reg_b, n, pie, factor):
|
12 |
+
l = len(reg_b)
|
13 |
+
for i in range(0, n + 1):
|
14 |
+
if (n - i) > l - 1:
|
15 |
+
pass
|
16 |
+
else:
|
17 |
+
qc.cp(factor*pie / float(2**(i)), reg_b[n - i], reg_a[n])
|
18 |
+
|
19 |
+
def inverseQFT(qc, reg, n, pie):
|
20 |
+
for i in range(0, n):
|
21 |
+
qc.cp(-1 * pie / float(2**(n - i)), reg[i], reg[n])
|
22 |
+
qc.h(reg[n])
|
23 |
+
|
24 |
+
def add(reg_a, reg_b, circ, factor):
|
25 |
+
pie = pi
|
26 |
+
n = len(reg_a) - 1
|
27 |
+
|
28 |
+
for i in range(0, n + 1):
|
29 |
+
createInputState(circ, reg_a, n - i, pie)
|
30 |
+
|
31 |
+
for i in range(0, n + 1):
|
32 |
+
evolveQFTState(circ, reg_a, reg_b, n - i, pie, factor)
|
33 |
+
|
34 |
+
for i in range(0, n + 1):
|
35 |
+
inverseQFT(circ, reg_a, i, pie)
|
36 |
+
|
37 |
+
def quantum_multiply(multiplicand_in, multiplier_in):
|
38 |
+
multiplicand = QuantumRegister(len(multiplicand_in))
|
39 |
+
multiplier = QuantumRegister(len(multiplier_in))
|
40 |
+
accumulator = QuantumRegister(len(multiplicand_in) + len(multiplier_in))
|
41 |
+
cl = ClassicalRegister(len(multiplicand_in) + len(multiplier_in))
|
42 |
+
d = QuantumRegister(1)
|
43 |
+
circ = QuantumCircuit(accumulator, multiplier, multiplicand, d, cl, name="qc")
|
44 |
+
|
45 |
+
circ.x(d)
|
46 |
+
|
47 |
+
for i in range(len(multiplicand_in)):
|
48 |
+
if multiplicand_in[i] == '1':
|
49 |
+
circ.x(multiplicand[len(multiplicand_in) - i - 1])
|
50 |
+
|
51 |
+
for i in range(len(multiplier_in)):
|
52 |
+
if multiplier_in[i] == '1':
|
53 |
+
circ.x(multiplier[len(multiplicand_in) - i - 1])
|
54 |
+
|
55 |
+
multiplier_str = '1'
|
56 |
+
while int(multiplier_str) != 0:
|
57 |
+
add(accumulator, multiplicand, circ, 1)
|
58 |
+
add(multiplier, d, circ, -1)
|
59 |
+
for i in range(len(multiplier)):
|
60 |
+
circ.measure(multiplier[i], cl[i])
|
61 |
+
result = execute(circ, backend=Aer.get_backend('qasm_simulator'), shots=2).result().get_counts(circ.name)
|
62 |
+
multiplier_str = list(result.keys())[0]
|
63 |
+
|
64 |
+
circ.measure(accumulator, cl)
|
65 |
+
result = execute(circ, backend=Aer.get_backend('qasm_simulator'), shots=2).result().get_counts(circ.name)
|
66 |
+
|
67 |
+
return result
|
68 |
+
|
69 |
+
def main():
|
70 |
+
st.title("Quantum Multiplication App")
|
71 |
+
st.write("Enter two binary numbers to perform quantum multiplication.")
|
72 |
+
|
73 |
+
multiplicand_in = st.text_input("Enter the multiplicand (binary):")
|
74 |
+
multiplier_in = st.text_input("Enter the multiplier (binary):")
|
75 |
+
|
76 |
+
if st.button("Multiply"):
|
77 |
+
result = quantum_multiply(multiplicand_in, multiplier_in)
|
78 |
+
st.write("Result of Quantum Multiplication:", result)
|
79 |
+
|
80 |
+
if __name__ == "__main__":
|
81 |
+
main()
|