{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "aea62c4c-1e42-4472-9f1d-e52783e81fc6",
      "metadata": {},
      "source": [
        "---\n",
        "title: \"回路ライブラリー\"\n",
        "description: \"Qiskit回路ライブラリが提供する、N-local回路、時間発展回路、データ符号化回路など、すぐに使える回路について詳しく読む\"\n",
        "---\n",
        "\n",
        "{/* cspell:ignore cout */}\n",
        "\n",
        "<span id=\"circuit-library\" />\n",
        "\n",
        "# 回路ライブラリー\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "14baab34-71a4-4d3c-bdc1-083bb2c32065",
      "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",
        "    ```\n",
        "  </AccordionItem>\n",
        "</Accordion>\n",
        "\n"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "6f257ff9-15c4-48d8-9503-7f0ab16a91b2",
      "metadata": {},
      "source": [
        "Qiskit SDKには、独自のプログラムのビルディングブロックとして使用する一般的な回路のライブラリが含まれています。 あらかじめ定義された回路を使うことで、調査、コード作成、デバッグの時間を節約できる。 このライブラリには、量子コンピューティングでよく使われる回路や、古典的なシミュレーションが困難な回路、量子ハードウェアのベンチマークに役立つ回路などが含まれている。\n",
        "\n",
        "このページでは、ライブラリが提供するさまざまな回路カテゴリを一覧表示します。 回路の全リストについては、 [回路ライブラリAPIドキュメントを](/docs/api/qiskit/circuit_library)参照のこと。\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "0b473350",
      "metadata": {},
      "source": [
        "<span id=\"standard-gates\" />\n",
        "\n",
        "## 標準ゲート\n",
        "\n",
        "回路ライブラリには、標準的な量子ゲートも含まれている。 より基本的なゲート（例えば `UGate` ）もあれば、通常は1量子ビットゲートや2量子ビットゲートから構築する必要がある多量子ビットゲートもある。 インポートされたゲートを回路に追加するには、 `append` メソッドを使います。最初の引数はゲートで、次の引数はゲートを適用する量子ビットのリストです。\n",
        "\n",
        "例えば、次のコード・セルは、ハダマード・ゲートとマルチ制御Xゲートを持つ回路を作成する。\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "id": "a846a845-7ac5-4c92-b124-d2b90a773ba2",
      "metadata": {
        "tags": [
          "ignore-warnings"
        ]
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/circuit-library/extracted-outputs/a846a845-7ac5-4c92-b124-d2b90a773ba2-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 1,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from qiskit import QuantumCircuit\n",
        "from qiskit.circuit.library import HGate, MCXGate\n",
        "\n",
        "mcx_gate = MCXGate(3)\n",
        "hadamard_gate = HGate()\n",
        "\n",
        "qc = QuantumCircuit(4)\n",
        "qc.append(hadamard_gate, [0])\n",
        "qc.append(mcx_gate, [0, 1, 2, 3])\n",
        "qc.draw(\"mpl\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "e5066595",
      "metadata": {},
      "source": [
        "回路ライブラリAPIドキュメントの[標準ゲートを](/docs/api/qiskit/circuit_library#standard-gates)参照。\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "f1d7c8c9-1b4d-45e1-9cd5-c5d76c2e25ab",
      "metadata": {},
      "source": [
        "<span id=\"n-local-circuits\" />\n",
        "\n",
        "## N-局所回路\n",
        "\n",
        "これらの回路は、1量子ビットの回転ゲートと多量子ビットのもつれゲートを交互に重ねている。\n",
        "\n",
        "この回路群は、さまざまな量子状態を作り出すことができるため、変分量子アルゴリズムでよく使われている。 変分アルゴリズムは、特定の特性（最適化問題の良い解を示す状態など）を持つ状態を見つけるために、ゲートのパラメーターを調整する。 この目的のために、ライブラリ内の多くの回路は**パラメータ化されて**おり、固定値なしで定義できる。\n",
        "\n",
        "次のコードセルは、 `n_local` 回路をインポートしたもので、エンタングルゲートは2量子ビットゲートである。 この回路は、パラメータ化された1量子ビットゲートのブロックをインターリーブし、その後に2量子ビットゲートのブロックをエンタングルする。 次のコードは、1量子RXゲートと2量子CZゲートを持つ3量子ビット回路を作成する。\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "id": "3ccaeb1b-03c6-4dfa-9000-e48db2516303",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/circuit-library/extracted-outputs/3ccaeb1b-03c6-4dfa-9000-e48db2516303-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 2,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from qiskit.circuit.library import n_local\n",
        "\n",
        "two_local = n_local(3, \"rx\", \"cz\")\n",
        "two_local.draw(\"mpl\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "e32e031b-3218-4c1c-af7c-b40ad6c82100",
      "metadata": {},
      "source": [
        "`parameters` アトリビュートから、回路のパラメーターのリストのようなオブジェクトを得ることができる。\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "id": "d503e1c8-0ccd-4868-a5dc-4806d00ab54b",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "ParameterView([ParameterVectorElement(θ[0]), ParameterVectorElement(θ[1]), ParameterVectorElement(θ[2]), ParameterVectorElement(θ[3]), ParameterVectorElement(θ[4]), ParameterVectorElement(θ[5]), ParameterVectorElement(θ[6]), ParameterVectorElement(θ[7]), ParameterVectorElement(θ[8]), ParameterVectorElement(θ[9]), ParameterVectorElement(θ[10]), ParameterVectorElement(θ[11])])"
            ]
          },
          "execution_count": 3,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "two_local.parameters"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "555aca2a-a754-4372-8846-22ee4773ece1",
      "metadata": {},
      "source": [
        "また、 `{ Parameter: number }` という形式の辞書を使って、これらのパラメータを実際の値に割り当てることもできる。 次のコード・セルでは、回路の各パラメータを `0` に割り当てている。\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "id": "89227b48-12b2-4b1b-9680-55a7fce88a2b",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/circuit-library/extracted-outputs/89227b48-12b2-4b1b-9680-55a7fce88a2b-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 4,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "bound_circuit = two_local.assign_parameters(\n",
        "    {p: 0 for p in two_local.parameters}\n",
        ")\n",
        "bound_circuit.decompose().draw(\"mpl\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "78772993-d0da-4651-9211-706e86a59432",
      "metadata": {},
      "source": [
        "詳細については、回路ライブラリAPIドキュメントの [N-local gatesを](/docs/api/qiskit/circuit_library#n-local-circuits)参照するか、 IBM Quantum Learningの [Variational algorithm designコースを](/learning/courses/variational-algorithm-design)受講してほしい。\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "3e463081-bce9-4ee2-9046-97659e9ac847",
      "metadata": {},
      "source": [
        "<span id=\"data-encoding-circuits\" />\n",
        "\n",
        "## データ符号化回路\n",
        "\n",
        "これらのパラメータ化された回路は、量子機械学習アルゴリズムによって処理される量子状態にデータをエンコードする。 Qiskitがサポートする回路には次のようなものがある：\n",
        "\n",
        "* 振幅エンコーディング：各数値を基底状態の振幅にエンコードする。 これは、 $2^n$ 番号を1つのステートに格納することができるが、実装にはコストがかかる。\n",
        "* 対応する基底状態 $|k\\rangle$ を用意することで、整数 $k$ を符号化する基底符号化。\n",
        "* 角度エンコーディング。データ内の各数値を回転角度としてパラメータ化された回路に設定する。\n",
        "\n",
        "最適なアプローチは、アプリケーションの仕様によって異なります。 しかし、現在の量子コンピュータでは、 `zz_feature_map` のような角度エンコード回路を使うことが多い。\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "id": "cf8b1efc-57b3-4681-8e6a-d5b8406d092d",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/circuit-library/extracted-outputs/cf8b1efc-57b3-4681-8e6a-d5b8406d092d-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 5,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from qiskit.circuit.library import zz_feature_map\n",
        "\n",
        "features = [0.2, 0.4, 0.8]\n",
        "feature_map = zz_feature_map(feature_dimension=len(features))\n",
        "\n",
        "encoded = feature_map.assign_parameters(features)\n",
        "encoded.draw(\"mpl\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "031bf004-ca80-4cc0-b153-2cd5cd778386",
      "metadata": {},
      "source": [
        "回路ライブラリAPIドキュメントの[データ符号化回路を](/docs/api/qiskit/circuit_library#data-encoding-circuits)参照のこと。\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "5c5d2735-ef6a-48db-8382-9dc03c9af20a",
      "metadata": {},
      "source": [
        "<span id=\"time-evolution-circuits\" />\n",
        "\n",
        "## 時間発展回路\n",
        "\n",
        "これらの回路は、時間的に進化する量子状態をシミュレートする。 時間発展回路を使用して、系の熱伝導や相転移などの物理的効果を調べる。 時間発展回路は、化学の波動関数（ユニタリー・クラスター結合試行状態など）や、最適化問題に用いるQAOAアルゴリズムの基本的な構成要素でもある。\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "id": "834794df-86e9-4bea-8efa-5380499e359b",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/circuit-library/extracted-outputs/834794df-86e9-4bea-8efa-5380499e359b-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 6,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from qiskit.circuit.library import PauliEvolutionGate\n",
        "from qiskit.circuit import QuantumCircuit\n",
        "from qiskit.quantum_info import SparsePauliOp\n",
        "\n",
        "\n",
        "# Prepare an initial state with a Hadamard on the middle qubit\n",
        "state = QuantumCircuit(3)\n",
        "state.h(1)\n",
        "\n",
        "hamiltonian = SparsePauliOp([\"ZZI\", \"IZZ\"])\n",
        "evolution = PauliEvolutionGate(hamiltonian, time=1)\n",
        "\n",
        "# Evolve state by appending the evolution gate\n",
        "state.compose(evolution, inplace=True)\n",
        "\n",
        "state.draw(\"mpl\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "e2dad12f-535a-4f42-8ac4-dbfcfb5533cc",
      "metadata": {},
      "source": [
        "[`PauliEvolutionGate` API ドキュメントを](/docs/api/qiskit/qiskit.circuit.library.PauliEvolutionGate)読む。\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "0cf7122d-c3fe-41a6-936c-b3770b33f0f1",
      "metadata": {},
      "source": [
        "<span id=\"benchmarking-and-complexity-theory-circuits\" />\n",
        "\n",
        "## ベンチマーキングと複雑性理論回路\n",
        "\n",
        "ベンチマーク回路は、私たちのハードウェアが実際にどの程度動作しているのかを感じさせてくれるし、複雑性理論回路は、私たちが解決したい問題がどの程度難しいのかを理解するのに役立つ。\n",
        "\n",
        "例えば、「量子ボリューム」ベンチマークは、量子コンピューターが一種のランダム量子回路をどれだけ正確に実行できるかを測定する。 量子コンピュータのスコアは、確実に実行できる回路のサイズが大きくなるほど高くなる。 これは、量子ビット数、命令の忠実度、量子ビットの接続性、ソフトウェアスタックのトランスパイルと後処理結果など、コンピュータのあらゆる側面を考慮したものである。 量子体積については、 [量子体積の](https://arxiv.org/abs/1811.12926)原著論文をお読みください。\n",
        "\n",
        "次のコードは、Qiskitで作られた4量子ビットで動作する量子体積回路の例である（ `unitary` ブロックはランダム化された2量子ビットゲート）。\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "id": "9629a507-8191-409e-b895-fd0833c8fcd7",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/circuit-library/extracted-outputs/9629a507-8191-409e-b895-fd0833c8fcd7-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 7,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from qiskit.circuit.library import quantum_volume\n",
        "\n",
        "quantum_volume(4).draw(\"mpl\")"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "908e4b4a-5edf-4390-82a0-e755050c2a37",
      "metadata": {},
      "source": [
        "この回路ライブラリには、瞬間量子多項式（iqp）回路など、古典的なシミュレーションが難しいとされる回路も含まれている。 これらの回路は、ハダマードゲートのブロックの間に（計算基底の）ある対角ゲートを挟んでいる。\n",
        "\n",
        "その他の回路としては、グローバーのアルゴリズムで使われる `grover_operator` 、フーリエ検査問題用の `fourier_checking` 。 これらの回路については、回路ライブラリAPIドキュメントの [Particular quantum circuitsを](/docs/api/qiskit/circuit_library#particular-quantum-circuits)参照のこと。\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "58b1a7b8-c173-4de8-957a-ca5d58332073",
      "metadata": {},
      "source": [
        "<span id=\"arithmetic-circuits\" />\n",
        "\n",
        "## 算術回路\n",
        "\n",
        "算術演算は、整数の加算やビット単位の演算など、古典的な関数である。 これらは、金融アプリケーションの振幅推定などのアルゴリズムや、線形連立方程式を解くHHLアルゴリズムのようなアルゴリズムで有用である。\n",
        "\n",
        "例として、インプレース加算(`FullAdderGate`)を行う「リップル・キャリー」回路を使って、2つの3ビッ ト数を加算してみよう。この加算器は、2つの数値（ここでは \"A \"と \"B \"と呼ぶ）を加算し、結果をBを保持するレジスタに書き込む。 以下の例では、 A=2 と B=3。\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "id": "77555a5a-a81c-47b8-a9ae-3015d84adcf5",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/circuit-library/extracted-outputs/77555a5a-a81c-47b8-a9ae-3015d84adcf5-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 8,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from qiskit.circuit.library import FullAdderGate\n",
        "from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister\n",
        "\n",
        "adder = FullAdderGate(3)  # Adder of 3-bit numbers\n",
        "\n",
        "# Create the number A=2\n",
        "reg_a = QuantumRegister(3, \"a\")\n",
        "number_a = QuantumCircuit(reg_a)\n",
        "number_a.initialize(2)  # Number 2; |010>\n",
        "\n",
        "# Create the number B=3\n",
        "reg_b = QuantumRegister(3, \"b\")\n",
        "number_b = QuantumCircuit(reg_b)\n",
        "number_b.initialize(3)  # Number 3; |011>\n",
        "\n",
        "# Create a circuit to hold everything, including a classical register for\n",
        "# the result\n",
        "qregs = [\n",
        "    QuantumRegister(1, \"cin\"),\n",
        "    QuantumRegister(3, \"a\"),\n",
        "    QuantumRegister(3, \"b\"),\n",
        "    QuantumRegister(1, \"cout\"),\n",
        "]\n",
        "reg_result = ClassicalRegister(3)\n",
        "circuit = QuantumCircuit(*qregs, reg_result)\n",
        "\n",
        "# Compose number initializers with the adder. Adder stores the result to\n",
        "# register B, so we'll measure those qubits.\n",
        "circuit = (\n",
        "    circuit.compose(number_a, qubits=reg_a)\n",
        "    .compose(number_b, qubits=reg_b)\n",
        "    .compose(adder)\n",
        ")\n",
        "circuit.measure(reg_b, reg_result)\n",
        "circuit.draw(\"mpl\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "e8a8deee-ad46-42cd-844e-51d5541c3f65",
      "metadata": {},
      "source": [
        "回路をシミュレートすると、すべての `1024` ショットに対して `5` を出力する（すなわち、確率 `1.0` で測定される）ことがわかる。\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "id": "ede21ca4-0358-4c83-9af5-63a1c67ae3cb",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Count data:\n",
            " {5: 1024}\n"
          ]
        }
      ],
      "source": [
        "from qiskit.primitives import StatevectorSampler\n",
        "\n",
        "result = StatevectorSampler().run([circuit]).result()\n",
        "\n",
        "print(f\"Count data:\\n {result[0].data.c0.get_int_counts()}\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cbd99d49-eb5c-4cd5-bac2-528497b8405e",
      "metadata": {},
      "source": [
        "回路ライブラリAPIドキュメントの[算術演算を](/docs/api/qiskit/circuit_library#arithmetic)参照のこと。\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "9a900a84-c52f-4a03-b3e9-87c71fa93e88",
      "metadata": {},
      "source": [
        "<span id=\"next-steps\" />\n",
        "\n",
        "## 次のステップ\n",
        "\n",
        "<Admonition type=\"tip\" title=\"推奨事項\">\n",
        "  * 「 [Construct回路](/docs/guides/construct-circuits) 」のトピックで、回路を作成するための高度な手法を学びましょう。\n",
        "  * [Grover's Algorithmの](/docs/tutorials/grovers-algorithm)チュートリアルで使用されている回路の例をご覧ください。\n",
        "  * [回路ライブラリAPI](/docs/api/qiskit/circuit_library) リファレンスを確認する。\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": 5
}