Colab using qml.interfaces.torch.to_torch show Nonetype Error

Hello everyone, I tried to do some coding on Colab. It seems that Colab have problem using pytorch interface. Here is my problem:


import os
import pennylane as qml
import numpy as np
import torch
from torch.autograd import Variable
import torch.optim as optim

dev = qml.device("default.qubit", wires=num_qubits)
qnode = qml.QNode(circuit, dev)
torch_qnode = qnode.to_torch()


Output On Colab

<class ‘pennylane.devices.default_qubit.DefaultQubit’>
<class ‘pennylane.tape.qnode.QNode’>
<class ‘NoneType’>

Output On Local

<class ‘pennylane.devices.default_qubit.DefaultQubit’>
<class ‘pennylane.interfaces.autograd.to_autograd..AutogradQNode’>
<class ‘pennylane.interfaces.torch.to_torch..TorchQNode’>

So, when I tried to call qnode function, it show up this error:
TypeError: ‘NoneType’ object is not callable

Did I miss pip install anything or add other things?

Hi @Yian_Chen,

Thank you so much for the question! :slight_smile:

This will probably be caused by the fact that Colab pulled in the new release of PennyLane which was just released today!

The latest release includes a completely new core for PennyLane and the QNode.to_torch method is now in-place (it doesn’t return a new TorchQNode object, but rather transforms the existing QNode).

After an update locally, could you check if the following definition of the QNode works fine?

qnode = qml.QNode(circuit, dev, interface='torch')
1 Like

Thank you very much, @antalszava !

It helps a lot!

Thanks for your replying !!!