{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "92e0efa3-cda3-491c-99fc-650b2ce4cfa8",
      "metadata": {},
      "source": [
        "---\n",
        "title: \"トランスパイラープラグインのインストールと使用\"\n",
        "description: \"Qiskitにおけるトランスパイラープラグインのインストールと使用方法\"\n",
        "---\n",
        "\n",
        "<span id=\"install-and-use-transpiler-plugins\" />\n",
        "\n",
        "# トランスパイラープラグインのインストールと使用\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "5a4052f5-554d-4633-a588-eb0583f66871",
      "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",
        "    ```\n",
        "  </AccordionItem>\n",
        "</Accordion>\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "12018a80-a1ce-4508-9d04-23c4176028e6",
      "metadata": {},
      "source": [
        "Qiskitユーザーの幅広いコミュニティによるカスタムトランスパイルコードの開発と再利用を促進するため、Qiskit SDKは、サードパーティの Python パッケージがQiskit経由でアクセス可能な拡張トランスパイルファンクションを提供することを宣言できるプラグインインターフェースをサポートしています。\n",
        "\n",
        "現在、サードパーティのプラグインは、3つの方法で拡張トランスピレーション機能を提供することができます：\n",
        "\n",
        "* [トランスパイラ・ステージ・プラグインは](/docs/api/qiskit/transpiler_plugins)、プリセット・ステージド・パスマネージャーの [6つのステージの](transpiler-stages)うちの1つの代わりに使用できるパスマネージャーを提供します： `init` `layout`、 `routing`、 `translation`、 `optimization`、 `scheduling`。\n",
        "* [ユニタリー合成プラグインは](/docs/api/qiskit/qiskit.transpiler.passes.synthesis.plugin.UnitarySynthesisPlugin)、ユニタリーゲート合成のための拡張機能を提供します。\n",
        "* [高位合成プラグインは](/docs/api/qiskit/qiskit.transpiler.passes.synthesis.plugin.HighLevelSynthesisPlugin)、線形関数やクリフォード演算子などの「高位オブジェクト」を合成するための拡張機能を提供する。 高レベルのオブジェクトは、 [Operation](/docs/api/qiskit/qiskit.circuit.Operation) クラスのサブクラスで表される。\n",
        "\n",
        "残りのページでは、利用可能なプラグインをリストアップし、新しいプラグインをインストールし、それらを使用する方法について説明します。\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "934388b3-8efd-43b6-82f8-ace84d3bb0d4",
      "metadata": {},
      "source": [
        "<span id=\"list-available-plugins-and-install-new-ones\" />\n",
        "\n",
        "## 利用可能なプラグインの一覧を表示し、新しいプラグインをインストールする\n",
        "\n",
        "Qiskitにはすでにいくつかのトランスピレーション用プラグインが組み込まれています。 さらにインストールするには、 Python パッケージマネージャを使用できます。 例えば、 [Qiskit TOQM](https://github.com/qiskit-toqm/qiskit-toqm) ルーティングステージプラグインをインストールするために `pip install qiskit-toqm` 。 数多くのサードパーティ製プラグインが [Qiskitエコシステムの](https://qiskit.github.io/ecosystem/#transpiler_plugin)一部となっています。\n",
        "\n",
        "<span id=\"list-available-transpiler-stage-plugins\" />\n",
        "\n",
        "### 利用可能なトランスパイラーステージプラグインの一覧\n",
        "\n",
        "[list\\_stage\\_plugins](/docs/api/qiskit/transpiler_plugins#qiskit.transpiler.preset_passmanagers.plugin.list_stage_plugins) 関数を使用して、プラグインをリストしたいステージ名を渡す。\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "id": "817254a8-77d7-4eb4-8630-623e67bb2186",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "['default', 'dense', 'sabre', 'trivial']"
            ]
          },
          "execution_count": 1,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from qiskit.transpiler.preset_passmanagers.plugin import list_stage_plugins\n",
        "\n",
        "list_stage_plugins(\"layout\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "id": "354f9be8-3567-4449-ba60-4d1c3258bb50",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "['basic', 'default', 'lookahead', 'none', 'sabre']"
            ]
          },
          "execution_count": 2,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "list_stage_plugins(\"routing\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "ac57d3ce-afb3-4aba-8601-93513d4dd491",
      "metadata": {},
      "source": [
        "`qiskit-toqm` がインストールされていれば、 `routing` のプラグインリストに `toqm` が表示される。\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "1397de7f-c447-463e-8aba-ed1f569be86c",
      "metadata": {},
      "source": [
        "<span id=\"list-available-unitary-synthesis-plugins\" />\n",
        "\n",
        "### 利用可能なユニタリー合成プラグインの一覧\n",
        "\n",
        "[unitary\\_synthesis](/docs/api/qiskit/qiskit.transpiler.passes.synthesis.plugin.unitary_synthesis_plugin_names) 関数を使用する。\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "id": "596cbb3d-2ece-4193-929c-79149f7e23bd",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "['aqc', 'clifford', 'default', 'gridsynth', 'sk']"
            ]
          },
          "execution_count": 3,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from qiskit.transpiler.passes.synthesis import unitary_synthesis_plugin_names\n",
        "\n",
        "unitary_synthesis_plugin_names()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "e33885d4-788f-4759-bd63-e26d7a45ff9e",
      "metadata": {},
      "source": [
        "<span id=\"list-available-high-level-synthesis-plugins\" />\n",
        "\n",
        "### 利用可能なハイレベル合成プラグインの一覧\n",
        "\n",
        "[high\\_level\\_synthesis](/docs/api/qiskit/qiskit.transpiler.passes.synthesis.plugin.high_level_synthesis_plugin_names) 関数を使用し、合成する \"高レベルオブジェクト \"のタイプ名を渡す。 この名前は、合成されるオブジェクトのタイプを表すOperationクラスの [`name`](/docs/api/qiskit/qiskit.circuit.Operation#name) 属性に対応します[。](/docs/api/qiskit/qiskit.circuit.Operation)\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "id": "3b2d72bb-d661-4432-acfe-970f0a2dcd53",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "['ag', 'bm', 'default', 'greedy', 'layers', 'lnn', 'rb_default']"
            ]
          },
          "execution_count": 4,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from qiskit.transpiler.passes.synthesis import (\n",
        "    high_level_synthesis_plugin_names,\n",
        ")\n",
        "\n",
        "high_level_synthesis_plugin_names(\"clifford\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "583d7529-ab3e-449d-b7d0-e732801d7ae0",
      "metadata": {},
      "source": [
        "この [HighLevelSynthesisPluginManager](/docs/api/qiskit/qiskit.transpiler.passes.synthesis.plugin.HighLevelSynthesisPluginManager) クラスを使って、すべての高位合成プラグインの名前をリストすることができます：\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "id": "b17192e5-ddac-4cf6-b2e2-597e8c505568",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "['FullAdder.default',\n",
              " 'FullAdder.ripple_c04',\n",
              " 'FullAdder.ripple_v95',\n",
              " 'HalfAdder.default',\n",
              " 'HalfAdder.qft_d00',\n",
              " 'HalfAdder.ripple_c04',\n",
              " 'HalfAdder.ripple_r25',\n",
              " 'HalfAdder.ripple_v95',\n",
              " 'IntComp.default',\n",
              " 'IntComp.noaux',\n",
              " 'IntComp.twos',\n",
              " 'ModularAdder.default',\n",
              " 'ModularAdder.modular_v17',\n",
              " 'ModularAdder.qft_d00',\n",
              " 'ModularAdder.ripple_c04',\n",
              " 'ModularAdder.ripple_v95',\n",
              " 'Multiplier.cumulative_h18',\n",
              " 'Multiplier.default',\n",
              " 'Multiplier.qft_r17',\n",
              " 'PauliEvolution.default',\n",
              " 'PauliEvolution.rustiq',\n",
              " 'WeightedSum.default',\n",
              " 'annotated.default',\n",
              " 'clifford.ag',\n",
              " 'clifford.bm',\n",
              " 'clifford.default',\n",
              " 'clifford.greedy',\n",
              " 'clifford.layers',\n",
              " 'clifford.lnn',\n",
              " 'linear_function.default',\n",
              " 'linear_function.kms',\n",
              " 'linear_function.pmh',\n",
              " 'mcmt.default',\n",
              " 'mcmt.noaux',\n",
              " 'mcmt.vchain',\n",
              " 'mcmt.xgate',\n",
              " 'mcx.1_clean_b95',\n",
              " 'mcx.1_clean_kg24',\n",
              " 'mcx.1_dirty_kg24',\n",
              " 'mcx.2_clean_kg24',\n",
              " 'mcx.2_dirty_kg24',\n",
              " 'mcx.default',\n",
              " 'mcx.gray_code',\n",
              " 'mcx.n_clean_m15',\n",
              " 'mcx.n_dirty_i15',\n",
              " 'mcx.noaux_hp24',\n",
              " 'mcx.noaux_v24',\n",
              " 'permutation.acg',\n",
              " 'permutation.basic',\n",
              " 'permutation.default',\n",
              " 'permutation.kms',\n",
              " 'permutation.token_swapper',\n",
              " 'qft.default',\n",
              " 'qft.full',\n",
              " 'qft.line',\n",
              " 'clifford.rb_default']"
            ]
          },
          "execution_count": 5,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from qiskit.transpiler.passes.synthesis.plugin import (\n",
        "    HighLevelSynthesisPluginManager,\n",
        ")\n",
        "\n",
        "HighLevelSynthesisPluginManager().plugins.names()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "4eed49d1-4723-4366-9edc-1f5aa73907ba",
      "metadata": {},
      "source": [
        "<span id=\"use-a-plugin\" />\n",
        "\n",
        "## プラグインを使用する\n",
        "\n",
        "このセクションでは、トランスパイラ・プラグインの使い方を紹介します。 コード例ではQiskit付属のプラグインを使用していますが、サードパーティパッケージからインストールしたプラグインも同様に使用できます。\n",
        "\n",
        "<span id=\"use-a-transpiler-stage-plugin\" />\n",
        "\n",
        "### トランスパイラーステージプラグインを使用する\n",
        "\n",
        "トランスパイラ・ステージ・プラグインを使用するには、適切な引数とともにその名前を または [`generate_preset_pass_manager`](/docs/api/qiskit/qiskit.transpiler.generate_preset_pass_manager#qiskit.transpiler.generate_preset_pass_manager) に指定してください [`transpile`](/docs/api/qiskit/compiler#qiskit.compiler.transpile)。 引数は、トランスパイル段階の名前の末尾に を `_method` 付加することで構成されます。 たとえば、 `lookahead` ルーティングプラグインを使用するには、引数として `routing_method` を `lookahead` 指定します。\n",
        "\n",
        "<Admonition type=\"note\">\n",
        "  これらの例では の `FakeSherbrooke``qiskit_ibm_runtime` モックバックエンドを使用していますが、Qiskitと互換性のある実在のバックエンドや模擬バックエンドであれば、どれでも試すことができます。  実際の結果は異なる場合があります。\n",
        "</Admonition>\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "id": "d5e598b3-9226-4fbc-ac76-8eca08ec4af3",
      "metadata": {},
      "outputs": [],
      "source": [
        "from qiskit.transpiler import generate_preset_pass_manager\n",
        "from qiskit_ibm_runtime.fake_provider import FakeSherbrooke\n",
        "\n",
        "backend = FakeSherbrooke()\n",
        "\n",
        "pass_manager = generate_preset_pass_manager(\n",
        "    optimization_level=3, backend=backend, routing_method=\"lookahead\"\n",
        ")"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "657db73c-7064-4569-a212-87642f16216e",
      "metadata": {},
      "source": [
        "<span id=\"use-a-unitary-synthesis-plugin\" />\n",
        "\n",
        "### ユニタリー合成プラグインを使用する\n",
        "\n",
        "ユニタリー合成プラグインを使用するには、 `unitary_synthesis_method` の引数にその名前を指定します。 [`generate_preset_pass_manager`](/docs/api/qiskit/qiskit.transpiler.generate_preset_pass_manager#qiskit.transpiler.generate_preset_pass_manager) または [`transpile`](/docs/api/qiskit/compiler#qiskit.compiler.transpile):\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "id": "28184c68-4b1e-46d2-a8a0-5480814bad9b",
      "metadata": {},
      "outputs": [],
      "source": [
        "pass_manager = generate_preset_pass_manager(\n",
        "    optimization_level=3,\n",
        "    backend=backend,\n",
        "    unitary_synthesis_method=\"sk\",\n",
        "    unitary_synthesis_plugin_config=dict(\n",
        "        basis_gates=[\"cz\", \"id\", \"rz\", \"sx\", \"x\"]\n",
        "    ),\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "c019cb3d-5069-4257-bfb6-e15600caa342",
      "metadata": {},
      "source": [
        "によって返されるステージドパスマネージャーの `init`、 `translation`、 `optimization` の各ステージでユニタリー合成が使用される。 [`generate_preset_pass_manager`](/docs/api/qiskit/qiskit.transpiler.generate_preset_pass_manager#qiskit.transpiler.generate_preset_pass_manager) で使用される。 [`transpile`](/docs/api/qiskit/compiler#qiskit.compiler.transpile). これらのステージの説明については、 [トランスパイラのステージを](transpiler-stages)参照のこと。\n",
        "\n",
        "引数 `unitary_synthesis_plugin_config` （自由形式の辞書）を使って、ユニタリー合成法のオプションを渡す。 合成法のドキュメントは、それがサポートするオプションを説明すべきである。 組み込みのユニタリー合成プラグインのドキュメントへのリンクは、 [このリストを](/docs/api/qiskit/transpiler_synthesis_plugins#unitary-synthesis-plugins-2)参照してください。\n",
        "\n"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "d4ce7ad9-711f-4e43-9012-3f404b885ab1",
      "metadata": {},
      "source": [
        "<span id=\"use-a-high-level-synthesis-plugin\" />\n",
        "\n",
        "### 高レベル合成プラグインを使用する\n",
        "\n",
        "まず、 [HLSConfigを](/docs/api/qiskit/qiskit.transpiler.passes.HLSConfig)作成する。 を作成し、さまざまな高レベルオブジェクトに使用するプラグインの名前を格納する。 例:\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "id": "d4672004-aead-4827-9c10-5c70c3c2289b",
      "metadata": {},
      "outputs": [],
      "source": [
        "from qiskit.transpiler.passes import HLSConfig\n",
        "\n",
        "hls_config = HLSConfig(clifford=[\"layers\"], linear_function=[\"pmh\"])"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "4eab8d19-4b58-4f50-a205-039288e4a00d",
      "metadata": {},
      "source": [
        "このコード・セルは、 `layers` オブジェクトを合成する プラグインと、 オブジェクトを合成する プラグインを使用する高レベル合成コンフィギュレーションを作成します。 [クリフォード](/docs/api/qiskit/qiskit.quantum_info.Clifford#clifford) オブジェクトの合成には `pmh` プラグインを使用します。 [LinearFunction](/docs/api/qiskit/qiskit.circuit.library.LinearFunction#linearfunction) オブジェクトを合成する キーワード引数の名前は [`name`](/docs/api/qiskit/qiskit.circuit.Operation#name) 属性に対応する[。](/docs/api/qiskit/qiskit.circuit.Operation)\n",
        "各ハイレベルオブジェクトに対して、与えられたプラグインのリストが、そのうちの1つが成功するまで順番に試される。 が成功するまで順番に試される（上の例では、各リストには1つのプラグインしか含まれていない）。\n",
        "\n",
        "プラグインの名前で を指定する代わりに、 `(name, options)` のタプルを渡すことができます。タプルの2番目の要素は、プラグインのオプションを含む辞書です。 合成法のドキュメントは、それがサポートするオプションを説明すべきである。 組み込みの高位合成プラグインのドキュメントへのリンクは、 [このリストを](/docs/api/qiskit/transpiler_synthesis_plugins#high-level-synthesis-plugins-2)参照のこと。\n",
        "\n",
        "`HLSConfig` オブジェクトを作成したら、それを `hls_config` への引数として渡す。 [`generate_preset_pass_manager`](/docs/api/qiskit/qiskit.transpiler.generate_preset_pass_manager#qiskit.transpiler.generate_preset_pass_manager) または [`transpile`](/docs/api/qiskit/compiler#qiskit.compiler.transpile):\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "id": "8784b881-2f37-4f75-b5c7-c84cdb8b8a60",
      "metadata": {},
      "outputs": [],
      "source": [
        "pass_manager = generate_preset_pass_manager(\n",
        "    optimization_level=3, backend=backend, hls_config=hls_config\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "ea63c611-c213-4736-83f7-c46e918eb862",
      "metadata": {},
      "source": [
        "によって返されるステージド・パスマネージャーの `init`、 `translation`、 `optimization` の各ステージで高位合成が使われる。 [`generate_preset_pass_manager`](/docs/api/qiskit/qiskit.transpiler.generate_preset_pass_manager#qiskit.transpiler.generate_preset_pass_manager) で使用される。 [`transpile`](/docs/api/qiskit/compiler#qiskit.compiler.transpile). これらのステージの説明については、 [トランスパイラのステージを](transpiler-stages)参照のこと。\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "aae87a98-f16d-4ac1-9b85-11a771e04224",
      "metadata": {},
      "source": [
        "<span id=\"next-steps\" />\n",
        "\n",
        "## 次のステップ\n",
        "\n",
        "<Admonition type=\"tip\" title=\"勧告\">\n",
        "  * [トランスパイラプラグインを作成する](/docs/guides/create-transpiler-plugin)。\n",
        "  * 量子回路のトランスパイルと実行の例については、 [チュートリアルを](/docs/tutorials)チェックしてほしい。\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
}