Running a QiskitPattern as a function#

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

Writing the QiskitPattern#

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

The code for the pattern is shown below:

# source_files/pattern.py

from qiskit import QuantumCircuit
from qiskit.primitives import Sampler

from quantum_serverless import save_result

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

# 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 pattern execution,
# which will be accessible by calling `.result()`
save_result(quasi_dists)
print("Completed running pattern.")

Deploying the function#

To run the pattern, 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 pattern locally on our machine. If you want to run the pattern elsewhere, you will need to provide the corresponding host and authentication details.

[1]:
from quantum_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]:
serverless = ServerlessClient(
    token=os.environ.get("GATEWAY_TOKEN", "awesome_token"),
    host=os.environ.get("GATEWAY_HOST", "http://localhost:8000"),
)

serverless
[2]:
<gateway-client>

QiskitFunction accepts couple of required parameters:

  • title - name of the program

  • 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 quantum_serverless import QiskitFunction

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

serverless.upload(function)
[3]:
'my-first-pattern'

Running the QiskitFunction#

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

[4]:
functions = {f.title: f for f in serverless.list()}
my_pattern_function = functions.get("my-first-pattern")
my_pattern_function
[4]:
QiskitFunction(my-first-pattern)

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

[5]:
job = my_pattern_function.run()
job
[5]:
<Job | 3148c695-0e48-4bfa-82ff-119288cfac00>

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

[6]:
job.status()
[6]:
'QUEUED'

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

[7]:
job.result()
[7]:
[{'0': 0.4999999999999999, '3': 0.4999999999999999}]

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

[8]:
print(job.logs())
OpenBLAS WARNING - could not determine the L2 cache size on this system, assuming 256k
OpenBLAS WARNING - could not determine the L2 cache size on this system, assuming 256k
Running pattern...
Completed running pattern.

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

[10]:
serverless.widget()
[10]:
[ ]: