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:

>>> 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(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 0

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:

>>> 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 one extra auxiliary wire is being tracked.