qml.deallocate

deallocate(wires)[source]

Deallocates wires that have previously been allocated with allocate(). Upon deallocating, those wires is available to be allocated thereafter.

Parameters:

wires (DynamicWire, Wires, Sequence[DynamicWire]) – one or more dynamic wires.

See also

allocate()

Note

The allocate() function can be used as a context manager with automatic deallocation (recommended for most cases) upon exiting the scope.

Example

import pennylane as qml

@qml.qnode(qml.device("default.qubit"))
def circuit():
    qml.H(0)

    wire = qml.allocate(1, state="zero", restored=True)[0]
    qml.CNOT((0, wire))
    qml.CNOT((0, wire))
    qml.deallocate(wire)

    new_wires = qml.allocate(2, state="zero", restored=True)
    qml.SWAP((new_wires[1], new_wires[0]))
    qml.deallocate(new_wires)

    return qml.expval(qml.Z(0))
>>> print(qml.draw(circuit)())
            0: ──H────────╭●────╭●──────────────────────┤  <Z>
<DynamicWire>: ──Allocate─╰X────╰X───────────Deallocate─┤
<DynamicWire>: ─╭Allocate─╭SWAP─╭Deallocate─────────────┤
<DynamicWire>: ─╰Allocate─╰SWAP─╰Deallocate─────────────┤

Here, three dynamic wires were allocated in the circuit originally. When PennyLane determines which concrete values to use for dynamic wires to send to the device for execution, we can see that the first dynamic wire is already deallocated back into the zero state. This allows us to use it as one of the wires requested in the second allocation, resulting in a total of three wires being required from the device, including two dynamically allocated wires:

>>> print(qml.draw(circuit, level="device")())
0: ──H─╭●─╭●───────┤  <Z>
1: ────╰X─╰X─╭SWAP─┤
2: ──────────╰SWAP─┤

Contents

Using PennyLane

Release news

Development

API

Internals