Exemplo n.º 1
0
    def test_minimal_active_space(self):
        """Test a minimal active space manually."""
        driver = HDF5Driver(
            hdf5_input=self.get_resource_path('H2_631g.hdf5', 'transformers'))
        q_molecule = driver.run()

        trafo = ActiveSpaceTransformer(num_electrons=2, num_orbitals=2)
        q_molecule_reduced = trafo.transform(q_molecule)

        expected_mo_onee_ints = np.asarray([[-1.24943841, 0.0],
                                            [0.0, -0.547816138]])
        expected_mo_eri_ints = np.asarray([[[[0.652098466, 0.0],
                                             [0.0, 0.433536565]],
                                            [[0.0, 0.0794483182],
                                             [0.0794483182, 0.0]]],
                                           [[[0.0, 0.0794483182],
                                             [0.0794483182, 0.0]],
                                            [[0.433536565, 0.0],
                                             [0.0, 0.385524695]]]])

        assert np.allclose(q_molecule_reduced.mo_onee_ints,
                           expected_mo_onee_ints)
        assert np.allclose(q_molecule_reduced.mo_eri_ints,
                           expected_mo_eri_ints)
        assert np.isclose(
            q_molecule_reduced.energy_shift['ActiveSpaceTransformer'], 0.0)
    def test_minimal_active_space(self):
        """Test a minimal active space manually."""
        driver = HDF5Driver(hdf5_input=self.get_resource_path(
            "H2_631g.hdf5", "transformers/second_quantization/electronic"))
        q_molecule = driver.run()

        trafo = ActiveSpaceTransformer(num_electrons=2,
                                       num_molecular_orbitals=2)
        q_molecule_reduced = trafo.transform(q_molecule)

        expected = QMolecule()
        expected.mo_onee_ints = np.asarray([[-1.24943841, 0.0],
                                            [0.0, -0.547816138]])
        expected.mo_eri_ints = np.asarray([
            [
                [[0.652098466, 0.0], [0.0, 0.433536565]],
                [[0.0, 0.0794483182], [0.0794483182, 0.0]],
            ],
            [
                [[0.0, 0.0794483182], [0.0794483182, 0.0]],
                [[0.433536565, 0.0], [0.0, 0.385524695]],
            ],
        ])

        expected.x_dip_mo_ints = np.zeros((2, 2))
        expected.y_dip_mo_ints = np.zeros((2, 2))
        expected.z_dip_mo_ints = np.asarray([[0.69447435, -1.01418298],
                                             [-1.01418298, 0.69447435]])

        expected.energy_shift["ActiveSpaceTransformer"] = 0.0
        expected.x_dip_energy_shift["ActiveSpaceTransformer"] = 0.0
        expected.y_dip_energy_shift["ActiveSpaceTransformer"] = 0.0
        expected.z_dip_energy_shift["ActiveSpaceTransformer"] = 0.0

        self.assertQMolecule(q_molecule_reduced, expected)
Exemplo n.º 3
0
    def test_arbitrary_active_orbitals(self):
        """Test manual selection of active orbital indices."""
        driver = HDF5Driver(
            hdf5_input=self.get_resource_path('H2_631g.hdf5', 'transformers'))
        q_molecule = driver.run()

        trafo = ActiveSpaceTransformer(num_electrons=2,
                                       num_orbitals=2,
                                       active_orbitals=[0, 2])
        q_molecule_reduced = trafo.transform(q_molecule)

        expected_mo_onee_ints = np.asarray([[-1.24943841, -0.16790838],
                                            [-0.16790838, -0.18307469]])
        expected_mo_eri_ints = np.asarray([[[[0.65209847, 0.16790822],
                                             [0.16790822, 0.53250905]],
                                            [[0.16790822, 0.10962908],
                                             [0.10962908, 0.11981429]]],
                                           [[[0.16790822, 0.10962908],
                                             [0.10962908, 0.11981429]],
                                            [[0.53250905, 0.11981429],
                                             [0.11981429, 0.46345617]]]])

        assert np.allclose(q_molecule_reduced.mo_onee_ints,
                           expected_mo_onee_ints)
        assert np.allclose(q_molecule_reduced.mo_eri_ints,
                           expected_mo_eri_ints)
        assert np.isclose(
            q_molecule_reduced.energy_shift['ActiveSpaceTransformer'], 0.0)
    def test_unpaired_electron_active_space(self):
        """Test an active space with an unpaired electron."""
        driver = HDF5Driver(hdf5_input=self.get_resource_path('BeH_sto3g.hdf5', 'transformers'))
        q_molecule = driver.run()

        trafo = ActiveSpaceTransformer(num_electrons=(2, 1), num_molecular_orbitals=3)
        q_molecule_reduced = trafo.transform(q_molecule)

        expected = HDF5Driver(hdf5_input=self.get_resource_path('BeH_sto3g_reduced.hdf5',
                                                                'transformers')).run()

        self.assertQMolecule(q_molecule_reduced, expected)
