File size: 9,660 Bytes
e77fa31
 
 
 
 
 
 
 
 
 
 
 
 
 
980efa8
e77fa31
 
 
980efa8
e77fa31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
980efa8
e77fa31
 
 
 
 
 
 
 
 
 
980efa8
 
 
e77fa31
980efa8
e77fa31
 
 
 
 
 
980efa8
 
 
 
e77fa31
 
 
980efa8
 
e77fa31
980efa8
 
 
 
e77fa31
 
 
30add96
e77fa31
30add96
e77fa31
980efa8
 
 
 
a63cf6b
980efa8
a63cf6b
e77fa31
 
 
980efa8
a63cf6b
170aab6
980efa8
a63cf6b
9c99c87
a63cf6b
9c99c87
a63cf6b
 
e77fa31
9c99c87
980efa8
170aab6
980efa8
e77fa31
 
9c99c87
a63cf6b
9c99c87
980efa8
9c99c87
 
980efa8
 
 
e77fa31
 
980efa8
30add96
e77fa31
 
980efa8
e77fa31
9c99c87
980efa8
e77fa31
980efa8
e77fa31
9c99c87
 
980efa8
e77fa31
 
980efa8
 
 
 
 
 
e77fa31
980efa8
 
 
 
e77fa31
 
30add96
 
 
 
 
 
 
 
980efa8
 
e77fa31
30add96
 
 
980efa8
 
30add96
 
e77fa31
980efa8
e77fa31
 
 
 
30add96
980efa8
e77fa31
980efa8
 
 
e77fa31
980efa8
 
e77fa31
30add96
e77fa31
 
980efa8
 
 
e77fa31
980efa8
 
e77fa31
30add96
 
e77fa31
30add96
e77fa31
980efa8
 
 
 
 
e77fa31
980efa8
 
 
e77fa31
 
 
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import gradio as gr
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import logging
import requests
from typing import List, Tuple, Dict, Optional
from datasets import load_dataset
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder

# Logging configuration
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Dataset repository configuration
REPO_CONFIG = {
    "Core (Clean)": {
        "repo": "QSBench/QSBench-Core-v1.0.0-demo",
        "meta_url": "https://huggingface.co/datasets/QSBench/QSBench-Core-v1.0.0-demo/raw/metadata/meta/meta.json",
        "report_url": "https://huggingface.co/datasets/QSBench/QSBench-Core-v1.0.0-demo/raw/metadata/meta/report.json"
    },
    "Depolarizing Noise": {
        "repo": "QSBench/QSBench-Depolarizing-Demo-v1.0.0",
        "meta_url": "https://huggingface.co/datasets/QSBench/QSBench-Depolarizing-Demo-v1.0.0/raw/meta/meta/meta.json",
        "report_url": "https://huggingface.co/datasets/QSBench/QSBench-Depolarizing-Demo-v1.0.0/raw/meta/meta/report.json"
    },
    "Amplitude Damping": {
        "repo": "QSBench/QSBench-Amplitude-v1.0.0-demo",
        "meta_url": "https://huggingface.co/datasets/QSBench/QSBench-Amplitude-v1.0.0-demo/raw/meta/meta/meta.json",
        "report_url": "https://huggingface.co/datasets/QSBench/QSBench-Amplitude-v1.0.0-demo/raw/meta/meta/report.json"
    },
    "Transpilation (10q)": {
        "repo": "QSBench/QSBench-Transpilation-v1.0.0-demo",
        "meta_url": "https://huggingface.co/datasets/QSBench/QSBench-Transpilation-v1.0.0-demo/raw/meta/meta/meta.json",
        "report_url": "https://huggingface.co/datasets/QSBench/QSBench-Transpilation-v1.0.0-demo/raw/meta/meta/report.json"
    }
}

# Define non-feature columns to exclude from training
NON_FEATURE_COLS = {
    "sample_id", "sample_seed", "circuit_hash", "split", "circuit_qasm", 
    "qasm_raw", "qasm_transpiled", "circuit_type_resolved", "circuit_type_requested",
    "noise_type", "noise_prob", "observable_bases", "observable_mode", "backend_device", 
    "precision_mode", "circuit_signature", "entanglement", "shots", "gpu_requested", "gpu_available"
}

