Skip to content

Commit 852e858

Browse files
DmitryVasilevskyDmitry Vasilevsky
andauthored
Added sample notebook with noise (#1980)
Demonstrates use of Pauli noise in Python and display histogram widget in the notebook. --------- Co-authored-by: Dmitry Vasilevsky <[email protected]>
1 parent 2a75600 commit 852e858

File tree

1 file changed

+208
-0
lines changed

1 file changed

+208
-0
lines changed

samples/notebooks/noise.ipynb

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Simulating Pauli noise\n",
8+
"This notebook shows how to run simulations with Pauli noise, such as bit-flip or depolarizing noise.\n",
9+
"\n",
10+
"First, make sure prerequisites are available. Packages `qsharp` and `qsharp_widgets` must be already installed."
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 5,
16+
"metadata": {},
17+
"outputs": [],
18+
"source": [
19+
"import qsharp\n",
20+
"import qsharp_widgets"
21+
]
22+
},
23+
{
24+
"cell_type": "markdown",
25+
"metadata": {},
26+
"source": [
27+
"## Simulation with noise\n",
28+
"\n",
29+
"Define a simple program that creates a Bell state on two qubits and measures both qubits."
30+
]
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": 6,
35+
"metadata": {
36+
"vscode": {
37+
"languageId": "qsharp"
38+
}
39+
},
40+
"outputs": [],
41+
"source": [
42+
"%%qsharp\n",
43+
"\n",
44+
"operation BellPair() : Result[] {\n",
45+
" use q = Qubit[2];\n",
46+
" H(q[0]);\n",
47+
" CNOT(q[0], q[1]);\n",
48+
" MResetEachZ(q)\n",
49+
"}"
50+
]
51+
},
52+
{
53+
"cell_type": "markdown",
54+
"metadata": {},
55+
"source": [
56+
"Run 20 shots without noise and display results."
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": null,
62+
"metadata": {},
63+
"outputs": [],
64+
"source": [
65+
"results = qsharp.run(\"BellPair()\", 20)\n",
66+
"results"
67+
]
68+
},
69+
{
70+
"cell_type": "markdown",
71+
"metadata": {},
72+
"source": [
73+
"Note that measurements always agree within a shot as expected. Now run 20 shots of the same program with 10% [depolarizing noise](https://en.wikipedia.org/wiki/Quantum_depolarizing_channel). Depolarizing noise is applied to each gate and each measurement."
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": null,
79+
"metadata": {},
80+
"outputs": [],
81+
"source": [
82+
"results = qsharp.run(\"BellPair()\", 20, noise=qsharp.DepolarizingNoise(0.1))\n",
83+
"results"
84+
]
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"metadata": {},
89+
"source": [
90+
"Note that measurements do not always agree within the shot."
91+
]
92+
},
93+
{
94+
"cell_type": "markdown",
95+
"metadata": {},
96+
"source": [
97+
"## Histograms\n",
98+
"\n",
99+
"Define a program to prepare a cat state on five qubits and measure each qubit."
100+
]
101+
},
102+
{
103+
"cell_type": "code",
104+
"execution_count": 9,
105+
"metadata": {
106+
"vscode": {
107+
"languageId": "qsharp"
108+
}
109+
},
110+
"outputs": [],
111+
"source": [
112+
"%%qsharp\n",
113+
"\n",
114+
"operation Cat5() : Result[] {\n",
115+
" use q = Qubit[5];\n",
116+
" H(q[0]);\n",
117+
" ApplyCNOTChain(q);\n",
118+
" MResetEachZ(q)\n",
119+
"}"
120+
]
121+
},
122+
{
123+
"cell_type": "markdown",
124+
"metadata": {},
125+
"source": [
126+
"First, run this program without noise. Roughly half of the outcomes should be $\\ket{00000}$ and another half should be $\\ket{11111}$."
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"execution_count": null,
132+
"metadata": {},
133+
"outputs": [],
134+
"source": [
135+
"result = qsharp.run(\"Cat5()\", 1000)\n",
136+
"qsharp_widgets.Histogram(result)\n"
137+
]
138+
},
139+
{
140+
"cell_type": "markdown",
141+
"metadata": {},
142+
"source": [
143+
"Now, run the same program with bit-flip noise of 1%, 5%, 10%, 25%."
144+
]
145+
},
146+
{
147+
"cell_type": "code",
148+
"execution_count": null,
149+
"metadata": {},
150+
"outputs": [],
151+
"source": [
152+
"for p in [0.01, 0.05, 0.1, 0.25]:\n",
153+
" result = qsharp.run(\"Cat5()\", 1000, noise=qsharp.BitFlipNoise(p))\n",
154+
" display(f\"Noise probability = {p}\")\n",
155+
" display(qsharp_widgets.Histogram(result))"
156+
]
157+
},
158+
{
159+
"cell_type": "markdown",
160+
"metadata": {},
161+
"source": [
162+
"We can see that with 1% noise, cat state can still be clearly seen, but when noise approaches 25%, the cat state is indistinguishable from noise."
163+
]
164+
},
165+
{
166+
"cell_type": "markdown",
167+
"metadata": {},
168+
"source": [
169+
"## Arbitrary Pauli noise\n",
170+
"\n",
171+
"Standard bit-flip, phase-flip, and [depolarizing](https://en.wikipedia.org/wiki/Quantum_depolarizing_channel) noise are available, but arbitrary Pauli noise is also possible. The following example runs the same Cat5 program. First it applies noise with 20% probability (bit-flip half the time and phase-flip half the time). In a second experiment it applies Pauli-Y noise with 10% probability."
172+
]
173+
},
174+
{
175+
"cell_type": "code",
176+
"execution_count": null,
177+
"metadata": {},
178+
"outputs": [],
179+
"source": [
180+
"result = qsharp.run(\"Cat5()\", 1000, noise=(0.1, 0.0, 0.1))\n",
181+
"display(qsharp_widgets.Histogram(result))\n",
182+
"result = qsharp.run(\"Cat5()\", 1000, noise=(0.0, 0.1, 0.0))\n",
183+
"display(qsharp_widgets.Histogram(result))"
184+
]
185+
}
186+
],
187+
"metadata": {
188+
"kernelspec": {
189+
"display_name": "Python 3",
190+
"language": "python",
191+
"name": "python3"
192+
},
193+
"language_info": {
194+
"codemirror_mode": {
195+
"name": "ipython",
196+
"version": 3
197+
},
198+
"file_extension": ".py",
199+
"mimetype": "text/x-python",
200+
"name": "python",
201+
"nbconvert_exporter": "python",
202+
"pygments_lexer": "ipython3",
203+
"version": "3.11.9"
204+
}
205+
},
206+
"nbformat": 4,
207+
"nbformat_minor": 2
208+
}

0 commit comments

Comments
 (0)