-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_convergence.py
More file actions
234 lines (196 loc) · 10.2 KB
/
test_convergence.py
File metadata and controls
234 lines (196 loc) · 10.2 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
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
#!/usr/bin/env python3
"""
Test convergence of quantum sampling with increasing shot count.
Shows how errors decrease as 1/√N with increasing shots.
"""
import numpy as np
import matplotlib.pyplot as plt
from quantum_discrete_gaussian import QuantumDiscreteGaussian
def test_convergence():
"""Test how errors converge with increasing shot count."""
print("=" * 70)
print("CONVERGENCE TEST: Error vs Shot Count")
print("=" * 70)
print()
# Initialize
qdg = QuantumDiscreteGaussian(circuit_type='symmetric')
# Test parameters
mu_x, mu_y, mu_z = 0.1, -0.05, 0.15
T = 0.2
# Shot counts to test
shot_counts = [1000, 2000, 5000, 10000, 20000, 50000]
n_trials = 5 # Average over multiple trials
print(f"Test parameters: mu_x={mu_x}, mu_y={mu_y}, mu_z={mu_z}, T={T}")
print(f"Trials per shot count: {n_trials}")
print()
# Get theoretical values
moments_theory = qdg.compute_theoretical_moments_3d(mu_x, mu_y, mu_z, T)
# Storage for results
results = {
'shots': [],
'mean_error_x': [],
'mean_error_y': [],
'mean_error_z': [],
'mean_error_var_x': [],
'mean_error_var_y': [],
'mean_error_var_z': [],
'std_error_x': [],
'std_error_y': [],
'std_error_z': [],
'std_error_var_x': [],
'std_error_var_y': [],
'std_error_var_z': [],
'max_error_x': [],
'max_error_y': [],
'max_error_z': [],
'max_error_var_x': [],
'max_error_var_y': [],
'max_error_var_z': [],
}
print(f"{'Shots':>6s} {'E[vx] err':>10s} {'E[vy] err':>10s} {'E[vz] err':>10s} {'Expected σ':>10s} {'Time (s)':>8s}")
print("-" * 70)
import time
for shots in shot_counts:
errors_x = []
errors_y = []
errors_z = []
errors_var_x = []
errors_var_y = []
errors_var_z = []
start_time = time.time()
# Run multiple trials
for trial in range(n_trials):
velocity_counts = qdg.quantum_sample_grid_point_3d_parametric(
mu_x, mu_y, mu_z, T, shots=shots
)
moments = qdg.compute_moments_from_samples_3d(velocity_counts)
errors_x.append(abs(moments['mean_x'] - moments_theory['mean_x']))
errors_y.append(abs(moments['mean_y'] - moments_theory['mean_y']))
errors_z.append(abs(moments['mean_z'] - moments_theory['mean_z']))
errors_var_x.append(abs(moments['var_x'] - moments_theory['var_x']))
errors_var_y.append(abs(moments['var_y'] - moments_theory['var_y']))
errors_var_z.append(abs(moments['var_z'] - moments_theory['var_z']))
elapsed = time.time() - start_time
# Compute statistics
mean_err_x = np.mean(errors_x)
mean_err_y = np.mean(errors_y)
mean_err_z = np.mean(errors_z)
expected_sigma = 1.0 / np.sqrt(shots)
results['shots'].append(shots)
results['mean_error_x'].append(mean_err_x)
results['mean_error_y'].append(mean_err_y)
results['mean_error_z'].append(mean_err_z)
# Mean errors for variances (temperature)
results['mean_error_var_x'].append(np.mean(errors_var_x))
results['mean_error_var_y'].append(np.mean(errors_var_y))
results['mean_error_var_z'].append(np.mean(errors_var_z))
results['std_error_x'].append(np.std(errors_x))
results['std_error_y'].append(np.std(errors_y))
results['std_error_z'].append(np.std(errors_z))
results['std_error_var_x'].append(np.std(errors_var_x))
results['std_error_var_y'].append(np.std(errors_var_y))
results['std_error_var_z'].append(np.std(errors_var_z))
results['max_error_x'].append(np.max(errors_x))
results['max_error_y'].append(np.max(errors_y))
results['max_error_z'].append(np.max(errors_z))
results['max_error_var_x'].append(np.max(errors_var_x))
results['max_error_var_y'].append(np.max(errors_var_y))
results['max_error_var_z'].append(np.max(errors_var_z))
print(f"{shots:6d} {mean_err_x:10.6f} {mean_err_y:10.6f} {mean_err_z:10.6f} {expected_sigma:10.6f} {elapsed:8.2f}")
print()
print("=" * 70)
print("CONVERGENCE ANALYSIS")
print("=" * 70)
print()
# Check if errors scale as 1/√N
print("Scaling check (should be ~1.0 if following 1/√N):")
for i in range(1, len(shot_counts)):
ratio_shots = shot_counts[i] / shot_counts[0]
expected_ratio = np.sqrt(shot_counts[0] / shot_counts[i])
actual_ratio_x = results['mean_error_x'][i] / results['mean_error_x'][0]
actual_ratio_y = results['mean_error_y'][i] / results['mean_error_y'][0]
actual_ratio_z = results['mean_error_z'][i] / results['mean_error_z'][0]
print(f" {shot_counts[0]:5d} → {shot_counts[i]:5d} shots (√{ratio_shots:.1f} = {np.sqrt(ratio_shots):.2f}×):")
print(f" Expected: error × {expected_ratio:.3f}")
print(f" Actual: vx × {actual_ratio_x:.3f}, vy × {actual_ratio_y:.3f}, vz × {actual_ratio_z:.3f}")
print()
print("=" * 70)
print("RECOMMENDATIONS")
print("=" * 70)
print()
# Find shot count for different accuracy levels
target_errors = [0.01, 0.005, 0.002, 0.001]
print("Shot count needed for target accuracy:")
for target in target_errors:
# Use average scaling from all dimensions
avg_error_1000 = np.mean([results['mean_error_x'][0],
results['mean_error_y'][0],
results['mean_error_z'][0]])
shots_needed = int((avg_error_1000 / target) ** 2 * shot_counts[0])
print(f" Error < {target:.4f}: ~{shots_needed:,} shots")
print()
print("For your visualization (60 grid points):")
for target in [0.01, 0.005]:
avg_error_1000 = np.mean([results['mean_error_x'][0],
results['mean_error_y'][0],
results['mean_error_z'][0]])
shots_needed = int((avg_error_1000 / target) ** 2 * shot_counts[0])
time_per_point = results['shots'][0] / shot_counts[0] # Rough estimate
total_time = 60 * time_per_point * shots_needed / shot_counts[0] / 60 # minutes
print(f" Target error {target:.4f}: {shots_needed:,} shots/point, ~{total_time:.1f} min total")
# Create convergence plot
print()
print("Generating convergence plot...")
fig, axes = plt.subplots(1, 2, figsize=(14, 5))
# Plot 1: Error vs Shots (log-log)
ax = axes[0]
ax.loglog(results['shots'], results['mean_error_x'], 'o-', label='E[vx] error', linewidth=2)
ax.loglog(results['shots'], results['mean_error_y'], 's-', label='E[vy] error', linewidth=2)
ax.loglog(results['shots'], results['mean_error_z'], '^-', label='E[vz] error', linewidth=2)
# Plot temperature (variance) errors as dashed lighter markers
ax.loglog(results['shots'], results['mean_error_var_x'], 'o--', label='Var[vx] error', linewidth=1.5, alpha=0.8)
ax.loglog(results['shots'], results['mean_error_var_y'], 's--', label='Var[vy] error', linewidth=1.5, alpha=0.8)
ax.loglog(results['shots'], results['mean_error_var_z'], '^--', label='Var[vz] error', linewidth=1.5, alpha=0.8)
# Theoretical 1/√N line
theoretical_line = [1.0 / np.sqrt(n) for n in results['shots']]
ax.loglog(results['shots'], theoretical_line, 'k--', label='1/√N (theoretical)', linewidth=2, alpha=0.5)
ax.set_xlabel('Number of Shots', fontsize=12)
ax.set_ylabel('Mean Absolute Error', fontsize=12)
ax.set_title('Convergence: Error vs Shot Count', fontsize=14, fontweight='bold')
ax.legend(fontsize=10)
ax.grid(True, alpha=0.3)
# Plot 2: Error vs Shots (linear)
ax = axes[1]
ax.plot(results['shots'], results['mean_error_x'], 'o-', label='E[vx] error', linewidth=2, markersize=8)
ax.plot(results['shots'], results['mean_error_y'], 's-', label='E[vy] error', linewidth=2, markersize=8)
ax.plot(results['shots'], results['mean_error_z'], '^-', label='E[vz] error', linewidth=2, markersize=8)
ax.plot(results['shots'], results['mean_error_var_x'], 'o--', label='Var[vx] error', linewidth=1.5, markersize=7, alpha=0.9)
ax.plot(results['shots'], results['mean_error_var_y'], 's--', label='Var[vy] error', linewidth=1.5, markersize=7, alpha=0.9)
ax.plot(results['shots'], results['mean_error_var_z'], '^--', label='Var[vz] error', linewidth=1.5, markersize=7, alpha=0.9)
ax.axhline(y=0.01, color='orange', linestyle='--', alpha=0.5, label='0.01 threshold')
ax.axhline(y=0.005, color='red', linestyle='--', alpha=0.5, label='0.005 threshold')
ax.set_xlabel('Number of Shots', fontsize=12)
ax.set_ylabel('Mean Absolute Error', fontsize=12)
ax.set_title('Error Reduction with More Shots', fontsize=14, fontweight='bold')
ax.legend(fontsize=10)
ax.grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig('convergence_analysis.png', dpi=300, bbox_inches='tight')
print("✓ Plot saved: convergence_analysis.png")
print()
print("=" * 70)
print("CONCLUSION")
print("=" * 70)
print()
print(f"• Errors follow 1/√N scaling as expected ✓")
print(f"• At 1000 shots: error ~{np.mean([results['mean_error_x'][0], results['mean_error_y'][0], results['mean_error_z'][0]]):.4f}")
print(f"• At 10000 shots: error ~{np.mean([results['mean_error_x'][3], results['mean_error_y'][3], results['mean_error_z'][3]]):.4f}")
print(f"• At 1000 shots (variance): temp error ~{np.mean([results['mean_error_var_x'][0], results['mean_error_var_y'][0], results['mean_error_var_z'][0]]):.4f}")
print(f"• At 10000 shots: error ~{np.mean([results['mean_error_x'][3], results['mean_error_y'][3], results['mean_error_z'][3]]):.4f}")
print(f"• At 50000 shots: error ~{np.mean([results['mean_error_x'][-1], results['mean_error_y'][-1], results['mean_error_z'][-1]]):.4f}")
print()
print("For best visualization quality with reasonable time:")
print(" → Use 15000-20000 shots per point")
print()
if __name__ == "__main__":
test_convergence()