{ "cells": [ { "cell_type": "markdown", "id": "noisy-run-0", "metadata": {}, "source": [ "# A noisy run\n", "\n", "A job may name noise channels: `depolarizing_1q`, `depolarizing_2q`, `amplitude_damping`, `dephasing` and `readout`, each a probability. The density matrix is exact; trajectories are an estimate, reported with a standard error; a Clifford circuit with Pauli noise samples Pauli frames." ] }, { "cell_type": "code", "execution_count": 1, "id": "noisy-run-1", "metadata": { "execution": { "iopub.execute_input": "2026-11-11T12:23:55.075966Z", "iopub.status.busy": "2026-11-11T12:23:55.075881Z", "iopub.status.idle": "2026-11-11T12:23:55.081150Z", "shell.execute_reply": "2026-11-11T12:23:55.080713Z" } }, "outputs": [], "source": [ "import wai_quantum as wq" ] }, { "cell_type": "code", "execution_count": 2, "id": "noisy-run-2", "metadata": { "execution": { "iopub.execute_input": "2026-11-11T12:23:55.082015Z", "iopub.status.busy": "2026-11-11T12:23:55.081940Z", "iopub.status.idle": "2026-11-11T12:23:55.084498Z", "shell.execute_reply": "2026-11-11T12:23:55.084339Z" } }, "outputs": [ { "data": { "text/plain": [ "Key(did:key:z6MkehRgf7yJbgaGfYsdoAsKdBPE3dj2CYhowQdcjqSJgvVd, in memory)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# A fixed key for this page, so that its receipts repeat byte for byte when it runs\n", "# again. Keep a key of your own: wq.Key.generate().save(\"my.key\"), then\n", "# wq.Key.from_file(\"my.key\"); with no key given, wq.run signs with this user's key,\n", "# the command line's.\n", "key = wq.Key(bytes(range(32)))\n", "key" ] }, { "cell_type": "code", "execution_count": 3, "id": "noisy-run-3", "metadata": { "execution": { "iopub.execute_input": "2026-11-11T12:23:55.085190Z", "iopub.status.busy": "2026-11-11T12:23:55.085125Z", "iopub.status.idle": "2026-11-11T12:23:55.088765Z", "shell.execute_reply": "2026-11-11T12:23:55.088538Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Z0 Z1 exact 0.957788 estimate 0.9580 ± 0.0045\n", "Z1 Z2 exact 0.978667 estimate 0.9850 ± 0.0027\n", "X0 X1 X2 exact 0.865765 estimate 0.8680 ± 0.0079\n", "exact estimate\n" ] } ], "source": [ "ghz = \"OPENQASM 3.0; qubit[3] q; bit[3] c; h q[0]; cx q[0], q[1]; cx q[1], q[2]; c = measure q;\"\n", "noise = {\"depolarizing_2q\": 0.02, \"dephasing\": 0.01, \"readout\": 0.01}\n", "exact = wq.run(ghz, observables=[\"Z0 Z1\", \"Z1 Z2\", \"X0 X1 X2\"], noise=noise, method=\"density_matrix\", key=key)\n", "estimate = wq.run(ghz, observables=[\"Z0 Z1\", \"Z1 Z2\", \"X0 X1 X2\"], noise=noise, method=\"trajectories\", shots=4000, seed=3, key=key)\n", "for e, t in zip(exact.result[\"expectations\"], estimate.result[\"expectations\"]):\n", " print(f\"{e['observable']:<9} exact {e['value']:.6f} estimate {t['value']:.4f} ± {t['stderr']:.4f}\")\n", "print(exact.accuracy, estimate.accuracy)" ] }, { "cell_type": "markdown", "id": "noisy-run-4", "metadata": {}, "source": [ "Each result says how it was made. The estimate's counts are samples of noisy shots:" ] }, { "cell_type": "code", "execution_count": 4, "id": "noisy-run-5", "metadata": { "execution": { "iopub.execute_input": "2026-11-11T12:23:55.089368Z", "iopub.status.busy": "2026-11-11T12:23:55.089295Z", "iopub.status.idle": "2026-11-11T12:23:55.135149Z", "shell.execute_reply": "2026-11-11T12:23:55.134826Z" } }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "1719bebf8da14d69abc5f95030bcc389", "version_major": 2, "version_minor": 1 }, "text/html": [ "
trajectories · accuracy estimate (each shot is an independent noisy trajectory in law; shots that took the same errors share one state (histories counts the distinct ones); expectations are means over the trajectories with their standard errors)
4000 noisy trajectories of 3 qubits, shots that share their errors sharing a state
4000 shots, 8 distinct outcomes; the 8 most frequent:
111111: 1957 of 40001957000000: 1831 of 40001831110110: 53 of 400053001001: 49 of 400049010010: 30 of 400030100100: 30 of 400030011011: 25 of 400025101101: 25 of 400025
receipt verified signed by did:key:z6Mkeh…howQdcjqSJgvVd
" ], "text/plain": [ "Result(trajectories, accuracy estimate, 4000 shots, 0 expectation values)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "noisy = wq.run(ghz, shots=4000, seed=3, noise=noise, method=\"trajectories\", key=key)\n", "wq.Result(noisy)" ] }, { "cell_type": "markdown", "id": "noisy-run-6", "metadata": {}, "source": [ "## Noise shortens a Bloch vector\n", "\n", "Dephasing with probability p after a gate scales a qubit's X and Y components by 1 − 2p." ] }, { "cell_type": "code", "execution_count": 5, "id": "noisy-run-7", "metadata": { "execution": { "iopub.execute_input": "2026-11-11T12:23:55.136083Z", "iopub.status.busy": "2026-11-11T12:23:55.135985Z", "iopub.status.idle": "2026-11-11T12:23:55.138766Z", "shell.execute_reply": "2026-11-11T12:23:55.138485Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "p = 0.0 x = 1.000000 1 - 2p = 1.000000\n", "p = 0.05 x = 0.900000 1 - 2p = 0.900000\n", "p = 0.1 x = 0.800000 1 - 2p = 0.800000\n", "p = 0.25 x = 0.500000 1 - 2p = 0.500000\n" ] } ], "source": [ "for p in (0.0, 0.05, 0.1, 0.25):\n", " b = wq.bloch(\"OPENQASM 3.0; qubit[1] q; h q[0];\", noise={\"dephasing\": p}, method=\"density_matrix\", key=key)\n", " print(f\"p = {p:<5} x = {b['qubits'][0]['x']:.6f} 1 - 2p = {1 - 2 * p:.6f}\")" ] }, { "cell_type": "code", "execution_count": 6, "id": "noisy-run-8", "metadata": { "execution": { "iopub.execute_input": "2026-11-11T12:23:55.139413Z", "iopub.status.busy": "2026-11-11T12:23:55.139341Z", "iopub.status.idle": "2026-11-11T12:23:55.142472Z", "shell.execute_reply": "2026-11-11T12:23:55.142139Z" } }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "63e463d96fc9464384271efce00790fa", "version_major": 2, "version_minor": 1 }, "text/html": [ "
Bloch vectors · density_matrix · accuracy exact
|0⟩|1⟩+x+yq0
x 0.715542 · y 0 · z 0.2
length 0.742967
|0⟩|1⟩+x+yq1
x 0.666913 · y 0 · z 0.489886
length 0.827503
Each qubit's reduced state: length 1 is pure, shorter is mixed (entangled with the others, or noisy).
" ], "text/plain": [ "Bloch(2 qubits, density_matrix, accuracy exact)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wq.Bloch(\"OPENQASM 3.0; qubit[2] q; h q[0]; ry(1.2) q[1];\", noise={\"dephasing\": 0.1, \"amplitude_damping\": 0.2}, method=\"density_matrix\", key=key)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.15" } }, "nbformat": 4, "nbformat_minor": 5 }