Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[features/qalloc] Error checking on number of elements in MLIR Verifier #1490

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/cudaq/Optimizer/Dialect/Quake/QuakeOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ def quake_InitializeStateOp : QuakeOp<"init_state", [MemoryEffects<[MemAlloc,
let assemblyFormat = [{
$targets `,` $state `:` functional-type(operands, results) attr-dict
}];

let hasVerifier = 1;
}

def quake_RelaxSizeOp : QuakeOp<"relax_size", [Pure]> {
Expand Down
4 changes: 4 additions & 0 deletions lib/Frontend/nvqpp/ConvertExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2402,6 +2402,10 @@ bool QuakeBridgeVisitor::VisitCXXConstructExpr(clang::CXXConstructExpr *x) {
builder.create<math::CountTrailingZerosOp>(loc, size);
} else {
std::size_t arraySize = arrTy.getSize();
if (!std::has_single_bit(arraySize)) {
reportClangError(x, mangler,
"state vector must be a power of 2 in length");
}
numQubits = builder.create<arith::ConstantIntOp>(
loc, std::countr_zero(arraySize), 64);
}
Expand Down
13 changes: 13 additions & 0 deletions lib/Optimizer/Dialect/Quake/QuakeOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,19 @@ LogicalResult quake::ExtractRefOp::verify() {
return success();
}

//===----------------------------------------------------------------------===//
// InitializeStateOp
//===----------------------------------------------------------------------===//

LogicalResult quake::InitializeStateOp::verify() {
auto veqTy = cast<quake::VeqType>(getTargets().getType());
if (veqTy.hasSpecifiedSize())
if (!std::has_single_bit(veqTy.getSize()))
return emitOpError("initialize state vector must be power of 2, but is " +
std::to_string(veqTy.getSize()) + " instead.");
return success();
}

//===----------------------------------------------------------------------===//
// RelaxSizeOp
//===----------------------------------------------------------------------===//
Expand Down
5 changes: 4 additions & 1 deletion runtime/cudaq/builder/kernel_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,12 @@ QuakeValue qalloc(ImplicitLocOpBuilder &builder, std::size_t hash,
throw std::runtime_error(
"Could not create code for setting the state global data.");

if (!std::has_single_bit(size))
throw std::runtime_error("state vector must be a power of 2 in length");

// Allocate the qubits
Value qubits = builder.create<quake::AllocaOp>(
quake::VeqType::get(context, std::log2(size)));
quake::VeqType::get(context, std::countr_zero(size)));

// Get the pointer to the global
auto f64Ty = builder.getF64Type();
Expand Down
Loading