Source code for pennylane.templates.state_preparations.state_prep_mps

# Copyright 2024 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.
"""
Contains the MPSPrep template.
"""

import numpy as np

import pennylane as qp
from pennylane.core.operator import Operator2
from pennylane.decomposition import add_decomps, register_resources
from pennylane.typing import Complex, Wire


def _validate_mps_shape(mps):
    r"""Validate that the MPS dimensions are correct.

    Args:
        mps (list[TensorLike]): List of tensors representing the MPS.
    """

    # Validate the shape and dimensions of the first tensor
    assert qp.math.isclose(
        len(qp.math.shape(mps[0])), 2
    ), "The first tensor must have exactly 2 dimensions."
    dj0, dj2 = qp.math.shape(mps[0])
    assert qp.math.isclose(dj0, 2), "The first dimension of the first tensor must be exactly 2."
    assert qp.math.log2(
        dj2
    ).is_integer(), "The second dimension of the first tensor must be a power of 2."

    # Validate the shapes of the intermediate tensors
    for i, array in enumerate(mps[1:-1], start=1):
        shape = qp.math.shape(array)
        assert qp.math.isclose(len(shape), 3), f"Tensor {i} must have exactly 3 dimensions."
        new_dj0, new_dj1, new_dj2 = shape
        assert qp.math.isclose(new_dj1, 2), f"The second dimension of tensor {i} must be exactly 2."
        assert qp.math.log2(
            new_dj0
        ).is_integer(), f"The first dimension of tensor {i} must be a power of 2."
        assert qp.math.isclose(new_dj1, 2), f"The second dimension of tensor {i} must be exactly 2."
        assert qp.math.log2(
            new_dj2
        ).is_integer(), f"The third dimension of tensor {i} must be a power of 2."
        assert qp.math.isclose(
            new_dj0, dj2
        ), f"Dimension mismatch: tensor {i}'s first dimension does not match the previous third dimension."
        dj2 = new_dj2

    # Validate the shape and dimensions of the last tensor
    assert qp.math.isclose(
        len(qp.math.shape(mps[-1])), 2
    ), "The last tensor must have exactly 2 dimensions."
    new_dj0, new_dj1 = qp.math.shape(mps[-1])
    assert new_dj1 == 2, "The second dimension of the last tensor must be exactly 2."
    assert qp.math.log2(
        new_dj0
    ).is_integer(), "The first dimension of the last tensor must be a power of 2."
    assert qp.math.isclose(
        new_dj0, dj2
    ), "Dimension mismatch: the last tensor's first dimension does not match the previous third dimension."


