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: SamplingResult

A result of a riqu sampling job.

Parameters:

result

A result of dict type. This dict should have the key counts. The value of counts 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, then properties is a mapping from the index of bit string to the index of the quantum circuit.

Raises:

ValueError – If counts or properties 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,
        },
    },
    "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 counts is “00”, “01”, and “11” respectively. The index of these 2 bits is the key of properties and the index of the quantum circuit is qubit_index. The LSB (Least Significant Bit) of the bit string representation is index=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: 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: 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 or job_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 job_type: str

The type of Job.

property status: str

The status of Job.

property created: datetime

datetime when riqu server received the new job.

property in_queue: datetime

datetime when the job is in queue.

property out_queue: datetime

datetime when the job is out queue.

property ended: 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: float | None = None, wait: float = 10.0) Job | None

Waits until the job progress to the end such as success or failure, 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, or cancelled, the job is retrieved from riqu server at intervals of wait seconds. If the job does not progress to the end after timeout seconds, raise BackendError.

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: 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 or api_token is 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 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 is default. Each section describes a setting in the format key=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 sectionA settings are to be used, initialize RiquSamplingBackend as follows

backend = RiquSamplingBackend(RiquConfig.from_file("sectionA"))
class quri_parts.riqu.backend.sampling.RiquSamplingBackend(config: RiquConfig | None = None)

Bases: 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: 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_shots is 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_shots is 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.