def heisenberg_expand(self, U, wires):
    'Expand the given local Heisenberg-picture array into a full-system one.\n\n        Args:\n            U (array[float]): array to expand (expected to be of the dimension ``1+2*self.num_wires``)\n            wires (Wires): wires on the device the array ``U`` should be expanded\n                to apply to\n\n        Raises:\n            ValueError: if the size of the input matrix is invalid or `num_wires` is incorrect\n\n        Returns:\n            array[float]: expanded array, dimension ``1+2*num_wires``\n        '
    U_dim = len(U)
    nw = len(self.wires)
    if (U.ndim > 2):
        raise ValueError('Only order-1 and order-2 arrays supported.')
    if (U_dim != (1 + (2 * nw))):
        raise ValueError('{}: Heisenberg matrix is the wrong size {}.'.format(self.name, U_dim))
    if ((len(wires) == 0) or (len(self.wires) == len(wires))):
        return U
    if (not wires.contains_wires(self.wires)):
        raise ValueError('{}: Some observable wires {} do not exist on this device with wires {}'.format(self.name, self.wires, wires))
    wire_indices = wires.indices(self.wires)
    dim = (1 + (len(wires) * 2))

    def loc(w):
        'Returns the slice denoting the location of (x_w, p_w) in the basis.'
        ind = ((2 * w) + 1)
        return slice(ind, (ind + 2))
    if (U.ndim == 1):
        W = np.zeros(dim)
        W[0] = U[0]
        for (k, w) in enumerate(wire_indices):
            W[loc(w)] = U[loc(k)]
    elif (U.ndim == 2):
        if isinstance(self, Observable):
            W = np.zeros((dim, dim))
        else:
            W = np.eye(dim)
        W[(0, 0)] = U[(0, 0)]
        for (k1, w1) in enumerate(wire_indices):
            s1 = loc(k1)
            d1 = loc(w1)
            W[(d1, 0)] = U[(s1, 0)]
            W[(0, d1)] = U[(0, s1)]
            for (k2, w2) in enumerate(wire_indices):
                W[(d1, loc(w2))] = U[(s1, loc(k2))]
    return W
Expand the given local Heisenberg-picture array into a full-system one.

Args:
    U (array[float]): array to expand (expected to be of the dimension ``1+2*self.num_wires``)
    wires (Wires): wires on the device the array ``U`` should be expanded
        to apply to

Raises:
    ValueError: if the size of the input matrix is invalid or `num_wires` is incorrect

Returns:
    array[float]: expanded array, dimension ``1+2*num_wires``
def heisenberg_expand(self, U, wires):
    'Expand the given local Heisenberg-picture array into a full-system one.\n\n        Args:\n            U (array[float]): array to expand (expected to be of the dimension ``1+2*self.num_wires``)\n            wires (Wires): wires on the device the array ``U`` should be expanded\n                to apply to\n\n        Raises:\n            ValueError: if the size of the input matrix is invalid or `num_wires` is incorrect\n\n        Returns:\n            array[float]: expanded array, dimension ``1+2*num_wires``\n        '
    U_dim = len(U)
    nw = len(self.wires)
    if (U.ndim > 2):
        raise ValueError('Only order-1 and order-2 arrays supported.')
    if (U_dim != (1 + (2 * nw))):
        raise ValueError('{}: Heisenberg matrix is the wrong size {}.'.format(self.name, U_dim))
    if ((len(wires) == 0) or (len(self.wires) == len(wires))):
        return U
    if (not wires.contains_wires(self.wires)):
        raise ValueError('{}: Some observable wires {} do not exist on this device with wires {}'.format(self.name, self.wires, wires))
    wire_indices = wires.indices(self.wires)
    dim = (1 + (len(wires) * 2))

    def loc(w):
        'Returns the slice denoting the location of (x_w, p_w) in the basis.'
        ind = ((2 * w) + 1)
        return slice(ind, (ind + 2))
    if (U.ndim == 1):
        W = np.zeros(dim)
        W[0] = U[0]
        for (k, w) in enumerate(wire_indices):
            W[loc(w)] = U[loc(k)]
    elif (U.ndim == 2):
        if isinstance(self, Observable):
            W = np.zeros((dim, dim))
        else:
            W = np.eye(dim)
        W[(0, 0)] = U[(0, 0)]
        for (k1, w1) in enumerate(wire_indices):
            s1 = loc(k1)
            d1 = loc(w1)
            W[(d1, 0)] = U[(s1, 0)]
            W[(0, d1)] = U[(0, s1)]
            for (k2, w2) in enumerate(wire_indices):
                W[(d1, loc(w2))] = U[(s1, loc(k2))]
    return W