File size: 5,028 Bytes
a5fcd60
 
 
e0d7f28
 
a5fcd60
e0d7f28
a5fcd60
 
 
 
 
 
e0d7f28
 
a5fcd60
 
 
 
 
 
 
 
 
 
 
 
e0d7f28
 
 
a5fcd60
e0d7f28
 
a5fcd60
 
 
 
 
 
 
 
 
 
 
 
 
 
e0d7f28
a5fcd60
 
 
 
 
e0d7f28
a5fcd60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0d7f28
a5fcd60
 
 
 
e0d7f28
 
 
 
 
 
 
 
 
 
 
 
 
 
a5fcd60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0d7f28
a5fcd60
 
 
 
 
 
 
 
 
e0d7f28
a5fcd60
 
 
 
 
e0d7f28
 
 
 
 
 
 
a5fcd60
 
 
 
 
e0d7f28
a5fcd60
 
 
 
e0d7f28
a5fcd60
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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()