def test_generate_fake_data_raises_on_bad_sample_error(self): with self.assertRaises(ValueError): pdata.simulate_data(self.dataset, self.circuit_list, num_samples=None, sample_error='foobar', seed=100)
def test_generate_fake_data(self): dataset = pdata.simulate_data(self.dataset, self.circuit_list, num_samples=None, sample_error='none', seed=100) dataset = pdata.simulate_data(self.dataset, self.circuit_list, num_samples=100, sample_error='round', seed=100) dataset = pdata.simulate_data(self.dataset, self.circuit_list, num_samples=100, sample_error='multinomial', seed=100) randState = np.random.RandomState(1234) dataset1 = pdata.simulate_data(dataset, self.circuit_list, num_samples=100, sample_error='binomial', rand_state=randState) dataset2 = pdata.simulate_data(dataset, self.circuit_list, num_samples=100, sample_error='binomial', seed=1234) for dr1, dr2 in zip(dataset1.values(), dataset2.values()): self.assertEqual(dr1.counts, dr2.counts)
def setUpClass(cls): #Let's make our underlying model have a little bit of random unitary noise. cls.mdl_exp_0 = std1Q_XYI.target_model().randomize_with_unitary(.01, seed=0) cls.mdl_exp_1 = std1Q_XYI.target_model().randomize_with_unitary(.01, seed=1234) germs = std1Q_XYI.germs fiducials = std1Q_XYI.fiducials max_lengths = [1, 2, 4, 8] gate_sequences = pc.create_lsgst_circuits(std1Q_XYI.gates, fiducials, fiducials, germs, max_lengths) #Generate the data for the two data, using the same model, with 100 repetitions of each sequence. N = 100 cls.DS_0 = pdata.simulate_data(cls.mdl_exp_0, gate_sequences, N, 'binomial', seed=10) cls.DS_1 = pdata.simulate_data(cls.mdl_exp_1, gate_sequences, N, 'binomial', seed=20)
def ds(self): expList = circuits.create_lsgst_circuits(self.opLabels, self.fiducials, self.fiducials, self.germs, self.maxLengthList) return data.simulate_data(self.datagen_gateset, expList, num_samples=1000, sample_error='binomial', seed=_SEED)
def setUpClass(cls): cls.gst_design = smq1Q_XYI.get_gst_experiment_design(max_max_length=4) cls.mdl_target = smq1Q_XYI.target_model() cls.mdl_datagen = cls.mdl_target.depolarize(op_noise=0.05, spam_noise=0.025) ds = simulate_data(cls.mdl_datagen, cls.gst_design.all_circuits_needing_data, 1000, sample_error='none') cls.gst_data = ProtocolData(cls.gst_design, ds)
def setUpClass(cls): super(RobustDataScalingTester, cls).setUpClass() datagen_gateset = cls.model.depolarize(op_noise=0.1, spam_noise=0.03).rotate( (0.05, 0.13, 0.02)) ds2 = pdata.simulate_data(datagen_gateset, cls.lsgstStrings[-1], num_samples=1000, sample_error='binomial', seed=100).copy_nonstatic() ds2.add_counts_from_dataset(cls.ds) ds2.done_adding_data() cls.ds = ds2
def setUp(self): super(RPEToolsFuncBase, self).setUp() self.target = stdXY.target_model() self.target.operations['Gi'] = std.target_model().operations[ 'Gi'] # need a Gi gate... self.stringListD = rpc.create_rpe_angle_circuits_dict(2, self.config) self.mdl_depolXZ = self.target.depolarize(op_noise=0.1, spam_noise=0.1, seed=_SEED) self.ds = pdata.simulate_data(self.mdl_depolXZ, self.stringListD['totalStrList'], num_samples=1000, sample_error='binomial', seed=_SEED)
def test_get_model_lgst(self): #LGST datagen_model = self.target_model.depolarize(op_noise=0.1) ds = simulate_data(datagen_model, self.edesign.all_circuits_needing_data, 1000, sample_error='none') # no error for reproducibility im1 = gst.GSTInitialModel(self.target_model, "LGST") mdl1 = im1.retrieve_model(self.edesign, None, ds, None) im2 = gst.GSTInitialModel(self.target_model, "LGST-if-possible") mdl2 = im2.retrieve_model(self.edesign, None, ds, None) self.assertTrue(mdl1.frobeniusdist(mdl2) < 1e-6)
def setUpClass(cls): #Construct a results object gst_design = smq1Q_XYI.get_gst_experiment_design(max_max_length=4) mdl_target = smq1Q_XYI.target_model() mdl_datagen = mdl_target.depolarize(op_noise=0.05, spam_noise=0.025) ds = simulate_data(mdl_datagen, gst_design.all_circuits_needing_data, 1000, seed=2020) data = ProtocolData(gst_design, ds) cls.results = gst.ModelEstimateResults(data, Protocol("test-protocol")) cls.results.add_estimate(Estimate.create_gst_estimate( cls.results, mdl_target, mdl_target, [mdl_datagen] * len(gst_design.circuit_lists), parameters={'objective': 'logl'}), estimate_key="test-estimate") cls.target_model = mdl_target
def setUpClass(cls): super(DirectXTester, cls).setUpClass() cls._tgt = fixtures.model.copy() cls.prepStrs = fixtures.fiducials cls.effectStrs = fixtures.fiducials cls.strs = pc.to_circuits([ (), # always need empty string ('Gx', ), ('Gy', ), ('Gi', ), # need these for include_target_ops=True ('Gx', 'Gx'), ('Gx', 'Gy', 'Gx') # additional ]) expstrs = pc.create_circuits("f0+base+f1", order=['f0', 'f1', 'base'], f0=fixtures.fiducials, f1=fixtures.fiducials, base=cls.strs) cls._ds = pdata.simulate_data(fixtures.datagen_gateset.copy(), expstrs, 1000, 'multinomial', seed=_SEED)
def dataset(self): return pdata.simulate_data( self.datagen_gateset, self.lsgstStrings[-1], num_samples=1000, sample_error='binomial', seed=100 )
def create_rpe_dataset(model_or_dataset, string_list_d, n_samples, sample_error='binomial', seed=None): """ Generate a fake RPE DataSet using the probabilities obtained from a model. Is a thin wrapper for pygsti.data.simulate_data, changing default behavior of sample_error, and taking a dictionary of operation sequences as input. Parameters ---------- model_or_dataset : Model or DataSet object If a Model, the model whose probabilities generate the data. If a DataSet, the data set whose frequencies generate the data. string_list_d : Dictionary of list of (tuples or Circuits) Each tuple or Circuit contains operation labels and specifies a gate sequence whose counts are included in the returned DataSet. The dictionary must have the key 'totalStrList'; easiest if this dictionary is generated by make_rpe_string_list_d. n_samples : int or list of ints or None The simulated number of samples for each operation sequence. This only has effect when sample_error == "binomial" or "multinomial". If an integer, all operation sequences have this number of total samples. If a list, integer elements specify the number of samples for the corresponding operation sequence. If None, then model_or_dataset must be a DataSet, and total counts are taken from it (on a per-circuit basis). sample_error : string, optional What type of sample error is included in the counts. Can be: - "none" - no sample error: counts are floating point numbers such that the exact probabilty can be found by the ratio of count / total. - "round" - same as "none", except counts are rounded to the nearest integer. - "binomial" - the number of counts is taken from a binomial distribution. Distribution has parameters p = probability of the operation sequence and n = number of samples. This can only be used when there are exactly two outcome labels in model_or_dataset. - "multinomial" - counts are taken from a multinomial distribution. Distribution has parameters p_k = probability of the operation sequence using the k-th outcome label and n = number of samples. This should not be used for RPE. seed : int, optional If not None, a seed for numpy's random number generator, which is used to sample from the binomial or multinomial distribution. Returns ------- DataSet A static data set filled with counts for the specified operation sequences. """ return _data.simulate_data(model_or_dataset, string_list_d['totalStrList'], n_samples, sample_error=sample_error, seed=seed)
def ds_lgst(self): return data.simulate_data(self.datagen_gateset, self.lgstStrings, num_samples=10000, sample_error='binomial', seed=_SEED)
def setUp(self): # TODO optimize self.model = models.create_explicit_model_from_expressions( [('Q0', )], ['Gi', 'Gx', 'Gy'], ["I(Q0)", "X(pi/2,Q0)", "Y(pi/2,Q0)"]) self.depolGateset = self.model.depolarize(op_noise=0.1) def make_lsgst_lists(opLabels, fiducialList, germList, maxLengthList): singleOps = pc.to_circuits([(g, ) for g in opLabels]) lgstStrings = pc.create_lgst_circuits(fiducialList, fiducialList, opLabels) lsgst_list = pc.to_circuits([ () ]) # 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_circuits("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( lt.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.to_circuits([(), ('Gx', ), ('Gy', ), ('Gx', 'Gx'), ('Gx', 'Gx', 'Gx'), ('Gy', 'Gy', 'Gy') ]) # fiducials for 1Q MUB germs = pc.to_circuits([('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.circuit_list = self.lsgst_lists[-1] self.dataset = pdata.simulate_data(self.depolGateset, self.circuit_list, num_samples=1000, sample_error='binomial', seed=100)
def rpe_ensemble_test(alpha_true, epsilon_true, y_rot, spam_depol, log2k_max, n, runs): """ Experimental test function """ from pygsti.circuits.rpecircuits import make_rpe_alpha_str_lists_gx_gz, make_rpe_theta_str_lists_gx_gz, \ make_rpe_epsilon_str_lists_gx_gz import pygsti.data as _dsc kList = [2**k for k in range(log2k_max + 1)] alphaCosStrList, alphaSinStrList = make_rpe_alpha_str_lists_gx_gz(kList) epsilonCosStrList, epsilonSinStrList = make_rpe_epsilon_str_lists_gx_gz( kList) thetaCosStrList, thetaSinStrList = make_rpe_theta_str_lists_gx_gz(kList) #percentAlphaError = 100*_np.abs((_np.pi/2-alpha_true)/alpha_true) #percentEpsilonError = 100*_np.abs((_np.pi/4 - epsilon_true)/epsilon_true) simModel = _setc.create_explicit_model_from_expressions( [('Q0', )], ['Gi', 'Gx', 'Gz'], [ "I(Q0)", "X(" + str(epsilon_true) + ",Q0)", "Z(" + str(alpha_true) + ",Q0)" ], prep_labels=["rho0"], prep_expressions=["0"], effect_labels=["E0", "Ec"], effect_expressions=["0", "complement"], spamdefs={ '0': ('rho0', 'E0'), '1': ('rho0', 'Ec') }) modelAux1 = _setc.create_explicit_model_from_expressions( [('Q0', )], ['Gi', 'Gy', 'Gz'], ["I(Q0)", "Y(" + str(y_rot) + ",Q0)", "Z(pi/2,Q0)"], prep_labels=["rho0"], prep_expressions=["0"], effect_labels=["E0", "Ec"], effect_expressions=["0", "complement"], spamdefs={ '0': ('rho0', 'E0'), '1': ('rho0', 'Ec') }) simModel.operations['Gx'] = _op.FullArbitraryOp( _np.dot( _np.dot(_np.linalg.inv(modelAux1.operations['Gy']), simModel.operations['Gx']), modelAux1.operations['Gy'])) simModel = simModel.depolarize(spam_noise=spam_depol) thetaTrue = _tools.rpe.extract_theta(simModel) #SPAMerror = _np.dot(simModel.effects['E0'].T,simModel.preps['rho0'])[0,0] jMax = runs alphaHatListArray = _np.zeros([jMax, log2k_max + 1], dtype='object') epsilonHatListArray = _np.zeros([jMax, log2k_max + 1], dtype='object') thetaHatListArray = _np.zeros([jMax, log2k_max + 1], dtype='object') alphaErrorArray = _np.zeros([jMax, log2k_max + 1], dtype='object') epsilonErrorArray = _np.zeros([jMax, log2k_max + 1], dtype='object') thetaErrorArray = _np.zeros([jMax, log2k_max + 1], dtype='object') PhiFunErrorArray = _np.zeros([jMax, log2k_max + 1], dtype='object') for j in range(jMax): simDS = _dsc.simulate_data(simModel, alphaCosStrList + alphaSinStrList + epsilonCosStrList + epsilonSinStrList + thetaCosStrList + thetaSinStrList, n, sample_error='binomial', seed=j) alphaErrorList = [] epsilonErrorList = [] thetaErrorList = [] PhiFunErrorList = [] alphaHatList = _tools.rpe.estimate_angles(simDS, alphaSinStrList, alphaCosStrList, 'alpha') epsilonHatList = _tools.rpe.estimate_angles(simDS, epsilonSinStrList, epsilonCosStrList, 'epsilon') thetaHatList, PhiFunList = _tools.rpe.estimate_thetas( simDS, thetaSinStrList, thetaCosStrList, epsilonHatList, return_phi_fun_list=True) for alphaTemp1 in alphaHatList: alphaErrorList.append(abs(alpha_true - alphaTemp1)) for epsilonTemp1 in epsilonHatList: epsilonErrorList.append(abs(epsilon_true - epsilonTemp1)) # print abs(_np.pi/2-abs(alphaTemp1)) for thetaTemp1 in thetaHatList: thetaErrorList.append(abs(thetaTrue - thetaTemp1)) for PhiFunTemp1 in PhiFunList: PhiFunErrorList.append(PhiFunTemp1) alphaErrorArray[j, :] = _np.array(alphaErrorList) epsilonErrorArray[j, :] = _np.array(epsilonErrorList) thetaErrorArray[j, :] = _np.array(thetaErrorList) PhiFunErrorArray[j, :] = _np.array(PhiFunErrorList) alphaHatListArray[j, :] = _np.array(alphaHatList) epsilonHatListArray[j, :] = _np.array(epsilonHatList) thetaHatListArray[j, :] = _np.array(thetaHatList) #print "True alpha:",alpha_true #print "True alpha:",alpha_true #print "True alpha:",alpha_true #print "True alpha:",alpha_true #print "% true alpha deviation from target:", percentAlphaError outputDict = {} # outputDict['alphaArray'] = alphaHatListArray # outputDict['alphaErrorArray'] = alphaErrorArray # outputDict['epsilonArray'] = epsilonHatListArray # outputDict['epsilonErrorArray'] = epsilonErrorArray # outputDict['thetaArray'] = thetaHatListArray # outputDict['thetaErrorArray'] = thetaErrorArray # outputDict['PhiFunErrorArray'] = PhiFunErrorArray # outputDict['alpha'] = alpha_true # outputDict['epsilon_true'] = epsilon_true # outputDict['thetaTrue'] = thetaTrue # outputDict['y_rot'] = y_rot # outputDict['spam_depol'] = spam_depol#Input value to depolarize SPAM by # outputDict['SPAMerror'] = SPAMerror#<<E|rho>> # outputDict['mdl'] = simModel # outputDict['n'] = n return outputDict