Skip to main content

PyTorch-style quantum-ML layers that run on CPUs, GPUs or real QPUs via NVIDIA CUDA-Q

Project description

🌀 torch-quantum

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

PyPIPythonLicense


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!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torch_qu-0.1.4.tar.gz (27.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torch_qu-0.1.4-py3-none-any.whl (34.7 kB view details)

Uploaded Python 3

File details

Details for the file torch_qu-0.1.4.tar.gz.

File metadata

  • Download URL: torch_qu-0.1.4.tar.gz
  • Upload date:
  • Size: 27.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.6

File hashes

Hashes for torch_qu-0.1.4.tar.gz
Algorithm Hash digest
SHA256 9e3cf0c018dbd311870272231b4dc3c8db7047349c5bd03bce70d1aa96b3051f
MD5 87b1e01cc1409da1d2baf92244e6f519
BLAKE2b-256 639782b16b5e5dc4093e8e77cdf0843eb791d9ef686f9d69ba775c320f6f12e6

See more details on using hashes here.

File details

Details for the file torch_qu-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: torch_qu-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 34.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.6

File hashes

Hashes for torch_qu-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a56270e4fad9e8875ec3aebe9553ad5b12e30943fc52578320457ee165041f96
MD5 09675c500ac894010f7346cdae24e6a2
BLAKE2b-256 b653e566da3c976a1e42b6709f408bfd51c0ed30d97026c32b0dc80772f91312

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page