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:
buildCoordinateToAodIndex - Common mapping creation from keys to AOD indices
collectQubitsPerLine - Materialize x/y → set
applyVirtualOffsetAndPushNeighbors - Handle virtual offset insertion and neighboring push logic with min/max bounds, parameterized by axis and site kind
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
Context
This issue tracks a follow-up refactoring task identified during review of PR #848.
Problem
The
loadRowByRow,loadColumnByColumn,storeRowByRow, andstoreColumnByColumnmethods insrc/na/zoned/code_generator/CodeGenerator.cppare quite large (100-300 lines each) and contain substantial duplicated structure: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:
buildCoordinateToAodIndex- Common mapping creation from keys to AOD indicescollectQubitsPerLine- Materialize x/y → setapplyVirtualOffsetAndPushNeighbors- Handle virtual offset insertion and neighboring push logic with min/max bounds, parameterized by axis and site kindpackCompatibleLines- Merge additional adjacent lines when isSameMappedSet matchesThen refactor the four large methods to call these helpers with axis/site-specific lambdas.
Requirements
References