{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "bed19311-f71d-4d8c-9a05-1b68fa72df29",
      "metadata": {},
      "source": [
        "---\n",
        "title: Specify Sampler options\n",
        "description: Specify options when building with the Sampler primitive.\n",
        "---\n",
        "\n",
        "# Specify Sampler options\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "3adc856a-0fcd-47e4-94d9-22b90bddc24f",
      "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",
        "<details>\n",
        "  <summary><b>Package versions</b></summary>\n",
        "\n",
        "  The code on this page was developed using the following requirements.\n",
        "  We recommend using these versions or newer.\n",
        "\n",
        "  ```\n",
        "  qiskit[all]~=2.3.0\n",
        "  qiskit-ibm-runtime~=0.43.1\n",
        "  ```\n",
        "</details>\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "56db929d-8b84-49b2-a98b-55d33f91ea30",
      "metadata": {},
      "source": [
        "You can use options to customize the Sampler primitive. This section focuses on how to specify Qiskit Runtime primitive options. While the interface of the primitives' `run()`  method is common across all implementations, their options are not. Consult the corresponding API references for information about the [`qiskit.primitives.BackendSamplerV2`](/docs/api/qiskit/qiskit.primitives.BackendSamplerV2) and [`qiskit_aer.primitives.SamplerV2`](https://qiskit.github.io/qiskit-aer/stubs/qiskit_aer.primitives.SamplerV2.html) options.\n",
        "\n",
        "<Admonition type=\"note\" title=\"Notes about specifying options in the Sampler primitives\">\n",
        "  * You can see the available options and update option values during or after primitive initialization.\n",
        "  * Use the `update()` method to apply changes to the `options` attribute.\n",
        "  * If you do not specify a value for an option, it is given a special value of `Unset` and the server defaults are used.\n",
        "  * The `options` attribute is the `dataclass` Python type.  You can use the built-in `asdict` method to convert it to a dictionary.\n",
        "</Admonition>\n",
        "\n",
        "<span id=\"pass-options\" />\n",
        "\n",
        "## Set Sampler options\n",
        "\n",
        "You can set options when initializing the primitive, after initializing the primitive, or (for `shots` only), in the `run()` method.\n",
        "\n",
        "### Primitive initialization\n",
        "\n",
        "You can pass in an instance of the options class or a dictionary when initializing Sampler, which then makes a copy of those options. Thus, changing the original dictionary or options instance doesn't affect the options owned by the primitive.\n",
        "\n",
        "#### Options class\n",
        "\n",
        "When creating an instance of the `SamplerV2` class, you can pass in an instance of the options class. Those options will then be applied when you use `run()` to perform the calculation.  Specify the options in this format:  `options.option.sub-option.sub-sub-option = choice`.  For example: `options.dynamical_decoupling.enable = True`\n",
        "\n",
        "See [`SamplerOptions`](/docs/api/qiskit-ibm-runtime/options-sampler-options) for full details about the options class.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "id": "50938cb3-3bee-4a28-9f30-1b73b1c6a70f",
      "metadata": {},
      "outputs": [],
      "source": [
        "from qiskit_ibm_runtime import QiskitRuntimeService\n",
        "from qiskit_ibm_runtime import SamplerV2 as Sampler\n",
        "from qiskit_ibm_runtime.options import SamplerOptions\n",
        "\n",
        "service = QiskitRuntimeService()\n",
        "backend = service.least_busy(operational=True, simulator=False)\n",
        "\n",
        "options = SamplerOptions(\n",
        "    dynamical_decoupling={\"enable\": True, \"sequence_type\": \"XpXm\"},\n",
        ")\n",
        "\n",
        "# or...\n",
        "options = SamplerOptions()\n",
        "options.dynamical_decoupling.enable = True\n",
        "options.dynamical_decoupling.sequence_type = \"XpXm\"\n",
        "\n",
        "sampler = Sampler(mode=backend, options=options)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "4d30fcfc-58b7-4cb4-9e2d-6addfb25e9c2",
      "metadata": {},
      "source": [
        "#### Dictionary\n",
        "\n",
        "You can specify options as a dictionary when initializing Sampler.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "id": "5849c541-c779-470e-8ab0-a6d0e1a14775",
      "metadata": {},
      "outputs": [],
      "source": [
        "from qiskit_ibm_runtime import QiskitRuntimeService\n",
        "from qiskit_ibm_runtime import SamplerV2 as Sampler\n",
        "\n",
        "service = QiskitRuntimeService()\n",
        "backend = service.least_busy(operational=True, simulator=False)\n",
        "\n",
        "# Setting options during primitive initialization\n",
        "sampler = Sampler(\n",
        "    backend,\n",
        "    options={\n",
        "        \"dynamical_decoupling\": {\n",
        "            \"enable\": True,\n",
        "            \"sequence_type\": \"XpXm\",\n",
        "        },\n",
        "    },\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "2417e82a-7bca-4365-a0ab-0a1304ac3f0f",
      "metadata": {},
      "source": [
        "### Update options after initialization\n",
        "\n",
        "You can specify the options in this format: `sampler.options.option.sub-option.sub-sub-option = choice` to take advantage of auto-complete, or use the `update()` method to make bulk updates.\n",
        "\n",
        "The `SamplerV2` options class ([`SamplerOptions`](/docs/api/qiskit-ibm-runtime/options-sampler-options)) does not need to be instantiated if you are setting options after initializing the primitive.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "id": "07353a3e-0226-4b68-b280-15ad51f11093",
      "metadata": {},
      "outputs": [],
      "source": [
        "from qiskit_ibm_runtime import QiskitRuntimeService\n",
        "from qiskit_ibm_runtime import SamplerV2 as Sampler\n",
        "\n",
        "service = QiskitRuntimeService()\n",
        "backend = service.least_busy(operational=True, simulator=False)\n",
        "\n",
        "sampler = Sampler(mode=backend)\n",
        "\n",
        "# Setting options after primitive initialization\n",
        "# This uses auto-complete.\n",
        "sampler.options.default_shots = 4000\n",
        "# This does bulk update.\n",
        "sampler.options.update(\n",
        "    default_shots=4000,\n",
        "    dynamical_decoupling={\"enable\": True, \"sequence_type\": \"XpXm\"},\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "3ca44016-acfe-4540-ab0e-659b9577c4eb",
      "metadata": {},
      "source": [
        "<span id=\"run-method\" />\n",
        "\n",
        "### Run() method\n",
        "\n",
        "The only values you can pass to `run()` are those defined in the interface.  That is, `shots`. This overwrites any value set for `default_shots` for the current run.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "c2423fcc-7f7f-4674-af6f-fc90f4b28b17",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<RuntimeJobV2('d7ans0b0g7hs73doium0', 'sampler')>"
            ]
          },
          "execution_count": 8,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from qiskit_ibm_runtime import QiskitRuntimeService\n",
        "from qiskit_ibm_runtime import SamplerV2 as Sampler\n",
        "from qiskit.circuit.library import random_iqp\n",
        "from qiskit.transpiler import generate_preset_pass_manager\n",
        "\n",
        "service = QiskitRuntimeService()\n",
        "backend = service.least_busy(operational=True, simulator=False)\n",
        "\n",
        "circuit1 = random_iqp(3)\n",
        "circuit1.measure_all()\n",
        "circuit2 = random_iqp(3)\n",
        "circuit2.measure_all()\n",
        "\n",
        "pass_manager = generate_preset_pass_manager(\n",
        "    optimization_level=3, backend=backend\n",
        ")\n",
        "\n",
        "transpiled1 = pass_manager.run(circuit1)\n",
        "transpiled2 = pass_manager.run(circuit2)\n",
        "\n",
        "sampler = Sampler(mode=backend)\n",
        "# Default shots to use if not specified in run()\n",
        "sampler.options.default_shots = 500\n",
        "# Sample two circuits at 128 shots each.\n",
        "sampler.run([transpiled1, transpiled2], shots=128)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "4bf67f60-ce62-4b95-b70c-b15f131be46d",
      "metadata": {},
      "source": [
        "### Special cases\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "857e40a1-de1d-46f0-9db8-db55a6954f47",
      "metadata": {},
      "source": [
        "<span id=\"shots\" />\n",
        "\n",
        "#### Shots\n",
        "\n",
        "The `SamplerV2.run` method accepts two arguments: a list of PUBs, each of which can specify a PUB-specific value for shots, and a shots keyword argument. These shot values are a part of the Sampler execution interface, and are independent of the Runtime Sampler's options.  They take precedence over any values specified as options in order to comply with the Sampler abstraction.\n",
        "\n",
        "However, if `shots` is not specified by any PUB or in the run keyword argument (or if they are all `None`), then the shots value from the options is used, most notably `default_shots`.\n",
        "\n",
        "To summarize, this is the order of precedence for specifying shots in the Sampler, for any particular PUB:\n",
        "\n",
        "1. If the PUB specifies shots, use that value.\n",
        "2. If the `shots` keyword argument is specified in `run`, use that value.\n",
        "3. If `twirling` is enabled  (True by default), then the product of `num_randomizations` and `shots_per_randomization`, as specified as  [`twirling` options](/docs/api/qiskit-ibm-runtime/options-twirling-options), is used.\n",
        "4. If `sampler.options.default_shots` is specified, use that value.\n",
        "\n",
        "Thus, if shots are specified in all possible places, the one with highest precedence (shots specified in the PUB) is used.\n",
        "\n",
        "Example:\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "id": "e06376f4-fcba-4d03-8cbc-c53ca02d81af",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<RuntimeJobV2('d5k96icjt3vs73ds5t0g', 'sampler')>"
            ]
          },
          "execution_count": 6,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from qiskit_ibm_runtime import QiskitRuntimeService\n",
        "from qiskit_ibm_runtime import SamplerV2 as Sampler\n",
        "from qiskit.circuit.library import random_iqp\n",
        "from qiskit.transpiler import generate_preset_pass_manager\n",
        "\n",
        "service = QiskitRuntimeService()\n",
        "backend = service.least_busy(operational=True, simulator=False)\n",
        "\n",
        "circuit1 = random_iqp(3)\n",
        "circuit1.measure_all()\n",
        "circuit2 = random_iqp(3)\n",
        "circuit2.measure_all()\n",
        "\n",
        "pass_manager = generate_preset_pass_manager(\n",
        "    optimization_level=3, backend=backend\n",
        ")\n",
        "\n",
        "transpiled1 = pass_manager.run(circuit1)\n",
        "transpiled2 = pass_manager.run(circuit2)\n",
        "\n",
        "\n",
        "# Setting shots during primitive initialization\n",
        "sampler = Sampler(mode=backend, options={\"default_shots\": 4096})\n",
        "\n",
        "# Setting options after primitive initialization\n",
        "# This uses auto-complete.\n",
        "sampler.options.default_shots = 2000\n",
        "\n",
        "# This does bulk update.  The value for default_shots is overridden if you specify shots with run() or in the PUB.\n",
        "sampler.options.update(\n",
        "    default_shots=1024, dynamical_decoupling={\"sequence_type\": \"XpXm\"}\n",
        ")\n",
        "\n",
        "# Sample two circuits at 128 shots each.\n",
        "sampler.run([transpiled1, transpiled2], shots=128)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "29451d36-d88d-4726-96d2-f3083ac9e4a9",
      "metadata": {},
      "source": [
        "<span id=\"options-table\" />\n",
        "\n",
        "## Available options\n",
        "\n",
        "The following table documents options from the latest version of `qiskit-ibm-runtime`. To see older option versions, visit the [`qiskit-ibm-runtime` API reference](/docs/api/qiskit-ibm-runtime) and select a previous version.\n",
        "\n",
        "<Accordion>\n",
        "  <AccordionItem title=\"`default_shots`\">\n",
        "    The total number of shots to use per circuit per configuration.\n",
        "\n",
        "    **Choices**: Integer >= 0\n",
        "\n",
        "    **Default**: None\n",
        "\n",
        "    [`default_shots` API documentation](/docs/api/qiskit-ibm-runtime/options-sampler-options#default_shots)\n",
        "  </AccordionItem>\n",
        "\n",
        "  <AccordionItem title=\"`dynamical_decoupling`\">\n",
        "    Control dynamical decoupling error mitigation settings.\n",
        "\n",
        "    [`dynamical_decoupling` API documentation](/docs/api/qiskit-ibm-runtime/options-sampler-options#dynamical_decoupling)\n",
        "\n",
        "    <Accordion>\n",
        "      <AccordionItem title=\"`dynamical_decoupling.enable`\">\n",
        "        **Choices**: `True`, `False`\n",
        "\n",
        "        **Default**: `False`\n",
        "      </AccordionItem>\n",
        "\n",
        "      <AccordionItem title=\"`dynamical_decoupling.extra_slack_distribution`\">\n",
        "        **Choices**: `middle`, `edges`\n",
        "\n",
        "        **Default**: `middle`\n",
        "      </AccordionItem>\n",
        "\n",
        "      <AccordionItem title=\"`dynamical_decoupling.scheduling_method`\">\n",
        "        Choices: `asap`, `alap`\n",
        "        Default: `alap`\n",
        "      </AccordionItem>\n",
        "\n",
        "      <AccordionItem title=\"`dynamical_decoupling.sequence_type`\">\n",
        "        Choices: `XX`, `XpXm`, `XY4`\n",
        "        Default: `XX`\n",
        "      </AccordionItem>\n",
        "\n",
        "      <AccordionItem title=\"`dynamical_decoupling.skip_reset_qubits`\">\n",
        "        Choices: `True`, `False`\n",
        "        Default: `False`\n",
        "      </AccordionItem>\n",
        "    </Accordion>\n",
        "  </AccordionItem>\n",
        "\n",
        "  <AccordionItem title=\"`environment`\">\n",
        "    [`environment` API documentation](/docs/api/qiskit-ibm-runtime/options-sampler-options#environment)\n",
        "\n",
        "    <Accordion>\n",
        "      <AccordionItem title=\"`environment.job_tags`\">\n",
        "        List of tags.\n",
        "\n",
        "        **Choices**: None\n",
        "\n",
        "        **Default**: None\n",
        "      </AccordionItem>\n",
        "\n",
        "      <AccordionItem title=\"`environment.log_level`\">\n",
        "        **Choices**: DEBUG, INFO, WARNING, ERROR, CRITICAL\n",
        "\n",
        "        **Default**: WARNING\n",
        "      </AccordionItem>\n",
        "\n",
        "      <AccordionItem title=\"`environment.private`\">\n",
        "        **Choices**: `True`, `False`\n",
        "\n",
        "        **Default**: `False`\n",
        "      </AccordionItem>\n",
        "    </Accordion>\n",
        "  </AccordionItem>\n",
        "\n",
        "  <AccordionItem title=\"`execution`\">\n",
        "    [`execution` API documentation](/docs/api/qiskit-ibm-runtime/options-sampler-options#execution)\n",
        "\n",
        "    <Accordion>\n",
        "      <AccordionItem title=\"`execution.init_qubits`\">\n",
        "        Whether to reset the qubits to the ground state for each shot.\n",
        "\n",
        "        **Choices**: `True`, `False`\n",
        "\n",
        "        **Default**: `True`\n",
        "      </AccordionItem>\n",
        "\n",
        "      <AccordionItem title=\"`execution.rep_delay`\">\n",
        "        The delay between a measurement and the subsequent quantum circuit.\n",
        "\n",
        "        **Choices**: Value in the range supplied by `backend.rep_delay_range`\n",
        "\n",
        "        **Default**: Given by `backend.default_rep_delay`\n",
        "      </AccordionItem>\n",
        "\n",
        "      <AccordionItem title=\"`execution.meas_type`\">\n",
        "        **Choices**: `classified`, `kerneled`, `avg_kerneled`\n",
        "\n",
        "        **Default**: `classified`\n",
        "      </AccordionItem>\n",
        "    </Accordion>\n",
        "  </AccordionItem>\n",
        "\n",
        "  <AccordionItem title=\"`max_execution_time`\">\n",
        "    Limits how long a job can run, in seconds. See the [maximum execution time](/docs/guides/max-execution-time) guide for details.\n",
        "\n",
        "    **Choices**: Integer number of seconds in the range \\[1, 10800]\n",
        "\n",
        "    **Default**: 10800 (3 hours)\n",
        "\n",
        "    [`max_execution_time` API documentation](/docs/api/qiskit-ibm-runtime/options-sampler-options#max_execution_time)\n",
        "  </AccordionItem>\n",
        "\n",
        "  <AccordionItem title=\"`simulator`\">\n",
        "    Options to pass when simulating a backend\n",
        "\n",
        "    [`simulator` API documentation](/docs/api/qiskit-ibm-runtime/options-simulator-options)\n",
        "\n",
        "    <Accordion>\n",
        "      <AccordionItem title=\"`simulator.basis_gates`\">\n",
        "        **Choices**: List of basis gate names to unroll to\n",
        "\n",
        "        **Default**: The set of all basis gates supported by [Qiskit Aer simulator](https://qiskit.github.io/qiskit-aer/stubs/qiskit_aer.AerSimulator.html)\n",
        "      </AccordionItem>\n",
        "\n",
        "      <AccordionItem title=\"`simulator.coupling_map`\">\n",
        "        **Choices**: List of directed two-qubit interactions\n",
        "\n",
        "        **Default**: None, which implies no connectivity constraints (full connectivity).\n",
        "      </AccordionItem>\n",
        "\n",
        "      <AccordionItem title=\"`simulator.noise_model`\">\n",
        "        **Choices**: [Qiskit Aer NoiseModel](/docs/guides/build-noise-models), or its representation\n",
        "\n",
        "        **Default**: None\n",
        "      </AccordionItem>\n",
        "\n",
        "      <AccordionItem title=\"`simulator.seed_simulator`\">\n",
        "        **Choices**: Integer\n",
        "\n",
        "        **Default**: None\n",
        "      </AccordionItem>\n",
        "    </Accordion>\n",
        "  </AccordionItem>\n",
        "\n",
        "  <AccordionItem title=\"`twirling`\">\n",
        "    Twirling options\n",
        "\n",
        "    [`twirling` API documentation](/docs/api/qiskit-ibm-runtime/options-twirling-options)\n",
        "\n",
        "    <Accordion>\n",
        "      <AccordionItem title=\"`twirling.enable_gates`\">\n",
        "        **Choices**: True, False\n",
        "\n",
        "        **Default**: False\n",
        "      </AccordionItem>\n",
        "\n",
        "      <AccordionItem title=\"`twirling.enable_measure`\">\n",
        "        **Choices**: True, False\n",
        "\n",
        "        **Default**: False\n",
        "      </AccordionItem>\n",
        "\n",
        "      <AccordionItem title=\"`twirling.num_randomizations`\">\n",
        "        **Choices**: `auto`, Integer >= 1\n",
        "\n",
        "        **Default**: `auto`\n",
        "      </AccordionItem>\n",
        "\n",
        "      <AccordionItem title=\"`twirling.shots_per_randomization`\">\n",
        "        **Choices**: `auto`, Integer >= 1\n",
        "\n",
        "        **Default**: `auto`\n",
        "      </AccordionItem>\n",
        "\n",
        "      <AccordionItem title=\"`twirling.strategy`\">\n",
        "        **Choices**: `active`, `active-circuit`, `active-accum`, `all`\n",
        "\n",
        "        **Default**: `active-accum`\n",
        "      </AccordionItem>\n",
        "    </Accordion>\n",
        "  </AccordionItem>\n",
        "\n",
        "  <AccordionItem title=\"`experimental`\">\n",
        "    Experimental options, when available.\n",
        "  </AccordionItem>\n",
        "</Accordion>\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "80737dfd-a1c7-4b5f-b1ea-3391dcfa61b7",
      "metadata": {},
      "source": [
        "<span id=\"options-compatibility-table\" />\n",
        "\n",
        "## Feature compatibility\n",
        "\n",
        "Certain runtime features cannot be used together in a single job. Click the appropriate tab for a list of features that are incompatible with the selected feature:\n",
        "\n",
        "<Tabs>\n",
        "  <TabItem value=\"Dynamic\" label=\"Dynamic circuits\">\n",
        "    Incompatible with:\n",
        "\n",
        "    * Dynamical decoupling\n",
        "\n",
        "    Other notes:\n",
        "\n",
        "    * Gate twirling can be applied to dynamic circuits, but only to gates not inside conditional blocks. Measurement twirling can only be applied to terminal measurements.\n",
        "    * Compatible with fractional gates when using `qiskit-ibm-runtime` v0.42.0 or later.\n",
        "  </TabItem>\n",
        "\n",
        "  <TabItem value=\"DD\" label=\"Dynamical decoupling\">\n",
        "    Incompatible with dynamic circuits.\n",
        "  </TabItem>\n",
        "\n",
        "  <TabItem value=\"Fractional\" label=\"Fractional gates\">\n",
        "    Incompatible with:\n",
        "\n",
        "    * Gate twirling\n",
        "\n",
        "    Compatible with dynamic circuits when using `qiskit-ibm-runtime` v0.42.0 or later.\n",
        "  </TabItem>\n",
        "\n",
        "  <TabItem value=\"Twirling\" label=\"Gate twirling\">\n",
        "    Incompatible with fractional gates or with stretches.\n",
        "\n",
        "    Other notes:\n",
        "\n",
        "    * Gate twirling can be applied to dynamic circuits, but only to gates not inside conditional blocks. Measurement twirling can only be applied to terminal measurements.\n",
        "    * Does not work with non-Clifford entanglers.\n",
        "  </TabItem>\n",
        "</Tabs>\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "b95e9e8e-575c-411e-8504-4ef0a00166d3",
      "metadata": {},
      "source": [
        "## Next steps\n",
        "\n",
        "<Admonition type=\"tip\" title=\"Recommendations\">\n",
        "  * Find more details about the `SamplerV2` methods in the [Sampler API reference](../api/qiskit-ibm-runtime/sampler-v2).\n",
        "  * Decide what [execution mode](execution-modes) to run your job in.\n",
        "  * Learn about [noise management with Sampler](/docs/guides/sampler-noise-management).\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
}