{ "cells": [ { "cell_type": "markdown", "id": "5b2ac00e-c7fc-45cc-888e-8c26dabf80d2", "metadata": {}, "source": [ "# Readout Error Mitigation\n", "\n", "Readout errors are caused by imperfect qubit measurement and are a common source of error in quantum computing. Properly modelling these errors in simulation can give the user tools to understand better what these errors are and how to mitigate them when running on actual quantum devices.\n", "\n", "Readout errors can be mitigated with a confusion matrix $A$. It is a square matrix of size $2^n \\times 2^n$ and tells about the probability of observing the state $|y\\rangle$ given the true state $|x\\rangle$. The confusion matrix characterizes the readout error of the device and is circuit-independent. Once $A$ is estimated, we can compute its pseudoinverse $A^+$ which can be applied to the noisy probability distribution $p_{\\text{noisy}}$ to obtain an adjusted quasi-probability distribution \n", "\n", "$$\n", "p_{\\text{mitigated}} = A^+ p_{\\text{noisy}}\n", "$$\n", "\n", "In this tutorial, we show how to build a confusion matrix with the following approaches.\n", "\n", "- Using a single qubit model\n", "- Using $k$ local confusion matrices\n", "- A full confusion matrix for each $2^n$ combination\n", "\n", "The last method works well for correcting correlated errors (which affect multiple qubits together). However, it becomes impractical for large numbers of qubits." ] }, { "cell_type": "code", "execution_count": 1, "id": "04b58854-7dab-4f1b-8c59-d8b9506bce44", "metadata": {}, "outputs": [], "source": [ "# Install the relevant packages.\n", "\n", "!pip install matplotlib==3.8.4 pandas==2.2.2 scipy==1.13.1 seaborn==0.13.2 -q" ] }, { "cell_type": "code", "execution_count": 2, "id": "710a2a46-4e55-4508-a0b7-21969aac6a87", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import cudaq\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "import pandas as pd\n", "import itertools\n", "from functools import reduce\n", "from scipy.optimize import minimize\n", "from typing import Union\n", "\n", "sns.set_style(\"dark\")\n", "\n", "cudaq.set_target(\"density-matrix-cpu\")\n", "seed = 42\n", "\n", "cudaq.set_random_seed(seed)\n", "np.random.seed(seed)" ] }, { "cell_type": "markdown", "id": "8eab761a-0a82-4e20-8cf8-54602cd05d6a", "metadata": {}, "source": [ "To model the readout error, we apply a bitflip channel on every qubit at the end of the circuit using an Identity gate. The probability of bitflip `probs` can be the same or different for all the qubits.\n", "\n", "