{ "cells": [ { "cell_type": "markdown", "id": "33be72af", "metadata": {}, "source": [ "# Custom Expectation Value Program for the Qiskit Runtime\n", "\n", "```{post} 2021-10-13\n", ":tags: Custom\n", ":category: Runtime\n", "```\n", "\n", "

\n", "Paul Nation\n", "

\n", "

\n", "IBM Quantum Partners Technical Enablement Team\n", "

\n", "\n", "Here we will show how to make a program that takes a circuit, or list of circuits, and computes the expectation values of one or more diagonal operators." ] }, { "cell_type": "markdown", "id": "118e72f0", "metadata": {}, "source": [ "## Prerequisites\n", "\n", "- You must have Qiskit 0.30+ installed.\n", "- You must have an IBM Quantum Experience account with the ability to upload a Runtime program. **Currently there is no way to know if you have Runtime upload ability outside of an email from IBM**." ] }, { "cell_type": "markdown", "id": "6368c90a", "metadata": {}, "source": [ "## Background\n", "\n", "The primary method by which information is obtained from quantum computers is via expectation values. Indeed, the samples that come from executing a quantum circuit multiple times, once converted to probabilities, can be viewed as just a finite sample approximation to the expectation value for the projection operators corresponding to each bitstring. More practically, many quantum algorithms require computing expectation values over Pauli operators, e.g. Variational Quantum Eigensolvers, and thus having a runtime program that computes these quantities is of fundamental importance. Here we look at one such example, where an user passes one or more circuits and expectation operators and gets back the computed expectation values, and possibly error bounds.\n", "\n", "### Expectation value of a diagonal operator\n", "\n", "Consider a generic observable given by the tensor product of diagonal operators over $N$ qubits $O = O_{N-1}\\dots O_{0}$ where the subscript indicates the qubit on which the operator acts. Then for a set of observed $M$ bitstrings $\\{b_{0}, \\dots b_{M-1}\\}$, where $M \\leq 2^N $, with corresponding approximate probabilites $p_{m}$ the expectation value is given by\n", "\n", "$$\n", "\\langle O\\rangle \\simeq \\sum_{m=0}^{M-1} p_{m}\\prod_{n=0}^{N-1}O_{n}[b_{m}[N-n-1], b_{m}[N-n-1]],\n", "$$\n", "\n", "where $O_{n}[b_{m}[N-n-1], b_{m}[N-n-1]]$ is the diagonal element of $O_{n}$ specified by the $N-n-1$th bit in bitstring $b_{m}$. The reason for the complicated indexing in `b_{m}` is because Qiskit uses least-sginificant bit indexing where the zeroth element of the bit-strings is given by the right-most bit.\n", "\n", "Here we will use built-in routines to compute these expectation values. However, it is not hard to do yourself, with plenty of examples to be found." ] }, { "cell_type": "markdown", "id": "42df9e62", "metadata": {}, "source": [ "## Main program\n", "\n", "Here we define our main function for the expectation value runtime program. As always, our program must start with the `backend`, and `user_messenger` arguements, followed by the actual inputs we pass to the program. Here our options are quite simple:\n", "\n", "- `circuits`: A single QuantumCircuit or list of QuantumCircuits to be executed on the target backend.\n", "\n", "\n", "- `expectation_operators`: The operators we want to evaluate. These can be strings of diagonal Pauli's, eg, `ZIZZ`, or custom operators defined by dictionarys. For example, the projection operator on the all ones state of 4 qubits is `{'1111': 1}`.\n", "\n", "\n", "- `shots`: Howe many times to sample each circuit.\n", "\n", "\n", "- `transpiler_config`: A dictionary that passes additional arguments on to the transpile function, eg. `optimization_level`.\n", "\n", "\n", "- `run_config`: A dictionary that passes additional arguments on to `backend.run()`.\n", "\n", "\n", "- `skip_transpilation`: A flag to skip transpilation altogether and just run the circuits. This is useful for situations where you need to transpile parameterized circuits once, but must bind parameters multiple times and evaluate. \n", "\n", "\n", "- `return_stddev`: Flag to return bound on standard deviation. If using measurement mitigation this adds some overhead to the computation.\n", "\n", "\n", "- `use_measurement_mitigation`: Use M3 measurement mitigation and compute expecation value and standard deviation bound from quasi-probabilities.\n", "\n", "At the top of the cell below you will see a commented out `%%writefile sample_expval.py`. We will use this to convert the cell to a Python module named `sample_expval.py` to upload." ] }, { "cell_type": "code", "execution_count": 1, "id": "06b48e43", "metadata": {}, "outputs": [], "source": [ "#%%writefile sample_expval.py\n", "import mthree\n", "from qiskit import transpile\n", "\n", "# The entrypoint for our Runtime Program\n", "def main(backend, user_messenger,\n", " circuits,\n", " expectation_operators='',\n", " shots = 8192,\n", " transpiler_config={},\n", " run_config={},\n", " skip_transpilation=False,\n", " return_stddev=False,\n", " use_measurement_mitigation=False,\n", " ):\n", " \n", " \"\"\"Compute expectation \n", " values for a list of operators after\n", " executing a list of circuits on the target backend.\n", " \n", " Parameters:\n", " backend (ProgramBackend): Qiskit backend instance.\n", " user_messenger (UserMessenger): Used to communicate with the program user.\n", " circuits: (QuantumCircuit or list): A single list of QuantumCircuits.\n", " expectation_operators (str or dict or list): Expectation values to evaluate.\n", " shots (int): Number of shots to take per circuit.\n", " transpiler_config (dict): A collection of kwargs passed to transpile().\n", " run_config (dict): A collection of kwargs passed to backend.run().\n", " skip_transpilation (bool): Skip transpiling of circuits, default=False.\n", " return_stddev (bool): Return upper bound on standard devitation,\n", " default=False. \n", " use_measurement_mitigation (bool): Improve resulting using measurement\n", " error mitigation, default=False.\n", " \n", " Returns:\n", " array_like: Returns array of expectation values or a list of (expval, stddev)\n", " tuples if return_stddev=True.\n", " \"\"\"\n", " \n", " # transpiling the circuits using given transpile options\n", " if not skip_transpilation:\n", " trans_circuits = transpile(circuits, backend=backend,\n", " **transpiler_config)\n", " # Make sure everything is a list\n", " if not isinstance(trans_circuits, list):\n", " trans_circuits = [trans_circuits]\n", " # If skipping set circuits -> trans_circuits\n", " else:\n", " if not isinstance(circuits, list):\n", " trans_circuits = [circuits]\n", " else:\n", " trans_circuits = circuits\n", "\n", " # If we are given a single circuit but requesting multiple expectation\n", " # values, then set flag to make multiple pointers to same result.\n", " duplicate_results = False\n", " if isinstance(expectation_operators, list):\n", " if len(expectation_operators) and len(trans_circuits) == 1:\n", " duplicate_results = True\n", " \n", " # If doing measurement mitigation we must build and calibrate a\n", " # mitigator object. Will also determine which qubits need to be\n", " # calibrated.\n", " if use_measurement_mitigation:\n", " # Get an the measurement mappings at end of circuits\n", " meas_maps = mthree.utils.final_measurement_mapping(trans_circuits)\n", " # Get an M3 mitigator\n", " mit = mthree.M3Mitigation(backend)\n", " # Calibrate over the set of qubits measured in the transpiled circuits.\n", " mit.cals_from_system(meas_maps)\n", "\n", " # Compute raw results\n", " result = backend.run(trans_circuits, shots=shots, **run_config).result()\n", " raw_counts = result.get_counts()\n", "\n", " # When using measurement mitigation we need to apply the correction and then\n", " # compute the expectation values from the computed quasi-probabilities.\n", " if use_measurement_mitigation:\n", " quasi_dists = mit.apply_correction(raw_counts, meas_maps,\n", " return_mitigation_overhead=return_stddev)\n", " \n", " if duplicate_results:\n", " quasi_dists = mthree.classes.QuasiCollection(\n", " [quasi_dists]*len(expectation_operators))\n", " # There are two different calls depending on what we want returned.\n", " if return_stddev:\n", " return quasi_dists.expval_and_stddev(expectation_operators)\n", " return quasi_dists.expval(expectation_operators)\n", " \n", " # If the program didn't return in the mitigation loop above it means\n", " # we are processing the raw_counts data. We do so here using the\n", " # mthree utilities\n", " if duplicate_results:\n", " raw_counts = [raw_counts]*len(expectation_operators)\n", " if return_stddev:\n", " return mthree.utils.expval_and_stddev(raw_counts, expectation_operators)\n", " return mthree.utils.expval(raw_counts, expectation_operators)" ] }, { "cell_type": "markdown", "id": "edadd3f8", "metadata": {}, "source": [ "## Local testing\n", "\n", "Here we test with a local \"Fake\" backend that mimics the noise properties of a real system and a 4-qubit GHZ state." ] }, { "cell_type": "code", "execution_count": 2, "id": "2a25b3ac", "metadata": {}, "outputs": [], "source": [ "from qiskit import QuantumCircuit\n", "from qiskit.test.mock import FakeSantiago\n", "from qiskit.providers.ibmq.runtime import UserMessenger\n", "msg = UserMessenger()\n", "backend = FakeSantiago()" ] }, { "cell_type": "code", "execution_count": 3, "id": "6339a144", "metadata": {}, "outputs": [], "source": [ "qc = QuantumCircuit(4)\n", "qc.h(2)\n", "qc.cx(2, 1)\n", "qc.cx(1, 0)\n", "qc.cx(2, 3)\n", "qc.measure_all()" ] }, { "cell_type": "code", "execution_count": 4, "id": "3966f447", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.974614 , 1. , 0.02428596])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "main(backend, msg,\n", " qc,\n", " expectation_operators=['ZZZZ', 'IIII', 'IZZZ'],\n", " transpiler_config={'optimization_level':3, 'layout_method': 'sabre',\n", " 'routing_method': 'sabre'},\n", " run_config={},\n", " skip_transpilation=False,\n", " return_stddev=False,\n", " use_measurement_mitigation=True\n", " )" ] }, { "cell_type": "markdown", "id": "fafe3295", "metadata": {}, "source": [ "If we have done our job correctly, the above should print out two expectation values close to one and a final expectation value close to zero." ] }, { "cell_type": "markdown", "id": "f1fa15d4", "metadata": {}, "source": [ "## Program metadata\n", "\n", "
\n", "Important: At this time all program names must be unique amongst all users. Thus you need to change the metadata `name` value to soemthing other than `sample-expval` if you want to upload.\n", "
\n", "\n", "Next we add the needed program data to a dictionary for uploading with our program." ] }, { "cell_type": "code", "execution_count": 297, "id": "cdb8037d", "metadata": {}, "outputs": [], "source": [ "meta = {\n", " \"name\": \"sample-expval\",\n", " \"description\": \"A sample expectation value program.\",\n", " \"max_execution_time\": 1000,\n", " \"version\": \"1.0\",\n", "}\n", "\n", "meta[\"parameters\"] = [\n", " {\"name\": \"circuits\", \"description\": \"A single or list of QuantumCircuits.\",\n", " \"type\": \"QuantumCircuit or list\", \"required\": True},\n", " {\"name\": \"expectation_operators\", \"description\": \"One or more expectation values to evaluate\",\n", " \"type\": \"str or dict or list\", \"required\": False},\n", " {\"name\": \"shots\", \"description\": \"Number of shots per circuit.\", \"type\": \"int\", \"required\": False},\n", " {\"name\": \"transpiler_config\", \"description\": \"A collection of kwargs passed to transpile.\",\n", " \"type\": \"dict\", \"required\": False},\n", " {\"name\": \"run_config\", \"description\": \"A collection of kwargs passed to backend.run.\",\n", " \"type\": \"dict\", \"required\": False},\n", " {\"name\": \"return_stddev\", \"description\": \"Return upper-bound on standard deviation.\",\n", " \"type\": \"bool\", \"required\": False},\n", " {\"name\": \"skip_transpilation\", \"description\": \"Skip circuit transpilation.\",\n", " \"type\": \"bool\", \"required\": False},\n", " {\"name\": \"use_measurement_mitigation\", \"description\": \"Use measurement mitigation to improve results.\",\n", " \"type\": \"bool\", \"required\": False}\n", " ]\n", "\n", "meta[\"return_values\"]: [\n", " {\"name\": \"result\", \"description\": \"A list of expectation values and optionally standard deviations.\", \"type\": \"list\"}\n", " ]" ] }, { "cell_type": "markdown", "id": "09521947", "metadata": {}, "source": [ "## Upload the program\n", "\n", "We are now in a position to upload the program. To do so we first uncomment and excute the line `%%writefile sample_expval.py` giving use the `sample_expval.py` file we need to upload. " ] }, { "cell_type": "code", "execution_count": 5, "id": "935a21d6", "metadata": {}, "outputs": [], "source": [ "from qiskit import IBMQ\n", "IBMQ.load_account();" ] }, { "cell_type": "code", "execution_count": 6, "id": "d93c23d6", "metadata": {}, "outputs": [], "source": [ "provider = IBMQ.get_provider(group='deployed')" ] }, { "cell_type": "code", "execution_count": 276, "id": "59a3e697", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'sample-expval'" ] }, "execution_count": 276, "metadata": {}, "output_type": "execute_result" } ], "source": [ "program_id = provider.runtime.upload_program(data='sample_expval.py', metadata=meta)\n", "program_id" ] }, { "cell_type": "markdown", "id": "58430b3e", "metadata": {}, "source": [ "### Delete program if needed" ] }, { "cell_type": "code", "execution_count": 255, "id": "4ec662be", "metadata": {}, "outputs": [], "source": [ "#provider.runtime.delete_program(program_id)" ] }, { "cell_type": "markdown", "id": "4e304be8", "metadata": {}, "source": [ "## Making the program public\n", "\n", "If you want to make your program(s) public for the whole world to see you can do so by calling" ] }, { "cell_type": "code", "execution_count": 298, "id": "4b920893", "metadata": {}, "outputs": [], "source": [ "#provider.runtime.set_program_visibility(program_id=program_id, public=True)" ] }, { "cell_type": "markdown", "id": "8542a282", "metadata": {}, "source": [ "## Wrapping the runtime program\n", "\n", "As always, it is best to wrap the call to the runtime program with a function (or possibly a class) that makes input easier and does some validation." ] }, { "cell_type": "code", "execution_count": 7, "id": "c685ef09", "metadata": {}, "outputs": [], "source": [ "def expectation_value_runner(backend,\n", " circuits,\n", " expectation_operators='',\n", " shots = 8192,\n", " transpiler_config={},\n", " run_config={},\n", " skip_transpilation=False,\n", " return_stddev=False,\n", " use_measurement_mitigation=False):\n", " \n", " \"\"\"Compute expectation values for a list of operators after\n", " executing a list of circuits on the target backend.\n", " \n", " Parameters:\n", " backend (Backend or str): Qiskit backend instance or name.\n", " circuits: (QuantumCircuit or list): A single or list of QuantumCircuits.\n", " expectation_operators (str or dict or list): Expectation values to evaluate.\n", " shots (int): Number of shots to take per circuit.\n", " transpiler_config (dict): A collection of kwargs passed to transpile().\n", " run_config (dict): A collection of kwargs passed to backend.run().\n", " return_stddev (bool): Return upper bound on standard devitation,\n", " default=False. \n", " skip_transpilation (bool): Skip transpiling of circuits, default=False.\n", " use_measurement_mitigation (bool): Improve resulting using measurement\n", " error mitigation, default=False.\n", " \n", " Returns:\n", " array_like: Returns array of expectation values or a list of (expval, stddev)\n", " pairs if return_stddev=True.\n", " \"\"\"\n", " if not isinstance(backend, str):\n", " backend = backend.name()\n", " options = {'backend_name': backend}\n", " \n", " if isinstance(circuits, list) and len(circuits) != 1:\n", " if isinstance(expectation_operators, list):\n", " if len(circuits) != 1 and len(expectation_operators) == 1:\n", " expectation_operators = expectation_operators*len(circuits)\n", " elif len(circuits) != len(expectation_operators): \n", " raise ValueError('Number of circuits must match number of expectation \\\n", " values if more than one of each')\n", " inputs = {}\n", " inputs['circuits'] = circuits\n", " inputs['expectation_operators'] = expectation_operators\n", " inputs['shots'] = shots\n", " inputs['transpiler_config'] = transpiler_config\n", " inputs['run_config'] = run_config\n", " inputs['return_stddev'] = return_stddev\n", " inputs['skip_transpilation'] = skip_transpilation\n", " inputs['use_measurement_mitigation'] = use_measurement_mitigation\n", " \n", " return provider.runtime.run('sample-expval', options=options, inputs=inputs)" ] }, { "cell_type": "markdown", "id": "766a8961", "metadata": {}, "source": [ "### Trying it out\n", "\n", "Because we made our program public anyone can try it out. Lets do so here with our previously made GHZ state and running on the simulator." ] }, { "cell_type": "code", "execution_count": 8, "id": "b9f2a955", "metadata": {}, "outputs": [], "source": [ "backend = provider.backend.ibmq_qasm_simulator\n", "\n", "all_zeros_proj = {'0000': 1}\n", "all_ones_proj = {'1111': 1}\n", "job = expectation_value_runner(backend, qc, [all_zeros_proj, all_ones_proj, 'ZZZZ'])" ] }, { "cell_type": "code", "execution_count": 9, "id": "beb8550b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.50012207, 0.49987793, 1. ])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "job.result()" ] }, { "cell_type": "markdown", "id": "29268312", "metadata": {}, "source": [ "The first two projectors should be nearly $0.50$ as they tell use the probability of being in the all zeros and ones states, respectively, which should be 50/50 for our GHZ state. The final expectation value of `ZZZZ` should be one since this is a GHZ over an even number of qubits. It should go close to zero for an odd number." ] }, { "cell_type": "code", "execution_count": 10, "id": "5494d586", "metadata": {}, "outputs": [], "source": [ "qc2 = QuantumCircuit(3)\n", "qc2.h(2)\n", "qc2.cx(2, 1)\n", "qc2.cx(1, 0)\n", "qc2.measure_all()" ] }, { "cell_type": "code", "execution_count": 11, "id": "2c326f9d", "metadata": {}, "outputs": [], "source": [ "all_zeros_proj = {'000': 1}\n", "all_ones_proj = {'111': 1}\n", "job2 = expectation_value_runner(backend, qc2, [all_zeros_proj, all_ones_proj, 'ZZZ'])" ] }, { "cell_type": "code", "execution_count": 12, "id": "58d9a637", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.50415039, 0.49584961, 0.00830078])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "job2.result()" ] }, { "cell_type": "markdown", "id": "05ff6e0d", "metadata": {}, "source": [ "## Quantum Volume as an expectation value\n", "\n", "Here we formulate QV as an expectation value of a projector onto the heavy-output elements on a distribution. We can then use our expectation value routine to compute whether a given circuit has passed the QV metric.\n", "\n", "QV is defined in terms of heavy-ouputs of a distribution. Heavy-outputs are those bit-strings that are those that have probabilities above the median value of the distribution. Below we define the projection operator onto the set of bit-strings that are heavy-outputs for a given distribution." ] }, { "cell_type": "code", "execution_count": 13, "id": "778b6a77", "metadata": {}, "outputs": [], "source": [ "def heavy_projector(qv_probs):\n", " \"\"\"Forms the projection operator onto the heavy-outputs of a given probability distribution.\n", "\n", " Parameters:\n", " qv_probs (dict): A dictionary of bitstrings and associated probabilities.\n", "\n", " Returns:\n", " dict: Projector onto the heavy-set.\n", " \"\"\"\n", " median_prob = np.median(list(qv_probs.values()))\n", " heavy_strs = {}\n", " for key, val in qv_probs.items():\n", " if val > median_prob:\n", " heavy_strs[key] = 1\n", " return heavy_strs" ] }, { "cell_type": "markdown", "id": "1c05e1b2", "metadata": {}, "source": [ "Now we generate 10 QV circuits as our dataset." ] }, { "cell_type": "code", "execution_count": 16, "id": "20fab8af", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from qiskit.quantum_info import Statevector\n", "from qiskit.circuit.library import QuantumVolume\n", "# Generate QV circuits\n", "N = 10\n", "qv_circs = [QuantumVolume(5) for _ in range(N)]" ] }, { "cell_type": "markdown", "id": "927a0946", "metadata": {}, "source": [ "Next, we have to determine the heavy-set of each circuit from the ideal answer, and then pass this along to our heavy-set projector function that we defined above." ] }, { "cell_type": "code", "execution_count": 17, "id": "73d822cc", "metadata": {}, "outputs": [], "source": [ "ideal_probs = [Statevector.from_instruction(circ).probabilities_dict() for circ in qv_circs]\n", "heavy_projectors = [heavy_projector(probs) for probs in ideal_probs]" ] }, { "cell_type": "markdown", "id": "33559bc5", "metadata": {}, "source": [ "QV circuits have no meaasurements on them so need to add them:" ] }, { "cell_type": "code", "execution_count": 19, "id": "bf818575", "metadata": {}, "outputs": [], "source": [ "circs = [circ.measure_all(inplace=False) for circ in qv_circs]" ] }, { "cell_type": "markdown", "id": "dd9579fb", "metadata": {}, "source": [ "With a list of circuits and projection operators we now need only to pass both sets to our above expection value runner targeting the desired backend. We will also set the best transpiler arguments to give us a sporting chance of getting some passing scores." ] }, { "cell_type": "code", "execution_count": 18, "id": "8d692921", "metadata": {}, "outputs": [], "source": [ "backend = provider.backend.ibmq_manila" ] }, { "cell_type": "code", "execution_count": 36, "id": "5c169661", "metadata": {}, "outputs": [], "source": [ "job3 = expectation_value_runner(backend, circs, heavy_projectors,\n", " transpiler_config={'optimization_level':3, 'layout_method': 'sabre',\n", " 'routing_method': 'sabre'})" ] }, { "cell_type": "code", "execution_count": 37, "id": "98f6efd9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.52392578, 0.60400391, 0.57189941, 0.57897949, 0.7734375 ,\n", " 0.65844727, 0.56225586, 0.73706055, 0.69030762, 0.61193848])" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "qv_scores = job3.result()\n", "qv_scores" ] }, { "cell_type": "markdown", "id": "947b5149", "metadata": {}, "source": [ "A passing QV score is one where the value of the heavy-set projector is above $2/3$. So let us see who passed:" ] }, { "cell_type": "code", "execution_count": 38, "id": "3f6394d6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([False, False, False, False, True, False, False, True, True,\n", " False])" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "qv_scores > 2/3" ] }, { "cell_type": "code", "execution_count": 39, "id": "0a8fe223", "metadata": {}, "outputs": [ { "data": { "text/html": [ "

This code is a part of Qiskit

© Copyright IBM 2017, 2021.

This code is licensed under the Apache License, Version 2.0. You may
obtain a copy of this license in the LICENSE.txt file in the root directory
of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.

Any modifications or derivative works of this code must retain this
copyright notice, and modified files need to carry a notice indicating
that they have been altered from the originals.

" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from qiskit.tools.jupyter import *\n", "%qiskit_copyright" ] }, { "cell_type": "code", "execution_count": null, "id": "5c482e31", "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.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }