Skip to main content

Pure Python interface for ID Quantique Quantis Quantum Random Number Generators

Project description

Quantix

Pure Python interface for ID Quantique Quantis Quantum Random Number Generators.

This is an independent, clean-room implementation that provides direct access to Quantis PCI/PCIe quantum RNG devices through /dev/qrandom* device files.

Background

This library was created to modernize access to quantum random number generators on Linux. The original ID Quantique software from 2013 included only a C library (libQuantis) with no Python implementation.

Rather than try to bring forward compatibility to the aging C library codebase, we reverse-engineered the interface and wrote a pure Python implementation from scratch. The result is a modern, type-safe library with zero C dependencies.

Hardware: Developed for and tested with a Quantis PCI 16Mbps card (circa 2013) on kernel 6.1.

This project includes:

  • A pure Python library (this package) - reverse-engineered from scratch
  • A ported kernel driver (quantis-pci) - updated from 2013 source for kernel 6.1+ compatibility

The kernel driver required significant updates:

  • Porting work: Modernized APIs (access_ok, ioremap, proc_fs), fixed deprecated functions
  • Critical bug fixes:
    • Removed obsolete Big Kernel Lock (BKL) that caused "scheduling while atomic" kernel oops
    • Fixed PCI BAR reservation to only request BAR 1 (the driver was incorrectly trying to reserve all BARs, causing -EBUSY errors)

Without these fixes, the driver would not load or would crash when performing ioctl operations on kernel 6.1+.

Installation

Prerequisites

  1. Quantis kernel driver must be loaded:

    sudo modprobe quantis_pci
    
  2. Device permissions: Add your user to the quantis group:

    sudo usermod -a -G quantis $USER
    # Log out and back in for group changes to take effect
    

Install Quantix

# From PyPI
pip install quantix-rng

# Or with uv (recommended)
uv pip install quantix-rng

Note: The package name is quantix-rng but you import it as quantix:

from quantix import Quantix, DeviceType

Development Installation

# Clone the repository
git clone https://github.com/tcdent/quantix.git
cd quantix

# Install in editable mode with dev dependencies
uv sync
# or
pip install -e ".[dev]"

Quick Start

from quantix import Quantix, DeviceType

# Initialize device
qrng = Quantix(DeviceType.PCI, device_number=0)

# Use as context manager (recommended)
with qrng:
    # Read 16 random bytes
    random_bytes = qrng.read(16)
    print(f"Random bytes: {random_bytes.hex()}")

    # Read random integer
    random_int = qrng.read_int()
    print(f"Random int: {random_int}")

    # Read random float [0.0, 1.0)
    random_float = qrng.read_float()
    print(f"Random float: {random_float}")

API Examples

Device Information

with Quantix(DeviceType.PCI, 0) as qrng:
    # Get device information via ioctl
    driver_version = qrng.get_driver_version()
    board_version = qrng.get_board_version()
    modules_count = qrng.get_modules_count()

    print(f"Driver: v{driver_version}")
    print(f"Board: 0x{board_version:08x}")
    print(f"Working modules: {modules_count}")

Reading Random Data

with Quantix(DeviceType.PCI, 0) as qrng:
    # Read bytes
    data = qrng.read(1024)

    # Read typed values
    value_int = qrng.read_int()          # 32-bit unsigned
    value_short = qrng.read_short()      # 16-bit unsigned
    value_float = qrng.read_float()      # [0.0, 1.0)
    value_double = qrng.read_double()    # [0.0, 1.0) higher precision

    # Read in range
    dice_roll = qrng.read_int_range(1, 6)  # 1-6 inclusive

Module Control

with Quantix(DeviceType.PCI, 0) as qrng:
    # Get module status
    mask = qrng.get_modules_mask()      # Available modules
    status = qrng.get_modules_status()  # Working modules

    # Control modules
    qrng.enable_module(0)
    qrng.disable_module(1)

    # Reset board
    qrng.reset_board()

Architecture

Quantix uses pure Python to interact directly with the Quantis hardware:

┌──────────────┐
│   Python     │
│  Application │
└──────┬───────┘
       │ Quantix (Pure Python)
       │ - open(/dev/qrandom0)
       │ - read() syscall
       │ - ioctl() for device info
       ▼
┌──────────────┐
│    Kernel    │
│    Driver    │  quantis_pci.ko
└──────┬───────┘
       │ Memory-mapped I/O
       ▼
┌──────────────┐
│   Quantis    │
│   Hardware   │  PCI device
└──────────────┘

No C library required! Direct syscalls for maximum simplicity.

Why Quantix?

The original libQuantis C library from 2013 has compatibility issues with modern systems (segfaults, deprecated threading, C++17 incompatibility). Quantix provides a clean, modern alternative:

  • No compilation needed - pure Python
  • No CFFI/ctypes complexity - direct syscalls
  • Modern Python practices - type hints, context managers
  • Better error messages - Pythonic exceptions
  • Maintainable - easy to read and modify

Testing

# Run all tests
make test

# Run with verbose output
make test-verbose

All tests require actual hardware and proper permissions.

Development

# Install development dependencies
uv sync

# Run tests
pytest tests/ -v

# Check types
mypy src/

# Format code
black src/ tests/

License

MIT License - see LICENSE file for details.

This is an independent implementation not affiliated with ID Quantique.

Hardware Support

Currently supported:

  • ✅ Quantis PCI 16Mbps
  • ✅ Quantis PCIe (via PCI driver)

Not yet supported:

  • ❌ Quantis USB (contributions welcome!)

Troubleshooting

Permission Denied

# Add user to quantis group
sudo usermod -a -G quantis $USER

# Verify group membership
groups | grep quantis

# If not showing, log out and back in or run:
newgrp quantis

No Device Found

# Check if driver is loaded
lsmod | grep quantis

# Check if device exists
ls -l /dev/qrandom*

# Load driver if needed
sudo modprobe quantis_pci

Multiple Devices

# List all devices
from quantix import count_devices, DeviceType

count = count_devices(DeviceType.PCI)
print(f"Found {count} PCI devices")

# Access specific device
with Quantix(DeviceType.PCI, device_number=1) as qrng:
    data = qrng.read(16)

Contributing

Contributions welcome! Please ensure:

  • Tests pass (make test)
  • Type checks pass (uv run mypy src/quantix/)

License

MIT License - see LICENSE file for details.

References

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

quantix_rng-0.1.0.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

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

quantix_rng-0.1.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file quantix_rng-0.1.0.tar.gz.

File metadata

  • Download URL: quantix_rng-0.1.0.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.5

File hashes

Hashes for quantix_rng-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cd87a18ba5a6a00bd39c6f6b23ad25e37dac5ed4f532731c9c568c9bdc560fcf
MD5 df773a222476dc6317e4e50586ded875
BLAKE2b-256 f3d8eb1a1ba4762352934664cfd925d000cd282c15dadc0d3540992deb3cadcf

See more details on using hashes here.

File details

Details for the file quantix_rng-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for quantix_rng-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b333a1bb0b73ed770dc9d8fe789a9745f8393db99056d077fcff7a593aa46dc9
MD5 a7aab7006ebc53224d85cf82ead7310d
BLAKE2b-256 2c46adc40901060a65a044ccdd47a2e569f28d2e081cb4ff9a98b8c9725a84f0

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