Skip to content

Commit f48146f

Browse files
committed
refactor(QuantumBitGenerator): random bitstring and uint signature
1 parent 3271fc1 commit f48146f

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

qrand/_qiskit_bit_generator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def load_cache(self, bitstring: str, flush: bool = False) -> None:
230230
self._bitcache.flush()
231231
self._bitcache.push(bitstring)
232232

233-
def random_bitstring(self, n_bits: int = 0) -> str:
233+
def random_bitstring(self, n_bits: int = 0) -> str: # type: ignore
234234
"""
235235
Returns a random bitstring of a given lenght. If less than one it
236236
defaults to the raw number of bits for the instance QiskitBitGenerator
@@ -252,7 +252,7 @@ def random_bitstring(self, n_bits: int = 0) -> str:
252252
self._fetch_random_bits()
253253
return self._bitcache.pop(n_bits)
254254

255-
def random_uint(self, n_bits: int = 0) -> int:
255+
def random_uint(self, n_bits: int = 0) -> int: # type: ignore
256256
"""
257257
Returns a random unsigned int of a given size in bits.
258258

qrand/quantum_bit_generator.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,13 @@ def load_cache(self, bitstring: str, flush: bool = False) -> None:
181181
self.bitcache.flush()
182182
self.bitcache.push(bitstring)
183183

184-
def random_bitstring(self, num_bits: int = 0) -> str:
184+
def random_bitstring(self, num_bits: Optional[int] = None) -> str:
185185
"""
186186
Returns a random bitstring of a given lenght.
187187
188188
Parameters
189189
----------
190-
num_bits: int, default: 0
190+
num_bits: int, optional
191191
Number of bits to retrieve. If less than one it defaults to the raw
192192
number of BITS for the instance QuantumBitGenerator (i.e. 32 or 64).
193193
@@ -196,8 +196,11 @@ def random_bitstring(self, num_bits: int = 0) -> str:
196196
out: str
197197
Bitstring of lenght `num_bits`.
198198
"""
199-
if num_bits < 1:
200-
num_bits = self.BITS
199+
num_bits = (
200+
num_bits
201+
if num_bits and type(num_bits) is int and num_bits > 0
202+
else self.BITS
203+
)
201204
while self.bitcache.size < num_bits:
202205
self._refill_cache()
203206
return self.bitcache.pop(num_bits)
@@ -232,13 +235,13 @@ def random_double(self, n: float = 1) -> float:
232235
value: float = unpack("d", packed)[0] - 1.0
233236
return value * n
234237

235-
def random_uint(self, num_bits: int = 0) -> int:
238+
def random_uint(self, num_bits: Optional[int] = None) -> int:
236239
"""
237240
Returns a random unsigned int of a given size in bits.
238241
239242
Parameters
240243
----------
241-
num_bits: int, default: 0
244+
num_bits: int, optional
242245
Number of bits to retrieve. If less than one it defaults to the raw
243246
number of BITS for the instance QuantumBitGenerator (i.e. 32 or 64).
244247
@@ -247,8 +250,11 @@ def random_uint(self, num_bits: int = 0) -> int:
247250
out: int
248251
Unsigned int of `num_bits` bits.
249252
"""
250-
if num_bits < 1:
251-
num_bits = self.BITS
253+
num_bits = (
254+
num_bits
255+
if num_bits and type(num_bits) is int and num_bits > 0
256+
else self.BITS
257+
)
252258
return int(self.random_bitstring(num_bits), 2)
253259

254260
############################### PRIVATE API ###############################

0 commit comments

Comments
 (0)