Skip to content

Conversation

@viiccwen
Copy link
Contributor

Purpose of PR

Refactor QdpEngine.encode() method to improve code maintainability and readability by splitting the monolithic 150+ line method into smaller and focused helper methods.

This PR also adds/refactors tests to ensure all adjust code paths are covered.

Related Issues or PRs

comment

Changes Made

  • Bug fix
  • New feature
  • Refactoring
  • Documentation
  • Test
  • CI/CD pipeline
  • Other

Breaking Changes

  • Yes
  • No

Detailed Changes

Code Refactoring (src/lib.rs)

  1. Split encode() method into focused helper methods:

    • encode_from_numpy() - Handles NumPy array input (1D and 2D)
    • encode_from_pytorch() - Handles PyTorch tensor input (1D and 2D)
    • encode_from_list() - Handles Python list input
    • Main encode() method now acts as a router, delegating to appropriate handlers
  2. Benefits:

    • Improved code readability (each method has a single responsibility)
    • Easier to maintain and modify individual input type handlers
    • Better testability (each helper method can be tested independently)
    • Reduced code duplication

Test Improvements (tests/test_bindings.py)

  1. Added 3 tests for refactored paths:

    • test_encode_numpy_array() - Tests NumPy array encoding (1D and 2D)
    • test_encode_pytorch_tensor() - Tests PyTorch tensor encoding (1D and 2D)
    • test_encode_pathlib_path() - Tests pathlib.Path object input
  2. Code quality improvements:

    • Extracted test data constants (TEST_DATA_1D, NUM_QUBITS, SAMPLE_SIZE)
    • Added pytest fixtures (engine, engine_float64) to reduce duplication
    • Simplified test functions by using fixtures and constants

Testing

  • All existing tests pass
  • New tests added for all refactored code paths
  • Tests verify both 1D and 2D inputs for NumPy arrays and PyTorch tensors
  • Tests verify pathlib.Path object handling

Checklist

  • Added or updated unit tests for all changes
  • Added or updated documentation for all changes
  • Successfully built and ran all unit tests or manual tests locally
  • PR title follows "MAHOUT-XXX: Brief Description" format (if related to an issue)
  • Code follows ASF guidelines

@viiccwen viiccwen changed the title Refactor 1d2d shape matching [QDP] Refactor encode() method into helper functions with tests Jan 12, 2026
@viiccwen
Copy link
Contributor Author

cc @400Ping, @guan404ming 🙌

@400Ping
Copy link
Contributor

400Ping commented Jan 14, 2026

Please resolve conflicts

@ryankert01 ryankert01 self-requested a review January 14, 2026 19:03
@rich7420
Copy link
Contributor

plz resolve the pre-commit error

@ryankert01
Copy link
Member

please resolve the conflict and re-think the solution with newly introduced testing refactor if needed

@viiccwen
Copy link
Contributor Author

please resolve the conflict and re-think the solution with newly introduced testing refactor if needed

Sure, I'll kick off today or tomorrow!

@guan404ming
Copy link
Member

Hey @viiccwen, how this going? Feel free let me know if you need any help. Thanks!

@viiccwen
Copy link
Contributor Author

viiccwen commented Jan 20, 2026

replying here!
#881 (comment)

I'll dive into refactor encode (qdp/qdp-python/src/lib.rs) after this PR merged.

Edit: bcz this file updated frequently.

@guan404ming
Copy link
Member

Make sense, thanks.

@guan404ming guan404ming added this to the Qumat 0.5.0 milestone Jan 20, 2026
Copy link
Member

@ryankert01 ryankert01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to resolve conflicts

- Introduced dedicated methods `encode_from_numpy` and `encode_from_pytorch` to streamline the encoding process for NumPy arrays and PyTorch tensors, respectively.
- Improved error handling for unsupported shapes in both encoding methods.
- Simplified the main encoding logic by delegating to these new methods.
Replace deprecated PyReadonlyArrayDyn with dimension-specific types
PyReadonlyArray1 and PyReadonlyArray2 for better type safety and
compatibility with newer pyo3-numpy versions.
@viiccwen viiccwen force-pushed the refactor-1d2d-shape-matching branch from 496fb7c to 5a22fa0 Compare January 22, 2026 11:09
Copy link
Contributor

@rich7420 rich7420 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@viiccwen thanks for the update!
Tests all passed locally.
left some comments:

Comment on lines +252 to +255
/// CUDA device ID from DLPack metadata.
/// Currently unused but kept for potential future device validation or multi-GPU support.
#[allow(dead_code)]
device_id: i32,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a dead code but may be used in the future.
I think maybe we could add a defensive assertion to verify DLPack device_id matches the validated device_id.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, it could be a follow-up. WDYT?
bcz this PR only deals with refactoring encode path & adding missing tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no problem, plz open a issue.

@viiccwen
Copy link
Contributor Author

another conflict 💀

@guan404ming guan404ming merged commit 11db508 into apache:main Jan 23, 2026
6 checks passed
@viiccwen
Copy link
Contributor Author

Thanks for reviewing! 🙌

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.

5 participants