# Pass batches with @qml.batch\_params but not as a decorator

**URL:** <https://discuss.pennylane.ai/t/pass-batches-with-qml-batch-params-but-not-as-a-decorator/3025>\
**Category:** PennyLane Help\
**Created:** [June 8, 2023, 2:01pm UTC](https://discuss.pennylane.ai/t/pass-batches-with-qml-batch-params-but-not-as-a-decorator/3025 "2023-06-08T14:01:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vallevaro](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/vallevaro/32/1561_2.png) [@vallevaro](https://discuss.pennylane.ai/u/vallevaro)\
**Post date:** [June 8, 2023, 2:01pm UTC](https://discuss.pennylane.ai/t/pass-batches-with-qml-batch-params-but-not-as-a-decorator/3025/1 "2023-06-08T14:01:06Z")

</div>

Hello,

I am working on building a nn.Module class that has a circuit, and that uses qml.qnn.TorchLayer.  
For training, I would like to pass batches of images through this layer. This is my code:

```auto
class MyNet(nn.Module):
    def __init__ (self, n_qubits, n_layers=1, device='lightning.qubit', rot_axis='X):
        super(). __init__ ()
        self.n_qubits = n_qubits
        self.rot_axis = rot_axis
        self.device = device
        self.dev = qml.device(self.device, wires=self.n_qubits)
        self.qnode = qml.QNode(self.circuit, self.dev, interface='torch')
        self.weight_shapes = {"weights": qml.StronglyEntanglingLayers.shape(n_layers=n_layers, n_wires=self.n_qubits)}
        self.qlayer = qml.qnn.TorchLayer(self.qnode, self.weight_shapes)

    @qml.batch_params
    def circuit(self, inputs, weights):
        qml.AngleEmbedding(features=inputs, wires=range(self.n_qubits), rotation=self.rot_axis)
        qml.StronglyEntanglingLayers(weights=weights, wires=range(self.n_qubits), imprimitive=qml.ops.CZ)
        return [qml.expval(qml.PauliZ(i)) for i in range(self.n_qubits)]

    def forward(self, x):
       return self.qlayer(x)

```

I tried using the @qml.batch\_params decorator in my code, but it gives me the error:

```auto
TypeError: QNode must include an argument with name inputs for inputting data

```

How can I make passing batches of data work through this self.qlayer?  
Why is the decorator not working?

Thank you.

---

<div class="post-metadata">

**Author:** ![eddddddy](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/eddddddy/32/1716_2.png) [@eddddddy](https://discuss.pennylane.ai/u/eddddddy)\
**Post date:** [June 8, 2023, 2:34pm UTC](https://discuss.pennylane.ai/t/pass-batches-with-qml-batch-params-but-not-as-a-decorator/3025/2 "2023-06-08T14:34:40Z")

</div>

Hi @vallevaro,

Recently we’ve added NumPy-style broadcasting capabilities to PennyLane, so the `qml.batch_params` is no longer necessary. We’re looking to deprecating that transform soon.

In your case, simply removing the `qml.batch_params` decorator will make your code works as expected:

```auto
class MyNet(nn.Module):
    def __init__ (self, n_qubits, n_layers=1, device='lightning.qubit', rot_axis='X'):
        super(). __init__ ()
        self.n_qubits = n_qubits
        self.rot_axis = rot_axis
        self.device = device
        self.dev = qml.device(self.device, wires=self.n_qubits)
        self.qnode = qml.QNode(self.circuit, self.dev, interface='torch')
        self.weight_shapes = {"weights": qml.StronglyEntanglingLayers.shape(n_layers=n_layers, n_wires=self.n_qubits)}
        self.qlayer = qml.qnn.TorchLayer(self.qnode, self.weight_shapes)

    def circuit(self, inputs, weights):
        qml.AngleEmbedding(features=inputs, wires=range(self.n_qubits), rotation=self.rot_axis)
        qml.StronglyEntanglingLayers(weights=weights, wires=range(self.n_qubits), imprimitive=qml.ops.CZ)
        return [qml.expval(qml.PauliZ(i)) for i in range(self.n_qubits)]

    def forward(self, x):
       return self.qlayer(x)

def main():
    n_qubits = 2
    net = MyNet(n_qubits)

    x = torch.tensor(np.random.uniform(0, 1, (10, 2)))
    print(net(x))

```

Output:

```auto
tensor([[-0.9911, 0.9202],
        [-0.6943, 0.8678],
        [-0.9854, 0.9979],
        [-0.8679, 0.6809],
        [-0.9817, 0.7578],
        [-0.9891, 0.9288],
        [-0.9823, 0.9442],
        [-0.7939, 0.5708],
        [-0.8812, 0.6121],
        [-0.7665, 0.9585]], dtype=torch.float64, grad_fn=<StackBackward0>)

```

---

<div class="post-metadata">

**Author:** ![vallevaro](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/vallevaro/32/1561_2.png) [@vallevaro](https://discuss.pennylane.ai/u/vallevaro)\
**Post date:** [June 9, 2023, 3:37pm UTC](https://discuss.pennylane.ai/t/pass-batches-with-qml-batch-params-but-not-as-a-decorator/3025/3 "2023-06-09T15:37:08Z")

</div>

> [@eddddddy](#):
>
> ```auto
> class MyNet(nn.Module):
> def __init__ (self, n_qubits, n_layers=1, device='lightning.qubit', rot_axis='X'):
> super(). __init__ ()
> self.n_qubits = n_qubits
> self.rot_axis = rot_axis
> self.device = device
> self.dev = qml.device(self.device, wires=self.n_qubits)
> self.qnode = qml.QNode(self.circuit, self.dev, interface='torch')
> self.weight_shapes = {"weights": qml.StronglyEntanglingLayers.shape(n_layers=n_layers, n_wires=self.n_qubits)}
> self.qlayer = qml.qnn.TorchLayer(self.qnode, self.weight_shapes)
> 
> def circuit(self, inputs, weights):
> qml.AngleEmbedding(features=inputs, wires=range(self.n_qubits), rotation=self.rot_axis)
> qml.StronglyEntanglingLayers(weights=weights, wires=range(self.n_qubits), imprimitive=qml.ops.CZ)
> return [qml.expval(qml.PauliZ(i)) for i in range(self.n_qubits)]
> 
> def forward(self, x):
> return self.qlayer(x)
> 
> def main():
> n_qubits = 2
> net = MyNet(n_qubits)
> 
> x = torch.tensor(np.random.uniform(0, 1, (10, 2)))
> print(net(x))
> 
> ```

Thank you so much! I don’t know why I didn’t try this out. 😃
