-
Notifications
You must be signed in to change notification settings - Fork 50
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
[ECC] Cost enhancements #1405
Open
mpharrigan
wants to merge
1
commit into
quantumlib:main
Choose a base branch
from
mpharrigan:2024-09/ecc-costs-2
base: main
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
[ECC] Cost enhancements #1405
Changes from all commits
Commits
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 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 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 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 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 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 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 |
---|---|---|
|
@@ -13,12 +13,14 @@ | |
# limitations under the License. | ||
|
||
from functools import cached_property | ||
from typing import Optional, Tuple | ||
from typing import Optional, Sequence, Tuple | ||
|
||
import attrs | ||
from attrs import frozen | ||
|
||
from qualtran import Bloq, CompositeBloq, DecomposeTypeError, QBit, Register, Side, Signature | ||
from qualtran import Bloq, DecomposeTypeError, QBit, QUInt, Register, Side, Signature | ||
from qualtran.drawing import RarrowTextBox, Text, WireSymbol | ||
from qualtran.resource_counting import CostKey, QubitCount | ||
|
||
|
||
@frozen | ||
|
@@ -41,5 +43,24 @@ def wire_symbol( | |
return RarrowTextBox('MeasQFT') | ||
raise ValueError(f'Unrecognized register name {reg.name}') | ||
|
||
def cost_attrs(self): | ||
return [('n', self.n)] | ||
def my_static_costs(self, cost_key: 'CostKey'): | ||
# TODO https://github.com/quantumlib/Qualtran/issues/1261 | ||
if cost_key == QubitCount(): | ||
return self.n | ||
return NotImplemented | ||
|
||
|
||
@frozen | ||
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. why a shim? why not use the QROM bloq itself (with |
||
class SimpleQROM(Bloq): | ||
selection_bitsize: int | ||
targets: Sequence[Tuple[str, int]] = attrs.field(converter=tuple) | ||
|
||
@cached_property | ||
def signature(self) -> 'Signature': | ||
return Signature( | ||
[Register('selection', QUInt(self.selection_bitsize))] | ||
+ [Register(tname, QUInt(tsize)) for tname, tsize in self.targets] | ||
) | ||
|
||
def __str__(self): | ||
return 'QROM' |
This file contains 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 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 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 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 |
---|---|---|
|
@@ -11,9 +11,9 @@ | |
# 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. | ||
|
||
import functools | ||
from functools import cached_property | ||
from typing import Dict | ||
from typing import Dict, Set, Union | ||
|
||
import sympy | ||
from attrs import frozen | ||
|
@@ -23,6 +23,7 @@ | |
bloq_example, | ||
BloqBuilder, | ||
BloqDocSpec, | ||
DecomposeNotImplementedError, | ||
DecomposeTypeError, | ||
QUInt, | ||
Register, | ||
|
@@ -34,7 +35,7 @@ | |
from qualtran.resource_counting import BloqCountDictT, SympySymbolAllocator | ||
|
||
from ._ecc_shims import MeasureQFT | ||
from .ec_add_r import ECAddR | ||
from .ec_add_r import ECAddR, ECWindowAddR | ||
from .ec_point import ECPoint | ||
|
||
|
||
|
@@ -48,27 +49,41 @@ class ECPhaseEstimateR(Bloq): | |
Args: | ||
n: The bitsize of the elliptic curve points' x and y registers. | ||
point: The elliptic curve point to phase estimate against. | ||
window_size: If non-zero, use windowed elliptic curve point addition. | ||
""" | ||
|
||
n: int | ||
point: ECPoint | ||
window_size: int = 0 | ||
|
||
@cached_property | ||
def signature(self) -> 'Signature': | ||
return Signature([Register('x', QUInt(self.n)), Register('y', QUInt(self.n))]) | ||
|
||
@property | ||
def ec_add(self) -> Union[ECAddR, ECWindowAddR]: | ||
if self.window_size == 0: | ||
return functools.partial(ECAddR, n=self.n) | ||
return functools.partial(ECWindowAddR, n=self.n, window_size=self.window_size) | ||
|
||
def build_composite_bloq(self, bb: 'BloqBuilder', x: Soquet, y: Soquet) -> Dict[str, 'SoquetT']: | ||
if isinstance(self.n, sympy.Expr): | ||
raise DecomposeTypeError("Cannot decompose symbolic `n`.") | ||
if self.window_size != 0: | ||
raise DecomposeNotImplementedError("We don't support a windowed addition circuit yet.") | ||
|
||
ctrl = [bb.add(PlusState()) for _ in range(self.n)] | ||
for i in range(self.n): | ||
ctrl[i], x, y = bb.add(ECAddR(n=self.n, R=2**i * self.point), ctrl=ctrl[i], x=x, y=y) | ||
|
||
bb.add(MeasureQFT(n=self.n), x=ctrl) | ||
return {'x': x, 'y': y} | ||
|
||
def build_call_graph(self, ssa: 'SympySymbolAllocator') -> 'BloqCountDictT': | ||
return {ECAddR(n=self.n, R=self.point): self.n, MeasureQFT(n=self.n): 1} | ||
def build_call_graph(self, ssa: 'SympySymbolAllocator') -> Set['BloqCountT']: | ||
return { | ||
(self.ec_add(R=self.point), self.n / (2**self.window_size)), | ||
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. float division? |
||
(MeasureQFT(n=self.n), 1), | ||
} | ||
|
||
def __str__(self) -> str: | ||
return f'PE${self.point}$' | ||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is no need for this shim. you can use the LinearDepthGreaterThan bloq