# QML Algorithm doesn't learn

**URL:** <https://discuss.pennylane.ai/t/qml-algorithm-doesnt-learn/468>\
**Category:** PennyLane Help\
**Created:** [July 13, 2020, 3:10pm UTC](https://discuss.pennylane.ai/t/qml-algorithm-doesnt-learn/468 "2020-07-13T15:10:13Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![josh](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/josh/32/100_2.png) [@josh](https://discuss.pennylane.ai/u/josh)\
**Post date:** [July 15, 2020, 2:41am UTC](https://discuss.pennylane.ai/t/qml-algorithm-doesnt-learn/468/8 "2020-07-15T02:41:49Z")

</div>

Hi Shawn,

The trace of the system, \text{Tr}(\rho), can also be written as \text{Tr}(\rho I)=\langle I\rangle, so we can equivalently think of it as the expectation of the identity operator.

This allows you to construct a QNode that returns the trace like so:

```python
@qml.qnode(dev)
def layer(inputs, w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10):
    qml.templates.SqueezingEmbedding(inputs, wires=range(wires))
    qml.templates.CVNeuralNetLayers(w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10,wires=range(wires))
    return qml.Identity(wires=range(wires))

```

Alternatively, without modifying your existing QNode, you can inspect the device **after QNode evaluation** to find the trace:

```python
@qml.qnode(dev)
def layer(inputs, w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10):
    qml.templates.SqueezingEmbedding(inputs, wires=range(wires))
    qml.templates.CVNeuralNetLayers(w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10,wires=range(wires))
    return [qml.expval(qml.X(wires=i)) for i in range(wires)]

# evaluate the QNode
result = layers(**inputs)

# Check the device trace
dev.state.trace()

```

---

_[View the full topic](https://discuss.pennylane.ai/t/qml-algorithm-doesnt-learn/468)._
