Skip to content
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

Add SubtractFrom to subtract from a register in place #1158

Merged
merged 8 commits into from
Jul 19, 2024

Conversation

charlesyuan314
Copy link
Contributor

@charlesyuan314 charlesyuan314 commented Jul 18, 2024

Add a new version of Subtract that instead of taking $|a\rangle|b\rangle \mapsto |a\rangle|a-b\rangle$, takes it to $|a\rangle|b-a\rangle$. This is essentially equivalent to the statement b -= a.

The implementation uses an additional Negate, which could likely be improved, but this is a first approach. For now, it only supports a and b register of equal dtype.

The reason we need this for Subtract but not for Add is because addition is commutative and we could always swap the operands, but we can't for subtraction.

def build_composite_bloq(self, bb: 'BloqBuilder', a: Soquet, b: Soquet) -> Dict[str, 'SoquetT']:
neg = Negate(self.dtype)
b = bb.add(neg, x=b) # a, -b
b, a = bb.add_t(Add(self.dtype_as_unsigned, self.dtype_as_unsigned), a=b, b=a) # a - b, -b
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can still use bb.add() in these cases

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. My IDE type checker (Pylance) prefers that I use add_t though for the slightly finer grained information.

@mpharrigan
Copy link
Collaborator

Makes sense to me. @tanujkhattar do you have any qualms with this approach? also cc @NoureldinYosri for your inventory of arithmetic bloqs

qualtran/bloqs/arithmetic/subtraction.py Outdated Show resolved Hide resolved
qualtran/bloqs/arithmetic/subtraction.py Outdated Show resolved Hide resolved
qualtran/bloqs/arithmetic/subtraction.py Show resolved Hide resolved
Co-authored-by: Anurudh Peduri <7265746+anurudhp@users.noreply.github.com>
@charlesyuan314 charlesyuan314 enabled auto-merge (squash) July 19, 2024 17:17
@charlesyuan314 charlesyuan314 merged commit 22b3d5e into quantumlib:main Jul 19, 2024
7 checks passed
@charlesyuan314 charlesyuan314 deleted the sub-from branch July 19, 2024 18:04
@NoureldinYosri
Copy link
Contributor

this could have been just an extra flag to Subtract

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants