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:
quri_parts.backend.SamplingResult
A result of a riqu sampling job.
- Parameters
result –
A result of dict type. This dict should have the key
counts
. The value ofcounts
is 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
counts
is expressed as a bit string, thenproperties
is a mapping from the index of bit string to the index of the quantum circuit.- Raises
ValueError – If
counts
orproperties
does 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 } } }
In the above case, the bit string representation of 0, 1, and 3 in the keys of
counts
is “00”, “01”, and “11” respectively. The index of these 2 bits is the key ofproperties
and the index of the quantum circuit isqubit_index
. The LSB (Least Significant Bit) of the bit string representation isindex=0
.If the same
qubit_index
is measured multiple times in one quantum circuit,measurement_window_index
are set to 0, 1, 2, …- property counts: collections.abc.Mapping[int, int]#
Returns the dict input for the counts.
- property properties: Dict#
Returns properties.
- class quri_parts.riqu.backend.sampling.RiquSamplingJob(job: quri_parts.riqu.rest.models.job.Job, job_api: quri_parts.riqu.rest.api.job_api.JobApi)#
Bases:
quri_parts.backend.SamplingJob
A job for a riqu sampling measurement.
- Parameters
Job – A result of dict type.
job_api – A result of dict type.
- Raises
ValueError – If
job
orjob_api
is 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.datetime#
datetime
when riqu server received the new job.
- property in_queue: datetime.datetime#
datetime
when the job is in queue.
- property out_queue: datetime.datetime#
datetime
when the job is out queue.
- property ended: datetime.datetime#
datetime
when 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: Optional[float] = None, wait: float = 10.0) Optional[quri_parts.riqu.rest.models.job.Job] #
Waits until the job progress to the end such as
success
orfailure
,cancelled
.- Parameters
timeout – The number of seconds to wait for job.
wait – Time in seconds between queries.
- result(timeout: Optional[float] = None, wait: float = 10.0) quri_parts.backend.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 ofwait
seconds. If the job does not progress to the end aftertimeout
seconds, 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)#
Bases:
object
A configuration information class for using riqu backend.
- Parameters
url – Base URL for riqu server.
api_token – API token for riqu server.
- Raises
ValueError – If
url
orapi_token
is None.
- property url: str#
- property api_token: str#
- static from_file(section: str = 'default', path: str = '~/.riqu') quri_parts.riqu.backend.sampling.RiquConfig #
Reads configuration information from a file.
- Parameters
section – A
RiquConfig
for 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~/.riqu
and 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>
If
sectionA
settings are to be used, initializeRiquSamplingBackend
as followsbackend = RiquSamplingBackend(RiquConfig.from_file("sectionA"))
- class quri_parts.riqu.backend.sampling.RiquSamplingBackend(config: Optional[quri_parts.riqu.backend.sampling.RiquConfig] = None)#
Bases:
quri_parts.backend.SamplingBackend
A riqu backend for a sampling measurement.
- Parameters
config – A
RiquConfig
for circuit execution. If this parameter is None,default
section in~/.riqu
file is read.
- sample(circuit: quri_parts.circuit.circuit.NonParametricQuantumCircuit, n_shots: int, transpiler: Optional[str] = 'normal', remark: Optional[str] = None) quri_parts.backend.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_shots
is not a positive integer.BackendError – If job is wrong or if an authentication error occurred, etc.
- sample_qasm(qasm: str, n_shots: int, transpiler: Optional[str] = 'normal', remark: Optional[str] = None) quri_parts.backend.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_shots
is not a positive integer.BackendError – If job is wrong or if an authentication error occurred, etc.
- retrieve_job(job_id: str) quri_parts.riqu.backend.sampling.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.