{ "cells": [ { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "# Using python packages with your QiskitPattern\n", "\n", "In this document, we will learn how to install custom dependencies to your pattern.\n", "\n", "Let's create another file with our new pattern [./source_files/pattern_with_dependencies.py](./source_files/pattern_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", "# source_files/pattern_with_dependencies.py\n", "\n", "# source_files/program_4.py\n", "\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 pattern might use we need to pass it as the `dependencies` argument to the `QiskitFunctions` class constructor. \n", "You can pass multiple dependencies and specify versions. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [] }, "outputs": [], "source": [ "from qiskit_serverless import QiskitFunction\n", "\n", "function = QiskitFunction(\n", " title=\"pattern-with-dependencies\",\n", " entrypoint=\"pattern_with_dependencies.py\",\n", " working_dir=\"./source_files/\",\n", " dependencies=[\"pendulum==3.0.0\"],\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": 2, "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", ")\n", "client" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "QiskitFunction(pattern-with-dependencies)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "client.upload(function)\n", "my_pattern_function = client.get(\"pattern-with-dependencies\")\n", "my_pattern_function" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "job = my_pattern_function.run()\n", "job" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'QUEUED'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "job.status()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'quasi_dists': {'0': 1.0}}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "job.result()" ] } ], "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": 4 }