{ "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 operation sequences across the data sets are inconsistent with being generated by the same underlying model. 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 models with Markovian errors. But this protocol can also be used regardless of the underlying error type." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pygsti\n", "import numpy as np\n", "import scipy\n", "from scipy import stats\n", "from pygsti.modelpacks import smq1Q_XYI" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "Let's first compare two `Dataset` objects where the underlying models 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 operation sequences across the datasets are the same." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Let's make our underlying model have a little bit of random unitary noise.\n", "mdl_exp_0 = smq1Q_XYI.target_model()\n", "mdl_exp_0 = mdl_exp_0.randomize_with_unitary(.01,seed=0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "germs = smq1Q_XYI.germs()\n", "prep_fiducials = smq1Q_XYI.prep_fiducials()\n", "meas_fiducials = smq1Q_XYI.meas_fiducials()\n", "max_lengths = [1,2,4,8,16,32,64,128,256]\n", "gate_sequences = pygsti.construction.create_lsgst_circuits(smq1Q_XYI.gates,prep_fiducials,meas_fiducials,germs,max_lengths)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Generate the data for the two datasets, using the same model, with 100 repetitions of each sequence.\n", "N=100\n", "DS_0 = pygsti.construction.simulate_data(mdl_exp_0,gate_sequences,N,'binomial',seed=10)\n", "DS_1 = pygsti.construction.simulate_data(mdl_exp_0,gate_sequences,N,'binomial',seed=20)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Let's compare the two datasets.\n", "comparator_0_1 = pygsti.objects.DataComparator([DS_0,DS_1])\n", "comparator_0_1.run(significance=0.05)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "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": null, "metadata": {}, "outputs": [], "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 models 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": null, "metadata": {}, "outputs": [], "source": [ "#Color box plot comparing two datasets from same model\n", "lists = pygsti.construction.create_lsgst_circuit_lists(smq1Q_XYI.gates, prep_fiducials, meas_fiducials, germs, max_lengths)\n", "w.ColorBoxPlot('dscmp', lists[-1], None, None, dscomparator=comparator_0_1)\n", "#A lack of green boxes indicates consistency between datasets!" ] }, { "cell_type": "code", "execution_count": null, "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": null, "metadata": {}, "outputs": [], "source": [ "mdl_exp_1 = smq1Q_XYI.target_model()\n", "mdl_exp_1 = mdl_exp_1.randomize_with_unitary(.01,seed=1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "DS_2 = pygsti.construction.simulate_data(mdl_exp_1,gate_sequences,N,'binomial',seed=30)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "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.run(significance=0.05)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "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": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "w.ColorBoxPlot('dscmp', lists[-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 API 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 circuit:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "opstr = list(DS_1.keys())[20]\n", "print(opstr.format_display_str(80))\n", "print(comparator_1_2.jsd(opstr))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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.8.5" } }, "nbformat": 4, "nbformat_minor": 1 }