{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Pure Data Analysis\n", "\n", "This tutorial covers different methods of analysing data *without* running GST. So far, there's only one, which checks for consistency between two (or more) datasets, called \"Data Set Comparison\".\n", "\n", "## Data Set Comparison\n", "This method declares that two or more `DataSet`s are \"inconsistent\" if the observed counts for the same gate strings across the data sets are inconsistent with being generated by the same underlying gateset. This protocol can be used to test for, among other things, drift and crosstalk. It can also be used\n", "to compare an experimental dataset to an \"ideal\" dataset. The methods in this tutorial have been presented in \"Probing context-dependent errors in quantum processors\" by Rudinger et al.\n", "\n", "Here we demonstrate the tool on simulated data from gate sets with Markovian errors. But this protocol can also be used regardless of the underlying error type." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from __future__ import division, print_function\n", "\n", "import pygsti\n", "import numpy as np\n", "import scipy\n", "from scipy import stats\n", "from pygsti.construction import std1Q_XYI" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "Let's first compare two `Dataset` objects where the underlying gate sets are the same. The data sets we'll use will be GST datasets (which allows us to do some nice visualization), but arbitrary datasets will work in general, provided that the gate sequences across the datasets are the same." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "#Let's make our underlying gate set have a little bit of random unitary noise.\n", "gs_exp_0 = std1Q_XYI.gs_target.copy()\n", "gs_exp_0 = gs_exp_0.randomize_with_unitary(.01,seed=0)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "germs = std1Q_XYI.germs\n", "fiducials = std1Q_XYI.fiducials\n", "max_lengths = [1,2,4,8,16,32,64,128,256]\n", "gate_sequences = pygsti.construction.make_lsgst_experiment_list(std1Q_XYI.gates,fiducials,fiducials,germs,max_lengths)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "#Generate the data for the two datasets, using the same gate set, with 100 repetitions of each sequence.\n", "N=100\n", "DS_0 = pygsti.construction.generate_fake_data(gs_exp_0,gate_sequences,N,'binomial',seed=10)\n", "DS_1 = pygsti.construction.generate_fake_data(gs_exp_0,gate_sequences,N,'binomial',seed=20)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Statistical hypothesis tests did NOT find inconsistency between the datasets at 5.00% significance.\n" ] } ], "source": [ "#Let's compare the two datasets.\n", "comparator_0_1 = pygsti.objects.DataComparator([DS_0,DS_1])\n", "comparator_0_1.implement(significance=0.05)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "
Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#Create a workspace to show plots\n", "w = pygsti.report.Workspace()\n", "w.init_notebook_mode(connected=False, autodisplay=True) " ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "
\n", "
\n", "
\n", "\n", "
\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#As we expect, the datasets are consistent!\n", "#We can also visualize this in a few ways:\n", "\n", "#This is will show a histogram of the p-values associated with the different strings.\n", "#If the null hypothesis (that the underlying gate sets are the same) is true,\n", "#then we expect the distribution to roughly follow the dotted green line.\n", "w.DatasetComparisonHistogramPlot(comparator_0_1, log=True, display='pvalue')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "
\n", "
\n", "
\n", "\n", "
\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Color box plot comparing two datasets from same gateset\n", "gssList = pygsti.construction.make_lsgst_structs(std1Q_XYI.gates, fiducials, fiducials, germs, max_lengths)\n", "w.ColorBoxPlot('dscmp', gssList[-1], None, None, dscomparator=comparator_0_1)\n", " #A lack of green boxes indicates consistency between datasets!" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "#Now let's generate data from two similar but not identical datasets and see if our tests can detect them." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "gs_exp_1 = std1Q_XYI.gs_target.copy()\n", "gs_exp_1 = gs_exp_1.randomize_with_unitary(.01,seed=1)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "DS_2 = pygsti.construction.generate_fake_data(gs_exp_1,gate_sequences,N,'binomial',seed=30)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The datasets are INCONSISTENT at 5.00% significance.\n", " - Details:\n", " - The aggregate log-likelihood ratio test is significant at 629.10 standard deviations.\n", " - The aggregate log-likelihood ratio test standard deviations signficance threshold is 1.98\n", " - The number of sequences with data that is inconsistent is 718\n", " - The maximum SSTVD over all sequences is 1.00\n", " - The maximum SSTVD was observed for GxGx(GxGiGi)^42\n" ] } ], "source": [ "#Let's make the comparator and get the report.\n", "comparator_1_2 = pygsti.objects.DataComparator([DS_1,DS_2])\n", "comparator_1_2.implement(significance=0.05)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
\n", "
\n", "
\n", "
\n", "\n", "
\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#The datasets are significantly inconsistent! Let's see what the distribution of p-values looks like now:\n", "w.DatasetComparisonHistogramPlot(comparator_1_2)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
\n", "
\n", "
\n", "
\n", "\n", "
\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w.ColorBoxPlot('dscmp', gssList[-1], None, None, dscomparator=comparator_1_2)\n", "#The colored boxes indicate inconsistency between datasets!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you'd like to extract the various quantities calculated by the DataComparator, use the `.get_` methods. There are methods for extracting all of the quantities discussed in \"Probing context-dependent errors in quantum processors\" by Rudinger et al. For example, below we extract the Jensen-Shannon divergence for a particular gatestring:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "GxGx(GxGiGi)^21GxGxGx\n", "0.19430446226785072\n" ] } ], "source": [ "gstr = DS_1.keys()[2000]\n", "print(gstr)\n", "print(comparator_1_2.get_JSD(gstr))" ] } ], "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.7.0" } }, "nbformat": 4, "nbformat_minor": 1 }