Beispiel #1
0
    def test_build_fermionic_op_from_ints_one(self):
        """Tests that the correct FermionicOp is built from 1-body integrals."""
        expected_num_of_terms_ferm_op = 16
        expected_fermionic_op_path = self.get_resource_path(
            "H2_631g_ferm_op_one_int",
            "problems/second_quantization/electronic/resources",
        )
        expected_fermionic_op = read_expected_file(expected_fermionic_op_path)

        driver = HDF5Driver(
            hdf5_input=self.get_resource_path(
                "H2_631g.hdf5", "transformers/second_quantization/electronic"
            )
        )
        driver_result = driver.run()
        electronic_energy = cast(ElectronicEnergy, driver_result.get_property(ElectronicEnergy))
        reduced = ElectronicEnergy(
            [electronic_energy.get_electronic_integral(ElectronicBasis.MO, 1)]
        )
        fermionic_op = reduced.second_q_ops()[0]

        with self.subTest("Check type of fermionic operator"):
            assert isinstance(fermionic_op, FermionicOp)
        with self.subTest("Check expected number of terms in a fermionic operator."):
            assert len(fermionic_op) == expected_num_of_terms_ferm_op
        with self.subTest("Check expected content of a fermionic operator."):
            assert all(
                s[0] == t[0] and np.isclose(s[1], t[1])
                for s, t in zip(fermionic_op.to_list(), expected_fermionic_op)
            )
    def test_second_q_ops_with_active_space(self):
        """Tests that the correct second quantized operator is created if an active space
        transformer is provided."""
        expected_num_of_sec_quant_ops = 7
        expected_fermionic_op_path = self.get_resource_path(
            'H2_631g_ferm_op_active_space', 'problems/second_quantization/'
            'electronic/resources')
        expected_fermionic_op = read_expected_file(expected_fermionic_op_path)
        driver = HDF5Driver(
            hdf5_input=self.get_resource_path('H2_631g.hdf5', 'transformers'))
        trafo = ActiveSpaceTransformer(num_electrons=2,
                                       num_molecular_orbitals=2)

        electronic_structure_problem = ElectronicStructureProblem(
            driver, [trafo])
        second_quantized_ops = electronic_structure_problem.second_q_ops()
        electr_sec_quant_op = second_quantized_ops[0]

        with self.subTest(
                "Check expected length of the list of second quantized operators."
        ):
            assert len(second_quantized_ops) == expected_num_of_sec_quant_ops
        with self.subTest(
                "Check types in the list of second quantized operators."):
            for second_quantized_op in second_quantized_ops:
                assert isinstance(second_quantized_op, SecondQuantizedOp)
        with self.subTest(
                "Check components of electronic second quantized operator."):
            assert all(s[0] == t[0] and np.isclose(s[1], t[1]) for s, t in zip(
                expected_fermionic_op, electr_sec_quant_op.to_list()))
    def test_second_q_ops_without_transformers(self):
        """Tests that the list of second quantized operators is created if no transformers
        provided."""
        expected_num_of_sec_quant_ops = 7
        expected_fermionic_op_path = self.get_resource_path(
            "H2_631g_ferm_op_two_ints",
            "problems/second_quantization/electronic/resources",
        )
        expected_fermionic_op = read_expected_file(expected_fermionic_op_path)

        driver = HDF5Driver(hdf5_input=self.get_resource_path("H2_631g.hdf5", "transformers"))
        electronic_structure_problem = ElectronicStructureProblem(driver)

        second_quantized_ops = electronic_structure_problem.second_q_ops()
        electr_sec_quant_op = second_quantized_ops[0]
        with self.subTest("Check expected length of the list of second quantized operators."):
            assert len(second_quantized_ops) == expected_num_of_sec_quant_ops
        with self.subTest("Check types in the list of second quantized operators."):
            for second_quantized_op in second_quantized_ops:
                assert isinstance(second_quantized_op, SecondQuantizedOp)
        with self.subTest("Check components of electronic second quantized operator."):
            assert all(
                s[0] == t[0] and np.isclose(s[1], t[1])
                for s, t in zip(expected_fermionic_op, electr_sec_quant_op.to_list())
            )
    def test_second_q_ops_with_active_space(self):
        """Tests that the correct second quantized operator is created if an active space
        transformer is provided."""
        expected_num_of_sec_quant_ops = 7
        expected_fermionic_op_path = self.get_resource_path(
            "H2_631g_ferm_op_active_space",
            "problems/second_quantization/electronic/resources",
        )
        expected_fermionic_op = read_expected_file(expected_fermionic_op_path)

        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", category=DeprecationWarning)
            driver = LegacyHDF5Driver(hdf5_input=self.get_resource_path(
                "H2_631g.hdf5", "transformers/second_quantization/electronic"))
            trafo = LegacyActiveSpaceTransformer(num_electrons=2,
                                                 num_molecular_orbitals=2)

            electronic_structure_problem = ElectronicStructureProblem(
                driver, [trafo])
            second_quantized_ops = electronic_structure_problem.second_q_ops()
            electr_sec_quant_op = second_quantized_ops[0]

        with self.subTest("Check that the correct properties are/aren't None"):
            with warnings.catch_warnings():
                warnings.filterwarnings("ignore", category=DeprecationWarning)
                # legacy driver used, molecule_data should not be None
                self.assertIsNotNone(
                    electronic_structure_problem.molecule_data)
                # transformer used, molecule_data_transformed should not be None
                self.assertIsNotNone(
                    electronic_structure_problem.molecule_data_transformed)
            # converted properties should never be None
            self.assertIsNotNone(electronic_structure_problem.grouped_property)
            self.assertIsNotNone(
                electronic_structure_problem.grouped_property_transformed)

        with self.subTest(
                "Check that the deprecated molecule_data property is not None"
        ):
            with warnings.catch_warnings():
                warnings.filterwarnings("ignore", category=DeprecationWarning)
                self.assertIsNotNone(
                    electronic_structure_problem.molecule_data)
        with self.subTest(
                "Check expected length of the list of second quantized operators."
        ):
            assert len(second_quantized_ops) == expected_num_of_sec_quant_ops
        with self.subTest(
                "Check types in the list of second quantized operators."):
            for second_quantized_op in second_quantized_ops:
                assert isinstance(second_quantized_op, SecondQuantizedOp)
        with self.subTest(
                "Check components of electronic second quantized operator."):
            assert all(s[0] == t[0] and np.isclose(s[1], t[1]) for s, t in zip(
                expected_fermionic_op, electr_sec_quant_op.to_list()))
    def test_second_q_ops_without_transformers(self):
        """Tests that the list of second quantized operators is created if no transformers
        provided."""
        expected_num_of_sec_quant_ops = 7
        expected_fermionic_op_path = self.get_resource_path(
            "H2_631g_ferm_op_two_ints",
            "problems/second_quantization/electronic/resources",
        )
        expected_fermionic_op = read_expected_file(expected_fermionic_op_path)

        driver = HDF5Driver(hdf5_input=self.get_resource_path(
            "H2_631g.hdf5", "transformers/second_quantization/electronic"))
        electronic_structure_problem = ElectronicStructureProblem(driver)

        second_quantized_ops = electronic_structure_problem.second_q_ops()
        electr_sec_quant_op = second_quantized_ops[
            electronic_structure_problem.main_property_name]
        second_quantized_ops = list(second_quantized_ops.values())

        with self.subTest("Check that the correct properties are/aren't None"):
            with warnings.catch_warnings():
                warnings.filterwarnings("ignore", category=DeprecationWarning)
                # new driver used, molecule_data* should be None
                self.assertIsNone(electronic_structure_problem.molecule_data)
                self.assertIsNone(
                    electronic_structure_problem.molecule_data_transformed)
            # converted properties should never be None
            self.assertIsNotNone(electronic_structure_problem.grouped_property)
            self.assertIsNotNone(
                electronic_structure_problem.grouped_property_transformed)

        with self.subTest(
                "Check expected length of the list of second quantized operators."
        ):
            assert len(second_quantized_ops) == expected_num_of_sec_quant_ops
        with self.subTest(
                "Check types in the list of second quantized operators."):
            for second_quantized_op in second_quantized_ops:
                assert isinstance(second_quantized_op, SecondQuantizedOp)
        with self.subTest(
                "Check components of electronic second quantized operator."):
            assert all(s[0] == t[0] and np.isclose(s[1], t[1]) for s, t in zip(
                expected_fermionic_op, electr_sec_quant_op.to_list()))
