Source code for pennylane.utils

# Copyright 2018-2021 Xanadu Quantum Technologies Inc.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This module contains utilities and auxiliary functions which are shared
across the PennyLane submodules.
"""
import functools
import inspect
import numbers

# pylint: disable=protected-access,too-many-branches
from collections.abc import Iterable

import numpy as np

import pennylane as qml


def _flatten(x):
    """Iterate recursively through an arbitrarily nested structure in depth-first order.

    See also :func:`_unflatten`.

    Args:
        x (array, Iterable, Any): each element of an array or an Iterable may itself be any of these types

    Yields:
        Any: elements of x in depth-first order
    """
    if isinstance(x, np.ndarray):
        yield from _flatten(x.flat)  # should we allow object arrays? or just "yield from x.flat"?
    elif isinstance(x, qml.wires.Wires):
        # Reursive calls to flatten `Wires` will cause infinite recursion (`Wires` atoms are `Wires`).
        # Since Wires are always flat, just yield.
        yield from x
    elif isinstance(x, Iterable) and not isinstance(x, (str, bytes)):
        for item in x:
            yield from _flatten(item)
    else:
        yield x


def _unflatten(flat, model):
    """Restores an arbitrary nested structure to a flattened iterable.

    See also :func:`_flatten`.

    Args:
        flat (array): 1D array of items
        model (array, Iterable, Number): model nested structure

    Raises:
        TypeError: if ``model`` contains an object of unsupported type

    Returns:
        Union[array, list, Any], array: first elements of flat arranged into the nested
        structure of model, unused elements of flat
    """
    if isinstance(model, (numbers.Number, str)):
        return flat[0], flat[1:]

    if isinstance(model, np.ndarray):
        idx = model.size
        res = np.array(flat)[:idx].reshape(model.shape)
        return res, flat[idx:]

    if isinstance(model, Iterable):
        res = []
        for x in model:
            val, flat = _unflatten(flat, x)
            res.append(val)
        return res, flat

    raise TypeError(f"Unsupported type in the model: {type(model)}")


[docs]def unflatten(flat, model): """Wrapper for :func:`_unflatten`. Args: flat (array): 1D array of items model (array, Iterable, Number): model nested structure Raises: ValueError: if ``flat`` has more elements than ``model`` """ # pylint:disable=len-as-condition res, tail = _unflatten(np.asarray(flat), model) if len(tail) != 0: raise ValueError("Flattened iterable has more elements than the model.") return res
def _inv_dict(d): """Reverse a dictionary mapping. Returns multimap where the keys are the former values, and values are sets of the former keys. Args: d (dict[a->b]): mapping to reverse Returns: dict[b->set[a]]: reversed mapping """ ret = {} for k, v in d.items(): ret.setdefault(v, set()).add(k) return ret def _get_default_args(func): """Get the default arguments of a function. Args: func (callable): a function Returns: dict[str, tuple]: mapping from argument name to (positional idx, default value) """ signature = inspect.signature(func) return { k: (idx, v.default) for idx, (k, v) in enumerate(signature.parameters.items()) if v.default is not inspect.Parameter.empty }
[docs]@functools.lru_cache() def pauli_eigs(n): r"""Eigenvalues for :math:`A^{\otimes n}`, where :math:`A` is Pauli operator, or shares its eigenvalues. As an example if n==2, then the eigenvalues of a tensor product consisting of two matrices sharing the eigenvalues with Pauli matrices is returned. Args: n (int): the number of qubits the matrix acts on Returns: list: the eigenvalues of the specified observable """ if n == 1: return np.array([1.0, -1.0]) return np.concatenate([pauli_eigs(n - 1), -pauli_eigs(n - 1)])
[docs]def expand_vector(vector, original_wires, expanded_wires): r"""Expand a vector to more wires. Args: vector (array): :math:`2^n` vector where n = len(original_wires). original_wires (Sequence[int]): original wires of vector expanded_wires (Union[Sequence[int], int]): expanded wires of vector, can be shuffled If a single int m is given, corresponds to list(range(m)) Returns: array: :math:`2^m` vector where m = len(expanded_wires). """ if len(original_wires) == 0: val = qml.math.squeeze(vector) return val * qml.math.ones(2 ** len(expanded_wires)) if isinstance(expanded_wires, numbers.Integral): expanded_wires = list(range(expanded_wires)) N = len(original_wires) M = len(expanded_wires) D = M - N len_vector = qml.math.shape(vector)[0] qudit_order = int(2 ** (np.log2(len_vector) / N)) if not set(expanded_wires).issuperset(original_wires): raise ValueError("Invalid target subsystems provided in 'original_wires' argument.") if qml.math.shape(vector) != (qudit_order**N,): raise ValueError(f"Vector parameter must be of length {qudit_order}**len(original_wires)") dims = [qudit_order] * N tensor = qml.math.reshape(vector, dims) if D > 0: extra_dims = [qudit_order] * D ones = qml.math.ones(qudit_order**D).reshape(extra_dims) expanded_tensor = qml.math.tensordot(tensor, ones, axes=0) else: expanded_tensor = tensor wire_indices = [expanded_wires.index(wire) for wire in original_wires] wire_indices = np.array(wire_indices) # Order tensor factors according to wires original_indices = np.array(range(N)) expanded_tensor = qml.math.moveaxis( expanded_tensor, tuple(original_indices), tuple(wire_indices) ) return qml.math.reshape(expanded_tensor, (qudit_order**M,))