Skip to content

[add-measurements] Pass does not handle dealloc correctly #3492

@khalatepradnya

Description

@khalatepradnya

Required prerequisites

  • Consult the security policy. If reporting a security vulnerability, do not report the bug using this form. Use the process described in the policy to report the issue.
  • Make sure you've read the documentation. Your issue may be addressed there.
  • Search the issue tracker to verify that this hasn't already been reported. +1 or comment there if it has.
  • If possible, make a PR with a failing test to give us a starting point to work on!

Describe the bug

The add-measurements pass inserts measurement operations on the allocated qubits if no measurement operations are present in the kernel.
The pass does not handle the presence of dealloc operation correctly, however. If dealloc is present on a qubit, the mz operation should be inserted before it.

Steps to reproduce the bug

Example

// -----// IR Dump Before AddMeasurements (add-measurements) ('func.func' operation: @__nvqpp__mlirgen__function_bell_no_return._Z14bell_no_returnv) //----- //
module attributes {cc.sizeof_string = 32 : i64, llvm.data_layout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128", llvm.triple = "x86_64-unknown-linux-gnu", quake.mangled_name_map = {__nvqpp__mlirgen__function_bell_no_return._Z14bell_no_returnv = "_Z14bell_no_returnv"}} {
  func.func @__nvqpp__mlirgen__function_bell_no_return._Z14bell_no_returnv() attributes {"cudaq-entrypoint", "cudaq-kernel", no_this} {
    %0 = quake.alloca !quake.veq<2>
    %1 = quake.extract_ref %0[0] : (!quake.veq<2>) -> !quake.ref
    quake.h %1 : (!quake.ref) -> ()
    %2 = quake.extract_ref %0[1] : (!quake.veq<2>) -> !quake.ref
    quake.x [%1] %2 : (!quake.ref, !quake.ref) -> ()
    quake.dealloc %0 : !quake.veq<2>
    return
  }
}
// -----// IR Dump After AddMeasurements (add-measurements) ('func.func' operation: @__nvqpp__mlirgen__function_bell_no_return._Z14bell_no_returnv) //----- //
module attributes {cc.sizeof_string = 32 : i64, llvm.data_layout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128", llvm.triple = "x86_64-unknown-linux-gnu", quake.mangled_name_map = {__nvqpp__mlirgen__function_bell_no_return._Z14bell_no_returnv = "_Z14bell_no_returnv"}} {
  func.func @__nvqpp__mlirgen__function_bell_no_return._Z14bell_no_returnv() attributes {"cudaq-entrypoint", "cudaq-kernel", no_this} {
    %0 = quake.alloca !quake.veq<2>
    %1 = quake.extract_ref %0[0] : (!quake.veq<2>) -> !quake.ref
    quake.h %1 : (!quake.ref) -> ()
    %2 = quake.extract_ref %0[1] : (!quake.veq<2>) -> !quake.ref
    quake.x [%1] %2 : (!quake.ref, !quake.ref) -> ()
    quake.dealloc %0 : !quake.veq<2>
    cf.br ^bb1
  ^bb1:  // pred: ^bb0
    %measOut = quake.mz %0 : (!quake.veq<2>) -> !cc.stdvec<!quake.measure>
    return
  }
}

Expected behavior

Expected o/p

// -----// IR Dump After AddMeasurements (add-measurements) ('func.func' operation: @__nvqpp__mlirgen__function_bell_no_return._Z14bell_no_returnv) //----- //
module attributes {cc.sizeof_string = 32 : i64, llvm.data_layout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128", llvm.triple = "x86_64-unknown-linux-gnu", quake.mangled_name_map = {__nvqpp__mlirgen__function_bell_no_return._Z14bell_no_returnv = "_Z14bell_no_returnv"}} {
  func.func @__nvqpp__mlirgen__function_bell_no_return._Z14bell_no_returnv() attributes {"cudaq-entrypoint", "cudaq-kernel", no_this} {
    %0 = quake.alloca !quake.veq<2>
    %1 = quake.extract_ref %0[0] : (!quake.veq<2>) -> !quake.ref
    quake.h %1 : (!quake.ref) -> ()
    %2 = quake.extract_ref %0[1] : (!quake.veq<2>) -> !quake.ref
    quake.x [%1] %2 : (!quake.ref, !quake.ref) -> ()    
    cf.br ^bb1
  ^bb1:  // pred: ^bb0
    %measOut = quake.mz %0 : (!quake.veq<2>) -> !cc.stdvec<!quake.measure>
    quake.dealloc %0 : !quake.veq<2>
    return
  }
}

Is this a regression? If it is, put the last known working version (or commit) here.

Not a regression

Environment

  • CUDA-Q version: main as of commit ID ed24b0f
  • Python version:
  • C++ compiler:
  • Operating system:

Suggestions

Recommendation from @schweitzpgi: This is way, way easier in value semantics form.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions