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.27k stars 585 forks source link

Raise an error or warning in `qml.compile` if `basis_set` contains operations instead of strings. #6132

Open dwierichs opened 3 weeks ago

dwierichs commented 3 weeks ago

Feature details

As the title suggests: qml.compile takes an argument basis_set, which is expected to be a sequence of operation names, i.e. strings. If a user passes a sequence of PennyLane operator types, no error is raised but the basis set becomes equivalent to []. It would be nice to raise a warning, or even an error, if users pass non-empty sequences that contain operators instead of operator names.

Implementation

Straightforward.

How important would you say this feature is?

1: Not important. Would be nice to have.

Additional information

No response

albi3ro commented 3 weeks ago

Or we could update the stopping condition to:

class_types = tuple(o for o in basis_set if isinstance(o, type)
class_names = set(o for o in basis_set if isinstance(o, str))
def stopping_condition(obj):
    return obj.name in class_names or isinstance(obj, class_types)

Why raise an error when we can just support the operation types?

I think we could even turn this into a good first issue.

dwierichs commented 3 weeks ago

Haha, yes, this works, too, of course :rocket:

AnuravModak commented 3 weeks ago

Hi @albi3ro and @dwierichs , if this is available for external contributors and want contribution on this, do let me know! Will be happy to take this up!

albi3ro commented 3 weeks ago

@AnuravModak Yes this issue is open for external contributions.

It will involve updating this function here:

https://github.com/PennyLaneAI/pennylane/blob/2f9631b1ce61217665f0beaefd5b83a76b9c2688/pennylane/transforms/compile.py#L185

to accept types specified in basis_set in addition to string based names. The docstring and type hints will also need to be updated.

We may still want to consider an error if basis_set contains anything that is not a string or an Operator subclass.