Skip to content
This repository has been archived by the owner on Oct 27, 2023. It is now read-only.

Add test breaking applyMultiQubitOp and then fix it. #75

Merged
merged 19 commits into from
Aug 21, 2023

Conversation

vincentmr
Copy link
Contributor

@vincentmr vincentmr commented Aug 14, 2023

Before submitting

Please complete the following checklist when submitting a PR:

  • All new features must include a unit test.
    If you've fixed a bug or added code that should be tested, add a test to the
    tests directory!

  • All new functions and code must be clearly commented and documented.
    If you do make documentation changes, make sure that the docs build and
    render correctly by running make docs.

  • Ensure that the test suite passes, by running make test.

  • Add a new entry to the .github/CHANGELOG.md file, summarizing the
    change, and including a link back to the PR.

  • Ensure that code is properly formatted by running make format.

When all the above are checked, delete everything above the dashed
line and fill in the pull request template.


Context:
The multiQubitOpFunctor functor attributes indices and coeffs_in are used as local variables, causing issues in parallel for loops. Managed view (standard) declaration is considered a host function, and hence simply declaring local variables solves the parallel host implementation, but not the parallel device implementation.

Description of the Change:
Add breaking test in Test_StateVectorKokkos_NonParam.cpp. Fix it using a TeamPolicy instead of a RangePolicy as a parallelization policy for multiQubitOpFunctor. Use the TeamPolicy scratch space as a temporary variable. Add a second level of parallelism (indices & matrix product) in passing. Instead of fixing the same bug in getExpectationValueMultiQubitOpFunctor, we simply change the implementation to call multiQubitOpFunctor and then take the inner product.

Benefits:
Bug fix, more parallelism.

Possible Drawbacks:
Possibly slower in certain cases.

Related GitHub Issues:
PR 25

… an unmanage scratch view for tmp variables and parallelize both the matrix product and the loop over basis states.
@vincentmr vincentmr marked this pull request as ready for review August 14, 2023 18:27
@multiphaseCFD
Copy link
Member

Thanks @vincentmr ! It looks like it still needs some updates to let macos py tests pass.

@vincentmr
Copy link
Contributor Author

Thanks @vincentmr ! It looks like it still needs some updates to let macos py tests pass.

Yes, strange I don't think I did anything to break them ...

@vincentmr
Copy link
Contributor Author

This PR is breaking the Lightning CI.

Copy link
Member

@multiphaseCFD multiphaseCFD left a comment

Choose a reason for hiding this comment

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

Thanks @vincentmr ! I'm curious about the performance of the new implementation.

@vincentmr
Copy link
Contributor Author

vincentmr commented Aug 15, 2023

Thanks @vincentmr ! I'm curious about the performance of the new implementation.

I quick check with the script below (Kokkos+SERIAL+OPENMP+CUDA_AMPERE80) yields on a A100 card (Perlmutter)

  • main Time: 1.248e-01 (s)
  • bugfix/multiqubitop Time: 9.571e-02 (s)
# %% Import PennyLane
import pennylane as qml
import pennylane.numpy as np

n_wires = 28
n_uni = 3
n_layers = 10
obs = [qml.PauliZ(i) for i in range(n_wires)]
obs = qml.operation.Tensor(*obs)
dev = qml.device('lightning.kokkos', wires=n_wires)
    
U = np.random.rand(2**n_uni, 2**n_uni) + 1j * np.random.rand(2**n_uni, 2**n_uni)
@qml.qnode(dev)
def example_circuit():
    qml.QubitUnitary(U, wires=range(n_uni))
    return qml.expval(qml.PauliZ(0))

# %%
import time
repeat = 100

t0 = time.time()
for _ in range(repeat):
    example_circuit()
print(f"Time: {(time.time()-t0)/repeat:1.3e} (s)")

@vincentmr vincentmr requested a review from mlxd August 15, 2023 18:37
Copy link
Contributor

@AmintorDusko AmintorDusko left a comment

Choose a reason for hiding this comment

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

I left some comments to keep track of what we need to change back before merging.
Let me know when a bugfix comes on Pennylane so I will give a final review here.

.github/workflows/tests_linux.yml Outdated Show resolved Hide resolved
.github/workflows/tests_linux.yml Outdated Show resolved Hide resolved
.github/workflows/tests_linux.yml Outdated Show resolved Hide resolved
.github/workflows/tests_linux.yml Outdated Show resolved Hide resolved
.github/workflows/tests_linux.yml Outdated Show resolved Hide resolved
.github/workflows/tests_linux_x86_nvidia_gpu.yml Outdated Show resolved Hide resolved
.github/workflows/tests_macos.yml Outdated Show resolved Hide resolved
.github/workflows/wheel_linux_x86_64.yml Outdated Show resolved Hide resolved
.github/workflows/wheel_macos_arm64.yml Outdated Show resolved Hide resolved
.github/workflows/wheel_macos_x86_64.yml Outdated Show resolved Hide resolved
Copy link
Contributor

@AmintorDusko AmintorDusko left a comment

Choose a reason for hiding this comment

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

Nothing more to add. Good job on this!

Copy link
Member

@multiphaseCFD multiphaseCFD left a comment

Choose a reason for hiding this comment

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

Thanks @vincentmr for the nice work!

Copy link
Member

@mlxd mlxd left a comment

Choose a reason for hiding this comment

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

Thanks @vincentmr
Feel free to merge when ready

@vincentmr vincentmr merged commit c91184e into main Aug 21, 2023
28 checks passed
@vincentmr vincentmr deleted the bugfix/multiqubitop branch August 21, 2023 13:19
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants