QuantumFunctionError: Invalid device

Hello!

I am trying to use a PennyLane device as follows:

Step 1: Define the Quantum Device

I start by defining my device:

# Define the quantum device
n_qubits = 5
dev = qml.device("lightning.qubit", wires=n_qubits)
print(dev)

Step 2: Create the Quantum Model Class

Next, I create a model class that accepts the device as a parameter:

class QuantumCell(nn.Module):
    def __init__(self, quantum_circuit, input_size, hidden_size, n_qubits, n_qlayers, device):
        super(QuantumCell, self).__init__()
        self.device = device
        # Additional initialization code...

Following this, I define a quantum model:

class QM(nn.Module):
    def __init__(self, quantum_circuit, input_size, hidden_size, n_qubits, n_qlayers, device, batch_first=True):
        super(QM, self).__init__()
        self.cell = QuantumCell(quantum_circuit, input_size, hidden_size, n_qubits, n_qlayers, device)
        # Additional initialization code...

Step 3: Instantiate the Model

Finally, I instantiate the quantum model:

# Create a Q model
model = QM(Basic_VQC_RZ, input_size, embedding_size, hidden_size, n_qubits, n_qlayers, dev)

Issue Encountered

However, when creating the model instance, I encounter the following error:

__init__(self, func, device, interface, diff_method, grad_on_execution, cache, cachesize, max_diff, device_vjp, postselect_mode, mcm_method, **gradient_kwargs)
    537 
    538         if not isinstance(device, (qml.devices.LegacyDevice, qml.devices.Device)):
--> 539             raise qml.QuantumFunctionError(
    540                 "Invalid device. Device must be a valid PennyLane device."
    541             )

QuantumFunctionError: Invalid device. Device must be a valid PennyLane device.

This error appears each time I create an instance of my model, even when using "lightning.qubit" or "default.qubit" devices.


Would you like help troubleshooting this issue or advice on debugging?

Hi @yousrabou ,

When you’re instantiating the quantum model you’re adding an “embedding_size” argument that doesn’t actually exist in QM. So all of your arguments are getting shifted and the device is getting stored in the wrong one. If you instantiate the model as below (removing embedding_size) it should work.

model = QM(Basic_VQC_RZ, input_size, hidden_size, n_qubits, n_qlayers, dev)

If you have any additional issues please make sure to include a minimal reproducible example that we can copy-paste directly to try to replicate your issue.

I hope this helps!