Contextuality package
Contextuality is an open-source Python package for studying contextuality in measurement scenarios using the sheaf-theoretic approach to contextuality by Abramsky and Brandenburger.
This package features:
- Custom measurement scenarios definitions.
- Predefined CHSH, KCBS, and Peres-Mermin measurement scenarios.
- Empirical models from probability vectors or matrices.
- Many features for empirical models, including generations, validations and other utilities.
- Build quantum empirical models from density matrices and PVMs.
- Compute: the Contextual Fraction (CF), the Signalling Fraction (SF), Dual contextual-fraction and more.
- Generate non-contextual and signalling polytopes in V- and H-representations (non-optimized).
- Combine empirical models using scalar multiplication, division, and convex mixtures.
Example usage
The following example covers the main workflow: define a measurement scenario, construct empirical models, inspect their probabilities, and compute their contextual and signalling fractions.
import numpy as np
from contextuality import EmpiricalModel, MeasurementScenario, MeasurementScenarioImplementations
# Define a scenario directly.
X = [0, 1, 2, 3, 4]
M = [[i, (i + 1) % 5] for i in X]
O = [0, 1]
custom_kcbs = MeasurementScenario(X, M, O)
# Or use one of the predefined scenarios.
chsh = MeasurementScenarioImplementations.chsh()
kcbs = MeasurementScenarioImplementations.kcbs()
peres_mermin = MeasurementScenarioImplementations.peres_mermin()
# Build a deterministic empirical model from one outcome position per context.
deterministic = EmpiricalModel(
chsh,
chsh.generate_deterministic([0, 0, 1, 2]),
)
print(deterministic.is_valid, deterministic.is_deterministic)
# A PR-box model is a useful contextual, no-signalling example.
pr_box = EmpiricalModel(
chsh,
np.array([
[0.5, 0.0, 0.0, 0.5],
[0.5, 0.0, 0.0, 0.5],
[0.5, 0.0, 0.0, 0.5],
[0.0, 0.5, 0.5, 0.0],
]),
)
print(pr_box.probability_outcome(1, [0, 2], 0))
print(pr_box.maximum_incompatibility_of_marginals())
print(pr_box.compute_cf(solver="highs"))
print(pr_box.compute_sf(solver="highs"))
# Quantum realizations can be supplied as a density matrix and PVMs.
from qutip import basis, identity, ket2dm, sigmax, sigmaz, tensor
zero, one = basis(2, 0), basis(2, 1)
psi = (tensor(zero, one) - tensor(one, zero)) / np.sqrt(2)
rho = ket2dm(psi).unit().full()
sz, sx = sigmaz(), sigmax()
A0 = [ket2dm(state) for state in sz.eigenstates()[1]]
A1 = [ket2dm(state) for state in sx.eigenstates()[1]]
B0 = [ket2dm(state) for state in (-(sx + sz) / np.sqrt(2)).eigenstates()[1]]
B1 = [ket2dm(state) for state in ((sx - sz) / np.sqrt(2)).eigenstates()[1]]
pvms = [
[tensor(projector, identity(2)).full() for projector in A0],
[tensor(projector, identity(2)).full() for projector in A1],
[tensor(identity(2), projector).full() for projector in B0],
[tensor(identity(2), projector).full() for projector in B1],
]
quantum_model = EmpiricalModel(chsh)
quantum_model.quantum_realisation(rho, pvms)
print(quantum_model.compute_cf(solver="highs")["CF"])
More examples in the form of notebooks can be found in the notebooks folder.
Install
The package is working with pycddlib which is a python library for the double description method and you need to install cdd for it to work. The installation procedure is on their website. For example, for the aptitude package manager this amounts to:
$ sudo apt update
$ sudo apt install libcdd-dev libgmp-dev python3-dev
The package also uses solvers for linear programs and you need to install one. 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.
You can then install the package from pypi:
$ python -m pip install contextuality
Documentation
The documentation is available online on readthedocs.
Development
Install from source
You can install the package directly from source:
$ git clone https://github.com/Kim-Vallee/contextuality.git
$ cd contextuality
$ poetry install --with dev
$ pip install -e . # or for poetry: poetry add --editable .
Running tests
The tests are managed with pytest, which you can directly run with
$ pytest
Building documentation
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.
Credits
- Kim Vallée — Author and main contributor
- Adel Sohbi — Author
License
Contextuality is free and open-source software released under the GNU General Public Licence v3.0.
Please see License File for more information.
Metadata
Release files for contextuality 2.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| contextuality-2.1.0.tar.gz | 29.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| contextuality-2.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 60.4 kB
Release files / contextuality-2.1.0.tar.gz
| Download URL | contextuality-2.1.0.tar.gz |
|---|---|
| Size | 29.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5d19320abc72cb785d525c1e373e456d1a20d33ab82b21a4d6046f815cce573e
|
|
BLAKE2b-256 checksum How to use checksums |
83bf48ec2b41c88c7759be74b8f87d414435dc9cb617337f3c23d3fbe42ac479
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 9, 2026.
Transparency logRelease files / contextuality-2.1.0-py3-none-any.whl
| Download URL | contextuality-2.1.0-py3-none-any.whl |
|---|---|
| Size | 31.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
92905e8e144e9162c7a953687b6f6112324b87d3aba9c79f110451a7006812f0
|
|
BLAKE2b-256 checksum How to use checksums |
4e8610ceee882b7728c671a394c58197928491ecd1363c80286bcb02d5b0f170
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 9, 2026.
Transparency log