Using GPU with StrawberryFields dev

Hi,

I have some code involving CV model and qml.CVNeuralNetLayers. I am using strawberryfields.fock dev to simulate my circuits. However, I would like to use a GPU for improved performance. The problem is that lightning.gpu dev does not support CV operations as it is not a CV model device. Is there any alternative that I could use with the CV model and GPU? I have seen tutorials using StrawberryFields and TensorFlow that may work, such as
https://strawberryfields.ai/photonics/demos/run_tutorial_machine_learning.html#machine-learning-tutorial

However, that would imply modifying the whole code, so simpler solutions are appreciated. Thanks in advance!

Hey @Pablo_Vinas! Yep, I do recommend introducing another library to integrate with your pennylane-sf code, be it Tensorflow, PyTorch, etc. Personally, I prefer PyTorch. Here’s an example of using the strawberryfields.fock device with the PyTorch interface:

import pennylane as qml
import torch

dev = qml.device("strawberryfields.fock", wires=3, cutoff_dim=3)

@qml.qnode(dev, interface="torch")
def quantum_function(x, theta):
    qml.Displacement(x, 0, wires=0)
    qml.Beamsplitter(theta, 0, wires=[0, 1])
    return qml.expval(qml.NumberOperator(0))

# To use a GPU here, change 'cpu' to 'cuda'
x = torch.tensor(0.1, device='cpu', requires_grad=True)
theta = torch.tensor(0.1, device='cpu', requires_grad=True)

opt = torch.optim.Adam([x, theta], lr=0.1)

for _ in range(10):
    opt.zero_grad()
    loss = quantum_function(x, theta)
    loss.backward()
    opt.step()

    print(quantum_function(x, theta))

'''
tensor(2.1328e-16, dtype=torch.float64, grad_fn=<SqueezeBackward0>)
tensor(0.0042, dtype=torch.float64, grad_fn=<SqueezeBackward0>)
tensor(0.0052, dtype=torch.float64, grad_fn=<SqueezeBackward0>)
tensor(0.0024, dtype=torch.float64, grad_fn=<SqueezeBackward0>)
tensor(0.0003, dtype=torch.float64, grad_fn=<SqueezeBackward0>)
tensor(0.0002, dtype=torch.float64, grad_fn=<SqueezeBackward0>)
tensor(0.0011, dtype=torch.float64, grad_fn=<SqueezeBackward0>)
tensor(0.0017, dtype=torch.float64, grad_fn=<SqueezeBackward0>)
tensor(0.0016, dtype=torch.float64, grad_fn=<SqueezeBackward0>)
tensor(0.0010, dtype=torch.float64, grad_fn=<SqueezeBackward0>)
'''

Hope this helps!

1 Like

Hey @isaacdevlugt. Thank you for the answer! It apparently worked. Still, I am not getting much improvement so I might be missing something. I will try to find out what is going on!

Hey @Pablo_Vinas, it might also be worth your time to use Strawberryfields directly, not the PennyLane plugin, if you need CV operations. We’re not planning on supporting the PL-SF plugin for much longer. It’s up to you! But, if your code requires a lot of work to get this working, it might be worth it to work in SF :smile:

Thanks @isaacdevlugt, I will follow your advice. Indeed, it seems more practical to use SF directly. Cheers!

1 Like

Glad I could help! Let me know if you have any questions about strawberryfields :slight_smile:

1 Like