def test_reduce_terms(self): """Test reduce_terms function using LiH Hamiltonian.""" hamiltonian, spectrum = lih_hamiltonian() qubit_hamiltonian = jordan_wigner(hamiltonian) stab1 = QubitOperator('Z0 Z2', -1.0) stab2 = QubitOperator('Z1 Z3', -1.0) red_eigenspectrum = eigenspectrum( reduce_number_of_terms(qubit_hamiltonian, stab1 + stab2)) self.assertAlmostEqual(spectrum[0], red_eigenspectrum[0])
def test_reduce_terms_manual_input(self): """Test reduce_terms function using LiH Hamiltonian.""" hamiltonian, spectrum = lih_hamiltonian() qubit_hamiltonian = jordan_wigner(hamiltonian) stab1 = QubitOperator('Z0 Z2', -1.0) stab2 = QubitOperator('Z1 Z3', -1.0) red_eigenspectrum = eigenspectrum( reduce_number_of_terms(qubit_hamiltonian, [stab1, stab2], manual_input=True, fixed_positions=[0, 1])) self.assertAlmostEqual(spectrum[0], red_eigenspectrum[0])
def test_function_errors(self): """Test error of main function.""" hamiltonian, spectrum = lih_hamiltonian() qubit_hamiltonian = jordan_wigner(hamiltonian) stab1 = QubitOperator('Z0 Z2', -1.0) stab2 = QubitOperator('Z1 Z3', -1.0) with self.assertRaises(TypeError): reduce_number_of_terms(operator=1, stabilizers=stab1 + stab2) with self.assertRaises(TypeError): reduce_number_of_terms(operator=qubit_hamiltonian, stabilizers=1) with self.assertRaises(TypeError): reduce_number_of_terms(operator=qubit_hamiltonian, stabilizers=stab1 + stab2, manual_input=True, fixed_positions=None) with self.assertRaises(StabilizerError): reduce_number_of_terms(operator=qubit_hamiltonian, stabilizers=stab1 + stab2, manual_input=True, fixed_positions=[1]) with self.assertRaises(StabilizerError): reduce_number_of_terms(operator=qubit_hamiltonian, stabilizers=stab1 + stab2, manual_input=True, fixed_positions=[1, 1]) with self.assertRaises(StabilizerError): # Check Identity as stabilizer error. reduce_number_of_terms(operator=qubit_hamiltonian, stabilizers=(stab1 + QubitOperator(' ', 1.0))) with self.assertRaises(StabilizerError): # Check complex coefficient stabilizer error. reduce_number_of_terms(operator=qubit_hamiltonian, stabilizers=(stab1 + QubitOperator('Z0', 1.0j))) with self.assertRaises(StabilizerError): # Check linearly-dependent stabilizer error. reduce_number_of_terms( operator=qubit_hamiltonian, stabilizers=(stab1 + QubitOperator('Z0 Z1 Z2 Z3', 1.0) + stab2)) with self.assertRaises(StabilizerError): # Check anti-commuting stabilizer error. reduce_number_of_terms(operator=qubit_hamiltonian, stabilizers=(QubitOperator('X0', 1.0) + QubitOperator('Y0', 1.0))) with self.assertRaises(StabilizerError): # Check linearly-dependent stabilizer error. _reduce_terms( terms=qubit_hamiltonian, stabilizer_list=list(stab1 + QubitOperator('Z0 Z1 Z2 Z3', 1.0) + stab2), manual_input=False, fixed_positions=[]) with self.assertRaises(StabilizerError): # Check complex coefficient stabilizer error. _reduce_terms(terms=qubit_hamiltonian, stabilizer_list=list(stab1 + QubitOperator('Z0', 1.0j)), manual_input=False, fixed_positions=[]) with self.assertRaises(StabilizerError): # Check linearly-dependent stabilizer error. par_qop = QubitOperator('Z0 Z1 Z2 Z3', 1.0) _reduce_terms_keep_length(terms=qubit_hamiltonian, stabilizer_list=[stab1, par_qop, stab2], manual_input=False, fixed_positions=[]) with self.assertRaises(StabilizerError): # Check complex coefficient stabilizer error. aux_qop = QubitOperator('Z0', 1.0j) _reduce_terms_keep_length(terms=qubit_hamiltonian, stabilizer_list=[stab1, aux_qop], manual_input=False, fixed_positions=[]) with self.assertRaises(StabilizerError): # Test check_commuting_stabilizer function # Requires a list of QubitOperators one of which # has an imaginary term. check_commuting_stabilizers(stabilizer_list=[ QubitOperator('Z0 Z1', 1.0), QubitOperator('X0', 1j) ], msg='This test fails.') with self.assertRaises(StabilizerError): # Test check_stabilizer_linearity function. # Requires a list of QUbitOperators one of which is # the identity. check_stabilizer_linearity( [QubitOperator('Z0 Z1', 1.0), QubitOperator(' ', 1.0)], msg='This test fails.')