Esempio n. 1
0
    def test_drift_characterization(self):
        tds = pygsti.io.load_tddataset(compare_files +
                                       "/timeseries_data_trunc.txt")
        results_gst = drift.do_basic_drift_characterization(tds)
        results_gst.any_drift_detect()
        gstr = list(tds.keys())[0]

        print(results_gst.global_drift_frequencies)

        if bMPL:
            results_gst.plot_power_spectrum()
            results_gst.plot_power_spectrum(sequence=gstr, loc='upper right')

            # This box constructs some GST objects, needed to create any sort of boxplot with GST data

        # This manually specifies the germ and fiducial structure for the imported data.
        fiducial_strs = ['{}', 'Gx', 'Gy', 'GxGx', 'GxGxGx', 'GyGyGy']
        germ_strs = [
            'Gi', 'Gx', 'Gy', 'GxGy', 'GxGyGi', 'GxGiGy', 'GxGiGi', 'GyGiGi',
            'GxGxGiGy', 'GxGyGyGi', 'GxGxGyGxGyGy'
        ]
        log2maxL = 1  # log2 of the maximum germ power

        # Below we use the maxlength, germ and fuducial lists to create the GST structures needed for box plots.
        fiducials = [
            pygsti.objects.GateString(None, fs) for fs in fiducial_strs
        ]
        germs = [pygsti.objects.GateString(None, gs) for gs in germ_strs]
        max_lengths = [2**i for i in range(0, log2maxL)]
        gssList = pygsti.construction.make_lsgst_structs(
            std1Q_XYI.gates, fiducials, fiducials, germs, max_lengths)

        w = pygsti.report.Workspace()
        w.init_notebook_mode(connected=False, autodisplay=True)

        # Create a boxplot of the maximum power in the power spectra for each sequence.
        w.ColorBoxPlot('driftpwr',
                       gssList[-1],
                       None,
                       None,
                       driftresults=results_gst)

        if bMPL:
            results_gst.plot_most_drifty_probability(plot_data=True)

            gstrs = [
                pygsti.objects.GateString(None, '(Gx)^' + str(2**l))
                for l in range(0, 1)
            ]
            results_gst.plot_multi_estimated_probabilities(gstrs,
                                                           loc='upper right')

        #More from bottom of tutorial:
        fname = compare_files + "/timeseries_data_trunc.txt"
        data_gst, indices_to_gstrs = drift.load_bitstring_data(fname)
        gstrs_to_indices = results_gst.sequences_to_indices
        parray_gst = drift.load_bitstring_probabilities(
            fname, gstrs_to_indices)
