cuda-quantum
cuda-quantum copied to clipboard
[RFC] Allow more classical data to be created/modified/computed in kernels
At present, one is disallowed from creating classical data such as a std::vector<double>
from within a kernel. This may be too restrictive for kernel writers.
Example 1: creating complex data structures.
std::tuple<std::vector<std::vector<size_t>>, std::vector<std::vector<size_t>>> generateExcitations(std::size_t nElectrons, std::size_t nOrbitals);
__qpu__ void uccsd(cudaq::qspan<> qubits, std::vector<double> thetas, std::size_t nElectrons) {
auto [singles, doubles] = generateExcitations(nElectrons, qubits.size());
...
Additionally, some computations of classical values may involve calling back to the C++ standard library (math) functions, etc.
Example 2: computing non-trivial expressions such as sum +=
expr .
__qpu__ double loglikelihood(std::vector<int> &h, std::vector<int> &mk, int N_shots, double theta_val) {
double sum = 0.0;
for (int i = 0; i < h.size(); i++) {
double arg = (2 * mk[i] + 1) * theta_val;
sum += 2 * h[i] * log(fabs(sin(arg))) + 2 * (N_shots - h[i]) * log(fabs(cos(arg)));
}
...
### Tasks
- [ ] Fix bug when declaration type != initializer type (zero padding). This can happen when the initialization list has a length smaller than the declared length of the type.
- [ ] Fix bug with indexing off the end of a pointer to a scalar. In the CC dialect, we want to use pointers to arrays of `?` length when indexing. Indexing off the end of a scalar shouldn't be allowed.
- [ ] Add warning about constant indexing off the front of an array. Need to add verification checks that disallow IR that indexes off the front of references to arrays.
- [ ] Create a list of math intrinsic functions we want to support,<sup>[1]</sup> add it to the documentation, and make sure they are all supported in the bridge. Add tests.
- [ ] Objects would typically be created on demand in the heap, but there may not be a heap or it may be very limited in size or control hardware. Should decide what object types, if any, must be supported. At present, we require that the target is able to support allocating space for measured results as a vector of bytes. It is also required that there is enough stack space to pass the supported aggregates of arithmetic types.<sup>[1]</sup>
- [ ] Finish implementations of features from the table.<sup>[2]</sup>
- [ ] Add std::tuple and std::pair support (tracked as issue #644)
- [ ] Add support for recursive vectors of structs with vector members. vectors of vectors ... has been implemented, but not vectors of vectors ... of struct with vectors.
Related: https://github.com/NVIDIA/cuda-quantum/issues/489
[1] See the CUDA-Q specification. [2] Table capturing additional support and implementation status (as of 0.7.0): language_support.pdf