Пример #1
0
    def test_LGST(self):

        ds = self.ds

        print("GG0 = ", self.model.default_gauge_group)
        mdl_lgst = pygsti.do_lgst(ds,
                                  self.fiducials,
                                  self.fiducials,
                                  self.model,
                                  svdTruncateTo=4,
                                  verbosity=0)
        mdl_lgst_verb = self.runSilent(pygsti.do_lgst,
                                       ds,
                                       self.fiducials,
                                       self.fiducials,
                                       self.model,
                                       svdTruncateTo=4,
                                       verbosity=10)
        self.assertAlmostEqual(mdl_lgst.frobeniusdist(mdl_lgst_verb), 0)

        print("GG = ", mdl_lgst.default_gauge_group)
        mdl_lgst_go = pygsti.gaugeopt_to_target(mdl_lgst,
                                                self.model, {
                                                    'spam': 1.0,
                                                    'gates': 1.0
                                                },
                                                checkJac=True)
        mdl_clgst = pygsti.contract(mdl_lgst_go, "CPTP")

        # RUN BELOW LINES TO SEED SAVED GATESET FILES
        if regenerate_references():
            pygsti.io.write_model(
                mdl_lgst, compare_files + "/lgst.model",
                "Saved LGST Model before gauge optimization")
            pygsti.io.write_model(mdl_lgst_go,
                                  compare_files + "/lgst_go.model",
                                  "Saved LGST Model after gauge optimization")
            pygsti.io.write_model(
                mdl_clgst, compare_files + "/clgst.model",
                "Saved LGST Model after G.O. and CPTP contraction")

        mdl_lgst_compare = pygsti.io.load_model(compare_files + "/lgst.model")
        mdl_lgst_go_compare = pygsti.io.load_model(compare_files +
                                                   "/lgst_go.model")
        mdl_clgst_compare = pygsti.io.load_model(compare_files +
                                                 "/clgst.model")

        self.assertAlmostEqual(mdl_lgst.frobeniusdist(mdl_lgst_compare),
                               0,
                               places=5)
        self.assertAlmostEqual(mdl_lgst_go.frobeniusdist(mdl_lgst_go_compare),
                               0,
                               places=5)
        self.assertAlmostEqual(mdl_clgst.frobeniusdist(mdl_clgst_compare),
                               0,
                               places=5)
Пример #2
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
Пример #3
0
    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)
Пример #4
0
    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)
Пример #5
0
    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)
Пример #6
0
    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)
Пример #7
0
    def test_MLGST(self):

        ds = self.ds

        mdl_lgst = pygsti.do_lgst(ds,
                                  self.fiducials,
                                  self.fiducials,
                                  self.model,
                                  svdTruncateTo=4,
                                  verbosity=0)
        mdl_lgst_go = pygsti.gaugeopt_to_target(mdl_lgst,
                                                self.model, {
                                                    'spam': 1.0,
                                                    'gates': 1.0
                                                },
                                                checkJac=True)
        mdl_clgst = pygsti.contract(mdl_lgst_go, "CPTP")
        mdl_clgst = mdl_clgst.depolarize(
            op_noise=0.02, spam_noise=0.02
        )  # just to avoid infinity objective funct & jacs below
        CM = profiler._get_mem_usage()

        mdl_single_mlgst = pygsti.do_mlgst(ds,
                                           mdl_clgst,
                                           self.lsgstStrings[0],
                                           minProbClip=1e-4,
                                           probClipInterval=(-1e2, 1e2),
                                           verbosity=0)

        #this test often gives an assetion error "finite Jacobian has inf norm!" on Travis CI Python 3 case
        try:
            mdl_single_mlgst_cpsp = pygsti.do_mlgst(
                ds,
                mdl_clgst,
                self.lsgstStrings[0],
                minProbClip=1e-4,
                probClipInterval=(-1e2, 1e2),
                cptp_penalty_factor=1.0,
                spam_penalty_factor=1.0,
                verbosity=10)  #uses both penalty factors w/verbosity > 0
        except ValueError:
            pass  # ignore when assertions in customlm.py are disabled
        except AssertionError:
            pass  # just ignore for now.  FUTURE: see what we can do in custom LM about scaling large jacobians...

        try:
            mdl_single_mlgst_cp = pygsti.do_mlgst(ds,
                                                  mdl_clgst,
                                                  self.lsgstStrings[0],
                                                  minProbClip=1e-4,
                                                  probClipInterval=(-1e2, 1e2),
                                                  cptp_penalty_factor=1.0,
                                                  verbosity=10)
        except ValueError:
            pass  # ignore when assertions in customlm.py are disabled
        except AssertionError:
            pass  # just ignore for now.  FUTURE: see what we can do in custom LM about scaling large jacobians...

        try:
            mdl_single_mlgst_sp = pygsti.do_mlgst(ds,
                                                  mdl_clgst,
                                                  self.lsgstStrings[0],
                                                  minProbClip=1e-4,
                                                  probClipInterval=(-1e2, 1e2),
                                                  spam_penalty_factor=1.0,
                                                  verbosity=10)
        except ValueError:
            pass  # ignore when assertions in customlm.py are disabled
        except AssertionError:
            pass  # just ignore for now.  FUTURE: see what we can do in custom LM about scaling large jacobians...

        mdl_mlegst = pygsti.do_iterative_mlgst(ds,
                                               mdl_clgst,
                                               self.lsgstStrings,
                                               verbosity=0,
                                               minProbClip=1e-4,
                                               probClipInterval=(-1e2, 1e2),
                                               memLimit=CM + 1024**3)
        maxLogL, all_gs_mlegst_tups = pygsti.do_iterative_mlgst(
            ds,
            mdl_clgst,
            [[mdl.tup for mdl in gsList] for gsList in self.lsgstStrings],
            minProbClip=1e-4,
            probClipInterval=(-1e2, 1e2),
            returnAll=True,
            returnMaxLogL=True)

        mdl_mlegst_verb = self.runSilent(pygsti.do_iterative_mlgst,
                                         ds,
                                         mdl_clgst,
                                         self.lsgstStrings,
                                         verbosity=10,
                                         minProbClip=1e-4,
                                         probClipInterval=(-1e2, 1e2),
                                         memLimit=CM + 1024**3)
        self.assertAlmostEqual(mdl_mlegst.frobeniusdist(mdl_mlegst_verb),
                               0,
                               places=5)
        self.assertAlmostEqual(mdl_mlegst.frobeniusdist(
            all_gs_mlegst_tups[-1]),
                               0,
                               places=5)

        #Run internal checks on less max-L values (so it doesn't take forever)
        mdl_mlegst_chk = pygsti.do_iterative_mlgst(ds,
                                                   mdl_clgst,
                                                   self.lsgstStrings[0:2],
                                                   verbosity=0,
                                                   minProbClip=1e-4,
                                                   probClipInterval=(-1e2,
                                                                     1e2),
                                                   check=True)

        #Forcing function used by linear response error bars
        forcingfn_grad = np.ones((1, mdl_clgst.num_params()), 'd')
        mdl_lsgst_chk_opts3 = pygsti.algorithms.core._do_mlgst_base(
            ds,
            mdl_clgst,
            self.lsgstStrings[0],
            verbosity=0,
            minProbClip=1e-4,
            probClipInterval=(-1e2, 1e2),
            forcefn_grad=forcingfn_grad)
        with self.assertRaises(NotImplementedError):
            # Non-poisson picture needs support for a non-leastsq solver (not impl yet)
            mdl_lsgst_chk_opts4 = pygsti.algorithms.core._do_mlgst_base(
                ds,
                mdl_clgst,
                self.lsgstStrings[0],
                verbosity=0,
                poissonPicture=False,
                minProbClip=1e-4,
                probClipInterval=(-1e2, 1e2),
                forcefn_grad=forcingfn_grad)  # non-poisson picture

        #Check with small but ok memlimit -- not anymore since new mem estimation uses current memory, making this non-robust
        #self.runSilent(pygsti.do_mlgst, ds, mdl_clgst, self.lsgstStrings[0], minProbClip=1e-6,
        #                probClipInterval=(-1e2,1e2), verbosity=4, memLimit=curMem+8500000) #invoke memory control

        #non-Poisson picture - should use (-1,-1) model for consistency?
        with self.assertRaises(NotImplementedError):
            # Non-poisson picture needs support for a non-leastsq solver (not impl yet)
            pygsti.do_mlgst(ds,
                            mdl_clgst,
                            self.lsgstStrings[0],
                            minProbClip=1e-4,
                            probClipInterval=(-1e2, 1e2),
                            verbosity=0,
                            poissonPicture=False)
            try:
                pygsti.do_mlgst(
                    ds,
                    mdl_clgst,
                    self.lsgstStrings[0],
                    minProbClip=1e-1,  # 1e-1 b/c get inf Jacobians...
                    probClipInterval=(-1e2, 1e2),
                    verbosity=0,
                    poissonPicture=False,
                    spam_penalty_factor=1.0,
                    cptp_penalty_factor=1.0)
            except ValueError:
                pass  # ignore when assertions in customlm.py are disabled
            except AssertionError:
                pass  # just ignore for now.  FUTURE: see what we can do in custom LM about scaling large jacobians...

        #Check errors:
        with self.assertRaises(MemoryError):
            pygsti.do_mlgst(ds,
                            mdl_clgst,
                            self.lsgstStrings[0],
                            minProbClip=1e-4,
                            probClipInterval=(-1e2, 1e2),
                            verbosity=0,
                            memLimit=1)

        # RUN BELOW LINES TO SEED SAVED GATESET FILES
        if regenerate_references():
            pygsti.io.write_model(mdl_mlegst, compare_files + "/mle_gst.model",
                                  "Saved MLE-GST Model")

        mdl_mle_compare = pygsti.io.load_model(compare_files +
                                               "/mle_gst.model")
        mdl_mlegst_go = pygsti.gaugeopt_to_target(mdl_mlegst,
                                                  mdl_mle_compare,
                                                  {'spam': 1.0},
                                                  checkJac=True)

        self.assertAlmostEqual(mdl_mlegst_go.frobeniusdist(mdl_mle_compare),
                               0,
                               places=4)
Пример #8
0
    def setUp(self):
        super(ReportBaseCase, self).setUp()

        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 = list(
            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=compare_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(compare_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,
                                           gateWeight=1.0,
                                           spamWeight=0.0)
        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_files + "",
            'defaultBasename': "MyDefaultReportName"
        })
        self.results.options.precision = 4
        self.results.options.polar_precision = 3

        #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)
        self.results_logL.options.precision = 4
        self.results_logL.options.polar_precision = 3

        try:
            basestring  #Only defined in Python 2
            self.versionsuffix = ""  #Python 2
        except NameError:
            self.versionsuffix = "v3"  #Python 3
Пример #9
0
    def test_LGST(self):

        ds = self.ds
        #pygsti.construction.generate_fake_data(self.datagen_gateset, self.lgstStrings, nSamples=1000,
        #                                            sampleError='binomial', seed=None)

        print("GG0 = ",self.gateset.default_gauge_group)
        gs_lgst = pygsti.do_lgst(ds, self.fiducials, self.fiducials, self.gateset, svdTruncateTo=4, verbosity=0)
        gs_lgst_verb = self.runSilent(pygsti.do_lgst, ds, self.fiducials, self.fiducials, self.gateset, svdTruncateTo=4, verbosity=10)
        self.assertAlmostEqual(gs_lgst.frobeniusdist(gs_lgst_verb),0)

        print("GG = ",gs_lgst.default_gauge_group)
        gs_lgst_go = pygsti.gaugeopt_to_target(gs_lgst,self.gateset, {'spam':1.0, 'gates': 1.0}, checkJac=True)
        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, places=5)
        self.assertAlmostEqual( gs_lgst_go.frobeniusdist(gs_lgst_go_compare), 0, places=5)
        self.assertAlmostEqual( gs_clgst.frobeniusdist(gs_clgst_compare), 0, places=5)

        #Check for error conditions
        with self.assertRaises(ValueError):
            gs_lgst = pygsti.do_lgst(ds, self.fiducials, self.fiducials, None, svdTruncateTo=4, verbosity=0) #no target gateset

        with self.assertRaises(ValueError):
            gs_lgst = pygsti.do_lgst(ds, self.fiducials, self.fiducials, None, gateLabels=list(self.gateset.gates.keys()),
                                     svdTruncateTo=4, verbosity=0) #no spam dict

        #No need for identity vector anymore
        #with self.assertRaises(ValueError):
        #    gs_lgst = pygsti.do_lgst(ds, self.fiducials, self.fiducials, 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_fids =pygsti.construction.gatestring_list([('Gx',),('Gx',),('Gx',),('Gx',)])
            gs_lgst = pygsti.do_lgst(ds, bad_fids, bad_fids, self.gateset, svdTruncateTo=4, verbosity=0) # bad fiducials (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.fiducials, self.fiducials, 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.fiducials, self.fiducials, self.gateset,
                                     svdTruncateTo=4, verbosity=0)
