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 GF2Multiplication bloq for multiplication over GF($2^m$) #1436

Merged
merged 4 commits into from
Oct 4, 2024

Conversation

tanujkhattar
Copy link
Collaborator

Part of a larger effort to support galois field arithmetic in qualtran. xref #1419

Copy link
Collaborator

@mpharrigan mpharrigan left a comment

Choose a reason for hiding this comment

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

approval modulo some nits

qubits in each of the two input registers $a$ and $b$ that should be multiplied.

Registers:
- $x$: Input THRU register of size $m$ that stores elements from $GF(2^m)$.
Copy link
Collaborator

Choose a reason for hiding this comment

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

format this section like Args so it can be parsed by the autgen thing. The register names shouldn't be latex anywho since they are indeed the python strings 'x', 'y', and 'result' that we are documenting.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

ctrl, result[i] = bb.add(Toffoli(), ctrl=ctrl, target=result[i])
x[j], y[i - j] = ctrl[0], ctrl[1]

# Done :)
Copy link
Collaborator

Choose a reason for hiding this comment

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

:)

@@ -69,7 +69,7 @@ def adjoint(self) -> 'Bloq':
return Free(self.dtype, self.dirty)

def on_classical_vals(self) -> Dict[str, int]:
return {'reg': 0}
return {'reg': self.dtype.from_bits([0] * self.dtype.num_qubits)}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is fine for now, but let's add this to the forthcoming issue to track "friction related to adding new data types that aren't integers". Specifically: having allocate always allocate a bunch of zero bits that get interpreted in whatever encoding the dtype specifies is a fine choice but it's a choice nonetheless that we should probably discuss in the broader context of datatypes and it should be documented somewhere.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added to #1437

def test_gf2_multiplication_classical_sim(m):
bloq = GF2Multiplication(m)
GFM = GF(2**m)
for x in GFM.elements:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you should be able to use @NoureldinYosri 's helper function for writing tests like these

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What's the helper function? assert_consistent_classical_action ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

refactored using assert_consistent_classical_action

for y in GFM.elements:
xout, yout, resout = bloq.call_classically(x=x, y=y)
assert x == xout and y == yout
expected_res = x * y
Copy link
Collaborator

Choose a reason for hiding this comment

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

Usually, we can put the "reference behavior" as the bloq's on_classical_vals method and test call_classically's outputs on bloq vs bloq.decompose_bloq().

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added on_classical_vals to the bloq

bloq_autotester(_gf2_multiplication_symbolic)


@pytest.mark.parametrize('m', [2, 3, 4, 5])
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a borderline case, but consider factoring into a slow, exhaustive test marked with slow and a quick check that finishes in < 2 seconds.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

@tanujkhattar tanujkhattar enabled auto-merge (squash) October 4, 2024 21:15
@tanujkhattar
Copy link
Collaborator Author

Addressed all nits, merging now.

@tanujkhattar tanujkhattar merged commit 14e6fe7 into quantumlib:main Oct 4, 2024
8 checks passed
shab5 pushed a commit that referenced this pull request Oct 7, 2024
* Add GF2Multiplication for multiplication over GF(2^m)

* Fix formatting

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

Successfully merging this pull request may close these issues.

2 participants