qml.estimator.wires_manager.Deallocate

class Deallocate(num_wires)[source]

Bases: _WireAction

Allows freeing any_state work wires through WireResourceManager.

Parameters:

num_wires (int) – number of any_state work wires to be freed.

The Deallocate class is typically used within a decomposition function to track the allocation of auxiliary wires. This allows to accurately determine the wire overhead of a circuit. In this example, we show the decomposition for a 3-controlled X gate, which requires one work wire that is returned in a zeroed state.

First, we define a custom decomposition which allocates the work wire but doesn’t free it.

>>> import pennylane.estimator as qre
>>> from pennylane.estimator import GateCount, resource_rep
>>> def resource_decomp(num_ctrl_wires=3, num_zero_ctrl=0, **kwargs):
...     gate_list = []
...     gate_list.append(qre.Allocate(num_wires=1))
...     gate_list.append(GateCount(resource_rep(qre.TemporaryAND), 1))
...     gate_list.append(GateCount(resource_rep(qre.Adjoint, {"base_cmpr_op": resource_rep(qre.TemporaryAND)}), 1))
...     gate_list.append(GateCount(resource_rep(qre.Toffoli), 1))
...     return gate_list
>>> config = qre.ResourceConfig()
>>> config.set_decomp(qre.MultiControlledX, resource_decomp)
>>> res = qre.estimate(qre.MultiControlledX(3, 0), config=config)
>>> print(res.algo_wires, res.zeroed_wires, res.any_state_wires)
4 0 1

This decomposition uses a total of 4 algorithmic wires and 1 work wire which is returned in an arbitrary state.

We can free this wire using Deallocate, allowing it to be reused with more operations. The decomposition can be redefined as:

>>> import pennylane.estimator as qre
>>> from pennylane.estimator import GateCount, resource_rep
>>> def resource_decomp(num_ctrl_wires=3, num_zero_ctrl=0, **kwargs):
...     gate_list = []
...     gate_list.append(qre.Allocate(num_wires=1))
...     gate_list.append(GateCount(resource_rep(qre.TemporaryAND), 1))
...     gate_list.append(GateCount(resource_rep(qre.Adjoint, {"base_cmpr_op": resource_rep(qre.TemporaryAND)}), 1))
...     gate_list.append(GateCount(resource_rep(qre.Toffoli), 1))
...     gate_list.append(qre.Deallocate(num_wires=1))
...     return gate_list
>>> config = qre.ResourceConfig()
>>> config.set_decomp(qre.MultiControlledX, resource_decomp)
>>> res = qre.estimate(qre.MultiControlledX(3, 0), config=config)
>>> print(res.algo_wires, res.zeroed_wires, res.any_state_wires)
4 1 0

Now, the auxiliary wire is freed, meaning that it is described as being in the zeroed state after the decomposition, and that it can now be used for other operators which require zeroed auxiliary wires.