Hello! If applicable, put your complete code example down below. Make sure that your code:
- is 100% self-contained — someone can copy-paste exactly what is here and run it to
reproduce the behaviour you are observing - includes comments
I want to run a hybrid quantum CNN model. However if I use the parameter-shift method @qml.qnode(dev, diff_method=“parameter-shift”), an error returns, that is ------
‘NotImplementedError: Computing the gradient of broadcasted tapes with respect to the broadcasted parameters using the parameter-shift rule gradient transform is currently not supported. See #4462 for details.’ Is there any method to fix the error? Thank you!
# Put code here
Define the quantum circuit using PennyLane
n_qubits = 5
dev = qml.device(“default.qubit”, wires=n_qubits)
@qml.qnode(dev, diff_method=“parameter-shift”)
def qnode(inputs, weights):
qml.AngleEmbedding(inputs, wires=range(n_qubits))
qml.BasicEntanglerLayers(weights, wires=range(n_qubits))
return [qml.expval(qml.PauliZ(wires=i)) for i in range(n_qubits)]
Define the QLayer
n_layers = 1
weight_shapes = {“weights”: (n_layers, n_qubits)}
Define a simple CNN architecture
class Net(nn.Module):
def init(self):
super(Net, self).init()
# Convolutional layer 1 with 1 input channels (for greyscale images), 16 output channels, and 5x5 kernel
self.conv1 = nn.Conv2d(1, 16, 5, stride=1, padding=2)
# Batch normalization after convolutional layer 1
self.bn1 = nn.BatchNorm2d(16)
# Max pooling layer with a 2x2 window
self.pool = nn.MaxPool2d(2, 2)
# Convolutional layer 2 with 16 input channels (from the previous layer), 32 output channels, and 5x5 kernel
self.conv2 = nn.Conv2d(16, 32, 5, stride=1, padding=2)
# Batch normalization after convolutional layer 2
self.bn2 = nn.BatchNorm2d(32)
# Quantum layer
self.qlayer1 = qml.qnn.TorchLayer(qnode, weight_shapes)
self.qlayer2 = qml.qnn.TorchLayer(qnode, weight_shapes)
self.qlayer3 = qml.qnn.TorchLayer(qnode, weight_shapes)
self.qlayer4 = qml.qnn.TorchLayer(qnode, weight_shapes)
# Fully connected layers
self.fc1 = nn.Linear(32 * 7 * 7, 120)
self.fc2 = nn.Linear(120, 20)
self.fc3 = nn.Linear(20, 10)
def forward(self, x):
# Propagate the input through the CNN layers
x = self.pool(F.relu(self.bn1(self.conv1(x))))
x = self.pool(F.relu(self.bn2(self.conv2(x))))
# Flatten the output from the convolutional layers
x = x.view(-1, 32 * 7 * 7)
# Pass the output to the quantum layer
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x_1, x_2, x_3, x_4 = torch.split(x, 5, dim=1)
x_1 = self.qlayer1(x_1)
x_2 = self.qlayer2(x_2)
x_3 = self.qlayer3(x_3)
x_4 = self.qlayer4(x_4)
x = torch.cat([x_1, x_2, x_3, x_4], axis=1)
x = self.fc3(x)
return x
device = torch.device(“cuda” if torch.cuda.is_available() else “cpu”)
model = Net().to(device)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=0.01)
from torchvision.transforms import ToTensor
from torch.utils.data import DataLoader, Subset
num_train_images = 1000
num_test_images = 200
Download and load the MNIST dataset
train_data = datasets.MNIST(
root=‘data’,
train=True,
transform=ToTensor(),
download=True,
)
test_data = datasets.MNIST(
root=‘data’,
train=False,
transform=ToTensor()
)
Create subsets of the data
train_subset = Subset(train_data, range(num_train_images))
test_subset = Subset(test_data, range(num_test_images))
Create data loaders for the subsets
train_loader = DataLoader(train_subset, batch_size=32, shuffle=True)
test_loader = DataLoader(test_subset, batch_size=32, shuffle=False)
Example: Print the number of images in the subsets
print(f’Number of training images: {len(train_loader.dataset)}‘)
print(f’Number of test images: {len(test_loader.dataset)}’)
dataset = train_data
Initialize your CNN model
cnn = Net()
Define loss function and optimizer
criterion = nn.CrossEntropyLoss() # Cross-entropy loss for classification
optimizer = torch.optim.SGD(cnn.parameters(), lr=0.001, momentum=0.9) # Stochastic Gradient Descent optimizer
Split your data into training and validation sets
train_size = int(0.8 * len(dataset))
train_set, val_set = torch.utils.data.random_split(dataset, [train_size, len(dataset) - train_size])
train_loader = torch.utils.data.DataLoader(train_set, batch_size=4, shuffle=True)
#val_loader = torch.utils.data.DataLoader(val_set, batch_size=4, shuffle=False)
Training loop
num_epochs = 10
for epoch in range(num_epochs):
ct = datetime.datetime.now()
print(f"{epoch=}, {ct}")
running_loss = 0.0
for i, data in enumerate(train_loader, 0):
inputs, labels = data
optimizer.zero_grad() # Zero the parameter gradients to avoid accumulation
outputs = cnn(inputs) # Forward pass
loss = criterion(outputs, labels) # Compute the loss
loss.backward() # Backpropagation
optimizer.step() # Update the model parameters
print(‘Finished Training’)
If you want help with diagnosing an error, please put the full error message below:
# Put full error message here
And, finally, make sure to include the versions of your packages. Specifically, show us the output of qml.about()
.
Name: PennyLane
Version: 0.37.0
Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Home-page: GitHub - PennyLaneAI/pennylane: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Author:
Author-email:
License: Apache License 2.0
Location: d:\anaconda3\lib\site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane_Lightning
Platform info: Windows-10-10.0.22631-SP0
Python version: 3.10.9
Numpy version: 1.26.4
Scipy version: 1.10.0
Installed devices:
- default.clifford (PennyLane-0.37.0)
- default.gaussian (PennyLane-0.37.0)
- default.mixed (PennyLane-0.37.0)
- default.qubit (PennyLane-0.37.0)
- default.qubit.autograd (PennyLane-0.37.0)
- default.qubit.jax (PennyLane-0.37.0)
- default.qubit.legacy (PennyLane-0.37.0)
- default.qubit.tf (PennyLane-0.37.0)
- default.qubit.torch (PennyLane-0.37.0)
- default.qutrit (PennyLane-0.37.0)
- default.qutrit.mixed (PennyLane-0.37.0)
- default.tensor (PennyLane-0.37.0)
- null.qubit (PennyLane-0.37.0)
- lightning.qubit (PennyLane_Lightning-0.37.0)