Hello I am working on a quantum-classical model which has got the following structure : [Classical] → [QVC] . Following is the minimal working code that is representative of my actual model.
import math
import pennylane as qml
import torch
import torch.nn as nn
import numpy as np
dev = qml.device("default.qubit", wires=1)
@qml.qnode(dev, interface = 'torch')
def simple_qubit_circuit(inputs, theta):
qml.RX(inputs, wires=0)
qml.RY(theta, wires=0)
return qml.expval(qml.PauliZ(0))
class QNet(nn.Module):
def __init__(self):
super().__init__()
quantum_weights = np.random.normal(0, np.pi)
self.quantum_weights = nn.parameter.Parameter(torch.tensor(quantum_weights,\
dtype=torch.float32,requires_grad=True))
shapes = {
"theta": 1
}
self.q = qml.qnn.TorchLayer(simple_qubit_circuit, shapes)
def forward(self, input_value):
return self.q(input_value)
class Q_PPO1(nn.Module):
def __init__(self):
super().__init__()
self.shared_network = nn.Linear(5, 1)
self.value_network = QNet()
def forward(self, obs):
features = self.shared_network(obs).squeeze(1).to(torch.float64)
value = self.value_network(features)
return value
PPO_model = Q_PPO1()
x_train = torch.rand(10, 5) # Batch of 10 input vectors of dimension 5
y_train = torch.rand(10, 1)
y_out = PPO_model(x_train)
loss = (y_out - y_train).mean()
loss.backward()
loss.backward() returns the following error message.
RuntimeError: Function ExecuteTapesBackward returned an invalid gradient at index 0 - got [] but expected shape compatible with [1]
Note that in the forward() method I had to convert features vector to torch64 datatype. If I don’t do this, I get the following error in forward propagation.
ValueError: probabilities do not sum to 1
I am not sure what’s wrong here. Following are the details of qml.info()
Name: PennyLane
Version: 0.31.1
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/PennyLaneAI/pennylane
Author:
Author-email:
License: Apache License 2.0
Location: c:\users\aksi01\appdata\roaming\python\python38\site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane-Lightning, PennyLane-qiskit
Platform info: Windows-10-10.0.19045-SP0
Python version: 3.8.15
Numpy version: 1.23.5
Scipy version: 1.10.1
Installed devices:
- default.gaussian (PennyLane-0.31.1)
- default.mixed (PennyLane-0.31.1)
- default.qubit (PennyLane-0.31.1)
- default.qubit.autograd (PennyLane-0.31.1)
- default.qubit.jax (PennyLane-0.31.1)
- default.qubit.tf (PennyLane-0.31.1)
- default.qubit.torch (PennyLane-0.31.1)
- default.qutrit (PennyLane-0.31.1)
- null.qubit (PennyLane-0.31.1)
- lightning.qubit (PennyLane-Lightning-0.31.0)
- qiskit.aer (PennyLane-qiskit-0.29.0)
- qiskit.basicaer (PennyLane-qiskit-0.29.0)
- qiskit.ibmq (PennyLane-qiskit-0.29.0)
- qiskit.ibmq.circuit_runner (PennyLane-qiskit-0.29.0)
- qiskit.ibmq.sampler (PennyLane-qiskit-0.29.0)