{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Driver functions for running Gate Set Tomography"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `pygsti` package provides multiple levels of abstraction over the core Gate Set Tomography (GST) algorithms.  This  tutorial will show you how to work with pyGSTi's top-level functions for performing GST with a minimial amount of effort.  In order to run the GST protocol there are 3 essential ingredients: 1) data specifing the experimental outcomes, 2) a desired, or \"target\", `Model`, and 3) lists of `Circuit` objects, specifying the operation sequences to use at each successive step in the GST optimization.  The [GST overview tutorial](GST-Overview.ipynb), gave an end-to-end example of how to construct GST circuits, run GST, and generate a report.  This tutorial focus on the second step in more detail; more information about circuit construction and report generation can be found in the [GST circuits tutorial](../objects/advanced/GSTCircuitConstruction.ipynb) and [report generation tutorial](../reporting/ReportGeneration.ipynb).\n",
    "\n",
    "There are several different \"driver routines\" for running GST, and we'll cover in turn:\n",
    "- [`run_long_sequence_gst`](#run_long_sequence_gst) - runs a single instance of GST with \"standard\" circuit lists.\n",
    "- [`run_long_sequence_gst_base`](#run_long_sequence_gst_base) - runs a single instance of GST with custom circuit lists.\n",
    "- [`run_stdpractice_gst`](#run_stdpractice_gst) - runs a multiple instances of GST with \"standard\" circuits based on an `ExplicitOpModel` model.\n",
    "\n",
    "Each function returns a single `pygsti.objects.Results` object (see the [Result object tutorial](../objects/advanced/Results.ipynb), which contains the *single* input `DataSet` and one or more *estimates* (`pygsti.objects.Estimate` objects). \n",
    "\n",
    "Note: The abbreviation **LSGST** (lowercase in function names to follow Python naming conventions) stands for *Long Sequence LinearOperator Set Tomography*, and refers to the more powerful and flexible of GST that utilizes long sequences to find model estimates.  LSGST can be compared to *Linear GST*, or **LGST**, which only uses short sequences and as a result provides much less accurate estimates."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pygsti"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Setup\n",
    "First, we set our desired *target model* to be the standard $X(\\pi/2)$, $Y(\\pi/2)$ model that we've been using in many of these tutorials, and use the standard fiducial and germ sequences needed to generate the GST operation sequences (see the [standard module tutorial](../objects/advanced/StandardModules.ipynb)).  We also specify a list of maximum lengths.  We'll analyze the simulated data generated in the [DataSet tutorial](../objects/DataSet.ipynb), so you'll need to run that tutorial if you haven't already."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Reading ../tutorial_files/Example_Dataset.txt: 100%\n",
      "Writing cache file (to speed future loads): ../tutorial_files/Example_Dataset.txt.cache\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\ciostro\\Documents\\pyGSTi_random_bugfixes\\pygsti\\tools\\legacytools.py:38: UserWarning: The function save is deprecated, and may not be present in future versions of pygsti.\n",
      "    Please use write_binary instead.\n",
      "  _warnings.warn(message)\n"
     ]
    }
   ],
   "source": [
    "from pygsti.modelpacks import smq1Q_XY\n",
    "target_model = smq1Q_XY.target_model()\n",
    "prep_fiducials, meas_fiducials = smq1Q_XY.prep_fiducials(), smq1Q_XY.meas_fiducials()\n",
    "germs = smq1Q_XY.germs()\n",
    "\n",
    "maxLengths = [1,2,4,8,16]\n",
    "\n",
    "ds = pygsti.io.load_dataset(\"../tutorial_files/Example_Dataset.txt\", cache=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"run_long_sequence_gst\"></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `run_long_sequence_gst`\n",
    "This driver function finds what is logically a **single GST estimate** given a `DataSet`, a target `Model`, and other parameters.  We say \"logically\" because the returned `Results` object may actually contain multiple related estimates in certain cases.  Most important among the other parameters are the fiducial and germ sequences and list of maximum lengths needed to define a *standard*  (*prep_fiducial + germ^power + meas_fiducial*) set of GST circuit lists.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "results = pygsti.run_long_sequence_gst(ds, target_model, prep_fiducials, meas_fiducials, germs, maxLengths)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A summary of what's inside a Results object is obtained by printing it\n",
    "(for more examples of how to use a Results object, see the [Results tutorial](../objects/advanced/Results.ipynb))."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(results)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Beyond the minimum\n",
    "The above example supplies the minimal amount of information required to run the long-sequence GST algorithm.  `run_long_sequence_gst` can be used in a variety of contexts and accepts additional (optional) arguments that affect the way the algorithm is run.  Here we make several remarks regarding alternate or more advanced usage of `run_long_sequence_gst`.\n",
    "\n",
    "- For many of the arguments, you can supply either a filename or a python object (e.g. dataset, target model, operation sequence lists), so if you find yourself loading things from files just to pass them in as arguments, you're probabaly working too hard.\n",
    "\n",
    "- Typically we want to apply certain constraints to a GST optimization.  As mentioned in the model tutorial, the space over which a gate-set estimation is carried out is dictated by the parameterization of the `target_model` argument.  For example, to constrain a GST estimate to be trace-preserving, one should call `set_all_parameterizations(\"TP\")` on the target `Model` before calling `run_long_sequence_gst`.  See the [tutorial on explicit models](../objects/ExplicitModel.ipynb) for more information.\n",
    "\n",
    "- the `gauge_opt_params` argument specifies a dictionary of parameters ultimately to be passed to the `gaugeopt_to_target` function (which provides full documentation).  By specifying an `item_weights` argument we can set the ratio of the state preparation and measurement (SPAM) weighting to the gate weighting when performing a gauge optimization.  In the example below, the gate parameters are weighted 1000 times more relative to the SPAM parameters.  Mathematically this corresponds to a multiplicative factor of 0.001 preceding the sum-of-squared-difference terms corresponding to SPAM elements in the model.   Typically it is good to weight the gates parameters more heavily since GST amplifies gate parameter errors via long operation sequences but cannot amplify SPAM parameter errors.  If unsure, 0.001 is a good value to start with.  For more details on the arguments of `gaugeopt_to_target`, see the previous tutorial on low-level algorithms.  For more infomation, see the [gauge optimization tutorial](advanced/GaugeOpt.ipynb).\n",
    "\n",
    "The below call illustrates all three of these."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mdl_target_TP = target_model.copy() #make a copy so we don't change target_model's parameterization, \n",
    "                                #  since this could be confusing later...\n",
    "mdl_target_TP.set_all_parameterizations(\"full TP\") #constrain GST estimate to TP\n",
    "\n",
    "results_TP = pygsti.run_long_sequence_gst(\"../tutorial_files/Example_Dataset.txt\", mdl_target_TP,\n",
    "                                         prep_fiducials, meas_fiducials, germs, maxLengths,\n",
    "                                        gauge_opt_params={'item_weights': {'gates': 1.0, 'spam': 0.001}})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"run_long_sequence_gst_base\"></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `run_long_sequence_gst_base`\n",
    "This function performs the same analysis as `run_long_sequence_gst` except it allows the user to fully specify the list of operation sequences as either a list of lists of `Circuit` objects or a list of or single `CircuitStructure` object(s). A `CircuitStructure` is preferable as it allows the structured plotting of the sequences in report figures.  In this example, we'll just generate a standard set of structures, but with some of the sequences randomly dropped (see the [tutorial on GST circuit reduction](advanced/GST-FiducialPairReduction.ipynb).  Note that like `run_long_sequence_gst`, `run_long_sequence_gst_base` is able to take filenames as arguments and accepts a `gauge_opt_params` argument for customizing the gauge optimization that is performed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Create the same sequences but drop 50% of them randomly for each repeated-germ block.\n",
    "lsgst_structs = pygsti.circuits.create_lsgst_circuit_lists(target_model, prep_fiducials, meas_fiducials,\n",
    "                                                       germs, maxLengths, keep_fraction=0.5, keep_seed=2018)\n",
    "results_reduced = pygsti.run_long_sequence_gst_base(ds, target_model, lsgst_structs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"run_stdpractice_gst\"></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `run_stdpractice_gst`\n",
    "This function calls `run_long_sequence_gst` multiple times using typical variations in gauge optimization parameters and `ExplicitOpModel` parameterization (this doesn't work for other types `Model` objects, e.g. *implicit* models, which don't implement `set_all_parameterizations`).  This function provides a clean and simple interface to performing a \"usual\" set of GST analyses on a set of data.  As such, it takes a single `DataSet`, similar gate-sequence-specifying parameters to `run_long_sequence_gst`, and a new `modes` argument which is a comma-separated list of \"canned\" GST analysis types (e.g. `\"TP,CPTP\"` will compute a Trace-Preserving estimate *and* a Completely-Positive & Trace-Preserving estimate). The currently available modes are:\n",
    " - \"full\" : unconstrained gates (fully parameterized)                                                                 \n",
    " - \"TP\"   : TP-constrained gates and state preparations\n",
    " - \"CPTP\" : CPTP-constrained gates and TP-constrained state preparations               \n",
    " - \"H+S\"  : Only Hamiltonian and Pauli stochastic errors allowed (CPTP)                                             \n",
    " - \"S\"    : Only Pauli-stochastic errors allowed (CPTP)                                                           \n",
    " - \"Target\" : use the target (ideal) gates as the estimate       \n",
    "\n",
    "The gauge optimization(s) `run_stdpractice_gst` performs are controlled by its `gauge_opt_suite` and `gaugeOptTarget` arguments. The former is can be either a string, specifying a standard \"suite\" of gauge optimizations, or a dictionary of dictionaries similar to the `gauge_opt_params` argument of `run_long_sequence_gst` (see docstring).  The `gaugeOptTarget` argument may be set to a `Model` that is used as the target for gauge optimization, overriding the (typically ideal) target gates given by the `target_modelFilenameOrObj` argument."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "results_stdprac = pygsti.run_stdpractice_gst(ds, target_model, prep_fiducials, meas_fiducials, germs, maxLengths,\n",
    "                                        modes=\"full TP,CPTP,Target\") #uses the default suite of gauge-optimizations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Estimates: \", \", \".join(results_stdprac.estimates.keys()))\n",
    "print(\"TP Estimate's gauge optimized models: \", \", \".join(results_stdprac.estimates[\"full TP\"].goparameters.keys()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, we'll perform the same analysis but with a **non-default standard suite of gauge optimizations** - this one toggles the SPAM penalty in addition to varying the spam weight (the default suite just varies the spam weight without any SPAM penalty).  See the [gauge optimization tutorial](advanced/GaugeOpt.ipynb) for more details on gauge optmization \"suites\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "results_stdprac_nondefaultgo = pygsti.run_stdpractice_gst(\n",
    "    ds, target_model, prep_fiducials, meas_fiducials, germs, maxLengths,\n",
    "    modes=\"full TP,CPTP,Target\", gaugeopt_suite=\"varySpam\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Estimates: \", \", \".join(results_stdprac_nondefaultgo.estimates.keys()))\n",
    "print(\"TP Estimate's gauge optimized models: \", \", \".join(results_stdprac_nondefaultgo.estimates[\"full TP\"].goparameters.keys()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, we'll demonstrate how to specify a fully custom set of gauge optimization parameters and how to use a **separately-specified target model for gauge optimization**.  You can get a more intuitive gauge-optimized `Model` when by placing as much expected noise as possible into the gauge-optimization target, as this essentially tells the algorithm \"this is what I think the estimated model should look like\".  If you just use the perfect or ideal model for this (the default), then the gauge optimizer may make tradeoffs which don't reflect the expected physics (remember, all gauge-equivalent models product the same observables!).  For example, it may spread error across all your gate operations when you expect just the 2-qubit operations are noisy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_goparams = { 'item_weights': {'gates': 1.0, 'spam': 0.001} }\n",
    "my_gaugeOptTarget= smq1Q_XY.target_model('full TP')\n",
    "my_gaugeOptTarget = my_gaugeOptTarget.depolarize(op_noise=0.005, spam_noise=0.01) # a guess at what estimate should be\n",
    "results_stdprac_customgo = pygsti.run_stdpractice_gst(\n",
    "    ds, target_model, prep_fiducials, meas_fiducials, germs, maxLengths,\n",
    "    modes=\"full TP,CPTP,Target\", gaugeopt_suite={ 'myGO': my_goparams }, gaugeopt_target=my_gaugeOptTarget)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Estimates: \", \", \".join(results_stdprac_customgo.estimates.keys()))\n",
    "print(\"TP Estimate's gauge optimized models: \", \", \".join(results_stdprac_customgo.estimates[\"full TP\"].goparameters.keys()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To finish up, we'll pickle the results, potentially for processing in other tutorials."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import pickle\n",
    "pickle.dump(results, open('../tutorial_files/exampleResults.pkl',\"wb\"))\n",
    "pickle.dump(results_TP, open('../tutorial_files/exampleResults_TP.pkl',\"wb\"))\n",
    "pickle.dump(results_reduced, open('../tutorial_files/exampleResults_reduced.pkl',\"wb\"))\n",
    "pickle.dump(results_stdprac, open('../tutorial_files/exampleResults_stdprac.pkl',\"wb\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
