SHORs_Algorithm / app.py
ibrahim321123's picture
Create app.py
1b8c9a6 verified
raw
history blame
1.23 kB
# app.py
from qiskit import Aer
from qiskit.utils import QuantumInstance
from qiskit.algorithms import Shor
import gradio as gr
# Function to run Shor's algorithm
def run_shor(N):
try:
N = int(N)
if N % 2 == 0 or N <= 1:
return f"Please enter an odd integer > 1."
if N > 21:
return f"Too big! Factor small numbers like 15 or 21 for simulation."
# Use Aer simulator backend
backend = Aer.get_backend('aer_simulator')
quantum_instance = QuantumInstance(backend, shots=1024)
shor = Shor(quantum_instance=quantum_instance)
result = shor.factor(N)
if result.factors:
return f"Factors of {N} are: {result.factors}"
else:
return f"Failed to find factors. Try again!"
except Exception as e:
return str(e)
# Gradio interface
iface = gr.Interface(
fn=run_shor,
inputs=gr.Number(label="Enter an integer to factor"),
outputs=gr.Textbox(label="Result"),
title="Shor's Algorithm Demo",
description="A simple Qiskit + Gradio demo to factor integers using Shor's Algorithm. Runs on simulator, so keep N small (e.g., 15, 21)."
)
if __name__ == "__main__":
iface.launch()