{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "WZ1G8QHhdHZR" }, "source": [ "##### Copyright 2020 The Cirq Developers" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "cellView": "form", "execution": { "iopub.execute_input": "2025-05-30T09:52:44.293551Z", "iopub.status.busy": "2025-05-30T09:52:44.293310Z", "iopub.status.idle": "2025-05-30T09:52:44.297182Z", "shell.execute_reply": "2025-05-30T09:52:44.296636Z" }, "id": "KQa9t_gadIuR" }, "outputs": [], "source": [ "# @title Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# https://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License." ] }, { "cell_type": "markdown", "metadata": { "id": "xwec7FrkdFmi" }, "source": [ "# Operators and observables" ] }, { "cell_type": "markdown", "metadata": { "id": "5KZia7jmdJ3V" }, "source": [ "\n", " \n", " \n", " \n", " \n", "
\n", " View on QuantumAI\n", " \n", " Run in Google Colab\n", " \n", " View source on GitHub\n", " \n", " Download notebook\n", "
" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2025-05-30T09:52:44.299761Z", "iopub.status.busy": "2025-05-30T09:52:44.299518Z", "iopub.status.idle": "2025-05-30T09:52:59.884413Z", "shell.execute_reply": "2025-05-30T09:52:59.883318Z" }, "id": "bd9529db1c0b" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "installing cirq...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\r\n", "tensorflow-metadata 1.17.1 requires protobuf<4.22,>=4.21.6; python_version < \"3.11\", but you have protobuf 5.29.5 which is incompatible.\u001b[0m\u001b[31m\r\n", "\u001b[0m" ] }, { "name": "stdout", "output_type": "stream", "text": [ "installed cirq.\n" ] } ], "source": [ "try:\n", " import cirq\n", "except ImportError:\n", " print(\"installing cirq...\")\n", " !pip install --quiet cirq\n", " print(\"installed cirq.\")\n", " import cirq\n", "\n", "import numpy as np\n", "import sympy.parsing.sympy_parser as sympy_parser" ] }, { "cell_type": "markdown", "metadata": { "id": "254c2d1ee631" }, "source": [ "This guide is directed at those already familiar with quantum operations (operators) and observables who want to know how to use them in Cirq. The following table shows an overview of operators." ] }, { "cell_type": "markdown", "metadata": { "id": "9d8dbd4b4e5c" }, "source": [ "| Operator | Cirq representation | Guides | Examples | \n", "|-----|-------|--------|-----|\n", "| Unitary operators | Any class implementing the `_unitary_` and `_has_unitary_` protocol | [Protocols](protocols.ipynb), [Gates and operations](gates.ipynb), [Custom gates](custom_gates.ipynb) | `cirq.Gate`
`cirq.X(qubit)`
`cirq.CNOT(q0, q1)`
`cirq.MatrixGate.on(qubit)`
`cirq.Circuit` (if it only contains unitary operations) |\n", "| Measurements | `cirq.measure` and `cirq.MeasurementGate` | [Gates and operations](gates.ipynb) | `cirq.measure(cirq.LineQubit(0))` | \n", "| Quantum channels | | [Protocols](protocols.ipynb) | `cirq.DepolarizingChannel(p=0.2)(q0)`
`cirq.X.with_probability(0.5)`|" ] }, { "cell_type": "markdown", "metadata": { "id": "c35a9e89ec98" }, "source": [ "Cirq also supports observables on qubits that can be used to calculate expectation values on a given state.\n", "\n", "* You can use `cirq.PauliString` to express them,\n", "* Or you can use `cirq.PauliSum` with real coefficients." ] }, { "cell_type": "markdown", "metadata": { "id": "71ae01d45738" }, "source": [ "## Operators" ] }, { "cell_type": "markdown", "metadata": { "id": "7cabbfa3ba55" }, "source": [ "Quantum operations (or just *operators*) include unitary gates, measurements, and noisy channels. Operators that act on a given set of qubits implement `cirq.Operation` which supports the Kraus operator representation\n", "\n", "$$\n", "\\rho \\mapsto \\sum_{k} A_k \\rho A_k^\\dagger .\n", "$$\n", "\n", "Here, $\\sum_{k} A_k^\\dagger A_k = I$ and $\\rho$ is a quantum state. Operators are defined in the `cirq.ops` module." ] }, { "cell_type": "markdown", "metadata": { "id": "329bba1d23f2" }, "source": [ "### Unitary operators" ] }, { "cell_type": "markdown", "metadata": { "id": "67317e02f377" }, "source": [ "Standard unitary operators used in quantum information can be found in `cirq.ops`, for example Pauli-$X$ as shown below." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2025-05-30T09:52:59.889107Z", "iopub.status.busy": "2025-05-30T09:52:59.888347Z", "iopub.status.idle": "2025-05-30T09:52:59.893280Z", "shell.execute_reply": "2025-05-30T09:52:59.892462Z" }, "id": "330de6a68bb9" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "X(q(0))\n" ] } ], "source": [ "qubit = cirq.LineQubit(0)\n", "unitary_operation = cirq.ops.X.on(qubit) # cirq.X can also be used for cirq.ops.X\n", "print(unitary_operation)" ] }, { "cell_type": "markdown", "metadata": { "id": "6771bf1c439a" }, "source": [ "Cirq makes a distinction between gates (independent of qubits) and operations (gates acting on qubits). Thus `cirq.X` is a gate where `cirq.X.on(qubit)` is an operation. See the [guide on gates](gates.ipynb) for more details and additional common unitaries defined in Cirq." ] }, { "cell_type": "markdown", "metadata": { "id": "4aad9e53cc3b" }, "source": [ "> **Note**: The method `cirq.X.on_each` is a utility to apply `cirq.X` to multiple qubits. Similarly for other operations." ] }, { "cell_type": "markdown", "metadata": { "id": "7074f82814a8" }, "source": [ "Every `cirq.Operation` supports the `cirq.channel` protocol which returns its Kraus operators. (Read more about [protocols](protocols.ipynb) in Cirq.)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2025-05-30T09:52:59.896173Z", "iopub.status.busy": "2025-05-30T09:52:59.895928Z", "iopub.status.idle": "2025-05-30T09:52:59.900858Z", "shell.execute_reply": "2025-05-30T09:52:59.899921Z" }, "id": "8690317e6bdc" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Kraus operators of X are:\n", "[[0.+0.j 1.+0.j]\n", " [1.+0.j 0.+0.j]]\n" ] } ], "source": [ "kraus_ops = cirq.kraus(unitary_operation)\n", "print(f\"Kraus operators of {unitary_operation.gate} are:\", *kraus_ops, sep=\"\\n\")" ] }, { "cell_type": "markdown", "metadata": { "id": "775d97a27450" }, "source": [ "Unitary operators also support the `cirq.unitary` protocol." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2025-05-30T09:52:59.903825Z", "iopub.status.busy": "2025-05-30T09:52:59.903200Z", "iopub.status.idle": "2025-05-30T09:52:59.907716Z", "shell.execute_reply": "2025-05-30T09:52:59.906967Z" }, "id": "b50465b311e3" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Unitary of X is:\n", " [[0.+0.j 1.+0.j]\n", " [1.+0.j 0.+0.j]]\n" ] } ], "source": [ "unitary = cirq.unitary(cirq.ops.X)\n", "print(f\"Unitary of {unitary_operation.gate} is:\\n\", unitary)" ] }, { "cell_type": "markdown", "metadata": { "id": "19c518b04864" }, "source": [ "Unitary gates can be raised to powers, for example to implement a $\\sqrt{X}$ operation." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2025-05-30T09:52:59.910610Z", "iopub.status.busy": "2025-05-30T09:52:59.910043Z", "iopub.status.idle": "2025-05-30T09:52:59.913966Z", "shell.execute_reply": "2025-05-30T09:52:59.913400Z" }, "id": "96a7a994333c" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.5+0.5j 0.5-0.5j]\n", " [0.5-0.5j 0.5+0.5j]]\n" ] } ], "source": [ "sqrt_not = cirq.X ** (1 / 2)\n", "print(cirq.unitary(sqrt_not))" ] }, { "cell_type": "markdown", "metadata": { "id": "8d9eca419bbb" }, "source": [ "Any gate can be controlled via `cirq.ControlledGate` as follows." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2025-05-30T09:52:59.916463Z", "iopub.status.busy": "2025-05-30T09:52:59.916062Z", "iopub.status.idle": "2025-05-30T09:52:59.920719Z", "shell.execute_reply": "2025-05-30T09:52:59.920103Z" }, "id": "db3aadb0f0ff" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1. +0.j 0. +0.j 0. +0.j 0. +0.j]\n", " [ 0. +0.j 1. +0.j 0. +0.j 0. +0.j]\n", " [ 0. +0.j 0. +0.j 0.707+0.j 0.707+0.j]\n", " [ 0. +0.j 0. +0.j 0.707+0.j -0.707+0.j]]\n" ] } ], "source": [ "controlled_hadamard = cirq.ControlledGate(sub_gate=cirq.H, num_controls=1)\n", "print(cirq.unitary(controlled_hadamard).round(3))" ] }, { "cell_type": "markdown", "metadata": { "id": "0d9a466b502b" }, "source": [ "Custom gates can be defined as described in [this guide](custom_gates.ipynb). Some common subroutines which consist of several operations are pre-defined - e.g., `cirq.qft` returns the operations to implement the quantum Fourier transform." ] }, { "cell_type": "markdown", "metadata": { "id": "c68dc1d3ca0d" }, "source": [ "### Measurements" ] }, { "cell_type": "markdown", "metadata": { "id": "43505ede0131" }, "source": [ "Cirq supports measurements in the computational basis." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2025-05-30T09:52:59.923882Z", "iopub.status.busy": "2025-05-30T09:52:59.923264Z", "iopub.status.idle": "2025-05-30T09:52:59.927246Z", "shell.execute_reply": "2025-05-30T09:52:59.926637Z" }, "id": "e9f53647a2d7" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Measurement: cirq.MeasurementGate(1, cirq.MeasurementKey(name='key'), ())\n" ] } ], "source": [ "measurement = cirq.MeasurementGate(num_qubits=1, key=\"key\")\n", "print(\"Measurement:\", measurement)" ] }, { "cell_type": "markdown", "metadata": { "id": "00e6acd6ca82" }, "source": [ "The `key` can be used to identify results of measurements when [simulating circuits](../simulate/simulation.ipynb). A measurement gate acting on a qubit forms an operation." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2025-05-30T09:52:59.929862Z", "iopub.status.busy": "2025-05-30T09:52:59.929334Z", "iopub.status.idle": "2025-05-30T09:52:59.933120Z", "shell.execute_reply": "2025-05-30T09:52:59.932493Z" }, "id": "c5e87b2b60dd" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cirq.MeasurementGate(1, cirq.MeasurementKey(name='key'), ())(q(0))\n" ] } ], "source": [ "measurement_operation = measurement.on(qubit)\n", "print(measurement_operation)" ] }, { "cell_type": "markdown", "metadata": { "id": "1996e9afe508" }, "source": [ "> **Note**: The function `cirq.measure` is a utility to measure a single qubit, and the function `cirq.measure_each` is a utility to measure multiple qubits." ] }, { "cell_type": "markdown", "metadata": { "id": "fe89f00b0aad" }, "source": [ "Again measurement operations implement `cirq.Operation` so the `cirq.channel` protocol can be used to get the Kraus operators." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2025-05-30T09:52:59.935972Z", "iopub.status.busy": "2025-05-30T09:52:59.935375Z", "iopub.status.idle": "2025-05-30T09:52:59.939347Z", "shell.execute_reply": "2025-05-30T09:52:59.938786Z" }, "id": "33cffa2ff062" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Kraus operators of cirq.MeasurementGate(1, cirq.MeasurementKey(name='key'), ()) are:\n", "\n", "[[1. 0.]\n", " [0. 0.]]\n", "\n", "[[0. 0.]\n", " [0. 1.]]\n" ] } ], "source": [ "kraus_ops = cirq.kraus(measurement)\n", "print(f\"Kraus operators of {measurement} are:\", *kraus_ops, sep=\"\\n\\n\")" ] }, { "cell_type": "markdown", "metadata": { "id": "4bd48811b1ab" }, "source": [ "The functions `cirq.measure_state_vector` and `cirq.measure_density_matrix` can be used to perform computational basis measurements on state vectors and density matrices, respectively, represented by NumPy arrays." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2025-05-30T09:52:59.942174Z", "iopub.status.busy": "2025-05-30T09:52:59.941689Z", "iopub.status.idle": "2025-05-30T09:52:59.945725Z", "shell.execute_reply": "2025-05-30T09:52:59.945190Z" }, "id": "3993dbcab9cc" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Wavefunction:\n", " [0.707 0.707]\n" ] } ], "source": [ "psi = np.ones(shape=(2,)) / np.sqrt(2)\n", "print(\"Wavefunction:\\n\", psi.round(3))" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2025-05-30T09:52:59.948314Z", "iopub.status.busy": "2025-05-30T09:52:59.947700Z", "iopub.status.idle": "2025-05-30T09:52:59.952372Z", "shell.execute_reply": "2025-05-30T09:52:59.951659Z" }, "id": "018ba586e9b1" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Measured: 0\n", "Resultant state:\n", " [1. 0.]\n" ] } ], "source": [ "results, psi_prime = cirq.measure_state_vector(psi, indices=[0])\n", "\n", "print(\"Measured:\", results[0])\n", "print(\"Resultant state:\\n\", psi_prime)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2025-05-30T09:52:59.954920Z", "iopub.status.busy": "2025-05-30T09:52:59.954399Z", "iopub.status.idle": "2025-05-30T09:52:59.958350Z", "shell.execute_reply": "2025-05-30T09:52:59.957761Z" }, "id": "59d80d14827d" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "State:\n", " [[0.5 0.5]\n", " [0.5 0.5]]\n" ] } ], "source": [ "rho = np.ones(shape=(2, 2)) / 2.0\n", "print(\"State:\\n\", rho)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "execution": { "iopub.execute_input": "2025-05-30T09:52:59.961015Z", "iopub.status.busy": "2025-05-30T09:52:59.960452Z", "iopub.status.idle": "2025-05-30T09:52:59.964784Z", "shell.execute_reply": "2025-05-30T09:52:59.964109Z" }, "id": "c71ad678ed35" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Measured: 0\n", "Resultant state:\n", " [[1. 0.]\n", " [0. 0.]]\n" ] } ], "source": [ "measurements, rho_prime = cirq.measure_density_matrix(rho, indices=[0])\n", "\n", "print(\"Measured:\", measurements[0])\n", "print(\"Resultant state:\\n\", rho_prime)" ] }, { "cell_type": "markdown", "metadata": { "id": "1051898954e3" }, "source": [ "These functions do not modify the input state (`psi` or `rho`) unless the optional argument `out` is provided as the input state." ] }, { "cell_type": "markdown", "metadata": { "id": "be9296fe8291" }, "source": [ "### Noisy channels" ] }, { "cell_type": "markdown", "metadata": { "id": "c7240ca42a70" }, "source": [ "Like common unitary gates, Cirq defines many common noisy channels, for example the depolarizing channel below." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "execution": { "iopub.execute_input": "2025-05-30T09:52:59.967590Z", "iopub.status.busy": "2025-05-30T09:52:59.967051Z", "iopub.status.idle": "2025-05-30T09:52:59.970884Z", "shell.execute_reply": "2025-05-30T09:52:59.970259Z" }, "id": "7b38ee1528a7" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "depolarize(p=0.01)\n" ] } ], "source": [ "depo_channel = cirq.DepolarizingChannel(p=0.01, n_qubits=1)\n", "print(depo_channel)" ] }, { "cell_type": "markdown", "metadata": { "id": "ade1f59e1323" }, "source": [ "Just like unitary gates and measurements, noisy channels implement `cirq.Operation`, and we can always use `cirq.channel` to get the Kraus operators." ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "execution": { "iopub.execute_input": "2025-05-30T09:52:59.973620Z", "iopub.status.busy": "2025-05-30T09:52:59.973033Z", "iopub.status.idle": "2025-05-30T09:52:59.978425Z", "shell.execute_reply": "2025-05-30T09:52:59.977832Z" }, "id": "26639f48fec8" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Kraus operators of depolarize(p=0.01) are:\n", "\n", "[[0.99 0. ]\n", " [0. 0.99]]\n", "\n", "[[0. +0.j 0.06+0.j]\n", " [0.06+0.j 0. +0.j]]\n", "\n", "[[0.+0.j 0.-0.06j]\n", " [0.+0.06j 0.+0.j ]]\n", "\n", "[[ 0.06+0.j 0. +0.j]\n", " [ 0. +0.j -0.06+0.j]]\n" ] } ], "source": [ "kraus_ops = cirq.kraus(depo_channel)\n", "print(f\"Kraus operators of {depo_channel} are:\", *[op.round(2) for op in kraus_ops], sep=\"\\n\\n\")" ] }, { "cell_type": "markdown", "metadata": { "id": "82535883b164" }, "source": [ "Some channels can be written\n", "\n", "$$\n", "\\rho \\mapsto \\sum_k p_k U_k \\rho U_k ^\\dagger\n", "$$\n", "\n", "where real numbers $p_k$ form a probability distribution and $U_k$ are unitary. Such a *probabilistic mixture* of unitaries supports the `cirq.mixture` protocol which returns $p_k$ and $U_k$. An example is shown below for the bit-flip channel $\\rho \\mapsto (1 - p) \\rho + p X \\rho X$." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "execution": { "iopub.execute_input": "2025-05-30T09:52:59.981049Z", "iopub.status.busy": "2025-05-30T09:52:59.980567Z", "iopub.status.idle": "2025-05-30T09:52:59.985440Z", "shell.execute_reply": "2025-05-30T09:52:59.984628Z" }, "id": "14450e5ea043" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "With probability 0.95, apply \n", "[[1. 0.]\n", " [0. 1.]]\n", "\n", "With probability 0.05, apply \n", "[[0.+0.j 1.+0.j]\n", " [1.+0.j 0.+0.j]]\n", "\n" ] } ], "source": [ "bit_flip = cirq.bit_flip(p=0.05)\n", "probs, unitaries = cirq.mixture(bit_flip)\n", "\n", "for prob, unitary in cirq.mixture(bit_flip):\n", " print(f\"With probability {prob}, apply \\n{unitary}\\n\")" ] }, { "cell_type": "markdown", "metadata": { "id": "84d85d11ec0b" }, "source": [ "> **Note**: Any unitary gate/operation supports `cirq.mixture` because it can be interpreted as applying a single unitary with probability one." ] }, { "cell_type": "markdown", "metadata": { "id": "aa5f8ad751d3" }, "source": [ "Custom noisy channels can be defined as described in [this guide](../simulate/noisy_simulation.ipynb)." ] }, { "cell_type": "markdown", "metadata": { "id": "712d08a1aede" }, "source": [ "### In circuits" ] }, { "cell_type": "markdown", "metadata": { "id": "4797150e7c2b" }, "source": [ "Any `cirq.Operation` (pre-defined or user-defined) can be placed in a `cirq.Circuit`. An example with a unitary, noisy channel, and measurement is shown below." ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "execution": { "iopub.execute_input": "2025-05-30T09:52:59.988681Z", "iopub.status.busy": "2025-05-30T09:52:59.988220Z", "iopub.status.idle": "2025-05-30T09:52:59.994511Z", "shell.execute_reply": "2025-05-30T09:52:59.993842Z" }, "id": "4005ebd62eab" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0: ───H───D(0.01)───M───\n" ] } ], "source": [ "circuit = cirq.Circuit(cirq.H(qubit), cirq.depolarize(p=0.01).on(qubit), cirq.measure(qubit))\n", "print(circuit)" ] }, { "cell_type": "markdown", "metadata": { "id": "9eeae0d8a85e" }, "source": [ "The general input to the circuit constructor is a `cirq.OP_TREE`, i.e., an operation or nested collection of operations. Circuits can be manipulated as described in the [circuits guide](circuits.ipynb) and simulated as described in the [simulation guide](../simulate/simulation.ipynb)." ] }, { "cell_type": "markdown", "metadata": { "id": "8vIVDW1JZ2F5" }, "source": [ "### Alternate representations\n", "\n", "In addition to the above representations for operators. Cirq also supports some more non-standard representations as well. To convert a set of kraus operators to a choi representation you can do:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "execution": { "iopub.execute_input": "2025-05-30T09:52:59.997440Z", "iopub.status.busy": "2025-05-30T09:52:59.996883Z", "iopub.status.idle": "2025-05-30T09:53:00.001725Z", "shell.execute_reply": "2025-05-30T09:53:00.001092Z" }, "id": "Lck6naRWaQeR" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(array([[0.99498744, 0. ],\n", " [0. , 0.99498744]]), array([[0. +0.j, 0.05773503+0.j],\n", " [0.05773503+0.j, 0. +0.j]]), array([[0.+0.j , 0.-0.05773503j],\n", " [0.+0.05773503j, 0.+0.j ]]), array([[ 0.05773503+0.j, 0. +0.j],\n", " [ 0. +0.j, -0.05773503+0.j]]))\n" ] } ], "source": [ "depo_channel = cirq.DepolarizingChannel(p=0.01, n_qubits=1)\n", "kraus_rep = cirq.kraus(depo_channel)\n", "print(kraus_rep)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "execution": { "iopub.execute_input": "2025-05-30T09:53:00.004127Z", "iopub.status.busy": "2025-05-30T09:53:00.003562Z", "iopub.status.idle": "2025-05-30T09:53:00.007744Z", "shell.execute_reply": "2025-05-30T09:53:00.007094Z" }, "id": "7qyxleiKaW64" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.99333333+0.j 0. +0.j 0. +0.j 0.98666667+0.j]\n", " [0. +0.j 0.00666667+0.j 0. +0.j 0. +0.j]\n", " [0. +0.j 0. +0.j 0.00666667+0.j 0. +0.j]\n", " [0.98666667+0.j 0. +0.j 0. +0.j 0.99333333+0.j]]\n" ] } ], "source": [ "choi_rep = cirq.kraus_to_choi(kraus_rep)\n", "print(choi_rep)" ] }, { "cell_type": "markdown", "metadata": { "id": "XeI2KlEwbmn-" }, "source": [ "And to get the superoperator representation you can do:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "execution": { "iopub.execute_input": "2025-05-30T09:53:00.010231Z", "iopub.status.busy": "2025-05-30T09:53:00.009708Z", "iopub.status.idle": "2025-05-30T09:53:00.013647Z", "shell.execute_reply": "2025-05-30T09:53:00.013089Z" }, "id": "x2weMpDSbqpW" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.99333333+0.j 0. +0.j 0. +0.j 0.00666667+0.j]\n", " [0. +0.j 0.98666667+0.j 0. +0.j 0. +0.j]\n", " [0. +0.j 0. +0.j 0.98666667+0.j 0. +0.j]\n", " [0.00666667+0.j 0. +0.j 0. +0.j 0.99333333+0.j]]\n" ] } ], "source": [ "super_rep = cirq.kraus_to_superoperator(kraus_rep)\n", "print(super_rep)" ] } ], "metadata": { "colab": { "collapsed_sections": [ "Sh9QBnKbFf_B" ], "name": "operators.ipynb", "toc_visible": true }, "kernelspec": { "display_name": "Python 3", "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.10.17" } }, "nbformat": 4, "nbformat_minor": 0 }