def test_no_freeze_core(self): """Test the disabled `freeze_core` convenience argument. Regression test against https://github.com/Qiskit/qiskit-nature/issues/652 """ driver = HDF5Driver( hdf5_input=self.get_resource_path("LiH_sto3g.hdf5", "second_q/transformers") ) driver_result = driver.run() trafo = FreezeCoreTransformer(freeze_core=False) driver_result_reduced = trafo.transform(driver_result) electronic_energy = driver_result_reduced.get_property("ElectronicEnergy") electronic_energy_exp = driver_result.get_property("ElectronicEnergy") with self.subTest("MO 1-electron integrals"): np.testing.assert_array_almost_equal( electronic_energy.get_electronic_integral(ElectronicBasis.MO, 1).to_spin(), electronic_energy_exp.get_electronic_integral(ElectronicBasis.MO, 1).to_spin(), ) with self.subTest("MO 2-electron integrals"): np.testing.assert_array_almost_equal( electronic_energy.get_electronic_integral(ElectronicBasis.MO, 2).to_spin(), electronic_energy_exp.get_electronic_integral(ElectronicBasis.MO, 2).to_spin(), ) with self.subTest("Inactive energy"): self.assertAlmostEqual(electronic_energy._shift["FreezeCoreTransformer"], 0.0)
def test_freeze_core(self): """Test the `freeze_core` convenience argument.""" driver = HDF5Driver( hdf5_input=self.get_resource_path("LiH_sto3g.hdf5", "second_q/transformers") ) driver_result = driver.run() trafo = FreezeCoreTransformer(freeze_core=True) driver_result_reduced = trafo.transform(driver_result) expected = HDF5Driver( hdf5_input=self.get_resource_path("LiH_sto3g_reduced.hdf5", "second_q/transformers") ).run() self.assertDriverResult(driver_result_reduced, expected, dict_key="FreezeCoreTransformer")
def test_freeze_core_with_remove_orbitals(self): """Test the `freeze_core` convenience argument in combination with `remove_orbitals`.""" driver = HDF5Driver( hdf5_input=self.get_resource_path("BeH_sto3g.hdf5", "second_q/transformers") ) driver_result = driver.run() trafo = FreezeCoreTransformer(freeze_core=True, remove_orbitals=[4, 5]) driver_result_reduced = trafo.transform(driver_result) expected = HDF5Driver( hdf5_input=self.get_resource_path("BeH_sto3g_reduced.hdf5", "second_q/transformers") ).run() expected.get_property("ParticleNumber")._num_spin_orbitals = 6 self.assertDriverResult(driver_result_reduced, expected, dict_key="FreezeCoreTransformer")
def test_oh_with_atoms(self): """OH with num_atoms test""" driver = FCIDumpDriver( self.get_resource_path("test_driver_fcidump_oh.fcidump", "second_q/drivers/fcidumpd"), # atoms=["O", "H"], ) result = self._run_driver(driver, transformers=[FreezeCoreTransformer()]) self._assert_energy(result, "oh")
def test_full_active_space(self, kwargs): """Test that transformer has no effect when all orbitals are active.""" driver = HDF5Driver( hdf5_input=self.get_resource_path("H2_sto3g.hdf5", "second_q/transformers") ) driver_result = driver.run() # The references which we compare too were produced by the `ActiveSpaceTransformer` and, # thus, the key here needs to stay the same as in that test case. driver_result.get_property("ElectronicEnergy")._shift["ActiveSpaceTransformer"] = 0.0 for prop in driver_result.get_property("ElectronicDipoleMoment")._dipole_axes.values(): prop._shift["ActiveSpaceTransformer"] = 0.0 trafo = FreezeCoreTransformer(**kwargs) driver_result_reduced = trafo.transform(driver_result) self.assertDriverResult( driver_result_reduced, driver_result, dict_key="FreezeCoreTransformer" )
def test_lih_uhf(self): """lih uhf test""" driver = PySCFDriver( atom=self.lih, unit=UnitsType.ANGSTROM, charge=0, spin=0, basis="sto-3g", method=MethodType.UHF, ) result = self._run_driver(driver, transformers=[FreezeCoreTransformer()]) self._assert_energy_and_dipole(result, "lih")
def test_oh_freeze_core(self): """OH freeze core test""" with self.assertRaises(QiskitNatureError) as ctx: driver = FCIDumpDriver( self.get_resource_path( "test_driver_fcidump_oh.fcidump", "second_q/drivers/fcidumpd" ) ) result = self._run_driver(driver, transformers=[FreezeCoreTransformer()]) self._assert_energy(result, "oh") msg = ( "'The provided `GroupedElectronicProperty` does not contain an `ElectronicBasisTransform`" " property, which is required by this transformer!'" ) self.assertEqual(msg, str(ctx.exception))
def test_lih_rhf_parity_2q(self): """lih rhf parity 2q test""" driver = PySCFDriver( atom=self.lih, unit=UnitsType.ANGSTROM, charge=0, spin=0, basis="sto-3g", method=MethodType.RHF, ) result = self._run_driver( driver, converter=QubitConverter(ParityMapper(), two_qubit_reduction=True), transformers=[FreezeCoreTransformer()], ) self._assert_energy_and_dipole(result, "lih")
def test_lih_rhf_bk(self): """lih rhf bk test""" driver = PySCFDriver( atom=self.lih, unit=UnitsType.ANGSTROM, charge=0, spin=0, basis="sto-3g", method=MethodType.RHF, ) result = self._run_driver( driver, converter=QubitConverter(BravyiKitaevMapper()), transformers=[FreezeCoreTransformer()], ) self._assert_energy_and_dipole(result, "lih")
def setUp(self): super().setUp() self.driver = PySCFDriver(atom="H 0 0 0.735; H 0 0 0", basis="631g") self.qubit_converter = QubitConverter(ParityMapper(), two_qubit_reduction=True) self.electronic_structure_problem = ElectronicStructureProblem( self.driver, [FreezeCoreTransformer()] ) self.num_spin_orbitals = 8 self.num_particles = (1, 1) # because we create the initial state and ansatzes early, we need to ensure the qubit # converter already ran such that convert_match works as expected main_op, _ = self.electronic_structure_problem.second_q_ops() _ = self.qubit_converter.convert( main_op, self.num_particles, ) self.reference_energy_pUCCD = -1.1434447924298028 self.reference_energy_UCCD0 = -1.1476045878481704 self.reference_energy_UCCD0full = -1.1515491334334347 # reference energy of UCCSD/VQE with tapering everywhere self.reference_energy_UCCSD = -1.1516142309717594 # reference energy of UCCSD/VQE when no tapering on excitations is used self.reference_energy_UCCSD_no_tap_exc = -1.1516142309717594 # excitations for succ self.reference_singlet_double_excitations = [ [0, 1, 4, 5], [0, 1, 4, 6], [0, 1, 4, 7], [0, 2, 4, 6], [0, 2, 4, 7], [0, 3, 4, 7], ] # groups for succ_full self.reference_singlet_groups = [ [[0, 1, 4, 5]], [[0, 1, 4, 6], [0, 2, 4, 5]], [[0, 1, 4, 7], [0, 3, 4, 5]], [[0, 2, 4, 6]], [[0, 2, 4, 7], [0, 3, 4, 6]], [[0, 3, 4, 7]], ]
def test_freeze_core_z2_symmetry_compatibility(self): """Regression test against #192. An issue arose when the FreezeCoreTransformer was combined with the automatic Z2Symmetry reduction. This regression test ensures that this behavior remains fixed. """ driver = HDF5Driver(hdf5_input=self.get_resource_path( "LiH_sto3g.hdf5", "second_q/transformers")) problem = ElectronicStructureProblem(driver, [FreezeCoreTransformer()]) qubit_converter = QubitConverter( ParityMapper(), two_qubit_reduction=True, z2symmetry_reduction="auto", ) solver = NumPyMinimumEigensolverFactory() gsc = GroundStateEigensolver(qubit_converter, solver) result = gsc.solve(problem) self.assertAlmostEqual(result.total_energies[0], -7.882, places=2)
def test_sector_locator_homonuclear(self): """Test sector locator.""" molecule = Molecule( geometry=[("Li", [0.0, 0.0, 0.0]), ("Li", [0.0, 0.0, 2.771])], charge=0, multiplicity=1 ) freeze_core_transformer = FreezeCoreTransformer(True) driver = ElectronicStructureMoleculeDriver( molecule, basis="sto3g", driver_type=ElectronicStructureDriverType.PYSCF ) es_problem = ElectronicStructureProblem(driver, transformers=[freeze_core_transformer]) qubit_conv = QubitConverter( mapper=ParityMapper(), two_qubit_reduction=True, z2symmetry_reduction="auto" ) main_op, _ = es_problem.second_q_ops() qubit_conv.convert( main_op, num_particles=es_problem.num_particles, sector_locator=es_problem.symmetry_sector_locator, ) self.assertListEqual(qubit_conv.z2symmetries.tapering_values, [-1, 1])
def test_lih_uhf(self): """lih uhf test""" driver = PSI4Driver(config=self.psi4_lih_config.format("uhf")) result = self._run_driver(driver, transformers=[FreezeCoreTransformer()]) self._assert_energy_and_dipole(result, "lih")
def test_oh_uhf(self): """oh uhf test""" driver = GaussianDriver(config=self.g16_oh_config.format("uhf")) result = self._run_driver(driver, transformers=[FreezeCoreTransformer()]) self._assert_energy_and_dipole(result, "oh")