ibrahim321123 commited on
Commit
e14fa43
·
verified ·
1 Parent(s): 921b5d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -21
app.py CHANGED
@@ -1,36 +1,36 @@
1
- from qiskit import QuantumCircuit, transpile, assemble, Aer, execute
2
- from qiskit.visualization import plot_histogram
 
3
  import gradio as gr
4
 
5
- # This is just a placeholder for a basic quantum circuit that shows QFT
6
- def run_basic_qft_demo(N):
7
  try:
8
  N = int(N)
9
- if N <= 0 or N > 8:
10
- return "Please enter a small integer (1–8) for demo."
 
 
11
 
12
- qc = QuantumCircuit(N)
13
- # Apply Hadamards to all qubits as a trivial QFT placeholder
14
- for i in range(N):
15
- qc.h(i)
16
 
17
- backend = Aer.get_backend("aer_simulator")
18
- qc.measure_all()
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=run_basic_qft_demo,
30
- inputs=gr.Number(label="Enter number of qubits (1–8)"),
31
  outputs=gr.Textbox(label="Result"),
32
- title="Modern Qiskit Demo — QFT placeholder",
33
- description="Modern Qiskit 1.x example: runs a trivial circuit to show basic QFT structure. For real Shor, you’d build modular exponentiation + QFT manually."
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__":