-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUntitled
More file actions
1 lines (1 loc) · 18.1 KB
/
Copy pathUntitled
File metadata and controls
1 lines (1 loc) · 18.1 KB
1
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyN16sxe0IrtTCmksNMt5h4h"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"code","execution_count":1,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"H3YRApaSeDcG","executionInfo":{"status":"ok","timestamp":1780260884655,"user_tz":240,"elapsed":27272,"user":{"displayName":"elijah griffin","userId":"02345076815743728231"}},"outputId":"d0d2b72e-52ff-4493-c569-8ffcc7f70ceb"},"outputs":[{"output_type":"stream","name":"stdout","text":["Collecting cirq\n"," Downloading cirq-1.6.1-py3-none-any.whl.metadata (16 kB)\n","Collecting cirq-aqt==1.6.1 (from cirq)\n"," Downloading cirq_aqt-1.6.1-py3-none-any.whl.metadata (4.7 kB)\n","Collecting cirq-core==1.6.1 (from cirq)\n"," Downloading cirq_core-1.6.1-py3-none-any.whl.metadata (4.8 kB)\n","Collecting cirq-google==1.6.1 (from cirq)\n"," Downloading cirq_google-1.6.1-py3-none-any.whl.metadata (4.9 kB)\n","Collecting cirq-ionq==1.6.1 (from cirq)\n"," Downloading cirq_ionq-1.6.1-py3-none-any.whl.metadata (4.7 kB)\n","Collecting cirq-pasqal==1.6.1 (from cirq)\n"," Downloading cirq_pasqal-1.6.1-py3-none-any.whl.metadata (4.7 kB)\n","Collecting cirq-web==1.6.1 (from cirq)\n"," Downloading cirq_web-1.6.1-py3-none-any.whl.metadata (5.4 kB)\n","Requirement already satisfied: requests~=2.32 in /usr/local/lib/python3.12/dist-packages (from cirq-aqt==1.6.1->cirq) (2.32.4)\n","Requirement already satisfied: attrs>=21.3.0 in /usr/local/lib/python3.12/dist-packages (from cirq-core==1.6.1->cirq) (26.1.0)\n","Collecting duet>=0.2.8 (from cirq-core==1.6.1->cirq)\n"," Downloading duet-0.2.9-py3-none-any.whl.metadata (2.3 kB)\n","Requirement already satisfied: matplotlib~=3.8 in /usr/local/lib/python3.12/dist-packages (from cirq-core==1.6.1->cirq) (3.10.0)\n","Requirement already satisfied: networkx~=3.4 in /usr/local/lib/python3.12/dist-packages (from cirq-core==1.6.1->cirq) (3.6.1)\n","Requirement already satisfied: numpy>=1.26 in /usr/local/lib/python3.12/dist-packages (from cirq-core==1.6.1->cirq) (2.0.2)\n","Requirement already satisfied: pandas~=2.1 in /usr/local/lib/python3.12/dist-packages (from cirq-core==1.6.1->cirq) (2.2.2)\n","Collecting sortedcontainers~=2.0 (from cirq-core==1.6.1->cirq)\n"," Downloading sortedcontainers-2.4.0-py2.py3-none-any.whl.metadata (10 kB)\n","Requirement already satisfied: scipy~=1.12 in /usr/local/lib/python3.12/dist-packages (from cirq-core==1.6.1->cirq) (1.16.3)\n","Requirement already satisfied: sympy in /usr/local/lib/python3.12/dist-packages (from cirq-core==1.6.1->cirq) (1.14.0)\n","Requirement already satisfied: typing_extensions>=4.2 in /usr/local/lib/python3.12/dist-packages (from cirq-core==1.6.1->cirq) (4.15.0)\n","Requirement already satisfied: tqdm>=4.12 in /usr/local/lib/python3.12/dist-packages (from cirq-core==1.6.1->cirq) (4.67.3)\n","Requirement already satisfied: google-api-core~=2.22 in /usr/local/lib/python3.12/dist-packages (from google-api-core[grpc]~=2.22->cirq-google==1.6.1->cirq) (2.30.3)\n","Requirement already satisfied: proto-plus~=1.25 in /usr/local/lib/python3.12/dist-packages (from cirq-google==1.6.1->cirq) (1.28.0)\n","Requirement already satisfied: protobuf<6.0dev,>=5.26.1 in /usr/local/lib/python3.12/dist-packages (from cirq-google==1.6.1->cirq) (5.29.6)\n","Collecting typedunits (from cirq-google==1.6.1->cirq)\n"," Downloading typedunits-0.0.2-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.metadata (4.9 kB)\n","Requirement already satisfied: googleapis-common-protos<2.0.0,>=1.63.2 in /usr/local/lib/python3.12/dist-packages (from google-api-core~=2.22->google-api-core[grpc]~=2.22->cirq-google==1.6.1->cirq) (1.75.0)\n","Requirement already satisfied: google-auth<3.0.0,>=2.14.1 in /usr/local/lib/python3.12/dist-packages (from google-api-core~=2.22->google-api-core[grpc]~=2.22->cirq-google==1.6.1->cirq) (2.47.0)\n","Requirement already satisfied: grpcio<2.0.0,>=1.33.2 in /usr/local/lib/python3.12/dist-packages (from google-api-core[grpc]~=2.22->cirq-google==1.6.1->cirq) (1.80.0)\n","Requirement already satisfied: grpcio-status<2.0.0,>=1.33.2 in /usr/local/lib/python3.12/dist-packages (from google-api-core[grpc]~=2.22->cirq-google==1.6.1->cirq) (1.71.2)\n","Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.12/dist-packages (from matplotlib~=3.8->cirq-core==1.6.1->cirq) (1.3.3)\n","Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.12/dist-packages (from matplotlib~=3.8->cirq-core==1.6.1->cirq) (0.12.1)\n","Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.12/dist-packages (from matplotlib~=3.8->cirq-core==1.6.1->cirq) (4.63.0)\n","Requirement already satisfied: kiwisolver>=1.3.1 in /usr/local/lib/python3.12/dist-packages (from matplotlib~=3.8->cirq-core==1.6.1->cirq) (1.5.0)\n","Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.12/dist-packages (from matplotlib~=3.8->cirq-core==1.6.1->cirq) (26.2)\n","Requirement already satisfied: pillow>=8 in /usr/local/lib/python3.12/dist-packages (from matplotlib~=3.8->cirq-core==1.6.1->cirq) (11.3.0)\n","Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.12/dist-packages (from matplotlib~=3.8->cirq-core==1.6.1->cirq) (3.3.2)\n","Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.12/dist-packages (from matplotlib~=3.8->cirq-core==1.6.1->cirq) (2.9.0.post0)\n","Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.12/dist-packages (from pandas~=2.1->cirq-core==1.6.1->cirq) (2025.2)\n","Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.12/dist-packages (from pandas~=2.1->cirq-core==1.6.1->cirq) (2026.2)\n","Requirement already satisfied: charset_normalizer<4,>=2 in /usr/local/lib/python3.12/dist-packages (from requests~=2.32->cirq-aqt==1.6.1->cirq) (3.4.7)\n","Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.12/dist-packages (from requests~=2.32->cirq-aqt==1.6.1->cirq) (3.15)\n","Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.12/dist-packages (from requests~=2.32->cirq-aqt==1.6.1->cirq) (2.5.0)\n","Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.12/dist-packages (from requests~=2.32->cirq-aqt==1.6.1->cirq) (2026.5.20)\n","Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.12/dist-packages (from sympy->cirq-core==1.6.1->cirq) (1.3.0)\n","Requirement already satisfied: cython>=3.0.0 in /usr/local/lib/python3.12/dist-packages (from typedunits->cirq-google==1.6.1->cirq) (3.0.12)\n","Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.12/dist-packages (from google-auth<3.0.0,>=2.14.1->google-api-core~=2.22->google-api-core[grpc]~=2.22->cirq-google==1.6.1->cirq) (0.4.2)\n","Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.12/dist-packages (from google-auth<3.0.0,>=2.14.1->google-api-core~=2.22->google-api-core[grpc]~=2.22->cirq-google==1.6.1->cirq) (4.9.1)\n","Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.12/dist-packages (from python-dateutil>=2.7->matplotlib~=3.8->cirq-core==1.6.1->cirq) (1.17.0)\n","Requirement already satisfied: pyasn1<0.7.0,>=0.6.1 in /usr/local/lib/python3.12/dist-packages (from pyasn1-modules>=0.2.1->google-auth<3.0.0,>=2.14.1->google-api-core~=2.22->google-api-core[grpc]~=2.22->cirq-google==1.6.1->cirq) (0.6.3)\n","Downloading cirq-1.6.1-py3-none-any.whl (11 kB)\n","Downloading cirq_aqt-1.6.1-py3-none-any.whl (32 kB)\n","Downloading cirq_core-1.6.1-py3-none-any.whl (2.0 MB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.0/2.0 MB\u001b[0m \u001b[31m48.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hDownloading cirq_google-1.6.1-py3-none-any.whl (670 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m670.8/670.8 kB\u001b[0m \u001b[31m33.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hDownloading cirq_ionq-1.6.1-py3-none-any.whl (73 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m73.5/73.5 kB\u001b[0m \u001b[31m4.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hDownloading cirq_pasqal-1.6.1-py3-none-any.whl (33 kB)\n","Downloading cirq_web-1.6.1-py3-none-any.whl (430 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m430.5/430.5 kB\u001b[0m \u001b[31m29.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hDownloading duet-0.2.9-py3-none-any.whl (29 kB)\n","Downloading sortedcontainers-2.4.0-py2.py3-none-any.whl (29 kB)\n","Downloading typedunits-0.0.2-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (2.7 MB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.7/2.7 MB\u001b[0m \u001b[31m57.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hInstalling collected packages: sortedcontainers, typedunits, duet, cirq-core, cirq-web, cirq-pasqal, cirq-ionq, cirq-aqt, cirq-google, cirq\n","Successfully installed cirq-1.6.1 cirq-aqt-1.6.1 cirq-core-1.6.1 cirq-google-1.6.1 cirq-ionq-1.6.1 cirq-pasqal-1.6.1 cirq-web-1.6.1 duet-0.2.9 sortedcontainers-2.4.0 typedunits-0.0.2\n","\n","=== Positive-time channel ===\n","Circuit:\n"," ┌──┐ ┌──┐ ┌──┐ ┌─────┐ ┌─────┐ ┌─────┐\n","0: ───H───@────@──────@──────@─────────K───────────@─────────@─────────@────────@───H───M('m')───\n"," │ │ │ │ │ │ │ │ │\n","1: ───────X────┼S─────┼──────┼─────────K───────────┼─────────┼─────────┼S^-1────X───────M────────\n"," │ │ │ │ │ │ │\n","2: ────────────X──────┼S─────┼─────────K───────────┼─────────┼S^-1─────X────────────────M────────\n"," │ │ │ │ │\n","3: ───────────────────X──────┼S────────K───────────┼S^-1─────X──────────────────────────M────────\n"," │ │ │\n","4: ──────────────────────────X─────S───K───S^-1────X────────────────────────────────────M────────\n"," └──┘ └──┘ └──┘ └─────┘ └─────┘ └─────┘\n","\n","Histogram (measurement of 5 physical qubits after decode):\n","00000: 944\n","00001: 27\n","00010: 20\n","00011: 4\n","00100: 24\n","00101: 2\n","00110: 2\n","00111: 1\n","01000: 25\n","01010: 1\n","01101: 1\n","01110: 2\n","01111: 34\n","10000: 802\n","10001: 22\n","10010: 24\n","10011: 3\n","10100: 14\n","10101: 3\n","10110: 2\n","11000: 17\n","11010: 1\n","11011: 3\n","11100: 1\n","11101: 2\n","11110: 1\n","11111: 18\n","\n","=== Negative-time channel ===\n","Circuit:\n"," ┌──┐ ┌──┐ ┌──┐ ┌─────┐ ┌─────┐ ┌─────┐\n","0: ───H───@────@──────@──────@─────────K───────────@─────────@─────────@────────@───H───M('m')───\n"," │ │ │ │ │ │ │ │ │\n","1: ───────X────┼S─────┼──────┼─────────K───────────┼─────────┼─────────┼S^-1────X───────M────────\n"," │ │ │ │ │ │ │\n","2: ────────────X──────┼S─────┼─────────K───────────┼─────────┼S^-1─────X────────────────M────────\n"," │ │ │ │ │\n","3: ───────────────────X──────┼S────────K───────────┼S^-1─────X──────────────────────────M────────\n"," │ │ │\n","4: ──────────────────────────X─────S───K───S^-1────X────────────────────────────────────M────────\n"," └──┘ └──┘ └──┘ └─────┘ └─────┘ └─────┘\n","\n","Histogram (measurement of 5 physical qubits after decode):\n","00000: 638\n","00001: 10\n","00010: 13\n","00100: 12\n","01000: 18\n","01010: 1\n","01111: 14\n","10000: 1244\n","10001: 11\n","10010: 12\n","10011: 1\n","10100: 9\n","10111: 1\n","11000: 10\n","11111: 6\n"]}],"source":["!pip install cirq\n","import cirq\n","import numpy as np\n","\n","\n","\n","q = cirq.LineQubit.range(5)\n","\n","\n","class KrausChannel(cirq.Gate):\n"," def __init__(self, kraus_ops):\n"," self.kraus_ops = tuple(np.array(k, dtype=np.complex128) for k in kraus_ops)\n","\n"," def _num_qubits_(self):\n"," return 1\n","\n"," def _kraus_(self):\n"," return self.kraus_ops\n","\n"," def _has_kraus_(self):\n"," return True\n","\n"," def _circuit_diagram_info_(self, args):\n"," return [\"K\"]\n","\n","\n","def make_channel(p_loss: float, p_phase: float, theta: float) -> KrausChannel:\n"," \"\"\"Build a single-qubit channel:\n"," E = U(theta) ∘ PhaseFlip(p_phase) ∘ AmpDamp(p_loss)\n"," \"\"\"\n","\n","\n"," E0 = np.array([[1, 0],\n"," [0, np.sqrt(1 - p_loss)]], dtype=np.complex128)\n"," E1 = np.array([[0, np.sqrt(p_loss)],\n"," [0, 0]], dtype=np.complex128)\n","\n","\n"," I = np.eye(2, dtype=np.complex128)\n"," Z = np.array([[1, 0],\n"," [0, -1]], dtype=np.complex128)\n"," F0 = np.sqrt(1 - p_phase) * I\n"," F1 = np.sqrt(p_phase) * Z\n","\n"," U = cirq.unitary(cirq.rz(theta))\n","\n","\n"," kraus = []\n"," for A in (E0, E1):\n"," for B in (F0, F1):\n"," kraus.append(U @ B @ A)\n","\n"," return KrausChannel(kraus)\n","\n","\n","\n","pos_channel = make_channel(\n"," p_loss=0.05,\n"," p_phase=0.02,\n"," theta=+0.3\n",")\n","\n","\n","neg_channel = make_channel(\n"," p_loss=0.02,\n"," p_phase=0.08,\n"," theta=-0.5\n",")\n","\n","\n","\n","def encode_5qubit() -> cirq.Circuit:\n"," \"\"\"Toy encoder for |0_L> of the 5-qubit code.\n","\n"," This is not the only encoding; you can swap in a more\n"," canonical circuit if you want to match a specific paper.\n"," \"\"\"\n"," c = cirq.Circuit()\n","\n","\n"," c.append([\n"," cirq.H(q[0]),\n"," cirq.CNOT(q[0], q[1]),\n"," cirq.CNOT(q[0], q[2]),\n"," cirq.CNOT(q[0], q[3]),\n"," cirq.CNOT(q[0], q[4]),\n"," cirq.S(q[1]),\n"," cirq.S(q[2]),\n"," cirq.S(q[3]),\n"," cirq.S(q[4]),\n"," ])\n","\n"," return c\n","\n","\n","def apply_noise(channel: KrausChannel) -> cirq.Circuit:\n"," \"\"\"Apply the same single-qubit channel to all 5 physical qubits.\"\"\"\n"," return cirq.Circuit(channel.on(q[i]) for i in range(5))\n","\n","\n","def decode_5qubit() -> cirq.Circuit:\n"," \"\"\"Decode by inverting the encoder.\"\"\"\n"," return cirq.inverse(encode_5qubit())\n","\n","\n","def build_pipeline(channel: KrausChannel) -> cirq.Circuit:\n"," \"\"\"Encode -> Noise -> Decode -> Measure.\"\"\"\n"," circuit = cirq.Circuit()\n"," circuit += encode_5qubit()\n"," circuit += apply_noise(channel)\n"," circuit += decode_5qubit()\n"," circuit.append(cirq.measure(*q, key=\"m\"))\n"," return circuit\n","\n","\n","def run_and_print(label: str, channel: KrausChannel, reps: int = 2000):\n"," sim = cirq.DensityMatrixSimulator()\n"," circuit = build_pipeline(channel)\n"," result = sim.run(circuit, repetitions=reps)\n"," hist = result.histogram(key=\"m\")\n"," print(f\"\\n=== {label} ===\")\n"," print(\"Circuit:\")\n"," print(circuit)\n"," print(\"\\nHistogram (measurement of 5 physical qubits after decode):\")\n"," for outcome, count in sorted(hist.items()):\n"," bitstring = format(outcome, \"05b\")\n"," print(f\"{bitstring}: {count}\")\n","\n","\n","if __name__ == \"__main__\":\n"," run_and_print(\"Positive-time channel\", pos_channel)\n"," run_and_print(\"Negative-time channel\", neg_channel)\n"]}]}