Running a Qiskit Function as a function

In this tutorial, we will write a basic Qiskit Function using Qiskit Serverless. We will show how to run the function remotely and retrieve the results from the serverless client.

Writing the Qiskit Function

First, we need to write the function code and save it to a file called function.py. This function creates a two-qubit quantum circuit that prepares a Bell state, measures the result, and saves the measured probability distribution.

The code for the function is shown below:

from qiskit import QuantumCircuit
from qiskit.primitives import Sampler

from qiskit_serverless import save_result

# all print statement will be available in job logs
print("Running function...")

# creating circuit
circuit = QuantumCircuit(2)
circuit.h(0)
circuit.cx(0, 1)
circuit.measure_all()

# running Sampler primitive
sampler = Sampler()
quasi_dists = sampler.run(circuit).result().quasi_dists

# save results of function execution,
# which will be accessible by calling `.result()`
save_result(quasi_dists)
print("Completed running function.")

Deploying the function

To run the function, we need to import the necessary classes and configure them. One of these classes is ServerlessClient, which is a client class for interacting with compute resources.

The client stores configuration information about our compute resources, such as where they are located and how to connect to them. In this example, we will use a provider that is connected to a local Docker Compose setup. In this case, it allows us to run the function locally on our machine. If you want to run the function elsewhere, you will need to provide the corresponding host and authentication details.

[1]:
from qiskit_serverless import ServerlessClient
import os

⚠ 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]:
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
[2]:
<gateway-client>

QiskitFunction accepts couple of required parameters:

  • title - name of the Qiskit Function

  • entrypoint - name of python file you want to execute

  • working_dir - directory where your script is located (directory size must be less than 50MB). This is optional parameter and will be current folder by default.

Warning! All content of working_dir will be shipped to cluster for execution

Warning! Execution of upload function ships All content of working_dir. When the contents of working_dir is changed, the upload function must be called again to update the shipped directory contents.

[3]:
from qiskit_serverless import QiskitFunction

function = QiskitFunction(
    title="my-first-function", entrypoint="function.py", working_dir="./source_files/"
)

client.upload(function)
[3]:
QiskitFunction(my-first-function)

Running the QiskitFunction

After deploying the QiskitFunction, we can see our function in a list of availiable functions.

[4]:
my_first_function = client.get("my-first-function")
my_first_function
[4]:
QiskitFunction(my-first-function)

We can run any function by calling run method on function object.

[5]:
job = my_first_function.run()
job
[5]:
<Job | 860c98e2-ec3c-4197-8042-115ec3c6f5e8>

Job instances have a status() method to check status of the function execution.

[15]:
job.status()
[15]:
'DONE'

Job instances also have a result() method for retrieving results. The result() method will not return until the job is done running the function.

[16]:
job.result()
[16]:
{'00': 539, '11': 485}

To inspect the logs from a function, access them from the Job instance.

[17]:
print(job.logs())
2024-12-05 17:26:40,785 INFO job_manager.py:531 -- Runtime env is setting up.
Running function...
Completed running function.

ServerlessClient object has method .widget which renders Jupyter widget to see list of executed programs.

[ ]:
client.widget()