# Qml.ctrl operation on GPU

**URL:** <https://discuss.pennylane.ai/t/qml-ctrl-operation-on-gpu/2836>\
**Category:** PennyLane Help\
**Created:** [April 13, 2023, 4:20pm UTC](https://discuss.pennylane.ai/t/qml-ctrl-operation-on-gpu/2836 "2023-04-13T16:20:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![imakash](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/imakash/32/1428_2.png) [@imakash](https://discuss.pennylane.ai/u/imakash)\
**Post date:** [April 13, 2023, 4:20pm UTC](https://discuss.pennylane.ai/t/qml-ctrl-operation-on-gpu/2836/1 "2023-04-13T16:20:12Z")

</div>

Hello, I am trying to implement a quantum circuit that contains controlled CY gates. The circuit contains multiple layers. Following is the code snippet for a single layer.

```auto
def ansatz_2(n_qubits, y_weight):
    for wire, y_weight in enumerate(y_weight):
                qml.ctrl(op=qml.RY(y_weight, wires=wire), control=(wire-1)%n_qubits)

```

Other details are as follows:

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

```

When I run the ansatz on the CPU, it works well. However, on running the same ansatz on GPU, it throws the following error:

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

Also, If I replace the layer with the following code snippet, the operation happens on GPU without any error.

```auto
def ansatz_2(n_qubits, y_weight):
    for wire, y_weight in enumerate(y_weight):
            qml.RY(y_weight, wires=wire)

```

So, I assume that I am doing something wrong with qml.ctrl implementation. Any help here would be appreciated.

---

<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:** [April 14, 2023, 2:00pm UTC](https://discuss.pennylane.ai/t/qml-ctrl-operation-on-gpu/2836/2 "2023-04-14T14:00:51Z")

</div>

Hey @imakash! I’m missing a lot of your code here, so I can’t say for certain what’s going on. That said, it sounds to me like you’re passing in two `torch.tensor` objects that are on different devices. Something akin to this:

```python
import pennylane as qml
import torch

dev = qml.device("default.qubit", wires=2)

@qml.qnode(dev, interface="torch")
def circuit(weight_1, weight_2):
    qml.ctrl(op=qml.RY(weight_1, wires=1), control=0)
    qml.RX(weight_2, wires=0)
    return qml.state()

weights_1 = torch.tensor(0.1, device='cpu')
weights_2 = torch.tensor(0.1, device='cuda') # if I change this to 'cpu' it works

print(circuit(weights_1, weights_2))

```

Can you share your full code?

---

<div class="post-metadata">

**Author:** ![imakash](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/imakash/32/1428_2.png) [@imakash](https://discuss.pennylane.ai/u/imakash)\
**Post date:** [April 14, 2023, 5:08pm UTC](https://discuss.pennylane.ai/t/qml-ctrl-operation-on-gpu/2836/3 "2023-04-14T17:08:19Z")

</div>

Hello @isaacdevlugt

Thank you for your response.

The entire code is quite complex and part of an ongoing research project. Therefore, I can’t post it on a public forum. However, are you able to run qml.ctrl operation on GPU?

---

<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:** [April 14, 2023, 8:03pm UTC](https://discuss.pennylane.ai/t/qml-ctrl-operation-on-gpu/2836/4 "2023-04-14T20:03:10Z")

</div>

Ah! Was able to replicate the issue. Had to dust off my other laptop with an NVIDIA card 😅. You found a bug! Nice work! I encourage you to make a bug report on our GitHub (click [here](https://github.com/PennyLaneAI/pennylane/issues/new?assignees=&labels=bug+%3Abug%3A&template=bug_report.yml&title=%5BBUG%5D) and it will direct you to the bug report form).

Indeed, this code replicates the error you see:

```python
import pennylane as qml
import torch

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

@qml.qnode(dev, interface="torch")
def circuit(weight_1, weight_2):
    qml.ctrl(op=qml.RY(weight_1, wires=1), control=0) # when this gate is removed, all is fine
    qml.RX(weight_2, wires=0)
    return qml.state()

weights_1 = torch.tensor(0.1, device='cuda')
weights_2 = torch.tensor(0.1, device='cuda')

print(circuit(weights_1, weights_2))
# RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0, cpu!

```

As a fix for now, you can use `qml.CRY` (see [here](https://docs.pennylane.ai/en/stable/code/api/pennylane.CRY.html)) instead of `qml.ctrl`. This works:

```python
import pennylane as qml
import torch

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

@qml.qnode(dev, interface="torch", diff_method="backprop")
def circuit(weight_1, weight_2):
    qml.CRY(weight_1, wires=[0, 1])
    qml.RX(weight_2, wires=0)
    return qml.state()

weights_1 = torch.tensor(0.1, device='cuda')
weights_2 = torch.tensor(0.1, device='cuda')

print(circuit(weights_1, weights_2))
'''
 tensor([0.9988+0.0000j, 0.0000+0.0000j, 0.0000-0.0500j, 0.0000+0.0000j],
       device='cuda:0', dtype=torch.complex128)
'''

```

---

<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:** [April 14, 2023, 8:05pm UTC](https://discuss.pennylane.ai/t/qml-ctrl-operation-on-gpu/2836/5 "2023-04-14T20:05:37Z")

</div>

Also worth mentioning here is that `lightning.gpu` is the best way to utilize a GPU in PennyLane. You can find out more here: [Installation — PennyLane-Lightning-GPU 0.30.0.dev0 documentation](https://docs.pennylane.ai/projects/lightning-gpu/en/latest/installation.html)

That said, if it doesn’t suit your needs, then no worries!

---

<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:** [April 14, 2023, 9:44pm UTC](https://discuss.pennylane.ai/t/qml-ctrl-operation-on-gpu/2836/6 "2023-04-14T21:44:51Z")

</div>

Our resident CUDA whisperer @mlxd has already made a fix for this: [Qml.ctrl operation on GPU - #5 by isaacdevlugt](https://discuss.pennylane.ai/t/qml-ctrl-operation-on-gpu/2836/5)

---

<div class="post-metadata">

**Author:** ![imakash](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/imakash/32/1428_2.png) [@imakash](https://discuss.pennylane.ai/u/imakash)\
**Post date:** [April 15, 2023, 2:39pm UTC](https://discuss.pennylane.ai/t/qml-ctrl-operation-on-gpu/2836/7 "2023-04-15T14:39:20Z")

</div>

Hey @isaacdevlugt

Thank you very much for your help. I will also submit a bug report as you suggested.

---

<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:** [April 17, 2023, 1:47pm UTC](https://discuss.pennylane.ai/t/qml-ctrl-operation-on-gpu/2836/8 "2023-04-17T13:47:29Z")

</div>

Awesome! Looks like this is set to be merged in good time!
