Skip to content

Accept MajoranaSparse in MajoranaEncoding.encode - #216

Merged
MIWdlB merged 3 commits into
mainfrom
claude/pymajoranaencoding-wrapper-3e2a09
Aug 6, 2026
Merged

Accept MajoranaSparse in MajoranaEncoding.encode#216
MIWdlB merged 3 commits into
mainfrom
claude/pymajoranaencoding-wrapper-3e2a09

Conversation

@MIWdlB

@MIWdlB MIWdlB commented Aug 6, 2026

Copy link
Copy Markdown
Collaborator

Adds pyo3 wrapper to allow passing existing PyMajoranaSparse to PyMajoranaEncoding.

The Majorana representation was already the internal encoding path:
encode() converted a FermionHamiltonian via to_majorana_sparse() and
dispatched to Encode<&MajoranaSparse, QubitHamiltonian>. Callers already
holding a MajoranaSparse had to round-trip through a FermionHamiltonian
to reach it.

encode() now takes either type. Python has no overload resolution and
#[pymethods] cannot expose a generic method, so the two inputs are
dispatched at runtime through an untagged FromPyObject enum. Both
already produce a QubitHamiltonian, so the signature stays uniform.

MajoranaSparse carries no mode count and the core encode indexes
`operators` unguarded, so out-of-range indices are rejected up front
rather than panicking inside a rayon worker. Terms are ordered
lexicographically rather than by magnitude, so the whole index set is
scanned.

The encode parameter is renamed fham -> operator, as it is no longer
restricted to a FermionHamiltonian. TernaryTree.encode_naive is widened
to match.

Bumps the version to 0.13.0.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@codspeed-hq

codspeed-hq Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 56 untouched benchmarks
⏩ 100 skipped benchmarks1


Comparing claude/pymajoranaencoding-wrapper-3e2a09 (7daefee) with main (b6bc7ef)

Open in CodSpeed

Footnotes

  1. 100 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

MIWdlB and others added 2 commits August 6, 2026 16:24
Encodes a single product of Majorana operators, the Majorana counterpart
of encode_fermion_product. Majorana operators are hermitian, so unlike
the ladder-operator case the product is described by its indices alone
and takes no signature.

Operators are multiplied in the order given rather than canonicalised:
the symplectic product already tracks the phase from each multiplication,
so reordering would silently change the caller's operator. Indices are
bounded by 2 * n_modes rather than n_modes, and out-of-range values are
rejected up front to keep them away from the unguarded row indexing in
the core encode.

Exposed directly as a pymethod rather than split into a private _impl
helper, as encode_product_impl is, since it has a single caller.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…Tree

A Majorana product always encodes to exactly one Pauli term, so wrapping
it in a QubitHamiltonian made callers unpack a one-entry map. Return the
(pauli_string, coefficient) pair directly instead.

Also adds TernaryTree.encode_majorana_product, which builds the encoding
on demand, so callers no longer reach into the private _encoding
attribute to get at it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@MIWdlB
MIWdlB merged commit 28c8c0b into main Aug 6, 2026
4 of 7 checks passed
@MIWdlB MIWdlB mentioned this pull request Aug 10, 2026
MIWdlB added a commit that referenced this pull request Aug 10, 2026
* Consolidate to_sparse_majorana into to_majorana_sparse

Remove the duplicate `to_sparse_majorana()` method and add `to_dict()` to
`PyMajoranaSparse` to provide the same dict-like functionality. This simplifies
the API by having a single conversion method that can return either the
structured `PyMajoranaSparse` object or a dict via `.to_dict()`.

- Remove `to_sparse_majorana()` from FermionHamiltonian
- Add `to_dict()` method to PyMajoranaSparse that returns simplified terms
- Update test callers to use `to_majorana_sparse().to_dict()`
- Update comment in functions.rs to reference new method

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>

* Accept MajoranaSparse in remaining FermionHamiltonian-only encoding methods

Widens MajoranaEncoding.encode_annealed, .anneal_enumeration,
.batch_pauli_weights, the hatt and encode_topphatt module functions, and
their Python wrappers (TernaryTree.encode_annealed/.topphatt/.encode,
hamiltonian_adaptive_ternary_tree) to accept a MajoranaSparse directly,
matching the pattern already used by MajoranaEncoding.encode (#216).

Also fixes TernaryTree.encode, whose type hint already claimed to accept
MajoranaSparse but would raise a TypeError at the pyo3 boundary since
encode_topphatt only accepted FermionHamiltonian.

PyMajoranaSparse now carries the source Hamiltonian's n_modes, since the
core MajoranaSparse type has no mode count of its own and several of these
methods need one to compute defaults (temperature, n_modes). This also lets
the ad-hoc index-range scan in MajoranaEncoding.encode be replaced with a
direct mode-count comparison, mirroring the check already used for
FermionHamiltonian inputs.

Also removes the to_sparse_majorana stub left behind in core.pyi from the
prior consolidation onto to_majorana_sparse, and adds the missing to_dict
stub for MajoranaSparse.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>

* 0.13.1

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Haiku 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant