Qiskit-Torch-Module: Fast Prototyping of Quantum Neural Networks

Nico Meyer, Christian Ufrecht, Maniraman Periyasamy, Axel Plinge, Christopher Mutschler, Daniel D. Scherer, Andreas K. Maier

TL;DR

The qiskit-torch-module (qtm) is a new open-source framework that accelerates the training of Quantum Neural Networks (QNNs) by up to two orders of magnitude compared to standard Qiskit libraries. By optimizing how observables are measured and parallelizing batch processing on single-machine CPUs, it turns training tasks that previously took hours into minutes, all while maintaining a seamless integration with PyTorch.

What Problem Does This Paper Address?

Variational Quantum Algorithms (VQAs) are the workhorses of the current "Noisy Intermediate-Scale Quantum" (NISQ) era. They function much like classical neural networks: you have a quantum circuit with tunable parameters, and you use classical optimization (like gradient descent) to find the best settings for a specific task, such as chemistry simulations or machine learning.

However, simulating these quantum circuits on classical hardware is computationally expensive. The standard tool, qiskit-machine-learning (qml), often suffers from significant runtime overhead, especially when calculating gradients for many parameters or processing large batches of data. For researchers working on standard desktop machines without access to massive HPC clusters, this creates a bottleneck that stifles rapid prototyping. The authors set out to build a "drop-in" alternative that eliminates this overhead without requiring researchers to learn an entirely new programming syntax.

Methods

The authors implemented three primary optimizations in qtm:

  1. Efficient Multiple Observable Evaluation: In standard simulations, if you want to measure ten different properties (observables) of a quantum state, the software often re-simulates the state evolution ten times. qtm evolves the state once and then performs post-processing to extract all expectation values, leading to a speedup roughly equal to the number of observables.
  2. Batch Parallelization: Machine learning relies on "batches" of data. qtm distributes these independent data points across multiple CPU threads. Unlike existing Qiskit tools that try to parallelize the internal math of a single circuit, qtm parallelizes at the data level, which is far more efficient for system sizes up to ~14 qubits.
  3. Advanced Adjoint Gradients: The framework extends the "adjoint method"—a clever way to compute gradients by running the circuit in reverse—to handle multiple observables simultaneously.
  4. Seamless PyTorch Integration: qtm provides a HybridModule that allows users to easily sandwich quantum layers between classical PyTorch layers. It also fixes common pitfalls in Qiskit regarding how parameters are assigned and initialized.

Datasets

The researchers evaluated qtm using two classic benchmarks adapted for the quantum realm:

  • MNIST: The standard dataset of handwritten digits (0-9). They used a 10-qubit Variational Quantum Circuit (VQC) with 220 trainable parameters.
  • CartPole-v1: A reinforcement learning environment from OpenAI Gym where an agent must balance a pole on a cart. This was used to test a Quantum Policy Gradient (QPG) approach on 4 qubits.

Key Insights and Ideas

  • The "One-Pass" Advantage: The most significant "aha" moment is realizing that for classical simulation, the restriction of quantum hardware (only being able to measure jointly measurable observables) doesn't apply. By exploiting this, qtm achieves massive gains simply by not repeating redundant state evolutions.
  • Hyperparameter Flexibility: Unlike the standard qml library, qtm allows for fine-grained control over learning rates for different sets of quantum parameters (e.g., input scaling vs. variational weights). In the CartPole experiment, this flexibility led to much faster and smoother convergence.
  • Single-Machine Focus: While many frameworks aim for massive scale, qtm acknowledges that most daily research happens on local CPUs. By optimizing for this specific environment, they provide the most value to the average researcher.

Key Results

The performance gains are dramatic across the board:

  • MNIST Training: The end-to-end training time for the quantum classifier dropped from 14 hours using qml-rev to just 14 minutes with qtm—a 60-fold improvement.
  • Scaling: As shown in Figure 1, qtm maintains a consistent 1-2 order of magnitude lead over other Qiskit-based methods as the number of qubits increases.
Fig. 1: Benchmarking results of the proposed qtm module, compared to the available implementations in qml. All experiments are averaged over 10 independent runs with standard deviations depicted in pale colors. The runtimes refer to a batch size of B = 48, with single-qubit Pauli-Z observables on all qubits, and a circuit depth of d = 3. The number of trainable parameters scales linearly in the number of qubits. (a) Depicts the combined times for one pass of computing expectation values and gradients on a logarithmic scale; (b) depicts the times for the forward pass, i.e. computing expectation values, with a cut-off at 5 seconds; (c) depicts the times for the backward pass, i.e. computing gradients, with a cut-off at 100 seconds.
  • Circuit Depth: The runtime scales linearly with circuit depth, but qtm stays significantly faster than the competition, making it feasible to train more expressive (deeper) models.
Fig. 2: Benchmarking results of the proposed qtm module, compared to qml-rev. The values depict gradient computation times for a batch size of B = 48, with single-qubit Pauli-Z observables on all 12 qubits. The num- ber of trainable parameters scales linearly in the depth. All experiments are averaged over 10 independent runs with standard deviations depicted in pale colors. The times for qml-ps are too long to appear in the plot, qml-spsa was intentionally omitted since it only calculates approximate gradients.
  • Cross-Platform: While Linux offers the best performance (up to 300x speedup due to better multi-threading), significant improvements were also measured on Windows and macOS.

Limitations and Future Work

The authors note a few key limitations:

  • OS Overhead: On Windows and macOS, the efficiency of batch parallelization is hampered by the overhead of Python's multiprocessing library.
  • Simulation vs. Hardware: qtm is explicitly designed for classical simulation. While some techniques (like the parameter-shift rule) can be used on real quantum hardware, the core speedups come from bypassing hardware-specific measurement constraints.
  • Qubit Scaling: For systems larger than 14 qubits, the exponential cost of simulating the state vector begins to dominate, eventually hitting the limits of single-machine memory and compute regardless of the framework used.

Future work could involve integrating more advanced optimization techniques, such as Quantum Natural Gradients, directly into the modular structure of qtm.

Summary updated

Abstract

Quantum computer simulation software is an integral tool for the research efforts in the quantum computing community. An important aspect is the efficiency of respective frameworks, especially for training variational quantum algorithms. Focusing on the widely used Qiskit software environment, we develop the qiskit-torch-module. It improves runtime performance by two orders of magnitude over comparable libraries, while facilitating low-overhead integration with existing codebases. Moreover, the framework provides advanced tools for integrating quantum neural networks with PyTorch. The pipeline is tailored for single-machine compute systems, which constitute a widely employed setup in day-to-day research efforts.
BibTeX
@inproceedings{meyer2024qiskit,
  author = {Nico Meyer and Christian Ufrecht and Maniraman Periyasamy and Axel Plinge and Christopher Mutschler and Daniel D. Scherer and Andreas K. Maier},
  title = {{Qiskit-Torch-Module: Fast Prototyping of Quantum Neural Networks}},
  booktitle = {QCE},
  year = {2024},
  url = {https://openreview.net/forum?id=OBOFLSMnJz},
}
Figure