Pasqal Cloud Connection
WorkloadNotDoneError
Section titled “
WorkloadNotDoneError
”
Bases: Exception
Is raised if a workload is not yet finished running on remote.
WorkloadSpec(circuit, backend, result_types, parameter_values=None, observable=None)
dataclass
Section titled “
WorkloadSpec(circuit, backend, result_types, parameter_values=None, observable=None)
dataclass
”Specification of a workload to be executed on Pasqal Cloud.
This data class defines a single workload specification that is to be executed on Pasqal's cloud platform.
| PARAMETER | DESCRIPTION |
|---|---|
circuit
|
The quantum circuit to be executed.
TYPE:
|
backend
|
The backend to execute the workload on. Not all backends are available on the
cloud platform. Currently the supported backend is
TYPE:
|
result_types
|
The types of result to compute for this workload. The circuit will be run for all result types specified here one by one.
TYPE:
|
parameter_values
|
If the quantum circuit has feature parameters, values for those need to
be provided. In the case there are only variational parameters, this field is
optional. In the case there are no parameters, this field needs to be
TYPE:
|
observable
|
Observable that is used when
TYPE:
|
WorkloadStoppedError
Section titled “
WorkloadStoppedError
”
Bases: Exception
Is raised when a workload has stopped running on remote for some reason.
check_status(connection, workload_id)
Section titled “
check_status(connection, workload_id)
”Checks if the workload is successfully finished on remote connection.
| PARAMETER | DESCRIPTION |
|---|---|
connection
|
A
TYPE:
|
workload_id
|
the id
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
WorkloadNotDoneError
|
Is raised when the workload status is "PENDING", "RUNNING" or "PAUSED". |
WorkloadStoppedError
|
Is raise when the workload status is "CANCELED", "TIMED_OUT" or "ERROR". |
ValueError
|
Is raised when the workload status has an unsupported value. |
| RETURNS | DESCRIPTION |
|---|---|
Workload
|
The workload result if its status is "DONE" as a |
Source code in qadence/pasqal_cloud_connection.py
174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206def check_status(connection: SDK, workload_id: str) -> WorkloadResult: """Checks if the workload is successfully finished on remote connection.
Args: connection: A `pasqal_cloud.SDK` instance which is used to connect to the cloud. workload_id: the id `str` that is associated with the workload.
Raises: WorkloadNotDoneError: Is raised when the workload status is "PENDING", "RUNNING" or "PAUSED". WorkloadStoppedError: Is raise when the workload status is "CANCELED", "TIMED_OUT" or "ERROR". ValueError: Is raised when the workload status has an unsupported value.
Returns: The workload result if its status is "DONE" as a `pasqal_cloud.Workload` object. """ # TODO Make the function return a "nice" result object result = connection.get_workload(workload_id) if result.status == "DONE": return result if result.status in ("PENDING", "RUNNING", "PAUSED"): raise WorkloadNotDoneError( f"Workload with id {workload_id} is not yet finished, the status is {result.status}" ) if result.status in ("CANCELED", "TIMED_OUT", "ERROR"): message = f"Workload with id {workload_id} couldn't finish, the status is {result.status}." if result.status == "ERROR": message += f"The following error(s) occurred {result.errors}" raise WorkloadStoppedError(message) raise ValueError( f"Undefined workload status ({result.status}) was returned for workload ({result.id})" )
get_result(connection, workload_id, timeout=60.0, refresh_time=1.0)
Section titled “
get_result(connection, workload_id, timeout=60.0, refresh_time=1.0)
”Repeatedly checks if a workload has finished and returns the result.
| PARAMETER | DESCRIPTION |
|---|---|
connection
|
A
TYPE:
|
workload_id
|
the id
TYPE:
|
timeout
|
Time in seconds after which the function times out. Defaults to 60.0.
TYPE:
|
refresh_time
|
Time in seconds after which the remote is requested to update the status again, when the workload is not finished yet. Defaults to 1.0.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
TimeoutError
|
description |
| RETURNS | DESCRIPTION |
|---|---|
Workload
|
The workload result if its status is "DONE" as a |
Source code in qadence/pasqal_cloud_connection.py
209210211212213214215216217218219220221222223224225226227228229230231232233234235def get_result( connection: SDK, workload_id: str, timeout: float = 60.0, refresh_time: float = 1.0) -> WorkloadResult: """Repeatedly checks if a workload has finished and returns the result.
Args: connection: A `pasqal_cloud.SDK` instance which is used to connect to the cloud. workload_id: the id `str` that is associated with the workload. timeout: Time in seconds after which the function times out. Defaults to 60.0. refresh_time: Time in seconds after which the remote is requested to update the status again, when the workload is not finished yet. Defaults to 1.0.
Raises: TimeoutError: _description_
Returns: The workload result if its status is "DONE" as a `pasqal_cloud.Workload` object. """ max_refresh_count = int(timeout // refresh_time) for _ in range(max_refresh_count): try: result = check_status(connection, workload_id) except WorkloadNotDoneError: time.sleep(refresh_time) continue return result raise TimeoutError("Request timed out because it wasn't finished in the specified time. ")
get_workload_spec(model, result_types, parameter_values=None, observable=None)
Section titled “
get_workload_spec(model, result_types, parameter_values=None, observable=None)
”Creates a WorkloadSpec from a quantum model.
This function creates a WorkloadSpec from a QuantumModel and the other arguments provided.
The circuit, that is extracted from the model, is the original circuit that was used to
initialize the model, not the backend converted circuit in model.circuit. The backend set in
the model will be used in the workload specification.
It is important to note that in case there is an observable defined in the model, it is ignored in the workload specification. To provide an observable to the workload specification, it is only possible to set it in the observable argument of this function.
| PARAMETER | DESCRIPTION |
|---|---|
model
|
The quantum model that defines the circuit and backend for the workload spec.
TYPE:
|
result_types
|
A list of result types that is requested in this workload.
TYPE:
|
parameter_values
|
The parameter values that should be used during execution of the workload.
TYPE:
|
observable
|
Observable that is used when
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
WorkloadSpec
|
A |
Source code in qadence/pasqal_cloud_connection.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103def get_workload_spec( model: QuantumModel, result_types: list[ResultType], parameter_values: dict[str, Tensor] | None = None, observable: AbstractBlock | None = None,) -> WorkloadSpec: """Creates a `WorkloadSpec` from a quantum model.
This function creates a `WorkloadSpec` from a `QuantumModel` and the other arguments provided. The circuit, that is extracted from the model, is the original circuit that was used to initialize the model, not the backend converted circuit in `model.circuit`. The backend set in the model will be used in the workload specification.
It is important to note that in case there is an observable defined in the model, it is ignored in the workload specification. To provide an observable to the workload specification, it is only possible to set it in the observable argument of this function.
Args: model: The quantum model that defines the circuit and backend for the workload spec. result_types: A list of result types that is requested in this workload. parameter_values: The parameter values that should be used during execution of the workload. observable: Observable that is used when `result_types` contains `ResultType.EXPECTATION`. The observable field is mandatory in this case. If not, the value of this field will be ignored. Only a single observable can be passed for cloud submission; providing a list of observables is not supported.
Returns: A `WorkloadSpec` instance based on the quantum model passed to this function. """ circuit = model._circuit.original backend = model._backend_name return WorkloadSpec(circuit, backend, result_types, parameter_values, observable)
submit_workload(connection, workload)
Section titled “
submit_workload(connection, workload)
”Uploads a workload to Pasqal's Cloud and returns the created workload ID.
| PARAMETER | DESCRIPTION |
|---|---|
connection
|
A
TYPE:
|
workload
|
A
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
A workload id as a |
Source code in qadence/pasqal_cloud_connection.py
143144145146147148149150151152153154155156157158159def submit_workload(connection: SDK, workload: WorkloadSpec) -> str: """Uploads a workload to Pasqal's Cloud and returns the created workload ID.
Args: connection: A `pasqal_cloud.SDK` instance which is used to connect to the cloud. workload: A `WorkloadSpec` object, defining the specification of the workload that needs to be uploaded.
Returns: A workload id as a `str`. """ workload_json = _workload_spec_to_json(workload) remote_workload = connection.create_workload( workload_json.workload_type, workload_json.backend_type, workload_json.config ) workload_id: str = remote_workload.id return workload_id