qml.estimator.wires_manager.Allocate

class Allocate(num_wires)[source]

Bases: _WireAction

Allows allocation of work wires through WireResourceManager.

Parameters:

num_wires (int) – number of work wires to be allocated

The Allocate class is typically used within a decomposition function to track the allocation of auxiliary wires. This allows determination of a circuit’s wire overhead. In this example, we show the decomposition for a 3-controlled X gate, which requires one work wire.

First, we define a custom decomposition which doesn’t track the extra work wire:

>>> from pennylane import estimator as qre
>>> from pennylane.estimator import GateCount, resource_rep
>>> def resource_decomp(num_ctrl_wires=3, num_ctrl_values=0, **kwargs):
...     gate_list = []
...
...     gate_list.append(GateCount(resource_rep(qre.TempAND), 1))
...     gate_list.append(GateCount(resource_rep(qre.Adjoint, {"base_cmpr_op": resource_rep(qre.TempAND)}), 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)
>>> print(res.WireResourceManager)
WireResourceManager(zeroed wires =0, any_state wires=0, algorithmic wires=4, tight budget=False)

This decomposition uses a total of 4 wires and doesn’t track the work wires.

Now, if we want to track the allocation of wires using Allocate, the decomposition can be redefined as:

>>> from pennylane import estimator as qre
>>> from pennylane.estimator import GateCount, resource_rep
>>> def resource_decomp():
...     gate_list = []
...     gate_list.append(qre.Allocate(num_wires=1))
...
...     gate_list.append(GateCount(resource_rep(qre.TempAND), 1))
...     gate_list.append(GateCount(resource_rep(qre.Adjoint, {"base_cmpr_op": resource_rep(qre.TempAND)}), 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)
>>> print(res.WireResourceManager)
WireResourceManager(zeroed wires=1, any_state wires=0, algorithmic wires=4, tight budget=False)

Now, the one extra auxiliary wire is being tracked.