# How to debug optimize function in PF4.5b

**URL:** <https://discuss.pennylane.ai/t/how-to-debug-optimize-function-in-pf4-5b/8896>\
**Category:** Codebook\
**Created:** [August 11, 2025, 8:41am UTC](https://discuss.pennylane.ai/t/how-to-debug-optimize-function-in-pf4-5b/8896 "2025-08-11T08:41:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wen\_Yi\_Hing](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/wen_yi_hing/32/3978_2.png) [@Wen\_Yi\_Hing](https://discuss.pennylane.ai/u/Wen_Yi_Hing)\
**Post date:** [August 11, 2025, 8:41am UTC](https://discuss.pennylane.ai/t/how-to-debug-optimize-function-in-pf4-5b/8896/1 "2025-08-11T08:41:07Z")

</div>

```auto
def optimize(cost_function, init_params, steps):

  opt = qml.GradientDescentOptimizer(stepsize = 0.4) # Change this as you see fit
  
  params = init_params
  
  for i in range(steps):
  
      params = opt.step(cost_function, params)
  
  return params, cost_function(params)

init_params = \[0.1, 0.2, 0.3, 0.4\]

# Run the optimizer for, say, 200 steps

opt_params, minimum = optimize(cost_function, init_params, 200)

# Convert the zero-dimensional result to a NumPy array

minimum = np.array(minimum) # An np.tensor of shape () containing the minimum of cost_function. 

Error: Incorrect: The output of your cost function isn't quite right!. How to debug this code or Use the `optimize` to find the `minimum` of the cost function (`cost_function`) you defined on the previous codercise. You may use `cost_function` without defining it again here. Note that minimum should be an `np.ndarray` containing only the minimum value of `cost_function`

```

---

<div class="post-metadata">

**Author:** ![daniela.murcillo](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/daniela.murcillo/32/3612_2.png) [@daniela.murcillo](https://discuss.pennylane.ai/u/daniela.murcillo)\
**Post date:** [August 11, 2025, 6:52pm UTC](https://discuss.pennylane.ai/t/how-to-debug-optimize-function-in-pf4-5b/8896/2 "2025-08-11T18:52:22Z")

</div>

Hi!  
I think your optimize function is correct. The problem is when you define the initial parameters. Try using a numpy array for that: `init_params = np.array([0.1, 0.2, 0.3, 0.4])`  
Recall that this array should be differentiable.

Let me know if this helps.
