Sampling on riqu server#

Overview of the quri-parts-riqu repository. QURI Parts riqu Documentation

This section requires topics described in QURI Parts document (Sampling simulation), so you need to read it before this section.

In QURI Parts document (Sampling simulation), it is described how to estimate expectation value of operators using sampling measurements on a quantum circuit simulator. Since QURI Parts is designed to be platform independent, you can execute almost the same code on a real quantum computer.

Prerequisite#

riqu, short for “REST Interface for Quantum Computer”, is an interface for performing quantum computers. riqu is a REST interface, and post requests of jobs include OpenQASM 3 format quantum circuits. However, riqu currently supports only a subset of OpenQASM 3, such as basic gates and measurements. riqu server performs quantum circuits through riqu interface as a cloud service.

We use riqu server as an example of a platform with real quantum computers. In order to use riqu, you need to sign up for riqu server. Please contact riqu server operator for information on how to sign up. In this section, instead, we perform quantum circuits on riqu server.

You can install quri-parts-riqu as follows:

[ ]:
!pip install quri-parts-riqu

Prepare ~/.riqu configuration file#

Create a configuration file in path ~/.riqu. Replace <base URL> and <API token> with your settings for riqu server and execute the following code:

[1]:
import os

config = """[default]
url=<base URL>
api_token=<API token>
"""
with open(os.path.expanduser("~/.riqu"), "w") as f:
    f.write(config)

Prepare a circuit#

As a preparation, we create a circuit to be sampled:

[1]:
from math import pi
from quri_parts.circuit import QuantumCircuit

circuit = QuantumCircuit(2)
circuit.add_H_gate(0)
circuit.add_CNOT_gate(0, 1)

SamplingBackend#

In order to use a real device, you need to create a SamplingBackend object and then a Sampler using the backend. The SamplingBackend provides a unified interface for handling various backend devices, computation jobs for the devices and results of the jobs.

[3]:
from quri_parts.riqu.backend import RiquSamplingBackend

# Create a SamplingBackend with the device
backend = RiquSamplingBackend()

It is possible to use this backend directly, though it is usually unnecessary as we will see below. The SamplingBackend has sample() method, which returns a SamplingJob object, and you can extract a result of the sampling job:

[4]:
job = backend.sample(circuit, n_shots=1000)
result = job.result()
print(result.counts)
Counter({0: 507, 3: 493})

Sampler#

Instead of using the backend directly, you can create a Sampler from it. See the tutorial in QURI Parts document for more details.

Notes#

  • quri-parts-riqu is experimental and may undergo breaking changes without notice. Use it at your own risk.

  • See the API Reference for more details.