qml.estimator.resource_operator.ResourceOperator

class ResourceOperator(*args, wires=None, **kwargs)[source]

Bases: ABC

Base class to represent quantum operators according to the fundamental set of information required for resource estimation.

A ResourceOperator is uniquely defined by its name (the class type) and its resource parameters (op.resource_params).

This example shows how to create a custom ResourceOperator class for resource estimation. We use QFT as a well known gate for simplicity.

import pennylane.estimator as qre

class QFT(qre.ResourceOperator):

    resource_keys = {"num_wires"}  # the only parameter that its resources depend upon.

    def __init__(self, num_wires, wires=None):  # wire labels are optional
        self.num_wires = num_wires
        super().__init__(wires=wires)

    @property
    def resource_params(self) -> dict:        # The keys must match the `resource_keys`
        return {"num_wires": self.num_wires}  # and values obtained from the operator.

    @classmethod
    def resource_rep(cls, num_wires):   # Takes the same input as `resource_keys` and
        params = {"num_wires": num_wires}  #  produces a compressed representation
        return qre.CompressedResourceOp(cls, num_wires, params)

    @classmethod
    def resource_decomp(cls, num_wires):  # `resource_keys` are input

        # Get compressed reps for each gate in the decomposition:

        swap = qre.resource_rep(qre.SWAP)
        hadamard = qre.resource_rep(qre.Hadamard)
        ctrl_phase_shift = qre.resource_rep(qre.ControlledPhaseShift)

        # Figure out the associated counts for each type of gate:

        swap_counts = num_wires // 2
        hadamard_counts = num_wires
        ctrl_phase_shift_counts = num_wires*(num_wires - 1) // 2

        return [    # Return the decomposition
            qre.GateCount(swap, swap_counts),
            qre.GateCount(hadamard, hadamard_counts),
            qre.GateCount(ctrl_phase_shift, ctrl_phase_shift_counts),
        ]

Which can be instantiated as a normal operation, but now contains the resources:

>>> op = QFT(num_wires=3)
>>> print(qre.estimate(op, gate_set={'Hadamard', 'SWAP', 'ControlledPhaseShift'}))
--- Resources: ---
 Total wires: 3
    algorithmic wires: 3
    allocated wires: 0
         zero state: 0
         any state: 0
 Total gates : 7
  'SWAP': 1,
  'ControlledPhaseShift': 3,
  'Hadamard': 3

num_wires

resource_keys

resource_params

A dictionary containing the minimal information needed to compute a resource estimate of the operator's decomposition.

num_wires = None
resource_keys = {}
resource_params

A dictionary containing the minimal information needed to compute a resource estimate of the operator’s decomposition. The keys of this dictionary should match the resource_keys attribute of the operator class.

add_parallel(other)

Adds a ResourceOperator or Resources in parallel.

add_series(other)

Adds a ResourceOperator or Resources in series.

adjoint_resource_decomp([target_resource_params])

Returns a list representing the resources for the adjoint of the operator.

controlled_resource_decomp(num_ctrl_wires, ...)

Returns a list representing the resources for a controlled version of the operator.

pow_resource_decomp(pow_z[, ...])

Returns a list representing the resources for an operator raised to a power.

queue([context])

Append the operator to the Operator queue.

resource_decomp(*args, **kwargs)

Returns a list of actions that define the resources of the operator.

resource_rep(*args, **kwargs)

Returns a compressed representation containing only the parameters of the operator that are needed to estimate the resources.

resource_rep_from_op()

Returns a compressed representation directly from the operator

tracking_name(*args, **kwargs)

Returns a name used to track the operator during resource estimation.

add_parallel(other)[source]

Adds a ResourceOperator or Resources in parallel.

Parameters:

other (ResourceOperator) – The other object to combine with, it can be another ResourceOperator or a Resources object.

Returns:

added Resources

Return type:

Resources

add_series(other)[source]

Adds a ResourceOperator or Resources in series.

Parameters:

other (ResourceOperator) – The other object to combine with, it can be another ResourceOperator or a Resources object.

Returns:

added Resources

Return type:

Resources

classmethod adjoint_resource_decomp(target_resource_params=None)[source]

Returns a list representing the resources for the adjoint of the operator.

Parameters:

target_resource_params (dict | None) – A dictionary containing the resource parameters of the target operator.

classmethod controlled_resource_decomp(num_ctrl_wires, num_zero_ctrl, target_resource_params=None)[source]

Returns a list representing the resources for a controlled version of the operator.

Parameters:
  • num_ctrl_wires (int) – the number of qubits the operation is controlled on

  • num_zero_ctrl (int) – the number of control qubits, that are controlled when in the \(|0\rangle\) state

  • target_resource_params (dict | None) – A dictionary containing the resource parameters of the target operator.

classmethod pow_resource_decomp(pow_z, target_resource_params=None)[source]

Returns a list representing the resources for an operator raised to a power.

Parameters:
  • pow_z (int) – exponent that the operator is being raised to

  • target_resource_params (dict | None) – A dictionary containing the resource parameters of the target operator.

queue(context=<class 'pennylane.queuing.QueuingManager'>)[source]

Append the operator to the Operator queue.

abstract classmethod resource_decomp(*args, **kwargs)[source]

Returns a list of actions that define the resources of the operator.

abstract classmethod resource_rep(*args, **kwargs)[source]

Returns a compressed representation containing only the parameters of the operator that are needed to estimate the resources.

resource_rep_from_op()[source]

Returns a compressed representation directly from the operator

classmethod tracking_name(*args, **kwargs)[source]

Returns a name used to track the operator during resource estimation.