{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "8cfa587b",
      "metadata": {},
      "source": [
        "---\n",
        "title: Build a Qiskit Function for chemistry simulation\n",
        "description: Learn how to deploy and execute the chemistry workflow template\n",
        "---\n",
        "\n",
        "# Deploy and run a template for electronic structure simulation with an implicit solvent model\n",
        "\n",
        "{/* cspell:ignore pvdz, fcisolver, avas, ncas, nelecas, ecore, chkfile, fcivec, hcore, ncore, myci, sqdvec, myeps, mymethod, mysolvmethod, myavas, mcscf, MCSCF, chkfile, prqs */}\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "f9bfec87",
      "metadata": {
        "tags": [
          "version-info"
        ]
      },
      "source": [
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "f3a337d1",
      "metadata": {},
      "source": [
        "This template, developed in collaboration with the Cleveland Clinic, consists of a workflow to calculate the ground state energy and solvation free energy of a molecule in an implicit solvent [\\[1\\]](#references). These simulations are based on the sample-based quantum diagonalization (SQD) method [\\[2-6\\]](#references) and the integral equation formalism polarizable continuum model (IEF-PCM) of solvent [\\[7\\]](#references).\n",
        "\n",
        "This guide utilizes the template with a methanol molecule as the solute, the electronic structure of which is simulated explicitly, and water as the solvent, approximated as a continuous dielectric medium. To account for the [electron correlation effects](https://onlinelibrary.wiley.com/doi/epdf/10.1002/ijch.202100111) in methanol, while maintaining the balance between the computational cost and accuracy, we only include the $\\sigma$, $\\sigma^{*}$, and lone pair orbitals in the active space simulated with SQD IEF-PCM. This orbital selection is done with [atomic valence active space (AVAS) method](https://github.com/pyscf/pyscf.github.io/blob/master/examples/mcscf/43-avas.py) using the C\\[2s,2p], O\\[2s,2p], and H\\[1s] atomic orbital components, which results in the active space of 14 electrons and 12 orbitals (14e,12o). The reference orbitals are calculated with closed-shell Hartree Fock using the cc-pvdz basis set.\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "25d18ba5",
      "metadata": {},
      "source": [
        "## Workflow introduction\n",
        "\n",
        "This interactive guide shows how to upload this function template to Qiskit Serverless and run an example workload. The template is structured as a Qiskit pattern with four steps:\n",
        "\n",
        "#### 1. Collect input and map the problem\n",
        "\n",
        "This step takes the geometry of the molecule, selected active space, solvation model, LUCJ options, and SQD options as an input. It then produces the PySCF Checkpoint file, which contains the Hartree-Fock (HF) IEF-PCM data. This data will be used in the SQD portion of the workflow. For the LUCJ portion of the workflow, the input section also generates the gas-phase HF data, which is stored internally in PySCF FCIDUMP format.\n",
        "\n",
        "The information from the HF gas-phase simulation and the definition of the active space are taken as input. Importantly, it also uses the user-defined information from the input section concerning the error suppression, number of shots, circuit transpiler optimization level, and the qubit layout.\n",
        "\n",
        "It generates one-electron and two-electron integrals within the defined active space. The integrals are then used to perform classical CCSD calculations, which return t2 amplitudes that we use to parametrize the LUCJ circuit.\n",
        "\n",
        "#### 2. Optimize the circuit\n",
        "\n",
        "The LUCJ circuit is then transpiled into an ISA circuit for the target hardware. A Sampler primitive is then instantiated with a default set of error mitigation options to manage the execution.\n",
        "\n",
        "#### 3. Execute the circuit\n",
        "\n",
        "The LUCJ calculations return the bitstrings for each measurement, where these bitstrings correspond to electron configurations of the studied system. The bitstrings are then used as input for post-processing.\n",
        "\n",
        "#### 4. Post-process by using SQD\n",
        "\n",
        "This final step takes the PySCF Checkpoint file containing the HF IEF-PCM information, the bitstrings representing the electron configurations predicted by LUCJ, and the user-defined SQD options selected in the input section as input. As  output, it produces the SQD IEF-PCM total energy of the lowest energy batch and the corresponding solvation free energy.\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "4832ef18",
      "metadata": {},
      "source": [
        "### Options\n",
        "\n",
        "For this template you must specify options for generating the LUCJ circuit, and SQD run parameters.\n",
        "\n",
        "#### LUCJ options\n",
        "\n",
        "When the LUCJ quantum circuit is executed, a set of samples that represent the computational basis states from the probability distribution of the molecular system are produced. To balance the depth of the LUCJ circuit and its expressibility, the qubits corresponding to the spin orbitals with the opposite spin have the two-qubit gates applied between them when these qubits are neighbors through a single ancilla qubit. To implement this approach on IBM hardware with a heavy-hex topology, qubits that represent the spin orbitals with the same spin are connected through a line topology where each line takes a zig-zag shape due to the heavy-hex connectivity of the target hardware, while the qubits that represent the spin orbitals with the opposite spin only have a connection at every fourth qubit.\n",
        "\n",
        "<Accordion>\n",
        "  <AccordionItem title=\"Click to expand for more details on the required options:\">\n",
        "    The user has to provide the `initial_layout` array corresponding to the qubits that satisfy this [*zig-zag* pattern](https://pubs.rsc.org/en/content/articlehtml/2023/sc/d3sc02516k) in the `lucj_options` section of the SQD IEF-PCM function. In case of SQD IEF-PCM (14e,12o)/cc-pvdz simulations of methanol, we chose the initial qubit layout corresponding to the main diagonal of the Eagle R3 QPU. Here, the first 12 elements of the `initial_layout` array `[0, 14, 18, 19, 20, 33, 39, 40, 41, 53, 60, 61, ...]` correspond to the alpha spin orbitals. The last 12 elements `[... 2, 3, 4, 15, 22, 23, 24, 34, 43, 44, 45, 54]` correspond to beta spin orbitals.\n",
        "\n",
        "    Importantly, the user has to determine the `number_of_shots`, which corresponds to the number of measurements in the LUCJ circuit. The number of shots needs to be sufficiently large because the first step of S-CORE procedure relies on the samples in the right particle sector to obtain the initial approximation to the ground-state occupation number distribution.\n",
        "\n",
        "    The number of shots is highly system- and hardware-dependent, but [non-covalent](https://arxiv.org/abs/2410.09209), [fragment-based](https://arxiv.org/abs/2411.09861), and [implicit solvent](https://pubs.acs.org/doi/10.1021/acs.jpcb.5c01030) SQD studies suggest that one can reach the chemical accuracy by following these guidelines:\n",
        "\n",
        "    * 20,000 - 200,000 shots for systems with fewer than 16 molecular orbitals (32 spin orbitals)\n",
        "    * 200,000 shots for systems with 16 - 18 molecular orbitals\n",
        "    * 200,000 - 2,000,000 shots for systems with more than 18 molecular orbitals\n",
        "\n",
        "    The required number of shots is affected by the number of spin orbitals in the studied system and by the size of the Hilbert space corresponding to the selected active space within the studied system. Generally, instances with smaller Hilbert spaces require fewer shots. Other available LUCJ options are [circuit transpiler optimization level](https://docs.quantum.ibm.com/guides/set-optimization) and [error suppression options](https://docs.quantum.ibm.com/guides/error-mitigation-and-suppression-techniques). Note that these options also affect the required number of shots and the resulting accuracy.\n",
        "  </AccordionItem>\n",
        "</Accordion>\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "e2b46498",
      "metadata": {},
      "source": [
        "#### SQD options\n",
        "\n",
        "Important options in SQD simulations include the `sqd_iterations`, `number_of_batches`, and `samples_per_batch`. Generally, the lower number of samples per batch can be counteracted with more batches (`number_of_batches`) and more iterations of S-CORE (`sqd_iterations`). With more batches we can sample more variations of the configurational subspaces. Since the lowest-energy batch is taken as the solution for the ground state energy of the system, more batches can improve the results through better statistics. Additional iterations of S-CORE allow more configurations to be recovered from the original LUCJ distribution if the number of samples in the correct particle sector is low. This can allow the number of samples per batch to be reduced.\n",
        "\n",
        "<Accordion>\n",
        "  <AccordionItem title=\"Click to expand for more information about configuring the SQD options:\">\n",
        "    An alternative strategy is to use more samples per batch, which ensures that most of the initial LUCJ samples in right particle space are used during the S-CORE procedure, and individual subspaces encapsulate a sufficient variety of electron configurations. In turn, this reduces the number of required S-CORE steps, where only two or three iterations of SQD are needed if the number of samples per batch is large enough. However, more samples per batch results in a higher computational cost of each diagonalization step. Hence, the balance between the accuracy and computational cost in SQD simulations can be achieved by choosing  `sqd_iterations`, `number_of_batches`, and `samples_per_batch` optimally.\n",
        "\n",
        "    The [SQD IEF-PCM study](https://pubs.acs.org/doi/10.1021/acs.jpcb.5c01030) shows that when three iterations of S-CORE are used, the chemical accuracy can be reached by following these guidelines:\n",
        "\n",
        "    * 600 samples per batch in methanol SQD IEF-PCM (14e,12o) simulations\n",
        "    * 1500 samples per batch in methylamine SQD IEF-PCM (14e,13o) simulations\n",
        "    * 6000 samples per batch in water SQD IEF-PCM (8e,23o) simulations\n",
        "    * 16000 samples per batch in ethanol SQD IEF-PCM (20e,18o) simulations\n",
        "\n",
        "    Just like the required number of shots in LUCJ, the required number of samples per batch used in S-CORE procedure is highly system- and hardware-dependent. The examples above can be used to estimate the initial point for the benchmark of required number of samples per batch. The tutorial on systematic benchmark of the required number of samples per batch can be found [here](https://qiskit.github.io/qiskit-addon-sqd/how_tos/choose_subspace_dimension.html).\n",
        "  </AccordionItem>\n",
        "</Accordion>\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "998a0f25",
      "metadata": {},
      "source": [
        "## Deploy and execute the template SQD IEF-PCM function\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "6c92ac84",
      "metadata": {
        "vscode": {
          "languageId": "plaintext"
        }
      },
      "source": [
        "### Authentication\n",
        "\n",
        "Use `qiskit-ibm-catalog` to authenticate to `QiskitServerless` with your API key (token), which can be found on the [IBM Quantum Platform]() dashboard. This allows for the instantiation of the serverless client to upload or run the selected function:\n",
        "\n",
        "```python\n",
        "from qiskit_ibm_catalog import QiskitServerless\n",
        "\n",
        "serverless = QiskitServerless(\n",
        "    channel=\"ibm_quantum_platform\",\n",
        "    instance=\"INSTANCE_CRN\",\n",
        "    token=\"YOUR_API_KEY\" # Use the 44-character API_KEY you created and saved from the IBM Quantum Platform Home dashboard\n",
        ")\n",
        "```\n",
        "\n",
        "Optionally, use `save_account()` to save your credentials in a local environment (see the [Set up your IBM Cloud account](/docs/guides/cloud-setup#cloud-save) guide). Note that this writes your credentials to the same file as [`QiskitRuntimeService.save_account()`](/docs/api/qiskit-ibm-runtime/qiskit-runtime-service#save_account):\n",
        "\n",
        "```python\n",
        "QiskitServerless.save_account(token=\"YOUR_API_KEY\", channel=\"ibm_quantum_platform\", instance=\"INSTANCE_CRN\")\n",
        "```\n",
        "\n",
        "If the [account is saved](/docs/guides/save-credentials), there is no need to provide the token to authenticate:\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "9276e2d4",
      "metadata": {},
      "outputs": [],
      "source": [
        "from qiskit_ibm_catalog import QiskitServerless\n",
        "\n",
        "serverless = QiskitServerless()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "e1f99d80",
      "metadata": {
        "vscode": {
          "languageId": "plaintext"
        }
      },
      "source": [
        "### Upload the template\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "3e0e8cc8",
      "metadata": {
        "vscode": {
          "languageId": "plaintext"
        }
      },
      "source": [
        "To upload a custom Qiskit Function, you must first instantiate a `QiskitFunction` object that defines the function source code. The title will allow you to identify the function once it's in the remote cluster. The main entry point is the file that contains `if __name__ == \"__main__\"`. If your workflow requires additional source files, you can define a working directory that will be uploaded together with the entry point.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "77b2b9b6",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "QiskitFunction(sqd_pcm_template)\n"
          ]
        }
      ],
      "source": [
        "from qiskit_ibm_catalog import QiskitFunction\n",
        "\n",
        "template = QiskitFunction(\n",
        "    title=\"sqd_pcm_template\",\n",
        "    entrypoint=\"sqd_pcm_entrypoint.py\",\n",
        "    working_dir=\"./source_files/\",  # all files in this directory will be uploaded\n",
        "    dependencies=[\n",
        "        \"ffsim==0.0.54\",\n",
        "        \"pyscf==2.9.0\",\n",
        "        \"qiskit_addon_sqd==0.10.0\",\n",
        "    ],\n",
        ")\n",
        "print(template)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "72854f5a",
      "metadata": {},
      "source": [
        "Once the instance is ready, upload it to serverless:\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "id": "59e7fdb5",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "QiskitFunction(sqd_pcm_template)"
            ]
          },
          "execution_count": 3,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "serverless.upload(template)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "ac7d8764",
      "metadata": {
        "vscode": {
          "languageId": "plaintext"
        }
      },
      "source": [
        "To check if the program successfully uploaded, use `serverless.list()`:\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "id": "03a91030",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "[QiskitFunction(sqd_pcm_template),\n",
              " QiskitFunction(hamiltonian_simulation_template)]"
            ]
          },
          "execution_count": 4,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "serverless.list()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "99408586",
      "metadata": {},
      "source": [
        "## Load and run the template remotely\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "62b37d7a",
      "metadata": {},
      "source": [
        "The function template has been uploaded, so you can run it remotely with Qiskit Serverless. First, load the template by name:\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "id": "854d12cf",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "QiskitFunction(sqd_pcm_template)\n"
          ]
        }
      ],
      "source": [
        "template = serverless.load(\"sqd_pcm_template\")\n",
        "print(template)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "fa2dc721",
      "metadata": {
        "vscode": {
          "languageId": "plaintext"
        }
      },
      "source": [
        "Next, run the template with the domain-level inputs for SQD-IEF PCM. This example specifies a methanol-based workload.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "id": "a1719ab1",
      "metadata": {},
      "outputs": [],
      "source": [
        "molecule = {\n",
        "    \"atom\": \"\"\"\n",
        "    O -0.04559 -0.75076 -0.00000;\n",
        "    C -0.04844 0.65398 -0.00000;\n",
        "    H 0.85330 -1.05128 -0.00000;\n",
        "    H -1.08779 0.98076 -0.00000;\n",
        "    H 0.44171 1.06337 0.88811;\n",
        "    H 0.44171 1.06337 -0.88811\n",
        "    \"\"\",  # Must be specified\n",
        "    \"basis\": \"cc-pvdz\",  # default is \"sto-3g\"\n",
        "    \"spin\": 0,  # default is 0\n",
        "    \"charge\": 0,  # default is 0\n",
        "    \"verbosity\": 0,  # default is 0\n",
        "    \"number_of_active_orb\": 12,  # Must be specified\n",
        "    \"number_of_active_alpha_elec\": 7,  # Must be specified\n",
        "    \"number_of_active_beta_elec\": 7,  # Must be specified\n",
        "    \"avas_selection\": [\n",
        "        \"%d O %s\" % (k, x) for k in [0] for x in [\"2s\", \"2px\", \"2py\", \"2pz\"]\n",
        "    ]\n",
        "    + [\"%d C %s\" % (k, x) for k in [1] for x in [\"2s\", \"2px\", \"2py\", \"2pz\"]]\n",
        "    + [\"%d H 1s\" % k for k in [2, 3, 4, 5]],  # default is None\n",
        "}\n",
        "\n",
        "solvent_options = {\n",
        "    \"method\": \"IEF-PCM\",  # other available methods are COSMO, C-PCM, SS(V)PE, see https://manual.q-chem.com/5.4/topic_pcm-em.html\n",
        "    \"eps\": 78.3553,  # value for water\n",
        "}\n",
        "\n",
        "lucj_options = {\n",
        "    \"initial_layout\": [\n",
        "        0,\n",
        "        14,\n",
        "        18,\n",
        "        19,\n",
        "        20,\n",
        "        33,\n",
        "        39,\n",
        "        40,\n",
        "        41,\n",
        "        53,\n",
        "        60,\n",
        "        61,\n",
        "        2,\n",
        "        3,\n",
        "        4,\n",
        "        15,\n",
        "        22,\n",
        "        23,\n",
        "        24,\n",
        "        34,\n",
        "        43,\n",
        "        44,\n",
        "        45,\n",
        "        54,\n",
        "    ],\n",
        "    \"dynamical_decoupling_choice\": True,\n",
        "    \"twirling_choice\": True,\n",
        "    \"number_of_shots\": 200000,\n",
        "    \"optimization_level\": 2,\n",
        "}\n",
        "\n",
        "sqd_options = {\n",
        "    \"sqd_iterations\": 3,\n",
        "    \"number_of_batches\": 10,\n",
        "    \"samples_per_batch\": 1000,\n",
        "    \"max_davidson_cycles\": 200,\n",
        "}\n",
        "\n",
        "backend_name = \"ibm_sherbrooke\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "id": "01c0667c",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "39f8fb70-79b2-43ca-b723-84e6b6135821\n"
          ]
        }
      ],
      "source": [
        "job = template.run(\n",
        "    backend_name=backend_name,\n",
        "    molecule=molecule,\n",
        "    solvent_options=solvent_options,\n",
        "    lucj_options=lucj_options,\n",
        "    sqd_options=sqd_options,\n",
        ")\n",
        "print(job.job_id)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "a9101a94",
      "metadata": {},
      "source": [
        "Check the detailed status of the job:\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 18,
      "id": "4385a34f",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "time = 2.35, status = DONE\n"
          ]
        }
      ],
      "source": [
        "import time\n",
        "\n",
        "t0 = time.time()\n",
        "status = job.status()\n",
        "if status == \"QUEUED\":\n",
        "    print(f\"time = {time.time()-t0:.2f}, status = QUEUED\")\n",
        "while True:\n",
        "    status = job.status()\n",
        "    if status == \"QUEUED\":\n",
        "        continue\n",
        "    print(f\"time = {time.time()-t0:.2f}, status = {status}\")\n",
        "    if status == \"DONE\" or status == \"ERROR\":\n",
        "        break"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "4adc5293",
      "metadata": {},
      "source": [
        "While the job is running, you can fetch logs created from the `logger.info` outputs. These can provide actionable information about the progress of the SQD IEF-PCM workflow. For example, the same spin orbital connections, or the two-qubit depth of the final ISA circuit intended for execution on hardware.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "a5b1f190",
      "metadata": {},
      "outputs": [],
      "source": [
        "print(job.logs())"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "ba179114",
      "metadata": {},
      "source": [
        "Calling for the job result blocks the rest of the program until a result is available. After the job is done, you can retrieve the results. These include the solvation free energy, as well as information about the lowest energy batch, lowest energy value, and other useful information such as the total solver duration.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "id": "3500adce",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'total_energy_hist': array([[-115.14768518, -115.1368396 , -114.19181692, -115.13745429,\n",
              "         -115.1445012 , -114.19673326, -115.1547003 , -114.20563866,\n",
              "         -115.13748344, -115.14764974],\n",
              "        [-115.15768392, -115.15850126, -115.15857275, -115.15770916,\n",
              "         -115.15801684, -115.15822125, -115.15833521, -115.15844051,\n",
              "         -115.15735538, -115.15862354],\n",
              "        [-115.15795148, -115.15847925, -115.15856677, -115.15811156,\n",
              "         -115.15815602, -115.15785171, -115.1583672 , -115.1585533 ,\n",
              "         -115.15833528, -115.15808791]]),\n",
              " 'spin_squared_value_hist': array([[5.37327508e-03, 1.32981759e-02, 1.36214922e-02, 8.84413615e-03,\n",
              "         7.26723578e-03, 1.94875195e-02, 3.03153152e-03, 6.07543106e-03,\n",
              "         1.04951849e-02, 5.36529204e-03],\n",
              "        [6.39397528e-04, 1.36814350e-04, 9.09054260e-05, 5.99361358e-04,\n",
              "         3.64261739e-04, 2.54905866e-04, 2.32540370e-04, 1.53181990e-04,\n",
              "         7.23519739e-04, 6.80737671e-05],\n",
              "        [4.53776416e-04, 1.63043449e-04, 1.05317263e-04, 3.82912836e-04,\n",
              "         3.41047803e-04, 5.18620393e-04, 2.06819142e-04, 1.17086537e-04,\n",
              "         2.32357159e-04, 4.26071537e-04]]),\n",
              " 'solvation_free_energy_hist': array([[-0.00725018, -0.00743955, -0.01132905, -0.0073377 , -0.00722221,\n",
              "         -0.01136705, -0.00719279, -0.01072829, -0.00733404, -0.00725961],\n",
              "        [-0.00719252, -0.00718315, -0.00718074, -0.00719325, -0.00717703,\n",
              "         -0.00718391, -0.00718354, -0.00717928, -0.00719887, -0.0071801 ],\n",
              "        [-0.00719351, -0.00718255, -0.00718198, -0.00718429, -0.00718349,\n",
              "         -0.00718329, -0.0071882 , -0.00718363, -0.00718549, -0.00718814]]),\n",
              " 'occupancy_hist': [[array([0.99712298, 0.99278936, 0.99083163, 0.97328469, 0.98959809,\n",
              "          0.98922134, 0.720333  , 0.25683194, 0.01939338, 0.02840332,\n",
              "          0.00946988, 0.0327204 ]),\n",
              "   array([0.99712298, 0.99278936, 0.99083163, 0.97328469, 0.98959809,\n",
              "          0.98922134, 0.720333  , 0.25683194, 0.01939338, 0.02840332,\n",
              "          0.00946988, 0.0327204 ])],\n",
              "  [array([0.9959042 , 0.9922607 , 0.99018862, 0.99265843, 0.98927447,\n",
              "          0.9900833 , 0.99403876, 0.00989025, 0.01120814, 0.01137717,\n",
              "          0.01152871, 0.01158725]),\n",
              "   array([0.9959042 , 0.9922607 , 0.99018862, 0.99265843, 0.98927447,\n",
              "          0.9900833 , 0.99403876, 0.00989025, 0.01120814, 0.01137717,\n",
              "          0.01152871, 0.01158725])],\n",
              "  [array([0.99590079, 0.99222193, 0.99016753, 0.99265045, 0.98927264,\n",
              "          0.99007179, 0.99407207, 0.00986684, 0.01125181, 0.01141439,\n",
              "          0.01150733, 0.01160243]),\n",
              "   array([0.99590079, 0.99222193, 0.99016753, 0.99265045, 0.98927264,\n",
              "          0.99007179, 0.99407207, 0.00986684, 0.01125181, 0.01141439,\n",
              "          0.01150733, 0.01160243])]],\n",
              " 'lowest_energy_batch': 2,\n",
              " 'lowest_energy_value': -115.1585667736213,\n",
              " 'solvation_free_energy': -0.007181981952470838,\n",
              " 'sci_solver_total_duration': 493.997501373291,\n",
              " 'metadata': {'resources_usage': {'RUNNING: MAPPING': {'CPU_TIME': 6.080063343048096},\n",
              "   'RUNNING: OPTIMIZING_FOR_HARDWARE': {'CPU_TIME': 1.999896764755249},\n",
              "   'RUNNING: WAITING_FOR_QPU': {'CPU_TIME': 6.2850868701934814},\n",
              "   'RUNNING: EXECUTING_QPU': {'QPU_TIME': 21.639373540878296},\n",
              "   'RUNNING: POST_PROCESSING': {'CPU_TIME': 495.40831995010376}},\n",
              "  'num_iterations_executed': 3}}"
            ]
          },
          "execution_count": 16,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "result = job.result()\n",
        "\n",
        "result"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "94a2c921",
      "metadata": {},
      "source": [
        "Note that the result metadata includes a resource usage summary that lets you better estimate the QPU and CPU time required for each workload (this example ran on a dummy device, so actual resource usage times might differ).\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "49d0b26d",
      "metadata": {
        "vscode": {
          "languageId": "plaintext"
        }
      },
      "source": [
        "After the job completes, the entire logging output will be available.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 17,
      "id": "ddcba564",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "2025-06-27 08:42:41,358\tINFO job_manager.py:531 -- Runtime env is setting up.\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:42:45,015: Starting runtime service\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:42:45,621: Backend: ibm_sherbrooke\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:42:46,809: Initializing molecule object\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:42:51,599: Performing CCSD\n",
            "Parsing /tmp/ray/session_2025-06-27_08-42-13_898146_1/runtime_resources/working_dir_files/_ray_pkg_4bc93dcc58c04b91/output_sqd_pcm/2025-06-27_08-42-45.fcidump.txt\n",
            "Overwritten attributes  get_ovlp get_hcore  of <class 'pyscf.scf.hf_symm.SymAdaptedRHF'>\n",
            "/usr/local/lib/python3.11/site-packages/pyscf/gto/mole.py:1293: UserWarning: Function mol.dumps drops attribute energy_nuc because it is not JSON-serializable\n",
            "  warnings.warn(msg)\n",
            "/usr/local/lib/python3.11/site-packages/pyscf/gto/mole.py:1293: UserWarning: Function mol.dumps drops attribute intor_symmetric because it is not JSON-serializable\n",
            "  warnings.warn(msg)\n",
            "converged SCF energy = -115.049680672847\n",
            "E(CCSD) = -115.1519910037652  E_corr = -0.1023103309180226\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:42:51,694: Same spin orbital connections: [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9), (9, 10), (10, 11)]\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:42:51,694: Opposite spin orbital connections: [(0, 0), (4, 4), (8, 8)]\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:42:53,718: Optimization level: 2, ops: OrderedDict([('rz', 2438), ('sx', 1496), ('ecr', 766), ('x', 185), ('measure', 24), ('barrier', 1)]), depth: 391\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:42:53,736: Two-qubit gate depth: 94\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:42:53,737: Submitting sampler job\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:42:54,273: Job ID: d1f5j3lqbivc73ebqpj0\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:42:54,313: Job Status: QUEUED\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:43:24,813: Starting configuration recovery iteration 0\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:43:24,841: Batch 0 subspace dimension: 531441\n",
            "2025-06-27 08:43:24,844\tINFO worker.py:1588 -- Using address 172.17.16.124:6379 set in the environment variable RAY_ADDRESS\n",
            "2025-06-27 08:43:24,847\tINFO worker.py:1723 -- Connecting to existing Ray cluster at address: 172.17.16.124:6379...\n",
            "2025-06-27 08:43:24,876\tINFO worker.py:1908 -- Connected to Ray cluster. View the dashboard at \u001b[1m\u001b[32mhttp://172.17.16.124:8265 \u001b[39m\u001b[22m\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:43:24,945: Batch 1 subspace dimension: 519841\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:43:24,950: Batch 2 subspace dimension: 543169\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:43:24,955: Batch 3 subspace dimension: 532900\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:43:24,960: Batch 4 subspace dimension: 534361\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:43:24,964: Batch 5 subspace dimension: 531441\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:43:24,969: Batch 6 subspace dimension: 540225\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:43:24,974: Batch 7 subspace dimension: 524176\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:43:24,979: Batch 8 subspace dimension: 537289\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:43:24,983: Batch 9 subspace dimension: 540225\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:48:09,006: Lowest energy batch: 6\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:48:09,007: Lowest energy value: -115.15470029849135\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:48:09,007: Corresponding g_solv value: -0.0071927910374866375\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:48:09,007: -----------------------------------\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:48:09,007: Starting configuration recovery iteration 1\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:48:40,564: Batch 0 subspace dimension: 413449\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:48:40,572: Batch 1 subspace dimension: 399424\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:48:40,578: Batch 2 subspace dimension: 438244\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:48:40,583: Batch 3 subspace dimension: 422500\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:48:40,589: Batch 4 subspace dimension: 409600\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:48:40,596: Batch 5 subspace dimension: 404496\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:48:40,601: Batch 6 subspace dimension: 410881\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:48:40,605: Batch 7 subspace dimension: 442225\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:48:40,611: Batch 8 subspace dimension: 409600\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:48:40,618: Batch 9 subspace dimension: 405769\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:49:54,917: Lowest energy batch: 9\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:49:54,917: Lowest energy value: -115.15862353596414\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:49:54,917: Corresponding g_solv value: -0.0071800982859467006\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:49:54,918: -----------------------------------\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:49:54,918: Starting configuration recovery iteration 2\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:50:25,501: Batch 0 subspace dimension: 399424\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:50:25,508: Batch 1 subspace dimension: 412164\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:50:25,514: Batch 2 subspace dimension: 432964\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:50:25,519: Batch 3 subspace dimension: 400689\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:50:25,524: Batch 4 subspace dimension: 432964\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:50:25,529: Batch 5 subspace dimension: 418609\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:50:25,533: Batch 6 subspace dimension: 418609\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:50:25,538: Batch 7 subspace dimension: 425104\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:50:25,543: Batch 8 subspace dimension: 404496\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:50:25,548: Batch 9 subspace dimension: 429025\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:51:37,900: Lowest energy batch: 2\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:51:37,900: Lowest energy value: -115.1585667736213\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:51:37,901: Corresponding g_solv value: -0.007181981952470838\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:51:37,901: -----------------------------------\n",
            "sqd_pcm_entrypoint.run_function:INFO:2025-06-27 08:51:37,901: SCI_solver totally takes: 493.997501373291 seconds\n",
            "\n"
          ]
        }
      ],
      "source": [
        "print(job.logs())"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "d7cc1cb2",
      "metadata": {},
      "source": [
        "## Next steps\n",
        "\n",
        "<Admonition type=\"info\" title=\"Recommendations\">\n",
        "  * Review the guide on building a function template for [Hamiltonian simulation](/docs/guides/function-template-hamiltonian-simulation)\n",
        "  * Check out the source files for this template on [GitHub](https://github.com/qiskit-community/qiskit-function-templates/tree/main/chemistry/sqd_pcm)\n",
        "</Admonition>\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "aabba015",
      "metadata": {},
      "source": [
        "#### References\n",
        "\n",
        "\\[1] Danil Kaliakin, Akhil Shajan, Fangchun Liang, and Kenneth M. Merz Jr. [Implicit Solvent Sample-Based Quantum Diagonalization](https://pubs.acs.org/doi/10.1021/acs.jpcb.5c01030), The Journal of Physical Chemistry B, 2025, DOI: 10.1021/acs.jpcb.5c01030\n",
        "\n",
        "\\[2] Javier Robledo-Moreno, et al., [Chemistry Beyond Exact Solutions on a Quantum-Centric Supercomputer](https://arxiv.org/abs/2405.05068), arXiv:2405.05068 \\[quant-ph].\n",
        "\n",
        "\\[3] Jeffery Yu, et al., [Quantum-Centric Algorithm for Sample-Based Krylov Diagonalization](https://arxiv.org/abs/2501.09702), arXiv:2501.09702 \\[quant-ph].\n",
        "\n",
        "\\[4] Keita Kanno, et al., [Quantum-Selected Configuration Interaction: classical diagonalization of Hamiltonians in subspaces selected by quantum computers](https://arxiv.org/abs/2302.11320), arXiv:2302.11320 \\[quant-ph].\n",
        "\n",
        "\\[5] Kenji Sugisaki, et al., [Hamiltonian simulation-based quantum-selected configuration interaction for large-scale electronic structure calculations with a quantum computer](https://arxiv.org/abs/2412.07218), arXiv:2412.07218 \\[quant-ph].\n",
        "\n",
        "\\[6] Mathias Mikkelsen, Yuya O. Nakagawa, [Quantum-selected configuration interaction with time-evolved state](https://arxiv.org/abs/2412.13839), arXiv:2412.13839 \\[quant-ph].\n",
        "\n",
        "\\[7] Herbert, John M. [Dielectric continuum methods for quantum chemistry. WIREs Computational Molecular Science](https://wires.onlinelibrary.wiley.com/doi/10.1002/wcms.1519), 2021, 11, 1759-0876.\n",
        "\n",
        "\\[8] Saki, A. A.; Barison, S.; Fuller, B.; Garrison, J. R.; Glick, J. R.; Johnson, C.; Mezzacapo, A.; Robledo-Moreno, J.; Rossmannek, M.; Schweigert, P. et al. Qiskit addon: sample-based quantum diagonalization, 2024; [https://github.com/Qiskit/qiskit-addon-sqd](https://github.com/Qiskit/qiskit-addon-sqd)\n",
        "\n",
        "\\[9] Asun, Q.; Zhang, X.; Banerjee, S.; Bao, P.; Barbry, M.; Blunt, N. S.; Bogdanov, N. A.; Booth, G. H.; Chen, J.; Cui, Z.-H. PySCF: Python-based Simulations of Chemistry Framework, 2025; [https://github.com/pyscf/pyscf](https://github.com/pyscf/pyscf)\n",
        "\n",
        "\\[10] Kevin J. Sung; et al., FFSIM: Faster simulations of fermionic quantum circuits, 2024. [https://github.com/qiskit-community/ffsim](https://github.com/qiskit-community/ffsim)\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": 5
}