It is possible to use Pytorch checkpoint files (.ckpt) with pennylane?

I’m wondering if it is possible to use models checkpoints with the PyTorch interface within pennylane and the Lightning-GPU device.

Hi @PCesteban,

I don’t think we natively support this.

Could you please share a code example of what you would like to do?

If we can get a better sense of what you would like to do we can see if there is a potential option to use qml.BasisState or qml.QubitStateVector to enable the copy from CPU to GPU of your data.

Thank you for this question!

Hi @CatalinaAlbornoz,

Yes, so for example, with Pytorch I could load a checkpoint of a model in order to evaluate its performance with new data like this:

import torch

class Net(torch.nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.fc1 = torch.nn.Linear(784, 64)
        self.fc2 = torch.nn.Linear(64, 10)

    def forward(self, x):
        x = torch.relu(self.fc1(x))
        x = self.fc2(x)
        return x

# Load the saved model from the checkpoint file
model = Net()
checkpoint = torch.load('path/to/checkpoint')
model.load_state_dict(checkpoint['model_state_dict'])

# Evaluate the model on some evaluation data
criterion = torch.nn.CrossEntropyLoss()
with torch.no_grad():
    for inputs, labels in eval_loader:
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        _, predicted = torch.max(outputs.data, 1)
        accuracy = (predicted == labels).sum().item() / labels.size(0)

print('Loss:', loss.item())
print('Accuracy:', accuracy)

Let’s say I have a checkpoint of a model and I want to evaluate its performance on a quantum simulator (or hardware architecture) by adding a quantum subroutine to the model, just to give an example. So my question is if this is possible with pennylane using its various interface for ML frameworks like PyTorch. The Lightning-GPU device part would be a plus.

Hi @PCesteban,

In principle I think this could work but I guess it depends on exactly how you code the quantum part. I recommend that you actually start the other way around, with a hybrid model that works and then seeing where you can (or cannot) add a checkpoint.

A good place to start is this demo on turning qnodes into Torch layers.

Please let me know if you have any questions about the demo or if you run into any issues or successes when adding checkpoints.

I look forward to hearing back from you!

Thank you @CatalinaAlbornoz, I will get back to you soon.