Skip to content
Merged
Show file tree
Hide file tree
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 Sep 5, 2025
ca4cc33
Fixed tests
ddhawan11 Sep 5, 2025
b7ef85a
Merge branch 'add_wires_manager' into resource_base_classes
ddhawan11 Sep 5, 2025
1bc8b51
Merge branch 'master' into add_wires_manager
astralcai Sep 5, 2025
0d8e01e
restored labs before config
ddhawan11 Sep 5, 2025
f439f98
Update pennylane/estimator/resources_base.py
ddhawan11 Sep 5, 2025
3dbff81
Update pennylane/estimator/__init__.py
ddhawan11 Sep 5, 2025
218f639
Update pennylane/estimator/wires_manager.py
ddhawan11 Sep 5, 2025
042919e
Update pennylane/estimator/wires_manager.py
ddhawan11 Sep 5, 2025
04e1d83
Update pennylane/labs/resource_estimation/qubit_manager.py
ddhawan11 Sep 5, 2025
8719574
removed changes from config
ddhawan11 Sep 5, 2025
4244947
removed changes from config
ddhawan11 Sep 5, 2025
f342594
Addressed comments
ddhawan11 Sep 5, 2025
331263e
Addressed comments
ddhawan11 Sep 5, 2025
be78d76
docs update
ddhawan11 Sep 5, 2025
c57186d
Merge branch 'master' into add_wires_manager
ddhawan11 Sep 5, 2025
a7472bd
Fixed import
ddhawan11 Sep 5, 2025
8367754
Update tests/estimator/test_wires_manager.py
ddhawan11 Sep 5, 2025
cb76547
Update pennylane/estimator/wires_manager.py
ddhawan11 Sep 5, 2025
a335f20
Update pennylane/estimator/wires_manager.py
ddhawan11 Sep 5, 2025
b4d9d7c
Addressed comments
ddhawan11 Sep 5, 2025
c5b77ab
Merge branch 'master' into add_wires_manager
ddhawan11 Sep 5, 2025
3a3e8da
Merge branch 'master' into add_wires_manager
ddhawan11 Sep 5, 2025
82a35fa
Added module to toml file
ddhawan11 Sep 6, 2025
478d809
Fixed formatting
ddhawan11 Sep 6, 2025
7b4f443
Updated tach.toml
ddhawan11 Sep 8, 2025
eee7a7d
Merge branch 'master' into add_wires_manager
ddhawan11 Sep 8, 2025
95ebe1e
Update pennylane/estimator/wires_manager.py
ddhawan11 Sep 8, 2025
ce6cbeb
Update pennylane/estimator/wires_manager.py
ddhawan11 Sep 8, 2025
483841c
Merge branch 'master' into add_wires_manager
ddhawan11 Sep 8, 2025
03c2e6e
Addressed comments
ddhawan11 Sep 8, 2025
59b5359
minor docs change
ddhawan11 Sep 8, 2025
5c97177
Merge branch 'add_wires_manager' into resource_base_classes
ddhawan11 Sep 8, 2025
0b2d559
Update tests/estimator/test_wires_manager.py
ddhawan11 Sep 8, 2025
5cef8c9
Update tests/estimator/test_wires_manager.py
ddhawan11 Sep 8, 2025
e0231ab
Updated the naming
ddhawan11 Sep 8, 2025
c28e3de
Updated changelog
ddhawan11 Sep 8, 2025
7c51cf6
Added the rst file for documentation rendering
ddhawan11 Sep 8, 2025
3315419
Update pennylane/estimator/resources_base.py
ddhawan11 Sep 8, 2025
7f8d205
Update pennylane/estimator/resources_base.py
ddhawan11 Sep 8, 2025
bd3974d
Update pennylane/estimator/resources_base.py
ddhawan11 Sep 8, 2025
0d30274
Update doc/releases/changelog-dev.md
ddhawan11 Sep 8, 2025
52653db
Addressed comments
ddhawan11 Sep 8, 2025
789f970
Updated index.rst
ddhawan11 Sep 8, 2025
72c04fe
Merge branch 'master' into add_wires_manager
ddhawan11 Sep 8, 2025
50236bc
Merge branch 'add_wires_manager' into resource_base_classes
ddhawan11 Sep 8, 2025
d287069
Fixed rendering issues
ddhawan11 Sep 8, 2025
e65d160
Update pennylane/estimator/wires_manager.py
ddhawan11 Sep 8, 2025
a51c60b
Update pennylane/estimator/wires_manager.py
ddhawan11 Sep 8, 2025
284b510
Addressed comments
ddhawan11 Sep 8, 2025
0996697
formatting fixed
ddhawan11 Sep 8, 2025
c2e57e2
minor typo
ddhawan11 Sep 9, 2025
9501764
Started
ddhawan11 Sep 9, 2025
0ab6902
changed the signature for WireManager
ddhawan11 Sep 9, 2025
a9737eb
Merge branch 'master' into add_wires_manager
ddhawan11 Sep 9, 2025
3b1370c
Updated repr for the new signature
ddhawan11 Sep 9, 2025
1487fd4
Merge branch 'add_wires_manager' into resource_base_classes
ddhawan11 Sep 9, 2025
2f36ef5
Fixed tests
ddhawan11 Sep 9, 2025
967503c
Update doc/releases/changelog-dev.md
ddhawan11 Sep 9, 2025
da91f71
Update pennylane/estimator/wires_manager.py
ddhawan11 Sep 9, 2025
5c15be4
Update pennylane/estimator/wires_manager.py
ddhawan11 Sep 9, 2025
52e7c12
Update pennylane/estimator/wires_manager.py
ddhawan11 Sep 9, 2025
d1d8ae9
Update pennylane/estimator/wires_manager.py
ddhawan11 Sep 9, 2025
0a19e1e
fixed formatting
ddhawan11 Sep 9, 2025
a8d2adb
Retrigger CI
ddhawan11 Sep 9, 2025
2e9f4a4
changed test
ddhawan11 Sep 9, 2025
5a03ec2
Initial commit
ddhawan11 Sep 9, 2025
2ab363f
Merge branch 'resource_base_classes' into add_resource_operator
ddhawan11 Sep 9, 2025
ea18a46
Updated the resource decomps
ddhawan11 Sep 9, 2025
5706f67
Updated test
ddhawan11 Sep 9, 2025
37f878d
updated test coverage
ddhawan11 Sep 9, 2025
d201ff7
Fixed tests
ddhawan11 Sep 9, 2025
748242c
modified docs
ddhawan11 Sep 9, 2025
a71e084
Fixed formatting
ddhawan11 Sep 9, 2025
3e103cc
Updated changelog
ddhawan11 Sep 9, 2025
03d33d9
Fixed raise/return
ddhawan11 Sep 9, 2025
3f787e8
Update pennylane/estimator/resources_base.py
ddhawan11 Sep 9, 2025
6d1c89c
Update pennylane/estimator/resources_base.py
ddhawan11 Sep 9, 2025
acea5f2
Update pennylane/estimator/resources_base.py
ddhawan11 Sep 9, 2025
29720a8
increased test coverage
ddhawan11 Sep 9, 2025
8af85c2
Added sphinx directives
ddhawan11 Sep 9, 2025
eea8e35
Fixed indenting in examples
ddhawan11 Sep 9, 2025
dac56e9
Increased test coverage
ddhawan11 Sep 9, 2025
63149c4
Update pennylane/estimator/resources_base.py
ddhawan11 Sep 9, 2025
5eef474
Update pennylane/estimator/resources_base.py
ddhawan11 Sep 9, 2025
5c5b75e
Update pennylane/estimator/resources_base.py
ddhawan11 Sep 9, 2025
89b3d59
Update pennylane/estimator/resources_base.py
ddhawan11 Sep 9, 2025
097f699
Addressed some comments
ddhawan11 Sep 9, 2025
752f701
Applied the name change
ddhawan11 Sep 9, 2025
2cabef7
removed dunder methods
ddhawan11 Sep 10, 2025
9a26288
Merge remote-tracking branch 'origin/add_wires_manager' into resource…
ddhawan11 Sep 10, 2025
a8b090c
Made compatible with new convention
ddhawan11 Sep 10, 2025
2c75938
Merge branch 'master' into add_wires_manager
ddhawan11 Sep 10, 2025
254c0e2
Fixed tests
ddhawan11 Sep 10, 2025
573be8d
Updated test coverage
ddhawan11 Sep 10, 2025
ce27c38
Merge branch 'resource_base_classes' into add_resource_operator
ddhawan11 Sep 10, 2025
6890f1d
Docstring updated to remove dunder methods
ddhawan11 Sep 10, 2025
93a4a06
removing dunder methods
ddhawan11 Sep 10, 2025
7189cef
Update pennylane/estimator/wires_manager.py
ddhawan11 Sep 10, 2025
379dccd
Update pennylane/estimator/wires_manager.py
ddhawan11 Sep 10, 2025
1c54e8f
Update pennylane/estimator/wires_manager.py
ddhawan11 Sep 10, 2025
e372d24
Update pennylane/estimator/wires_manager.py
ddhawan11 Sep 10, 2025
29d728a
Update pennylane/estimator/wires_manager.py
ddhawan11 Sep 10, 2025
eabaa6d
Update pennylane/estimator/wires_manager.py
ddhawan11 Sep 10, 2025
05466e7
Update pennylane/estimator/wires_manager.py
ddhawan11 Sep 10, 2025
517378b
Update pennylane/estimator/wires_manager.py
ddhawan11 Sep 10, 2025
9a6458f
Update pennylane/estimator/wires_manager.py
ddhawan11 Sep 10, 2025
c3b6e18
Update pennylane/estimator/wires_manager.py
ddhawan11 Sep 10, 2025
c1ad55f
Update tests/estimator/test_wires_manager.py
ddhawan11 Sep 10, 2025
584bf3e
Update pennylane/estimator/wires_manager.py
ddhawan11 Sep 10, 2025
455f7d7
Update pennylane/estimator/wires_manager.py
ddhawan11 Sep 10, 2025
d4c8584
Update tests/estimator/test_wires_manager.py
ddhawan11 Sep 10, 2025
d8ca1b3
Update tests/estimator/test_wires_manager.py
ddhawan11 Sep 10, 2025
99a0254
Update pennylane/estimator/wires_manager.py
ddhawan11 Sep 10, 2025
b844903
Addressed comments and fixed formatting
ddhawan11 Sep 10, 2025
d60c50b
Merge branch 'master' into add_wires_manager
ddhawan11 Sep 10, 2025
46f9013
changed names
ddhawan11 Sep 10, 2025
58fedee
Formatting
ddhawan11 Sep 10, 2025
9109d10
Merge remote-tracking branch 'origin/add_wires_manager' into resource…
ddhawan11 Sep 10, 2025
682758f
Changed names
ddhawan11 Sep 10, 2025
4c32576
names reverted
ddhawan11 Sep 10, 2025
e7d9709
Merge remote-tracking branch 'origin/add_wires_manager' into resource…
ddhawan11 Sep 10, 2025
b3df33d
made consistent with wire manager
ddhawan11 Sep 10, 2025
f3b57a6
Update doc/releases/changelog-dev.md
ddhawan11 Sep 10, 2025
1c578cd
Update pennylane/estimator/resources_base.py
ddhawan11 Sep 10, 2025
4730de4
Update pennylane/estimator/resources_base.py
ddhawan11 Sep 10, 2025
38567e7
Update pennylane/estimator/resources_base.py
ddhawan11 Sep 10, 2025
46a49c7
Added checks and addressed some comments
ddhawan11 Sep 10, 2025
139baa0
add init
obliviateandsurrender Sep 10, 2025
4bf9634
undo
obliviateandsurrender Sep 10, 2025
9c5908f
undo2
obliviateandsurrender Sep 10, 2025
495b599
Addressed comments
ddhawan11 Sep 10, 2025
2d6c84b
minor
obliviateandsurrender Sep 10, 2025
8f1a906
Docs fix
ddhawan11 Sep 10, 2025
d34c07c
Doc fix
ddhawan11 Sep 10, 2025
9440470
Fixed examples
ddhawan11 Sep 10, 2025
a1a016a
Merge branch 'master' into resource_base_classes
ddhawan11 Sep 10, 2025
0c62dc2
Update pennylane/estimator/resources_base.py
ddhawan11 Sep 10, 2025
1f45815
Update pennylane/estimator/wires_manager.py
ddhawan11 Sep 10, 2025
691b086
Update doc/releases/changelog-dev.md
ddhawan11 Sep 10, 2025
e3a34cf
Updated signature and added gate_breakdown
ddhawan11 Sep 10, 2025
f74e0de
Update doc/releases/changelog-dev.md
ddhawan11 Sep 10, 2025
5509a33
Update doc/releases/changelog-dev.md
ddhawan11 Sep 10, 2025
3c14c58
Update doc/releases/changelog-dev.md
ddhawan11 Sep 10, 2025
5aa294c
Addressed comments
ddhawan11 Sep 11, 2025
440e8a6
added tests for gate_breakdown
ddhawan11 Sep 11, 2025
fef40ad
Merge branch 'master' into resource_base_classes
ddhawan11 Sep 11, 2025
4efa992
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 11, 2025
7884bd9
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 11, 2025
b7632bd
Formatting fix
ddhawan11 Sep 11, 2025
756ba5b
Fixed broken tests
ddhawan11 Sep 11, 2025
b24b361
Updated test coverage
ddhawan11 Sep 11, 2025
94c6359
Merge branch 'master' into resource_base_classes
ddhawan11 Sep 11, 2025
7714e95
Update doc/releases/changelog-dev.md
ddhawan11 Sep 11, 2025
961997d
Update pennylane/estimator/resources_base.py
ddhawan11 Sep 11, 2025
88f908b
Update pennylane/estimator/resources_base.py
ddhawan11 Sep 11, 2025
aee34f4
Addressed comments
ddhawan11 Sep 11, 2025
610232f
Merge remote-tracking branch 'origin/resource_base_classes' into add_…
ddhawan11 Sep 11, 2025
4a74350
Fixed broken tests
ddhawan11 Sep 11, 2025
42aeaf6
Increased test coverage
ddhawan11 Sep 11, 2025
3d3b31f
Addressed comments
ddhawan11 Sep 11, 2025
e17f607
Formatting fixed
ddhawan11 Sep 11, 2025
1c6b3ab
Update pennylane/estimator/resources_base.py
ddhawan11 Sep 11, 2025
7de428a
Update pennylane/estimator/resources_base.py
ddhawan11 Sep 11, 2025
b04037a
Update pennylane/estimator/resources_base.py
ddhawan11 Sep 11, 2025
9ce3219
Update pennylane/estimator/resources_base.py
ddhawan11 Sep 11, 2025
786b2f1
Addressed comments
ddhawan11 Sep 11, 2025
8d7647b
Merge remote-tracking branch 'origin/resource_base_classes' into add_…
ddhawan11 Sep 11, 2025
ffabc37
Fixed tests
ddhawan11 Sep 11, 2025
6a4e5cd
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 11, 2025
7a92fa5
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 11, 2025
95db020
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 11, 2025
ecae731
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 11, 2025
b6f273d
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 11, 2025
646dd2c
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 11, 2025
aeaab45
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 11, 2025
51fbc46
Update pennylane/exceptions.py
ddhawan11 Sep 11, 2025
d044d7d
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 11, 2025
bb68414
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 11, 2025
1db463d
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 11, 2025
147ad82
Addressed comments
ddhawan11 Sep 11, 2025
ce0a65b
Formatting fixed
ddhawan11 Sep 11, 2025
1eef56d
Updated changelog and addressed comments
ddhawan11 Sep 11, 2025
ec2f954
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 11, 2025
831042c
Update tests/estimator/test_resource_operator.py
ddhawan11 Sep 11, 2025
f613db6
Merge branch 'master' into add_resource_operator
ddhawan11 Sep 12, 2025
f29d72c
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 12, 2025
5393f36
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 12, 2025
e3f6ddb
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 12, 2025
8edb7cd
Update doc/releases/changelog-dev.md
ddhawan11 Sep 12, 2025
89066bd
fixed the num_wires
ddhawan11 Sep 12, 2025
7e71e0c
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 12, 2025
f2cb7eb
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 12, 2025
7956fc3
Merge branch 'master' into add_resource_operator
ddhawan11 Sep 12, 2025
6e470df
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 12, 2025
805ab6b
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 12, 2025
14479fb
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 12, 2025
a4714b5
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 12, 2025
c61d8f7
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 12, 2025
1ab8703
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 12, 2025
2599f8c
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 12, 2025
358d673
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 12, 2025
be8cca2
formatting
ddhawan11 Sep 12, 2025
70b0509
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 12, 2025
56a45e0
Merge branch 'master' into add_resource_operator
ddhawan11 Sep 12, 2025
bfa89c0
initial commit
austingmhuang Sep 12, 2025
33ef139
config
austingmhuang Sep 12, 2025
22fd097
Merge branch 'master' into move_config_class
austingmhuang Sep 12, 2025
ca7780d
tests
austingmhuang Sep 12, 2025
0134e6b
Merge branch 'master' into add_resource_operator
austingmhuang Sep 12, 2025
254cb8f
Merge branch 'add_resource_operator' into move_config_class
austingmhuang Sep 12, 2025
d1794dd
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 12, 2025
7b7fd65
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 12, 2025
7df1408
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 12, 2025
673c19b
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 12, 2025
dad849e
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 12, 2025
63413f6
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 12, 2025
9ff0b75
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 12, 2025
6f20b5f
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 12, 2025
571d45f
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 12, 2025
a786d08
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 12, 2025
59974f5
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 12, 2025
bc0ca5f
Update pennylane/estimator/resource_operator.py
ddhawan11 Sep 12, 2025
c4fbc75
Merge branch 'add_resource_operator' into move_config_class
austingmhuang Sep 12, 2025
c0cf92d
black
austingmhuang Sep 12, 2025
410869f
codecov
austingmhuang Sep 12, 2025
230c992
fixes to the docs
austingmhuang Sep 15, 2025
1a5cf95
fixes to docs
austingmhuang Sep 15, 2025
35b054c
fix
austingmhuang Sep 15, 2025
c72ef01
dcstring
austingmhuang Sep 15, 2025
71d0909
Merge branch 'master' into move_config_class
austingmhuang Sep 15, 2025
fbaa116
Delete tests/estimator/test_resource_operator.py
austingmhuang Sep 15, 2025
eaad1f2
Update pennylane/estimator/resource_config.py
austingmhuang Sep 15, 2025
9cb0972
Update pennylane/estimator/resource_config.py
austingmhuang Sep 15, 2025
12a5fdd
Update pennylane/estimator/resource_config.py
austingmhuang Sep 15, 2025
3690d6f
changelog
austingmhuang Sep 15, 2025
a2f1f46
black
austingmhuang Sep 15, 2025
53792e3
Update doc/releases/changelog-dev.md
austingmhuang Sep 15, 2025
3599ac1
Update pennylane/estimator/resource_config.py
austingmhuang Sep 15, 2025
ca091f1
Update pennylane/estimator/resource_config.py
austingmhuang Sep 15, 2025
c396d4a
Update pennylane/estimator/resource_config.py
austingmhuang Sep 15, 2025
e9c8f7d
Merge branch 'master' into move_config_class
austingmhuang Sep 15, 2025
9c5934b
changelog
austingmhuang Sep 15, 2025
e026508
black
austingmhuang Sep 15, 2025
6a87dc0
changelog move
austingmhuang Sep 15, 2025
f654fff
pylint
austingmhuang Sep 15, 2025
f05d92d
black
austingmhuang Sep 16, 2025
8789184
Update pennylane/estimator/resource_config.py
austingmhuang Sep 16, 2025
59007b2
Merge branch 'master' into move_config_class
austingmhuang Sep 16, 2025
5acdb8c
imports
austingmhuang Sep 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
[(#8205)](https://github.com/PennyLaneAI/pennylane/pull/8205)
* The test files were renamed to avoid the dual definition error with labs module.
[(#8261)](https://github.com/PennyLaneAI/pennylane/pull/8261)
* Added a new `ResourceConfig` class to store the configuration used for resource estimation, including operator precisions and custom decompositions.
[(#8259)](https://github.com/PennyLaneAI/pennylane/pull/8259)

* Dynamic wire allocation with `qml.allocation.allocate` can now be executed on `default.qubit`.
[(#7718)](https://github.com/PennyLaneAI/pennylane/pull/7718)
Expand Down
3 changes: 3 additions & 0 deletions pennylane/estimator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
:toctree: api

~Resources
~ResourceConfig
~ResourceOperator
~CompressedResourceOp
~GateCount
Expand All @@ -44,6 +45,8 @@

from .resources_base import Resources

from .resource_config import ResourceConfig

from .resource_operator import (
ResourceOperator,
CompressedResourceOp,
Expand Down
253 changes: 253 additions & 0 deletions pennylane/estimator/resource_config.py
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.
"""

def __init__(self) -> None:
_DEFAULT_PRECISION = 1e-9
_DEFAULT_BIT_PRECISION = 15
self.resource_op_precisions = {}
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:
: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**

.. 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
Loading