本文整理汇总了Python中sympy.eye方法的典型用法代码示例。如果您正苦于以下问题:Python sympy.eye方法的具体用法?Python sympy.eye怎么用?Python sympy.eye使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sympy
的用法示例。
在下文中一共展示了sympy.eye方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: prepare_channel_operator_list
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import eye [as 别名]
def prepare_channel_operator_list(ops_list):
"""
Prepares a list of channel operators.
Args:
ops_list (List): The list of operators to prepare
Returns:
List: The channel operator list
"""
# convert to sympy matrices and verify that each singleton is
# in a tuple; also add identity matrix
from sympy import Matrix, eye
result = []
for ops in ops_list:
if not isinstance(ops, tuple) and not isinstance(ops, list):
ops = [ops]
result.append([Matrix(op) for op in ops])
n = result[0][0].shape[0] # grab the dimensions from the first element
result = [[eye(n)]] + result
return result
# pylint: disable=invalid-name
示例2: test_cu3
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import eye [as 别名]
def test_cu3():
E = eye(2)
UPPER = Matrix([[1, 0], [0, 0]])
LOWER = Matrix([[0, 0], [0, 1]])
theta, phi, lambd = symbols("theta phi lambd")
U = Circuit().rz(lambd)[0].ry(theta)[0].rz(phi)[0].run_with_sympy_unitary()
actual_1 = Circuit().cu3(theta, phi, lambd)[0, 1].run(backend="sympy_unitary")
expected_1 = reduce(TensorProduct, [E, UPPER]) + reduce(TensorProduct, [U, LOWER])
print("actual")
print(simplify(actual_1))
print("expected")
print(simplify(expected_1))
print("diff")
print(simplify(actual_1 - expected_1))
assert simplify(actual_1 - expected_1) == zeros(4)
示例3: test_cu3_realvalue
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import eye [as 别名]
def test_cu3_realvalue():
E = eye(2)
UPPER = Matrix([[1, 0], [0, 0]])
LOWER = Matrix([[0, 0], [0, 1]])
theta = pi * 7 / 11
phi = pi * 5 / 13
lambd = pi * 8 / 17
U = Circuit().u3(theta, phi, lambd)[0].run_with_sympy_unitary()
expected_1 = reduce(TensorProduct, [E, UPPER]) + reduce(TensorProduct, [U, LOWER])
print(expected_1)
for i in range(4):
c = Circuit()
if i % 2 == 1:
c.x[0]
if (i // 2) % 2 == 1:
c.x[1]
actual_i = c.cu3(theta.evalf(), phi.evalf(), lambd.evalf())[0, 1].run_with_numpy()
actual_i = np.array(actual_i).astype(complex).reshape(-1)
expected_i = np.array(expected_1.col(i)).astype(complex).reshape(-1)
assert 0.99999 < np.abs(np.dot(actual_i.conj(), expected_i)) < 1.00001
示例4: rotation_to_matrix
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import eye [as 别名]
def rotation_to_matrix(w):
wx,wy,wz = w
theta = sp.sqrt(wx**2 + wy**2 + wz**2 + wy**2 + wz**2) + EPS
omega = sp.Matrix([[0,-wz,wy],
[wz,0,-wx],
[-wy,wx,0]])
R = sp.eye(3) +\
omega*(sp.sin(theta)/theta) +\
(omega*omega)*((1-sp.cos(theta))/(theta*theta))
return R
示例5: to_transformation_matrix
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import eye [as 别名]
def to_transformation_matrix(translation, orientation_matrix=np.zeros((3, 3))):
"""Convert a tuple (translation_vector, orientation_matrix) to a transformation matrix
Parameters
----------
translation: numpy.array
The translation of your frame presented as a 3D vector.
orientation_matrix: numpy.array
Optional : The orientation of your frame, presented as a 3x3 matrix.
"""
matrix = np.eye(4)
matrix[:-1, :-1] = orientation_matrix
matrix[:-1, -1] = translation
return matrix
示例6: cartesian_to_homogeneous
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import eye [as 别名]
def cartesian_to_homogeneous(cartesian_matrix, matrix_type="numpy"):
"""Converts a cartesian matrix to an homogenous matrix"""
dimension_x, dimension_y = cartesian_matrix.shape
# Square matrix
# Manage different types fo input matrixes
if matrix_type == "numpy":
homogeneous_matrix = np.eye(dimension_x + 1)
elif matrix_type == "sympy":
homogeneous_matrix = sympy.eye(dimension_x + 1)
else:
raise ValueError("Unknown matrix_type: {}".format(matrix_type))
# Add a column filled with 0 and finishing with 1 to the cartesian matrix to transform it into an homogeneous one
homogeneous_matrix[:-1, :-1] = cartesian_matrix
return homogeneous_matrix
示例7: get_generating_function
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import eye [as 别名]
def get_generating_function(f, T=None):
"""
Get the generating function
:param f: function addr
:param T: edges matrix of the function f
:return: the generating function
"""
if not T:
edges = [(a[0].addr, a[1].addr) for a in f.graph.edges()]
N = sorted([x for x in f.block_addrs])
T = []
for b1 in N:
T.append([])
for b2 in N:
if b1 == b2 or (b1, b2) in edges:
T[-1].append(1)
else:
T[-1].append(0)
else:
N = T[0]
T = sympy.Matrix(T)
z = sympy.var('z')
I = sympy.eye(len(N))
tmp = I - z * T
tmp.row_del(len(N) - 1)
tmp.col_del(0)
det_num = tmp.det()
det_den = (I - z * T).det()
quot = det_num / det_den
g_z = ((-1) ** (len(N) + 1)) * quot
return g_z
示例8: test_dagger_unitary
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import eye [as 别名]
def test_dagger_unitary(circuit):
circuit += circuit.dagger()
u = sympy.simplify(sympy.trigsimp(circuit.to_unitary()))
s1, s2 = u.shape
assert s1 == s2
assert u == sympy.eye(s1)
示例9: test_dagger_unitary2
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import eye [as 别名]
def test_dagger_unitary2():
c = Circuit().h[0].s[0]
c += c.dagger()
u = sympy.simplify(c.to_unitary())
assert u.shape == (2, 2)
assert u == sympy.eye(2)
示例10: test_sympy_backend_for_one_qubit_gate
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import eye [as 别名]
def test_sympy_backend_for_one_qubit_gate():
E = eye(2)
X = Matrix([[0, 1], [1, 0]])
Y = Matrix([[0, -I], [I, 0]])
Z = Matrix([[1, 0], [0, -1]])
H = Matrix([[1, 1], [1, -1]]) / sqrt(2)
T = Matrix([[1, 0], [0, exp(I*pi/4)]])
S = Matrix([[1, 0], [0, exp(I*pi/2)]])
x, y, z = symbols('x, y, z')
RX = Matrix([[cos(x / 2), -I * sin(x / 2)], [-I * sin(x / 2), cos(x / 2)]])
RY = Matrix([[cos(y / 2), -sin(y / 2)], [sin(y / 2), cos(y / 2)]])
RZ = Matrix([[exp(-I * z / 2), 0], [0, exp(I * z / 2)]])
actual_1 = Circuit().x[0, 1].y[1].z[2].run(backend="sympy_unitary")
expected_1 = reduce(TensorProduct, [Z, Y * X, X])
assert actual_1 == expected_1
actual_2 = Circuit().y[0].z[3].run(backend="sympy_unitary")
expected_2 = reduce(TensorProduct, [Z, E, E, Y])
assert actual_2 == expected_2
actual_3 = Circuit().x[0].z[3].h[:].t[1].s[2].run(backend="sympy_unitary")
expected_3 = reduce(TensorProduct, [H * Z, S * H, T * H, H * X])
assert actual_3 == expected_3
actual_4 = Circuit().rx(-pi / 2)[0].rz(pi / 2)[1].ry(pi)[2].run(backend="sympy_unitary")
expected_4 = reduce(TensorProduct, [RY, RZ, RX]).subs([[x, -pi / 2], [y, pi], [z, pi / 2]])
assert actual_4 == expected_4
示例11: test_cu1
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import eye [as 别名]
def test_cu1():
E = eye(2)
UPPER = Matrix([[1, 0], [0, 0]])
LOWER = Matrix([[0, 0], [0, 1]])
lambd = symbols("lambd")
U = Circuit().rz(lambd)[0].run_with_sympy_unitary()
U /= U[0, 0]
actual_1 = Circuit().cu1(lambd)[0, 1].run(backend="sympy_unitary")
actual_1 /= actual_1[0, 0]
expected_1 = reduce(TensorProduct, [UPPER, E]) + reduce(TensorProduct, [LOWER, U])
assert simplify(actual_1 - expected_1) == zeros(4)
示例12: test_cswapgate
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import eye [as 别名]
def test_cswapgate():
expected = eye(8)
expected[4:, 4:] = Circuit().swap[1, 0].to_unitary()
assert simplify(Circuit().cswap[2, 1, 0].to_unitary() - expected) == zeros(8)
示例13: lazy_import
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import eye [as 别名]
def lazy_import():
global eye, symbols, sin, cos, exp, sqrt, pi, I, Matrix, sympy_gate, TensorProduct, sympy
from sympy import eye, symbols, sin, cos, exp, sqrt, pi, I, Matrix
from sympy.physics.quantum import TensorProduct
import sympy
示例14: __init__
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import eye [as 别名]
def __init__(self, n_qubits, ignore_global):
self.n_qubits = n_qubits
self.matrix_of_circuit = eye(2 ** n_qubits)
self.ignore_global = ignore_global
示例15: test_sympy_backend_for_two_qubit_gate
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import eye [as 别名]
def test_sympy_backend_for_two_qubit_gate():
E = eye(2)
UPPER = Matrix([[1, 0], [0, 0]])
LOWER = Matrix([[0, 0], [0, 1]])
X = Matrix([[0, 1], [1, 0]])
Z = Matrix([[1, 0], [0, -1]])
H = Matrix([[1, 1], [1, -1]]) / sqrt(2)
H_3 = reduce(TensorProduct, [H, E, H])
H_4 = reduce(TensorProduct, [H, E, E, H])
CX_3 = reduce(TensorProduct, [E, E, UPPER]) + reduce(TensorProduct, [X, E, LOWER])
CZ_3 = reduce(TensorProduct, [E, E, UPPER]) + reduce(TensorProduct, [Z, E, LOWER])
CX_4 = reduce(TensorProduct, [E, E, E, UPPER]) + reduce(TensorProduct, [X, E, E, LOWER])
CZ_4 = reduce(TensorProduct, [E, E, E, UPPER]) + reduce(TensorProduct, [Z, E, E, LOWER])
actual_1 = Circuit().cx[0, 3].run(backend="sympy_unitary")
assert actual_1 == CX_4
actual_2 = Circuit().cx[1, 3].x[4].run(backend="sympy_unitary")
expected_2 = reduce(TensorProduct, [X, CX_3, E])
assert actual_2 == expected_2
actual_3 = Circuit().cz[0, 3].run(backend="sympy_unitary")
assert actual_3 == CZ_4
actual_4 = Circuit().cz[1, 3].x[4].run(backend="sympy_unitary")
expected_4 = reduce(TensorProduct, [X, CZ_3, E])
assert actual_4 == expected_4
actual_5 = Circuit().cx[3, 0].run(backend="sympy_unitary")
assert actual_5 == H_4 * CX_4 * H_4
actual_6 = Circuit().cx[3, 1].x[4].run(backend="sympy_unitary")
assert actual_6 == reduce(TensorProduct, [X, H_3 * CX_3 * H_3, E])
actual_7 = Circuit().cz[3, 0].run(backend="sympy_unitary")
assert actual_7 == CZ_4
actual_8 = Circuit().cz[3, 1].x[4].run(backend="sympy_unitary")
assert actual_8 == reduce(TensorProduct, [X, CZ_3, E])
x, y, z = symbols('x, y, z')
RX = Matrix([[cos(x / 2), -I * sin(x / 2)], [-I * sin(x / 2), cos(x / 2)]])
RY = Matrix([[cos(y / 2), -sin(y / 2)], [sin(y / 2), cos(y / 2)]])
RZ = Matrix([[exp(-I * z / 2), 0], [0, exp(I * z / 2)]])
CRX_3 = reduce(TensorProduct, [UPPER, E, E]) + reduce(TensorProduct, [LOWER, E, RX])
CRY_4 = reduce(TensorProduct, [E, UPPER, E, E]) + reduce(TensorProduct, [E, LOWER, RY, E])
CRZ_3 = reduce(TensorProduct, [E, E, UPPER]) + reduce(TensorProduct, [RZ, E, LOWER])
actual_9 = Circuit().crx(x)[2, 0].run(backend="sympy_unitary")
assert simplify(actual_9) == CRX_3
actual_10 = Circuit().cry(y)[2, 1].i[3].run(backend="sympy_unitary")
assert simplify(actual_10) == CRY_4
actual_11 = Circuit().crz(z)[0, 2].run(backend="sympy_unitary")
assert simplify(actual_11) == CRZ_3