Repository: PennyLaneAI/pennylane

Issue #6941: [BUG] probs doesn't work with Pauli words
Created: 2025-02-07T21:28:11Z
Expected behavior
qml.probs() outputs the correct probabilities when using Pauli words as observables.
This is related to issue #6878 but in this case, because we're working with Pauli words, we may be able to find an easier solution.
Actual behavior
The probabilities are sometimes in the wrong order and sometimes just fully incorrect.
Additional information
For Pauli words, maybe we shouldn't need to take the tensor product of component eigvals, since we know a priori the correct eigenvalues and ordering of the full word.
E.g., this function https://docs.pennylane.ai/en/stable/code/api/pennylane.pauli.pauli_eigs.html should generate the correct eigenvalues in the correct order, the only input needed is the number of Pauli ops.
Optionally, you may then need to permute the list of eigenvalues if the wire order is not the same as the underlying device.
Source code
import pennylane as qml
import numpy as np

dev = qml.device("default.qubit", wires=3)

ob1 = qml.Y(0) @ qml.Y(1) @ qml.X(2)
ob2 = qml.X(2) @ qml.Y(0) @ qml.Y(1)

@qml.qnode(dev)
def circuit():
    qml.RX(0.4,wires=1)
    return qml.probs(op=ob1), qml.probs(op=ob2)

ans = circuit()
# In this case all the answers are different where they shouldn't be
print("ob1: ",ans[0],"ob2: ",ans[1],sep="\n")
Tracebacks
ob1: 
[0.07632271 0.07632271 0.17367729 0.17367729 0.07632271 0.07632271
 0.17367729 0.17367729]
ob2: 
[0.07632271 0.17367729 0.07632271 0.17367729 0.07632271 0.17367729
 0.07632271 0.17367729]
System information
Name: PennyLane
Version: 0.40.0
Summary: 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.
Home-page: https://github.com/PennyLaneAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /usr/local/lib/python3.11/dist-packages
Requires: appdirs, autograd, autoray, cachetools, diastatic-malt, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, tomlkit, typing-extensions
Required-by: PennyLane_Lightning

Platform info:           Linux-6.1.85+-x86_64-with-glibc2.35
Python version:          3.11.11
Numpy version:           1.26.4
Scipy version:           1.13.1
Installed devices:
- default.clifford (PennyLane-0.40.0)
- default.gaussian (PennyLane-0.40.0)
- default.mixed (PennyLane-0.40.0)
- default.qubit (PennyLane-0.40.0)
- default.qutrit (PennyLane-0.40.0)
- default.qutrit.mixed (PennyLane-0.40.0)
- default.tensor (PennyLane-0.40.0)
- null.qubit (PennyLane-0.40.0)
- reference.qubit (PennyLane-0.40.0)
- lightning.qubit (PennyLane_Lightning-0.40.0)
Existing GitHub issues

 I have searched existing GitHub issues to make sure the issue does not already exist.

Duplicate issue #2761: [BUG] `qml.probs` incorrect output order when using custom eigendecomposition
Created: 2022-06-21T15:39:52Z
Expected behavior
Providing an observable to qml.probs works all the time, even with observables like qml.Hermitian.
Actual behavior
Using qml.probs(op=qml.Hadamard(0)) we get [0.14644661 0.85355339], whereas with
mx = qml.matrix(qml.Hadamard(0))
had = qml.Hermitian(mx, wires=[0])
qml.probs(op=had)
we get
[0.85355339 0.14644661] (probs reversed).
Additional information
qml.Hermitian is an example of an observable that computes its eigendecomposition on the fly to create its diagonalizing gates. However, the order in which the eigenvectors and eigenvalues are computed may not correspond to the order of basis states that we assume in PennyLane.
Source code
dev = qml.device("default.qubit", wires=1)
    
mx = qml.matrix(qml.Hadamard(0))
had = qml.Hermitian(mx, wires=[0])
    
@qml.qnode(dev)
def my_circ(hadamard_direct):
    qml.PauliX(0)
    if hadamard_direct:
        return qml.probs(op=qml.Hadamard(0))

    return qml.probs(op=had)

print(my_circ(True), my_circ(False))