_ASSET_CACHE = {}

def load_all_assets(key: str) -> Dict:
    """
    Fetch and cache dataset and metadata from Hugging Face.
    """
    if key not in _ASSET_CACHE:
        logger.info(f"Fetching {key} assets...")
        ds = load_dataset(REPO_CONFIG[key]["repo"])
        meta = requests.get(REPO_CONFIG[key]["meta_url"]).json()
        report = requests.get(REPO_CONFIG[key]["report_url"]).json()
        _ASSET_CACHE[key] = {"df": pd.DataFrame(ds["train"]), "meta": meta, "report": report}
    return _ASSET_CACHE[key]

def load_guide_content() -> str:
    """
    Load Markdown content for the Methodology/Guide tab.
    """
    try:
        with open("GUIDE.md", "r", encoding="utf-8") as f:
            return f.read()
    except FileNotFoundError:
        return "### ⚠️ GUIDE.md not found."

def sync_ml_metrics(ds_name: str) -> gr.update:
    """
    Filter and return available numerical features for the selected dataset.
    """
    assets = load_all_assets(ds_name)
    df = assets["df"]
    numeric_cols = df.select_dtypes(include=[np.number]).columns.tolist()
    valid_features = [c for c in numeric_cols if c not in NON_FEATURE_COLS and not any(p in c for p in ["ideal_", "noisy_", "error_"])]
    defaults = [f for f in ["gate_entropy", "meyer_wallach", "adjacency", "depth", "cx_count"] if f in valid_features]
    return gr.update(choices=valid_features, value=defaults)

def train_classifier(ds_name: str, features: List[str]) -> Tuple[Optional[plt.Figure], str]:
    """
    Perform multi-class classification on circuit families and return metrics/plots.
    """
    if not features: 
        return None, "### ❌ Error: No features selected."
    
    assets = load_all_assets(ds_name)
    df = assets["df"]
    
    # Target column selection fallback logic
    target_col = 'circuit_type_resolved' if 'circuit_type_resolved' in df.columns else 'circuit_type_requested'
    
    # Data preprocessing and cleaning
    train_df = df.dropna(subset=features + [target_col])
    if 'mixed' in train_df[target_col].unique() and len(train_df[target_col].unique()) > 1:
        train_df = train_df[train_df[target_col] != 'mixed']

    X = train_df[features]
    y = train_df[target_col]

    if len(y.unique()) < 2:
        return None, f"### ❌ Error: Dataset contains insufficient classes for training ({y.unique()})."

    # Label encoding and dataset splitting
    le = LabelEncoder()
    y_encoded = le.fit_transform(y)
    
    try:
        X_train, X_test, y_train, y_test = train_test_split(X, y_encoded, test_size=0.2, random_state=42, stratify=y_encoded)
    except (ValueError, TypeError):
        X_train, X_test, y_train, y_test = train_test_split(X, y_encoded, test_size=0.2, random_state=42)

    # Model initialization and training
    clf = RandomForestClassifier(n_estimators=100, max_depth=12, n_jobs=-1, random_state=42)
    clf.fit(X_train, y_train)
    preds = clf.predict(X_test)

    # Visualization generation
    sns.set_theme(style="whitegrid")
    fig, axes = plt.subplots(1, 2, figsize=(20, 8))

    # Confusion Matrix Plot
    cm = confusion_matrix(y_test, preds)
    sns.heatmap(cm, annot=True, fmt='d', cmap='viridis', xticklabels=le.classes_, yticklabels=le.classes_, ax=axes[0], cbar=False)
    axes[0].set_title(f"Confusion Matrix (Accuracy: {accuracy_score(y_test, preds):.2%})")

    # Feature Importance Plot
    importances = clf.feature_importances_
    idx = np.argsort(importances)[-10:]
    axes[1].barh([features[i] for i in idx], importances[idx], color='#2ecc71')
    axes[1].set_title("Top-10 Predictive Features")

    plt.tight_layout()
    
    # Performance metrics string generation
    cls_report = classification_report(y_test, preds, target_names=le.classes_, output_dict=False)
    results_md = f"### πŸ† Classification Results\n**Target:** `{target_col}`\n**Accuracy:** {accuracy_score(y_test, preds):.2%}\n\n**Metrics:**\n```text\n{cls_report}\n```"
    
    return fig, results_md

