Skip to content

Generic Encode and API Update - #194

Merged
MIWdlB merged 9 commits into
mainfrom
python-api
Jun 11, 2026
Merged

Generic Encode and API Update#194
MIWdlB merged 9 commits into
mainfrom
python-api

Conversation

@MIWdlB

@MIWdlB MIWdlB commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

Update to the python and rust APIs. Diff looks big but it's mostly moving and deleting files, creating wrappers and re-running notebooks.

Makes the Encode trait generic over output type by introducing the idea of spaces as marker traits. This should make it possible to generically define encodings from fermionic inputs to qubit outputs.

Moves QubitHamiltonian, FermionHamiltonian and MajoranaEncoding to be pyclass pyo3 wrappers.

The significant difference in terms of user experience, is that TernaryTree is now a builder for MajoranaEncoding rather than a subclass, which was an early choice to simplify things for myself and not the best model.

MIWdlB and others added 8 commits June 10, 2026 10:12
- New ferrmion_core::hamiltonians::FermionHamiltonian aggregating
  FermionMatrix terms with a constant energy offset, converting to
  MajoranaSparse via the existing FermionSparse chain.
- FermionMatrix gains Clone/Debug/PartialEq and signature/action/
  coefficients/n_modes accessors.
- MajoranaEncoding gains Clone and PartialEq for use in pyclass wrappers.
- SymplecticMatrix::to_concatenated() for the [x|z] numpy layout.
- Fix missing Clifford import in pauli.rs (pre-existing build breakage).

https://claude.ai/code/session_01NYJx6y4n3rdbAgjEuVmM56
Replace the duplicated Python type layer with PyO3 pyclass wrappers over
the ferrmion-core types:

- New QubitHamiltonian pyclass (mapping protocol, pauli weights,
  clifford_heuristic / randomised_subsystem_descent methods, pickling).
- New FermionHamiltonian pyclass (fluent builder, terms dict, sparse
  Majorana conversion, pickling).
- New frozen MajoranaEncoding pyclass with factory staticmethods
  (jordan_wigner/bravyi_kitaev/parity/jkmn/maxnto/from_flatpack/from_json)
  and encode/encode_annealed/anneal_enumeration/decode/hartree_fock_state/
  operator-construction/batch_pauli_weights/apply_mode_enumeration methods.
- src/lib.rs split into error.rs, hamiltonians.rs, encoding.rs and
  functions.rs; module registration only in lib.rs.
- Superseded loose core functions removed; hatt/topphatt/encode_topphatt
  now take FermionHamiltonian and return encoding pyclasses.
- Python package migrated: encode/base.py and encode/maxnto.py deleted,
  TernaryTree is now a pure builder with build_encoding(), factories
  return MajoranaEncoding, qiskit/visualise/optimize consumers updated,
  core.pyi stubs rewritten.

https://claude.ai/code/session_01NYJx6y4n3rdbAgjEuVmM56
- tests/test_base_encoding.py replaced by tests/test_majorana_encoding.py
  exercising the MajoranaEncoding/QubitHamiltonian/FermionHamiltonian
  pyclasses directly (constructor validation, JSON and pickle round-trips,
  mapping protocol, operator construction, mode-enumeration permutations).
- Remaining test files migrated off the removed core functions:
  encode/batch_pauli_weights/anneal_enumerations/encode_standard/
  maxnto_symplectic_matrix/flatpack_symplectic_matrix/
  fermionic_to_sparse_majorana all replaced by class methods; tree-based
  tests use the TernaryTree classmethod constructors.
- CHANGELOG.md documents the new classes and all breaking changes.
- Fix pre-existing clippy/bench breakage (module_inception allow, double
  unwrap in decode bench) and apply cargo fmt/ruff format.

https://claude.ai/code/session_01NYJx6y4n3rdbAgjEuVmM56
Update the tutorial notebooks and nbutils.py helper for the new
Rust-backed classes (code cells only; notebooks are not re-executed and
stored outputs are kept, as docs builds use nb_execution_mode='off'):

- Tree-building call sites switch from the encoding factories to the
  TernaryTree classmethods (jordan_wigner/bravyi_kitaev/parity/jkmn)
  wherever the object is drawn, given an enumeration scheme, or run
  through TOPP-HATT.
