{ "cells": [ { "cell_type": "markdown", "id": "12bbcb1f-f2f5-471b-9c8e-f8d46cfb8479", "metadata": {}, "source": [ "# EstimatorV2 Usage\n", "### Modified for Quantum Rings toolkit for Qiskit 2.x\n", "#### See here: https://qiskit.qotlabs.org/docs/guides/primitives-examples" ] }, { "cell_type": "code", "execution_count": 1, "id": "a93bbe11-8439-454e-b979-d3618fea1d30", "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", "\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": "markdown", "id": "66a99345-6ebe-422f-91c5-dd3979a72c8f", "metadata": {}, "source": [ "## Basic Estimator Usage" ] }, { "cell_type": "code", "execution_count": 2, "id": "682f7d6e-84bd-49a7-a2ca-b59724dcb4a2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['scarlet_quantum_rings']\n", ">>> Expectation value: -1.0636533500290943\n" ] } ], "source": [ "from dataclasses import asdict\n", "#from qiskit_ibm_runtime import QiskitRuntimeService\n", "#from qiskit_ibm_runtime import EstimatorV2 as Estimator\n", " \n", "#service = QiskitRuntimeService()\n", "#backend = service.least_busy(operational=True, simulator=False)\n", "\n", "from quantumrings.toolkit.qiskit import QrRuntimeService\n", "\n", "service = QrRuntimeService( token = my_token , name = my_name)\n", "avaiable_backends = service.backends()\n", "print(avaiable_backends)\n", "#backend = service.least_busy(operational=True, simulator=False)\n", "backend = service.backend(name = my_backend, precision = \"double\", gpu = 0, num_qubits = 12)\n", "\n", "from qiskit import QuantumCircuit\n", "from qiskit.quantum_info import SparsePauliOp\n", " \n", "# Step 1: Define operator\n", "op = SparsePauliOp.from_list(\n", " [\n", " (\"II\", -1.052373245772859),\n", " (\"IZ\", 0.39793742484318045),\n", " (\"ZI\", -0.39793742484318045),\n", " (\"ZZ\", -0.01128010425623538),\n", " (\"XX\", 0.18093119978423156),\n", " ]\n", ")\n", " \n", "# Step 2: Define quantum state\n", "circuit = QuantumCircuit(2)\n", "circuit.x(0)\n", "circuit.x(1)\n", "\n", "from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager\n", " \n", "pm = generate_preset_pass_manager(backend=backend, optimization_level=1)\n", "isa_circuit = pm.run(circuit)\n", " \n", "isa_observable = op.apply_layout(isa_circuit.layout)\n", "\n", "from quantumrings.toolkit.qiskit import QrEstimatorV2 as Estimator\n", "estimator = Estimator(backend = backend)\n", "job = estimator.run([(isa_circuit, isa_observable)])\n", " \n", "# Get results for the first (and only) PUB\n", "pub_result = job.result()[0]\n", "\n", "#expect: [-0.8879899326312926]\n", "print(f\">>> Expectation value: {pub_result.data.evs[0]}\")" ] }, { "cell_type": "markdown", "id": "beb9565b-2a11-4eb6-8578-306646f39bbe", "metadata": {}, "source": [ "## Run a Single Experiment" ] }, { "cell_type": "code", "execution_count": 3, "id": "20650cc8-0c5d-44d5-93b4-1385529ff6d7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " > Expectation value: 0.7071066498756409\n", " > Metadata: {'target_precision': 0.001, 'shots': 1, 'circuit_metadata': {}}\n" ] } ], "source": [ "import numpy as np\n", "from qiskit.circuit.library import iqp\n", "from qiskit.transpiler import generate_preset_pass_manager\n", "from qiskit.quantum_info import SparsePauliOp, random_hermitian\n", "#from qiskit_ibm_runtime import QiskitRuntimeService, EstimatorV2 as Estimator\n", " \n", "n_qubits = 5 #0\n", " \n", "#service = QiskitRuntimeService()\n", "#backend = service.least_busy(\n", "# operational=True, simulator=False, min_num_qubits=n_qubits\n", "#)\n", "\n", "from quantumrings.toolkit.qiskit import QrRuntimeService\n", "from quantumrings.toolkit.qiskit import QrEstimatorV2 as Estimator\n", "\n", "service = QrRuntimeService( token = my_token , name = my_name)\n", "backend = service.backend(name = my_backend, precision = \"single\", gpu = 0, num_qubits = n_qubits)\n", "\n", "mat = np.real(random_hermitian(n_qubits, seed=1234))\n", "circuit = iqp(mat)\n", "observable = SparsePauliOp(\"Z\" * n_qubits)\n", " \n", "pm = generate_preset_pass_manager(backend=backend, optimization_level=1)\n", "isa_circuit = pm.run(circuit)\n", "isa_observable = observable.apply_layout(isa_circuit.layout)\n", " \n", "estimator = Estimator(mode=backend)\n", "job = estimator.run([(isa_circuit, isa_observable)])\n", "result = job.result()\n", " \n", "print(f\" > Expectation value: {result[0].data.evs[0]}\")\n", "print(f\" > Metadata: {result[0].metadata}\")" ] }, { "cell_type": "markdown", "id": "55ee521c-fad7-4e15-a941-911614cfbf6a", "metadata": {}, "source": [ "## Run multiple experiments in a single job" ] }, { "cell_type": "code", "execution_count": 4, "id": "2a29fbd3-d4ce-4c9f-b18d-c70e4afb0756", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ">>> Expectation values for PUB 0: 0.0\n", ">>> Standard errors for PUB 0: [0.]\n", ">>> Expectation values for PUB 1: 0.0\n", ">>> Standard errors for PUB 1: [0.]\n", ">>> Expectation values for PUB 2: 0.0\n", ">>> Standard errors for PUB 2: [0.]\n" ] } ], "source": [ "import numpy as np\n", "from qiskit.circuit.library import iqp\n", "from qiskit.transpiler import generate_preset_pass_manager\n", "from qiskit.quantum_info import SparsePauliOp, random_hermitian\n", "#from qiskit_ibm_runtime import QiskitRuntimeService, EstimatorV2 as Estimator\n", " \n", "n_qubits = 5 #0\n", " \n", "#service = QiskitRuntimeService()\n", "#backend = service.least_busy(\n", "# operational=True, simulator=False, min_num_qubits=n_qubits\n", "#)\n", "\n", "from quantumrings.toolkit.qiskit import QrRuntimeService\n", "from quantumrings.toolkit.qiskit import QrEstimatorV2 as Estimator\n", "\n", "service = QrRuntimeService( token = my_token , name = my_name)\n", "backend = service.backend(name = my_backend, precision = \"single\", gpu = 0, num_qubits = n_qubits)\n", "\n", "rng = np.random.default_rng()\n", "mats = [np.real(random_hermitian(n_qubits, seed=rng)) for _ in range(3)]\n", " \n", "pubs = []\n", "circuits = [iqp(mat) for mat in mats]\n", "observables = [\n", " SparsePauliOp(\"X\" * n_qubits),\n", " SparsePauliOp(\"Y\" * n_qubits),\n", " SparsePauliOp(\"Z\" * n_qubits),\n", "]\n", " \n", "# Get ISA circuits\n", "pm = generate_preset_pass_manager(optimization_level=1, backend=backend)\n", " \n", "for qc, obs in zip(circuits, observables):\n", " isa_circuit = pm.run(qc)\n", " isa_obs = obs.apply_layout(isa_circuit.layout)\n", " pubs.append((isa_circuit, isa_obs))\n", " \n", "estimator = Estimator(backend=backend)\n", "job = estimator.run(pubs)\n", "job_result = job.result()\n", " \n", "for idx in range(len(pubs)):\n", " pub_result = job_result[idx]\n", " print(f\">>> Expectation values for PUB {idx}: {pub_result.data.evs[0]}\")\n", " print(f\">>> Standard errors for PUB {idx}: {pub_result.data.stds}\")" ] }, { "cell_type": "markdown", "id": "40113849-aba2-4797-b09f-267c87a59578", "metadata": {}, "source": [ "## Run parameterized circuits" ] }, { "cell_type": "code", "execution_count": 5, "id": "9e797bfc-4341-46d3-90dd-2e8b5066fc2a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "reshaped_ops: [[SparsePauliOp(['ZZ'],\n", " coeffs=[1.+0.j])]\n", " [SparsePauliOp(['ZX'],\n", " coeffs=[1.+0.j])]\n", " [SparsePauliOp(['XZ'],\n", " coeffs=[1.+0.j])]\n", " [SparsePauliOp(['XX'],\n", " coeffs=[1.+0.j])]]\n", ">>> Expectation values: [[ 1.00000012e+00 9.51056659e-01 8.09017062e-01 5.87785363e-01\n", " 3.09017062e-01 0.00000000e+00 -3.09017062e-01 -5.87785363e-01\n", " -8.09017181e-01 -9.51056659e-01 -1.00000012e+00 -9.51056659e-01\n", " -8.09017003e-01 -5.87785065e-01 -3.09017181e-01 0.00000000e+00\n", " 3.09017211e-01 5.87785125e-01 8.09017003e-01 9.51056659e-01\n", " 1.00000012e+00]\n", " [ 0.00000000e+00 3.09017062e-01 5.87785363e-01 8.09017181e-01\n", " 9.51056659e-01 1.00000000e+00 9.51056659e-01 8.09017181e-01\n", " 5.87785363e-01 3.09017122e-01 -8.74227837e-08 -3.09017003e-01\n", " -5.87785363e-01 -8.09017241e-01 -9.51056540e-01 -1.00000000e+00\n", " -9.51056600e-01 -8.09017181e-01 -5.87785363e-01 -3.09017003e-01\n", " 0.00000000e+00]\n", " [ 0.00000000e+00 -3.09017062e-01 -5.87785363e-01 -8.09017181e-01\n", " -9.51056659e-01 -1.00000000e+00 -9.51056659e-01 -8.09017181e-01\n", " -5.87785363e-01 -3.09017122e-01 8.74227837e-08 3.09017003e-01\n", " 5.87785363e-01 8.09017241e-01 9.51056540e-01 1.00000000e+00\n", " 9.51056600e-01 8.09017181e-01 5.87785363e-01 3.09017003e-01\n", " 0.00000000e+00]\n", " [ 1.00000012e+00 9.51056659e-01 8.09017062e-01 5.87785363e-01\n", " 3.09017062e-01 0.00000000e+00 -3.09017062e-01 -5.87785363e-01\n", " -8.09017181e-01 -9.51056659e-01 -1.00000012e+00 -9.51056659e-01\n", " -8.09017003e-01 -5.87785065e-01 -3.09017181e-01 0.00000000e+00\n", " 3.09017211e-01 5.87785125e-01 8.09017003e-01 9.51056659e-01\n", " 1.00000012e+00]]\n", ">>> Standard errors: [[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]\n", ">>> Metadata: {'target_precision': 0.001, 'shots': 1, 'circuit_metadata': {}}\n" ] } ], "source": [ "import numpy as np\n", " \n", "from qiskit.circuit import QuantumCircuit, Parameter\n", "from qiskit.quantum_info import SparsePauliOp\n", "from qiskit.transpiler import generate_preset_pass_manager\n", "#from qiskit_ibm_runtime import QiskitRuntimeService, EstimatorV2 as Estimator\n", " \n", "#service = QiskitRuntimeService()\n", "#backend = service.least_busy(operational=True, simulator=False)\n", "\n", "from quantumrings.toolkit.qiskit import QrRuntimeService\n", "from quantumrings.toolkit.qiskit import QrEstimatorV2 as Estimator\n", "\n", "service = QrRuntimeService( token = my_token , name = my_name)\n", "backend = service.backend(name = my_backend, precision = \"single\", gpu = 0, num_qubits = 2)\n", " \n", "# Step 1: Map classical inputs to a quantum problem\n", "theta = Parameter(\"θ\")\n", " \n", "chsh_circuit = QuantumCircuit(2)\n", "chsh_circuit.h(0)\n", "chsh_circuit.cx(0, 1)\n", "chsh_circuit.ry(theta, 0)\n", " \n", "number_of_phases = 21\n", "phases = np.linspace(0, 2 * np.pi, number_of_phases)\n", "individual_phases = [[ph] for ph in phases]\n", " \n", "ZZ = SparsePauliOp.from_list([(\"ZZ\", 1)])\n", "ZX = SparsePauliOp.from_list([(\"ZX\", 1)])\n", "XZ = SparsePauliOp.from_list([(\"XZ\", 1)])\n", "XX = SparsePauliOp.from_list([(\"XX\", 1)])\n", "ops = [ZZ, ZX, XZ, XX]\n", " \n", "# Step 2: Optimize problem for quantum execution.\n", " \n", "pm = generate_preset_pass_manager(backend=backend, optimization_level=1)\n", "chsh_isa_circuit = pm.run(chsh_circuit)\n", "isa_observables = [\n", " operator.apply_layout(chsh_isa_circuit.layout) for operator in ops\n", "]\n", " \n", "# Step 3: Execute using Qiskit primitives.\n", " \n", "# Reshape observable array for broadcasting\n", "reshaped_ops = np.fromiter(isa_observables, dtype=object)\n", "reshaped_ops = reshaped_ops.reshape((4, 1))\n", "\n", "print (f\"reshaped_ops: {reshaped_ops}\")\n", "\n", "estimator = Estimator(backend=backend, options={\"default_shots\": int(1e4)})\n", "job = estimator.run([(chsh_isa_circuit, reshaped_ops, individual_phases)])\n", "# Get results for the first (and only) PUB\n", "pub_result = job.result()[0]\n", "print(f\">>> Expectation values: {pub_result.data.evs}\")\n", "print(f\">>> Standard errors: {pub_result.data.stds}\")\n", "print(f\">>> Metadata: {pub_result.metadata}\")" ] }, { "cell_type": "markdown", "id": "7c9c8f72-9cb5-4420-a324-30d9540d686f", "metadata": {}, "source": [ "## Use sessions and advanced options" ] }, { "cell_type": "code", "execution_count": 6, "id": "25ecbbe8-6505-4fcb-a63c-f38b9b08b8d6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " > Expectation value: 0.0\n", " > Metadata: {'target_precision': 0.001, 'shots': 1, 'circuit_metadata': {}}\n", " > Another Expectation value: 0.0\n", " > More Metadata: {'target_precision': 0.001, 'shots': 1, 'circuit_metadata': {}}\n" ] } ], "source": [ "import numpy as np\n", "from qiskit.circuit.library import iqp\n", "from qiskit.transpiler import generate_preset_pass_manager\n", "from qiskit.quantum_info import SparsePauliOp, random_hermitian\n", "#from qiskit_ibm_runtime import (\n", "# QiskitRuntimeService,\n", "# Session,\n", "# EstimatorV2 as Estimator,\n", "#)\n", " \n", "n_qubits = 5 #0\n", " \n", "#service = QiskitRuntimeService()\n", "#backend = service.least_busy(\n", "# operational=True, simulator=False, min_num_qubits=n_qubits\n", "#)\n", "\n", "from quantumrings.toolkit.qiskit import QrRuntimeService\n", "from quantumrings.toolkit.qiskit import QrEstimatorV2 as Estimator\n", "from quantumrings.toolkit.qiskit import QrSession as Session\n", "\n", "service = QrRuntimeService( token = my_token , name = my_name)\n", "backend = service.backend(name = my_backend, precision = \"single\", gpu = 0, num_qubits = n_qubits)\n", " \n", "rng = np.random.default_rng(1234)\n", "mat = np.real(random_hermitian(n_qubits, seed=rng))\n", "circuit = iqp(mat)\n", "mat = np.real(random_hermitian(n_qubits, seed=rng))\n", "another_circuit = iqp(mat)\n", "observable = SparsePauliOp(\"X\" * n_qubits)\n", "another_observable = SparsePauliOp(\"Y\" * n_qubits)\n", " \n", "pm = generate_preset_pass_manager(optimization_level=1, backend=backend)\n", "isa_circuit = pm.run(circuit)\n", "another_isa_circuit = pm.run(another_circuit)\n", "isa_observable = observable.apply_layout(isa_circuit.layout)\n", "another_isa_observable = another_observable.apply_layout(\n", " another_isa_circuit.layout\n", ")\n", " \n", "with Session(backend=backend) as session:\n", " estimator = Estimator(mode=session)\n", " \n", " estimator.options.resilience_level = 1\n", " \n", " job = estimator.run([(isa_circuit, isa_observable)])\n", " another_job = estimator.run(\n", " [(another_isa_circuit, another_isa_observable)]\n", " )\n", " result = job.result()\n", " another_result = another_job.result()\n", " \n", " # first job\n", " print(f\" > Expectation value: {result[0].data.evs[0]}\")\n", " print(f\" > Metadata: {result[0].metadata}\")\n", " \n", " # second job\n", " print(f\" > Another Expectation value: {another_result[0].data.evs[0]}\")\n", " print(f\" > More Metadata: {another_result[0].metadata}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "f4eca7de-a529-416f-85e8-953715d6f4b6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "878099d5-332f-4d2a-bc1d-db71bcc2d57e", "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 }