Beispiel #1
0
 def test_multiplicity(self):
     """test multiplicity"""
     mol = Molecule(geometry=[("H", [0.0, 0.0, 0.0]), ("H",
                                                       [0.0, 0.0, 1.0])])
     self.assertEqual(mol.multiplicity, 1)
     mol.multiplicity = 0
     self.assertEqual(mol.multiplicity, 0)
Beispiel #2
0
 def test_charge(self):
     """test charge"""
     mol = Molecule(geometry=[("H", [0.0, 0.0, 0.0]), ("H",
                                                       [0.0, 0.0, 1.0])])
     self.assertEqual(mol.charge, 0)
     mol.charge = 1
     self.assertEqual(mol.charge, 1)
Beispiel #3
0
    def test_perturbations(self):
        """test perturbations"""
        stretch1 = partial(Molecule.absolute_stretching, atom_pair=(1, 0))
        bend = partial(Molecule.absolute_bending, atom_trio=(1, 0, 2))
        stretch2 = partial(Molecule.absolute_stretching, atom_pair=(0, 1))

        mol = Molecule(
            geometry=[
                ("H", [0.0, 0.0, 0.0]),
                ("O", [0.0, 0.0, 1.0]),
                ("Li", [0.0, 1.0, -1.0]),
            ],
            degrees_of_freedom=[stretch1, bend, stretch2],
            masses=[1, 1, 1],
        )

        with self.subTest("Before perturbing"):
            geom = mol.geometry
            self.assertEqual(geom[0][0], "H")
            self.assertEqual(geom[1][0], "O")
            self.assertEqual(geom[2][0], "Li")
            np.testing.assert_array_almost_equal(geom[0][1], [0.0, 0.0, 0.0])
            np.testing.assert_array_almost_equal(geom[1][1], [0.0, 0.0, 1.0])
            np.testing.assert_array_almost_equal(geom[2][1], [0.0, 1.0, -1.0])
            self.assertIsNone(mol.perturbations)

        with self.subTest("Perturbations: [2, np.pi / 2, -.5]"):
            mol.perturbations = [2, np.pi / 2, -0.5]
            geom = mol.geometry
            self.assertEqual(geom[0][0], "H")
            self.assertEqual(geom[1][0], "O")
            self.assertEqual(geom[2][0], "Li")
            np.testing.assert_array_almost_equal(geom[0][1], [0.0, 0.5, 0.0])
            np.testing.assert_array_almost_equal(geom[1][1], [0.0, 3.0, 0.0])
            np.testing.assert_array_almost_equal(geom[2][1], [0.0, 1.0, -1.0])
            self.assertListEqual(mol.perturbations, [2, np.pi / 2, -0.5])

        with self.subTest("Perturbations: None"):
            mol.perturbations = None  # Should be original geometry
            geom = mol.geometry
            self.assertEqual(geom[0][0], "H")
            self.assertEqual(geom[1][0], "O")
            self.assertEqual(geom[2][0], "Li")
            np.testing.assert_array_almost_equal(geom[0][1], [0.0, 0.0, 0.0])
            np.testing.assert_array_almost_equal(geom[1][1], [0.0, 0.0, 1.0])
            np.testing.assert_array_almost_equal(geom[2][1], [0.0, 1.0, -1.0])
            self.assertIsNone(mol.perturbations)
Beispiel #4
0
    def test_stretch(self):
        """test stretch"""
        geom = None

        with self.subTest("From original"):
            geom = Molecule.absolute_stretching(
                atom_pair=(1, 0),
                perturbation=2,
                geometry=[("H", [0.0, 0.0, 0.0]), ("H", [0.0, 0.0, 1.0])],
            )
            self.assertListEqual(geom[1][1], [0.0, 0.0, 3.0])

        with self.subTest("Reduce stretch"):
            geom = Molecule.absolute_stretching(atom_pair=(1, 0),
                                                perturbation=-0.1,
                                                geometry=geom)
            self.assertListEqual(geom[1][1], [0.0, 0.0, 3.0 - 0.1])
