예제 #1
0
    def test_no_deep_copy(self):
        """Test that objects are not being deeply copied.

        This is a regression test against the fix applied by
        https://github.com/Qiskit/qiskit-nature/pull/659
        """
        driver = HDF5Driver(hdf5_input=self.get_resource_path(
            "H2_631g.hdf5", "transformers/second_quantization/electronic"))
        driver_result = driver.run()

        trafo = ActiveSpaceTransformer(num_electrons=2,
                                       num_molecular_orbitals=2)
        driver_result_reduced = trafo.transform(driver_result)

        active_transform = np.asarray([
            [0.32774803333032304, 0.12166492852424596],
            [0.27055282555225113, 1.7276386116201712],
            [0.32774803333032265, -0.12166492852424832],
            [0.2705528255522547, -1.727638611620168],
        ])

        self.assertTrue(
            np.allclose(
                driver_result_reduced.get_property(
                    "ElectronicBasisTransform").coeff_alpha,
                active_transform,
            ))
예제 #2
0
    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/second_quantization/electronic"))
        driver_result = driver.run()

        driver_result.get_property(
            "ElectronicEnergy")._shift["ActiveSpaceTransformer"] = 0.0
        for prop in iter(driver_result.get_property("ElectronicDipoleMoment")):
            prop._shift["ActiveSpaceTransformer"] = 0.0

        trafo = ActiveSpaceTransformer(**kwargs)
        driver_result_reduced = trafo.transform(driver_result)

        self.assertDriverResult(driver_result_reduced, driver_result)
예제 #3
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/second_quantization/electronic"))
        driver_result = driver.run()

        trafo = ActiveSpaceTransformer(num_electrons=(2, 1),
                                       num_molecular_orbitals=3)
        driver_result_reduced = trafo.transform(driver_result)

        expected = HDF5Driver(hdf5_input=self.get_resource_path(
            "BeH_sto3g_reduced.hdf5",
            "transformers/second_quantization/electronic")).run()

        self.assertDriverResult(driver_result_reduced, expected)
예제 #4
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/second_quantization/electronic"))
        driver_result = driver.run()

        driver_result.get_property(
            "ElectronicEnergy")._shift["ActiveSpaceTransformer"] = 0.0
        for prop in iter(driver_result.get_property("ElectronicDipoleMoment")):
            prop._shift["ActiveSpaceTransformer"] = 0.0

        trafo = ActiveSpaceTransformer(num_electrons=2,
                                       num_molecular_orbitals=2)
        driver_result_reduced = trafo.transform(driver_result)

        self.assertDriverResult(driver_result_reduced, driver_result)
예제 #5
0
    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/second_quantization/electronic"))
        driver_result = driver.run()

        with self.assertRaises(QiskitNatureError, msg=message):
            ActiveSpaceTransformer(
                num_electrons=num_electrons,
                num_molecular_orbitals=num_molecular_orbitals,
                active_orbitals=active_orbitals,
            ).transform(driver_result)
예제 #6
0
    def test_numpy_integer(self):
        """Tests that numpy integer objects do not cause issues in `isinstance` checks.

        This is a regression test against the fix applied by
        https://github.com/Qiskit/qiskit-nature/pull/712
        """
        driver = HDF5Driver(hdf5_input=self.get_resource_path(
            "H2_631g.hdf5", "transformers/second_quantization/electronic"))
        driver_result = driver.run()

        particle_number = driver_result.get_property("ParticleNumber")
        particle_number.num_alpha = np.int64(particle_number.num_alpha)
        particle_number.num_beta = np.int64(particle_number.num_beta)
        particle_number.num_spin_orbitals = np.int64(
            particle_number.num_spin_orbitals)

        driver_result.add_property(particle_number)

        trafo = ActiveSpaceTransformer(
            num_electrons=particle_number.num_particles,
            num_molecular_orbitals=2)
        _ = trafo.transform(driver_result)
    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/second_quantization/electronic"))
        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[
            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()))
예제 #8
0
    def test_LiH(self):
        """Lih test"""
        driver = PySCFDriver(
            atom="Li .0 .0 .0; H .0 .0 1.6",
            unit=UnitsType.ANGSTROM,
            basis="sto3g",
        )
        transformer = ActiveSpaceTransformer(num_electrons=2,
                                             num_molecular_orbitals=3)
        problem = ElectronicStructureProblem(driver, [transformer])

        solver = VQEUCCFactory(quantum_instance=QuantumInstance(
            BasicAer.get_backend("statevector_simulator")))
        calc = AdaptVQE(self.qubit_converter, solver)
        res = calc.solve(problem)
        self.assertAlmostEqual(res.electronic_energies[0],
                               -8.855126478,
                               places=6)
