コード例 #1
0
    def vqe_run(self,
                ansatz,
                init_guess_parameters=None,
                init_state_qasm=None,
                excited_state=0,
                cache=None):

        assert len(ansatz) > 0
        if init_guess_parameters is None:
            var_parameters = numpy.zeros(
                sum([element.n_var_parameters for element in ansatz]))
        else:
            assert len(init_guess_parameters) == sum(
                [element.n_var_parameters for element in ansatz])
            var_parameters = init_guess_parameters

        LogUtils.vqe_info(self.q_system, self.backend, self.optimizer, ansatz)

        self.iteration = 1
        self.time_previous_iter = time.time()

        # functions to be called by the optimizer
        get_energy = partial(self.get_energy,
                             ansatz=ansatz,
                             backend=self.backend,
                             init_state_qasm=init_state_qasm,
                             excited_state=excited_state,
                             cache=cache)

        get_gradient = partial(self.backend.ansatz_gradient,
                               ansatz=ansatz,
                               q_system=self.q_system,
                               init_state_qasm=init_state_qasm,
                               cache=cache,
                               excited_state=excited_state)

        if self.use_ansatz_gradient:
            result = scipy.optimize.minimize(get_energy,
                                             var_parameters,
                                             jac=get_gradient,
                                             method=self.optimizer,
                                             options=self.optimizer_options,
                                             tol=config.optimizer_tol,
                                             bounds=config.optimizer_bounds)
        else:

            result = scipy.optimize.minimize(get_energy,
                                             var_parameters,
                                             method=self.optimizer,
                                             options=self.optimizer_options,
                                             tol=config.optimizer_tol,
                                             bounds=config.optimizer_bounds)

        result['n_iters'] = self.iteration  # cheating

        return result
コード例 #2
0
                           optimizer_options={'gtol': 1e-08},
                           use_ansatz_gradient=use_energy_vector_gradient)

    # create a vqe_runner for excited states, where the minimum may be away from the zero, which will make gradient
    # descent optimizers useless

    # vqe_runner_2 = VQERunner(molecule, backend=QiskitSim, optimizer='BFGS', optimizer_options={'gtol': 1e-08},
    #                          use_ansatz_gradient=use_grad)
    vqe_runner_2 = VQERunner(molecule,
                             backend=backend,
                             optimizer='Nelder-Mead',
                             optimizer_options=None)

    # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    LogUtils.log_config()
    logging.info('{}, r={} ,{}'.format(molecule.name, r, ansatz_element_type))
    time_stamp = datetime.datetime.now().strftime("%d-%b-%Y (%H:%M:%S.%f)")

    # create the pool of ansatz elements
    if spin_complement:
        ansatz_element_pool = SpinCompGSDExcitations(
            molecule.n_orbitals,
            molecule.n_electrons,
            element_type=ansatz_element_type).get_excitations()
    else:
        ansatz_element_pool = GSDExcitations(
            molecule.n_orbitals,
            molecule.n_electrons,
            ansatz_element_type=ansatz_element_type).get_excitations()