Quantum Endianess


This is one of my favorite shirts, it’s basically a bunch of binary in a Microsoft logo…

My children love to ask, what does this mean? And as I think of an answer, my old consultant answer always come back: “It depends” 🙂 It depends on how you told the computer to interpret the binary. Looking at the binary below:

01010101010011000000000110010100000000011100100000000001100101000000000110111000000000011010010000000001110100000000000111100100000000

You could tell a computer, “Hey this is UTF-16” and suddenly that becomes the word “Serenity” to the computer. By default, UTF-16 encoding uses little endian on Windows.

If I change this to UTF-16 big endian, suddenly the binary is interpreted as “匀攀爀攀渀椀琀礀”. Quite a difference!

What is Most Significant?

The best example to start with is the familiar decimal numbers. For these numbers, the most significant digit is the one with the greatest value in the number. For the number 238, the number two is the most significant as it stands for 200. The least significant digit is the one with the least value in the number. For 238, this would be the number 8.

These definitions are the same in binary. The most significant bit (MSB) represents the greatest value of a binary string and the least significant bit (LSB) the least value. Let’s look at the number nine in binary:

\displaystyle {{1001}_{{binary}}}=1\times {{2}^{3}}+0\times {{2}^{2}}+0\times {{2}^{1}}+1\times {{2}^{0}}=8+0+0+1={{9}_{{decimal}}}

As you can see the left most one is the MSB as it represents 8 and the right most one is the LSB as it represents one.

Big-Endian vs Little-Endian

Endianness in our context has to do with how qubits are stored in memory. For this, qubits are analogous to bits and we will use the terms MSB and LSB. Big-Endian (BE) stores the MSB at the smallest memory address and the LSB at the biggest memory address. Little-Endian (LE) does the exact opposite in that the MSB is at the largest memory address and the LSB is at the smallest address.

The next question becomes, what is the smallest and biggest memory address? Well in Q#, qubits are stored in arrays called registers as shown in this code:

// allocate a register of n qubits in |0>
use qubits = Qubit[n]; 

Most of the time endianness does not matter because you can specify which qubits in the register to operate on such as:

CNOT(qubits[0], qubits[1]);

But for certain operations, it matters a lot! One of these is the Quantum Fourier Transform (QFT). The Q# operation QFT expects a quantum register in Big-Endian format. If however you want to call it on a Little-Endian register, you can use the operation QFTLE (the LE at the end is for Little-Endian). There are a number of these operations in Q# such as ApplyReversedOpBE and ApplyReversedOpLE.

Now to the big reveal, what is endianness in quantum computing, specifically Q#? Well, let’s think about it. If a register is nothing more than an array, then its lowest memory address will be at the index 0 and its highest index will be at the number n – 1. Using our definitions above, this means that BE implies that the MSB is stored at index 0 and the LSB is stored at index n – 1. Indeed, this is what the documentation says for the BigEndian type in Q#:

BigEndian user defined type

Register that encodes an unsigned integer in big-endian order. The qubit with index 0 encodes the highest bit of an unsigned integer.

newtype BigEndian = (Qubit[]);

The documentation for LittleEndian says the exact opposite.

Using these definitions, let’s look at the number |8> which in quantum is:

\displaystyle |8\rangle =|1\rangle \otimes |0\rangle \otimes |0\rangle \otimes |0\rangle =|1000\rangle

So |1⟩ is the MSB and the rightmost |0⟩ is the LSB. In an array or register it would look like this for BE:

\displaystyle \begin{array}{*{20}{c}} {Index} & 3 & 2 & 1 & 0 \\ {Qubit} & {|0\rangle } & {|0\rangle } & {|0\rangle } & {|1\rangle } \end{array}

And in LE like this:

\displaystyle \begin{array}{*{20}{c}} {Index} & 3 & 2 & 1 & 0 \\ {Qubit} & {|1\rangle } & {|0\rangle } & {|0\rangle } & {|0\rangle } \end{array}

I hope this has answered any questions you had about quantum endianness!

Leave a comment