Skip to main content

High-performance binary arithmetic.

Project description

binar - High-performance binary arithmetic

Fast bit vectors and bit matrices with linear algebra over GF(2).

Overview

binar provides efficient Python bindings to high-performance Rust implementations of:

  • Bit vectors (BitVector) - Variable-length sequences of bits
  • Bit matrices (BitMatrix) - 2D arrays of bits with linear algebra operations
  • Operations optimized for quantum computing and error correction

The library is designed for applications requiring fast linear algebra over GF(2) (the binary field with elements {0, 1}), where addition is XOR and multiplication is AND.

Installation

pip install binar

For development:

cd binar/bindings/python
maturin develop --release

Quick Start

import binar

# Bit vectors: create, manipulate, and compute
v1 = binar.BitVector("10110")
v2 = binar.BitVector([True, False, True, False, False])
print(v1.weight)  # 3 (number of 1s)
print(v1.support)  # [0, 2, 3] (indices of 1s)

# Boolean operations
v3 = v1 ^ v2  # XOR
print(v1.dot(v2))  # Inner product over GF(2)

# Bit matrices: linear algebra over GF(2)
m = binar.BitMatrix([
    "1010",
    "0110",
    "1100",
    "0011"
])
print(m.shape)  # (4, 4)

# Matrix operations
identity = binar.BitMatrix.identity(4)
product = m @ identity  # Matrix multiplication
m_rref = m.echelonized()  # Row echelon form
kernel = m.kernel()  # Null space basis

Key Features

BitVector

  • Create from strings, lists, or factory methods
  • Boolean operations: XOR, AND, OR
  • Hamming weight and parity computation
  • Inner product over GF(2)
  • Support (indices of set bits)

BitMatrix

  • Create from rows or factory methods
  • Matrix multiplication over GF(2)
  • Element-wise boolean operations
  • Row echelon form and reduced row echelon form
  • Null space (kernel) computation
  • Transpose and submatrix extraction

Use Cases

binar is particularly useful for:

  • Quantum error correction: Parity check matrices, stabilizer codes
  • Linear codes: Generator and check matrices over GF(2)
  • Graph theory: Adjacency matrices, graph algorithms
  • Cryptography: Linear feedback shift registers, boolean functions
  • Computational algebra: Gaussian elimination, system solving over GF(2)

Performance

Built on optimized Rust code with:

  • SIMD acceleration for bit operations
  • Cache-friendly memory layout
  • Efficient Gaussian elimination algorithms
  • Zero-copy integration between Python and Rust

Examples

Solving Linear Systems over GF(2)

import binar

# Coefficient matrix
A = binar.BitMatrix([
    "110",
    "101",
    "011"
])

# Find kernel (solutions to Ax = 0)
kernel = A.kernel()
print(f"Null space dimension: {kernel.row_count}")

# Verify solution
for row in kernel.rows:
    result = A @ row
    assert result.is_zero  # Ax = 0

Parity Check Matrix for [7,4,3] Hamming Code

import binar

# Parity check matrix for [7,4,3] Hamming code
H = binar.BitMatrix([
    "1010101",
    "0110011",
    "0001111"
])

# Check syndrome for error vector
error = binar.BitVector("0001000")  # Error on bit 3
syndrome = H @ error
print(f"Syndrome: {syndrome}")  # Points to error location

# Generate all codewords by finding kernel
codewords = H.kernel()
print(f"Code dimension: {codewords.row_count}")  # 4

API Reference

See the type stubs file for complete API documentation with type hints.

Related Packages

  • paulimer: Pauli and Clifford algebra built on binar

License

MIT License - See LICENSE file for details.

Contributing

Contributions welcome! See github.com/microsoft/qdk-ec for guidelines.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

binar-0.1.0-cp39-abi3-win_arm64.whl (209.5 kB view details)

Uploaded CPython 3.9+Windows ARM64

binar-0.1.0-cp39-abi3-win_amd64.whl (224.8 kB view details)

Uploaded CPython 3.9+Windows x86-64

binar-0.1.0-cp39-abi3-manylinux_2_34_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.34+ x86-64

binar-0.1.0-cp39-abi3-manylinux_2_34_aarch64.whl (2.8 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.34+ ARM64

binar-0.1.0-cp39-abi3-macosx_11_0_arm64.whl (316.7 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

binar-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl (326.0 kB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

Details for the file binar-0.1.0-cp39-abi3-win_arm64.whl.

File metadata

  • Download URL: binar-0.1.0-cp39-abi3-win_arm64.whl
  • Upload date:
  • Size: 209.5 kB
  • Tags: CPython 3.9+, Windows ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: RestSharp/106.13.0.0

File hashes

Hashes for binar-0.1.0-cp39-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 bce64d6a852adb1bdb5d29b67993b20b5a25c8c6649f78c841b1afd97c70742f
MD5 1156aef9a4d5f422cbb74cb6fb4c7dd2
BLAKE2b-256 f389c452ad7d000f27dfc443407757cc3d8c1b3a93de6b3419e88db1ca9d3fd1

See more details on using hashes here.

File details

Details for the file binar-0.1.0-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: binar-0.1.0-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 224.8 kB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: RestSharp/106.13.0.0

File hashes

Hashes for binar-0.1.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 3a02fee8e6ade5f14a3d8e0ca409e40dcfc861555d49b1191220481c82fba113
MD5 861b36874a1b25c3f0b1b4cfd7edc16f
BLAKE2b-256 3b54813dbddbfb41ed6d0518394a653319099c57cf0afee1f158b055c9d0da66

See more details on using hashes here.

File details

Details for the file binar-0.1.0-cp39-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for binar-0.1.0-cp39-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 85e79d11c450ae6148cdfa3c8a302ba17e8eb9bf4a7040bb095b2d2b57f8f9a3
MD5 b7f57161b35df00f68803132d69f276c
BLAKE2b-256 f945c5d699bebd6918814918648b7c2d39ca96008a3839f33ed0376c20988c4e

See more details on using hashes here.

File details

Details for the file binar-0.1.0-cp39-abi3-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for binar-0.1.0-cp39-abi3-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 b8e12f67d04accdba9c7397d8dc95e361d998623a0f7ba7d5d6ea5382315d271
MD5 35fc212e1bcfdd32a3f8f3974bd4add2
BLAKE2b-256 5b3e3f06395a5ab5f6a0b6b803c1c99355cbecb852ad5bad5af64704a54b223f

See more details on using hashes here.

File details

Details for the file binar-0.1.0-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for binar-0.1.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f0fb32a84395ccfcbc3ad3e506dbf42ab01a38c1f1b5cab91f56aa7a4fc2aa94
MD5 e08a99bd4ca910affc431584d2bb617b
BLAKE2b-256 250bafde5729e0a9f7e09107a4591e6e9b6f47bd62017c4b4beb1c206b2c01b0

See more details on using hashes here.

File details

Details for the file binar-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for binar-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a60011125381ff1ff8318acfb032982a05e3c32eb2b960f67cec50be1cb4f18a
MD5 3850fa936e11e14fe3e2e3f2d3348ba7
BLAKE2b-256 57dede2d9470a967d1b8172e9d97b0272836e3628002d25c4cdbe36f3ac0ff3f

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