Dear Pennylane team,
Thank you for the great efforts on developing Pennylane and keeping fast iteration. When I update to the new released version of pennnylane, it reports an error when I run the following code. However, when running it on the pennylane of version 0.14.1
, everything works well. Sorry for that I have not tracked the latest change of pennylane recently.
import pennylane as qml
from pennylane import numpy as np
import qiskit.providers.aer.noise as noise
p_phase = 0.05
phase_flip = noise.pauli_error([('Z', p_phase), ('I', 1 - p_phase)])
noise_model = noise.NoiseModel()
noise_model.add_all_qubit_quantum_error(phase_flip, ['u1', 'u2', 'u3'])
dev = qml.device('qiskit.aer', wires=4, noise_model=noise_model)
def encode_layer(feature, n_qubits):
for i in range(n_qubits):
qml.RY(feature[i], wires=i)
def layer(params, n_qubits):
for i in range(n_qubits):
qml.RX(params[i], wires=i)
qml.CZ(wires=[0, 1])
qml.CZ(wires=[1, 2])
qml.CZ(wires=[2, 3])
def circuit(params, feature=None, A=None):
encode_layer(feature, 4)
for j in range(2):
layer(params[j], 4)
return qml.expval(qml.Hermitian(A, wires=[0, 1, 2, 3]))
def cost_fn_retrain(params, model, inputs, labels, A):
loss = 0
size = inputs.shape[0]
for data, label in zip(inputs, labels):
out = model(params, data, A)
loss += (label - out)**2
loss /= size
return loss
model = qml.QNode(circuit, dev)
opt = qml.AdamOptimizer(0.2)
A = np.kron(np.eye(8), np.array([[1, 0], [0, 0]]))
data = np.random.uniform(0, np.pi*2, (2, 4))
label = np.random.uniform(0, np.pi*2, (2))
params = np.random.uniform(0, np.pi * 2, (2, 4))
opt.step_and_cost(lambda para: cost_fn_retrain(para, model, data, label, A), params)
The raised error is quite confusing. Here is the full picture of the error information.
D:\xxx\software\Python\Python38\lib\site-packages\pennylane\tape\jacobian_tape.py:562: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different
lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
params = np.array(params)
TypeError: only size-1 arrays can be converted to Python scalars
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:\xxx\software\Python\Python38\lib\runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "D:\xxx\software\Python\Python38\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "c:\Users\xxx\.vscode\extensions\ms-python.python-2021.10.1336267007\pythonFiles\lib\python\debugpy\__main__.py", line 45, in <module>
cli.main()
File "c:\Users\xxx\.vscode\extensions\ms-python.python-2021.10.1336267007\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 444, in main
run()
File "c:\Users\xxx\.vscode\extensions\ms-python.python-2021.10.1336267007\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 285, in run_file
runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))
File "D:\xxx\software\Python\Python38\lib\runpy.py", line 265, in run_path
return _run_module_code(code, init_globals, run_name,
File "D:\xxx\software\Python\Python38\lib\runpy.py", line 97, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "D:\xxx\software\Python\Python38\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "d:\xxx\document\quantum academy\QAS-original\machine_learning\tmp.py", line 46, in <module>
opt.step_and_cost(lambda para: cost_fn_retrain(para, model, data, label, A), params)
File "D:\xxx\software\Python\Python38\lib\site-packages\pennylane\optimize\gradient_descent.py", line 100, in step_and_cost
g, forward = self.compute_grad(objective_fn, args, kwargs, grad_fn=grad_fn)
File "D:\xxx\software\Python\Python38\lib\site-packages\pennylane\optimize\gradient_descent.py", line 158, in compute_grad
grad = g(*args, **kwargs)
File "D:\xxx\software\Python\Python38\lib\site-packages\pennylane\_grad.py", line 101, in __call__
grad_value, ans = self._get_grad_fn(args)(*args, **kwargs)
File "D:\xxx\software\Python\Python38\lib\site-packages\autograd\wrap_util.py", line 20, in nary_f
return unary_operator(unary_f, x, *nary_op_args, **nary_op_kwargs)
File "D:\xxx\software\Python\Python38\lib\site-packages\pennylane\_grad.py", line 126, in _grad_with_forward
grad_value = vjp(vspace(ans).ones())
File "D:\xxx\software\Python\Python38\lib\site-packages\autograd\core.py", line 14, in vjp
def vjp(g): return backward_pass(g, end_node)
File "D:\xxx\software\Python\Python38\lib\site-packages\autograd\core.py", line 21, in backward_pass
ingrads = node.vjp(outgrad[0])
File "D:\xxx\software\Python\Python38\lib\site-packages\autograd\core.py", line 67, in <lambda>
return lambda g: (vjp(g),)
File "D:\xxx\software\Python\Python38\lib\site-packages\pennylane\interfaces\autograd.py", line 274, in gradient_product
vjp = dy @ jacobian(params)
File "D:\xxx\software\Python\Python38\lib\site-packages\autograd\tracer.py", line 48, in f_wrapped
return f_raw(*args, **kwargs)
File "D:\xxx\software\Python\Python38\lib\site-packages\pennylane\interfaces\autograd.py", line 252, in jacobian
return _evaluate_grad_matrix(p, "jacobian")
File "D:\xxx\software\Python\Python38\lib\site-packages\pennylane\interfaces\autograd.py", line 235, in _evaluate_grad_matrix
grad_matrix = getattr(self, grad_matrix_fn)(device, params=p, **self.jacobian_options)
File "D:\xxx\software\Python\Python38\lib\site-packages\pennylane\tape\qubit_param_shift.py", line 131, in jacobian
return super().jacobian(device, params, **options)
File "D:\xxx\software\Python\Python38\lib\site-packages\pennylane\tape\jacobian_tape.py", line 584, in jacobian
params_f64 = np.array(params, dtype=np.float64)
ValueError: setting an array element with a sequence.
Any suggestions are greatly appreciated.