Skip to main content

Quantum random numbers in Python

Tests Version Python Versions Download Stats License

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:

  1. Set the QRANDOM_API_KEY environment variable.
  2. Write the key to qrandom.ini using the qrandom-init setup utility (included with the package). By default, the INI file is saved in your home config directory (e.g., ~/.config/ in Linux) and qrandom will find it without you having to set any environment variables. If you choose to save to a different location, you must set QRANDOM_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.

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

Uploaded Source

Built Distribution

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

quantum_random-1.5.3-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file quantum_random-1.5.3.tar.gz.

File metadata

  • Download URL: quantum_random-1.5.3.tar.gz
  • Upload date:
  • Size: 257.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","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}

File hashes

Hashes for quantum_random-1.5.3.tar.gz
Algorithm Hash digest
SHA256 2bb0d9c15e0f240102b24cd14b032d71e2c4289e2fb5ce21dcac75f25af3c3b9
MD5 64b1e7c3d6159202d1ffcb1fcff2a427
BLAKE2b-256 3fcae848f45e4c66189796840139ab56c305eed16fbc7cf0214b9ae33b45e6b5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: quantum_random-1.5.3-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","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}

File hashes

Hashes for quantum_random-1.5.3-py3-none-any.whl
Algorithm Hash digest
SHA256 325587119f6fcfbb2b04f01ae4bd4bde701ae31ccb008d6c914d92c5bc06c75f
MD5 cac038e2d5437416368228bebdbaa274
BLAKE2b-256 80108e9c87fd57086a03048d75623c67502585a012885f347f4bbde0e2c1e9d9

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.5.3 This release

2 files

1.5.2

2 files

1.5.1

2 files

1.5.0

2 files

1.4.2

2 files

1.4.1

2 files

1.4.0

2 files

1.3.2

2 files

1.3.1

2 files

1.3.0

2 files

1.2.4

2 files

1.2.3

2 files

1.2.2

2 files

1.1.1

2 files

1.0.1

2 files

1.0.0

2 files

0.2.3

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page