Segmentation fault / kernel crash when using SPSAOptimizer with simple QNode

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!

Update: I noticed that sometimes instead of Segmentation fault (core dumped) I also get Illegal instruction (core dumped).

Hi @Dominik_Freinberger , welcome to the Forum!

Unfortunately segmentation faults are due to a memory issue, so it shouldn’t be related to SPSA. In fact I just ran your code with no issues.

Could you please try the following?

Use your system Python, rather than conda, and create a fresh environment with

python3 -m venv pypenv 
source ./pyenv/bin/activate 
python -m pip install pennylane

Please let us know if this resolves your issue!

Dear Catalina,

thank you for the quick reply!

I have tried creating a new environment with fresh Pennylane install multiple times, also different versions of it. Actually, I am not using Conda, just plain venv from Python.

I noticed the error is somehow related to default.qubit because with lightning.qubit I do not observe it.

Also on Google Colab the code runs just fine, so I assume it really is an issue with my local OS + Python + packages setup. Unfortunately I have tried 3 different Ubuntu versions in WSL (20.04, 22.04 and 24.04) with accordingly different versions of Python (3.9, 3.11 and 3.12) which did not resolve the problem.

Kind regards,
Dominik

Hi @Dominik_Freinberger ,

I’m sorry to hear that. It’s particularly strange that you only observe this behaviour with default.qubit, I hadn’t heard of this happening before.

I’ll check with our team here to see if anyone knows what the issue could be.

1 Like

Hi @Dominik_Freinberger,

My colleague Ali made some tests and ran into some Illegal instruction errors due to the incompatibility of sklearn with his AMD CPU on his laptop. So it looks like there may be some compatibility issues between some CPUs (or WSL setups) and NumPy instructions used in PennyLane’s default.qubit and sklearn.

Is it enough for you to use Google Colab or qBraid?
This looks to be a deep and complicated issue so I don’t know if we can resolve it or when.

Dear @CatalinaAlbornoz,
thank you very much for the effort and checking with you colleague. Yes, I already assumed that was a very low level issue as the code runs fine on Colab and a friends PC. So I am fine with closing this issue.

Thank you very much.

1 Like