{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "0998165a-381f-4561-a79a-bf584aed9687",
      "metadata": {},
      "source": [
        "---\n",
        "title: Estimator with the REST API\n",
        "description: How to use the Estimator primitive with the Qiskit Runtime REST API.\n",
        "---\n",
        "\n",
        "{/* cspell:ignore IIZII, XIZZZ, accum */}\n",
        "\n",
        "# Estimator with the REST API\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "da948b6b-2ead-4359-aed4-b824e43ccbfb",
      "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",
        "  ```\n",
        "</details>\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "3d9ef591-91fd-4a49-a065-5e2cda4d41be",
      "metadata": {},
      "source": [
        "The steps in this topic describe how to run and configure workloads with the REST API, and demonstrate how to invoke them in any program of your choice.\n",
        "\n",
        "<Admonition type=\"note\">\n",
        "  This documentation utilizes the Python `requests` module to demonstrate the Qiskit Runtime REST API. However, this workflow can be executed using any language or framework that supports working with REST APIs. Refer to the [API reference documentation](/docs/api/qiskit-ibm-runtime/tags/jobs) for details.\n",
        "</Admonition>\n",
        "\n",
        "## 1. Initialize the account\n",
        "\n",
        "Because Qiskit Runtime Estimator is a managed service, you first need to initialize your account. You can then select the device you want to use to calculate the expectation value.\n",
        "\n",
        "Find details on how to initialize your account, view available backends, and invalidate tokens in this [topic](/docs/guides/cloud-setup-rest-api).\n",
        "\n",
        "## 2. Create a QASM circuit\n",
        "\n",
        "You need at least one circuit as the input to the Estimator primitive.\n",
        "\n",
        "Define a QASM quantum circuit. For example:\n",
        "\n",
        "```python\n",
        "qasm_string='''\n",
        "OPENQASM 3;\n",
        "include \"stdgates.inc\";\n",
        "qreg q[2];\n",
        "creg c[2];\n",
        "x q[0];\n",
        "cx q[0], q[1];\n",
        "c[0] = measure q[0];\n",
        "c[1] = measure q[1];\n",
        "'''\n",
        "```\n",
        "\n",
        "The following code snippets assume that the `qasm_string` has been transpiled to a new string `resulting_qasm`.\n",
        "\n",
        "## 3. Run the quantum circuit using the Estimator V2 API\n",
        "\n",
        "<Admonition type=\"note\">\n",
        "  The following jobs use [Qiskit Runtime V2 primitives](/docs/guides/v2-primitives). Both `SamplerV2` and `EstimatorV2` take one or more primitive unified blocs (PUBs) as the input. Each PUB is a tuple that contains one circuit and the data broadcasted to that circuit, which can be multiple observables and parameters. Each PUB returns a result.\n",
        "</Admonition>\n",
        "\n",
        "```python\n",
        "import requests\n",
        "\n",
        "url = 'https://quantum.cloud.ibm.com/api/v1/jobs'\n",
        "auth_id = \"Bearer <YOUR_BEARER_TOKEN>\"\n",
        "crn = \"<SERVICE-CRN>\"\n",
        "backend = \"<BACKEND_NAME>\"\n",
        "\n",
        "headers = {\n",
        "    'Content-Type': 'application/json',\n",
        "    'Authorization':auth_id,\n",
        "    'Service-CRN': crn\n",
        "    }\n",
        "\n",
        "job_input = {\n",
        "    'program_id': 'estimator',\n",
        "    \"backend\": backend,\n",
        "    \"params\": {\n",
        "        \"pubs\": [ #primitive unified blocs (PUBs) containing one circuit each.\n",
        "            [resulting_qasm, # QASM circuit\n",
        "             {\"IIZII\": 1, \"XIZZZ\": 2.3}, # Observable\n",
        "             None # parameter values\n",
        "             ]]\n",
        "}}\n",
        "\n",
        "response = requests.post(url, headers=headers, json=job_input)\n",
        "\n",
        "if response.status_code == 200:\n",
        "    job_id = response.json().get('id')\n",
        "    print(\"Job created:\",response.text)\n",
        "else:\n",
        "    print(f\"Error: {response.status_code}\")\n",
        "```\n",
        "\n",
        "## 4. Check job status and get results\n",
        "\n",
        "Next, pass the `job_id` to the API:\n",
        "\n",
        "```python\n",
        "response_status_singlejob= requests.get(url+'/'+job_id, headers=headers)\n",
        "response_status_singlejob.json().get('state')\n",
        "```\n",
        "\n",
        "Output\n",
        "\n",
        "```text\n",
        ">>> Job ID: 58223448-5100-4dec-a47a-942fb30edcad\n",
        ">>> Job Status: JobStatus.RUNNING\n",
        "```\n",
        "\n",
        "Get job results:\n",
        "\n",
        "```python\n",
        "response_result= requests.get(url+'/'+job_id+'/results', headers=headers)\n",
        "\n",
        "res_dict=response_result.json()\n",
        "\n",
        "estimator_result=res_dict['results']\n",
        "print(estimator_result)\n",
        "```\n",
        "\n",
        "Output\n",
        "\n",
        "```text\n",
        "[{'data': {'evs': 0.7428980350102542, 'stds': 0.029884014518789213, 'ensemble_standard_error': 0.03261147170624149}, 'metadata': {'shots': 10016, 'target_precision': 0.01, 'circuit_metadata': {}, 'resilience': {}, 'num_randomizations': 32}}]\n",
        "```\n",
        "\n",
        "## 5. Work with Runtime options\n",
        "\n",
        "Error mitigation techniques allow users to mitigate circuit errors by modeling the device noise at the time of execution. This typically results in quantum pre-processing overhead related to model training, and classical post-processing overhead to mitigate errors in the raw results by using the generated model.\n",
        "\n",
        "The error mitigation techniques built in to primitives are advanced resilience options. To specify these options, use the `resilience_level` option when submitting your job.\n",
        "\n",
        "The following examples demonstrate the default options for dynamical decoupling, twirling, and TREX + ZNE. Find more options and further details in the [Error mitigation and suppression techniques](/docs/guides/error-mitigation-and-suppression-techniques) topic.\n",
        "\n",
        "### TREX + ZNE\n",
        "\n",
        "```python\n",
        "import requests\n",
        "\n",
        "url = 'https://quantum.cloud.ibm.com/api/v1/jobs'\n",
        "auth_id = \"Bearer <YOUR_BEARER_TOKEN>\"\n",
        "crn = \"<SERVICE-CRN>\"\n",
        "backend = \"BACKEND_NAME\"\n",
        "\n",
        "headers = {\n",
        "    'Content-Type': 'application/json',\n",
        "    'Authorization':auth_id,\n",
        "    'Service-CRN': crn\n",
        "    }\n",
        "job_input = {\n",
        "    'program_id': 'estimator',\n",
        "    \"backend\": backend,\n",
        "    \"params\": {\n",
        "        \"pubs\": [ #primitive unified blocs (PUBs) containing one circuit each\n",
        "            [resulting_qasm, # QASM circuit\n",
        "             {\"IIZII\": 1, \"XIZZZ\": 2.3}, # Observable\n",
        "             None # parameter values\n",
        "             ]]\n",
        "        \"options\": {\n",
        "            \"resilience\": {\n",
        "              \"measure_mitigation\": True,\n",
        "              \"zne_mitigation\": True,\n",
        "              \"zne\": {\n",
        "                  \"extrapolator\":[\"exponential\", \"linear\"],\n",
        "                  \"noise_factors\":[1, 3, 5],\n",
        "              },\n",
        "          },\n",
        "        },\n",
        "    }\n",
        "}\n",
        "\n",
        "response = requests.post(url, headers=headers, json=job_input)\n",
        "\n",
        "if response.status_code == 200:\n",
        "    job_id = response.json().get('id')\n",
        "    print(\"Job created:\",response.text)\n",
        "else:\n",
        "    print(f\"Error: {response.status_code}\")\n",
        "```\n",
        "\n",
        "### Dynamical Decoupling\n",
        "\n",
        "```python\n",
        "import requests\n",
        "\n",
        "url = 'https://quantum.cloud.ibm.com/api/v1/jobs'\n",
        "auth_id = \"Bearer <YOUR_BEARER_TOKEN>\"\n",
        "crn = \"<SERVICE-CRN>\"\n",
        "backend = \"BACKEND_NAME\"\n",
        "\n",
        "headers = {\n",
        "    'Content-Type': 'application/json',\n",
        "    'Authorization':auth_id,\n",
        "    'Service-CRN': crn\n",
        "    }\n",
        "job_input = {\n",
        "    'program_id': 'estimator',\n",
        "    \"backend\": backend,\n",
        "    \"params\": {\n",
        "        \"pubs\": [ #primitive unified blocs (PUBs) containing one circuit each\n",
        "            [resulting_qasm, # QASM circuit\n",
        "             {\"IIZII\": 1, \"XIZZZ\": 2.3}, # Observable\n",
        "             None # parameter values\n",
        "             ]]\n",
        "        \"options\": {\n",
        "            \"dynamical_decoupling\": {\n",
        "                \"enable\": True,\n",
        "                \"sequence_type\": 'XpXm',\n",
        "                \"extra_slack_distribution\": 'middle',\n",
        "                \"scheduling_method\": 'alap',\n",
        "            },\n",
        "        },\n",
        "    }\n",
        "}\n",
        "\n",
        "response = requests.post(url, headers=headers, json=job_input)\n",
        "\n",
        "if response.status_code == 200:\n",
        "    job_id = response.json().get('id')\n",
        "    print(\"Job created:\",response.text)\n",
        "else:\n",
        "    print(f\"Error: {response.status_code}\")\n",
        "```\n",
        "\n",
        "### Twirling\n",
        "\n",
        "```python\n",
        "import requests\n",
        "\n",
        "url = 'https://quantum.cloud.ibm.com/api/v1/jobs'\n",
        "auth_id = \"Bearer <YOUR_BEARER_TOKEN>\"\n",
        "crn = \"<SERVICE-CRN>\"\n",
        "backend = \"BACKEND_NAME\"\n",
        "\n",
        "headers = {\n",
        "    'Content-Type': 'application/json',\n",
        "    'Authorization':auth_id,\n",
        "    'Service-CRN': crn\n",
        "    }\n",
        "job_input = {\n",
        "    'program_id': 'estimator',\n",
        "    \"backend\": backend,\n",
        "    \"params\": {\n",
        "        \"pubs\": [ #primitive unified blocs (PUBs) containing one circuit each\n",
        "            [resulting_qasm, # QASM circuit\n",
        "             {\"IIZII\": 1, \"XIZZZ\": 2.3}, # Observable\n",
        "             None # parameter values\n",
        "             ]]\n",
        "        \"options\": {\n",
        "            \"twirling\": {\n",
        "                \"enable_gates\": True,\n",
        "                \"enable_measure\": True,\n",
        "                \"num_randomizations\": \"auto\",\n",
        "                \"shots_per_randomization\": \"auto\",\n",
        "                \"strategy\": \"active-accum\",\n",
        "                },\n",
        "        },\n",
        "    }\n",
        "}\n",
        "\n",
        "response = requests.post(url, headers=headers, json=job_input)\n",
        "\n",
        "if response.status_code == 200:\n",
        "    job_id = response.json().get('id')\n",
        "    print(\"Job created:\",response.text)\n",
        "else:\n",
        "    print(f\"Error: {response.status_code}\")\n",
        "```\n",
        "\n",
        "## Parameterized circuits\n",
        "\n",
        "### 1. Initialize the account\n",
        "\n",
        "Because Qiskit Runtime is a managed service, you first need to initialize your account. You can then select the device you want to use to run your calculations on.\n",
        "\n",
        "Find details on how to initialize your account, view available backends, and invalidate tokens in this [topic](/docs/guides/cloud-setup-rest-api).\n",
        "\n",
        "### 2. Define parameters\n",
        "\n",
        "```python\n",
        "import requests\n",
        "import qiskit_ibm_runtime\n",
        "from qiskit_ibm_runtime import QiskitRuntimeService\n",
        "from qiskit.transpiler import generate_preset_pass_manager\n",
        "from qiskit.qasm3 import dumps\n",
        "from qiskit import QuantumCircuit\n",
        "from qiskit.circuit import Parameter\n",
        "from qiskit import transpile\n",
        "\n",
        "service = QiskitRuntimeService(channel='ibm_quantum')\n",
        "backend = service.backend(\"<SPECIFY BACKEND>\")\n",
        "\n",
        "pm = generate_preset_pass_manager(backend=backend, optimization_level=1)\n",
        "\n",
        "theta = Parameter('theta')\n",
        "phi = Parameter('phi')\n",
        "parameter_values = {'theta': 1.57, 'phi': 3.14}   # In case we want to pass a dictionary\n",
        "```\n",
        "\n",
        "### 3. Create a quantum circuit and add parameterized gates\n",
        "\n",
        "```python\n",
        "qc = QuantumCircuit(2)\n",
        "\n",
        "# Add parameterized gates\n",
        "qc.rx(theta, 0)\n",
        "qc.ry(phi, 1)\n",
        "qc.cx(0, 1)\n",
        "qc.measure_all()\n",
        "\n",
        "# Draw the original circuit\n",
        "qc.draw('mpl')\n",
        "\n",
        "# Get an ISA circuit\n",
        "isa_circuit = pm.run(qc)\n",
        "```\n",
        "\n",
        "### 4. Generate QASM 3 code\n",
        "\n",
        "```python\n",
        "qasm_str = dumps(isa_circuit)\n",
        "print(\"Generated QASM 3 code:\")\n",
        "print(qasm_str)\n",
        "```\n",
        "\n",
        "### 5. Run the quantum circuit using Estimator V2 API\n",
        "\n",
        "```python\n",
        "import requests\n",
        "\n",
        "url = 'https://quantum.cloud.ibm.com/api/v1/jobs'\n",
        "auth_id = \"Bearer <YOUR_BEARER_TOKEN>\"\n",
        "crn = \"<SERVICE-CRN>\"\n",
        "backend = \"<BACKEND_NAME>\"\n",
        "\n",
        "headers = {\n",
        "    'Content-Type': 'application/json',\n",
        "    'Authorization':auth_id,\n",
        "    'Service-CRN': crn\n",
        "    }\n",
        "\n",
        "job_input = {\n",
        "    'program_id': 'estimator',\n",
        "    \"backend\": backend,\n",
        "    \"params\": {\n",
        "        # Choose one option: direct parameter transfer or through a dictionary\n",
        "        #\"pubs\": [[qasm_str,[1,2],500]], # primitive unified blocs (PUBs) containing one circuit each.\n",
        "        \"pubs\": [[qasm_str,parameter_values,500]], # primitive unified blocs (PUBs) containing one circuit each.\n",
        "}}\n",
        "\n",
        "response = requests.post(url, headers=headers, json=job_input)\n",
        "\n",
        "if response.status_code == 200:\n",
        "    job_id = response.json().get('id')\n",
        "    print(f\"Job created: {response.text}\")\n",
        "else:\n",
        "    print(f\"Error: {response.status_code}\")\n",
        "```\n",
        "\n",
        "```python\n",
        "print(response.text)\n",
        "```\n",
        "\n",
        "### 6. Check job status and get results\n",
        "\n",
        "Next, pass the `job_id` to the API:\n",
        "\n",
        "```python\n",
        "response_status_singlejob = requests.get(f\"{url}/{job_id}\", headers=headers)\n",
        "response_status_singlejob.json().get('state')\n",
        "```\n",
        "\n",
        "Output\n",
        "\n",
        "```text\n",
        "{'status': 'Completed'}\n",
        "```\n",
        "\n",
        "Get job results:\n",
        "\n",
        "```python\n",
        "response_result = requests.get(f\"{url}/{job_id}/results\", headers=headers)\n",
        "\n",
        "res_dict=response_result.json()\n",
        "\n",
        "# Get results for the first PUB\n",
        "counts=res_dict['results'][0]['data']['c']['samples']\n",
        "\n",
        "print(counts[:20])\n",
        "```\n",
        "\n",
        "Output\n",
        "\n",
        "```text\n",
        "['0x1', '0x2', '0x1', '0x2', '0x1', '0x2', '0x0', '0x2', '0x1', '0x1', '0x2', '0x2', '0x1', '0x1', '0x1', '0x1', '0x1', '0x1', '0x1', '0x1']\n",
        "```\n",
        "\n",
        "## Next steps\n",
        "\n",
        "<Admonition type=\"tip\" title=\"Recommendations\">\n",
        "  * There are several ways to run workloads, depending on your needs: job mode, session mode, and batch mode. Learn how to work with session mode and batch mode in the [execution modes topic](/docs/guides/execution-modes-rest-api). Note that Open Plan users cannot submit session jobs.\n",
        "  * Learn how to [initialize your account](/docs/guides/cloud-setup-rest-api) with REST API.\n",
        "  * Practice with primitives by working through the [Cost function lesson](/learning/courses/variational-algorithm-design/cost-functions) in IBM Quantum® Learning.\n",
        "  * Learn how to transpile locally in the [Transpile](/docs/guides/transpile) section.\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
}