{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "255dc213-977b-46de-86f4-3bab0a005340",
      "metadata": {},
      "source": [
        "---\n",
        "title: Qubit initialization\n",
        "description: Explanatory content about dynamic repetition rate execution\n",
        "---\n",
        "\n",
        "# Qubit initialization\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "6e5f6e09-ed60-4b74-a2fd-72da40d8645e",
      "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": "abdfb100-5321-41d7-8aee-117062aed595",
      "metadata": {},
      "source": [
        "When a circuit is executed on an IBM® quantum processing unit (QPU), an implicit reset is typically inserted at the beginning of the circuit to ensure the qubits are initialized to zero. This is controlled by the `init_qubits` flag, set as a [primitive execution option](/docs/api/qiskit-ibm-runtime/options-execution-options-v2).\n",
        "\n",
        "However, imperfections in the reset process can introduce state-preparation errors. To alleviate the error, the QPU also inserts repetition delay time (or `rep_delay`) between circuits. Each backend has a different default `rep_delay`, but it's usually set to balance reset fidelity against total execution time. Run `backend.default_rep_delay` to find the default `rep_delay` for a specific QPU.\n",
        "\n",
        "Because all IBM QPUs use dynamic repetition rate execution, you can change the `rep_delay` for each job. Circuits you submit in a primitive job are batched together for execution on the QPU. These circuits are executed by iterating over the circuits for each shot requested; the execution is column-wise over a matrix of circuits and shots, as illustrated in the following figure.\n",
        "\n",
        "![The first column represents shot 0.  The circuits are run in order from 0 through 3.  The second column represents shot 1.  The circuits are run in order from 0 through 3.  The remaining columns follow the same pattern. ](https://quantum.cloud.ibm.com/docs/images/guides/repetition-rate-execution/circuits_shots_matrix1.avif \"Column-wise execution matrix\")\n",
        "\n",
        "Because `rep_delay` is inserted between circuits, each shot of the execution encounters this delay. Therefore, as you lower the `rep_delay`, the total QPU execution time decreases, at the expense of increasing the state preparation error rate, as the following image illustrates:\n",
        "\n",
        "![This image shows that as the rep\\_delay value is lowered, the state preparation error rate increases.](https://quantum.cloud.ibm.com/docs/images/guides/repetition-rate-execution/rep_delay.avif \"Repetition delay versus error rate\")\n",
        "\n",
        "If you set both `rep_delay=0` and `init_qubits=False`, the circuits \"merge\" together, since the qubits will begin in the final state from the previous shot.\n",
        "\n",
        "Note that while circuits in a primitive job are batched together for QPU execution, there is no guarantee on the order the circuits from PUBs are executed. For example, if you submit `pubs=[pub1, pub2]`, circuits from `pub1` might not run before those from `pub2`. There is also no guarantee that circuits from the same job would run as a single batch on the QPU.\n",
        "\n",
        "## Specify `rep_delay` for a primitive job\n",
        "\n",
        "<Admonition type=\"note\" title=\"Verify the rep_delay value for a QPU\">\n",
        "  Always verify the supported `rep_delay` range for the specific QPU you are using. These values are not the same for every QPU and can also change over time.\n",
        "\n",
        "  Please note that an increase in `rep_delay` will have a direct impact on your execution time and capacity consumption.\n",
        "</Admonition>\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "id": "aca7f18c-ee37-4f6f-a6a2-fe389459bb9b",
      "metadata": {},
      "outputs": [],
      "source": [
        "from qiskit_ibm_runtime import QiskitRuntimeService, SamplerV2 as Sampler\n",
        "\n",
        "service = QiskitRuntimeService()\n",
        "\n",
        "# Make sure your backend supports it\n",
        "backend = service.least_busy(\n",
        "    operational=True, min_num_qubits=100, dynamic_reprate_enabled=True\n",
        ")\n",
        "\n",
        "# Determine the allowable range\n",
        "backend.rep_delay_range\n",
        "sampler = Sampler(mode=backend)\n",
        "\n",
        "# Specify a value in the supported range\n",
        "sampler.options.execution.rep_delay = 0.0005"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "53c33092-8d59-4422-b438-1331afbacf17",
      "metadata": {},
      "source": [
        "## Next steps\n",
        "\n",
        "<Admonition type=\"tip\" title=\"Recommendations\">\n",
        "  * Try an example in the [Quantum approximate optimization algorithm (QAOA)](/docs/tutorials/quantum-approximate-optimization-algorithm) tutorial.\n",
        "  * Review how to [get started with Estimator](/docs/guides/get-started-with-estimator).\n",
        "  * Review how to [get started with Sampler](/docs/guides/get-started-with-sampler).\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
}