Пример #1
0
 def test_default(self):
     """ Default execution """
     solver = VQEUCCSDFactory(
         QuantumInstance(BasicAer.get_backend('statevector_simulator')))
     calc = AdaptVQE(self.transformation, solver)
     res = calc.solve(self.driver)
     self.assertAlmostEqual(res.electronic_energy, self.expected, places=6)
Пример #2
0
    def test_custom_minimum_eigensolver(self):
        """ Test custom MES """

        # Note: the VQEUCCSDFactory actually allows to specify an optimizer through its constructor.
        # Thus, this example is quite far fetched but for a proof-of-principle test it still works.
        class CustomFactory(VQEUCCSDFactory):
            """A custom MESFactory"""
            def get_solver(self, transformation):
                num_orbitals = transformation.molecule_info['num_orbitals']
                num_particles = transformation.molecule_info['num_particles']
                qubit_mapping = transformation.qubit_mapping
                two_qubit_reduction = transformation.molecule_info[
                    'two_qubit_reduction']
                z2_symmetries = transformation.molecule_info['z2_symmetries']
                initial_state = HartreeFock(num_orbitals, num_particles,
                                            qubit_mapping, two_qubit_reduction,
                                            z2_symmetries.sq_list)
                var_form = UCCSD(num_orbitals=num_orbitals,
                                 num_particles=num_particles,
                                 initial_state=initial_state,
                                 qubit_mapping=qubit_mapping,
                                 two_qubit_reduction=two_qubit_reduction,
                                 z2_symmetries=z2_symmetries)
                vqe = VQE(var_form=var_form,
                          quantum_instance=self._quantum_instance,
                          optimizer=L_BFGS_B())
                return vqe

        solver = CustomFactory(
            QuantumInstance(BasicAer.get_backend('statevector_simulator')))

        calc = AdaptVQE(self.transformation, solver)
        res = calc.solve(self.driver)
        self.assertAlmostEqual(res.electronic_energy, self.expected, places=6)
Пример #3
0
    def test_aux_ops_reusability(self):
        """ Test that the auxiliary operators can be reused """
        # Regression test against #1475
        solver = VQEUCCSDFactory(QuantumInstance(BasicAer.get_backend('statevector_simulator')))
        calc = AdaptVQE(self.transformation, solver)

        modes = 4
        h_1 = np.eye(modes, dtype=complex)
        h_2 = np.zeros((modes, modes, modes, modes))
        aux_ops = [FermionicOperator(h_1, h_2)]
        aux_ops_copy = copy.deepcopy(aux_ops)

        _ = calc.solve(self.driver, aux_ops)
        assert all([a == b for a, b in zip(aux_ops, aux_ops_copy)])
Пример #4
0
    def test_custom_excitation_pool(self):
        """ Test custom excitation pool """
        class CustomFactory(VQEUCCSDFactory):
            """A custom MES factory."""
            def get_solver(self, transformation):
                solver = super().get_solver(transformation)
                # Here, we can create essentially any custom excitation pool.
                # For testing purposes only, we simply select some hopping operator already
                # available in the variational form object.
                # pylint: disable=no-member
                custom_excitation_pool = [solver.var_form._hopping_ops[2]]
                solver.var_form.excitation_pool = custom_excitation_pool
                return solver

        solver = CustomFactory(
            QuantumInstance(BasicAer.get_backend('statevector_simulator')))
        calc = AdaptVQE(self.transformation, solver)
        res = calc.solve(self.driver)
        self.assertAlmostEqual(res.electronic_energy, self.expected, places=6)