{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Testing variable-gateset-dimension GST with model selection" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Setup" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Populating the interactive namespace from numpy and matplotlib\n" ] } ], "source": [ "import pygsti\n", "%pylab inline" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#Load gateset and some string lists\n", "gs_target = pygsti.io.load_gateset(\"tutorial_files/Example_Gateset.txt\")\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", "specs = pygsti.construction.build_spam_specs(fiducialList)\n", "expList = [1,2,4]\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#Create some testing gate string lists\n", "lgstList = pygsti.construction.list_lgst_gatestrings(specs, gs_target.keys())\n", "lsgstLists = [ lgstList[:] ]\n", "for exp in expList:\n", " gsList = pygsti.construction.create_gatestring_list(\n", " \"f0+germ*exp+f1\", f0=fiducialList, f1=fiducialList,\n", " germ=germList, exp=exp, order=['germ','f0','f1'])\n", " lsgstLists.append( lsgstLists[-1] + gsList )\n", " \n", "dsList = pygsti.remove_duplicates( lsgstLists[-1] )" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Gi', 'Gx', 'Gy']\n" ] } ], "source": [ "#Test on fake data by depolarizing target set, increasing it's dimension,\n", "# and adding leakage to the gates into the new dimension.\n", "\n", "gs_dataGen4 = gs_target.depolarize(gate_noise=0.1)\n", "gs_dataGen5 = gs_dataGen4.increase_dimension(5)\n", "leakGate = pygsti.construction.build_gate( [2,1],[('Q0',),('L0',)] , \"LX(pi/4.0,0,2)\",\"gm\") # X(pi,Q0)*LX(pi,0,2)\n", "\n", "gs_dataGen5.set_gate('Gx', pygsti.objects.compose( gs_dataGen5.get_gate('Gx'), leakGate))\n", "gs_dataGen5.set_gate('Gy', pygsti.objects.compose( gs_dataGen5.get_gate('Gy'), leakGate))\n", "print gs_dataGen5.keys()\n", "\n", "#Some debugging...\n", "#NOTE: with LX(pi,0,2) above, dim 5 test will choose a dimension 3 gateset, which may be sensible\n", "# looking at the gate matrices in this case... but maybe LX(pi,...) is faulty?\n", "#print gs_dataGen4\n", "#print gs_dataGen5\n", "\n", "#Jmx = GST.JOps.jamiolkowski_iso(gs_dataGen4['Gx'])\n", "#Jmx = GST.JOps.jamiolkowski_iso(gs_dataGen5['Gx'],dimOrStateSpaceDims=[2,1])\n", "#print \"J = \\n\",Jmx\n", "#print \"evals = \",eigvals(Jmx)\n", "\n", "dsFake4 = pygsti.construction.generate_fake_data(gs_dataGen4, dsList, nSamples=1000000, sampleError=\"binomial\", seed=1234)\n", "dsFake5 = pygsti.construction.generate_fake_data(gs_dataGen5, dsList, nSamples=1000000, sampleError=\"binomial\", seed=1234)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of gates = 3\n", "Number of fiducials = 6\n", "Maximum length for a gate string in ds = 30\n", "Number of LGST strings = 92\n", "Number of LSGST strings = [92, 488, 884, 1280]\n" ] } ], "source": [ "print \"Number of gates = \",len(gs_target.keys())\n", "print \"Number of fiducials =\",len(fiducialList)\n", "print \"Maximum length for a gate string in ds =\",max(map(len,dsList))\n", "print \"Number of LGST strings = \",len(lgstList)\n", "print \"Number of LSGST strings = \",map(len,lsgstLists)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Test using dimension-4 fake data" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "LGST: Singular values of I_tilde (truncating to first 4 of 6) = \n", "[ 3.00484791e+00 8.25822588e-01 6.70903966e-01 6.65330643e-01\n", " 1.32123551e-03 7.62101431e-04]\n", "\n", "--- LGST ---\n", "LGST: Singular values of I_tilde (truncating to first 6 of 6) = \n", "[ 3.00484791e+00 8.25822588e-01 6.70903966e-01 6.65330643e-01\n", " 1.32123551e-03 7.62101431e-04]\n", "LGST: Padding target B with sqrt of low singular values of I_tilde: \n", "[ 0.00132124 0.0007621 ]\n", "\n", "--- LGST ---\n", "LGST dim=4 chiSq = 584.010103572\n", "LGST dim=6 chiSq = 218.874929215\n", "\n", "--- Iterative LSGST: Beginning iter 1 of 4 : 92 gate strings ---\n", "--- Least Squares GST with model selection (starting dim = 4) ---\n", "--- Least Squares GST ---\n", " Sum of Chi^2 = 58.5712 (92 data params - 40 model params = expected mean of 52; p-value = 0.247042)\n", "Dim 4: chi^2 = 58.5712, nGateStrings=92, nParams=56 (so expected mean = 36)\n", "--- Least Squares GST ---\n", " Sum of Chi^2 = 5.51511e+06 (92 data params - 24 model params = expected mean of 68; p-value = 0)\n", "Rejected dim 3: chi^2 = 5.51511e+06 (+5.51505e+06 w.r.t. expected mean of 92 strings - 33 params = 59) (dChi^2=5515046, 2*dParams=-46)\n", "--- Least Squares GST ---\n", " Sum of Chi^2 = 36.8586 (92 data params - 60 model params = expected mean of 32; p-value = 0.254145)\n", "Rejected dim 5: chi^2 = 36.8586 (+29.8586 w.r.t. expected mean of 92 strings - 85 params = 7) (dChi^2=-21, 2*dParams=58)\n", "\n", "--- Iterative LSGST: Beginning iter 2 of 4 : 488 gate strings ---\n", "--- Least Squares GST with model selection (starting dim = 4) ---\n", "--- Least Squares GST ---\n", " Sum of Chi^2 = 437.023 (488 data params - 40 model params = expected mean of 448; p-value = 0.635868)\n", "Dim 4: chi^2 = 437.023, nGateStrings=488, nParams=56 (so expected mean = 432)\n", "--- Least Squares GST ---\n", " Sum of Chi^2 = 3.39746e+07 (488 data params - 24 model params = expected mean of 464; p-value = 0)\n", "Rejected dim 3: chi^2 = 3.39746e+07 (+3.39742e+07 w.r.t. expected mean of 488 strings - 33 params = 455) (dChi^2=33974191, 2*dParams=-46)\n", "--- Least Squares GST ---\n", " Sum of Chi^2 = 391.592 (488 data params - 60 model params = expected mean of 428; p-value = 0.89591)\n", "Rejected dim 5: chi^2 = 391.592 (-11.4078 w.r.t. expected mean of 488 strings - 85 params = 403) (dChi^2=-45, 2*dParams=58)\n", "\n", "--- Iterative LSGST: Beginning iter 3 of 4 : 884 gate strings ---\n", "--- Least Squares GST with model selection (starting dim = 4) ---\n", "--- Least Squares GST ---\n", " Sum of Chi^2 = 859.588 (884 data params - 40 model params = expected mean of 844; p-value = 0.347052)\n", "Dim 4: chi^2 = 859.588, nGateStrings=884, nParams=56 (so expected mean = 828)\n", "--- Least Squares GST ---\n", " Sum of Chi^2 = 4.28947e+07 (884 data params - 24 model params = expected mean of 860; p-value = 0)\n", "Rejected dim 3: chi^2 = 4.28947e+07 (+4.28939e+07 w.r.t. expected mean of 884 strings - 33 params = 851) (dChi^2=42893852, 2*dParams=-46)\n", "--- Least Squares GST ---\n", " Sum of Chi^2 = 803.152 (884 data params - 60 model params = expected mean of 824; p-value = 0.691985)\n", "Rejected dim 5: chi^2 = 803.152 (+4.15226 w.r.t. expected mean of 884 strings - 85 params = 799) (dChi^2=-56, 2*dParams=58)\n", "\n", "--- Iterative LSGST: Beginning iter 4 of 4 : 1280 gate strings ---\n", "--- Least Squares GST with model selection (starting dim = 4) ---\n", "--- Least Squares GST ---\n", " Sum of Chi^2 = 1262.44 (1280 data params - 40 model params = expected mean of 1240; p-value = 0.322291)\n", "Dim 4: chi^2 = 1262.44, nGateStrings=1280, nParams=56 (so expected mean = 1224)\n", "--- Least Squares GST ---\n", " Sum of Chi^2 = 4.94824e+07 (1280 data params - 24 model params = expected mean of 1256; p-value = 0)\n", "Rejected dim 3: chi^2 = 4.94824e+07 (+4.94812e+07 w.r.t. expected mean of 1280 strings - 33 params = 1247) (dChi^2=49481143, 2*dParams=-46)\n", "--- Least Squares GST ---\n", " Sum of Chi^2 = 1225.25 (1280 data params - 60 model params = expected mean of 1220; p-value = 0.452425)\n", "Rejected dim 5: chi^2 = 1225.25 (+30.2457 w.r.t. expected mean of 1280 strings - 85 params = 1195) (dChi^2=-37, 2*dParams=58)\n" ] } ], "source": [ "#Run LGST to get an initial estimate for the gates in gs_target based on the data in ds\n", "# NOTE: with nSamples less than 1M (100K, 10K, 1K) this routine will choose a higher-than-4 dimensional gateset\n", "ds = dsFake4\n", "gs_lgst4 = pygsti.do_lgst(ds, specs, targetGateset=gs_target, svdTruncateTo=4, verbosity=3)\n", "gs_lgst6 = pygsti.do_lgst(ds, specs, targetGateset=gs_target, svdTruncateTo=6, verbosity=3)\n", "\n", "#Print chi^2 of 4-dim and 6-dim estimates\n", "chiSq4 = pygsti.chi2(ds, gs_lgst4, lgstList, minProbClipForWeighting=1e-4)\n", "chiSq6 = pygsti.chi2(ds, gs_lgst6, lgstList, minProbClipForWeighting=1e-4)\n", "print \"LGST dim=4 chiSq = \",chiSq4\n", "print \"LGST dim=6 chiSq = \",chiSq6\n", "\n", "# Least squares GST with model selection\n", "gs_lsgst = pygsti.do_iterative_mc2gst_with_model_selection(ds, gs_lgst4, 1, lsgstLists, verbosity=2,\n", " minProbClipForWeighting=1e-3, probClipInterval=(-1e5,1e5))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "rhoVec[0] = 0.7071 0.0245 -0.0259 0.7537\n", "\n", "\n", "EVec[0] = 0.6838 -0.0010 0.0062 -0.6413\n", "\n", "\n", "Gi = \n", " 1.0000 0 0 0\n", " 0.0033 0.9001 -0.0001 0.0003\n", " -0.0037 0 0.8998 0.0002\n", " -0.0038 -0.0001 0.0002 0.9000\n", "\n", "\n", "Gx = \n", " 1.0000 0 0 0\n", " 0.0030 0.8998 0.0120 -0.0166\n", " -0.0019 0.0023 -0.0104 0.9883\n", " -0.0674 -0.0003 -0.8197 0.0103\n", "\n", "\n", "Gy = \n", " 1.0000 0 0 0\n", " -0.0047 0.0004 -0.0038 -0.9997\n", " -0.0038 -0.0004 0.9000 -0.0020\n", " -0.0622 0.8102 0.0207 -0.0006\n", "\n", "\n", "\n" ] } ], "source": [ "print gs_lsgst" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Test using dimension-5 fake data" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "LGST: Singular values of I_tilde (truncating to first 4 of 6) = \n", "[ 2.36368304e+00 6.59319496e-01 4.68883491e-01 4.54819490e-01\n", " 3.15418560e-03 8.91359166e-04]\n", "\n", "--- LGST ---\n", "LGST: Singular values of I_tilde (truncating to first 6 of 6) = \n", "[ 2.36368304e+00 6.59319496e-01 4.68883491e-01 4.54819490e-01\n", " 3.15418560e-03 8.91359166e-04]\n", "LGST: Padding target B with sqrt of low singular values of I_tilde: \n", "[ 0.00315419 0.00089136]\n", "\n", "--- LGST ---\n", "LGST dim=4 chiSq = 1316593.37275\n", "LGST dim=6 chiSq = 580943.33613\n", "\n", "--- Iterative LSGST: Beginning iter 1 of 4 : 92 gate strings ---\n", "--- Least Squares GST with model selection (starting dim = 4) ---\n", "--- Least Squares GST ---\n", " Sum of Chi^2 = 143358 (92 data params - 40 model params = expected mean of 52; p-value = 0)\n", "Dim 4: chi^2 = 143358, nGateStrings=92, nParams=56 (so expected mean = 36)\n", "--- Least Squares GST ---\n", " Sum of Chi^2 = 3.0845e+06 (92 data params - 24 model params = expected mean of 68; p-value = 0)\n", "Rejected dim 3: chi^2 = 3.0845e+06 (+3.08444e+06 w.r.t. expected mean of 92 strings - 33 params = 59) (dChi^2=2941145, 2*dParams=-46)\n", "--- Least Squares GST ---\n", " Sum of Chi^2 = 48.7083 (92 data params - 60 model params = expected mean of 32; p-value = 0.0295689)\n", "Selected dim 5: chi^2 = 48.7083 (+41.7083 w.r.t. expected mean of 92 strings - 85 params = 7) (dChi^2=-143309, 2*dParams=58)\n", "\n", "--- Iterative LSGST: Beginning iter 2 of 4 : 488 gate strings ---\n", "--- Least Squares GST with model selection (starting dim = 5) ---\n", "--- Least Squares GST ---\n", " Sum of Chi^2 = 401.245 (488 data params - 60 model params = expected mean of 428; p-value = 0.818942)\n", "Dim 5: chi^2 = 401.245, nGateStrings=488, nParams=85 (so expected mean = 403)\n", "--- Least Squares GST ---\n", " Sum of Chi^2 = 870354 (488 data params - 40 model params = expected mean of 448; p-value = 0)\n", "Rejected dim 4: chi^2 = 870354 (+869922 w.r.t. expected mean of 488 strings - 56 params = 432) (dChi^2=869952, 2*dParams=-58)\n", "--- Least Squares GST ---\n", " Sum of Chi^2 = 332.471 (488 data params - 84 model params = expected mean of 404; p-value = 0.996076)\n", "Rejected dim 6: chi^2 = 332.471 (-35.5285 w.r.t. expected mean of 488 strings - 120 params = 368) (dChi^2=-68, 2*dParams=70)\n", "\n", "--- Iterative LSGST: Beginning iter 3 of 4 : 884 gate strings ---\n", "--- Least Squares GST with model selection (starting dim = 5) ---\n", "--- Least Squares GST ---\n", " Sum of Chi^2 = 820.581 (884 data params - 60 model params = expected mean of 824; p-value = 0.527076)\n", "Dim 5: chi^2 = 820.581, nGateStrings=884, nParams=85 (so expected mean = 799)\n", "--- Least Squares GST ---\n", " Sum of Chi^2 = 1.73941e+06 (884 data params - 40 model params = expected mean of 844; p-value = 0)\n", "Rejected dim 4: chi^2 = 1.73941e+06 (+1.73858e+06 w.r.t. expected mean of 884 strings - 56 params = 828) (dChi^2=1738590, 2*dParams=-58)\n", "--- Least Squares GST ---\n", " Sum of Chi^2 = 761.55 (884 data params - 84 model params = expected mean of 800; p-value = 0.831561)\n", "Rejected dim 6: chi^2 = 761.55 (-2.44962 w.r.t. expected mean of 884 strings - 120 params = 764) (dChi^2=-59, 2*dParams=70)\n", "\n", "--- Iterative LSGST: Beginning iter 4 of 4 : 1280 gate strings ---\n", "--- Least Squares GST with model selection (starting dim = 5) ---\n", "--- Least Squares GST ---\n", " Sum of Chi^2 = 1230.81 (1280 data params - 60 model params = expected mean of 1220; p-value = 0.40843)\n", "Dim 5: chi^2 = 1230.81, nGateStrings=1280, nParams=85 (so expected mean = 1195)\n", "--- Least Squares GST ---\n", " Sum of Chi^2 = 2.69389e+06 (1280 data params - 40 model params = expected mean of 1240; p-value = 0)\n", "Rejected dim 4: chi^2 = 2.69389e+06 (+2.69267e+06 w.r.t. expected mean of 1280 strings - 56 params = 1224) (dChi^2=2692661, 2*dParams=-58)\n", "--- Least Squares GST ---\n", " Sum of Chi^2 = 1170.16 (1280 data params - 84 model params = expected mean of 1196; p-value = 0.697951)\n", "Rejected dim 6: chi^2 = 1170.16 (+10.1609 w.r.t. expected mean of 1280 strings - 120 params = 1160) (dChi^2=-60, 2*dParams=70)\n" ] } ], "source": [ "#Run LGST to get an initial estimate for the gates in gs_target based on the data in ds\n", "ds = dsFake5\n", "gs_lgst4 = pygsti.do_lgst(ds, specs, targetGateset=gs_target, svdTruncateTo=4, verbosity=3)\n", "gs_lgst6 = pygsti.do_lgst(ds, specs, targetGateset=gs_target, svdTruncateTo=6, verbosity=3)\n", "\n", "#Print chi^2 of 4-dim and 6-dim estimates\n", "chiSq4 = pygsti.chi2(ds, gs_lgst4, lgstList, minProbClipForWeighting=1e-2)\n", "chiSq6 = pygsti.chi2(ds, gs_lgst6, lgstList, minProbClipForWeighting=1e-2)\n", "print \"LGST dim=4 chiSq = \",chiSq4\n", "print \"LGST dim=6 chiSq = \",chiSq6\n", "\n", "# Least squares GST with model selection\n", "gs_lsgst = pygsti.do_iterative_mc2gst_with_model_selection(ds, gs_lgst4, 1, lsgstLists, verbosity=2, minProbClipForWeighting=1e-3, probClipInterval=(-1e5,1e5), useFreqWeightedChiSq=False, regularizeFactor=1.0, check=False, check_jacobian=False)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "rhoVec[0] = 0.7070 0.0050 -0.0639 0.7780 0.0234\n", "\n", "\n", "EVec[0] = 0.5716 -0.0291 0.0344 -0.5175 0.0379\n", "\n", "\n", "Gi = \n", " 1.0000 0 0 0 0\n", " -0.0021 0.8993 0.0010 0.0006 -0.0087\n", " 0.0017 0.0010 0.8984 0.0002 0.0092\n", " -0.0252 -0.0031 0.0042 0.9006 -0.0321\n", " -0.0007 0.0089 -0.0123 -0.0017 1.0004\n", "\n", "\n", "Gx = \n", " 1.0000 0 0 0 0\n", " 0.0225 0.8381 -0.0553 0.0334 0.0170\n", " 0.1321 0.1337 -0.0508 0.9314 0.2930\n", " -0.1495 -0.0125 -0.7087 0.0713 -0.1649\n", " -0.2761 -0.0055 -0.0912 -0.0781 0.7522\n", "\n", "\n", "Gy = \n", " 1.0000 0 0 0 0\n", " -0.1373 -0.0720 0.1428 -0.9426 -0.2899\n", " -0.0175 -0.1133 0.8380 -0.0076 -0.0112\n", " -0.1399 0.7026 0.0437 0.1038 -0.1646\n", " -0.2799 0.0572 0.0165 -0.1096 0.7421\n", "\n", "\n", "\n" ] } ], "source": [ "print gs_lsgst" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.3" } }, "nbformat": 4, "nbformat_minor": 0 }