Пример #10
0
    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.fiducials, self.fiducials, self.gateset, svdTruncateTo=4, verbosity=0)
        gs_lgst_go = pygsti.gaugeopt_to_target(gs_lgst,self.gateset, {'spam':1.0, 'gates': 1.0}, checkJac=True)
        gs_clgst = pygsti.contract(gs_lgst_go, "CPTP")
        CM = pygsti.baseobjs.profiler._get_mem_usage()

        gs_single_lsgst = pygsti.do_mc2gst(ds, gs_clgst, self.lsgstStrings[0], minProbClipForWeighting=1e-4,
                                           probClipInterval=(-1e6,1e6), regularizeFactor=1e-3,
                                           verbosity=0) #uses regularizeFactor

        gs_single_lsgst_cp = pygsti.do_mc2gst(ds, gs_clgst, self.lsgstStrings[0], minProbClipForWeighting=1e-4,
                                           probClipInterval=(-1e6,1e6), cptp_penalty_factor=1.0,
                                           verbosity=0) #uses cptp_penalty_factor

        gs_single_lsgst_sp = pygsti.do_mc2gst(ds, gs_clgst, self.lsgstStrings[0], minProbClipForWeighting=1e-4,
                                              probClipInterval=(-1e6,1e6), spam_penalty_factor=1.0,
                                              verbosity=0) #uses spam_penalty_factor

        gs_single_lsgst_cpsp = pygsti.do_mc2gst(ds, gs_clgst, self.lsgstStrings[0], minProbClipForWeighting=1e-4,
                                                probClipInterval=(-1e6,1e6), cptp_penalty_factor=1.0,
                                                spam_penalty_factor=1.0, verbosity=0) #uses both penalty factors

        gs_single_lsgst_cpsp = self.runSilent(pygsti.do_mc2gst, ds, gs_clgst, self.lsgstStrings[0], minProbClipForWeighting=1e-4,
                                              probClipInterval=(-1e6,1e6), cptp_penalty_factor=1.0,
                                              spam_penalty_factor=1.0, verbosity=10) #uses both penalty factors w/verbosity high
        gs_single_lsgst_cp = self.runSilent(pygsti.do_mc2gst, ds, gs_clgst, self.lsgstStrings[0], minProbClipForWeighting=1e-4,
                                            probClipInterval=(-1e6,1e6), cptp_penalty_factor=1.0,
                                            verbosity=10) #uses cptp_penalty_factor w/verbosity high
        gs_single_lsgst_sp = self.runSilent(pygsti.do_mc2gst, ds, gs_clgst, self.lsgstStrings[0], minProbClipForWeighting=1e-4,
                                            probClipInterval=(-1e6,1e6), spam_penalty_factor=1.0,
                                            verbosity=10) #uses spam_penalty_factor w/verbosity high


        
        gs_lsgst = pygsti.do_iterative_mc2gst(ds, gs_clgst, self.lsgstStrings, verbosity=0,
                                             minProbClipForWeighting=1e-6, probClipInterval=(-1e6,1e6),
                                             memLimit=CM + 1024**3)
        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=CM + 1024**3)
        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=CM + 1024**3)
        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=CM + 1024**3)

        #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 } )

        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"]
        del gs_withA1.gates["Gx"] # otherwise gs_withA1 will have Gx params that we have no knowledge of!
        gs_lsgst_chk_opts2 = pygsti.do_mc2gst(ds, gs_withA1, aliased_list, minProbClipForWeighting=1e-6,
                                              probClipInterval=(-1e2,1e2), verbosity=10,
                                              gateLabelAliases={ 'GA1': ('Gx',) })

        
        #Check with small but ok memlimit -- not anymore since new mem estimation uses current memory, making this non-robust
        #self.runSilent(pygsti.do_mc2gst,ds, gs_clgst, self.lsgstStrings[0], minProbClipForWeighting=1e-6,
        #                 probClipInterval=(-1e6,1e6), regularizeFactor=1e-3,
        #                 verbosity=10, memLimit=CM + 1024**3)


        #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(AssertionError):
            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) #can't specify both cptp_penalty_factor and regularizeFactor


        # 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.gaugeopt_to_target(gs_lsgst, gs_lsgst_compare, {'spam':1.0}, checkJac=True)

        gs_lsgst_reg_go = pygsti.gaugeopt_to_target(gs_lsgst_reg, gs_lsgst_reg_compare, {'spam':1.0}, checkJac=True)

        self.assertAlmostEqual( gs_lsgst_go.frobeniusdist(gs_lsgst_compare), 0, places=4)
        self.assertAlmostEqual( gs_lsgst_reg_go.frobeniusdist(gs_lsgst_reg_compare), 0, places=4)
