{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "a533cde3", "metadata": {}, "source": [ "# Sampling on riqu server\n", "\n", "Overview of the quri-parts-riqu repository. [QURI Parts riqu Documentation](../index.html)\n", "\n", "This section requires topics described in QURI Parts document ([Sampling simulation](https://quri-parts.qunasys.com/tutorials/sampling_simulation.html)), so you need to read it before this section.\n", "\n", "In QURI Parts document ([Sampling simulation](https://quri-parts.qunasys.com/tutorials/sampling_simulation.html)), 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." ] }, { "attachments": {}, "cell_type": "markdown", "id": "f0ff7af6", "metadata": {}, "source": [ "## Prerequisite\n", "\n", "*riqu*, short for \"**R**EST **I**nterface for **Qu**antum Computer\", is an interface for performing quantum computers.\n", "riqu is a REST interface, and post requests of jobs include OpenQASM 3 format quantum circuits.\n", "However, riqu currently supports only a subset of OpenQASM 3, such as basic gates and measurements.\n", "*riqu server* performs quantum circuits through riqu interface as a cloud service.\n", "\n", "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.\n", "\n", "You can install `quri-parts-riqu` as follows:" ] }, { "cell_type": "code", "execution_count": null, "id": "cdf46a7a", "metadata": {}, "outputs": [], "source": [ "!pip install quri-parts-riqu" ] }, { "attachments": {}, "cell_type": "markdown", "id": "b6f5ddab", "metadata": {}, "source": [ "## Prepare ``~/.riqu`` configuration file\n", "\n", "Create a configuration file in path ``~/.riqu``.\n", "Replace ```` and ```` with your settings for riqu server and execute the following code:" ] }, { "cell_type": "code", "execution_count": 1, "id": "f1d44aaa", "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "config = \"\"\"[default]\n", "url=\n", "api_token=\n", "\"\"\"\n", "with open(os.path.expanduser(\"~/.riqu\"), \"w\") as f:\n", " f.write(config)" ] }, { "cell_type": "markdown", "id": "8aaa543b", "metadata": {}, "source": [ "## Prepare a circuit\n", "\n", "As a preparation, we create a circuit to be sampled:" ] }, { "cell_type": "code", "execution_count": 1, "id": "89fd8970", "metadata": {}, "outputs": [], "source": [ "from math import pi\n", "from quri_parts.circuit import QuantumCircuit\n", "\n", "circuit = QuantumCircuit(2)\n", "circuit.add_H_gate(0)\n", "circuit.add_CNOT_gate(0, 1)" ] }, { "attachments": {}, "cell_type": "markdown", "id": "ddbcded0", "metadata": {}, "source": [ "## SamplingBackend\n", "\n", "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. " ] }, { "cell_type": "code", "execution_count": 3, "id": "c6e85888", "metadata": {}, "outputs": [], "source": [ "from quri_parts.riqu.backend import RiquSamplingBackend\n", "\n", "# Create a SamplingBackend with the device\n", "backend = RiquSamplingBackend()" ] }, { "cell_type": "markdown", "id": "d552513b", "metadata": {}, "source": [ "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:" ] }, { "cell_type": "code", "execution_count": 4, "id": "e0e34af0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Counter({0: 507, 3: 493})\n" ] } ], "source": [ "job = backend.sample(circuit, n_shots=1000)\n", "result = job.result()\n", "print(result.counts)" ] }, { "attachments": {}, "cell_type": "markdown", "id": "99617015", "metadata": {}, "source": [ "## Sampler\n", "\n", "Instead of using the backend directly, you can create a `Sampler` from it.\n", "See the [tutorial](https://quri-parts.qunasys.com/tutorials/sampling_real.html#SamplingBackend-and-Sampler) in QURI Parts document for more details." ] }, { "attachments": {}, "cell_type": "markdown", "id": "0107c023", "metadata": {}, "source": [ "## Notes\n", "\n", "- `quri-parts-riqu` is experimental and may undergo breaking changes without notice.\n", "Use it at your own risk.\n", "- See the [API Reference](../quri_parts/riqu/quri_parts.riqu.backend.sampling.html) for more details." ] }, { "cell_type": "markdown", "id": "afd2f807", "metadata": {}, "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.6" } }, "nbformat": 4, "nbformat_minor": 5 }