microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/notebooks/noise.ipynb
271lines · modecode
| 1 | { |
| 2 | "cells": [ |
| 3 | { |
| 4 | "cell_type": "markdown", |
| 5 | "metadata": {}, |
| 6 | "source": [ |
| 7 | "# Simulating Pauli noise and Qubit Loss\n", |
| 8 | "This notebook shows how to run simulations with Pauli noise, such as bit-flip or depolarizing noise, as well as qubit loss.\n", |
| 9 | "\n", |
| 10 | "First, make sure prerequisites are available. The package and extra `qdk[jupyter]` must be already installed." |
| 11 | ] |
| 12 | }, |
| 13 | { |
| 14 | "cell_type": "code", |
| 15 | "execution_count": null, |
| 16 | "metadata": {}, |
| 17 | "outputs": [], |
| 18 | "source": [ |
| 19 | "from qdk import qsharp\n", |
| 20 | "from qdk.widgets import Histogram" |
| 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": null, |
| 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": null, |
| 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 | "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(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(Histogram(result))\n", |
| 182 | "result = qsharp.run(\"Cat5()\", 1000, noise=(0.0, 0.1, 0.0))\n", |
| 183 | "display(Histogram(result))" |
| 184 | ] |
| 185 | }, |
| 186 | { |
| 187 | "cell_type": "markdown", |
| 188 | "metadata": {}, |
| 189 | "source": [ |
| 190 | "## Simulating with Qubit Loss\n", |
| 191 | "\n", |
| 192 | "For some qubit modalities, a qubit may undergo \"loss\" during execution. This means the physical system backing the qubit is unable to be measured or operated on. To test behaviors of an algorithm in the presence of qubit loss, you can use the `qubit_loss` parameter to `qsharp.run` and set the probability of a qubit being lost on each operation. Qubit loss is reported only at measurement time, where a special `Loss` value is returned that is neither `One` nor `Zero`. During simulation, a lost qubit is no longer acted on by gates and remains in the $\\ket{0}$ state until it is measured or reset, at which point the simulation reloads a fresh qubit to support future operations.\n", |
| 193 | "\n", |
| 194 | "In this example, we'll set the qubit loss probability to high 50% to ensure some qubits are lost during simulation:" |
| 195 | ] |
| 196 | }, |
| 197 | { |
| 198 | "cell_type": "code", |
| 199 | "execution_count": null, |
| 200 | "metadata": {}, |
| 201 | "outputs": [], |
| 202 | "source": [ |
| 203 | "result = qsharp.run(\"BellPair()\", 100, qubit_loss=0.5)\n", |
| 204 | "display(Histogram(result))" |
| 205 | ] |
| 206 | }, |
| 207 | { |
| 208 | "cell_type": "markdown", |
| 209 | "metadata": {}, |
| 210 | "source": [ |
| 211 | "Note that the `Loss` value is not usable inside of the simulation, and any comparisons on that value will trigger a runtime failure. To avoid this failure, use `IsLossResult` to check whether the result value corresponds to qubit loss before using it in any branching logic:\n" |
| 212 | ] |
| 213 | }, |
| 214 | { |
| 215 | "cell_type": "code", |
| 216 | "execution_count": null, |
| 217 | "metadata": { |
| 218 | "vscode": { |
| 219 | "languageId": "qsharp" |
| 220 | } |
| 221 | }, |
| 222 | "outputs": [], |
| 223 | "source": [ |
| 224 | "%%qsharp\n", |
| 225 | "\n", |
| 226 | "operation CheckForLoss() : (Bool, Bool) {\n", |
| 227 | " use q = Qubit();\n", |
| 228 | " H(q);\n", |
| 229 | " let res = MResetZ(q);\n", |
| 230 | " if IsLossResult(res) {\n", |
| 231 | " return (true, false);\n", |
| 232 | " } elif res == One {\n", |
| 233 | " return (false, true);\n", |
| 234 | " } else {\n", |
| 235 | " return (false, false);\n", |
| 236 | " }\n", |
| 237 | "}" |
| 238 | ] |
| 239 | }, |
| 240 | { |
| 241 | "cell_type": "code", |
| 242 | "execution_count": null, |
| 243 | "metadata": {}, |
| 244 | "outputs": [], |
| 245 | "source": [ |
| 246 | "Histogram(qsharp.run(\"CheckForLoss()\", 100, qubit_loss=0.5))" |
| 247 | ] |
| 248 | } |
| 249 | ], |
| 250 | "metadata": { |
| 251 | "kernelspec": { |
| 252 | "display_name": "Python 3", |
| 253 | "language": "python", |
| 254 | "name": "python3" |
| 255 | }, |
| 256 | "language_info": { |
| 257 | "codemirror_mode": { |
| 258 | "name": "ipython", |
| 259 | "version": 3 |
| 260 | }, |
| 261 | "file_extension": ".py", |
| 262 | "mimetype": "text/x-python", |
| 263 | "name": "python", |
| 264 | "nbconvert_exporter": "python", |
| 265 | "pygments_lexer": "ipython3", |
| 266 | "version": "3.11.13" |
| 267 | } |
| 268 | }, |
| 269 | "nbformat": 4, |
| 270 | "nbformat_minor": 2 |
| 271 | } |
| 272 | |