Skip to content

Commit a0d4d6b

Browse files
authored
More explicit documentation of endianness (#1500)
1 parent 6de1e12 commit a0d4d6b

2 files changed

Lines changed: 24 additions & 7 deletions

File tree

qualtran/_infra/data_types.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
We often wish to write algorithms which operate on quantum data. One can think
1717
of quantum data types, similar to classical data types, where a collection of
1818
qubits can be used to represent a specific quantum data type (eg: a quantum
19-
integer of width 32 would comprise of 32 qubits, similar to a classical uint32
19+
integer of width 32 would comprise 32 qubits, similar to a classical uint32
2020
type). More generally, many current primitives and algorithms in qualtran
2121
implicitly expect registers which represent signed or unsigned integers,
2222
fixed-point (fp) numbers , or “classical registers” which store some classical
@@ -40,7 +40,7 @@
4040
type assuming the bitsizes match. QInt(32) == QAny(32), QInt(32) !=
4141
QFxp(32, 16). QInt(32) != QUInt(32).
4242
5. We assume a big endian convention for addressing QBits in registers
43-
throughout qualtran. Recall that in a big endian convention the most signficant
43+
throughout qualtran. Recall that in a big endian convention the most significant
4444
bit is at index 0. If you iterate through the bits in a register they will be
4545
yielded from most significant to least significant.
4646
6. Ones' complement integers are used extensively in quantum algorithms. We have
@@ -181,7 +181,7 @@ def __str__(self):
181181

182182
@attrs.frozen
183183
class QAny(QDType):
184-
"""Opaque bag-of-qbits type."""
184+
"""Opaque bag-of-qubits type."""
185185

186186
bitsize: SymbolicInt
187187

@@ -214,7 +214,10 @@ def assert_valid_classical_val_array(self, val_array, debug_str: str = 'val'):
214214
class QInt(QDType):
215215
"""Signed Integer of a given width bitsize.
216216
217-
A two's complement representation is assumed for negative integers.
217+
A two's complement representation is used for negative integers.
218+
219+
Here (and throughout Qualtran), we use a big-endian bit convention. The most significant
220+
bit is at index 0.
218221
219222
Attributes:
220223
bitsize: The number of qubits used to represent the integer.
@@ -275,7 +278,11 @@ def __str__(self):
275278
class QIntOnesComp(QDType):
276279
"""Signed Integer of a given width bitsize.
277280
278-
A ones' complement representation is assumed for negative integers.
281+
In contrast to `QInt`, this data type uses the ones' complement representation for negative
282+
integers.
283+
284+
Here (and throughout Qualtran), we use a big-endian bit convention. The most significant
285+
bit is at index 0.
279286
280287
Attributes:
281288
bitsize: The number of qubits used to represent the integer.
@@ -323,8 +330,11 @@ def assert_valid_classical_val(self, val, debug_str: str = 'val'):
323330
class QUInt(QDType):
324331
"""Unsigned integer of a given width bitsize which wraps around upon overflow.
325332
326-
Similar to unsigned integer types in C. Any intended wrap around effect is
327-
expected to be handled by the developer.
333+
Any intended wrap around effect is expected to be handled by the developer, similar
334+
to an unsigned integer type in C.
335+
336+
Here (and throughout Qualtran), we use a big-endian bit convention. The most significant
337+
bit is at index 0.
328338
329339
Attributes:
330340
bitsize: The number of qubits used to represent the integer.

qualtran/_infra/data_types_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,9 @@ def test_qint_to_and_from_bits():
295295
assert qint4.from_bits(qint4.to_bits(x)) == x
296296
assert list(qint4.to_bits(-2)) == [1, 1, 1, 0]
297297
assert list(QInt(4).to_bits(2)) == [0, 0, 1, 0]
298+
# MSB at lowest index -- big-endian
299+
assert qint4.from_bits([0, 0, 0, 1]) == 1
300+
assert qint4.from_bits([0, 0, 0, 1]) < qint4.from_bits([0, 1, 0, 0])
298301
assert qint4.from_bits(qint4.to_bits(-2)) == -2
299302
assert qint4.from_bits(qint4.to_bits(2)) == 2
300303
with pytest.raises(ValueError):
@@ -325,6 +328,10 @@ def test_quint_to_and_from_bits():
325328
assert [*quint4.get_classical_domain()] == [*range(0, 16)]
326329
assert list(quint4.to_bits(10)) == [1, 0, 1, 0]
327330
assert quint4.from_bits(quint4.to_bits(10)) == 10
331+
# MSB at lowest index -- big-endian
332+
assert quint4.from_bits([0, 0, 0, 1]) == 1
333+
assert quint4.from_bits([0, 0, 0, 1]) < quint4.from_bits([1, 0, 0, 0])
334+
328335
for x in range(16):
329336
assert quint4.from_bits(quint4.to_bits(x)) == x
330337
with pytest.raises(ValueError):

0 commit comments

Comments
 (0)