Spaces:
Configuration error
Configuration error
File size: 10,619 Bytes
50cdd10 |
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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# -*- coding: utf-8 -*-
"""
Quantum Backend Comparison Module
Compares Russian vs IBM quantum backends on multilingual edit reliability
"""
import time
import numpy as np
from typing import Dict, List, Optional, Tuple
from dataclasses import dataclass, asdict
import json
@dataclass
class BackendMetrics:
"""Metrics for backend performance"""
backend_name: str
edit_success_rate: float
hallucination_rate: float
correction_efficiency: float
avg_latency_ms: float
circuit_fidelity: float
throughput_edits_per_sec: float
language_accuracy: Dict[str, float]
domain_performance: Dict[str, float]
class QuantumBackendComparator:
"""Compare Russian and IBM quantum backends"""
def __init__(
self,
backends: List[str] = None,
languages: List[str] = None,
domains: List[str] = None
):
"""
Initialize backend comparator
Args:
backends: List of backend names ['russian', 'ibm']
languages: List of languages to test
domains: List of domains to test
"""
self.backends = backends or ['russian', 'ibm']
self.languages = languages or ['en', 'ru', 'es', 'fr', 'de', 'zh', 'ar']
self.domains = domains or ['code', 'math', 'text', 'scientific', 'legal', 'medical']
# Backend configurations
self.backend_configs = {
'russian': {
'base_fidelity': 0.92,
'base_latency': 85,
'cyrillic_boost': 0.05
},
'ibm': {
'base_fidelity': 0.94,
'base_latency': 92,
'cyrillic_boost': 0.0
}
}
self.results = {}
def compare_backends(
self,
edit_stream: List[Dict],
metrics: List[str] = None
) -> Dict[str, BackendMetrics]:
"""
Compare backends on edit stream
Args:
edit_stream: List of edits to process
metrics: Metrics to track
Returns:
Dict mapping backend names to metrics
"""
metrics = metrics or ['success_rate', 'latency', 'fidelity']
results = {}
for backend in self.backends:
print(f"\n🔬 Benchmarking {backend.upper()} backend...")
backend_metrics = self._benchmark_backend(backend, edit_stream)
results[backend] = backend_metrics
print(f"✓ {backend}: Success={backend_metrics.edit_success_rate:.1%}, "
f"Latency={backend_metrics.avg_latency_ms:.1f}ms")
self.results = results
return results
def _benchmark_backend(
self,
backend: str,
edit_stream: List[Dict]
) -> BackendMetrics:
"""Benchmark single backend"""
config = self.backend_configs.get(backend, self.backend_configs['ibm'])
# Process edits
successful_edits = 0
hallucinated_edits = 0
corrected_edits = 0
latencies = []
language_stats = {lang: {'total': 0, 'success': 0} for lang in self.languages}
domain_stats = {domain: {'total': 0, 'success': 0} for domain in self.domains}
for edit in edit_stream:
start_time = time.time()
# Simulate edit processing
lang = edit.get('lang', 'en')
domain = edit.get('domain', 'text')
# Calculate success probability
base_success = 0.85
if backend == 'russian' and lang == 'ru':
base_success += config['cyrillic_boost']
# Add noise
success = np.random.rand() < base_success
hallucinated = np.random.rand() < 0.08
corrected = hallucinated and (np.random.rand() < 0.92)
if success:
successful_edits += 1
if hallucinated:
hallucinated_edits += 1
if corrected:
corrected_edits += 1
# Track language stats
if lang in language_stats:
language_stats[lang]['total'] += 1
if success:
language_stats[lang]['success'] += 1
# Track domain stats
if domain in domain_stats:
domain_stats[domain]['total'] += 1
if success:
domain_stats[domain]['success'] += 1
# Simulate latency
latency = config['base_latency'] + np.random.normal(0, 10)
latencies.append(max(latency, 10)) # Min 10ms
time.sleep(0.001) # Small delay for realism
# Calculate metrics
total_edits = len(edit_stream)
edit_success_rate = successful_edits / total_edits if total_edits > 0 else 0
hallucination_rate = hallucinated_edits / total_edits if total_edits > 0 else 0
correction_efficiency = corrected_edits / hallucinated_edits if hallucinated_edits > 0 else 1.0
avg_latency = np.mean(latencies) if latencies else 0
throughput = 1000 / avg_latency if avg_latency > 0 else 0
# Language accuracy
language_accuracy = {
lang: stats['success'] / stats['total'] if stats['total'] > 0 else 0
for lang, stats in language_stats.items()
}
# Domain performance
domain_performance = {
domain: stats['success'] / stats['total'] if stats['total'] > 0 else 0
for domain, stats in domain_stats.items()
}
return BackendMetrics(
backend_name=backend,
edit_success_rate=edit_success_rate,
hallucination_rate=hallucination_rate,
correction_efficiency=correction_efficiency,
avg_latency_ms=avg_latency,
circuit_fidelity=config['base_fidelity'],
throughput_edits_per_sec=throughput,
language_accuracy=language_accuracy,
domain_performance=domain_performance
)
def quick_compare(self, num_edits: int = 100) -> Dict:
"""Quick comparison with synthetic data"""
# Generate synthetic edit stream
edit_stream = []
for i in range(num_edits):
edit_stream.append({
'id': f'edit_{i}',
'lang': np.random.choice(self.languages),
'domain': np.random.choice(self.domains),
'code': f'sample_code_{i}'
})
return self.compare_backends(edit_stream)
def generate_report(
self,
results: Dict[str, BackendMetrics] = None,
output: str = 'backend_comparison.html'
):
"""Generate comparison report"""
results = results or self.results
if not results:
print("⚠️ No results to report. Run compare_backends() first.")
return
# Generate HTML report
html = self._generate_html_report(results)
with open(output, 'w', encoding='utf-8') as f:
f.write(html)
print(f"✓ Report generated: {output}")
def _generate_html_report(self, results: Dict[str, BackendMetrics]) -> str:
"""Generate HTML report"""
html = """
<!DOCTYPE html>
<html>
<head>
<title>Backend Comparison Report</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
table { border-collapse: collapse; width: 100%; margin: 20px 0; }
th, td { border: 1px solid #ddd; padding: 12px; text-align: left; }
th { background-color: #4CAF50; color: white; }
tr:nth-child(even) { background-color: #f2f2f2; }
.metric { font-weight: bold; }
.good { color: green; }
.warning { color: orange; }
.bad { color: red; }
</style>
</head>
<body>
<h1>Quantum Backend Comparison Report</h1>
<h2>Performance Metrics</h2>
<table>
<tr>
<th>Metric</th>
"""
for backend in results.keys():
html += f" <th>{backend.upper()}</th>\n"
html += " </tr>\n"
# Add metrics rows
metrics_to_show = [
('Edit Success Rate', 'edit_success_rate', '%'),
('Hallucination Rate', 'hallucination_rate', '%'),
('Correction Efficiency', 'correction_efficiency', '%'),
('Avg Latency', 'avg_latency_ms', 'ms'),
('Circuit Fidelity', 'circuit_fidelity', ''),
('Throughput', 'throughput_edits_per_sec', 'edits/s')
]
for metric_name, metric_key, unit in metrics_to_show:
html += f" <tr>\n <td class='metric'>{metric_name}</td>\n"
for backend_metrics in results.values():
value = getattr(backend_metrics, metric_key)
if unit == '%':
formatted = f"{value*100:.1f}%"
elif unit == 'ms':
formatted = f"{value:.1f}{unit}"
else:
formatted = f"{value:.2f}{unit}"
html += f" <td>{formatted}</td>\n"
html += " </tr>\n"
html += """
</table>
<p><em>Generated by Quantum LIMIT-GRAPH v2.4.0</em></p>
</body>
</html>
"""
return html
def export_results(self, filepath: str = 'backend_results.json'):
"""Export results to JSON"""
if not self.results:
print("⚠️ No results to export.")
return
export_data = {
backend: asdict(metrics)
for backend, metrics in self.results.items()
}
with open(filepath, 'w') as f:
json.dump(export_data, f, indent=2)
print(f"✓ Results exported: {filepath}")
# Convenience function
def quick_benchmark(backends: List[str] = None, num_edits: int = 100) -> Dict:
"""Quick benchmark comparison"""
comparator = QuantumBackendComparator(backends=backends)
return comparator.quick_compare(num_edits=num_edits)
|