I was able to correct that error but now I get the following ( seems I need to convert to square matrix )
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[7], line 1
----> 1 cost = [optimize_circuit(reshaped_array,hamiltonian)]
File D:\anaconda3\envs\QCML\Lib\site-packages\pennylane\workflow\qnode.py:1092, in QNode.__call__(self, *args, **kwargs)
1089 override_shots = kwargs["shots"]
1091 # construct the tape
-> 1092 self.construct(args, kwargs)
1094 original_grad_fn = [self.gradient_fn, self.gradient_kwargs, self.device]
1095 self._update_gradient_fn(shots=override_shots, tape=self._tape)
File D:\anaconda3\envs\QCML\Lib\site-packages\pennylane\workflow\qnode.py:929, in QNode.construct(self, args, kwargs)
926 self.interface = qml.math.get_interface(*args, *list(kwargs.values()))
928 with qml.queuing.AnnotatedQueue() as q:
--> 929 self._qfunc_output = self.func(*args, **kwargs)
931 self._tape = QuantumScript.from_queue(q, shots)
933 params = self.tape.get_parameters(trainable_only=False)
Cell In[1], line 71, in optimize_circuit(params, hamiltonian)
69 # Optimization loop
70 for n in range(steps):
---> 71 theta, prev_cost = opt.step_and_cost(variational_circuit, params,hamiltonian)
72 cost.append(cost_function(theta))
73 angle.append(theta)
File D:\anaconda3\envs\QCML\Lib\site-packages\pennylane\optimize\gradient_descent.py:64, in GradientDescentOptimizer.step_and_cost(self, objective_fn, grad_fn, *args, **kwargs)
44 def step_and_cost(self, objective_fn, *args, grad_fn=None, **kwargs):
45 """Update trainable arguments with one step of the optimizer and return the corresponding
46 objective function value prior to the step.
47
(...)
61 If single arg is provided, list [array] is replaced by array.
62 """
---> 64 g, forward = self.compute_grad(objective_fn, args, kwargs, grad_fn=grad_fn)
65 new_args = self.apply_grad(g, args)
67 if forward is None:
File D:\anaconda3\envs\QCML\Lib\site-packages\pennylane\optimize\gradient_descent.py:122, in GradientDescentOptimizer.compute_grad(objective_fn, args, kwargs, grad_fn)
104 r"""Compute gradient of the objective function at the given point and return it along with
105 the objective function forward pass (if available).
106
(...)
119 will not be evaluted and instead ``None`` will be returned.
120 """
121 g = get_gradient(objective_fn) if grad_fn is None else grad_fn
--> 122 grad = g(*args, **kwargs)
123 forward = getattr(g, "forward", None)
125 num_trainable_args = sum(getattr(arg, "requires_grad", False) for arg in args)
File D:\anaconda3\envs\QCML\Lib\site-packages\pennylane\_grad.py:165, in grad.__call__(self, *args, **kwargs)
162 self._forward = self._fun(*args, **kwargs)
163 return ()
--> 165 grad_value, ans = grad_fn(*args, **kwargs) # pylint: disable=not-callable
166 self._forward = ans
168 return grad_value
File D:\anaconda3\envs\QCML\Lib\site-packages\autograd\wrap_util.py:20, in unary_to_nary.<locals>.nary_operator.<locals>.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)
File D:\anaconda3\envs\QCML\Lib\site-packages\pennylane\_grad.py:183, in grad._grad_with_forward(fun, x)
177 @staticmethod
178 @unary_to_nary
179 def _grad_with_forward(fun, x):
180 """This function is a replica of ``autograd.grad``, with the only
181 difference being that it returns both the gradient *and* the forward pass
182 value."""
--> 183 vjp, ans = _make_vjp(fun, x) # pylint: disable=redefined-outer-name
185 if vspace(ans).size != 1:
186 raise TypeError(
187 "Grad only applies to real scalar-output functions. "
188 "Try jacobian, elementwise_grad or holomorphic_grad."
189 )
File D:\anaconda3\envs\QCML\Lib\site-packages\autograd\core.py:10, 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()
File D:\anaconda3\envs\QCML\Lib\site-packages\autograd\tracer.py:10, 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
File D:\anaconda3\envs\QCML\Lib\site-packages\autograd\wrap_util.py:15, in unary_to_nary.<locals>.nary_operator.<locals>.nary_f.<locals>.unary_f(x)
13 else:
14 subargs = subvals(args, zip(argnum, x))
---> 15 return fun(*subargs, **kwargs)
File D:\anaconda3\envs\QCML\Lib\site-packages\pennylane\workflow\qnode.py:1092, in QNode.__call__(self, *args, **kwargs)
1089 override_shots = kwargs["shots"]
1091 # construct the tape
-> 1092 self.construct(args, kwargs)
1094 original_grad_fn = [self.gradient_fn, self.gradient_kwargs, self.device]
1095 self._update_gradient_fn(shots=override_shots, tape=self._tape)
File D:\anaconda3\envs\QCML\Lib\site-packages\pennylane\workflow\qnode.py:929, in QNode.construct(self, args, kwargs)
926 self.interface = qml.math.get_interface(*args, *list(kwargs.values()))
928 with qml.queuing.AnnotatedQueue() as q:
--> 929 self._qfunc_output = self.func(*args, **kwargs)
931 self._tape = QuantumScript.from_queue(q, shots)
933 params = self.tape.get_parameters(trainable_only=False)
Cell In[1], line 38, in variational_circuit(params, hamiltonian)
36 parameters = params.reshape((LAYERS, WIRES, 3))
37 qml.templates.StronglyEntanglingLayers(parameters, wires=range(WIRES))
---> 38 return qml.expval(qml.Hermitian(hamiltonian, wires = [0,1]))
File D:\anaconda3\envs\QCML\Lib\site-packages\pennylane\ops\qubit\observables.py:84, in Hermitian.__init__(self, A, wires, id)
80 else:
81 # Assumably wires is an int; further validation checks are performed by calling super().__init__
82 expected_mx_shape = self._num_basis_states
---> 84 Hermitian._validate_input(A, expected_mx_shape)
86 super().__init__(A, wires=wires, id=id)
File D:\anaconda3\envs\QCML\Lib\site-packages\pennylane\ops\qubit\observables.py:91, in Hermitian._validate_input(A, expected_mx_shape)
88 @staticmethod
89 def _validate_input(A, expected_mx_shape=None):
90 """Validate the input matrix."""
---> 91 if len(A.shape) != 2 or A.shape[0] != A.shape[1]:
92 raise ValueError("Observable must be a square matrix.")
94 if expected_mx_shape is not None and A.shape[0] != expected_mx_shape:
AttributeError: 'LinearCombination' object has no attribute 'shape'