Spaces:
Runtime error
Runtime error
Download src/app/gradio_demo.py from QuantumGenAI/ErrorMitigation: direct link, hf CLI and curl.
- Browser
- Download file 5.03 kB
-
https://huggingface.co/spaces/QuantumGenAI/ErrorMitigation/resolve/main/src/app/gradio_demo.py
- Command line
-
hf download hf://spaces/QuantumGenAI/ErrorMitigation/src/app/gradio_demo.py
-
curl -L -o gradio_demo.py https://huggingface.co/spaces/QuantumGenAI/ErrorMitigation/resolve/main/src/app/gradio_demo.py
5.03 kB
| import gradio as gr | |
| import logging | |
| from circuits.circuit_optimizer import optimize_circuit_with_llm | |
| from circuits.noisy_circuit_runner import simulate_noisy_circuit | |
| from utils.qasm_utils import remove_measurements | |
| from utils.visualization import plot_circuit | |
| from utils.metrics import euclidean_distance | |
| # Setup logging | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| def process_qasm(qasm_code, noise_model): | |
| """Handles QASM input, optimizes with LLM, and returns results.""" | |
| if not qasm_code.strip(): | |
| return "β οΈ Error: No QASM code provided.", None, None, None, None | |
| try: | |
| # Clean input QASM | |
| cleaned_qasm = remove_measurements(qasm_code) | |
| # Generate optimized QASM using LLM | |
| logger.info("Optimizing QASM with LLM...") | |
| optimized_qasm = optimize_circuit_with_llm(cleaned_qasm) | |
| # Simulate both circuits with selected noise model | |
| logger.info(f"Simulating original noisy circuit with {noise_model}...") | |
| original_results = simulate_noisy_circuit(cleaned_qasm, noise_model) | |
| logger.info(f"Simulating optimized circuit with {noise_model}...") | |
| optimized_results = simulate_noisy_circuit(optimized_qasm, noise_model) | |
| # Compute distance metric | |
| circuit_distance = euclidean_distance(original_results, optimized_results) | |
| # Generate visualizations | |
| original_plot = plot_circuit(cleaned_qasm) | |
| optimized_plot = plot_circuit(optimized_qasm) | |
| return optimized_qasm, original_plot, optimized_plot, f"{circuit_distance:.4f}" | |
| except Exception as e: | |
| logger.error(f"Error processing QASM: {e}") | |
| return f"β Error: {str(e)}", None, None, None | |
| def fetch_recent_circuits(): | |
| """Fetches recent circuits from DuckDB and returns as dropdown options.""" | |
| circuits = [] | |
| return [(c["id"], c["qasm"]) for c in circuits] | |
| def main(): | |
| """Launches the Gradio demo interface with a full-screen, beautiful design.""" | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| gr.Markdown( | |
| "<h1 style='text-align: center;'>β¨ Quantum Circuit Error Mitigation</h1>" | |
| "<h3 style='text-align: center; color: gray;'>Optimize noisy circuits using AI-powered correction π</h3>" | |
| ) | |
| with gr.Row(): | |
| gr.Markdown("### π Original Circuit") | |
| # INPUT AREA | |
| with gr.Row(): | |
| with gr.Column(scale=1, min_width=500): | |
| qasm_input = gr.Textbox( | |
| lines=10, | |
| label="π Input QASM Code", | |
| placeholder="Paste your QASM code here...", | |
| elem_classes="full-width-textbox", | |
| ) | |
| circuit_selector = gr.Dropdown( | |
| label="π Load Circuit From Duck DB", | |
| choices=fetch_recent_circuits(), | |
| interactive=True, | |
| ) | |
| noise_model_selector = gr.Dropdown( | |
| label="π² Select Noise Model", | |
| choices=[ | |
| "Depolarizing", | |
| "Bit-Flip", | |
| "Phase-Flip", | |
| "Amplitude-Damping", | |
| "Custom", | |
| ], | |
| value="Depolarizing", | |
| interactive=True, | |
| ) | |
| with gr.Column(scale=1, min_width=600): | |
| original_plot = gr.Image(label="Original Circuit Visualization") | |
| with gr.Row(): | |
| submit_button = gr.Button("β‘ Optimize Circuit", size="lg") | |
| # VISUALIZATION AREA | |
| with gr.Row(): | |
| gr.Markdown("### π Optimized Circuit") | |
| with gr.Row(): | |
| with gr.Column(scale=1, min_width=600): | |
| optimized_qasm_output = gr.Textbox( | |
| lines=10, | |
| label="β¨ Optimized QASM Code", | |
| interactive=False, | |
| elem_classes="full-width-textbox", | |
| ) | |
| with gr.Column(scale=1, min_width=600): | |
| optimized_plot = gr.Image(label="Optimized Circuit Visualization") | |
| with gr.Row(): | |
| circuit_distance = gr.Textbox( | |
| label="π Circuit Distance (Euclidean)", | |
| interactive=False, | |
| elem_classes="full-width-textbox", | |
| ) | |
| # Button Actions | |
| submit_button.click( | |
| process_qasm, | |
| inputs=[qasm_input, noise_model_selector], | |
| outputs=[ | |
| optimized_qasm_output, | |
| original_plot, | |
| optimized_plot, | |
| circuit_distance, | |
| ], | |
| ) | |
| circuit_selector.change( | |
| lambda cid: next(q for i, q in fetch_recent_circuits() if i == cid), | |
| inputs=[circuit_selector], | |
| outputs=[qasm_input], | |
| ) | |
| demo.launch(share=False) | |
| launch_demo = main | |
| if __name__ == "__main__": | |
| main() | |