@qml.qnode(dev)
def circuit(weights, angles):
statepreparation(angles)
for W in weights:
layer(W)
return qml.expval(qml.PauliZ(0))
num_qubits = qbits
num_layers = 6
x = np.random.randn(2 ** qbits, requires_grad=False)
init_params = np.random.randn(num_layers, num_qubits, 3, requires_grad=True)
HEA = qml.QNGOptimizer(stepsize=0.01)
conv_tol = 1e-06
params = init_params
qngd_param_history = [params]
qngd_cost_history = []
for n in range(60):
# Take step
params, prev_energy = HEA.step_and_cost(circuit, params, x)
qngd_param_history.append(params)
qngd_cost_history.append(prev_energy)
# Compute energy
energy = cost_fn(params)
# Calculate difference between new and old energies
conv = np.abs(energy - prev_energy)
if n % 20 == 0:
print(
"Iteration = {:}, Energy = {:.8f} Ha, Convergence parameter = {"
":.8f} Ha".format(n, energy, conv)
)
if conv <= conv_tol:
break
print()
print("Final value of the energy = {:.8f} Ha".format(energy))
print("Number of iterations = ", n)
TypeError Traceback (most recent call last)
Input In [135], in <cell line: 12>()
10 qngd_cost_history =
12 for n in range(60):
13
14 # Take step
—> 15 params, prev_energy = HEA.step_and_cost(circuit, params, x)
16 qngd_param_history.append(params)
17 qngd_cost_history.append(prev_energy)
File d:\miniconda3\lib\site-packages\pennylane\optimize\qng.py:212, in QNGOptimizer.step_and_cost(self, qnode, grad_fn, recompute_tensor, metric_tensor_fn, *args, **kwargs)
210 shape = qml.math.shape(_metric_tensor)
211 size = qml.math.prod(shape[: len(shape) // 2])
→ 212 self.metric_tensor = qml.math.reshape(_metric_tensor, (size, size))
213 # Add regularization
214 self.metric_tensor = self.metric_tensor + self.lam * qml.math.eye(
215 size, like=_metric_tensor
216 )File d:\miniconda3\lib\site-packages\autoray\autoray.py:85, in do(fn, like, *args, **kwargs)
82 else:
83 backend = infer_backend(like)
—> 85 return get_lib_fn(backend, fn)(*args, **kwargs)File <array_function internals>:180, in reshape(*args, **kwargs)
File d:\miniconda3\lib\site-packages\numpy\core\fromnumeric.py:298, in reshape(a, newshape, order)
198 @array_function_dispatch(_reshape_dispatcher)
199 def reshape(a, newshape, order=‘C’):
200 “”"
201 Gives a new shape to an array without changing its data.
202
(…)
296 [5, 6]])
297 “”"
→ 298 return _wrapfunc(a, ‘reshape’, newshape, order=order)File d:\miniconda3\lib\site-packages\numpy\core\fromnumeric.py:54, in _wrapfunc(obj, method, *args, **kwds)
52 bound = getattr(obj, method, None)
53 if bound is None:
—> 54 return _wrapit(obj, method, *args, **kwds)
56 try:
57 return bound(*args, **kwds)File d:\miniconda3\lib\site-packages\numpy\core\fromnumeric.py:43, in _wrapit(obj, method, *args, **kwds)
41 except AttributeError:
42 wrap = None
—> 43 result = getattr(asarray(obj), method)(*args, **kwds)
44 if wrap:
45 if not isinstance(result, mu.ndarray):TypeError: ‘numpy.float64’ object cannot be interpreted as an integer
@Maria_Schuld
@Andre_Sequeira
@Ashley
@Muhammad_Kashif
@isaacdevlugt