{ "cells": [ { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "# Using python packages with your QiskitFunction\n", "\n", "In this document, we will learn how to install custom dependencies to your function.\n", "\n", "Let's create another file with our new function [./source_files/function_with_dependencies.py](./source_files/function_with_dependencies.py). \n", "\n", "For the sake of this example, let's use the `pendulum` package as our custom dependency. We are going to calculate the difference in ours between Toronto and Vancouver timezones.\n", "\n", "Here's what the file would look like:\n", "\n", "```python\n", "from qiskit_serverless import save_result\n", "\n", "import pendulum\n", "\n", "dt_toronto = pendulum.datetime(2012, 1, 1, tz='America/Toronto')\n", "dt_vancouver = pendulum.datetime(2012, 1, 1, tz='America/Vancouver')\n", "\n", "diff = dt_vancouver.diff(dt_toronto).in_hours() \n", "\n", "print(diff)\n", "save_result({\"hours\": diff})\n", "```\n", "\n", "As you can see, we've imported our custom dependency, `pendulum`, and used its `datetime` method to calculate their date times.\n", "\n", "Now, let's create and configure our client" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To install a custom dependency that our function might use we need to pass it as the `dependencies` argument to the `QiskitFunctions` class constructor. \n", "You can pass multiple dependencies." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [] }, "outputs": [], "source": [ "from qiskit_serverless import QiskitFunction\n", "\n", "function = QiskitFunction(\n", " title=\"function-with-dependencies\",\n", " entrypoint=\"function.py\",\n", " working_dir=\"./source_files/\",\n", " dependencies=[\"pendulum\"],\n", ")" ] }, { "cell_type": "markdown", "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": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 2, "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", " # If you are using the kubernetes approach the URL must be http://localhost\n", ")\n", "client" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "QiskitFunction(function-with-dependencies)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "client.upload(function)\n", "my_function = client.get(\"function-with-dependencies\")\n", "my_function" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "job = my_function.run()\n", "job" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'DONE'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "job.status()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'11': 513, '00': 511}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "job.result()" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "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.11.9" } }, "nbformat": 4, "nbformat_minor": 4 }