def test_construct_from_oplabels(self): # Now repeat the read-in with no parallelize, but a list of lists of oplabels labels = [[Label('Gi', 'Q0'), Label('Gp', 'Q8')], [Label('Gh', 'Q1'), Label('Gp', 'Q12')]] c = circuit.Circuit(layer_labels=labels, line_labels=['Q0', 'Q1', 'Q8', 'Q12']) self.assertLess(0, c.depth())
def test_insert_labels_into_layers_with_nonidle_qubits(self): # Test inserting a gate when the relevant qubits aren't # idling at that layer self.c.insert_labels_into_layers([Label('Gx', 'Q0')], 2) self.assertEqual(self.c.size(), 9) self.assertEqual(self.c.depth(), 6) self.assertEqual(self.c[2, 'Q0'], Label('Gx', 'Q0'))
def test_construction_label_conversion(self): # XXX what is tested here that is not covered by other tests? EGN: this is more of a use case for when this input is a *nested* tuple. # Check that parallel operation labels get converted to circuits properly opstr = circuit.Circuit(((('Gx', 0), ('Gy', 1)), ('Gcnot', 0, 1))) c = circuit.Circuit(layer_labels=opstr, num_lines=2) self.assertEqual(c._labels, (Label( (('Gx', 0), ('Gy', 1))), Label('Gcnot', (0, 1))))
def test_insert_labels_into_layers_with_idle_qubits(self): # Test inserting a gate when the relevant qubits are # idling at that layer -- depth shouldn't increase self.c[2, 'Q1'] = Label('Gx', 'Q1') self.assertEqual(self.c.size(), 8) self.assertEqual(self.c.depth(), 5) self.assertEqual(self.c[2, 'Q1'], Label('Gx', 'Q1'))
def test_construct_from_label_parallelized(self): # Do again with parallelization labels = [Label('Gi', 'Q0'), Label('Gp', 'Q8')] c = circuit.Circuit(layer_labels=labels, line_labels=['Q0', 'Q1', 'Q8']) c = c.parallelize() self.assertEqual(c.depth(), 1) self.assertEqual(c.size(), 2)
def test_to_pythonstr(self): mdl = circuit.Circuit(None, stringrep="Gx^3Gy^2GxGz") op_labels = (Label('Gx'), Label('Gy'), Label('Gz')) pystr = mdl.to_pythonstr(op_labels) self.assertEqual(pystr, "AAABBAC") gs2_tup = circuit.Circuit.from_pythonstr(pystr, op_labels) self.assertEqual(gs2_tup, tuple(mdl))
def test_convert_to_quil(self): # Check that convert_to_quil runs, doesn't check the output makes sense. labels = [ Label(('Gi', 'Q1')), Label(('Gxpi', 'Q1')), Label('Gcnot', ('Q1', 'Q2')) ] c = circuit.Circuit(layer_labels=labels, line_labels=['Q1', 'Q2']) s = c.convert_to_quil()
def test_change_gate_library_missing_gates(self): # Change gate library using a dict with some gates missing comp = {} comp[Label('Gz', 'Q0')] = circuit.Circuit(layer_labels=[Label('Gx', 'Q0')], line_labels=['Q0']) self.c.change_gate_library(comp, allow_unchanged_gates=True) self.assertTrue(Label('Gx', 'Q0') in self.c[0].components) # c.get_layer(0) self.assertTrue(Label('Gy', 'Q1') in self.c[0].components)
def test_construct_from_label(self): # Test initializing a circuit from a non-empty circuit that is a list # containing Label objects. Also test that it can have non-integer line_labels # and a different identity identifier. labels = [Label('Gi', 'Q0'), Label('Gp', 'Q8')] c = circuit.Circuit(layer_labels=labels, line_labels=['Q0', 'Q1', 'Q8', 'Q12']) # Not parallelized by default, so will be depth 2. self.assertEqual(c.depth(), 2) self.assertEqual(c.size(), 2) self.assertEqual(c.number_of_lines(), 4) self.assertEqual(c.line_labels, ('Q0', 'Q1', 'Q8', 'Q12'))
def test_predicted_error_probability(self): # Test the error-probability prediction method labels = circuit.Circuit(None, stringrep="[Gx:Q0][Gi:Q0Gi:Q1]") c = circuit.Circuit(layer_labels=labels, line_labels=['Q0', 'Q1']) infidelity_dict = {} infidelity_dict[Label('Gi', 'Q0')] = 0.7 infidelity_dict[Label('Gi', 'Q1')] = 0.9 infidelity_dict[Label('Gx', 'Q0')] = 0.8 infidelity_dict[Label('Gx', 'Q2')] = 0.9 # TODO fix epsilon = c.predicted_error_probability(infidelity_dict) self.assertLess( abs(epsilon - (1 - (1 - 0.7) * (1 - 0.8) * (1 - 0.9)**2)), 10**-10)
def test_do_mlgst_alias_model(self): aliased_list = [ Circuit([(x if x != Label("Gx") else Label("GA1")) for x in mdl]) for mdl in self.lsgstStrings[0] ] aliased_model = self.mdl_clgst.copy() aliased_model.operations['GA1'] = self.mdl_clgst.operations['Gx'] aliased_model.operations.pop('Gx') model = core.do_mlgst(self.ds, aliased_model, aliased_list, minProbClip=1e-4, probClipInterval=(-1e6, 1e6), opLabelAliases={Label('GA1'): Circuit(['Gx'])})
def test_circuit_layer_by_co2Qgates(self): self.skipTest("RB analysis is known to be broken. Skip tests until it gets fixed.") n = self.pspec_1.number_of_qubits C01 = Label('Gcnot', ('Q0', 'Q1')) C23 = Label('Gcnot', ('Q2', 'Q3')) co2Qgates = [[], [C01, C23]] layer = sample.circuit_layer_by_co2Qgates( self.pspec_1, None, co2Qgates, co2Qgatesprob='uniform', twoQprob=1.0, oneQgatenames='all', modelname='clifford' ) self.assertTrue(len(layer) == n or len(layer) == n // 2) layer = sample.circuit_layer_by_co2Qgates( self.pspec_1, None, co2Qgates, co2Qgatesprob=[0., 1.], twoQprob=1.0, oneQgatenames='all', modelname='clifford' ) self.assertEqual(len(layer), n // 2) layer = sample.circuit_layer_by_co2Qgates( self.pspec_1, None, co2Qgates, co2Qgatesprob=[1., 0.], twoQprob=1.0, oneQgatenames=['Gx'], modelname='clifford' ) self.assertEqual(len(layer), n) self.assertEqual(layer[0].name, 'Gx') co2Qgates = [[], [C23]] layer = sample.circuit_layer_by_co2Qgates( self.pspec_1, ['Q2', 'Q3'], co2Qgates, co2Qgatesprob=[0.25, 0.75], twoQprob=0.5, oneQgatenames='all', modelname='clifford' ) # TODO assert correctness co2Qgates = [[C01]] layer = sample.circuit_layer_by_co2Qgates( self.pspec_1, None, co2Qgates, co2Qgatesprob=[1.], twoQprob=1.0, oneQgatenames='all', modelname='clifford' ) self.assertEqual(layer[0].name, 'Gcnot') self.assertEqual(len(layer), 3) # Tests the nested co2Qgates option. co2Qgates = [[], [[C01, C23], [C01]]] layer = sample.circuit_layer_by_co2Qgates( self.pspec_1, None, co2Qgates, co2Qgatesprob='uniform', twoQprob=1.0, oneQgatenames='all', modelname='clifford' )
def test_direct_rb_experiment(self): self.skipTest("RB analysis is known to be broken. Skip tests until it gets fixed.") lengths = [0, 2, 5] circuits_per_length = 2 # Test DRB experiment with all defaults. exp = sample.direct_rb_experiment(self.pspec_2, lengths, circuits_per_length, verbosity=0) # TODO assert correctness exp = sample.direct_rb_experiment( self.pspec_2, lengths, circuits_per_length, subsetQs=[0, 1], sampler='pairingQs', cliffordtwirl=False, conditionaltwirl=False, citerations=2, partitioned=True, verbosity=0 ) # TODO assert correctness exp = sample.direct_rb_experiment( self.pspec_2, lengths, circuits_per_length, subsetQs=[0, 1], sampler='co2Qgates', samplerargs=[[[], [Label('Gcphase', (0, 1)), ]], [0., 1.]], cliffordtwirl=False, conditionaltwirl=False, citerations=2, partitioned=True, verbosity=0 ) # TODO assert correctness exp = sample.direct_rb_experiment( self.pspec_2, lengths, circuits_per_length, subsetQs=[0, 1], sampler='local', cliffordtwirl=False, conditionaltwirl=False, citerations=2, partitioned=True, verbosity=0 )
def test_simulate(self): # TODO optimize # Create a pspec, to test the circuit simulator. n = 4 qubit_labels = ['Q' + str(i) for i in range(n)] gate_names = ['Gh', 'Gp', 'Gxpi', 'Gpdag', 'Gcnot'] # 'Gi', ps = ProcessorSpec(n, gate_names=gate_names, qubit_labels=qubit_labels) # Tests the circuit simulator c = circuit.Circuit( layer_labels=[Label('Gh', 'Q0'), Label('Gcnot', ('Q0', 'Q1'))], line_labels=['Q0', 'Q1']) out = c.simulate(ps.models['target']) self.assertLess(abs(out['00'] - 0.5), 10**-10) self.assertLess(abs(out['11'] - 0.5), 10**-10)
def test_prefix_circuit(self): c2 = circuit.Circuit(layer_labels=[Label('Gx', 'Q0')], line_labels=[ 'Q0', ], editable=True) self.c.prefix_circuit(c2)
def test_append_circuit(self): # Test appending c2 = circuit.Circuit(layer_labels=[Label('Gx', 'Q0')], line_labels=[ 'Q0', ], editable=True) self.c.append_circuit(c2)
def test_insert_layer(self): # Test layer insertion layer = [ Label('Gx', 'Q1'), ] self.c.insert_layer(layer, 1) self.assertEqual(self.c.size(), 9) self.assertEqual(self.c.depth(), 6) self.assertEqual(self.c[1], Label('Gx', 'Q1')) self.c.insert_layer([], 1) self.assertTrue(len(self.c[1, ('Q0', 'Q1')].components) == 0) self.assertTrue(len(self.c[1, 'Q0'].components) == 0) self.assertFalse(len(self.c[2, 'Q1'].components) == 0) self.assertFalse(self.c.is_line_idling('Q1')) self.c.append_idling_lines(['Q3']) self.assertFalse(self.c.is_line_idling('Q0')) self.assertFalse(self.c.is_line_idling('Q1')) self.assertTrue(self.c.is_line_idling('Q3'))
def test_do_iterative_mlgst_circuit_weights_dict(self): model = core.do_iterative_mlgst(self.ds, self.mdl_clgst, self.lsgstStrings, minProbClip=1e-4, probClipInterval=(-1e2, 1e2), circuitWeightsDict={ (Label('Gx'), ): 2.0 })
def test_insert_circuit_with_qubit_subset(self): # Test inserting a circuit that is on *less* qubits. c2 = circuit.Circuit(layer_labels=[Label('Gx', 'Q0')], line_labels=[ 'Q0', ]) self.c.insert_circuit(c2, 1) self.assertEqual(self.c.line_labels, ('Q0', 'Q1')) self.assertEqual(self.c.number_of_lines(), 2)
def test_delete_layers(self): # Test layer deletion layer = [ Label('Gx', 'Q1'), ] c_copy = self.c.copy() c_copy.insert_layer(layer, 1) c_copy.delete_layers([1]) self.assertEqual(self.c, c_copy)
def test_random_circuit(self): self.skipTest("RB analysis is known to be broken. Skip tests until it gets fixed.") C01 = Label('Gcnot', ('Q0', 'Q1')) C23 = Label('Gcnot', ('Q2', 'Q3')) co2Qgates = [[], [[C01, C23], [C01, ]]] circuit = sample.random_circuit(self.pspec_1, length=100, sampler='Qelimination') self.assertEqual(circuit.depth(), 100) circuit = sample.random_circuit(self.pspec_2, length=100, sampler='Qelimination', samplerargs=[0.1, ], addlocal=True) self.assertEqual(circuit.depth(), 201) self.assertLessEqual(len(circuit.get_layer(0)), self.pspec_2.number_of_qubits) circuit = sample.random_circuit(self.pspec_1, length=100, sampler='pairingQs') # TODO assert correctness circuit = sample.random_circuit( self.pspec_1, length=10, sampler='pairingQs', samplerargs=[0.1, ['Gx', ]] ) # TODO assert correctness circuit = sample.random_circuit(self.pspec_1, length=100, sampler='co2Qgates', samplerargs=[co2Qgates]) # TODO assert correctness circuit = sample.random_circuit( self.pspec_1, length=100, sampler='co2Qgates', samplerargs=[co2Qgates, [0.1, 0.2], 0.1], addlocal=True, lsargs=[['Gx', ]] ) self.assertEqual(circuit.depth(), 201) circuit = sample.random_circuit(self.pspec_1, length=5, sampler='local') self.assertEqual(circuit.depth(), 5) circuit = sample.random_circuit(self.pspec_1, length=5, sampler='local', samplerargs=[['Gx']]) self.assertEqual(circuit[0, 'Q0'].name, 'Gx')
def test_translate_circuit_list(self): orig_list = cc.circuit_list([('Gx', 'Gx'), ('Gx', 'Gy'), ('Gx', 'Gx', 'Gx'), ('Gy', 'Gy'), ('Gi', )]) list0 = cc.translate_circuit_list(orig_list, None) self.assertEqual(list0, orig_list) list1 = cc.translate_circuit_list(orig_list, { Label('Gx'): (Label('Gx2'), ), Label('Gy'): (Label('Gy'), ) }) expected_list1 = cc.circuit_list([('Gx2', 'Gx2'), ('Gx2', 'Gy'), ('Gx2', 'Gx2', 'Gx2'), ('Gy', 'Gy'), ('Gi', )]) self.assertEqual(list1, expected_list1) list2 = cc.translate_circuit_list(orig_list, { Label('Gi'): (Label('Gx'), Label('Gx'), Label('Gx'), Label('Gx')) }) expected_list2 = cc.circuit_list([('Gx', 'Gx'), ('Gx', 'Gy'), ('Gx', 'Gx', 'Gx'), ('Gy', 'Gy'), ('Gx', 'Gx', 'Gx', 'Gx')]) self.assertEqual(list2, expected_list2)
def setUp(self): self.opLabels = [Label('Gx'), Label('Gy')] self.strs = cc.circuit_list([('Gx', ), ('Gy', ), ('Gx', 'Gx')]) self.germs = cc.circuit_list([('Gx', 'Gy'), ('Gy', 'Gy')]) self.testFidPairs = [(0, 1)] self.testFidPairsDict = { (Label('Gx'), Label('Gy')): [(0, 0), (0, 1)], (Label('Gy'), Label('Gy')): [(0, 0)] } self.ds = DataSet(outcomeLabels=['0', '1']) # a dataset that is missing self.ds.add_count_dict(('Gx', ), { '0': 10, '1': 90 }) # almost all our strings... self.ds.done_adding_data()
def test_circuit_layer_of_oneQgates(self): self.skipTest("RB analysis is known to be broken. Skip tests until it gets fixed.") layer = sample.circuit_layer_of_oneQgates( self.pspec_1, oneQgatenames='all', pdist='uniform', modelname='clifford' ) self.assertEqual(len(layer), self.pspec_1.number_of_qubits) layer = sample.circuit_layer_of_oneQgates( self.pspec_1, subsetQs=['Q1', 'Q2'], oneQgatenames=['Gx', 'Gy'], pdist=[1., 0.], modelname='clifford' ) self.assertEqual(len(layer), 2) self.assertEqual(layer[0].name, 'Gx') layer = sample.circuit_layer_of_oneQgates( self.pspec_1, subsetQs=['Q2'], oneQgatenames=['Gx'], pdist=[3.], modelname='clifford' ) self.assertEqual(layer[0], Label('Gx', 'Q2')) self.assertEqual(len(layer), 1) layer = sample.circuit_layer_of_oneQgates( self.pspec_1, oneQgatenames=['Gx'], pdist='uniform', modelname='clifford' )
def test_compress_depth(self): ls = [ Label('H', 1), Label('P', 1), Label('P', 1), Label(()), Label('CNOT', (2, 3)) ] ls += [Label('HP', 1), Label('PH', 1), Label('CNOT', (1, 2))] ls += [Label(()), Label(()), Label('CNOT', (1, 2))] labels = circuit.Circuit(ls) c = circuit.Circuit(layer_labels=labels, num_lines=4, editable=True) c.compress_depth(verbosity=0) self.assertEqual(c.depth(), 7) # Get a dictionary that relates H, P gates etc. oneQrelations = symplectic.oneQclifford_symplectic_group_relations() c.compress_depth(oneQgate_relations=oneQrelations) self.assertEqual(c.depth(), 3)
def test_change_gate_library(self): # Change gate library using an ordinary dict with every gate as a key. (we test # changing gate library using a CompilationLibrary elsewhere in the tests). labels = circuit.Circuit( None, stringrep="[Gz:Q0Gy:Q1][Gy:Q0Gz:Q1]Gz:Q0Gi:Q1") c = circuit.Circuit(layer_labels=labels, line_labels=['Q0', 'Q1'], editable=True) comp = {} comp[Label('Gz', 'Q0')] = circuit.Circuit(layer_labels=[Label('Gx', 'Q0')], line_labels=['Q0']) comp[Label('Gy', 'Q0')] = circuit.Circuit(layer_labels=[Label('Gx', 'Q0')], line_labels=['Q0']) comp[Label('Gz', 'Q1')] = circuit.Circuit(layer_labels=[Label('Gx', 'Q1')], line_labels=['Q1']) comp[Label('Gy', 'Q1')] = circuit.Circuit(layer_labels=[Label('Gx', 'Q1')], line_labels=['Q1']) comp[Label('Gi', 'Q1')] = circuit.Circuit(layer_labels=[Label('Gi', 'Q1')], line_labels=['Q1']) c.change_gate_library(comp) self.assertTrue(Label('Gx', 'Q0') in c[0].components)
def test_insert_circuit_label_collision(self): # Test inserting a circuit when they are over the same labels. c_copy = self.c.copy() self.c.insert_circuit(c_copy, 2) self.assertTrue(Label('Gx', 'Q0') in self.c[2].components)
def test_empty_tuple_makes_idle_layer(self): c = circuit.Circuit(['Gi', Label(())]) self.assertEqual(len(c), 2)
def test_replace_layer_with_layer(self): # Test replacing a layer with a layer. newlayer = [Label('Gx', 'Q0')] self.c[1] = newlayer self.assertEqual(self.c.depth(), 5)
fixture_2Q = Namespace( n=2, qubit_labels=['Q0', 'Q1'], availability={'Gcnot': [('Q0', 'Q1')]}, gate_names=['Gh', 'Gp', 'Gxpi', 'Gpdag', 'Gcnot'], # generated as before: clifford_sym=np.array([[0, 1, 1, 1], [1, 0, 1, 1], [1, 0, 1, 0], [0, 1, 0, 1]]), clifford_phase=np.array([2, 0, 1, 3])) fixture_2Q.pspec = ProcessorSpec(fixture_2Q.n, gate_names=fixture_2Q.gate_names, availability=fixture_2Q.availability, qubit_labels=fixture_2Q.qubit_labels) # 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],