Esempio n. 2
0
    def test_plot_creation(self):
        w = pygsti.report.Workspace()
        prepStrs = self.results.gatestring_lists['prep fiducials']
        effectStrs = self.results.gatestring_lists['effect fiducials']
        non_gatestring_strs = [ 'GxString', 'GyString' ]
        
        plts = []
        plts.append( w.BoxKeyPlot(prepStrs, effectStrs) )
        plts.append( w.BoxKeyPlot(non_gatestring_strs, non_gatestring_strs) )
        plts.append( w.ColorBoxPlot(("chi2","logl"), self.gss, self.ds, self.gs, boxLabels=True,
                                    hoverInfo=True, sumUp=False, invert=False) )
        plts.append( w.ColorBoxPlot(("chi2","logl"), self.gss, self.ds, self.gs, boxLabels=False,
                                    hoverInfo=True, sumUp=True, invert=False) )
        plts.append( w.ColorBoxPlot(("chi2","logl"), self.gss, self.ds, self.gs, boxLabels=False,
                                    hoverInfo=True, sumUp=False, invert=True) )
        plts.append( w.ColorBoxPlot(("chi2","logl"), self.gss, self.ds, self.gs, boxLabels=False,
                                    hoverInfo=True, sumUp=False, invert=False, typ="scatter") )

        mds = pygsti.objects.MultiDataSet()
        mds.add_dataset("DS0",self.ds)
        mds.add_dataset("DS1",self.ds)
        dsc = pygsti.objects.DataComparator([self.ds,self.ds], gate_exclusions=['Gfoo'], gate_inclusions=['Gx','Gy','Gi'])
        dsc2 = pygsti.objects.DataComparator(mds)
        plts.append( w.ColorBoxPlot(("dscmp",), self.gss, None, self.gs, dscomparator=dsc) ) # dscmp with 'None' dataset specified
        plts.append( w.ColorBoxPlot(("dscmp",), self.gss, None, self.gs, dscomparator=dsc2) )

        tds = pygsti.io.load_tddataset(compare_files + "/timeseries_data_trunc.txt")
        driftresults = drift.do_basic_drift_characterization(tds)
        plts.append( w.ColorBoxPlot(("driftpv","driftpwr"), self.gss, self.ds, self.gs, boxLabels=False,
                                    hoverInfo=True, sumUp=True, invert=False, driftresults=driftresults) )

        with self.assertRaises(ValueError):
            w.ColorBoxPlot(("foobar",), self.gss, self.ds, self.gs)
        with self.assertRaises(ValueError):
            w.ColorBoxPlot(("chi2",), self.gss, self.ds, self.gs, typ="foobar")

        from pygsti.algorithms import directx as dx
        #specs = pygsti.construction.build_spam_specs(
        #        prepStrs=prepStrs,
        #        effectStrs=effectStrs,
        #        prep_labels=list(self.gs.preps.keys()),
        #        effect_labels=self.gs.get_effect_labels() )
        baseStrs = self.gss.get_basestrings()
        directGatesets = dx.direct_mlgst_gatesets(
            baseStrs, self.ds, prepStrs, effectStrs, self.tgt, svdTruncateTo=4)
        plts.append( w.ColorBoxPlot(["chi2","logl","blank",'directchi2','directlogl'], self.gss,
                                    self.ds, self.gs, boxLabels=False, directGSTgatesets=directGatesets) )
        plts.append( w.ColorBoxPlot(["errorrate"], self.gss,
                                    self.ds, self.gs, boxLabels=False, sumUp=True,
                                    directGSTgatesets=directGatesets) )
        
        gmx = np.identity(4,'d'); gmx[3,0] = 0.5
        plts.append( w.MatrixPlot(gmx, -1,1, ['a','b','c','d'], ['e','f','g','h'], "X", "Y",
                                  colormap = pygsti.report.colormaps.DivergingColormap(vmin=-2, vmax=2)) )
        plts.append( w.MatrixPlot(gmx, -1,1, ['a','b','c','d'], ['e','f','g','h'], "X", "Y",colormap=None))
        plts.append( w.GateMatrixPlot(gmx, -1,1, "pp", "in", "out", boxLabels=True) )
        plts.append( w.PolarEigenvaluePlot([np.linalg.eigvals(self.gs.gates['Gx'])],["purple"],scale=1.5) )
        plts.append( w.PolarEigenvaluePlot([np.linalg.eigvals(self.gs.gates['Gx'])],["purple"],amp=2.0) )

        projections = np.zeros(16,'d')
        plts.append( w.ProjectionsBoxPlot(projections, "pp", boxLabels=False) )
        plts.append( w.ProjectionsBoxPlot(projections, "gm", boxLabels=True) )
        plts.append( w.ProjectionsBoxPlot(np.zeros(9,'d'), "gm", boxLabels=True) )  # non-qubit case
        plts.append( w.ProjectionsBoxPlot(np.zeros(64,'d'), "gm", boxLabels=True) ) # >2Q case
        
        choievals = np.array([-0.03, 0.02, 0.04, 0.98])
        choieb = np.array([0.05, 0.01, 0.02, 0.01])
        plts.append( w.ChoiEigenvalueBarPlot(choievals, None) )
        plts.append( w.ChoiEigenvalueBarPlot(choievals, choieb) )

        plts.append( w.FitComparisonBarPlot(self.gss.Ls, self.results.gatestring_structs['iteration'],
                                          self.results.estimates['default'].gatesets['iteration estimates'], self.ds,) )
        plts.append( w.GramMatrixBarPlot(self.ds,self.tgt) )

        plts.append( w.DatasetComparisonHistogramPlot(dsc, nbins=50, frequency=True, 
                                                      log=True, display='pvalue'))
        plts.append( w.DatasetComparisonHistogramPlot(dsc, nbins=None, frequency=True, 
                                                      log=False, display='llr'))
        with self.assertRaises(ValueError):
            w.DatasetComparisonHistogramPlot(dsc, nbins=50, frequency=True, 
                                             log=False, display='foobar')
                    
        # Note: RandomizedBenchmarkingPlot is tested in extras/test_rb.py
                     
        #Now test table rendering in html
        for i,plt in enumerate(plts):
            print("Plot: %s" % str(type(plt)))
            out_html = plt.render("html")
            plt.saveas(temp_files + "/saved_plot_%d.html" % i)
            plt.saveas(temp_files + "/saved_plot_%d.pkl" % i) #Note: plots don't need Pandas for pickling
            if bLatex:
                plt.saveas(temp_files + "/saved_plot_%d.pdf" % i)
            with self.assertRaises(ValueError):
                plt.saveas(temp_files + "/saved_plot_%d.foobar" % i) # Unknown file type

        w.save_cache(temp_files + "/saved_workspace_cache1.pkl")
        w.load_cache(temp_files + "/saved_workspace_cache1.pkl")