[docs] def right_canonicalize_mps(mps): r"""Transform a matrix product state (MPS) into its right-canonical form. A right-canonicalized MPS is a matrix product state in which the constituent tensors, :math:`A^{(j)}`, satisfy the following orthonormality condition [Eq. (21) of `arXiv:2310.18410 <https://arxiv.org/pdf/2310.18410>`_]: .. math:: \sum_{d_{j,1}, d_{j,2}} A^{(j)}_{d_{j, 0}, d_{j, 1}, d_{j, 2}} \left( A^{(j)}_{d'_{j, 0}, d_{j, 1}, d_{j, 2}} \right)^* = \delta_{d_{j, 0}, d'_{j, 0}}, where :math:`d_{i,j}` denotes the :math:`j` dimension of the :math:`i` tensor and :math:`\delta` is the Kronecker delta. Args: mps (list[TensorLike]): List of tensors representing the MPS. Returns: List of tensors representing the MPS in right-canonical form with the same dimensions as the initial MPS. .. seealso:: :class:`~.MPSPrep`. **Example** .. code-block:: n_sites = 4 import numpy as np mps = ([np.ones((2, 4))] + [np.ones((4, 2, 4)) for _ in range(1, n_sites - 1)] + [np.ones((4, 2))]) mps_rc = qp.right_canonicalize_mps(mps) # Check that the right-canonical definition is fulfilled for i in range(1, n_sites - 1): tensor = mps_rc[i] contraction_matrix = np.tensordot(tensor, tensor.conj(), axes=([1, 2], [1, 2])) assert np.allclose(contraction_matrix, np.eye(tensor.shape[0])) .. details:: :title: Usage Details The input MPS must be a list of :math:`n` tensors :math:`[A^{(0)}, ..., A^{(n-1)}]` with shapes :math:`d_0, ..., d_{n-1}`, respectively. The first and last tensors have rank :math:`2` while the intermediate tensors have rank :math:`3`. The first tensor must have the shape :math:`d_0 = (d_{0,0}, d_{0,1})` where :math:`d_{0,0}` and :math:`d_{0,1}` correspond to the physical dimension of the site and an auxiliary bond dimension connecting it to the next tensor, respectively. The last tensor must have the shape :math:`d_{n-1} = (d_{n-1,0}, d_{n-1,1})` where :math:`d_{n-1,0}` and :math:`d_{n-1,1}` represent the auxiliary dimension from the previous site and the physical dimension of the site, respectively. The intermediate tensors must have the shape :math:`d_j = (d_{j,0}, d_{j,1}, d_{j,2})`, where: - :math:`d_{j,0}` is the bond dimension connecting to the previous tensor - :math:`d_{j,1}` is the physical dimension of the site - :math:`d_{j,2}` is the bond dimension connecting to the next tensor Note that the bond dimensions must match between adjacent tensors such that :math:`d_{j-1,2} = d_{j,0}`. Additionally, the physical dimension of the site should always be fixed at :math:`2` (since the dimension of a qubit is :math:`2`), while the other dimensions must be powers of two. The following example shows a valid MPS input containing four tensors with dimensions :math:`[(2,2), (2,2,4), (4,2,2), (2,2)]` which satisfy the criteria described above. .. code-block:: mps = [ np.array([[0.0, 0.107], [0.994, 0.0]]), np.array( [ [[0.0, 0.0, 0.0, -0.0], [1.0, 0.0, 0.0, -0.0]], [[0.0, 1.0, 0.0, -0.0], [0.0, 0.0, 0.0, -0.0]], ] ), np.array( [ [[-1.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 1.0]], [[0.0, -1.0], [0.0, 0.0]], [[0.0, 0.0], [1.0, 0.0]], ] ), np.array([[-1.0, -0.0], [-0.0, -1.0]]), ] """ _validate_mps_shape(mps) mps = mps.copy() mps[0] = mps[0].reshape((1, *mps[0].shape)) mps[-1] = mps[-1].reshape((*mps[-1].shape, 1)) if not qp.math.is_tracer(mps[0]): is_right_canonical = True for tensor in mps[1:-1]: # Right-canonical definition input_matrix = qp.math.tensordot(tensor, tensor.conj(), axes=([1, 2], [1, 2])) if not qp.math.allclose(input_matrix, qp.math.eye(tensor.shape[0])): is_right_canonical = False break if is_right_canonical: mps[0] = mps[0][0] mps[-1] = mps[-1][:, :, 0] return mps d_shapes = [] for tensor in mps[1:-1]: d_shapes += tensor.shape max_bond_dim = qp.math.max(d_shapes) n_sites = len(mps) output_mps = [None] * n_sites # Procedure analogous to the left-canonical conversion but starting from the right and storing the Vd, # where Vd is the right matrix in the Singular Value Decomposition (SVD) for i in range(n_sites - 1, 0, -1): chi_left, d, chi_right = mps[i].shape input_matrix = mps[i].reshape(chi_left, d * chi_right) u_matrix, s_diag, vd_matrix = qp.math.linalg.svd(input_matrix, full_matrices=False) # Truncate SVD components if needed chi_new = min(int(max_bond_dim), len(s_diag)) u_matrix = u_matrix[:, :chi_new] s_diag = s_diag[:chi_new] vd_matrix = vd_matrix[:chi_new, :] # Store Vd reshaped as an MPS tensor in the output MPS output_mps[i] = vd_matrix.reshape(chi_new, d, chi_right) # Contract U with diag(S) and merge it with the preceding MPS tensor, preserving the canonical structure mps[i - 1] = qp.math.tensordot(mps[i - 1], u_matrix @ qp.math.diag(s_diag), axes=([2], [0])) output_mps[0] = mps[0][0] output_mps[-1] = output_mps[-1][:, :, 0] return output_mps
[docs] class MPSPrep(Operator2): r"""Prepares an initial state from a matrix product state (MPS) representation. .. note:: The gate-based decomposition prepares the state vector represented by the MPS using Eq. (23) in `arXiv:2310.18410 <https://arxiv.org/pdf/2310.18410>`_. It requires a right-canonical MPS and auxiliary qubits supplied through ``work_wires``. Set ``right_canonicalize=True`` to convert the input MPS to right-canonical form, or use :func:`~.right_canonicalize_mps` before constructing the operator. Args: mps (list[TensorLike]): list of arrays of rank-3 and rank-2 tensors representing an MPS state as a product of site matrices. See the usage details section for more information. wires (Sequence[int]): wires that the template acts on. It should match the number of MPS tensors. work_wires (Sequence[int]): extra qubits needed for the gate-based decomposition. At least one work wire is required. If the maximum bond dimension of the MPS is :math:`2^k`, provide at least :math:`k` work wires. right_canonicalize (bool): indicates whether a conversion to right-canonical form should be performed to the MPS. Default is ``False``. .. seealso:: :func:`~.right_canonicalize_mps`. **Example** Prepare an MPS on ``default.qubit`` using one auxiliary wire: .. code-block:: python mps = [ np.array([[0.0, 0.107], [0.994, 0.0]]), np.array( [ [[0.0, 0.0], [1.0, 0.0]], [[0.0, 1.0], [0.0, 0.0]], ] ), np.array([[-1.0, -0.0], [-0.0, -1.0]]), ] dev = qp.device("default.qubit", wires=4) @qp.qnode(dev) def circuit(): qp.MPSPrep(mps, wires=[1, 2, 3], work_wires=[0]) return qp.state() The auxiliary wire returns to :math:`|0\rangle`, so the first eight amplitudes contain the prepared three-qubit state: >>> print(circuit()[:8]) [ 0. +0.j -0.10702756+0.j 0. +0.j 0. +0.j 0. +0.j 0. +0.j -0.99425605+0.j 0. +0.j] .. details:: :title: Available decompositions .. decomposition-table:: qp.MPSPrep :stacked: :symbols: :math:`n` MPS tensors (one per wire), :math:`w` work wires and maximal bond dimension :math:`\chi` :example: :math:`n=3`, :math:`w=1` .. decomposition-rule:: sequential_unitaries :image: /_static/decomposition_thumbnails/mps_prep.svg :requires: :math:`w \geq \log_2 \chi` work wires :class:`~.QubitUnitary` on :math:`w + 1` wires | :math:`n` | 3 .. details:: :title: Usage Details The input MPS must be a list of :math:`n` tensors :math:`[A^{(0)}, ..., A^{(n-1)}]` with shapes :math:`d_0, ..., d_{n-1}`, respectively. The first and last tensors have rank :math:`2` while the intermediate tensors have rank :math:`3`. The first tensor must have the shape :math:`d_0 = (d_{0,0}, d_{0,1})` where :math:`d_{0,0}` and :math:`d_{0,1}` correspond to the physical dimension of the site and an auxiliary bond dimension connecting it to the next tensor, respectively. The last tensor must have the shape :math:`d_{n-1} = (d_{n-1,0}, d_{n-1,1})` where :math:`d_{n-1,0}` and :math:`d_{n-1,1}` represent the auxiliary dimension from the previous site and the physical dimension of the site, respectively. The intermediate tensors must have the shape :math:`d_j = (d_{j,0}, d_{j,1}, d_{j,2})`, where: - :math:`d_{j,0}` is the bond dimension connecting to the previous tensor - :math:`d_{j,1}` is the physical dimension of the site - :math:`d_{j,2}` is the bond dimension connecting to the next tensor Note that the bond dimensions must match between adjacent tensors such that :math:`d_{j-1,2} = d_{j,0}`. Additionally, the physical dimension of the site should always be fixed at :math:`2` (since the dimension of a qubit is :math:`2`), while the other dimensions must be powers of two. The following example shows a valid MPS input containing four tensors with dimensions :math:`[(2,2), (2,2,4), (4,2,2), (2,2)]` which satisfy the criteria described above. .. code-block:: mps = [ np.array([[0.0, 0.107], [0.994, 0.0]]), np.array( [ [[0.0, 0.0, 0.0, -0.0], [1.0, 0.0, 0.0, -0.0]], [[0.0, 1.0, 0.0, -0.0], [0.0, 0.0, 0.0, -0.0]], ] ), np.array( [ [[-1.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 1.0]], [[0.0, -1.0], [0.0, 0.0]], [[0.0, 0.0], [1.0, 0.0]], ] ), np.array([[-1.0, -0.0], [-0.0, -1.0]]), ] """ hybrid_argnames = ("mps",) wire_argnames = ("wires", "work_wires") static_argnames = ("right_canonicalize",) arg_specs = {"wires": Wire[-1], "work_wires": Wire[-1]} grad_method = None def __init__(self, mps, wires, work_wires=None, right_canonicalize=False): _validate_mps_shape(mps) # Cast each tensor to an array so that every tensor is treated as a single pytree mps = [tensor if hasattr(tensor, "shape") else qp.math.asarray(tensor) for tensor in mps] work_wires = () if work_wires is None else work_wires super().__init__( mps, wires=wires, work_wires=work_wires, right_canonicalize=right_canonicalize )
# pylint: disable-next=unused-argument def _mps_prep_standard_resources(mps, wires, work_wires=None, right_canonicalize=False): num_work_wires = len(work_wires) num_sites = len(mps) return { qp.QubitUnitary( Complex[2 ** (1 + num_work_wires), 2 ** (1 + num_work_wires)], wires=Wire[1 + num_work_wires], ): num_sites } # pylint: disable-next=unused-argument def _work_wires_bond_dimension_condition(mps, wires, work_wires=None, right_canonicalize=False): bond_dimensions = [data.shape[-1] for data in mps] max_bond_dimension = max(bond_dimensions[:-1]) return ( work_wires is not None and len(work_wires) > 0 and 2 ** len(work_wires) >= max_bond_dimension ) @qp.register_condition(_work_wires_bond_dimension_condition) @register_resources(_mps_prep_standard_resources, name="sequential_unitaries") def _mps_prep_standard(mps, wires, work_wires=None, right_canonicalize=False): mps = list(mps) n_wires = len(work_wires) + 1 mps = mps.copy() # Transform the MPS to ensure that the generated matrix is unitary if right_canonicalize: mps = right_canonicalize_mps(mps) # NOTE: tensor legs assignment convention is (vL, p, vR) mps[0] = mps[0].reshape((1, *mps[0].shape)) mps[-1] = mps[-1].reshape((*mps[-1].shape, 1)) interface, dtype = qp.math.get_interface(mps[0]), mps[0].dtype for i, Ai in enumerate(mps): vectors = [] for column in Ai: vector = qp.math.zeros(2**n_wires, like=interface, dtype=dtype) if interface == "jax": vector = vector.at[: len(column[0])].set(column[0]) vector = vector.at[2 ** (n_wires - 1) : 2 ** (n_wires - 1) + len(column[1])].set( column[1] ) else: vector[: len(column[0])] = column[0] vector[2 ** (n_wires - 1) : 2 ** (n_wires - 1) + len(column[1])] = column[1] vectors.append(vector) vectors = qp.math.stack(vectors).T # The unitary is completed using QR decomposition d, k = vectors.shape assert d == 2**n_wires, "The first dimension of the vectors must match 2**n_wires." assert ( k <= d ), "The second dimension of the vectors must be less than or equal to 2**(n_wires-1)." new_columns = qp.math.array(np.random.RandomState(42).random((d, d - k))) unitary_matrix, R = qp.math.linalg.qr(qp.math.hstack([vectors, new_columns])) unitary_matrix *= qp.math.sign(qp.math.diag(R)) # Enforce uniqueness for QR decomposition qp.QubitUnitary(unitary_matrix, wires=[wires[i], *work_wires]) add_decomps(MPSPrep, _mps_prep_standard)