{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "539f98fa-9ccc-472a-99da-ebe6382243dc",
      "metadata": {},
      "source": [
        "---\n",
        "title: Save circuits to disk\n",
        "description: Store Qiskit objects to disk so you can continue where you left off\n",
        "---\n",
        "\n",
        "# Save circuits to disk\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "2cd24003-6d72-432a-8573-7a4809e3411c",
      "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=\"Package versions\">\n",
        "    The code on this page was developed using the following requirements.\n",
        "    We recommend using these versions or newer.\n",
        "\n",
        "    ```\n",
        "    qiskit[all]~=2.4.0\n",
        "    ```\n",
        "  </AccordionItem>\n",
        "</Accordion>\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "3aeaa5b8-c8f5-4448-9d72-1a2452d79f74",
      "metadata": {},
      "source": [
        "Use [QPY serialization](/docs/api/qiskit/qpy) to save your circuit to file. QPY files store the full Qiskit circuit object and will be compatible with newer versions of Qiskit (although not necessarily with older versions of Qiskit).\n",
        "\n",
        "To demonstrate, the following cell creates a simple quantum circuit.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "id": "fa58c6e1-1102-4409-80b8-01b02e4e6ee5",
      "metadata": {},
      "outputs": [],
      "source": [
        "from qiskit import QuantumCircuit\n",
        "\n",
        "qc = QuantumCircuit(2)\n",
        "qc.h(0)\n",
        "qc.cx(0, 1)\n",
        "qc.measure_all()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "d208d7a6-d465-4f08-909e-fd6803577700",
      "metadata": {},
      "source": [
        "To save this file to disk, use the `qpy.dump` function. You can also save a list of circuits.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "id": "e02f1f2a-2705-4406-bb1c-c80d436b5110",
      "metadata": {},
      "outputs": [],
      "source": [
        "from qiskit import qpy\n",
        "\n",
        "with open(\"test.qpy\", \"wb\") as file:\n",
        "    qpy.dump(qc, file)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "3af68772-b488-411d-9084-14129b9a9f1d",
      "metadata": {},
      "source": [
        "This circuit is now saved to the file `test.qpy`. If you restart your Python kernel, you can re-load the circuit using the `qpy.load` function. Note that this always returns a list of circuits, even if you only serialized one circuit.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "id": "210d05c8-c9eb-4135-bb3f-94c281708f4b",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/save-circuits/extracted-outputs/210d05c8-c9eb-4135-bb3f-94c281708f4b-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 3,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "with open(\"test.qpy\", \"rb\") as handle:\n",
        "    qc = qpy.load(handle)\n",
        "\n",
        "qc[0].draw(\"mpl\")"
      ]
    },
    {
      "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": 2
}