Skip to main content

ncpolopt

Python 3.13+ License: GPL-3.0 CI status PyPI version PyPI downloads

Sparse SDP relaxations of polynomial optimization problems with noncommuting variables — the NPA hierarchy and its relatives. ncpolopt is a modern rewrite of the GPL-3 ncpol2sdpa package: the same hierarchies and the same four solver backends, behind a fresh, typed API built on dataclasses and lazy solver discovery.

  • NPA hierarchy for noncommutative operators, plus the Moroder (PPT), steering, and RDM (reduced density matrix) hierarchies, and operator-insertion (MDI-style) helpers that generate the trace pins and functional-class moment equalities of a moment-insertion relaxation from an explicit matrix realization (trace_moment_pins, class_moment_equalities).
  • Lasserre-style hierarchies for commuting variables, with an optional chordal sparsity extension (SparsePOP-style) that splits the relaxation into independent moment blocks per clique.
  • Four solver backends: cvxpy, MOSEK, PICOS/cvxopt, and the external SDPA binary — selected by name or auto-detected — plus a direct sparse CLARABEL backend (solver="clarabel", explicit selection only) for SDPs too large for cvxpy's dense canonicalization.
  • Moment expressions (MomentEntry) instead of a string DSL for writing conditions on the moment matrix directly.

Installation

Requires Python 3.13+. The core package needs only numpy, scipy, and sympy; install a solver extra to actually solve:

uv pip install ncpolopt[cvxpy]        # or: pip install ncpolopt[cvxpy]

Available extras: cvxpy, clarabel (direct sparse backend), mosek, cvxopt (PICOS + cvxopt), chordal (chompack, only needed for the chompack completion method), and all. With no solver installed the package still imports and builds relaxations; only solving raises a SolverError telling you which extra to install.

Quickstart

A tiny noncommutative problem: two Hermitian operators x0, x1, minimize the anticommutator x0*x1 + x1*x0 under the constraint x1 - x1² + ½ ≥ 0 and the projection x0² = x0:

import ncpolopt as nc

X = nc.generate_operators("x", 2, hermitian=True)
problem = nc.Problem(
    X,
    objective=X[0] * X[1] + X[1] * X[0],
    inequalities=[-X[1] ** 2 + X[1] + 0.5],
    substitutions={X[0] ** 2: X[0]},
)
solution = problem.solve(level=2)
print(solution.primal)   # -0.75

Object model

Work flows through three layers, each a frozen dataclass:

  1. Problem — the symbolic model: variables (a list of lists for multipartite problems), objective, polynomial and moment constraints, substitution rules. Reusable; building a relaxation never mutates it.
  2. NpaRelaxation — the SDP at a hierarchy level, built by problem.relaxation(level) or subclass hierarchies MoroderHierarchy, SteeringHierarchy, RdmHierarchy. The assembled SDP data (relaxation.sdp) is a frozen blockwise COO representation.
  3. Solution — the immutable solve result (primal, dual, status, x_mat, y_mat) with extraction helpers: monomial_value(), dual_value(), dual_block(), sos_decomposition(), solution_ranks().

Command line

ncpolopt                # version, platform, detected solvers
ncpolopt --version

Development

uv sync --group dev
uv run pytest
uv run ruff check .

Multi-minute solver certification tests are marked slow and skipped in CI (uv run pytest -m "not slow"); run them locally with uv run pytest -m slow.

Examples

examples/quantum_memory/ — numerical verification of a measurement-device-independent quantum-memory certification (depolarizing channel): correlation tables, dual SDPs, and moment-matrix relaxations of the NPA-tau form, including a four-output Bell-measurement variant solved through the direct sparse backends (CLARABEL by default, or MOSEK with tp_pin=True).

License

GPL-3.0-only. This package is derived from ncpol2sdpa, which is Copyright (C) 2012-2016 Peter Wittek and contributors.

Metadata

Release files for ncpolopt 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ncpolopt 0.2.0
File Size Uploaded
ncpolopt-0.2.0.tar.gz 68.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ncpolopt 0.2.0
File Interpreter ABI Platform
ncpolopt-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 155.6 kB

Release files / ncpolopt-0.2.0.tar.gz

Download URL ncpolopt-0.2.0.tar.gz
Size 68.2 kB
Tags Source
SHA-256 checksum
How to use checksums
841833adbfbeb5e1760a4711a25b4fd984a1327205dfd12298c3a6d4b95402af
BLAKE2b-256 checksum
How to use checksums
d76fcada266d666e1d711226e47ea6efb9ddce9dfd60fe9d9765e6decd4b6cd5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","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}

Release files / ncpolopt-0.2.0-py3-none-any.whl

Download URL ncpolopt-0.2.0-py3-none-any.whl
Size 87.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
10edf83ece8c6f231bc9915b44c98fabbe2fc1d757ac2af312da923f68774ede
BLAKE2b-256 checksum
How to use checksums
5048d85505b250381f0a92ab153002adb6944b4b1a956f615948219354003a49
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","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}

Release history Release notifications | RSS feed

0.2.2

2 release files

0.2.1

2 release files

This release

0.2.0 This release

2 release files

0.1.0

2 release 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