jcmgray / quimb

A python library for quantum information and many-body calculations including tensor networks.
http://quimb.readthedocs.io
Other
455 stars 107 forks source link

Norm calculation of MPS with modified data fails #191

Closed VolodyaCO closed 11 months ago

VolodyaCO commented 12 months ago

What happened?

I am trying to compute the norm of an MPS that has an identity node in-between each physical node. When I try to compare the norm of this modified MPS versus its original version, the norms differ.

What did you expect to happen?

Since both MPSs have the same tensors apart from some interleaving identity tensors, I'd expect the norm to be the same.

Minimal Complete Verifiable Example

def create_mps_from_random_mps(mps: qtn.MatrixProductState):
    tn = qtn.Tensor(data=mps.tensors[0].data, inds=("k0", "b0a"), tags=("P0"))
    for i, tensor in enumerate(mps.tensors[1:-1]):
        i += 1
        tn &= qtn.Tensor(data=np.eye(mps.max_bond()), inds=(f"b{i-1}a", f"b{i-1}b"), tags=(f"V{i}"))
        tn &= qtn.Tensor(data=tensor.data, inds=(f"b{i-1}b", f"k{i}", f"b{i}a"), tags=(f"P{i}"))
    n_sites = mps.nsites
    tn &= qtn.Tensor(data=np.eye(mps.max_bond()), inds=(f"b{n_sites-2}a", f"b{n_sites-2}b"), tags=(f"V{n_sites-1}"))
    tn &= qtn.Tensor(data=mps.tensors[-1].data, inds=(f"b{n_sites-2}b", f"k{n_sites-1}"), tags=(f"P{n_sites-1}"))
    return tn

mps3 = qtn.MPS_rand_state(3, 2, 2)
mps4 = create_mps_from_random_mps(mps3)

print(mps3.norm(), mps4.norm())
# (0.9999999999999999, 2.778019363799768)

Relevant log output

No response

Anything else we need to know?

No response

Environment

Version: 1.5.0

jcmgray commented 12 months ago

Hi @VolodyaCO, the problem I think is just that you are creating the new MPS with different ordering of the indices - you will see if you put it a bond_dim != phys_dim then the indices are not aligned.

You can see from the constructor of MatrixProductState that it is created with ordering ='lrp', i.e. left right physical rather than left physical right, your example works if you label your new tensors the same way.

However, in general I would say that the ethos of quimb is that one shouldn't write things depending on a specific order, instead using the labelled indices and tags! E.g.

Screenshot from 2023-07-11 18-31-03

Though of course no problem if you are just learning how things are structured in quimb.