Hello PennyLane community!
I’m running into an unexpected kernel crash when trying to train a simple variational circuit with SPSAOptimizer
. I get segmentation fault (core dumped)
when running as a standalone script. When run as Jupyter Notebook, the kernel dies. Sometimes a few training loops are executed, but this is non-deterministic.
Below is the script that reproduces the issue (you can copy-paste and run it directly):
import numpy as np
import pennylane as qml
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
# Create dataset
X, y = make_classification(n_samples=100, n_features=4, n_informative=2, n_redundant=0, random_state=42, class_sep=0.7)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
n_qubits = 4
n_layers = 5
dev = qml.device("default.qubit", wires=n_qubits)
all_pauliz_tensor_prod = qml.prod(*[qml.PauliZ(i) for i in range(n_qubits)])
# Define Qnode
@qml.qnode(dev)
def qnode(inputs, params):
for r in range(n_layers):
qml.AngleEmbedding(inputs, wires=range(n_qubits))
qml.StronglyEntanglingLayers(params[r : r+1], wires=range(n_qubits))
return qml.expval(all_pauliz_tensor_prod)
weight_shapes = (n_layers, n_qubits, 3)
# Initialize parameters
params = np.random.uniform(-0.1*np.pi, 0.1*np.pi, size=weight_shapes)
# Define cost function
def cost(params):
preds = [qnode(x, params) for x in X_train]
preds = (np.array(preds) + 1) / 2 # map to [0,1]
return np.mean((preds - y_train) ** 2) # NumPy scalar
# Train the model using SPSA optimizer
opt = qml.SPSAOptimizer(maxiter=100)
for i in range(100):
params, curr_cost = opt.step_and_cost(cost, params)
if (i + 1) % 10 == 0:
print(f"Step {i+1}: cost = {curr_cost:.4f}")
As said in the beginning, when run as Python script, I get the following error (either immediately or after couple of iterations of the training loop at the end):
Segmentation fault (core dumped)
This is the output of qml.about()
:
Name: PennyLane
Version: 0.40.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: https://github.com/PennyLaneAI/pennylane
Author:
Author-email:
License: Apache License 2.0
Location: /home/domi/projects/.venvs/qml/lib/python3.12/site-packages
Requires: appdirs, autograd, autoray, cachetools, diastatic-malt, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, tomlkit, typing-extensions
Required-by: PennyLane_Lightning
Platform info: Linux-5.15.167.4-microsoft-standard-WSL2-x86_64-with-glibc2.39
Python version: 3.12.3
Numpy version: 1.26.4
Scipy version: 1.15.3
Installed devices:
- lightning.qubit (PennyLane_Lightning-0.40.0)
- default.clifford (PennyLane-0.40.0)
- default.gaussian (PennyLane-0.40.0)
- default.mixed (PennyLane-0.40.0)
- default.qubit (PennyLane-0.40.0)
- default.qutrit (PennyLane-0.40.0)
- default.qutrit.mixed (PennyLane-0.40.0)
- default.tensor (PennyLane-0.40.0)
- null.qubit (PennyLane-0.40.0)
- reference.qubit (PennyLane-0.40.0)
Other packages:
scikit-learn==1.7.0
I am running Ubuntu-24.04
in Windows 11 WSL 2.
Has anyone seen this behavior with SPSA or a minimal QNode? Any ideas on what might be causing a low-level crash in PennyLane (or how to debug further) would be greatly appreciated.
Thank you!