def setUp(self):
        super(DataSetConstructionTestCase, self).setUp()
        self.gateset = pc.build_gateset( [2], [('Q0',)],
                                         ['Gi','Gx','Gy'], [ "I(Q0)","X(pi/2,Q0)", "Y(pi/2,Q0)"],
                                         prepLabels = ['rho0'], prepExpressions=["0"],
                                         effectLabels = ['E0'], effectExpressions=["1"],
                                         spamdefs={'plus': ('rho0','E0'), 'minus': ('rho0','remainder') })
        self.depolGateset = self.gateset.depolarize(gate_noise=0.1)

        def make_lsgst_lists(gateLabels, fiducialList, germList, maxLengthList):
            singleGates = pc.gatestring_list([(g,) for g in gateLabels])
            lgstStrings = pc.list_lgst_gatestrings(pc.build_spam_specs(fiducialList), gateLabels)
            lsgst_list  = pc.gatestring_list([ () ]) #running list of all strings so far

            if maxLengthList[0] == 0:
                lsgst_listOfLists = [ lgstStrings ]
                maxLengthList = maxLengthList[1:]
            else: lsgst_listOfLists = [ ]

            for maxLen in maxLengthList:
                lsgst_list += pc.create_gatestring_list("f0+R(germ,N)+f1", f0=fiducialList,
                f1=fiducialList, germ=germList, N=maxLen,
                R=pc.repeat_with_max_length,
                order=('germ','f0','f1'))
                lsgst_listOfLists.append( pygsti.remove_duplicates(lgstStrings + lsgst_list) )

            print("%d LSGST sets w/lengths" % len(lsgst_listOfLists), map(len,lsgst_listOfLists))
            return lsgst_listOfLists

        gates      = ['Gi','Gx','Gy']
        fiducials  = pc.gatestring_list([ (), ('Gx',), ('Gy',), ('Gx','Gx'), ('Gx','Gx','Gx'), ('Gy','Gy','Gy') ]) # fiducials for 1Q MUB
        germs      = pc.gatestring_list( [('Gx',), ('Gy',), ('Gi',), ('Gx', 'Gy',),
                                     ('Gx', 'Gy', 'Gi',), ('Gx', 'Gi', 'Gy',),('Gx', 'Gi', 'Gi',), ('Gy', 'Gi', 'Gi',),
                                     ('Gx', 'Gx', 'Gi', 'Gy',), ('Gx', 'Gy', 'Gy', 'Gi',),
                                     ('Gx', 'Gx', 'Gy', 'Gx', 'Gy', 'Gy',)] )
        maxLengths = [0, 1, 2, 4, 8, 16, 32, 64, 128, 256]
        self.lsgst_lists     = make_lsgst_lists(gates, fiducials, germs, maxLengths)
        self.gatestring_list = self.lsgst_lists[-1]
        self.dataset = pc.generate_fake_data(self.depolGateset, self.gatestring_list, nSamples=1000,
                                             sampleError='binomial', seed=100)
    def test_auto_experiment_desgin(self):
        # Let's construct a 1-qubit $X(\pi/2)$, $Y(\pi/2)$, $I$ gateset for which we will need to find germs and fiducials.

        gs_target = constr.build_gateset([2], [('Q0',)], ['Gi', 'Gx', 'Gy'],
                                         ["I(Q0)", "X(pi/2,Q0)", "Y(pi/2,Q0)"],
                                         prepLabels=['rho0'], prepExpressions=["0"],
                                         effectLabels=['E0'], effectExpressions=["1"],
                                         spamdefs={'plus': ('rho0', 'E0'),
                                                   'minus': ('rho0', 'remainder')})


        # ## Hands-off

        # We begin by demonstrating the most hands-off approach.

        # We can generate a germ set simply by providing the target gateset.


        germs = germsel.generate_germs(gs_target)


        # In the same way we can generate preparation and measurement fiducials.


        prepFiducials, measFiducials = fidsel.generate_fiducials(gs_target)


        # Now that we have germs and fiducials, we can construct the list of experiments we need to perform in
        # order to do GST. The only new things to provide at this point are the sizes for the experiments we want
        # to perform (in this case we want to perform between 0 and 256 gates between fiducial pairs, going up
        # by a factor of 2 at each stage).


        maxLengths = [0] + [2**n for n in range(8 + 1)]
        listOfExperiments = constr.make_lsgst_experiment_list(gs_target.gates.keys(), prepFiducials,
                                                              measFiducials, germs, maxLengths)


        # The list of `GateString` that the previous function gave us isn't necessarily the most readable
        # form to present the information in, so we can write the experiment list out to an empty data
        # file to be filled in after the experiments are performed.

        graspGerms = germsel.generate_germs(gs_target, algorithm='grasp', algorithm_kwargs={'iterations': 1})
        slackPrepFids, slackMeasFids = fidsel.generate_fiducials(gs_target, algorithm='slack',
                                                                 algorithm_kwargs={'slackFrac': 0.25})


        max([len(germ) for germ in germs])

        germsMaxLength5 = germsel.generate_germs(gs_target, maxGermLength=5)

        max([len(germ) for germ in germsMaxLength5])

        germsMaxLength3 = germsel.generate_germs(gs_target, maxGermLength=3)

        uniformPrepFids, uniformMeasFids = fidsel.generate_fiducials(gs_target, maxFidLength=3,
                                                                     algorithm='grasp',
                                                                     algorithm_kwargs={'iterations': 100})


        incompletePrepFids, incompleteMeasFids = fidsel.generate_fiducials(gs_target, maxFidLength=1)

        nonSingletonGerms = germsel.generate_germs(gs_target, forceSingletons=False, maxGermLength=4,
                                                   algorithm='grasp', algorithm_kwargs={'iterations': 5})


        omitIdentityPrepFids, omitIdentityMeasFids = fidsel.generate_fiducials(gs_target, omitIdentity=False,
                                                                               gatesToOmit=['Gi'])
    def test_auto_experiment_desgin(self):
        # Let's construct a 1-qubit $X(\pi/2)$, $Y(\pi/2)$, $I$ gateset for which we will need to find germs and fiducials.

        gs_target = constr.build_gateset([2], [('Q0', )], ['Gi', 'Gx', 'Gy'],
                                         ["I(Q0)", "X(pi/2,Q0)", "Y(pi/2,Q0)"],
                                         prepLabels=['rho0'],
                                         prepExpressions=["0"],
                                         effectLabels=['E0'],
                                         effectExpressions=["1"],
                                         spamdefs={
                                             'plus': ('rho0', 'E0'),
                                             'minus': ('rho0', 'remainder')
                                         })

        # ## Hands-off

        # We begin by demonstrating the most hands-off approach.

        # We can generate a germ set simply by providing the target gateset.

        germs = germsel.generate_germs(gs_target)

        # In the same way we can generate preparation and measurement fiducials.

        prepFiducials, measFiducials = fidsel.generate_fiducials(gs_target)

        # Now that we have germs and fiducials, we can construct the list of experiments we need to perform in
        # order to do GST. The only new things to provide at this point are the sizes for the experiments we want
        # to perform (in this case we want to perform between 0 and 256 gates between fiducial pairs, going up
        # by a factor of 2 at each stage).

        maxLengths = [0] + [2**n for n in range(8 + 1)]
        listOfExperiments = constr.make_lsgst_experiment_list(
            gs_target.gates.keys(), prepFiducials, measFiducials, germs,
            maxLengths)

        # The list of `GateString` that the previous function gave us isn't necessarily the most readable
        # form to present the information in, so we can write the experiment list out to an empty data
        # file to be filled in after the experiments are performed.

        graspGerms = germsel.generate_germs(gs_target,
                                            algorithm='grasp',
                                            algorithm_kwargs={'iterations': 1})
        slackPrepFids, slackMeasFids = fidsel.generate_fiducials(
            gs_target, algorithm='slack', algorithm_kwargs={'slackFrac': 0.25})

        max([len(germ) for germ in germs])

        germsMaxLength5 = germsel.generate_germs(gs_target, maxGermLength=5)

        max([len(germ) for germ in germsMaxLength5])

        germsMaxLength3 = germsel.generate_germs(gs_target, maxGermLength=3)

        uniformPrepFids, uniformMeasFids = fidsel.generate_fiducials(
            gs_target,
            maxFidLength=3,
            algorithm='grasp',
            algorithm_kwargs={'iterations': 100})

        incompletePrepFids, incompleteMeasFids = fidsel.generate_fiducials(
            gs_target, maxFidLength=1)

        nonSingletonGerms = germsel.generate_germs(
            gs_target,
            forceSingletons=False,
            maxGermLength=4,
            algorithm='grasp',
            algorithm_kwargs={'iterations': 5})

        omitIdentityPrepFids, omitIdentityMeasFids = fidsel.generate_fiducials(
            gs_target, omitIdentity=False, gatesToOmit=['Gi'])
    def setUp(self):
        super(DataSetConstructionTestCase, self).setUp()
        self.gateset = pc.build_gateset([2], [('Q0', )], ['Gi', 'Gx', 'Gy'],
                                        ["I(Q0)", "X(pi/2,Q0)", "Y(pi/2,Q0)"],
                                        prepLabels=['rho0'],
                                        prepExpressions=["0"],
                                        effectLabels=['E0'],
                                        effectExpressions=["1"],
                                        spamdefs={
                                            'plus': ('rho0', 'E0'),
                                            'minus': ('rho0', 'remainder')
                                        })
        self.depolGateset = self.gateset.depolarize(gate_noise=0.1)

        def make_lsgst_lists(gateLabels, fiducialList, germList,
                             maxLengthList):
            singleGates = pc.gatestring_list([(g, ) for g in gateLabels])
            lgstStrings = pc.list_lgst_gatestrings(
                pc.build_spam_specs(fiducialList), gateLabels)
            lsgst_list = pc.gatestring_list([
                ()
            ])  #running list of all strings so far

            if maxLengthList[0] == 0:
                lsgst_listOfLists = [lgstStrings]
                maxLengthList = maxLengthList[1:]
            else:
                lsgst_listOfLists = []

            for maxLen in maxLengthList:
                lsgst_list += pc.create_gatestring_list(
                    "f0+R(germ,N)+f1",
                    f0=fiducialList,
                    f1=fiducialList,
                    germ=germList,
                    N=maxLen,
                    R=pc.repeat_with_max_length,
                    order=('germ', 'f0', 'f1'))
                lsgst_listOfLists.append(
                    pygsti.remove_duplicates(lgstStrings + lsgst_list))

            print("%d LSGST sets w/lengths" % len(lsgst_listOfLists),
                  map(len, lsgst_listOfLists))
            return lsgst_listOfLists

        gates = ['Gi', 'Gx', 'Gy']
        fiducials = pc.gatestring_list([(), ('Gx', ), ('Gy', ), ('Gx', 'Gx'),
                                        ('Gx', 'Gx', 'Gx'), ('Gy', 'Gy', 'Gy')
                                        ])  # fiducials for 1Q MUB
        germs = pc.gatestring_list([('Gx', ), ('Gy', ), ('Gi', ), (
            'Gx',
            'Gy',
        ), (
            'Gx',
            'Gy',
            'Gi',
        ), (
            'Gx',
            'Gi',
            'Gy',
        ), (
            'Gx',
            'Gi',
            'Gi',
        ), (
            'Gy',
            'Gi',
            'Gi',
        ), (
            'Gx',
            'Gx',
            'Gi',
            'Gy',
        ), (
            'Gx',
            'Gy',
            'Gy',
            'Gi',
        ), (
            'Gx',
            'Gx',
            'Gy',
            'Gx',
            'Gy',
            'Gy',
        )])
        maxLengths = [0, 1, 2, 4, 8, 16, 32, 64, 128, 256]
        self.lsgst_lists = make_lsgst_lists(gates, fiducials, germs,
                                            maxLengths)
        self.gatestring_list = self.lsgst_lists[-1]
        self.dataset = pc.generate_fake_data(self.depolGateset,
                                             self.gatestring_list,
                                             nSamples=1000,
                                             sampleError='binomial',
                                             seed=100)