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 QGF data type for Galois Fields #1433

Merged
merged 12 commits into from
Oct 4, 2024
Merged

Conversation

tanujkhattar
Copy link
Collaborator

Part of #1419

Serialization support for new QDType will be added in a follow up PR

@tanujkhattar
Copy link
Collaborator Author

Ugh, looks like both galois and quimb use numba and that's causing issues with notebook tests that I'm unable to reproduce locally. @mpharrigan Do you have any idea how to fix this?

@mpharrigan
Copy link
Collaborator

usually depending on the same package would be a boon for compatibility! I'll investigate

@tanujkhattar
Copy link
Collaborator Author

From a brief look, I think quimb setting the number of numba threads explicitly may be causing the issue:

https://github.com/jcmgray/quimb/blob/8ecf9d6ef7d2055ebbae5f7dbb5aeb0e65c0d422/quimb/core.py#L37-L51

Numba also has a default but one uses os.cpu_count() and other uses psutil.cpu_count(). Both of these should be the same but looks like there have been discrepancies in the past.

https://github.com/numba/numba/blob/717308e5cb6f7ddcdbab9fadd9972b1bf1b83240/numba/core/config.py#L524C28-L524C37

I couldn't find anything in galois that explicitly sets number of threads for numba, but maybe there's some sneaky logic living somewhere.

Thanks for looking into this and let me know what you find!

@mpharrigan
Copy link
Collaborator

@tanujkhattar
Copy link
Collaborator Author

I guess the question is who is changing the NUMBA_NUM_THREADS after its precached, and galois is probably doing that given the failures are seen in this PR.

@mpharrigan
Copy link
Collaborator

I tried just importing the packages, which didn't trigger the bug. I tried disabling the parallel execution of notebooks which didn't fix the bug.

From the error message and synthesizing some of the info from @tanujkhattar 's investigation above: it looks like the original value of 2 is incorrect if we're trying to set it to the number of cores, which github documents as 4 https://docs.github.com/en/actions/using-github-hosted-runners/using-github-hosted-runners/about-github-hosted-runners#standard-github-hosted-runners-for-public-repositories which is the second value

@mpharrigan
Copy link
Collaborator

How about we patch the CI failure by explicitly setting the environment variable (see current status) and keep an eye out for this showing up on an actual computer in a reproducible way.

@tanujkhattar
Copy link
Collaborator Author

Thanks, that sounds like a good plan to me

@tanujkhattar
Copy link
Collaborator Author

@mpharrigan Since all tests are passing now, can you please take a look at the PR?

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.

we love types!

degree: The degree $m$ of the field $GF(p^{m})$. The degree must be a positive integer.

References
[Finite Field](https://en.wikipedia.org/wiki/Finite_field)
Copy link
Collaborator

Choose a reason for hiding this comment

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

how does this get rendered in the api docs? Usually we separate individual references with a blank newline; but I admit that that is primarily for the notebook-based doc system

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 blank newline between references.

A Finite Field or Galois Field is a field that contains finite number of elements. The order
of a finite field is the number of elements in the field, which is either a prime number or
a prime power. For every prime number $p$ and every positive integer $m$ there are fields of
order $p^{m}0$, all of which are isomorphic. When m=1, the finite field of order p can be
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this 0 supposed to be here? what does it mean?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

was a typo, removed.

as the irreducible polynomial.

Attributes:
characteristic: The characteristic $p$ of the field $GF(p^{m})$.
Copy link
Collaborator

Choose a reason for hiding this comment

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

personal preference, but I generally remove rendundant latex { }. You can just write $p^m$.

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

@@ -81,16 +81,23 @@ def _numpy_dtype_from_qdtype(dtype: 'QDType') -> Type:
return object


def _numpy_empty_reg_from_reg(reg: Register) -> np.ndarray:
Copy link
Collaborator

Choose a reason for hiding this comment

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

"nump" and "from reg" makes sense; but we're not returning an empty register per se. _numpy_empty_array_from_reg? _np_empty_from_reg? _empty_ndarray_from_reg?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this function be an overridable method on QDType? Not blocking this PR but if you think it is a good idea we can open an issue

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I think it's a good idea to have this function on then QDType. It would also make Fxp integration better I think; since Fxp also derives from numpy array and we don't make use of that. This is also why Fxp is slow for us I think.

I'll open an issue.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Did you end up opening one? There's something during review of #1436 that I'd like to note on the issue as well

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Opened #1437

@@ -53,6 +53,7 @@
QUInt,
BQUInt,
QMontgomeryUInt,
QGF,
Copy link
Collaborator

Choose a reason for hiding this comment

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

any concerns about effects on import time? I know nothing will be slower than quimb but you can theoretically avoid importing quimb sometimes

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Moved from galois import GF to a local import inside a method of QGF. Verified that time python -c 'import qualtran' has no degradation from this PR now.

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

Thanks for the review, I've addressed the nits. Merging now.

@tanujkhattar tanujkhattar merged commit 6036ad7 into quantumlib:main Oct 4, 2024
8 checks passed
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