quantum-serverless/client
github-actions[bot] 509e0c2375
update-version-0.18.0 (#1536)
* [create-pull-request] automated change

* Update last references

---------

Co-authored-by: Tansito <9059044+Tansito@users.noreply.github.com>
2024-11-04 15:23:27 +01:00
..
qiskit_serverless update-version-0.18.0 (#1536) 2024-11-04 15:23:27 +01:00
tests Add `is_running_in_serverless()` function (#1532) 2024-11-04 13:33:32 +01:00
.pylintrc Rename function.get_jobs to function.jobs and make it return Job objects (#1480) 2024-09-11 11:09:10 -04:00
README.md Python 3.11 is only supported python version (#1420) 2024-07-31 12:19:29 -04:00
mypy.ini Quantum Serverless initial implementation (#1) 2022-09-19 09:31:56 -04:00
pyproject.toml bump setuptools version (#1419) 2024-07-23 16:21:53 -04:00
requirements-dev.txt Fix lint issues with ray image (#1432) 2024-08-02 12:36:48 -04:00
requirements.txt update zipp version in client (#1495) 2024-09-18 14:51:00 -04:00
setup.cfg Update README.md (#43) 2022-10-31 14:35:41 -04:00
setup.py Python 3.11 is only supported python version (#1420) 2024-07-31 12:19:29 -04:00
tox.ini Python 3.11 is only supported python version (#1420) 2024-07-31 12:19:29 -04:00

README.md

Stability Client verify process License Code style: Black Python Qiskit

Qiskit Serverless client

diagram

Installation

pip install qiskit_serverless

Documentation

Full docs can be found at https://qiskit.github.io/qiskit-serverless/

Usage

Step 1: write funtion in ./src/function.py

from qiskit_serverless import distribute_task, get, get_arguments, save_result

from qiskit import QuantumCircuit
from qiskit.circuit.random import random_circuit
from qiskit.primitives import StatevectorSampler as Sampler
from qiskit.quantum_info import SparsePauliOp

# 1. let's annotate out function to convert it
# to distributed async function
# using `distribute_task` decorator
@distribute_task()
def distributed_sample(circuit: QuantumCircuit):
    """Calculates quasi dists as a distributed function."""
    return Sampler().run([(circuit)]).result()[0].data.meas.get_counts()

# 2. our program will have one arguments
# `circuits` which will store list of circuits
# we want to sample in parallel.
# Let's use `get_arguments` funciton
# to access all program arguments
arguments = get_arguments()
circuits = arguments.get("circuits", [])

# 3. run our functions in a loop
# and get execution references back
function_references = [
    distributed_sample(circuit)
    for circuit in circuits
]

# 4. `get` function will collect all
# results from distributed functions
collected_results = get(function_references)

# 5. `save_result` will save results of program execution
# so we can access it later
save_result({
    "quasi_dists": collected_results
})

Step 2: run function

from qiskit_serverless import ServerlessClient, QiskitFunction
from qiskit.circuit.random import random_circuit

client = ServerlessClient(
    token="<TOKEN>",
    host="<GATEWAY_ADDRESS>",
)

# create function
function = QiskitFunction(
    title="Quickstart",
    entrypoint="program.py",
    working_dir="./src"
)
client.upload(function)

# create inputs to our program
circuits = []
for _ in range(3):
    circuit = random_circuit(3, 2)
    circuit.measure_all()
    circuits.append(circuit)

# run program
my_function = client.get("Quickstart")
job = my_function.run(circuits=circuits)

Step 3: monitor job status

job.status()
# 'DONE'

# or get logs
job.logs()

Step 4: get results

job.result()
# {'quasi_dists': [
# {'101': 902, '011': 66, '110': 2, '111': 37, '100': 17},
# {'100': 626, '101': 267, '001': 49, '000': 82},
# {'010': 145, '100': 126, '011': 127, '001': 89, '110': 173, '111': 166, '000': 94, '101': 104}
# ]}