Skip to main content

Quantum random numbers

Project description

Quantum random numbers in Python

Tests

This package brings the ANU quantum random numbers to Python 3.7 to 3.10.

The default pseudo-random generator in Python is replaced by calls to the ANU API that serves real quantum random numbers.

Install

pip install quantum-random

Optionally, for NumPy support,

pip install quantum-random[numpy]

Note that the NumPy integration is not well-tested and is not available for Python 3.10.

Usage

Just import qrandom and use it like you'd use the standard Python random module. For example,

>>> import qrandom

>>> qrandom.random()
0.15357449726583722

>>> qrandom.sample(range(10), 2)
[6, 4]

>>> qrandom.gauss(0.0, 1.0)
-0.8370871276247828

Alternatively, you can import QuantumRandom from qrandom and use the class directly (just like random.Random).

Under the hood, batches of quantum numbers are fetched from the API as needed and each batch contains 1024 numbers. If you wish to pre-fetch more, use qrandom.fill(n), where n is the number of batches.

Optionally, if you have installed the NumPy integration,

>>> 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]])

Tests

To run the tests locally, you will need poetry and Python 3.7-3.10. One way of having multiple Python versions is to use pyenv and list the versions in .python-version.

poetry install
poetry run tox

See here for a visualisation and a Kolmogorov–Smirnov test.

Notes on implementation

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 ints). 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 at the moment are getrandbits() and randbytes() that 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 there is no state. For the same reason, getstate() and setstate() are not implemented.

NumPy support is provided using RandomGen.

License

See LICENCE.

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

quantum-random-1.1.1.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

quantum_random-1.1.1-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file quantum-random-1.1.1.tar.gz.

File metadata

  • Download URL: quantum-random-1.1.1.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.8.10 Linux/5.10.16.3-microsoft-standard-WSL2

File hashes

Hashes for quantum-random-1.1.1.tar.gz
Algorithm Hash digest
SHA256 f08100db875e62ba4f5ab5bae67201844657b1018e505200dd69c8fc383a6945
MD5 c39dccf9f0d9f703cf0cb6bebdfba599
BLAKE2b-256 60a39d11719d04968b3aad1d16355e1a61265a72c5228c4757b28326ecfa0d1e

See more details on using hashes here.

File details

Details for the file quantum_random-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: quantum_random-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.8.10 Linux/5.10.16.3-microsoft-standard-WSL2

File hashes

Hashes for quantum_random-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 17f00862ecb25702f1940dd7cc54d8622be0d521cf68852a8905a9bd094f864b
MD5 095b9b4468af3299e817e75bc47c7839
BLAKE2b-256 c8b1aa91a62c0734d2c5a6fdde785dc70f0f13938fcbdeb6c702bf7884cf880c

See more details on using hashes here.

Supported by

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