question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Decomposition of unsupported operations broken between v0.24.0 and v0.25.0-rc0

See original GitHub issue

Expected behavior

Operations not supported by a device should be decomposed by PennyLane. This still seems to work for operations that are not supported by default.qubit but devices that only support a subset of operations of default.qubit do no longer get them decomposed.

Here is a minimal example demonstrating the problem:

import pennylane as qml
from pennylane.devices import DefaultQubit

class MyDevice(DefaultQubit):
    operations = {'RZ', 'RY'} # Rot is not supported

    def apply(self, operations, rotations=None, **kwargs):
        for operation in operations:
            if operation.__class__.__name__ not in self.operations:
                raise ValueError(f"Operation {operation} is not supported")

dev = MyDevice(wires=1)

@qml.qnode(dev, diff_method="parameter-shift")
def circuit():
    qml.Rot(0.1, 0.2, 0.3, wires=0)
    return qml.probs(wires=0)

print(qml.draw(circuit, expansion_strategy='device')())
print(circuit())

The expected behavior is displayed under v0.24.0:

0: ──RZ(0.10)──RY(0.20)──RZ(0.30)─┤  Probs
[1. 0.]

Actual behavior

Under v0.25.0-rc0 I get:

0: ──Rot(0.10,0.20,0.30)─┤  Probs
Traceback (most recent call last):
  File "pl_tests/pl_test14.py", line 20, in <module>
    print(circuit())
  File "/.../pennylane/pennylane/qnode.py", line 660, in __call__
    res = qml.execute(
  File "/.../pennylane/pennylane/interfaces/execution.py", line 443, in execute
    res = _execute(
  File "/.../pennylane/pennylane/interfaces/autograd.py", line 66, in execute
    return _execute(
  File "/.../autograd/autograd/tracer.py", line 48, in f_wrapped
    return f_raw(*args, **kwargs)
  File "/.../pennylane/pennylane/interfaces/autograd.py", line 110, in _execute
    res, jacs = execute_fn(tapes, **gradient_kwargs)
  File "/.../pennylane/pennylane/interfaces/execution.py", line 197, in wrapper
    res = fn(execution_tapes.values(), **kwargs)
  File "/.../pennylane/pennylane/interfaces/execution.py", line 122, in fn
    return original_fn(tapes, **kwargs)
  File "/home/cvjjm/miniforge3/envs/qcware/lib/python3.8/contextlib.py", line 75, in inner
    return func(*args, **kwds)
  File "/.../pennylane/pennylane/_qubit_device.py", line 579, in batch_execute
    res = self.execute(circuit)
  File "/.../pennylane/pennylane/_qubit_device.py", line 313, in execute
    self.apply(circuit.operations, rotations=circuit.diagonalizing_gates, **kwargs)
  File "pl_tests/pl_test14.py", line 10, in apply
    raise ValueError(f"Operation {operation} is not supported")
ValueError: Operation Rot(0.1, 0.2, 0.3, wires=[0]) is not supported

The decomposition happens neither during qml.draw() nor when executing the circuit.

This breaks pretty much all my custom devices…

Additional information

No response

Source code

No response

Tracebacks

No response

System information

Python 3.8.8 | packaged by conda-forge | (default, Feb 20 2021, 16:22:27)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pennylane as qml; qml.about()
Name: PennyLane
Version: 0.25.0
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/XanaduAI/pennylane
Author: None
Author-email: None
License: Apache License 2.0
Location: /home/cvjjm/src/covqcstack/qcware/pennylane
Requires: numpy, scipy, networkx, retworkx, autograd, toml, appdirs, semantic-version, autoray, cachetools, pennylane-lightning
Required-by: pytket-pennylane, PennyLane-Qchem, PennyLane-Lightning, covvqetools

Platform info:           Linux-5.10.102.1-microsoft-standard-WSL2-x86_64-with-glibc2.10
Python version:          3.8.8
Numpy version:           1.20.1
Scipy version:           1.8.0
Installed devices:
- default.gaussian (PennyLane-0.25.0)
- default.mixed (PennyLane-0.25.0)
- default.qubit (PennyLane-0.25.0)
- default.qubit.autograd (PennyLane-0.25.0)
- default.qubit.jax (PennyLane-0.25.0)
- default.qubit.tf (PennyLane-0.25.0)
- default.qubit.torch (PennyLane-0.25.0)
- default.qutrit (PennyLane-0.25.0)
- pytket.pytketdevice (pytket-pennylane-0.1.0)
- lightning.qubit (PennyLane-Lightning-0.24.0)

Existing GitHub issues

  • I have searched existing GitHub issues to make sure the issue does not already exist.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
albi3rocommented, Aug 9, 2022

@cvjjm , In order to support general operations, including user-custom operations and nested operator arithmetic, we updated the DefaultQubit.stopping_condition so that DefaultQubit supports anything with a matrix.

To override this behaviour, you can add a custom stopping_condition:

class MyDevice(DefaultQubit):
    operations = {'RZ', 'RY'} # Rot is not supported

    @property
    def stopping_condition(self):
        def accepts_obj(obj):
            return obj.name in self.observables.union(self.operations)
        return qml.BooleanFn(accepts_obj)

    def apply(self, operations, rotations=None, **kwargs):
        for operation in operations:
            if operation.__class__.__name__ not in self.operations:
                raise ValueError(f"Operation {operation} is not supported")

I hope these couple of addition will be easy to add.

By relying on the stopping_condition instead of a list of names, we can have much more flexible logic for determining what operations are supported and which ones aren’t. Now you can write your own operations and have DefaultQubit natively execute them as long as they define their own matrix (see op.has_matrix)!

2reactions
cvjjmcommented, Aug 9, 2022

Great! The above minimal example can be easily made into a unit test to prevent this from happening un-noticed in the future.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[问题] 在ProFiled中如何拿到form实例,然后修改值? - IssueHint
[BUG] Decomposition of unsupported operations broken between v0.24.0 and v0.25.0-rc0, 13, 2022-08-09, 2022-08-25.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found