Repository navigation
Expand file tree
/
Copy pathquickstart.py
More file actions
112 lines (87 loc) · 3.9 KB
/
Copy pathquickstart.py
File metadata and controls
112 lines (87 loc) · 3.9 KB
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
"""
Quick Start Script for QPoland Hackathon Challenge
Demonstrates the complete pipeline on MUTAG dataset.
Run this first to validate your setup!
"""
import sys
from pathlib import Path
import logging
import numpy as np
# Add qkernels to path
sys.path.append(str(Path(__file__).parent))
from qkernels.datasets import MolecularGraphDataset
from qkernels.features import (
TopologicalFeatureExtractor,
CTQWFeatureExtractor,
HybridFeatureExtractor
)
from qkernels.kernels import KernelSVM
from qkernels.eval import ClassificationEvaluator
from qkernels.utils import setup_logging, print_results_table
def main():
"""Run quick start demo."""
# Setup logging
setup_logging(logging.INFO)
logger = logging.getLogger(__name__)
logger.info("=" * 80)
logger.info("QPoland Quantum Hackathon - Quick Start Validation")
logger.info("=" * 80)
# 1. Load MUTAG dataset
logger.info("\nStep 1: Loading MUTAG dataset...")
dataset = MolecularGraphDataset('MUTAG', data_dir='data')
graphs, labels = dataset.get_graphs_and_labels()
dataset.summary()
# 2. Extract features
logger.info("\nStep 2: Extracting features...")
# Try different feature extractors
extractors = {
'Topological': TopologicalFeatureExtractor(),
'CTQW': CTQWFeatureExtractor(gamma=1.0, time_points=[0.5, 1.0, 2.0]),
'Hybrid': HybridFeatureExtractor()
}
results = {}
for name, extractor in extractors.items():
logger.info(f"\n--- Testing {name} Features ---")
try:
# Extract features
scaler = extractor.fit_scaler(graphs)
features = np.array([extractor.extract_features(g) for g in graphs])
features_scaled = scaler.transform(features)
logger.info(f"Feature shape: {features_scaled.shape}")
logger.info(f"Number of features: {len(extractor.get_feature_names())}")
# 3. Train and evaluate SVM with RBF kernel
logger.info(f"\nStep 3: Training SVM with RBF kernel...")
model = KernelSVM(kernel='rbf', C=1.0, gamma='scale')
evaluator = ClassificationEvaluator(cv_folds=10, random_state=42)
cv_results = evaluator.cross_validate(
model, features_scaled, np.array(labels),
model_name=f"{name}_RBF"
)
results[name] = cv_results
logger.info(f"Results for {name} Features:")
logger.info(f" Accuracy: {cv_results['accuracy_mean']:.4f} ± {cv_results['accuracy_std']:.4f}")
logger.info(f" F1-Score: {cv_results['f1_mean']:.4f} ± {cv_results['f1_std']:.4f}")
except Exception as e:
logger.error(f"Error with {name} Features: {e}")
continue
# 4. Print summary
logger.info("\n" + "=" * 80)
logger.info("QUICK START VALIDATION COMPLETED")
logger.info("=" * 80)
if results:
print_results_table(results)
# Find best method
best_method = max(results.items(), key=lambda x: x[1]['accuracy_mean'])
logger.info(f"\nBest Method: {best_method[0]}")
logger.info(f" Accuracy: {best_method[1]['accuracy_mean']:.4f} ± {best_method[1]['accuracy_std']:.4f}")
logger.info(f" F1-Score: {best_method[1]['f1_mean']:.4f} ± {best_method[1]['f1_std']:.4f}")
logger.info("\n" + "=" * 80)
logger.info("Next Steps:")
logger.info("1. Run on all datasets: python experiments/run_full_benchmark.py")
logger.info("2. Try quantum kernels: python experiments/run_cv.py --dataset MUTAG --feature quantum --kernel quantum")
logger.info("3. Optimize hyperparameters for best performance")
logger.info("4. Generate visualizations and write report")
logger.info("=" * 80)
return results
if __name__ == "__main__":
results = main()