{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# How to mute status messages during calculations\n",
    "**Change the verbosity of the messaging**\n",
    "\n",
    "When running calculations on Boulder Opal, you receive information of their status via printed messages.\n",
    "These display for instance, whether a task has started or completed, and include a unique action ID you can use to identify and manage the calculation.\n",
    "\n",
    "In this user guide we demonstrate how to change this behavior and mute these messages."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Selecting the message verbosity\n",
    "\n",
    "### Choosing the display behavior during authentication\n",
    "\n",
    "By default, messsages updating you on the status of your calculations will be displayed during calculations.\n",
    "You can use `boulderopal.cloud.set_verbosity` to choose how you want to be notified of the progress of your tasks.\n",
    "For instance, you can mute them with `boulderopal.cloud.set_verbosity(\"QUIET\")`. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Infidelity: 4.441e-16\n"
     ]
    }
   ],
   "source": [
    "def run_calculation():\n",
    "    \"\"\"\n",
    "    Run a simple π-pulse simulation and print the resulting infidelity.\n",
    "    \"\"\"\n",
    "\n",
    "    graph = bo.Graph()\n",
    "    amplitude = np.pi * 1e5  # rad/s\n",
    "    duration = 5e-6  # s\n",
    "    pi_pulse = graph.constant_pwc(amplitude, duration)\n",
    "    infidelity = graph.infidelity_pwc(\n",
    "        hamiltonian=pi_pulse * graph.pauli_matrix(\"X\"),\n",
    "        target=graph.target(graph.pauli_matrix(\"X\")),\n",
    "        name=\"infidelity\",\n",
    "    )\n",
    "\n",
    "    result = bo.execute_graph(graph, \"infidelity\")\n",
    "    print(f\"Infidelity: {result['output']['infidelity']['value']:.3e}\")\n",
    "    return result\n",
    "\n",
    "\n",
    "import numpy as np\n",
    "import boulderopal as bo\n",
    "\n",
    "# Mute messges from Boulder Opal calls.\n",
    "bo.cloud.set_verbosity(\"QUIET\")\n",
    "\n",
    "# Run simple calculation.\n",
    "result = run_calculation()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also turn the messages back on with \"VERBOSE\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Your task (action_id=\"1828558\") has completed.\n",
      "Infidelity: 4.441e-16\n"
     ]
    }
   ],
   "source": [
    "# Turning on output messages.\n",
    "bo.cloud.set_verbosity(\"VERBOSE\")\n",
    "\n",
    "result = run_calculation()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Retrieving action IDs\n",
    "\n",
    "Note that by muting the messages for calculations, their corresponding action IDs will not be displayed.\n",
    "These are useful in case you want to retrieve the calculation results at a later time, see [this user guide](https://docs.q-ctrl.com/boulder-opal/toolkit/discover/set-up/how-to-manage-and-monitor-your-calculations) for more details.\n",
    "In that case, you can still retrieve the action ID in the [Boulder Opal web app](https://boulder.q-ctrl.com)."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
