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

**URL:** <https://discuss.pennylane.ai/t/implementing-expval-of-the-matrix-exponential-of-the-max-cut-hamiltonian/2616>\
**Category:** PennyLane Help\
**Created:** [February 23, 2023, 3:10pm UTC](https://discuss.pennylane.ai/t/implementing-expval-of-the-matrix-exponential-of-the-max-cut-hamiltonian/2616 "2023-02-23T15:10:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pontus\_Lindgren](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/pontus_lindgren/32/1502_2.png) [@Pontus\_Lindgren](https://discuss.pennylane.ai/u/Pontus_Lindgren)\
**Post date:** [February 23, 2023, 3:10pm UTC](https://discuss.pennylane.ai/t/implementing-expval-of-the-matrix-exponential-of-the-max-cut-hamiltonian/2616/1 "2023-02-23T15:10:12Z")

</div>

Hi!

I am trying to test the cost function described in [https://journals.aps.org/prresearch/pdf/10.1103/PhysRevResearch.2.023074](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.

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [February 25, 2023, 1:32am UTC](https://discuss.pennylane.ai/t/implementing-expval-of-the-matrix-exponential-of-the-max-cut-hamiltonian/2616/2 "2023-02-25T01:32:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![christina](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/christina/32/583_2.png) [@christina](https://discuss.pennylane.ai/u/christina)\
**Post date:** [February 27, 2023, 4:00pm UTC](https://discuss.pennylane.ai/t/implementing-expval-of-the-matrix-exponential-of-the-max-cut-hamiltonian/2616/3 "2023-02-27T16:00:27Z")

</div>

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:

```auto
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 🙂

---

<div class="post-metadata">

**Author:** ![Pontus\_Lindgren](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/pontus_lindgren/32/1502_2.png) [@Pontus\_Lindgren](https://discuss.pennylane.ai/u/Pontus_Lindgren)\
**Post date:** [February 27, 2023, 6:01pm UTC](https://discuss.pennylane.ai/t/implementing-expval-of-the-matrix-exponential-of-the-max-cut-hamiltonian/2616/4 "2023-02-27T18:01:58Z")

</div>

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,

```auto
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.

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [March 2, 2023, 10:58pm UTC](https://discuss.pennylane.ai/t/implementing-expval-of-the-matrix-exponential-of-the-max-cut-hamiltonian/2616/5 "2023-03-02T22:58:52Z")

</div>

Hi @Pontus_Lindgren,

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.

Please let me know if this fixes your issue!

---

<div class="post-metadata">

**Author:** ![Pontus\_Lindgren](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/pontus_lindgren/32/1502_2.png) [@Pontus\_Lindgren](https://discuss.pennylane.ai/u/Pontus_Lindgren)\
**Post date:** [March 3, 2023, 8:12am UTC](https://discuss.pennylane.ai/t/implementing-expval-of-the-matrix-exponential-of-the-max-cut-hamiltonian/2616/6 "2023-03-03T08:12:31Z")

</div>

Thanks! Now it works.

---

<div class="post-metadata">

**Author:** ![christina](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/christina/32/583_2.png) [@christina](https://discuss.pennylane.ai/u/christina)\
**Post date:** [March 3, 2023, 2:28pm UTC](https://discuss.pennylane.ai/t/implementing-expval-of-the-matrix-exponential-of-the-max-cut-hamiltonian/2616/7 "2023-03-03T14:28:49Z")

</div>

@Pontus_Lindgren

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