{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#Import the pyGSTi module -- you probably want this at the beginning of every notebook\n", "import pygsti\n", "import pygsti.construction as pc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The Basic Functions: create_gatestring_list and pygsti.construction.*" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "list1 = [('a1',), ('a2',)]\n", "list2 = [GateString(a1b1b2), GateString(a1b3b4), GateString(a2b1b2), GateString(a2b3b4)]\n", "list3 = ['a1a1c', 'a2a2c']\n" ] } ], "source": [ "#The workhorse function is GST.create_gatestring_list, which executes its positional arguments within a nested\n", "# loop given by iterable keyword arguments. That's a mouthful, so let's look at a few examples:\n", "As = [('a1',),('a2',)]\n", "Bs = [('b1','b2'), ('b3','b4')]\n", "\n", "def rep2(x):\n", " return x+x\n", "\n", "list1 = pc.create_gatestring_list(\"a\", a=As)\n", "list2 = pc.create_gatestring_list(\"a+b\", a=As, b=Bs, order=['a','b'])\n", "list3 = pc.create_gatestring_list(\"R(a)+c\", a=As, c=[('c',)], R=rep2)\n", "\n", "print \"list1 = \",map(tuple,list1)\n", "print \"list2 = \",list2\n", "print \"list3 = \",map(str,list3)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('A', 'B', 'C', 'A', 'B')\n", "('A', 'B', 'C')\n", "1\n" ] } ], "source": [ "#The functions within GST.GateStringTools are useful for calling within a createGateString(...) loop.\n", "# Here's how some of the most common ones behave:\n", "print pc.repeat_and_truncate (('A','B','C'),5) #args (x,N): repeat x until it is exactly length N \n", "print pc.repeat_with_max_length (('A','B','C'),5) #args (x,N): repeat x the maximum integer number of times so len(x) < N\n", "print pc.repeat_count_with_max_length(('A','B','C'),5) #args (x,N): the maximum integer number of times so len(x) < N" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "gateStrings1 = \n", "Gf0(G0)^2Gf0\n", "Gf0(G0)^2Gf1\n", "Gf1(G0)^2Gf0\n", "Gf1(G0)^2Gf1\n", "Gf0(G1aG1b)^2Gf0\n", "Gf0(G1aG1b)^2Gf1\n", "Gf1(G1aG1b)^2Gf0\n", "Gf1(G1aG1b)^2Gf1 \n", "\n", "gateStrings2 = \n", "Gf0G0G0G0Gf0\n", "Gf0G0G0G0Gf1\n", "Gf1G0G0G0Gf0\n", "Gf1G0G0G0Gf1\n", "Gf0G1aG1bG1aGf0\n", "Gf0G1aG1bG1aGf1\n", "Gf1G1aG1bG1aGf0\n", "Gf1G1aG1bG1aGf1 \n", "\n", "gateStrings3 = \n", "Gf0(G0)^3Gf0\n", "Gf0(G0)^3Gf1\n", "Gf1(G0)^3Gf0\n", "Gf1(G0)^3Gf1\n", "Gf0(G1aG1b)Gf0\n", "Gf0(G1aG1b)Gf1\n", "Gf1(G1aG1b)Gf0\n", "Gf1(G1aG1b)Gf1 \n", "\n" ] } ], "source": [ "#Now let's have a look at some more advanced examples of using create_gatestring_list along with the functions above.\n", "fids = pc.gatestring_list( [ ('Gf0',), ('Gf1',) ] )\n", "germs = pc.gatestring_list( [ ('G0',), ('G1a','G1b')] )\n", "\n", "gateStrings1 = pc.create_gatestring_list(\"f0+germ*e+f1\", f0=fids, f1=fids,\n", " germ=germs, e=2, order=[\"germ\",\"f0\",\"f1\"])\n", "print \"gateStrings1 = \\n\",\"\\n\".join(map(str,gateStrings1)),\"\\n\"\n", "\n", "gateStrings2 = pc.create_gatestring_list(\"f0+T(germ,N)+f1\", f0=fids, f1=fids,\n", " germ=germs, N=3, T=pc.repeat_and_truncate,\n", " order=[\"germ\",\"f0\",\"f1\"])\n", "print \"gateStrings2 = \\n\",\"\\n\".join(map(str,gateStrings2)),\"\\n\"\n", "\n", "gateStrings3 = pc.create_gatestring_list(\"f0+T(germ,N)+f1\", f0=fids, f1=fids,\n", " germ=germs, N=3, T=pc.repeat_with_max_length,\n", " order=[\"germ\",\"f0\",\"f1\"])\n", "print \"gateStrings3 = \\n\",\"\\n\".join(map(str,gateStrings3)),\"\\n\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Examples of more concrete ways to create gate string lists for GST" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#Let's suppose we have some initial lists of strings and gates\n", "myGerms = pc.gatestring_list( [ ('G1',), ('G2',), ('G3',) ]) #list of germs\n", "myFiducialList = pc.gatestring_list([ ('Gf1',), ('Gf2',) ]) #list of fiducials\n", "myGates = [ 'Gx', 'Gy' ] #gate labels -- often just gateset.keys()\n", "mySpecs = pc.build_spam_specs(fiducialGateStrings=myFiducialList) #Don't worry about this for now - it's just a conversion from fiducial strings to the more general \"specs\" object that GST uses" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "All strings using ['Gx', 'Gy'] up to length 2 = \n", "{}\n", "Gx\n", "Gy\n", "GxGx\n", "GxGy\n", "GyGx\n", "GyGy\n", "\n", "LGST strings = \n", "Gf1\n", "Gf2\n", "Gf1Gf1\n", "Gf1Gf2\n", "Gf2Gf1\n", "Gf2Gf2\n", "Gf1(Gx)Gf1\n", "Gf1(Gx)Gf2\n", "Gf2(Gx)Gf1\n", "Gf2(Gx)Gf2\n", "Gf1(Gy)Gf1\n", "Gf1(Gy)Gf2\n", "Gf2(Gy)Gf1\n", "Gf2(Gy)Gf2\n" ] } ], "source": [ "#Two special cases: LGST strings and all the gate strings within a certain length range\n", "allStringsInLengthRange = pc.list_all_gatestrings(myGates, minlength=0, maxlength=2)\n", "print \"\\nAll strings using %s up to length 2 = \\n\" % str(myGates), \"\\n\".join(map(str,allStringsInLengthRange))\n", "\n", "lgstStrings = pc.list_lgst_gatestrings(mySpecs,myGates)\n", "print \"\\nLGST strings = \\n\",\"\\n\".join(map(str,lgstStrings))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Germ^Exp strings = \n", "(G1)\n", "(G1)^2\n", "(G2)\n", "(G2)^2\n", "(G3)\n", "(G3)^2\n", "\n", "Germ^Exp strings sandwiched by fiducials = \n", "Gf1(G1)Gf1\n", "Gf1(G1)Gf2\n", "Gf2(G1)Gf1\n", "Gf2(G1)Gf2\n", "Gf1(G1)^2Gf1\n", "Gf1(G1)^2Gf2\n", "Gf2(G1)^2Gf1\n", "Gf2(G1)^2Gf2\n", "Gf1(G2)Gf1\n", "Gf1(G2)Gf2\n", "Gf2(G2)Gf1\n", "Gf2(G2)Gf2\n", "Gf1(G2)^2Gf1\n", "Gf1(G2)^2Gf2\n", "Gf2(G2)^2Gf1\n", "Gf2(G2)^2Gf2\n", "Gf1(G3)Gf1\n", "Gf1(G3)Gf2\n", "Gf2(G3)Gf1\n", "Gf2(G3)Gf2\n", "Gf1(G3)^2Gf1\n", "Gf1(G3)^2Gf2\n", "Gf2(G3)^2Gf1\n", "Gf2(G3)^2Gf2\n" ] } ], "source": [ "#germ^exponent strings\n", "germExpStrings = pc.create_gatestring_list(\"germ*exp\", germ=myGerms, exp=[1,2], order=('germ','exp'))\n", "print \"\\nGerm^Exp strings = \\n\", \"\\n\".join(map(str,germExpStrings))\n", "\n", "germExpStringsSand = pc.create_gatestring_list(\"f0+germ*exp+f1\", f0=myFiducialList, f1=myFiducialList,\n", " germ=myGerms, exp=[1,2], order=('germ','exp','f0','f1'))\n", "print \"\\nGerm^Exp strings sandwiched by fiducials = \\n\", \"\\n\".join(map(str,germExpStringsSand))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Typical eLGST list of lists:\n", "\n", "List 0:\n", "Gx\n", "Gy \n", "\n", "List 1:\n", "Gx\n", "Gy\n", "(G1)\n", "(G2)\n", "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gf1(G2)Gf2\n", "Gf2(G2)Gf1\n", "Gf2(G2)Gf2\n", "Gf1(G3)Gf1\n", "Gf1(G3)Gf2\n", "Gf2(G3)Gf1\n", "Gf2(G3)Gf2\n", "Gf1(G1)^2Gf1\n", "Gf1(G1)^2Gf2\n", "Gf2(G1)^2Gf1\n", "Gf2(G1)^2Gf2\n", "Gf1(G2)^2Gf1\n", "Gf1(G2)^2Gf2\n", "Gf2(G2)^2Gf1\n", "Gf2(G2)^2Gf2\n", "Gf1(G3)^2Gf1\n", "Gf1(G3)^2Gf2\n", "Gf2(G3)^2Gf1\n", "Gf2(G3)^2Gf2 \n", "\n" ] } ], "source": [ "# germ^exponent strings that are in some way truncated to a given length (these will be useful later on)\n", "\n", "#for eLGST lists typically include just the gate strings you want LGST to estimate - NOT the fiducials\n", "listOfLists_eLGST = [ myGates[:] ]\n", "for maxLen in [1,2]:\n", " gsList = pc.create_gatestring_list(\"R(germ,N)\", germ=myGerms, N=maxLen,\n", " R=pc.repeat_with_max_length,\n", " order=('germ','f0','f1'))\n", " listOfLists_eLGST.append( listOfLists_eLGST[-1] + gsList )\n", "\n", "print \"Typical eLGST list of lists:\\n\"\n", "for i,l in enumerate(listOfLists_eLGST):\n", " print \"List %d:\\n\" % i,\"\\n\".join(map(str,l)),\"\\n\"\n", "\n", "#In order to have LSGST use gate strings containing the fiducials on either end, you must include the\n", "# fiducials in the gate strings.\n", "listOfLists_LSGST = [ pc.list_lgst_gatestrings(mySpecs,myGates) ]\n", "for maxLen in [1,2]:\n", " gsList = pc.create_gatestring_list(\"f0+R(germ,N)+f1\", f0=myFiducialList,\n", " f1=myFiducialList, germ=myGerms, N=maxLen,\n", " R=pc.repeat_with_max_length,\n", " order=('germ','f0','f1'))\n", " listOfLists_LSGST.append( listOfLists_LSGST[-1] + gsList )\n", "\n", "print \"\\nTypical LSGST list of lists:\\n\"\n", "for i,l in enumerate(listOfLists_LSGST):\n", " print \"List %d:\\n\" % i,\"\\n\".join(map(str,l)),\"\\n\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generating gate string lists for further tutorials" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# As a final full-fledged example we give a function which generates gate string lists for:\n", "# 1) running eLGST\n", "# 2) running LSGST\n", "# 3) creating a template dataset file\n", "# all from some lists of gates, fiducials, germs, and maximum lengths. This example function can be copied and used\n", "# verbatim in many circumstances and modified in others in order to fit typical gate string generation tasks.\n", "\n", "def make_lsgst_lists(gateLabels, fiducialList, germList, maxLengthList):\n", " singleGates = pc.gatestring_list([(g,) for g in gateLabels])\n", " lgstStrings = pc.list_lgst_gatestrings(pc.build_spam_specs(fiducialList), gateLabels)\n", " lsgst_list = pc.gatestring_list([ () ]) #running list of all strings so far\n", " \n", " if maxLengthList[0] == 0:\n", " lsgst_listOfLists = [ lgstStrings ]\n", " maxLengthList = maxLengthList[1:]\n", " else: lsgst_listOfLists = [ ]\n", " \n", " for maxLen in maxLengthList:\n", " lsgst_list += pc.create_gatestring_list(\"f0+R(germ,N)+f1\", f0=fiducialList,\n", " f1=fiducialList, germ=germList, N=maxLen,\n", " R=pc.repeat_with_max_length,\n", " order=('germ','f0','f1'))\n", " lsgst_listOfLists.append( pygsti.remove_duplicates(lgstStrings + lsgst_list) )\n", "\n", " print \"%d LSGST sets w/lengths\" % len(lsgst_listOfLists),map(len,lsgst_listOfLists)\n", " return lsgst_listOfLists\n", "\n", "def make_elgst_lists(gateLabels, fiducialList, germList, maxLengthList):\n", " singleGates = pc.gatestring_list([(g,) for g in gateLabels])\n", " lgstStrings = pc.list_lgst_gatestrings(pc.build_spam_specs(fiducialList), gateLabels)\n", " elgst_list = pc.gatestring_list([ () ]) #running list of all strings so far\n", " \n", " if maxLengthList[0] == 0:\n", " elgst_listOfLists = [ singleGates ]\n", " maxLengthList = maxLengthList[1:]\n", " else: elgst_listOfLists = [ ]\n", " \n", " for maxLen in maxLengthList:\n", " elgst_list += pc.create_gatestring_list(\"R(germ,N)\", germ=germList, N=maxLen,\n", " R=pc.repeat_with_max_length)\n", " elgst_listOfLists.append( pygsti.remove_duplicates(singleGates + elgst_list) )\n", "\n", " print \"%d eLGST sets w/lengths\" % len(elgst_listOfLists),map(len,elgst_listOfLists)\n", " return elgst_listOfLists" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10 eLGST sets w/lengths [3, 4, 8, 18, 29, 40, 51, 62, 73, 84]\n", "10 LSGST sets w/lengths [92, 92, 168, 441, 817, 1201, 1585, 1969, 2353, 2737]\n", "\n", "First 20 items for dataset generation in label : string format\n", "{} : ()\n", "Gx : ('Gx',)\n", "Gy : ('Gy',)\n", "GxGx : ('Gx', 'Gx')\n", "GxGxGx : ('Gx', 'Gx', 'Gx')\n", "GyGyGy : ('Gy', 'Gy', 'Gy')\n", "GxGy : ('Gx', 'Gy')\n", "GxGxGxGx : ('Gx', 'Gx', 'Gx', 'Gx')\n", "GxGyGyGy : ('Gx', 'Gy', 'Gy', 'Gy')\n", "GyGx : ('Gy', 'Gx')\n", "GyGy : ('Gy', 'Gy')\n", "GyGxGx : ('Gy', 'Gx', 'Gx')\n", "GyGxGxGx : ('Gy', 'Gx', 'Gx', 'Gx')\n", "GyGyGyGy : ('Gy', 'Gy', 'Gy', 'Gy')\n", "GxGxGy : ('Gx', 'Gx', 'Gy')\n", "GxGxGxGxGx : ('Gx', 'Gx', 'Gx', 'Gx', 'Gx')\n", "GxGxGyGyGy : ('Gx', 'Gx', 'Gy', 'Gy', 'Gy')\n", "GxGxGxGy : ('Gx', 'Gx', 'Gx', 'Gy')\n", "GxGxGxGxGxGx : ('Gx', 'Gx', 'Gx', 'Gx', 'Gx', 'Gx')\n", "GxGxGxGyGyGy : ('Gx', 'Gx', 'Gx', 'Gy', 'Gy', 'Gy')\n", "GyGyGyGx : ('Gy', 'Gy', 'Gy', 'Gx')\n", "GyGyGyGxGx : ('Gy', 'Gy', 'Gy', 'Gx', 'Gx')\n", "GyGyGyGxGxGx : ('Gy', 'Gy', 'Gy', 'Gx', 'Gx', 'Gx')\n", "GyGyGyGyGyGy : ('Gy', 'Gy', 'Gy', 'Gy', 'Gy', 'Gy')\n", "(Gi) : ('Gi',)\n", "(Gi)Gx : ('Gi', 'Gx')\n", "(Gi)Gy : ('Gi', 'Gy')\n", "(Gi)GxGx : ('Gi', 'Gx', 'Gx')\n", "(Gi)GxGxGx : ('Gi', 'Gx', 'Gx', 'Gx')\n", "(Gi)GyGyGy : ('Gi', 'Gy', 'Gy', 'Gy')\n" ] } ], "source": [ "#We'll now use this function to generate some lists we'll use in other tutorials\n", "gates = ['Gi','Gx','Gy']\n", "fiducials = pc.gatestring_list([ (), ('Gx',), ('Gy',), ('Gx','Gx'), ('Gx','Gx','Gx'), ('Gy','Gy','Gy') ]) # fiducials for 1Q MUB\n", "germs = pc.gatestring_list( [('Gx',), ('Gy',), ('Gi',), ('Gx', 'Gy',),\n", " ('Gx', 'Gy', 'Gi',), ('Gx', 'Gi', 'Gy',),('Gx', 'Gi', 'Gi',), ('Gy', 'Gi', 'Gi',),\n", " ('Gx', 'Gx', 'Gi', 'Gy',), ('Gx', 'Gy', 'Gy', 'Gi',),\n", " ('Gx', 'Gx', 'Gy', 'Gx', 'Gy', 'Gy',)] )\n", "maxLengths = [0,1,2,4,8,16,32,64,128,256] \n", "elgst_lists = make_elgst_lists(gates, fiducials, germs, maxLengths)\n", "lsgst_lists = make_lsgst_lists(gates, fiducials, germs, maxLengths) \n", "\n", "print \"\\nFirst 20 items for dataset generation in label : string format\"\n", "for gateString in lsgst_lists[-1][0:30]:\n", " print str(gateString), \": \", tuple(gateString)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#Write example gatestring list files for later use\n", "pygsti.io.write_gatestring_list(\"tutorial_files/Example_FiducialList.txt\", fiducials,\"#My fiducial strings\")\n", "pygsti.io.write_gatestring_list(\"tutorial_files/Example_GermsList.txt\", germs,\"#My germ strings\")\n", "\n", "pygsti.io.write_gatestring_list(\"tutorial_files/Example_GatestringList.txt\",lsgst_lists[-1],\"#All the gate strings to be in my dataset\")\n", "pygsti.io.write_empty_dataset(\"tutorial_files/Example_DatasetTemplate.txt\",lsgst_lists[-1])\n", "\n", "for l,lst in zip(maxLengths,elgst_lists):\n", " pygsti.io.write_gatestring_list(\"tutorial_files/Example_eLGSTlist%d.txt\" % l,lst,\n", " \"# eLGST gate strings for max length %d\" % l)\n", "\n", "for l,lst in zip(maxLengths,lsgst_lists):\n", " pygsti.io.write_gatestring_list(\"tutorial_files/Example_LSGSTlist%d.txt\" % l,lst,\n", " \"# LSGST gate strings for max length %d\" % l)\n", " \n", "#Also write the max lengths we used to file\n", "import json\n", "json.dump(maxLengths, open(\"tutorial_files/Example_maxLengths.json\",\"w\"))" ] }, { "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 }