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

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:

# 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=[])]

Hey @Qin! Welcome to the forum! :smile:

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

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!

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 :grin:

Ah! Something just came to me. You can actually do this with qml.Snapshot. Here’s how to use it in the example in my previous comment:

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!