Optimization routine doesn't work

Hello! I am tryting to optimize the paraemters in a circuit to find the minimum of a cost function, but it doesn’t work. This is my circuit

def ansatzz(parameters):

  qml.RX(parameters[0], wires=0)
  qml.RY(parameters[1], wires=1)
  qml.CNOT(wires=[0,1])
  qml.CNOT(wires=[1,2])
  qml.CNOT(wires=[2,0])
  qml.RY(parameters[2], wires=0)
  qml.RY(parameters[3], wires=1)
  qml.RY(parameters[4], wires=2)
    
  return qml.expval(qml.PauliZ(0))

I get this error

bad operand type for unary -: 'ExpectationMP'

Can you help me undersrand whaht it means?

Thank you :slight_smile:

And, finally, make sure to include the versions of your packages. Specifically, show us the output of qml.about().

Hi @i_am_confusion !

Thank you for your question, it’s a very interesting topic. The error you’re encountering seems to be related to a negative value in the observable within the ExpectationMP
class, which is responsible for calculating the expected value of the specified observable.

For example, if the last line of code were:

return -qml.expval(qml.PauliZ(0))

This would indicate the use of an invalid operator, as the values within qml.expval must be either positive or negative, depending on the context, but applied correctly.

However, this doesn’t seem to be the issue in your case. Could you share more of your code where the error occurs? I’m using the following versions of the software and haven’t encountered that error.

qml.about()

Name: PennyLane
Version: 0.37.0
Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Home-page: GitHub - PennyLaneAI/pennylane: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Author:
Author-email:
License: Apache License 2.0
Location: /home/toto/anaconda3/envs/quantum/lib/python3.12/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane_Lightning, PennyLane_Lightning_GPU

Platform info: Linux-6.8.0-40-generic-x86_64-with-glibc2.35
Python version: 3.12.4
Numpy version: 1.26.4
Scipy version: 1.14.0
Installed devices:

  • lightning.qubit (PennyLane_Lightning-0.37.0)
  • default.clifford (PennyLane-0.37.0)
  • default.gaussian (PennyLane-0.37.0)
  • default.mixed (PennyLane-0.37.0)
  • default.qubit (PennyLane-0.37.0)
  • default.qubit.autograd (PennyLane-0.37.0)
  • default.qubit.jax (PennyLane-0.37.0)
  • default.qubit.legacy (PennyLane-0.37.0)
  • default.qubit.tf (PennyLane-0.37.0)
  • default.qubit.torch (PennyLane-0.37.0)
  • default.qutrit (PennyLane-0.37.0)
  • default.qutrit.mixed (PennyLane-0.37.0)
  • default.tensor (PennyLane-0.37.0)
  • null.qubit (PennyLane-0.37.0)
  • lightning.gpu (PennyLane_Lightning_GPU-0.37.0)

I can also share an example that I believe is similar to what you’re trying to achieve. Feel free to review How to create your own optimizer

Hi @maldoalberto

Thank you for your reply, here’s all I have in my jupyter notebook:

import pennylane as qml
from pennylane import numpy as np

dev = qml.device("default.qubit", wires=3)

def ansatzz(parameters):

  qml.RX(parameters[0], wires=0)
  qml.RY(parameters[1], wires=1)
  qml.CNOT(wires=[0,1])
  qml.CNOT(wires=[1,2])
  qml.CNOT(wires=[2,0])
  qml.RY(parameters[2], wires=0)
  qml.RY(parameters[3], wires=1)
  qml.RY(parameters[4], wires=2)

  return qml.expval(qml.PauliZ(0))

def cost(parameters):
  return -ansatzz(parameters)

def optimize(parameters):
  optimizer = qml.GradientDescentOptimizer(stepsize=0.4)
  steps = 100
  for i in range(steps):
    parameters = optimizer.step(cost, parameters)

  return parameters

I tried changing the sing but it didn’t work. I get this when i run qml.about()

Name: PennyLane
Version: 0.38.1
Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Home-page: https://github.com/PennyLaneAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /usr/local/lib/python3.10/dist-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, toml, typing-extensions
Required-by: PennyLane_Lightning

