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

Checking vector size for a power of 2 #2491

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
45 changes: 20 additions & 25 deletions docs/sphinx/examples/python/visualization.ipynb

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions python/runtime/cudaq/algorithms/py_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ state pyGetState(py::object kernel, py::args args) {

auto kernelName = kernel.attr("name").cast<std::string>();
auto kernelMod = kernel.attr("module").cast<MlirModule>();

// Validate vector size if it is used for state initialization
if (args.size() == 1) {
py::object arg = args[0];

// Check if the argument is an iterable
if (py::hasattr(arg, "__len__")) {
size_t size = py::len(arg);

if (size == 0 || (size & (size - 1)) != 0) {
throw std::invalid_argument(
fmt::format("Invalid vector size: {}. When initializing a qubit "
"register, the vector size must be a power of 2.",
size));
}
}
}

auto *argData = toOpaqueArgs(args, kernelMod, kernelName);

return details::extractState([&]() mutable {
Expand Down
Loading