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 with the generator completeness relations imposed (certified bounds on the analytic line (2p+1)/6), including a four-output Bell-measurement variant solved through the direct sparse backends (CLARABEL by default, or MOSEK when a license is available).

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

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.1
File Size Uploaded
ncpolopt-0.2.1.tar.gz 70.3 kB Details

Built distribution (wheel)

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

Total release size: 160.1 kB

Release files / ncpolopt-0.2.1.tar.gz

Download URL ncpolopt-0.2.1.tar.gz
Size 70.3 kB
Tags Source
SHA-256 checksum
How to use checksums
0810a79dc0e1b20ef23e2b6e1647966b088d8942bcebdcfd41e7feee71c4e0f9
BLAKE2b-256 checksum
How to use checksums
56ed5058ababfcd584f0f17e94860fd5936a0c6f1a7ddbe06b88cfa631521ac2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.19 {"installer":{"name":"uv","version":"0.12.19","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.1-py3-none-any.whl

Download URL ncpolopt-0.2.1-py3-none-any.whl
Size 89.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4d15b5c9b3b0daab578fe90d54dbe53d945a09a4f48d796b784102141f5cdf25
BLAKE2b-256 checksum
How to use checksums
f6cf25c770e238e43e6dc8a172e1748e757c74d34a8e3f570023fb2240cb8953
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.19 {"installer":{"name":"uv","version":"0.12.19","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

This release

0.2.1 This release

2 release files

0.2.0

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