I try to make VQE optimizer, but something goes wrong at the optimization stage, more precisely, the parameters do not change and there is no gradient descent. Please tell me what I'm doing wrong?
import pennylane as qml
from pennylane import qaoa
from pennylane import numpy as np
from matplotlib import pyplot as plt
import networkx as nx
numer_stability_const = 0.1
op_new = sparse.csr_matrix(op)
ham = qml.SparseHamiltonian(op_new * numer_stability_const, wires=list(range(16)))
size = len(ham.wires)
dev = qml.device("default.qubit", wires=size)
def circuit(params):
for i in range(size):
qml.RY(params[0][i][0], wires=i)
qml.RZ(params[0][i][1], wires=i)
for i in range(size - 1):
qml.CZ(wires=[i, i + 1])
def layer(params):
for i in range(size):
qml.RY(params[i][0], wires=i)
qml.RZ(params[i][1], wires=i)
for i in range(size - 1):
qml.CZ(wires=[i, i + 1])
device = qml.device("default.qubit", wires=size)
num_layers = 3
@qml.qnode(device, diff_method="parameter-shift")
def feed_forward(params, wires=size):
for k in range(3):
layer(params[k])
for i in range(size):
qml.RY(params[-1][i][0], wires=i)
qml.RZ(params[-1][i][1], wires=i)
return qml.expval(ham)
opt = qml.MomentumOptimizer()
params = tuple(
tuple(
(
np.random.rand() * 2 * np.pi,
np.random.rand() * 2 * np.pi,
)
for _ in range(size)
)
for _ in range(num_layers + 1)
)
energy = [
feed_forward(params),
]
for epoch in range(25):
params, e = opt.step_and_cost(feed_forward, params)
energy.append(e)
if epoch % 5 == 0:
print(epoch, energy[-1])
params
, not tuples. See here for more details: https://docs.pennylane.ai/en/stable/code/api/pennylane.numpy.tensor.html – co9olguy Jul 25 '23 at 19:03