Skip to content

Commit

Permalink
fix random statevector distribution (Qiskit#10866)
Browse files Browse the repository at this point in the history
* fix random statevector distribution

* add citation and release note

* mention Haar measure in doc and use link in release note

* cite better reference

* Format reference

---------

Co-authored-by: Julien Gacon <[email protected]>
  • Loading branch information
kevinsung and Cryoris authored Sep 22, 2023
1 parent d3a6172 commit 364debc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
19 changes: 10 additions & 9 deletions qiskit/quantum_info/states/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def random_statevector(
) -> Statevector:
"""Generator a random Statevector.
The statevector is sampled from the uniform (Haar) measure.
The statevector is sampled from the uniform distribution. This is the measure
induced by the Haar measure on unitary matrices.
Args:
dims (int or tuple): the dimensions of the state.
Expand All @@ -40,6 +41,10 @@ def random_statevector(
Returns:
Statevector: the random statevector.
Reference:
K. Zyczkowski and H. Sommers (2001), "Induced measures in the space of mixed quantum states",
`J. Phys. A: Math. Gen. 34 7111 <https://arxiv.org/abs/quant-ph/0012101>`__.
"""
if seed is None:
rng = np.random.default_rng()
Expand All @@ -49,14 +54,10 @@ def random_statevector(
rng = default_rng(seed)

dim = np.prod(dims)

# Random array over interval (0, 1]
x = rng.random(dim)
x += x == 0
x = -np.log(x)
sumx = sum(x)
phases = rng.random(dim) * 2.0 * np.pi
return Statevector(np.sqrt(x / sumx) * np.exp(1j * phases), dims=dims)
vec = rng.standard_normal(dim).astype(complex)
vec += 1j * rng.standard_normal(dim)
vec /= np.linalg.norm(vec)
return Statevector(vec, dims=dims)


def random_density_matrix(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
Fixes the implementation of :func:`.random_statevector` so that it samples from the uniform distribution.

0 comments on commit 364debc

Please sign in to comment.