def asys(U, t, H, dH): """ Coupled differential equation to solve. Use sparse*dense to speed up """ RHS = [] dUdt = -1j * utils.to_scipy_sparse(H(t)) * np.matrix(U[0]) # dUdt = dUdt.toarray() RHS.append(dUdt) for i, key in enumerate(dH): temp = -1j * (utils.to_scipy_sparse(dH[key](t)) * np.matrix(U[0]) + utils.to_scipy_sparse(H(t)) * np.matrix(U[i + 1])) RHS.append(temp) return RHS
def test_format_scipy_sparse(): for test in _tests: lhs = represent(test[0], basis=A, format='scipy.sparse') rhs = to_scipy_sparse(test[1]) if isinstance(lhs, scipy_sparse_matrix): assert np.linalg.norm((lhs-rhs).todense()) == 0.0 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 _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_to_scipy_sparse(): if not np: skip("numpy not installed.") if not scipy: skip("scipy not installed.") else: sparse = scipy.sparse result = sparse.csr_matrix([[1, 2], [3, 4]], dtype='complex') assert np.linalg.norm((to_scipy_sparse(m) - result).todense()) == 0.0
def test_to_scipy_sparse(): if not np: skip("numpy not installed or Python too old.") if not scipy: skip("scipy not installed.") else: sparse = scipy.sparse result = sparse.csr_matrix([[1,2],[3,4]], dtype='complex') assert np.linalg.norm((to_scipy_sparse(m) - result).todense()) == 0.0
def test_format_scipy_sparse(): if not np: skip("numpy not installed or Python too old.") if not scipy: skip("scipy not installed.") for test in _tests: lhs = represent(test[0], basis=A, format='scipy.sparse') rhs = to_scipy_sparse(test[1]) if isinstance(lhs, scipy_sparse_matrix): assert np.linalg.norm((lhs-rhs).todense()) == 0.0 else: assert lhs == rhs
def test_format_scipy_sparse(): if not np: skip("numpy not installed or Python too old.") if not scipy: skip("scipy not installed.") for test in _tests: lhs = represent(test[0], basis=A, format='scipy.sparse') rhs = to_scipy_sparse(test[1]) if isinstance(lhs, scipy_sparse_matrix): assert np.linalg.norm((lhs - rhs).todense()) == 0.0 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 infidelity_jac(Utarg, U): d = Utarg.ndim #conjugate transpose of Utarg Utarg = Utarg.conj().T Utarg = utils.to_scipy_sparse(Utarg) #Sort out the components of U Uact = U[0] Ugrad = U[1:] #compute the overlap # gstar = np.conjugate(np.trace(Utarg.dot(Uact))) gstar = np.conjugate(np.trace(Utarg * np.matrix(Uact))) dg = [] for dU in Ugrad: inside = (gstar / np.abs(gstar)) * (1 / d) * np.trace( Utarg * np.matrix(dU)) dg.append(-inside.real) dg = np.array(dg) return dg
def _scipy_sparse_matrix(self, name, m): # TODO: explore different sparse formats. But sparse.kron will use # coo in most cases, so we use that here. m = to_scipy_sparse(m, dtype=self.dtype) self._store_matrix(name, 'scipy.sparse', m)
def test_to_scipy_sparse(): result = sparse.csr_matrix([[1,2],[3,4]], dtype='complex') assert np.linalg.norm((to_scipy_sparse(m) - result).todense()) == 0.0
def test_to_scipy_sparse(): result = sparse.csr_matrix([[1, 2], [3, 4]], dtype='complex') assert np.linalg.norm((to_scipy_sparse(m) - result).todense()) == 0.0
def infidelity(Utarg, Uact): d = len(Utarg) Utarg = Utarg.conj().T Utarg = utils.to_scipy_sparse(Utarg) g = 1.0 - (1 / d) * np.abs(np.trace(Utarg * np.matrix(Uact))) return g