def test_model_selection(self): ds = self.ds #pygsti.construction.generate_fake_data(self.datagen_gateset, self.lsgstStrings[-1], # nSamples=1000,sampleError='binomial', seed=100) gs_lgst4 = pygsti.do_lgst(ds, self.specs, self.gateset, svdTruncateTo=4, verbosity=0) gs_lgst6 = pygsti.do_lgst(ds, self.specs, self.gateset, svdTruncateTo=6, verbosity=0) sys.stdout.flush() self.runSilent(pygsti.do_lgst, ds, self.specs, self.gateset, svdTruncateTo=6, verbosity=4) # test verbose prints chiSq4 = pygsti.chi2(ds, gs_lgst4, self.lgstStrings, minProbClipForWeighting=1e-4) chiSq6 = pygsti.chi2(ds, gs_lgst6, self.lgstStrings, minProbClipForWeighting=1e-4) print("LGST dim=4 chiSq = ",chiSq4) print("LGST dim=6 chiSq = ",chiSq6) #self.assertAlmostEqual(chiSq4, 174.061524953) #429.271983052) #self.assertAlmostEqual(chiSq6, 267012993.861, places=1) #1337.74222467) #Why is this so large??? -- DEBUG later # Least squares GST with model selection gs_lsgst = self.runSilent(pygsti.do_iterative_mc2gst_with_model_selection, ds, gs_lgst4, 1, self.lsgstStrings[0:3], verbosity=10, minProbClipForWeighting=1e-3, probClipInterval=(-1e5,1e5)) # Run again with other parameters tuple_strings = [ list(map(tuple, gsList)) for gsList in self.lsgstStrings[0:3] ] #to test tuple argument errorVecs, gs_lsgst_wts = self.runSilent(pygsti.do_iterative_mc2gst_with_model_selection, ds, gs_lgst4, 1, tuple_strings, verbosity=10, minProbClipForWeighting=1e-3, probClipInterval=(-1e5,1e5), gatestringWeightsDict={ ('Gx',): 2.0 }, returnAll=True, returnErrorVec=True) # Do non-iterative to cover GateString->tuple conversion gs_non_iterative = self.runSilent( pygsti.do_mc2gst_with_model_selection, ds, gs_lgst4, 1, self.lsgstStrings[0], verbosity=10, probClipInterval=(-1e5,1e5) ) # RUN BELOW LINES TO SEED SAVED GATESET FILES #pygsti.io.write_gateset(gs_lsgst,compare_files + "/lsgstMS.gateset", "Saved LSGST Gateset with model selection") gs_lsgst_compare = pygsti.io.load_gateset(compare_files + "/lsgstMS.gateset") gs_lsgst_go = pygsti.optimize_gauge(gs_lsgst, 'target', targetGateset=gs_lsgst_compare, spamWeight=1.0) self.assertAlmostEqual( gs_lsgst_go.frobeniusdist(gs_lsgst_compare), 0, places=5)
def runAnalysis(obj, myspecs, mygerms, gsTarget, seed, maxLs = [1,2,4,8], nSamples=1000, useFreqWeightedChiSq=False, minProbClipForWeighting=1e-4, fidPairList=None, comm=None, distributeMethod="gatestrings"): rhoStrs, EStrs = pygsti.construction.get_spam_strs(myspecs) lgstStrings = pygsti.construction.list_lgst_gatestrings( myspecs, gsTarget.gates.keys()) lsgstStrings = pygsti.construction.make_lsgst_lists( gsTarget.gates.keys(), rhoStrs, EStrs, mygerms, maxLs, fidPairList ) print len(myspecs[0]), " rho specifiers" print len(myspecs[1]), " effect specifiers" print len(mygerms), " germs" print len(lgstStrings), " total LGST gate strings" print len(lsgstStrings[-1]), " LSGST strings before thinning" lsgstStringsToUse = lsgstStrings allRequiredStrs = pygsti.remove_duplicates(lgstStrings + lsgstStrings[-1]) gs_dataGen = gsTarget.depolarize(gate_noise=0.1) dsFake = pygsti.construction.generate_fake_data( gs_dataGen, allRequiredStrs, nSamples, sampleError="multinomial", seed=seed) #Run LGST to get starting gate set gs_lgst = pygsti.do_lgst(dsFake, myspecs, gsTarget, svdTruncateTo=gsTarget.dim, verbosity=3) gs_lgst_go = pygsti.optimize_gauge(gs_lgst,"target", targetGateset=gs_dataGen) #Run full iterative LSGST tStart = time.time() if obj == "chi2": all_gs_lsgst = pygsti.do_iterative_mc2gst( dsFake, gs_lgst_go, lsgstStringsToUse, minProbClipForWeighting=minProbClipForWeighting, probClipInterval=(-1e5,1e5), verbosity=1, memLimit=3*(1024)**3, returnAll=True, useFreqWeightedChiSq=useFreqWeightedChiSq, comm=comm, distributeMethod=distributeMethod) elif obj == "logl": all_gs_lsgst = pygsti.do_iterative_mlgst( dsFake, gs_lgst_go, lsgstStringsToUse, minProbClip=minProbClipForWeighting, probClipInterval=(-1e5,1e5), verbosity=1, memLimit=3*(1024)**3, returnAll=True, useFreqWeightedChiSq=useFreqWeightedChiSq, comm=comm, distributeMethod=distributeMethod) tEnd = time.time() print "Time = ",(tEnd-tStart)/3600.0,"hours" return all_gs_lsgst, gs_dataGen
def test_eLGST(self): ds = self.ds #pygsti.construction.generate_fake_data(self.datagen_gateset, self.lsgstStrings[-1], # nSamples=1000,sampleError='binomial', seed=100) gs_lgst = pygsti.do_lgst(ds, self.specs, self.gateset, svdTruncateTo=4, verbosity=0) gs_lgst_go = pygsti.optimize_gauge(gs_lgst,"target",targetGateset=self.gateset, spamWeight=1.0, gateWeight=1.0) gs_clgst = pygsti.contract(gs_lgst_go, "CPTP") gs_single_exlgst = pygsti.do_exlgst(ds, gs_clgst, self.elgstStrings[0], self.specs, self.gateset, regularizeFactor=1e-3, svdTruncateTo=4, verbosity=0) gs_single_exlgst_verb = self.runSilent(pygsti.do_exlgst, ds, gs_clgst, self.elgstStrings[0], self.specs, self.gateset, regularizeFactor=1e-3, svdTruncateTo=4, verbosity=10) gs_exlgst = pygsti.do_iterative_exlgst(ds, gs_clgst, self.specs, self.elgstStrings, targetGateset=self.gateset, svdTruncateTo=4, verbosity=0) all_minErrs, all_gs_exlgst_tups = pygsti.do_iterative_exlgst( ds, gs_clgst, self.specs, [ [gs.tup for gs in gsList] for gsList in self.elgstStrings], targetGateset=self.gateset, svdTruncateTo=4, verbosity=0, returnAll=True, returnErrorVec=True) gs_exlgst_verb = self.runSilent(pygsti.do_iterative_exlgst, ds, gs_clgst, self.specs, self.elgstStrings, targetGateset=self.gateset, svdTruncateTo=4, verbosity=10) gs_exlgst_reg = pygsti.do_iterative_exlgst(ds, gs_clgst, self.specs, self.elgstStrings, targetGateset=self.gateset, svdTruncateTo=4, verbosity=0, regularizeFactor=10) self.assertAlmostEqual(gs_exlgst.frobeniusdist(gs_exlgst_verb),0) self.assertAlmostEqual(gs_exlgst.frobeniusdist(all_gs_exlgst_tups[-1]),0) #Run internal checks on less max-L values (so it doesn't take forever) gs_exlgst_chk = pygsti.do_iterative_exlgst(ds, gs_clgst, self.specs, self.elgstStrings[0:2], targetGateset=self.gateset, svdTruncateTo=4, verbosity=0, check_jacobian=True) gs_exlgst_chk_verb = self.runSilent(pygsti.do_iterative_exlgst,ds, gs_clgst, self.specs, self.elgstStrings[0:2], targetGateset=self.gateset, svdTruncateTo=4, verbosity=10, check_jacobian=True) # RUN BELOW LINES TO SEED SAVED GATESET FILES #pygsti.io.write_gateset(gs_exlgst,compare_files + "/exlgst.gateset", "Saved Extended-LGST (eLGST) Gateset") #pygsti.io.write_gateset(gs_exlgst_reg,compare_files + "/exlgst_reg.gateset", "Saved Extended-LGST (eLGST) Gateset w/regularization") gs_exlgst_compare = pygsti.io.load_gateset(compare_files + "/exlgst.gateset") gs_exlgst_reg_compare = pygsti.io.load_gateset(compare_files + "/exlgst_reg.gateset") gs_exlgst_go = pygsti.optimize_gauge(gs_exlgst, 'target', targetGateset=gs_exlgst_compare, spamWeight=1.0) gs_exlgst_reg_go = pygsti.optimize_gauge(gs_exlgst_reg, 'target', targetGateset=gs_exlgst_reg_compare, spamWeight=1.0) self.assertAlmostEqual( gs_exlgst_go.frobeniusdist(gs_exlgst_compare), 0, places=5) self.assertAlmostEqual( gs_exlgst_reg_go.frobeniusdist(gs_exlgst_reg_compare), 0, places=5)
def test_LGST_1overSqrtN_dependence(self): my_datagen_gateset = self.gateset.depolarize(gate_noise=0.05, spam_noise=0) # !!don't depolarize spam or 1/sqrt(N) dependence saturates!! nSamplesList = np.array([ 16, 128, 1024, 8192 ]) diffs = [] for nSamples in nSamplesList: ds = pygsti.construction.generate_fake_data(my_datagen_gateset, self.lgstStrings, nSamples, sampleError='binomial', seed=100) gs_lgst = pygsti.do_lgst(ds, self.specs, self.gateset, svdTruncateTo=4, verbosity=0) gs_lgst_go = pygsti.optimize_gauge(gs_lgst,"target",targetGateset=my_datagen_gateset, spamWeight=1.0, gateWeight=1.0) diffs.append( my_datagen_gateset.frobeniusdist(gs_lgst_go) ) diffs = np.array(diffs, 'd') a,b = polyfit(np.log10(nSamplesList), np.log10(diffs), deg=1) #print "\n",nSamplesList; print diffs; print a #DEBUG self.assertLess( a+0.5, 0.05 )
def setUp(self): #Set GateSet objects to "strict" mode for testing pygsti.objects.GateSet._strict = True self.targetGateset = std.gs_target datagen_gateset = self.targetGateset.depolarize(gate_noise=0.05, spam_noise=0.1) self.fiducials = std.fiducials self.germs = std.germs self.specs = pygsti.construction.build_spam_specs(self.fiducials, effect_labels=['E0']) #only use the first EVec self.gateLabels = self.targetGateset.gates.keys() # also == std.gates self.lgstStrings = pygsti.construction.list_lgst_gatestrings(self.specs, self.gateLabels) self.maxLengthList = [0,1,2,4,8] self.lsgstStrings = pygsti.construction.make_lsgst_lists( self.gateLabels, self.fiducials, self.fiducials, self.germs, self.maxLengthList) self.ds = pygsti.objects.DataSet(fileToLoadFrom="cmp_chk_files/reportgen.dataset") # RUN BELOW LINES TO GENERATE ANALYSIS DATASET #ds = pygsti.construction.generate_fake_data(datagen_gateset, lsgstStrings[-1], nSamples=1000, # sampleError='binomial', seed=100) #ds.save("cmp_chk_files/reportgen.dataset") gs_lgst = pygsti.do_lgst(self.ds, self.specs, self.targetGateset, svdTruncateTo=4, verbosity=0) gs_lgst_go = pygsti.optimize_gauge(gs_lgst,"target",targetGateset=self.targetGateset) self.gs_clgst = pygsti.contract(gs_lgst_go, "CPTP") self.gs_clgst_tp = pygsti.contract(self.gs_clgst, "vSPAM") self.gs_clgst_tp.set_all_parameterizations("TP") try: import pptx self.have_python_pptx = True except ImportError: warnings.warn("**** IMPORT: Cannot import pptx (python-pptx), and so" + " Powerpoint slide generation tests have been disabled.") self.have_python_pptx = False
def runAnalysis(obj, ds, myspecs, gsTarget, lsgstStringsToUse, useFreqWeightedChiSq=False, minProbClipForWeighting=1e-4, fidPairList=None, comm=None, distributeMethod="gatestrings"): #Run LGST to get starting gate set assertGatesetsInSync(gsTarget, comm) gs_lgst = pygsti.do_lgst(ds, myspecs, gsTarget, svdTruncateTo=gsTarget.dim, verbosity=3) assertGatesetsInSync(gs_lgst, comm) gs_lgst_go = pygsti.optimize_gauge(gs_lgst,"target", targetGateset=gsTarget) assertGatesetsInSync(gs_lgst_go, comm) #Run full iterative LSGST tStart = time.time() if obj == "chi2": all_gs_lsgst = pygsti.do_iterative_mc2gst( ds, gs_lgst_go, lsgstStringsToUse, minProbClipForWeighting=minProbClipForWeighting, probClipInterval=(-1e5,1e5), verbosity=1, memLimit=3*(1024)**3, returnAll=True, useFreqWeightedChiSq=useFreqWeightedChiSq, comm=comm, distributeMethod=distributeMethod) elif obj == "logl": all_gs_lsgst = pygsti.do_iterative_mlgst( ds, gs_lgst_go, lsgstStringsToUse, minProbClip=minProbClipForWeighting, probClipInterval=(-1e5,1e5), verbosity=1, memLimit=3*(1024)**3, returnAll=True, useFreqWeightedChiSq=useFreqWeightedChiSq, comm=comm, distributeMethod=distributeMethod) tEnd = time.time() print("Time = ",(tEnd-tStart)/3600.0,"hours") return all_gs_lsgst
def test_LGST(self): ds = self.ds #pygsti.construction.generate_fake_data(self.datagen_gateset, self.lgstStrings, nSamples=1000, # sampleError='binomial', seed=None) gs_lgst = pygsti.do_lgst(ds, self.specs, self.gateset, svdTruncateTo=4, verbosity=0) gs_lgst_verb = self.runSilent(pygsti.do_lgst, ds, self.specs, self.gateset, svdTruncateTo=4, verbosity=10) self.assertAlmostEqual(gs_lgst.frobeniusdist(gs_lgst_verb),0) gs_lgst_go = pygsti.optimize_gauge(gs_lgst,"target",targetGateset=self.gateset, spamWeight=1.0, gateWeight=1.0) gs_clgst = pygsti.contract(gs_lgst_go, "CPTP") # RUN BELOW LINES TO SEED SAVED GATESET FILES #pygsti.io.write_gateset(gs_lgst,compare_files + "/lgst.gateset", "Saved LGST Gateset before gauge optimization") #pygsti.io.write_gateset(gs_lgst_go,compare_files + "/lgst_go.gateset", "Saved LGST Gateset after gauge optimization") #pygsti.io.write_gateset(gs_clgst,compare_files + "/clgst.gateset", "Saved LGST Gateset after G.O. and CPTP contraction") gs_lgst_compare = pygsti.io.load_gateset(compare_files + "/lgst.gateset") gs_lgst_go_compare = pygsti.io.load_gateset(compare_files + "/lgst_go.gateset") gs_clgst_compare = pygsti.io.load_gateset(compare_files + "/clgst.gateset") self.assertAlmostEqual( gs_lgst.frobeniusdist(gs_lgst_compare), 0) self.assertAlmostEqual( gs_lgst_go.frobeniusdist(gs_lgst_go_compare), 0) self.assertAlmostEqual( gs_clgst.frobeniusdist(gs_clgst_compare), 0) #Check for error conditions with self.assertRaises(ValueError): gs_lgst = pygsti.do_lgst(ds, self.specs, None, svdTruncateTo=4, verbosity=0) #no gate labels with self.assertRaises(ValueError): gs_lgst = pygsti.do_lgst(ds, self.specs, None, gateLabels=list(self.gateset.gates.keys()), svdTruncateTo=4, verbosity=0) #no spam dict with self.assertRaises(ValueError): gs_lgst = pygsti.do_lgst(ds, self.specs, None, gateLabels=list(self.gateset.gates.keys()), spamDict=self.gateset.get_reverse_spam_defs(), svdTruncateTo=4, verbosity=0) #no identity vector with self.assertRaises(ValueError): bad_specs = pygsti.construction.build_spam_specs( pygsti.construction.gatestring_list([('Gx',),('Gx',),('Gx',),('Gx',)]), effect_labels=['E0']) gs_lgst = pygsti.do_lgst(ds, bad_specs, self.gateset, svdTruncateTo=4, verbosity=0) # bad specs (rank deficient) with self.assertRaises(KeyError): # AB-matrix construction error incomplete_strings = self.lgstStrings[5:] #drop first 5 strings... bad_ds = pygsti.construction.generate_fake_data( self.datagen_gateset, incomplete_strings, nSamples=10, sampleError='none') gs_lgst = pygsti.do_lgst(bad_ds, self.specs, self.gateset, svdTruncateTo=4, verbosity=0) # incomplete dataset with self.assertRaises(KeyError): # X-matrix construction error incomplete_strings = self.lgstStrings[:-5] #drop last 5 strings... bad_ds = pygsti.construction.generate_fake_data( self.datagen_gateset, incomplete_strings, nSamples=10, sampleError='none') gs_lgst = pygsti.do_lgst(bad_ds, self.specs, self.gateset, svdTruncateTo=4, verbosity=0)
def test_gaugeopt_and_contract(self): ds = self.ds_lgst #pygsti.construction.generate_fake_data(self.datagen_gateset, self.lgstStrings, # nSamples=10000,sampleError='binomial', seed=100) gs_lgst = pygsti.do_lgst(ds, self.specs, self.gateset, svdTruncateTo=4, verbosity=0) #Gauge Opt to Target gs_lgst_target = self.runSilent(pygsti.optimize_gauge, gs_lgst,"target",targetGateset=self.gateset,verbosity=10) #Gauge Opt to Target using non-frobenius metrics gs_lgst_targetAlt = self.runSilent(pygsti.optimize_gauge, gs_lgst_target,"target",targetGateset=self.gateset, targetGatesMetric='fidelity', verbosity=10) gs_lgst_targetAlt = self.runSilent(pygsti.optimize_gauge, gs_lgst_target,"target",targetGateset=self.gateset, targetGatesMetric='tracedist', verbosity=10) gs_lgst_targetAlt = self.runSilent(pygsti.optimize_gauge, gs_lgst_target,"target",targetGateset=self.gateset, targetSpamMetric='fidelity', verbosity=10) gs_lgst_targetAlt = self.runSilent(pygsti.optimize_gauge, gs_lgst_target,"target",targetGateset=self.gateset, targetSpamMetric='tracedist', verbosity=10) with self.assertRaises(ValueError): self.runSilent(pygsti.optimize_gauge, gs_lgst_target,"target",targetGateset=self.gateset, targetGatesMetric='foobar', verbosity=10) #bad targetGatesMetric with self.assertRaises(ValueError): self.runSilent(pygsti.optimize_gauge, gs_lgst_target,"target",targetGateset=self.gateset, targetSpamMetric='foobar', verbosity=10) #bad targetSpamMetric with self.assertRaises(ValueError): self.runSilent(pygsti.optimize_gauge, gs_lgst_target,"foobar",targetGateset=self.gateset, targetSpamMetric='target', verbosity=10) #bad toGetTo #Contractions gs_clgst_tp = self.runSilent(pygsti.contract, gs_lgst_target, "TP",verbosity=10, tol=10.0) gs_clgst_cp = self.runSilent(pygsti.contract, gs_lgst_target, "CP",verbosity=10, tol=10.0) gs_clgst_cptp = self.runSilent(pygsti.contract, gs_lgst_target, "CPTP",verbosity=10, tol=10.0) gs_clgst_cptp2 = self.runSilent(pygsti.contract, gs_lgst_target, "CPTP",verbosity=10, useDirectCP=False) gs_clgst_cptp3 = self.runSilent(pygsti.contract, gs_lgst_target, "CPTP",verbosity=10, tol=10.0, maxiter=0) gs_clgst_xp = self.runSilent(pygsti.contract, gs_lgst_target, "XP", ds,verbosity=10, tol=10.0) gs_clgst_xptp = self.runSilent(pygsti.contract, gs_lgst_target, "XPTP", ds,verbosity=10, tol=10.0) gs_clgst_vsp = self.runSilent(pygsti.contract, gs_lgst_target, "vSPAM",verbosity=10, tol=10.0) gs_clgst_none = self.runSilent(pygsti.contract, gs_lgst_target, "nothing",verbosity=10, tol=10.0) #test bad effect vector cases gs_bad_effect = gs_lgst_target.copy() gs_bad_effect.effects['E0'] = [100.0,0,0,0] # E eigvals all > 1.0 self.runSilent(pygsti.contract, gs_bad_effect, "vSPAM",verbosity=10, tol=10.0) gs_bad_effect.effects['E0'] = [-100.0,0,0,0] # E eigvals all < 0 self.runSilent(pygsti.contract, gs_bad_effect, "vSPAM",verbosity=10, tol=10.0) with self.assertRaises(ValueError): self.runSilent(pygsti.contract, gs_lgst_target, "foobar",verbosity=10, tol=10.0) #bad toWhat #More gauge optimizations gs_lgst_target_cp = self.runSilent(pygsti.optimize_gauge, gs_clgst_cptp,"target",targetGateset=self.gateset, constrainToCP=True,constrainToTP=True,constrainToValidSpam=True,verbosity=10) gs_lgst_cptp = self.runSilent(pygsti.optimize_gauge, gs_lgst,"CPTP",verbosity=10) gs_lgst_cptp_tp = self.runSilent(pygsti.optimize_gauge, gs_lgst,"CPTP",verbosity=10, constrainToTP=True) gs_lgst_tp = self.runSilent(pygsti.optimize_gauge, gs_lgst,"TP",verbosity=10) gs_lgst_tptarget = self.runSilent(pygsti.optimize_gauge, gs_lgst,"TP and target",targetGateset=self.gateset,verbosity=10) gs_lgst_cptptarget = self.runSilent(pygsti.optimize_gauge, gs_lgst,"CPTP and target",targetGateset=self.gateset,verbosity=10) gs_lgst_cptptarget2= self.runSilent(pygsti.optimize_gauge, gs_lgst,"CPTP and target",targetGateset=self.gateset, verbosity=10, constrainToTP=True) gs_lgst_cd = self.runSilent(pygsti.optimize_gauge, gs_lgst,"Completely Depolarized",targetGateset=self.gateset,verbosity=10) #TODO: check output lies in space desired # big kick that should land it outside XP, TP, etc, so contraction # routines are more tested gs_bigkick = gs_lgst_target.kick(absmag=1.0) gs_badspam = gs_bigkick.copy() gs_badspam.effects['E0'] = np.array( [[2],[0],[0],[4]], 'd') #set a bad evec so vSPAM has to work... gs_clgst_tp = self.runSilent(pygsti.contract,gs_bigkick, "TP", verbosity=10, tol=10.0) gs_clgst_cp = self.runSilent(pygsti.contract,gs_bigkick, "CP", verbosity=10, tol=10.0) gs_clgst_cptp = self.runSilent(pygsti.contract,gs_bigkick, "CPTP", verbosity=10, tol=10.0) gs_clgst_xp = self.runSilent(pygsti.contract,gs_bigkick, "XP", ds, verbosity=10, tol=10.0) gs_clgst_xptp = self.runSilent(pygsti.contract,gs_bigkick, "XPTP", ds, verbosity=10, tol=10.0) gs_clgst_vsp = self.runSilent(pygsti.contract,gs_badspam, "vSPAM", verbosity=10, tol=10.0) gs_clgst_none = self.runSilent(pygsti.contract,gs_bigkick, "nothing", verbosity=10, tol=10.0) #TODO: check output lies in space desired #Check Errors with self.assertRaises(ValueError): pygsti.optimize_gauge(gs_lgst,"FooBar",verbosity=0) # bad toGetTo argument with self.assertRaises(ValueError): pygsti.contract(gs_lgst_target, "FooBar",verbosity=0) # bad toWhat argument
def test_MLGST(self): ds = self.ds #pygsti.construction.generate_fake_data(self.datagen_gateset, self.lsgstStrings[-1], # nSamples=1000, sampleError='binomial', seed=100) gs_lgst = pygsti.do_lgst(ds, self.specs, self.gateset, svdTruncateTo=4, verbosity=0) gs_lgst_go = pygsti.optimize_gauge(gs_lgst,"target",targetGateset=self.gateset, spamWeight=1.0, gateWeight=1.0) gs_clgst = pygsti.contract(gs_lgst_go, "CPTP") gs_single_mlgst = pygsti.do_mlgst(ds, gs_clgst, self.lsgstStrings[0], minProbClip=1e-6, probClipInterval=(-1e2,1e2), verbosity=0) gs_mlegst = pygsti.do_iterative_mlgst(ds, gs_clgst, self.lsgstStrings, verbosity=0, minProbClip=1e-6, probClipInterval=(-1e2,1e2), memLimit=1000*1024**2) maxLogL, all_gs_mlegst_tups = pygsti.do_iterative_mlgst( ds, gs_clgst, [ [gs.tup for gs in gsList] for gsList in self.lsgstStrings], minProbClip=1e-6, probClipInterval=(-1e2,1e2), returnAll=True, returnMaxLogL=True) gs_mlegst_verb = self.runSilent(pygsti.do_iterative_mlgst, ds, gs_clgst, self.lsgstStrings, verbosity=10, minProbClip=1e-6, probClipInterval=(-1e2,1e2), memLimit=10*1024**2) self.assertAlmostEqual(gs_mlegst.frobeniusdist(gs_mlegst_verb),0) self.assertAlmostEqual(gs_mlegst.frobeniusdist(all_gs_mlegst_tups[-1]),0) #Run internal checks on less max-L values (so it doesn't take forever) gs_mlegst_chk = pygsti.do_iterative_mlgst(ds, gs_clgst, self.lsgstStrings[0:2], verbosity=0, minProbClip=1e-6, probClipInterval=(-1e2,1e2), check=True) #Other option variations - just make sure they run at this point gs_mlegst_chk_opts = pygsti.do_iterative_mlgst(ds, gs_clgst, self.lsgstStrings[0:2], verbosity=0, minProbClip=1e-6, probClipInterval=(-1e2,1e2), gateStringSetLabels=["Set1","Set2"], useFreqWeightedChiSq=True ) aliased_list = [ pygsti.obj.GateString( [ (x if x != "Gx" else "GA1") for x in gs]) for gs in self.lsgstStrings[0] ] gs_withA1 = gs_clgst.copy(); gs_withA1.gates["GA1"] = gs_clgst.gates["Gx"] gs_mlegst_chk_opts2 = pygsti.do_mlgst(ds, gs_withA1, aliased_list, minProbClip=1e-6, probClipInterval=(-1e2,1e2), verbosity=0, gateLabelAliases={ 'GA1': ('Gx',) }) #Other option variations - just make sure they run at this point gs_lsgst_chk_opts = pygsti.do_iterative_mc2gst(ds, gs_clgst, self.lsgstStrings[0:2], verbosity=0, minProbClipForWeighting=1e-6, probClipInterval=(-1e6,1e6), useFreqWeightedChiSq=True, gateStringSetLabels=["Set1","Set2"], gatestringWeightsDict={ ('Gx',): 2.0 } ) self.runSilent(pygsti.do_mlgst, ds, gs_clgst, self.lsgstStrings[0], minProbClip=1e-6, probClipInterval=(-1e2,1e2), verbosity=4, memLimit=300000) #invoke memory control pygsti.do_mlgst(ds, gs_clgst, self.lsgstStrings[0], minProbClip=1e-6, probClipInterval=(-1e2,1e2), verbosity=0, poissonPicture=False) #non-Poisson picture - should use (-1,-1) gateset for consistency? #Check errors: with self.assertRaises(MemoryError): pygsti.do_mlgst(ds, gs_clgst, self.lsgstStrings[0], minProbClip=1e-6, probClipInterval=(-1e2,1e2),verbosity=0, memLimit=1) # RUN BELOW LINES TO SEED SAVED GATESET FILES #pygsti.io.write_gateset(gs_mlegst,compare_files + "/mle_gst.gateset", "Saved MLE-GST Gateset") gs_mle_compare = pygsti.io.load_gateset(compare_files + "/mle_gst.gateset") gs_mlegst_go = pygsti.optimize_gauge(gs_mlegst, 'target', targetGateset=gs_mle_compare, spamWeight=1.0) self.assertAlmostEqual( gs_mlegst_go.frobeniusdist(gs_mle_compare), 0, places=5)
def test_MC2GST(self): ds = self.ds #pygsti.construction.generate_fake_data(self.datagen_gateset, self.lsgstStrings[-1], # nSamples=1000, sampleError='binomial', seed=100) gs_lgst = pygsti.do_lgst(ds, self.specs, self.gateset, svdTruncateTo=4, verbosity=0) gs_lgst_go = pygsti.optimize_gauge(gs_lgst,"target",targetGateset=self.gateset, spamWeight=1.0, gateWeight=1.0) gs_clgst = pygsti.contract(gs_lgst_go, "CPTP") gs_single_lsgst = pygsti.do_mc2gst(ds, gs_clgst, self.lsgstStrings[0], minProbClipForWeighting=1e-6, probClipInterval=(-1e6,1e6), regularizeFactor=1e-3, verbosity=0) gs_lsgst = pygsti.do_iterative_mc2gst(ds, gs_clgst, self.lsgstStrings, verbosity=0, minProbClipForWeighting=1e-6, probClipInterval=(-1e6,1e6), memLimit=1000*1024**2) all_minErrs, all_gs_lsgst_tups = pygsti.do_iterative_mc2gst( ds, gs_clgst, [ [gs.tup for gs in gsList] for gsList in self.lsgstStrings], minProbClipForWeighting=1e-6, probClipInterval=(-1e6,1e6), returnAll=True, returnErrorVec=True) gs_lsgst_verb = self.runSilent(pygsti.do_iterative_mc2gst, ds, gs_clgst, self.lsgstStrings, verbosity=10, minProbClipForWeighting=1e-6, probClipInterval=(-1e6,1e6), memLimit=10*1024**2) gs_lsgst_reg = self.runSilent(pygsti.do_iterative_mc2gst,ds, gs_clgst, self.lsgstStrings, verbosity=10, minProbClipForWeighting=1e-6, probClipInterval=(-1e6,1e6), regularizeFactor=10, memLimit=100*1024**2) self.assertAlmostEqual(gs_lsgst.frobeniusdist(gs_lsgst_verb),0) self.assertAlmostEqual(gs_lsgst.frobeniusdist(all_gs_lsgst_tups[-1]),0) #Run internal checks on less max-L values (so it doesn't take forever) gs_lsgst_chk = pygsti.do_iterative_mc2gst(ds, gs_clgst, self.lsgstStrings[0:2], verbosity=0, minProbClipForWeighting=1e-6, probClipInterval=(-1e6,1e6), check=True, check_jacobian=True) gs_lsgst_chk_verb = self.runSilent(pygsti.do_iterative_mc2gst, ds, gs_clgst, self.lsgstStrings[0:2], verbosity=10, minProbClipForWeighting=1e-6, probClipInterval=(-1e6,1e6), check=True, check_jacobian=True, memLimit=100*1024**2) #Other option variations - just make sure they run at this point gs_lsgst_chk_opts = pygsti.do_iterative_mc2gst(ds, gs_clgst, self.lsgstStrings[0:2], verbosity=0, minProbClipForWeighting=1e-6, probClipInterval=(-1e6,1e6), useFreqWeightedChiSq=True, gateStringSetLabels=["Set1","Set2"], gatestringWeightsDict={ ('Gx',): 2.0 } ) #Check with small but ok memlimit self.runSilent(pygsti.do_mc2gst,ds, gs_clgst, self.lsgstStrings[0], minProbClipForWeighting=1e-6, probClipInterval=(-1e6,1e6), regularizeFactor=1e-3, verbosity=10, memLimit=300000) #Check errors: with self.assertRaises(MemoryError): pygsti.do_mc2gst(ds, gs_clgst, self.lsgstStrings[0], minProbClipForWeighting=1e-6, probClipInterval=(-1e6,1e6), regularizeFactor=1e-3, verbosity=0, memLimit=1) with self.assertRaises(NotImplementedError): pygsti.do_mc2gst(ds, gs_clgst, self.lsgstStrings[0], minProbClipForWeighting=1e-6, probClipInterval=(-1e6,1e6), regularizeFactor=1e-3, verbosity=0, cptp_penalty_factor=1.0) #cptp pentalty not implemented yet # RUN BELOW LINES TO SEED SAVED GATESET FILES #pygsti.io.write_gateset(gs_lsgst,compare_files + "/lsgst.gateset", "Saved LSGST Gateset") #pygsti.io.write_gateset(gs_lsgst_reg,compare_files + "/lsgst_reg.gateset", "Saved LSGST Gateset w/Regularization") gs_lsgst_compare = pygsti.io.load_gateset(compare_files + "/lsgst.gateset") gs_lsgst_reg_compare = pygsti.io.load_gateset(compare_files + "/lsgst_reg.gateset") gs_lsgst_go = pygsti.optimize_gauge(gs_lsgst, 'target', targetGateset=gs_lsgst_compare, spamWeight=1.0) gs_lsgst_reg_go = pygsti.optimize_gauge(gs_lsgst_reg, 'target', targetGateset=gs_lsgst_reg_compare, spamWeight=1.0) self.assertAlmostEqual( gs_lsgst_go.frobeniusdist(gs_lsgst_compare), 0, places=5) self.assertAlmostEqual( gs_lsgst_reg_go.frobeniusdist(gs_lsgst_reg_compare), 0, places=5)
def test_LGST_no_sample_error(self): ds = pygsti.construction.generate_fake_data(self.datagen_gateset, self.lgstStrings, nSamples=1000, sampleError='none') gs_lgst = pygsti.do_lgst(ds, self.specs, self.gateset, svdTruncateTo=4, verbosity=0) gs_lgst = pygsti.optimize_gauge(gs_lgst, "target", targetGateset=self.datagen_gateset, gateWeight=1.0, spamWeight=1.0) self.assertAlmostEqual( gs_lgst.frobeniusdist(self.datagen_gateset), 0)
def setUp(self): #Set GateSet objects to "strict" mode for testing pygsti.objects.GateSet._strict = True self.targetGateset = std.gs_target datagen_gateset = self.targetGateset.depolarize(gate_noise=0.05, spam_noise=0.1) self.fiducials = std.fiducials self.germs = std.germs self.specs = pygsti.construction.build_spam_specs(self.fiducials, effect_labels=['E0']) #only use the first EVec self.gateLabels = self.targetGateset.gates.keys() # also == std.gates self.lgstStrings = pygsti.construction.list_lgst_gatestrings(self.specs, self.gateLabels) self.maxLengthList = [0,1,2,4,8] self.lsgstStrings = pygsti.construction.make_lsgst_lists( self.gateLabels, self.fiducials, self.fiducials, self.germs, self.maxLengthList) self.ds = pygsti.objects.DataSet(fileToLoadFrom="cmp_chk_files/reportgen.dataset") # RUN BELOW LINES TO GENERATE ANALYSIS DATASET #ds = pygsti.construction.generate_fake_data(datagen_gateset, lsgstStrings[-1], nSamples=1000, # sampleError='binomial', seed=100) #ds.save("cmp_chk_files/reportgen.dataset") gs_lgst = pygsti.do_lgst(self.ds, self.specs, self.targetGateset, svdTruncateTo=4, verbosity=0) gs_lgst_go = pygsti.optimize_gauge(gs_lgst,"target",targetGateset=self.targetGateset) self.gs_clgst = pygsti.contract(gs_lgst_go, "CPTP") self.gs_clgst_tp = pygsti.contract(self.gs_clgst, "vSPAM") self.gs_clgst_tp.set_all_parameterizations("TP") try: import pptx self.have_python_pptx = True except ImportError: warnings.warn("**** IMPORT: Cannot import pptx (python-pptx), and so" + " Powerpoint slide generation tests have been disabled.") self.have_python_pptx = False #Compute results for MC2GST lsgst_gatesets_prego = pygsti.do_iterative_mc2gst(self.ds, self.gs_clgst, self.lsgstStrings, verbosity=0, minProbClipForWeighting=1e-6, probClipInterval=(-1e6,1e6), returnAll=True) lsgst_gatesets = [ pygsti.optimize_gauge(gs, "target", targetGateset=self.targetGateset, gateWeight=1,spamWeight=0.001) for gs in lsgst_gatesets_prego] self.results = pygsti.report.Results() self.results.init_Ls_and_germs("chi2", self.targetGateset, self.ds, self.gs_clgst, self.maxLengthList, self.germs, lsgst_gatesets, self.lsgstStrings, self.fiducials, self.fiducials, pygsti.construction.repeat_with_max_length, False, None, lsgst_gatesets_prego) self.results.parameters.update({'minProbClip': 1e-6, 'minProbClipForWeighting': 1e-4, 'probClipInterval': (-1e6,1e6), 'radius': 1e-4, 'weights': None, 'defaultDirectory': "temp_test_files", 'defaultBasename': "MyDefaultReportName" } ) #Compute results for MLGST with TP constraint lsgst_gatesets_TP = pygsti.do_iterative_mlgst(self.ds, self.gs_clgst_tp, self.lsgstStrings, verbosity=0, minProbClip=1e-4, probClipInterval=(-1e6,1e6), returnAll=True) lsgst_gatesets_TP = [ pygsti.optimize_gauge(gs, "target", targetGateset=self.targetGateset, constrainToTP=True, gateWeight=1,spamWeight=0.001) for gs in lsgst_gatesets_TP] self.results_logL = pygsti.report.Results() self.results_logL.init_Ls_and_germs("logl", self.targetGateset, self.ds, self.gs_clgst_tp, self.maxLengthList, self.germs, lsgst_gatesets_TP, self.lsgstStrings, self.fiducials, self.fiducials, pygsti.construction.repeat_with_max_length, True)
def test_bootstrap(self): ds = pygsti.objects.DataSet(fileToLoadFrom=compare_files + "/drivers.dataset") specs = self.runSilent(pygsti.construction.build_spam_specs, std.fiducials) gs = pygsti.do_lgst(ds, specs, targetGateset=std.gs_target, svdTruncateTo=4, verbosity=0) bootds_p = pygsti.drivers.make_bootstrap_dataset( ds,'parametric', gs, seed=1234 ) bootds_np = pygsti.drivers.make_bootstrap_dataset( ds,'nonparametric', seed=1234 ) with self.assertRaises(ValueError): pygsti.drivers.make_bootstrap_dataset(ds,'foobar', seed=1) #bad generationMethod with self.assertRaises(ValueError): pygsti.drivers.make_bootstrap_dataset(ds,'parametric', seed=1) # must specify gateset for parametric mode with self.assertRaises(ValueError): pygsti.drivers.make_bootstrap_dataset(ds,'nonparametric',gs,seed=1) # must *not* specify gateset for nonparametric mode maxLengths = [0] #just do LGST strings to make this fast... bootgs_p = self.runSilent(pygsti.drivers.make_bootstrap_gatesets, 2, ds, 'parametric', std.fiducials, std.fiducials, std.germs, maxLengths, inputGateSet=gs, constrainToTP=True, returnData=False) default_maxLens = [0]+[2**k for k in range(10)] gateStrings = pygsti.construction.make_lsgst_experiment_list( self.gateLabels, self.fiducials, self.fiducials, self.germs, default_maxLens, fidPairs=None, truncScheme="whole germ powers") ds_defaultMaxLens = pygsti.construction.generate_fake_data( gs, gateStrings, nSamples=1000, sampleError='round') bootgs_p_defaultMaxLens = self.runSilent( pygsti.drivers.make_bootstrap_gatesets, 2, ds_defaultMaxLens, 'parametric', std.fiducials, std.fiducials, std.germs, None, inputGateSet=gs, constrainToTP=True, returnData=False) #test when maxLengths == None bootgs_np, bootds_np2 = self.runSilent( pygsti.drivers.make_bootstrap_gatesets, 2, ds, 'nonparametric', std.fiducials, std.fiducials, std.germs, maxLengths, targetGateSet=gs, constrainToTP=True, returnData=True) with self.assertRaises(ValueError): pygsti.drivers.make_bootstrap_gatesets( 2, ds, 'parametric', std.fiducials, std.fiducials, std.germs, maxLengths, constrainToTP=True, returnData=False) #must specify either inputGateSet or targetGateSet with self.assertRaises(ValueError): pygsti.drivers.make_bootstrap_gatesets( 2, ds, 'parametric', std.fiducials, std.fiducials, std.germs, maxLengths, inputGateSet=gs, targetGateSet=gs, constrainToTP=True, returnData=False) #cannot specify both inputGateSet and targetGateSet self.runSilent(pygsti.drivers.gauge_optimize_gs_list, bootgs_p, std.gs_target, constrainToTP=True, gateMetric = 'frobenius', spamMetric = 'frobenius', plot=False) #Test plotting -- seems to work. self.runSilent(pygsti.drivers.gauge_optimize_gs_list, bootgs_p, std.gs_target, constrainToTP=True, gateMetric = 'frobenius', spamMetric = 'frobenius', plot=True) #Test utility functions -- just make sure they run for now... def gsFn(gs): return gs.get_dimension() tp_target = std.gs_target.copy() tp_target.set_all_parameterizations("TP") pygsti.drivers.gs_stdev(gsFn, bootgs_p) pygsti.drivers.gs_mean(gsFn, bootgs_p) pygsti.drivers.gs_stdev1(gsFn, bootgs_p) pygsti.drivers.gs_mean1(gsFn, bootgs_p) pygsti.drivers.to_vector(bootgs_p[0]) pygsti.drivers.to_mean_gateset(bootgs_p, tp_target) pygsti.drivers.to_std_gateset(bootgs_p, tp_target) pygsti.drivers.to_rms_gateset(bootgs_p, tp_target) pygsti.drivers.gateset_jtracedist(bootgs_p[0], tp_target) pygsti.drivers.gateset_process_fidelity(bootgs_p[0], tp_target) pygsti.drivers.gateset_diamonddist(bootgs_p[0], tp_target) pygsti.drivers.gateset_decomp_angle(bootgs_p[0]) pygsti.drivers.gateset_decomp_decay_diag(bootgs_p[0]) pygsti.drivers.gateset_decomp_decay_offdiag(bootgs_p[0]) pygsti.drivers.spamrameter(bootgs_p[0])