This repository was archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 180
Less-than comparison with a constant #318
Open
msoeken
wants to merge
19
commits into
main
Choose a base branch
from
msoeken/arithmetic
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
Changes from 14 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e359143
ApplyXorInPlace with BigInt constant.
msoeken 9545056
Comparison with constant.
msoeken fcd0536
Save one AND gate.
msoeken fda06a6
Save one temporary qubit.
msoeken e7320c9
Remove unused qubit.
msoeken 5253680
Simplify code.
msoeken 6c60299
Simplify code.
msoeken 35578cb
Save another qubit.
msoeken 912be5d
Remove temporary qubits for constants.
msoeken 5d62b1a
Simplify code.
msoeken f232d3e
Code finished.
msoeken fbf0742
Docs.
msoeken fa84d2d
Merge branch 'master' into msoeken/arithmetic
msoeken b5d93fe
Resources count test.
msoeken 03a187f
Apply suggestions from code review
msoeken 3c0bb3b
Refactor tests for greater-than comparison.
msoeken 35f3357
Merge branch 'msoeken/arithmetic' of github.com:microsoft/QuantumLibr…
msoeken 15f0dfe
Addressing Chris comments.
msoeken ff13033
Ceanup.
msoeken 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,12 @@ | |
| // Licensed under the MIT License. | ||
|
|
||
| namespace Microsoft.Quantum.Arithmetic { | ||
| open Microsoft.Quantum.Intrinsic; | ||
| open Microsoft.Quantum.Canon; | ||
| open Microsoft.Quantum.Arrays; | ||
| open Microsoft.Quantum.Canon; | ||
| open Microsoft.Quantum.Convert; | ||
| open Microsoft.Quantum.Diagnostics; | ||
| open Microsoft.Quantum.Intrinsic; | ||
| open Microsoft.Quantum.Logical; | ||
|
|
||
| /// # Summary | ||
| /// This operation tests if an integer represented by a register of qubits | ||
|
|
@@ -56,4 +59,95 @@ namespace Microsoft.Quantum.Arithmetic { | |
| } | ||
| } | ||
|
|
||
| /// # Summary | ||
| /// This operation tests if an integer represented by a register of qubits is | ||
| /// less than a big integer provided as a constant. | ||
| /// | ||
| /// # Description | ||
| /// Given two integers `x` and `c`, `x` stored in a qubit register, and `c` being | ||
| /// a big integer constant, this operation checks if they satisfy `x < c`. If true, | ||
| /// the output qubit is changed to state $\ket 1$. The output qubit is assumed to | ||
| /// be in state $\ket 0$ when the operation is being called. | ||
| /// | ||
| /// # Input | ||
| /// ## c | ||
| /// Non-negative constant number to compared to | ||
| /// ## x | ||
| /// Number in qubit register to compare | ||
| /// ## output | ||
| /// Qubit that stores the result of comparison (must be initialized to $\ket 0$) | ||
| /// | ||
| /// # Remark | ||
| /// This operation applies several optimizations in addition to the construction | ||
| /// described in the original work. Special cases are applied if `c` is $0$, | ||
| /// `c` is $2^n$, or `c` is $2^{n-1}$, where $n$ is the number of bits in `x`. | ||
| /// Qubits and AND gates are saved for each trailing `0` in the bit representation of | ||
| /// `c`. Further, one AND gate is saved for the least significant bit in `c`, which | ||
| /// is 1, after the trailing `0`s have been removed. Further, all qubits associated | ||
| /// to constant inputs in the construction of the original work are propagated and | ||
| /// not allocated in this implementation. | ||
| /// | ||
| /// # References | ||
| /// - Qubitization of Arbitrary Basis Quantum Chemistry Leveraging Sparsity and Low Rank Factorization | ||
| /// Dominic W. Berry, Craig Gidney, Mario Motta, Jarrod R. McClean, Ryan Babbush | ||
| /// Quantum 3, 208 (2019) | ||
| /// https://arxiv.org/abs/1902.02134v4 | ||
| operation LessThanConstantUsingRippleCarry(c : BigInt, x : LittleEndian, output : Qubit) | ||
|
||
| : Unit { | ||
msoeken marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| body (...) { | ||
| let bitwidth = Length(x!); | ||
| AssertAllZero([output]); | ||
| Fact(c >= 0L, "Constant input `c` must not be negative"); | ||
msoeken marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (c == 0L) { | ||
| // do nothing; output stays 0 | ||
| } elif (c >= (1L <<< bitwidth)) { | ||
| X(output); | ||
| } elif (c == (1L <<< (bitwidth - 1))) { | ||
| CNOT(Tail(x!), output); | ||
| X(output); | ||
| } else { | ||
| let bits = BigIntAsBoolArray(c); | ||
| let l = IndexOf(EqualB(true, _), bits); | ||
msoeken marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| using (tmpAnd = Qubit[bitwidth - 2 - l]) { | ||
| let tmpCarry = x![l..l] + tmpAnd; | ||
| within { | ||
| ApplyPauliFromBitString(PauliX, true, bits, x!); | ||
| for (i in 1..bitwidth - l - 2) { | ||
| within { | ||
| ApplyIfA(X, bits[i + l], tmpCarry[i - 1]); | ||
| } apply { | ||
| ApplyAnd(tmpCarry[i - 1], x![i + l], tmpCarry[i]); | ||
| } | ||
| CNOT(tmpCarry[i - 1], tmpCarry[i]); | ||
msoeken marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } apply { | ||
| within { | ||
| ApplyIfA(X, bits[bitwidth - 1], Tail(tmpCarry)); | ||
| } apply { | ||
| ApplyAnd(Tail(tmpCarry), Tail(x!), output); | ||
| } | ||
| CNOT(Tail(tmpCarry), output); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| adjoint self; | ||
|
|
||
| controlled (controls, ...) { | ||
| using (q = Qubit()) { | ||
| within { | ||
| LessThanConstantUsingRippleCarry(c, x, q); | ||
| } apply { | ||
| if (Length(controls) == 1) { | ||
| ApplyAnd(Head(controls), q, output); | ||
| } else { | ||
| Controlled X(controls + [q], output); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| adjoint controlled self; | ||
| } | ||
|
|
||
| } | ||
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
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.