{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "c2b497b5-bdbc-4c1a-b6f3-bb09a46001bc", "metadata": {}, "outputs": [], "source": [ "#\n", "# Setup your account\n", "# You can also save your account locally using the class method QrRuntimeService.save_account(...) and\n", "# invoke the QrRuntimeService class constructor without any arguments.\n", "#\n", "\n", "import os\n", "my_token = os.environ[\"QR_TOKEN\"]\n", "my_name = os.environ[\"QR_ACCOUNT\"]\n", "\n", "#\n", "# Set the backend of your choice, depending upon the task and your hardware configuration.\n", "# See SDK documentation for additional help.\n", "#\n", "\n", "my_backend = \"scarlet_quantum_rings\"" ] }, { "cell_type": "code", "execution_count": 2, "id": "b2691935-2fce-40e3-bbc0-960af576ac39", "metadata": {}, "outputs": [], "source": [ "# Import from Quantum Rings Toolkit\n", "from quantumrings.toolkit.qiskit import QrRuntimeService\n", "\n", "# Acquire Quantum Rings backend\n", "qr_services = QrRuntimeService(name = my_name, token = my_token)\n", "qr_backend = qr_services.backend(name = my_backend, precision = \"single\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "7d965d4d-3a88-421f-8a85-2a046d1fe137", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['00', '00', '10', '00', '00', '10', '10', '00', '10', '10', '00', '10', '10', '00', '00', '10', '10', '10', '10', '00', '10', '00', '00', '00', '00', '10', '10', '00', '10', '10', '00', '00', '10', '10', '10', '10', '00', '10', '10', '00', '00', '10', '10', '00', '10', '10', '00', '00', '10', '10', '10', '10', '00', '10', '10', '00', '10', '10', '00', '00', '00', '00', '10', '00', '00', '10', '00', '10', '00', '00', '10', '10', '00', '00', '00', '10', '00', '10', '10', '00', '00', '10', '10', '10', '00', '10', '10', '10', '00', '00', '10', '10', '10', '00', '00', '10', '10', '10', '10', '10', '10', '10', '10', '00', '00', '00', '10', '10', '10', '10', '10', '00', '10', '10', '00', '10', '10', '10', '00', '10', '00', '00', '10', '10', '00', '10', '00', '10', '10', '10', '00', '00', '10', '00', '00', '00', '00', '10', '10', '00', '00', '00', '10', '10', '10', '00', '10', '10', '10', '00', '00', '00', '10', '10', '10', '00', '00', '10', '10', '00', '10', '10', '00', '10', '10', '10', '00', '10', '00', '00', '00', '10', '10', '10', '10', '10', '00', '10', '00', '10', '00', '10', '10', '00', '10', '00', '10', '00', '10', '10', '00', '10', '10', '10', '10', '00', '10', '10', '10', '10', '10', '10', '10', '10', '10', '10', '10', '00', '10', '00', '10', '10', '00', '10', '10', '00', '00', '10', '10', '10', '10', '10', '10', '10', '10', '10', '00', '10', '00', '00', '10', '10', '00', '00', '00', '10', '10', '10', '00', '00', '10', '10', '00', '10', '00', '10', '10', '10', '00', '10', '10', '00', '10', '10', '10', '00']\n" ] } ], "source": [ "from qiskit.circuit import (\n", " Parameter, QuantumCircuit, ClassicalRegister, QuantumRegister\n", ")\n", "\n", "#from qiskit.primitives import StatevectorSampler\n", "from quantumrings.toolkit.qiskit import QrStatevectorSampler\n", "\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", " \n", "# Define our circuit registers, including classical registers\n", "# called 'alpha' and 'beta'.\n", "qreg = QuantumRegister(3)\n", "alpha = ClassicalRegister(2, \"alpha\")\n", "beta = ClassicalRegister(1, \"beta\")\n", " \n", "# Define a quantum circuit with two parameters.\n", "circuit = QuantumCircuit(qreg, alpha, beta)\n", "circuit.h(0)\n", "circuit.cx(0, 1)\n", "circuit.cx(1, 2)\n", "circuit.ry(Parameter(\"a\"), 0)\n", "circuit.rz(Parameter(\"b\"), 0)\n", "circuit.cx(1, 2)\n", "circuit.cx(0, 1)\n", "circuit.h(0)\n", "circuit.measure([0, 1], alpha)\n", "circuit.measure([2], beta)\n", "\n", "number_of_params = 100\n", "\n", "# Define a sweep over parameter values, where the second axis is over.\n", "# the two parameters in the circuit.\n", "params = np.vstack([\n", " np.linspace(-np.pi, np.pi, number_of_params),\n", " np.linspace(-4 * np.pi, 4 * np.pi, number_of_params)\n", "]).T\n", " \n", "# Instantiate a new statevector simulation based sampler object.\n", "sampler = QrStatevectorSampler(backend = qr_backend)\n", " \n", "# Start a job that will return shots for all 100 parameter value sets.\n", "pub = (circuit, params)\n", "job = sampler.run([pub], shots=256)\n", " \n", "# Extract the result for the 0th pub (this example only has one pub).\n", "result = job.result()[0]\n", " \n", "# There is one BitArray object for each ClassicalRegister in the\n", "# circuit. Here, we can see that the BitArray for alpha contains data\n", "# for all 100 sweep points, and that it is indeed storing data for 2\n", "# bits over 256 shots.\n", "assert result.data.alpha.shape == (number_of_params,)\n", "assert result.data.alpha.num_bits == 2\n", "assert result.data.alpha.num_shots == 256\n", " \n", "# We can work directly with a binary array in performant applications.\n", "raw = result.data.alpha.array\n", " \n", "# For small registers where it is anticipated to have many counts\n", "# associated with the same bitstrings, we can turn the data from,\n", "# for example, the 22nd sweep index into a dictionary of counts.\n", "counts = result.data.alpha.get_counts(22)\n", " \n", "# Or, convert into a list of bitstrings that preserve shot order.\n", "bitstrings = result.data.alpha.get_bitstrings(22)\n", "print(bitstrings)" ] }, { "cell_type": "code", "execution_count": null, "id": "edcb5879-9f59-4cfc-99e6-6811ef8a64fe", "metadata": {}, "outputs": [], "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.12.9" } }, "nbformat": 4, "nbformat_minor": 5 }