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

Reverse Endianness #1079

Merged
merged 7 commits into from
Jan 30, 2024
Merged

Reverse Endianness #1079

merged 7 commits into from
Jan 30, 2024

Conversation

swernli
Copy link
Collaborator

@swernli swernli commented Jan 27, 2024

Based on feedback, we are reversing the endianness of ket displays from the QDK along with associated code. This includes reversing the display for state dumps from within Q#, in the debugger, and in Jupyter output. Included in this change is the revert of updates to kata content that tried to accomodate the different ordering between exercise text and the state display. The change also includes update to the new dump_operation in Python so that it is consistent with the endianness and outputs matrices that match the same conventions.

This change reverse the display order of our state vector indices to make it line up with order of qubit allocation and ordering in result arrays. It also updates the descrptive text around the state dump and changes the ket representation for output histograms.
@minestarks
Copy link
Member

@swernli Shouldn't we reverse the order of the formatted tables as well so they still go from 00001 => 11111?

@ScottCarda-MS
Copy link
Contributor

@swernli Shouldn't we reverse the order of the formatted tables as well so they still go from 00001 => 11111?

image
Yes I think that is what I'm seeing here as well.

@swernli
Copy link
Collaborator Author

swernli commented Jan 27, 2024

@swernli Shouldn't we reverse the order of the formatted tables as well so they still go from 00001 => 11111?

No, because that is the correct order for what those entries are. |10⟩ is the first entry in the state vector, |01⟩ is the second entry, etc.

If there were 4 qubits in equal superposition, it would go:

|0000⟩
|1000⟩
|0100⟩
|1100⟩
|0010⟩
|1010⟩
|0110⟩
|1110⟩
|0001⟩
|1001⟩
|0101⟩
|1101⟩
|0011⟩
|1011⟩
|0111⟩
|1111⟩

Copy link

Benchmark for 9daf12d

Click to view benchmark
Test Base PR %
Array append evaluation 878.1±14.74µs 868.1±31.81µs -1.14%
Array update evaluation 953.9±4.27µs 932.7±4.37µs -2.22%
Deutsch-Jozsa evaluation 31.3±0.23ms 31.5±1.62ms +0.64%
Large file parity evaluation 36.0±0.23ms 35.9±0.11ms -0.28%
Large input file 38.8±2.21ms 38.0±0.55ms -2.06%
Standard library 21.3±0.59ms 20.8±0.20ms -2.35%
Teleport evaluation 103.9±1.98µs 106.1±1.97µs +2.12%

@tcNickolas
Copy link
Member

The order of the printed entries is the second part of our discussion today, do we interpret bit strings as integers using little endian (least significant bit first) or big endian (most significant bit first)? For the CNOT matrix to have the right ("textbook") shape, we need to use big endian, so it makes sense to have the same for ordering the entries. (|0000⟩ then |0001⟩ then |0010⟩ etc.)

@ScottCarda-MS
Copy link
Contributor

ScottCarda-MS commented Jan 27, 2024

@swernli Shouldn't we reverse the order of the formatted tables as well so they still go from 00001 => 11111?

No, because that is the correct order for what those entries are. |10⟩ is the first entry in the state vector, |01⟩ is the second entry, etc.

If there were 4 qubits in equal superposition, it would go:

|0000⟩
|1000⟩
|0100⟩
|1100⟩
|0010⟩
|1010⟩
|0110⟩
|1110⟩
|0001⟩
|1001⟩
|0101⟩
|1101⟩
|0011⟩
|1011⟩
|0111⟩
|1111⟩

Is there something imposing a particular order other than what we want to display? Displaying the kets in that order seems confusing to me.

Co-authored-by: Scott Carda <55811729+ScottCarda-MS@users.noreply.github.com>
Copy link

Benchmark for 5b3dab7

Click to view benchmark
Test Base PR %
Array append evaluation 859.5±7.82µs 878.6±24.81µs +2.22%
Array update evaluation 935.2±8.90µs 952.4±9.75µs +1.84%
Deutsch-Jozsa evaluation 31.3±0.26ms 31.2±0.17ms -0.32%
Large file parity evaluation 35.9±0.10ms 36.0±0.18ms +0.28%
Large input file 39.9±1.05ms 39.3±0.86ms -1.50%
Standard library 21.6±0.44ms 21.4±0.40ms -0.93%
Teleport evaluation 102.2±1.56µs 105.5±2.51µs +3.23%

@swernli
Copy link
Collaborator Author

swernli commented Jan 27, 2024

Given the feedback and sticking with the theme of trying to make things less confusing, I'll make sure the state display is sorted according to the display format rather than the original state vector ordering.

@billti
Copy link
Member

billti commented Jan 27, 2024

No, because that is the correct order for what those entries are. |10⟩ is the first entry in the state vector, |01⟩ is the second entry, etc.

Is this correct? If |01> is the tensor product of |0> and |1>, then it is [1, 0]T x [0, 1]T which is [0, 1, 0, 0]T. In other words, the kets should be ordered from |00>, |01>, |10>, |11> to map to the way state vectors are typically ordered as sequential entries of [1, 0, 0, 0]T, [0, 1, 0, 0]T, [0, 0, 1, 0]T, and [0, 0, 0, 1]T.

Copy link

Benchmark for b096d88

Click to view benchmark
Test Base PR %
Array append evaluation 860.0±19.22µs 864.8±5.59µs +0.56%
Array update evaluation 933.6±5.90µs 935.6±15.54µs +0.21%
Deutsch-Jozsa evaluation 31.2±0.19ms 31.5±0.92ms +0.96%
Large file parity evaluation 35.9±0.14ms 36.2±0.27ms +0.84%
Large input file 37.6±0.74ms 38.3±1.17ms +1.86%
Standard library 21.2±0.59ms 20.9±0.27ms -1.42%
Teleport evaluation 102.5±1.90µs 104.7±2.46µs +2.15%

@swernli
Copy link
Collaborator Author

swernli commented Jan 27, 2024

Now all the endianness matches and the ordering of the state entries are also sequential:
image
The state display in the debugger also matches this too.

Copy link
Member

@minestarks minestarks left a comment

Choose a reason for hiding this comment

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

Phew, looks pretty thorough. Thanks!

@swernli swernli added this pull request to the merge queue Jan 29, 2024
@billti billti removed this pull request from the merge queue due to a manual request Jan 30, 2024
@billti billti merged commit 8f65f2e into main Jan 30, 2024
14 checks passed
@billti billti deleted the swernli/reverse-endianness branch January 30, 2024 00:11
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.

6 participants