Using python packages with your QiskitFunction

In this document, we will learn how to install custom dependencies to your function.

Let’s create another file with our new function ./source_files/function_with_dependencies.py.

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.

Here’s what the file would look like:

from qiskit_serverless import save_result

import pendulum

dt_toronto = pendulum.datetime(2012, 1, 1, tz='America/Toronto')
dt_vancouver = pendulum.datetime(2012, 1, 1, tz='America/Vancouver')

diff = dt_vancouver.diff(dt_toronto).in_hours()

print(diff)
save_result({"hours": diff})

As you can see, we’ve imported our custom dependency, pendulum, and used its datetime method to calculate their date times.

Now, let’s create and configure our client

To install a custom dependency that our function might use we need to pass it as the dependencies argument to the QiskitFunctions class constructor. You can pass multiple dependencies.

[1]:
from qiskit_serverless import QiskitFunction

function = QiskitFunction(
    title="function-with-dependencies",
    entrypoint="function.py",
    working_dir="./source_files/",
    dependencies=["pendulum"],
)

⚠ 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.

[ ]:
from qiskit_serverless import ServerlessClient
import os

client = ServerlessClient(
    token=os.environ.get("GATEWAY_TOKEN", "awesome_token"),
    host=os.environ.get("GATEWAY_HOST", "http://localhost:8000"),
    # If you are using the kubernetes approach the URL must be http://localhost
)
client
<gateway-client>
[3]:
client.upload(function)
my_function = client.get("function-with-dependencies")
my_function
[3]:
QiskitFunction(function-with-dependencies)
[4]:
job = my_function.run()
job
[4]:
<Job | f1e286c1-f6b1-434d-b2eb-3c4aeacf797e>
[7]:
job.status()
[7]:
'DONE'
[6]:
job.result()
[6]:
{'11': 513, '00': 511}