Skip to main content

🌀 torch-quantum

Quantum-native deep-learning layers that slot directly into PyTorch and run on NVIDIA CUDA-Q simulators or real QPUs.

PyPI Python License


Why use torch-quantum?

Current quantum SDKs expose powerful primitives but leave many “deep-learning conveniences” to the user. Torch-Q fills that gap by giving PyTorch practitioners a familiar module interface, autograd-compatible parameter-shift gradients and ready-made feature-map / ansatz building blocks. You build models exactly as you do with classic layers, choose a CUDA-Q target (CPU, GPU or cloud QPU) and start iterating.

Classical DL torch-quantum Notes
nn.Linear quantum.QuantumLayer drop-in layer that outputs class probabilities
GPU accel cuStateVec / cuTensorNet automatic, single or multi-GPU
Autograd parameter-shift implemented in pure PyTorch works with all optimizers

Features

  • Drop-in PyTorch modules (QuantumLayer, QNN, HybridQNN) that behave like any other nn.Module and are compatible with torch.optim.
  • Flexible circuit construction: pick standard feature maps (Z, ZZ) or supply custom kernels; choose from RealAmplitudes, EfficientSU2, PauliTwoDesign ansätze or roll your own.
  • Hybrid workflow: seamlessly chain classical layers before or after a quantum block, making it easy to build quantum-enhanced CNNs, MLPs or Transformers.
  • Multiple back-ends: one line (cudaq.set_target(...)) switches from local CPU simulation to GPU acceleration or a cloud device (IonQ, Quantinuum, OQC, Infleqtion, Pasqal, QuEra…).
  • Exact gradients via the parameter-shift rule, automatically invoked by loss.backward(); no manual circuit plumbing required.
  • Research-friendly utilities: empirical Fisher information, layer-wise kernel drawers, circuit depth counters and parameter initialisation helpers.

Installation

pip install git+https://github.com/SeroviICAI/cuda-quantum.git
pip install torch-qu

Quick example (4-qubit classifier)

import torch, torch.nn as nn, torch.optim as optim
from torch_quantum.nn import QNN
import cudaq

# Use fast GPU simulator if available
cudaq.set_target("nvidia", option="fp32")

model = QNN(
    in_features = 4,        # qubits / input dimension
    out_features = 3,       # number of classes
    num_layers = 2,         # ansatz depth
    shots = 1024,           # measurement shots per forward pass
    feature_map = "zz",     # entangling data embedding
    var_form = "efficientSU2",
    reupload = False
)

x = torch.randn(16, 4)              # mini-batch
y = torch.randint(0, 3, (16,))      # labels
opt = optim.Adam(model.parameters(), lr=0.02)
criterion = nn.CrossEntropyLoss()

for step in range(50):
    opt.zero_grad()
    logits = model(x)               # parameter-shift handled automatically
    loss = criterion(logits, y)
    loss.backward()
    opt.step()
print("final loss:", loss.item())

Change back-end to a real device with one line:

# Add CREDENTIALS here
cudaq.set_target("ionq", qpu="qpu.aria-1")  # 25-qubit trapped-ion hardware

All circuits are now queued to the cloud without further code changes.

Documentation & book

The open-access book “Toward a Quantum Advantage in Deep Learning Architectures” lives in docs/ and is rendered online at https://SeroviICAI.github.io/torch-quantum/book. It introduces quantum mechanics, CUDA-Q programming, variational circuits and information-geometric capacity in detail and Torch-Q code examples.

Citation

@software{torchquantum2025,
  author  = {Sergio Rodríguez Vidal},
  title   = {torch-quantum: Quantum-ready layers for PyTorch},
  year    = {2025},
  url     = {https://github.com/SeroviICAI/torch-quantum},
  license = {Apache-2.0}
}

License

Apache 2.0 — free to use, modify and distribute, with permissive terms.

Happy hacking — and welcome to quantum-enhanced deep learning!

Metadata

Release files for torch-qu 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for torch-qu 0.1.4
File Size Uploaded Provenance Actions
torch_qu-0.1.4.tar.gz 27.7 kB Without provenance Details for torch_qu-0.1.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for torch-qu 0.1.4
File Interpreter ABI Platform Actions
torch_qu-0.1.4-py3-none-any.whl Python 3 none any Details for torch_qu-0.1.4-py3-none-any.whl

Total release size: 62.3 kB

Release files / torch_qu-0.1.4.tar.gz

Download URL torch_qu-0.1.4.tar.gz
Size 27.7 kB
Tags Source
SHA-256 checksum
How to use checksums
9e3cf0c018dbd311870272231b4dc3c8db7047349c5bd03bce70d1aa96b3051f
BLAKE2b-256 checksum
How to use checksums
639782b16b5e5dc4093e8e77cdf0843eb791d9ef686f9d69ba775c320f6f12e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.6

Release files / torch_qu-0.1.4-py3-none-any.whl

Download URL torch_qu-0.1.4-py3-none-any.whl
Size 34.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a56270e4fad9e8875ec3aebe9553ad5b12e30943fc52578320457ee165041f96
BLAKE2b-256 checksum
How to use checksums
b653e566da3c976a1e42b6709f408bfd51c0ed30d97026c32b0dc80772f91312
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.6

Security for torch-qu 0.1.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page