Skip to main content

Contextuality is a package that allows to compute various contextuality related quantities and formalize scenarios in quantum theory.

Project description

Contextuality package

Software License Version Tests

This project is a starter project to have many tools to compute various quantities in measurement scenarios as defined by Abramsky and Brandenburger. It can be used in a variety of cases.

Install

The package is working with pycddlib, thus you need to install cdd. See directly on their website. For aptitute this amounts to:

$ sudo apt update
$ sudo apt install libcdd-dev libgmp-dev python3-dev

The package also uses solvers for linear programs. The default is Mosek (see installation instructions), for which you can have a licence for free if you work in academia here. Another option is to go for HiGHS solver, which is free.

The you can either install the package from pypi:

$ python -m pip install contextuality

Or you can install it from source:

$ git clone https://github.com/Kim-Vallee/contextuality.git
$ cd contextuality
$ poetry install
$ poetry build
$ python -m pip install dist/contextuality-<version>-py3-none-any.whl

Contribute

If you wish to improve the package you can install from the sources with poetry and make pull requests:

$ git clone https://github.com/Kim-Vallee/contextuality.git
$ cd contextuality
$ poetry install --with dev
$ pip install -e . # or for poetry:
$ poetry add --editable .

Documentation

The documentation is available on readthedocs.

Compile documentations

The documentation can be compiled in the docs directory.

$ cd docs
$ make html

then navigate to docs/build/html and open index.html to access the documentation.

Usage example

from contextuality.measurement_scenario import MeasurementScenario, MeasurementScenarioImplementations
import numpy as np
from contextuality.empirical_model import EmpiricalModel
from contextuality.utils import compute_max_cf, compute_deterministic_fraction

# Defining the contextuality scenario
X = [0, 1, 2, 3, 4]
M = [[i, (i + 1) % 5] for i in X]
O = [0, 1]
kcbs = MeasurementScenario(X, M, O)

# Equivalently from pre-defined scenarios
chsh = MeasurementScenarioImplementations.CHSH()

# We can make a simple empirical model...
empirical_model_ex = EmpiricalModel(kcbs, np.array([1, 0, 0, 0] * 5))

# ... or make a quantum realization of an empirical model
empirical_model = EmpiricalModel(kcbs)
meas = np.zeros((5, 2, 3, 3))  # shape = number mesurements, number of outcomes, dimension of state (d x d)
N = 1 / np.sqrt(1 + np.cos(np.pi / 5))
for i in range(5):
    vec = N * np.array([np.cos(4 * np.pi * i / 5), np.sin(4 * np.pi * i / 5), np.sqrt(np.cos(np.pi / 5))])
    meas[i][1] = np.outer(vec, vec)
    meas[i][0] = np.eye(3) - meas[i][1]

psi = np.array([0, 0, 1])
rho = np.outer(psi, psi)
empirical_model.quantum_realisation(rho, meas)

# We can compute the contextual fraction
ncf_empirical_model = empirical_model.compute_cf(solver="MOSEK")["NCF"]

# The signalling fraction from the utils
sf_empirical_model = empirical_model.compute_sf()['SF']

# Then there are plenty of functions to use from utils
result = compute_max_cf(kcbs, eta=0.3, sigma=0.5)  # Experimental

print(result['EmpiricalModel'].vector)

df = compute_deterministic_fraction(result["EmpiricalModel"], verbose=False)
print(df)

CF_result = result['EmpiricalModel'].compute_cf()

Notebooks

Examples in the form of notebooks can be found in the notebooks folder.

Credits

License

The CC BY-NC 4.0. Please see License File for more information.

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

contextuality-2.0.2.tar.gz (22.2 kB view details)

Uploaded Source

Built Distribution

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

contextuality-2.0.2-py3-none-any.whl (24.9 kB view details)

Uploaded Python 3

File details

Details for the file contextuality-2.0.2.tar.gz.

File metadata

  • Download URL: contextuality-2.0.2.tar.gz
  • Upload date:
  • Size: 22.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.1 Linux/6.11.0-1018-azure

File hashes

Hashes for contextuality-2.0.2.tar.gz
Algorithm Hash digest
SHA256 4a4452a33b258936965640c8bd2bfc41ebc1a9aedf3659673058534e6ec00553
MD5 5f622a04dd8e102417c78f3a2c1c769b
BLAKE2b-256 a09538e4956146ba922b2b0c75e8bd97b101a5e8777503ceae0e5fda8b1f40b0

See more details on using hashes here.

File details

Details for the file contextuality-2.0.2-py3-none-any.whl.

File metadata

  • Download URL: contextuality-2.0.2-py3-none-any.whl
  • Upload date:
  • Size: 24.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.1 Linux/6.11.0-1018-azure

File hashes

Hashes for contextuality-2.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4fab2725cebcd1f03e4b6013f78378f0f02d8cf7f5c99b302e3c0e32271a0c23
MD5 998af8a2b129c2187436ff5121a3dc29
BLAKE2b-256 d26fd701d8c43a9b52de5efd60604a0fab1162c745b9fe8105b91a6565436701

See more details on using hashes here.

Supported by

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