Esempio n. 3
0
    def test_single_sequence_1Q(self):

        N = 5  # Counts per timestep
        T = 100  # Number of timesteps

        # The confidence of the statistical tests. Here we set it to 0.999, which means that
        # if we detect drift we are 0.999 confident that we haven't incorrectly rejected the
        # initial hypothesis of no drift.
        confidence = 0.999

        # A drifting probability to obtain the measurement outcome with index 1 (out of [0,1])
        def pt_drift(t):
            return 0.5 + 0.2 * np.cos(0.1 * t)

        # A drift-free probability to obtain the measurement outcome with index 1 (out of [0,1])
        def pt_nodrift(t):
            return 0.5

        # If we want the sequence to have a label, we define a list for this (here, a list of length 1).
        # The labels can, but need not be, pyGSTi GateString objects.
        sequences = [
            pygsti.objects.GateString(None, 'Gx(Gi)^64Gx'),
        ]

        # If we want the outcomes to have labels, we define a list for this.
        outcomes = ['0', '1']

        # Let's create some fake data by sampling from these p(t) at integer times. Here we have
        # created a 1D array, but we could have instead created a 1 x 1 x 1 x T array.
        data_1seq_drift = np.array(
            [binomial(N, pt_drift(t)) for t in range(0, T)])
        data_1seq_nodrift = np.array(
            [binomial(N, pt_nodrift(t)) for t in range(0, T)])

        # If we want frequencies in Hertz, we need to specify the timestep in seconds. If this isn't
        # specified, the frequencies are given in 1/timestep with timestep defaulting to 1.
        timestep = 1e-5

        # We hand these 1D arrays to the analysis function, along with the number of counts, and other
        # optional information
        results_1seq_drift = drift.do_basic_drift_characterization(
            data_1seq_drift,
            counts=N,
            outcomes=outcomes,
            confidence=confidence,
            timestep=timestep,
            indices_to_sequences=sequences)
        results_1seq_nodrift = drift.do_basic_drift_characterization(
            data_1seq_nodrift,
            counts=N,
            outcomes=outcomes,
            confidence=confidence,
            timestep=timestep,
            indices_to_sequences=sequences)

        if bMPL:
            results_1seq_drift.plot_power_spectrum()
            results_1seq_nodrift.plot_power_spectrum()

        print(results_1seq_drift.global_pvalue)
        print(results_1seq_nodrift.global_pvalue)

        # The power spectrum obtained after averaging over everthing
        print(results_1seq_drift.global_power_spectrum[:4])
        # The power spectrum obtained after averaging over everthing except sequence label
        print(results_1seq_drift.ps_power_spectrum[0, :4])
        # The power spectrum obtained after averaging over everthing except entity label
        print(results_1seq_drift.pe_power_spectrum[0, :4])
        # The power spectrum obtained after averaging over everthing except sequene and entity label
        print(results_1seq_drift.pspe_power_spectrum[0, 0, :4])
        # The two power spectra obtained after averaging over nothing
        print(results_1seq_drift.pspepo_power_spectrum[0, 0, 0, :4])
        print(results_1seq_drift.pspepo_power_spectrum[0, 0, 1, :4])

        # Lets create an array of the true probability. This needs to be
        # of dimension S x E x M x T
        parray_1seq = np.zeros((1, 1, 2, T), float)
        parray_1seq[0, 0, 0, :] = np.array([pt_drift(t) for t in range(0, T)])
        parray_1seq[0, 0, 1, :] = 1 - parray_1seq[0, 0, 0, :]

        # The measurement outcome index we want to look at (here the esimated p(t)
        # for one index is just 1 - the p(t) for the other index, because we are
        # looking at a two-outcome measurement).
        outcome = 1

        # If we hand the parray to the plotting function, it will also plot
        # the true probability alongside our estimate from the data
        if bMPL:
            results_1seq_drift.plot_estimated_probability(sequence=0,
                                                          outcome=outcome,
                                                          parray=parray_1seq,
                                                          plot_data=True)
