def test_get_context_selection_circuit_offdiagonal(self): term = QubitOperator("X0 Y1") circuit, ising_operator = get_context_selection_circuit(term) # Need to convert to QubitOperator in order to get matrix representation qubit_operator = change_operator_type(ising_operator, QubitOperator) target_unitary = qubit_operator_sparse(term) transformed_unitary = ( circuit.to_unitary().conj().T @ qubit_operator_sparse(qubit_operator) @ circuit.to_unitary()) assert np.allclose(target_unitary.todense(), transformed_unitary)
def test_get_context_selection_circuit_for_group(self): group = QubitOperator("X0 Y1") - 0.5 * QubitOperator((1, "Y")) circuit, ising_operator = get_context_selection_circuit_for_group( group) # Need to convert to QubitOperator in order to get matrix representation qubit_operator = change_operator_type(ising_operator, QubitOperator) target_unitary = qubit_operator_sparse(group) transformed_unitary = ( circuit.to_unitary().conj().T @ qubit_operator_sparse(qubit_operator) @ circuit.to_unitary()) assert np.allclose(target_unitary.todense(), transformed_unitary)
def test_get_context_selection_circuit_offdiagonal(self): term = ((0, "X"), (1, "Y")) circuit, ising_operator = get_context_selection_circuit(term) # Need to convert to QubitOperator in order to get matrix representation qubit_operator = QubitOperator() for ising_term in ising_operator.terms: qubit_operator += QubitOperator(ising_term, ising_operator.terms[ising_term]) target_unitary = qubit_operator_sparse(QubitOperator(term)) transformed_unitary = ( circuit.to_unitary().conj().T @ qubit_operator_sparse(qubit_operator) @ circuit.to_unitary()) assert np.allclose(target_unitary.todense(), transformed_unitary)
def test_run_psi4(self): geometry = { "sites": [{ 'species': 'H', 'x': 0, 'y': 0, 'z': 0 }, { 'species': 'H', 'x': 0, 'y': 0, 'z': 1.7 }] } results, hamiltonian = run_psi4(geometry, save_hamiltonian=True) self.assertAlmostEqual(results['energy'], -0.8544322638069642) self.assertEqual(results['n_alpha'], 1) self.assertEqual(results['n_beta'], 1) self.assertEqual(results['n_mo'], 2) self.assertEqual(results['n_frozen_core'], 0) self.assertEqual(results['n_frozen_valence'], 0) self.assertEqual(hamiltonian.n_qubits, 4) qubit_operator = qubit_operator_sparse(jordan_wigner(hamiltonian)) energy, state = jw_get_ground_state_at_particle_number( qubit_operator, 2) results_cisd, hamiltonian = run_psi4(geometry, method='ccsd') # For this system, the CCSD energy should be exact. self.assertAlmostEqual(energy, results_cisd['energy'])
def test_get_context_selection_circuit_for_group(self): group = QubitOperator(((0, "X"), (1, "Y"))) - 0.5 * QubitOperator( ((1, "Y"), )) circuit, ising_operator = get_context_selection_circuit_for_group( group) # Need to convert to QubitOperator in order to get matrix representation qubit_operator = QubitOperator() for ising_term in ising_operator.terms: qubit_operator += QubitOperator(ising_term, ising_operator.terms[ising_term]) target_unitary = qubit_operator_sparse(group) transformed_unitary = ( circuit.to_unitary().conj().T @ qubit_operator_sparse(qubit_operator) @ circuit.to_unitary()) self.assertTrue( np.allclose(target_unitary.todense(), transformed_unitary))
def test_qubitop_matrix_converion(self): # Given m = 4 n = 2 ** m TOL = 10 ** -15 random.seed(RNDSEED) A = np.array([[random.uniform(-1, 1) for x in range(n)] for y in range(n)]) # When A_qubitop = get_qubitop_from_matrix(A) A_qubitop_matrix = np.array(qubit_operator_sparse(A_qubitop).todense()) test_matrix = A_qubitop_matrix - A # Then for row in test_matrix: for elem in row: self.assertEqual(abs(elem) < TOL, True)
def test_run_psi4( self, psi4_config: Psi4Config, expected_tuple: ExpectedTuple, jw_particle_num: Optional[int], ): results_dict = run_psi4( geometry=psi4_config.geometry, n_active_extract=psi4_config.n_active_extract, n_occupied_extract=psi4_config.n_occupied_extract, freeze_core=psi4_config.freeze_core, freeze_core_extract=psi4_config.freeze_core_extract, method=psi4_config.method, options=psi4_config.options, save_hamiltonian=True, save_rdms=psi4_config.save_rdms, ) results, hamiltonian, rdm = ( results_dict["results"], results_dict["hamiltonian"], results_dict["rdms"], ) if rdm: energy_from_rdm = rdm.expectation(hamiltonian) if psi4_config.save_rdms: assert math.isclose(results["energy"], energy_from_rdm) else: assert (math.isclose(results["energy"], expected_tuple.exp_energy) if expected_tuple.exp_energy else True) assert (results["n_alpha"] == expected_tuple.exp_alpha if expected_tuple.exp_alpha else True) assert (results["n_beta"] == expected_tuple.exp_beta if expected_tuple.exp_beta else True) assert (results["n_mo"] == expected_tuple.exp_mo if expected_tuple.exp_mo else True) assert (results["n_frozen_core"] == expected_tuple.exp_frozen_core if expected_tuple.exp_frozen_core else True) assert (results["n_frozen_valence"] == expected_tuple.exp_frozen_valence if expected_tuple.exp_frozen_valence else True) assert (hamiltonian.n_qubits == expected_tuple.exp_n_qubits if expected_tuple.exp_n_qubits else True) if expected_tuple.exp_one_body_tensor_shape: assert (rdm.one_body_tensor.shape[0] == expected_tuple.exp_one_body_tensor_shape) assert math.isclose(einsum("ii->", rdm.one_body_tensor), 2) if jw_particle_num: qubit_operator = qubit_operator_sparse(jordan_wigner(hamiltonian)) energy, _ = jw_get_ground_state_at_particle_number( qubit_operator, jw_particle_num) if expected_tuple.extra_energy_check: assert math.isclose(energy, expected_tuple.extra_energy_check, rel_tol=1e-7) else: assert math.isclose(energy, results["energy"], rel_tol=1e-7)