Using python packages with your QiskitPattern

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

Let’s create another file with our new pattern ./source_files/pattern_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:

# source_files/pattern_with_dependencies.py

# source_files/program_4.py

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 pattern might use we need to pass it as the dependencies argument to the QiskitFunctions class constructor. You can pass multiple dependencies and specify versions.

[1]:
from qiskit_serverless import QiskitFunction

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

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

[2]:
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"),
)
client
[2]:
<gateway-client>
[4]:
client.upload(function)
my_pattern_function = client.get("pattern-with-dependencies")
my_pattern_function
[4]:
QiskitFunction(pattern-with-dependencies)
[6]:
job = my_pattern_function.run()
job
[6]:
<Job | 5c579361-13e6-4d7c-9fc3-98e0b57ae232>
[6]:
job.status()
[6]:
'QUEUED'
[7]:
job.result()
[7]:
{'quasi_dists': {'0': 1.0}}