Пример #11
0
    def test_eLGST(self):

        ds = self.ds
        #pygsti.construction.generate_fake_data(self.datagen_gateset, self.lsgstStrings[-1],
        #                                            nSamples=1000,sampleError='binomial', seed=100)

        assert(pygsti.obj.GateSet._pcheck)
        gs_lgst = pygsti.do_lgst(ds, self.fiducials, self.fiducials, self.gateset, svdTruncateTo=4, verbosity=0)
        gs_lgst._check_paramvec()
        gs_lgst_go = pygsti.gaugeopt_to_target(gs_lgst,self.gateset, {'spam':1.0, 'gates': 1.0}, checkJac=True)
        gs_lgst_go._check_paramvec()
        gs_clgst = pygsti.contract(gs_lgst_go, "CPTP")
        gs_clgst._check_paramvec()
        self.gateset._check_paramvec()
        
        _,gs_single_exlgst = pygsti.do_exlgst(ds, gs_clgst, self.elgstStrings[0], self.fiducials, self.fiducials,
                                            self.gateset, regularizeFactor=1e-3, svdTruncateTo=4,
                                              verbosity=0)
        gs_single_exlgst._check_paramvec()

        _,gs_single_exlgst_verb = self.runSilent(pygsti.do_exlgst, ds, gs_clgst, self.elgstStrings[0], self.fiducials, self.fiducials,
                                               self.gateset, regularizeFactor=1e-3, svdTruncateTo=4,
                                               verbosity=10)
        gs_single_exlgst_verb._check_paramvec()
        
        self.assertAlmostEqual(gs_single_exlgst.frobeniusdist(gs_single_exlgst_verb),0)

        gs_exlgst = pygsti.do_iterative_exlgst(ds, gs_clgst, self.fiducials, self.fiducials, self.elgstStrings,
                                               targetGateset=self.gateset, svdTruncateTo=4, verbosity=0)

        all_minErrs, all_gs_exlgst_tups = pygsti.do_iterative_exlgst(
            ds, gs_clgst, self.fiducials, self.fiducials, [ [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.fiducials, self.fiducials, self.elgstStrings,
                                        targetGateset=self.gateset, svdTruncateTo=4, verbosity=10)
        gs_exlgst_reg = pygsti.do_iterative_exlgst(ds, gs_clgst, self.fiducials, self.fiducials, 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.fiducials, self.fiducials, 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.fiducials, self.fiducials, 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.set_all_parameterizations("full") # b/c ex-LGST sets spam to StaticSPAMVec objects (b/c they're not optimized)
        gs_exlgst_reg.set_all_parameterizations("full") # b/c ex-LGST sets spam to StaticSPAMVec objects (b/c they're not optimized)
        gs_exlgst_go = pygsti.gaugeopt_to_target(gs_exlgst,gs_exlgst_compare, {'spam':1.0 }, checkJac=True)
        gs_exlgst_reg_go = pygsti.gaugeopt_to_target(gs_exlgst_reg,gs_exlgst_reg_compare, {'spam':1.0 }, checkJac=True)
Пример #12
0
    def setUpClass(cls):
        """ 
        Handle all once-per-class (slow) computation and loading,
         to avoid calling it for each test (like setUp).  Store
         results in class variable for use within setUp.
        """
        super(ReportBaseCase, cls).setUpClass()

        orig_cwd = os.getcwd()
        os.chdir(os.path.abspath(os.path.dirname(__file__)))
        os.chdir('..')  # The test_packages directory

        targetGateset = std.gs_target
        datagen_gateset = targetGateset.depolarize(gate_noise=0.05,
                                                   spam_noise=0.1)
        datagen_gateset2 = targetGateset.depolarize(gate_noise=0.1,
                                                    spam_noise=0.05).rotate(
                                                        (0.15, -0.03, 0.03))

        #cls.specs = pygsti.construction.build_spam_specs(std.fiducials, effect_labels=['E0'])
        #  #only use the first EVec

        gateLabels = std.gates
        cls.lgstStrings = pygsti.construction.list_lgst_gatestrings(
            std.fiducials, std.fiducials, gateLabels)
        cls.maxLengthList = [1, 2, 4, 8]

        cls.lsgstStrings = pygsti.construction.make_lsgst_lists(
            gateLabels, std.fiducials, std.fiducials, std.germs,
            cls.maxLengthList)
        cls.lsgstStructs = pygsti.construction.make_lsgst_structs(
            gateLabels, std.fiducials, std.fiducials, std.germs,
            cls.maxLengthList)

        try:
            basestring  #Only defined in Python 2
            cls.versionsuffix = ""  #Python 2
        except NameError:
            cls.versionsuffix = "v3"  #Python 3

        # RUN BELOW LINES TO GENERATE ANALYSIS DATASET
        #ds = pygsti.construction.generate_fake_data(datagen_gateset, cls.lsgstStrings[-1], nSamples=1000,
        #                                            sampleError='binomial', seed=100)
        #ds.save(compare_files + "/reportgen.dataset%s" % cls.versionsuffix)
        #ds2 = pygsti.construction.generate_fake_data(datagen_gateset2, cls.lsgstStrings[-1], nSamples=1000,
        #                                            sampleError='binomial', seed=100)
        #ds2.save(compare_files + "/reportgen2.dataset%s" % cls.versionsuffix)

        cls.ds = pygsti.objects.DataSet(
            fileToLoadFrom=compare_files +
            "/reportgen.dataset%s" % cls.versionsuffix)
        cls.ds2 = pygsti.objects.DataSet(
            fileToLoadFrom=compare_files +
            "/reportgen2.dataset%s" % cls.versionsuffix)

        gs_lgst = pygsti.do_lgst(cls.ds,
                                 std.fiducials,
                                 std.fiducials,
                                 targetGateset,
                                 svdTruncateTo=4,
                                 verbosity=0)
        gs_lgst_go = pygsti.gaugeopt_to_target(gs_lgst, targetGateset, {
            'gates': 1.0,
            'spam': 0.0
        })
        cls.gs_clgst = pygsti.contract(gs_lgst_go, "CPTP")
        cls.gs_clgst_tp = pygsti.contract(cls.gs_clgst, "vSPAM")
        cls.gs_clgst_tp.set_all_parameterizations("TP")

        #Compute results for MC2GST
        lsgst_gatesets_prego = pygsti.do_iterative_mc2gst(
            cls.ds,
            cls.gs_clgst,
            cls.lsgstStrings,
            verbosity=0,
            minProbClipForWeighting=1e-6,
            probClipInterval=(-1e6, 1e6),
            returnAll=True)

        cls.results = pygsti.objects.Results()
        cls.results.init_dataset(cls.ds)
        cls.results.init_gatestrings(cls.lsgstStructs)
        cls.results.add_estimate(
            targetGateset, cls.gs_clgst, lsgst_gatesets_prego, {
                'objective': "chi2",
                'minProbClipForWeighting': 1e-4,
                'probClipInterval': (-1e6, 1e6),
                'radius': 1e-4,
                'weights': None,
                'defaultDirectory': temp_files + "",
                'defaultBasename': "MyDefaultReportName"
            })

        gaugeOptParams = collections.OrderedDict([
            ('gateset', lsgst_gatesets_prego[-1]),  #so can gauge-propagate CIs
            ('targetGateset', targetGateset),  #so can gauge-propagate CIs
            ('cptp_penalty_factor', 0),
            ('gatesMetric', "frobenius"),
            ('spamMetric', "frobenius"),
            ('itemWeights', {
                'gates': 1.0,
                'spam': 0.001
            }),
            ('returnAll', True)
        ])

        _, gaugeEl, go_final_gateset = pygsti.gaugeopt_to_target(
            **gaugeOptParams)
        gaugeOptParams['_gaugeGroupEl'] = gaugeEl  #so can gauge-propagate CIs
        cls.results.estimates['default'].add_gaugeoptimized(
            gaugeOptParams, go_final_gateset)
        cls.results.estimates['default'].add_gaugeoptimized(
            gaugeOptParams, go_final_gateset, "go_dup")

        #Compute results for MLGST with TP constraint
        # Use do_long_sequence_gst with a non-mark dataset to trigger data scaling
        tp_target = targetGateset.copy()
        tp_target.set_all_parameterizations("TP")

        cls.ds3 = cls.ds.copy_nonstatic()
        cls.ds3.add_counts_from_dataset(cls.ds2)
        cls.ds3.done_adding_data()

        cls.results_logL = pygsti.do_long_sequence_gst(
            cls.ds3,
            tp_target,
            std.fiducials,
            std.fiducials,
            std.germs,
            cls.maxLengthList,
            verbosity=0,
            advancedOptions={
                'tolerance': 1e-6,
                'starting point': 'LGST',
                'onBadFit': ["robust", "Robust", "robust+", "Robust+"],
                'badFitThreshold': -1.0,
                'germLengthLimits': {
                    ('Gx', 'Gi', 'Gi'): 2
                }
            })
        #OLD
        #lsgst_gatesets_TP = pygsti.do_iterative_mlgst(cls.ds, cls.gs_clgst_tp, cls.lsgstStrings, verbosity=0,
        #                                           minProbClip=1e-4, probClipInterval=(-1e6,1e6),
        #                                           returnAll=True) #TP initial gateset => TP output gatesets
        #cls.results_logL = pygsti.objects.Results()
        #cls.results_logL.init_dataset(cls.ds)
        #cls.results_logL.init_gatestrings(cls.lsgstStructs)
        #cls.results_logL.add_estimate(targetGateset, cls.gs_clgst_tp,
        #                         lsgst_gatesets_TP,
        #                         {'objective': "logl",
        #                          'minProbClip': 1e-4,
        #                          'probClipInterval': (-1e6,1e6), 'radius': 1e-4,
        #                          'weights': None, 'defaultDirectory': temp_files + "",
        #                          'defaultBasename': "MyDefaultReportName"})
        #
        #tp_target = targetGateset.copy(); tp_target.set_all_parameterizations("TP")
        #gaugeOptParams = gaugeOptParams.copy() #just to be safe
        #gaugeOptParams['gateset'] = lsgst_gatesets_TP[-1]  #so can gauge-propagate CIs
        #gaugeOptParams['targetGateset'] = tp_target  #so can gauge-propagate CIs
        #_, gaugeEl, go_final_gateset = pygsti.gaugeopt_to_target(**gaugeOptParams)
        #gaugeOptParams['_gaugeGroupEl'] = gaugeEl #so can gauge-propagate CIs
        #cls.results_logL.estimates['default'].add_gaugeoptimized(gaugeOptParams, go_final_gateset)
        #
        ##self.results_logL.options.precision = 3
        ##self.results_logL.options.polar_precision = 2

        os.chdir(orig_cwd)
Пример #13
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
Пример #14
0
    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)
Пример #15
0
    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)
Пример #16
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)

        mdl_lgst = pygsti.do_lgst(ds, self.fiducials, self.fiducials, self.model, svdTruncateTo=4, verbosity=0)


        #Gauge Opt to Target
        mdl_lgst_target     = self.runSilent(pygsti.gaugeopt_to_target, mdl_lgst, self.model, verbosity=10, checkJac=True)

        #
        mdl_lgst.basis = self.model.basis.copy()
        mdl_clgst_cp    = self.runSilent(pygsti.contract, mdl_lgst, "CP",verbosity=10, tol=10.0, useDirectCP=False) #non-direct CP contraction

        #Gauge Opt to Target using non-frobenius metrics
        mdl_lgst_targetAlt  = self.runSilent(pygsti.gaugeopt_to_target, mdl_lgst_target, self.model,
                                            gatesMetric='fidelity', verbosity=10, checkJac=True)

        mdl_lgst_targetAlt  = self.runSilent(pygsti.gaugeopt_to_target, mdl_lgst_target, self.model,
                                            gatesMetric='tracedist', verbosity=10, checkJac=True)

        mdl_lgst_targetAlt  = self.runSilent(pygsti.gaugeopt_to_target, mdl_lgst_target, self.model,
                                            spamMetric='fidelity', verbosity=10, checkJac=True)

        mdl_lgst_targetAlt  = self.runSilent(pygsti.gaugeopt_to_target, mdl_lgst_target, self.model,
                                            spamMetric='tracedist', verbosity=10, checkJac=True)

        #Using other methods
        mdl_BFGS = self.runSilent(pygsti.gaugeopt_to_target, mdl_lgst, self.model, method='BFGS', verbosity=10)
        with self.assertRaises(ValueError): #Invalid metric
            self.runSilent(pygsti.gaugeopt_to_target, mdl_lgst, self.model, method='BFGS', spamMetric='foobar', verbosity=10)
        with self.assertRaises(ValueError): #Invalid metric
            self.runSilent(pygsti.gaugeopt_to_target, mdl_lgst, self.model, method='BFGS', gatesMetric='foobar', verbosity=10)
            
        with self.assertRaises(ValueError): #can't use least-squares for anything but frobenius metric
            self.runSilent(pygsti.gaugeopt_to_target, mdl_lgst_target, self.model,
                           spamMetric='tracedist', method='ls', verbosity=10, checkJac=True)


        #with self.assertRaises(ValueError):
        #    self.runSilent(pygsti.gaugeopt_to_target, mdl_lgst_target, self.model,
        #                   gatesMetric='foobar', verbosity=10) #bad gatesMetric
        #
        #with self.assertRaises(ValueError):
        #    self.runSilent(pygsti.gaugeopt_to_target, mdl_lgst_target, self.model,
        #                   spamMetric='foobar', verbosity=10) #bad spamMetric
                

        #Contractions
        mdl_clgst_tp    = self.runSilent(pygsti.contract, mdl_lgst_target, "TP",verbosity=10, tol=10.0)
        mdl_clgst_cp    = self.runSilent(pygsti.contract, mdl_lgst_target, "CP",verbosity=10, tol=10.0)
        mdl_clgst_cp2    = self.runSilent(pygsti.contract, mdl_lgst_target, "CP",verbosity=10, tol=10.0)
        mdl_clgst_cptp  = self.runSilent(pygsti.contract, mdl_lgst_target, "CPTP",verbosity=10, tol=10.0)
        mdl_clgst_cptp2 = self.runSilent(pygsti.contract, mdl_lgst_target, "CPTP",verbosity=10, useDirectCP=False)
        mdl_clgst_cptp3 = self.runSilent(pygsti.contract, mdl_lgst_target, "CPTP",verbosity=10, tol=10.0, maxiter=0)
        mdl_clgst_xp    = self.runSilent(pygsti.contract, mdl_lgst_target, "XP", ds,verbosity=10, tol=10.0)
        mdl_clgst_xptp  = self.runSilent(pygsti.contract, mdl_lgst_target, "XPTP", ds,verbosity=10, tol=10.0)
        mdl_clgst_vsp   = self.runSilent(pygsti.contract, mdl_lgst_target, "vSPAM",verbosity=10, tol=10.0)
        mdl_clgst_none  = self.runSilent(pygsti.contract, mdl_lgst_target, "nothing",verbosity=10, tol=10.0)

          #test bad effect vector cases
        mdl_bad_effect = mdl_lgst_target.copy()
        mdl_bad_effect.povms['Mdefault'] = pygsti.obj.UnconstrainedPOVM( [('0',[100.0,0,0,0])] ) # E eigvals all > 1.0
        self.runSilent(pygsti.contract, mdl_bad_effect, "vSPAM",verbosity=10, tol=10.0)
        mdl_bad_effect.povms['Mdefault'] = pygsti.obj.UnconstrainedPOVM( [('0',[-100.0,0,0,0])] ) # E eigvals all < 0
        self.runSilent(pygsti.contract, mdl_bad_effect, "vSPAM",verbosity=10, tol=10.0)

        #with self.assertRaises(ValueError):
        #    self.runSilent(pygsti.contract, mdl_lgst_target, "foobar",verbosity=10, tol=10.0) #bad toWhat

            
        #More gauge optimizations
        TP_gauge_group = pygsti.obj.TPGaugeGroup(mdl_lgst.dim)
        mdl_lgst_target_cp  = self.runSilent(pygsti.gaugeopt_to_target, mdl_clgst_cptp, self.model, 
                                            cptp_penalty_factor=1.0, gauge_group=TP_gauge_group, verbosity=10, checkJac=True)

        mdl_lgst_tp         = self.runSilent(pygsti.gaugeopt_to_target, mdl_lgst, None,
                                            spam_penalty_factor=1.0, verbosity=10, checkJac=True)

        mdl_lgst.basis = Basis.cast("gm",4) #so CPTP optimizations can work on mdl_lgst
        mdl_lgst_cptp       = self.runSilent(pygsti.gaugeopt_to_target, mdl_lgst, None,
                                            cptp_penalty_factor=1.0, spam_penalty_factor=1.0, verbosity=10, checkJac=True)

        mdl_lgst_cptp_tp    = self.runSilent(pygsti.gaugeopt_to_target, mdl_lgst, None,
                                            cptp_penalty_factor=1.0, spam_penalty_factor=1.0, gauge_group=TP_gauge_group, verbosity=10, checkJac=True) #no point? (remove?)

        #I'm not sure why moving this test upward fixes a singlar matrix error (TODO LATER? - could one of above tests modify mdl_lgst??)
        #mdl_lgst_tp         = self.runSilent(pygsti.gaugeopt_to_target( mdl_lgst, None,
        #                                    spam_penalty_factor=1.0, verbosity=10, checkJac=True)

        mdl_lgst_tptarget   = self.runSilent(pygsti.gaugeopt_to_target, mdl_lgst, self.model,
                                            spam_penalty_factor=1.0, verbosity=10, checkJac=True)

        mdl_lgst_cptptarget = self.runSilent(pygsti.gaugeopt_to_target, mdl_lgst, self.model,
                                            cptp_penalty_factor=1.0, spam_penalty_factor=1.0, verbosity=10, checkJac=True)

        mdl_lgst_cptptarget2= self.runSilent(pygsti.gaugeopt_to_target, mdl_lgst, self.model,
                                            cptp_penalty_factor=1.0, spam_penalty_factor=1.0, gauge_group=TP_gauge_group, verbosity=10, checkJac=True) #no point? (remove?)

        #Use "None" gauge group
        mdl_none = self.runSilent(pygsti.gaugeopt_to_target, mdl_lgst, self.model, gauge_group=None, verbosity=10)
        soln, trivialEl, mdl_none = self.runSilent(pygsti.gaugeopt_to_target, mdl_lgst, self.model, gauge_group=None, verbosity=10, returnAll=True)

        #Use "None" default gauge group
        mdl_none = self.model.copy()
        mdl_none.default_gauge_group = None
        self.runSilent(pygsti.gaugeopt_to_target, mdl_none, self.model, verbosity=10)
        soln, trivialEl, mdl_none = self.runSilent(pygsti.gaugeopt_to_target, mdl_none, self.model, verbosity=10, returnAll=True)


        #TODO: check output lies in space desired

        # big kick that should land it outside XP, TP, etc, so contraction
        # routines are more tested
        mdl_bigkick = mdl_lgst_target.kick(absmag=1.0)
        mdl_badspam = mdl_bigkick.copy()
        mdl_badspam.povms['Mdefault'] = pygsti.obj.UnconstrainedPOVM( [('0',np.array( [[2],[0],[0],[4]], 'd'))] )
          #set a bad evec so vSPAM has to work...
        

        mdl_clgst_tp    = self.runSilent(pygsti.contract,mdl_bigkick, "TP", verbosity=10, tol=10.0)
        mdl_clgst_cp    = self.runSilent(pygsti.contract,mdl_bigkick, "CP", verbosity=10, tol=10.0)
        mdl_clgst_cptp  = self.runSilent(pygsti.contract,mdl_bigkick, "CPTP", verbosity=10, tol=10.0)
        mdl_clgst_xp    = self.runSilent(pygsti.contract,mdl_bigkick, "XP", ds, verbosity=10, tol=10.0)
        mdl_clgst_xptp  = self.runSilent(pygsti.contract,mdl_bigkick, "XPTP", ds, verbosity=10, tol=10.0)
        mdl_clgst_vsp   = self.runSilent(pygsti.contract,mdl_badspam, "vSPAM", verbosity=10, tol=10.0)
        mdl_clgst_none  = self.runSilent(pygsti.contract,mdl_bigkick, "nothing", verbosity=10, tol=10.0)

        #TODO: check output lies in space desired

        #Check Errors
        with self.assertRaises(ValueError):
            pygsti.contract(mdl_lgst_target, "FooBar",verbosity=0) # bad toWhat argument
