Closed ziofil closed 2 weeks ago
PR-Agent was enabled for this repository. To continue using it, please link your git user with your CodiumAI identity here.
Here are some key observations to aid the review process:
β±οΈ Estimated effort to review: 3 π΅π΅π΅βͺβͺ |
π Score: 85 |
π§ͺ PR contains tests |
π No security concerns identified |
β‘ Recommended focus areas for review Performance Concern The `sample_prob_dist` method is now duplicating functionality from `sample`. Consider removing one of these methods or refactoring to avoid duplication. Potential Bug The reshaping of `quad` might not work correctly for all input shapes. Verify that this change doesn't break existing functionality. |
PR-Agent was enabled for this repository. To continue using it, please link your git user with your CodiumAI identity here.
Explore these optional code suggestions:
Category | Suggestion | Score |
Best practice |
β Improve consistency and clarity of multidimensional array creation___Suggestion Impact:The suggestion to use np.meshgrid with indexing='ij' was implemented, ensuring consistent behavior and matching the expected output shape. code diff: ```diff - quad = np.array(quad) - if len(quad.shape) != 1 and len(quad.shape) != self.n_modes: - raise ValueError( - "The dimensionality of quad should be 1, or match the number of modes." - ) - - if len(quad.shape) == 1: - quad = math.astensor(np.meshgrid(*[quad] * len(self.modes))).T.reshape( - -1, len(self.modes) - ) - - if isinstance(self, Ket): - return math.abs(self.quadrature(quad, phi)) ** 2 - else: - quad = math.tile(quad, (1, 2)) ```np.meshgrid with indexing='ij' to ensure consistent behavior across different numpy versions and to match the expected output shape.** [mrmustard/lab_dev/states/base.py [378-380]](https://github.com/XanaduAI/MrMustard/pull/505/files#diff-782e8b404915d7267d5cc991ba0b8c52926808d9005e7b45fdb98da00dfa9f00R378-R380) ```diff -quad = math.astensor(np.meshgrid(*[quad] * len(self.modes))).T.reshape( - -1, len(self.modes) -) +quad = math.astensor(np.meshgrid(*[quad] * len(self.modes), indexing='ij')).reshape( + len(self.modes), -1 +).T ``` - [ ] **Apply this suggestion** Suggestion importance[1-10]: 7Why: Using `np.meshgrid` with `indexing='ij'` ensures consistent behavior across different numpy versions and matches the expected output shape, improving code clarity and robustness. | 7 |
Improve code readability by using more descriptive variable names___ **Consider using a more descriptive variable name instead ofG for the Ggate object to improve code readability.** [tests/test_math/test_compactFock.py [109-115]](https://github.com/XanaduAI/MrMustard/pull/505/files#diff-e768fecde2483177fd28c19adbf8366f829fc52e592281f14bdab77400291d73R109-R115) ```diff -G = Ggate(num_modes=1, symplectic_trainable=True) +gaussian_gate = Ggate(num_modes=1, symplectic_trainable=True) def cost_fn(): n1 = 2 # number of detected photons - state_opt = Vacuum(1) >> G + state_opt = Vacuum(1) >> gaussian_gate A, B, G0 = wigner_to_bargmann_rho(state_opt.cov, state_opt.means) ``` - [ ] **Apply this suggestion** Suggestion importance[1-10]: 6Why: Using a more descriptive variable name instead of `G` for the `Ggate` object enhances code readability, making it easier for others to understand the code's purpose. | 6 | |
Enhancement |
Enhance test coverage by parameterizing cutoff values___ **Consider usingpytest.mark.parametrize for the cutoffs parameter to test different cutoff values, improving test coverage.** [tests/test_math/test_compactFock.py [42-51]](https://github.com/XanaduAI/MrMustard/pull/505/files#diff-e768fecde2483177fd28c19adbf8366f829fc52e592281f14bdab77400291d73R42-R51) ```diff -with settings(PRECISION_BITS_HERMITE_POLY=precision): - cutoffs = (5, 5, 5) +@pytest.mark.parametrize("cutoffs", [(5, 5, 5), (3, 4, 5), (7, 7, 7)]) +def test_compactFock_diagonal(precision, A_B_G0, cutoffs): + with settings(PRECISION_BITS_HERMITE_POLY=precision): + A, B, G0 = A_B_G0 # Create random state (M mode Gaussian state with displacement) - A, B, G0 = A_B_G0 # Create random state (M mode Gaussian state with displacement) + # Vanilla MM + G_ref = math.hermite_renormalized( + math.conj(-A), math.conj(B), math.conj(G0), shape=list(cutoffs) * 2 + ) # note: shape=[C1,C2,C3,...,C1,C2,C3,...] + G_ref = math.asnumpy(G_ref) - # Vanilla MM - G_ref = math.hermite_renormalized( - math.conj(-A), math.conj(B), math.conj(G0), shape=list(cutoffs) * 2 - ) # note: shape=[C1,C2,C3,...,C1,C2,C3,...] - G_ref = math.asnumpy(G_ref) - ``` - [ ] **Apply this suggestion** Suggestion importance[1-10]: 6Why: Parameterizing the `cutoffs` values using `pytest.mark.parametrize` can improve test coverage by testing different scenarios, making the tests more comprehensive. | 6 |
Simplify random sampling by using numpy's random choice function directly___ **Consider usingnp.random.choice directly instead of creating a separate rng object. This can simplify the code and potentially improve performance.** [mrmustard/lab_dev/samplers.py [125-134]](https://github.com/XanaduAI/MrMustard/pull/505/files#diff-4d9070f5ff009cacc13f5f4d76b6c28c4c3801a3d641ebfce8d31c5824b4f895R125-R134) ```diff -rng = np.random.default_rng(seed) if seed else settings.rng probs = self.probabilities(state) meas_outcomes = list(product(self.meas_outcomes, repeat=len(state.modes))) -samples = rng.choice( +samples = np.random.choice( a=meas_outcomes, p=probs, size=n_samples, + replace=True, ) ``` - [ ] **Apply this suggestion** Suggestion importance[1-10]: 5Why: The suggestion to use `np.random.choice` directly instead of creating a separate `rng` object could simplify the code. However, it may not significantly improve performance, and the current approach allows for more flexibility with seeding. | 5 |
π‘ Need additional feedback ? start a PR chat
All modified and coverable lines are covered by tests :white_check_mark:
Project coverage is 89.41%. Comparing base (
67b8624
) to head (b527f92
). Report is 1 commits behind head on develop.
User description
Added some small changes here and there which considerably speed up the TF tests for the sampler and somewhat for the diagonal Fock strategies. For the sampler I improved the code, for the fock strategies I simplified the tests.
PR Type
enhancement, tests
Description
samplers.py
, improving code clarity and performance.states/base.py
by optimizing array handling.state.py
.Changes walkthrough π
state.py
Clean up code by removing debugging comments
mrmustard/lab/abstract/state.py - Removed commented-out debugging code.
samplers.py
Refactor and optimize sampling methods
mrmustard/lab_dev/samplers.py
sample_prob_dist
function.math.sum
.sample
method to usesample_prob_dist
.base.py
Optimize quadrature distribution computation
mrmustard/lab_dev/states/base.py
quad
to usenp.array
.quad
.test_compactFock.py
Simplify and optimize compact Fock strategy tests
tests/test_math/test_compactFock.py