Skip to content

[BUG] qml.Hermitian doesn't work with probs (neither do operator arithmetic observables) #6878

Open
@CatalinaAlbornoz

Description

@CatalinaAlbornoz

Expected behavior

Option 1:
qml.probs() outputs the correct probabilities when using qml.Hermitian as an observable (or operator arithmetic observables).

Option 2:
Since using probs with Hermitian renders incorrect results it should be clearly stated in the docs for Hermitian and probs that the combination of them is not supported. Same for operator arithmetic observables, unless we find an efficient way to make them work together.
Ideally users should get an error when using qml.Hermitian together with qml.probs

Actual behavior

The probabilities are sometimes in the wrong order and sometimes just fully incorrect.

Additional information

This seems related to issue #2761.

Source code

# Error test
import pennylane as qml
import numpy as np

## Test 1
dev = qml.device("default.qubit", wires=1)

H = 1 / np.sqrt(2) * np.array([[1, 1], [1, -1]])

@qml.qnode(dev)
def circuit():
    qml.H(wires=0)
    return qml.probs(op=qml.Hermitian(H, wires=0)), qml.probs(op=qml.Hadamard(wires=0))

# In this case the outputs are in the wrong order
print("Test 1")
print(circuit())

## Test 2

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

ob = qml.X(0) @ qml.Y(1)

@qml.qnode(dev)
def circuit():
    return qml.probs(op=qml.Hermitian(ob.matrix(), wires=[0,1])), qml.probs(op=ob)

# In this case the output is completely wrong
print("Test 2")
print(circuit())

## Test 3

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

ob = 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=qml.Hermitian(ob.matrix(), wires=[0,1,2])), qml.probs(op=ob), qml.probs(op=ob2)

ans = circuit()
# In this case all the answers are different
print("Test 3")
print("Hermitian: ",ans[0],"ob1: ",ans[1],"ob2: ",ans[2],sep="\n")

Tracebacks

Test 1
(array([0.14644661, 0.85355339]), array([0.85355339, 0.14644661]))
Test 2
(array([0.5, 0. , 0. , 0.5]), array([0.25, 0.25, 0.25, 0.25]))
Test 3
Hermitian: 
[0.48026525 0.01973475 0.         0.         0.         0.01973475
 0.         0.48026525]
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:
- lightning.qubit (PennyLane_Lightning-0.40.0)
- 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)

Existing GitHub issues

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐛Something isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions