def test_format_numpy(): for test in _tests: lhs = represent(test[0], basis=A, format='numpy') rhs = to_numpy(test[1]) if isinstance(lhs, numpy_ndarray): assert (lhs == rhs).all() else: assert lhs == rhs
def _format_represent(self, result, format): if format == "sympy" and not isinstance(result, Matrix): return to_sympy(result) elif format == "numpy" and not isinstance(result, numpy_ndarray): return to_numpy(result) elif format == "scipy.sparse" and not isinstance(result, scipy_sparse_matrix): return to_scipy_sparse(result) return result
def test_format_numpy(): if not np: skip("numpy not installed or Python too old.") for test in _tests: lhs = represent(test[0], basis=A, format='numpy') rhs = to_numpy(test[1]) if isinstance(lhs, numpy_ndarray): assert (lhs == rhs).all() else: assert lhs == rhs
def _represent(self, **options): """Represent this object in a given basis. This method dispatches to the actual methods that perform the representation. Subclases of QExpr should define various methods to determine how the object will be represented in various bases. The format of these methods is:: def _represent_BasisName(self, basis, **options): Thus to define how a quantum object is represented in the basis of the operator Position, you would define:: def _represent_Position(self, basis, **options): Usually, basis object will be instances of Operator subclasses, but there is a chance we will relax this in the future to accomodate other types of basis sets that are not associated with an operator. If the ``format`` option is given it can be ("sympy", "numpy", "scipy.sparse"). This will ensure that any matrices that result from representing the object are returned in the appropriate matrix format. Parameters ========== basis : Operator The Operator whose basis functions will be used as the basis for representation. options : dict A dictionary of key/value pairs that give options and hints for the representation, such as the number of basis functions to be used. """ basis = options.pop('basis',None) if basis is None: result = self._represent_default_basis(**options) else: result = dispatch_method(self, '_represent', basis, **options) # If we get a matrix representation, convert it to the right format. format = options.get('format', 'sympy') if format == 'sympy' and not isinstance(result, Matrix): result = to_sympy(result) elif format == 'numpy' and not isinstance(result, numpy_ndarray): result = to_numpy(result) elif format == 'scipy.sparse' and\ not isinstance(result, scipy_sparse_matrix): result = to_scipy_sparse(result) return result
def entropy(density): """Compute the entropy of a matrix/density object. This computes -Tr(density*ln(density)) using the eigenvalue decomposition of density, which is given as either a Density instance or a matrix (numpy.ndarray, sympy.Matrix or scipy.sparse). Parameters ========== density : density matrix of type Density, sympy matrix, scipy.sparse or numpy.ndarray Examples ======== >>> from sympy.physics.quantum.density import Density, entropy >>> from sympy.physics.quantum.represent import represent >>> from sympy.physics.quantum.matrixutils import scipy_sparse_matrix >>> from sympy.physics.quantum.spin import JzKet, Jz >>> from sympy import S, log >>> up = JzKet(S(1)/2,S(1)/2) >>> down = JzKet(S(1)/2,-S(1)/2) >>> d = Density((up,S(1)/2),(down,S(1)/2)) >>> entropy(d) log(2)/2 """ if isinstance(density, Density): density = represent(density) # represent in Matrix if isinstance(density, scipy_sparse_matrix): density = to_numpy(density) if isinstance(density, Matrix): eigvals = density.eigenvals().keys() return expand(-sum(e*log(e) for e in eigvals)) elif isinstance(density, numpy_ndarray): import numpy as np eigvals = np.linalg.eigvals(density) return -np.sum(eigvals*np.log(eigvals)) else: raise ValueError( "numpy.ndarray, scipy.sparse or sympy matrix expected")
def _numpy_matrix(self, name, m): m = to_numpy(m, dtype=self.dtype) self._store_matrix(name, 'numpy', m)
def test_to_numpy(): if not np: skip("numpy not installed.") result = np.matrix([[1, 2], [3, 4]], dtype='complex') assert (to_numpy(m) == result).all()
def test_to_numpy(): if not np: skip("numpy not installed or Python too old.") result = np.matrix([[1,2],[3,4]], dtype='complex') assert (to_numpy(m) == result).all()
def test_to_numpy(): if not np: skip("numpy not installed.") result = np.matrix([[1, 2], [3, 4]], dtype="complex") assert (to_numpy(m) == result).all()
def test_to_numpy(): result = np.matrix([[1,2],[3,4]], dtype='complex') assert (to_numpy(m) == result).all()
def test_to_numpy(): result = np.matrix([[1, 2], [3, 4]], dtype='complex') assert (to_numpy(m) == result).all()