Skip to content
2 changes: 2 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
[(#8163)](https://github.com/PennyLaneAI/pennylane/pull/8163)
[(#8179)](https://github.com/PennyLaneAI/pennylane/pull/8179)
[(#8198)](https://github.com/PennyLaneAI/pennylane/pull/8198)
[(#8381)](https://github.com/PennyLaneAI/pennylane/pull/8381)

The :func:`~.allocate` function can accept three arguments that dictate how dynamically allocated
wires are handled:
Expand Down Expand Up @@ -1572,5 +1573,6 @@ Justin Pickering,
Alex Preciado,
Shuli Shu,
Jay Soni,
Paul Haochen Wang,
David Wierichs,
Jake Zaia
8 changes: 8 additions & 0 deletions pennylane/allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from typing import Literal

from pennylane.capture import enabled as capture_enabled
from pennylane.math import is_abstract
from pennylane.operation import Operator
from pennylane.wires import DynamicWire, Wires

Expand Down Expand Up @@ -227,6 +228,9 @@ def allocate(
The ``allocate`` function can be used as a context manager with automatic deallocation
(recommended for most cases) or with manual deallocation via :func:`~.deallocate`.

.. note::
The ``num_wires`` argument must be static when capture is enabled.

.. seealso::
:func:`~.deallocate`

Expand Down Expand Up @@ -357,6 +361,10 @@ def circuit():
"""
state = AllocateState(state)
if capture_enabled():
if is_abstract(num_wires):
raise NotImplementedError(
"Number of allocated wires must be static when capture is enabled."
)
wires = allocate_prim.bind(num_wires=num_wires, state=state, restored=restored)
else:
wires = [DynamicWire() for _ in range(num_wires)]
Expand Down
13 changes: 13 additions & 0 deletions tests/test_allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,19 @@ def test_no_implementation(self):
with pytest.raises(NotImplementedError):
deallocate(2)

def test_no_dynamic_allocation_size(self):
"""Test that allocation size must be static with capture."""
with pytest.raises(
NotImplementedError,
match="Number of allocated wires must be static when capture is enabled.",
):

@qml.qnode(qml.device("default.qubit", wires=2))
def c(n: int):
allocate(n)

c(1)


@pytest.mark.integration
class TestDeviceIntegration:
Expand Down