{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Algorithms Tutorial\n", "Once we have data for GST, there are several *algorithms* we can run on it to produce tomographic estimates. Depending on the amount of data you have, and time available for running Gate Set Tomography, one algorithm may be preferable over the others.\n", "\n", "Currently, `pygsti` provides support for the following GST algorithms:\n", "\n", "* **Linear Gate Set Tomography (LGST)**: Uses short gate sequences to quickly compute a rough (low accuracy) estimate of a gate set by linear inversion.\n", "\n", "* **Extended Linear Gate Set Tomography (eLGST or EXLGST)**: Minimizes the sub-of-squared errors between independent LGST estimates and the estimates obtained from a single gate set to find a best-estimate gate set. This is typically done in an interative fashion, using LGST estimates for longer and longer sequences. \n", "\n", "* **Minimum-$\\chi^2$ Gate Set Tomography (MC2GST)**: Minimizes the $\\chi^{2}$ statistic of the data frequencies and gate set probabilities to find a best-estimate gate set. Typically done in an interative fashion, using successively larger sets of longer and longer gate sequences. \n", "\n", "* **Maximum-Likelihood Gate Set Tomography (MLGST)**: Maximizes the log-likelihood statistic of the data frequencies and gate set probabilities to find a best-estimate gate set. Typically done in an interative fashion similar to MC2GST. This maximum likelihood estimation (MLE) is very well-motivated from a statistics standpoint and should be the **most accurate among the algorithms**. \n", "\n", "If you're curious, the implementation of the algorithms for LGST, EXLGST, MC2GST, and MLGST may be found in the `pygsti.algorithms.core` module. In this tutorial, we'll show how to invoke each of these algorithms." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Setup\n", "The ingredients needed to as input to the GST algorithms are:\n", "- a \"target\" `GateSet` which defines the desired gates. This gate set is used by LGST to specify the various gate, state preparation, POVM effect, and SPAM labels, as well as to provide an initial guess for the *gauge* degrees of freedom.\n", "- a `DataSet` containing the data that GST attempts to fit using the probabilities generated by a single `GateSet`. This data set must at least contain the data for the gate sequences required by the algorithm that is chosen.\n", "- for EXLGST, MC2GST, and MLGST, a list-of-lists of `GateString` objects, which specify which gate strings are used during each iteration of the algorithm (the length of the top-level list defines the number of interations). Note that which gate strings are included in these lists is different for EXLGST than it is for MC2GST and MLGST." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from __future__ import print_function" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import pygsti\n", "import json" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loading tutorial_files/Example_Dataset_LowCnts.txt: 100%\n", "Writing cache file (to speed future loads): tutorial_files/Example_Dataset_LowCnts.txt.cache\n", "Loaded target gateset with gate labels: odict_keys(['Gi', 'Gx', 'Gy'])\n", "Loaded fiducial list of length: 6\n", "Loaded dataset of length: 2737\n" ] } ], "source": [ "#Load target gateset, dataset, and list of fiducial gate strings.\n", "#In this case we load directly from files created in past tutorials\n", "\n", "gs_target = pygsti.io.load_gateset(\"tutorial_files/Example_Gateset.txt\")\n", "ds = pygsti.io.load_dataset(\"tutorial_files/Example_Dataset.txt\", cache=True)\n", "dsLowCounts = pygsti.io.load_dataset(\"tutorial_files/Example_Dataset_LowCnts.txt\", cache=True)\n", "fiducialList = pygsti.io.load_gatestring_list(\"tutorial_files/Example_FiducialList.txt\")\n", "\n", "depol_gateset = gs_target.depolarize(gate_noise=0.1)\n", "\n", "#Could also load a fiducial dictionary file like this:\n", "#fiducialDict = GST.load_gatestring_dict(\"Example_FiducialList.txt\")\n", "#fiducialList = fiducialDict.values() #all we really need are the fiducial strings themselves\n", "\n", "print(\"Loaded target gateset with gate labels: \", gs_target.gates.keys())\n", "print(\"Loaded fiducial list of length: \", len(fiducialList))\n", "print(\"Loaded dataset of length: \", len(ds))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Using LGST to get an initial estimate\n", "\n", "An important and distinguising property of the LGST algorithm is that it does *not* require an initial-guess `GateSet` as an input. It uses linear inversion and short sequences to obtain a rough gate set estimate. As such, it is very common to use the LGST estimate as the initial-guess starting point for more advanced forms of GST." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--- LGST ---\n" ] } ], "source": [ "#Run LGST to get an initial estimate for the gates in gs_target based on the data in ds\n", "\n", "#create \"spam specs\" from the list of fiducial gate strings. See the gate strings tutorial for more info.\n", "specs = pygsti.construction.build_spam_specs(fiducialGateStrings=fiducialList)\n", "\n", "#run LGST\n", "gs_lgst = pygsti.do_lgst(ds, specs, targetGateset=gs_target, svdTruncateTo=4, verbosity=1)\n", "\n", "#Gauge optimize the result to match the target gateset\n", "gs_lgst_after_gauge_opt = pygsti.gaugeopt_to_target(gs_lgst, gs_target)\n", "\n", "#Contract the result to CPTP, guaranteeing that the gates are CPTP\n", "gs_clgst = pygsti.contract(gs_lgst_after_gauge_opt, \"CPTP\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "rho0 = 0.7094 -0.0205 0.0230 0.7544\n", "\n", "\n", "E0 = 0.6877 0.0052 -0.0021 -0.6498\n", "\n", "\n", "Gi = \n", " 1.0065 -0.0005 -0.0018 0.0055\n", " -0.0059 0.9258 0.0528 -0.0169\n", " 0.0422 -0.0172 0.9029 0.0212\n", " -0.0078 0.0118 0.0219 0.9076\n", "\n", "\n", "Gx = \n", " 0.9970 -0.0073 -0.0088 0.0003\n", " 0.0056 0.9077 0.0232 -0.0016\n", " 0.0146 0.0163 0.0013 -1.0031\n", " -0.0695 -0.0060 0.8032 0.0079\n", "\n", "\n", "Gy = \n", " 1.0040 0.0074 -0.0055 -0.0005\n", " -0.0377 -0.0225 -0.0012 0.9963\n", " 0.0189 0.0094 0.8877 -0.0266\n", " -0.0679 -0.8058 -0.0250 0.0178\n", "\n", "\n", "\n" ] } ], "source": [ "print(gs_lgst)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Extended LGST (eLGST or EXLGST)\n", "EXLGST requires a list-of-lists of gate strings, one per iteration. The elements of these lists are typically repetitions of short \"germ\" strings such that the final strings does not exceed some maximum length. We created such lists in the gate string tutorial. Now, we just load these lists from the text files they were saved in." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--- Iterative eLGST: Iter 10 of 10 ; 84 gate strings ---: \n" ] } ], "source": [ "#Get rho and E specifiers, needed by LGST\n", "specs = pygsti.construction.build_spam_specs(fiducialGateStrings=fiducialList)\n", "germList = pygsti.io.load_gatestring_list(\"tutorial_files/Example_GermsList.txt\")\n", "maxLengthList = json.load(open(\"tutorial_files/Example_maxLengths.json\",\"r\"))\n", "elgstListOfLists = [ pygsti.io.load_gatestring_list(\"tutorial_files/Example_eLGSTlist%d.txt\" % l) for l in maxLengthList]\n", " \n", "#run EXLGST. The result, gs_exlgst, is a GateSet containing the estimated quantities\n", "gs_exlgst = pygsti.do_iterative_exlgst(ds, gs_clgst, specs, elgstListOfLists, targetGateset=gs_target,\n", " svdTruncateTo=4, verbosity=2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Minimum-$\\chi^2$ GST (MC2GST)\n", "MC2GST and MLGST also require a list-of-lists of gate strings, one per iteration. However, the elements of these lists are typically repetitions of short \"germ\" strings *sandwiched between fiducial strings* such that the repeated-germ part of the string does not exceed some maximum length. We created such lists in the gate string tutorial. Now, we just load these lists from the text files they were saved in." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--- Iterative MC2GST: Iter 01 of 10 92 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 40.9237 (92 data params - 40 model params = expected mean of 52; p-value = 0.866036)\n", " Completed in 0.1s\n", " Iteration 1 took 0.1s\n", " \n", "--- Iterative MC2GST: Iter 02 of 10 92 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 40.9237 (92 data params - 40 model params = expected mean of 52; p-value = 0.866036)\n", " Completed in 0.0s\n", " Iteration 2 took 0.0s\n", " \n", "--- Iterative MC2GST: Iter 03 of 10 168 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 124.56 (168 data params - 40 model params = expected mean of 128; p-value = 0.569542)\n", " Completed in 0.1s\n", " Iteration 3 took 0.1s\n", " \n", "--- Iterative MC2GST: Iter 04 of 10 441 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 424.143 (441 data params - 40 model params = expected mean of 401; p-value = 0.204567)\n", " Completed in 0.2s\n", " Iteration 4 took 0.2s\n", " \n", "--- Iterative MC2GST: Iter 05 of 10 817 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 772.113 (817 data params - 40 model params = expected mean of 777; p-value = 0.542726)\n", " Completed in 0.3s\n", " Iteration 5 took 0.3s\n", " \n", "--- Iterative MC2GST: Iter 06 of 10 1201 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 1154.51 (1201 data params - 40 model params = expected mean of 1161; p-value = 0.548163)\n", " Completed in 0.4s\n", " Iteration 6 took 0.4s\n", " \n", "--- Iterative MC2GST: Iter 07 of 10 1585 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 1600.17 (1585 data params - 40 model params = expected mean of 1545; p-value = 0.160387)\n", " Completed in 0.6s\n", " Iteration 7 took 0.6s\n", " \n", "--- Iterative MC2GST: Iter 08 of 10 1969 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 2031.01 (1969 data params - 40 model params = expected mean of 1929; p-value = 0.0520838)\n", " Completed in 0.9s\n", " Iteration 8 took 0.9s\n", " \n", "--- Iterative MC2GST: Iter 09 of 10 2353 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 2422.67 (2353 data params - 40 model params = expected mean of 2313; p-value = 0.055082)\n", " Completed in 1.4s\n", " Iteration 9 took 1.4s\n", " \n", "--- Iterative MC2GST: Iter 10 of 10 2737 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 2795.59 (2737 data params - 40 model params = expected mean of 2697; p-value = 0.0908715)\n", " Completed in 2.3s\n", " Iteration 10 took 2.3s\n", " \n", "Iterative MC2GST Total Time: 6.3s\n" ] } ], "source": [ "#Get lists of gate strings for successive iterations of LSGST to use\n", "specs = pygsti.construction.build_spam_specs(fiducialGateStrings=fiducialList)\n", "germList = pygsti.io.load_gatestring_list(\"tutorial_files/Example_GermsList.txt\")\n", "maxLengthList = json.load(open(\"tutorial_files/Example_maxLengths.json\",\"r\"))\n", "lsgstListOfLists = [ pygsti.io.load_gatestring_list(\"tutorial_files/Example_LSGSTlist%d.txt\" % l) for l in maxLengthList]\n", " \n", "#run MC2GST. The result is a GateSet containing the estimated quantities\n", "gs_mc2 = pygsti.do_iterative_mc2gst(ds, gs_clgst, lsgstListOfLists, verbosity=2)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#Write the resulting EXLGST and MC2GST results to gate set text files for later reference.\n", "pygsti.io.write_gateset(gs_exlgst, \"tutorial_files/Example_eLGST_Gateset.txt\",\"# Example result from running eLGST\")\n", "pygsti.io.write_gateset(gs_mc2, \"tutorial_files/Example_MC2GST_Gateset.txt\",\"# Example result from running MC2GST\")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--- Iterative MC2GST: Iter 01 of 10 92 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 64.0312 (92 data params - 40 model params = expected mean of 52; p-value = 0.122289)\n", " Completed in 0.1s\n", " Iteration 1 took 0.1s\n", " \n", "--- Iterative MC2GST: Iter 02 of 10 92 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 64.0312 (92 data params - 40 model params = expected mean of 52; p-value = 0.122289)\n", " Completed in 0.0s\n", " Iteration 2 took 0.0s\n", " \n", "--- Iterative MC2GST: Iter 03 of 10 168 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 161.612 (168 data params - 40 model params = expected mean of 128; p-value = 0.0237625)\n", " Completed in 0.1s\n", " Iteration 3 took 0.1s\n", " \n", "--- Iterative MC2GST: Iter 04 of 10 441 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 427.539 (441 data params - 40 model params = expected mean of 401; p-value = 0.173449)\n", " Completed in 0.2s\n", " Iteration 4 took 0.2s\n", " \n", "--- Iterative MC2GST: Iter 05 of 10 817 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 770.758 (817 data params - 40 model params = expected mean of 777; p-value = 0.556396)\n", " Completed in 0.3s\n", " Iteration 5 took 0.3s\n", " \n", "--- Iterative MC2GST: Iter 06 of 10 1201 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 1181.51 (1201 data params - 40 model params = expected mean of 1161; p-value = 0.331064)\n", " Completed in 0.5s\n", " Iteration 6 took 0.5s\n", " \n", "--- Iterative MC2GST: Iter 07 of 10 1585 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 1551.99 (1585 data params - 40 model params = expected mean of 1545; p-value = 0.445324)\n", " Completed in 0.7s\n", " Iteration 7 took 0.7s\n", " \n", "--- Iterative MC2GST: Iter 08 of 10 1969 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 1904.4 (1969 data params - 40 model params = expected mean of 1929; p-value = 0.650628)\n", " Completed in 0.9s\n", " Iteration 8 took 0.9s\n", " \n", "--- Iterative MC2GST: Iter 09 of 10 2353 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 2302.72 (2353 data params - 40 model params = expected mean of 2313; p-value = 0.556292)\n", " Completed in 1.4s\n", " Iteration 9 took 1.4s\n", " \n", "--- Iterative MC2GST: Iter 10 of 10 2737 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 2677.85 (2737 data params - 40 model params = expected mean of 2697; p-value = 0.599592)\n", " Completed in 1.5s\n", " Iteration 10 took 1.5s\n", " \n", "Iterative MC2GST Total Time: 5.5s\n" ] } ], "source": [ "#Run MC2GST again but use a DataSet with a lower number of counts \n", "gs_mc2_lowcnts = pygsti.do_iterative_mc2gst(dsLowCounts, gs_clgst, lsgstListOfLists, verbosity=2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Maximum Likelihood GST (MLGST)\n", "Executing MLGST is very similar to MC2GST: the same gate string lists can be used and calling syntax is nearly identitcal." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--- Iterative MLGST: Iter 01 of 10 92 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 40.9237 (92 data params - 40 model params = expected mean of 52; p-value = 0.866036)\n", " Completed in 0.1s\n", " 2*Delta(log(L)) = 41.1104\n", " Iteration 1 took 0.1s\n", " \n", "--- Iterative MLGST: Iter 02 of 10 92 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 40.9237 (92 data params - 40 model params = expected mean of 52; p-value = 0.866036)\n", " Completed in 0.0s\n", " 2*Delta(log(L)) = 41.1104\n", " Iteration 2 took 0.0s\n", " \n", "--- Iterative MLGST: Iter 03 of 10 168 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 124.56 (168 data params - 40 model params = expected mean of 128; p-value = 0.569542)\n", " Completed in 0.1s\n", " 2*Delta(log(L)) = 124.957\n", " Iteration 3 took 0.1s\n", " \n", "--- Iterative MLGST: Iter 04 of 10 441 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 424.143 (441 data params - 40 model params = expected mean of 401; p-value = 0.204567)\n", " Completed in 0.1s\n", " 2*Delta(log(L)) = 425.013\n", " Iteration 4 took 0.2s\n", " \n", "--- Iterative MLGST: Iter 05 of 10 817 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 772.113 (817 data params - 40 model params = expected mean of 777; p-value = 0.542726)\n", " Completed in 0.2s\n", " 2*Delta(log(L)) = 773.808\n", " Iteration 5 took 0.3s\n", " \n", "--- Iterative MLGST: Iter 06 of 10 1201 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 1154.51 (1201 data params - 40 model params = expected mean of 1161; p-value = 0.548163)\n", " Completed in 0.4s\n", " 2*Delta(log(L)) = 1156.61\n", " Iteration 6 took 0.4s\n", " \n", "--- Iterative MLGST: Iter 07 of 10 1585 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 1600.17 (1585 data params - 40 model params = expected mean of 1545; p-value = 0.160387)\n", " Completed in 0.5s\n", " 2*Delta(log(L)) = 1602.65\n", " Iteration 7 took 0.6s\n", " \n", "--- Iterative MLGST: Iter 08 of 10 1969 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 2031.01 (1969 data params - 40 model params = expected mean of 1929; p-value = 0.0520838)\n", " Completed in 0.9s\n", " 2*Delta(log(L)) = 2033.99\n", " Iteration 8 took 1.0s\n", " \n", "--- Iterative MLGST: Iter 09 of 10 2353 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 2422.67 (2353 data params - 40 model params = expected mean of 2313; p-value = 0.055082)\n", " Completed in 1.4s\n", " 2*Delta(log(L)) = 2426.07\n", " Iteration 9 took 1.5s\n", " \n", "--- Iterative MLGST: Iter 10 of 10 2737 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 2795.59 (2737 data params - 40 model params = expected mean of 2697; p-value = 0.0908715)\n", " Completed in 2.3s\n", " 2*Delta(log(L)) = 2799.4\n", " Iteration 10 took 2.6s\n", " \n", " Switching to ML objective (last iteration)\n", " --- MLGST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Maximum log(L) = 1399.31 below upper bound of -4.60013e+06\n", " 2*Delta(log(L)) = 2798.62 (2737 data params - 40 model params = expected mean of 2697; p-value = 0.0844567)\n", " Completed in 2.0s\n", " 2*Delta(log(L)) = 2798.62\n", " Final MLGST took 2.0s\n", " \n", "Iterative MLGST Total Time: 8.7s\n" ] } ], "source": [ "maxLengthList = json.load(open(\"tutorial_files/Example_maxLengths.json\",\"r\"))\n", "lsgstListOfLists = [ pygsti.io.load_gatestring_list(\"tutorial_files/Example_LSGSTlist%d.txt\" % l) for l in maxLengthList] \n", "\n", "#run MLGST. The result is a GateSet containing the estimated quantities\n", "gs_mle = pygsti.do_iterative_mlgst(ds, gs_clgst, lsgstListOfLists, verbosity=2)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--- Iterative MLGST: Iter 01 of 10 92 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 64.0312 (92 data params - 40 model params = expected mean of 52; p-value = 0.122289)\n", " Completed in 0.1s\n", " 2*Delta(log(L)) = 65.3199\n", " Iteration 1 took 0.1s\n", " \n", "--- Iterative MLGST: Iter 02 of 10 92 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 64.0312 (92 data params - 40 model params = expected mean of 52; p-value = 0.122289)\n", " Completed in 0.0s\n", " 2*Delta(log(L)) = 65.3199\n", " Iteration 2 took 0.0s\n", " \n", "--- Iterative MLGST: Iter 03 of 10 168 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 161.612 (168 data params - 40 model params = expected mean of 128; p-value = 0.0237625)\n", " Completed in 0.1s\n", " 2*Delta(log(L)) = 164.828\n", " Iteration 3 took 0.1s\n", " \n", "--- Iterative MLGST: Iter 04 of 10 441 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 427.539 (441 data params - 40 model params = expected mean of 401; p-value = 0.173449)\n", " Completed in 0.2s\n", " 2*Delta(log(L)) = 439.032\n", " Iteration 4 took 0.2s\n", " \n", "--- Iterative MLGST: Iter 05 of 10 817 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 770.758 (817 data params - 40 model params = expected mean of 777; p-value = 0.556396)\n", " Completed in 0.3s\n", " 2*Delta(log(L)) = 790.062\n", " Iteration 5 took 0.3s\n", " \n", "--- Iterative MLGST: Iter 06 of 10 1201 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 1181.51 (1201 data params - 40 model params = expected mean of 1161; p-value = 0.331064)\n", " Completed in 0.5s\n", " 2*Delta(log(L)) = 1209.23\n", " Iteration 6 took 0.5s\n", " \n", "--- Iterative MLGST: Iter 07 of 10 1585 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 1551.99 (1585 data params - 40 model params = expected mean of 1545; p-value = 0.445324)\n", " Completed in 0.6s\n", " 2*Delta(log(L)) = 1586.54\n", " Iteration 7 took 0.7s\n", " \n", "--- Iterative MLGST: Iter 08 of 10 1969 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 1904.4 (1969 data params - 40 model params = expected mean of 1929; p-value = 0.650628)\n", " Completed in 0.9s\n", " 2*Delta(log(L)) = 1945.36\n", " Iteration 8 took 1.0s\n", " \n", "--- Iterative MLGST: Iter 09 of 10 2353 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 2302.72 (2353 data params - 40 model params = expected mean of 2313; p-value = 0.556292)\n", " Completed in 1.4s\n", " 2*Delta(log(L)) = 2352.79\n", " Iteration 9 took 1.5s\n", " \n", "--- Iterative MLGST: Iter 10 of 10 2737 gate strings ---: \n", " --- Minimum Chi^2 GST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Sum of Chi^2 = 2677.85 (2737 data params - 40 model params = expected mean of 2697; p-value = 0.599592)\n", " Completed in 1.4s\n", " 2*Delta(log(L)) = 2735.38\n", " Iteration 10 took 1.7s\n", " \n", " Switching to ML objective (last iteration)\n", " --- MLGST ---\n", " Created evaluation tree with 1 subtrees. Will divide 1 procs into 1 (subtree-processing)\n", " groups of ~1 procs each, to distribute over 56 params (taken as 1 param groups of ~56 params).\n", " Maximum log(L) = 1360.55 below upper bound of -228670\n", " 2*Delta(log(L)) = 2721.09 (2737 data params - 40 model params = expected mean of 2697; p-value = 0.368373)\n", " Completed in 2.6s\n", " 2*Delta(log(L)) = 2721.09\n", " Final MLGST took 2.6s\n", " \n", "Iterative MLGST Total Time: 8.6s\n" ] } ], "source": [ "#Run MLGST again but use a DataSet with a lower number of counts \n", "gs_mle_lowcnts = pygsti.do_iterative_mlgst(dsLowCounts, gs_clgst, lsgstListOfLists, verbosity=2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compare MLGST with MC2GST\n", "\n", "Both MLGST and MC2GST use a $\\chi^{2}$ optimization procedure for all but the final iteration. For the last set of gatestrings (the last iteration), MLGST uses a maximum likelihood estimation. Below, we show how close the two estimates are to one another. First, we optimize the gauge so the estimated gates are as close to the target gates as the gauge degrees of freedom allow." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# We optimize over the gate set gauge\n", "gs_mle = pygsti.gaugeopt_to_target(gs_mle,depol_gateset)\n", "gs_mle_lowcnts = pygsti.gaugeopt_to_target(gs_mle_lowcnts,depol_gateset)\n", "gs_mc2 = pygsti.gaugeopt_to_target(gs_mc2,depol_gateset)\n", "gs_mc2_lowcnts = pygsti.gaugeopt_to_target(gs_mc2_lowcnts,depol_gateset)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Frobenius diff btwn MLGST and datagen = 0.020418\n", "Frobenius diff btwn MC2GST and datagen = 0.020418\n", "Frobenius diff btwn MLGST and LGST = 0.014499\n", "Frobenius diff btwn MLGST and MC2GST = 5.3e-05\n", "Chi^2 ( MC2GST ) = 2795.5891\n", "Chi^2 ( MLGST ) = 2796.2956\n", "LogL ( MC2GST ) = -4601529.7298\n", "LogL ( MLGST ) = -4601529.3414\n" ] } ], "source": [ "print(\"Frobenius diff btwn MLGST and datagen = {0}\".format(round(gs_mle.frobeniusdist(depol_gateset), 6)))\n", "print(\"Frobenius diff btwn MC2GST and datagen = {0}\".format(round(gs_mc2.frobeniusdist(depol_gateset), 6)))\n", "print(\"Frobenius diff btwn MLGST and LGST = {0}\".format(round(gs_mle.frobeniusdist(gs_clgst), 6)))\n", "print(\"Frobenius diff btwn MLGST and MC2GST = {0}\".format(round(gs_mle.frobeniusdist(gs_mc2), 6)))\n", "print(\"Chi^2 ( MC2GST ) = {0}\".format(round(pygsti.chi2(ds, gs_mc2, lsgstListOfLists[-1]), 4)))\n", "print(\"Chi^2 ( MLGST ) = {0}\".format(round(pygsti.chi2(ds, gs_mle, lsgstListOfLists[-1] ), 4)))\n", "print(\"LogL ( MC2GST ) = {0}\".format(round(pygsti.logl(gs_mc2, ds, lsgstListOfLists[-1]), 4)))\n", "print(\"LogL ( MLGST ) = {0}\".format(round(pygsti.logl(gs_mle, ds, lsgstListOfLists[-1]), 4)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that, as expected, the MC2GST estimate has a *slightly* lower $\\chi^{2}$ score than the MLGST estimate, and the MLGST estimate has a *slightly* higher loglikelihood than the MC2GST estimate. In addition, _both_ are close (in terms of the Frobenius difference) to the depolarized gateset. Which is good - it means GST is giving us estimates which are close to the _true_ gateset used to generate the data. Performing the same analysis with the low-count data shows larger differences between the two, which is expected since the $\\chi^2$ and loglikelihood statistics are more similar at large $N$, that is, for large numbers of samples." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "LOW COUNT DATA:\n", "Frobenius diff btwn MLGST and datagen = 0.0241\n", "Frobenius diff btwn MC2GST and datagen = 0.024054\n", "Frobenius diff btwn MLGST and LGST = 0.018807\n", "Frobenius diff btwn MLGST and MC2GST = 0.010513\n", "Chi^2 ( MC2GST ) = 2677.8492\n", "Chi^2 ( MLGST ) = 2692.2308\n", "LogL ( MC2GST ) = -230037.9222\n", "LogL ( MLGST ) = -230030.7786\n" ] } ], "source": [ "print(\"LOW COUNT DATA:\")\n", "print(\"Frobenius diff btwn MLGST and datagen = {0}\".format(round(gs_mle_lowcnts.frobeniusdist(depol_gateset), 6)))\n", "print(\"Frobenius diff btwn MC2GST and datagen = {0}\".format(round(gs_mc2_lowcnts.frobeniusdist(depol_gateset), 6)))\n", "print(\"Frobenius diff btwn MLGST and LGST = {0}\".format(round(gs_mle_lowcnts.frobeniusdist(gs_clgst), 6)))\n", "print(\"Frobenius diff btwn MLGST and MC2GST = {0}\".format(round(gs_mle_lowcnts.frobeniusdist(gs_mc2), 6)))\n", "print(\"Chi^2 ( MC2GST ) = {0}\".format(round(pygsti.chi2(dsLowCounts, gs_mc2_lowcnts, lsgstListOfLists[-1]), 4)))\n", "print(\"Chi^2 ( MLGST ) = {0}\".format(round(pygsti.chi2(dsLowCounts, gs_mle_lowcnts, lsgstListOfLists[-1] ), 4)))\n", "print(\"LogL ( MC2GST ) = {0}\".format(round(pygsti.logl(gs_mc2_lowcnts, dsLowCounts, lsgstListOfLists[-1]), 4)))\n", "print(\"LogL ( MLGST ) = {0}\".format(round(pygsti.logl(gs_mle_lowcnts, dsLowCounts, lsgstListOfLists[-1]), 4)))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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.5.2" } }, "nbformat": 4, "nbformat_minor": 0 }