{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Plotting Tutorial\n", "PyGSTi provides support for many different _plots_ you may want to make with your estimates. In this tutorial, we give examples of how to generate the more commonly-used plots using the functions within `pygsti.report.plotting`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from __future__ import print_function" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import pygsti\n", "import pygsti.report.plotting as pplt\n", "import json\n", "%pylab inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup\n", "Gather the data we need to create some example plots." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#Gate sets\n", "gs_target = pygsti.io.load_gateset(\"tutorial_files/Example_Gateset.txt\")\n", "gs_mc2gst = pygsti.io.load_gateset(\"tutorial_files/Example_MC2GST_Gateset.txt\")\n", "gs_elgst = pygsti.io.load_gateset(\"tutorial_files/Example_eLGST_Gateset.txt\")\n", "\n", "#Data set\n", "ds = pygsti.io.load_dataset(\"tutorial_files/Example_Dataset.txt\", cache=False)\n", "\n", "#Fiducials, germs, and maximum lengths (necessary to label the plots)\n", "fiducialList = pygsti.io.load_gatestring_list(\"tutorial_files/Example_FiducialList.txt\")\n", "germList = pygsti.io.load_gatestring_list(\"tutorial_files/Example_GermsList.txt\")\n", "maxLengthList = json.load(open(\"tutorial_files/Example_maxLengths.json\",\"r\"))\n", "specs = pygsti.construction.build_spam_specs(fiducialGateStrings=fiducialList)\n", "strs = pygsti.construction.get_spam_strs(specs)\n", "\n", "print(\"Gates (%d): \" % len(gs_target.gates), gs_target.gates.keys())\n", "print(\"Fiducials (%d): \" % len(fiducialList), map(str,fiducialList))\n", "print(\"Germs (%d): \" % len(germList),map(str,germList))\n", "print(\"Dataset has %d gate strings\" % len(ds))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pplt.gate_matrix_boxplot(gs_target['Gx'], mxBasis=\"pp\", mxBasisDims=2, xlabel=\"testX\", ylabel=\"testY\", title=\"mytitle\", boxLabels=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pplt.pauliprod_hamiltonian_boxplot(gs_mc2gst['Gx'],gs_target['Gx'],mxBasis=\"gm\",boxLabels=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Many of the plots in the following examples plot groups of boxes. Each group corresponds to a particular \"base\" gate string that is sandwiched between sets of fiducial gate strings to create the group. Here we create a \"gate-string-dictionary\" which associates a base gate string with a (x,y) coordinate pair. Plotting functions take sets of X-coordinates (`Xs`) and Y-coordinates (`Ys`) to plot, along with a gate string dictionary (`gateStrDict`) which serves as a lookkup table for base gate strings. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#Collect data we need for making plots\n", "Xs = maxLengthList[1:]; xlbl = r\"$L$ (max length)\"\n", "Ys = germList; ylbl = \"Germ\"\n", "gateStrDict = { (x,y):pygsti.construction.repeat_with_max_length(y,x,False) for x in Xs for y in Ys }\n", "\n", "#remove duplicates by replacing duplicate strings with None\n", "runningList = []\n", "for x in Xs:\n", " for y in Ys:\n", " if gateStrDict[(x,y)] in runningList:\n", " gateStrDict[(x,y)] = None\n", " else: runningList.append( gateStrDict[(x,y)] )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## $\\chi^2$ Plots\n", "\n", "One of the most straightforward ways to evaluate GST estimates is to ask whether the estimates correctly reproduce the observed frequencies of the counts (or \"clicks\") for the experiments. The answer to this question is to compute the $\\chi^{2}$ function for the gateset." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [], "source": [ "#Demonstrates a blank chi2 boxplot\n", "pplt.blank_boxplot( Xs, Ys, gateStrDict, strs, xlbl, ylbl, sumUp=True, ticSize=20)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "#Compute the chi2 scores for the estimated gateset using the MC2GST algorithm\n", "pplt.chi2_boxplot( Xs, Ys, gateStrDict, ds, gs_mc2gst, strs, xlbl, ylbl,\n", " scale=1.0, sumUp=False, histogram=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Because most of the $\\chi^{2}$ values are close to 0, we conclude this estimated gateset does a good job of reproducing the observed frequencies. Which it should, since the figure of merit we were using in our estimation was the $\\chi^{2}$ function!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "#Make another chi2 plot, with different x and y labels\n", "pplt.chi2_boxplot( Xs, Ys, gateStrDict, ds, gs_mc2gst, strs, xlbl, ylbl,\n", " scale=1.0, sumUp=False, histogram=True, invert=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#Sum up the chi2 boxes for each (Germ, L) pair\n", "pplt.chi2_boxplot( Xs, Ys, gateStrDict, ds, gs_mc2gst, strs, xlbl, ylbl,\n", " scale=1.0, sumUp=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Direct $\\chi^2$ Plots" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "directLGST = pplt.direct_lgst_gatesets( [gs for gs in gateStrDict.values() if gs is not None],\n", " ds, specs, gs_target, svdTruncateTo=4, verbosity=2)\n", "directLSGST = pplt.direct_mc2gst_gatesets( [gs for gs in gateStrDict.values() if gs is not None],\n", " ds, specs, gs_target, svdTruncateTo=4, minProbClipForWeighting=1e-2, \n", " probClipInterval=(-1e6,1e6), verbosity=2)\n", "\n", "#focusedGS = AT.focused_mc2gst_gatesets( [gs for gs in gateStrDict.values() if gs is not None],\n", "# ds, specs, gs_mc2gst, minProbClipForWeighting=1e-2, \n", "# probClipInterval=(-1e6,1e6), verbosity=2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "pplt.direct_chi2_boxplot( Xs, Ys, gateStrDict, ds, directLSGST, strs, xlbl, ylbl,\n", " scale=1.0, boxLabels=True )" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pplt.direct_deviation_boxplot(Xs, Ys, gateStrDict, ds, gs_mc2gst, directLSGST, xlbl, ylbl, prec=4,\n", " scale=1.0, boxLabels=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pplt.direct2x_comp_boxplot( Xs, Ys, gateStrDict, ds, directLSGST, strs, xlbl, ylbl,\n", " scale=1.0, boxLabels=True )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Error Rate Plots (uses Direct-X results)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pplt.small_eigval_err_rate_boxplot(Xs, Ys, gateStrDict, ds, directLSGST, xlbl, ylbl,\n", " scale=1.0, boxLabels=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Whack-a-$\\chi^2$-mole Plots" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [], "source": [ "whack = pygsti.objects.GateString( ('Gi',)*256 )\n", "fullGatestringList = pygsti.io.load_gatestring_list(\"tutorial_files/Example_LSGSTlist256.txt\")\n", "pplt.whack_a_chi2_mole_boxplot( whack, fullGatestringList, Xs, Ys, gateStrDict, ds, gs_mc2gst, strs, xlbl, ylbl,\n", " scale=1.0, sumUp=False, histogram=True )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Other plots" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pplt.gof_boxplot_keyplot(strs)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pplt.gate_matrix_boxplot(gs_target['Gx'], mxBasis=\"gm\", mxBasisDims=2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pplt.polar_eigenval_plot(gs_mc2gst['Gx'], gs_target['Gx'], size=(4,4), title=\"Gx\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pplt.choi_eigenvalue_barplot([2,4,5,7,-2,-3],[1,2,1,1,1,1])" ] }, { "cell_type": "code", "execution_count": null, "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.1" } }, "nbformat": 4, "nbformat_minor": 0 }