Skip to content

Commit

Permalink
BoundedQUInt -> BQUInt (#1363)
Browse files Browse the repository at this point in the history
* BoundedQUInt -> BQUInt.

* Update proto defs too.

* Fix serialization.
  • Loading branch information
fdmalone authored Aug 29, 2024
1 parent 6f378c0 commit 5c2c915
Show file tree
Hide file tree
Showing 48 changed files with 294 additions and 359 deletions.
2 changes: 1 addition & 1 deletion qualtran/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
QFxp,
QIntOnesComp,
QUInt,
BoundedQUInt,
BQUInt,
QMontgomeryUInt,
)

Expand Down
6 changes: 3 additions & 3 deletions qualtran/_infra/composite_bloq_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
SoquetT,
)
from qualtran._infra.composite_bloq import _create_binst_graph, _get_dangling_soquets
from qualtran._infra.data_types import BoundedQUInt, QAny, QBit, QFxp, QUInt
from qualtran._infra.data_types import BQUInt, QAny, QBit, QFxp, QUInt
from qualtran.bloqs.basic_gates import CNOT, IntEffect, ZeroEffect
from qualtran.bloqs.bookkeeping import Join
from qualtran.bloqs.for_testing.atom import TestAtom, TestTwoBitOp
Expand Down Expand Up @@ -515,7 +515,7 @@ def test_flatten():

def test_type_error():
bb = BloqBuilder()
a = bb.add_register_from_dtype('i', BoundedQUInt(4, 3))
a = bb.add_register_from_dtype('i', BQUInt(4, 3))
b = bb.add_register_from_dtype('j', QFxp(8, 6, True))
c = bb.add_register_from_dtype('k', QFxp(8, 8))
d = bb.add_register_from_dtype('l', QUInt(8))
Expand All @@ -527,7 +527,7 @@ def test_type_error():
with pytest.raises(BloqError, match=r'.*register dtypes are not consistent.*'):
b, a = bb.add(TestQFxp(), xx=b, yy=a)
bb = BloqBuilder()
a = bb.add_register_from_dtype('i', BoundedQUInt(4, 3))
a = bb.add_register_from_dtype('i', BQUInt(4, 3))
b = bb.add_register_from_dtype('j', QFxp(8, 6, True))
c = bb.add_register_from_dtype('k', QFxp(8, 8))
d = bb.add_register_from_dtype('l', QUInt(8))
Expand Down
12 changes: 6 additions & 6 deletions qualtran/_infra/data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,12 @@ def __str__(self):


