# Turning quantum nodes into Torch Layers

**URL:** https://discuss.pennylane.ai/t/turning-quantum-nodes-into-torch-layers/3395
**Category:** PennyLane Help
**Created:** [September 5, 2023, 4:11pm UTC](https://discuss.pennylane.ai/t/turning-quantum-nodes-into-torch-layers/3395 "2023-09-05T16:11:33Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![kevinkawchak](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kevinkawchak/32/1801_2.png) [@kevinkawchak](https://discuss.pennylane.ai/u/kevinkawchak)
#### Post date: [September 5, 2023, 4:11pm UTC](https://discuss.pennylane.ai/t/turning-quantum-nodes-into-torch-layers/3395/1 "2023-09-05T16:11:33Z")

</div>

Hello @Tom_Bromley,

Do you have any suggestions to obtain better accuracies using qml.qnn.TorchLayers vs. original models? The code has been implemented into another classical notebook.

Thank you,

Reference:

> **[Turning quantum nodes into Torch Layers | PennyLane Demos](https://pennylane.ai/qml/demos/tutorial_qnn_module_torch/)**
>
> Learn how to create hybrid ML models in PennyLane using Torch

---

<div class="post-metadata">

### Author: ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)
#### Post date: [September 5, 2023, 9:46pm UTC](https://discuss.pennylane.ai/t/turning-quantum-nodes-into-torch-layers/3395/2 "2023-09-05T21:46:07Z")

</div>

Hey @kevinkawchak!

What do you mean by “original models”? Are you trying to compare quantum / hybrid models to purely classical ones on the same task?

---

<div class="post-metadata">

### Author: ![kevinkawchak](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kevinkawchak/32/1801_2.png) [@kevinkawchak](https://discuss.pennylane.ai/u/kevinkawchak)
#### Post date: [September 6, 2023, 1:07am UTC](https://discuss.pennylane.ai/t/turning-quantum-nodes-into-torch-layers/3395/3 "2023-09-06T01:07:37Z")

</div>

Yes, this is the case.

---

<div class="post-metadata">

### Author: ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)
#### Post date: [September 6, 2023, 1:32pm UTC](https://discuss.pennylane.ai/t/turning-quantum-nodes-into-torch-layers/3395/4 "2023-09-06T13:32:53Z")

</div>

Thanks for clarifying! This is a question we get quite often, and unfortunately the answer isn’t very satisfying 😅. There are many things that go into making a machine learning model work well for a specific task, including the choice of optimizer, the choice of hyperparameters — learning rate, step size, batch size, etc — the cost function, the model architecture itself, and many more. It’s a tedious task to tweak all of those things and find the right combination.

I also think it’s not good to assume that quantum / hybrid models _should_ train better (better. accuracy, for instance) than a classical model. The question is much more nuanced there. Maria Schuld gave a nice talk on this topic recently, you should check it out!

[![](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/2X/1/1514c018eda8b27aa2ca4f54a60913347621d31a.jpeg "QML: 2023 and Beyond!") ](https://www.youtube.com/watch?v=zNqYkPByyP0)

---

<div class="post-metadata">

### Author: ![kevinkawchak](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kevinkawchak/32/1801_2.png) [@kevinkawchak](https://discuss.pennylane.ai/u/kevinkawchak)
#### Post date: [September 6, 2023, 5:05pm UTC](https://discuss.pennylane.ai/t/turning-quantum-nodes-into-torch-layers/3395/5 "2023-09-06T17:05:02Z")

</div>

Hello @isaacdevlugt,

I appreciate the resources. The issue appears to be with implementing the def forward() step from “Creating non-sequential models” into other notebooks. For instance, the demo make\_moons dataset for the first qlayer uses: x\_1 = self.qlayer\_1(x\_1)

A hybrid model I am working on returns parameters in the run summary as “qlayer\_1 | TorchLayer | 12” using self.qlayer\_1 = qml.qnn.TorchLayer(qnode, weight\_shapes)

I would need an analogous solution to “x\_1” for the hybrid notebook for def forward(self, pixel\_values) in order to fully incorporate the quantum circuit. Thank you.

References:

> **[Turning quantum nodes into Torch Layers | PennyLane Demos](https://pennylane.ai/qml/demos/tutorial_qnn_module_torch/)**
>
> Learn how to create hybrid ML models in PennyLane using Torch

‘[https://github.com/NielsRogge/Transformers-Tutorials/blob/master/VisionTransformer/Fine\_tuning\_the\_Vision\_Transformer\_on\_CIFAR\_10\_with\_PyTorch\_Lightning.ipynb](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/VisionTransformer/Fine_tuning_the_Vision_Transformer_on_CIFAR_10_with_PyTorch_Lightning.ipynb)’

---

<div class="post-metadata">

### Author: ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)
#### Post date: [September 7, 2023, 7:24pm UTC](https://discuss.pennylane.ai/t/turning-quantum-nodes-into-torch-layers/3395/6 "2023-09-07T19:24:01Z")

</div>

Can you reply back with some code that summarizes the issue you’re facing? That would help me understand the problem better 🙂

---

<div class="post-metadata">

### Author: ![kevinkawchak](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kevinkawchak/32/1801_2.png) [@kevinkawchak](https://discuss.pennylane.ai/u/kevinkawchak)
#### Post date: [September 8, 2023, 2:27am UTC](https://discuss.pennylane.ai/t/turning-quantum-nodes-into-torch-layers/3395/7 "2023-09-08T02:27:13Z")

</div>

Hello @isaacdevlugt,

class ViTLightningModule(pl.LightningModule):

```
def __init__ (self, num_labels=10):
    super(ViTLightningModule, self). __init__ ()
    self.vit = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224-in21k',
                                                          num_labels=10,
                                                          id2label=id2label,
                                                          label2id=label2id)
    self.qlayer_1 = qml.qnn.TorchLayer(qnode, weight_shapes)

def forward(self, pixel_values):
    outputs = self.vit(pixel_values=pixel_values)
    outputs = self.qlayer_1()
    return outputs.logits

```

Some weights of ViTForImageClassification were not initialized from the model checkpoint at google/vit-base-patch16-224-in21k and are newly initialized: [‘classifier.weight’, ‘classifier.bias’]  
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.  
INFO:pytorch\_lightning.utilities.rank\_zero:GPU available: True (cuda), used: True  
INFO:pytorch\_lightning.utilities.rank\_zero:TPU available: False, using: 0 TPU cores  
INFO:pytorch\_lightning.utilities.rank\_zero:IPU available: False, using: 0 IPUs  
INFO:pytorch\_lightning.utilities.rank\_zero:HPU available: False, using: 0 HPUs  
INFO:pytorch\_lightning.accelerators.cuda:LOCAL\_RANK: 0 - CUDA\_VISIBLE\_DEVICES: [0]  
INFO:pytorch\_lightning.callbacks.model\_summary:  
| Name | Type | Params

0 | vit | ViTForImageClassification | 85.8 M  
1 | qlayer\_1 | TorchLayer | 16

85.8 M Trainable params  
0 Non-trainable params  
85.8 M Total params  
343.225 Total estimated model params size (MB)  
Sanity Checking DataLoader 0: 0%  
0/2 [00:00\<?, ?it/s]

TypeError Traceback (most recent call last)  
 in \<cell line: 15\>()  
13 model = ViTLightningModule()  
14 trainer = Trainer(accelerator=‘gpu’, max\_epochs=5) #, callbacks=[EarlyStopping(monitor=‘validation\_loss’)])  
—\> 15 trainer.fit(model)

15 frames  
/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in \_call\_impl(self, \*args, \*\*kwargs)  
1499 or \_global\_backward\_pre\_hooks or \_global\_backward\_hooks  
1500 or \_global\_forward\_hooks or \_global\_forward\_pre\_hooks):  
 → 1501 return forward\_call(\*args, \*\*kwargs)  
1502 # Do not call functions when jit is used  
1503 full\_backward\_hooks, non\_full\_backward\_hooks = ,

TypeError: TorchLayer.forward() missing 1 required positional argument: ‘inputs’

---

<div class="post-metadata">

### Author: ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)
#### Post date: [September 8, 2023, 4:39pm UTC](https://discuss.pennylane.ai/t/turning-quantum-nodes-into-torch-layers/3395/8 "2023-09-08T16:39:09Z")

</div>

Thanks @kevinkawchak! I still don’t quite understand what the issue you’re facing is. I can’t run your code either since there are dependencies missing. However, your forward pass does look problematic:

```auto
def forward(self, pixel_values):
    outputs = self.vit(pixel_values=pixel_values)
    outputs = self.qlayer_1()
    return outputs.logits

```

`qlayer_1` probably needs some inputs. You are also overriding what `outputs` is after you calculate `self.vit(pixel_values=pixel_values)`.

If this isn’t the issue, having a complete code example that I can run would help 🙂

---

<div class="post-metadata">

### Author: ![kevinkawchak](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kevinkawchak/32/1801_2.png) [@kevinkawchak](https://discuss.pennylane.ai/u/kevinkawchak)
#### Post date: [September 9, 2023, 12:08am UTC](https://discuss.pennylane.ai/t/turning-quantum-nodes-into-torch-layers/3395/9 "2023-09-09T00:08:54Z")

</div>

> [@kevinkawchak](#):
>
> ‘[https://github.com/NielsRogge/Transformers-Tutorials/blob/master/VisionTransformer/Fine\_tuning\_the\_Vision\_Transformer\_on\_CIFAR\_10\_with\_PyTorch\_Lightning.ipynb](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/VisionTransformer/Fine_tuning_the_Vision_Transformer_on_CIFAR_10_with_PyTorch_Lightning.ipynb)’

Implementing code from [Turning quantum nodes into Torch Layers | PennyLane Demos](https://pennylane.ai/qml/demos/tutorial_qnn_module_torch)

---

<div class="post-metadata">

### Author: ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)
#### Post date: [September 11, 2023, 2:13pm UTC](https://discuss.pennylane.ai/t/turning-quantum-nodes-into-torch-layers/3395/10 "2023-09-11T14:13:35Z")

</div>

If you’re just trying to inject some “quantumness” into the code provided in that GitHub repository, then definitely there’s something wrong with your forward pass:

```auto
def forward(self, pixel_values):
    outputs = self.vit(pixel_values=pixel_values)
    outputs = self.qlayer_1()
    return outputs.logits

```

`qlayer_1` probably needs inputs, and you are also overriding what `outputs` is after you calculate `self.vit(pixel_values=pixel_values)`. If `qlayer_1` comes after `vit`, then you’d need to do something like this:

```auto
def forward(self, pixel_values):
    outputs = self.vit(pixel_values=pixel_values)
    outputs = self.qlayer_1(outputs)
    return outputs.logits

```

The output of `vit` would then be the input to `qlayer_1`. Also, I’m not sure that `outputs` with have a `logits` attribute, so that might have to be changed as well. Let me know if that helps!

---

<div class="post-metadata">

### Author: ![kevinkawchak](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kevinkawchak/32/1801_2.png) [@kevinkawchak](https://discuss.pennylane.ai/u/kevinkawchak)
#### Post date: [September 18, 2023, 3:35am UTC](https://discuss.pennylane.ai/t/turning-quantum-nodes-into-torch-layers/3395/11 "2023-09-18T03:35:11Z")

</div>

Thank you,

Is this what you are suggesting?

class ViTLightningModule(pl.LightningModule):  
def **init** (inputs, self, num\_labels=10):  
super(ViTLightningModule, self). **init** ()  
self.vit = ViTForImageClassification.from\_pretrained(‘google/vit-base-patch16-224-in21k’,  
num\_labels=10,  
id2label=id2label,  
label2id=label2id)  
self.qlayer\_1 = qml.qnn.TorchLayer(inputs, qnode, weight\_shapes)

Reference:  
'[https://github.com/NielsRogge/Transformers-Tutorials/blob/master/VisionTransformer/Fine\_tuning\_the\_Vision\_Transformer\_on\_CIFAR\_10\_with\_PyTorch\_Lightning.ipynb](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/VisionTransformer/Fine_tuning_the_Vision_Transformer_on_CIFAR_10_with_PyTorch_Lightning.ipynb)

---

<div class="post-metadata">

### Author: ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)
#### Post date: [September 18, 2023, 2:42pm UTC](https://discuss.pennylane.ai/t/turning-quantum-nodes-into-torch-layers/3395/12 "2023-09-18T14:42:47Z")

</div>

Not quite — I think the key might be to just modify your `forward` function to this 🙂

```auto
def forward(self, pixel_values):
    outputs = self.vit(pixel_values=pixel_values)
    outputs = self.qlayer_1(outputs)
    return outputs.logits

```

---

<div class="post-metadata">

### Author: ![kevinkawchak](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kevinkawchak/32/1801_2.png) [@kevinkawchak](https://discuss.pennylane.ai/u/kevinkawchak)
#### Post date: [December 11, 2023, 7:21pm UTC](https://discuss.pennylane.ai/t/turning-quantum-nodes-into-torch-layers/3395/13 "2023-12-11T19:21:35Z")

</div>

Hello, for the original Torch layer demo running in Colab, it is only allowing for a single qubit to be used with qml.Hadamard OR qml.RX. These are the code and errors for 2 qubits.

```auto
    qml.Hadamard(wires=range(n_qubits))
    qml.AngleEmbedding(inputs, wires=range(n_qubits))
    qml.BasicEntanglerLayers(weights, wires=range(n_qubits))

```

OR

```auto
    qml.RX(inputs, wires=range(n_qubits))
    qml.AngleEmbedding(inputs, wires=range(n_qubits))
    qml.BasicEntanglerLayers(weights, wires=range(n_qubits))

```

* * *

ValueError Traceback (most recent call last)  
 in \<cell line: 13\>()  
18 opt.zero\_grad()  
19  
—\> 20 loss\_evaluated = loss(model(xs), ys)  
21 loss\_evaluated.backward()  
22

11 frames  
/usr/local/lib/python3.10/dist-packages/pennylane/operation.py in **init** (self, wires, id, \*params)  
1045  
1046 elif len(self.\_wires) != self.num\_wires:  
 → 1047 raise ValueError(  
1048 f"{self.name}: wrong number of wires. "  
1049 f"{len(self.\_wires)} wires given, {self.num\_wires} expected."

ValueError: Hadamard: wrong number of wires. 2 wires given, 1 expected.

OR

* * *

ValueError Traceback (most recent call last)  
 in \<cell line: 13\>()  
18 opt.zero\_grad()  
19  
—\> 20 loss\_evaluated = loss(model(xs), ys)  
21 loss\_evaluated.backward()  
22

12 frames  
/usr/local/lib/python3.10/dist-packages/pennylane/operation.py in **init** (self, wires, id, \*params)  
1045  
1046 elif len(self.\_wires) != self.num\_wires:  
 → 1047 raise ValueError(  
1048 f"{self.name}: wrong number of wires. "  
1049 f"{len(self.\_wires)} wires given, {self.num\_wires} expected."

ValueError: RX: wrong number of wires. 2 wires given, 1 expected.

---

<div class="post-metadata">

### Author: ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)
#### Post date: [December 12, 2023, 5:16pm UTC](https://discuss.pennylane.ai/t/turning-quantum-nodes-into-torch-layers/3395/14 "2023-12-12T17:16:54Z")

</div>

Hey @kevinkawchak,

`qml.Hadamard` and `qml.RX` only accept one wire 🙂. Applying the same gate to multiple wires can be done with `qml.broadcast` ([qml.broadcast — PennyLane 0.33.0 documentation](https://docs.pennylane.ai/en/stable/code/api/pennylane.broadcast.html)) or with a good old fashion `for` loop!

---

<div class="post-metadata">

### Author: ![kevinkawchak](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kevinkawchak/32/1801_2.png) [@kevinkawchak](https://discuss.pennylane.ai/u/kevinkawchak)
#### Post date: [December 19, 2023, 9:13am UTC](https://discuss.pennylane.ai/t/turning-quantum-nodes-into-torch-layers/3395/15 "2023-12-19T09:13:36Z")

</div>

Hello, the qml.Hadamard broadcast works, but qml.RY for ‘Broadcasting single gates’ or ‘Broadcasting templates’ does not for the Torch layer demo running in Colab.

```auto
n_qubits = 4
dev = qml.device("default.qubit", wires=n_qubits)

def mytemplate(weights, wires):
    qml.RY(weights, wires=range(n_qubits))

@qml.qnode(dev)
def qnode(inputs, weights):
    broadcast(unitary=mytemplate, pattern="single", wires=[0,1,2,3], parameters=weights)
    return [qml.expval(qml.PauliZ(wires=i)) for i in range(n_qubits)]

```

Error:

```auto
<ipython-input-224-ab4485a0c93b>:1: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
  X = torch.tensor(X, requires_grad=True).float()
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-224-ab4485a0c93b> in <cell line: 12>()
     16 opt.zero_grad()
     17 
---> 18 loss_evaluated = loss_func(model(xs), ys)
     19 loss_evaluated.backward()
     20 

11 frames
/usr/local/lib/python3.10/dist-packages/pennylane/templates/broadcast.py in _preprocess(parameters, pattern, wires)
    130 num_params = PATTERN_TO_NUM_PARAMS[pattern](_wires)
    131 if shape[0] != num_params:
--> 132 raise ValueError(
    133 f"Parameters must contain entries for {num_params} unitaries; got {shape[0]} entries"
    134 )

ValueError: Parameters must contain entries for 4 unitaries; got 6 entries

```

---

<div class="post-metadata">

### Author: ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)
#### Post date: [December 20, 2023, 10:26pm UTC](https://discuss.pennylane.ai/t/turning-quantum-nodes-into-torch-layers/3395/16 "2023-12-20T22:26:19Z")

</div>

Hi @kevinkawchak ,

I see a couple of issues.

1. On one hand RY only acts on a single wire at a time so `qml.RY(weights, wires=range(n_qubits))` should actually be `qml.RY(weights, wires=wire)`, where `wire` is a single number.
2. You don’t really need to create a custom template for RY. You can just use it as your unitary! Custom templates are most useful when you want to use more than one gate in a single broadcast pattern.
3. Your qnode doesn’t seem to be using the `inputs` argument.

Given these 3 points I think that what you want to do is the following.

```auto
n_qubits = 4
dev = qml.device("default.qubit", wires=n_qubits)

@qml.qnode(dev)
def qnode(weights):
    broadcast(unitary=qml.RY, pattern="single", wires=[0,1,2,3], parameters=weights)
    return [qml.expval(qml.PauliZ(wires=i)) for i in range(n_qubits)]

weights = [1,2,3,4];

qnode(weights)

```

This code works for me so hopefully it works for you too!

---

<div class="post-metadata">

### Author: ![kevinkawchak](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kevinkawchak/32/1801_2.png) [@kevinkawchak](https://discuss.pennylane.ai/u/kevinkawchak)
#### Post date: [December 21, 2023, 4:31am UTC](https://discuss.pennylane.ai/t/turning-quantum-nodes-into-torch-layers/3395/17 "2023-12-21T04:31:47Z")

</div>

Thank you, is there a way to make the weights trainable?

---

<div class="post-metadata">

### Author: ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)
#### Post date: [December 21, 2023, 3:07pm UTC](https://discuss.pennylane.ai/t/turning-quantum-nodes-into-torch-layers/3395/18 "2023-12-21T15:07:04Z")

</div>

Yep! `weights` should be differentiable if its written in numpy:

```auto
n_qubits = 4
dev = qml.device("default.qubit", wires=n_qubits)

@qml.qnode(dev)
def qnode(weights):
    qml.broadcast(unitary=qml.RY, pattern="single", wires=[0,1,2,3], parameters=weights)
    return [qml.expval(qml.PauliZ(wires=i)) for i in range(n_qubits)]

def cost(weights):
    outs = qnode(weights)
    return np.sum(outs)

weights = np.array([1,2,3,4]);

opt = qml.GradientDescentOptimizer(0.1)

opt.step_and_cost(cost, weights)

```

```auto
(tensor([1., 2., 3., 4.], requires_grad=True), -1.5194806481430605)

```

---

<div class="post-metadata">

### Author: ![kevinkawchak](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kevinkawchak/32/1801_2.png) [@kevinkawchak](https://discuss.pennylane.ai/u/kevinkawchak)
#### Post date: [December 24, 2023, 1:41am UTC](https://discuss.pennylane.ai/t/turning-quantum-nodes-into-torch-layers/3395/19 "2023-12-24T01:41:53Z")

</div>

Hello, I can’t get either of these methods to work inside the Torch Layers demo. The original unitary/entry error was removed by using the code provided and setting n\_layers=n\_qubits, but now receive this error:

````auto
<ipython-input-109-d039678a60e4>:1: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
  X = torch.tensor(X, requires_grad=True).float()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-109-d039678a60e4> in <cell line: 13>()
     18 opt.zero_grad()
     19 
---> 20 loss_evaluated = loss(model(xs), ys)
     21 loss_evaluated.backward()
     22 

10 frames
/usr/local/lib/python3.10/dist-packages/pennylane/templates/broadcast.py in broadcast(unitary, wires, pattern, parameters, kwargs)
    565 else:
    566 for i in range(len(wire_sequence)):
--> 567 unitary(*parameters[i], wires=wire_sequence[i], **kwargs)

TypeError: RY. __init__ () got multiple values for argument 'wires'```
````

---

<div class="post-metadata">

### Author: ![kevinkawchak](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kevinkawchak/32/1801_2.png) [@kevinkawchak](https://discuss.pennylane.ai/u/kevinkawchak)
#### Post date: [January 2, 2024, 6:37am UTC](https://discuss.pennylane.ai/t/turning-quantum-nodes-into-torch-layers/3395/20 "2024-01-02T06:37:20Z")

</div>

Hello, How many RY embedding layers and RY trainable layers is this in the Torch Layers demo?

```auto
n_qubits = 10
dev = qml.device("default.qubit", wires=n_qubits)

@qml.qnode(dev)
def qnode(inputs, weights):
    qml.AngleEmbedding(inputs, wires=range(n_qubits), rotation='Y')
    qml.RandomLayers(weights, wires=range(n_qubits), ratio_imprim=0, rotations=[qml.RY], seed=42)
    return [qml.expval(qml.PauliZ(wires=i)) for i in range(n_qubits)]
weights = np.array([1,2,3,4,5,6,7,8,9,10]);

n_layers = 1
weight_shapes = {"weights": (n_layers, n_qubits)}

```

[Next page](https://discuss.pennylane.ai/t/turning-quantum-nodes-into-torch-layers/3395.md?page=2)
