Utilities

Module name: pennylane.utils

This module contains utilities and auxiliary functions, which are shared across the PennyLane submodules.

Summary

_flatten(x) Iterate through an arbitrarily nested structure, flattening it in depth-first order.
_unflatten(flat, model) Restores an arbitrary nested structure to a flattened iterable.
unflatten(flat, model) Wrapper for _unflatten().

Code details

_flatten(x)[source]

Iterate through an arbitrarily nested structure, flattening it in depth-first order.

See also _unflatten().

Parameters:x (array, Iterable, other) – each element of the Iterable may itself be an iterable object
Yields:other – elements of x in depth-first order
_unflatten(flat, model)[source]

Restores an arbitrary nested structure to a flattened iterable.

See also _flatten().

Parameters:
  • flat (array) – 1D array of items
  • model (array, Iterable, Number) – model nested structure
Returns:

first elements of flat arranged into the nested structure of model, unused elements of flat

Return type:

(other, array)

unflatten(flat, model)[source]

Wrapper for _unflatten().