- encode_annealed/encode_topphatt results unpacked as
  (QubitHamiltonian, MajoranaEncoding) tuples; clifford_heuristic's
  get_ch_results threads the TOPP-HATT-optimised encoding through the
  downstream comparisons instead of relying on in-place mutation.
- MajoranaStringEncoding JSON round-trip replaced with
  MajoranaEncoding.to_json/from_json, saving the annealed encoding that
  is actually compared (fixes the stored-error cell, output cleared).
- fermionic_to_sparse_majorana calls replaced with
  FermionHamiltonian(...).to_sparse_majorana(); _terms -> terms;
  private _build_symplectic_matrix/_batch_pauli_weights/
  _encode_fermion_product usages replaced with the public equivalents,
  generating explicit random permutations for batch weights.

https://claude.ai/code/session_01NYJx6y4n3rdbAgjEuVmM56
PyO3 converts Vec<u8> to Python bytes, so the ipowers entry was not a
list and the dict was not JSON-serialisable. Widen to u32 before
conversion so a list of ints is produced, and extend the round-trip
test to assert list types and a full json.dumps/json.loads cycle.

https://claude.ai/code/session_01NYJx6y4n3rdbAgjEuVmM56
@review-notebook-app

Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@MIWdlB
MIWdlB force-pushed the python-api branch 2 times, most recently from 4b9f12a to ec0a929 Compare June 11, 2026 12:52
@codspeed-hq

codspeed-hq Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will improve performance by 10.48%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 8 improved benchmarks
🆕 48 new benchmarks
⏩ 64 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
test_benchmark_batch_pauli_weights[1000-JordanWigner] 9.2 s 8.1 s +13.15%
test_benchmark_batch_pauli_weights[1000-BravyiKitaev] 9.2 s 8.1 s +13.15%
test_benchmark_batch_pauli_weights[500-BravyiKitaev] 4.6 s 4.1 s +13%
test_benchmark_batch_pauli_weights[500-JordanWigner] 4.6 s 4.1 s +12.98%
test_benchmark_batch_pauli_weights[100-BravyiKitaev] 969.3 ms 866.9 ms +11.82%
test_benchmark_batch_pauli_weights[100-JordanWigner] 968.8 ms 866.8 ms +11.76%
test_benchmark_batch_pauli_weights[10-BravyiKitaev] 149.5 ms 143.5 ms +4.23%
test_benchmark_batch_pauli_weights[10-JordanWigner] 149.5 ms 143.4 ms +4.21%
🆕 test_benchmark_decode_zbasis_ensemble[128-bravyi_kitaev] N/A 2.1 s N/A
🆕 test_benchmark_decode_zbasis_ensemble[128-jkmn] N/A 2.1 s N/A
🆕 test_benchmark_decode_zbasis_ensemble[128-jordan_wigner] N/A 2.1 s N/A
🆕 test_benchmark_decode_zbasis_ensemble[128-parity] N/A 2.1 s N/A
🆕 test_benchmark_decode_zbasis_ensemble[32-bravyi_kitaev] N/A 159.6 ms N/A
🆕 test_benchmark_decode_zbasis_ensemble[32-jkmn] N/A 159.5 ms N/A
🆕 test_benchmark_decode_zbasis_ensemble[32-jordan_wigner] N/A 159.3 ms N/A
🆕 test_benchmark_decode_zbasis_ensemble[32-parity] N/A 159.5 ms N/A
🆕 test_benchmark_decode_zbasis_ensemble[64-bravyi_kitaev] N/A 557.2 ms N/A
🆕 test_benchmark_decode_zbasis_ensemble[64-jkmn] N/A 557.3 ms N/A
🆕 test_benchmark_decode_zbasis_ensemble[64-jordan_wigner] N/A 557.8 ms N/A
🆕 test_benchmark_decode_zbasis_ensemble[64-parity] N/A 558.9 ms N/A
... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing python-api (5b3c86e) with main (5da966b)

Open in CodSpeed

Footnotes

  1. 64 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
MIWdlB force-pushed the python-api branch 2 times, most recently from 85f649b to b3c769f Compare June 11, 2026 13:46
@MIWdlB
MIWdlB merged commit 07ce869 into main Jun 11, 2026
4 checks passed
@MIWdlB
MIWdlB deleted the python-api branch June 11, 2026 13:56
This was referenced Jun 11, 2026
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.

2 participants