my_circ(False)
print(list(my_circ.qtape))
Tracebacks
No response
System information
Name: PennyLane
Version: 0.24.0
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/XanaduAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /anaconda3/lib/python3.8/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, retworkx, scipy, semantic-version, toml
Required-by: amazon-braket-pennylane-plugin, PennyLane-AQT, PennyLane-Cirq, PennyLane-Forest, PennyLane-Honeywell, PennyLane-IonQ, PennyLane-Lightning, PennyLane-Orquestra, PennyLane-PQ, PennyLane-Qchem, PennyLane-qiskit, PennyLane-qsharp, pennylane-qulacs, PennyLane-SF

Platform info:           Linux-5.13.0-51-generic-x86_64-with-glibc2.10
Python version:          3.8.5
Numpy version:           1.22.4
Scipy version:           1.8.1
Installed devices:
- lightning.qubit (PennyLane-Lightning-0.24.0)
- default.gaussian (PennyLane-0.24.0)
- default.mixed (PennyLane-0.24.0)
- default.qubit (PennyLane-0.24.0)
- default.qubit.autograd (PennyLane-0.24.0)
- default.qubit.jax (PennyLane-0.24.0)
- default.qubit.tf (PennyLane-0.24.0)
- default.qubit.torch (PennyLane-0.24.0)
- orquestra.forest (PennyLane-Orquestra-0.15.0)
- orquestra.ibmq (PennyLane-Orquestra-0.15.0)
- orquestra.qiskit (PennyLane-Orquestra-0.15.0)
- orquestra.qulacs (PennyLane-Orquestra-0.15.0)
- qulacs.simulator (pennylane-qulacs-0.17.0.dev0)
- aqt.noisy_sim (PennyLane-AQT-0.18.0)
- aqt.sim (PennyLane-AQT-0.18.0)
- projectq.classical (PennyLane-PQ-0.18.0.dev0)
- projectq.ibm (PennyLane-PQ-0.18.0.dev0)
- projectq.simulator (PennyLane-PQ-0.18.0.dev0)
- microsoft.QuantumSimulator (PennyLane-qsharp-0.19.0)
- strawberryfields.fock (PennyLane-SF-0.21.0.dev0)
- strawberryfields.gaussian (PennyLane-SF-0.21.0.dev0)
- strawberryfields.gbs (PennyLane-SF-0.21.0.dev0)
- strawberryfields.remote (PennyLane-SF-0.21.0.dev0)
- strawberryfields.tf (PennyLane-SF-0.21.0.dev0)
- ionq.qpu (PennyLane-IonQ-0.21.0.dev0)
- ionq.simulator (PennyLane-IonQ-0.21.0.dev0)
- honeywell.hqs (PennyLane-Honeywell-0.19.0.dev0)
- qiskit.aer (PennyLane-qiskit-0.23.0.dev0)
- qiskit.basicaer (PennyLane-qiskit-0.23.0.dev0)
- qiskit.ibmq (PennyLane-qiskit-0.23.0.dev0)
- qiskit.ibmq.circuit_runner (PennyLane-qiskit-0.23.0.dev0)
- qiskit.ibmq.sampler (PennyLane-qiskit-0.23.0.dev0)
- forest.numpy_wavefunction (PennyLane-Forest-0.21.0.dev0)
- forest.qvm (PennyLane-Forest-0.21.0.dev0)
- forest.wavefunction (PennyLane-Forest-0.21.0.dev0)
- cirq.mixedsimulator (PennyLane-Cirq-0.23.0.dev0)
- cirq.pasqal (PennyLane-Cirq-0.23.0.dev0)
- cirq.qsim (PennyLane-Cirq-0.23.0.dev0)
- cirq.qsimh (PennyLane-Cirq-0.23.0.dev0)
- cirq.simulator (PennyLane-Cirq-0.23.0.dev0)
- braket.aws.qubit (amazon-braket-pennylane-plugin-1.6.8.dev0)
- braket.local.qubit (amazon-braket-pennylane-plugin-1.6.8.dev0)
Existing GitHub issues

 I have searched existing GitHub issues to make sure the issue does not already exist.

Closing comment (2025-02-10T20:38:40Z): This seems to be related to #6941