Error in simple custom unitary

I am making a custom unitary operator, in the example code below I take np.identity() for an easy demonstration.

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

nqubits = 16

class transform_matrix(Operation):

    num_params = 0
    num_wires = nqubits
    #ndim_params = (0,)
    par_domain = None

    @staticmethod
    def compute_matrix():
        B = np.identity(nqubits)
        return B
        
dev = qml.device("default.qubit")
@qml.qnode(dev)
def circ():
    transform_matrix(wires=range(nqubits))
    return qml.expval(qml.PauliZ(0))
    
x = circ()

File “/home/Downloads/test.py”, line 37, in
x = circ()
File “/home/sreetamadas/penny-qiskit/lib/python3.10/site-packages/pennylane/workflow/qnode.py”, line 1164, in call
return self._impl_call(*args, **kwargs)
File “/home/sreetamadas/penny-qiskit/lib/python3.10/site-packages/pennylane/workflow/qnode.py”, line 1150, in _impl_call
res = self._execution_component(args, kwargs, override_shots=override_shots)
File “/home/sreetamadas/penny-qiskit/lib/python3.10/site-packages/pennylane/workflow/qnode.py”, line 1103, in _execution_component
res = qml.execute(
File “/home/sreetamadas/penny-qiskit/lib/python3.10/site-packages/pennylane/workflow/execution.py”, line 666, in execute
results = inner_execute(tapes)
File “/home/sreetamadas/penny-qiskit/lib/python3.10/site-packages/pennylane/workflow/execution.py”, line 316, in inner_execute
results = device_execution(transformed_tapes)
File “/home/sreetamadas/penny-qiskit/lib/python3.10/site-packages/pennylane/devices/modifiers/simulator_tracking.py”, line 30, in execute
results = untracked_execute(self, circuits, execution_config)
File “/home/sreetamadas/penny-qiskit/lib/python3.10/site-packages/pennylane/devices/modifiers/single_tape_support.py”, line 32, in execute
results = batch_execute(self, circuits, execution_config)
File “/home/sreetamadas/penny-qiskit/lib/python3.10/site-packages/pennylane/logging/decorators.py”, line 61, in wrapper_entry
return func(*args, **kwargs)
File “/home/sreetamadas/penny-qiskit/lib/python3.10/site-packages/pennylane/devices/default_qubit.py”, line 597, in execute
return tuple(
File “/home/sreetamadas/penny-qiskit/lib/python3.10/site-packages/pennylane/devices/default_qubit.py”, line 598, in
_simulate_wrapper(
File “/home/sreetamadas/penny-qiskit/lib/python3.10/site-packages/pennylane/devices/default_qubit.py”, line 863, in _simulate_wrapper
return simulate(circuit, **kwargs)
File “/home/sreetamadas/penny-qiskit/lib/python3.10/site-packages/pennylane/logging/decorators.py”, line 61, in wrapper_entry
return func(*args, **kwargs)
File “/home/sreetamadas/penny-qiskit/lib/python3.10/site-packages/pennylane/devices/qubit/simulate.py”, line 354, in simulate
state, is_state_batched = get_final_state(
File “/home/sreetamadas/penny-qiskit/lib/python3.10/site-packages/pennylane/logging/decorators.py”, line 61, in wrapper_entry
return func(*args, **kwargs)
File “/home/sreetamadas/penny-qiskit/lib/python3.10/site-packages/pennylane/devices/qubit/simulate.py”, line 174, in get_final_state
state = apply_operation(
File “/home/sreetamadas/anaconda3/lib/python3.10/functools.py”, line 889, in wrapper
return dispatch(args[0].class)(*args, **kw)
File “/home/sreetamadas/penny-qiskit/lib/python3.10/site-packages/pennylane/devices/qubit/apply_operation.py”, line 218, in apply_operation
return _apply_operation_default(op, state, is_state_batched, debugger)
File “/home/sreetamadas/penny-qiskit/lib/python3.10/site-packages/pennylane/devices/qubit/apply_operation.py”, line 229, in _apply_operation_default
return apply_operation_tensordot(op, state, is_state_batched=is_state_batched)
File “/home/sreetamadas/penny-qiskit/lib/python3.10/site-packages/pennylane/devices/qubit/apply_operation.py”, line 129, in apply_operation_tensordot
reshaped_mat = math.reshape(mat, new_mat_shape)
File “/home/sreetamadas/penny-qiskit/lib/python3.10/site-packages/autoray/autoray.py”, line 81, in do
return func(*args, **kwargs)
File “/home/sreetamadas/penny-qiskit/lib/python3.10/site-packages/numpy/core/fromnumeric.py”, line 285, in reshape
return _wrapfunc(a, ‘reshape’, newshape, order=order)
File “/home/sreetamadas/penny-qiskit/lib/python3.10/site-packages/numpy/core/fromnumeric.py”, line 59, in _wrapfunc
return bound(*args, **kwds)
ValueError: cannot reshape array of size 256 into shape (2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2)


I am using the latest version of Pennylane installed in an environment along with Pennylane-qiskit.

Why not use QubitUnitary from the PennyLane library?

Great suggestion @Bnesh ! :raised_hands:

Some additional suggestions @sdas,

Your code isn’t really adding any gates to the circuit and your class seems to have some issues with receiving the wires in the way in which you have set it at the moment.

You might indeed benefit from using qml.QubitUnitary . You can see an example of how to use it in the documentation.

Why don’t you try it out and let us know how it goes?