{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "cdc8dc1e-0cff-46c1-94d0-97456d278e27",
      "metadata": {},
      "source": [
        "---\n",
        "title: Write your first Qiskit Serverless program\n",
        "description: How to create a parallel transpilation program and deploy it to IBM Quantum Platform to use as a reusable remote service.\n",
        "---\n",
        "\n",
        "{/* cspell:ignore mypath  */}\n",
        "\n",
        "# Write your first Qiskit Serverless program\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "b6d632cb-d884-4963-9c8d-8f3a3acd8a7d",
      "metadata": {
        "tags": [
          "version-info"
        ]
      },
      "source": [
        "<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]~=1.3.1\n",
        "    qiskit-ibm-runtime~=0.34.0\n",
        "    qiskit-aer~=0.15.1\n",
        "    qiskit-serverless~=0.18.1\n",
        "    qiskit-ibm-catalog~=0.2\n",
        "    qiskit-addon-sqd~=0.8.1\n",
        "    qiskit-addon-utils~=0.1.0\n",
        "    qiskit-addon-mpf~=0.2.0\n",
        "    qiskit-addon-aqc-tensor~=0.1.2\n",
        "    qiskit-addon-obp~=0.1.0\n",
        "    scipy~=1.15.0\n",
        "    pyscf~=2.8.0\n",
        "    ```\n",
        "  </AccordionItem>\n",
        "</Accordion>\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "1cc46aeb-aff6-4fab-9d78-69f2bfe98d98",
      "metadata": {},
      "source": [
        "<Admonition type=\"tip\">\n",
        "  **Qiskit Serverless is getting an upgrade, and its features are changing fast.** During this development phase, find release notes and the most recent documentation at the [Qiskit Serverless GitHub](https://qiskit.github.io/qiskit-serverless/index.html) page.\n",
        "</Admonition>\n",
        "\n",
        "This example demonstrates how to use `qiskit-serverless` tools to create a parallel transpilation program, and then implement `qiskit-ibm-catalog` to upload your program to IBM Quantum Platform to use as a reusable remote service.\n",
        "\n",
        "## Workflow overview\n",
        "\n",
        "1. Create a local directory and empty program file (`./source_files/transpile_remote.py`)\n",
        "2. Add code to your program that, when uploaded to Qiskit Serverless, will transpile a circuit\n",
        "3. Use `qiskit-ibm-catalog` to authenticate to Qiskit Serverless\n",
        "4. Upload the program to Qiskit Serverless\n",
        "\n",
        "After uploading your program , you can run it to transpile the circuit by following the [Run your first Qiskit Serverless workload remotely](/docs/guides/serverless-run-first-workload) guide.\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cad16f43-8548-4849-a4b8-09059a8b747c",
      "metadata": {},
      "source": [
        "## Example: Remote transpilation with Qiskit Serverless\n",
        "\n",
        "This example walks you through creating and adding to a program file that, when you upload it to Qiskit Serverless, will transpile a `circuit` against a given `backend` and target `optimization_level`.\n",
        "\n",
        "<Admonition type=\"tip\">\n",
        "  Qiskit Serverless requires setting up your workload’s `.py` files into a dedicated directory. The following structure is an example of good practice:\n",
        "\n",
        "  ```text\n",
        "  serverless_program\n",
        "  ├── program_uploader.ipynb\n",
        "  └── source_files\n",
        "      ├── transpile_remote.py\n",
        "      └── *.py\n",
        "  ```\n",
        "</Admonition>\n",
        "\n",
        "Serverless uploads the contents of a specific directory (in this example, the `source_files` directory) to run remotely. After these are set up, you can adjust `transpile_remote.py` to fetch inputs and return outputs.\n",
        "\n",
        "### Create the directory and an empty program file\n",
        "\n",
        "First, create a directory named `source_files`, then create a program file in the directory, so that its path is `./source_files/transpile_remote.py`. This is the file you will upload to Qiskit Serverless.\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "a9b3fb7d-abd6-4a3e-b9b1-b21202d8db6d",
      "metadata": {},
      "source": [
        "### Add code to your program file\n",
        "\n",
        "Populate your program file with the following code, then save it.\n",
        "\n",
        "<Admonition type=\"caution\">\n",
        "  If you're reading the code cells locally in a notebook, you will see the `%%writefile` [magic command](https://ipython.readthedocs.io/en/stable/interactive/magics.html#cellmagic-writefile). Executing cells with this magic command saves them to disk rather than executing them.\n",
        "</Admonition>\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "214eb803-d1fa-4ac0-b955-8b24717086f4",
      "metadata": {},
      "outputs": [],
      "source": [
        "# This cell is hidden from users, it creates a new folder\n",
        "from pathlib import Path\n",
        "\n",
        "Path(\"./source_files\").mkdir(exist_ok=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "64722041-22ed-42bd-8d83-d8e9f5e5b55b",
      "metadata": {
        "editable": true,
        "slideshow": {
          "slide_type": ""
        },
        "tags": []
      },
      "outputs": [],
      "source": [
        "%%writefile ./source_files/transpile_remote.py\n",
        "# If you include the preceding `%%writefile` command (visible only when you read this locally in a notebook), running this cell saves to disk rather than executing the code.\n",
        "\n",
        "from qiskit.transpiler import generate_preset_pass_manager\n",
        "\n",
        "def transpile_remote(circuit, optimization_level, backend):\n",
        "    \"\"\"Transpiles an abstract circuit into an ISA circuit for a given backend.\"\"\"\n",
        "    pass_manager = generate_preset_pass_manager(\n",
        "        optimization_level=optimization_level,\n",
        "\t\tbackend=backend\n",
        "    )\n",
        "    isa_circuit = pass_manager.run(circuit)\n",
        "    return isa_circuit"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "7e80b5bb-0f76-43d1-a68c-97b4b22cc88a",
      "metadata": {},
      "source": [
        "### Add code to get program arguments\n",
        "\n",
        "Now add the following code to your program file, which sets up program arguments.\n",
        "\n",
        "Your initial `transpile_remote.py` has three inputs: `circuits`, `backend_name`, and `optimization_level`. Serverless is currently limited to only accept serializable inputs and outputs. For this reason, you cannot pass in `backend` directly, so use `backend_name` as a string instead.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "6819379b-d7b2-4fda-9e6b-459eec748a79",
      "metadata": {},
      "outputs": [],
      "source": [
        "%%writefile --append ./source_files/transpile_remote.py\n",
        "# If you include the preceding `%%writefile` command (visible only when you read this locally in a notebook), running this cell saves to disk rather than executing the code.\n",
        "\n",
        "from qiskit_serverless import get_arguments, save_result, distribute_task, get\n",
        "\n",
        "# Get program arguments\n",
        "arguments = get_arguments()\n",
        "circuits = arguments.get(\"circuits\")\n",
        "backend_name = arguments.get(\"backend_name\")\n",
        "optimization_level = arguments.get(\"optimization_level\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "5ae9fb12-f06f-4f7d-8f37-1d872285deab",
      "metadata": {},
      "source": [
        "### Add code that calls the backend\n",
        "\n",
        "Add the following code to your program file, which calls your backend with `QiskitRuntimeService`.\n",
        "\n",
        "The following code assumes that you have already followed the process to save your credentials by using `QiskitRuntimeService.save_account`, and will load your default saved account unless you specify otherwise. See [Save your login credentials](/docs/guides/save-credentials) and [Initialize your Qiskit Runtime service account](/docs/guides/initialize-account) for more information.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "4b0c7d24-9b87-4e00-bb1d-f82aa967cb1d",
      "metadata": {},
      "outputs": [],
      "source": [
        "%%writefile --append ./source_files/transpile_remote.py\n",
        "# If you include the preceding `%%writefile` command (visible only when you read this locally in a notebook), running this cell saves to disk rather than executing the code.\n",
        "\n",
        "from qiskit_ibm_runtime import QiskitRuntimeService\n",
        "\n",
        "service = QiskitRuntimeService()\n",
        "backend = service.backend(backend_name)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "61e7cb3b-6d62-4b68-817a-39c0c1d95a9b",
      "metadata": {},
      "source": [
        "### Add code to transpile\n",
        "\n",
        "Finally, add the following code to your program file. This code runs `transpile_remote()` across all `circuits` passed in, and returns the `transpiled_circuits` as a result:\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "041e7f85-e9e8-424d-8694-d54316225cc4",
      "metadata": {},
      "outputs": [],
      "source": [
        "%%writefile --append ./source_files/transpile_remote.py\n",
        "# If you include the preceding `%%writefile` command (visible only when you read this locally in a notebook), running this cell saves to disk rather than executing the code.\n",
        "\n",
        "# Each circuit is being transpiled and will populate the array\n",
        "results = [\n",
        "    transpile_remote(circuit, 1, backend)\n",
        "    for circuit in circuits\n",
        "]\n",
        "\n",
        "save_result({\n",
        "    \"transpiled_circuits\": results\n",
        "})"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "dac118ab-e447-4ddd-a5f8-d13e3953db76",
      "metadata": {},
      "source": [
        "<span id=\"upload-to-qiskit-serverless\" />\n",
        "\n",
        "### Authenticate to Qiskit Serverless\n",
        "\n",
        "Use `qiskit-ibm-catalog` to authenticate to `QiskitServerless` with your API key (you can use your `QiskitRuntimeService` API key, or create a new API key on the [IBM Quantum Platform dashboard]()).\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "id": "8d1385c3-09a5-42c6-a5bc-53f686d8410e",
      "metadata": {},
      "outputs": [],
      "source": [
        "from qiskit_ibm_catalog import QiskitServerless, QiskitFunction\n",
        "\n",
        "# Authenticate to the remote cluster and submit the pattern for remote execution\n",
        "serverless = QiskitServerless()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "a0f64820-5fb1-41b9-9cb4-5095d0b5db43",
      "metadata": {},
      "source": [
        "### Run code to upload\n",
        "\n",
        "Run the following code to upload the program. Qiskit Serverless compresses the contents of `working_dir` (in this case, `source_files`) into a `tar`, which is uploaded and then cleaned up. The `entrypoint` identifies the main program executable for Qiskit Serverless to run.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "id": "c1cdd46d-71e9-4711-a09b-6eca569ede6a",
      "metadata": {},
      "outputs": [],
      "source": [
        "transpile_remote_demo = QiskitFunction(\n",
        "    title=\"transpile_remote_serverless\",\n",
        "    entrypoint=\"transpile_remote.py\",\n",
        "    working_dir=\"./source_files/\",\n",
        ")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "id": "8b675d04-bde8-4b3d-b9e5-99982ef742e1",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "QiskitFunction(transpile_remote_serverless)"
            ]
          },
          "execution_count": 9,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "serverless.upload(transpile_remote_demo)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "3a840c18-3010-4d05-91cb-592e2692c1d7",
      "metadata": {},
      "source": [
        "### Verify upload\n",
        "\n",
        "To check if it successfully uploaded, use `serverless.list()`, as in the following code:\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "id": "afaa3935-adf7-4b28-a8f0-583a82bbe3f1",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "QiskitFunction(transpile_remote_serverless)"
            ]
          },
          "execution_count": 10,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Get program from serverless.list() that matches the title of the one we uploaded\n",
        "next(\n",
        "    program\n",
        "    for program in serverless.list()\n",
        "    if program.title == \"transpile_remote_serverless\"\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "a416b143-6f70-49dc-ab2f-ad66f042cab1",
      "metadata": {},
      "source": [
        "## Next steps\n",
        "\n",
        "<Admonition type=\"info\" title=\"Recommendations\">\n",
        "  * Learn how to pass inputs and run your program remotely in the [Run your first Qiskit Serverless workload remotely](./serverless-run-first-workload) topic.\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
}