quri_parts.riqu.backend.sampling module¶
A module to perform sampling on riqu server.
Before sampling, sign up for riqu server and create a configuration file in
path ~/.riqu. See the description of RiquConfig.from_file() method
for how to write ~/.riqu file.
Examples
To perform sampling 1000 shots on riqu server, run the following code:
from quri_parts.circuit import QuantumCircuit
from quri_parts.riqu.backend import RiquSamplingBackend
circuit = QuantumCircuit(2)
circuit.add_H_gate(0)
circuit.add_CNOT_gate(0, 1)
backend = RiquSamplingBackend()
job = backend.sample(circuit, n_shots=1000)
counts = job.result().counts
print(counts)
To perform with the transpiler setting on riqu server, run the following code:
from quri_parts.circuit import QuantumCircuit
from quri_parts.riqu.backend import RiquSamplingBackend
circuit = QuantumCircuit(2)
circuit.add_H_gate(0)
circuit.add_CNOT_gate(0, 1)
backend = RiquSamplingBackend()
job = backend.sample(circuit, n_shots=10000, transpiler="normal")
counts = job.result().counts
print(counts)
The specifications of the transpiler setting is as follows:
"none": no transpiler"pass": use the “do nothing transpiler” (same as"none")"normal": use default transpiler (by default)
You can also input OpenQASM 3.0 program.
from quri_parts.circuit import QuantumCircuit
from quri_parts.riqu.backend import RiquSamplingBackend
qasm = """OPENQASM 3;
include "stdgates.inc";
qubit[2] q;
h q[0];
cx q[0], q[1];"""
backend = RiquSamplingBackend()
job = backend.sample_qasm(qasm, n_shots=1000)
counts = job.result().counts
print(counts)
To retrieve jobs already sent to riqu server, run the following code:
from quri_parts.riqu.backend import RiquSamplingBackend
job = backend.retrieve_job("<put target job id>")
counts = job.result().counts
print(counts)
- class quri_parts.riqu.backend.sampling.RiquSamplingResult(result: dict[str, Any])¶
Bases:
SamplingResultA result of a riqu sampling job.
- Parameters:
result –
A result of dict type. This dict should have the key
counts. The value ofcountsis the dict input for the counts. Where the keys represent a measured classical value and the value is an integer the number of shots with that result.If the keys of
countsis expressed as a bit string, thenpropertiesis a mapping from the index of bit string to the index of the quantum circuit.- Raises:
ValueError – If
countsorpropertiesdoes not exist in result.
Examples
An example of a dict of result is as below:
{ "counts": { 0: 600, 1: 300, 3: 100, }, "properties": { 0: { "qubit_index": 0, "measurement_window_index": 0, }, 1: { "qubit_index": 1, "measurement_window_index": 0, }, }, "transpiler_info": { "physical_virtual_mapping": { "0": 1, "1": 0, }, }, "message": "SUCCESS!", }
In the above case, the bit string representation of 0, 1, and 3 in the keys of
countsis “00”, “01”, and “11” respectively. The index of these 2 bits is the key ofpropertiesand the index of the quantum circuit isqubit_index. The LSB (Least Significant Bit) of the bit string representation isindex=0.If the same
qubit_indexis measured multiple times in one quantum circuit,measurement_window_indexare set to 0, 1, 2, …- property counts: Mapping[int, float | int]¶
Returns the dict input for the counts.
- property properties: dict¶
Returns properties.
- property transpiler_info: dict¶
Returns transpiler_info.
- property message: str¶
Returns message.
- property divided_result: Dict¶
Returns divided_result.
- class quri_parts.riqu.backend.sampling.RiquSamplingJob(job: Job, job_api: JobApi)¶
Bases:
SamplingJobA job for a riqu sampling measurement.
- Parameters:
Job – A result of dict type.
job_api – A result of dict type.
- Raises:
ValueError – If
joborjob_apiis None.
- property id: str¶
The id of the job.
- property qasm: str¶
The circuit converted to OpenQASM 3.0 format.
- property transpiled_qasm: str¶
The circuit in OpenQASM 3.0 format transpiled by riqu server.
- property transpiler: str¶
The transpiler setting.
- property shots: int¶
Number of repetitions of each circuit, for sampling.
- property status: str¶
The status of Job.
- property created: datetime¶
datetimewhen riqu server received the new job.
- property in_queue: datetime¶
datetimewhen the job is in queue.
- property out_queue: datetime¶
datetimewhen the job is out queue.
- property ended: datetime¶
datetimewhen the job is ended.
- property remark: str¶
The remark to be assigned to the job.
- refresh() None¶
Retrieves the latest job information from riqu server.
- wait_for_completion(timeout: float | None = None, wait: float = 10.0) Job | None¶
Waits until the job progress to the end such as
successorfailure,cancelled.- Parameters:
timeout – The number of seconds to wait for job.
wait – Time in seconds between queries.
- result(timeout: float | None = None, wait: float | None = 10.0) SamplingResult¶
Waits until the job progress to the end and returns the result of the job.
If the status of job is not
success,failure, orcancelled, the job is retrieved from riqu server at intervals ofwaitseconds. If the job does not progress to the end aftertimeoutseconds, raiseBackendError.- Parameters:
timeout – The number of seconds to wait for job.
wait – Time in seconds between queries.
- Raises:
BackendError – If job cannot be found or if an authentication error occurred or timeout occurs, etc.
- cancel() None¶
Cancels the job.
If the job statuses are success, failure, or cancelled, then cannot be cancelled and an error occurs.
- Raises:
BackendError – If job cannot be found or if an authentication error occurred or if job cannot be cancelled, etc.
- class quri_parts.riqu.backend.sampling.RiquConfig(url: str, api_token: str, proxy: str | None = None)¶
Bases:
objectA configuration information class for using riqu backend.
- Parameters:
url – Base URL for riqu server.
api_token – API token for riqu server.
- Raises:
ValueError – If
urlorapi_tokenis None.
- property url: str¶
- property api_token: str¶
- property proxy: str | None¶
- static from_file(section: str | None = 'default', path: str | None = '~/.riqu') RiquConfig¶
Reads configuration information from a file.
- Parameters:
section – A
RiquConfigfor circuit execution.path – A path for config file.
- Returns:
Configuration information
RiquConfig.
Examples
The riqu configuration file describes configuration information for each section. A section has a header in the form
[section]. The default file path is~/.riquand the default section name isdefault. Each section describes a setting in the formatkey=value. An example of a configuration file description is as below:[default] url=<base URL> api_token=<API token> [sectionA] url=<base URL> api_token=<API token> [sectioB] url=<base URL> api_token=<API token> proxy=http://<proxy>:<port>
If
sectionAsettings are to be used, initializeRiquSamplingBackendas followsbackend = RiquSamplingBackend(RiquConfig.from_file("sectionA"))
- class quri_parts.riqu.backend.sampling.RiquSamplingBackend(config: RiquConfig | None = None)¶
Bases:
SamplingBackendA riqu backend for a sampling measurement.
- Parameters:
config – A
RiquConfigfor circuit execution. If this parameter is None,defaultsection in~/.riqufile is read.
- sample(circuit: NonParametricQuantumCircuit | list[NonParametricQuantumCircuit], n_shots: int, transpiler: str | None = 'normal', remark: str | None = None) SamplingJob¶
Perform a sampling measurement of a circuit.
The circuit is transpiled on riqu server. The QURI Parts transpiling feature is not supported. The circuit is converted to OpenQASM 3.0 format and sent to riqu server.
- Parameters:
circuit – The circuit to be sampled.
n_shots – Number of repetitions of each circuit, for sampling.
transpiler – The transpiler setting.
remark – The remark to be assigned to the job.
- Returns:
The job to be executed.
- Raises:
ValueError – If
n_shotsis not a positive integer.BackendError – If job is wrong or if an authentication error occurred, etc.
- sample_qasm(qasm: str | list[str], n_shots: int, transpiler: str | None = 'normal', remark: str | None = None, job_type: str | None = None) SamplingJob¶
Perform a sampling measurement of a OpenQASM 3.0 program.
The OpenQASM 3.0 program is transpiled on riqu server. The QURI Parts transpiling feature is not supported.
- Parameters:
qasm – The OpenQASM 3.0 program to be sampled.
n_shots – Number of repetitions of each circuit, for sampling.
transpiler – The transpiler setting.
remark – The remark to be assigned to the job.
- Returns:
The job to be executed.
- Raises:
ValueError – If
n_shotsis not a positive integer.BackendError – If job is wrong or if an authentication error occurred, etc.
- retrieve_job(job_id: str) RiquSamplingJob¶
Retrieves the job with the given id from riqu server.
- Parameters:
job_id – The id of the job to retrieve.
- Returns:
The job with the given
job_id.- Raises:
BackendError – If job cannot be found or if an authentication error occurred, etc.