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.3k stars 592 forks source link

[BUG] Grouping functionality can not handle things that aren't paulis, tensors of hamiltonians, or hamiltonians of paulis #4362

Closed isaacdevlugt closed 3 months ago

isaacdevlugt commented 1 year ago

Expected behavior

Returning multiple projector expectation values in a QNode should work.

import pennylane as qml

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

@qml.qnode(dev)
def func():
    qml.Hadamard(0)
    return qml.expval(qml.Projector([0], wires=[0])), qml.expval(qml.Projector([1], wires=[0]))

func()
0.5, 0.5

Actual behavior

TypeError: Expected a Pauli word Observable instance, instead got expval(Projector(array([0]), wires=[0])).

Additional information

Discovered while trying to propose a workaround for this: https://discuss.pennylane.ai/t/mid-circuit-measurement-in-pennylane/3187/3

Source code

import pennylane as qml

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

@qml.qnode(dev)
def func():
    qml.Hadamard(0)
    return qml.expval(qml.Projector([0], wires=[0])), qml.expval(qml.Projector([1], wires=[0]))

func()

Tracebacks

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[3], line 8
      5     qml.Hadamard(0)
      6     return qml.expval(qml.Projector([0], wires=[0])), qml.expval(qml.Projector([1], wires=[0]))
----> 8 func()

File [~/pennylane-stable/lib/python3.9/site-packages/pennylane/qnode.py:950](https://file+.vscode-resource.vscode-cdn.net/Users/isaac/Documents/user_questions/~/pennylane-stable/lib/python3.9/site-packages/pennylane/qnode.py:950), in QNode.__call__(self, *args, **kwargs)
    948     self.execute_kwargs.pop("mode")
    949 # pylint: disable=unexpected-keyword-arg
--> 950 res = qml.execute(
    951     [self.tape],
    952     device=self.device,
    953     gradient_fn=self.gradient_fn,
    954     interface=self.interface,
    955     gradient_kwargs=self.gradient_kwargs,
    956     override_shots=override_shots,
    957     **self.execute_kwargs,
    958 )
    960 res = res[0]
    962 # convert result to the interface in case the qfunc has no parameters

File [~/pennylane-stable/lib/python3.9/site-packages/pennylane/interfaces/execution.py:486](https://file+.vscode-resource.vscode-cdn.net/Users/isaac/Documents/user_questions/~/pennylane-stable/lib/python3.9/site-packages/pennylane/interfaces/execution.py:486), in execute(tapes, device, gradient_fn, interface, grad_on_execution, gradient_kwargs, cache, cachesize, max_diff, override_shots, expand_fn, max_expansion, device_batch_transform)
    482 expand_fn = _preprocess_expand_fn(expand_fn, device, max_expansion)
    484 #### Executing the configured setup #####
...
--> 277     raise TypeError(f"Expected a Pauli word Observable instance, instead got {pauli_word}.")
    279 if wire_map is None:
    280     num_wires = len(pauli_word.wires)

TypeError: Expected a Pauli word Observable instance, instead got expval(Projector(array([0]), wires=[0])).

System information

Name: PennyLane
Version: 0.31.0
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/PennyLaneAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /Users/isaac/pennylane-stable/lib/python3.9/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml
Required-by: PennyLane-Lightning

Platform info:           macOS-13.4.1-x86_64-i386-64bit
Python version:          3.9.14
Numpy version:           1.23.5
Scipy version:           1.10.0
Installed devices:
- default.gaussian (PennyLane-0.31.0)
- default.mixed (PennyLane-0.31.0)
- default.qubit (PennyLane-0.31.0)
- default.qubit.autograd (PennyLane-0.31.0)
- default.qubit.jax (PennyLane-0.31.0)
- default.qubit.tf (PennyLane-0.31.0)
- default.qubit.torch (PennyLane-0.31.0)
- default.qutrit (PennyLane-0.31.0)
- null.qubit (PennyLane-0.31.0)
- lightning.qubit (PennyLane-Lightning-0.31.0)

Existing GitHub issues

eddddddy commented 1 year ago

The issue is that in split_non_commuting we assume that all observables are Pauli words: https://github.com/PennyLaneAI/pennylane/blob/3f7bbd682d3be11728cdefd1e85ad20c320cf485/pennylane/transforms/split_non_commuting.py#L163

Of course, the fact that Projector isn't itself a Pauli word is where the error comes from.

Not sure what we want to do in this case @albi3ro @Jaybsoni. Was this being handled correctly by the old grouping module? Is the Pauli grouping module meant to support these cases as well?

albi3ro commented 4 months ago

Note that this issue is no longer present in default.qubit or lightning.qubit, but it will still occur on any device that relies on split_non_commuting.