def test_projection_error(self): coefficient = 0.5 opstring = ((0, 'X'), (1, 'X'), (2, 'Z')) opstring2 = ((0, 'X'), (2, 'Z'), (3, 'Z')) operator = QubitOperator(opstring, coefficient) operator += QubitOperator(opstring2, coefficient) new_operator = project_onto_sector(operator, qubits=[1], sectors=[0]) error = projection_error(operator, qubits=[1], sectors=[0]) self.assertEqual(count_qubits(new_operator), 3) self.assertTrue(((0, 'X'), (1, 'Z'), (2, 'Z')) in new_operator.terms) self.assertEqual(new_operator.terms[((0, 'X'), (1, 'Z'), (2, 'Z'))], 0.5) self.assertEqual(error, 0.5)
def test_function_errors(self): """Test main function errors.""" operator = (QubitOperator('Z0 X1', 1.0) + QubitOperator('X1', 2.0)) sector1 = [0] sector2 = [1] qbt_list = [0] with self.assertRaises(TypeError): project_onto_sector(operator=1.0, qubits=qbt_list, sectors=sector1) with self.assertRaises(TypeError): projection_error(operator=1.0, qubits=qbt_list, sectors=sector1) with self.assertRaises(TypeError): project_onto_sector(operator=operator, qubits=0.0, sectors=sector2) with self.assertRaises(TypeError): projection_error(operator=operator, qubits=0.0, sectors=sector2) with self.assertRaises(TypeError): project_onto_sector(operator=operator, qubits=qbt_list, sectors=operator) with self.assertRaises(TypeError): projection_error(operator=operator, qubits=qbt_list, sectors=operator) with self.assertRaises(ValueError): project_onto_sector(operator=operator, qubits=[ 0, 1], sectors=sector1) with self.assertRaises(ValueError): projection_error(operator=operator, qubits=[0, 1], sectors=sector1) with self.assertRaises(ValueError): project_onto_sector(operator=operator, qubits=qbt_list, sectors=[0, 0]) with self.assertRaises(ValueError): projection_error(operator=operator, qubits=qbt_list, sectors=[0, 0]) with self.assertRaises(ValueError): project_onto_sector(operator=operator, qubits=qbt_list, sectors=[-1]) with self.assertRaises(ValueError): projection_error(operator=operator, qubits=qbt_list, sectors=[-1])