Quantum library penny lane gives wrong results for expectation value

in quantum physics if we have tensor product of two Pauli X it will be identity matrix, when examine this theory it does not work properly, here I two function for two qubit which I calculate expectation value of tensor product of X(0) X(1) X(1) which means X(0) (X(0) for qubit one, X(1) for qubit two), so both of them must return same result they didn’t .

import pennylane as qml
dev3 = qml.device("default.qubit", wires=2)
@qml.qnode(dev3)
def my_quantum_function(x, y):
    qml.RY(x, wires=0)
    qml.RY(y, wires=1)
    return qml.expval(qml.PauliX(0) @ qml.PauliX(1) @ qml.PauliX(1))
print(my_quantum_function(10,5))


#and 

import pennylane as qml
dev3 = qml.device("default.qubit", wires=2)
@qml.qnode(dev3)
def my_quantum_function(x, y):
    qml.RY(x, wires=0)
    qml.RY(y, wires=1)
    return qml.expval(qml.PauliX(0))
print(my_quantum_function(10,5))

If you want help with diagnosing an error, please put the full error message below:

for the first one :-0.1543182172530106
for the second one:-0.5440211108893694

And, finally, make sure to include the versions of your packages. Specifically, show us the output of qml.about().

Hello @Shalqam_Shalqami!

Welcome to the forum :smiley: ! There seems to be a little bit of confusion here between the concepts of a tensor product and a matrix product. The matrix product of two PauliX matrices is the identity:

X\cdot X = \begin{pmatrix} 0 & 1 \\ 1 & 0\end{pmatrix}\cdot \begin{pmatrix} 0 & 1 \\ 1 & 0\end{pmatrix} = \begin{pmatrix} 1 & 0 \\ 0 & 1\end{pmatrix}.

But the tensor product of two Pauli Matrices isn’t the identity:

X\cdot X = \begin{pmatrix} 0 & 1 \\ 1 & 0\end{pmatrix}\otimes \begin{pmatrix} 0 & 1 \\ 1 & 0\end{pmatrix} = \begin{pmatrix} 0 & 0 & 0 & 1\\ 0 & 0 & 1 & 0\\ 0 & 1 & 0 & 0 \\ 1 & 0 & 0 & 0\end{pmatrix}.

The confusion may be coming from the fact that sometimes in Python we represent the matrix product with the operator @. But in PennyLane, @ is reserved for the tensor product, which is a completely different operation, defined as

It makes sense that it’s a 4\times4 matrix, since it has to act on two qubits :eyes:.

Hope this helps!

Alvaro

1 Like