# GPU usage for pennylane-qiskit

**URL:** <https://discuss.pennylane.ai/t/gpu-usage-for-pennylane-qiskit/1567>\
**Category:** PennyLane Plugins\
**Created:** [December 20, 2021, 9:18am UTC](https://discuss.pennylane.ai/t/gpu-usage-for-pennylane-qiskit/1567 "2021-12-20T09:18:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Davide\_Zuliani](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/davide_zuliani/32/775_2.png) [@Davide\_Zuliani](https://discuss.pennylane.ai/u/Davide_Zuliani)\
**Post date:** [December 20, 2021, 9:18am UTC](https://discuss.pennylane.ai/t/gpu-usage-for-pennylane-qiskit/1567/1 "2021-12-20T09:18:22Z")

</div>

Dear experts,

I’ve seen that the Qiskit framework allows simulating quantum circuits using GPUs. I was wondering if this is possible also in Pennylane through the pennylane-qiskit plugin.

Thank you very much 🙂

---

<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:** [December 20, 2021, 12:06pm UTC](https://discuss.pennylane.ai/t/gpu-usage-for-pennylane-qiskit/1567/2 "2021-12-20T12:06:16Z")

</div>

Hi @Davide_Zuliani Thanks for the question.  
It is possible to enable the Aer GPU backend, though we currently cannot guarantee it will work across all use-cases.

If you would like to try it out with a supported device and let us know, following [https://qiskit.org/documentation/tutorials/simulators/1\_aer\_provider.html#GPU-Simulation](https://qiskit.org/documentation/tutorials/simulators/1_aer_provider.html#GPU-Simulation) ensure the `qiskit-aer-gpu` package is installed first. Then we can enable the device with:

```python
dev = qml.device("qiskit.aer", wires=1)
dev.backend.set_options(device='GPU')

```

Feel free to get in touch if you need any additional assistance. Hope this helps!

---

<div class="post-metadata">

**Author:** ![Davide\_Zuliani](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/davide_zuliani/32/775_2.png) [@Davide\_Zuliani](https://discuss.pennylane.ai/u/Davide_Zuliani)\
**Post date:** [December 20, 2021, 1:34pm UTC](https://discuss.pennylane.ai/t/gpu-usage-for-pennylane-qiskit/1567/3 "2021-12-20T13:34:49Z")

</div>

Hi @mlxd, thank you for your quick answer 🙂  
So I gave it a try but it doesn’t seem to work (the GPU-util is always at 0%).  
Here is a simplified version of my code:

```
import os
import argparse
import qiskit
import pytorch_lightning as pl
import torch.nn as nn
import torch
from torch.functional import F
import pennylane as qml
from data_preprocessing import prepare_data
import torchmetrics
import pandas as pd
import numpy as np

CUDA_VISIBLE_DEVICES=0

#for noise
from qiskit.providers.aer.noise import NoiseModel
from qiskit import IBMQ, Aer

parser = argparse.ArgumentParser()
parser.add_argument("-c", "--city", help="city name for IBM hardware")
args = parser.parse_args()

WantNoise = True

#load ibm account
IBMQ.enable_account('')
provider = IBMQ.get_provider(hub='')
city = args.city

print("hardware selected: ", "ibmq_"+city)
print("possible choices: sydney, toronto, guadalupe, lagos, santiago, manila, bogota, jakarta, quito, belem, lima")

n_qubits = 4
n_QAOA_layers = 1
n_SE_layers = 4

def circuit(inputs,QAOA_weights,SE_weights):
    qml.templates.QAOAEmbedding(inputs,QAOA_weights,wires=range(n_qubits))
    qml.templates.StronglyEntanglingLayers(weights=SE_weights,wires=range(n_qubits))
return qml.expval(qml.PauliZ(0))

class MuonModel(pl.LightningModule):
def __init__ (self):
    super(). __init__ ()
    if WantNoise:
        noise_model = NoiseModel.from_backend(provider.get_backend('ibmq_'+city))
        tfdev = qml.device('qiskit.aer',wires=n_qubits,noise_model=noise_model)
        tfdev.backend.set_options(device='GPU')
    else:
        tfdev = qml.device('qiskit.aer',wires=n_qubits)
    
    circ = qml.QNode(circuit,tfdev,interface='torch',diff_method='best')
     
    qlayer = qml.qnn.TorchLayer(circ,{'QAOA_weights': (n_QAOA_layers, 2*n_qubits) , 'SE_weights' : (n_SE_layers,n_qubits,3)})
    self.training_acc = torchmetrics.Accuracy()
    self.validation_acc = torchmetrics.Accuracy()
    self.quantum = qlayer

def forward(self, x):
    return 0.5*(self.quantum(x)+1)
    #return self.output(self.quantum(x))

def training_step(self, batch, batch_idx):
    x, y = batch
    #print(y)
    y_hat = self(x.float())
    #print(y_hat)
    loss = F.mse_loss(y_hat,y.float())
    #print(list(self.parameters()))
     
    return {'loss' : loss, 'preds' : y_hat, 'target' : y}

def training_step_end(self, outputs):
    self.training_acc(outputs['preds'], outputs['target'])
    self.log('train_accuracy', self.training_acc,on_epoch=True,prog_bar=True)
    self.log('train_loss',outputs['loss'],on_epoch=True,sync_dist=True)

    return outputs

def training_epoch_end(self, outputs):
    return None

def validation_step(self, batch, batch_idx):
    x, y = batch
    #print(y)
    y_hat = self(x.float())
    #print(y_hat)
    loss = F.mse_loss(y_hat,y.float())
    #print(list(self.parameters()))
     
    return {'loss' : loss, 'preds' : y_hat, 'target' : y}

def validation_step_end(self, outputs):
    self.validation_acc(outputs['preds'], outputs['target'])
    self.log('val_accuracy', self.validation_acc,on_epoch=True,prog_bar=True)
    self.log('val_loss',outputs['loss'],on_epoch=True)

    return outputs

def validation_epoch_end(self, outputs):
    return None

def configure_optimizers(self):
    return torch.optim.Adam(self.parameters(), lr=0.01)

if __name__ == ' __main__':

model = MuonModel()

trainer = pl.Trainer(
                    gpus=1,
                    max_epochs=100,
                    min_epochs=50)

```

As you can see I’m embedding the quantum circuit into a TorchLayer and then I’m using PyTorch Lightning to perform the training. Hope this helps in clarifying the issue, anyway thank you for your help 🙂

---

<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:** [December 20, 2021, 2:34pm UTC](https://discuss.pennylane.ai/t/gpu-usage-for-pennylane-qiskit/1567/4 "2021-12-20T14:34:07Z")

</div>

Thanks for the update @Davide_Zuliani  
I had trouble running your example code as given. Would it be possible to simplify it further to a minimum working example? That way we can help try to understand if something is going wrong in the pipeline on our side, and hopefully see if we can get it going.

---

<div class="post-metadata">

**Author:** ![jackaraz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/jackaraz/32/852_2.png) [@jackaraz](https://discuss.pennylane.ai/u/jackaraz)\
**Post date:** [December 26, 2021, 3:35pm UTC](https://discuss.pennylane.ai/t/gpu-usage-for-pennylane-qiskit/1567/5 "2021-12-26T15:35:10Z")

</div>

Hi @mlxd I have a similar problem even after setting GPU for the device and installing the GPU support for qiskit `pip install qiskit-aer-gpu` I do not see any change in GPU usage but I do see a single CPU core running at 100%. Also, pennylane does not raise any error when I request GPU run without any GPU or when I don’t have `qiskit-aer-gpu` installed. Is there another plugin in pennylane that I should install for GPU support?

## System settings:

- PennyLane-qiskit v0.20.0
- qiskit v0.34.0
- qiskit-aer v0.10.1
- qiskit-aer-gpu v0.10.1
- PennyLane v0.20.0

### Correction: pennylane does give me an error if I don’t have a GPU when I execute the circuit.

Thanks

---

<div class="post-metadata">

**Author:** ![Romain\_Moyard](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/romain_moyard/32/567_2.png) [@Romain\_Moyard](https://discuss.pennylane.ai/u/Romain_Moyard)\
**Post date:** [January 4, 2022, 8:01pm UTC](https://discuss.pennylane.ai/t/gpu-usage-for-pennylane-qiskit/1567/6 "2022-01-04T20:01:14Z")

</div>

@jackaraz Thank you for your question, by using `pip install qiskit-aer-gpu`, you should overwrite the installation of `qiskit-aer`. There is more information about the package [here](https://pypi.org/project/qiskit-aer-gpu/). Maybe you could try to create a new environment and install it without the `qiskit_aer`. Could share a minimal example of what you are trying to run such I can potentially help you!
