qml.labs.resource_estimation.Resources¶
- class Resources(qubit_manager, gate_types=None)[source]¶
Bases:
objectA container to track and update the resources used throughout a quantum circuit. The resources tracked include number of gates, number of wires, and gate types.
- Parameters:
qubit_manager (QubitManager) – A qubit tracker class which contains the number of available work wires, categorized as clean, dirty or algorithmic wires.
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
The resources can be accessed as class attributes. Additionally, the
Resourcesinstance can be nicely displayed in the console.>>> H = re.resource_rep(re.ResourceHadamard) >>> X = re.resource_rep(re.ResourceX) >>> Y = re.resource_rep(re.ResourceY) >>> qm = re.QubitManager(work_wires=3) >>> gt = defaultdict(int, {H: 10, X:7, Y:3}) >>> >>> res = re.Resources(qubit_manager=qm, gate_types=gt) >>> print(res) --- Resources: --- Total qubits: 3 Total gates : 20 Qubit breakdown: clean qubits: 3, dirty qubits: 0, algorithmic qubits: 0 Gate breakdown: {'Hadamard': 10, 'X': 7, 'Y': 3}
Usage Details
The
Resourcesobject supports arithmetic operations which allow for quick addition and multiplication of resources. When combining resources, we can make a simplifying assumption about how they are applied in a quantum circuit (in series or in parallel).When assuming the circuits were executed in series, the number of algorithmic qubits add together. When assuming the circuits were executed in parallel, the maximum of each set of algorithmic qubits is used. The clean auxiliary qubits can be reused between the circuits, and thus we always use the maximum of each set when combining the resources. Finally, the dirty qubits cannot be reused between circuits, thus we always add them together.
from collections import defaultdict # Resource reps for each operator: H = re.resource_rep(re.ResourceHadamard) X = re.resource_rep(re.ResourceX) Z = re.resource_rep(re.ResourceZ) CNOT = re.resource_rep(re.ResourceCNOT) # state of qubits: qm1 = re.QubitManager(work_wires={"clean":2, "dirty":1}, algo_wires=3) qm2 = re.QubitManager(work_wires={"clean":1, "dirty":2}, algo_wires=4) # state of gates: gt1 = defaultdict(int, {H: 10, X:5, CNOT:2}) gt2 = defaultdict(int, {H: 15, Z:5, CNOT:4}) # resources: res1 = re.Resources(qubit_manager=qm1, gate_types=gt1) res2 = re.Resources(qubit_manager=qm2, gate_types=gt2)
>>> print(res1) --- Resources: --- Total qubits: 6 Total gates : 17 Qubit breakdown: clean qubits: 2, dirty qubits: 1, algorithmic qubits: 3 Gate breakdown: {'Hadamard': 10, 'X': 5, 'CNOT': 2} >>> print(res2) --- Resources: --- Total qubits: 7 Total gates : 24 Qubit breakdown: clean qubits: 1, dirty qubits: 2, algorithmic qubits: 4 Gate breakdown: {'Hadamard': 15, 'Z': 5, 'CNOT': 4}
Specifically, users can add together two instances of resources using the
+and&operators. These represent combining the resources assuming the circuits were executed in series or parallel respectively.>>> res_in_series = res1 + res2 >>> print(res_in_series) --- Resources: --- Total qubits: 9 Total gates : 41 Qubit breakdown: clean qubits: 2, dirty qubits: 3, algorithmic qubits: 4 Gate breakdown: {'Hadamard': 25, 'X': 5, 'CNOT': 6, 'Z': 5} >>> res_in_parallel = res1 & res2 >>> print(res_in_parallel) --- Resources: --- Total qubits: 12 Total gates : 41 Qubit breakdown: clean qubits: 2, dirty qubits: 3, algorithmic qubits: 7 Gate breakdown: {'Hadamard': 25, 'X': 5, 'CNOT': 6, 'Z': 5}
Similarly, users can scale up the resources for an operator by some integer factor using the
*and@operators. These represent scaling the resources assuming the circuits were executed in series or parallel respectively.>>> res_in_series = 5 * res1 >>> print(res_in_series) --- Resources: --- Total qubits: 10 Total gates : 85 Qubit breakdown: clean qubits: 2, dirty qubits: 5, algorithmic qubits: 3 Gate breakdown: {'Hadamard': 50, 'X': 25, 'CNOT': 10} >>> res_in_parallel = 5 @ res1 >>> print(res_in_parallel) --- Resources: --- Total qubits: 22 Total gates : 85 Qubit breakdown: clean qubits: 2, dirty qubits: 5, algorithmic qubits: 15 Gate breakdown: {'Hadamard': 50, 'X': 25, 'CNOT': 10}
Attributes
Produce a dictionary which stores the gate counts using the operator names as keys.
- clean_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