Platform info:           Linux-6.1.85+-x86_64-with-glibc2.35
Python version:          3.10.12
Numpy version:           1.26.4
Scipy version:           1.13.1
Installed devices:
- default.clifford (PennyLane-0.38.1)
- default.gaussian (PennyLane-0.38.1)
- default.mixed (PennyLane-0.38.1)
- default.qubit (PennyLane-0.38.1)
- default.qubit.autograd (PennyLane-0.38.1)
- default.qubit.jax (PennyLane-0.38.1)
- default.qubit.legacy (PennyLane-0.38.1)
- default.qubit.tf (PennyLane-0.38.1)
- default.qubit.torch (PennyLane-0.38.1)
- default.qutrit (PennyLane-0.38.1)
- default.qutrit.mixed (PennyLane-0.38.1)
- default.tensor (PennyLane-0.38.1)
- null.qubit (PennyLane-0.38.1)
- lightning.qubit (PennyLane_Lightning-0.38.0)

I think my version is more recent, maybe it’s a buig in the new version?!

Thank you :slight_smile:

Hey @i_am_confusion !

ChatGPT

My apologies for the delayed response. Regarding your question, you’re missing the qnode method before your ansatz circuit; otherwise, an error will occur. This method is crucial because qnode connects the values that will be optimized, such as the expected value. Without this method, it won’t calculate correctly using something like expval(Z[0]), as the expected numerical value won’t be obtained. Therefore, your code should be structured as follows.

import pennylane as qml
from pennylane import numpy as np


dev = qml.device("default.qubit", wires=3)

@qml.qnode(dev)
def ansatzz(parameters):

  qml.RX(parameters[0], wires=0)
  qml.RY(parameters[1], wires=1)
  qml.CNOT(wires=[0,1])
  qml.CNOT(wires=[1,2])
  qml.CNOT(wires=[2,0])
  qml.RY(parameters[2], wires=0)
  qml.RY(parameters[3], wires=1)
  qml.RY(parameters[4], wires=2)

  return qml.expval(qml.PauliZ(0))

def cost(parameters):
  return ansatzz(parameters)

To verify that it works, I used the same tutorial I sent you in my first response. The following code demonstrates how to do it:

from pennylane import AdamOptimizer


n_steps = 200
theta = np.random.rand(5, 1, requires_grad=True)

costs_list = []
opt = AdamOptimizer()
for i in range(1, n_steps+1):
    if i%100==0: print("Running... Current step: ", i)
    theta = opt.step(cost, theta)
    costs_list.append(cost(theta))

tell me if is working in your side, and I’m using the pennylane versión’s 0.38.1

qml.about()

Name: PennyLane
Version: 0.38.1
Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Home-page: GitHub - PennyLaneAI/pennylane: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Author:
Author-email:
License: Apache License 2.0
Location: /usr/local/lib/python3.10/dist-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, toml, typing-extensions
Required-by: PennyLane_Lightning

Platform info: Linux-6.1.85±x86_64-with-glibc2.35
Python version: 3.10.12
Numpy version: 1.26.4
Scipy version: 1.13.1
Installed devices:

  • lightning.qubit (PennyLane_Lightning-0.38.0)
  • default.clifford (PennyLane-0.38.1)
  • default.gaussian (PennyLane-0.38.1)
  • default.mixed (PennyLane-0.38.1)
  • default.qubit (PennyLane-0.38.1)
  • default.qubit.autograd (PennyLane-0.38.1)
  • default.qubit.jax (PennyLane-0.38.1)
  • default.qubit.legacy (PennyLane-0.38.1)
  • default.qubit.tf (PennyLane-0.38.1)
  • default.qubit.torch (PennyLane-0.38.1)
  • default.qutrit (PennyLane-0.38.1)
  • default.qutrit.mixed (PennyLane-0.38.1)
  • default.tensor (PennyLane-0.38.1)
  • null.qubit (PennyLane-0.38.1)

I hope is this a solution for you, if not let me know how to solve it :smile:

Thank you, this is working now :slight_smile:

1 Like