Skip to content

Commit

Permalink
fix: drop numpy random from QRNG and mock in test
Browse files Browse the repository at this point in the history
  • Loading branch information
stavros11 committed Jan 27, 2025
1 parent 209c79e commit af710eb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
9 changes: 1 addition & 8 deletions src/qibolab/_core/instruments/qrng.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ def upscale(samples: npt.NDArray, input_bits=4, output_bits=32) -> npt.NDArray:


class QRNG(Instrument):
"""Driver to sample numbers from a Quantum Random Number Generator (QRNG).
Note that if we are not connected to a physical QRNG device, this will
return pseudo-random generated numbers using ``numpy.random``.
"""
"""Driver to sample numbers from a Quantum Random Number Generator (QRNG)."""

address: str
baudrate: int = 115200
Expand Down Expand Up @@ -123,9 +119,6 @@ def extract(self, n: int) -> npt.NDArray:
Args:
n: Number of samples to retrieve.
"""
if self.port is None:
return np.random.randint(0, 2**self.extracted_bits, size=(n,))

samples = self.read(2 * n)
extracted = self._extractor(samples)
return extracted
Expand Down
10 changes: 7 additions & 3 deletions tests/instruments/test_qrng.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@


@pytest.fixture
def qrng():
def qrng(mocker):
qrng = QRNG(address="/dev/ttyACM0")
try:
qrng.connect()
except SerialException:
pass

def extract(n):
return np.random.randint(0, 2**qrng.extracted_bits, size=(n,))

mocker.patch.object(qrng, "extract", side_effect=extract)
return qrng


Expand All @@ -29,4 +33,4 @@ def test_random_chisquare(qrng):
expected_frequency = len(data) / nbins
expected_frequencies = np.full(nbins, expected_frequency)
_, p_value = chisquare(observed_frequencies, expected_frequencies)
assert p_value > 0.05
assert p_value > P_VALUE_CUTOFF

0 comments on commit af710eb

Please sign in to comment.