Пример #17
0
    def test_table_generation(self):
        import pygsti.report.generation as gen
        formats = ['latex','html','test','ppt'] #all the formats we know
        tableclass = "cssClass"
        longtable = False
        confidenceRegionInfo = None

        gateset = pygsti.io.load_gateset(compare_files + "/analysis.gateset")
        ds = pygsti.objects.DataSet(fileToLoadFrom=compare_files + "/analysis.dataset")

        chi2, chi2Hessian = pygsti.chi2(ds, gateset, returnHessian=True)
        ci = pygsti.obj.ConfidenceRegion(gateset, chi2Hessian, 95.0,
                                         hessianProjection="std")
        gateset_tp = pygsti.contract(gateset,"TP"); gateset_tp.set_all_parameterizations("TP")
        chi2, chi2Hessian_TP = pygsti.chi2(ds, gateset_tp, returnHessian=True)
        ci_TP = pygsti.obj.ConfidenceRegion(gateset_tp, chi2Hessian_TP, 95.0,
                                            hessianProjection="std")

        chi2, chi2Hessian_tgt = pygsti.chi2(ds, std.gs_target, returnHessian=True)
        ci_tgt = pygsti.obj.ConfidenceRegion(std.gs_target, chi2Hessian_tgt, 95.0,
                                         hessianProjection="std")
        target_tp = std.gs_target.copy(); target_tp.set_all_parameterizations("TP")
        chi2, chi2Hessian_tgt_TP = pygsti.chi2(ds, target_tp, returnHessian=True)
        ci_TP_tgt = pygsti.obj.ConfidenceRegion(target_tp, chi2Hessian_tgt_TP, 95.0,
                                            hessianProjection="std")

        gateset_2q = pygsti.construction.build_gateset(
            [4], [('Q0','Q1')],['GIX','GIY','GXI','GYI','GCNOT'],
            [ "I(Q0):X(pi/2,Q1)", "I(Q0):Y(pi/2,Q1)", "X(pi/2,Q0):I(Q1)", "Y(pi/2,Q0):I(Q1)", "CX(pi,Q0,Q1)" ],
            prepLabels=['rho0'], prepExpressions=["0"], effectLabels=['E0','E1','E2'], effectExpressions=["0","1","2"],
            spamdefs={'upup': ('rho0','E0'), 'updn': ('rho0','E1'), 'dnup': ('rho0','E2'),
                           'dndn': ('rho0','remainder') }, basis="pp" )


        #tests which fill in the cracks of the full-report tests
        tab = gen.get_gateset_spam_table(gateset, None)
        tab_wCI = gen.get_gateset_spam_table(gateset, ci)
        table_wCI_as_str = str(tab_wCI)

        gen.get_gateset_spam_table(gateset, None)
        gen.get_gateset_gates_table(gateset_tp, ci_TP) #test zero-padding
        gen.get_unitary_gateset_gates_table(std.gs_target, ci_tgt) #unitary gates w/CIs
        gen.get_unitary_gateset_gates_table(target_tp, ci_TP_tgt) #unitary gates w/CIs
        gen.get_gateset_closest_unitary_table(gateset_2q, None) #test higher-dim gateset
        gen.get_gateset_closest_unitary_table(gateset, ci) #test with CIs (long...)
        gen.get_gateset_rotn_axis_table(std.gs_target, None) #try to get "--"s and "X"s to display
        gen.get_chi2_progress_table([0], [gateset_tp], [ [('Gx',)],], ds) #TP case
        gen.get_chi2_confidence_region(gateset_tp, ds, 95) #TP case

        gen.get_gatestring_multi_table([ [('Gx',),('Gz',)], [('Gy',)] ],
                                       ["list1","list2"], commonTitle=None) #commonTitle == None case w/diff length lists

        with self.assertRaises(ValueError):
            gen.get_unitary_gateset_gates_table(std.gs_target, ci) #gateset-CI mismatch
        with self.assertRaises(ValueError):
            gen.get_gateset_spam_parameters_table(std.gs_target, ci) #gateset-CI mismatch




        #LogL case tests
        gen.get_logl_progress_table([0], [gateset_tp], [ [('Gx',)],], ds) # logL case
        gen.get_logl_progress_table([0], [gateset], [ [('Gx',)],], ds) # logL case

        gen.get_logl_confidence_region(gateset_tp, ds, 95,
                                       gatestring_list=None, probClipInterval=(-1e6,1e6),
                                       minProbClip=1e-4, radius=1e-4, hessianProjection="std")
        gen.get_logl_confidence_region(gateset, ds, 95,
                                       gatestring_list=None, probClipInterval=(-1e6,1e6),
                                       minProbClip=1e-4, radius=1e-4, hessianProjection="std")
Пример #18
0
    def test_MC2GST(self):

        ds = self.ds
        #pygsti.construction.generate_fake_data(self.datagen_gateset, self.lsgstStrings[-1],
        #                                            nSamples=1000, sampleError='binomial', seed=100)

        mdl_lgst = pygsti.do_lgst(ds, self.fiducials, self.fiducials, self.model, svdTruncateTo=4, verbosity=0)
        mdl_lgst_go = pygsti.gaugeopt_to_target(mdl_lgst,self.model, {'spam':1.0, 'gates': 1.0}, checkJac=True)
        mdl_clgst = pygsti.contract(mdl_lgst_go, "CPTP")
        CM = pygsti.baseobjs.profiler._get_mem_usage()

        mdl_single_lsgst = pygsti.do_mc2gst(ds, mdl_clgst, self.lsgstStrings[0], minProbClipForWeighting=1e-4,
                                           probClipInterval=(-1e6,1e6), regularizeFactor=1e-3,
                                           verbosity=0) #uses regularizeFactor

        mdl_single_lsgst_cp = pygsti.do_mc2gst(ds, mdl_clgst, self.lsgstStrings[0], minProbClipForWeighting=1e-4,
                                           probClipInterval=(-1e6,1e6), cptp_penalty_factor=1.0,
                                           verbosity=0) #uses cptp_penalty_factor

        mdl_single_lsgst_sp = pygsti.do_mc2gst(ds, mdl_clgst, self.lsgstStrings[0], minProbClipForWeighting=1e-4,
                                              probClipInterval=(-1e6,1e6), spam_penalty_factor=1.0,
                                              verbosity=0) #uses spam_penalty_factor

        mdl_single_lsgst_cpsp = pygsti.do_mc2gst(ds, mdl_clgst, self.lsgstStrings[0], minProbClipForWeighting=1e-4,
                                                probClipInterval=(-1e6,1e6), cptp_penalty_factor=1.0,
                                                spam_penalty_factor=1.0, verbosity=0) #uses both penalty factors

        mdl_single_lsgst_cpsp = self.runSilent(pygsti.do_mc2gst, ds, mdl_clgst, self.lsgstStrings[0], minProbClipForWeighting=1e-4,
                                              probClipInterval=(-1e6,1e6), cptp_penalty_factor=1.0,
                                              spam_penalty_factor=1.0, verbosity=10) #uses both penalty factors w/verbosity high
        mdl_single_lsgst_cp = self.runSilent(pygsti.do_mc2gst, ds, mdl_clgst, self.lsgstStrings[0], minProbClipForWeighting=1e-4,
                                            probClipInterval=(-1e6,1e6), cptp_penalty_factor=1.0,
                                            verbosity=10) #uses cptp_penalty_factor w/verbosity high
        mdl_single_lsgst_sp = self.runSilent(pygsti.do_mc2gst, ds, mdl_clgst, self.lsgstStrings[0], minProbClipForWeighting=1e-4,
                                            probClipInterval=(-1e6,1e6), spam_penalty_factor=1.0,
                                            verbosity=10) #uses spam_penalty_factor w/verbosity high


        
        mdl_lsgst = pygsti.do_iterative_mc2gst(ds, mdl_clgst, self.lsgstStrings, verbosity=0,
                                             minProbClipForWeighting=1e-6, probClipInterval=(-1e6,1e6),
                                             memLimit=CM + 1024**3)
        all_minErrs, all_gs_lsgst_tups = pygsti.do_iterative_mc2gst(
            ds, mdl_clgst, [ [mdl.tup for mdl in gsList] for gsList in self.lsgstStrings],
            minProbClipForWeighting=1e-6, probClipInterval=(-1e6,1e6), returnAll=True, returnErrorVec=True)
        mdl_lsgst_verb = self.runSilent(pygsti.do_iterative_mc2gst, ds, mdl_clgst, self.lsgstStrings, verbosity=10,
                                             minProbClipForWeighting=1e-6, probClipInterval=(-1e6,1e6),
                                             memLimit=CM + 1024**3)
        mdl_lsgst_reg = self.runSilent(pygsti.do_iterative_mc2gst,ds, mdl_clgst,
                                      self.lsgstStrings, verbosity=10,
                                      minProbClipForWeighting=1e-6,
                                      probClipInterval=(-1e6,1e6),
                                      regularizeFactor=10, memLimit=CM + 1024**3)
        self.assertAlmostEqual(mdl_lsgst.frobeniusdist(mdl_lsgst_verb),0)
        self.assertAlmostEqual(mdl_lsgst.frobeniusdist(all_gs_lsgst_tups[-1]),0)


        #Run internal checks on less max-L values (so it doesn't take forever)
        mdl_lsgst_chk = pygsti.do_iterative_mc2gst(ds, mdl_clgst, self.lsgstStrings[0:2], verbosity=0,
                                                 minProbClipForWeighting=1e-6, probClipInterval=(-1e6,1e6),
                                                 check=True, check_jacobian=True)
        mdl_lsgst_chk_verb = self.runSilent(pygsti.do_iterative_mc2gst, ds, mdl_clgst, self.lsgstStrings[0:2], verbosity=10,
                                                      minProbClipForWeighting=1e-6, probClipInterval=(-1e6,1e6),
                                                      check=True, check_jacobian=True, memLimit=CM + 1024**3)

        #Other option variations - just make sure they run at this point
        mdl_lsgst_chk_opts = pygsti.do_iterative_mc2gst(ds, mdl_clgst, self.lsgstStrings[0:2], verbosity=0,
                                                      minProbClipForWeighting=1e-6, probClipInterval=(-1e6,1e6),
                                                      useFreqWeightedChiSq=True, circuitSetLabels=["Set1","Set2"],
                                                      circuitWeightsDict={ ('Gx',): 2.0 } )

        aliased_list = [ pygsti.obj.Circuit( [ (x if x != L("Gx") else L("GA1")) for x in mdl]) for mdl in self.lsgstStrings[0] ]
        mdl_withA1 = mdl_clgst.copy(); mdl_withA1.operations["GA1"] = mdl_clgst.operations["Gx"]
        del mdl_withA1.operations["Gx"] # otherwise mdl_withA1 will have Gx params that we have no knowledge of!
        mdl_lsgst_chk_opts2 = pygsti.do_mc2gst(ds, mdl_withA1, aliased_list, minProbClipForWeighting=1e-6,
                                              probClipInterval=(-1e2,1e2), verbosity=10,
                                              opLabelAliases={ L('GA1'): (L('Gx'),) })

        
        #Check with small but ok memlimit -- not anymore since new mem estimation uses current memory, making this non-robust
        #self.runSilent(pygsti.do_mc2gst,ds, mdl_clgst, self.lsgstStrings[0], minProbClipForWeighting=1e-6,
        #                 probClipInterval=(-1e6,1e6), regularizeFactor=1e-3,
        #                 verbosity=10, memLimit=CM + 1024**3)


        #Check errors:
        with self.assertRaises(MemoryError):
            pygsti.do_mc2gst(ds, mdl_clgst, self.lsgstStrings[0], minProbClipForWeighting=1e-6,
                             probClipInterval=(-1e6,1e6), regularizeFactor=1e-3,
                             verbosity=0, memLimit=1)

        with self.assertRaises(AssertionError):
            pygsti.do_mc2gst(ds, mdl_clgst, self.lsgstStrings[0], minProbClipForWeighting=1e-6,
                             probClipInterval=(-1e6,1e6), regularizeFactor=1e-3,
                             verbosity=0, cptp_penalty_factor=1.0) #can't specify both cptp_penalty_factor and regularizeFactor


        # RUN BELOW LINES TO SEED SAVED GATESET FILES
        if os.environ.get('PYGSTI_REGEN_REF_FILES','no').lower() in ("yes","1","true"):
            pygsti.io.write_model(mdl_lsgst,compare_files + "/lsgst.model", "Saved LSGST Model")
            pygsti.io.write_model(mdl_lsgst_reg,compare_files + "/lsgst_reg.model", "Saved LSGST Model w/Regularization")

        mdl_lsgst_compare = pygsti.io.load_model(compare_files + "/lsgst.model")
        mdl_lsgst_reg_compare = pygsti.io.load_model(compare_files + "/lsgst_reg.model")

        mdl_lsgst_go = pygsti.gaugeopt_to_target(mdl_lsgst, mdl_lsgst_compare, {'spam':1.0}, checkJac=True)

        mdl_lsgst_reg_go = pygsti.gaugeopt_to_target(mdl_lsgst_reg, mdl_lsgst_reg_compare, {'spam':1.0}, checkJac=True)

        self.assertAlmostEqual( mdl_lsgst_go.frobeniusdist(mdl_lsgst_compare), 0, places=4)
        self.assertAlmostEqual( mdl_lsgst_reg_go.frobeniusdist(mdl_lsgst_reg_compare), 0, places=4)

        # RUN BELOW LINES TO SEED SAVED GATESET FILES
        if os.environ.get('PYGSTI_REGEN_REF_FILES','no').lower() in ("yes","1","true"):
            mdl_lsgst_go = pygsti.gaugeopt_to_target(mdl_lsgst, self.model, {'spam':1.0})
            pygsti.io.write_model(mdl_lsgst_go,compare_files + "/analysis.model", "Saved LSGST Analysis Model")
            print("DEBUG: analysis.model = "); print(mdl_lgst_go)
