{ "cells": [ { "cell_type": "markdown", "id": "66030e20-b384-4dcf-9c5f-7664f7ad1693", "metadata": {}, "source": [ "# Retrieving Results from Old Jobs\n", "\n", "In this tutorial, we will run two patterns and then retrieve the results of each pattern using the job IDs and the serverless client." ] }, { "cell_type": "markdown", "id": "37322958-a029-46bb-bc46-82b7ded329b5", "metadata": {}, "source": [ "First, create [ServerlessProvider](https://qiskit.github.io/qiskit-serverless/stubs/qiskit_serverless.core.Provider.html#qiskit_serverless.core.ServerlessProvider) instance.\n", "\n", "> ⚠ 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": 1, "id": "acdec789-4967-48ee-8f6c-8d2b0ff57e91", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from qiskit_serverless import ServerlessClient\n", "import os\n", "\n", "client = ServerlessClient(\n", " token=os.environ.get(\"GATEWAY_TOKEN\", \"awesome_token\"),\n", " host=os.environ.get(\"GATEWAY_HOST\", \"http://localhost:8000\"),\n", ")\n", "client" ] }, { "cell_type": "markdown", "id": "e076c12a-b372-4335-bd4a-7e3e24fcca73", "metadata": {}, "source": [ "Run two patterns in parallel." ] }, { "cell_type": "code", "execution_count": 2, "id": "d51df836-3f22-467c-b637-5803145d5d8a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(,\n", " )" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from qiskit_serverless import QiskitFunction\n", "\n", "function = QiskitFunction(\n", " title=\"pattern-to-fetch-results\", entrypoint=\"pattern.py\", working_dir=\"./source_files/\"\n", ")\n", "client.upload(function)\n", "\n", "my_function = client.get(\"pattern-to-fetch-results\")\n", "\n", "job1 = my_function.run()\n", "job2 = my_function.run()\n", "\n", "job1, job2" ] }, { "cell_type": "markdown", "id": "59a49dc9-cbad-4c05-a831-b0e9dc882ca0", "metadata": {}, "source": [ "Retrieve the job IDs for each of the running patterns." ] }, { "cell_type": "code", "execution_count": 3, "id": "f621f786-6ba7-4ef1-8121-741f21f70233", "metadata": {}, "outputs": [], "source": [ "job_id1 = job1.job_id\n", "job_id2 = job2.job_id" ] }, { "cell_type": "markdown", "id": "675f9b7a-7d44-43e3-baea-0289cf29e573", "metadata": {}, "source": [ "Call the blocking comand, [Job](https://qiskit.github.io/qiskit-serverless/stubs/qiskit_serverless.core.Job.html#qiskit_serverless.core.Job)``.result()``, to ensure the results are ready in the following cells." ] }, { "cell_type": "code", "execution_count": 4, "id": "cc7ccea6-bbae-4184-ba7f-67b6c20a0b0b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'0': 0.4999999999999999, '3': 0.4999999999999999}]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "job1.result()\n", "job2.result()" ] }, { "cell_type": "markdown", "id": "d0bd294e-af9e-4817-a58d-84125e104c7f", "metadata": {}, "source": [ "Retrieve the completed jobs through the serverless client, using the job IDs." ] }, { "cell_type": "code", "execution_count": 5, "id": "1dc78690-f61a-4dfe-bc0e-7007cf561a5b", "metadata": {}, "outputs": [], "source": [ "retrieved_job1 = client.get_job_by_id(job_id1)\n", "retrieved_job2 = client.get_job_by_id(job_id2)" ] }, { "cell_type": "markdown", "id": "a02841bd-ca61-4b01-a726-c3ddb5469ef8", "metadata": {}, "source": [ "Inspect the results of the retrieved jobs." ] }, { "cell_type": "code", "execution_count": 6, "id": "ca1cbfee-df32-4306-988f-ef0fa31605ee", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Job 1 results: [{'0': 0.4999999999999999, '3': 0.4999999999999999}]\n", "Job 2 results: [{'0': 0.4999999999999999, '3': 0.4999999999999999}]\n" ] } ], "source": [ "print(f\"Job 1 results: {retrieved_job1.result()}\")\n", "print(f\"Job 2 results: {retrieved_job2.result()}\")" ] }, { "cell_type": "markdown", "id": "24942f00-680e-4cea-b0e7-bc75b19565fe", "metadata": {}, "source": [ "To inspect the logs from a pattern, access them from the [Job](https://qiskit.github.io/qiskit-serverless/stubs/qiskit_serverless.core.Job.html#qiskit_serverless.core.Job) instance." ] }, { "cell_type": "code", "execution_count": 7, "id": "5e39caf1-1506-44de-9fbc-248e106be6b5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Job 1 logs: 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(f\"Job 1 logs: {retrieved_job1.logs()}\")" ] }, { "cell_type": "markdown", "id": "15983446-497e-487b-b92d-5c5096f911d7", "metadata": {}, "source": [ "To get a list of all previously executed patterns, use the `.get_jobs()` method of the `QiskitServerless` object. \n", "The `get_jobs` method accepts 2 optional parameters, `limit` and `offset`, which control the size of returned results." ] }, { "cell_type": "code", "execution_count": 8, "id": "53f63569-3b80-4ebe-a181-1aa5b3537a7f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[,\n", " ]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "client.get_jobs(limit=2, offset=1)" ] } ], "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 }