Skip to main content

XQCP -- X-Quadratic Constraint Programmer

A constraint-programming DSL that compiles a problem description into three XQVM assembly programs: an encoder, a verifier, and a decoder. See Three Programs for what those three are and why there are three.

Install

pip install xqcp

A complete example

Max-Cut for three nodes -- the same graph Quadratic Models derives by hand -- built with Problem and compiled to .xqasm:

from xqcp import Domain, Problem, Types

problem = Problem("Triangle")
n = problem.input("n", type=Types.Int)
edges = problem.input("edges", type=Types.Vec)
problem.define_model(size=n, domain=Domain.BINARY)

edge_count = problem.stow("edge_count", edges.veclen() // 3)
with problem.range(0, edge_count) as e:
    offset = e * 3
    i = problem.stow("i", edges.get(offset))
    j = problem.stow("j", edges.get(offset + 1))
    w = problem.stow("w", edges.get(offset + 2))
    problem.model.linear[i].add(-w)
    problem.model.linear[j].add(-w)
    problem.model.quadratic[i, j].add(w * 2)

partition = problem.output("partition", type=Types.Vec)
with problem.range(0, n) as node:
    partition.append(problem.sample.getline(node))

programs = problem.compile()
print(programs.encoder.count("\n"), "lines in the compiled encoder")
# 57 lines in the compiled encoder

programs.encoder builds the model; programs.verifier checks a sample against it and computes its energy; programs.decoder reads a sample back into a partition. Each is a standalone .xqasm string, runnable through any XQVM interpreter.

Domain also covers integer variables (INTEGER with k=, or with lo=/hi= for a range XQCP shifts into the model and back out through sample.value()) and unordered cases (CATEGORICAL with k= and penalty=, recorded as a one-hot binary grid read with sample.case()). The Modelling chapter covers each form and what constraints are available on it.

How it works

Problem records every DSL call (input, define_model, range, stow, model.linear[i].add(w), and the rest) as an Action. Nothing is assembled until compile() runs: it walks the recorded actions once per program and emits assembly with automatic register allocation and loop nesting.

DSL reference

The full DSL -- inputs, expressions, objectives, constraints, control flow, outputs, and what compile() produces -- is Modelling, eight chapters built around this exact package. The normative reference is spec/xqcp/SPEC.md; this package follows it, and any divergence here is a bug.

Examples

Fifteen complete problems built with this DSL, from Max-Cut to Travelling Salesman, are listed in the example gallery.

Also see

  • xqffi -- pyo3 FFI bindings to the Rust runtime, which assembles, verifies and runs the compiled programs.
  • xqsa -- solver adapters for the models this package builds.
  • xquad -- umbrella meta-package.

License

AGPL-3.0-or-later.

Metadata

Release files for xqcp 0.5.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 xqcp 0.5.0
File Size Uploaded Provenance Actions
xqcp-0.5.0.tar.gz 49.3 kB Without provenance Details for xqcp-0.5.0.tar.gz

Built distribution (wheel)

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

Total release size: 83.7 kB

Release files / xqcp-0.5.0.tar.gz

Download URL xqcp-0.5.0.tar.gz
Size 49.3 kB
Tags Source
SHA-256 checksum
How to use checksums
4ce881aa6cfbeb7cf77edd761008ec5989abf34ea4e8c9d96547ec06fc89b5cb
BLAKE2b-256 checksum
How to use checksums
8180134c2fa511d810613083c30ae3a3190cb3231245356b38f3421c05b1d7f4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.5

Release files / xqcp-0.5.0-py3-none-any.whl

Download URL xqcp-0.5.0-py3-none-any.whl
Size 34.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dd14a9debc3a7f4b662ca2e2bcd18f883852b978315ed2d22100f96db01af56d
BLAKE2b-256 checksum
How to use checksums
2df5051063d194ee74d22a48e5bbbdf50494b30920921273697985f8bd23635e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.5

Security for xqcp 0.5.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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