Пример #1
0
    def random(sites,
               bonds=None,
               cut=None,
               nmax=None,
               ttype='D',
               dtype=np.float64):
        '''
        Generate a random mps.

        Parameters
        ----------
        sites : list of Label/int/QuantumNumbers
            The labels/number-of-degrees-of-freedom/quantum-numbers of the physical legs.
        bonds : optional
            * list of Label/str
                The labels/identifiers of the virtual legs.
            * 2-list of QuantumNumber
                The quantum number of the first and last virtual legs.
        cut : int, optional
            The index of the connecting link.
        nmax : int, optional
            The maximum number of singular values to be kept.
        ttype : 'D'/'S', optional
            Tensor type. 'D' for dense and 'S' for sparse.
        dtype : np.float64, np.complex128, optional
            The data type of the random mps.

        Returns
        -------
        MPS
            The random mixed-canonical mps.
        '''
        np.random.seed()
        sites = [
            site if isinstance(site, Label) else Label(
                '__MPS_RANDOM_S_%s__' % i, qns=site)
            for i, site in enumerate(sites)
        ]
        if bonds is None or not isinstance(bonds[+0], Label) or not isinstance(
                bonds[-1], Label):
            if bonds is not None:
                iqns = bonds[+0].qns if isinstance(
                    bonds[+0], Label) else bonds[+0] if isinstance(
                        bonds[+0], QNS) else QNS.mono(bonds[+0]) if isinstance(
                            bonds[+0], QN) else 1
                oqns = bonds[-1].qns if isinstance(
                    bonds[-1], Label) else bonds[-1] if isinstance(
                        bonds[-1], QNS) else QNS.mono(bonds[-1]) if isinstance(
                            bonds[-1], QN) else 1
            else:
                iqns, oqns = 1, 1
            bonds = [
                Label('__MPS_RANDOM_B_%s__' % i, None, None)
                for i in range(len(sites) + 1)
            ]
            bonds[+0] = bonds[+0].replace(qns=iqns)
            bonds[-1] = bonds[-1].replace(qns=oqns)
        else:
            assert len(bonds) == len(sites) + 1
            bonds = [
                bond if isinstance(bond, Label) else Label(bond, None, None)
                for bond in bonds
            ]
        qnon, shape = next(iter(sites)).qnon, tuple(
            [site.dim for site in sites])
        if ttype == 'S': assert qnon
        if qnon:
            result = 0
            if dtype in (np.float32, np.float64):
                coeffs = np.random.random(nmax)
            else:
                coeffs = np.random.random(nmax) + 1j * np.random.random(nmax)
            for k, indices in enumerate(
                    QNS.decomposition([site.qns for site in sites],
                                      bonds[-1].qns[0] - bonds[+0].qns[0],
                                      method='monte carlo',
                                      nmax=nmax)):
                ms = [
                    np.array(
                        [1.0 if i == index else 0.0 for i in range(site.dim)],
                        dtype=dtype) for site, index in zip(sites, indices)
                ]
                result += MPS.productstate(ms, sites, copy(bonds)) * coeffs[k]
            if ttype == 'S':
                for m in result:
                    m.qnsort()
                result = result.tosparse()
        else:
            ms = []
            for i in range(len(sites)):
                if dtype in (np.float32, np.float64):
                    ms.append(np.random.random((nmax, shape[i], nmax)))
                else:
                    ms.append(
                        np.random.random((nmax, shape[i], nmax)) +
                        1j * np.random.random((nmax, shape[i], nmax)))
            result = MPS.compose(ms=ms, sites=sites, bonds=bonds)
        if cut is None:
            result.canonicalize(cut=len(sites) / 2, nmax=nmax)
            result._merge_ABL_()
        else:
            result.canonicalize(cut=cut, nmax=nmax)
        return result
