Thank you sir @Alain_Delgado_Gran for such a beautiful explanation with appropriate references. I will go thorugh them.
Thank you @CatalinaAlbornoz for sharing the code.
I tried to run the code, upon running the line " hessian = qml.jacobian(qml.grad(energy, argnum=0))(opt_param) ", it gave me the following error.
“”"
AttributeError Traceback (most recent call last)
/tmp/ipykernel_1076/1091415110.py in
1 # compute the Hessian of the quantum circuit
2 energy = expval(H(coord), opt_param)
----> 3 hessian = qml.jacobian(qml.grad(energy, argnum=0))(opt_param)
~/psi4conda/envs/pennylane/lib/python3.9/site-packages/pennylane/_grad.py in _jacobian_function(*args, **kwargs)
179
180 if len(argnum) == 1:
–> 181 return _jacobian(func, argnum[0])(*args, **kwargs)
182
183 return np.stack([_jacobian(func, arg)(*args, **kwargs) for arg in argnum]).T
~/psi4conda/envs/pennylane/lib/python3.9/site-packages/autograd/wrap_util.py in nary_f(*args, **kwargs)
18 else:
19 x = tuple(args[i] for i in argnum)
—> 20 return unary_operator(unary_f, x, *nary_op_args, **nary_op_kwargs)
21 return nary_f
22 return nary_operator
~/psi4conda/envs/pennylane/lib/python3.9/site-packages/autograd/differential_operators.py in jacobian(fun, x)
55 (out1, out2, …) then the Jacobian has shape (out1, out2, …, in1, in2, …).
56 “”"
—> 57 vjp, ans = _make_vjp(fun, x)
58 ans_vspace = vspace(ans)
59 jacobian_shape = ans_vspace.shape + vspace(x).shape
~/psi4conda/envs/pennylane/lib/python3.9/site-packages/autograd/core.py in make_vjp(fun, x)
8 def make_vjp(fun, x):
9 start_node = VJPNode.new_root()
—> 10 end_value, end_node = trace(start_node, fun, x)
11 if end_node is None:
12 def vjp(g): return vspace(x).zeros()
~/psi4conda/envs/pennylane/lib/python3.9/site-packages/autograd/tracer.py in trace(start_node, fun, x)
8 with trace_stack.new_trace() as t:
9 start_box = new_box(x, t, start_node)
—> 10 end_box = fun(start_box)
11 if isbox(end_box) and end_box._trace == start_box._trace:
12 return end_box._value, end_box._node
~/psi4conda/envs/pennylane/lib/python3.9/site-packages/autograd/wrap_util.py in unary_f(x)
13 else:
14 subargs = subvals(args, zip(argnum, x))
—> 15 return fun(*subargs, **kwargs)
16 if isinstance(argnum, int):
17 x = args[argnum]
~/psi4conda/envs/pennylane/lib/python3.9/site-packages/pennylane/_grad.py in call(self, *args, **kwargs)
99 “”“Evaluates the gradient function, and saves the function value
100 calculated during the forward pass in :attr:.forward
.”""
–> 101 grad_value, ans = self._get_grad_fn(args)(*args, **kwargs)
102 self._forward = ans
103 return grad_value
~/psi4conda/envs/pennylane/lib/python3.9/site-packages/autograd/wrap_util.py in nary_f(*args, **kwargs)
18 else:
19 x = tuple(args[i] for i in argnum)
—> 20 return unary_operator(unary_f, x, *nary_op_args, **nary_op_kwargs)
21 return nary_f
22 return nary_operator
~/psi4conda/envs/pennylane/lib/python3.9/site-packages/pennylane/_grad.py in _grad_with_forward(fun, x)
116 difference being that it returns both the gradient and the forward pass
117 value."""
–> 118 vjp, ans = _make_vjp(fun, x)
119
120 if not vspace(ans).size == 1:
~/psi4conda/envs/pennylane/lib/python3.9/site-packages/autograd/core.py in make_vjp(fun, x)
8 def make_vjp(fun, x):
9 start_node = VJPNode.new_root()
—> 10 end_value, end_node = trace(start_node, fun, x)
11 if end_node is None:
12 def vjp(g): return vspace(x).zeros()
~/psi4conda/envs/pennylane/lib/python3.9/site-packages/autograd/tracer.py in trace(start_node, fun, x)
8 with trace_stack.new_trace() as t:
9 start_box = new_box(x, t, start_node)
—> 10 end_box = fun(start_box)
11 if isbox(end_box) and end_box._trace == start_box._trace:
12 return end_box._value, end_box._node
~/psi4conda/envs/pennylane/lib/python3.9/site-packages/autograd/wrap_util.py in unary_f(x)
13 else:
14 subargs = subvals(args, zip(argnum, x))
—> 15 return fun(*subargs, **kwargs)
16 if isinstance(argnum, int):
17 x = args[argnum]
/tmp/ipykernel_1076/3145879875.py in funct(params)
2 def expval(obs, params):
3 def funct(params):
----> 4 return circuit(params, obs, wires=range(qubits))
5 return funct
~/psi4conda/envs/pennylane/lib/python3.9/site-packages/pennylane/qnode.py in call(self, *args, **kwargs)
604 if self.mutable or self.qtape is None:
605 # construct the tape
–> 606 self.construct(args, kwargs)
607
608 # Execute the tape.
~/psi4conda/envs/pennylane/lib/python3.9/site-packages/pennylane/qnode.py in construct(self, args, kwargs)
523
524 with self.qtape:
–> 525 self.qfunc_output = self.func(*args, **kwargs)
526
527 if not isinstance(self.qfunc_output, Sequence):
/tmp/ipykernel_1076/3280061623.py in circuit(params, obs, wires)
6 qml.DoubleExcitation(params[0], wires=[0, 1, 2, 3])
7 qml.DoubleExcitation(params[1], wires=[0, 1, 4, 5])
----> 8 return qml.expval(obs)
~/psi4conda/envs/pennylane/lib/python3.9/site-packages/pennylane/measure.py in expval(op)
233 if not isinstance(op, (Observable, qml.Hamiltonian)):
234 raise qml.QuantumFunctionError(
–> 235 “{} is not an observable or Hamiltonian: cannot be used with expval”.format(op.name)
236 )
237
AttributeError: ‘tensor’ object has no attribute ‘name’
“”".
I am not sure why this error pops up. Can you help me troubleshoot this.
Thankyou.