# qml.templates.subroutines.qmc.probs_to_unitary¶

probs_to_unitary(probs)[source]

Calculates the unitary matrix corresponding to an input probability distribution.

For a given distribution $$p(i)$$, this function returns the unitary $$\mathcal{A}$$ that transforms the $$|0\rangle$$ state as

$\mathcal{A} |0\rangle = \sum_{i} \sqrt{p(i)} |i\rangle,$

so that measuring the resulting state in the computational basis will give the state $$|i\rangle$$ with probability $$p(i)$$. Note that the returned unitary matrix is real and hence an orthogonal matrix.

Parameters

probs (array) – input probability distribution as a flat array

Returns

unitary

Return type

array

Raises

ValueError – if the input array is not flat or does not correspond to a probability distribution

Example:

>>> p = np.ones(4) / 4
>>> probs_to_unitary(p)
array([[ 0.5       ,  0.5       ,  0.5       ,  0.5       ],
[ 0.5       , -0.83333333,  0.16666667,  0.16666667],
[ 0.5       ,  0.16666667, -0.83333333,  0.16666667],
[ 0.5       ,  0.16666667,  0.16666667, -0.83333333]])