sf.api.Connection

class Connection(token=None, host=None, port=None, use_ssl=None, verbose=True)[source]

Bases: object

Manages remote connections to the remote job execution platform and exposes various job operations.

For basic usage, it is not necessary to manually instantiate this object; the user is encouraged to use the higher-level interface provided by RemoteEngine.

Example:

The following example instantiates a Connection for a given API authentication token, tests the connection, submits a new job, and retrieves an existing job.

>>> connection = Connection(token="abc")
>>> success = connection.ping()  # `True` if successful, `False` if the connection fails
>>> job = connection.create_job("chip_name", program, run_options={"shots":123})
>>> job
<Job: id=d177cbf5-1816-4779-802f-cef2c117dc1a, ...>
>>> job.status
"queued"
>>> job.result
AttributeError                            Traceback (most recent call last)
<ipython-input-12-d54011f90b17> in <module>
...
AttributeError: The result is undefined for jobs that are not completed (current status: queued)
>>> job = connection.get_job(known_job_id)
>>> job
<Job: id=59a1c0b1-c6a7-4f9b-ae37-0ac5eec9c413, ...>
>>> job.status
"complete"
>>> job.result
[[0 1 0 2 1 0 0 0]]
Parameters
  • token (str) – the API authentication token

  • host (str) – the hostname of the remote platform

  • port (int) – the port to connect to on the remote host

  • use_ssl (bool) – whether to use SSL for the connection

host

The host for the remote platform.

port

The port to connect to on the remote host.

token

The API authentication token.

use_ssl

Whether to use SSL for the connection.

host

The host for the remote platform.

Return type

str

Returns

str

port

The port to connect to on the remote host.

Return type

int

Returns

int

token

The API authentication token.

Return type

str

Returns

str

use_ssl

Whether to use SSL for the connection.

Return type

bool

Returns

bool

cancel_job(job_id)

Cancels a job.

create_job(target, program[, run_options])

Creates a job with the given circuit.

get_all_jobs([after])

Gets a list of jobs created by the user, optionally filtered by datetime.

get_device_spec(target)

Gets the device specifications for target.

get_job(job_id)

Gets a job.

get_job_result(job_id)

Returns the result of a job.

get_job_status(job_id)

Returns the status of a job.

ping()

Tests the connection to the remote backend.

cancel_job(job_id)[source]

Cancels a job.

Parameters

job_id (str) – the job ID

create_job(target, program, run_options=None)[source]

Creates a job with the given circuit.

Parameters
  • target (str) – the target device

  • program (strawberryfields.Program) – the quantum circuit

  • run_options (Dict[str, Any]) – Runtime arguments for the program execution. If provided, overrides the value stored in the given program.

Returns

the created job

Return type

strawberryfields.api.Job

get_all_jobs(after=datetime.datetime(1970, 1, 1, 0, 0))[source]

Gets a list of jobs created by the user, optionally filtered by datetime.

Parameters

after (datetime.datetime) – if provided, only jobs more recently created then after are returned

Returns

the jobs

Return type

List[strawberryfields.api.Job]

get_device_spec(target)[source]

Gets the device specifications for target.

Parameters

target (str) – the target device

Returns

the created device specification

Return type

strawberryfields.api.DeviceSpec

get_job(job_id)[source]

Gets a job.

Parameters

job_id (str) – the job ID

Returns

the job

Return type

strawberryfields.api.Job

get_job_result(job_id)[source]

Returns the result of a job.

Parameters

job_id (str) – the job ID

Returns

the job result

Return type

strawberryfields.api.Result

get_job_status(job_id)[source]

Returns the status of a job.

Parameters

job_id (str) – the job ID

Returns

the job status

Return type

str

ping()[source]

Tests the connection to the remote backend.

Returns

True if the connection is successful, and False otherwise

Return type

bool