Exemplo n.º 5
0
    def test_unpaired_electron_active_space(self):
        """Test an active space with an unpaired electron."""
        driver = HDF5Driver(hdf5_input=self.get_resource_path(
            'BeH_sto3g.hdf5', 'transformers'))
        q_molecule = driver.run()

        trafo = ActiveSpaceTransformer(num_electrons=3,
                                       num_orbitals=3,
                                       num_alpha=2)
        q_molecule_reduced = trafo.transform(q_molecule)

        expected_mo_onee_ints = np.asarray([[-1.30228816, 0.03573328, 0.0],
                                            [0.03573328, -0.86652349, 0.0],
                                            [0.0, 0.0, -0.84868407]])

        expected_mo_eri_ints = np.asarray([[[[0.57237421, -0.05593597, 0.0],
                                             [-0.05593597, 0.30428426, 0.0],
                                             [0.0, 0.0, 0.36650821]],
                                            [[-0.05593597, 0.01937529, 0.0],
                                             [0.01937529, 0.02020237, 0.0],
                                             [0.0, 0.0, 0.01405676]],
                                            [[0.0, 0.0, 0.03600701],
                                             [0.0, 0.0, 0.028244],
                                             [0.03600701, 0.028244, 0.0]]],
                                           [[[-0.05593597, 0.01937529, 0.0],
                                             [0.01937529, 0.02020237, 0.0],
                                             [0.0, 0.0, 0.01405676]],
                                            [[0.30428426, 0.02020237, 0.0],
                                             [0.02020237, 0.48162669, 0.0],
                                             [0.0, 0.0, 0.40269913]],
                                            [[0.0, 0.0, 0.028244],
                                             [0.0, 0.0, 0.0564951],
                                             [0.028244, 0.0564951, 0.0]]],
                                           [[[0.0, 0.0, 0.03600701],
                                             [0.0, 0.0, 0.028244],
                                             [0.03600701, 0.028244, 0.0]],
                                            [[0.0, 0.0, 0.028244],
                                             [0.0, 0.0, 0.0564951],
                                             [0.028244, 0.0564951, 0.0]],
                                            [[0.36650821, 0.01405676, 0.0],
                                             [0.01405676, 0.40269913, 0.0],
                                             [0.0, 0.0, 0.44985904]]]])

        assert np.allclose(q_molecule_reduced.mo_onee_ints,
                           expected_mo_onee_ints)
        assert np.allclose(q_molecule_reduced.mo_eri_ints,
                           expected_mo_eri_ints)
        assert np.isclose(
            q_molecule_reduced.energy_shift['ActiveSpaceTransformer'],
            -14.2538029231)
    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', 'transformers'))
        q_molecule = driver.run()

        q_molecule.energy_shift['ActiveSpaceTransformer'] = 0.0
        q_molecule.x_dip_energy_shift['ActiveSpaceTransformer'] = 0.0
        q_molecule.y_dip_energy_shift['ActiveSpaceTransformer'] = 0.0
        q_molecule.z_dip_energy_shift['ActiveSpaceTransformer'] = 0.0

        trafo = ActiveSpaceTransformer(**kwargs)
        q_molecule_reduced = trafo.transform(q_molecule)

        self.assertQMolecule(q_molecule_reduced, q_molecule)
    def test_active_space_for_q_molecule_v2(self):
        """Test based on QMolecule v2 (mo_occ not available)."""
        driver = HDF5Driver(hdf5_input=self.get_resource_path('H2_sto3g_v2.hdf5', 'transformers'))
        q_molecule = driver.run()

        q_molecule.energy_shift['ActiveSpaceTransformer'] = 0.0
        q_molecule.x_dip_energy_shift['ActiveSpaceTransformer'] = 0.0
        q_molecule.y_dip_energy_shift['ActiveSpaceTransformer'] = 0.0
        q_molecule.z_dip_energy_shift['ActiveSpaceTransformer'] = 0.0

        trafo = ActiveSpaceTransformer(num_electrons=2, num_molecular_orbitals=2)
        q_molecule_reduced = trafo.transform(q_molecule)

        self.assertQMolecule(q_molecule_reduced, q_molecule)
