# Pennylane running on torch.cuda?

**URL:** <https://discuss.pennylane.ai/t/pennylane-running-on-torch-cuda/4367>\
**Category:** PennyLane Help\
**Created:** [March 28, 2024, 9:36am UTC](https://discuss.pennylane.ai/t/pennylane-running-on-torch-cuda/4367 "2024-03-28T09:36:15Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![oceaneyyys](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/oceaneyyys/32/2455_2.png) [@oceaneyyys](https://discuss.pennylane.ai/u/oceaneyyys)\
**Post date:** [March 28, 2024, 9:36am UTC](https://discuss.pennylane.ai/t/pennylane-running-on-torch-cuda/4367/1 "2024-03-28T09:36:15Z")

</div>

hello guys! thank your reading this.does anyone know how to run pennylane code on torch.cuda ,  
thank you if know this,much appreciate!

---

<div class="post-metadata">

**Author:** ![mlxd](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/mlxd/32/526_2.png) [@mlxd](https://discuss.pennylane.ai/u/mlxd)\
**Post date:** [March 28, 2024, 5:43pm UTC](https://discuss.pennylane.ai/t/pennylane-running-on-torch-cuda/4367/2 "2024-03-28T17:43:27Z")

</div>

Hi @oceaneyyys

You can create your state-vector using the Torch backend on a CUDA device with

```bash
dev = qml.device("default.qubit.torch", wires=2, torch_device="cuda:0")

```

and your circuit parameters too with something similar to

```auto
data = torch.rand(2, 2, device="cuda:0")

```

which should ensure the data is all allocated on the GPU at index 0. Hope this helps!

---

<div class="post-metadata">

**Author:** ![oceaneyyys](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/oceaneyyys/32/2455_2.png) [@oceaneyyys](https://discuss.pennylane.ai/u/oceaneyyys)\
**Post date:** [March 30, 2024, 1:54pm UTC](https://discuss.pennylane.ai/t/pennylane-running-on-torch-cuda/4367/3 "2024-03-30T13:54:33Z")

</div>

hi,thank you for your reply,so appreciated.  
but i got error when i run this code,so how can i make this work,i would so appreciate it if you know the solution.

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

---

<div class="post-metadata">

**Author:** ![Quvance](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/quvance/32/2421_2.png) [@Quvance](https://discuss.pennylane.ai/u/Quvance)\
**Post date:** [March 30, 2024, 3:57pm UTC](https://discuss.pennylane.ai/t/pennylane-running-on-torch-cuda/4367/4 "2024-03-30T15:57:01Z")

</div>

I have encountered this error before. There is somehow a conflict between resource allocation.

It’s a while ago, but the way I resolved this issue eventually was:

a. to install a new conda environment from scratch  
b.to ensure CUDA was properly installed and the right version.

I hope this helps

Berend

---

<div class="post-metadata">

**Author:** ![oceaneyyys](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/oceaneyyys/32/2455_2.png) [@oceaneyyys](https://discuss.pennylane.ai/u/oceaneyyys)\
**Post date:** [March 31, 2024, 1:22pm UTC](https://discuss.pennylane.ai/t/pennylane-running-on-torch-cuda/4367/5 "2024-03-31T13:22:43Z")

</div>

thank you so much for your reply!so aprreciate!

do you use the code:  
dev = qml.device(“default.qubit.torch”, wires=2, torch\_device=“cuda:0”)

or this code:  
dev = qml.device(“lightning.gpu”, wires=n\_qubits)

it’s bit confusing to use the cuda on pytorch and pennylane

---

<div class="post-metadata">

**Author:** ![Quvance](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/quvance/32/2421_2.png) [@Quvance](https://discuss.pennylane.ai/u/Quvance)\
**Post date:** [March 31, 2024, 9:49pm UTC](https://discuss.pennylane.ai/t/pennylane-running-on-torch-cuda/4367/6 "2024-03-31T21:49:08Z")

</div>

The code that caused my problem was very different. My bet would be that the first line is the correct one. I could be wrong but you can only learn through trial and error.

dev = qml.device(“default.qubit.torch”, wires=2, torch\_device=“cuda:0”)

---

<div class="post-metadata">

**Author:** ![mlxd](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/mlxd/32/526_2.png) [@mlxd](https://discuss.pennylane.ai/u/mlxd)\
**Post date:** [April 1, 2024, 3:00pm UTC](https://discuss.pennylane.ai/t/pennylane-running-on-torch-cuda/4367/7 "2024-04-01T15:00:06Z")

</div>

Hi @oceaneyyys

If you can give us a minimum working example of your code (a script that executes end-to-end and reproduces your error), that should help us to identify where the problem occurs.

---

<div class="post-metadata">

**Author:** ![oceaneyyys](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/oceaneyyys/32/2455_2.png) [@oceaneyyys](https://discuss.pennylane.ai/u/oceaneyyys)\
**Post date:** [April 5, 2024, 5:28am UTC](https://discuss.pennylane.ai/t/pennylane-running-on-torch-cuda/4367/8 "2024-04-05T05:28:51Z")

</div>

thank you for your reply!

---

<div class="post-metadata">

**Author:** ![oceaneyyys](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/oceaneyyys/32/2455_2.png) [@oceaneyyys](https://discuss.pennylane.ai/u/oceaneyyys)\
**Post date:** [April 5, 2024, 5:34am UTC](https://discuss.pennylane.ai/t/pennylane-running-on-torch-cuda/4367/9 "2024-04-05T05:34:45Z")

</div>

thank you for your help! my code is below:

```python
import pennylane as qml
import torch
import torch.nn as nn

# dev = qml.device('default.qubit', wires=4)
dev = qml.device('default.qubit.torch', wires=4, torch_device='cuda')

def circuit(inputs,weights):
    qml.AngleEmbedding(features=inputs, wires=range(4), rotation='Y')
    for i in range(4):
        qml.RX(weights[i], wires=i)
    return qml.expval(qml.PauliZ(0))

class Net(nn.Module):
    def __init__ (self):
        super(Net, self). __init__ ()
        weight_shapes = {"weights": (4)}
        qnode = qml.QNode(circuit, dev, interface='torch')
        self.ql1 = qml.qnn.TorchLayer(qnode, weight_shapes)
      

    def forward(self, X):
        X=self.ql1(X)
        X=torch.nn.functional.sigmoid(X)

        return X

torch_cuda=torch.device("cuda")
inputs = torch.rand(10, 4)
labels = torch.randint(0, 2, (10,))

network=Net()
criterion = nn.BCELoss() # loss function
optimizer = torch.optim.SGD(network.parameters(), lr = 0.01) # optimizer

inputs.to(torch_cuda)
labels.to(torch_cuda)
network.to(torch_cuda)
epochs=10
for epoch in range(epochs):
       

        
        tr_loss = 0
        labels=labels.type(torch.FloatTensor)
        optimizer.zero_grad()
        outputs = network(inputs)

        loss = criterion(outputs,labels)
        loss.backward()
        optimizer.step()
        tr_loss =loss.data.numpy()

        print(tr_loss)

```

and i got the code error:  
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

thank your help again,so appreciated!

---

<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:** [April 8, 2024, 2:50pm UTC](https://discuss.pennylane.ai/t/pennylane-running-on-torch-cuda/4367/10 "2024-04-08T14:50:16Z")

</div>

Hi @oceaneyyys,  
Thank you for sharing this example.

Could you please post the output of `qml.about()`?  
And please also post your full error traceback. It will help us find the issue 😃

---

<div class="post-metadata">

**Author:** ![mlxd](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/mlxd/32/526_2.png) [@mlxd](https://discuss.pennylane.ai/u/mlxd)\
**Post date:** [April 8, 2024, 3:23pm UTC](https://discuss.pennylane.ai/t/pennylane-running-on-torch-cuda/4367/11 "2024-04-08T15:23:06Z")

</div>

Hi @oceaneyyys

The issue here seems to be related to your torch input data. When casting the labels in your loop from an integer array to a float array, the data then resides on the CPU.

I made some modifications to the locality of your data in the provided script, and this allows it to run end to end:

```bash
import pennylane as qml
import torch
import torch.nn as nn

dev = qml.device('default.qubit.torch', wires=4, torch_device='cuda')

def circuit(inputs,weights):
    qml.AngleEmbedding(features=inputs, wires=range(4), rotation='Y')
    for i in range(4):
        qml.RX(weights[i], wires=i)
    return qml.expval(qml.PauliZ(0))

class Net(nn.Module):
    def __init__ (self):
        super(Net, self). __init__ ()
        weight_shapes = {"weights": (4)}
        qnode = qml.QNode(circuit, dev, interface='torch')
        self.ql1 = qml.qnn.TorchLayer(qnode, weight_shapes)

    def forward(self, X):
        X=self.ql1(X)
        X=torch.nn.functional.sigmoid(X)

        return X

torch_cuda=torch.device("cuda")
inputs = torch.rand(10, 4, device=torch_cuda)
labels = torch.randint(0, 2, (10,), device=torch_cuda)

inputs.to(torch_cuda)
labels.to(torch_cuda)
network=Net()
network.to(torch_cuda)
epochs=10

criterion = nn.BCELoss() # loss function
optimizer = torch.optim.SGD(network.parameters(), lr = 0.01) # optimizer

for epoch in range(epochs):
    tr_loss = 0 
        labels=labels.type(torch.FloatTensor)
        labels = labels.to("cuda")

        optimizer.zero_grad()
        outputs = network(inputs)

        loss = criterion(outputs,labels)
        loss.backward()
        optimizer.step()
        cpu_loss = loss.to("cpu")
        tr_loss = cpu_loss.data.numpy()

        print(tr_loss)

```

With Torch, to ensure end-to-end execution on the GPU, you often need to be explicit about where the data lives — even if you are working on an initially GPU-place buffer operations on it may sometimes give you CPU data back. Hope this helps.

---

<div class="post-metadata">

**Author:** ![oceaneyyys](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/oceaneyyys/32/2455_2.png) [@oceaneyyys](https://discuss.pennylane.ai/u/oceaneyyys)\
**Post date:** [April 9, 2024, 8:19am UTC](https://discuss.pennylane.ai/t/pennylane-running-on-torch-cuda/4367/12 "2024-04-09T08:19:16Z")

</div>

thank you so much! it worked.i’m so grateful for your constant help! much appreciated!

---

<div class="post-metadata">

**Author:** ![oceaneyyys](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/oceaneyyys/32/2455_2.png) [@oceaneyyys](https://discuss.pennylane.ai/u/oceaneyyys)\
**Post date:** [April 9, 2024, 8:19am UTC](https://discuss.pennylane.ai/t/pennylane-running-on-torch-cuda/4367/13 "2024-04-09T08:19:54Z")

</div>

thank you so much! it worked.i’m so grateful for your constant help! much appreciated!

---

<div class="post-metadata">

**Author:** ![oceaneyyys](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/oceaneyyys/32/2455_2.png) [@oceaneyyys](https://discuss.pennylane.ai/u/oceaneyyys)\
**Post date:** [April 9, 2024, 8:21am UTC](https://discuss.pennylane.ai/t/pennylane-running-on-torch-cuda/4367/14 "2024-04-09T08:21:07Z")

</div>

thank you for your reply!

---

<div class="post-metadata">

**Author:** ![Amit\_Kulshreshta](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/amit_kulshreshta/32/1849_2.png) [@Amit\_Kulshreshta](https://discuss.pennylane.ai/u/Amit_Kulshreshta)\
**Post date:** [November 20, 2024, 9:41am UTC](https://discuss.pennylane.ai/t/pennylane-running-on-torch-cuda/4367/15 "2024-11-20T09:41:38Z")

</div>

> [@mlxd](#):
>
> `qml.device('default.qubit.torch', wires=self.wires, torch_device='cuda')`

Error:  
`pennylane.DeviceError: Device default.qubit.torch does not exist. Make sure the required plugin is installed.`

Which is the required plugin for this that I need to install?

---

<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:** [November 21, 2024, 4:25pm UTC](https://discuss.pennylane.ai/t/pennylane-running-on-torch-cuda/4367/16 "2024-11-21T16:25:13Z")

</div>

Hi @Amit_Kulshreshta,

You don’t need any plugin. You should use `'default.qubit'` instead. PennyLane will know to use the Torch interface if you’re adding Torch data.

Let me know if this works for you!