Beispiel #5
0
    def test_bend(self):
        """test bend"""
        with self.subTest("pi/2 bend 1-0-2"):
            geom = Molecule.absolute_bending(
                atom_trio=(1, 0, 2),
                bend=np.pi / 2,
                geometry=[
                    ("H", [0.0, 0.0, 0.0]),
                    ("H", [0.0, 0.0, 1.0]),
                    ("Li", [0.0, 1.0, -1.0]),
                ],
            )
            self.assertListEqual(geom[1][1], [0.0, 1.0, 0.0])

        with self.subTest("-pi/4 bend 1-0-2"):
            geom = Molecule.absolute_bending(atom_trio=(1, 0, 2),
                                             bend=-np.pi / 4,
                                             geometry=geom)
            np.testing.assert_array_almost_equal(
                geom[1][1],
                [0.0, np.sqrt(2) / 2, np.sqrt(2) / 2])

        with self.subTest("-pi/4 bend 2-0-1"):
            geom = Molecule.absolute_bending(atom_trio=(2, 0, 1),
                                             bend=-np.pi / 4,
                                             geometry=geom)
            np.testing.assert_array_almost_equal(geom[2][1],
                                                 [0.0, 0.0, -np.sqrt(2)])

        # Test linear case
        with self.subTest("Linear case"):
            geom = Molecule.absolute_bending(
                atom_trio=(1, 0, 2),
                bend=np.pi / 2,
                geometry=[
                    ("H", [0.0, 0.0, 0.0]),
                    ("H", [0.0, 0.0, 1.0]),
                    ("Li", [0.0, 0.0, -1.0]),
                ],
            )
            self.assertListEqual(geom[1][1], [1.0, 0.0, 0.0])
 def test_driver_molecule(self):
     """Test the driver works with Molecule"""
     molecule = Molecule(
         geometry=[
             ("C", [-0.848629, 2.067624, 0.160992]),
             ("O", [0.098816, 2.655801, -0.159738]),
             ("O", [-1.796073, 1.479446, 0.481721]),
         ],
         multiplicity=1,
         charge=0,
     )
     driver = VibrationalStructureMoleculeDriver(
         molecule,
         basis="6-31g",
         driver_type=VibrationalStructureDriverType.GAUSSIAN_FORCES)
     result = driver.run()
     self._check_driver_result(self._get_expected_values(), result)
Beispiel #7
0
 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])