Пример #19
0
    def test_eLGST(self):

        ds = self.ds

        assert (pygsti.obj.Model._pcheck)
        mdl_lgst = pygsti.do_lgst(ds,
                                  self.fiducials,
                                  self.fiducials,
                                  self.model,
                                  svdTruncateTo=4,
                                  verbosity=0)
        #mdl_lgst._check_paramvec() #will fail, but OK, since paramvec is computed only when *needed* now
        mdl_lgst_go = pygsti.gaugeopt_to_target(mdl_lgst,
                                                self.model, {
                                                    'spam': 1.0,
                                                    'gates': 1.0
                                                },
                                                checkJac=True)
        mdl_lgst_go._check_paramvec()
        mdl_clgst = pygsti.contract(mdl_lgst_go, "CPTP")
        mdl_clgst.to_vector()  # to make sure we're in sync
        mdl_clgst._check_paramvec()
        self.model._check_paramvec()

        _, mdl_single_exlgst = pygsti.do_exlgst(ds,
                                                mdl_clgst,
                                                self.elgstStrings[0],
                                                self.fiducials,
                                                self.fiducials,
                                                self.model,
                                                regularizeFactor=1e-3,
                                                svdTruncateTo=4,
                                                verbosity=0)
        mdl_single_exlgst._check_paramvec()

        _, mdl_single_exlgst_verb = self.runSilent(pygsti.do_exlgst,
                                                   ds,
                                                   mdl_clgst,
                                                   self.elgstStrings[0],
                                                   self.fiducials,
                                                   self.fiducials,
                                                   self.model,
                                                   regularizeFactor=1e-3,
                                                   svdTruncateTo=4,
                                                   verbosity=10)
        mdl_single_exlgst_verb._check_paramvec()

        self.assertAlmostEqual(
            mdl_single_exlgst.frobeniusdist(mdl_single_exlgst_verb), 0)

        mdl_exlgst = pygsti.do_iterative_exlgst(ds,
                                                mdl_clgst,
                                                self.fiducials,
                                                self.fiducials,
                                                self.elgstStrings,
                                                targetModel=self.model,
                                                svdTruncateTo=4,
                                                verbosity=0)

        all_minErrs, all_gs_exlgst_tups = pygsti.do_iterative_exlgst(
            ds,
            mdl_clgst,
            self.fiducials,
            self.fiducials,
            [[mdl.tup for mdl in gsList] for gsList in self.elgstStrings],
            targetModel=self.model,
            svdTruncateTo=4,
            verbosity=0,
            returnAll=True,
            returnErrorVec=True)

        mdl_exlgst_verb = self.runSilent(pygsti.do_iterative_exlgst,
                                         ds,
                                         mdl_clgst,
                                         self.fiducials,
                                         self.fiducials,
                                         self.elgstStrings,
                                         targetModel=self.model,
                                         svdTruncateTo=4,
                                         verbosity=10)
        mdl_exlgst_reg = pygsti.do_iterative_exlgst(ds,
                                                    mdl_clgst,
                                                    self.fiducials,
                                                    self.fiducials,
                                                    self.elgstStrings,
                                                    targetModel=self.model,
                                                    svdTruncateTo=4,
                                                    verbosity=0,
                                                    regularizeFactor=10)
        self.assertAlmostEqual(mdl_exlgst.frobeniusdist(mdl_exlgst_verb), 0)
        self.assertAlmostEqual(
            mdl_exlgst.frobeniusdist(all_gs_exlgst_tups[-1]), 0)

        #Run internal checks on less max-L values (so it doesn't take forever)
        mdl_exlgst_chk = pygsti.do_iterative_exlgst(ds,
                                                    mdl_clgst,
                                                    self.fiducials,
                                                    self.fiducials,
                                                    self.elgstStrings[0:2],
                                                    targetModel=self.model,
                                                    svdTruncateTo=4,
                                                    verbosity=0,
                                                    check_jacobian=True)
        mdl_exlgst_chk_verb = self.runSilent(pygsti.do_iterative_exlgst,
                                             ds,
                                             mdl_clgst,
                                             self.fiducials,
                                             self.fiducials,
                                             self.elgstStrings[0:2],
                                             targetModel=self.model,
                                             svdTruncateTo=4,
                                             verbosity=10,
                                             check_jacobian=True)

        # RUN BELOW LINES TO SEED SAVED GATESET FILES
        if regenerate_references():
            pygsti.io.write_model(mdl_exlgst, compare_files + "/exlgst.model",
                                  "Saved Extended-LGST (eLGST) Model")
            pygsti.io.write_model(
                mdl_exlgst_reg, compare_files + "/exlgst_reg.model",
                "Saved Extended-LGST (eLGST) Model w/regularization")

        mdl_exlgst_compare = pygsti.io.load_model(compare_files +
                                                  "/exlgst.model")
        mdl_exlgst_reg_compare = pygsti.io.load_model(compare_files +
                                                      "/exlgst_reg.model")
        mdl_exlgst.set_all_parameterizations(
            "full"
        )  # b/c ex-LGST sets spam to StaticSPAMVec objects (b/c they're not optimized)
        mdl_exlgst_reg.set_all_parameterizations(
            "full"
        )  # b/c ex-LGST sets spam to StaticSPAMVec objects (b/c they're not optimized)
        mdl_exlgst_go = pygsti.gaugeopt_to_target(mdl_exlgst,
                                                  mdl_exlgst_compare,
                                                  {'spam': 1.0},
                                                  checkJac=True)
        mdl_exlgst_reg_go = pygsti.gaugeopt_to_target(mdl_exlgst_reg,
                                                      mdl_exlgst_reg_compare,
                                                      {'spam': 1.0},
                                                      checkJac=True)
