Closed moar55 closed 3 years ago
Nope that is a bug.
And its interesting - std::stoi()
converts '4*3' to 4, which is not something I expected. Hence the catch block which will properly handle the expression.
Fix would be to change the try { ... } catch(...) {...}
around the std::stoi()
call, to instead be an if {} else{}
based on ScopedSymbolTable::try_evaluate_constant_integer_expression( const std::string expr_str)
auto opt_size = symbol_table.try_evaluate_constant_integer_expression(
exp_list->expression(0)->getText());
if (opt_size.has_value()) {
size = opt_size.value();
} else {
// check if this is a constant expression
qasm3_expression_generator exp_generator(builder, symbol_table,
file_name);
exp_generator.visit(exp_list->expression(0));
auto arg = exp_generator.current_value;
if (auto constantOp = arg.getDefiningOp<mlir::ConstantOp>()) {
if (constantOp.getValue().isa<mlir::IntegerAttr>()) {
size = constantOp.getValue().cast<mlir::IntegerAttr>().getInt();
} else {
printErrorMessage(
"This variable qubit size must be a constant integer.");
}
} else {
size = symbol_table.get_global_constant<int64_t>(
exp_list->expression(0)->getText());
}
}
I noticed that when declaring a quantum array (either by
qubit
orqreg
), if the size of the array is provided as an expression the size is allocated with the first element of the expression. I found this in quantum_types_handler.cpp: heresize = std::stoi(exp_list->expression(0)->getText());
and hereexp_generator.visit(exp_list->expression(0));
From what I see it's valid syntax to have:qubit q1[4*3];
. Is only taking the first element of the expression an intended design decision?