Fix endianness bug when indexing into OpenQASM classical types #2600
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.
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.
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."
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.
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.We index classical types by first casting them to bitarrays, which are in big-endian order, therefore when doing
myInt[0]
, we need to dobit[32](myInt)[31]
. Note that we transformed the index0
into32 - 0 - 1 = 31
, effectively changing the endianness of the indexing operation.