<jupyter_start><jupyter_text><jupyter_code>import numpy as np

def HS(M1, M2):
    """Hilbert-Schmidt-Product of two matrices M1, M2"""
    return (np.dot(M1.conjugate().transpose(), M2)).trace()

def c2s(c):
    """Return a string representation of a complex number c"""
    if c == 0.0:
        return "0"
    if c.imag == 0:
        return "%g" % c.real
    elif c.real == 0:
        return "%gj" % c.imag
    else:
        return "%g+%gj" % (c.real, c.imag)

def decompose(H):
    """Decompose 4x4 matrix H into Pauli matrices"""
    from numpy import kron
    sx = np.array([[0, 1],  [ 1, 0]], dtype=np.complex128)
    sy = np.array([[0, -1j],[1j, 0]], dtype=np.complex128)
    sz = np.array([[1, 0],  [0, -1]], dtype=np.complex128)
    id = np.array([[1, 0],  [ 0, 1]], dtype=np.complex128)
    S = [id, sx, sy, sz]
    labels = ['I', 'sigma_x', 'sigma_y', 'sigma_z']
    for i in range(4):
        for j in range(4):
            label = labels[i] + ' times ' + labels[j]
            a_ij = 0.25 * HS(kron(S[i], S[j]), H)
            if a_ij != 0.0:
                print("%s\t*\t( %s )" % (c2s(a_ij), label))
H = np.array([[ 1.5,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12],
       [13, 14, 15, 16]], dtype=np.complex128)
decompose(H)<jupyter_output>8.5	*	( I times I )
8.5	*	( I times sigma_x )
-1.5j	*	( I times sigma_y )
-2.5	*	( I times sigma_z )
8.5	*	( sigma_x times I )
8.5	*	( sigma_x times sigma_x )
-1.5j	*	( sigma_x times sigma_y )
-2.5	*	( sigma_x times sigma_z )
-3j	*	( sigma_y times I )
-3j	*	( sigma_y times sigma_x )
-5	*	( sigma_z times I )
-5	*	( sigma_z times sigma_x )
{'fraction_non_alphanumeric': 0.18920557913887204, 'fraction_numerical': 0.058823529411764705, 'mean_word_length': 2.179190751445087, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '130417', 'license': 'Apache-2.0', 'n_tokens_mistral': 787, 'n_tokens_neox': 705, 'n_words': 186, 'path': 'Unitary.ipynb', 'repo_name': 'sid8123/qml', 'size': '1649'}