Beispiel #8
0
class TestDriver(ABC):
    """Common driver tests. For H2 @ 0.735, sto3g"""

    MOLECULE = Molecule(
        geometry=[("H", [0.0, 0.0, 0.0]), ("H", [0.0, 0.0, 0.735])],
        multiplicity=1,
        charge=0,
    )

    def __init__(self):
        self.log = None
        self.driver_result: ElectronicStructureDriverResult = None

    @abstractmethod
    def subTest(self, msg, **kwargs):
        # pylint: disable=invalid-name
        """subtest"""
        raise Exception("Abstract method")

    @abstractmethod
    def assertAlmostEqual(self, first, second, places=None, msg=None, delta=None):
        """assert Almost Equal"""
        raise Exception("Abstract method")

    @abstractmethod
    def assertEqual(self, first, second, msg=None):
        """assert equal"""
        raise Exception("Abstract method")

    @abstractmethod
    def assertSequenceEqual(self, seq1, seq2, msg=None, seq_type=None):
        """assert Sequence Equal"""
        raise Exception("Abstract method")

    def test_driver_result_electronic_energy(self):
        """Test the ElectronicEnergy property."""
        electronic_energy = cast(
            ElectronicEnergy, self.driver_result.get_property(ElectronicEnergy)
        )

        with self.subTest("reference energy"):
            self.log.debug("HF energy: %s", electronic_energy.reference_energy)
            self.assertAlmostEqual(electronic_energy.reference_energy, -1.117, places=3)

        with self.subTest("nuclear repulsion energy"):
            self.log.debug(
                "Nuclear repulsion energy: %s", electronic_energy.nuclear_repulsion_energy
            )
            self.assertAlmostEqual(electronic_energy.nuclear_repulsion_energy, 0.72, places=2)

        with self.subTest("orbital energies"):
            self.log.debug("orbital energies %s", electronic_energy.orbital_energies)
            np.testing.assert_array_almost_equal(
                electronic_energy.orbital_energies, [-0.5806, 0.6763], decimal=4
            )

        with self.subTest("1-body integrals"):
            mo_onee_ints = electronic_energy.get_electronic_integral(ElectronicBasis.MO, 1)
            self.log.debug("MO one electron integrals %s", mo_onee_ints)
            self.assertEqual(mo_onee_ints._matrices[0].shape, (2, 2))
            np.testing.assert_array_almost_equal(
                np.absolute(mo_onee_ints._matrices[0]),
                [[1.2563, 0.0], [0.0, 0.4719]],
                decimal=4,
            )

        with self.subTest("2-body integrals"):
            mo_eri_ints = electronic_energy.get_electronic_integral(ElectronicBasis.MO, 2)
            self.log.debug("MO two electron integrals %s", mo_eri_ints)
            self.assertEqual(mo_eri_ints._matrices[0].shape, (2, 2, 2, 2))
            np.testing.assert_array_almost_equal(
                np.absolute(mo_eri_ints._matrices[0]),
                [
                    [[[0.6757, 0.0], [0.0, 0.6646]], [[0.0, 0.1809], [0.1809, 0.0]]],
                    [[[0.0, 0.1809], [0.1809, 0.0]], [[0.6646, 0.0], [0.0, 0.6986]]],
                ],
                decimal=4,
            )

    def test_driver_result_particle_number(self):
        """Test the ParticleNumber property."""
        particle_number = cast(ParticleNumber, self.driver_result.get_property(ParticleNumber))

        with self.subTest("orbital number"):
            self.log.debug("Number of orbitals is %s", particle_number.num_spin_orbitals)
            self.assertEqual(particle_number.num_spin_orbitals, 4)

        with self.subTest("alpha electron number"):
            self.log.debug("Number of alpha electrons is %s", particle_number.num_alpha)
            self.assertEqual(particle_number.num_alpha, 1)

        with self.subTest("beta electron number"):
            self.log.debug("Number of beta electrons is %s", particle_number.num_beta)
            self.assertEqual(particle_number.num_beta, 1)

    def test_driver_result_molecule(self):
        """Test the Molecule object."""
        molecule = self.driver_result.molecule

        with self.subTest("molecular charge"):
            self.log.debug("molecular charge is %s", molecule.charge)
            self.assertEqual(molecule.charge, 0)

        with self.subTest("multiplicity"):
            self.log.debug("multiplicity is %s", molecule.multiplicity)
            self.assertEqual(molecule.multiplicity, 1)

        with self.subTest("atom number"):
            self.log.debug("num atoms %s", len(molecule.geometry))
            self.assertEqual(len(molecule.geometry), 2)

        with self.subTest("atoms"):
            self.log.debug("atom symbol %s", molecule.atoms)
            self.assertSequenceEqual(molecule.atoms, ["H", "H"])

        with self.subTest("coordinates"):
            coords = [coord for _, coord in molecule.geometry]
            self.log.debug("atom xyz %s", coords)
            np.testing.assert_array_almost_equal(
                coords, [[0.0, 0.0, 0.0], [0.0, 0.0, 0.735]], decimal=4
            )

    def test_driver_result_basis_transform(self):
        """Test the ElectronicBasisTransform object."""
        basis_transform = cast(
            ElectronicBasisTransform, self.driver_result.get_property(ElectronicBasisTransform)
        )

        self.log.debug("MO coeffs xyz %s", basis_transform.coeff_alpha)
        self.assertEqual(basis_transform.coeff_alpha.shape, (2, 2))
        np.testing.assert_array_almost_equal(
            np.absolute(basis_transform.coeff_alpha),
            [[0.5483, 1.2183], [0.5483, 1.2183]],
            decimal=4,
        )

    def test_driver_result_electronic_dipole(self):
        """Test the ElectronicDipoleMoment property."""
        dipole = self.driver_result.get_property(ElectronicDipoleMoment)

        self.log.debug("has dipole integrals %s", dipole is not None)
        if dipole is not None:
            dipole = cast(ElectronicDipoleMoment, dipole)

            with self.subTest("x axis"):
                mo_x_dip_ints = dipole._dipole_axes["DipoleMomentX"].get_electronic_integral(
                    ElectronicBasis.MO, 1
                )
                self.assertEqual(mo_x_dip_ints._matrices[0].shape, (2, 2))
                np.testing.assert_array_almost_equal(
                    np.absolute(mo_x_dip_ints._matrices[0]), [[0.0, 0.0], [0.0, 0.0]], decimal=4
                )

            with self.subTest("y axis"):
                mo_y_dip_ints = dipole._dipole_axes["DipoleMomentY"].get_electronic_integral(
                    ElectronicBasis.MO, 1
                )
                self.assertEqual(mo_y_dip_ints._matrices[0].shape, (2, 2))
                np.testing.assert_array_almost_equal(
                    np.absolute(mo_y_dip_ints._matrices[0]), [[0.0, 0.0], [0.0, 0.0]], decimal=4
                )

            with self.subTest("z axis"):
                mo_z_dip_ints = dipole._dipole_axes["DipoleMomentZ"].get_electronic_integral(
                    ElectronicBasis.MO, 1
                )
                self.assertEqual(mo_z_dip_ints._matrices[0].shape, (2, 2))
                np.testing.assert_array_almost_equal(
                    np.absolute(mo_z_dip_ints._matrices[0]),
                    [[0.6945, 0.9278], [0.9278, 0.6945]],
                    decimal=4,
                )

            with self.subTest("nuclear dipole moment"):
                np.testing.assert_array_almost_equal(
                    np.absolute(dipole.nuclear_dipole_moment), [0.0, 0.0, 1.3889], decimal=4
                )
