Пример #1
0
def calculate_ground_state_wf(d, e, saved_lanczos_vectors): 
    """Calculates the ground state wavefunction.
    
    Parameters
    ----------
    d : a numpy array with ndim = 1.
        The elements of the diagonal of the tridiagonal matrix. The size
	of `d`.
    e : a numpy array with ndim = 1.
        The off-diagonal elements of the tridiagonal matrix. The size of
	`e` equals the size of `d`, and it is padded with a zero at the
	end. 
    saved_lanczos_vectors : a list of Wavefunctions.
        The Lanczos vectors that are saved.

    Returns
    -------
    result : a Wavefunction.
        The ground state function (normalized).
    """
    evals, evecs = diagonalize_tridiagonal_matrix(d, e, True)
    min_index = np.argsort(evals)[0]
    coefficients_of_gs_in_krylov_space = evecs[:, min_index]
    assert(len(saved_lanczos_vectors) == len(coefficients_of_gs_in_krylov_space))
    
    result = Wavefunction(saved_lanczos_vectors[0].left_dim,
		          saved_lanczos_vectors[0].right_dim) 
    result.set_to_zero()
    for i in range(len(saved_lanczos_vectors)):
        result.as_matrix += ( coefficients_of_gs_in_krylov_space[i] * 
		saved_lanczos_vectors[i].as_matrix )

    result.normalize()
    return result 
Пример #2
0
    def apply(self, wf):
        """
    	Applies the composite operator to a wavefunction.

	Applies each of the operator components that form the composite
	operator and sums up the results.
    
    	Parameters
    	----------
    	wf : A Wavefunction
    	    The wavefunction you want to apply the operator.
    
    	Returns
    	-------
    	result : a Wavefunction
    	    The wavefunction resulting of the operation. It has the same
	    shape (i.e. is in the same Hilbert space) as the one passed as
	    argument.
    
    	Raises
    	------
    	DMRGException
    	    if self.list_of_components is empty.
	
	Examples
	--------
	>>> import numpy as np
	>>> from dmrg101.core.operators import CompositeOperator 
	>>> from dmrg101.core.wavefunction import Wavefunction
	>>> wf = Wavefunction(2, 2)
	>>> wf.randomize()
	>>> identity_operator = CompositeOperator(2, 2)
	>>> identity_operator.add(np.eye(2, 2), np.eye(2, 2))
	>>> new_wf = identity_operator.apply(wf)
	>>> np.array_equal(new_wf.as_matrix,  wf.as_matrix)
	True
    	"""
        if not self.list_of_components:
            raise DMRGException("Composite operator is empty.")

        result = Wavefunction(self.left_dim, self.right_dim)
        result.set_to_zero()

        for component in self.list_of_components:
            result.as_matrix += component.apply(wf).as_matrix
        return result
Пример #3
0
    def apply(self, wf):
        """
    	Applies the composite operator to a wavefunction.

	Applies each of the operator components that form the composite
	operator and sums up the results.
    
    	Parameters
    	----------
    	wf : A Wavefunction
    	    The wavefunction you want to apply the operator.
    
    	Returns
    	-------
    	result : a Wavefunction
    	    The wavefunction resulting of the operation. It has the same
	    shape (i.e. is in the same Hilbert space) as the one passed as
	    argument.
    
    	Raises
    	------
    	DMRGException
    	    if self.list_of_components is empty.
	
	Examples
	--------
	>>> import numpy as np
	>>> from dmrg101.core.operators import CompositeOperator 
	>>> from dmrg101.core.wavefunction import Wavefunction
	>>> wf = Wavefunction(2, 2)
	>>> wf.randomize()
	>>> identity_operator = CompositeOperator(2, 2)
	>>> identity_operator.add(np.eye(2, 2), np.eye(2, 2))
	>>> new_wf = identity_operator.apply(wf)
	>>> np.array_equal(new_wf.as_matrix,  wf.as_matrix)
	True
    	"""
        if not self.list_of_components:
            raise DMRGException("Composite operator is empty.")

        result = Wavefunction(self.left_dim, self.right_dim)
        result.set_to_zero()

        for component in self.list_of_components:
            result.as_matrix += component.apply(wf).as_matrix
        return result
