{ "cells": [ { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "# Workspace Pickling\n", "This example notebook shows how to generate reports with a pickled workspace object. \n", "First, we run GST:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [], "source": [ "import pygsti\n", "import pickle\n", "from pygsti.construction import std1Q_XYI\n", "\n", "gs_target = std1Q_XYI.gs_target\n", "fiducials = std1Q_XYI.fiducials\n", "germs = std1Q_XYI.germs\n", "maxLengths = [1,2,4]\n", "#maxLengths = [1, 2, 4, 8, 16, 32, 64]\n", "\n", "#Generate some data\n", "gs_datagen = gs_target.depolarize(gate_noise=0.1, spam_noise=0.001)\n", "gs_datagen = gs_datagen.rotate(rotate=0.04)\n", "listOfExperiments = pygsti.construction.make_lsgst_experiment_list(gs_target, fiducials, fiducials, germs, maxLengths)\n", "ds = pygsti.construction.generate_fake_data(gs_datagen, listOfExperiments, nSamples=1000,\n", " sampleError=\"binomial\", seed=1234)\n", "#Run GST\n", "gs_target.set_all_parameterizations(\"TP\") #TP-constrained\n", "results = pygsti.do_long_sequence_gst(ds, gs_target, fiducials, fiducials, germs,\n", " maxLengths, verbosity=0)\n", "with open('example_files/example_report_results.pkl', 'wb') as outfile:\n", " pickle.dump(results, outfile, protocol=2)\n", "\n", "# Case1: TP-constrained GST\n", "tpTarget = gs_target.copy()\n", "tpTarget.set_all_parameterizations(\"TP\")\n", "results_tp = pygsti.do_long_sequence_gst(ds, tpTarget, fiducials, fiducials, germs,\n", " maxLengths, gaugeOptParams=False, verbosity=0)\n", "# Gauge optimize\n", "est = results_tp.estimates['default']\n", "gsFinal = est.gatesets['final iteration estimate']\n", "gsTarget = est.gatesets['target']\n", "for spamWt in [1e-4,1e-3,1e-2,1e-1,1.0]:\n", " gs = pygsti.gaugeopt_to_target(gsFinal,gsTarget,{'gates':1, 'spam':spamWt})\n", " est.add_gaugeoptimized({'itemWeights': {'gates':1, 'spam':spamWt}}, gs, \"Spam %g\" % spamWt) \n", "\n", "#Case2: \"Full\" GST\n", "fullTarget = gs_target.copy()\n", "fullTarget.set_all_parameterizations(\"full\")\n", "results_full = pygsti.do_long_sequence_gst(ds, fullTarget, fiducials, fiducials, germs,\n", " maxLengths, gaugeOptParams=False, verbosity=0)\n", "#Gauge optimize\n", "est = results_full.estimates['default']\n", "gsFinal = est.gatesets['final iteration estimate']\n", "gsTarget = est.gatesets['target']\n", "for spamWt in [1e-4,1e-3,1e-2,1e-1,1.0]:\n", " gs = pygsti.gaugeopt_to_target(gsFinal,gsTarget,{'gates':1, 'spam':spamWt})\n", " est.add_gaugeoptimized({'itemWeights': {'gates':1, 'spam':spamWt}}, gs, \"Spam %g\" % spamWt)\n", "\n", "with open('example_files/full_report_results.pkl', 'wb') as outfile:\n", " pickle.dump((results_tp, results_full), outfile, protocol=2)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "## Workspace Pickling\n", "Now that we've saved some GST data, we can begin generating reports. \n", "The smart cache will track which plots we are unable to pickle. \n", "At the time of writing, these are only plotly plots. `ReportTable` objects should delete their `ws` and `figs` members, allowing themselves to be pickled. " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "pickle loading took 0.027332 seconds\n", "\n", "*** Creating workspace ***\n", "*** Generating switchboard tables ***\n", "*** Generating plots ***\n", "*** Merging into template file ***\n", "Output written to tutorial_files/exampleMultiGenReport.html\n", "TP/Full multi report took 4.755988 seconds\n", "\n", "Unpickled keys:\n", "set()\n" ] } ], "source": [ "from pygsti.tools import timed_block\n", "\n", "with timed_block('pickle loading'):\n", " with open('example_files/full_report_results.pkl', 'rb') as infile:\n", " results_tp, results_full = pickle.load(infile)\n", "with timed_block('TP/Full multi report'):\n", " ws = pygsti.report.create_general_report({'TP': results_tp, \"Full\": results_full},\n", " \"tutorial_files/exampleMultiGenReport.html\",\n", " \"Workspace Pickling Example Report1\", \n", " verbosity=3, auto_open=False)\n", "ws.save_cache('example_files/wscache.pkl', True)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "In the Cell below, we create the same report, but use the existing workspace object." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "*** Creating workspace ***\n", "*** Generating switchboard tables ***\n", "*** Generating plots ***\n", "*** Merging into template file ***\n", "Output written to tutorial_files/exampleMultiGenReport.html\n", "TP/Full multi report took 1.400488 seconds\n", "\n" ] } ], "source": [ "with timed_block('TP/Full multi report'):\n", " ws = pygsti.report.create_general_report({'TP': results_tp, \"Full\": results_full},\n", " \"tutorial_files/exampleMultiGenReport.html\",\n", " \"Workspace Pickling Example Report2\", \n", " verbosity=3, auto_open=False, ws=ws)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "However, most of the time, we care about the time of the full report generation progress:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "*** Creating workspace ***\n", "*** Generating switchboard tables ***\n", "*** Generating plots ***\n", "*** Merging into template file ***\n", "Output written to tutorial_files/exampleMultiGenReport.html\n", "Full report creation from pickle took 1.527072 seconds\n", "\n" ] } ], "source": [ "del ws\n", "with timed_block('Full report creation from pickle'):\n", " ws = pygsti.report.Workspace('example_files/wscache.pkl')\n", " pygsti.report.create_general_report({'TP': results_tp, \"Full\": results_full},\n", " \"tutorial_files/exampleMultiGenReport.html\",\n", " \"Workspace Pickling Example Report3\", \n", " verbosity=3, auto_open=False, ws=ws)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "deletable": true, "editable": 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": 2 }