Пример #20
0
    def test_LGST(self):

        ds = self.ds
        #pygsti.construction.generate_fake_data(self.datagen_gateset, self.lgstStrings, nSamples=1000,
        #                                            sampleError='binomial', seed=None)

        print("GG0 = ",self.model.default_gauge_group)
        mdl_lgst = pygsti.do_lgst(ds, self.fiducials, self.fiducials, self.model, svdTruncateTo=4, verbosity=0)
        mdl_lgst_verb = self.runSilent(pygsti.do_lgst, ds, self.fiducials, self.fiducials, self.model, svdTruncateTo=4, verbosity=10)
        self.assertAlmostEqual(mdl_lgst.frobeniusdist(mdl_lgst_verb),0)

        print("GG = ",mdl_lgst.default_gauge_group)
        mdl_lgst_go = pygsti.gaugeopt_to_target(mdl_lgst,self.model, {'spam':1.0, 'gates': 1.0}, checkJac=True)
        mdl_clgst = pygsti.contract(mdl_lgst_go, "CPTP")

        # RUN BELOW LINES TO SEED SAVED GATESET FILES
        if os.environ.get('PYGSTI_REGEN_REF_FILES','no').lower() in ("yes","1","true"):
            pygsti.io.write_model(mdl_lgst,compare_files + "/lgst.model", "Saved LGST Model before gauge optimization")
            pygsti.io.write_model(mdl_lgst_go,compare_files + "/lgst_go.model", "Saved LGST Model after gauge optimization")
            pygsti.io.write_model(mdl_clgst,compare_files + "/clgst.model", "Saved LGST Model after G.O. and CPTP contraction")

        mdl_lgst_compare = pygsti.io.load_model(compare_files + "/lgst.model")
        mdl_lgst_go_compare = pygsti.io.load_model(compare_files + "/lgst_go.model")
        mdl_clgst_compare = pygsti.io.load_model(compare_files + "/clgst.model")

        self.assertAlmostEqual( mdl_lgst.frobeniusdist(mdl_lgst_compare), 0, places=5)
        self.assertAlmostEqual( mdl_lgst_go.frobeniusdist(mdl_lgst_go_compare), 0, places=5)
        self.assertAlmostEqual( mdl_clgst.frobeniusdist(mdl_clgst_compare), 0, places=5)

        #Check for error conditions
        with self.assertRaises(ValueError):
            mdl_lgst = pygsti.do_lgst(ds, self.fiducials, self.fiducials, None, svdTruncateTo=4, verbosity=0) #no target model

        with self.assertRaises(ValueError):
            mdl_lgst = pygsti.do_lgst(ds, self.fiducials, self.fiducials, None, opLabels=list(self.model.operations.keys()),
                                     svdTruncateTo=4, verbosity=0) #no spam dict

        #No need for identity vector anymore
        #with self.assertRaises(ValueError):
        #    mdl_lgst = pygsti.do_lgst(ds, self.fiducials, self.fiducials, None, opLabels=list(self.model.operations.keys()),
        #                             spamDict=self.model.get_reverse_spam_defs(),
        #                             svdTruncateTo=4, verbosity=0) #no identity vector

        with self.assertRaises(ValueError):
            bad_fids =pygsti.construction.circuit_list([('Gx',),('Gx',),('Gx',),('Gx',)])
            mdl_lgst = pygsti.do_lgst(ds, bad_fids, bad_fids, self.model, svdTruncateTo=4, verbosity=0) # bad fiducials (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')
            mdl_lgst = pygsti.do_lgst(bad_ds, self.fiducials, self.fiducials, self.model,
                                     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')
            mdl_lgst = pygsti.do_lgst(bad_ds, self.fiducials, self.fiducials, self.model,
                                     svdTruncateTo=4, verbosity=0)
Пример #21
0
    def test_MC2GST(self):

        ds = self.ds

        mdl_lgst = pygsti.do_lgst(ds,
                                  self.fiducials,
                                  self.fiducials,
                                  self.model,
                                  svdTruncateTo=4,
                                  verbosity=0)
        mdl_lgst_go = pygsti.gaugeopt_to_target(mdl_lgst,
                                                self.model, {
                                                    'spam': 1.0,
                                                    'gates': 1.0
                                                },
                                                checkJac=True)
        mdl_clgst = pygsti.contract(mdl_lgst_go, "CPTP")
        CM = profiler._get_mem_usage()

        mdl_lsgst = pygsti.do_iterative_mc2gst(ds,
                                               mdl_clgst,
                                               self.lsgstStrings,
                                               verbosity=0,
                                               minProbClipForWeighting=1e-6,
                                               probClipInterval=(-1e6, 1e6),
                                               memLimit=CM + 1024**3)
        all_minErrs, all_gs_lsgst_tups = pygsti.do_iterative_mc2gst(
            ds,
            mdl_clgst,
            [[mdl.tup for mdl in gsList] for gsList in self.lsgstStrings],
            minProbClipForWeighting=1e-6,
            probClipInterval=(-1e6, 1e6),
            returnAll=True,
            returnErrorVec=True)
        mdl_lsgst_verb = self.runSilent(pygsti.do_iterative_mc2gst,
                                        ds,
                                        mdl_clgst,
                                        self.lsgstStrings,
                                        verbosity=10,
                                        minProbClipForWeighting=1e-6,
                                        probClipInterval=(-1e6, 1e6),
                                        memLimit=CM + 1024**3)
        mdl_lsgst_reg = self.runSilent(pygsti.do_iterative_mc2gst,
                                       ds,
                                       mdl_clgst,
                                       self.lsgstStrings,
                                       verbosity=10,
                                       minProbClipForWeighting=1e-6,
                                       probClipInterval=(-1e6, 1e6),
                                       regularizeFactor=10,
                                       memLimit=CM + 1024**3)
        self.assertAlmostEqual(mdl_lsgst.frobeniusdist(mdl_lsgst_verb), 0)
        self.assertAlmostEqual(mdl_lsgst.frobeniusdist(all_gs_lsgst_tups[-1]),
                               0)

        # RUN BELOW LINES TO SEED SAVED GATESET FILES
        if regenerate_references():
            pygsti.io.write_model(mdl_lsgst, compare_files + "/lsgst.model",
                                  "Saved LSGST Model")
            pygsti.io.write_model(mdl_lsgst_reg,
                                  compare_files + "/lsgst_reg.model",
                                  "Saved LSGST Model w/Regularization")

        mdl_lsgst_compare = pygsti.io.load_model(compare_files +
                                                 "/lsgst.model")
        mdl_lsgst_reg_compare = pygsti.io.load_model(compare_files +
                                                     "/lsgst_reg.model")

        mdl_lsgst_go = pygsti.gaugeopt_to_target(mdl_lsgst,
                                                 mdl_lsgst_compare,
                                                 {'spam': 1.0},
                                                 checkJac=True)

        mdl_lsgst_reg_go = pygsti.gaugeopt_to_target(mdl_lsgst_reg,
                                                     mdl_lsgst_reg_compare,
                                                     {'spam': 1.0},
                                                     checkJac=True)

        self.assertAlmostEqual(mdl_lsgst_go.frobeniusdist(mdl_lsgst_compare),
                               0,
                               places=4)
        self.assertAlmostEqual(
            mdl_lsgst_reg_go.frobeniusdist(mdl_lsgst_reg_compare), 0, places=4)

        # RUN BELOW LINES TO SEED SAVED GATESET FILES
        if regenerate_references():
            mdl_lsgst_go = pygsti.gaugeopt_to_target(mdl_lsgst, self.model,
                                                     {'spam': 1.0})
            pygsti.io.write_model(mdl_lsgst_go,
                                  compare_files + "/analysis.model",
                                  "Saved LSGST Analysis Model")
            print("DEBUG: analysis.model = ")
            print(mdl_lgst_go)
Пример #22
0
    def test_MLGST(self):

        ds = self.ds
        #pygsti.construction.generate_fake_data(self.datagen_gateset, self.lsgstStrings[-1],
        #                                            nSamples=1000, sampleError='binomial', seed=100)

        mdl_lgst = pygsti.do_lgst(ds, self.fiducials, self.fiducials, self.model, svdTruncateTo=4, verbosity=0)
        mdl_lgst_go = pygsti.gaugeopt_to_target(mdl_lgst,self.model, {'spam':1.0, 'gates': 1.0}, checkJac=True)
        mdl_clgst = pygsti.contract(mdl_lgst_go, "CPTP") 
        mdl_clgst = mdl_clgst.depolarize(op_noise=0.02, spam_noise=0.02) # just to avoid infinity objective funct & jacs below
        CM = pygsti.baseobjs.profiler._get_mem_usage()

        mdl_single_mlgst = pygsti.do_mlgst(ds, mdl_clgst, self.lsgstStrings[0], minProbClip=1e-4,
                                          probClipInterval=(-1e2,1e2), verbosity=0)

        #this test often gives an assetion error "finite Jacobian has inf norm!" on Travis CI Python 3 case
        try:
            mdl_single_mlgst_cpsp = pygsti.do_mlgst(ds, mdl_clgst, self.lsgstStrings[0], minProbClip=1e-4,
                                                  probClipInterval=(-1e2,1e2), cptp_penalty_factor=1.0,
                                                  spam_penalty_factor=1.0, verbosity=10) #uses both penalty factors w/verbosity > 0
        except ValueError: pass # ignore when assertions in customlm.py are disabled
        except AssertionError:
            pass # just ignore for now.  FUTURE: see what we can do in custom LM about scaling large jacobians...
        
        try:
            mdl_single_mlgst_cp = pygsti.do_mlgst(ds, mdl_clgst, self.lsgstStrings[0], minProbClip=1e-4,
                                                  probClipInterval=(-1e2,1e2), cptp_penalty_factor=1.0,
                                                  verbosity=10)
        except ValueError: pass # ignore when assertions in customlm.py are disabled
        except AssertionError:
            pass # just ignore for now.  FUTURE: see what we can do in custom LM about scaling large jacobians...
        
        try:
            mdl_single_mlgst_sp = pygsti.do_mlgst(ds, mdl_clgst, self.lsgstStrings[0], minProbClip=1e-4,
                                                  probClipInterval=(-1e2,1e2), spam_penalty_factor=1.0,
                                                  verbosity=10)
        except ValueError: pass # ignore when assertions in customlm.py are disabled
        except AssertionError:
            pass # just ignore for now.  FUTURE: see what we can do in custom LM about scaling large jacobians...
            

        mdl_mlegst = pygsti.do_iterative_mlgst(ds, mdl_clgst, self.lsgstStrings, verbosity=0,
                                               minProbClip=1e-4, probClipInterval=(-1e2,1e2),
                                               memLimit=CM + 1024**3)
        maxLogL, all_gs_mlegst_tups = pygsti.do_iterative_mlgst(
            ds, mdl_clgst, [ [mdl.tup for mdl in gsList] for gsList in self.lsgstStrings],
            minProbClip=1e-4, probClipInterval=(-1e2,1e2), returnAll=True, returnMaxLogL=True)

        mdl_mlegst_verb = self.runSilent(pygsti.do_iterative_mlgst, ds, mdl_clgst, self.lsgstStrings, verbosity=10,
                                             minProbClip=1e-4, probClipInterval=(-1e2,1e2),
                                             memLimit=CM + 1024**3)
        self.assertAlmostEqual(mdl_mlegst.frobeniusdist(mdl_mlegst_verb),0, places=5)
        self.assertAlmostEqual(mdl_mlegst.frobeniusdist(all_gs_mlegst_tups[-1]),0,places=5)


        #Run internal checks on less max-L values (so it doesn't take forever)
        mdl_mlegst_chk = pygsti.do_iterative_mlgst(ds, mdl_clgst, self.lsgstStrings[0:2], verbosity=0,
                                                 minProbClip=1e-4, probClipInterval=(-1e2,1e2),
                                                 check=True)

        #Other option variations - just make sure they run at this point
        mdl_mlegst_chk_opts = pygsti.do_iterative_mlgst(ds, mdl_clgst, self.lsgstStrings[0:2], verbosity=0,
                                                       minProbClip=1e-4, probClipInterval=(-1e2,1e2),
                                                       circuitSetLabels=["Set1","Set2"], useFreqWeightedChiSq=True,
                                                       circuitWeightsDict={ (L('Gx'),): 2.0 } )

        aliased_list = [ pygsti.obj.Circuit( [ (x if x != L("Gx") else L("GA1")) for x in mdl]) for mdl in self.lsgstStrings[0] ]
        mdl_withA1 = mdl_clgst.copy(); mdl_withA1.operations["GA1"] = mdl_clgst.operations["Gx"]
        del mdl_withA1.operations["Gx"] # otherwise mdl_withA1 will have Gx params that we have no knowledge of!
        mdl_mlegst_chk_opts2 = pygsti.do_mlgst(ds, mdl_withA1, aliased_list, minProbClip=1e-4,
                                              probClipInterval=(-1e2,1e2), verbosity=10,
                                              opLabelAliases={ L('GA1'): (L('Gx'),) })

        #Other option variations - just make sure they run at this point
        mdl_mlegst_chk_opts3 = pygsti.do_iterative_mlgst(ds, mdl_clgst, self.lsgstStrings[0:2], verbosity=0,
                                                       minProbClip=1e-4, probClipInterval=(-1e2,1e2),
                                                       circuitSetLabels=["Set1","Set2"], useFreqWeightedChiSq=True,
                                                        circuitWeightsDict={ (L('Gx'),): 2.0 }, alwaysPerformMLE=True )

        #Forcing function used by linear response error bars
        forcingfn_grad = np.ones((1,mdl_clgst.num_params()), 'd')
        mdl_lsgst_chk_opts3 = pygsti.algorithms.core._do_mlgst_base(
            ds, mdl_clgst, self.lsgstStrings[0], verbosity=0,
            minProbClip=1e-4, probClipInterval=(-1e2,1e2),
            forcefn_grad=forcingfn_grad)
        with self.assertRaises(NotImplementedError):
            # Non-poisson picture needs support for a non-leastsq solver (not impl yet)
            mdl_lsgst_chk_opts4 = pygsti.algorithms.core._do_mlgst_base(
                ds, mdl_clgst, self.lsgstStrings[0], verbosity=0, poissonPicture=False, 
                minProbClip=1e-4, probClipInterval=(-1e2,1e2),
                forcefn_grad=forcingfn_grad) # non-poisson picture

        #Check with small but ok memlimit -- not anymore since new mem estimation uses current memory, making this non-robust
        #self.runSilent(pygsti.do_mlgst, ds, mdl_clgst, self.lsgstStrings[0], minProbClip=1e-6,
        #                probClipInterval=(-1e2,1e2), verbosity=4, memLimit=curMem+8500000) #invoke memory control

        #non-Poisson picture - should use (-1,-1) model for consistency?
        with self.assertRaises(NotImplementedError):
            # Non-poisson picture needs support for a non-leastsq solver (not impl yet)
            pygsti.do_mlgst(ds, mdl_clgst, self.lsgstStrings[0], minProbClip=1e-4,
                            probClipInterval=(-1e2,1e2), verbosity=0, poissonPicture=False)
            try:
                pygsti.do_mlgst(ds, mdl_clgst, self.lsgstStrings[0], minProbClip=1e-1, # 1e-1 b/c get inf Jacobians...
                                probClipInterval=(-1e2,1e2), verbosity=0, poissonPicture=False,
                                spam_penalty_factor=1.0, cptp_penalty_factor=1.0)
            except ValueError: pass # ignore when assertions in customlm.py are disabled
            except AssertionError:
                pass # just ignore for now.  FUTURE: see what we can do in custom LM about scaling large jacobians...



        #Check errors:
        with self.assertRaises(MemoryError):
            pygsti.do_mlgst(ds, mdl_clgst, self.lsgstStrings[0], minProbClip=1e-4,
                            probClipInterval=(-1e2,1e2),verbosity=0, memLimit=1)


        # RUN BELOW LINES TO SEED SAVED GATESET FILES
        if os.environ.get('PYGSTI_REGEN_REF_FILES','no').lower() in ("yes","1","true"):
            pygsti.io.write_model(mdl_mlegst,compare_files + "/mle_gst.model", "Saved MLE-GST Model")

        mdl_mle_compare = pygsti.io.load_model(compare_files + "/mle_gst.model")
        mdl_mlegst_go = pygsti.gaugeopt_to_target(mdl_mlegst, mdl_mle_compare, {'spam':1.0}, checkJac=True)

        self.assertAlmostEqual( mdl_mlegst_go.frobeniusdist(mdl_mle_compare), 0, places=4)
Пример #23
0
    def setUpClass(cls):
        """
        Handle all once-per-class (slow) computation and loading,
         to avoid calling it for each test (like setUp).  Store
         results in class variable for use within setUp.
        """
        super(ReportBaseCase, cls).setUpClass()

        orig_cwd = os.getcwd()
        os.chdir(os.path.abspath(os.path.dirname(__file__)))
        os.chdir('..') # The test_packages directory

        target_model = std.target_model()
        datagen_gateset = target_model.depolarize(op_noise=0.05, spam_noise=0.1)
        datagen_gateset2 = target_model.depolarize(op_noise=0.1, spam_noise=0.05).rotate((0.15,-0.03,0.03))

        #cls.specs = pygsti.construction.build_spam_specs(std.fiducials, effect_labels=['E0'])
        #  #only use the first EVec

        op_labels = std.gates
        cls.lgstStrings = pygsti.circuits.create_lgst_circuits(std.fiducials, std.fiducials, op_labels)
        cls.maxLengthList = [1,2,4,8]

        cls.lsgstStrings = pygsti.circuits.create_lsgst_circuit_lists(
            op_labels, std.fiducials, std.fiducials, std.germs, cls.maxLengthList)
        cls.lsgstStructs = pygsti.circuits.make_lsgst_structs(
            op_labels, std.fiducials, std.fiducials, std.germs, cls.maxLengthList)


        # RUN BELOW LINES TO GENERATE ANALYSIS DATASET (SAVE)
        if regenerate_references():
            ds = pygsti.data.simulate_data(datagen_gateset, cls.lsgstStrings[-1], num_samples=1000,
                                                   sample_error='binomial', seed=100)
            ds.save(compare_files + "/reportgen.dataset")
            ds2 = pygsti.data.simulate_data(datagen_gateset2, cls.lsgstStrings[-1], num_samples=1000,
                                                    sample_error='binomial', seed=100)
            ds2.save(compare_files + "/reportgen2.dataset")


        cls.ds = pygsti.data.DataSet(file_to_load_from=compare_files + "/reportgen.dataset")
        cls.ds2 = pygsti.data.DataSet(file_to_load_from=compare_files + "/reportgen2.dataset")

        mdl_lgst = pygsti.run_lgst(cls.ds, std.fiducials, std.fiducials, target_model, svd_truncate_to=4, verbosity=0)
        mdl_lgst_go = pygsti.gaugeopt_to_target(mdl_lgst, target_model, {'gates': 1.0, 'spam': 0.0})
        cls.mdl_clgst = pygsti.contract(mdl_lgst_go, "CPTP")
        cls.mdl_clgst_tp = pygsti.contract(cls.mdl_clgst, "vSPAM")
        cls.mdl_clgst_tp.set_all_parameterizations("full TP")

        #Compute results for MC2GST
        lsgst_gatesets_prego, *_ = pygsti.run_iterative_gst(
            cls.ds, cls.mdl_clgst, cls.lsgstStrings,
            optimizer={'tol': 1e-5},
            iteration_objfn_builders=['chi2'],
            final_objfn_builders=[],
            resource_alloc=None,
            verbosity=0
        )

        experiment_design = pygsti.protocols.StandardGSTDesign(
            target_model.create_processor_spec(), std.fiducials, std.fiducials, std.germs, cls.maxLengthList
        )
        data = pygsti.protocols.ProtocolData(experiment_design, cls.ds)
        protocol = pygsti.protocols.StandardGST()
        cls.results = pygsti.protocols.gst.ModelEstimateResults(data, protocol)
        cls.results.add_estimate(pygsti.protocols.estimate.Estimate.create_gst_estimate(
            cls.results, target_model, cls.mdl_clgst,lsgst_gatesets_prego,
            {'objective': "chi2",
             'min_prob_clip_for_weighting': 1e-4,
             'prob_clip_interval': (-1e6,1e6), 'radius': 1e-4,
             'weights': None, 'defaultDirectory': temp_files + "",
             'defaultBasename': "MyDefaultReportName"}
        ))

        gaugeOptParams = collections.OrderedDict([
                ('model', lsgst_gatesets_prego[-1]),  #so can gauge-propagate CIs
                ('target_model', target_model),       #so can gauge-propagate CIs
                ('cptp_penalty_factor', 0),
                ('gates_metric',"frobenius"),
                ('spam_metric',"frobenius"),
                ('item_weights', {'gates': 1.0, 'spam': 0.001}),
                ('return_all', True) ])

        _, gaugeEl, go_final_gateset = pygsti.gaugeopt_to_target(**gaugeOptParams)
        gaugeOptParams['_gaugeGroupEl'] = gaugeEl  #so can gauge-propagate CIs
        cls.results.estimates['default'].add_gaugeoptimized(gaugeOptParams, go_final_gateset)
        cls.results.estimates['default'].add_gaugeoptimized(gaugeOptParams, go_final_gateset, "go_dup")

        #Compute results for MLGST with TP constraint
        # Use run_long_sequence_gst with a non-mark dataset to trigger data scaling
        tp_target = target_model.copy(); tp_target.set_all_parameterizations("full TP")


        cls.ds3 = cls.ds.copy_nonstatic()
        cls.ds3.add_counts_from_dataset(cls.ds2)
        cls.ds3.done_adding_data()

        cls.results_logL = pygsti.run_long_sequence_gst(cls.ds3, tp_target, std.fiducials, std.fiducials,
                                                        std.germs, cls.maxLengthList, verbosity=0,
                                                        advanced_options={'tolerance': 1e-6, 'starting_point': 'LGST',
                                                                        'on_bad_fit': ["robust","Robust","robust+","Robust+"],
                                                                        'bad_fit_threshold': -1.0,
                                                                        'germ_length_limits': {('Gx','Gi','Gi'): 2} })

        #OLD
        #lsgst_gatesets_TP = pygsti.do_iterative_mlgst(cls.ds, cls.mdl_clgst_tp, cls.lsgstStrings, verbosity=0,
        #                                           min_prob_clip=1e-4, prob_clip_interval=(-1e6,1e6),
        #                                           returnAll=True) #TP initial model => TP output models
        #cls.results_logL = pygsti.objects.Results()
        #cls.results_logL.init_dataset(cls.ds)
        #cls.results_logL.init_circuits(cls.lsgstStructs)
        #cls.results_logL.add_estimate(target_model, cls.mdl_clgst_tp,
        #                         lsgst_gatesets_TP,
        #                         {'objective': "logl",
        #                          'min_prob_clip': 1e-4,
        #                          'prob_clip_interval': (-1e6,1e6), 'radius': 1e-4,
        #                          'weights': None, 'defaultDirectory': temp_files + "",
        #                          'defaultBasename': "MyDefaultReportName"})
        #
        #tp_target = target_model.copy(); tp_target.set_all_parameterizations("full TP")
        #gaugeOptParams = gaugeOptParams.copy() #just to be safe
        #gaugeOptParams['model'] = lsgst_gatesets_TP[-1]  #so can gauge-propagate CIs
        #gaugeOptParams['target_model'] = tp_target  #so can gauge-propagate CIs
        #_, gaugeEl, go_final_gateset = pygsti.gaugeopt_to_target(**gaugeOptParams)
        #gaugeOptParams['_gaugeGroupEl'] = gaugeEl #so can gauge-propagate CIs
        #cls.results_logL.estimates['default'].add_gaugeoptimized(gaugeOptParams, go_final_gateset)
        #
        ##self.results_logL.options.precision = 3
        ##self.results_logL.options.polar_precision = 2

        os.chdir(orig_cwd)
Пример #24
0
    def test_table_generation(self):
        import pygsti.report.generation as gen
        formats = ['latex', 'html', 'py', 'ppt']  #all the formats we know
        tableclass = "cssClass"
        longtable = False
        confidenceRegionInfo = None

        gateset = pygsti.io.load_gateset("cmp_chk_files/analysis.gateset")
        ds = pygsti.objects.DataSet(
            fileToLoadFrom="cmp_chk_files/analysis.dataset")

        chi2, chi2Hessian = pygsti.chi2(ds, gateset, returnHessian=True)
        ci = pygsti.obj.ConfidenceRegion(gateset,
                                         chi2Hessian,
                                         95.0,
                                         hessianProjection="std")
        gateset_tp = pygsti.contract(gateset, "TP")
        gateset_tp.set_all_parameterizations("TP")
        chi2, chi2Hessian_TP = pygsti.chi2(ds, gateset_tp, returnHessian=True)
        ci_TP = pygsti.obj.ConfidenceRegion(gateset_tp,
                                            chi2Hessian_TP,
                                            95.0,
                                            hessianProjection="std")

        chi2, chi2Hessian_tgt = pygsti.chi2(ds,
                                            std.gs_target,
                                            returnHessian=True)
        ci_tgt = pygsti.obj.ConfidenceRegion(std.gs_target,
                                             chi2Hessian_tgt,
                                             95.0,
                                             hessianProjection="std")
        target_tp = std.gs_target.copy()
        target_tp.set_all_parameterizations("TP")
        chi2, chi2Hessian_tgt_TP = pygsti.chi2(ds,
                                               target_tp,
                                               returnHessian=True)
        ci_TP_tgt = pygsti.obj.ConfidenceRegion(target_tp,
                                                chi2Hessian_tgt_TP,
                                                95.0,
                                                hessianProjection="std")

        gateset_2q = pygsti.construction.build_gateset(
            [4], [('Q0', 'Q1')], ['GIX', 'GIY', 'GXI', 'GYI', 'GCNOT'], [
                "I(Q0):X(pi/2,Q1)", "I(Q0):Y(pi/2,Q1)", "X(pi/2,Q0):I(Q1)",
                "Y(pi/2,Q0):I(Q1)", "CX(pi,Q0,Q1)"
            ],
            prepLabels=['rho0'],
            prepExpressions=["0"],
            effectLabels=['E0', 'E1', 'E2'],
            effectExpressions=["0", "1", "2"],
            spamdefs={
                'upup': ('rho0', 'E0'),
                'updn': ('rho0', 'E1'),
                'dnup': ('rho0', 'E2'),
                'dndn': ('rho0', 'remainder')
            },
            basis="pp")

        #tests which fill in the cracks of the full-report tests
        tab = gen.get_gateset_spam_table(gateset, None)
        tab_wCI = gen.get_gateset_spam_table(gateset, ci)
        gen.get_gateset_spam_table(gateset, None)
        gen.get_gateset_gates_table(gateset_tp, ci_TP)  #test zero-padding
        gen.get_unitary_gateset_gates_table(std.gs_target,
                                            ci_tgt)  #unitary gates w/CIs
        gen.get_unitary_gateset_gates_table(target_tp,
                                            ci_TP_tgt)  #unitary gates w/CIs
        gen.get_gateset_closest_unitary_table(gateset_2q,
                                              None)  #test higher-dim gateset
        gen.get_gateset_closest_unitary_table(gateset,
                                              ci)  #test with CIs (long...)
        gen.get_gateset_rotn_axis_table(
            std.gs_target, None)  #try to get "--"s and "X"s to display
        gen.get_chi2_progress_table([0], [gateset_tp], [
            [('Gx', )],
        ], ds)  #TP case
        gen.get_chi2_confidence_region(gateset_tp, ds, 95)  #TP case

        gen.get_gatestring_multi_table(
            [[('Gx', ), ('Gz', )], [('Gy', )]], ["list1", "list2"],
            commonTitle=None)  #commonTitle == None case w/diff length lists

        with self.assertRaises(ValueError):
            gen.get_unitary_gateset_gates_table(std.gs_target,
                                                ci)  #gateset-CI mismatch
        with self.assertRaises(ValueError):
            gen.get_gateset_spam_parameters_table(std.gs_target,
                                                  ci)  #gateset-CI mismatch

        #Test ReportTable object
        rowLabels = tab.keys()
        row1Data = tab[rowLabels[0]]
        colLabels = row1Data.keys()

        self.assertTrue(rowLabels, tab.row_names)
        self.assertTrue(colLabels, tab.col_names)
        self.assertTrue(len(rowLabels), tab.num_rows)
        self.assertTrue(len(colLabels), tab.num_cols)

        el00 = tab[rowLabels[0]][colLabels[0]]
        self.assertTrue(rowLabels[0] in tab)
        self.assertTrue(tab.has_key(rowLabels[0]))

        table_len = len(tab)
        self.assertEqual(table_len, tab.num_rows)

        table_as_str = str(tab)
        table_wCI_as_str = str(tab_wCI)
        row1a = tab.row(key=rowLabels[0])
        col1a = tab.col(key=colLabels[0])
        row1b = tab.row(index=0)
        col1b = tab.col(index=0)
        self.assertEqual(row1a, row1b)
        self.assertEqual(col1a, col1b)

        with self.assertRaises(KeyError):
            tab['foobar']
        with self.assertRaises(KeyError):
            tab.row(key='foobar')  #invalid key
        with self.assertRaises(ValueError):
            tab.row(index=100000)  #out of bounds
        with self.assertRaises(ValueError):
            tab.row()  #must specify key or index
        with self.assertRaises(ValueError):
            tab.row(key='foobar', index=1)  #cannot specify key and index
        with self.assertRaises(KeyError):
            tab.col(key='foobar')  #invalid key
        with self.assertRaises(ValueError):
            tab.col(index=100000)  #out of bounds
        with self.assertRaises(ValueError):
            tab.col()  #must specify key or index
        with self.assertRaises(ValueError):
            tab.col(key='foobar', index=1)  #cannot specify key and index

        with self.assertRaises(ValueError):
            tab.render(fmt="foobar")  #invalid format

        #LogL case tests
        gen.get_logl_progress_table([0], [gateset_tp], [
            [('Gx', )],
        ], ds)  # logL case
        gen.get_logl_progress_table([0], [gateset], [
            [('Gx', )],
        ], ds)  # logL case

        gen.get_logl_confidence_region(gateset_tp,
                                       ds,
                                       95,
                                       gatestring_list=None,
                                       probClipInterval=(-1e6, 1e6),
                                       minProbClip=1e-4,
                                       radius=1e-4,
                                       hessianProjection="std")
        gen.get_logl_confidence_region(gateset,
                                       ds,
                                       95,
                                       gatestring_list=None,
                                       probClipInterval=(-1e6, 1e6),
                                       minProbClip=1e-4,
                                       radius=1e-4,
                                       hessianProjection="std")
Пример #25
0
    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)