Exemplo n.º 8
0
    def test_full_active_space(self):
        """Test that transformer has no effect when all orbitals are active."""
        driver = HDF5Driver(
            hdf5_input=self.get_resource_path('H2_sto3g.hdf5', 'transformers'))
        q_molecule = driver.run()

        trafo = ActiveSpaceTransformer(num_electrons=2, num_orbitals=2)
        q_molecule_reduced = trafo.transform(q_molecule)

        assert np.allclose(q_molecule_reduced.mo_onee_ints,
                           q_molecule.mo_onee_ints)
        assert np.allclose(q_molecule_reduced.mo_eri_ints,
                           q_molecule.mo_eri_ints)
        assert np.isclose(
            q_molecule_reduced.energy_shift['ActiveSpaceTransformer'], 0.0)
Exemplo n.º 9
0
    def test_active_space_for_q_molecule_v2(self):
        """Test based on QMolecule v2 (mo_occ not available)."""
        driver = HDF5Driver(hdf5_input=self.get_resource_path(
            'H2_sto3g_v2.hdf5', 'transformers'))
        q_molecule = driver.run()

        trafo = ActiveSpaceTransformer(num_electrons=2, num_orbitals=2)
        q_molecule_reduced = trafo.transform(q_molecule)

        assert np.allclose(q_molecule_reduced.mo_onee_ints,
                           q_molecule.mo_onee_ints)
        assert np.allclose(q_molecule_reduced.mo_eri_ints,
                           q_molecule.mo_eri_ints)
        assert np.isclose(
            q_molecule_reduced.energy_shift['ActiveSpaceTransformer'], 0.0)
    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_error_raising(self, num_electrons, num_molecular_orbitals, active_orbitals, message):
        """Test errors are being raised in certain scenarios."""
        driver = HDF5Driver(hdf5_input=self.get_resource_path('H2_sto3g.hdf5', 'transformers'))
        q_molecule = driver.run()

        with self.assertRaises(QiskitNatureError, msg=message):
            ActiveSpaceTransformer(num_electrons=num_electrons,
                                   num_molecular_orbitals=num_molecular_orbitals,
                                   active_orbitals=active_orbitals).transform(q_molecule)
    def test_arbitrary_active_orbitals(self):
        """Test manual selection of active orbital indices."""
        driver = HDF5Driver(hdf5_input=self.get_resource_path(
            "H2_631g.hdf5", "transformers/second_quantization/electronic"))
        q_molecule = driver.run()

        trafo = ActiveSpaceTransformer(num_electrons=2,
                                       num_molecular_orbitals=2,
                                       active_orbitals=[0, 2])
        q_molecule_reduced = trafo.transform(q_molecule)

        expected = QMolecule()
        expected.mo_onee_ints = np.asarray([[-1.24943841, -0.16790838],
                                            [-0.16790838, -0.18307469]])
        expected.mo_eri_ints = np.asarray([
            [
                [[0.65209847, 0.16790822], [0.16790822, 0.53250905]],
                [[0.16790822, 0.10962908], [0.10962908, 0.11981429]],
            ],
            [
                [[0.16790822, 0.10962908], [0.10962908, 0.11981429]],
                [[0.53250905, 0.11981429], [0.11981429, 0.46345617]],
            ],
        ])

        expected.x_dip_mo_ints = np.zeros((2, 2))
        expected.y_dip_mo_ints = np.zeros((2, 2))
        expected.z_dip_mo_ints = np.asarray([[0.69447435, 0.0],
                                             [0.0, 0.69447435]])

        expected.energy_shift["ActiveSpaceTransformer"] = 0.0
        expected.x_dip_energy_shift["ActiveSpaceTransformer"] = 0.0
        expected.y_dip_energy_shift["ActiveSpaceTransformer"] = 0.0
        expected.z_dip_energy_shift["ActiveSpaceTransformer"] = 0.0

        self.assertQMolecule(q_molecule_reduced, expected)