diff --git a/src/qibolab/_core/instruments/qrng.py b/src/qibolab/_core/instruments/qrng.py index 56a894d20..4b1bc9d14 100644 --- a/src/qibolab/_core/instruments/qrng.py +++ b/src/qibolab/_core/instruments/qrng.py @@ -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 @@ -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 diff --git a/tests/instruments/test_qrng.py b/tests/instruments/test_qrng.py index b7e5dac09..9ddaaeeae 100644 --- a/tests/instruments/test_qrng.py +++ b/tests/instruments/test_qrng.py @@ -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 @@ -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