-
Notifications
You must be signed in to change notification settings - Fork 71
Add Always
metabloq to simplify decompositions with compute-uncompute pairs
#1605
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
mpharrigan
merged 18 commits into
quantumlib:main
from
anurudhp:2025/03/26-ctrl-ham-sim
Apr 28, 2025
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d778b7a
Add `IgnoringCtrl` metabloq; fix ctrl ham-sim decomp
anurudhp d15612a
Merge branch 'main' into 2025/03/26-ctrl-ham-sim
anurudhp 964ac53
serialize
anurudhp cbaa58c
Merge branch 'main' into 2025/03/26-ctrl-ham-sim
anurudhp a7f999b
rename to `Always`
anurudhp 3ad4c4c
add tests
anurudhp b2cca50
docs + bloq example
anurudhp e3155c1
add small example
anurudhp 4aad22b
more docstring
anurudhp dd7ed76
render caution
anurudhp e8e53ad
simplify `AddK` using `Always`
anurudhp f958b26
Merge branch 'main' into 2025/03/26-ctrl-ham-sim
anurudhp 127adb3
cleanup `AddK` doc
anurudhp 183b830
fix notebook
anurudhp d2eacc9
more elaborate docstring in `AddK`
anurudhp b82dabb
update caution
anurudhp f1733e4
Merge branch 'main' into 2025/03/26-ctrl-ham-sim
anurudhp a0e7956
fix notebook
anurudhp 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
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,99 @@ | ||
# Copyright 2025 Google LLC | ||
# | ||
# 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 | ||
# | ||
# https://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. | ||
from typing import Iterable, Optional, Sequence | ||
|
||
import attrs | ||
|
||
from qualtran import ( | ||
AddControlledT, | ||
Bloq, | ||
bloq_example, | ||
BloqBuilder, | ||
BloqDocSpec, | ||
CtrlSpec, | ||
Signature, | ||
SoquetT, | ||
) | ||
|
||
|
||
@attrs.frozen | ||
class Always(Bloq): | ||
"""Always execute the wrapped bloq, even when a controlled version is requested | ||
|
||
A controlled version of a composite bloq in turn controls each subbloq in the decomposition. | ||
Wrapping a particular subbloq with `Always` lets it bypass the controls, | ||
i.e. it is "always" executed, irrespective of what the controls are. | ||
|
||
This is useful when writing decompositions for two known patterns: | ||
|
||
1. Compute-uncompute pairs: If a decomposition contains a compute-uncompute pair, | ||
then for a controlled version, we only need to control the rest of the bloqs. | ||
Wrapping both the compute and uncompute bloqs in `Always` lets them bypass the controls. | ||
|
||
2. Controlled data-loading: For example, in the `AddK` bloq which adds a constant `k` to the | ||
register, we (controlled) load the value `k` into a quantum register, and "always" perform an | ||
quantum-quantum addition using `Add`, and unload `k`. Here wrapping the middle `Add` with | ||
`Always` lets it bypass controls, e.g. when using `AddK.controlled()`. | ||
|
||
This simplifies the decompositions by avoiding the need to explicitly define the decomposition | ||
for the controlled version of bloq. | ||
|
||
**Caution:** This wrapper should be used with care. It is up to the bloq author to ensure that | ||
the controlled version of a decomposition containing `Always` bloqs still respects the | ||
controlled protocol. That is, ignoring controls on these subbloqs wrapped in `Always` should not | ||
change the action of the overall bloq with respect to the reference controlled implementation. | ||
|
||
Args: | ||
subbloq: The bloq to always apply, irrespective of any controls. | ||
""" | ||
|
||
subbloq: Bloq | ||
|
||
@property | ||
def signature(self) -> 'Signature': | ||
return self.subbloq.signature | ||
|
||
def build_composite_bloq(self, bb: 'BloqBuilder', **soqs: 'SoquetT') -> dict[str, 'SoquetT']: | ||
return bb.add_d(self.subbloq, **soqs) | ||
|
||
def get_ctrl_system( | ||
self, ctrl_spec: Optional['CtrlSpec'] = None | ||
) -> tuple['Bloq', 'AddControlledT']: | ||
"""Pass-through the control registers as-is""" | ||
|
||
def add_controlled( | ||
bb: 'BloqBuilder', ctrl_soqs: Sequence['SoquetT'], in_soqs: dict[str, 'SoquetT'] | ||
) -> tuple[Iterable['SoquetT'], Iterable['SoquetT']]: | ||
out_soqs = bb.add_t(self, **in_soqs) | ||
return ctrl_soqs, out_soqs | ||
|
||
return self, add_controlled | ||
|
||
def adjoint(self) -> 'Always': | ||
return Always(self.subbloq.adjoint()) | ||
|
||
def __str__(self) -> str: | ||
return str(self.subbloq) | ||
|
||
|
||
@bloq_example | ||
def _always_and() -> Always: | ||
from qualtran.bloqs.mcmt.and_bloq import And | ||
|
||
always_and = Always(And()) | ||
|
||
return always_and | ||
|
||
|
||
_ALWAYS_DOC = BloqDocSpec(bloq_cls=Always, examples=[_always_and]) |
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,24 @@ | ||
# Copyright 2025 Google LLC | ||
# | ||
# 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 | ||
# | ||
# https://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. | ||
from qualtran.bloqs.bookkeeping.always import _always_and, Always | ||
from qualtran.bloqs.for_testing import TestAtom | ||
|
||
|
||
def test_example(bloq_autotester): | ||
bloq_autotester(_always_and) | ||
|
||
|
||
def test_always(): | ||
bloq = Always(TestAtom()) | ||
assert bloq.controlled() == bloq |
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.