One wire with multi-observables

in the quantum-classical hybird neural network, to avoid time consuming, I can not give the qubit a great value.
when I put 3 observables on one wire and put measurement process on, I get some error as below:


it says Only observables that are qubit-wise commuting Pauli words can be returned on the same wire.
when I take the middle observable as qml.Identity, same error happens.
are there some proper qubit-wise commuting observables in pennylane?
or some other methods broaden the output dimension beyond nqubits?

Hey @qhc and welcome to the forum!

Hmm, could you post a fully running code example that reproduces your error? And do you have the latest PL version installed?

For example, running this code with PL 0.15.1 works with no problems:

import pennylane as qml

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

@qml.qnode(dev)
def circuit():
    qml.Hadamard(wires=0)
    return qml.expval(qml.Identity(0)), qml.expval(qml.Identity(0))

print(circuit()) # [1., 1.]

thanks for your reply, my PL version is 0.15.1, my code:

import numpy as np
import pennylane as qml
from pennylane.devices.default_qubit_tf import DefaultQubitTF

dev2 = DefaultQubitTF(wires=8)
@qml.qnode(dev2, diff_method='backprop', interface='tf')
def circuit(inputs, enc_1):
    for i in range(8):
        qml.Hadamard(wires=i)
    # ====================================================
    # ======================== enc_1 =====================
    for i in range(8):
        qml.RZ(enc_1[i]*inputs[i], wires=(i))
        # qml.RY(enc_1[2*i+1]*inputs[2*i+1], wires=(2*i+1))
    # ====================================================
    # ======================== output =====================
    output = []
    for ind in range(8):
        output.append(qml.expval(qml.PauliX(ind)))
        output.append(qml.expval(qml.Identity(ind)))
        output.append(qml.expval(qml.PauliZ(ind)))
    return output

inputs = np.float64(np.random.randn(24))
enc_1 = np.float64(np.random.randn(8))
print(circuit(inputs=inputs, enc_1=enc_1))

output: QuantumFunctionError: Only observables that are qubit-wise commuting Pauli words can be returned on the same wire

Thanks for the details!

The error message actually says it all here: PennyLane allows for multiple measurements on the same wire in a simulation as long as the observables are Pauli words (i.e. Pauli operators, identities or tensor products thereof) and commute. Your measurements are unfortunately non-commuting.

One reason for this restriction is that internally, PL decomposes measurements into a pre-measurement circuit and a simple measurement. But if two measurements don’t commute, their pre-measurement circuits are not the same. However, this may be up for a refactor.

If you would like to champion the idea that PL allows non-commuting measurements on the same wires on simulators, you could open an issue with your feature request - the more users need something, the more likely it gets implemented soon :slight_smile:

1 Like

does there exist some commuting Pauli words observables in PL?

Hi @qhc. If I understand your question correctly, yes PennyLane supports multiple measurements of such commuting observables performed on the same wire. For instance, the error in your code can be fixed by removing one of the PauliX or PauliZ measurements while keeping the Identity one such as:

for ind in range(8):
    output.append(qml.expval(qml.PauliX(ind)))
    output.append(qml.expval(qml.Identity(ind)))
return output

PennyLane also supports measurements of tensor product of observables as explained here. Please let us know if you have any other question.

1 Like

thanks, you all help me a lot
:grinning: