ibrahim321123 commited on
Commit
1b8c9a6
·
verified ·
1 Parent(s): 467f134

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+
3
+ from qiskit import Aer
4
+ from qiskit.utils import QuantumInstance
5
+ from qiskit.algorithms import Shor
6
+ import gradio as gr
7
+
8
+ # Function to run Shor's algorithm
9
+ def run_shor(N):
10
+ try:
11
+ N = int(N)
12
+ if N % 2 == 0 or N <= 1:
13
+ return f"Please enter an odd integer > 1."
14
+ if N > 21:
15
+ return f"Too big! Factor small numbers like 15 or 21 for simulation."
16
+
17
+ # Use Aer simulator backend
18
+ backend = Aer.get_backend('aer_simulator')
19
+ quantum_instance = QuantumInstance(backend, shots=1024)
20
+
21
+ shor = Shor(quantum_instance=quantum_instance)
22
+ result = shor.factor(N)
23
+
24
+ if result.factors:
25
+ return f"Factors of {N} are: {result.factors}"
26
+ else:
27
+ return f"Failed to find factors. Try again!"
28
+ except Exception as e:
29
+ return str(e)
30
+
31
+ # Gradio interface
32
+ iface = gr.Interface(
33
+ fn=run_shor,
34
+ inputs=gr.Number(label="Enter an integer to factor"),
35
+ outputs=gr.Textbox(label="Result"),
36
+ title="Shor's Algorithm Demo",
37
+ description="A simple Qiskit + Gradio demo to factor integers using Shor's Algorithm. Runs on simulator, so keep N small (e.g., 15, 21)."
38
+ )
39
+
40
+ if __name__ == "__main__":
41
+ iface.launch()