Skip to content

Refactor RearrangementGenerator load/store methods to reduce complexity and duplication #858

@coderabbitai

Description

@coderabbitai

Context

This issue tracks a follow-up refactoring task identified during review of PR #848.

Problem

The loadRowByRow, loadColumnByColumn, storeRowByRow, and storeColumnByColumn methods in src/na/zoned/code_generator/CodeGenerator.cpp are quite large (100-300 lines each) and contain substantial duplicated structure:

  • Row vs. column variants share similar logic
  • Storage vs. entanglement handling has near-duplicate patterns
  • "Pack additional rows/columns if mapped sets match" logic is repeated
  • Virtual offset and neighbor-pushing logic appears across all four methods

This complexity makes it hard to reason about correctness and has contributed to subtle bugs in the past.

Proposed Solution

Extract shared patterns into reusable helpers, for example:

  1. buildCoordinateToAodIndex - Common mapping creation from keys to AOD indices
  2. collectQubitsPerLine - Materialize x/y → set
  3. applyVirtualOffsetAndPushNeighbors - Handle virtual offset insertion and neighboring push logic with min/max bounds, parameterized by axis and site kind
  4. packCompatibleLines - Merge additional adjacent lines when isSameMappedSet matches

Then refactor the four large methods to call these helpers with axis/site-specific lambdas.

Requirements

  • Observable behavior must remain identical
  • Add focused unit tests for each helper (especially neighbor-push and packing)
  • Reduce duplication between row/column variants
  • Lower the chance of future copy-paste errors

References

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions