-
Notifications
You must be signed in to change notification settings - Fork 715
Update SelectTHC template to allow batching of Givens rotations #8682
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
Open
ddhawan11
wants to merge
11
commits into
qubitizeTHC_bugfix
Choose a base branch
from
modify_qubitize_thc
base: qubitizeTHC_bugfix
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
02acc8e
Updated Select THC to take another argument
ddhawan11 e240c1d
Updated changelog
ddhawan11 f2e4b7a
Merge branch 'master' into modify_qubitize_thc
ddhawan11 4174624
Merge branch 'master' into modify_qubitize_thc
ddhawan11 851c2ac
Added tests for compact Hamiltonian classes
ddhawan11 8c91cbe
removed 1-norm stuff
ddhawan11 2311a81
Added another example
ddhawan11 970f626
Updated with bugfix PR
ddhawan11 59d3f9b
updated
ddhawan11 bf771bd
Merge branch 'qubitizeTHC_bugfix' into modify_qubitize_thc
ddhawan11 a715ead
Apply suggestion from @Jaybsoni
ddhawan11 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -43,6 +43,10 @@ class SelectTHC(ResourceOperator): | |||||
| Args: | ||||||
| thc_ham (:class:`~pennylane.estimator.compact_hamiltonian.THCHamiltonian`): A tensor hypercontracted | ||||||
| Hamiltonian on which the select operator is being applied. | ||||||
| batched_rotations (int | None): The maximum number of rotation angles to load simultaneously | ||||||
| into temporary quantum registers for processing in the Givens rotation circuits. | ||||||
| The default value of ``None`` loads all angles at once, where the batch size is equal to | ||||||
| the number of orbitals minus one. | ||||||
| rotation_precision (int): The number of bits used to represent the precision for loading | ||||||
| the rotation angles for basis rotation. The default value is set to ``15`` bits. | ||||||
| select_swap_depth (int | None): A parameter of :class:`~.pennylane.estimator.templates.subroutines.QROM` | ||||||
|
|
@@ -74,13 +78,35 @@ class SelectTHC(ResourceOperator): | |||||
| 'S': 80, | ||||||
| 'Hadamard': 6.406E+3 | ||||||
|
|
||||||
| Let's also see how the resources change when batched rotations are used: | ||||||
|
|
||||||
| >>> res = qre.estimate(qre.SelectTHC(thc_ham, batched_rotations=10, rotation_precision=15)) | ||||||
| >>> print(res) | ||||||
| --- Resources: --- | ||||||
| Total wires: 227 | ||||||
| algorithmic wires: 58 | ||||||
| allocated wires: 169 | ||||||
| zero state: 169 | ||||||
| any state: 0 | ||||||
| Total gates : 2.534E+4 | ||||||
| 'Toffoli': 2.633E+3, | ||||||
| 'CNOT': 1.440E+4, | ||||||
| 'X': 804.0, | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| 'Z': 41, | ||||||
| 'S': 80, | ||||||
| 'Hadamard': 7.378E+3 | ||||||
|
|
||||||
| We can see that by using batched rotations, the number of allocated wires decreases | ||||||
| significantly, at the cost of an increased number of Toffoli gates. | ||||||
|
|
||||||
| """ | ||||||
|
|
||||||
| resource_keys = {"thc_ham", "rotation_precision", "select_swap_depth"} | ||||||
| resource_keys = {"thc_ham", "batched_rotations", "rotation_precision", "select_swap_depth"} | ||||||
|
|
||||||
| def __init__( | ||||||
| self, | ||||||
| thc_ham: THCHamiltonian, | ||||||
| batched_rotations: int | None = None, | ||||||
| rotation_precision: int = 15, | ||||||
| select_swap_depth: int | None = None, | ||||||
| wires: WiresLike | None = None, | ||||||
|
|
@@ -97,7 +123,15 @@ def __init__( | |||||
| f"`rotation_precision` must be an integer, but type {type(rotation_precision)} was provided." | ||||||
| ) | ||||||
|
|
||||||
| if batched_rotations is not None and ( | ||||||
| batched_rotations <= 0 or batched_rotations > thc_ham.num_orbitals - 1 | ||||||
| ): | ||||||
| raise ValueError( | ||||||
| f"`batched_rotations` must be a positive integer less than the number of orbitals {thc_ham.num_orbitals}, but got {batched_rotations}." | ||||||
| ) | ||||||
|
|
||||||
| self.thc_ham = thc_ham | ||||||
| self.batched_rotations = batched_rotations | ||||||
| self.rotation_precision = rotation_precision | ||||||
| self.select_swap_depth = select_swap_depth | ||||||
| num_orb = thc_ham.num_orbitals | ||||||
|
|
@@ -127,13 +161,18 @@ def resource_params(self) -> dict: | |||||
| dict: A dictionary containing the resource parameters: | ||||||
| * thc_ham (:class:`~.pennylane.estimator.compact_hamiltonian.THCHamiltonian`): a tensor hypercontracted | ||||||
| Hamiltonian on which the select operator is being applied | ||||||
| * batched_rotations (int | None): The maximum number of rotation angles to load simultaneously | ||||||
| into temporary quantum registers for processing in the Givens rotation circuits. | ||||||
ddhawan11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| The default value of :code:`None` loads all angles at once, where the batch size is equal to | ||||||
| the number of orbitals minus one. | ||||||
| * rotation_precision (int): The number of bits used to represent the precision for loading | ||||||
| the rotation angles for basis rotation. The default value is set to ``15`` bits. | ||||||
| * select_swap_depth (int | None): A parameter of :class:`~.pennylane.estimator.templates.QROM` | ||||||
| used to trade-off extra wires for reduced circuit depth. Defaults to :code:`None`, which internally determines the optimal depth. | ||||||
| """ | ||||||
| return { | ||||||
| "thc_ham": self.thc_ham, | ||||||
| "batched_rotations": self.batched_rotations, | ||||||
| "rotation_precision": self.rotation_precision, | ||||||
| "select_swap_depth": self.select_swap_depth, | ||||||
| } | ||||||
|
|
@@ -142,6 +181,7 @@ def resource_params(self) -> dict: | |||||
| def resource_rep( | ||||||
| cls, | ||||||
| thc_ham: THCHamiltonian, | ||||||
| batched_rotations: int | None = None, | ||||||
| rotation_precision: int = 15, | ||||||
| select_swap_depth: int | None = None, | ||||||
| ) -> CompressedResourceOp: | ||||||
|
|
@@ -151,6 +191,10 @@ def resource_rep( | |||||
| Args: | ||||||
| thc_ham (:class:`~pennylane.estimator.compact_hamiltonian.THCHamiltonian`): A tensor hypercontracted | ||||||
| Hamiltonian on which the select operator is being applied. | ||||||
| batched_rotations (int | None): The maximum number of rotation angles to load simultaneously | ||||||
| into temporary quantum registers for processing in the Givens rotation circuits. | ||||||
| The default value of :code:`None` loads all angles at once, where the batch size is equal to | ||||||
| the number of orbitals minus one. | ||||||
| rotation_precision (int): The number of bits used to represent the precision for loading | ||||||
| the rotation angles for basis rotation. The default value is set to ``15`` bits. | ||||||
| select_swap_depth (int | None): A parameter of :class:`~.pennylane.estimator.templates.QROM` | ||||||
|
|
@@ -170,6 +214,14 @@ def resource_rep( | |||||
| raise TypeError( | ||||||
| f"`rotation_precision` must be an integer, but type {type(rotation_precision)} was provided." | ||||||
| ) | ||||||
|
|
||||||
| if batched_rotations is not None and ( | ||||||
| batched_rotations <= 0 or batched_rotations > thc_ham.num_orbitals - 1 | ||||||
| ): | ||||||
| raise ValueError( | ||||||
| f"`batched_rotations` must be a positive integer less than the number of orbitals {thc_ham.num_orbitals}, but got {batched_rotations}." | ||||||
| ) | ||||||
|
|
||||||
| num_orb = thc_ham.num_orbitals | ||||||
| tensor_rank = thc_ham.tensor_rank | ||||||
|
|
||||||
|
|
@@ -185,6 +237,7 @@ def resource_rep( | |||||
| num_wires = num_orb * 2 + 2 * int(np.ceil(math.log2(tensor_rank + 1))) + 6 | ||||||
| params = { | ||||||
| "thc_ham": thc_ham, | ||||||
| "batched_rotations": batched_rotations, | ||||||
| "rotation_precision": rotation_precision, | ||||||
| "select_swap_depth": select_swap_depth, | ||||||
| } | ||||||
|
|
@@ -193,7 +246,8 @@ def resource_rep( | |||||
| @classmethod | ||||||
| def resource_decomp( | ||||||
| cls, | ||||||
| thc_ham, | ||||||
| thc_ham: THCHamiltonian, | ||||||
| batched_rotations: int | None = None, | ||||||
| rotation_precision: int = 15, | ||||||
| select_swap_depth: int | None = None, | ||||||
| ) -> list[GateCount]: | ||||||
|
|
@@ -209,6 +263,10 @@ def resource_decomp( | |||||
| Args: | ||||||
| thc_ham (:class:`~pennylane.estimator.compact_hamiltonian.THCHamiltonian`): A tensor hypercontracted | ||||||
| Hamiltonian on which the select operator is being applied. | ||||||
| batched_rotations (int | None): The maximum number of rotation angles to load simultaneously | ||||||
| into temporary quantum registers for processing in the Givens rotation circuits. | ||||||
| The default value of :code:`None` loads all angles at once, where the batch size is equal to | ||||||
| the number of orbitals minus one. | ||||||
| rotation_precision (int): The number of bits used to represent the precision for loading | ||||||
| the rotation angles for basis rotation. The default value is set to ``15`` bits. | ||||||
| select_swap_depth (int | None): A parameter of :class:`~.pennylane.estimator.templates.QROM` | ||||||
|
|
@@ -234,20 +292,29 @@ def resource_decomp( | |||||
| cswap = resource_rep(qre.CSWAP) | ||||||
| gate_list.append(GateCount(cswap, 4 * num_orb)) | ||||||
|
|
||||||
| if batched_rotations is None: | ||||||
| batched_rotations = num_orb - 1 | ||||||
|
|
||||||
| restore_qrom = True | ||||||
| if batched_rotations == num_orb - 1: | ||||||
| restore_qrom = False | ||||||
|
|
||||||
| num_givens_blocks = np.ceil((num_orb - 1) / batched_rotations) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is what was causing the floats in the number of allocated qubits.
Suggested change
|
||||||
|
|
||||||
| # Data output for rotations | ||||||
| gate_list.append(Allocate(rotation_precision * (num_orb - 1))) | ||||||
| gate_list.append(Allocate(rotation_precision * batched_rotations)) | ||||||
|
|
||||||
| # QROM to load rotation angles for 2-body integrals | ||||||
| qrom_twobody = resource_rep( | ||||||
| qre.QROM, | ||||||
| { | ||||||
| "num_bitstrings": tensor_rank + num_orb, | ||||||
| "size_bitstring": rotation_precision * (num_orb - 1), | ||||||
| "restored": False, | ||||||
| "size_bitstring": rotation_precision * batched_rotations, | ||||||
| "restored": restore_qrom, | ||||||
| "select_swap_depth": select_swap_depth, | ||||||
| }, | ||||||
| ) | ||||||
| gate_list.append(GateCount(qrom_twobody)) | ||||||
| gate_list.append(GateCount(qrom_twobody, num_givens_blocks)) | ||||||
|
|
||||||
| # Cost for rotations by adding the rotations into the phase gradient state | ||||||
| semiadder = resource_rep( | ||||||
|
|
@@ -261,7 +328,7 @@ def resource_decomp( | |||||
| "num_zero_ctrl": 0, | ||||||
| }, | ||||||
| ) | ||||||
| gate_list.append(GateCount(semiadder, num_orb - 1)) | ||||||
| gate_list.append(GateCount(semiadder, batched_rotations)) | ||||||
|
|
||||||
| # Adjoint of QROM for 2-body integrals Eq. 34 in arXiv:2011.03494 | ||||||
| gate_list.append(GateCount(resource_rep(qre.Adjoint, {"base_cmpr_op": qrom_twobody}))) | ||||||
|
|
@@ -275,12 +342,12 @@ def resource_decomp( | |||||
| qre.QROM, | ||||||
| { | ||||||
| "num_bitstrings": tensor_rank, | ||||||
| "size_bitstring": rotation_precision * (num_orb - 1), | ||||||
| "restored": False, | ||||||
| "size_bitstring": rotation_precision * batched_rotations, | ||||||
| "restored": restore_qrom, | ||||||
| "select_swap_depth": select_swap_depth, | ||||||
| }, | ||||||
| ) | ||||||
| gate_list.append(GateCount(qrom_onebody)) | ||||||
| gate_list.append(GateCount(qrom_onebody, num_givens_blocks)) | ||||||
|
|
||||||
| # Cost for rotations by adding the rotations into the phase gradient state | ||||||
| gate_list.append(GateCount(semiadder, num_orb - 1)) | ||||||
|
|
||||||
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
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.