Quantum Random Number Generator using IBM Quantum hardware
Project description
true-qrandom
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
randommodule 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
- Each qubit starts in ground state
|0โฉ - A Hadamard gate (H) puts it into 50/50 superposition
(|0โฉ + |1โฉ) / โ2 - The circuit is transpiled to the native gate set of the target chip
- Measuring collapses the qubit to
0or1โ governed by quantum mechanics, not an algorithm - 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.0qiskit-ibm-runtime >= 0.41qiskit-aer >= 0.14(simulator mode)
License
MIT โ see LICENSE
Contributing
Contributions are welcome! Open an issue or pull request on GitHub.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66bbddfefd88efc19af2cfe0ded9c0013ca461af8e537f6561fa46ffd6cb5083
|
|
| MD5 |
bf6b9969b46781fc29cfbba04d211523
|
|
| BLAKE2b-256 |
04d164aa741bbc7422b506d527f4abe5d08240a1871eae42cb9027792add8313
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8502c77e7ffcd1e8887a4cfddfd25061d7fa9c0882af8ed9c02149fa7654792e
|
|
| MD5 |
e893b85e5ffde44a803da3d089547791
|
|
| BLAKE2b-256 |
fc40d3924249b40c468d545642f2e10d91a51679f97988250b861ea6bbad5636
|