Simulation & Monte Carlo API
simulation
╔══════════════════════════════════════════════════════════════════════════════╗ ║ MHRQI - Monte Carlo Simulation Backend ║ ║ Efficient shot-based sampling for hierarchical quantum image processing ║ ║ ║ ║ Author: Keno S. Jose ║ ║ License: Apache 2.0 ║ ╚══════════════════════════════════════════════════════════════════════════════╝
HierarchicalMeasurementAggregator
Aggregates Monte Carlo measurements across hierarchical levels, computing statistics for binning and reconstruction.
Source code in mhrqi/core/simulation.py
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 | |
__init__(hierarchical_coord_matrix, bit_depth=8)
Initialize aggregator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hierarchical_coord_matrix
|
list
|
List of HCV vectors (one per pixel). |
required |
bit_depth
|
int
|
Bits per intensity value. |
8
|
Source code in mhrqi/core/simulation.py
196 197 198 199 200 201 202 203 204 205 206 | |
aggregate(measurement_counts)
Aggregate measurement outcomes into hierarchical bins.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
measurement_counts
|
Dict[str, int]
|
Dictionary from Monte Carlo sampling. |
required |
Source code in mhrqi/core/simulation.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
confidence_weighted_value(pos_bits, context_value=None)
Compute confidence-weighted reconstruction value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pos_bits
|
str
|
Position bits. |
required |
context_value
|
float
|
Value from hierarchical context (sibling average, etc.). |
None
|
Returns:
| Type | Description |
|---|---|
float
|
Blended reconstruction value. |
Source code in mhrqi/core/simulation.py
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 | |
get_statistics(pos_bits)
Compute statistics for a hierarchical position.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pos_bits
|
str
|
Position bits (binary string). |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, float]
|
Dictionary with mean, std, median, mode. |
Source code in mhrqi/core/simulation.py
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 | |
MonteCarloSimulator
Efficient Monte Carlo sampler for MHRQI quantum simulations.
Provides configurable shot-based sampling with GPU acceleration support, statistical aggregation, and reproducibility via seed management.
Source code in mhrqi/core/simulation.py
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 | |
gpu_available
property
Check if GPU acceleration is available.
__init__(seed=None, use_gpu=True)
Initialize Monte Carlo simulator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
Optional[int]
|
Random seed for reproducibility. If None, uses system entropy. |
None
|
use_gpu
|
bool
|
Attempt GPU acceleration via cuStateVec if available. |
True
|
Source code in mhrqi/core/simulation.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
adaptive_shot_allocation(statevector, target_error=0.01, max_shots=100000)
Determine minimum shots needed to achieve target error via binary search.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
statevector
|
ndarray
|
Full quantum state vector. |
required |
target_error
|
float
|
Target KL divergence. |
0.01
|
max_shots
|
int
|
Maximum shots to try. |
100000
|
Returns:
| Type | Description |
|---|---|
int
|
Recommended shot count. |
Source code in mhrqi/core/simulation.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
bootstrap_statistics(samples, n_bootstraps=1000, confidence=0.95)
Compute bootstrap confidence intervals for measurement statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
samples
|
ndarray
|
Measured sample values. |
required |
n_bootstraps
|
int
|
Number of bootstrap resamples. |
1000
|
confidence
|
float
|
Confidence level (e.g., 0.95 for 95% CI). |
0.95
|
Returns:
| Type | Description |
|---|---|
Dict[str, float]
|
Dictionary with mean, std, CI_lower, CI_upper. |
Source code in mhrqi/core/simulation.py
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 | |
estimate_convergence(statevector, max_shots=10000, step_size=100)
Estimate Monte Carlo convergence by increasing shot count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
statevector
|
ndarray
|
Full quantum state vector. |
required |
max_shots
|
int
|
Maximum number of shots to simulate. |
10000
|
step_size
|
int
|
Shots per convergence step. |
100
|
Returns:
| Type | Description |
|---|---|
Dict[int, float]
|
Dictionary mapping shot counts to estimated errors. |
Source code in mhrqi/core/simulation.py
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 | |
sample_statevector(statevector, shots, measured_qubits=None)
Sample measurement outcomes from statevector via Monte Carlo method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
statevector
|
ndarray
|
Full quantum state vector (shape: 2^n_qubits). |
required |
shots
|
int
|
Number of measurement repetitions. |
required |
measured_qubits
|
Optional[list]
|
Indices of qubits to measure. If None, measures all. |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, int]
|
Dictionary mapping measurement outcomes (binary strings) to counts. |
Source code in mhrqi/core/simulation.py
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 | |
configure_backend(use_gpu=True, seed=None)
Factory function to configure optimal simulation backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
use_gpu
|
bool
|
Attempt GPU acceleration. |
True
|
seed
|
Optional[int]
|
Random seed. |
None
|
Returns:
| Type | Description |
|---|---|
MonteCarloSimulator
|
Configured MonteCarloSimulator instance. |
Source code in mhrqi/core/simulation.py
288 289 290 291 292 293 294 295 296 297 298 299 | |
monte_carlo
╔══════════════════════════════════════════════════════════════════════════════╗ ║ MHRQI - Monte Carlo Utilities ║ ║ Helper functions for Monte Carlo sampling and statistical analysis ║ ║ ║ ║ Author: Keno S. Jose ║ ║ License: Apache 2.0 ║ ╚══════════════════════════════════════════════════════════════════════════════╝
aggregate_multi_run_results(results, method='average')
Aggregate multiple Monte Carlo runs for improved statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
List[Dict[int, int]]
|
List of count dictionaries from separate runs. |
required |
method
|
str
|
'average' (mean estimates) or 'pooled' (combine all samples). |
'average'
|
Returns:
| Type | Description |
|---|---|
Dict[int, float]
|
Aggregated probability estimates. |
Source code in mhrqi/utils/monte_carlo.py
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 | |
auto_correlate_length(samples, max_lag=None)
Estimate autocorrelation length to assess sampling efficiency.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
samples
|
ndarray
|
Time series of measurements. |
required |
max_lag
|
int
|
Maximum lag to check. If None, uses 10% of series length. |
None
|
Returns:
| Type | Description |
|---|---|
float
|
Autocorrelation length (ACL). |
Source code in mhrqi/utils/monte_carlo.py
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 | |
compute_statistical_error(samples, confidence=0.95)
Compute mean, standard error, and confidence interval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
samples
|
ndarray
|
Measurement samples. |
required |
confidence
|
float
|
Confidence level. |
0.95
|
Returns:
| Type | Description |
|---|---|
Tuple[float, float, float]
|
Tuple of (mean, std_error, ci_half_width). |
Source code in mhrqi/utils/monte_carlo.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | |
effective_sample_size(weights)
Compute effective sample size for importance-weighted samples.
ESS = (∑ w_i)² / ∑ w_i²
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weights
|
ndarray
|
Importance weights. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Effective sample size. |
Source code in mhrqi/utils/monte_carlo.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
estimate_required_shots(target_accuracy=0.95, target_error=0.05, num_outcomes=None)
Estimate Monte Carlo shots needed for target accuracy.
Uses Hoeffding inequality: P(|hat_p - p| > ε) ≤ 2 * exp(-2 * n * ε^2)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_accuracy
|
float
|
Desired confidence level (e.g., 0.95). |
0.95
|
target_error
|
float
|
Maximum error tolerance (e.g., 0.05). |
0.05
|
num_outcomes
|
int
|
Number of possible measurement outcomes. If None, computes conservative estimate. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Recommended number of shots. |
Source code in mhrqi/utils/monte_carlo.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
importance_sampling(target_probs, proposal_probs, shots, rng=None)
Importance sampling to estimate target distribution from proposal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_probs
|
ndarray
|
Target probability distribution. |
required |
proposal_probs
|
ndarray
|
Proposal (sampling) distribution. |
required |
shots
|
int
|
Number of proposal samples. |
required |
rng
|
RandomState
|
Random state for reproducibility. |
None
|
Returns:
| Type | Description |
|---|---|
Dict[int, float]
|
Dictionary of weighted estimates. |
Source code in mhrqi/utils/monte_carlo.py
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 | |
shot_count_for_precision(std_dev, target_uncertainty, confidence=0.95)
Determine shots needed for desired measurement precision.
Uses standard error formula: SE = σ / √n
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
std_dev
|
float
|
Estimated standard deviation of measurements. |
required |
target_uncertainty
|
float
|
Desired standard error. |
required |
confidence
|
float
|
Confidence level (affects z-score). |
0.95
|
Returns:
| Type | Description |
|---|---|
int
|
Recommended number of shots. |
Source code in mhrqi/utils/monte_carlo.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | |
stratified_sampling(probabilities, shots, rng=None)
Stratified Monte Carlo sampling to reduce variance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
probabilities
|
ndarray
|
Probability distribution over outcomes. |
required |
shots
|
int
|
Number of shots. |
required |
rng
|
RandomState
|
Random state for reproducibility. |
None
|
Returns:
| Type | Description |
|---|---|
Dict[int, int]
|
Dictionary mapping outcomes to counts. |
Source code in mhrqi/utils/monte_carlo.py
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 | |
visualize_convergence(convergence_data, metric_name='KL Divergence')
Generate ASCII plot of Monte Carlo convergence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
convergence_data
|
Dict[int, float]
|
Dictionary mapping shot counts to error values. |
required |
metric_name
|
str
|
Name of metric being tracked. |
'KL Divergence'
|
Returns:
| Type | Description |
|---|---|
str
|
ASCII plot string. |
Source code in mhrqi/utils/monte_carlo.py
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 | |