Skip to content

cuda-quake mishandles measure results, causing compiler crash #4562

@IlanIwumbwe

Description

@IlanIwumbwe

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

cudaq-quake fails to emit a quake.discriminate to map measure result type to bool when performing range based for loop.

Steps to reproduce the bug

Build from source and run the circuit below

struct main_circuit { 
uint64_t operator()() __qpu__ {
	cudaq::qarray<2>reg_1190;

        int64_t packed_result = 0; int bit_offset = 0;	
	
	for (bool b : mz(reg_1190)){
               if (b) packed_result |= (1ULL << bit_offset);
               bit_offset++;
        }

	return packed_result;
}
};

and get a compiler crash, stemming from cudaq-quake.
loc("test.cpp":10:2):error: 'cc.store' op failed to verify that type of value matches element type of pointer

Line 10 being for (bool b : mz(reg_1190)){

The MLIR that contains that cc.store is

%18 = "cc.alloca"() <{elementType = i1}> : () -> !cc.ptr<i1>
%19 = "cc.load"(%17) : (!cc.ptr<!cc.measure_handle>) -> !cc.measure_handle
"cc.store"(%19, %18) : (!cc.measure_handle, !cc.ptr<i1>) -> ()

The cc.store tries map something of type !cc.measure_handle to !cc.ptr<i1> which is ptr to a bool.

Running the code but with auto instead of bool works. The MLIR at the relevant section is

cc.store %9, %8 : !cc.ptr<!cc.measure_handle> loc(#loc6)
%10 = cc.load %8 : !cc.ptr<!cc.measure_handle> loc(#loc8)
%11 = quake.discriminate %10 : (!cc.measure_handle) -> i1 loc(#loc8)

quake.discriminate added here maps the measure result to the correct type.

Expected behavior

Compiler should not crash

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

Not a regression

Environment

  • CUDA-Q version: b04dee5
  • Python version: 3.12.3
  • C++ compiler: clang++-22
  • Operating system: Ubuntu 24.04

Suggestions

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions