Skip to content

Fix endianness bug when indexing into OpenQASM classical types #2600

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

Merged
merged 4 commits into from
Jul 16, 2025

Conversation

orpuente-MS
Copy link
Contributor

@orpuente-MS orpuente-MS commented Jul 15, 2025

The following list of conclusions gathered from the OpenQASM spec will be useful while reviewing this PR. You can find this information in the official OpenQASM spec.

  1. Bitstring Literals are written in big-endian order.
    The spec says: "For convenience, classical registers can be assigned a text string containing zeros and ones of the same length as the size of the register. It is interpreted to assign each bit of the register to corresponding value 0 or 1 in the string, where the least-significant bit is on the right."

  2. Bit indexing of bit arrays literals is in big-endian order.
    The spec says: "classical registers are static arrays of bits."
    So, when doing bitarray[0] we are accessing the first element of that "static array", the leftmost element, which according to (1) is the most significant bit. Therefore, indexing of bitarrays is in big-endian order.

  3. Bit indexing of int, uint and angle is in little-endian order.
    The spec has this example where they hint that given int[32] myInt = 15;, myInt[0] evaluates to 1, that means that the least significant bit is stored first, and indexing of int, uint, and angle is in little-endian order.

  4. We index classical types by first casting them to bitarrays, which are in big-endian order, therefore when doing myInt[0], we need to do bit[32](myInt)[31]. Note that we transformed the index 0 into 32 - 0 - 1 = 31, effectively changing the endianness of the indexing operation.

@idavis idavis mentioned this pull request Jul 16, 2025
17 tasks
Copy link

Change in memory usage detected by benchmark.

Memory Report for eb6baf8

Test This Branch On Main Difference
compile core + standard lib 25361838 bytes 25361638 bytes 200 bytes

Copy link

Change in memory usage detected by benchmark.

Memory Report for 59c9d25

Test This Branch On Main Difference
compile core + standard lib 25361838 bytes 25361638 bytes 200 bytes

Copy link
Collaborator

@swernli swernli left a comment

Choose a reason for hiding this comment

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

Changes look good to me, with the usual caveat that endianness switches are always tricky and nuanced.

@orpuente-MS orpuente-MS added this pull request to the merge queue Jul 16, 2025
Merged via the queue into main with commit 9a0a39d Jul 16, 2025
18 checks passed
@orpuente-MS orpuente-MS deleted the oscarpuente/fix-endianness-bug branch July 16, 2025 21:05
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