Python sympy.eye() Examples

The following are 15 code examples of sympy.eye(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sympy , or try the search function .
Example #1
Source File: noise_transformation.py    From qiskit-aer with Apache License 2.0 6 votes vote down vote up
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 
Example #2
Source File: test_sympy.py    From Blueqat with Apache License 2.0 6 votes vote down vote up
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) 
Example #3
Source File: test_sympy.py    From Blueqat with Apache License 2.0 6 votes vote down vote up
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 
Example #4
Source File: optimize_crappy.py    From twitchslam with MIT License 5 votes vote down vote up
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 
Example #5
Source File: geometry.py    From ikpy with GNU General Public License v2.0 5 votes vote down vote up
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 
Example #6
Source File: geometry.py    From ikpy with GNU General Public License v2.0 5 votes vote down vote up
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 
Example #7
Source File: utils.py    From karonte with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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 
Example #8
Source File: test_dagger.py    From Blueqat with Apache License 2.0 5 votes vote down vote up
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) 
Example #9
Source File: test_dagger.py    From Blueqat with Apache License 2.0 5 votes vote down vote up
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) 
Example #10
Source File: test_sympy.py    From Blueqat with Apache License 2.0 5 votes vote down vote up
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 
Example #11
Source File: test_sympy.py    From Blueqat with Apache License 2.0 5 votes vote down vote up
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) 
Example #12
Source File: test_sympy.py    From Blueqat with Apache License 2.0 5 votes vote down vote up
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) 
Example #13
Source File: sympy_backend.py    From Blueqat with Apache License 2.0 5 votes vote down vote up
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 
Example #14
Source File: sympy_backend.py    From Blueqat with Apache License 2.0 5 votes vote down vote up
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 
Example #15
Source File: test_sympy.py    From Blueqat with Apache License 2.0 4 votes vote down vote up
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