-
Notifications
You must be signed in to change notification settings - Fork 706
Add resource config class to Pennylane core #8259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
277 commits
Select commit
Hold shift + click to select a range
b78e0da
Add base classes for resources
ddhawan11 ca4cc33
Fixed tests
ddhawan11 b7ef85a
Merge branch 'add_wires_manager' into resource_base_classes
ddhawan11 1bc8b51
Merge branch 'master' into add_wires_manager
astralcai 0d8e01e
restored labs before config
ddhawan11 f439f98
Update pennylane/estimator/resources_base.py
ddhawan11 3dbff81
Update pennylane/estimator/__init__.py
ddhawan11 218f639
Update pennylane/estimator/wires_manager.py
ddhawan11 042919e
Update pennylane/estimator/wires_manager.py
ddhawan11 04e1d83
Update pennylane/labs/resource_estimation/qubit_manager.py
ddhawan11 8719574
removed changes from config
ddhawan11 4244947
removed changes from config
ddhawan11 f342594
Addressed comments
ddhawan11 331263e
Addressed comments
ddhawan11 be78d76
docs update
ddhawan11 c57186d
Merge branch 'master' into add_wires_manager
ddhawan11 a7472bd
Fixed import
ddhawan11 8367754
Update tests/estimator/test_wires_manager.py
ddhawan11 cb76547
Update pennylane/estimator/wires_manager.py
ddhawan11 a335f20
Update pennylane/estimator/wires_manager.py
ddhawan11 b4d9d7c
Addressed comments
ddhawan11 c5b77ab
Merge branch 'master' into add_wires_manager
ddhawan11 3a3e8da
Merge branch 'master' into add_wires_manager
ddhawan11 82a35fa
Added module to toml file
ddhawan11 478d809
Fixed formatting
ddhawan11 7b4f443
Updated tach.toml
ddhawan11 eee7a7d
Merge branch 'master' into add_wires_manager
ddhawan11 95ebe1e
Update pennylane/estimator/wires_manager.py
ddhawan11 ce6cbeb
Update pennylane/estimator/wires_manager.py
ddhawan11 483841c
Merge branch 'master' into add_wires_manager
ddhawan11 03c2e6e
Addressed comments
ddhawan11 59b5359
minor docs change
ddhawan11 5c97177
Merge branch 'add_wires_manager' into resource_base_classes
ddhawan11 0b2d559
Update tests/estimator/test_wires_manager.py
ddhawan11 5cef8c9
Update tests/estimator/test_wires_manager.py
ddhawan11 e0231ab
Updated the naming
ddhawan11 c28e3de
Updated changelog
ddhawan11 7c51cf6
Added the rst file for documentation rendering
ddhawan11 3315419
Update pennylane/estimator/resources_base.py
ddhawan11 7f8d205
Update pennylane/estimator/resources_base.py
ddhawan11 bd3974d
Update pennylane/estimator/resources_base.py
ddhawan11 0d30274
Update doc/releases/changelog-dev.md
ddhawan11 52653db
Addressed comments
ddhawan11 789f970
Updated index.rst
ddhawan11 72c04fe
Merge branch 'master' into add_wires_manager
ddhawan11 50236bc
Merge branch 'add_wires_manager' into resource_base_classes
ddhawan11 d287069
Fixed rendering issues
ddhawan11 e65d160
Update pennylane/estimator/wires_manager.py
ddhawan11 a51c60b
Update pennylane/estimator/wires_manager.py
ddhawan11 284b510
Addressed comments
ddhawan11 0996697
formatting fixed
ddhawan11 c2e57e2
minor typo
ddhawan11 9501764
Started
ddhawan11 0ab6902
changed the signature for WireManager
ddhawan11 a9737eb
Merge branch 'master' into add_wires_manager
ddhawan11 3b1370c
Updated repr for the new signature
ddhawan11 1487fd4
Merge branch 'add_wires_manager' into resource_base_classes
ddhawan11 2f36ef5
Fixed tests
ddhawan11 967503c
Update doc/releases/changelog-dev.md
ddhawan11 da91f71
Update pennylane/estimator/wires_manager.py
ddhawan11 5c15be4
Update pennylane/estimator/wires_manager.py
ddhawan11 52e7c12
Update pennylane/estimator/wires_manager.py
ddhawan11 d1d8ae9
Update pennylane/estimator/wires_manager.py
ddhawan11 0a19e1e
fixed formatting
ddhawan11 a8d2adb
Retrigger CI
ddhawan11 2e9f4a4
changed test
ddhawan11 5a03ec2
Initial commit
ddhawan11 2ab363f
Merge branch 'resource_base_classes' into add_resource_operator
ddhawan11 ea18a46
Updated the resource decomps
ddhawan11 5706f67
Updated test
ddhawan11 37f878d
updated test coverage
ddhawan11 d201ff7
Fixed tests
ddhawan11 748242c
modified docs
ddhawan11 a71e084
Fixed formatting
ddhawan11 3e103cc
Updated changelog
ddhawan11 03d33d9
Fixed raise/return
ddhawan11 3f787e8
Update pennylane/estimator/resources_base.py
ddhawan11 6d1c89c
Update pennylane/estimator/resources_base.py
ddhawan11 acea5f2
Update pennylane/estimator/resources_base.py
ddhawan11 29720a8
increased test coverage
ddhawan11 8af85c2
Added sphinx directives
ddhawan11 eea8e35
Fixed indenting in examples
ddhawan11 dac56e9
Increased test coverage
ddhawan11 63149c4
Update pennylane/estimator/resources_base.py
ddhawan11 5eef474
Update pennylane/estimator/resources_base.py
ddhawan11 5c5b75e
Update pennylane/estimator/resources_base.py
ddhawan11 89b3d59
Update pennylane/estimator/resources_base.py
ddhawan11 097f699
Addressed some comments
ddhawan11 752f701
Applied the name change
ddhawan11 2cabef7
removed dunder methods
ddhawan11 9a26288
Merge remote-tracking branch 'origin/add_wires_manager' into resource…
ddhawan11 a8b090c
Made compatible with new convention
ddhawan11 2c75938
Merge branch 'master' into add_wires_manager
ddhawan11 254c0e2
Fixed tests
ddhawan11 573be8d
Updated test coverage
ddhawan11 ce27c38
Merge branch 'resource_base_classes' into add_resource_operator
ddhawan11 6890f1d
Docstring updated to remove dunder methods
ddhawan11 93a4a06
removing dunder methods
ddhawan11 7189cef
Update pennylane/estimator/wires_manager.py
ddhawan11 379dccd
Update pennylane/estimator/wires_manager.py
ddhawan11 1c54e8f
Update pennylane/estimator/wires_manager.py
ddhawan11 e372d24
Update pennylane/estimator/wires_manager.py
ddhawan11 29d728a
Update pennylane/estimator/wires_manager.py
ddhawan11 eabaa6d
Update pennylane/estimator/wires_manager.py
ddhawan11 05466e7
Update pennylane/estimator/wires_manager.py
ddhawan11 517378b
Update pennylane/estimator/wires_manager.py
ddhawan11 9a6458f
Update pennylane/estimator/wires_manager.py
ddhawan11 c3b6e18
Update pennylane/estimator/wires_manager.py
ddhawan11 c1ad55f
Update tests/estimator/test_wires_manager.py
ddhawan11 584bf3e
Update pennylane/estimator/wires_manager.py
ddhawan11 455f7d7
Update pennylane/estimator/wires_manager.py
ddhawan11 d4c8584
Update tests/estimator/test_wires_manager.py
ddhawan11 d8ca1b3
Update tests/estimator/test_wires_manager.py
ddhawan11 99a0254
Update pennylane/estimator/wires_manager.py
ddhawan11 b844903
Addressed comments and fixed formatting
ddhawan11 d60c50b
Merge branch 'master' into add_wires_manager
ddhawan11 46f9013
changed names
ddhawan11 58fedee
Formatting
ddhawan11 9109d10
Merge remote-tracking branch 'origin/add_wires_manager' into resource…
ddhawan11 682758f
Changed names
ddhawan11 4c32576
names reverted
ddhawan11 e7d9709
Merge remote-tracking branch 'origin/add_wires_manager' into resource…
ddhawan11 b3df33d
made consistent with wire manager
ddhawan11 f3b57a6
Update doc/releases/changelog-dev.md
ddhawan11 1c578cd
Update pennylane/estimator/resources_base.py
ddhawan11 4730de4
Update pennylane/estimator/resources_base.py
ddhawan11 38567e7
Update pennylane/estimator/resources_base.py
ddhawan11 46a49c7
Added checks and addressed some comments
ddhawan11 139baa0
add init
obliviateandsurrender 4bf9634
undo
obliviateandsurrender 9c5908f
undo2
obliviateandsurrender 495b599
Addressed comments
ddhawan11 2d6c84b
minor
obliviateandsurrender 8f1a906
Docs fix
ddhawan11 d34c07c
Doc fix
ddhawan11 9440470
Fixed examples
ddhawan11 a1a016a
Merge branch 'master' into resource_base_classes
ddhawan11 0c62dc2
Update pennylane/estimator/resources_base.py
ddhawan11 1f45815
Update pennylane/estimator/wires_manager.py
ddhawan11 691b086
Update doc/releases/changelog-dev.md
ddhawan11 e3a34cf
Updated signature and added gate_breakdown
ddhawan11 f74e0de
Update doc/releases/changelog-dev.md
ddhawan11 5509a33
Update doc/releases/changelog-dev.md
ddhawan11 3c14c58
Update doc/releases/changelog-dev.md
ddhawan11 5aa294c
Addressed comments
ddhawan11 440e8a6
added tests for gate_breakdown
ddhawan11 fef40ad
Merge branch 'master' into resource_base_classes
ddhawan11 4efa992
Update pennylane/estimator/resource_operator.py
ddhawan11 7884bd9
Update pennylane/estimator/resource_operator.py
ddhawan11 b7632bd
Formatting fix
ddhawan11 756ba5b
Fixed broken tests
ddhawan11 b24b361
Updated test coverage
ddhawan11 94c6359
Merge branch 'master' into resource_base_classes
ddhawan11 7714e95
Update doc/releases/changelog-dev.md
ddhawan11 961997d
Update pennylane/estimator/resources_base.py
ddhawan11 88f908b
Update pennylane/estimator/resources_base.py
ddhawan11 aee34f4
Addressed comments
ddhawan11 610232f
Merge remote-tracking branch 'origin/resource_base_classes' into add_…
ddhawan11 4a74350
Fixed broken tests
ddhawan11 42aeaf6
Increased test coverage
ddhawan11 3d3b31f
Addressed comments
ddhawan11 e17f607
Formatting fixed
ddhawan11 1c6b3ab
Update pennylane/estimator/resources_base.py
ddhawan11 7de428a
Update pennylane/estimator/resources_base.py
ddhawan11 b04037a
Update pennylane/estimator/resources_base.py
ddhawan11 9ce3219
Update pennylane/estimator/resources_base.py
ddhawan11 786b2f1
Addressed comments
ddhawan11 8d7647b
Merge remote-tracking branch 'origin/resource_base_classes' into add_…
ddhawan11 ffabc37
Fixed tests
ddhawan11 6a4e5cd
Update pennylane/estimator/resource_operator.py
ddhawan11 7a92fa5
Update pennylane/estimator/resource_operator.py
ddhawan11 95db020
Update pennylane/estimator/resource_operator.py
ddhawan11 ecae731
Update pennylane/estimator/resource_operator.py
ddhawan11 b6f273d
Update pennylane/estimator/resource_operator.py
ddhawan11 646dd2c
Update pennylane/estimator/resource_operator.py
ddhawan11 aeaab45
Update pennylane/estimator/resource_operator.py
ddhawan11 51fbc46
Update pennylane/exceptions.py
ddhawan11 d044d7d
Update pennylane/estimator/resource_operator.py
ddhawan11 bb68414
Update pennylane/estimator/resource_operator.py
ddhawan11 1db463d
Update pennylane/estimator/resource_operator.py
ddhawan11 147ad82
Addressed comments
ddhawan11 ce0a65b
Formatting fixed
ddhawan11 1eef56d
Updated changelog and addressed comments
ddhawan11 ec2f954
Update pennylane/estimator/resource_operator.py
ddhawan11 831042c
Update tests/estimator/test_resource_operator.py
ddhawan11 f613db6
Merge branch 'master' into add_resource_operator
ddhawan11 f29d72c
Update pennylane/estimator/resource_operator.py
ddhawan11 5393f36
Update pennylane/estimator/resource_operator.py
ddhawan11 e3f6ddb
Update pennylane/estimator/resource_operator.py
ddhawan11 8edb7cd
Update doc/releases/changelog-dev.md
ddhawan11 89066bd
fixed the num_wires
ddhawan11 7e71e0c
Update pennylane/estimator/resource_operator.py
ddhawan11 f2cb7eb
Update pennylane/estimator/resource_operator.py
ddhawan11 7956fc3
Merge branch 'master' into add_resource_operator
ddhawan11 6e470df
Update pennylane/estimator/resource_operator.py
ddhawan11 805ab6b
Update pennylane/estimator/resource_operator.py
ddhawan11 14479fb
Update pennylane/estimator/resource_operator.py
ddhawan11 a4714b5
Update pennylane/estimator/resource_operator.py
ddhawan11 c61d8f7
Update pennylane/estimator/resource_operator.py
ddhawan11 1ab8703
Update pennylane/estimator/resource_operator.py
ddhawan11 2599f8c
Update pennylane/estimator/resource_operator.py
ddhawan11 358d673
Update pennylane/estimator/resource_operator.py
ddhawan11 be8cca2
formatting
ddhawan11 70b0509
Update pennylane/estimator/resource_operator.py
ddhawan11 56a45e0
Merge branch 'master' into add_resource_operator
ddhawan11 bfa89c0
initial commit
austingmhuang 33ef139
config
austingmhuang 22fd097
Merge branch 'master' into move_config_class
austingmhuang ca7780d
tests
austingmhuang 0134e6b
Merge branch 'master' into add_resource_operator
austingmhuang 254cb8f
Merge branch 'add_resource_operator' into move_config_class
austingmhuang d1794dd
Update pennylane/estimator/resource_operator.py
ddhawan11 7b7fd65
Update pennylane/estimator/resource_operator.py
ddhawan11 7df1408
Update pennylane/estimator/resource_operator.py
ddhawan11 673c19b
Update pennylane/estimator/resource_operator.py
ddhawan11 dad849e
Update pennylane/estimator/resource_operator.py
ddhawan11 63413f6
Update pennylane/estimator/resource_operator.py
ddhawan11 9ff0b75
Update pennylane/estimator/resource_operator.py
ddhawan11 6f20b5f
Update pennylane/estimator/resource_operator.py
ddhawan11 571d45f
Update pennylane/estimator/resource_operator.py
ddhawan11 a786d08
Update pennylane/estimator/resource_operator.py
ddhawan11 59974f5
Update pennylane/estimator/resource_operator.py
ddhawan11 bc0ca5f
Update pennylane/estimator/resource_operator.py
ddhawan11 c4fbc75
Merge branch 'add_resource_operator' into move_config_class
austingmhuang c0cf92d
black
austingmhuang 410869f
codecov
austingmhuang 230c992
fixes to the docs
austingmhuang 1a5cf95
fixes to docs
austingmhuang 35b054c
fix
austingmhuang c72ef01
dcstring
austingmhuang 71d0909
Merge branch 'master' into move_config_class
austingmhuang fbaa116
Delete tests/estimator/test_resource_operator.py
austingmhuang eaad1f2
Update pennylane/estimator/resource_config.py
austingmhuang 9cb0972
Update pennylane/estimator/resource_config.py
austingmhuang 12a5fdd
Update pennylane/estimator/resource_config.py
austingmhuang 3690d6f
changelog
austingmhuang a2f1f46
black
austingmhuang 53792e3
Update doc/releases/changelog-dev.md
austingmhuang 3599ac1
Update pennylane/estimator/resource_config.py
austingmhuang ca091f1
Update pennylane/estimator/resource_config.py
austingmhuang c396d4a
Update pennylane/estimator/resource_config.py
austingmhuang e9c8f7d
Merge branch 'master' into move_config_class
austingmhuang 9c5934b
changelog
austingmhuang e026508
black
austingmhuang 6a87dc0
changelog move
austingmhuang f654fff
pylint
austingmhuang f05d92d
black
austingmhuang 8789184
Update pennylane/estimator/resource_config.py
austingmhuang 59007b2
Merge branch 'master' into move_config_class
austingmhuang 5acdb8c
imports
austingmhuang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,253 @@ | ||
| # Copyright 2025 Xanadu Quantum Technologies Inc. | ||
|
|
||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
|
|
||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| r"""This module contains the ResourceConfig class, which tracks the configuration for resource estimation""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Callable | ||
| from enum import StrEnum | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pennylane.estimator.resource_operator import ResourceOperator | ||
|
|
||
|
|
||
| class DecompositionType(StrEnum): | ||
| """Specifies the type of decomposition to override.""" | ||
|
|
||
| ADJOINT = "adj" | ||
| CONTROLLED = "ctrl" | ||
| POW = "pow" | ||
| BASE = "base" | ||
|
|
||
|
|
||
| class ResourceConfig: | ||
| """A container to track the configuration for precisions and custom decompositions for the | ||
| resource estimation pipeline. | ||
austingmhuang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
|
|
||
| def __init__(self) -> None: | ||
| _DEFAULT_PRECISION = 1e-9 | ||
| _DEFAULT_BIT_PRECISION = 15 | ||
| self.resource_op_precisions = {} | ||
andrijapau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self._custom_decomps = {} | ||
| self._adj_custom_decomps = {} | ||
| self._ctrl_custom_decomps = {} | ||
| self._pow_custom_decomps = {} | ||
|
|
||
| def __str__(self) -> str: | ||
| decomps = [op.__name__ for op in self._custom_decomps] | ||
| adj_decomps = [f"Adjoint({op.__name__})" for op in self._adj_custom_decomps] | ||
| ctrl_decomps = [f"Controlled({op.__name__})" for op in self._ctrl_custom_decomps] | ||
| pow_decomps = [f"Pow({op.__name__})" for op in self._pow_custom_decomps] | ||
|
|
||
| all_op_strings = decomps + adj_decomps + ctrl_decomps + pow_decomps | ||
| op_names = ", ".join(all_op_strings) | ||
|
|
||
| dict_items_str = ",\n".join( | ||
| f" {key.__name__}: {value!r}" for key, value in self.resource_op_precisions.items() | ||
| ) | ||
|
|
||
| formatted_dict = f"{{\n{dict_items_str}\n}}" | ||
|
|
||
| return ( | ||
| f"ResourceConfig(\n" | ||
| f" precisions = {formatted_dict},\n" | ||
| f" custom decomps = [{op_names}]\n)" | ||
| ) | ||
|
|
||
| def __repr__(self) -> str: | ||
| return f"ResourceConfig(precisions = {self.resource_op_precisions}, custom_decomps = {self._custom_decomps}, adj_custom_decomps = {self._adj_custom_decomps}, ctrl_custom_decomps = {self._ctrl_custom_decomps}, pow_custom_decomps = {self._pow_custom_decomps})" | ||
|
|
||
| def set_precision(self, op_type: type[ResourceOperator], precision: float) -> None: | ||
| r"""Sets the precision for a given resource operator. | ||
|
|
||
| This method updates the precision value for operators that use a single | ||
| tolerance parameter (e.g., for synthesis error). It will raise an error | ||
| if you attempt to set the precision for an operator that is not | ||
| configurable or uses bit-precisions. A negative precision will also raise an error. | ||
|
|
||
| Args: | ||
| op_type (type[ResourceOperator]): the operator class for which | ||
| to set the precision | ||
| precision (float): The desired precision tolerance. A smaller | ||
| value corresponds to a higher precision compilation, which may | ||
| increase the required gate counts. Must be greater than 0. | ||
|
|
||
| Raises: | ||
| ValueError: If ``op_type`` is not a configurable operator or if setting | ||
| the precision for it is not supported, or if ``precision`` is negative. | ||
|
|
||
| **Example** | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| import pennylane.estimator as qre | ||
|
|
||
| config = qre.ResourceConfig() | ||
|
|
||
| # Check the default precision | ||
| default = config.resource_op_precisions[qre.SelectPauliRot]['precision'] | ||
| print(f"Default precision for SelectPauliRot: {default}") | ||
|
|
||
| # Set a new precision | ||
| config.set_precision(qre.SelectPauliRot, precision=1e-5) | ||
| new = config.resource_op_precisions[qre.SelectPauliRot]['precision'] | ||
| print(f"New precision for SelectPauliRot: {new}") | ||
|
|
||
| .. code-block:: pycon | ||
|
|
||
| Default precision for SelectPauliRot: 1e-09 | ||
| New precision for SelectPauliRot: 1e-05 | ||
| """ | ||
| if precision < 0: | ||
| raise ValueError(f"Precision must be a non-negative value, but got {precision}.") | ||
|
|
||
| if op_type not in self.resource_op_precisions: | ||
| configurable_ops = sorted( | ||
| [ | ||
| op.__name__ | ||
| for op, params in self.resource_op_precisions.items() | ||
| if "precision" in params | ||
| ] | ||
| ) | ||
| raise ValueError( | ||
| f"{op_type.__name__} is not a configurable operator. " | ||
| f"Configurable operators are: {', '.join(configurable_ops)}" | ||
| ) | ||
|
|
||
| if "precision" not in self.resource_op_precisions[op_type]: | ||
| raise ValueError(f"Setting precision for {op_type.__name__} is not supported.") | ||
|
|
||
| self.resource_op_precisions[op_type]["precision"] = precision | ||
|
|
||
| def set_single_qubit_rot_precision(self, precision: float) -> None: | ||
| r"""Sets the synthesis precision for all single-qubit rotation gates. | ||
|
|
||
| This is a convenience method to update the synthesis precision tolerance | ||
| for all standard single-qubit rotation gates (and their | ||
| controlled versions) at once. The synthesis precision dictates the precision | ||
| for compiling rotation gates into a discrete gate set, which in turn | ||
| affects the number of gates required. | ||
|
|
||
| This method updates the ``precision`` value for the following operators: | ||
austingmhuang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| :class:`~.pennylane.estimator.RX`, :class:`~.pennylane.estimator.RY`, | ||
| :class:`~.pennylane.estimator.RZ`, :class:`~.pennylane.estimator.CRX`, | ||
| :class:`~.pennylane.estimator.CRY`, :class:`~.pennylane.estimator.CRZ`. | ||
|
|
||
| Args: | ||
| precision (float): The desired synthesis precision tolerance. A smaller | ||
| value corresponds to a higher precision compilation, which may | ||
| increase the required gate counts. Must be greater than ``0``. | ||
|
|
||
| Raises: | ||
| ValueError: If ``precision`` is a negative value. | ||
|
|
||
| **Example** | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| import pennylane.estimator as qre | ||
|
|
||
| config = qre.ResourceConfig() | ||
| print(f"Default RX precision: {config.resource_op_precisions[qre.RX]['precision']}") | ||
| print(f"Default RY precision: {config.resource_op_precisions[qre.RY]['precision']}") | ||
| print(f"Default RZ precision: {config.resource_op_precisions[qre.RZ]['precision']}") | ||
|
|
||
| config.set_single_qubit_rot_precision(1e-5) | ||
| print(f"Updated RX precision: {config.resource_op_precisions[qre.RX]['precision']}") | ||
| print(f"Updated RY precision: {config.resource_op_precisions[qre.RY]['precision']}") | ||
| print(f"Updated RZ precision: {config.resource_op_precisions[qre.RZ]['precision']}") | ||
|
|
||
| .. code-block:: pycon | ||
|
|
||
| Default RX precision: 1e-09 | ||
| Default RY precision: 1e-09 | ||
| Default RZ precision: 1e-09 | ||
| Updated RX precision: 1e-05 | ||
| Updated RY precision: 1e-05 | ||
| Updated RZ precision: 1e-05 | ||
| """ | ||
| if precision < 0: | ||
| raise ValueError(f"Precision must be a non-negative value, but got {precision}.") | ||
|
|
||
| def set_decomp( | ||
| self, | ||
| op_type: type[ResourceOperator], | ||
| decomp_func: Callable, | ||
| decomp_type: DecompositionType | None = DecompositionType.BASE, | ||
| ) -> None: | ||
| """Sets a custom function to override the default resource decomposition. | ||
|
|
||
| Args: | ||
| op_type (type[ResourceOperator]): the operator class whose decomposition is being overriden. | ||
| decomp_func (Callable): the new resource decomposition function to be set as default. | ||
| decomp_type (None | DecompositionType): the decomposition type to override. Options are | ||
| ``"adj"``, ``"pow"``, ``"ctrl"``, | ||
| and ``"base"``. Default is ``"base"``. | ||
|
|
||
| Raises: | ||
| ValueError: If ``decomp_type`` is not a valid decomposition type. | ||
|
|
||
| .. note:: | ||
|
|
||
| The new decomposition function ``decomp_func`` should have the same signature as the one it replaces. | ||
| Specifically, the signature should match the :code:`resource_keys` of the base resource | ||
| operator class being overriden. | ||
|
|
||
| **Example** | ||
austingmhuang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| .. code-block:: python | ||
|
|
||
| import pennylane.estimator as qre | ||
|
|
||
| def custom_res_decomp(**kwargs): | ||
| h = qre.resource_rep(qre.Hadamard) | ||
| s = qre.resource_rep(qre.S) | ||
| return [qre.GateCount(h, 1), qre.GateCount(s, 2)] | ||
|
|
||
| .. code-block:: pycon | ||
|
|
||
| >>> print(qre.estimate_resources(qre.X(), gate_set={"Hadamard", "Z", "S"})) | ||
| --- Resources: --- | ||
| Total qubits: 1 | ||
| Total gates : 4 | ||
| Qubit breakdown: | ||
| clean qubits: 0, dirty qubits: 0, algorithmic qubits: 1 | ||
| Gate breakdown: | ||
| {'Hadamard': 2, 'S': 2} | ||
| >>> config = qre.ResourceConfig() | ||
| >>> config.set_decomp(qre.X, custom_res_decomp) | ||
| >>> print(qre.estimate_resources(qre.X(), gate_set={"Hadamard", "Z", "S"}, config=config)) | ||
| --- Resources: --- | ||
| Total qubits: 1 | ||
| Total gates : 3 | ||
| Qubit breakdown: | ||
| clean qubits: 0, dirty qubits: 0, algorithmic qubits: 1 | ||
| Gate breakdown: | ||
| {'S': 1, 'Hadamard': 2} | ||
| """ | ||
| if decomp_type is None: | ||
| decomp_type = DecompositionType("base") | ||
| else: | ||
| decomp_type = DecompositionType(decomp_type) | ||
|
|
||
| if decomp_type == DecompositionType.ADJOINT: | ||
| self._adj_custom_decomps[op_type] = decomp_func | ||
| elif decomp_type == DecompositionType.CONTROLLED: | ||
| self._ctrl_custom_decomps[op_type] = decomp_func | ||
| elif decomp_type == DecompositionType.POW: | ||
| self._pow_custom_decomps[op_type] = decomp_func | ||
| elif decomp_type is None or decomp_type == DecompositionType.BASE: | ||
| self._custom_decomps[op_type] = decomp_func | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.