def update_explorer(ds_name: str, split_name: str) -> Tuple[gr.update, pd.DataFrame, str, str, str]:
    """
    Refresh the Explorer view based on dataset and split selection.
    """
    assets = load_all_assets(ds_name)
    df = assets["df"]
    splits = df["split"].unique().tolist() if "split" in df.columns else ["train"]
    
    if split_name not in splits:
        split_name = splits[0]
        
    filtered = df[df["split"] == split_name] if "split" in df.columns else df
    display_df = filtered.head(10)
    
    raw_qasm = display_df["qasm_raw"].iloc[0] if "qasm_raw" in display_df.columns and not display_df.empty else "// N/A"
    transpiled_qasm = display_df["qasm_transpiled"].iloc[0] if "qasm_transpiled" in display_df.columns and not display_df.empty else "// N/A"
    
    return (
        gr.update(choices=splits, value=split_name), 
        display_df, 
        raw_qasm, 
        transpiled_qasm, 
        f"### πŸ“‹ {ds_name} Explorer"
    )

# Gradio interface definition
with gr.Blocks(theme=gr.themes.Soft(), title="QSBench Classifier") as demo:
    gr.Markdown("# 🌌 QSBench: Circuit Family Classifier")
    
    with gr.Tabs():
        with gr.TabItem("πŸ”Ž Explorer"):
            meta_label = gr.Markdown("### Initializing...")
            with gr.Row():
                ds_dropdown = gr.Dropdown(list(REPO_CONFIG.keys()), value="Core (Clean)", label="Dataset Type")
                split_dropdown = gr.Dropdown(["train"], value="train", label="Split")
            explorer_df = gr.Dataframe(interactive=False)
            with gr.Row():
                raw_qasm_code = gr.Code(label="Logical QASM", language="python")
                tr_qasm_code = gr.Code(label="Transpiled QASM", language="python")

        with gr.TabItem("🧠 Classification"):
            with gr.Row():
                with gr.Column(scale=1):
                    ml_ds_dropdown = gr.Dropdown(list(REPO_CONFIG.keys()), value="Core (Clean)", label="Noise Environment")
                    ml_feature_checks = gr.CheckboxGroup(label="Input Metrics", choices=[])
                    run_btn = gr.Button("Train & Evaluate", variant="primary")
                with gr.Column(scale=2):
                    plot_output = gr.Plot()
                    results_output = gr.Markdown()

        with gr.TabItem("πŸ“– Guide"):
            gr.Markdown(load_guide_content())

    gr.Markdown("--- \n ### πŸ”— [Website](https://qsbench.github.io) | [Hugging Face](https://huggingface.co/QSBench) | [GitHub](https://github.com/QSBench)")

    # UI Event bindings
    ds_dropdown.change(update_explorer, [ds_dropdown, split_dropdown], [split_dropdown, explorer_df, raw_qasm_code, tr_qasm_code, meta_label])
    split_dropdown.change(update_explorer, [ds_dropdown, split_dropdown], [split_dropdown, explorer_df, raw_qasm_code, tr_qasm_code, meta_label])
    ml_ds_dropdown.change(sync_ml_metrics, [ml_ds_dropdown], [ml_feature_checks])
    run_btn.click(train_classifier, [ml_ds_dropdown, ml_feature_checks], [plot_output, results_output])
    
    # Application startup triggers
    demo.load(update_explorer, [ds_dropdown, split_dropdown], [split_dropdown, explorer_df, raw_qasm_code, tr_qasm_code, meta_label])
    demo.load(sync_ml_metrics, [ml_ds_dropdown], [ml_feature_checks])

if __name__ == "__main__":
    demo.launch()