Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix random statevector distribution #10866

Merged
merged 5 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions qiskit/quantum_info/states/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ 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.
kevinsung marked this conversation as resolved.
Show resolved Hide resolved

Args:
dims (int or tuple): the dimensions of the state.
Expand All @@ -40,6 +40,10 @@ def random_statevector(

Returns:
Statevector: the random statevector.

Reference:
"Choosing a Point from the Surface of a Sphere" by George Marsaglia
http://doi.org/10.1214/aoms/1177692644
"""
if seed is None:
rng = np.random.default_rng()
Expand All @@ -49,14 +53,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 qiskit.quantum_info.random_statevector so that it samples from the uniform distribution.
kevinsung marked this conversation as resolved.
Show resolved Hide resolved