Skip to main content

Quantum Random Number Generator using IBM Quantum hardware

Project description

true-qrandom

PyPI version Python License: MIT

True quantum random number generation using IBM Quantum hardware via Qiskit Runtime.

Classical computers produce pseudo-random numbers โ€” deterministic sequences that only appear random. true-qrandom uses real quantum hardware: a qubit placed in superposition is measured, producing a result that is fundamentally unpredictable by the laws of quantum mechanics.


Features

  • ๐Ÿ”ฌ True quantum randomness โ€” powered by IBM Quantum hardware
  • ๐Ÿ”Œ Familiar random module API โ€” same interface, quantum backend
  • ๐Ÿงช Local simulator mode โ€” develop and test with no credentials
  • โšก Single multi-qubit circuit per request โ€” efficient and fast
  • ๐Ÿ”ข Multiple output types โ€” integers, floats, bytes, and raw bits

More features planned for v0.2.0.


Installation

pip install true-qrandom

Requires Python >= 3.10.


Setup

You need two things from IBM โ€” both are free:

What Where to get it
IBM Cloud API key cloud.ibm.com/iam/apikeys
Instance CRN quantum.cloud.ibm.com โ†’ Create instance โ†’ copy the CRN

Set them as environment variables:

IBMQ_API_KEY=your_ibm_cloud_api_key
IBMQ_INSTANCE=your_ibm_instance_crn

Usage

from true_qrandom import QuantumRandom

# Reads IBMQ_API_KEY and IBMQ_INSTANCE from environment
qr = QuantumRandom()

qr.randint(1, 100)           # โ†’ e.g. 42
qr.random()                  # โ†’ e.g. 0.7341...
qr.randbytes(4)              # โ†’ e.g. b'\x3f\xa1\x09\xcc'
qr.choice(["a", "b", "c"])  # โ†’ e.g. "b"

You can also pass credentials explicitly:

qr = QuantumRandom(
    api_key="your_ibm_cloud_api_key",
    instance="your_ibm_instance_crn"
)

Simulator mode (no credentials needed)

qr = QuantumRandom(use_simulator=True)
qr.random()

Simulator mode uses AerSimulator โ€” not true quantum randomness. Use IBM hardware for production.


API Reference

QuantumRandom(api_key=None, instance=None, use_simulator=False)

Parameter Type Description
api_key str IBM Cloud API key. Falls back to IBMQ_API_KEY env var.
instance str IBM instance CRN. Falls back to IBMQ_INSTANCE env var.
use_simulator bool Use local AerSimulator instead of IBM hardware.

Methods

Method Returns Description
random() float Random float in [0.0, 1.0)
randint(a, b) int Random integer N such that a <= N <= b
randbytes(n) bytes n random bytes
choice(seq) any Random element from a non-empty sequence

Low-level access

from true_qrandom import generate_bits

bits = generate_bits(num_bits=16, use_simulator=True)
print(bits)          # โ†’ "1011001001110100"
print(int(bits, 2))  # โ†’ 45172

How It Works

 |0โŸฉ โ”€โ”€[ H ]โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€[ M ]โ”€โ”€โ–บ 0 or 1   (bit 1)
                              
 |0โŸฉ โ”€โ”€[ H ]โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€[ M ]โ”€โ”€โ–บ 0 or 1   (bit 2)
        :                     :
        :                     :
 |0โŸฉ โ”€โ”€[ H ]โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€[ M ]โ”€โ”€โ–บ 0 or 1   (bit N)
                              โ”‚
                              โ–ผ      
transpile to native gates    1 0 1 1 0 ...  โ†’  int / float / bytes
  1. Each qubit starts in ground state |0โŸฉ
  2. A Hadamard gate (H) puts it into 50/50 superposition (|0โŸฉ + |1โŸฉ) / โˆš2
  3. The circuit is transpiled to the native gate set of the target chip
  4. Measuring collapses the qubit to 0 or 1 โ€” governed by quantum mechanics, not an algorithm
  5. N bits are assembled into the requested output type

Development

git clone https://github.com/yourname/true-qrandom
cd true-qrandom
python3 -m venv .venv
source .venv/bin/activate
pip install -e .

# Simulator tests โ€” no credentials needed
pytest

# Hardware tests
IBMQ_API_KEY="..." IBMQ_INSTANCE="..." pytest -k real_hardware

Requirements

  • Python >= 3.10
  • qiskit >= 1.0
  • qiskit-ibm-runtime >= 0.41
  • qiskit-aer >= 0.14 (simulator mode)

License

MIT โ€” see LICENSE


Contributing

Contributions are welcome! Open an issue or pull request on GitHub.

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

true_qrandom-0.1.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

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

true_qrandom-0.1.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: true_qrandom-0.1.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for true_qrandom-0.1.0.tar.gz
Algorithm Hash digest
SHA256 66bbddfefd88efc19af2cfe0ded9c0013ca461af8e537f6561fa46ffd6cb5083
MD5 bf6b9969b46781fc29cfbba04d211523
BLAKE2b-256 04d164aa741bbc7422b506d527f4abe5d08240a1871eae42cb9027792add8313

See more details on using hashes here.

File details

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

File metadata

  • Download URL: true_qrandom-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for true_qrandom-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8502c77e7ffcd1e8887a4cfddfd25061d7fa9c0882af8ed9c02149fa7654792e
MD5 e893b85e5ffde44a803da3d089547791
BLAKE2b-256 fc40d3924249b40c468d545642f2e10d91a51679f97988250b861ea6bbad5636

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