random_normal

Graph.random_normal(shape, mean, standard_deviation, seed=None, *, name=None)

Create a sample of normally distributed random numbers.

Parameters
  • shape (tuple or list) – The shape of the sampled random numbers.

  • mean (int or float) – The mean of the normal distribution.

  • standard_deviation (int or float) – The standard deviation of the normal distribution.

  • seed (int, optional) – A seed for the random number generator. Defaults to None, in which case a random value for the seed is used.

  • name (str, optional) – The name of the node.

Returns

A tensor containing a sample of normally distributed random numbers with shape shape.

Return type

Tensor

See also

calculate_stochastic_optimization()

Function to find the minimum of generic stochastic functions.

random_choices()

Create random samples from the data that you provide.

random_uniform()

Create a sample of uniformly distributed random numbers.

Examples

Create a random tensor by sampling from a Gaussian distribution.

>>> samples = graph.random_normal(
...     shape=(3, 1), mean=0.0, standard_deviation=0.05, seed=0, name="samples"
... )
>>> result = qctrl.functions.calculate_graph(graph=graph, output_node_names=["samples"])
>>> result.output["samples"]["value"]
array([[-0.03171833], [0.00816805], [-0.06874011]])

Create a batch of noise signals to construct a PWC Hamiltonian. The signal is defined as \(a \cos(\omega t)\), where \(a\) follows a normal distribution and \(\omega\) follows a uniform distribution.

>>> seed = 0
>>> batch_size = 3
>>> sigma_x = np.array([[0, 1], [1, 0]])
>>> sample_times = np.array([0.1, 0.2])
>>> a = graph.random_normal((batch_size, 1), mean=0.0, standard_deviation=0.05, seed=seed)
>>> omega = graph.random_uniform(
...     shape=(batch_size, 1), lower_bound=np.pi, upper_bound=2 * np.pi, seed=seed
... )
>>> sampled_signal = a * graph.cos(omega * sample_times[None])
>>> hamiltonian = graph.pwc_signal(sampled_signal, duration=0.2) * sigma_x
>>> hamiltonian.name = "hamiltonian"
>>> result = qctrl.functions.calculate_graph(graph=graph, output_node_names=["hamiltonian"])
>>> result.output["hamiltonian"]
[
    [
        {"value": array([[-0.0, -0.02674376], [-0.02674376, -0.0]]), "duration": 0.1},
        {"value": array([[-0.0, -0.01338043], [-0.01338043, -0.0]]), "duration": 0.1},
    ],
    [
        {"value": array([[0.0, 0.00691007], [0.00691007, 0.0]]), "duration": 0.1},
        {"value": array([[0.0, 0.00352363], [0.00352363, 0.0]]), "duration": 0.1},
    ],
    [
        {"value": array([[-0.0, -0.06230612], [-0.06230612, -0.0]]), "duration": 0.1},
        {"value": array([[-0.0, -0.04420857], [-0.04420857, -0.0]]), "duration": 0.1},
    ],
]

See more examples in the How to optimize controls robust to strong noise sources user guide.