Пример #26
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)
Пример #27
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
Пример #28
0
    def test_table_generation(self):
        import pygsti.report.generation as gen
        formats = ['latex','html','py','ppt'] #all the formats we know
        tableclass = "cssClass"
        longtable = False
        confidenceRegionInfo = None

        gateset = pygsti.io.load_gateset("cmp_chk_files/analysis.gateset")
        ds = pygsti.objects.DataSet(fileToLoadFrom="cmp_chk_files/analysis.dataset")

        chi2, chi2Hessian = pygsti.chi2(ds, gateset, returnHessian=True)
        ci = pygsti.obj.ConfidenceRegion(gateset, chi2Hessian, 95.0,
                                         hessianProjection="std")
        gateset_tp = pygsti.contract(gateset,"TP"); gateset_tp.set_all_parameterizations("TP")
        chi2, chi2Hessian_TP = pygsti.chi2(ds, gateset_tp, returnHessian=True)
        ci_TP = pygsti.obj.ConfidenceRegion(gateset_tp, chi2Hessian_TP, 95.0,
                                            hessianProjection="std")

        chi2, chi2Hessian_tgt = pygsti.chi2(ds, std.gs_target, returnHessian=True)
        ci_tgt = pygsti.obj.ConfidenceRegion(std.gs_target, chi2Hessian_tgt, 95.0,
                                         hessianProjection="std")
        target_tp = std.gs_target.copy(); target_tp.set_all_parameterizations("TP")
        chi2, chi2Hessian_tgt_TP = pygsti.chi2(ds, target_tp, returnHessian=True)
        ci_TP_tgt = pygsti.obj.ConfidenceRegion(target_tp, chi2Hessian_tgt_TP, 95.0,
                                            hessianProjection="std")

        gateset_2q = pygsti.construction.build_gateset( 
            [4], [('Q0','Q1')],['GIX','GIY','GXI','GYI','GCNOT'], 
            [ "I(Q0):X(pi/2,Q1)", "I(Q0):Y(pi/2,Q1)", "X(pi/2,Q0):I(Q1)", "Y(pi/2,Q0):I(Q1)", "CX(pi,Q0,Q1)" ],
            prepLabels=['rho0'], prepExpressions=["0"], effectLabels=['E0','E1','E2'], effectExpressions=["0","1","2"], 
            spamdefs={'upup': ('rho0','E0'), 'updn': ('rho0','E1'), 'dnup': ('rho0','E2'),
                           'dndn': ('rho0','remainder') }, basis="pp" )
        

        #tests which fill in the cracks of the full-report tests
        with self.assertRaises(ValueError):
            gen.get_gateset_spam_table(gateset, formats, tableclass,
                                       longtable, None, mxBasis="fooBar")
        tab = gen.get_gateset_spam_table(gateset, formats, tableclass, longtable, None, mxBasis="gm")
        tab_wCI = gen.get_gateset_spam_table(gateset, formats, tableclass, longtable, ci, mxBasis="gm")
        gen.get_gateset_spam_table(gateset, formats, tableclass, longtable, None, mxBasis="std")
        gen.get_gateset_spam_table(gateset, formats, tableclass, longtable, None, mxBasis="pp")
        gen.get_gateset_gates_table(gateset_tp, formats, tableclass, longtable, ci_TP) #test zero-padding
        gen.get_unitary_gateset_gates_table(std.gs_target, formats, tableclass, longtable, ci_tgt) #unitary gates w/CIs
        gen.get_unitary_gateset_gates_table(target_tp, formats, tableclass, longtable, ci_TP_tgt) #unitary gates w/CIs
        gen.get_gateset_closest_unitary_table(gateset_2q, formats, tableclass, longtable, None) #test higher-dim gateset
        gen.get_gateset_closest_unitary_table(gateset, formats, tableclass, longtable, ci) #test with CIs (long...)
        gen.get_gateset_rotn_axis_table(std.gs_target, formats, tableclass, longtable, None) #try to get "--"s and "X"s to display
        gen.get_chi2_progress_table([0], [gateset_tp], [ [('Gx',)],], ds, formats, tableclass, longtable) #TP case
        gen.get_chi2_confidence_region(gateset_tp, ds, 95) #TP case

        gen.get_gatestring_multi_table([ [('Gx',),('Gz',)], [('Gy',)] ], ["list1","list2"], formats,
                                       tableclass, longtable, commonTitle=None) #commonTitle == None case w/diff length lists

        with self.assertRaises(ValueError):
            gen.get_unitary_gateset_gates_table(std.gs_target, formats, tableclass, longtable, ci) #gateset-CI mismatch
        with self.assertRaises(ValueError):
            gen.get_gateset_spam_parameters_table(std.gs_target, formats, tableclass, longtable, ci) #gateset-CI mismatch

        #Test ReportTable object
        rowLabels = tab.keys()
        row1Data = tab[rowLabels[0]]
        colLabels = row1Data.keys()

        self.assertTrue(rowLabels, tab.row_names)
        self.assertTrue(colLabels, tab.col_names)
        self.assertTrue(len(rowLabels), tab.num_rows)
        self.assertTrue(len(colLabels), tab.num_cols)

        el00 = tab[rowLabels[0]][colLabels[0]]
        self.assertTrue( rowLabels[0] in tab )
        self.assertTrue( tab.has_key(rowLabels[0]) )

        table_len = len(tab)
        self.assertEqual(table_len, tab.num_rows)

        table_as_str = str(tab)
        table_wCI_as_str = str(tab_wCI)
        row1a = tab.row(key=rowLabels[0])
        col1a = tab.col(key=colLabels[0])
        row1b = tab.row(index=0)
        col1b = tab.col(index=0)
        self.assertEqual(row1a,row1b)
        self.assertEqual(col1a,col1b)

        with self.assertRaises(KeyError):
            tab['foobar']
        with self.assertRaises(KeyError):
            tab.row(key='foobar') #invalid key
        with self.assertRaises(ValueError):
            tab.row(index=100000) #out of bounds
        with self.assertRaises(ValueError):
            tab.row() #must specify key or index
        with self.assertRaises(ValueError):
            tab.row(key='foobar',index=1) #cannot specify key and index
        with self.assertRaises(KeyError):
            tab.col(key='foobar') #invalid key
        with self.assertRaises(ValueError):
            tab.col(index=100000) #out of bounds
        with self.assertRaises(ValueError):
            tab.col() #must specify key or index
        with self.assertRaises(ValueError):
            tab.col(key='foobar',index=1) #cannot specify key and index

        with self.assertRaises(ValueError):
            tab.render(fmt="foobar") #invalid format

        latexOnlyTab = pygsti.report.table.ReportTable(['latex'], ['Col1','Col2'], (None,None),
                                                       "tableClass",False)
        with self.assertRaises(KeyError):
            x = str(latexOnlyTab) #must have 'py' format
            



        #LogL case tests
        gen.get_logl_progress_table([0], [gateset_tp], [ [('Gx',)],], ds, formats, tableclass, longtable) # logL case
        gen.get_logl_progress_table([0], [gateset], [ [('Gx',)],], ds, formats, tableclass, longtable) # logL case

        gen.get_logl_confidence_region(gateset_tp, ds, 95,
                                       gatestring_list=None, probClipInterval=(-1e6,1e6),
                                       minProbClip=1e-4, radius=1e-4, hessianProjection="std")
        gen.get_logl_confidence_region(gateset, ds, 95,
                                       gatestring_list=None, probClipInterval=(-1e6,1e6),
                                       minProbClip=1e-4, radius=1e-4, hessianProjection="std")
Пример #29
0
def mdl_clgst(self):
    return pygsti.contract(self.mdl_lgst_go, "CPTP")
Пример #30
0
    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)