{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "d59934c8-76a1-4eda-97ba-4fcfb6f15776",
      "metadata": {},
      "source": [
        "---\n",
        "title: Configure noise management with Estimator\n",
        "description: How to configure noise management with the Qiskit Runtime Estimator primitive.\n",
        "---\n",
        "\n",
        "# Configure noise management with Estimator\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "8f3a25e1-b73d-48d1-8b82-9c57095c9108",
      "metadata": {
        "tags": [
          "version-info"
        ]
      },
      "source": [
        "{/*\n",
        "  DO NOT EDIT THIS CELL!!!\n",
        "  This cell's content is generated automatically by a script. Anything you add\n",
        "  here will be removed next time the notebook is run. To add new content, create\n",
        "  a new cell before or after this one.\n",
        "  */}\n",
        "\n",
        "<Accordion>\n",
        "  <AccordionItem title=\"Package versions\">\n",
        "    The code on this page was developed using the following requirements.\n",
        "    We recommend using these versions or newer.\n",
        "\n",
        "    ```\n",
        "    qiskit-ibm-runtime~=0.46.1\n",
        "    ```\n",
        "  </AccordionItem>\n",
        "</Accordion>\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "3f1ea669-4144-4071-a4e9-88e01056f465",
      "metadata": {},
      "source": [
        "There are several ways to manage noise, typically by using various error mitigation and error suppression techniques to avoid errors before they happen.  These techniques usually cause pre-processing overhead.  Therefore, it is important to achieve a balance between perfecting your results and ensuring that your job completes in a reasonable amount of time.\n",
        "\n",
        "Estimator supports the following noise management techniques. See [Error mitigation and suppression techniques](error-mitigation-and-suppression-techniques) for an explanation of each. See the [Custom error settings](#advanced-error) section for instructions to enable these techniques.\n",
        "\n",
        "* [dynamical decoupling](/docs/api/qiskit-ibm-runtime/options-dynamical-decoupling-options#dynamicaldecouplingoptions)\n",
        "* [Pauli twirling](/docs/api/qiskit-ibm-runtime/options-twirling-options)\n",
        "* [PEA](/docs/api/qiskit-ibm-runtime/options-zne-options)\n",
        "* [PEC](/docs/api/qiskit-ibm-runtime/options-resilience-options-v2#pec)\n",
        "* [`resilience_level`](/docs/api/qiskit-ibm-runtime/options-estimator-options#resilience_level)\n",
        "* [TREX](/docs/api/qiskit-ibm-runtime/options-resilience-options-v2#measure_mitigation)\n",
        "* [ZNE](/docs/api/qiskit-ibm-runtime/options-resilience-options-v2#zne)\n",
        "\n",
        "<span id=\"resilience\" />\n",
        "\n",
        "## Resilience level\n",
        "\n",
        "The `resilience_level` specifies how much resilience to build against\n",
        "errors. Higher levels generate more accurate results, at the expense of\n",
        "longer processing times. Resilience levels can be used to configure the\n",
        "cost/accuracy trade-off when applying noise management to your primitive\n",
        "query. Noise management reduces errors (bias) in results by processing\n",
        "the outputs from a collection, or ensemble, of related circuits. The\n",
        "degree of error reduction depends on the method applied. The resilience\n",
        "level abstracts the detailed choice of noise management method to allow\n",
        "users to reason about the cost/accuracy trade that is appropriate to\n",
        "their application.\n",
        "\n",
        "Given this, each level corresponds to a method or methods with\n",
        "increasing level of quantum sampling overhead to enable you experiment\n",
        "with different time-accuracy tradeoffs. The following table shows you\n",
        "which levels and corresponding methods are available for each of the\n",
        "primitives.\n",
        "\n",
        "<span id=\"resilience-table\" />\n",
        "\n",
        "| Resilience Level | Description                                                                                           | Technique                                                                                                                     |\n",
        "| ---------------- | ----------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- |\n",
        "| 0                | No mitigation                                                                                         | None                                                                                                                          |\n",
        "| 1 \\[Default]     | Minimal mitigation costs: Mitigate error associated with readout errors                               | [Twirled Readout Error eXtinction (TREX)](/docs/guides/error-mitigation-and-suppression-techniques#trex) measurement twirling |\n",
        "| 2                | Medium mitigation costs. Typically reduces bias in estimators, but is not guaranteed to be zero-bias. | Level 1 + [Zero Noise Extrapolation (ZNE)](/docs/guides/error-mitigation-and-suppression-techniques#zne) and gate twirling    |\n",
        "\n",
        "<Admonition type=\"info\" title=\"Attention\">\n",
        "  Resilience levels are currently in beta so sampling overhead and\n",
        "  solution quality will vary from circuit to circuit. New features,\n",
        "  advanced options, and management tools will be released on a rolling\n",
        "  basis. Specific noise management methods are not guaranteed to be\n",
        "  applied at each resilience level.\n",
        "</Admonition>\n",
        "\n",
        "### Configure Estimator with resilience levels\n",
        "\n",
        "You can use resilience levels to specify noise management techniques, or you can set custom techniques individually as described in [Custom error settings](#advanced-error).\n",
        "\n",
        "<Admonition type=\"note\" title=\"Important\">\n",
        "  Any options you manually specify in addition to the resilience level are applied in addition to the base set of options defined by the resilience level. Therefore, in principle, you could set the resilience level to 1, but then turn off measurement mitigation, although this is not advised.\n",
        "\n",
        "  For example, setting the resilience level to 0 turns off `zne_mitigation`, but `estimator.options.resilience.zne_mitigation = True` overrides that value.\n",
        "</Admonition>\n",
        "\n",
        "### Example\n",
        "\n",
        "The following code enables ZNE, TREX, and gate twirling by\n",
        "setting `resilience_level 2`.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "id": "841b526d-55b8-4125-bd2c-758d9db8dbb3",
      "metadata": {},
      "outputs": [],
      "source": [
        "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",
        "# Setting options during primitive initialization\n",
        "estimator = Estimator(backend, options={\"resilience_level\": 2})"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "c497a0e5-bcb7-4954-8096-70b4514ea498",
      "metadata": {},
      "source": [
        "<span id=\"advanced-error\" />\n",
        "\n",
        "## Custom noise management settings\n",
        "\n",
        "You can turn on and off individual noise management methods by using the [Estimator options](/docs/guides/estimator-options).\n",
        "\n",
        "<Admonition type=\"note\">\n",
        "  Not all options work together on all types of circuits.  See the [feature compatibility table](estimator-options#options-compatibility-table) for details.\n",
        "</Admonition>\n",
        "\n",
        "### Example\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "id": "89f1827d-92a1-40a4-bd71-6d67a18f0932",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            ">>> gate twirling is turned on: True\n",
            ">>> measurement error mitigation is turned on: True\n"
          ]
        }
      ],
      "source": [
        "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",
        "estimator = Estimator(backend)\n",
        "options = estimator.options\n",
        "# Turn on gate twirling.\n",
        "options.twirling.enable_gates = True\n",
        "# Turn on measurement error mitigation.\n",
        "options.resilience.measure_mitigation = True\n",
        "\n",
        "print(\n",
        "    f\">>> gate twirling is turned on: {estimator.options.twirling.enable_gates}\"\n",
        ")\n",
        "print(\n",
        "    f\">>> measurement error mitigation is turned on: {estimator.options.resilience.measure_mitigation}\"\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "0eba7825-2725-4406-82b7-3911c33957c5",
      "metadata": {},
      "source": [
        "## Turn off all error mitigation\n",
        "\n",
        "For instructions to turn off all error mitigation see the [Turn off all error suppression and mitigation](estimator-options#no-error-mitigation) section in the Estimator options guide.\n",
        "\n",
        "## Next steps\n",
        "\n",
        "<Admonition type=\"tip\" title=\"Recommendations\">\n",
        "  * Walk through an example that uses error mitigation in the [Cost function lesson](/learning/courses/variational-algorithm-design/cost-functions) in IBM Quantum Learning.\n",
        "  * Learn more about [error mitigation and error suppression techniques](error-mitigation-and-suppression-techniques).\n",
        "  * Explore [Estimator options](/docs/guides/estimator-options).\n",
        "  * Decide what [execution mode](/docs/guides/execution-modes) to run your job in.\n",
        "</Admonition>\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "id": "a1b8767d",
      "source": "© IBM Corp., 2017-2026"
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "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"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 4
}