def test_circuit_init(self): #Check that parallel operation labels get converted to circuits properly opstr = Circuit(((('Gx', 0), ('Gy', 1)), ('Gcnot', 0, 1))) c = Circuit(layer_labels=opstr, num_lines=2) print(c._labels) self.assertEqual(c._labels, (L( (('Gx', 0), ('Gy', 1))), L('Gcnot', (0, 1))))
def test_forward_simulation(self): pure_povm = self.povm.base_povm noise_op = self.povm.error_map # TODO: Would be nice to check more than densitymx evotype indep_mdl = ExplicitOpModel(['Q0'], evotype='default') indep_mdl['rho0'] = self.base_prep indep_mdl['G0'] = noise_op.copy() indep_mdl['Mdefault'] = pure_povm indep_mdl._clean_paramvec() composed_mdl = ExplicitOpModel(['Q0'], evotype='default') composed_mdl['rho0'] = self.base_prep composed_mdl['Mdefault'] = self.povm composed_mdl._clean_paramvec() # Sanity check indep_circ = Circuit(['rho0', 'G0', 'Mdefault']) indep_probs = indep_mdl.probabilities(indep_circ) for k, v in indep_probs.items(): self.assertAlmostEqual(self.expected_out[k], v) composed_circ = Circuit(['rho0', 'Mdefault']) composed_probs = composed_mdl.probabilities(composed_circ) for k, v in composed_probs.items(): self.assertAlmostEqual(self.expected_out[k], v)
def test_forward_simulation(self): pure_vec = self.vec.state_vec noise_op = self.vec.error_map # TODO: Would be nice to check more than densitymx evotype indep_mdl = ExplicitOpModel(['Q0'], evotype='default') indep_mdl['rho0'] = pure_vec indep_mdl['G0'] = noise_op indep_mdl['Mdefault'] = self.base_povm indep_mdl.num_params # triggers paramvec rebuild composed_mdl = ExplicitOpModel(['Q0'], evotype='default') composed_mdl['rho0'] = self.vec composed_mdl['Mdefault'] = self.base_povm composed_mdl.num_params # triggers paramvec rebuild # Sanity check indep_circ = Circuit(['rho0', 'G0', 'Mdefault']) indep_probs = indep_mdl.probabilities(indep_circ) for k, v in indep_probs.items(): self.assertAlmostEqual(self.expected_out[k], v) composed_circ = Circuit(['rho0', 'Mdefault']) composed_probs = composed_mdl.probabilities(composed_circ) for k, v in composed_probs.items(): self.assertAlmostEqual(self.expected_out[k], v)
def test_load_ignore_zero_count_lines3(self, pth): contents = ("## Outcomes = 0, 1\n" "Gc1 0:1 1:1 # {'test': 1}\n" "Gc2 # {'test': 1}\n") with open(pth, 'w') as f: f.write(contents) ds = io.read_dataset(pth, ignore_zero_count_lines=False) self.assertEqual(ds[Circuit('Gc1')]['0'], 1) self.assertEqual(ds[Circuit('Gc2')]['0'], 0) self.assertEqual(ds[Circuit('Gc1')].aux['test'], 1) self.assertEqual(ds[Circuit('Gc2')].aux['test'], 1)
def test_raises_on_conflicting_attribute_access(self): self.model.preps['rho1'] = self.model.preps['rho0'].copy() self.model.povms['M2'] = self.model.povms['Mdefault'].copy() with self.assertRaises(ValueError): self.model.prep # can only use this property when there's a *single* prep with self.assertRaises(ValueError): self.model.effects # can only use this property when there's a *single* POVM with self.assertRaises(ValueError): prep, gates, povm = self.model.split_circuit( Circuit(('rho0', 'Gx'))) with self.assertRaises(ValueError): prep, gates, povm = self.model.split_circuit( Circuit(('Gx', 'Mdefault')))
def test_repeat_methods(self): mdl = Circuit(('Gx', 'Gx', 'Gy')) gs2 = cc.repeat(mdl, 2) self.assertEqual(gs2, Circuit(('Gx', 'Gx', 'Gy', 'Gx', 'Gx', 'Gy'))) gs3 = cc.repeat_with_max_length(mdl, 7) self.assertEqual(gs3, Circuit(('Gx', 'Gx', 'Gy', 'Gx', 'Gx', 'Gy'))) gs4 = cc.repeat_and_truncate(mdl, 4) self.assertEqual(gs4, Circuit(('Gx', 'Gx', 'Gy', 'Gx'))) gs5 = cc._repeat_remainder_for_truncation(mdl, 4) self.assertEqual(gs5, Circuit(('Gx',)))
def setUp(self): self.gstrs = [('Gx',), ('Gx', 'Gy'), ('Gy',)] self.gstrInds = OrderedDict([(('Gx',), 0), (('Gx', 'Gy'), 1), (('Gy',), 2)]) self.gstrInds_static = OrderedDict([(Circuit(('Gx',)), slice(0, 2)), (Circuit(('Gx', 'Gy')), slice(2, 4)), (Circuit(('Gy',)), slice(4, 6))]) self.olInds = OrderedDict([('0', 0), ('1', 1)]) oli = np.array([0, 1], 'i') self.oli_static = np.array([0, 1] * 3, 'd') # 3 operation sequences * 2 outcome labels each self.time_static = np.zeros((6,), 'd') self.reps_static = 10 * np.ones((6,), 'd') self.oli_nonstc = [oli, oli, oli] # each item has num_outcomes elements self.time_nonstc = [np.zeros(2, 'd'), np.zeros(2, 'd'), np.zeros(2, 'd')] self.reps_nonstc = [10 * np.ones(2, 'i'), 10 * np.ones(2, 'i'), 10 * np.ones(2, 'i')]
def setUp(self): mock_model = mock.MagicMock() mock_model.evotype.return_value = "densitymx" mock_model.circuit_outcomes.return_value = ('NA', ) mock_model.num_params = 0 self.fwdsim = ForwardSimulator(mock_model) self.circuit = Circuit("GxGx")
def test_indexing(self): self.assertFalse(('Gz',) in self.ds) opstrs, rows = tuple(zip(*self.ds.items())) for a, b in zip(opstrs, self.ds): self.assertEqual(a, b) for opstr in self.ds: self.assertTrue(opstr in self.ds) self.assertTrue(Circuit(opstr) in self.ds)
def setUp(self): plaquettes = {} self.xvals = ['x1', 'x2'] self.yvals = ['y1', 'y2'] self.circuit = Circuit("GxGy") for x in self.xvals: for y in self.yvals: plaquettes[(x,y)] = cs.CircuitPlaquette({(minor_x, minor_y): self.circuit for minor_x in [0,1] for minor_y in [0,1]}) self.gss = cs.PlaquetteGridCircuitStructure(plaquettes, self.xvals, self.yvals, 'xlabel', 'ylabel')
def test_parse_circuit(self): lkup = { '1': ('G1', ), '2': ('G1', 'G2'), '3': ('G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7', 'G8', 'G9', 'G10'), 'G12': ('G1', 'G2'), 'S23': ('G2', 'G3') } string_tests = [ ("{}", ()), ("{}^127", ()), ("{}^0002", ()), ("G1", ('G1', )), ("G1G2G3", ('G1', 'G2', 'G3')), ("G1(G2)G3", ('G1', 'G2', 'G3')), ("G1(G2)^3G3", ('G1', 'G2', 'G2', 'G2', 'G3')), ("G1(G2G3)^2", ('G1', 'G2', 'G3', 'G2', 'G3')), ("G1*G2*G3", ('G1', 'G2', 'G3')), ("G1^02", ('G1', 'G1')), ("G1*((G2G3)^2G4G5)^2G7", ('G1', 'G2', 'G3', 'G2', 'G3', 'G4', 'G5', 'G2', 'G3', 'G2', 'G3', 'G4', 'G5', 'G7')), ("G1(G2^2(G3G4)^2)^2", ('G1', 'G2', 'G2', 'G3', 'G4', 'G3', 'G4', 'G2', 'G2', 'G3', 'G4', 'G3', 'G4')), ("G1*G2", ('G1', 'G2')), #("S<1>", ('G1',)), #("S<2>", ('G1', 'G2')), #("G1S<2>^2G3", ('G1', 'G1', 'G2', 'G1', 'G2', 'G3')), #("G1S<1>G3", ('G1', 'G1', 'G3')), #("S<3>[0:4]", ('G1', 'G2', 'G3', 'G4')), ("G_my_xG_my_y", ('G_my_x', 'G_my_y')), ("G_my_x*G_my_y", ('G_my_x', 'G_my_y')), ("G_my_x*G_my_y", ('G_my_x', 'G_my_y')), ("GsG___", ('Gs', 'G___')), #("S < 2 >G3", ('G1', 'G2', 'G3')), #("S<G12>", ('G1', 'G2')), #("S<S23>", ('G2', 'G3')), ("G1G2", ('G1', 'G2')), ("rho0*Gx", ('rho0', 'Gx')), ("rho0*Gx*Mdefault", ('rho0', 'Gx', 'Mdefault')) ] for s, expected in string_tests: result, line_labels, occurrence_id, compilable_indices = self.std.parse_circuit_raw( s, lookup=lkup) self.assertEqual(line_labels, None) self.assertEqual(compilable_indices, None) circuit_result = Circuit(result, line_labels="auto", expand_subcircuits=True) #use "auto" line labels since none are parsed. self.assertEqual(circuit_result.tup, expected)
def test_label_rep_evalulation(self): """ Make sure Label reps evaluate back to the correct Label """ from pygsti.baseobjs import Label, CircuitLabel labels_to_test = [] l = Label(('Gx', 0)) labels_to_test.append(l) l = Label('Gx', (0, )) labels_to_test.append(l) l = Label('rho0') labels_to_test.append(l) l = Label((('Gx', 0), ('Gy', 1))) labels_to_test.append(l) l = Label(('Gx', 0, ';', 0.1)) labels_to_test.append(l) l = Label('Gx', (0, ), args=(0.1, )) labels_to_test.append(l) l = Label(('Gx', 0), time=1.0) labels_to_test.append(l) l = Label('Gx', (0, ), time=1.0) labels_to_test.append(l) l = Label('rho0', time=1.0) labels_to_test.append(l) l = Label((('Gx', 0), ('Gy', 1)), time=1.0) labels_to_test.append(l) l = Label(('Gx', 0, ';', 0.1, '!', 1.0)) labels_to_test.append(l) l = Label('Gx', (0, ), args=(0.1, ), time=1.0) labels_to_test.append(l) c = Circuit("[Gx:0Gy:1]^2[Gi]", line_labels=(0, 1)) l = c.to_label() labels_to_test.append(l) for l in labels_to_test: print(l, type(l).__name__, end='') r = repr(l) print(' => eval ' + r) evald_repr_l = eval(r) self.assertEqual(l, evald_repr_l)
def test_construct_with_nonstd_gate_unitary_factory(self): nQubits = 2 def fn(args): if args is None: args = (0,) a, = args sigmaZ = np.array([[1, 0], [0, -1]], 'd') return scipy.linalg.expm(1j * float(a) * sigmaZ) ps = QubitProcessorSpec(nQubits, ('Gx', 'Gy', 'Gcnot', 'Ga'), nonstd_gate_unitaries={'Ga': fn}) mdl = mc.create_crosstalk_free_model(ps) c = Circuit("Gx:1Ga;0.3:1Gx:1@(0,1)") p = mdl.probabilities(c) self.assertAlmostEqual(p['00'], 0.08733219254516078) self.assertAlmostEqual(p['01'], 0.9126678074548386) c2 = Circuit("Gx:1Ga;0.78539816:1Gx:1@(0,1)") # a clifford: 0.78539816 = pi/4 p2 = mdl.probabilities(c2) self.assertAlmostEqual(p2['00'], 0.5) self.assertAlmostEqual(p2['01'], 0.5)
def test_load_ignore_zero_count_lines4(self, pth): c1 = Circuit('Gc1') c2 = Circuit('Gc2') c3 = Circuit('Gc3') ds = DataSet() ds.add_count_dict(c1, {}, aux={'test': 1}) ds.add_count_dict(c2, {'0': 1}, aux={'test': 1}) ds.add_count_dict(c3, {}, aux={'test': 1}) #print(ds) io.write_dataset(pth, ds, fixed_column_mode=False) ds = io.read_dataset(pth, ignore_zero_count_lines=False) self.assertEqual(ds[c1]['0'], 0) self.assertEqual(ds[c2]['0'], 1) self.assertEqual( ds[c3]['0'], 0) # especially make sure last line is read in properly! self.assertEqual(ds[c1].aux['test'], 1) self.assertEqual(ds[c2].aux['test'], 1) self.assertEqual(ds[c3].aux['test'], 1)
def test_build_cloud_crosstalk_model_with_nonstd_gate_unitary_factory( self): nQubits = 2 def fn(args): if args is None: args = (0, ) a, = args sigmaZ = np.array([[1, 0], [0, -1]], 'd') return scipy.linalg.expm(1j * float(a) * sigmaZ) fn.udim = 2 fn.shape = (2, 2) pspec = _QubitProcessorSpec(nQubits, ('Gx', 'Gy', 'Gcnot', 'Ga'), nonstd_gate_unitaries={'Ga': fn}, geometry='line') ccmdl = mc.create_cloud_crosstalk_model(pspec) c = Circuit("Gx:1Ga;0.3:1Gx:1@(0,1)") p1 = ccmdl.probabilities(c) self.assertAlmostEqual(p1['00'], 0.08733219254516078) self.assertAlmostEqual(p1['01'], 0.9126678074548386)
def setUp(self): super(FiducialPairReductionStdData, self).setUp() self.model = fixtures.model self.preps = fixtures.fiducials self.effects = fixtures.fiducials self.germs = fixtures.germs self.fiducial_pairs = [(0, 0), (0, 1), (0, 2)] self.fiducial_pairs_per_germ = { Circuit(('Gi', )): [(0, 0), (1, 1), (2, 2)], Circuit(('Gx', )): [(0, 0), (0, 1), (0, 2), (2, 2)], Circuit(('Gy', )): [(0, 0), (0, 1), (0, 2), (1, 1)], Circuit(('Gx', 'Gy')): [(0, 0), (0, 1), (0, 2), (2, 0)], Circuit(('Gx', 'Gx', 'Gy')): [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)], Circuit(('Gx', 'Gy', 'Gy')): [(0, 0), (0, 1), (0, 2), (2, 0), (2, 1), (2, 2)], Circuit(('Gx', 'Gy', 'Gi')): [(0, 0), (0, 1), (0, 2), (2, 0)], Circuit(('Gx', 'Gi', 'Gy')): [(0, 0), (0, 1), (0, 2), (2, 0)], Circuit(('Gx', 'Gi', 'Gi')): [(0, 0), (0, 1), (0, 2), (2, 2)], Circuit(('Gy', 'Gi', 'Gi')): [(0, 0), (0, 1), (0, 2), (1, 1)], Circuit(('Gx', 'Gy', 'Gy', 'Gi')): [(0, 0), (0, 1), (0, 2), (2, 0), (2, 1), (2, 2)], Circuit(('Gx', 'Gx', 'Gy', 'Gx', 'Gy', 'Gy')): [(0, 0), (0, 1), (0, 2), (1, 0)] } #I think this alternate set is due to a slightly different search ordering - it # still looks like a correct output (there are many), so we'll call this OK for now: self.fiducial_pairs_per_germ_alt = { Circuit(('Gi', )): [(0, 0), (1, 1), (2, 2)], Circuit(('Gx', )): [(0, 0), (0, 1), (0, 2), (2, 0)], # 2,2 => 2,0 Circuit(('Gy', )): [(0, 0), (0, 1), (0, 2), (1, 0)], # 1,1 => 1,0 Circuit(('Gx', 'Gy')): [(0, 0), (0, 1), (0, 2), (2, 0)], Circuit(('Gx', 'Gx', 'Gy')): [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)], Circuit(('Gx', 'Gy', 'Gy')): [(0, 0), (0, 1), (0, 2), (2, 0), (2, 1), (2, 2)], Circuit(('Gx', 'Gy', 'Gi')): [(0, 0), (0, 1), (0, 2), (2, 0)], Circuit(('Gx', 'Gi', 'Gy')): [(0, 0), (0, 1), (0, 2), (2, 0)], Circuit(('Gx', 'Gi', 'Gi')): [(0, 0), (0, 1), (0, 2), (2, 0)], # 2,2 => 2,0 Circuit(('Gy', 'Gi', 'Gi')): [(0, 0), (0, 1), (0, 2), (1, 0)], # 1,1 => 1,0 Circuit(('Gx', 'Gy', 'Gy', 'Gi')): [(0, 0), (0, 1), (0, 2), (2, 0), (2, 1), (2, 2)], Circuit(('Gx', 'Gx', 'Gy', 'Gx', 'Gy', 'Gy')): [(0, 0), (0, 1), (0, 2), (1, 0)] }
def build_lists(self, fids1, fids2, germ): lists = ([ Circuit(fids1 % (germ + str(length))) for length in self.lengths ], [Circuit(fids2 % (germ + str(length))) for length in self.lengths]) return lists
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(CalcMethods1QTestCase, cls).setUpClass() #Change to test_packages directory (since setUp hasn't been called yet...) origDir = os.getcwd() os.chdir(os.path.abspath(os.path.dirname(__file__))) os.chdir('..') # The test_packages directory #Standard GST dataset cls.maxLengths = [1, 2, 4] cls.mdl_datagen = std.target_model().depolarize(op_noise=0.03, spam_noise=0.001) cls.listOfExperiments = pygsti.circuits.create_lsgst_circuits( std.target_model(), std.prep_fiducials(), std.meas_fiducials(), std.germs(), cls.maxLengths) #RUN BELOW FOR DATAGEN (SAVE) if regenerate_references(): ds = pygsti.data.simulate_data(cls.mdl_datagen, cls.listOfExperiments, num_samples=1000, sample_error="multinomial", seed=1234) ds.save(compare_files + "/calcMethods1Q.dataset") #DEBUG TEST- was to make sure data files have same info -- seemed ultimately unnecessary #ds_swp = pygsti.objects.DataSet(file_to_load_from=compare_files + "/calcMethods1Q.datasetv3") # run in Python3 #pygsti.io.write_dataset(temp_files + "/dataset.3to2.txt", ds_swp) # run in Python3 #ds_swp = pygsti.io.read_dataset(temp_files + "/dataset.3to2.txt") # run in Python2 #ds_swp.save(compare_files + "/calcMethods1Q.dataset") # run in Python2 #assert(False),"STOP" cls.ds = pygsti.data.DataSet(file_to_load_from=compare_files + "/calcMethods1Q.dataset") #Reduced model GST dataset cls.nQubits = 1 # can't just change this now - see op_labels below cls.mdl_redmod_datagen = build_XYCNOT_cloudnoise_model( cls.nQubits, geometry="line", maxIdleWeight=1, maxhops=1, extraWeight1Hops=0, extraGateWeight=1, simulator="matrix", verbosity=1, roughNoise=(1234, 0.01)) #Create a reduced set of fiducials and germs op_labels = [L('Gx', 0), L('Gy', 0)] # 1Q gate labels fids1Q = std1Q_XY.fiducials[ 1:2] # for speed, just take 1 non-empty fiducial cls.redmod_fiducials = [ Circuit([], line_labels=(0, )) ] # special case for empty fiducial (need to change line label) for i in range(cls.nQubits): cls.redmod_fiducials.extend( pygsti.circuits.manipulate_circuits(fids1Q, [((L('Gx'), ), (L('Gx', i), )), ((L('Gy'), ), (L('Gy', i), ))])) #print(redmod_fiducials, "Fiducials") cls.redmod_germs = pygsti.circuits.to_circuits([(gl, ) for gl in op_labels]) cls.redmod_maxLs = [1] expList = pygsti.circuits.create_lsgst_circuits( op_labels, cls.redmod_fiducials, cls.redmod_fiducials, cls.redmod_germs, cls.redmod_maxLs) #RUN BELOW FOR DATAGEN (SAVE) if regenerate_references(): redmod_ds = pygsti.data.simulate_data(cls.mdl_redmod_datagen, expList, 1000, "round", seed=1234) redmod_ds.save(compare_files + "/calcMethods1Q_redmod.dataset") cls.redmod_ds = pygsti.data.DataSet(file_to_load_from=compare_files + "/calcMethods1Q_redmod.dataset") #print(len(expList)," reduced model sequences") #Random starting points - little kick so we don't get hung up at start np.random.seed(1234) cls.rand_start18 = np.random.random(18) * 1e-6 cls.rand_start25 = np.random.random(30) * 1e-6 # TODO: rename? cls.rand_start36 = np.random.random(30) * 1e-6 # TODO: rename? #Circuit Simulation circuits cls.csim_nQubits = 3 cls.circuit1 = pygsti.circuits.Circuit((('Gxpi2', 0), ('Gypi2', 0))) # now Circuit adds qubit labels... pygsti.circuits.Circuit(layer_labels=('Gx','Gy'), num_lines=1) # 1-qubit circuit cls.circuit3 = pygsti.circuits.Circuit(layer_labels=[('Gxpi', 0), ('Gypi', 1), ('Gcnot', 1, 2)], num_lines=3) # 3-qubit circuit os.chdir(origDir) # return to original directory
"""Shared test fixtures for pygsti.objects unit tests""" import pygsti from pygsti.modelpacks import smq1Q_XYI as smq from pygsti.baseobjs import Label from pygsti.circuits import Circuit, CircuitList from ..util import Namespace ns = Namespace() ns.model = smq.target_model('full TP') ns.max_max_length = 2 ns.aliases = {Label(('GA1', 0)): Circuit([('Gxpi2', 0)])} @ns.memo def datagen_model(self): return self.model.depolarize(op_noise=0.05, spam_noise=0.1) @ns.memo def circuits(self): return smq.create_gst_circuits(max_max_length=self.max_max_length) @ns.memo def dataset(self): return pygsti.data.simulate_data( self.datagen_model, self.circuits, 1000, seed=2020) @ns.memo def sparse_dataset(self):
def test_3Q(self): ##only test when reps are fast (b/c otherwise this test is slow!) #try: from pygsti.objects.replib import fastreplib #except ImportError: # warnings.warn("Skipping test_3Q b/c no fastreps!") # return nQubits = 3 print("Constructing Target LinearOperator Set") target_model = build_XYCNOT_cloudnoise_model(nQubits, geometry="line", maxIdleWeight=1, maxhops=1, extraWeight1Hops=0, extraGateWeight=1, simulator="map", verbosity=1) #print("nElements test = ",target_model.num_elements) #print("nParams test = ",target_model.num_params) #print("nNonGaugeParams test = ",target_model.num_nongauge_params) print("Constructing Datagen LinearOperator Set") mdl_datagen = build_XYCNOT_cloudnoise_model(nQubits, geometry="line", maxIdleWeight=1, maxhops=1, extraWeight1Hops=0, extraGateWeight=1, verbosity=1, roughNoise=(1234, 0.1), simulator="map") mdl_test = mdl_datagen print( "Constructed model with %d op-blks, dim=%d, and nParams=%d. Norm(paramvec) = %g" % (len(mdl_test.operation_blks), mdl_test.dim, mdl_test.num_params, np.linalg.norm(mdl_test.to_vector()))) op_labels = target_model.primitive_op_labels line_labels = tuple(range(nQubits)) fids1Q = std1Q_XY.fiducials fiducials = [] for i in range(nQubits): fiducials.extend( pygsti.circuits.manipulate_circuits(fids1Q, [((L('Gx'), ), (L('Gx', i), )), ((L('Gy'), ), (L('Gy', i), ))], line_labels=line_labels)) print(len(fiducials), "Fiducials") prep_fiducials = meas_fiducials = fiducials #TODO: add fiducials for 2Q pairs (edges on graph) germs = pygsti.circuits.to_circuits([(gl, ) for gl in op_labels], line_labels=line_labels) maxLs = [1] expList = pygsti.circuits.create_lsgst_circuits( mdl_datagen, prep_fiducials, meas_fiducials, germs, maxLs) self.assertTrue(Circuit((), line_labels) in expList) ds = pygsti.data.simulate_data(mdl_datagen, expList, 1000, "multinomial", seed=1234) print("Created Dataset with %d strings" % len(ds)) logL = pygsti.tools.logl(mdl_datagen, ds, expList) max_logL = pygsti.tools.logl_max(mdl_datagen, ds, expList) twoDeltaLogL = 2 * (max_logL - logL) chi2 = pygsti.tools.chi2(mdl_datagen, ds, expList) dof = ds.degrees_of_freedom() nParams = mdl_datagen.num_params print("Datagen 2DeltaLogL = 2(%g-%g) = %g" % (logL, max_logL, twoDeltaLogL)) print("Datagen chi2 = ", chi2) print("Datagen expected DOF = ", dof) print("nParams = ", nParams) print("Expected 2DeltaLogL or chi2 ~= %g-%g =%g" % (dof, nParams, dof - nParams)) #print("EXIT"); exit() return results = pygsti.run_long_sequence_gst( ds, target_model, prep_fiducials, meas_fiducials, germs, maxLs, verbosity=5, advanced_options={ 'max_iterations': 2 }) #keep this short; don't care if it doesn't converge. print("DONE!")
import pickle from collections import OrderedDict import numpy as np from pygsti.circuits import Circuit from pygsti.data import MultiDataSet, DataSet from ..util import BaseCase # module-level test fixtures gstrInds = OrderedDict([(Circuit(('Gx', )), slice(0, 2)), (Circuit(('Gx', 'Gy')), slice(2, 4)), (Circuit(('Gy', )), slice(4, 6))]) olInds = OrderedDict([('0', 0), ('1', 1)]) ds1_oli = np.array([0, 1] * 3, 'i') # 3 operation sequences * 2 outcome labels ds1_time = np.zeros(6, 'd') ds1_rep = 10 * np.ones(6, 'i') ds2_oli = np.array([0, 1] * 3, 'i') # 3 operation sequences * 2 outcome labels ds2_time = np.zeros(6, 'd') ds2_rep = 5 * np.ones(6, 'i') mds_oli = OrderedDict([('ds1', ds1_oli), ('ds2', ds2_oli)]) mds_time = OrderedDict([('ds1', ds1_time), ('ds2', ds2_time)]) mds_rep = OrderedDict([('ds1', ds1_rep), ('ds2', ds2_rep)]) class MultiDataSetTester(BaseCase): def test_construct_with_outcome_label_indices(self): mds = MultiDataSet(mds_oli,
def test_lsgst_lists_structs(self): maxLens = [1, 2] lsgstLists = gstcircuits.create_lsgst_circuit_lists( std1Q_XY.target_model(), self.strs, self.strs, self.germs, maxLens, fid_pairs=None, trunc_scheme="whole germ powers") # also try a Model as first arg self.assertEqual( lsgstLists[-1][26]._str, 'GxGx(Gx)^2GxGx' ) # ensure that (.)^2 appears in string (*not* expanded) lsgstLists2 = gstcircuits.create_lsgst_circuit_lists( self.opLabels, self.strs, self.strs, self.germs, maxLens, fid_pairs=None, trunc_scheme="truncated germ powers") self.assertEqual(set(lsgstLists[-1]), set(lsgstLists2[-1])) lsgstLists3 = gstcircuits.create_lsgst_circuit_lists( self.opLabels, self.strs, self.strs, self.germs, maxLens, fid_pairs=None, trunc_scheme="length as exponent") lsgstStructs3 = gstcircuits.make_lsgst_structs( self.opLabels, self.strs, self.strs, self.germs, maxLens, fid_pairs=None, trunc_scheme="length as exponent") self.assertEqual(set(lsgstLists3[-1]), set(lsgstStructs3[-1])) maxLens = [1, 2] lsgstLists4 = gstcircuits.create_lsgst_circuit_lists( self.opLabels, self.strs, self.strs, self.germs, maxLens, fid_pairs=None, trunc_scheme="whole germ powers", nest=False) lsgstStructs4 = gstcircuits.make_lsgst_structs( self.opLabels, self.strs, self.strs, self.germs, maxLens, fid_pairs=None, trunc_scheme="whole germ powers", nest=False) self.assertEqual(set(lsgstLists4[-1]), set(lsgstStructs4[-1])) lsgstLists5 = gstcircuits.create_lsgst_circuit_lists( self.opLabels, self.strs, self.strs, self.germs, maxLens, fid_pairs=self.testFidPairs, trunc_scheme="whole germ powers") lsgstStructs5 = gstcircuits.make_lsgst_structs( self.opLabels, self.strs, self.strs, self.germs, maxLens, fid_pairs=self.testFidPairs, trunc_scheme="whole germ powers") self.assertEqual(set(lsgstLists5[-1]), set(lsgstStructs5[-1])) lsgstLists6 = gstcircuits.create_lsgst_circuit_lists( self.opLabels, self.strs, self.strs, self.germs, maxLens, fid_pairs=self.testFidPairsDict, trunc_scheme="whole germ powers") lsgstStructs6 = gstcircuits.make_lsgst_structs( self.opLabels, self.strs, self.strs, self.germs, maxLens, fid_pairs=self.testFidPairsDict, trunc_scheme="whole germ powers") self.assertEqual(set(lsgstLists6[-1]), set(lsgstStructs6[-1])) lsgstLists7 = gstcircuits.create_lsgst_circuit_lists( self.opLabels, self.strs, self.strs, self.germs, maxLens, fid_pairs=None, trunc_scheme="whole germ powers", keep_fraction=0.5, keep_seed=1234) lsgstStructs7 = gstcircuits.make_lsgst_structs( self.opLabels, self.strs, self.strs, self.germs, maxLens, fid_pairs=None, trunc_scheme="whole germ powers", keep_fraction=0.5, keep_seed=1234) self.assertEqual(set(lsgstLists7[-1]), set(lsgstStructs7[-1])) lsgstLists8 = gstcircuits.create_lsgst_circuit_lists( self.opLabels, self.strs, self.strs, self.germs, maxLens, fid_pairs=self.testFidPairs, trunc_scheme="whole germ powers", keep_fraction=0.7, keep_seed=1234) lsgstStructs8 = gstcircuits.make_lsgst_structs( self.opLabels, self.strs, self.strs, self.germs, maxLens, fid_pairs=self.testFidPairs, trunc_scheme="whole germ powers", keep_fraction=0.7, keep_seed=1234) self.assertEqual(set(lsgstLists8[-1]), set(lsgstStructs8[-1])) # empty max-lengths ==> no output lsgstStructs9 = gstcircuits.make_lsgst_structs(self.opLabels, self.strs, self.strs, self.germs, [], include_lgst=False) self.assertEqual(len(lsgstStructs9), 0) # checks against data lgst_strings = cc.create_lgst_circuits(self.strs, self.strs, self.opLabels) lsgstStructs10 = gstcircuits.make_lsgst_structs( self.opLabels, self.strs, self.strs, self.germs, maxLens, dscheck=self.ds, action_if_missing="drop", verbosity=4) self.assertEqual([Circuit(('Gx', ))], list(lsgstStructs10[-1]))
fixture_2Q.clifford_abs = CliffordCompilationRules.create_standard( fixture_2Q.pspec, compile_type="absolute", what_to_compile=("1Qcliffords", "paulis"), verbosity=1) fixture_2Q.clifford_peq = CliffordCompilationRules.create_standard( fixture_2Q.pspec, compile_type="paulieq", what_to_compile=("1Qcliffords", "allcnots"), verbosity=1) # Totally arbitrary CNOT circuit fixture_2Q.cnot_circuit = Circuit(layer_labels=[ Label('CNOT', ('Q1', 'Q0')), Label('CNOT', ('Q1', 'Q0')), Label('CNOT', ('Q0', 'Q1')), Label('CNOT', ('Q1', 'Q0')) ], line_labels=fixture_2Q.qubit_labels) fixture_2Q.cnot_circuit_sym, fixture_2Q.cnot_circuit_phase = \ symplectic.symplectic_rep_of_clifford_circuit(fixture_2Q.cnot_circuit) fixture_3Q = Namespace( n=3, qubit_labels=['Q0', 'Q1', 'Q2'], availability={'Gcnot': [('Q0', 'Q1'), ('Q1', 'Q2')]}, gate_names=['Gh', 'Gp', 'Gxpi', 'Gpdag', 'Gcnot'], # generated as before: clifford_sym=np.array([[0, 0, 1, 1, 0, 1], [0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 1, 1], [1, 0, 1, 1, 1, 1], [0, 1, 0, 1, 1, 0], [0, 0, 1, 0, 0, 1]]),