Beispiel #9
0
    def test_construct(self):
        """test construct"""
        stretch = partial(Molecule.absolute_stretching,
                          kwargs={"atom_pair": (1, 0)})

        with self.subTest("Masses supplied"):
            mol = Molecule(
                geometry=[("H", [0.0, 0.0, 0.0]), ("H", [0.0, 0.0, 1.0])],
                degrees_of_freedom=[stretch],
                masses=[1, 1],
            )
            self.assertEqual(mol.units, UnitsType.ANGSTROM)
            self.assertListEqual(mol.geometry, [("H", [0.0, 0.0, 0.0]),
                                                ("H", [0.0, 0.0, 1.0])])
            self.assertEqual(mol.multiplicity, 1)
            self.assertEqual(mol.charge, 0)
            self.assertIsNone(mol.perturbations)
            self.assertListEqual(mol.masses, [1, 1])

        with self.subTest("No masses"):
            mol = Molecule(
                geometry=[("H", [0.0, 0.0, 0.0]), ("H", [0.0, 0.0, 1.0])],
                degrees_of_freedom=[stretch],
            )
            self.assertEqual(mol.units, UnitsType.ANGSTROM)
            self.assertIsNone(mol.masses)

        with self.subTest("All params"):
            mol = Molecule(
                geometry=[("H", [0.0, 0.0, 0.0]), ("H", [0.0, 0.0, 1.0])],
                multiplicity=2,
                charge=1,
                degrees_of_freedom=[stretch],
                masses=[0.7, 0.8],
            )
            self.assertEqual(mol.units, UnitsType.ANGSTROM)
            self.assertEqual(mol.multiplicity, 2)
            self.assertEqual(mol.charge, 1)
            self.assertIsNone(mol.perturbations)
            self.assertListEqual(mol.masses, [0.7, 0.8])

        with self.subTest("Mismatched masses length"):
            with self.assertRaises(ValueError):
                Molecule(
                    geometry=[("H", [0.0, 0.0, 0.0]), ("H", [0.0, 0.0, 1.0])],
                    masses=[1, 1, 1],
                )

        with self.subTest("Explicit Angstrom units"):
            mol = Molecule(
                geometry=[("H", [0.0, 0.0, 0.0]), ("H", [0.0, 0.0, 1.0])],
                units=UnitsType.ANGSTROM,
            )
            self.assertEqual(mol.units, UnitsType.ANGSTROM)
            self.assertListEqual(mol.geometry, [("H", [0.0, 0.0, 0.0]),
                                                ("H", [0.0, 0.0, 1.0])])

        with self.subTest("Bohr units"):
            mol = Molecule(
                geometry=[("H", [0.0, 0.0, 0.0]), ("H", [0.0, 0.0, 1.0])],
                units=UnitsType.BOHR,
            )
            self.assertEqual(mol.units, UnitsType.BOHR)
            self.assertListEqual(mol.geometry, [("H", [0.0, 0.0, 0.0]),
                                                ("H", [0.0, 0.0, 1.0])])