qml.estimator.resources_base.Resources¶
- class Resources(zeroed, any_state=0, algo_wires=0, gate_types=None)[source]¶
Bases:
objectA container for the resources used throughout a quantum circuit.
The resources tracked include: number of wires (by state), and number of gates (by type).
- Parameters:
zeroed (int) – Number of zeroed state work wires.
any_state (int) – Number of work wires in an unknown state, default is
0.algo_wires (int) – Number of algorithmic wires, default value is
0.gate_types (dict) – A dictionary storing operations (
ResourceOperator) as keys and the number of times they are used in the circuit (int) as values.
Example
>>> from pennylane import estimator as qre >>> H = qre.resource_rep(qre.Hadamard) >>> X = qre.resource_rep(qre.X) >>> RX = qre.resource_rep(qre.RX, {"precision":1e-8}) >>> RX_2 = qre.resource_rep(qre.RX, {"precision":1e-6}) >>> gt = defaultdict(int, {H: 10, X:7, RX:2, RX_2:2}) >>> >>> res = qre.Resources(zeroed=3, gate_types=gt) >>> print(res) --- Resources: --- Total wires: 2 algorithmic wires: 0 allocated wires: 2 zero state: 2 any state: 0 Total gates : 21 'RX': 4, 'X': 7, 'Hadamard': 10 >>> >>> print(res.gate_breakdown()) RX total: 4 RX {'eps': 1e-08}: 2 RX {'eps': 1e-06}: 2 X total: 7 Hadamard total: 10
Attributes
Produce a dictionary which stores the gate counts using the operator names as keys.
- gate_counts¶
Produce a dictionary which stores the gate counts using the operator names as keys.
- Returns:
- A dictionary with operator names (str) as keys
and the number of occurances in the circuit (int) as values.
- Return type:
dict
Methods
add_parallel(other)Add two Resources objects in parallel.
add_series(other)Add two Resources objects in series.
gate_breakdown([gate_set])Generates a string breakdown of gate counts by type and parameters, optionally for a specific set of gates.
multiply_parallel(scalar)Scale a Resources object in parallel
multiply_series(scalar)Scale a Resources object in series
- add_parallel(other)[source]¶
Add two Resources objects in parallel.
When combining resources for parallel execution, the following rules apply:
Zeroed wires: The maximum of the
zeroedauxiliary wires is used, as they can be reused across parallel circuits.Any state wires: The
any_statewires are added together, as they cannot be reused between circuits.Algorithmic wires: The
algo_wiresare added together, as each circuit is a separate unit running simultaneously.Gates: The gates from each circuit are added together.
- Parameters:
other (
Resources) – other resource object to combine with- Returns:
combined resources
- Return type:
Resources
Example
>>> from pennylane import estimator as qre >>> gate_set = {"X", "Y", "Z", "CNOT", "T", "S", "Hadamard"} >>> res1 = qre.estimate(qre.Toffoli(), gate_set) >>> res2 = qre.estimate(qre.QFT(num_wires=4), gate_set) >>> res_in_parallel = res1.add_parallel(res2) >>> print(res_in_parallel) --- Resources: --- Total wires: 9 algorithmic wires: 7 allocated wires: 2 zero state: 2 any state: 0 Total gates : 838 'T': 796, 'CNOT': 28, 'Z': 2, 'S': 3, 'Hadamard': 9
- add_series(other)[source]¶
Add two Resources objects in series.
When combining resources for serial execution, the following rules apply:
Zeroed wires: The total
zeroedauxiliary wires are the maximum of thezeroedwires in each circuit, as they can be reused.Any state wires: The
any_statewires are added together, as they cannot be reused.Algorithmic wires: The total
algo_wiresare the maximum of thealgo_wiresfrom each circuit.Gates: The gates from each circuit are added together.
- Parameters:
other (
Resources) – the other resource object to add in series with- Returns:
combined resources
- Return type:
Resources
Example
>>> from pennylane import estimator as qre >>> gate_set = {"X", "Y", "Z", "CNOT", "T", "S", "Hadamard"} >>> res1 = qre.estimate(qre.Toffoli(), gate_set) >>> res2 = qre.estimate(qre.QFT(num_wires=4), gate_set) >>> res_in_series = res1.add_series(res2) >>> print(res_in_series) --- Resources: --- Total wires: 6 algorithmic wires: 4 allocated wires: 2 zero state: 2 any state: 0 Total gates : 838 'T': 796, 'CNOT': 28, 'Z': 2, 'S': 3, 'Hadamard': 9
- gate_breakdown(gate_set=None)[source]¶
Generates a string breakdown of gate counts by type and parameters, optionally for a specific set of gates.
- Parameters:
gate_set (list) – A list of gate names to break down. If
None, details will be provided for all gate types.
Example
>>> from pennylane import estimator as qre >>> res1 = qre.estimate(qre.SemiAdder(10)) >>> print(res1.gate_breakdown()) Toffoli total: 9 Toffoli {'elbow': 'left'}: 9 CNOT total: 60 Hadamard total: 27
- multiply_parallel(scalar)[source]¶
Scale a Resources object in parallel
- Parameters:
scalar (int) – integer value by which to scale the resources
- Returns:
scaled resources
- Return type:
Resources
Example
>>> from pennylane import estimator as qre >>> gate_set = {"X", "Y", "Z", "CNOT", "T", "S", "Hadamard"} >>> res1 = qre.estimate(qre.Toffoli(), gate_set) >>> res_in_parallel = res1.multiply_parallel(3) >>> print(res_in_parallel) --- Resources: --- Total wires: 11 algorithmic wires: 9 allocated wires: 2 zero state: 2 any state: 0 Total gates : 72 'T': 12, 'CNOT': 30, 'Z': 6, 'S': 9, 'Hadamard': 15
- multiply_series(scalar)[source]¶
Scale a Resources object in series
- Parameters:
scalar (int) – integer value by which to scale the resources
- Returns:
scaled resources
- Return type:
Resources
Example
>>> from pennylane import estimator as qre >>> gate_set = {"X", "Y", "Z", "CNOT", "T", "S", "Hadamard"} >>> res1 = qre.estimate(qre.Toffoli(), gate_set) >>> res_in_series = res1.multiply_series(3) >>> print(res_in_series) --- Resources: --- Total wires: 5 algorithmic wires: 3 allocated wires: 2 zero state: 2 any state: 0 Total gates : 72 'T': 12, 'CNOT': 30, 'Z': 6, 'S': 9, 'Hadamard': 15