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
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
def test_step_and_cost_autograd_nesterov_multid_array(self): """Test that the correct cost is returned via the step_and_cost method for the Nesterov momentum optimizer""" stepsize, gamma = 0.1, 0.5 nesmom_opt = NesterovMomentumOptimizer(stepsize, momentum=gamma) multid_array = np.array([[0.1, 0.2], [-0.1, -0.4]]) @qml.qnode(qml.device("default.qubit", wires=1)) def quant_fun_mdarr(var): qml.RX(var[0, 1], wires=[0]) qml.RY(var[1, 0], wires=[0]) qml.RY(var[1, 1], wires=[0]) return qml.expval(qml.PauliZ(0)) _, res = nesmom_opt.step_and_cost(quant_fun_mdarr, multid_array) expected = quant_fun_mdarr(multid_array) assert np.all(res == expected)
def test_step_and_cost_autograd_nesterov_multiple_inputs(self): """Test that the correct cost is returned via the step_and_cost method for the Nesterov momentum optimizer""" stepsize, gamma = 0.1, 0.5 nesmom_opt = NesterovMomentumOptimizer(stepsize, momentum=gamma) @qml.qnode(qml.device("default.qubit", wires=1)) def quant_fun(*variables): qml.RX(variables[0][1], wires=[0]) qml.RY(variables[1][2], wires=[0]) qml.RY(variables[2], wires=[0]) return qml.expval(qml.PauliZ(0)) inputs = [ np.array((0.2, 0.3), requires_grad=True), np.array([0.4, 0.2, 0.4], requires_grad=True), np.array(0.1, requires_grad=True), ] _, res = nesmom_opt.step_and_cost(quant_fun, *inputs) expected = quant_fun(*inputs) assert np.all(res == expected)