{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "fd07a3db-1aa2-4884-98db-6f68765c3edc",
      "metadata": {},
      "source": [
        "---\n",
        "title: \"ノイズモデルを構築する\"\n",
        "description: \"エラー管理のためのノイズモデルの構築方法について学びましょう。\"\n",
        "---\n",
        "\n",
        "<span id=\"build-noise-models\" />\n",
        "\n",
        "# ノイズモデルを構築する\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "35e9a88b-6872-42b0-ac4f-36bf8898f2fa",
      "metadata": {
        "tags": [
          "version-info"
        ]
      },
      "source": [
        "{/*\n",
        "  DO NOT EDIT THIS CELL!!!\n",
        "  This cell's content is generated automatically by a script. Anything you add\n",
        "  here will be removed next time the notebook is run. To add new content, create\n",
        "  a new cell before or after this one.\n",
        "  */}\n",
        "\n",
        "<Accordion>\n",
        "  <AccordionItem title=\"パッケージ・バージョン\">\n",
        "    このページのコードは、以下の要件に基づいて開発された。\n",
        "    これらのバージョンまたは新しいバージョンの使用をお勧めします。\n",
        "\n",
        "    ```\n",
        "    qiskit[all]~=2.5.1\n",
        "    qiskit-ibm-runtime~=0.47.0\n",
        "    qiskit-aer~=0.17\n",
        "    ```\n",
        "  </AccordionItem>\n",
        "</Accordion>\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "19c35f2c-437a-48b7-8c9f-9ac3526811a7",
      "metadata": {},
      "source": [
        "このページではQiskit Aer [`noise`](https://qiskit.org/ecosystem/aer/apidocs/aer_noise.html) モジュールを使用して、誤差が存在する量子回路をシミュレーションするためのノイズモデルを構築する方法を紹介します。 これは、ノイズの多い量子プロセッサーのエミュレートや、量子アルゴリズムの実行に対するノイズの影響の研究に役立つ。\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "id": "85a94e7b-6b43-4a4a-a3b6-a412e1d66c7d",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-08-19T17:00:43.403378Z",
          "start_time": "2019-08-19T17:00:41.139269Z"
        }
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "from qiskit import QuantumCircuit\n",
        "from qiskit.quantum_info import Kraus, SuperOp\n",
        "from qiskit.visualization import plot_histogram\n",
        "from qiskit.transpiler import generate_preset_pass_manager\n",
        "from qiskit_aer import AerSimulator\n",
        "\n",
        "# Import from Qiskit Aer noise module\n",
        "from qiskit_aer.noise import (\n",
        "    NoiseModel,\n",
        "    QuantumError,\n",
        "    ReadoutError,\n",
        "    depolarizing_error,\n",
        "    pauli_error,\n",
        "    thermal_relaxation_error,\n",
        ")"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "da49be49-d174-413c-b05d-20236817aac2",
      "metadata": {
        "slideshow": {
          "slide_type": "slide"
        }
      },
      "source": [
        "<span id=\"qiskit-aer-noise-module\" />\n",
        "\n",
        "## Qiskit Aer `noise` モジュール\n",
        "\n",
        "Qiskit Aer `noise` モジュールには、シミュレーション用にカスタマイズされたノイズモデルを構築するための Python クラスが含まれています。 主要なクラスは3つある：\n",
        "\n",
        "1. `NoiseModel` ノイズシミュレーションに使用されるノイズモデルを格納するクラス。\n",
        "\n",
        "2. CPTP ゲートエラーを記述する `QuantumError` クラス。 これらを適用することができる：\n",
        "   * *ゲート*または*リセット*指示後\n",
        "   * *測定*前の注意事項\n",
        "\n",
        "3. `ReadoutError` 古典的な読み出しエラーを記述するクラス。\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "ea2762d5-7260-4ce9-9e44-2950331ba3e7",
      "metadata": {},
      "source": [
        "<span id=\"initialize-a-noise-model-from-a-backend\" />\n",
        "\n",
        "## バックエンドからノイズモデルを初期化する\n",
        "\n",
        "物理バックエンドの最新のキャリブレーションデータに基づいてパラメータを設定し、ノイズモデルを初期化することができます。\n",
        "\n",
        "<Admonition type=\"note\">\n",
        "  これらの例では の `FakeSherbrooke``qiskit_ibm_runtime` モックバックエンドを使用していますが、Qiskitと互換性のある実在または模擬のバックエンドであれば、どれでも試すことができます。\n",
        "</Admonition>\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "id": "b47efd06-6a64-455a-be12-07054e800a34",
      "metadata": {},
      "outputs": [],
      "source": [
        "from qiskit_ibm_runtime.fake_provider import FakeSherbrooke\n",
        "\n",
        "backend = FakeSherbrooke()\n",
        "noise_model = NoiseModel.from_backend(backend)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "54f7cb42-dd95-4089-afbb-db9e83d41b50",
      "metadata": {},
      "source": [
        "これにより、そのバックエンドを使用したときに遭遇するであろうエラーにほぼ近似したノイズモデルが得られる。 ノイズモデルのパラメータをより詳細にコントロールしたい場合は、このページの残りの部分で説明するように、独自のノイズモデルを作成する必要があります。\n",
        "\n"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "a4940a81-d6ab-4f15-8505-05282c67d823",
      "metadata": {},
      "source": [
        "<span id=\"quantum-errors\" />\n",
        "\n",
        "## 量子エラー\n",
        "\n",
        "オブジェクト `QuantumError` を直接扱う代わりに、特定の種類のパラメータ化された量子誤差を自動的に生成する多くのヘルパー関数が存在する。 これらは `noise` モジュールに含まれており、量子コンピューティング研究で使用される多くの一般的なエラータイプに対応する関数を含んでいます。 関数名とそれらが返すエラーの種類は以下の通りです：\n",
        "\n",
        "| 標準誤差関数                          | 詳細                                                                                             |\n",
        "| ------------------------------- | ---------------------------------------------------------------------------------------------- |\n",
        "| `kraus_error`                   | クラウス行列のリストとして与えられる一般的なn量子ビットCPTPエラーチャネル $[K_0, ...]$。                                          |\n",
        "| `mixed_unitary_error`           | n量子ビットの混合ユニタリーエラーは、ユニタリー行列と確率 $[(U_0, p_0),...]$ のリストとして与えられる。                                 |\n",
        "| `coherent_unitary_error`        | n-qubit コヒーレントユニタリーエラーは、単一のユニタリー行列 $U$ として与えられる。                                               |\n",
        "| `pauli_error`                   | パウリと確率のリストとして与えられるn量子ビットのパウリ誤差チャンネル（混合ユニタリー）。 $[(P_0, p_0),...]$                               |\n",
        "| `depolarizing_error`            | 脱分極確率 $p$ によってパラメータ化されたn量子ビットの脱分極エラーチャネル。                                                      |\n",
        "| `reset_error`                   | 単一量子ビットリセットエラーは、 $\\vert1\\rangle$ $\\vert0\\rangle$ 状態 へのリセット $p_0, p_1$ 確率 によってパラメータ化される。        |\n",
        "| `thermal_relaxation_error`      | 緩和時間定数 $T_1$、 $T_2$、ゲート時間 $t$、励起状態熱ポピュレーション $p_1$ によってパラメータ化された単一量子ビット熱緩和チャネル。                 |\n",
        "| `phase_amplitude_damping_error` | 振幅減衰パラメータ $\\lambda$、位相減衰パラメータ $\\gamma$、励起状態熱集団 $p_1$ によって与えられる単一量子ビットの一般化された位相と振幅の複合減衰エラーチャネル。 |\n",
        "| `amplitude_damping_error`       | 振幅減衰パラメータ $\\lambda$ と励起状態熱母集団 $p_1$ によって与えられる1量子ビット一般化振幅減衰誤差チャネル。                              |\n",
        "| `phase_damping_error`           | 位相減衰パラメータによって与えられる単一量子ビット $\\gamma$ の位相減衰エラーチャネル。                                               |\n",
        "\n",
        "<span id=\"combine-quantum-errors\" />\n",
        "\n",
        "### 量子エラーを統合する\n",
        "\n",
        "`QuantumError` インスタンスは、合成、テンソル積、テンソル展開（逆順テンソル積）を使って、新しい `QuantumErrors` ：\n",
        "\n",
        "* 組成： $\\cal{E}(\\rho)=\\cal{E_2}(\\cal{E_1}(\\rho))$ として `error = error1.compose(error2)`\n",
        "* テンソル積： $\\cal{E}(\\rho) =(\\cal{E_1}\\otimes\\cal{E_2})(\\rho)$ として `error = error1.tensor(error2)`\n",
        "* 製品を拡張: $\\cal{E}(\\rho) =(\\cal{E_2}\\otimes\\cal{E_1})(\\rho)$ as `error = error1.expand(error2)`\n",
        "\n"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "cd42ec1c-a971-4dd8-913c-2a4fd4f4a845",
      "metadata": {},
      "source": [
        "<span id=\"example\" />\n",
        "\n",
        "### 例\n",
        "\n",
        "5％の1量子ビットのビット反転エラーを構築する：\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "id": "437a8576-084c-499f-af7e-12b0a2fbfcd0",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-08-19T17:00:43.420358Z",
          "start_time": "2019-08-19T17:00:43.416062Z"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "QuantumError on 1 qubits. Noise circuits:\n",
            "  P(0) = 0.05, Circuit = \n",
            "   ┌───┐\n",
            "q: ┤ X ├\n",
            "   └───┘\n",
            "  P(1) = 0.95, Circuit = \n",
            "   ┌───┐\n",
            "q: ┤ I ├\n",
            "   └───┘\n",
            "QuantumError on 1 qubits. Noise circuits:\n",
            "  P(0) = 0.05, Circuit = \n",
            "   ┌───┐\n",
            "q: ┤ Z ├\n",
            "   └───┘\n",
            "  P(1) = 0.95, Circuit = \n",
            "   ┌───┐\n",
            "q: ┤ I ├\n",
            "   └───┘\n"
          ]
        }
      ],
      "source": [
        "# Construct a 1-qubit bit-flip and phase-flip errors\n",
        "p_error = 0.05\n",
        "bit_flip = pauli_error([(\"X\", p_error), (\"I\", 1 - p_error)])\n",
        "phase_flip = pauli_error([(\"Z\", p_error), (\"I\", 1 - p_error)])\n",
        "print(bit_flip)\n",
        "print(phase_flip)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "id": "d71ef2d9-e386-4649-9e1c-a7bfe7342687",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-08-19T17:00:43.435843Z",
          "start_time": "2019-08-19T17:00:43.432211Z"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "QuantumError on 1 qubits. Noise circuits:\n",
            "  P(0) = 0.0025000000000000005, Circuit = \n",
            "   ┌───┐┌───┐\n",
            "q: ┤ X ├┤ Z ├\n",
            "   └───┘└───┘\n",
            "  P(1) = 0.0475, Circuit = \n",
            "   ┌───┐┌───┐\n",
            "q: ┤ X ├┤ I ├\n",
            "   └───┘└───┘\n",
            "  P(2) = 0.0475, Circuit = \n",
            "   ┌───┐┌───┐\n",
            "q: ┤ I ├┤ Z ├\n",
            "   └───┘└───┘\n",
            "  P(3) = 0.9025, Circuit = \n",
            "   ┌───┐┌───┐\n",
            "q: ┤ I ├┤ I ├\n",
            "   └───┘└───┘\n"
          ]
        }
      ],
      "source": [
        "# Compose two bit-flip and phase-flip errors\n",
        "bitphase_flip = bit_flip.compose(phase_flip)\n",
        "print(bitphase_flip)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "id": "ffa9fd9c-3d98-4285-8daf-ee22ca2b0d55",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-08-19T17:00:43.460191Z",
          "start_time": "2019-08-19T17:00:43.456782Z"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "QuantumError on 2 qubits. Noise circuits:\n",
            "  P(0) = 0.0025000000000000005, Circuit = \n",
            "     ┌───┐\n",
            "q_0: ┤ X ├\n",
            "     ├───┤\n",
            "q_1: ┤ Z ├\n",
            "     └───┘\n",
            "  P(1) = 0.0475, Circuit = \n",
            "     ┌───┐\n",
            "q_0: ┤ I ├\n",
            "     ├───┤\n",
            "q_1: ┤ Z ├\n",
            "     └───┘\n",
            "  P(2) = 0.0475, Circuit = \n",
            "     ┌───┐\n",
            "q_0: ┤ X ├\n",
            "     ├───┤\n",
            "q_1: ┤ I ├\n",
            "     └───┘\n",
            "  P(3) = 0.9025, Circuit = \n",
            "     ┌───┐\n",
            "q_0: ┤ I ├\n",
            "     ├───┤\n",
            "q_1: ┤ I ├\n",
            "     └───┘\n"
          ]
        }
      ],
      "source": [
        "# Tensor product two bit-flip and phase-flip errors with\n",
        "# bit-flip on qubit-0, phase-flip on qubit-1\n",
        "error2 = phase_flip.tensor(bit_flip)\n",
        "print(error2)"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "80e275c7-b856-4301-9266-0eb7a783e0f8",
      "metadata": {},
      "source": [
        "<span id=\"convert-to-and-from-quantumchannel-operators\" />\n",
        "\n",
        "### QuantumChannel 演算子との相互変換\n",
        "\n",
        "また、Qiskit Aerの `QuantumError` オブジェクトとQiskitの `QuantumChannel` オブジェクトを相互に変換することもできます。\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "id": "2006e158-bea6-4e18-a06c-31b567401b6c",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-08-19T17:00:43.482424Z",
          "start_time": "2019-08-19T17:00:43.473779Z"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Kraus([[[-9.74679434e-01+0.j,  0.00000000e+00+0.j],\n",
            "        [ 0.00000000e+00+0.j, -9.74679434e-01+0.j]],\n",
            "\n",
            "       [[ 0.00000000e+00+0.j,  2.23606798e-01+0.j],\n",
            "        [ 2.23606798e-01+0.j, -4.96506831e-17+0.j]]],\n",
            "      input_dims=(2,), output_dims=(2,))\n"
          ]
        }
      ],
      "source": [
        "# Convert to Kraus operator\n",
        "bit_flip_kraus = Kraus(bit_flip)\n",
        "print(bit_flip_kraus)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "id": "425a9b6f-e078-4a48-ab1c-a5621a8b773a",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-08-19T17:00:43.509521Z",
          "start_time": "2019-08-19T17:00:43.503976Z"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "SuperOp([[1. +0.j, 0. +0.j, 0. +0.j, 0. +0.j],\n",
            "         [0. +0.j, 0.9+0.j, 0. +0.j, 0. +0.j],\n",
            "         [0. +0.j, 0. +0.j, 0.9+0.j, 0. +0.j],\n",
            "         [0. +0.j, 0. +0.j, 0. +0.j, 1. +0.j]],\n",
            "        input_dims=(2,), output_dims=(2,))\n"
          ]
        }
      ],
      "source": [
        "# Convert to Superoperator\n",
        "phase_flip_sop = SuperOp(phase_flip)\n",
        "print(phase_flip_sop)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "id": "97624c98-f1c0-4a9b-be5b-2c664d368e9f",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-08-19T17:00:43.794037Z",
          "start_time": "2019-08-19T17:00:43.778223Z"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "QuantumError on 1 qubits. Noise circuits:\n",
            "  P(0) = 1.0, Circuit = \n",
            "   ┌───────┐\n",
            "q: ┤ kraus ├\n",
            "   └───────┘\n"
          ]
        },
        {
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "execution_count": 8,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Convert back to a quantum error\n",
        "print(QuantumError(bit_flip_kraus))\n",
        "\n",
        "# Check conversion is equivalent to original error\n",
        "QuantumError(bit_flip_kraus) == bit_flip"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "7f663dfe-e238-418c-bff4-c751468a6678",
      "metadata": {},
      "source": [
        "<span id=\"readout-error\" />\n",
        "\n",
        "### 読み出しエラー\n",
        "\n",
        "古典的な読み出し誤差は、割り当て確率ベクトルのリストによって $P(A|B)$ 指定される：\n",
        "\n",
        "* $A$ は*記録された*古典的なビット値\n",
        "* $B$ は測定から返された*真の*ビット値\n",
        "\n",
        "例えば、1量子ビットの場合： $ P(A|B) = [P(A|0), P(A|1)]$.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "id": "f70f39f8-dec5-46cb-a194-4768cffbd6db",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-08-19T17:00:44.659598Z",
          "start_time": "2019-08-19T17:00:44.654818Z"
        }
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "ReadoutError([[0.95 0.05]\n",
              " [0.1  0.9 ]])"
            ]
          },
          "execution_count": 9,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Measurement misassignment probabilities\n",
        "p0given1 = 0.1\n",
        "p1given0 = 0.05\n",
        "\n",
        "ReadoutError([[1 - p1given0, p1given0], [p0given1, 1 - p0given1]])"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "e77fcdcd-346c-4f78-83eb-0c32359ba62c",
      "metadata": {},
      "source": [
        "読み出し誤差は、量子誤差と同様に、 `compose`、 `tensor` 、 `expand` を使って組み合わせることもできる。\n",
        "\n"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "b2258427-2283-435c-b964-60423c3b0f39",
      "metadata": {},
      "source": [
        "<span id=\"add-errors-to-a-noise-model\" />\n",
        "\n",
        "## ノイズモデルに誤差を追加する\n",
        "\n",
        "量子エラーをノイズモデルに追加する場合、それが作用する*命令の*種類と、それを適用する量子ビットを指定しなければならない。 量子エラーには2つのケースがある：\n",
        "\n",
        "1. 全量子ビット量子エラー\n",
        "2. 特定量子ビットの量子エラー\n",
        "\n",
        "<span id=\"1-all-qubit-quantum-error\" />\n",
        "\n",
        "### 1. 全量子ビット量子エラー\n",
        "\n",
        "これは、どの量子ビットに作用するかに関係なく、どの命令に対しても同じエラーを適用する。\n",
        "\n",
        "`noise_model.add_all_qubit_quantum_error(error, instructions)` として追加される：\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "id": "0a5ae501-87e5-4930-96c1-b0af2d6993ce",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-08-19T17:00:45.882254Z",
          "start_time": "2019-08-19T17:00:45.877630Z"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "NoiseModel:\n",
            "  Basis gates: ['cx', 'id', 'rz', 'sx', 'u1', 'u2', 'u3']\n",
            "  Instructions with noise: ['u2', 'u1', 'u3']\n",
            "  All-qubits errors: ['u1', 'u2', 'u3']\n"
          ]
        }
      ],
      "source": [
        "# Create an empty noise model\n",
        "noise_model = NoiseModel()\n",
        "\n",
        "# Add depolarizing error to all single qubit u1, u2, u3 gates\n",
        "error = depolarizing_error(0.05, 1)\n",
        "noise_model.add_all_qubit_quantum_error(error, [\"u1\", \"u2\", \"u3\"])\n",
        "\n",
        "# Print noise model info\n",
        "print(noise_model)"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "f2f360be-5d7b-435d-980a-e20450a0267f",
      "metadata": {},
      "source": [
        "<span id=\"2-specific-qubit-quantum-error\" />\n",
        "\n",
        "### 2. 特定の量子ビットの量子エラー\n",
        "\n",
        "これは、指定された量子ビットのリストに作用する命令のすべての発生に対してエラーを適用する。 例えば、2量子ビットゲートの場合、 \\[0と1の]量子ビットに適用されるエラーは、 \\[1と0の]量子ビットに適用されるエラーとは異なる。\n",
        "\n",
        "`noise_model.add_quantum_error(error, instructions, qubits)` として追加される：\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "id": "05d7e61f-75dc-488b-bfab-c00ad87fe1ea",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-08-19T17:00:46.615959Z",
          "start_time": "2019-08-19T17:00:46.612055Z"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "NoiseModel:\n",
            "  Basis gates: ['cx', 'id', 'rz', 'sx', 'u1', 'u2', 'u3']\n",
            "  Instructions with noise: ['u2', 'u1', 'u3']\n",
            "  Qubits with noise: [0]\n",
            "  Specific qubit errors: [('u1', (0,)), ('u2', (0,)), ('u3', (0,))]\n"
          ]
        }
      ],
      "source": [
        "# Create an empty noise model\n",
        "noise_model = NoiseModel()\n",
        "\n",
        "# Add depolarizing error to all single qubit u1, u2, u3 gates on qubit 0 only\n",
        "error = depolarizing_error(0.05, 1)\n",
        "noise_model.add_quantum_error(error, [\"u1\", \"u2\", \"u3\"], [0])\n",
        "\n",
        "# Print noise model info\n",
        "print(noise_model)"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "c2dc1bb3-66a9-4d63-8341-0c4c45ed89d6",
      "metadata": {},
      "source": [
        "<span id=\"note-on-non-local-qubit-quantum-error\" />\n",
        "\n",
        "### 非局所量子ビットの量子エラーに関する注記\n",
        "\n",
        "`NoiseModel` 非局所的な量子ビットの量子エラーの追加はサポートしていません。 これらは.の外で処理 `NoiseModel`すべきです。 つまり、 [独自の](/docs/guides/custom-transpiler-pass)条件で量子エラーを回路に挿入する必要がある場合は、独自のトランスパイラ・パス（`TransformationPass`）を作成し、シミュレータを実行する直前にそのパスを実行すべきだということです。\n",
        "\n"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "2617effb-ae2e-44a3-8a7b-28b68b756ac6",
      "metadata": {},
      "source": [
        "<span id=\"execute-a-noisy-simulation-with-a-noise-model\" />\n",
        "\n",
        "### ノイズモデルを用いたノイズシミュレーションを実行する\n",
        "\n",
        "`AerSimulator(noise_model=noise_model)` コマンドは、与えられたノイズモデルに設定されたシミュレータを返します。 シミュレータのノイズモデルを設定するだけでなく、ノイズモデルのゲートに従って、シミュレータの基底ゲートをオーバーライドします。\n",
        "\n"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "7df257fd-424b-486e-a607-7d4c392b4c98",
      "metadata": {
        "slideshow": {
          "slide_type": "subslide"
        }
      },
      "source": [
        "<span id=\"noise-model-examples\" />\n",
        "\n",
        "## ノイズモデルの例\n",
        "\n",
        "次に、ノイズモデルの例をいくつか示します。 デモンストレーションでは、n量子ビットGHZ状態を生成する単純なテスト回路を使用します：\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "id": "43df6b1f-b07a-4bf2-bba6-c7ec35eec620",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-08-19T17:00:48.817405Z",
          "start_time": "2019-08-19T17:00:48.806966Z"
        },
        "slideshow": {
          "slide_type": "fragment"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "        ┌───┐                ░ ┌─┐         \n",
            "   q_0: ┤ H ├──■─────────────░─┤M├─────────\n",
            "        └───┘┌─┴─┐           ░ └╥┘┌─┐      \n",
            "   q_1: ─────┤ X ├──■────────░──╫─┤M├──────\n",
            "             └───┘┌─┴─┐      ░  ║ └╥┘┌─┐   \n",
            "   q_2: ──────────┤ X ├──■───░──╫──╫─┤M├───\n",
            "                  └───┘┌─┴─┐ ░  ║  ║ └╥┘┌─┐\n",
            "   q_3: ───────────────┤ X ├─░──╫──╫──╫─┤M├\n",
            "                       └───┘ ░  ║  ║  ║ └╥┘\n",
            "meas: 4/════════════════════════╩══╩══╩══╩═\n",
            "                                0  1  2  3 \n"
          ]
        }
      ],
      "source": [
        "# System Specification\n",
        "n_qubits = 4\n",
        "circ = QuantumCircuit(n_qubits)\n",
        "\n",
        "# Test Circuit\n",
        "circ.h(0)\n",
        "for qubit in range(n_qubits - 1):\n",
        "    circ.cx(qubit, qubit + 1)\n",
        "circ.measure_all()\n",
        "print(circ)"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "eea319ec-8764-4d77-8863-c5a2e9ae370a",
      "metadata": {},
      "source": [
        "<span id=\"ideal-simulation\" />\n",
        "\n",
        "### 理想的なシミュレーション\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "id": "bc713d11-755e-41e4-94f0-1ed76e3c2469",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-08-19T17:00:50.560988Z",
          "start_time": "2019-08-19T17:00:50.415545Z"
        }
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/build-noise-models/extracted-outputs/bc713d11-755e-41e4-94f0-1ed76e3c2469-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 13,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Ideal simulator and execution\n",
        "sim_ideal = AerSimulator()\n",
        "result_ideal = sim_ideal.run(circ).result()\n",
        "plot_histogram(result_ideal.get_counts(0))"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "ac7111a6-04dc-4068-9044-54d93e745116",
      "metadata": {
        "slideshow": {
          "slide_type": "subslide"
        }
      },
      "source": [
        "<span id=\"noise-example-1-basic-bit-flip-error-noise-model\" />\n",
        "\n",
        "## ノイズ例1：基本的なビット反転エラーノイズモデル\n",
        "\n",
        "量子情報理論の研究でよく使われる簡単なおもちゃのノイズモデルの例を考えてみよう：\n",
        "\n",
        "* 単一量子ビットゲートを適用する場合、 `p_gate1` の確率で量子ビットの状態を反転させる。\n",
        "* 2量子ビットのゲートを適用する場合、それぞれの量子ビットに1量子ビットのエラーを適用する。\n",
        "* 量子ビットをリセットする場合、確率 `p_reset` で 0 ではなく 1 にリセットする。\n",
        "* 量子ビットを測定するとき、確率 `p_meas` で量子ビットの状態を反転させる。\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "id": "fee3c383-0499-4d1f-be9f-32b41a31f561",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-08-19T17:00:51.543615Z",
          "start_time": "2019-08-19T17:00:51.536564Z"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "NoiseModel:\n",
            "  Basis gates: ['cx', 'id', 'rz', 'sx', 'u1', 'u2', 'u3']\n",
            "  Instructions with noise: ['u3', 'u1', 'u2', 'reset', 'cx', 'measure']\n",
            "  All-qubits errors: ['reset', 'measure', 'u1', 'u2', 'u3', 'cx']\n"
          ]
        }
      ],
      "source": [
        "# Example error probabilities\n",
        "p_reset = 0.03\n",
        "p_meas = 0.1\n",
        "p_gate1 = 0.05\n",
        "\n",
        "# QuantumError objects\n",
        "error_reset = pauli_error([(\"X\", p_reset), (\"I\", 1 - p_reset)])\n",
        "error_meas = pauli_error([(\"X\", p_meas), (\"I\", 1 - p_meas)])\n",
        "error_gate1 = pauli_error([(\"X\", p_gate1), (\"I\", 1 - p_gate1)])\n",
        "error_gate2 = error_gate1.tensor(error_gate1)\n",
        "\n",
        "# Add errors to noise model\n",
        "noise_bit_flip = NoiseModel()\n",
        "noise_bit_flip.add_all_qubit_quantum_error(error_reset, \"reset\")\n",
        "noise_bit_flip.add_all_qubit_quantum_error(error_meas, \"measure\")\n",
        "noise_bit_flip.add_all_qubit_quantum_error(error_gate1, [\"u1\", \"u2\", \"u3\"])\n",
        "noise_bit_flip.add_all_qubit_quantum_error(error_gate2, [\"cx\"])\n",
        "\n",
        "print(noise_bit_flip)"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "dbf3f5ee-0f66-4a37-b512-038a21ce2ed2",
      "metadata": {},
      "source": [
        "<span id=\"execute-the-noisy-simulation\" />\n",
        "\n",
        "### ノイズの多いシミュレーションを実行する\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "id": "abeb9f09-d762-406d-983e-0357ade59636",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-08-19T17:00:52.951874Z",
          "start_time": "2019-08-19T17:00:52.687440Z"
        },
        "slideshow": {
          "slide_type": "-"
        }
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/build-noise-models/extracted-outputs/abeb9f09-d762-406d-983e-0357ade59636-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 15,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Create noisy simulator backend\n",
        "sim_noise = AerSimulator(noise_model=noise_bit_flip)\n",
        "\n",
        "# Transpile circuit for noisy basis gates\n",
        "passmanager = generate_preset_pass_manager(\n",
        "    optimization_level=3, backend=sim_noise\n",
        ")\n",
        "circ_tnoise = passmanager.run(circ)\n",
        "\n",
        "# Run and get counts\n",
        "result_bit_flip = sim_noise.run(circ_tnoise).result()\n",
        "counts_bit_flip = result_bit_flip.get_counts(0)\n",
        "\n",
        "# Plot noisy output\n",
        "plot_histogram(counts_bit_flip)"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "ced838fa-105d-4bdf-b240-51b78ff18e29",
      "metadata": {},
      "source": [
        "<span id=\"example-2-t1/t2-thermal-relaxation\" />\n",
        "\n",
        "## 例2： T1/T2 熱緩和\n",
        "\n",
        "ここで、量子ビット環境との熱緩和に基づく、より現実的な誤差モデルを考えてみよう：\n",
        "\n",
        "* それぞれの量子ビットは、熱緩和時定数 $T_1$ とデフェーズ時定数 $T_2$ によってパラメータ化される。\n",
        "* なお、 $T_2 \\le 2 T_1$。\n",
        "* 命令のエラー率は、ゲート時間と量子ビット $T_1$、 $T_2$ の値によって決まる。\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "id": "b2ab3829-98fa-4b5d-b622-25c155abfdf0",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-08-19T17:00:54.577456Z",
          "start_time": "2019-08-19T17:00:54.491018Z"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "NoiseModel:\n",
            "  Basis gates: ['cx', 'id', 'rz', 'sx', 'u2', 'u3']\n",
            "  Instructions with noise: ['u3', 'u2', 'reset', 'cx', 'measure']\n",
            "  Qubits with noise: [0, 1, 2, 3]\n",
            "  Specific qubit errors: [('reset', (0,)), ('reset', (1,)), ('reset', (2,)), ('reset', (3,)), ('measure', (0,)), ('measure', (1,)), ('measure', (2,)), ('measure', (3,)), ('u2', (0,)), ('u2', (1,)), ('u2', (2,)), ('u2', (3,)), ('u3', (0,)), ('u3', (1,)), ('u3', (2,)), ('u3', (3,)), ('cx', (0, 0)), ('cx', (0, 1)), ('cx', (0, 2)), ('cx', (0, 3)), ('cx', (1, 0)), ('cx', (1, 1)), ('cx', (1, 2)), ('cx', (1, 3)), ('cx', (2, 0)), ('cx', (2, 1)), ('cx', (2, 2)), ('cx', (2, 3)), ('cx', (3, 0)), ('cx', (3, 1)), ('cx', (3, 2)), ('cx', (3, 3))]\n"
          ]
        }
      ],
      "source": [
        "# T1 and T2 values for qubits 0-3\n",
        "T1s = np.random.normal(\n",
        "    50e3, 10e3, 4\n",
        ")  # Sampled from normal distribution mean 50 microsec\n",
        "T2s = np.random.normal(\n",
        "    70e3, 10e3, 4\n",
        ")  # Sampled from normal distribution mean 50 microsec\n",
        "\n",
        "# Truncate random T2s <= T1s\n",
        "T2s = np.array([min(T2s[j], 2 * T1s[j]) for j in range(4)])\n",
        "\n",
        "# Instruction times (in nanoseconds)\n",
        "time_u1 = 0  # virtual gate\n",
        "time_u2 = 50  # (single X90 pulse)\n",
        "time_u3 = 100  # (two X90 pulses)\n",
        "time_cx = 300\n",
        "time_reset = 1000  # 1 microsecond\n",
        "time_measure = 1000  # 1 microsecond\n",
        "\n",
        "# QuantumError objects\n",
        "errors_reset = [\n",
        "    thermal_relaxation_error(t1, t2, time_reset) for t1, t2 in zip(T1s, T2s)\n",
        "]\n",
        "errors_measure = [\n",
        "    thermal_relaxation_error(t1, t2, time_measure) for t1, t2 in zip(T1s, T2s)\n",
        "]\n",
        "errors_u1 = [\n",
        "    thermal_relaxation_error(t1, t2, time_u1) for t1, t2 in zip(T1s, T2s)\n",
        "]\n",
        "errors_u2 = [\n",
        "    thermal_relaxation_error(t1, t2, time_u2) for t1, t2 in zip(T1s, T2s)\n",
        "]\n",
        "errors_u3 = [\n",
        "    thermal_relaxation_error(t1, t2, time_u3) for t1, t2 in zip(T1s, T2s)\n",
        "]\n",
        "errors_cx = [\n",
        "    [\n",
        "        thermal_relaxation_error(t1a, t2a, time_cx).expand(\n",
        "            thermal_relaxation_error(t1b, t2b, time_cx)\n",
        "        )\n",
        "        for t1a, t2a in zip(T1s, T2s)\n",
        "    ]\n",
        "    for t1b, t2b in zip(T1s, T2s)\n",
        "]\n",
        "\n",
        "# Add errors to noise model\n",
        "noise_thermal = NoiseModel()\n",
        "for j in range(4):\n",
        "    noise_thermal.add_quantum_error(errors_reset[j], \"reset\", [j])\n",
        "    noise_thermal.add_quantum_error(errors_measure[j], \"measure\", [j])\n",
        "    noise_thermal.add_quantum_error(errors_u1[j], \"u1\", [j])\n",
        "    noise_thermal.add_quantum_error(errors_u2[j], \"u2\", [j])\n",
        "    noise_thermal.add_quantum_error(errors_u3[j], \"u3\", [j])\n",
        "    for k in range(4):\n",
        "        noise_thermal.add_quantum_error(errors_cx[j][k], \"cx\", [j, k])\n",
        "\n",
        "print(noise_thermal)"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "a3dd27c5-c032-44b9-ada9-19280e8a0140",
      "metadata": {},
      "source": [
        "<span id=\"execute-the-noisy-simulation\" />\n",
        "\n",
        "### ノイズの多いシミュレーションを実行する\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 17,
      "id": "ff52bf52-1323-40fc-a631-2b1889b21b20",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-08-19T17:00:55.689241Z",
          "start_time": "2019-08-19T17:00:55.515394Z"
        }
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/build-noise-models/extracted-outputs/ff52bf52-1323-40fc-a631-2b1889b21b20-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 17,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Run the noisy simulation\n",
        "sim_thermal = AerSimulator(noise_model=noise_thermal)\n",
        "\n",
        "# Transpile circuit for noisy basis gates\n",
        "passmanager = generate_preset_pass_manager(\n",
        "    optimization_level=3, backend=sim_thermal\n",
        ")\n",
        "circ_tthermal = passmanager.run(circ)\n",
        "\n",
        "# Run and get counts\n",
        "result_thermal = sim_thermal.run(circ_tthermal).result()\n",
        "counts_thermal = result_thermal.get_counts(0)\n",
        "\n",
        "# Plot noisy output\n",
        "plot_histogram(counts_thermal)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "0df436e1-b43c-41b1-9581-a4d530e51a7b",
      "metadata": {},
      "source": [
        "<span id=\"next-steps\" />\n",
        "\n",
        "## 次のステップ\n",
        "\n",
        "<Admonition type=\"tip\" title=\"推奨事項\">\n",
        "  * ノイズの多い回路をシミュレートするには、 [「Qiskit Aerプリミティブを用いた正確なシミュレーションとノイズの多いシミュレーション」](/docs/guides/simulate-with-qiskit-sdk-primitives) を参照してください。\n",
        "  * [Qiskit Aerノイズモジュールの](https://qiskit.org/ecosystem/aer/apidocs/aer_noise.html)リファレンスを確認してください。\n",
        "</Admonition>\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "id": "a1b8767d",
      "source": "© IBM Corp., 2017-2026"
    }
  ],
  "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"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 4
}