Skip to main content

Quantum random numbers

Project description

Quantum random numbers in Python

Tests Version Python Versions Download Stats License

Use the Python random module with real quantum random numbers from ANU. The default pseudo-random generator is replaced by calls to the ANU API.

Usage

Import qrandom and use it like the standard 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 use the class qrandom.QuantumRandom. It has the same interface as random.Random.

There is also a NumPy interface, although 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]])

NumPy is supported using RandomGen. Note that there are currently no RandomGen wheels available for Python 3.11.

Installation

The minimum supported Python version is 3.7. Install with pip:

python -m pip install -U quantum-random

If you want NumPy support:

python -m pip install -U quantum-random[numpy]

First-time setup: setting your API key

ANU requires you to use an API key. You can get a free trial or pay for a key here.

You can pass your key to qrandom in three ways:

  1. By setting the environment variable QRANDOM_API_KEY.
  2. By running qrandom-init to save your key in an INI file stored in a subdirectory of your home config directory as specified by XDG, e.g., /home/<your-username>/.config/qrandom/.
  3. By running qrandom-init to save your key in an INI file in a directory of your choice set by QRANDOM_CONFIG_DIR.

qrandom will look for the key in the order above. The qrandom-init command line utility is installed as part of the package.

Pre-fetching batches

Batches of quantum numbers are fetched from the API as needed. Each batch contains 1024 numbers. Use qrandom.fill(n) to fetch n batches if you need to pre-fetch at the start of your computation.

Tests

The tests run for Python 3.7 - 3.11 on the latest Windows, macOS and Ubuntu runner images. For Python 3.11 on macOS, the tests run only with no NumPy support as RandomGen fails to compile. Use tox to run the tests locally.

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 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.

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.3.0.tar.gz (142.3 kB view details)

Uploaded Source

Built Distribution

quantum_random-1.3.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: quantum-random-1.3.0.tar.gz
  • Upload date:
  • Size: 142.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for quantum-random-1.3.0.tar.gz
Algorithm Hash digest
SHA256 79c5317b9bcc13e802de6502eae6f05cd31f9d883a181d49ec64968c3d923fad
MD5 656882963577e1c60fb9d1d9f7a51b3e
BLAKE2b-256 8f7e12c43e5febb4d4d074f31e3df99d37d6ce5fe2e46f8dd2119299421fe9e7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for quantum_random-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1cae9aa1b4b50589fbd76d0643c18e98953920d1615e558c237e077b3c3cf285
MD5 7e25591ab02bd15eb5e0496b09e66e8a
BLAKE2b-256 8d3c2e0e4587c76fe791c4f2d6504fb3d0a66f8d85e1791e13958412306987bd

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