{ "cells": [ { "cell_type": "markdown", "id": "66030e20-b384-4dcf-9c5f-7664f7ad1693", "metadata": {}, "source": [ "# Running a QiskitPattern as a function\n", "\n", "In this tutorial, we will write a basic QiskitPattern using Qiskit Serverless. We will show how to run the pattern remotely and retrieve the results from the serverless client.\n", "\n", "### Writing the QiskitPattern\n", "\n", "First, we need to write the pattern code and save it to a file called [pattern.py](./source_files/pattern.py). This pattern creates a two-qubit quantum circuit that prepares a Bell state, measures the result, and saves the measured probability distribution.\n", "\n", "The code for the pattern is shown below:\n", "\n", "```python\n", "# source_files/pattern.py\n", "\n", "from qiskit import QuantumCircuit\n", "from qiskit.primitives import Sampler\n", "\n", "from qiskit_serverless import save_result\n", "\n", "# all print statement will be available in job logs\n", "print(\"Running pattern...\")\n", "\n", "# creating circuit\n", "circuit = QuantumCircuit(2)\n", "circuit.h(0)\n", "circuit.cx(0, 1)\n", "circuit.measure_all()\n", "\n", "# running Sampler primitive\n", "sampler = Sampler()\n", "quasi_dists = sampler.run(circuit).result().quasi_dists\n", "\n", "# save results of pattern execution, \n", "# which will be accessible by calling `.result()`\n", "save_result(quasi_dists)\n", "print(\"Completed running pattern.\")\n", "```\n", "\n", "### Deploying the function\n", "\n", "To run the pattern, we need to import the necessary classes and configure them. One of these classes is `ServerlessClient`, which is a client class for interacting with compute resources.\n", "\n", " The client stores configuration information about our compute resources, such as where they are located and how to connect to them. In this example, we will use a provider that is connected to a local Docker Compose setup. In this case, it allows us to run the pattern locally on our machine. If you want to run the pattern elsewhere, you will need to provide the corresponding host and authentication details." ] }, { "cell_type": "code", "execution_count": 1, "id": "81dd7807-7180-4b87-bbf9-832b7cf29d69", "metadata": {}, "outputs": [], "source": [ "from qiskit_serverless import ServerlessClient\n", "import os" ] }, { "cell_type": "markdown", "id": "7ac24f62-8487-47fb-9805-66f2192953d4", "metadata": {}, "source": [ "> ⚠ This provider is set up with default credentials to a test cluster intended to run on your machine. For information on setting up infrastructure on your local machine, check out the guide on [local infrastructure setup](https://qiskit.github.io/qiskit-serverless/deployment/local.html)." ] }, { "cell_type": "code", "execution_count": 2, "id": "acdec789-4967-48ee-8f6c-8d2b0ff57e91", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "client = ServerlessClient(\n", " token=os.environ.get(\"GATEWAY_TOKEN\", \"awesome_token\"),\n", " host=os.environ.get(\"GATEWAY_HOST\", \"http://localhost:8000\"),\n", ")\n", "\n", "client" ] }, { "cell_type": "markdown", "id": "4dd85621-9ab0-4f34-9ab4-07ad773c5e00", "metadata": {}, "source": [ "\n", "\n", "`QiskitFunction` accepts couple of required parameters:\n", "\n", "- title - name of the program\n", "- entrypoint - name of python file you want to execute\n", "- working_dir - directory where your script is located (directory size must be less than 50MB). This is optional parameter and will be current folder by default.\n", "\n", "> Warning! All content of `working_dir` will be shipped to cluster for execution\n", "\n", "> Warning! Execution of `upload` function ships All content of `working_dir`. When the contents of `working_dir` is changed, the `upload` function must be called again to update the shipped directory contents." ] }, { "cell_type": "code", "execution_count": 3, "id": "d51df836-3f22-467c-b637-5803145d5d8a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'my-first-pattern'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from qiskit_serverless import QiskitFunction\n", "\n", "function = QiskitFunction(\n", " title=\"my-first-pattern\", entrypoint=\"pattern.py\", working_dir=\"./source_files/\"\n", ")\n", "\n", "client.upload(function)" ] }, { "cell_type": "markdown", "id": "3e5326e2-9ff8-48e8-a8a9-18716633fd01", "metadata": {}, "source": [ "### Running the QiskitFunction\n", "\n", "After deploying the QiskitFunction, we can see our pattern in a `list` of availiable functions." ] }, { "cell_type": "code", "execution_count": 4, "id": "1aeefabf", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "QiskitFunction(my-first-pattern)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_pattern_function = client.get(\"my-first-pattern\")\n", "my_pattern_function" ] }, { "cell_type": "markdown", "id": "135eda5f", "metadata": {}, "source": [ "We can run any function by calling `run` method on function object." ] }, { "cell_type": "code", "execution_count": 5, "id": "d55e3b06-8ab4-42d6-ad47-0f974d8d2247", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "job = my_pattern_function.run()\n", "job" ] }, { "cell_type": "markdown", "id": "39ee31d2-3553-4e19-bcb9-4cccd0df0e4c", "metadata": {}, "source": [ "[Job](https://qiskit.github.io/qiskit-serverless/stubs/qiskit_serverless.core.Job.html#qiskit_serverless.core.Job) instances have a `status()` method to check status of pattern execution." ] }, { "cell_type": "code", "execution_count": 7, "id": "cc7ccea6-bbae-4184-ba7f-67b6c20a0b0b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'QUEUED'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "job.status()" ] }, { "cell_type": "markdown", "id": "f496adbe-3d82-4aad-b86b-6adb3b9d287d", "metadata": {}, "source": [ "`Job` instances also have a `result()` method for retrieving results. The `result()` method will not return until the job is done running the pattern." ] }, { "cell_type": "code", "execution_count": null, "id": "ca05d063", "metadata": {}, "outputs": [], "source": [ "job.result()" ] }, { "cell_type": "markdown", "id": "719d3572", "metadata": {}, "source": [ "To inspect the logs from a pattern, access them from the ``Job`` instance." ] }, { "cell_type": "code", "execution_count": 8, "id": "eb5ec85f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "OpenBLAS WARNING - could not determine the L2 cache size on this system, assuming 256k\n", "OpenBLAS WARNING - could not determine the L2 cache size on this system, assuming 256k\n", "Running pattern...\n", "Completed running pattern.\n", "\n" ] } ], "source": [ "print(job.logs())" ] }, { "cell_type": "markdown", "id": "9784597b-9377-4d26-8ab9-a8a9b363c924", "metadata": {}, "source": [ "`ServerlessClient` object has method `.widget` which renders Jupyter widget to see list of executed programs." ] }, { "cell_type": "code", "execution_count": 8, "id": "f24023e1-6ce4-481e-b43d-3e19bff81d57", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "96f50f9769514576a3a4499b5f2125a5", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Tab(children=(GridspecLayout(children=(Output(layout=Layout(grid_area='widget001')), Output(layout=Layout(grid…" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "client.widget()" ] }, { "cell_type": "code", "execution_count": null, "id": "f3129d55", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.19" } }, "nbformat": 4, "nbformat_minor": 5 }