예제 #9
0
    def test_tuple_num_electrons_with_manual_orbitals(self):
        """Regression test against https://github.com/Qiskit/qiskit-nature/issues/434."""
        driver = HDF5Driver(hdf5_input=self.get_resource_path(
            "H2_631g.hdf5", "transformers/second_quantization/electronic"))
        driver_result = driver.run()

        trafo = ActiveSpaceTransformer(
            num_electrons=(1, 1),
            num_molecular_orbitals=2,
            active_orbitals=[0, 1],
        )
        driver_result_reduced = trafo.transform(driver_result)

        expected = ElectronicStructureDriverResult()
        expected.add_property(
            ElectronicEnergy(
                [
                    OneBodyElectronicIntegrals(
                        ElectronicBasis.MO,
                        (np.asarray([[-1.24943841, 0.0], [0.0, -0.547816138]
                                     ]), None),
                    ),
                    TwoBodyElectronicIntegrals(
                        ElectronicBasis.MO,
                        (
                            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]],
                                ],
                            ]),
                            None,
                            None,
                            None,
                        ),
                    ),
                ],
                energy_shift={"ActiveSpaceTransformer": 0.0},
            ))
        expected.add_property(
            ElectronicDipoleMoment([
                DipoleMoment(
                    "x",
                    [
                        OneBodyElectronicIntegrals(ElectronicBasis.MO,
                                                   (np.zeros((2, 2)), None))
                    ],
                    shift={"ActiveSpaceTransformer": 0.0},
                ),
                DipoleMoment(
                    "y",
                    [
                        OneBodyElectronicIntegrals(ElectronicBasis.MO,
                                                   (np.zeros((2, 2)), None))
                    ],
                    shift={"ActiveSpaceTransformer": 0.0},
                ),
                DipoleMoment(
                    "z",
                    [
                        OneBodyElectronicIntegrals(
                            ElectronicBasis.MO,
                            (
                                np.asarray([[0.69447435, -1.01418298],
                                            [-1.01418298, 0.69447435]]),
                                None,
                            ),
                        )
                    ],
                    shift={"ActiveSpaceTransformer": 0.0},
                ),
            ]))

        self.assertDriverResult(driver_result_reduced, expected)
예제 #10
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/second_quantization/electronic"))
        driver_result = driver.run()

        trafo = ActiveSpaceTransformer(num_electrons=2,
                                       num_molecular_orbitals=2,
                                       active_orbitals=[0, 2])
        driver_result_reduced = trafo.transform(driver_result)

        expected = ElectronicStructureDriverResult()
        expected.add_property(
            ElectronicEnergy(
                [
                    OneBodyElectronicIntegrals(
                        ElectronicBasis.MO,
                        (
                            np.asarray([[-1.24943841, -0.16790838],
                                        [-0.16790838, -0.18307469]]),
                            None,
                        ),
                    ),
                    TwoBodyElectronicIntegrals(
                        ElectronicBasis.MO,
                        (
                            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]],
                                ],
                            ]),
                            None,
                            None,
                            None,
                        ),
                    ),
                ],
                energy_shift={"ActiveSpaceTransformer": 0.0},
            ))
        expected.add_property(
            ElectronicDipoleMoment([
                DipoleMoment(
                    "x",
                    [
                        OneBodyElectronicIntegrals(ElectronicBasis.MO,
                                                   (np.zeros((2, 2)), None))
                    ],
                    shift={"ActiveSpaceTransformer": 0.0},
                ),
                DipoleMoment(
                    "y",
                    [
                        OneBodyElectronicIntegrals(ElectronicBasis.MO,
                                                   (np.zeros((2, 2)), None))
                    ],
                    shift={"ActiveSpaceTransformer": 0.0},
                ),
                DipoleMoment(
                    "z",
                    [
                        OneBodyElectronicIntegrals(
                            ElectronicBasis.MO,
                            (np.asarray([[0.69447435, 0.0], [0.0, 0.69447435]
                                         ]), None),
                        )
                    ],
                    shift={"ActiveSpaceTransformer": 0.0},
                ),
            ]))
        self.assertDriverResult(driver_result_reduced, expected)