Quantum random numbers in Python
Use the Python random module with real quantum random numbers from ANU Quantum Numbers.
Usage
Import qrandom and use it like the standard random module:
>>> import qrandom
>>> qrandom.random()
0.15357449726583722
>>> qrandom.sample(range(10), 2)
[6, 4]
>>> qrandom.gauss(0.0, 1.0)
-0.8370871276247828
You can also use the class qrandom.QuantumRandom. It has the same
interface as random.Random.
There is also a NumPy interface (implemented using RandomGen) but it is not fully tested:
>>> from qrandom.numpy import quantum_rng
>>> qrng = quantum_rng()
>>> qrng.random((3, 3)) # use like numpy.random.default_rng()
array([[0.37220278, 0.24337193, 0.67534826],
[0.209068 , 0.25108681, 0.49201691],
[0.35894084, 0.72219929, 0.55388594]])
Installation
pip install quantum-random
The minimum supported version of Python is 3.9.
The NumPy interface is optional. To include it:
pip install 'quantum-random[numpy]'
Setting the ANU Quantum Numbers API key
ANU Quantum Numbers requires an API key. You can get a free trial or pay for a key here.
You can set the key in order of precedence as follows:
- Set the
QRANDOM_API_KEYenvironment variable. - Write the key to
qrandom.iniusing theqrandom-initsetup utility (included with the package). By default, the INI file is saved in your home config directory (e.g.,~/.config/in Linux) andqrandomwill find it without you having to set any environment variables. If you choose to save to a different location, you must setQRANDOM_CONFIG_DIR.
The config file is ignored if QRANDOM_API_KEY is set.
Pre-fetching batches
Quantum numbers are fetched from the API in batches of 1024 as needed. Use
qrandom.fill(n) to pre-fetch n batches at the start of your computation.
Implementation details
The default pseudo-random generator is replaced by calls to
the ANU API. The qrandom module exposes a class derived from random.Random with a
random() method that outputs quantum floats in the range [0, 1)
(converted from 64-bit integers). Overriding random.Random.random
is sufficient to make the qrandom module behave mostly like the
random module as described in the Python docs. The exceptions
are getrandbits() and randbytes(): these are not available in
qrandom. Because getrandbits() is not available, randrange() cannot
produce arbitrarily long sequences. Finally, the user is warned when seed()
is called because the quantum generator has no state. For the same reason,
getstate() and setstate() are not implemented.
Metadata
Release files for quantum-random 1.5.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| quantum_random-1.5.4.tar.gz | 287.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| quantum_random-1.5.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 296.7 kB
Release files / quantum_random-1.5.4.tar.gz
| Download URL | quantum_random-1.5.4.tar.gz |
|---|---|
| Size | 287.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5325770078759e3281dd874a0c00397c4ccfe95137dcca67bdacd9ec5ab75f5c
|
|
BLAKE2b-256 checksum How to use checksums |
3a7f0d18032c84c306046c01db18ca2e2d9e84d16c6cf7e08d2a06e1ecb81b64
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.20 {"installer":{"name":"uv","version":"0.12.20","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / quantum_random-1.5.4-py3-none-any.whl
| Download URL | quantum_random-1.5.4-py3-none-any.whl |
|---|---|
| Size | 9.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a6a57384e9d090200a7a4ea1a739a029f628c7103e259ed421f128d7814a6637
|
|
BLAKE2b-256 checksum How to use checksums |
4aa1af40aa0a1bc3440aac4b677d8ea6d17bb12d65057432a484048e9a8a1779
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.20 {"installer":{"name":"uv","version":"0.12.20","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|