microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
samples/notebooks/carbon_error_correction/carbon.ipynb
353lines · modecode
| 1 | { |
| 2 | "cells": [ |
| 3 | { |
| 4 | "cell_type": "markdown", |
| 5 | "id": "de73025f", |
| 6 | "metadata": {}, |
| 7 | "source": [ |
| 8 | "# Using QDK to explore the Carbon Error Correction Code.\n", |
| 9 | "\n", |
| 10 | "This notebook will show how you can use the QDK with the Neutral Atom Device to explore how the Carbon error correction code runs both with and without noise.\n", |
| 11 | "\n", |
| 12 | "For more information on the Carbon family of codes, see [Demonstration of logical qubits and repeated error correction with better-than-physical error rates](https://arxiv.org/abs/2404.02280).\n", |
| 13 | "\n", |
| 14 | "First, we import the required packages and utilities, initialize a `NeutralAtomDevice` and initialize the Q# environment with the project from the current folder. This includes a Python implementation of the Carbon (C12) decoder that can turn the raw physical qubit measurement results into corrected, logical qubit measurement results." |
| 15 | ] |
| 16 | }, |
| 17 | { |
| 18 | "cell_type": "code", |
| 19 | "execution_count": null, |
| 20 | "id": "1929d94c", |
| 21 | "metadata": {}, |
| 22 | "outputs": [], |
| 23 | "source": [ |
| 24 | "import decoder\n", |
| 25 | "from qdk import qsharp, code\n", |
| 26 | "from qdk.widgets import Histogram\n", |
| 27 | "from qdk.simulation import NeutralAtomDevice, NoiseConfig\n", |
| 28 | "\n", |
| 29 | "# Get an instance of the NeutralAtomDevice machine\n", |
| 30 | "device = NeutralAtomDevice()\n", |
| 31 | "\n", |
| 32 | "# Initialize Q# with the project and adaptive profile configured\n", |
| 33 | "qsharp.init(target_profile=qsharp.TargetProfile.Adaptive_RIF, project_root=\".\")" |
| 34 | ] |
| 35 | }, |
| 36 | { |
| 37 | "cell_type": "markdown", |
| 38 | "id": "f1d36f1f", |
| 39 | "metadata": {}, |
| 40 | "source": [ |
| 41 | "Next, this `%%qsharp` cell sets up the experiment we want to run, which is parametrized by the number of repetitions, which Pauli basis to prepare and measure with, and how many blocks of Carbon (C12) with ancillas should be used." |
| 42 | ] |
| 43 | }, |
| 44 | { |
| 45 | "cell_type": "code", |
| 46 | "execution_count": null, |
| 47 | "id": "6177ed62", |
| 48 | "metadata": { |
| 49 | "vscode": { |
| 50 | "languageId": "qsharp" |
| 51 | } |
| 52 | }, |
| 53 | "outputs": [], |
| 54 | "source": [ |
| 55 | "%%qsharp\n", |
| 56 | "import Std.Diagnostics.Fact;\n", |
| 57 | "import Utils.TransversalCNOT;\n", |
| 58 | "import C12;\n", |
| 59 | "\n", |
| 60 | "operation PerformTeleportExperiment(ec_repetitions : Int, basis : Pauli, num_blocks : Int) : (Result[], (Result[], Result[])[], Result[])[] {\n", |
| 61 | " Fact(num_blocks > 0, \"need at least one block\");\n", |
| 62 | " let size = (12 + 16); // 12 data + 16 ancillas per block\n", |
| 63 | " use qubits = Qubit[size * num_blocks];\n", |
| 64 | " mutable results = [];\n", |
| 65 | " for i in 0..(num_blocks - 1) {\n", |
| 66 | " let logical_block = qubits[(0+(i*size))..(11+(i*size))];\n", |
| 67 | " let ancillas = qubits[(12+(i*size))..(27+(i*size))];\n", |
| 68 | "\n", |
| 69 | " results += [C12_Teleport(ec_repetitions, basis, logical_block, ancillas)];\n", |
| 70 | " }\n", |
| 71 | " return results;\n", |
| 72 | "}\n", |
| 73 | "\n", |
| 74 | "operation C12_Teleport(ec_repetitions : Int, basis : Pauli, logical_block : Qubit[], ancillas : Qubit[]) : (Result[], (Result[], Result[])[], Result[]) {\n", |
| 75 | " Fact(basis == PauliZ or basis == PauliX or basis == PauliI, \"only PauliZ and PauliX supported\");\n", |
| 76 | " Fact(Length(logical_block) == 12, \"logical block must be 12 qubits\");\n", |
| 77 | " Fact(Length(ancillas) == 16, \"16 ancillas are required\");\n", |
| 78 | "\n", |
| 79 | " // Prepare in the requested basis\n", |
| 80 | " mutable preselect = if basis == PauliX {\n", |
| 81 | " C12.PrepareXX(logical_block, ancillas[...3])\n", |
| 82 | " } else {\n", |
| 83 | " C12.PrepareZZ(logical_block, ancillas[...3])\n", |
| 84 | " };\n", |
| 85 | "\n", |
| 86 | " mutable syndromes = [];\n", |
| 87 | "\n", |
| 88 | " for _ in 1..(ec_repetitions) {\n", |
| 89 | " // Sequential teleport on..\n", |
| 90 | " // Prepare Z, Teleport X\n", |
| 91 | " set preselect += C12.PrepareZZ(ancillas[...11], ancillas[12...]);\n", |
| 92 | " TransversalCNOT(logical_block, ancillas[...11]);\n", |
| 93 | " ApplyToEach(H, logical_block);\n", |
| 94 | " let syndrome_x = MResetEachZ(logical_block);\n", |
| 95 | "\n", |
| 96 | " // Prepare X, Teleport Z\n", |
| 97 | " set preselect += C12.PrepareXX(logical_block, ancillas[12...]);\n", |
| 98 | " TransversalCNOT(logical_block, ancillas[...11]);\n", |
| 99 | " let syndrome_z = MResetEachZ(ancillas[...11]);\n", |
| 100 | " set syndromes += [(syndrome_x, syndrome_z)];\n", |
| 101 | " }\n", |
| 102 | "\n", |
| 103 | " // Final measurement\n", |
| 104 | " if basis == PauliX {\n", |
| 105 | " ApplyToEach(H, logical_block);\n", |
| 106 | " }\n", |
| 107 | " let final = MResetEachZ(logical_block);\n", |
| 108 | "\n", |
| 109 | " (preselect, syndromes, final)\n", |
| 110 | "}" |
| 111 | ] |
| 112 | }, |
| 113 | { |
| 114 | "cell_type": "markdown", |
| 115 | "id": "21031218", |
| 116 | "metadata": {}, |
| 117 | "source": [ |
| 118 | "With the experiment set up, we first try running the Q# with the default, sparse state simulation and no noise. This `Histogram` should have one bar with all `[[Zero, Zero]]` results, indicating that the logical teleport was performed successfully for one Carbon (C12) block in all 100 shots." |
| 119 | ] |
| 120 | }, |
| 121 | { |
| 122 | "cell_type": "code", |
| 123 | "execution_count": null, |
| 124 | "id": "a8d0c4cd", |
| 125 | "metadata": {}, |
| 126 | "outputs": [], |
| 127 | "source": [ |
| 128 | "results = qsharp.run(code.PerformTeleportExperiment, 100, 1, qsharp.Pauli.Z, 1)\n", |
| 129 | "corrected_logical_results = decoder.decode_results(results, \"Z\")\n", |
| 130 | "\n", |
| 131 | "# Use counter to make a simple histogram of the corrected logical results.\n", |
| 132 | "Histogram(map(str, corrected_logical_results))" |
| 133 | ] |
| 134 | }, |
| 135 | { |
| 136 | "cell_type": "markdown", |
| 137 | "id": "1f2ead22", |
| 138 | "metadata": {}, |
| 139 | "source": [ |
| 140 | "Next, we compile the program, this time with two Carbon (C12) blocks. To visualize and test larger programs, try increasing the number of blocks (the last property) to larger values." |
| 141 | ] |
| 142 | }, |
| 143 | { |
| 144 | "cell_type": "code", |
| 145 | "execution_count": null, |
| 146 | "id": "313cb353", |
| 147 | "metadata": {}, |
| 148 | "outputs": [], |
| 149 | "source": [ |
| 150 | "qir = qsharp.compile(code.PerformTeleportExperiment, 1, qsharp.Pauli.Z, 2)" |
| 151 | ] |
| 152 | }, |
| 153 | { |
| 154 | "cell_type": "markdown", |
| 155 | "id": "c18dcefe", |
| 156 | "metadata": {}, |
| 157 | "source": [ |
| 158 | "We can use the Neutral Atom Device we previously initialized to trace the compiled program. This will decompose the gates in the QIR and perform layout and scheduling for the device." |
| 159 | ] |
| 160 | }, |
| 161 | { |
| 162 | "cell_type": "code", |
| 163 | "execution_count": null, |
| 164 | "id": "ce29c823", |
| 165 | "metadata": {}, |
| 166 | "outputs": [], |
| 167 | "source": [ |
| 168 | "device.show_trace(qir)" |
| 169 | ] |
| 170 | }, |
| 171 | { |
| 172 | "cell_type": "markdown", |
| 173 | "id": "5fbd6f5f", |
| 174 | "metadata": {}, |
| 175 | "source": [ |
| 176 | "We can also use the device to perform Clifford simulations of the experiment. Clifford simulation can scale to multiple Carbon (C12) blocks and multiple rounds of error correction. Here again, a noiseless simulation should produce a single bar in the histogram, this time with a number of logical result pairs equal to the number of blocks compiled into the program." |
| 177 | ] |
| 178 | }, |
| 179 | { |
| 180 | "cell_type": "code", |
| 181 | "execution_count": 7, |
| 182 | "id": "8e05e0f8", |
| 183 | "metadata": {}, |
| 184 | "outputs": [ |
| 185 | { |
| 186 | "data": { |
| 187 | "application/vnd.jupyter.widget-view+json": { |
| 188 | "model_id": "4410a84a0ec841a9a5daf7aa1e27c25b", |
| 189 | "version_major": 2, |
| 190 | "version_minor": 1 |
| 191 | }, |
| 192 | "text/plain": [ |
| 193 | "<qsharp_widgets.Histogram object at 0x10e17b450>" |
| 194 | ] |
| 195 | }, |
| 196 | "execution_count": 7, |
| 197 | "metadata": {}, |
| 198 | "output_type": "execute_result" |
| 199 | } |
| 200 | ], |
| 201 | "source": [ |
| 202 | "res = device.simulate(qir, shots=1000, type=\"clifford\")\n", |
| 203 | "corrected_logical_results = decoder.decode_results(res, \"Z\")\n", |
| 204 | "Histogram(map(str, corrected_logical_results))" |
| 205 | ] |
| 206 | }, |
| 207 | { |
| 208 | "cell_type": "markdown", |
| 209 | "id": "b97ffa2c", |
| 210 | "metadata": {}, |
| 211 | "source": [ |
| 212 | "Error correction is more interesting with noise configured, so now we can use the `NoiseConfig` class to specify what noise we'll use. These default values produce interesting results, showing some shots rejected as \"preselect\" and others corrected by the decoder. If we filter out those \"preselect\" results, we can see that the majority of the accepted results have been corrected into the expected `[[Zero, Zero], [Zero, Zero]]` result." |
| 213 | ] |
| 214 | }, |
| 215 | { |
| 216 | "cell_type": "code", |
| 217 | "execution_count": 8, |
| 218 | "id": "b0f2063e", |
| 219 | "metadata": {}, |
| 220 | "outputs": [ |
| 221 | { |
| 222 | "data": { |
| 223 | "application/vnd.jupyter.widget-view+json": { |
| 224 | "model_id": "f41032d29c7c4f31948d7089c786d40e", |
| 225 | "version_major": 2, |
| 226 | "version_minor": 1 |
| 227 | }, |
| 228 | "text/plain": [ |
| 229 | "<qsharp_widgets.Histogram object at 0x108584510>" |
| 230 | ] |
| 231 | }, |
| 232 | "metadata": {}, |
| 233 | "output_type": "display_data" |
| 234 | }, |
| 235 | { |
| 236 | "data": { |
| 237 | "application/vnd.jupyter.widget-view+json": { |
| 238 | "model_id": "eca2be5fcf0f468ca6f28458fef98dd3", |
| 239 | "version_major": 2, |
| 240 | "version_minor": 1 |
| 241 | }, |
| 242 | "text/plain": [ |
| 243 | "<qsharp_widgets.Histogram object at 0x10e2a0690>" |
| 244 | ] |
| 245 | }, |
| 246 | "metadata": {}, |
| 247 | "output_type": "display_data" |
| 248 | } |
| 249 | ], |
| 250 | "source": [ |
| 251 | "noise = NoiseConfig()\n", |
| 252 | "\n", |
| 253 | "def set_depolarizing_error(table, p: float):\n", |
| 254 | " \"\"\"One of three equally likely Pauli errors occurs with probability `p`.\"\"\"\n", |
| 255 | " table.x = p / 3\n", |
| 256 | " table.y = p / 3\n", |
| 257 | " table.z = p / 3\n", |
| 258 | "\n", |
| 259 | "# 1Q error\n", |
| 260 | "set_depolarizing_error(noise.sx, 0.00225)\n", |
| 261 | "\n", |
| 262 | "# Error per CZ\n", |
| 263 | "noise.cz.iz = 0.0105 / 3\n", |
| 264 | "noise.cz.zi = 0.0105 / 3\n", |
| 265 | "noise.cz.zz = 0.0105 / 3\n", |
| 266 | "\n", |
| 267 | "# Loss per CZ\n", |
| 268 | "noise.cz.loss = 0.0027\n", |
| 269 | "\n", |
| 270 | "# Error for moved atoms\n", |
| 271 | "noise.mov.z = 1e-3\n", |
| 272 | "\n", |
| 273 | "# Loss per moved atom\n", |
| 274 | "noise.mov.loss = 0.0005\n", |
| 275 | "\n", |
| 276 | "res = device.simulate(qir, shots=10000, noise=noise, type=\"clifford\")\n", |
| 277 | "corrected_logical_results = decoder.decode_results(res, \"Z\")\n", |
| 278 | "display(Histogram(map(str, corrected_logical_results)))\n", |
| 279 | "display(Histogram(map(str, filter(lambda res: not any(r == 'PREselect' for r in res), corrected_logical_results))))" |
| 280 | ] |
| 281 | }, |
| 282 | { |
| 283 | "cell_type": "markdown", |
| 284 | "id": "85cc8664", |
| 285 | "metadata": {}, |
| 286 | "source": [ |
| 287 | "To make it easier to see the results, this further processes them to treat each Carbon (C12) block as separate rather than plot the joint behavior of the blocks. This again shows both the full results in the first histogram and the filtered, non-preselect results in the second histogram." |
| 288 | ] |
| 289 | }, |
| 290 | { |
| 291 | "cell_type": "code", |
| 292 | "execution_count": 9, |
| 293 | "id": "35e7dc3c", |
| 294 | "metadata": {}, |
| 295 | "outputs": [ |
| 296 | { |
| 297 | "data": { |
| 298 | "application/vnd.jupyter.widget-view+json": { |
| 299 | "model_id": "d79a1d1efe2449f08aa22c870acc2def", |
| 300 | "version_major": 2, |
| 301 | "version_minor": 1 |
| 302 | }, |
| 303 | "text/plain": [ |
| 304 | "<qsharp_widgets.Histogram object at 0x10836de90>" |
| 305 | ] |
| 306 | }, |
| 307 | "metadata": {}, |
| 308 | "output_type": "display_data" |
| 309 | }, |
| 310 | { |
| 311 | "data": { |
| 312 | "application/vnd.jupyter.widget-view+json": { |
| 313 | "model_id": "af464fa55640435ca6362061162b0e60", |
| 314 | "version_major": 2, |
| 315 | "version_minor": 1 |
| 316 | }, |
| 317 | "text/plain": [ |
| 318 | "<qsharp_widgets.Histogram object at 0x10e134a90>" |
| 319 | ] |
| 320 | }, |
| 321 | "metadata": {}, |
| 322 | "output_type": "display_data" |
| 323 | } |
| 324 | ], |
| 325 | "source": [ |
| 326 | "flattened = [r for res in corrected_logical_results for r in res]\n", |
| 327 | "display(Histogram(map(str, flattened)))\n", |
| 328 | "display(Histogram(map(str, filter(lambda res: res != 'PREselect', flattened))))" |
| 329 | ] |
| 330 | } |
| 331 | ], |
| 332 | "metadata": { |
| 333 | "kernelspec": { |
| 334 | "display_name": "Python 3", |
| 335 | "language": "python", |
| 336 | "name": "python3" |
| 337 | }, |
| 338 | "language_info": { |
| 339 | "codemirror_mode": { |
| 340 | "name": "ipython", |
| 341 | "version": 3 |
| 342 | }, |
| 343 | "file_extension": ".py", |
| 344 | "mimetype": "text/x-python", |
| 345 | "name": "python", |
| 346 | "nbconvert_exporter": "python", |
| 347 | "pygments_lexer": "ipython3", |
| 348 | "version": "3.11.14" |
| 349 | } |
| 350 | }, |
| 351 | "nbformat": 4, |
| 352 | "nbformat_minor": 5 |
| 353 | } |
| 354 | |