# Implementing expval of the matrix exponential of the max-cut hamiltonian

Hi!

I am trying to test the cost function described in https://journals.aps.org/prresearch/pdf/10.1103/PhysRevResearch.2.023074. For that purpose, I would really like to get this code snippet to work. Right now it gives an error, “pennylane.operation.EigvalsUndefinedError: Cannot compute samples of Exp” which feels weird as the hamiltonian is hermitian.

import pennylane as qml
import networkx as nx

device=qml.device('default.qubit', wires = range(4), shots=10)
@qml.qnode(device)
def circuit(op, eta=1):
op=qml.exp(op, -eta)
return qml.expval(op)

graph=nx.complete_graph(3)
H_cost, H_mixer=qml.qaoa.maxcut(graph)
circuit(H_cost)


Hi @Pontus_Lindgren, I’m not sure what is going on here. Will investigate further.

1 Like

Due to historical reasons, Hamiltonian objects do not define their eigenvalues due to the potential size of the object and computational difficulty in computing the eigenvalues.

We do have a successor to Hamiltonian, Sum that will provide eigenvalues whenever possible, even when it may be computationally intensive to compute the eigendecomposition of the matrix.

So your code will work if you use the new class instead. You can convert to it by doing qml.dot(*H_cost.terms())

The total code should then look like:

import pennylane as qml
import networkx as nx

device=qml.device('default.qubit', wires = range(4), shots=10)
@qml.qnode(device)
def circuit(op, eta=1):
op=qml.exp(op, -eta)
return qml.expval(op)

graph=nx.complete_graph(3)
H_cost, H_mixer=qml.qaoa.maxcut(graph)
circuit(qml.dot(*H_cost.terms()))


Hopefully that works for you

Unfortunately, the suggested code does not work (for me at least). I get this traceback when using the :

Traceback (most recent call last):
File “c:/Users/46704/OneDrive/python101/leka med kvantgrindar/hur_gör_man_exp_av_en_produkt.py”, line 14, in
circuit(qml.dot(*H_cost.terms()))
File “C:\Users\46704\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pennylane\collections\dot.py”, line 146, in dot
raise ValueError(“At least one argument must be a QNodeCollection”)
ValueError: At least one argument must be a QNodeCollection

On a related note, as I am only working with hamiltonians corresponding to a diagonal matrix, is it possible to define the matrix of “op” manually and then calculate the expval? Or even supply the eigenvalues directly? I am unsure however if that would be more efficent. Naively I would like to do this,

import pennylane as qml
import networkx as nx

device=qml.device('default.qubit', wires = range(4), shots=10)
@qml.qnode(device)
def circuit(op, eta=1):
matrix=qml.matrix(op)
Eigvals=np.exp(-eta*np.diagonal(matrix))
op.eigvals=Eigvals #naively what I imagine the syntax could look like
return qml.expval(op)

graph=nx.complete_graph(3)
H_cost, H_mixer=qml.qaoa.maxcut(graph)
circuit(H_cost))



Thank you for your time, I am very new to Pennylane.

I was able to replicate your error with PennyLane v0.28. If you upgrade to v0.29, our latest version, you shouldn’t see the error anymore. There may we a deprecation warning but not an error.

As for the question about custom operations, you can define a qml.Hermitian operator from any matrix.