Esempio n. 4
0
    def test_single_sequence_multiQ(self):
        outcomes = ['00', '01', '10', '11']

        N = 10  # Counts per timestep
        T = 1000  # Number of timesteps

        # The drifting probabilities for the 4 outcomes
        def pt00(t):
            return (0.5 + 0.07 * np.cos(0.08 * t)) * (0.5 +
                                                      0.08 * np.cos(0.2 * t))

        def pt01(t):
            return (0.5 + 0.07 * np.cos(0.08 * t)) * (0.5 -
                                                      0.08 * np.cos(0.2 * t))

        def pt10(t):
            return (0.5 - 0.07 * np.cos(0.08 * t)) * (0.5 +
                                                      0.08 * np.cos(0.2 * t))

        def pt11(t):
            return (0.5 - 0.07 * np.cos(0.08 * t)) * (0.5 -
                                                      0.08 * np.cos(0.2 * t))

        # Because of the type of input (>2 measurement outcomes), we must record the
        # data in a 4D array (even though some of the dimensions are trivial)
        data_multiqubit = np.zeros((1, 1, 4, T), float)

        # Generate data from these p(t)
        for t in range(0, T):
            data_multiqubit[0, 0, :, t] = multinomial(
                N,
                [pt00(t), pt01(t), pt10(t), pt11(t)])

        results_multiqubit_full = drift.do_basic_drift_characterization(
            data_multiqubit, outcomes=outcomes, confidence=0.99)
        print(results_multiqubit_full.global_drift_frequencies)

        if bMPL:
            results_multiqubit_full.plot_power_spectrum()
            outcome = 0  # the outcome index associated with the '00' outcome
            results_multiqubit_full.plot_power_spectrum(sequence=0,
                                                        entity=0,
                                                        outcome=outcome)

        print(results_multiqubit_full.pspepo_drift_frequencies[0, 0, 0])
        print(results_multiqubit_full.pspepo_drift_frequencies[0, 0, 1])
        print(results_multiqubit_full.pspepo_drift_frequencies[0, 0, 2])
        print(results_multiqubit_full.pspepo_drift_frequencies[0, 0, 3])

        # Creates an array of the true probability.
        parray_multiqubit_full = np.zeros((1, 1, 4, T), float)
        parray_multiqubit_full[0, 0,
                               0, :] = np.array([pt00(t) for t in range(0, T)])
        parray_multiqubit_full[0, 0,
                               1, :] = np.array([pt01(t) for t in range(0, T)])
        parray_multiqubit_full[0, 0,
                               2, :] = np.array([pt10(t) for t in range(0, T)])
        parray_multiqubit_full[0, 0,
                               3, :] = np.array([pt11(t) for t in range(0, T)])

        if bMPL:
            results_multiqubit_full.plot_estimated_probability(
                sequence=0,
                outcome=1,
                plot_data=True,
                parray=parray_multiqubit_full)

        results_multiqubit_marg = drift.do_basic_drift_characterization(
            data_multiqubit,
            outcomes=outcomes,
            marginalize='std',
            confidence=0.99)
        self.assertEqual(np.shape(results_multiqubit_marg.data),
                         (1, 2, 2, 1000))

        if bMPL:
            results_multiqubit_marg.plot_power_spectrum()
            results_multiqubit_marg.plot_power_spectrum(sequence=0, entity=1)
            results_multiqubit_marg.plot_power_spectrum(sequence=0, entity=0)

        # Drift frequencies for the first qubit
        print(results_multiqubit_marg.pe_drift_frequencies[0])
        # Drift frequencies for the second qubit
        print(results_multiqubit_marg.pe_drift_frequencies[1])

        # Creates an array of the true probability.
        parray_multiqubit_marg = np.zeros((1, 2, 2, T), float)
        parray_multiqubit_marg[0, 0, 0, :] = np.array(
            [pt00(t) + pt01(t) for t in range(0, T)])
        parray_multiqubit_marg[0, 0, 1, :] = np.array(
            [pt10(t) + pt11(t) for t in range(0, T)])
        parray_multiqubit_marg[0, 1, 0, :] = np.array(
            [pt00(t) + pt10(t) for t in range(0, T)])
        parray_multiqubit_marg[0, 1, 1, :] = np.array(
            [pt01(t) + pt11(t) for t in range(0, T)])

        if bMPL:
            results_multiqubit_marg.plot_estimated_probability(
                sequence=0, entity=0, outcome=0, parray=parray_multiqubit_marg)

        N = 10  # Counts per timestep
        T = 1000  # Number of timesteps

        outcomes = ['00', '01', '10', '11']

        def pt_correlated00(t):
            return 0.25 - 0.05 * np.cos(0.05 * t)

        def pt_correlated01(t):
            return 0.25 + 0.05 * np.cos(0.05 * t)

        def pt_correlated10(t):
            return 0.25 + 0.05 * np.cos(0.05 * t)

        def pt_correlated11(t):
            return 0.25 - 0.05 * np.cos(0.05 * t)

        data_1seq_multiqubit = np.zeros((1, 1, 4, T), float)
        for t in range(0, T):
            pvec = [
                pt_correlated00(t),
                pt_correlated01(t),
                pt_correlated10(t),
                pt_correlated11(t)
            ]
            data_1seq_multiqubit[0, 0, :, t] = multinomial(N, pvec)

        results_correlatedrift_marg = drift.do_basic_drift_characterization(
            data_1seq_multiqubit, outcomes=outcomes, marginalize='std')

        results_correlatedrift_full = drift.do_basic_drift_characterization(
            data_1seq_multiqubit, outcomes=outcomes, marginalize='none')

        if bMPL:
            results_correlatedrift_marg.plot_power_spectrum()
            results_correlatedrift_full.plot_power_spectrum()