Skip to content

Commit

Permalink
Merge pull request #81 from qiboteam/M_nshots_hardware
Browse files Browse the repository at this point in the history
Raising errors for missing M or nshots
  • Loading branch information
BrunoLiegiBastonLiegi authored Oct 21, 2024
2 parents 0ea1e2f + 4c7aa4e commit d72c7e4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ obtain the needed token to run computations on the cluster.

The following snippet provides a basic usage example.
Replace the `your-token` string with your user token received during the
registration process.
registration process. To check which devices are available with your account
please visit the dashboard at [https://cloud.qibo.science/](https://cloud.qibo.science/).

```python
import qibo
Expand All @@ -46,7 +47,8 @@ token = "your-token"
client = qibo_client.Client(token)

# run the circuit
job = client.run_circuit(circuit, nshots=1000, device="<set device name>")
device = "device_name"
job = client.run_circuit(circuit, nshots=1000, device=device)
result = job.result()
print(result)
```
25 changes: 15 additions & 10 deletions src/qibo_client/qibo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,29 +67,34 @@ def check_client_server_qibo_versions(self):
def run_circuit(
self,
circuit: qibo.Circuit,
nshots: int = 1000,
device: str = "k2",
) -> T.Optional[qibo.result.QuantumState]:
device: str,
nshots: int = None,
) -> T.Optional[
T.Union[
qibo.result.QuantumState,
qibo.result.MeasurementOutcomes,
qibo.result.CircuitResult,
]
]:
"""Run circuit on the cluster.
:param circuit: the QASM representation of the circuit to run
:type circuit: Circuit
:param nshots: number of shots
:param nshots: number of shots, mandatory for non-simulation devices, defaults to `nshots=100` for simulation partitions
:type nshots: int
:param device: the device to run the circuit on. Default device is `k2`
:param device: the device to run the circuit on.
:type device: str
:param wait_for_results: whether to let the client hang until server results are ready or not. Defaults to True.
:type wait_for_results: bool
:return:
the numpy array with the results of the computation. None if the job
the result of the computation. None if the job
raised an error.
:rtype: Optional[QiboJobResult]
"""
self.check_client_server_qibo_versions()

logger.info("Post new circuit on the server")
job = self._post_circuit(circuit, nshots, device)
job = self._post_circuit(circuit, device, nshots)

logger.info("Job posted on server with pid %s", self.pid)
logger.info(
Expand All @@ -102,8 +107,8 @@ def run_circuit(
def _post_circuit(
self,
circuit: qibo.Circuit,
nshots: int = 100,
device: str = "k2",
device: str,
nshots: int = None,
) -> QiboJob:
url = self.base_url + "/client/run_circuit/"

Expand Down
7 changes: 4 additions & 3 deletions tests/test_qibo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@


class FakeCircuit:

@property
def raw(self):
return "fakeCircuit"
Expand Down Expand Up @@ -123,7 +124,7 @@ def test_run_circuit_with_invalid_token(self, pass_version_check):
pass_version_check.add(responses.POST, endpoint, status=404, json=response_json)

with pytest.raises(exceptions.JobApiError) as err:
self.obj.run_circuit(FAKE_CIRCUIT, FAKE_NSHOTS, FAKE_DEVICE)
self.obj.run_circuit(FAKE_CIRCUIT, FAKE_DEVICE, FAKE_NSHOTS)

expected_message = f"[404 Error] {message}"
assert str(err.value) == expected_message
Expand All @@ -135,7 +136,7 @@ def test_run_circuit_with_job_post_error(self, pass_version_check):
pass_version_check.add(responses.POST, endpoint, status=200, json=response_json)

with pytest.raises(exceptions.JobPostServerError) as err:
self.obj.run_circuit(FAKE_CIRCUIT, FAKE_NSHOTS, FAKE_DEVICE)
self.obj.run_circuit(FAKE_CIRCUIT, FAKE_DEVICE, FAKE_NSHOTS)

assert str(err.value) == message

Expand All @@ -145,7 +146,7 @@ def test_run_circuit_with_success(self, pass_version_check, caplog):
response_json = {"pid": FAKE_PID}
pass_version_check.add(responses.POST, endpoint, status=200, json=response_json)

job = self.obj.run_circuit(FAKE_CIRCUIT, FAKE_NSHOTS, FAKE_DEVICE)
job = self.obj.run_circuit(FAKE_CIRCUIT, FAKE_DEVICE, FAKE_NSHOTS)

assert job.pid == FAKE_PID
assert job.base_url == FAKE_URL
Expand Down

0 comments on commit d72c7e4

Please sign in to comment.