Skip to content

[WIP] Operators and Integration with cudensitymat#2407

Closed
sacpis wants to merge 248 commits intoNVIDIA:mainfrom
sacpis:dynamics_cpp_operators
Closed

[WIP] Operators and Integration with cudensitymat#2407
sacpis wants to merge 248 commits intoNVIDIA:mainfrom
sacpis:dynamics_cpp_operators

Conversation

@sacpis
Copy link
Collaborator

@sacpis sacpis commented Nov 25, 2024

Adding missing operations in operators

Co-authored with @anthony-santana.

@sacpis sacpis requested review from 1tnguyen and bettinaheim and removed request for 1tnguyen and bettinaheim November 25, 2024 04:48
@@ -0,0 +1,495 @@
/*******************************************************************************
Copy link
Collaborator

Choose a reason for hiding this comment

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

Very excited for this feature. I appreciate all the work you two have done on this one. One thought here -

Operators are more broad that dynamics. Ideally this would be part of a cudaq/operators.h public header. Did we ever finish the language review for this feature? Would be good to get a meeting scheduled.

@copy-pr-bot
Copy link

copy-pr-bot bot commented Dec 3, 2024

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@sacpis sacpis closed this Jan 9, 2025
github-actions bot pushed a commit that referenced this pull request Jan 9, 2025
@github-actions
Copy link

github-actions bot commented Jan 9, 2025

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Jan 9, 2025
@sacpis sacpis reopened this Jan 13, 2025
@sacpis sacpis changed the title [WIP] Adding missing operations in operators [WIP] Operators and Integration with cudensitymat Jan 23, 2025
Comment on lines +47 to +57
dynamics/scalar_ops_simple.cpp
dynamics/scalar_ops_arithmetic.cpp
dynamics/elementary_ops_simple.cpp
dynamics/elementary_ops_arithmetic.cpp
dynamics/product_operators_arithmetic.cpp
dynamics/test_runge_kutta_time_stepper.cpp
dynamics/test_runge_kutta_integrator.cpp
dynamics/test_helpers.cpp
dynamics/rydberg_hamiltonian.cpp
dynamics/test_cudm_helpers.cpp
dynamics/test_cudm_state.cpp
dynamics/test_cudm_time_stepper.cpp
Copy link
Collaborator

Choose a reason for hiding this comment

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

These dynamics tests are very different from the usual CUDAQ_RUNTIME_TEST_SOURCES so we should move them into a separate target. Adding them here will cause these tests to be repeated for all simulator backends.

# Make it so we can get function symbols
set (CMAKE_ENABLE_EXPORTS TRUE)

include_directories(/usr/local/cuda/targets/x86_64-linux/include)
Copy link
Collaborator

Choose a reason for hiding this comment

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

The CUDA include and lib paths should be detected using CMake. We probably want to wrap it in a CUDA_FOUND block as well since not all build environment has CUDA.

$ENV{CUQUANTUM_INSTALL_PREFIX}/lib/libcudensitymat.so.0
/usr/local/cuda-12.0/targets/x86_64-linux/lib/libcudart.so.12)
Copy link
Collaborator

Choose a reason for hiding this comment

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

After we do the refactoring in https://github.com/NVIDIA/cuda-quantum/pull/2407/files#r1933216013, we should only add these link libs to the dynamics unit test target.

Comment on lines +418 to +435
gtest_main
$ENV{CUQUANTUM_INSTALL_PREFIX}/lib/libcudensitymat.so.0)
Copy link
Collaborator

Choose a reason for hiding this comment

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

We shouldn't need to add this to existing unit tests.

cudm_state::cudm_state(std::vector<std::complex<double>> rawData)
: rawData_(rawData), state_(nullptr), handle_(nullptr),
hilbertSpaceDims_() {
HANDLE_CUDM_ERROR(cudensitymatCreate(&handle_));
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should only have a single cudensitymatHandle_t throughout the code (i.e., a single cudensitymatCreate and cudensitymatDestroy).

For example, we already create a handle in test_cudm_time_stepper.cpp (SetUp); thus creating another handle here is problematic.

I'd suggest passing the handle here, e.g.

cudm_state::cudm_state(cudensitymatHandle_t handle,
                       const std::vector<std::complex<double>> &rawData)
    : rawData_(rawData), state_(nullptr), handle_(handle), hilbertSpaceDims_() 

Comment on lines +28 to +36
if (handle_) {
cudensitymatDestroy(handle_);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Like above, we shouldn't be creating and destroying the handle during state object life cycle.

Comment on lines +169 to +171
rawComponentData[i] =
reinterpret_cast<std::complex<double> *>(rawData_.data()) + offset;
componentBuffers[i] = static_cast<void *>(rawComponentData[i]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

We're supposed to attached device memory here (rawData_ is a host array).
I think we need to add a gpuData_ member var and perform cudaMalloc + cudaMemcpy.

if (!liouvillian_) {
throw std::runtime_error("Liouvillian is not initialized.");
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks like we're missing some steps here:

  • cudensitymatOperatorPrepareAction
  • cudensitymatWorkspaceGetMemorySize
  • cudensitymatWorkspaceSetMemory


std::cout << "Update the state ..." << std::endl;
// Update the state
state = std::move(next_state);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we update the input state or return the action result (RHS) in this function?
We might need the latter since the RHS might be added to the input state with some scaling depending on the integration algorithm.

anthony-santana and others added 4 commits February 7, 2025 08:10
* add skeleton of new API functions without connecting to docs yet

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* updates

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* boilerplate

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* clean up linking bug

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* adding schedule implementation

* fixing typo

* small updates

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* Cleaning up docs preview for PR #6.

* push initial tests that show memory leak in current translation to complex matrix

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* completely work around eigen in default elementary ops

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* storing changes

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* start to build out callback function class

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* working function wrapper implementation

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* implement complex matrix equality operator

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* push with broken to matrix overload

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* fix scoping issue found in to matrix

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* fill out unit tests for to matrix overload

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* add skeleton of new API functions without connecting to docs yet

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* updates

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* boilerplate

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* clean up linking bug

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* initial scalar value support and tests

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* update to_value to evaluate to amtch python api

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* adding kwargs capability in C++ by using std::variant and std::bind

* renaming VariantArg to NumericType to match Python

* adding a std::variant returntype nad adjusting the test accordingly

* Cleaning up docs preview for PR #7.

* commit first draft of arithmetic against complex doubles

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* little build errors

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* call generator directly instead of evaluate in operator overloads

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* remove old constructor

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* copy constructor

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* push partially working arithmetic operations

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* comment back in tests

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* remove constructor that takes removed parameters member

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* still having memory issues

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* implement and test remaining pre defined elementary ops except displace and squeeze. implement complex matrix exponential

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* fix for segfault in copy constructor but still have memory issues from arithmetic

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* temp patch to get scalar arithmetic against complex doubles working. against doubles wrapped in functions still broken

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* potential fix for callback function going out of scope

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* confirm fix for scalar ops from functions and reenable tests

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* begin to support scalar against scalar ops and simplify other arithmetic definitions with macros

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* support for remaining arithmetic against other scalar ops except for assignment operators

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* add checks to ensure local variables are picked up fine in generator functions

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* clean up test file

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* full refactor to take a [arameter map with elementary operator implementation back under construction

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* fix elementary ops

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* start to manually merge in operator sum changes

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* camel case some things and underscore some others

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* fix build errors from header file

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* refactor implementation file names, delete unused dynamics.h, refactor unittests with new dynamics folder

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* storing large set of changes to dynamics folder structure and implementing more arithmetic

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* add cudaq tensor type

Signed-off-by: Alex McCaskey <amccaskey@nvidia.com>

* add uint8 tensor to python api

Signed-off-by: Alex McCaskey <amccaskey@nvidia.com>

* Update unittests/utils/tensor_tester.cpp

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Update unittests/utils/tensor_tester.cpp

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Update unittests/utils/tensor_tester.cpp

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Update runtime/cudaq/utils/details/impls/xtensor_impl.cpp

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Update runtime/cudaq/utils/details/impls/xtensor_impl.cpp

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Update runtime/cudaq/utils/details/impls/xtensor_impl.cpp

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Update python/runtime/utils/py_tensor.cpp

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Update runtime/cudaq/utils/extension_point.h

Co-authored-by: Ben Howe <141149032+bmhowe23@users.noreply.github.com>
Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Update runtime/cudaq/utils/extension_point.h

Co-authored-by: Ben Howe <141149032+bmhowe23@users.noreply.github.com>
Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Update runtime/cudaq/utils/tensor.h

Co-authored-by: Ben Howe <141149032+bmhowe23@users.noreply.github.com>
Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Update runtime/cudaq/utils/tensor.h

Co-authored-by: Ben Howe <141149032+bmhowe23@users.noreply.github.com>
Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Update runtime/cudaq/utils/details/impls/xtensor_impl.cpp

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Update runtime/cudaq/utils/details/tensor_impl.h

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Update runtime/cudaq/utils/details/tensor_impl.h

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Update runtime/cudaq/utils/details/tensor_impl.h

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Spelling fixes.

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* push current state

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* Format

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

* Format

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

* Drop debug code.

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Cleanup tensor types a tad.

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Fix spelling

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

* Fix spelling and format python code

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

* Sort spelling allowlist

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

* Cleaning up docs preview for PR #9.

* more arithmetic support

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* Update spelling and formatting

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

* more implementation and more tests

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* Update spelling and formatting

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

* This is an attempt to sort out the different ownership models of tensor data.
The python stub for "take" needs to be implemented however.

The semantics are:

  - copy : the tensor object makes a copy of the data and owns the copy.
           i.e. there is a unique_ptr.
  - take : the tensor object steals a copy of the data from the caller.
           In order for this case to make sense, we want the caller to
           guarantee that the data is unique before we steal it. This
           can be done by forcing the client code to wrap the tensor
           data in a unique_ptr *before* the take call.
  - borrow :
           In this case, the tensor object has no ownership of the
           tensor data and just naively assumes the client will
           manage the data correctly. In this case, a raw pointer
           to the client's data is used.

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Update the missing py_tensor code.

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Add handling for empty shape case.

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Add more python tests

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

* Add a take() with move semantics.

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* fix product operator constructor issue and tests

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* fix more test more

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* cover all arithmetic

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* update before merging in tensor pr

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* Fix __init__ argument order so tests pass. The shape must come first.

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Remove StateTensor. (Not used.)

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Add more comments on how to use this stuff.

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Added python tests and fixed some issues

* Merge with tensor

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

* DCO Remediation Commit for Anna Gringauze <agringauze@nvidia.com>

I, Anna Gringauze <agringauze@nvidia.com>, hereby add my Signed-off-by to this commit: e78def4

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

* Add some boilerplate for tensor operators.

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Make the compiler work a bit harder.

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Add move constructor.

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Make sure the return values for operators work as expected.

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* clang-format

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Add override to dtor.

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Add forward decls.

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* More fussy templates.

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Workaround warnings from g++.

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Fix typos.

Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>

* Support copy semantics for Numpy 2.0

* DCO Remediation Commit for Anna Gringauze <agringauze@nvidia.com>

I, Anna Gringauze <agringauze@nvidia.com>, hereby add my Signed-off-by to this commit: 85fae37

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

* Try fixing doc gen and c++ compilation errors

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

* Add compilation test for nvcc

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

* Remove temp file

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

* store changes

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* first pass of updating return types to cudaq tensor

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* store working version with tests before rebase

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* fix improper merge resolution issues

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* more issues with resolving merge

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* first pass of translation to matrix_2 with build errors fixed. now double checking tests

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* fix remaining artifacts from rebase

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* fix matrix checks for simple elementary op unit tests

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* first pass of implementing deeper matrix checks in tests. Have two files left to finish

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* fix copyright headers

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

* minor change to check verified commit

Signed-off-by: A.M. Santana <anthonys@nvidia.com>

---------

Signed-off-by: A.M. Santana <anthonys@nvidia.com>
Signed-off-by: Alex McCaskey <amccaskey@nvidia.com>
Signed-off-by: Eric Schweitz <eschweitz@nvidia.com>
Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
Co-authored-by: Sachin Pisal <spisal@nvidia.com>
Co-authored-by: cuda-quantum-bot <cuda-quantum-bot@users.noreply.github.com>
Co-authored-by: Alex McCaskey <amccaskey@nvidia.com>
Co-authored-by: Eric Schweitz <eschweitz@nvidia.com>
Co-authored-by: Ben Howe <141149032+bmhowe23@users.noreply.github.com>
Co-authored-by: Anna Gringauze <agringauze@nvidia.com>
Signed-off-by: Sachin Pisal <spisal@nvidia.com>
Signed-off-by: Sachin Pisal <spisal@nvidia.com>
1tnguyen and others added 28 commits February 10, 2025 08:08
Signed-off-by: Thien Nguyen <thiennguyen@nvidia.com>
Signed-off-by: Thien Nguyen <thiennguyen@nvidia.com>
Signed-off-by: Sachin Pisal <spisal@nvidia.com>
Signed-off-by: Sachin Pisal <spisal@nvidia.com>
Signed-off-by: Sachin Pisal <spisal@nvidia.com>
Signed-off-by: Sachin Pisal <spisal@nvidia.com>
Signed-off-by: Sachin Pisal <spisal@nvidia.com>
Signed-off-by: Sachin Pisal <spisal@nvidia.com>
* Adding mock function to create a matrix operator
* Adding unittests for mul
* Formatting

Signed-off-by: Sachin Pisal <spisal@nvidia.com>
Signed-off-by: Sachin Pisal <spisal@nvidia.com>
Signed-off-by: Sachin Pisal <spisal@nvidia.com>
* Adding function to get space_mode_extents
* Adding more unittests for op_conversion

Signed-off-by: Sachin Pisal <spisal@nvidia.com>
Signed-off-by: Sachin Pisal <spisal@nvidia.com>
…h a scalar

Signed-off-by: Sachin Pisal <spisal@nvidia.com>
Signed-off-by: Thien Nguyen <thiennguyen@nvidia.com>
…conversion

Signed-off-by: Sachin Pisal <spisal@nvidia.com>
Signed-off-by: Sachin Pisal <spisal@nvidia.com>
Signed-off-by: Thien Nguyen <thiennguyen@nvidia.com>
* Correcting tensor callback function name

Signed-off-by: Sachin Pisal <spisal@nvidia.com>
Signed-off-by: Thien Nguyen <thiennguyen@nvidia.com>
* Moved other reponsibilities like flattening the matrix, gettting
  subspace extents to the create elementary operator function
* Unittests updated
* Wrapped cudm_helper in a class

Signed-off-by: Sachin Pisal <spisal@nvidia.com>
Signed-off-by: Sachin Pisal <spisal@nvidia.com>
Signed-off-by: Thien Nguyen <thiennguyen@nvidia.com>
Signed-off-by: Sachin Pisal <spisal@nvidia.com>
…trix operator

Signed-off-by: Sachin Pisal <spisal@nvidia.com>
… from elementary ops

Signed-off-by: Thien Nguyen <thiennguyen@nvidia.com>
…tor_to_term signature

Signed-off-by: Sachin Pisal <spisal@nvidia.com>
github-actions bot pushed a commit that referenced this pull request Feb 12, 2025
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.

7 participants