@attrs.frozen
class BoundedQUInt(QDType):
class BQUInt(QDType):
"""Unsigned integer whose values are bounded within a range.
LCU methods often make use of coherent for-loops via UnaryIteration, iterating over a range
of values stored as a superposition over the `SELECT` register. Such (nested) coherent
for-loops can be represented using a `Tuple[Register(dtype=BoundedQUInt),
for-loops can be represented using a `Tuple[Register(dtype=BQUInt),
...]` where the i'th entry stores the bitsize and iteration length of i'th
nested for-loop.
Expand Down Expand Up @@ -466,7 +466,7 @@ def __attrs_post_init__(self):
if not self.is_symbolic():
if self.iteration_length > 2**self.bitsize:
raise ValueError(
"BoundedQUInt iteration length is too large for given bitsize. "
"BQUInt iteration length is too large for given bitsize. "
f"{self.iteration_length} vs {2**self.bitsize}"
)

Expand Down Expand Up @@ -810,8 +810,8 @@ def assert_valid_classical_val_array(
raise ValueError(f"Too-large classical values encountered in {debug_str}")


QAnyInt = (QInt, QUInt, BoundedQUInt, QMontgomeryUInt)
QAnyUInt = (QUInt, BoundedQUInt, QMontgomeryUInt)
QAnyInt = (QInt, QUInt, BQUInt, QMontgomeryUInt)
QAnyUInt = (QUInt, BQUInt, QMontgomeryUInt)


class QDTypeCheckingSeverity(Enum):
Expand All @@ -827,7 +827,7 @@ class QDTypeCheckingSeverity(Enum):
"""Strictly enforce type checking between registers. Only single bit conversions are allowed."""


def _check_uint_fxp_consistent(a: Union[QUInt, BoundedQUInt, QMontgomeryUInt], b: QFxp) -> bool:
def _check_uint_fxp_consistent(a: Union[QUInt, BQUInt, QMontgomeryUInt], b: QFxp) -> bool:
"""A uint / qfxp is consistent with a whole or totally fractional unsigned QFxp."""
if b.signed:
return False
Expand Down
42 changes: 20 additions & 22 deletions qualtran/_infra/data_types_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from qualtran.symbolics import is_symbolic

from .data_types import (
BoundedQUInt,
BQUInt,
check_dtypes_consistent,
QAny,
QAnyInt,
Expand Down Expand Up @@ -75,21 +75,21 @@ def test_quint():


def test_bounded_quint():
qint_3 = BoundedQUInt(2, 3)
assert str(qint_3) == 'BoundedQUInt(2, 3)'
qint_3 = BQUInt(2, 3)
assert str(qint_3) == 'BQUInt(2, 3)'

assert qint_3.bitsize == 2
assert qint_3.iteration_length == 3
with pytest.raises(ValueError, match="BoundedQUInt iteration length.*"):
BoundedQUInt(4, 76)
with pytest.raises(ValueError, match="BQUInt iteration length.*"):
BQUInt(4, 76)
n = sympy.symbols('x')
l = sympy.symbols('l')
qint_8 = BoundedQUInt(n, l)
qint_8 = BQUInt(n, l)
assert qint_8.num_qubits == n
assert qint_8.iteration_length == l
assert is_symbolic(BoundedQUInt(sympy.Symbol('x'), 2))
assert is_symbolic(BoundedQUInt(2, sympy.Symbol('x')))
assert is_symbolic(BoundedQUInt(*sympy.symbols('x y')))
assert is_symbolic(BQUInt(sympy.Symbol('x'), 2))
assert is_symbolic(BQUInt(2, sympy.Symbol('x')))
assert is_symbolic(BQUInt(*sympy.symbols('x y')))


def test_qfxp():
Expand Down Expand Up @@ -135,13 +135,13 @@ def test_qmontgomeryuint():
assert is_symbolic(QMontgomeryUInt(sympy.Symbol('x')))


@pytest.mark.parametrize('qdtype', [QBit(), QInt(4), QUInt(4), BoundedQUInt(3, 5)])
@pytest.mark.parametrize('qdtype', [QBit(), QInt(4), QUInt(4), BQUInt(3, 5)])
def test_domain_and_validation(qdtype: QDType):
for v in qdtype.get_classical_domain():
qdtype.assert_valid_classical_val(v)


@pytest.mark.parametrize('qdtype', [QBit(), QInt(4), QUInt(4), BoundedQUInt(3, 5)])
@pytest.mark.parametrize('qdtype', [QBit(), QInt(4), QUInt(4), BQUInt(3, 5)])
def test_domain_and_validation_arr(qdtype: QDType):
arr = np.array(list(qdtype.get_classical_domain()))
qdtype.assert_valid_classical_val_array(arr)
Expand All @@ -158,10 +158,10 @@ def test_validation_errs():
QUInt(3).assert_valid_classical_val(8)

with pytest.raises(ValueError):
BoundedQUInt(3, 5).assert_valid_classical_val(-1)
BQUInt(3, 5).assert_valid_classical_val(-1)

with pytest.raises(ValueError):
BoundedQUInt(3, 5).assert_valid_classical_val(6)
BQUInt(3, 5).assert_valid_classical_val(6)

with pytest.raises(ValueError):
QInt(4).assert_valid_classical_val(-9)
Expand All @@ -183,15 +183,13 @@ def test_validate_arrays():
QBit().assert_valid_classical_val_array(arr)


@pytest.mark.parametrize(
'qdtype', [QIntOnesComp(4), QFxp(4, 4), QInt(4), QUInt(4), BoundedQUInt(4, 5)]
)
@pytest.mark.parametrize('qdtype', [QIntOnesComp(4), QFxp(4, 4), QInt(4), QUInt(4), BQUInt(4, 5)])
def test_qany_consistency(qdtype):
# All Types with correct bitsize are ok with QAny
assert check_dtypes_consistent(qdtype, QAny(4))


@pytest.mark.parametrize('qdtype', [QUInt(4), BoundedQUInt(4, 5), QMontgomeryUInt(4)])
@pytest.mark.parametrize('qdtype', [QUInt(4), BQUInt(4, 5), QMontgomeryUInt(4)])
def test_type_errors_fxp_uint(qdtype):
assert check_dtypes_consistent(qdtype, QFxp(4, 4))
assert check_dtypes_consistent(qdtype, QFxp(4, 0))
Expand All @@ -213,10 +211,10 @@ def test_type_errors_fxp():


@pytest.mark.parametrize(
'qdtype_a', [QUInt(4), BoundedQUInt(4, 5), QMontgomeryUInt(4), QInt(4), QIntOnesComp(4)]
'qdtype_a', [QUInt(4), BQUInt(4, 5), QMontgomeryUInt(4), QInt(4), QIntOnesComp(4)]
)
@pytest.mark.parametrize(
'qdtype_b', [QUInt(4), BoundedQUInt(4, 5), QMontgomeryUInt(4), QInt(4), QIntOnesComp(4)]
'qdtype_b', [QUInt(4), BQUInt(4, 5), QMontgomeryUInt(4), QInt(4), QIntOnesComp(4)]
)
def test_type_errors_matrix(qdtype_a, qdtype_b):
if qdtype_a == qdtype_b:
Expand All @@ -233,7 +231,7 @@ def test_single_qubit_consistency():
assert check_dtypes_consistent(QBit(), QInt(1))
assert check_dtypes_consistent(QInt(1), QBit())
assert check_dtypes_consistent(QAny(1), QBit())
assert check_dtypes_consistent(BoundedQUInt(1), QBit())
assert check_dtypes_consistent(BQUInt(1), QBit())
assert check_dtypes_consistent(QFxp(1, 1), QBit())


Expand Down Expand Up @@ -313,11 +311,11 @@ def test_int_to_bits():


def test_bounded_quint_to_and_from_bits():
bquint4 = BoundedQUInt(4, 12)
bquint4 = BQUInt(4, 12)
assert [*bquint4.get_classical_domain()] == [*range(0, 12)]
assert list(bquint4.to_bits(10)) == [1, 0, 1, 0]
with pytest.raises(ValueError):
BoundedQUInt(4, 12).to_bits(13)
BQUInt(4, 12).to_bits(13)

assert_to_and_from_bits_array_consistent(bquint4, range(0, 12))

Expand Down
18 changes: 9 additions & 9 deletions qualtran/_infra/registers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import pytest
import sympy

from qualtran import BoundedQUInt, QAny, QBit, QInt, Register, Side, Signature
from qualtran import BQUInt, QAny, QBit, QInt, Register, Side, Signature
from qualtran._infra.gate_with_registers import get_named_qubits
from qualtran.symbolics import is_symbolic

Expand Down Expand Up @@ -47,7 +47,7 @@ def test_multidim_register():

@pytest.mark.parametrize('n, N, m, M', [(4, 10, 5, 19), (4, 16, 5, 32)])
def test_selection_registers_indexing(n, N, m, M):
dtypes = [BoundedQUInt(n, N), BoundedQUInt(m, M)]
dtypes = [BQUInt(n, N), BQUInt(m, M)]
regs = [Register(sym, dtype) for sym, dtype in zip(['x', 'y'], dtypes)]
for x in range(int(dtypes[0].iteration_length)):
for y in range(int(dtypes[1].iteration_length)):
Expand All @@ -59,25 +59,25 @@ def test_selection_registers_indexing(n, N, m, M):

def test_selection_registers_consistent():
with pytest.raises(ValueError, match=".*iteration length is too large "):
_ = Register('a', BoundedQUInt(3, 10))
_ = Register('a', BQUInt(3, 10))

selection_reg = Signature(
[
Register('n', BoundedQUInt(bitsize=3, iteration_length=5)),
Register('m', BoundedQUInt(bitsize=4, iteration_length=12)),
Register('n', BQUInt(bitsize=3, iteration_length=5)),
Register('m', BQUInt(bitsize=4, iteration_length=12)),
]
)
assert selection_reg[0] == Register('n', BoundedQUInt(3, 5))
assert selection_reg[1] == Register('m', BoundedQUInt(4, 12))
assert selection_reg[:1] == tuple([Register('n', BoundedQUInt(3, 5))])
assert selection_reg[0] == Register('n', BQUInt(3, 5))
assert selection_reg[1] == Register('m', BQUInt(4, 12))
assert selection_reg[:1] == tuple([Register('n', BQUInt(3, 5))])


def test_registers_getitem_raises():
g = Signature.build(a=4, b=3, c=2)
with pytest.raises(TypeError, match="indices must be integers or slices"):
_ = g[2.5] # type: ignore[call-overload]

selection_reg = Signature([Register('n', BoundedQUInt(bitsize=3, iteration_length=5))])
selection_reg = Signature([Register('n', BQUInt(bitsize=3, iteration_length=5))])
with pytest.raises(TypeError, match='indices must be integers or slices'):
_ = selection_reg[2.5] # type: ignore[call-overload]

Expand Down
6 changes: 3 additions & 3 deletions qualtran/bloqs/arithmetic/permutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
Bloq,
bloq_example,
BloqDocSpec,
BoundedQUInt,
BQUInt,
DecomposeTypeError,
QBit,
QUInt,
Expand Down Expand Up @@ -98,7 +98,7 @@ class PermutationCycle(Bloq):

@cached_property
def signature(self) -> Signature:
return Signature.build_from_dtypes(x=BoundedQUInt(self.bitsize, self.N))
return Signature.build_from_dtypes(x=BQUInt(self.bitsize, self.N))

@cached_property
def bitsize(self):
Expand Down Expand Up @@ -199,7 +199,7 @@ class Permutation(Bloq):

@cached_property
def signature(self) -> Signature:
return Signature.build_from_dtypes(x=BoundedQUInt(self.bitsize, self.N))
return Signature.build_from_dtypes(x=BQUInt(self.bitsize, self.N))

@cached_property
def bitsize(self):
Expand Down
4 changes: 2 additions & 2 deletions qualtran/bloqs/block_encoding/linear_combination.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
bloq_example,
BloqBuilder,
BloqDocSpec,
BoundedQUInt,
BQUInt,
QAny,
Register,
Signature,
Expand Down Expand Up @@ -201,7 +201,7 @@ def prepare(self) -> BlackBoxPrepare:
# disable spurious pylint
# pylint: disable=abstract-class-instantiated
prep = StatePreparationAliasSampling(
selection_registers=Register('selection', BoundedQUInt((N - 1).bit_length(), N)),
selection_registers=Register('selection', BQUInt((N - 1).bit_length(), N)),
alt=np.array(alt),
keep=np.array(keep),
mu=mu,
Expand Down
4 changes: 2 additions & 2 deletions qualtran/bloqs/block_encoding/sparse_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
bloq_example,
BloqBuilder,
BloqDocSpec,
BoundedQUInt,
BQUInt,
DecomposeTypeError,
QAny,
QBit,
Expand Down Expand Up @@ -94,7 +94,7 @@ def __attrs_post_init__(self):
@cached_property
def signature(self) -> Signature:
return Signature.build_from_dtypes(
l=BoundedQUInt(self.system_bitsize, self.num_nonzero), i=QUInt(self.system_bitsize)
l=BQUInt(self.system_bitsize, self.num_nonzero), i=QUInt(self.system_bitsize)
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import numpy as np
from numpy.typing import NDArray

from qualtran import bloq_example, BloqDocSpec, BoundedQUInt, QAny, Register, Signature
from qualtran import bloq_example, BloqDocSpec, BQUInt, QAny, Register, Signature
from qualtran.bloqs.basic_gates import CSwap
from qualtran.bloqs.mcmt.and_bloq import MultiAnd
from qualtran.bloqs.mod_arithmetic import ModAddK
Expand Down Expand Up @@ -81,14 +81,14 @@ def __attrs_post_init__(self):
@cached_property
def selection_registers(self) -> Tuple[Register, ...]:
return (
Register('U', BoundedQUInt(1, 2)),
Register('V', BoundedQUInt(1, 2)),
Register('p_x', BoundedQUInt((self.x_dim - 1).bit_length(), self.x_dim)),
Register('p_y', BoundedQUInt((self.y_dim - 1).bit_length(), self.y_dim)),
Register('alpha', BoundedQUInt(1, 2)),
Register('q_x', BoundedQUInt((self.x_dim - 1).bit_length(), self.x_dim)),
Register('q_y', BoundedQUInt((self.y_dim - 1).bit_length(), self.y_dim)),
Register('beta', BoundedQUInt(1, 2)),
Register('U', BQUInt(1, 2)),
Register('V', BQUInt(1, 2)),
Register('p_x', BQUInt((self.x_dim - 1).bit_length(), self.x_dim)),
Register('p_y', BQUInt((self.y_dim - 1).bit_length(), self.y_dim)),
Register('alpha', BQUInt(1, 2)),
Register('q_x', BQUInt((self.x_dim - 1).bit_length(), self.x_dim)),
Register('q_y', BQUInt((self.y_dim - 1).bit_length(), self.y_dim)),
Register('beta', BQUInt(1, 2)),
)

@cached_property
Expand Down
Loading

0 comments on commit 5c2c915

Please sign in to comment.