NVIDIA / cuda-quantum

C++ and Python support for the CUDA Quantum programming model for heterogeneous quantum-classical workflows
https://nvidia.github.io/cuda-quantum/
Other
486 stars 179 forks source link

Enable pure quantum struct usage in kernels with restrictions #2199

Open amccaskey opened 3 weeks ago

amccaskey commented 3 weeks ago

This PR addresses the need to support struct types with only quantum members, e.g.

struct test {
  cudaq::qview<> q;
  cudaq::qview<> r;
};
__qpu__ void entry_ctor() {
  cudaq::qvector q(2), r(2);
  test tt(q, r);
  h(tt.r[0]);
}

or

@dataclass
class patch:
    q : cudaq.qview
    r : cudaq.qview 

@cudaq.kernel 
def entry():
    q = cudaq.qvector(2)
    r = cudaq.qvector(2)
    p = patch(q, r)
    h(p.r[0])

Pure quantum struct types are treated as value types in the MLIR (no loading or storing).

We enforce the following restrictions in both C++ and Python with thrown errors with descriptive messages:

A canonicalizer is also added to ensure lowering to static circuit code is possible.

copy-pr-bot[bot] commented 3 weeks ago

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.

schweitzpgi commented 3 weeks ago

Maybe I forgot to comment before, but I think it will be handy to have a distinct type for these other than a cc.struct type. It would let us fine tune the semantics better. They are "quantum" types in the "quantum memory" space. They only interact with quake dialect artifacts. They can't be loaded and stored from and to regular memory, etc.

I saw the "size" information (utterly bogus in terms of the semantics here) in the tests and it reminded me...