Beispiel #6
0
 def test_build_fermionic_op(self):
     """Tests that the correct FermionicOp is built from QMolecule."""
     expected_num_of_terms_ferm_op = 184
     expected_fermionic_op_path = self.get_resource_path(
         'H2_631g_ferm_op_two_ints', 'problems/second_quantization/'
         'electronic/resources')
     expected_fermionic_op = read_expected_file(expected_fermionic_op_path)
     driver = HDF5Driver(
         hdf5_input=self.get_resource_path('H2_631g.hdf5', 'transformers'))
     q_molecule = driver.run()
     fermionic_op = fermionic_op_builder._build_fermionic_op(q_molecule)
     with self.subTest("Check type of fermionic operator"):
         assert isinstance(fermionic_op, FermionicOp)
     with self.subTest(
             "Check expected number of terms in a fermionic operator."):
         assert len(fermionic_op) == expected_num_of_terms_ferm_op
     with self.subTest("Check expected content of a fermionic operator."):
         assert all(
             s[0] == t[0] and np.isclose(s[1], t[1])
             for s, t in zip(fermionic_op.to_list(), expected_fermionic_op))
    def test_build_fermionic_op_from_ints_one(self):
        """Tests that the correct FermionicOp is built from 1-body integrals."""
        expected_num_of_terms_ferm_op = 16
        expected_fermionic_op_path = self.get_resource_path(
            "H2_631g_ferm_op_one_int",
            "problems/second_quantization/electronic/resources",
        )
        expected_fermionic_op = read_expected_file(expected_fermionic_op_path)

        driver = HDF5Driver(
            hdf5_input=self.get_resource_path("H2_631g.hdf5", "transformers"))
        q_molecule = driver.run()
        fermionic_op = fermionic_op_builder.build_ferm_op_from_ints(
            q_molecule.one_body_integrals)

        with self.subTest("Check type of fermionic operator"):
            assert isinstance(fermionic_op, FermionicOp)
        with self.subTest(
                "Check expected number of terms in a fermionic operator."):
            assert len(fermionic_op) == expected_num_of_terms_ferm_op
        with self.subTest("Check expected content of a fermionic operator."):
            assert all(
                s[0] == t[0] and np.isclose(s[1], t[1])
                for s, t in zip(fermionic_op.to_list(), expected_fermionic_op))