Skip to content

Commit

Permalink
moving the vector size check in get_state
Browse files Browse the repository at this point in the history
Signed-off-by: Sachin Pisal <[email protected]>
  • Loading branch information
sacpis committed Jan 3, 2025
1 parent 5281780 commit a1dda5a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
24 changes: 12 additions & 12 deletions docs/sphinx/examples/python/visualization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 4,
"metadata": {
"scrolled": true
},
Expand Down Expand Up @@ -64,7 +64,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -92,7 +92,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -113,7 +113,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand All @@ -140,7 +140,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 8,
"metadata": {},
"outputs": [
{
Expand All @@ -167,7 +167,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 9,
"metadata": {},
"outputs": [
{
Expand All @@ -194,7 +194,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 10,
"metadata": {},
"outputs": [
{
Expand All @@ -221,7 +221,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -243,7 +243,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 12,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -279,7 +279,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -302,7 +302,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 14,
"metadata": {},
"outputs": [
{
Expand All @@ -328,7 +328,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 15,
"metadata": {},
"outputs": [
{
Expand Down
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
8 changes: 0 additions & 8 deletions python/utils/OpaqueArguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,6 @@ inline py::args simplifiedValidateInputArguments(py::args &args) {
if (shape.size() != 1)
throw std::runtime_error("Cannot pass ndarray with shape != (N,).");

// Vector size validation
size_t size = shape[0].cast<size_t>();
if (size == 0 || (size & (size - 1)) != 0) {
throw std::invalid_argument(fmt::format(
"Invalid vector size: {}. Vector size must be a power of 2.",
size));
}

arg = args[i].attr("tolist")();
} else if (py::isinstance<py::str>(arg)) {
arg = py::cast<std::string>(arg);
Expand Down

0 comments on commit a1dda5a

Please sign in to comment.