本文整理汇总了Python中numpy.linalg.multi_dot方法的典型用法代码示例。如果您正苦于以下问题:Python linalg.multi_dot方法的具体用法?Python linalg.multi_dot怎么用?Python linalg.multi_dot使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy.linalg
的用法示例。
在下文中一共展示了linalg.multi_dot方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_boson_operator_sparse_multi_mode
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import multi_dot [as 别名]
def test_boson_operator_sparse_multi_mode(self):
op = BosonOperator('0^ 1 1^ 2')
res = boson_operator_sparse(op, self.d).toarray()
b0 = boson_ladder_sparse(3, 0, 0, self.d).toarray()
b1 = boson_ladder_sparse(3, 1, 0, self.d).toarray()
b2 = boson_ladder_sparse(3, 2, 0, self.d).toarray()
expected = multi_dot([b0.T, b1, b1.T, b2])
self.assertTrue(numpy.allclose(res, expected))
op = QuadOperator('q0 p0 p1')
res = boson_operator_sparse(op, self.d, self.hbar).toarray()
expected = numpy.identity(self.d**2)
for term in op.terms:
for i, j in term:
expected = expected.dot(single_quad_op_sparse(
2, i, j, self.hbar, self.d).toarray())
self.assertTrue(numpy.allclose(res, expected))
示例2: estimate_cor
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import multi_dot [as 别名]
def estimate_cor(wmat, ldmat, intercept=False):
"""
Estimate the sample correlation structure for predicted expression.
:param wmat: numpy.ndarray eQTL weight matrix for a risk region
:param ldmat: numpy.ndarray LD matrix for a risk region
:param intercept: bool to return the intercept variable or not
:return: tuple (pred_expr correlation, intercept variable; None if intercept=False)
"""
wcov = mdot([wmat.T, ldmat, wmat])
scale = np.diag(1 / np.sqrt(np.diag(wcov)))
wcor = mdot([scale, wcov, scale])
if intercept:
inter = mdot([scale, wmat.T, ldmat])
return wcor, inter
else:
return wcor, None
示例3: assoc_test
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import multi_dot [as 别名]
def assoc_test(weights, gwas, ldmat, heterogeneity=False):
"""
TWAS association test.
:param weights: numpy.ndarray of eQTL weights
:param gwas: pyfocus.GWAS object
:param ldmat: numpy.ndarray LD matrix
:param heterogeneity: bool estimate variance from multiplicative random effect
:return: tuple (beta, se)
"""
p = ldmat.shape[0]
assoc = np.dot(weights, gwas.Z)
if heterogeneity:
resid = assoc - gwas.Z
resid_var = mdot([resid, lin.pinvh(ldmat), resid]) / p
else:
resid_var = 1
se = np.sqrt(resid_var * mdot([weights, ldmat, weights]))
return assoc, se
示例4: test_diagonalization
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import multi_dot [as 别名]
def test_diagonalization(self, obs, mat, eigs, tol):
"""Test the method transforms standard observables into the Z-gate."""
ob = obs(wires=0)
A = ob.matrix
diag_gates = ob.diagonalizing_gates()
U = np.eye(2)
if diag_gates:
mats = [i.matrix for i in diag_gates]
# Need to revert the order in which the matrices are applied such that they adhere to the order
# of matrix multiplication
# E.g. for PauliY: [PauliZ(wires=self.wires), S(wires=self.wires), Hadamard(wires=self.wires)]
# becomes Hadamard @ S @ PauliZ, where @ stands for matrix multiplication
mats = mats[::-1]
U = multi_dot([np.eye(2)] + mats)
res = U @ A @ U.conj().T
expected = np.diag(eigs)
assert np.allclose(res, expected, atol=tol, rtol=0)
示例5: test_x_decomposition
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import multi_dot [as 别名]
def test_x_decomposition(self, tol):
"""Tests that the decomposition of the PauliX is correct"""
op = qml.PauliX(wires=0)
res = op.decomposition(0)
assert len(res) == 3
assert res[0].name == "PhaseShift"
assert res[0].wires == [0] #qml.wires.Wires([0])
assert res[0].params[0] == np.pi / 2
assert res[1].name == "RX"
assert res[1].wires == [0] #qml.wires.Wires([0])
assert res[1].params[0] == np.pi
assert res[2].name == "PhaseShift"
assert res[2].wires == [0] #qml.wires.Wires([0])
assert res[2].params[0] == np.pi / 2
decomposed_matrix = np.linalg.multi_dot([i.matrix for i in reversed(res)])
assert np.allclose(decomposed_matrix, op.matrix, atol=tol, rtol=0)
示例6: test_y_decomposition
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import multi_dot [as 别名]
def test_y_decomposition(self, tol):
"""Tests that the decomposition of the PauliY is correct"""
op = qml.PauliY(wires=0)
res = op.decomposition(0)
assert len(res) == 3
assert res[0].name == "PhaseShift"
assert res[0].wires == [0] #qml.wires.Wires([0])
assert res[0].params[0] == np.pi / 2
assert res[1].name == "RY"
assert res[1].wires == [0] #qml.wires.Wires([0])
assert res[1].params[0] == np.pi
assert res[2].name == "PhaseShift"
assert res[2].wires == [0] #qml.wires.Wires([0])
assert res[2].params[0] == np.pi / 2
decomposed_matrix = np.linalg.multi_dot([i.matrix for i in reversed(res)])
assert np.allclose(decomposed_matrix, op.matrix, atol=tol, rtol=0)
示例7: test_hadamard_decomposition
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import multi_dot [as 别名]
def test_hadamard_decomposition(self, tol):
"""Tests that the decomposition of the Hadamard gate is correct"""
op = qml.Hadamard(wires=0)
res = op.decomposition(0)
assert len(res) == 3
assert res[0].name == "PhaseShift"
assert res[0].wires == [0] #qml.wires.Wires([0])
assert res[0].params[0] == np.pi / 2
assert res[1].name == "RX"
assert res[1].wires == [0] #qml.wires.Wires([0])
assert res[0].params[0] == np.pi / 2
assert res[2].name == "PhaseShift"
assert res[2].wires == [0] #qml.wires.Wires([0])
assert res[0].params[0] == np.pi / 2
decomposed_matrix = np.linalg.multi_dot([i.matrix for i in reversed(res)])
assert np.allclose(decomposed_matrix, op.matrix, atol=tol, rtol=0)
示例8: __init__
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import multi_dot [as 别名]
def __init__(self):
matrix_pairs = {}
rng = np.random.RandomState(SEED)
for s in SIZES:
for n_terms in [2, 4]:
for explicit in [True, False]:
arrays = [
rng.standard_normal((s if t % 2 == 0 else 2 * s,
2 * s if t % 2 == 0 else s))
for t in range(n_terms)]
matrices_ = [
matrices.DenseRectangularMatrix(a) for a in arrays]
if explicit:
matrix = matrices.MatrixProduct(matrices_)
else:
matrix = reduce(lambda a, b: a @ b, matrices_)
matrix_pairs[(s, n_terms, explicit)] = (
matrix, nla.multi_dot(arrays))
super().__init__(matrix_pairs, rng)
示例9: test_basic_function_with_three_arguments
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import multi_dot [as 别名]
def test_basic_function_with_three_arguments(self):
# multi_dot with three arguments uses a fast hand coded algorithm to
# determine the optimal order. Therefore test it separately.
A = np.random.random((6, 2))
B = np.random.random((2, 6))
C = np.random.random((6, 2))
assert_almost_equal(multi_dot([A, B, C]), A.dot(B).dot(C))
assert_almost_equal(multi_dot([A, B, C]), np.dot(A, np.dot(B, C)))
示例10: test_basic_function_with_two_arguments
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import multi_dot [as 别名]
def test_basic_function_with_two_arguments(self):
# separate code path with two arguments
A = np.random.random((6, 2))
B = np.random.random((2, 6))
assert_almost_equal(multi_dot([A, B]), A.dot(B))
assert_almost_equal(multi_dot([A, B]), np.dot(A, B))
示例11: test_basic_function_with_dynamic_programing_optimization
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import multi_dot [as 别名]
def test_basic_function_with_dynamic_programing_optimization(self):
# multi_dot with four or more arguments uses the dynamic programing
# optimization and therefore deserve a separate
A = np.random.random((6, 2))
B = np.random.random((2, 6))
C = np.random.random((6, 2))
D = np.random.random((2, 1))
assert_almost_equal(multi_dot([A, B, C, D]), A.dot(B).dot(C).dot(D))
示例12: test_vector_as_first_argument
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import multi_dot [as 别名]
def test_vector_as_first_argument(self):
# The first argument can be 1-D
A1d = np.random.random(2) # 1-D
B = np.random.random((2, 6))
C = np.random.random((6, 2))
D = np.random.random((2, 2))
# the result should be 1-D
assert_equal(multi_dot([A1d, B, C, D]).shape, (2,))
示例13: test_vector_as_last_argument
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import multi_dot [as 别名]
def test_vector_as_last_argument(self):
# The last argument can be 1-D
A = np.random.random((6, 2))
B = np.random.random((2, 6))
C = np.random.random((6, 2))
D1d = np.random.random(2) # 1-D
# the result should be 1-D
assert_equal(multi_dot([A, B, C, D1d]).shape, (6,))
示例14: test_too_few_input_arrays
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import multi_dot [as 别名]
def test_too_few_input_arrays(self):
assert_raises(ValueError, multi_dot, [])
assert_raises(ValueError, multi_dot, [np.random.random((3, 3))])
示例15: test_vector_as_first_and_last_argument
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import multi_dot [as 别名]
def test_vector_as_first_and_last_argument(self):
# The first and last arguments can be 1-D
A1d = np.random.random(2) # 1-D
B = np.random.random((2, 6))
C = np.random.random((6, 2))
D1d = np.random.random(2) # 1-D
# the result should be a scalar
assert_equal(multi_dot([A1d, B, C, D1d]).shape, ())