qml.labs.resource_estimation.ResourceOperator¶
- class ResourceOperator(*args, wires=None, **kwargs)[source]¶
Bases:
ABC
Base class to represent quantum operators according to the 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
).Example
This example shows how to create a custom
ResourceOperator
class for resource estimation. We useQFT
as a well known gate for simplicity.from pennylane.labs import resource_estimation as plre class ResourceQFT(plre.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 `resource_keys` as input params = {"num_wires": num_wires} # and produces a compressed return plre.CompressedResourceOp(cls, params) # representation of the operator @classmethod def default_resource_decomp(cls, num_wires, **kwargs): # `resource_keys` are input # Get compressed reps for each gate in the decomposition: swap = plre.resource_rep(plre.ResourceSWAP) hadamard = plre.resource_rep(plre.ResourceHadamard) ctrl_phase_shift = plre.resource_rep(plre.ResourceControlledPhaseShift) # 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 plre.GateCount(swap, swap_counts), plre.GateCount(hadamard, hadamard_counts), plre.GateCount(ctrl_phase_shift, ctrl_phase_shift_counts), ]
Which can be instantiated as a normal operation, but now contains the resources:
>>> op = ResourceQFT(num_wires=3) >>> print(plre.estimate_resources(op, gate_set={'Hadamard', 'SWAP', 'ControlledPhaseShift'})) --- Resources: --- Total qubits: 3 Total gates : 7 Qubit breakdown: clean qubits: 0, dirty qubits: 0, algorithmic qubits: 3 Gate breakdown: {'SWAP': 1, 'Hadamard': 3, 'ControlledPhaseShift': 3}
Attributes
A dictionary containing the minimal information needed to compute a resource estimate of the operator's decomposition.
- num_wires = 0¶
- 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.
Methods
adjoint_resource_decomp
(*args, **kwargs)Returns a list of actions that define the resources of the operator.
Returns a list representing the resources for a controlled version of the operator.
default_adjoint_resource_decomp
(*args, **kwargs)Returns a list representing the resources for the adjoint of the operator.
Returns a list representing the resources for a controlled version of the operator.
default_pow_resource_decomp
(pow_z, *args, ...)Returns a list representing the resources for an operator raised to a power.
default_resource_decomp
(*args, **kwargs)Returns a list of actions that define the resources of the operator.
pow_resource_decomp
(pow_z, *args, **kwargs)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.
Returns a compressed representation directly from the operator
set_resources
(new_func[, override_type])Set a custom function to override the default resource decomposition.
tracking_name
(*args, **kwargs)Returns a name used to track the operator during resource estimation.
Returns the tracking name built with the operator's parameters.
- classmethod adjoint_resource_decomp(*args, **kwargs)[source]¶
Returns a list of actions that define the resources of the operator.
- classmethod controlled_resource_decomp(ctrl_num_ctrl_wires, ctrl_num_ctrl_values, *args, **kwargs)[source]¶
Returns a list representing the resources for a controlled version of the operator.
- Parameters:
ctrl_num_ctrl_wires (int) – the number of qubits the operation is controlled on
ctrl_num_ctrl_values (int) – the number of control qubits, that are controlled when in the \(|0\rangle\) state
- classmethod default_adjoint_resource_decomp(*args, **kwargs)[source]¶
Returns a list representing the resources for the adjoint of the operator.
- classmethod default_controlled_resource_decomp(ctrl_num_ctrl_wires, ctrl_num_ctrl_values, *args, **kwargs)[source]¶
Returns a list representing the resources for a controlled version of the operator.
- Parameters:
ctrl_num_ctrl_wires (int) – the number of qubits the operation is controlled on
ctrl_num_ctrl_values (int) – the number of control qubits, that are controlled when in the \(|0\rangle\) state
- classmethod default_pow_resource_decomp(pow_z, *args, **kwargs)[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
- abstract classmethod default_resource_decomp(*args, **kwargs)[source]¶
Returns a list of actions that define the resources of the operator.
- classmethod pow_resource_decomp(pow_z, *args, **kwargs)[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
- queue(context=<class 'pennylane.queuing.QueuingManager'>)[source]¶
Append the operator to the Operator queue.
- 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.
- classmethod set_resources(new_func, override_type='base')[source]¶
Set a custom function to override the default resource decomposition.
This method allows users to replace any of the resource_decomp, adjoint_resource_decomp, ctrl_resource_decomp, or pow_resource_decomp methods globally for every instance of the class.