Skip to content

Commit

Permalink
Specs dict fix (#4286)
Browse files Browse the repository at this point in the history
* Add copy function to SpecsDict.

* black

* Adjust docs to not throw warnings from deprecated specs keys.

* Formatting fix.

* update doc string

* update doc string

---------

Co-authored-by: Jay Soni <[email protected]>
Co-authored-by: Matthew Silverman <[email protected]>
  • Loading branch information
3 people authored Jun 23, 2023
1 parent 996730f commit d9779ed
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion doc/development/deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ Completed deprecation cycles
- Deprecated in v0.29
- Removed in v0.31

* ``qml.op_sum``` has been removed. Users should use ``qml.sum`` instead.
* ``qml.op_sum`` has been removed. Users should use ``qml.sum`` instead.

- Deprecated in v0.29.
- Removed in v0.31.
Expand Down
4 changes: 4 additions & 0 deletions pennylane/tape/qscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,10 @@ def __getitem__(self, item):
)
return super().__getitem__(item)

def copy(self):
"""Custom copy function to return a SpecsDict instead of a dict."""
return SpecsDict(self.items())


def make_qscript(fn, shots: Optional[Union[int, Sequence, Shots]] = None):
"""Returns a function that generates a qscript from a quantum function without any
Expand Down
4 changes: 2 additions & 2 deletions pennylane/transforms/optimization/pattern_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ def circuit():
In our case, it is possible to find three CNOTs and replace this pattern with only two CNOTs and therefore
optimizing the circuit. The number of CNOTs in the circuit is reduced by one.
>>> qml.specs(qnode)()["gate_types"]["CNOT"]
>>> qml.specs(qnode)()["resources"].gate_types["CNOT"]
4
>>> qml.specs(optimized_qnode)()["gate_types"]["CNOT"]
>>> qml.specs(optimized_qnode)()["resources"].gate_types["CNOT"]
3
>>> print(qml.draw(qnode)())
Expand Down
19 changes: 11 additions & 8 deletions pennylane/transforms/specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,23 @@ def specs_qnode(*args, **kwargs):
"""Returns information on the structure and makeup of provided QNode.
Dictionary keys:
* ``"num_operations"``
* ``"num_observables"``
* ``"num_diagonalizing_gates"``
* ``"gate_sizes"``: dictionary mapping gate number of wires to number of occurances
* ``"gate_types"``: dictionary mapping gate types to number of occurances
* ``"num_operations"`` number of operations in the qnode
* ``"num_observables"`` number of observables in the qnode
* ``"num_diagonalizing_gates"`` number of diagonalizing gates required for execution of the qnode
* ``"resources"``: a :class:`~.resource.Resources` object containing resource quantities used by the qnode
* ``"num_used_wires"``: number of wires used by the circuit
* ``"num_device_wires"``: number of wires in device
* ``"depth"``: longest path in directed acyclic graph representation
* ``"dev_short_name"``: name of QNode device
* ``"diff_method"``
* ``"device_name"``: name of QNode device
* ``"expansion_strategy"``: string specifying method for decomposing operations in the circuit
* ``"gradient_options"``: additional configurations for gradient computations
* ``"interface"``: autodiff framework to dispatch to for the qnode execution
* ``"diff_method"``: a string specifying the differntiation method
* ``"gradient_fn"``: executable to compute the gradient of the qnode
Potential Additional Information:
* ``"num_trainable_params"``: number of individual scalars that are trainable
* ``"num_parameter_shift_executions"``: number of times circuit will execute when
* ``"num_gradient_executions"``: number of times circuit will execute when
calculating the derivative
Returns:
Expand Down
10 changes: 10 additions & 0 deletions tests/tape/test_qscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,16 @@ def test_specs_tape(self, make_script):
assert specs["num_trainable_params"] == 5
assert specs["depth"] == 3

def test_specs_copy(self, make_script):
"""Test that the copy method of specs retuns a SpecsDict."""
qs = make_script
copied_specs = qs.specs.copy()

assert isinstance(copied_specs, qml.tape.qscript.SpecsDict)
with pytest.warns(UserWarning, match="key is deprecated and will be removed"):
for k, v in qs.specs.items():
assert copied_specs[k] == v

@pytest.mark.parametrize(
"shots, total_shots, shot_vector",
[
Expand Down

0 comments on commit d9779ed

Please sign in to comment.