dq.random.complex
complex(key: PRNGKeyArray, shape: int | tuple[int, ...], *, rmax: float = 1.0) -> Array
Returns an array of uniformly distributed random complex numbers.
Each element of the returned array is sampled uniformly in the disk of radius \(\text{rmax}\).
Uniform sampling in the complex plane
Here are three common options to generate random complex numbers,
dq.random.complex()
returns the last one:
_, (ax0, ax1, ax2) = dq.plot.grid(3, sharexy=True)
ax0.set(xlim=(-1.1, 1.1), ylim=(-1.1, 1.1))
n = 10_000
# option 1: uniformly distributed real and imaginary part
x = np.random.rand(n) * 2 - 1 + 1j * (np.random.rand(n) * 2 - 1)
ax0.scatter(x.real, x.imag, s=1.0)
# option 2: uniformly distributed magnitude and phase
x = np.random.rand(n) * jnp.exp(1j * 2 * jnp.pi * np.random.rand(n))
ax1.scatter(x.real, x.imag, s=1.0)
# option 3: uniformly distributed in a disk (in Dynamiqs)
key = jax.random.PRNGKey(42)
x = dq.random.complex(key, n)
ax2.scatter(x.real, x.imag, s=1.0)
Parameters
-
key
–
A PRNG key used as the random key.
-
shape
(int or tuple of ints)
–
Shape of the returned array.
-
rmax
–
Maximum magnitude.
Returns
(array of shape (*shape)) Random complex number array.
Examples
>>> key = jax.random.PRNGKey(42)
>>> dq.random.complex(key, (2, 3), rmax=5.0)
Array([[ 1.341+4.17j , 3.978-0.979j, -2.592-0.946j],
[-4.428+1.744j, -0.53 +1.668j, 2.582+0.65j ]], dtype=complex64)