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.17k stars 568 forks source link

Device support for `ExpecationMP` with eigvals and wires representation #5874

Open albi3ro opened 1 week ago

albi3ro commented 1 week ago

Pennylane allows specifying measurements like ExpectationMP via a combination of wires + eigenvalues:

ExpectationMP(eigvals=np.array([-1.0, 1.0]), wires=qml.wires.Wires((0,1)))

While not user-facing, this can be produced internally, for example from:

>>> qml.expval(qml.X(0)).expand().circuit
[Hadamard(wires=[0]), expval(eigvals=[ 1. -1.], wires=[0])]

Trying to measure such an observable on default.qubit gives:

File [pennylane/pennylane/devices/qubit/measure.py:190](pennylane/pennylane/devices/qubit/measure.py#line=189), in get_measurement_function(measurementprocess, state)
    187     return state_diagonalizing_gates
    189 if isinstance(measurementprocess, ExpectationMP):
--> 190     if measurementprocess.obs.name == "SparseHamiltonian":
    191         return csr_dot_products
    193     if measurementprocess.obs.name == "Hermitian":

AttributeError: 'NoneType' object has no attribute 'name'

I'm not too concerned about this, as there's really no user-facing of creating this, but we should at least track it.

A valid option might be to remove support for the eigvals+wires representation, if we are uninterested in supporting and maintaining it.