PennyLaneAI / catalyst

A JIT compiler for hybrid quantum programs in PennyLane
https://docs.pennylane.ai/projects/catalyst
Apache License 2.0
122 stars 27 forks source link

Gradient dialect removes `qnode` attribute from quantum functions when creating helper functions. #419

Open erick-xanadu opened 8 months ago

erick-xanadu commented 8 months ago
  func.func private @f(%arg0: tensor<f64>) -> tensor<f64> attributes {diff_method = "parameter-shift", llvm.linkage = #llvm.linkage<internal>, qnode} {
  }

Changes to:

  func.func private @f.quantum(%arg0: memref<f64>, %arg1: memref<?xf64>) -> memref<f64> attributes {gradient.qgrad = @f.qgrad, passthrough = ["noinline"]} {
}

This causes async to not be triggered since async looks for the qnode attribute.

dime10 commented 8 months ago

Copying description from the duplicate issue:

Some MLIR transforms, like the parameter shift pass, predate the introduction of the qnode attribute on MLIR functions with quantum code. The use of the attribute has also been extended, for example to determine asynchronous execution scopes.

In order to support the asynchronous execution of shifted circuits, the parameter-shift transform should propagate the qnode attribute from the original function it acts upon to the shifted function calls which each represent an independent quantum execution.

Note I don't think the current implementation would be able to benefit much from asynchronous execution, except perhaps for two evaluations at a time, because the shifted circuits are not generated in a batched but a procedural fashion. See the following snippet, where the results a quantum call a used pretty much immediately:

https://github.com/PennyLaneAI/catalyst/blob/b94be65521c726eb231dc9a42f00645757bd804b/mlir/lib/Gradient/Transforms/GradMethods/PS_QuantumGradient.cpp#L78-L91