# Qiskit: How did they use Haar measure in random_statevector?

I have read this article to understand sampling state vectors from Haar measure: Understanding the Haar Measure. Qiskit uses Haar measure to sample state vectors, but when I looked at the source code I couldn't see how did they use Haar measure.

Source

[docs]def random_statevector(dims, seed=None):
"""Generator a random Statevector.

The statevector is sampled from the uniform (Haar) measure.

Args:
dims (int or tuple): the dimensions of the state.
seed (int or np.random.Generator): Optional. Set a fixed seed or
generator for RNG.

Returns:
Statevector: the random statevector.
"""
if seed is None:
rng = np.random.default_rng()
elif isinstance(seed, np.random.Generator):
rng = seed
else:
rng = default_rng(seed)

dim = np.product(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)


As far as I understood, they applied a unitary matrix to a zero state $$|0\rangle$$, but where did this factor come from np.sqrt(x / sumx)?

The Haar measure is associated with a uniform probability distribution. In the above code, x is a list of randomly chosen squared amplitudes from the probability distribution. And exp(1j*phases) are the phases. Together they construct the statevector. sumx within the sqrt is the normalization coefficient.