utils
This module contains miscellaneous utilities useful for various steps involved in the isotropic error generation.
isotropic.utils
bisection
This module contains functions for the bisection algorithm to calculate $F^{-1}$.
get_theta(F, a, b, x, eps)
Find the value of theta such that $F(\theta) = x$ using the bisection method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
F
|
Callable
|
Function for which to compute the inverse. |
required |
a
|
float
|
Lower bound of the interval. |
required |
b
|
float
|
Upper bound of the interval. |
required |
x
|
float | ArrayLike
|
Value for which to find the inverse. |
required |
eps
|
float
|
Tolerance for convergence. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The value of $theta$ such that $F(\theta) = x$. |
Notes
This function assumes that $F$ is an increasing function in the interval $[a, b]$ and that $F(a) \leq x \leq F(b)$. The bisection method is a root-finding method that repeatedly bisects an interval and then selects a subinterval in which a root exists.
Source code in src/isotropic/utils/bisection.py
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 | |
data_generation
This module generates data for Grover's algorithm with isotropic error.
cli()
Command-line interface for data generation.
Source code in src/isotropic/utils/data_generation.py
415 416 417 418 419 420 421 422 | |
generate_data(min_qubits, max_qubits, min_iterations, max_iterations, min_sigma=None, max_sigma=None, num_sigma_points=2, sigma_values=None, data_dir='data', random_key=42, n_samples=1)
Generate data for Grover's algorithm with isotropic error and save to xarray files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min_qubits
|
int
|
Minimum number of qubits. |
required |
max_qubits
|
int
|
Maximum number of qubits. |
required |
min_iterations
|
int
|
Minimum number of Grover iterations to simulate. |
required |
max_iterations
|
int
|
Maximum number of Grover iterations to simulate. |
required |
min_sigma
|
float
|
Minimum sigma value for isotropic error. Required if sigma_values is not provided. |
None
|
max_sigma
|
float
|
Maximum sigma value for isotropic error. Required if sigma_values is not provided. |
None
|
num_sigma_points
|
int
|
Number of sigma points to evaluate between min_sigma and max_sigma. Default is 2. |
2
|
sigma_values
|
list[float]
|
Explicit list of sigma values. If provided, min_sigma/max_sigma/num_sigma_points are ignored. |
None
|
data_dir
|
str
|
Directory to save the generated data files. Default is "data". |
'data'
|
random_key
|
int
|
Integer seed for the JAX PRNG root key. Default is 42. |
42
|
n_samples
|
int
|
Number of independent error samples to average per |
1
|
Returns:
| Type | Description |
|---|---|
None
|
Saves the generated data to xarray files. |
Notes
We implement n_samples averaging via vmap to eliminate single-sample Monte
Carlo noise which introduces unexpected variance in the results. This is crucial
for generating smooth, monotonic results based on scaling of sigma.
Source code in src/isotropic/utils/data_generation.py
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 | |
run_experiment_batch(Phi_batch, marked_item, sigmas, gate_counts, random_key=42, n_samples=1)
Run batched experiment: vmap over iterations and sigmas.
For a fixed num_qubits all statevectors share the same shape, so the pure-JAX computation is vmapped over the iteration dimension (Phi_batch) and the sigma dimension in a single JIT-compiled XLA program.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Phi_batch
|
Array
|
Stack of complex statevectors, shape |
required |
marked_item
|
str
|
The marked item to search for in binary string format. |
required |
sigmas
|
Array
|
Sigma values to evaluate, shape |
required |
gate_counts
|
Array
|
Total gate counts excluding barriers for each iteration, shape |
required |
random_key
|
int
|
Integer seed for the JAX PRNG root key. Default is 42. |
42
|
n_samples
|
int
|
Number of independent error samples to average per |
1
|
Returns:
| Type | Description |
|---|---|
Array
|
Success probabilities, shape |
Notes
We use an error model of per-gate error, implying the effective sigma after k gates is sigma^k. The gate_counts array is used to scale the sigma values for each iteration accordingly.
We implement n_samples averaging via vmap to eliminate single-sample Monte
Carlo noise which introduces unexpected variance in the results. This is crucial
for generating smooth, monotonic results based on scaling of sigma.
Source code in src/isotropic/utils/data_generation.py
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 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | |
distribution
This module contains functions for relevant probability distributions.
double_factorial_jax(n)
Helper function to compute double factorial.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
The integer for which to compute the double factorial. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
The value of the double factorial n!! as a JAX array. |
Notes
The double factorial is defined as:
n!! = n * (n-2) * (n-4) * ... * 1 (if n is odd) or 2 (if n is even).
Source code in src/isotropic/utils/distribution.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
double_factorial_ratio(num, den)
Compute the ratio of double factorials num!! / den!! .
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num
|
int
|
The numerator double factorial. |
required |
den
|
int
|
The denominator double factorial. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The ratio num!! / den!! . |
Source code in src/isotropic/utils/distribution.py
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 | |
normal_integrand(theta, d, sigma, log_factorial_ratio=None)
Compute the function g(θ).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta
|
float
|
Angle parameter(s). |
required |
d
|
int
|
Dimension parameter. |
required |
sigma
|
float
|
Sigma parameter (should be in valid range). |
required |
log_factorial_ratio
|
float
|
Precomputed value of |
None
|
Returns:
| Type | Description |
|---|---|
Array
|
Value(s) of the function evaluated at |
Notes
g(θ) is integrated to calculate F(θ) which is the distribution function for the angle θ in a normal distribution:
$$g(\theta) = \frac{(d-1)!! \times (1-\sigma^2) \times \sin^{d-1}(\theta)}{\pi \times (d-2)!! \times (1+\sigma^2-2\sigma\cos(\theta))^{(d+1)/2}}$$.
Source code in src/isotropic/utils/distribution.py
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 | |
linalg
Linear algebra utilities for isotropic error analysis, implemented using JAX.
jax_null_space(A)
Compute the null space of a matrix $A$ using JAX.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
ArrayLike
|
The input matrix for which to compute the null space. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
The basis vectors of the null space of A. |
Notes
See also:
scipy.linalg.null_spacefor the reference implementation in SciPy.- https://github.com/jax-ml/jax/pull/14486 for an old JAX implementation.
Source code in src/isotropic/utils/linalg.py
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 | |
simpsons
This module contains functions for estimating the integral of a function using Simpson's rule.
simpsons_rule(f, a, b, C, tol)
Estimate the integral of a function using Simpson's rule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable
|
Function to integrate. |
required |
a
|
float
|
Lower limit of integration. |
required |
b
|
float
|
Upper limit of integration. |
required |
C
|
float
|
Bound on 4th derivative of f. |
required |
tol
|
float
|
Desired tolerance for the integral estimate. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Estimated value of the integral. |
Source code in src/isotropic/utils/simpsons.py
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 | |
state_transforms
This module contains functions for transforming the quantum state.
add_isotropic_error(Phi_sp, e2, theta_zero)
Add isotropic error to state $\Phi$ given $e_2$ and $\theta_0$.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Phi_sp
|
ArrayLike
|
State to which isotropic error is added (in spherical form). |
required |
e2
|
ArrayLike
|
Vector $e_2$ in $S_{d-1}$ with uniform distribution. |
required |
theta_zero
|
float
|
Angle $\theta_0$ in $[0,\pi]$ with density function $f(\theta_0)$. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Statevector in spherical form after adding isotropic error. |
Source code in src/isotropic/utils/state_transforms.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | |
generate_and_add_isotropic_error(Phi, sigma=0.9, key=random.PRNGKey(0))
Generate and add isotropic error to a given statevector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Phi
|
ArrayLike
|
The input statevector as a complex JAX array of dimension $2^n$, for n-qubits. |
required |
sigma
|
float
|
The standard deviation for the isotropic error, by default 0.9. |
0.9
|
key
|
ArrayLike
|
Random key for reproducibility, by default random.PRNGKey(0). |
PRNGKey(0)
|
Returns:
| Type | Description |
|---|---|
Array
|
The perturbed statevector after adding isotropic error. |
Source code in src/isotropic/utils/state_transforms.py
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 | |
hypersphere_to_statevector(S)
Generate the statevector $\Phi$ from hypersphere $S$.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
S
|
ArrayLike
|
Hypersphere as a real JAX array of dimension $2^{n+1}$ for n qubits. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Statevector as a complex JAX array of dimension $2^n$. |
Source code in src/isotropic/utils/state_transforms.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
statevector_to_hypersphere(Phi)
Generate the hypersphere from statevector $\Phi$.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Phi
|
ArrayLike
|
Statevector as a complex JAX array of dimension $2^n$, for n-qubits. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Hypersphere as a real JAX array of dimension $2^{n+1}$. |
Source code in src/isotropic/utils/state_transforms.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |