# Qml.state() cannot use with qml.expval()?

**URL:** <https://discuss.pennylane.ai/t/qml-state-cannot-use-with-qml-expval/2751>\
**Category:** PennyLane Help\
**Created:** [March 22, 2023, 10:33am UTC](https://discuss.pennylane.ai/t/qml-state-cannot-use-with-qml-expval/2751 "2023-03-22T10:33:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qin](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/qin/32/1566_2.png) [@Qin](https://discuss.pennylane.ai/u/Qin)\
**Post date:** [March 22, 2023, 10:33am UTC](https://discuss.pennylane.ai/t/qml-state-cannot-use-with-qml-expval/2751/1 "2023-03-22T10:33:25Z")

</div>

I am using VQE to obtain some ground states. However, I find tutorials only obtain the parameters instead of output state (I know building an alternative circuit can obtain the state).But if there is any chance to obtain both output state and expect value(energy) once?

For simplicity, code here:

```auto
# Evaluates the expectation value h over the circuit ansatz on the device dev
@qml.qnode(dev, interface="autograd")
def cost_fn(parameters):
    global conv_state # I want to collect the state while VQE converged
    circuit(parameters) # like any demo shows
    exp_H = qml.expval(H) # I can only get expval() or state(), even I place the expval() ahead
    conv_state = qml.state()
    return exp_H

Error:
  File "C:\ProgramData\Anaconda3\envs\pennylane\lib\site-packages\pennylane\tape\tape.py", line 77, in _validate_computational_basis_sampling
    raise qml.QuantumFunctionError(_err_msg_for_some_meas_not_qwc(measurements))
pennylane.QuantumFunctionError: Only observables that are qubit-wise commuting Pauli words can be returned on the same wire, some of the following measurements do not commute:
[(1.0) [Z0 Z1]
+ (1.0) [X0 X1]
+ (1.0) [Y0 Y1]
+ (1.0) [Z1 Z0]
+ (1.0) [X1 X0]
+ (1.0) [Y1 Y0], state(wires=[])]

```

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [March 22, 2023, 8:44pm UTC](https://discuss.pennylane.ai/t/qml-state-cannot-use-with-qml-expval/2751/2 "2023-03-22T20:44:32Z")

</div>

Hey @Qin! Welcome to the forum! 😄

Great question. Here is a simpler example that replicates the error you’re facing:

```python
dev = qml.device('default.qubit', wires=1)

@qml.qnode(dev)
def circuit():
    qml.PauliZ(0)
    return qml.expval(qml.PauliX(0)), qml.state()

print(circuit())
'''
# QuantumFunctionError: Only observables that are qubit-wise commuting Pauli 
words can be returned on the same wire, some of the following measurements do
not commute:
[expval(PauliX(wires=[0])), state(wires=[])]
'''

```

These two measurements don’t commute; you cannot calculate the expectation value of an operator and simultaneously know the full quantum state. However, if you were to measure, say, `qml.PauliX` and `H = 0.3 * qml.PauliX` on the same set of wires, this _is_ possible because H and X commute!

```python
dev = qml.device('default.qubit', wires=1)

H = 0.3 * qml.PauliX(0)

@qml.qnode(dev)
def circuit():
    qml.PauliZ(0)
    return qml.expval(qml.PauliX(0)), qml.expval(H)

print(circuit())

'''
[0. 0.]
'''

```

Hopefully this answers your question! Let me know otherwise 😁

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [March 23, 2023, 2:43pm UTC](https://discuss.pennylane.ai/t/qml-state-cannot-use-with-qml-expval/2751/3 "2023-03-23T14:43:24Z")

</div>

Ah! Something just came to me. You _can_ actually do this with [`qml.Snapshot`](https://docs.pennylane.ai/en/stable/code/api/pennylane.Snapshot.html). Here’s how to use it in the example in my previous comment:

```python
dev = qml.device('default.qubit', wires=1)

@qml.qnode(dev)
def circuit():
    qml.PauliZ(0)
    qml.Snapshot()
    return qml.expval(qml.PauliX(0))

print(circuit())
print(qml.snapshots(circuit)())

''' output:
0.0
{0: array([1.+0.j, -0.+0.j]), 'execution_results': tensor(0., requires_grad=True)}
'''

```

The state is in the 0th key!
