How can I deploy a PennyLane–PyTorch hybrid (U-Net + QNode) inside a C++ framework when TorchScript tracing is incompatible?

I am a student working on event reconstruction software in a high-energy physics experiment.
The framework we use is C+±based, called Wire-Cell Toolkit.

So far, our development team has been using a U-Net model for semantic segmentation in signal processing.
The classical U-Net (PyTorch) can be exported via torch.jit.trace() into TorchScript and then successfully loaded in C++.

Now, I am experimenting with a hybrid quantum–classical model (U-Net + PennyLane QNode) as a prototype within the same C++ framework.
However, when I try to export the hybrid model using the same approach (torch.jit.trace()), the process fails.

The error arises because PennyLane’s QNode depends on qml.math.is_abstract, which is not TorchScript compatible.

So my question is: What are the possible strategies or ideas for deploying a PennyLane–PyTorch hybrid model inside a C++ framework, given that TorchScript export is not compatible with PennyLane QNodes?

Hi @yujin000408 , welcome to the Forum!

Since PennyLane is all built on Python and it wasn’t designed to work with TorchScript I don’t think there’s any obvious answer. You can use PennyLane together with PyTorch at the Python level though. If you want to use something with C++ maybe you can try looking into the PennyLane Lightning simulators. They’re built on C++ and they’re compatible with JAX.jit. I hope this can help you as guidance on what to look into.

1 Like

Thanks for your advise!

1 Like