qp.resource.SpecsResources

class SpecsResources(*, counts, extra=<factory>, measurement_processes, num_wires, circuit_depth=None)[source]

Bases: Resources

Class for storing resource information for a quantum circuit. Contains attributes which store key resources such as gate counts, total wires, measurement processes, and circuit depth.

Note that this class is intended to be immutable. Modifying the attributes after creation may lead to unexpected behaviour.

Parameters:
  • counts (dict[str, int]) – A dictionary mapping gate names to their counts.

  • measurement_processes (dict[str, int]) – A dictionary mapping measurement processes to their counts.

  • num_wires (int) – The number of unique wires. For circuits that do not use dynamic wires, this should be equal to the number of device wires.

  • circuit_depth (int | None) – The depth of the circuit, or None if not computed. Defaults to None.

See also

Resources for the base class and its fields.

Warning

This class is intended to be immutable. Modifying the attributes after creation may lead to unexpected behaviour.

Methods have been provided to allow pretty-printing, as well as indexing into the object as a dictionary. See examples below.

Example

>>> from pennylane.resource import SpecsResources
>>> res = SpecsResources(
...     counts={'Hadamard': 1, 'CNOT': 1},
...     measurement_processes={'expval(PauliZ)': 1},
...     num_wires=2,
...     circuit_depth=2
... )
>>> print(res.total_quantum_operations)
2
>>> print(res["total_quantum_operations"])
2
>>> print(res)
Quantum operations:
- Total: 2
  - Hadamard: 1
  - CNOT: 1
Measurement processes:
- expval(PauliZ): 1
Total wires: 2
Circuit Depth: 2

measurement_processes

num_wires

circuit_depth

total_quantum_operations

counts

depth

The circuit depth (alias for circuit_depth).

extra

is_symbolic

Returns True if this object contains any symbolic variables, False otherwise.

quantum_operations

A dictionary mapping quantum operations to their counts (alias for counts).

vars

measurement_processes
num_wires
circuit_depth
total_quantum_operations
counts
depth

The circuit depth (alias for circuit_depth).

extra
is_symbolic

Returns True if this object contains any symbolic variables, False otherwise.

quantum_operations

A dictionary mapping quantum operations to their counts (alias for counts).

vars

subs([substitutions])

Substitute symbolic variables in the object with concrete values.

to_dict()

Convert this SpecsResources to a dictionary.

to_pretty_str([preindent])

Pretty string representation of this SpecsResources object.

subs(substitutions=None, **kwargs)

Substitute symbolic variables in the object with concrete values.

Automatically iterates over all fields of the dataclass and applies substitutions to any Expression instances, so derived classes do not have to explicitly implement this method unless they want to customize the behaviour.

Note

Every Expression leaf is substituted, including those nested to arbitrary depth within registered pytree containers (dict, list, tuple, …). The original container structure is preserved.

Parameters:

substitutions (dict[str, int | float] | None) – A dictionary mapping variable names to their values. If None, an empty dictionary is used. Additional keyword arguments can also be provided.

Example

>>> from pennylane.resource import SpecsResources, Expression
>>> res = SpecsResources(
...     counts={"CNOT": Expression({("x",): 1})},
...     measurement_processes={"expval(PauliX)": 1},
...     num_wires=2,
...     circuit_depth=1,
... )

Calling this method will return a new instance of the same type which has the same structure but with the symbolic variables substituted with the provided values. A substitution may either use a dictionary or keyword arguments:

>>> res.subs({"x": 3})
SpecsResources(counts={'CNOT': 3}, measurement_processes={'expval(PauliX)': 1}, num_wires=2, circuit_depth=1, total_quantum_operations=3)
>>> res.subs(x=3)
SpecsResources(counts={'CNOT': 3}, measurement_processes={'expval(PauliX)': 1}, num_wires=2, circuit_depth=1, total_quantum_operations=3)
to_dict()[source]

Convert this SpecsResources to a dictionary.

to_pretty_str(preindent=0)[source]

Pretty string representation of this SpecsResources object.

Parameters:

preindent (int) – Number of spaces to prepend to each line.

Returns:

A pretty representation of this object.

Return type:

str