Example #1
0
    def run_vqe(H, ansatz, params=None):
        from pennylane.optimize import NesterovMomentumOptimizer, AdamOptimizer
        num_qubits = len(H.wires)
        num_layers = 4

        if params is None:
            params = qml.init.strong_ent_layers_uniform(num_layers, num_qubits, 3)

        cost_fn = qml.ExpvalCost(ansatz, H, dev)

        stepsize = 0.1
        opt = NesterovMomentumOptimizer(stepsize)
        max_iterations = 300
        conv_tol = 1e-8

        energy = 0

        for n in range(max_iterations):
            params, prev_energy = opt.step_and_cost(cost_fn, params)
            energy = cost_fn(params)
            conv = np.abs(energy - prev_energy)

            stepsize *= 0.99
            opt.update_stepsize(stepsize)

            if DEBUG and n % 20 == 0:
                print('Iteration = {:},  Energy = {:.8f} Ha'.format(n, energy))

            if conv <= conv_tol:
                break

        return energy, params
Example #2
0
    def run_vqe_excited2(H, ansatz, gs_params, fes_params, params=None):
        from pennylane.optimize import NesterovMomentumOptimizer, AdamOptimizer
        num_qubits = len(H.wires)
        num_layers = 4

        if params is None:
            params = qml.init.strong_ent_layers_uniform(num_layers, num_qubits, 3)

        @qml.qnode(dev)
        def overlap(params, wires):
            variational_ansatz(gs_params, wires)
            qml.inv(qml.template(variational_ansatz)(params, wires))
            return qml.probs([0, 1, 2])

        @qml.qnode(dev)
        def overlap2(params, wires):
            variational_ansatz(fes_params, wires)
            qml.inv(qml.template(variational_ansatz)(params, wires))
            return qml.probs([0, 1, 2])

        def cost_fn(params, **kwargs):
            h_cost = qml.ExpvalCost(ansatz, H, dev)
            h = h_cost(params, **kwargs)
            o = overlap(params, wires=H.wires)
            o2 = overlap2(params, wires=H.wires)
            return h + 1.5 * o[0] + o2[0]

        stepsize = 0.3
        opt = NesterovMomentumOptimizer(stepsize)
        max_iterations = 300
        conv_tol = 1e-8

        energy = 0

        for n in range(max_iterations):
            params, prev_energy = opt.step_and_cost(cost_fn, params)
            energy = cost_fn(params)
            conv = np.abs(energy - prev_energy)

            stepsize *= 0.99
            opt.update_stepsize(stepsize)

            if DEBUG and n % 20 == 0:
                print('Iteration = {:},  Energy = {:.8f} Ha'.format(n, energy))

            if conv <= conv_tol:
                break

        return energy, params