Пример #2
0
    def random(sites, bonds=None, cut=None, nmax=None, dtype=np.float64):
        '''
        Generate a random mps.

        Parameters
        ----------
        sites : list of Label/int/QuantumNumbers
            The labels/number-of-degrees-of-freedom/quantum-numbers of the physical legs.
        bonds : optional
            * list of Label
                The labels of the virtual legs.
            * 2-list of QuantumNumber
                The quantum number of the first and last virtual legs.
        cut : int, optional
            The index of the connecting link.
        nmax : int, optional
            The maximum number of singular values to be kept.
        dtype : np.float64, np.complex128, optional
            The data type of the random mps.

        Returns
        -------
        MPS
            The random mixed-canonical mps.
        '''
        np.random.seed()
        sites = [
            site if isinstance(site, Label) else Label(
                '__MPS_RANDOM_S_%s__' % i, qns=site)
            for i, site in enumerate(sites)
        ]
        if bonds is not None and all(
                isinstance(bond, Label) for bond in bonds):
            assert len(bonds) == len(sites) + 1
        else:
            if bonds is not None:
                assert len(bonds) == 2 and isinstance(
                    bonds[0], QN) and isinstance(bonds[1], QN)
            iqns, oqns = (QNS.mono(bonds[0]),
                          QNS.mono(bonds[1])) if bonds is not None else (1, 1)
            bonds = [
                Label(
                    '__MPS_RANDOM_B_%s__' % i,
                    qns=iqns if i == 0 else oqns if i == len(sites) else None)
                for i in xrange(len(sites) + 1)
            ]
        mode, shape = 'QN' if next(iter(sites)).qnon else 'NB', tuple(
            [site.dim for site in sites])
        if mode == 'QN':
            result = 0
            if dtype in (np.float32, np.float64):
                coeffs = np.random.random(nmax)
            else:
                coeffs = np.random.random(nmax) + 1j * np.random.random(nmax)
            for k, indices in enumerate(
                    QNS.decomposition([site.qns for site in sites],
                                      bonds[-1].qns[0] - bonds[0].qns[0],
                                      method='monte carlo',
                                      nmax=nmax)):
                ms = [
                    np.array(
                        [1.0 if i == index else 0.0 for i in xrange(site.dim)],
                        dtype=dtype) for site, index in zip(sites, indices)
                ]
                result += MPS.productstate(ms, sites, copy(bonds)) * coeffs[k]
        else:
            ms = []
            for i in xrange(len(sites)):
                if dtype in (np.float32, np.float64):
                    ms.append(np.random.random((nmax, shape[i], nmax)))
                else:
                    ms.append(
                        np.random.random((nmax, shape[i], nmax)) +
                        1j * np.random.random((nmax, shape[i], nmax)))
            result = MPS(mode=mode, ms=ms, sites=sites, bonds=bonds)
        if cut is None:
            result.canonicalize(cut=len(sites) / 2, nmax=nmax)
            result._merge_ABL_()
        else:
            result.canonicalize(cut=cut, nmax=nmax)
        return result
Пример #3
0
    def random(sites,bonds=None,cut=None,nmax=None,ttype='D',dtype=np.float64):
        '''
        Generate a random mps.

        Parameters
        ----------
        sites : list of Label/int/QuantumNumbers
            The labels/number-of-degrees-of-freedom/quantum-numbers of the physical legs.
        bonds : optional
            * list of Label/str
                The labels/identifiers of the virtual legs.
            * 2-list of QuantumNumber
                The quantum number of the first and last virtual legs.
        cut : int, optional
            The index of the connecting link.
        nmax : int, optional
            The maximum number of singular values to be kept.
        ttype : 'D'/'S', optional
            Tensor type. 'D' for dense and 'S' for sparse.
        dtype : np.float64, np.complex128, optional
            The data type of the random mps.

        Returns
        -------
        MPS
            The random mixed-canonical mps.
        '''
        np.random.seed()
        sites=[site if isinstance(site,Label) else Label('__MPS_RANDOM_S_%s__'%i,qns=site) for i,site in enumerate(sites)]
        if bonds is None or not isinstance(bonds[+0],Label) or not isinstance(bonds[-1],Label):
            if bonds is not None:
                iqns=bonds[+0].qns if isinstance(bonds[+0],Label) else bonds[+0] if isinstance(bonds[+0],QNS) else QNS.mono(bonds[+0]) if isinstance(bonds[+0],QN) else 1
                oqns=bonds[-1].qns if isinstance(bonds[-1],Label) else bonds[-1] if isinstance(bonds[-1],QNS) else QNS.mono(bonds[-1]) if isinstance(bonds[-1],QN) else 1
            else:
                iqns,oqns=1,1
            bonds=[Label('__MPS_RANDOM_B_%s__'%i,None,None) for i in range(len(sites)+1)]
            bonds[+0]=bonds[+0].replace(qns=iqns)
            bonds[-1]=bonds[-1].replace(qns=oqns)
        else:
            assert len(bonds)==len(sites)+1
            bonds=[bond if isinstance(bond,Label) else Label(bond,None,None) for bond in bonds]
        qnon,shape=next(iter(sites)).qnon,tuple([site.dim for site in sites])
        if ttype=='S': assert qnon
        if qnon:
            result=0
            if dtype in (np.float32,np.float64):
                coeffs=np.random.random(nmax)
            else:
                coeffs=np.random.random(nmax)+1j*np.random.random(nmax)
            for k,indices in enumerate(QNS.decomposition([site.qns for site in sites],bonds[-1].qns[0]-bonds[+0].qns[0],method='monte carlo',nmax=nmax)):
                ms=[np.array([1.0 if i==index else 0.0 for i in range(site.dim)],dtype=dtype) for site,index in zip(sites,indices)]
                result+=MPS.productstate(ms,sites,copy(bonds))*coeffs[k]
            if ttype=='S':
                for m in result: m.qnsort()
                result=result.tosparse()
        else:
            ms=[]
            for i in range(len(sites)):
                if dtype in (np.float32,np.float64):
                    ms.append(np.random.random((nmax,shape[i],nmax)))
                else:
                    ms.append(np.random.random((nmax,shape[i],nmax))+1j*np.random.random((nmax,shape[i],nmax)))
            result=MPS.compose(ms=ms,sites=sites,bonds=bonds)
        if cut is None:
            result.canonicalize(cut=len(sites)/2,nmax=nmax)
            result._merge_ABL_()
        else:
            result.canonicalize(cut=cut,nmax=nmax)
        return result