Пример #4
0
    def apply(self, wf):
        """
    	Applies the operator to a wavefunction.

    	Parameters
    	----------
    	wf : A Wavefunction
    	    The wavefunction you want to apply the operator.
    
    	Returns
    	-------
    	result : a Wavefunction
    	    The wavefunction resulting of the operation. It has the same
	    shape (i.e. is in the same Hilbert space) as the one passed as
	    argument.
    
    	Raises
    	------
    	DMRGException
	    if `wf` is has not the correct dimensions as a matrix.
	
	Examples
	--------
	>>> import numpy as np
	>>> from dmrg101.core.operators import Operator
	>>> from dmrg101.core.wavefunction import Wavefunction
	>>> wf = Wavefunction(2, 2)
	>>> wf.randomize()
	>>> identity_operator = Operator(np.eye(2, 2), np.eye(2, 2))
	>>> new_wf = identity_operator.apply(wf)
	>>> np.array_equal(new_wf.as_matrix,  wf.as_matrix)
	True
    	"""
        if wf.as_matrix.shape != ((self.left_dim, self.right_dim)):
            raise DMRGException("Wavefunction does not fit.")

        result = Wavefunction(self.left_dim, self.right_dim)

        tmp = np.dot(self.right_op, wf.as_matrix.transpose())
        result.as_matrix = self.parameter * np.dot(self.left_op,
                                                   tmp.transpose())

        return result
Пример #5
0
    def apply(self, wf):
        """
    	Applies the operator to a wavefunction.

    	Parameters
    	----------
    	wf : A Wavefunction
    	    The wavefunction you want to apply the operator.
    
    	Returns
    	-------
    	result : a Wavefunction
    	    The wavefunction resulting of the operation. It has the same
	    shape (i.e. is in the same Hilbert space) as the one passed as
	    argument.
    
    	Raises
    	------
    	DMRGException
	    if `wf` is has not the correct dimensions as a matrix.
	
	Examples
	--------
	>>> import numpy as np
	>>> from dmrg101.core.operators import Operator
	>>> from dmrg101.core.wavefunction import Wavefunction
	>>> wf = Wavefunction(2, 2)
	>>> wf.randomize()
	>>> identity_operator = Operator(np.eye(2, 2), np.eye(2, 2))
	>>> new_wf = identity_operator.apply(wf)
	>>> np.array_equal(new_wf.as_matrix,  wf.as_matrix)
	True
    	"""
        if wf.as_matrix.shape != ((self.left_dim, self.right_dim)):
            raise DMRGException("Wavefunction does not fit.")

        result = Wavefunction(self.left_dim, self.right_dim)

        tmp = np.dot(self.right_op, wf.as_matrix.transpose())
        result.as_matrix = self.parameter * np.dot(self.left_op, tmp.transpose())

        return result
Пример #6
0
def calculate_ground_state_wf(d, e, saved_lanczos_vectors):
    """Calculates the ground state wavefunction.
    
    Parameters
    ----------
    d : a numpy array with ndim = 1.
        The elements of the diagonal of the tridiagonal matrix. The size
	of `d`.
    e : a numpy array with ndim = 1.
        The off-diagonal elements of the tridiagonal matrix. The size of
	`e` equals the size of `d`, and it is padded with a zero at the
	end. 
    saved_lanczos_vectors : a list of Wavefunctions.
        The Lanczos vectors that are saved.

    Returns
    -------
    result : a Wavefunction.
        The ground state function (normalized).
    """
    evals, evecs = diagonalize_tridiagonal_matrix(d, e, True)
    min_index = np.argsort(evals)[0]
    coefficients_of_gs_in_krylov_space = evecs[:, min_index]
    assert (
        len(saved_lanczos_vectors) == len(coefficients_of_gs_in_krylov_space))

    result = Wavefunction(saved_lanczos_vectors[0].left_dim,
                          saved_lanczos_vectors[0].right_dim)
    result.set_to_zero()
    for i in range(len(saved_lanczos_vectors)):
        result.as_matrix += (coefficients_of_gs_in_krylov_space[i] *
                             saved_lanczos_vectors[i].as_matrix)

    result.normalize()
    return result
