Skip to main content

A library to interact with your qserver

Project description

Qserver Connect

A python library to help you connect with your qserver instance.

With this one, is easy to submit a job and retrieve its results after its execution.

Installing

To install it, you must have installed:

  • python >= 3.12
  • pip/pip3
  • a qserver instance running somewhere

ensuring that, you can run at your client machine:

pip install qserver-connect

Usage

After installing, create a quantum circuit in your python framework of choice and then do the following:

  1. Add a plugin to your backend(qserver)

If you haven't added any plugins to your server, you can use this very library to add. Here is a simple example of how does it work:

from qserver_connect import Plugin
from qserver_connect.exceptions import FailedOnAddPlugin

# ... your code

# host = the ip/domain your instance is running
# http_port = by default the server exposes http as 8080 and https as 443, so add the one you want

# by default the plugin class uses https, so if you want to use http
# set secure_connection=False
plugin_manager = Plugin(host=host, port=http_port)

try:
    plugin_manager.add_plugin('plugin-name-you-want-to-add')
except FailedOnAddPlugin as error:
    # if there''s a problem with connection
    # or you already have added this plugin before
    # an error may be raised, so ensure you always
    # do exception handling    
    print(f"An error has occurred: {str(error)}")
  1. submit your job

Then, you can submit the quantum circuit you've done and get the results.

To do that you can choose doing manually or allow the library to generate the data for you.

Till this date (13/04/2025), the automatic job generation is only supported by Qiskit circuits.

Qiskit auto

from qserver_connect import Qiskit

# qc = your quantum circuit
# ... the remaining of your code


job = Qiskit(qc, {
    "backend" : # the backend you've chosen to use,
    "counts" : #set True if you want to retrieve the counts from this execution,
    "quasi_dist" : # set True if you want to retrieve the quasi_dist from this job,
    "expval": # set True if you want to retrieve expectation values from your circuit,
    "shots": # set the amount of shots you want. By default 1000 are taken. It's optional an only works when either "counts" or "quasi_dist" is/are True,
    "obs": # A list of Pauli Operators and coefficients following the qiskit pattern. It's required and only works when "expval" is True
})

Manual

from qserver_connect import Job

# First of all, you need to export your circuit to a .qasm file
# then run do:

job = Job({
    "qasm": #path to your qasm file,
    "counts": # True if you want to get counts from your circuit,
    "quasi_dist": # True if you want to get the quasi_dist from your circuit,
    "expval": # True if you want to get expectation values from your circuit,
    "simulator": # the target backend,
    "metadata": # the metadata must contain keys for shots (if you want a custom amount), and observables (when getting expval)
})

With your job object in hands, now you can, finally, submit your job and wait for the results. Here's a simple example of how could you do that:

from time import sleep

from qserver_connect import JobConnection

# host = the host your backend is running
# http_port = the port for http/https
# grpc_port = the port to connect via grpc. By default, the backend runs the same as the http_port (8080 or 443), in case you've changed it, insert the new value here

# by default it also uses tls. So to used the unsecure version set: secure_connection=False
backend = JobConnection(host=host, http_port=http_port, grpc_port)

try:
    job_id = backend.send_job(job)
    
    while True:
        status = backend.get_job_data(job_id).get('status')

        if status is None:
            raise ValueError("Invalid status")

        print(f"Current status: {status}")

        if status not in ('pending', 'running'):
            break
        
        print("waiting job to finish")
        sleep(5)

    results = backend.get_job_result(job_id)
    print(results)

except Exception as error:
    print(f"Error: {str(error)}")

Available functions:

Plugin

method parameters returns
add_plugin name:str -
delete_plugin name:str -

JobConnection

method parameters returns
send_job job_data:Job the job id (str)
get_job_data job_id:str the json response (dict)
get_job_result job_id:str the json response (dict)
get_all_jobs cursor:Optional[int] the paginated jobs list (List[dict])
delete_job job_id:str -

Dev usage

Testing

For tests, you must have:

  • qserver running
  • conda/pip(3)
  • python >= 3.12

First, to run the tests, you must install the dependencies. You can use the pure pip installation, or use conda/conda-lock to start.

# with pip
pip install -r requirements.txt -r dev-requirements.txt

# using conda based env
conda env create -f environment.yml
mamba env create -f environment.yml
conda-lock install -n qserver-connect conda-lock.yml

# remember to activate your environment

Then, run the tests with:

HOST="the-backend-host" tox

Get newest proto definition

Once we are using grpc/protobuf to handle jobs, we need to ensure we're using the latest definition.

It's not required to run it every time, but if you notice any major change in qserver, run:

# ensure you have installed everything and, if you've chosen to use a virtual env,
# the env is activated 

chmod +x get-proto-from-server.sh && ./get-proto-from-server.sh

Contributing

Feel free to open issues and PRs adding, updating, removing and requesting features you would like to see in this project.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

qserver_connect-0.0.22.tar.gz (19.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

qserver_connect-0.0.22-py3-none-any.whl (18.9 kB view details)

Uploaded Python 3

File details

Details for the file qserver_connect-0.0.22.tar.gz.

File metadata

  • Download URL: qserver_connect-0.0.22.tar.gz
  • Upload date:
  • Size: 19.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for qserver_connect-0.0.22.tar.gz
Algorithm Hash digest
SHA256 eac9924bfbcadbe9c1530e8ba704b62e4678ab41edd786941aff427c1f0fdfc5
MD5 fe9183059771698515bf1bac0422a2a5
BLAKE2b-256 953eaab3453d266997c7c7a98f641d926e52a69d0bfef1dc8eec02626ace1052

See more details on using hashes here.

Provenance

The following attestation bundles were made for qserver_connect-0.0.22.tar.gz:

Publisher: test-publish.yml on Dpbm/qserver-connect

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file qserver_connect-0.0.22-py3-none-any.whl.

File metadata

File hashes

Hashes for qserver_connect-0.0.22-py3-none-any.whl
Algorithm Hash digest
SHA256 33412e4b9ce06684ca2afe055281d1ae2758875710e847232d6b41023ad470d3
MD5 af5a177a3c1115c0fec66a63f09db880
BLAKE2b-256 da960a5045e3fc4a6d429aa99014068261d39ca86fc334fb652d7ac5355b6b50

See more details on using hashes here.

Provenance

The following attestation bundles were made for qserver_connect-0.0.22-py3-none-any.whl:

Publisher: test-publish.yml on Dpbm/qserver-connect

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page