Custom gate error using basic tutorial: Type error: RXX.decomposition() missing 2 required positional arguments

Hi, I have a custom parameterized ansatz which I would like to repeat in my circuit with different parameters for certain gates. I have a routine to acomplish this but I started by creating a template custom operator but am not able to do it. I am using the online tutorial for a simple custom gate but am getting the following error:

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pennylane\qnode.py:847, in QNode.__call__(self, *args, **kwargs)
    843     self._update_original_device()
    845     return res
--> 847 res = qml.execute(
    848     [self.tape],
    849     device=self.device,
    850     gradient_fn=self.gradient_fn,
    851     interface=self.interface,
    852     gradient_kwargs=self.gradient_kwargs,
    853     override_shots=override_shots,
    854     **self.execute_kwargs,
    855 )
    857 if old_interface == "auto":
    858     self.interface = "auto"

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pennylane\interfaces\execution.py:724, in execute(tapes, device, gradient_fn, interface, mode, gradient_kwargs, cache, cachesize, max_diff, override_shots, expand_fn, max_expansion, device_batch_transform)
    718 except ImportError as e:
    719     raise qml.QuantumFunctionError(
    720         f"{mapped_interface} not found. Please install the latest "
    721         f"version of {mapped_interface} to enable the '{mapped_interface}' interface."
...
-> 1346         result = fn(*args, **kwargs)
   1348     qscript = QuantumScript.from_queue(q)
   1349     qscript._qfunc_output = result

TypeError: RXX.decomposition() missing 2 required positional arguments: 'theta' and 'wires'

I have the following code:

import pennylane as qml
from pennylane.operation import Operation
from pennylane import numpy as np

class RXX(Operation):
    num_params = 1
    num_wires = 2
    par_domain = "R"

    @staticmethod
    def decomposition(theta, wires):
        return [qml.PauliRot(theta, 'XX', wires=wires)]
       dev = qml.device('default.qubit', wires=2, shots=100)

@qml.qnode(dev)
def circuit(theta):
    RXX(theta, wires=[0, 1])
    qml.Hadamard(1)
    return qml.expval(qml.PauliZ(0) @ qml.PauliY(1))
circuit(0.1)

I have tried different ways by first saving the params and wires at Operation initiazation and reference that in the static decomposition but that doesn’t work. Can you kindly guide me is this is even possible or should I just directly apply my gates?

Dear @omersajid9,
thanks for the report, this is a “bug” in the blog post you were using. Sorry about that!
The reason I say “bug” is because we tend to not update blog posts, as they carry a date and are intended to reflect the codebase at the time of blog release. The documentation is a more timely reference and is intended to be up to date at all times. In particular, for custom operators see this documentation page.

The reason for the thrown error is that the operator class was modified in the meantime. Luckily, there is a very simple fix for your problem :slight_smile:

Replace the function name decomposition by compute_decomposition and you’re good to go!
Let me know in case you run into any other issues, and happy coding! :slight_smile:

1 Like