Пример #7
0
def calculate_ground_state(hamiltonian,
                           initial_wf=None,
                           min_lanczos_iterations=3,
                           too_many_iterations=1000,
                           precision=0.000001):
    """Calculates the ground state energy and wavefunction.

    Parameters
    ----------
    hamiltonian : a CompositeOperator
        The hamiltonian you want to diagonalize.
    initial_wf : a Wavefunction, optional
        The wavefunction that will be used as seed. If None, a random one
	if used.
    min_lanczos_iterations : an int, optional.
        The number of iterations before starting the diagonalizations.
    too_many_iterations : a int, optional.
        The maximum number of iterations allowed.
    precision : a double, optional.
        The accepted precision to which the ground state energy is
	considered not improving.
    
    Returns 
    -------
    gs_energy : a double.
        The ground state energy.
    gs_wf : a Wavefunction.
        The ground state wavefunction (normalized.)
    """
    if initial_wf is None:
        initial_wf = Wavefunction(hamiltonian.left_dim, hamiltonian.right_dim)
        initial_wf.randomize()

    gs_energy, d, e, saved_lanczos_vectors = (calculate_ground_state_energy(
        hamiltonian, initial_wf, min_lanczos_iterations, too_many_iterations,
        precision))
    gs_wf = calculate_ground_state_wf(d, e, saved_lanczos_vectors)

    return gs_energy, gs_wf
Пример #8
0
def calculate_ground_state(hamiltonian, initial_wf = None, 
			   min_lanczos_iterations = 3, 
		           too_many_iterations = 1000, 
			   precision = 0.000001):
    """Calculates the ground state energy and wavefunction.

    Parameters
    ----------
    hamiltonian : a CompositeOperator
        The hamiltonian you want to diagonalize.
    initial_wf : a Wavefunction, optional
        The wavefunction that will be used as seed. If None, a random one
	if used.
    min_lanczos_iterations : an int, optional.
        The number of iterations before starting the diagonalizations.
    too_many_iterations : a int, optional.
        The maximum number of iterations allowed.
    precision : a double, optional.
        The accepted precision to which the ground state energy is
	considered not improving.
    
    Returns 
    -------
    gs_energy : a double.
        The ground state energy.
    gs_wf : a Wavefunction.
        The ground state wavefunction (normalized.)
    """
    if initial_wf is None:
        initial_wf = Wavefunction(hamiltonian.left_dim,
			          hamiltonian.right_dim)
	initial_wf.randomize()

    gs_energy, d, e, saved_lanczos_vectors = (
        calculate_ground_state_energy(hamiltonian, initial_wf, min_lanczos_iterations, 
		                      too_many_iterations, precision) )
    gs_wf = calculate_ground_state_wf(d, e, saved_lanczos_vectors)

    return gs_energy, gs_wf
Пример #9
0
def create_two_qbit_system_in_singlet(psi):
    """ Returns the wf of the system as a function of `psi`.

    The (normalized) wavefunction of the two-qbit system can be
    parametrized as a function an angle `psi`. 

    Parameters
    ----------
    psi : a double 
        Parametrizes the wavefunction.
    
    Returns
    -------
    result : a Wavefunction
        The wavefunction of the two-qbit system for the given `psi`.
    """
    result = Wavefunction(2, 2)

    # set the different components.
    result.as_matrix[0, 0] = 0.
    result.as_matrix[0, 1] = cos(psi)
    result.as_matrix[1, 0] = sin(psi)
    result.as_matrix[1, 1] = 0.
    return result
Пример #10
0
def create_two_qbit_system_in_singlet(psi):
    """ Returns the wf of the system as a function of `psi`.

    The (normalized) wavefunction of the two-qbit system can be
    parametrized as a function an angle `psi`. 

    Parameters
    ----------
    psi : a double 
        Parametrizes the wavefunction.
    
    Returns
    -------
    result : a Wavefunction
        The wavefunction of the two-qbit system for the given `psi`.
    """
    result = Wavefunction(2, 2)

    # set the different components.
    result.as_matrix[0, 0] = 0.
    result.as_matrix[0, 1] = cos(psi)
    result.as_matrix[1, 0] = sin(psi)
    result.as_matrix[1, 1] = 0.
    return result