Skip to content

Commit 0be87e1

Browse files
committed
example notebook to show effect of error vs sigma
1 parent 409001c commit 0be87e1

File tree

1 file changed

+173
-0
lines changed

1 file changed

+173
-0
lines changed
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "c9f41725",
6+
"metadata": {},
7+
"source": [
8+
"# Isotropic Error Perturbation\n",
9+
"\n",
10+
"In this notebook, we see the effect of adding isotropic errors to a given quantum statevector. We quantify the effect of the perturbation by measuring the overlap fidelity between the original state (an equal superposition of computational basis states) and the final state (after adding the isotropic error). We then compare the fidelities for different values of $\\sigma$."
11+
]
12+
},
13+
{
14+
"cell_type": "markdown",
15+
"id": "67e20480",
16+
"metadata": {},
17+
"source": [
18+
"## Imports"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": null,
24+
"id": "9cd270d6",
25+
"metadata": {},
26+
"outputs": [],
27+
"source": [
28+
"import jax\n",
29+
"import jax.numpy as jnp\n",
30+
"import matplotlib.pyplot as plt\n",
31+
"\n",
32+
"from isotropic.e2 import F_j, get_e2_coeffs\n",
33+
"from isotropic.orthonormal import get_orthonormal_basis\n",
34+
"from isotropic.thetazero import get_theta_zero\n",
35+
"from isotropic.utils.distribution import normal_integrand\n",
36+
"from isotropic.utils.state_transforms import (\n",
37+
" add_isotropic_error,\n",
38+
" hypersphere_to_statevector,\n",
39+
" statevector_to_hypersphere,\n",
40+
")\n",
41+
"from joblib import Parallel, delayed"
42+
]
43+
},
44+
{
45+
"cell_type": "markdown",
46+
"id": "40e43743",
47+
"metadata": {},
48+
"source": [
49+
"## Initial values & one-time computations"
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"execution_count": null,
55+
"id": "86259315",
56+
"metadata": {},
57+
"outputs": [],
58+
"source": [
59+
"Phi_original = jnp.ones(16, dtype=complex)\n",
60+
"Phi_original = Phi_original / jnp.linalg.norm(Phi_original)\n",
61+
"Phi_spherical = statevector_to_hypersphere(Phi_original)\n",
62+
"basis = get_orthonormal_basis(Phi_spherical) # gives d vectors with d+1 elements each\n",
63+
"_, coeffs = get_e2_coeffs(\n",
64+
" d=basis.shape[0], # gives d coefficients for the d vectors above\n",
65+
" F_j=F_j,\n",
66+
" key=jax.random.PRNGKey(0),\n",
67+
")\n",
68+
"e2 = jnp.expand_dims(coeffs, axis=-1) * basis"
69+
]
70+
},
71+
{
72+
"cell_type": "markdown",
73+
"id": "24133358",
74+
"metadata": {},
75+
"source": [
76+
"## Helper functions\n",
77+
"\n",
78+
"### Generate a perturbed state"
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": null,
84+
"id": "30cfe587",
85+
"metadata": {},
86+
"outputs": [],
87+
"source": [
88+
"def get_perturbed_state(sigma: float = 0.9) -> jnp.ndarray:\n",
89+
" def g(theta):\n",
90+
" return normal_integrand(theta, d=Phi_spherical.shape[0], sigma=sigma)\n",
91+
"\n",
92+
" theta_zero = get_theta_zero(x=0.5, g=g)\n",
93+
" Psi_spherical = add_isotropic_error(Phi_spherical, e2=e2, theta_zero=theta_zero)\n",
94+
" Psi = hypersphere_to_statevector(Psi_spherical)\n",
95+
" return Psi"
96+
]
97+
},
98+
{
99+
"cell_type": "markdown",
100+
"id": "c55cf3ba",
101+
"metadata": {},
102+
"source": [
103+
"### Calculate Fidelity"
104+
]
105+
},
106+
{
107+
"cell_type": "code",
108+
"execution_count": null,
109+
"id": "798b005e",
110+
"metadata": {},
111+
"outputs": [],
112+
"source": [
113+
"def get_fidelity(sigma: float = 0.9) -> float:\n",
114+
" Psi = get_perturbed_state(sigma=sigma)\n",
115+
" fidelity = jnp.abs(jnp.vdot(Phi_original, Psi)) ** 2\n",
116+
" return fidelity"
117+
]
118+
},
119+
{
120+
"cell_type": "markdown",
121+
"id": "87bde394",
122+
"metadata": {},
123+
"source": [
124+
"## Final results"
125+
]
126+
},
127+
{
128+
"cell_type": "code",
129+
"execution_count": null,
130+
"id": "8af76fbe",
131+
"metadata": {},
132+
"outputs": [],
133+
"source": [
134+
"sigmas = jnp.linspace(0.9, 0.99, 100)\n",
135+
"fidelities = Parallel(n_jobs=4)(delayed(get_fidelity)(sigma) for sigma in sigmas)"
136+
]
137+
},
138+
{
139+
"cell_type": "code",
140+
"execution_count": null,
141+
"id": "45a141e8",
142+
"metadata": {},
143+
"outputs": [],
144+
"source": [
145+
"plt.plot(sigmas, fidelities)\n",
146+
"plt.xlabel(\"Sigma\")\n",
147+
"plt.ylabel(\"Fidelity\")\n",
148+
"plt.show()"
149+
]
150+
}
151+
],
152+
"metadata": {
153+
"kernelspec": {
154+
"display_name": ".venv",
155+
"language": "python",
156+
"name": "python3"
157+
},
158+
"language_info": {
159+
"codemirror_mode": {
160+
"name": "ipython",
161+
"version": 3
162+
},
163+
"file_extension": ".py",
164+
"mimetype": "text/x-python",
165+
"name": "python",
166+
"nbconvert_exporter": "python",
167+
"pygments_lexer": "ipython3",
168+
"version": "3.11.13"
169+
}
170+
},
171+
"nbformat": 4,
172+
"nbformat_minor": 5
173+
}

0 commit comments

Comments
 (0)