Commit
·
a87f7b2
1
Parent(s):
a011025
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,4 @@
|
|
| 1 |
-
|
| 2 |
-
# pip install qiskit
|
| 3 |
-
# pip install qiskit-aer
|
| 4 |
-
"""
|
| 5 |
-
multiply.py: Multiply two numbers using repeated fourier
|
| 6 |
-
transform based addition.
|
| 7 |
-
"""
|
| 8 |
-
|
| 9 |
from qiskit import QuantumRegister, QuantumCircuit, ClassicalRegister
|
| 10 |
from qiskit import Aer, execute
|
| 11 |
from math import pi
|
|
@@ -22,7 +15,6 @@ def createInputState(qc, reg, n, pie):
|
|
| 22 |
for i in range(0, n):
|
| 23 |
qc.cp(pie / float(2**(i + 1)), reg[n - (i + 1)], reg[n])
|
| 24 |
|
| 25 |
-
|
| 26 |
def evolveQFTState(qc, reg_a, reg_b, n, pie, factor):
|
| 27 |
"""
|
| 28 |
Evolves the state |F(ψ(reg_a))> to |F(ψ(reg_a+reg_b))> using the quantum
|
|
@@ -37,7 +29,6 @@ def evolveQFTState(qc, reg_a, reg_b, n, pie, factor):
|
|
| 37 |
else:
|
| 38 |
qc.cp(factor*pie / float(2**(i)), reg_b[n - i], reg_a[n])
|
| 39 |
|
| 40 |
-
|
| 41 |
def inverseQFT(qc, reg, n, pie):
|
| 42 |
"""
|
| 43 |
Performs the inverse quantum Fourier transform on a register reg.
|
|
@@ -49,7 +40,6 @@ def inverseQFT(qc, reg, n, pie):
|
|
| 49 |
qc.cp(-1 * pie / float(2**(n - i)), reg[i], reg[n])
|
| 50 |
qc.h(reg[n])
|
| 51 |
|
| 52 |
-
|
| 53 |
def add(reg_a, reg_b, circ, factor):
|
| 54 |
"""
|
| 55 |
Add two quantum registers reg_a and reg_b, and store the result in
|
|
@@ -69,50 +59,46 @@ def add(reg_a, reg_b, circ, factor):
|
|
| 69 |
for i in range(0, n + 1):
|
| 70 |
inverseQFT(circ, reg_a, i, pie)
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
circ.x(
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
add(multiplier, d, circ, -1)
|
| 108 |
-
for i in range(len(multiplier)):
|
| 109 |
-
circ.measure(multiplier[i], cl[i])
|
| 110 |
result = execute(circ, backend=Aer.get_backend('qasm_simulator'),
|
| 111 |
-
|
| 112 |
-
|
|
|
|
| 113 |
|
| 114 |
-
|
| 115 |
-
result = execute(circ, backend=Aer.get_backend('qasm_simulator'),
|
| 116 |
-
shots=2).result().get_counts(circ.name)
|
| 117 |
|
| 118 |
-
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from qiskit import QuantumRegister, QuantumCircuit, ClassicalRegister
|
| 3 |
from qiskit import Aer, execute
|
| 4 |
from math import pi
|
|
|
|
| 15 |
for i in range(0, n):
|
| 16 |
qc.cp(pie / float(2**(i + 1)), reg[n - (i + 1)], reg[n])
|
| 17 |
|
|
|
|
| 18 |
def evolveQFTState(qc, reg_a, reg_b, n, pie, factor):
|
| 19 |
"""
|
| 20 |
Evolves the state |F(ψ(reg_a))> to |F(ψ(reg_a+reg_b))> using the quantum
|
|
|
|
| 29 |
else:
|
| 30 |
qc.cp(factor*pie / float(2**(i)), reg_b[n - i], reg_a[n])
|
| 31 |
|
|
|
|
| 32 |
def inverseQFT(qc, reg, n, pie):
|
| 33 |
"""
|
| 34 |
Performs the inverse quantum Fourier transform on a register reg.
|
|
|
|
| 40 |
qc.cp(-1 * pie / float(2**(n - i)), reg[i], reg[n])
|
| 41 |
qc.h(reg[n])
|
| 42 |
|
|
|
|
| 43 |
def add(reg_a, reg_b, circ, factor):
|
| 44 |
"""
|
| 45 |
Add two quantum registers reg_a and reg_b, and store the result in
|
|
|
|
| 59 |
for i in range(0, n + 1):
|
| 60 |
inverseQFT(circ, reg_a, i, pie)
|
| 61 |
|
| 62 |
+
def quantum_multiply(multiplicand_in, multiplier_in):
|
| 63 |
+
multiplicand_in = multiplicand_in.strip()
|
| 64 |
+
multiplier_in = multiplier_in.strip()
|
| 65 |
+
|
| 66 |
+
multiplicand = QuantumRegister(len(multiplicand_in))
|
| 67 |
+
multiplier = QuantumRegister(len(multiplier_in))
|
| 68 |
+
accumulator = QuantumRegister(len(multiplicand_in) + len(multiplier_in))
|
| 69 |
+
cl = ClassicalRegister(len(multiplicand_in) + len(multiplier_in))
|
| 70 |
+
d = QuantumRegister(1)
|
| 71 |
+
|
| 72 |
+
circ = QuantumCircuit(accumulator, multiplier, multiplicand,
|
| 73 |
+
d, cl, name="qc")
|
| 74 |
+
|
| 75 |
+
# Store bit strings in quantum registers
|
| 76 |
+
for i in range(len(multiplicand_in)):
|
| 77 |
+
if multiplicand_in[i] == '1':
|
| 78 |
+
circ.x(multiplicand[len(multiplicand_in) - i - 1])
|
| 79 |
+
|
| 80 |
+
for i in range(len(multiplier_in)):
|
| 81 |
+
if multiplier_in[i] == '1':
|
| 82 |
+
circ.x(multiplier[len(multiplicand_in) - i - 1])
|
| 83 |
+
|
| 84 |
+
multiplier_str = '1'
|
| 85 |
+
# Perform repeated addition until the multiplier
|
| 86 |
+
# is zero
|
| 87 |
+
while(int(multiplier_str) != 0):
|
| 88 |
+
add(accumulator, multiplicand, circ, 1)
|
| 89 |
+
add(multiplier, d, circ, -1)
|
| 90 |
+
for i in range(len(multiplier)):
|
| 91 |
+
circ.measure(multiplier[i], cl[i])
|
| 92 |
+
result = execute(circ, backend=Aer.get_backend('qasm_simulator'),
|
| 93 |
+
shots=2).result().get_counts(circ.name)
|
| 94 |
+
multiplier_str = list(result.keys())[0]
|
| 95 |
+
|
| 96 |
+
circ.measure(accumulator, cl)
|
|
|
|
|
|
|
|
|
|
| 97 |
result = execute(circ, backend=Aer.get_backend('qasm_simulator'),
|
| 98 |
+
shots=2).result().get_counts(circ.name)
|
| 99 |
+
|
| 100 |
+
return list(result.keys())[0]
|
| 101 |
|
| 102 |
+
iface = gr.Interface(quantum_multiply, inputs=["text", "text"], outputs="text")
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
iface.launch()
|