Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,36 @@
|
|
| 1 |
-
from
|
| 2 |
-
from qiskit.
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
def
|
| 7 |
try:
|
| 8 |
N = int(N)
|
| 9 |
-
if N
|
| 10 |
-
return "Please enter
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
for i in range(N):
|
| 15 |
-
qc.h(i)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
result = execute(qc, backend, shots=1024).result()
|
| 21 |
-
counts = result.get_counts()
|
| 22 |
-
|
| 23 |
-
return f"Simulated simple QFT-like circuit for {N} qubits.\nResult: {counts}"
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
except Exception as e:
|
| 26 |
return str(e)
|
| 27 |
|
| 28 |
iface = gr.Interface(
|
| 29 |
-
fn=
|
| 30 |
-
inputs=gr.Number(label="Enter
|
| 31 |
outputs=gr.Textbox(label="Result"),
|
| 32 |
-
title="
|
| 33 |
-
description="
|
| 34 |
)
|
| 35 |
|
| 36 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
from qiskit_aer import Aer
|
| 2 |
+
from qiskit.utils import QuantumInstance
|
| 3 |
+
from qiskit.algorithms import Shor
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
# Function to run Shor's algorithm
|
| 7 |
+
def run_shor(N):
|
| 8 |
try:
|
| 9 |
N = int(N)
|
| 10 |
+
if N % 2 == 0 or N <= 1:
|
| 11 |
+
return f"Please enter an odd integer > 1."
|
| 12 |
+
if N > 21:
|
| 13 |
+
return f"Too big! Factor small numbers like 15 or 21 for simulation."
|
| 14 |
|
| 15 |
+
backend = Aer.get_backend('aer_simulator')
|
| 16 |
+
quantum_instance = QuantumInstance(backend, shots=1024)
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
shor = Shor(quantum_instance=quantum_instance)
|
| 19 |
+
result = shor.factor(N)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
if result.factors:
|
| 22 |
+
return f"Factors of {N} are: {result.factors}"
|
| 23 |
+
else:
|
| 24 |
+
return f"Failed to find factors. Try again!"
|
| 25 |
except Exception as e:
|
| 26 |
return str(e)
|
| 27 |
|
| 28 |
iface = gr.Interface(
|
| 29 |
+
fn=run_shor,
|
| 30 |
+
inputs=gr.Number(label="Enter an integer to factor"),
|
| 31 |
outputs=gr.Textbox(label="Result"),
|
| 32 |
+
title="Shor's Algorithm Demo",
|
| 33 |
+
description="A simple Qiskit + Gradio demo using the classic Shor's Algorithm. Runs on a simulator — use small N like 15, 21."
|
| 34 |
)
|
| 35 |
|
| 36 |
if __name__ == "__main__":
|