diff --git a/README.md b/README.md index c9783b4..ef34d34 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,10 @@ To run the sample example with SQC, you also need to set the library options to SQC_LIBS="-lsqc_api -lsqc_rpc ... (omitted) ... -pthread" ``` +### Tutorials + +See [tutorial](tutorials/readme.md) for more examples. + ### Making your own interface to Quantum hardware Qiskit C++ offers an abstract interface to access Quantum hardware. You can make your own interface to the hardware diff --git a/tutorials/circuit.ipynb b/tutorials/circuit.ipynb new file mode 100644 index 0000000..f38b9ac --- /dev/null +++ b/tutorials/circuit.ipynb @@ -0,0 +1,240 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "afafa430-5f99-4d27-93bb-be3751de7dc5", + "metadata": {}, + "source": [ + "# Making Quantum Circuits\n", + "This tutorial is the first step for Qiskit C++ to make a new quantum circuit.\n", + "\n", + "## Setting for Notebook\n", + "Before starting the tutorial, make sure you already install `Qiskit C-API`, `QRMI` and `xeus-cling` (see readme.md).\n", + "To write C++ application on the notebook, the path to `Qiskit` should be set by using following `#pragma` for `xeus-cling`\n", + "\n", + "It is easy to run this tutorial by putting all components in the same directory level as:\n", + "```\n", + "doichan@doichanT14:/mnt/c/dev/Quantum/qiskit-cpp/tutorial$ ls -l\n", + "total 0\n", + "drwxr-xr-x 1 doichan doichan 4096 Jan 23 17:35 qiskit\n", + "drwxr-xr-x 1 doichan doichan 4096 Jan 23 17:38 qiskit-cpp\n", + "drwxr-xr-x 1 doichan doichan 4096 Jan 22 18:25 qrmi\n", + "doichan@doichanT14:/mnt/c/dev/Quantum/qiskit-cpp/tutorial$\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "41f8e2db-7a40-4650-b2b7-76e90990b8a6", + "metadata": {}, + "outputs": [], + "source": [ + "// setting include path to Qiskit C-API, set your own path here\n", + "#pragma cling add_include_path(\"../../qiskit/dist/c/include\")\n", + "// setting library path to Qiskit C-API, set your own path here\n", + "#pragma cling add_library_path(\"../../qiskit/dist/c/lib\")\n", + "// loading Qiskit\n", + "#pragma cling load(\"qiskit\")\n", + "\n", + "// then set include path for Qiskit C++\n", + "#pragma cling add_include_path(\"../src\")" + ] + }, + { + "cell_type": "markdown", + "id": "8c47e2bf-361e-4a43-8daf-ff710a33f279", + "metadata": {}, + "source": [ + "## Make the first Quantum Circuit\n", + "To make a quantum circuit with Qiskit C++, include following header file. " + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "f712771a-c9aa-4db0-9163-0baa5b5cfc76", + "metadata": {}, + "outputs": [], + "source": [ + "#include \"circuit/quantumcircuit.hpp\"" + ] + }, + { + "cell_type": "markdown", + "id": "aa3e19b6-85fc-4fff-b295-63895bc8f850", + "metadata": {}, + "source": [ + "Then you can make a quantum circuit by Qiskit C++. This example will make a quantum circuit with 2 qubits and 2 classical bits." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "5dc30268-ebd8-4fa6-a3d4-aef7faef5c98", + "metadata": {}, + "outputs": [], + "source": [ + "// by using namespace, you can write as similar to Qiskit Python\n", + "using namespace Qiskit::circuit;\n", + "\n", + "// new quantum circuit with 2 qubits and 2 classical bits\n", + "QuantumCircuit circ(2, 2);\n", + "circ.h(0);\n", + "circ.z(1);\n", + "circ.measure(0, 0);\n", + "circ.measure(1, 1);" + ] + }, + { + "cell_type": "markdown", + "id": "599fadb0-efc8-4409-984a-01cabc6d24b2", + "metadata": {}, + "source": [ + "You can print out the list of gates on the quantum circuit by `print` function." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "5d8d2bf0-c249-4cfa-a86d-4cf8558a1a5f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "h(0) \n", + "z(1) \n", + "measure(0) (0) \n", + "measure(1) (1) \n" + ] + } + ], + "source": [ + "circ.print();" + ] + }, + { + "cell_type": "markdown", + "id": "ff76952a-081f-440c-a5a9-24c4ecda7e7d", + "metadata": {}, + "source": [ + "## Using Registers\n", + "`QuantumRegister` and `ClassicalRegister` classes are used to name the registers. The qubits and classical bits can be grouped into some register classes and can be used to identify instead of using global bits numbers. It is usefull for grouping classical bits for measurements.\n", + "\n", + "This example makes a quantum circuit from `QuantumRegister` and `ClassicalRegister` classes." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "12b8b710-792c-4359-afaa-09b360689b51", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "measure(0) (2) \n", + "measure(1) (3) \n", + "h(0) \n", + "x(1) \n", + "measure(0) (0) \n", + "measure(1) (1) \n" + ] + } + ], + "source": [ + "using namespace Qiskit::circuit;\n", + "\n", + "// new quantum register with 2 qubits\n", + "auto qreg = QuantumRegister(2);\n", + "// new classical register with 2 bits named as measure\n", + "auto creg = ClassicalRegister(2, std::string(\"measure\"));\n", + "// new classical register with 2 bits named as test\n", + "auto ctest = ClassicalRegister(2, std::string(\"test\"));\n", + "// make a new quantum circuit with registers\n", + "QuantumCircuit circ_with_reg(std::vector({qreg,}), std::vector({creg, ctest}));\n", + "\n", + "circ_with_reg.measure(qreg, ctest);\n", + "circ_with_reg.h(qreg[0]);\n", + "circ_with_reg.x(qreg[1]);\n", + "circ_with_reg.measure(qreg, creg);\n", + "\n", + "circ_with_reg.print();" + ] + }, + { + "cell_type": "markdown", + "id": "76b04d75-fa03-4e9b-b1fa-86aa135b48ef", + "metadata": {}, + "source": [ + "Also you can print out the quantum circuit as QASM3 format." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "28fcc7ad-a2e1-468b-b412-0ce7361c2bc8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "OPENQASM 3.0;\n", + "include \"stdgates.inc\";\n", + "qubit[2] q;\n", + "bit[2] measure;\n", + "bit[2] test;\n", + "test[0] = measure q[0];\n", + "test[1] = measure q[1];\n", + "h q[0];\n", + "x q[1];\n", + "measure[0] = measure q[0];\n", + "measure[1] = measure q[1];\n", + "\n" + ] + } + ], + "source": [ + "std::cout << circ_with_reg.to_qasm3() << std::endl;" + ] + }, + { + "cell_type": "markdown", + "id": "225680c9-721e-4bd4-8b5b-bf4f5b78d799", + "metadata": {}, + "source": [ + "***\n", + "Next tutorial : \n", + "[Running Quantum Circuits with Qiskit C++](run_circuits.ipynb)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f6ded246-8801-465e-8033-a10de2543adc", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "C++11", + "language": "C++11", + "name": "xcpp11" + }, + "language_info": { + "codemirror_mode": "text/x-c++src", + "file_extension": ".cpp", + "mimetype": "text/x-c++src", + "name": "c++", + "version": "11" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/tutorials/hgz_on_iqp.png b/tutorials/hgz_on_iqp.png new file mode 100644 index 0000000..084b6d9 Binary files /dev/null and b/tutorials/hgz_on_iqp.png differ diff --git a/tutorials/python_vs_cpp.png b/tutorials/python_vs_cpp.png new file mode 100644 index 0000000..35e0210 Binary files /dev/null and b/tutorials/python_vs_cpp.png differ diff --git a/tutorials/qiskit-cpp-structure.png b/tutorials/qiskit-cpp-structure.png new file mode 100644 index 0000000..f222cf9 Binary files /dev/null and b/tutorials/qiskit-cpp-structure.png differ diff --git a/tutorials/readme.md b/tutorials/readme.md new file mode 100644 index 0000000..a5dc533 --- /dev/null +++ b/tutorials/readme.md @@ -0,0 +1,93 @@ +# Qiskit C++ Tutorial + +## Qiskit C++ Overview +Qiskit C++ is an opensource interface library for Quantum Centric Super Computing applications. +Qiskit C++ provides similar interface classes to Python interface of Qiskit through Qiskit C-API, can be used from C++ applications. +You do not have to bind your applications to Python to make and run the quantum circuits. +Qiskit C++ is easy to use library, consists of header files only, so you do not have to build any binaries and install to the system, but you only have to include Qiskit C++ header files from your applicaitons. + + +Qiskit C++ supports: + +- Building quantum circuits using standard gates +- Transpiling quantum circuits to ISA circuits for a target quantum hardware +- Running quantum circuits through sampler interface on a quantum hardware + + +![alt text](python_vs_cpp.png) +This example shows building GHZ circuit in Qiskit (Python) and Qiksit C++. +As you can see, only the difference between Python and C++ is type definitions and end of line semicorons in C++. +Most of classes and functions implemented in Qiskit C++ are as same as that of Qiskit Python. +So it is easy to port Qiskit codes into your C++ applications using Qiskit C++. + + +![alt C-API and Qiskit C++ interface structure](qiskit-cpp-structure.png) +This chart shows how Qiskit C++ is implemented by using Qiskit C-API. Qiskit C++ accesses Qiskit's core functions written in Rust through C-API. +To access quantum resources, we use one of `QRMI` https://github.com/qiskit-community/qrmi, `Qiskit IBM Runtime C` https://github.com/Qiskit/qiskit-ibm-runtime-c + or `SQC` https://github.com/jhpc-quantum/SQC (for `ibm_kobe`) through runtime class of Qiskit C++. + +## Getting started + +Before coding with Qiskit C++, install dependencies. + +### Bulding Qiskit C-API Library + +``` +git clone git@github.com:Qiskit/qiskit.git +cd qiskit +make c +``` +To build Qiskit C-API library, you will need Rust compiler. +See also https://github.com/Qiskit/qiskit/tree/main/crates/cext + +Make sure you will need the path to the Qiskit later to include and link from Qiskit C++. + +### Building Runtime Service Interface Library + +To access quantum hardware from Qiskit C++, install one of the runtime service API. +This example installs `QRMI` as a runtiem service API. + +``` +git clone git@github.com:qiskit-community/qrmi.git +cd qrmi +cargo build --release +``` +Make sure you will need the path to the QRMI later to include and link from Qiskit C++. + +### Other Software + +Qiskit C++ uses nlohmann_json https://github.com/nlohmann/json to read/write JSON format. +Install and set include path so that Qiskit C++ can find it. + +### Building Sample of Qiskit C++ + +Now you can use Qiskit C++ from the applications. This example builds examples of Qiskit C++. + +``` +cd samples +mkdir build +cd build +cmake -DQISKIT_ROOT=(put a path to the root of Qiskit you cloned) -DQRMI_ROOT=(put a path to the root of QRMI you cloned) .. +make +``` +Set `QISKIT_ROOT` and `QRMI_ROOT` to cmake to set include path and library path referred from Qiskit C++. + +In your applications, set include path and library path to compiler options: +``` +-I${QISKIT_ROOT}/dist/c/include -I${QRMI}/ +-L${QISKIT_ROOT}/dist/c/lib -L${QRMI}/target/Release +``` + +See [CMakeLists.txt](../samples/CMakeLists.txt) in samples. + +## Getting Ready for Qiskit C++ Tutorial + +Qiskit C++ tutorial uses Jupyter notebook to learn interactively. Before go into the tutorial, install Qiskit C-API and QRMI shown above, and install `xeus-cling` https://github.com/jupyter-xeus/xeus-cling to run C++ code interactively on Jupyter notebook. + +*** +## Tutorials + +1. [Making Quantum Circuits](circuit.ipynb) +2. [Runnning Quantum Circuits](run_circuit.ipynb) + + diff --git a/tutorials/run_circuit.ipynb b/tutorials/run_circuit.ipynb new file mode 100644 index 0000000..e26f092 --- /dev/null +++ b/tutorials/run_circuit.ipynb @@ -0,0 +1,404 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "268f8630-8d94-496f-9060-886c36ba220e", + "metadata": {}, + "source": [ + "# Running Quantum Circuits with Qiskit C++\n", + "This tutorial introduces how to run quantum circuits through sampler interface with Qiskit C++.\n", + "To run the quantum circuits with Qiskit C++, make sure you need an [IBM Quantum Platform](https://quantum.cloud.ibm.com/) (IQP) account.\n" + ] + }, + { + "cell_type": "markdown", + "id": "cc64827d-2560-4d99-b63e-6f196d356238", + "metadata": {}, + "source": [ + "## Setting for This Tutorial\n", + "In addition to the previous tutorial, the path to `QRMI` is also needed to be set in `#pragma` for `xeus-cling`." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "27789d3c-81ff-4e8c-a775-d18843566a52", + "metadata": {}, + "outputs": [], + "source": [ + "// setting include path to Qiskit C-API, set your own path here\n", + "#pragma cling add_include_path(\"../../qiskit/dist/c/include\")\n", + "// setting library path to Qiskit C-API, set your own path here\n", + "#pragma cling add_library_path(\"../../qiskit/dist/c/lib\")\n", + "// loading Qiskit\n", + "#pragma cling load(\"qiskit\")\n", + "\n", + "// setting include path to QRMI, set your own path here\n", + "#pragma cling add_include_path(\"../../qrmi\")\n", + "// setting library path to QRMI, set your own path here\n", + "#pragma cling add_library_path(\"../../qrmi/target/release\")\n", + "// loading QRMI\n", + "#pragma cling load(\"qrmi\")\n", + "\n", + "// then set include path for Qiskit C++\n", + "#pragma cling add_include_path(\"../src\")" + ] + }, + { + "cell_type": "markdown", + "id": "98cc6a5c-12da-4838-bc90-f3be1a5f8884", + "metadata": {}, + "source": [ + "## Setting Your IQP Account\n", + "Qiskit C++ accepts IQP account settings as similar to [Qiskit IBM Runtime](https://github.com/Qiskit/qiskit-ibm-runtime). You will need to set your `API key` and `Instance CRN` with one of the followings.\n", + "\n", + "### Setting in Environment Variables\n", + "You can set your account information in environment variables as:\n", + "```\n", + "export QISKIT_IBM_TOKEN=\"Your API key\"\n", + "export QISKIT_IBM_INSTANCE=\"Your CRN\"\n", + "```\n", + "\n", + "### Using Saved Account Informations\n", + "If you have previously used Qiskit IBM Runtime client, you already have saved account information in `$HOME/.qiskit/qiskit-ibm.json` see [here](https://github.com/Qiskit/qiskit-ibm-runtime?tab=readme-ov-file#save-your-account-on-disk) to save your account in the file.\n", + "\n", + "\n", + "```\n", + "{\n", + " \"default-ibm-quantum-platform\": {\n", + " \"channel\": \"ibm_quantum_platform\",\n", + " \"instance\": \"Your CRN\",\n", + " \"token\": \"Your API key\",\n", + " \"url\": \"https://cloud.ibm.com\"\n", + " }\n", + "}\n", + "```\n", + "\n", + "### Pass by Parameters\n", + "Or you can pass `API key` and `CRN` to the parameters to initialize `QiskitRuntimeService` class of Qiskit C++.\n", + "```\n", + "#include \"service/qiskit_runtime_service qrmi.hpp\"\n", + "auto service = Qiskit::service::QiskitRuntimeService(\"Your API key here\", \"Your CRN here\");\n", + "```" + ] + }, + { + "cell_type": "markdown", + "id": "54c7bf0f-7723-4886-8394-3378641ed051", + "metadata": {}, + "source": [ + "## Making a GHZ Circuit\n", + "In this tutorial, we use a simple circuit to create [GHZ state](https://en.wikipedia.org/wiki/Greenberger%E2%80%93Horne%E2%80%93Zeilinger_state)." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "cf3d6a6a-c8ae-45be-84c0-55248bf931ce", + "metadata": {}, + "outputs": [], + "source": [ + "#include \"circuit/quantumcircuit.hpp\"\n", + "using namespace Qiskit::circuit;\n", + "\n", + "int num_qubits = 10;\n", + "auto qreg = QuantumRegister(num_qubits);\n", + "auto creg = ClassicalRegister(num_qubits);\n", + "QuantumCircuit circ(std::vector({qreg,}), std::vector({creg, }));\n", + "\n", + "circ.h(0);\n", + "for (int i = 1; i < num_qubits; i++) {\n", + " circ.cx(0, i);\n", + "}\n", + "circ.measure(qreg, creg);" + ] + }, + { + "cell_type": "markdown", + "id": "9b6b838f-74bc-46bf-a1e8-7b8ebdb99ed0", + "metadata": {}, + "source": [ + "## Using Runtime Service and Transpiling a Quantum Circuit\n", + "To run the quantum circuit on the quantum device, the input circuit should be transpiled for the target backend device, because supported quantum gates are depending on the target device and quantum gates should be mapped to the topology of qubits on the target device.\n", + "\n", + "A `QiskitRuntimeService` class provides interface to the target backend device to transpile and run the quantum circuits. `QiskitRuntimeService` class can be accessed by including one of the runtime interface header file `qiskit_runtime_service_qrmi.hpp` or `qiskit_runtime_service_c.hpp` or `qiskit_runtime_service_sqc.hpp`. (We use `qiskit_runtime_service_qrmi.hpp` for this tutorial)\n", + "\n", + "We can get backend by the name of the backend device, you can find the name of backends you can access on the IQP dashboard." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4594448b-a636-41ed-99a0-f895e6067a8b", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "QRMI connecting : ibm_fez\n" + ] + } + ], + "source": [ + "#include \"service/qiskit_runtime_service qrmi.hpp\"\n", + "\n", + "using namespace Qiskit::service;\n", + "auto service = QiskitRuntimeService();\n", + "// get backend by name, set your backend name here\n", + "auto backend = service.backend(\"ibm_fez\");" + ] + }, + { + "cell_type": "markdown", + "id": "f2b3706e-83f1-4c1b-b3e9-f78ca2e9a0a4", + "metadata": {}, + "source": [ + "Then GHZ circuit is transpiled for the target device." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5d40e4b5-00f1-4b31-97e2-8e31f0abf8f1", + "metadata": {}, + "outputs": [], + "source": [ + "#include \"compiler/transpiler.hpp\"\n", + "using namespace Qiskit::compiler;\n", + "auto transpiled_circ = transpile(circ, backend);" + ] + }, + { + "cell_type": "markdown", + "id": "7be68415-8603-4f7e-8a9c-0e95f77d9b89", + "metadata": {}, + "source": [ + "## Running GHZ Circuit with Sampler Interface\n", + "\n", + "Pass the transpiled circuit to `run` function in the `SamplerPub` format to run the quantum circuit.\n", + "`run` function immediately return the `Job` class so you can asynchronously run the quantum circuit, so you can do other computation on the classical computer while waiting for the job will be finished." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6642ca30-6ff8-4a7d-a204-96f22991c557", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + " QRMI Job submitted to ibm_fez, JOB ID = d4tmfusgk3fc73at2tkg\n" + ] + } + ], + "source": [ + "#include \"primitives/backend_sampler_v2.hpp\"\n", + "using namespace Qiskit::primitives;\n", + "using Sampler = BackendSamplerV2;\n", + "// initialize sampler interface with target backend and default number of shots\n", + "auto sampler = Sampler(backend, 1000);\n", + "// run transpiled circuit\n", + "auto job = sampler.run({SamplerPub(transpiled_circ)});\n", + "// you can do something here while job will be finished" + ] + }, + { + "cell_type": "markdown", + "id": "8770bfb7-da97-47c6-8985-95031c9cca53", + "metadata": {}, + "source": [ + "On the IQP dashboard, submitted quantum circuit can be seen like this.\n", + "![Submitted Quantum Circuit on IQP dash board](hgz_on_iqp.png)" + ] + }, + { + "cell_type": "markdown", + "id": "3dd7dd63-1660-42be-853f-ee99d3857d07", + "metadata": {}, + "source": [ + "## Get the Sampler Result\n", + "\n", + "`result()` function of `Job` class is a blocking function that waits until the job will be finished. It will return the sampler result when the job will be finished. Or you can also check the status of the job by using `status()` function." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ce2beb13-b544-4b6e-9927-1af32d9ebad3", + "metadata": {}, + "outputs": [], + "source": [ + "// waiting for the job will be finished and get result\n", + "auto result = job->result();" + ] + }, + { + "cell_type": "markdown", + "id": "e9e5b57b-fe43-4503-9fc6-bccfb9658fdb", + "metadata": {}, + "source": [ + "`PrimitiveResult` class returned from `Job` contains sampling results for all pubs (quantum circuits). We can access by using `[]` operator to get `SamplerPubResult` for each pub. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "928f50be-dfab-4747-b092-d6fe0075c2e3", + "metadata": {}, + "outputs": [], + "source": [ + "// get result for the first pub (only 1 pub in this example)\n", + "auto pub_result = result[0];" + ] + }, + { + "cell_type": "markdown", + "id": "79a72ace-da8e-4685-a087-fb18466be984", + "metadata": {}, + "source": [ + "`SamplerPubResult` class has sampled bit strings. `SamplerPubResult::data` function returns bit strings measured on the classical register in `BitArray` class. `BitArray` contains a list of bit strings and `BitArray::get_bitstrings` function returns `std::vector` formatted bit strings. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "14731b90-4292-49d1-98bf-264e3d5f836d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " ===== samples for pub[0] =====\n", + "0100000000, 0001111111, 1111111111, 1111111111, 1111111111, 0000000000, 0000000000, 1111111111, 1111111111, 0000000000, 0000000000, 0000000000, 0000000000, 0000000000, 0000000000, 1111111111, 1111111111, 1111111111, 0000000000, 0000000000, 1111111111, 0000000000, 1111111111, 1100000000, 0000000000, 1111111111, 1111111111, 1111111111, 0000000000, 0000000000, 1111111110, 1111111111, 1111111111, 0000000000, 0000000000, 1111111111, 1111111111, 0000000000, 1111111111, 0111111111, 0000001000, 0000000000, 0000000000, 1111111111, 0000001000, 0000001000, 1111111110, 1111011111, 0000000000, 1111111111, 0000000000, 0000000000, 1111111111, 1111111111, 0000000000, 1100000000, 1000000000, 1111111111, 0000000000, 0000000000, 1111111111, 1111111111, 1000000000, 0000000000, 1111111111, 1111111111, 0000000001, 1111111111, 0000000010, 0000000000, 1111111111, 1111111111, 1111111111, 0000000000, 0000000000, 1110111111, 0000000000, 1111111110, 0000000000, 0000000000, 1111111111, 0000000000, 0000000000, 1111111111, 1111111111, 0000000000, 1111111000, 0000000000, 0000000000, 0000000000, 0000000000, 0000000000, 0000000000, 0000000000, 0000000000, 0000000000, 0000000000, 0000000000, 1111111111, 1111111111, 0000000000, 0000000000, 1111111111, 0000000000, 1111111111, 1111111111, 0111111111, 1110111111, 0000000000, 0000000000, 1111111111, 1111111111, 1000000000, 1111111111, 1111111111, 1111111111, 1111111111, 0000000000, 1111111111, 1111111111, 1111111111, 1111111111, 1111111111, 1111111111, 0000000000, 0000000000, 0000000000, 1111111111, 0000000000, 1111111111, 0000000000, 0000000000, 1111111111, 0000000000, 0000000000, 1111111111, 1111111111, 1111111111, 1111111111, 0000000000, 0000000000, 0000000000, 0000000000, 0000000000, 0000011111, 0000000000, 1111111111, 1111111111, 1111111111, 0111111111, 0000000000, 0000000011, 1111111111, 0000000000, 0000000000, 1111111111, 0000000000, 0000000000, 0000000000, 1111111111, 1111111111, 1111111111, 1111111111, 1111111111, 1111111111, 1100000000, 1111111111, 0000000000, 0010000001, 1111111111, 0000000000, 1111111111, 1111011111, 0111111111, 1111111111, 0000000000, 1111111111, 1111111111, 1110000000, 1111111111, 0000000000, 1111111111, 0011111101, 1111111111, 1111111111, 0000000000, 0000000000, 1111111111, 1111111111, 0000000000, 1111111111, 1111111111, 1111111111, 1111111111, 0000000000, 0000000000, 0000000000, 1111111000, 0000000000, 1111101111, 0000000110, 1111111000, 0000000000, 1111111111, 0000000000, 1111111111, 0000000000, 0000000000, 1111111111, 0000000000, 1111111111, 0111111110, 0000000000, 1111111111, 1111111111, 0000000000, 1111111111, 1111111111, 1111111111, 0000000000, 0000000000, 1000000000, 1111111111, 1111111111, 1000000000, 1111101111, 1111111111, 1111111111, 0111111111, 0000000000, 0000000001, 1111111111, 0000000000, 1111111111, 1111111111, 0000000000, 0000000000, 1000000000, 1111111111, 0000000000, 0000000000, 1111111111, 0000000000, 1111111111, 0000000000, 1111111111, 0000000000, 1111111111, 0000000000, 1111111111, 1111111111, 1111111111, 1111111111, 1111111111, 0000000111, 0000000000, 0000000000, 1110111111, 1111111111, 1111111111, 0000000000, 1111111111, 1111111111, 0111111111, 0000000000, 0000000000, 1111111111, 0000000000, 1000000000, 0000000000, 0000000000, 1111111111, 1111111111, 1111111010, 1111111111, 1111111101, 1111111111, 0000000000, 0111111111, 0000000000, 0000000000, 0000000000, 1111111111, 0000000000, 1111111111, 1111111111, 1111111111, 0111111111, 0000000000, 0000000000, 1111111111, 1111111111, 1111111111, 0000000000, 0000000000, 1111111111, 1111111111, 1111111111, 1111111111, 1111110111, 1111111111, 1101111111, 0000000000, 1111111111, 0000000000, 1111111111, 0000000000, 1111111101, 1111111111, 0000000000, 1111111101, 1111111111, 0000000000, 0000000000, 1111111111, 1111111111, 0000000000, 1111111111, 1111111111, 1111111111, 0000000000, 1111111111, 1111111111, 0000000000, 0000000000, 1111111111, 0000000000, 0000000000, 1110111111, 1111111111, 0000000000, 0000000011, 1111111100, 0000000000, 1111111111, 1111111111, 1111111111, 0000000000, 1111111111, 1010000000, 1000000000, 1111111111, 1111111111, 1111111111, 1111111111, 0000000000, 1111111111, 1111111111, 0000000000, 0000000000, 0000000000, 1111111111, 0000000000, 1111101111, 1100000000, 1111111111, 0000000000, 0000000000, 0000000000, 1111111111, 0000000000, 1111111111, 1111111111, 1101110111, 0000000000, 0000000000, 1111111111, 0000000000, 1111111111, 0000000000, 0000000000, 0000000000, 1111111111, 0000000000, 0000000000, 0000000000, 0111111111, 0000000000, 1111111111, 1111111110, 0000000000, 1111111111, 1111111111, 0000000000, 0000000000, 1111111111, 0000000000, 1111111111, 1111111111, 0000000000, 0000000000, 0000000000, 0000000000, 0000000000, 0000000000, 1111111111, 0000000000, 1111111111, 0000000000, 0000000000, 0000000000, 1111111111, 0000000000, 1111111111, 1111111111, 0000000000, 0000000000, 0000000000, 1111111111, 0000000000, 0000000000, 1111111111, 0010000000, 0000000000, 1111111110, 0000000000, 1111111000, 1111111110, 1111111111, 0000000000, 0000000000, 1111111111, 1111111111, 1111111111, 0000000000, 0000000000, 1111111111, 1111111111, 1111111111, 1111111111, 1111111111, 1111111111, 0000000000, 0000000000, 0000000000, 0000000000, 1111111111, 0000000100, 1111111111, 1111111111, 0000000000, 1111111111, 1111111111, 0000000000, 0000000000, 0000000000, 1111111011, 1111100000, 0000011111, 1111111111, 0000000000, 0000000000, 0000000000, 1111101111, 0000000000, 0000000000, 0000000000, 0000000000, 1111111111, 1111111111, 1100000000, 0000000000, 1111111111, 1111111111, 1011111111, 1111110111, 1111111111, 1111111011, 0000000001, 1111111111, 1111111111, 1111101111, 0000000000, 1111111111, 0000000000, 0000000000, 1111111111, 1111111111, 0000000000, 0000000000, 1111101111, 0000000000, 1111111111, 0000000000, 0000000000, 1111111111, 1111111111, 1111111111, 0000000000, 1111111111, 1111111110, 0000000000, 1111111111, 1111111111, 1000000000, 1111111111, 0000000001, 0000000000, 1111111111, 0110111111, 1111111111, 0000000000, 0000000000, 0000000000, 1111111111, 0000000000, 1111111111, 0000000000, 0000000000, 1111111111, 1111111111, 0000000000, 1111111111, 0000000000, 0000000000, 0000000000, 1111111000, 0000000000, 0000000000, 0000000000, 1111111111, 1111111111, 1111111111, 0000000000, 0000000000, 0000000000, 0000000000, 0000000000, 1111111111, 0000000000, 1111111111, 1111111111, 1111111110, 0000000000, 0000000000, 1111111101, 1111111111, 0000000000, 1111111111, 1111111111, 0000000000, 1111111110, 0000000000, 0000000000, 0111111111, 1111111111, 0000000000, 1111111111, 0000000111, 0000000000, 0000000000, 1111111110, 0111101111, 1111111111, 0000000000, 0000000000, 1111111111, 0000000000, 0000000000, 1111111111, 0000000000, 0000000000, 0000000111, 0000000000, 0000000000, 0000000000, 0000000000, 0000000000, 1111111111, 0000000000, 0000000000, 1000000000, 0000000000, 0000000000, 0000000000, 1111111110, 1111100000, 0000000000, 0000000000, 1111111111, 0000000000, 0000000111, 0001111111, 0000000000, 0000000000, 1111111110, 1111111111, 0000000000, 1000000000, 1111111111, 1111111111, 0000000000, 0000000000, 1111111111, 0000000000, 1111000000, 1111000000, 0000000000, 0000000000, 1111111111, 1111111111, 1100000000, 0000000000, 0000000000, 1111111111, 1111111111, 0000000000, 0000000000, 1111011111, 0000000000, 0000000000, 0000000000, 0000001000, 1111111111, 0000000000, 0000000000, 0000000000, 0000000000, 1111111111, 0000000000, 1110111111, 1111111111, 0000000000, 0000000000, 0000000000, 0000000000, 0000000000, 1111111111, 1111110000, 1111111111, 0000000000, 1000000000, 1000000000, 1111111111, 0000000000, 0000000000, 0000000000, 0000000000, 1111110111, 1111110111, 1101111111, 0000000000, 0000000000, 1111111111, 0000000000, 1111111111, 0000000000, 1111111111, 0000000000, 1111111111, 0000000000, 0000000000, 0000000000, 1111111111, 0000000000, 0001111111, 0111111111, 0000000000, 1111111111, 0111111111, 0000000000, 0111111011, 0000000000, 0000000000, 1111111111, 1111111111, 1111110111, 0110111111, 1111111111, 0000000010, 0000000000, 0000000000, 1111111111, 0000000000, 1111111111, 1111111111, 1111110111, 0000000000, 1111111111, 1111111111, 0000000000, 1111111111, 1111111110, 0000000000, 0000000000, 1111111111, 0000000000, 1111111111, 1000000000, 0000000000, 0000000000, 0000000000, 1111111111, 0000000000, 1111111111, 1111111111, 0000000000, 1111111110, 0000000000, 1111111111, 1111111111, 0000000000, 1111111111, 0000000000, 0000001000, 1111111111, 1111111111, 1111111111, 1111111111, 0000000000, 1111111111, 0000011111, 0000000000, 0010000000, 1111111111, 0000000000, 1111111111, 1111111111, 1111111111, 1000000000, 1111111111, 0000000000, 1111111110, 1111111111, 1111101111, 1111111111, 0000000000, 0000000000, 0000000000, 0000000000, 1111111111, 0000000000, 1111111111, 0000000000, 0000000000, 1101111111, 1111111111, 1001111111, 0000000000, 0000000000, 1111101111, 0000000000, 1111111111, 0000000000, 1111111111, 1111111111, 1101111111, 1111111111, 1000000000, 0000000011, 1111111111, 0000000000, 1111111111, 1111111111, 1111111111, 1101111111, 0000000000, 0000000000, 0000000000, 1111111111, 1111111111, 0000000000, 0000000000, 0111111111, 1000000000, 0111111111, 1111111111, 1111111110, 0000000000, 1111111111, 1111111111, 1111111111, 1111111110, 0000000000, 0000000000, 1111111101, 1111111111, 0000010000, 1111111111, 0000000000, 1111111111, 0000000000, 1111111111, 1111111111, 1111111111, 1111111111, 0000000000, 1111111111, 0000000000, 0000000000, 0000000000, 0111111111, 0000000000, 1111111111, 1000000000, 0011111111, 1111111111, 0001000000, 0000000000, 0000000000, 1111111110, 0000000000, 1111111111, 1111111111, 0000000000, 0111111111, 0000000000, 0000000000, 0000000000, 1111111110, 1000000000, 0000000000, 0000000000, 1111111111, 1111111111, 1111111111, 0000000000, 1111111111, 0000000000, 0111111111, 1111111110, 1111111111, 1111011111, 0000000000, 1111111111, 1111111111, 1000000000, 1101111111, 1111111111, 1110111111, 0000000000, 1000000000, 1111111111, 0000000000, 1111111111, 1111111111, 0000000000, 1111111111, 1111111111, 0000000000, 0000000000, 1111110111, 1000000000, 1111111111, 0000000100, 0000000100, 0000000000, 1111111111, 1111111111, 0000000000, 1111111111, 1000000000, 1101111111, 1111111111, 0000000000, 1100000000, 0000000000, 0000000000, 0100000000, 0000000000, 0000000000, 1111111111, 0000000000, 0000000000, 1111111111, 0000000000, 1111111111, 0111111111, 0000000000, 1111111111, 0000000000, 0000000000, 1111111111, 0000000000, 1111111111, 0000000000, 0000000000, 1111111111, 1111111111, 0000000000, 1111111111, 1111111111, 0000000000, 1111111111, 1000000000, 0000000000, 1111111111, 1111111111, 0000000000, 1111111111, 0000000000, 1110111111, 0011111111, 0000000000, 0000000000, 0000000000, 0000000000, 1000000000, 0000000000, 0111111111, 0000000000, 1111111111, 1111111111, 0000000000, 0000111111, 1111111110, 0000000000, 1111111111, 0000000000, 0000000000, 1101111111, 1111111111, 1111111111, 1111111111, 0000000000, 0000000000, 1111111111, 0000000000, 0000000000, 1111111111, 0000000000, 0111111111, 0000000000, 1111111111, 1111111111, 1000000000, 1111111111, 1111111111, 0000000000, 1111111111, 0000000100, 0111111111, 0000000000, 1111111111, 0000000000, 0000000000, 1111111111, 0000000000, 0000000000, 0000000000, 0000000000, 0000000000, 0111111111, 0000000000, 1000111111, 0000000000, 0000000000, 0000000000, 0000000000, 0000000000, 1111111111, 1111111111, 1111110000, 0000001111, 1111111111, 0000000000, 1111111111, 1111111000, 0000000000, 0000000000, 1111111111, 0000000000, 1111111111, 1111000000, 0000000000, 1111111111, 1111111111, 0000000000, 1111111111, 0000000000, 1111110111, 1111111111, 0000000000, 0111111110, 0000000000, 1111111111, 0000000000, 1111111111, 0000000000, 1111111111, 1111111011, 0000000000, 0000000000, 1111111111, 0000000000, 1111111111, 0000000000, 0000000000, 1111111111, 1101111111, 0000000000, 0000000111, 0000000000, 0000000000, 1111011111, 1111111111, 0000000000, 1111111111, 1111111111, 0000000000, \n" + ] + } + ], + "source": [ + "// get the first classical register (only 1 in this example)\n", + "auto meas_bits = pub_result.data();\n", + "// list of string (binary number in text)\n", + "auto bits = meas_bits.get_bitstrings();\n", + "std::cout << \" ===== samples for pub[0] =====\" << std::endl;\n", + "for (auto b : bits) {\n", + " std::cout << b << \", \";\n", + "}\n", + "std::cout << std::endl;" + ] + }, + { + "cell_type": "markdown", + "id": "d9f0d672-b4a3-49e8-9389-ff074ad6dd33", + "metadata": {}, + "source": [ + "Also we can get counts for each possibility in a list by using `BitArray::get_counts` function." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f518fcb6-d18e-44bc-8505-190b45ac6875", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " ===== counts for pub[0] =====\n", + "1000111111 : 1\n", + "0000111111 : 1\n", + "0001000000 : 1\n", + "0011111111 : 2\n", + "0000010000 : 1\n", + "0111111011 : 1\n", + "1111110000 : 2\n", + "1111000000 : 3\n", + "0110111111 : 2\n", + "1111100000 : 2\n", + "1111111011 : 3\n", + "0010000001 : 1\n", + "1111111110 : 21\n", + "0000001000 : 5\n", + "0111101111 : 1\n", + "1110111111 : 7\n", + "1111111100 : 1\n", + "0111111111 : 22\n", + "1111111000 : 6\n", + "0010000000 : 2\n", + "1100000000 : 7\n", + "0000000000 : 433\n", + "0001111111 : 3\n", + "0000000010 : 2\n", + "0000000001 : 4\n", + "1111111111 : 377\n", + "1111111101 : 5\n", + "0100000000 : 2\n", + "1001111111 : 1\n", + "0000011111 : 3\n", + "1011111111 : 1\n", + "1111011111 : 5\n", + "0000000111 : 5\n", + "0000000110 : 1\n", + "1111101111 : 8\n", + "0111111110 : 2\n", + "0000000011 : 3\n", + "1101110111 : 1\n", + "0011111101 : 1\n", + "0000001111 : 1\n", + "0000000100 : 4\n", + "1000000000 : 26\n", + "1110000000 : 1\n", + "1101111111 : 9\n", + "1111111010 : 1\n", + "1111110111 : 8\n", + "1010000000 : 1\n" + ] + } + ], + "source": [ + "auto counts = meas_bits.get_counts();\n", + "std::cout << \" ===== counts for pub[0] =====\" << std::endl;\n", + "for (auto c : counts) {\n", + " // counts is pair of (possibility bitstring, count)\n", + " std::cout << c.first << \" : \" << c.second << std::endl;\n", + "}" + ] + }, + { + "cell_type": "markdown", + "id": "b574f9d6-4290-4989-874e-54c865b9aa93", + "metadata": {}, + "source": [ + "***\n", + "[back to top](readme.md)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2b7ae83b-9111-4799-85cb-372937fa65e4", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "C++11", + "language": "C++11", + "name": "xcpp11" + }, + "language_info": { + "codemirror_mode": "text/x-c++src", + "file_extension": ".cpp", + "mimetype": "text/x-c++src", + "name": "c++", + "version": "11" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}