Skip to content

Commit 7e96e33

Browse files
Fix random number generator seed argument for quantum_volume (#14586) (#14591)
* Fix random number generator seed argument for quantum_volume The `seed` argument to `quantum_volume` being a `numpy.random.Generator` resulted in `integers` being called with 0 for the `low` argument. When called with only a `low` argument like this, this value is taken as one more than the maximum and 0 is treated as the minimum. This resulted in a `TypeError` because the range is from 0 to -1. Here the handling of `Generator` was updated to match how the deprecated `QuantumVolume` class handled `seed` (which was slightly odd -- it caps the range at the int64 limit even though the argument is a `u64` in the Rust function -- but probably fine; keeping the range helps keep the behavior consistent between `QuantumVolume` and `quantum_volume` for a generator with the same seed). * Address pylint's misunderstanding of the test import (cherry picked from commit ba74b5c) Co-authored-by: Will Shanks <[email protected]>
1 parent c26f15e commit 7e96e33

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

qiskit/circuit/library/quantum_volume.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ def quantum_volume(
174174
`arXiv:1811.12926 <https://arxiv.org/abs/1811.12926>`__
175175
"""
176176
if isinstance(seed, np.random.Generator):
177-
seed = seed.integers(0, dtype=np.uint64)
177+
max_value = np.iinfo(np.int64).max
178+
seed = seed.integers(max_value, dtype=np.int64)
178179
depth = depth or num_qubits
179180
return QuantumCircuit._from_circuit_data(qv_rs(num_qubits, depth, seed))
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
fixes:
3+
- |
4+
:func:`qiskit.circuit.library.quantum_volume` was updated to handle a
5+
:class:`numpy.random.Generator` as input for its ``seed`` argument.
6+
Previously, such a generator argument would result in a ``TypeError``.

test/python/circuit/library/test_quantum_volume.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
import unittest
1616

1717
from test.utils.base import QiskitTestCase
18+
19+
import numpy as np
20+
1821
from qiskit.circuit.library import QuantumVolume
1922
from qiskit.circuit.library.quantum_volume import quantum_volume
2023

@@ -50,6 +53,12 @@ def test_qv_function_seed_reproducibility(self):
5053
right = quantum_volume(10, 10, seed=4196)
5154
self.assertEqual(left, right)
5255

56+
rng = np.random.default_rng(256)
57+
left = quantum_volume(10, 10, seed=rng)
58+
rng = np.random.default_rng(256)
59+
right = quantum_volume(10, 10, seed=rng)
60+
self.assertEqual(left, right)
61+
5362

5463
if __name__ == "__main__":
5564
unittest.main()

0 commit comments

Comments
 (0)