def test_compute(self): """Test when grouped_property and ansatz are set via compute.""" mp2_initial_point = MP2InitialPoint() mp2_initial_point.compute(ansatz=self.mock_ansatz, grouped_property=self.mock_grouped_property) with self.subTest("Test grouped property is set."): self.assertEqual(self.mock_grouped_property, mp2_initial_point.grouped_property) with self.subTest("Test ansatz is set."): self.assertEqual(self.mock_ansatz, mp2_initial_point.ansatz) with self.subTest("Test initial_point array is computed."): np.testing.assert_array_equal(mp2_initial_point.to_numpy_array(), [0.0]) with self.subTest("Test initial_point array is computed on demand."): mp2_initial_point._corrections = None np.testing.assert_array_equal(mp2_initial_point.to_numpy_array(), [0.0]) with self.subTest("Test energy correction is computed on demand."): mp2_initial_point._corrections = None np.testing.assert_array_equal(mp2_initial_point.energy_correction, 0.0) with self.subTest("Test energy is computed on demand."): mp2_initial_point._corrections = None np.testing.assert_array_equal(mp2_initial_point.total_energy, 123.45)
def test_no_ansatz(self): """Test when no ansatz is provided.""" mp2_initial_point = MP2InitialPoint() with self.assertRaises(QiskitNatureError): mp2_initial_point.compute( ansatz=None, grouped_property=self.mock_grouped_property)
def test_set_excitations_directly(self): """Test when setting excitation_list directly.""" mp2_initial_point = MP2InitialPoint() mp2_initial_point.excitation_list = self.excitation_list mp2_initial_point.compute(ansatz=None, grouped_property=self.mock_grouped_property) self.assertEqual(mp2_initial_point.excitation_list, self.excitation_list) self.assertEqual(mp2_initial_point.to_numpy_array(), [0.0])
def test_mp2_initial_point_with_real_molecules( self, atom, ): """Test MP2InitialPoint with real molecules.""" from pyscf import gto # pylint: disable=import-error # Compute the PySCF result pyscf_mol = gto.M(atom=atom, basis="sto3g", verbose=0) pyscf_mp = pyscf_mol.MP2().run(verbose=0) driver = PySCFDriver(atom=atom, basis="sto3g") problem = ElectronicStructureProblem(driver) problem.second_q_ops() grouped_property = problem.grouped_property_transformed particle_number = grouped_property.get_property(ParticleNumber) num_particles = (particle_number.num_alpha, particle_number.num_beta) num_spin_orbitals = particle_number.num_spin_orbitals qubit_converter = QubitConverter(mapper=JordanWignerMapper()) initial_state = HartreeFock( num_spin_orbitals=num_spin_orbitals, num_particles=num_particles, qubit_converter=qubit_converter, ) ansatz = UCC( num_spin_orbitals=num_spin_orbitals, num_particles=num_particles, excitations="sd", qubit_converter=qubit_converter, initial_state=initial_state, ) mp2_initial_point = MP2InitialPoint() mp2_initial_point.grouped_property = grouped_property mp2_initial_point.ansatz = ansatz with self.subTest("Test the MP2 energy correction."): np.testing.assert_almost_equal(mp2_initial_point.energy_correction, pyscf_mp.e_corr, decimal=4) with self.subTest("Test the total MP2 energy."): np.testing.assert_almost_equal(mp2_initial_point.total_energy, pyscf_mp.e_tot, decimal=4) with self.subTest("Test the T2 amplitudes."): mp2_initial_point.compute() np.testing.assert_array_almost_equal( mp2_initial_point.t2_amplitudes, pyscf_mp.t2, decimal=4)
def test_no_two_body_mo_integrals(self): """Test when the two body MO integrals are missing.""" electronic_energy = Mock(spec=ElectronicEnergy) electronic_energy.orbital_energies = Mock(np.ndarray) electronic_energy.get_electronic_integral = Mock(return_value=None) self.mock_grouped_property.get_property = Mock( return_value=electronic_energy) mp2_initial_point = MP2InitialPoint() with self.assertRaises(QiskitNatureError): mp2_initial_point.compute( ansatz=self.mock_ansatz, grouped_property=self.mock_grouped_property)
def test_no_orbital_energies(self): """Test when the orbital energies are missing.""" electronic_integrals = Mock(spec=ElectronicIntegrals) electronic_energy = Mock(spec=ElectronicEnergy) electronic_energy.get_electronic_integral = Mock( return_value=electronic_integrals) electronic_energy.orbital_energies = None grouped_property = Mock(spec=GroupedSecondQuantizedProperty) grouped_property.get_property = Mock(return_value=electronic_energy) mp2_initial_point = MP2InitialPoint() with self.assertRaises(QiskitNatureError): mp2_initial_point.compute(ansatz=self.mock_ansatz, grouped_property=grouped_property)
def test_no_particle_number(self): """Test when the particle number is missing.""" def get_property(prop: type[Property]) -> Property | None: if prop == ParticleNumber: return None elif prop == ElectronicEnergy: return self.electronic_energy else: return None self.mock_grouped_property.get_property = get_property mp2_initial_point = MP2InitialPoint() with self.assertRaises(QiskitNatureError): mp2_initial_point.compute( ansatz=self.mock_ansatz, grouped_property=self.mock_grouped_property)
def test_vqe_ucc_factory_with_mp2(self): """Test when using MP2InitialPoint to generate the initial point.""" informed_start = MP2InitialPoint() solver = VQEUCCFactory( quantum_instance=QuantumInstance( BasicAer.get_backend("statevector_simulator")), initial_point=informed_start, ) calc = GroundStateEigensolver(self.qubit_converter, solver) res = calc.solve(self.electronic_structure_problem) np.testing.assert_array_almost_equal( solver.initial_point.to_numpy_array(), [0.0, 0.0, -0.07197145]) self.assertAlmostEqual(res.total_energies[0], self.reference_energy, places=6)
def test_raises_error_for_non_restricted_spins(self): """Test when grouped_property and ansatz are set via compute.""" def get_matrix(value: int | None = None) -> np.ndarray: matrix = [1] if value == 2 else [0] return np.asarray(matrix) electronic_integrals = Mock(spec=ElectronicIntegrals) electronic_integrals.get_matrix = get_matrix electronic_energy = Mock(spec=ElectronicEnergy) electronic_energy.orbital_energies = Mock(spec=np.ndarray) electronic_energy.get_electronic_integral = Mock( return_value=electronic_integrals) grouped_property = Mock(spec=GroupedSecondQuantizedProperty) grouped_property.get_property = Mock(return_value=electronic_energy) mp2_initial_point = MP2InitialPoint() with self.assertRaises(NotImplementedError): mp2_initial_point.compute(ansatz=self.mock_ansatz, grouped_property=grouped_property)
def test_negative_threshold(self): """Test when a negative threshold is provided.""" mp2_initial_point = MP2InitialPoint(threshold=-3.0) self.assertEqual(mp2_initial_point.threshold, 3.0)
def test_no_threshold(self): """Test when no threshold is provided.""" mp2_initial_point = MP2InitialPoint(threshold=None) self.assertEqual(mp2_initial_point.threshold, 0.0)