PennyLaneAI / pennylane

PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
https://pennylane.ai
Apache License 2.0
2.27k stars 585 forks source link

[BUG] qml.expval returns unexpected values #6017

Open yokkon opened 1 month ago

yokkon commented 1 month ago

Expected behavior

qml.expval(cost_h).process_counts({'000000':1}, wire_order=dev_shots_100.wires)

returns the largest eigenvalue of the cost_h Hamiltonian. probably because it sorts the eigenvalues and then it doesn't sort the counts / samples

Actual behavior

The provided code should return the eigenvalue corresponding to the eigenstate provided.

Additional information

No response

Source code

No response

Tracebacks

No response

System information

Python version:          3.9.12
Numpy version:           1.23.0
Scipy version:           1.8.0
Installed devices:
- default.clifford (PennyLane-0.37.0)
- default.gaussian (PennyLane-0.37.0)
- default.mixed (PennyLane-0.37.0)
- default.qubit (PennyLane-0.37.0)
- default.qubit.autograd (PennyLane-0.37.0)
- default.qubit.jax (PennyLane-0.37.0)
- default.qubit.legacy (PennyLane-0.37.0)
- default.qubit.tf (PennyLane-0.37.0)
- default.qubit.torch (PennyLane-0.37.0)
- default.qutrit (PennyLane-0.37.0)
- default.qutrit.mixed (PennyLane-0.37.0)
- default.tensor (PennyLane-0.37.0)
- null.qubit (PennyLane-0.37.0)
- lightning.qubit (PennyLane-Lightning-0.37.0)
- braket.aws.qubit (amazon-braket-pennylane-plugin-1.13.1)
- braket.local.qubit (amazon-braket-pennylane-plugin-1.13.1)

Existing GitHub issues

trbromley commented 1 month ago

Thanks for posting this but report @yokkon! We'll take a look and get back to you soon.

albi3ro commented 1 month ago

Both process_counts and process_samples assume that the diagonalizing gates have already been applied before the sampling was performed.

All we need is consistency between the eigenstates created by the diagonalizing gates and the eigenvalues returned by Operator.eigvals(). So the diagonalizing gates of cost_h should map the eigenstate with the largest eigenvalue to 000000. This can be double-checked with qml.ops.functions.assert_valid(cost_h).

If the eigenvalues and diagonalizing gates are indeed inconsistent, we'd be happy to look into fixing that.

DSGuala commented 5 days ago

Hi @yokkon, have you been able to test @albi3ro 's suggestion? Let us know if you're still having issues with this.