Accept MajoranaSparse in MajoranaEncoding.encode - #216
Merged
Conversation
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>
Contributor
Merging this PR will not alter performance
Comparing Footnotes
|
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>
Merged
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds pyo3 wrapper to allow passing existing PyMajoranaSparse to PyMajoranaEncoding.