Skip to content

algebra

qoptcraft.basis.algebra

algebra_basis(modes, photons)

Generate the basis for the algebra and image algebra.

Source code in qoptcraft/basis/algebra.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def algebra_basis(modes: int, photons: int) -> tuple[BasisAlgebra, BasisAlgebra]:
    """Generate the basis for the algebra and image algebra."""
    basis = []
    basis_image = []
    photon_basis = get_photon_basis(modes, photons)

    for mode_1 in range(modes):
        for mode_2 in range(mode_1 + 1):
            matrix = sym_matrix(mode_1, mode_2, modes)
            basis.append(matrix)
            basis_image.append(image_sym_matrix(mode_1, mode_2, photon_basis))

    # Divide into two loops to separate symmetric from antisymmetric matrices
    for mode_1 in range(modes):
        for mode_2 in range(mode_1):
            matrix = antisym_matrix(mode_1, mode_2, modes)
            basis.append(matrix)
            basis_image.append(image_antisym_matrix(mode_1, mode_2, photon_basis))

    return basis, basis_image

antisym_matrix(mode_1, mode_2, dim)

Create the element of the algebra 1/2(|j><j|).

Source code in qoptcraft/basis/algebra.py
89
90
91
92
93
94
def antisym_matrix(mode_1: int, mode_2: int, dim: int) -> spmatrix:
    """Create the element of the algebra 1/2(|j><k| - |k><j|)."""
    matrix = np.zeros((dim, dim), dtype=np.complex128)
    matrix[mode_1, mode_2] = 0.5
    matrix[mode_2, mode_1] = -0.5
    return matrix

get_algebra_basis(modes, photons)

Return a basis for the Hilbert space with n photons and m modes. If the basis was saved retrieve it, otherwise the function creates and saves the basis to a file.

Parameters:

Name Type Description Default
photons int

number of photons.

required
modes int

number of modes.

required

Returns:

Type Description
tuple[BasisAlgebra, BasisAlgebra]

BasisAlgebra, BasisAlgebra: basis of the algebra and the image algebra.

Source code in qoptcraft/basis/algebra.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def get_algebra_basis(modes: int, photons: int) -> tuple[BasisAlgebra, BasisAlgebra]:
    """Return a basis for the Hilbert space with n photons and m modes.
    If the basis was saved retrieve it, otherwise the function creates
    and saves the basis to a file.

    Args:
        photons (int): number of photons.
        modes (int): number of modes.

    Returns:
        BasisAlgebra, BasisAlgebra: basis of the algebra and the image algebra.
    """
    folder_path = config.SAVE_DATA_PATH / f"m={modes} n={photons}"
    folder_path.mkdir(parents=True, exist_ok=True)

    basis_path = folder_path / "algebra.pkl"
    basis_image_path = folder_path / "image_algebra.pkl"
    basis_path.touch()  # create file if it doesn't exist
    basis_image_path.touch()
    try:
        with basis_path.open("rb") as f:
            basis = pickle.load(f)
        with basis_image_path.open("rb") as f:
            basis_image = pickle.load(f)

    except EOFError:
        basis, basis_image = algebra_basis(modes, photons)
        with basis_path.open("wb") as f:
            pickle.dump(basis, f)
        with basis_image_path.open("wb") as f:
            pickle.dump(basis_image, f)
        print(f"Algebra basis saved in {folder_path}")

    return basis, basis_image

image_antisym_matrix(mode_1, mode_2, photon_basis)

Image of the antisymmetric basis matrix by the lie algebra homomorphism.

Source code in qoptcraft/basis/algebra.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
def image_antisym_matrix(mode_1: int, mode_2: int, photon_basis: BasisPhoton) -> spmatrix:
    """Image of the antisymmetric basis matrix by the lie algebra homomorphism."""
    dim = len(photon_basis)
    matrix = lil_matrix((dim, dim), dtype=np.complex128)

    for col, fock_ in enumerate(photon_basis):
        if fock_[mode_1] != 0:
            fock, coef = annihilation(mode_1, fock_)
            fock, coef_ = creation(mode_2, fock)
            matrix[photon_basis.index(fock), col] = -0.5 * coef * coef_

        if fock_[mode_2] != 0:
            fock, coef = annihilation(mode_2, fock_)
            fock, coef_ = creation(mode_1, fock)
            matrix[photon_basis.index(fock), col] += 0.5 * coef * coef_

    return matrix.tocsr()

image_sym_matrix(mode_1, mode_2, photon_basis)

Image of the symmetric basis matrix by the lie algebra homomorphism.

Source code in qoptcraft/basis/algebra.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
def image_sym_matrix(mode_1: int, mode_2: int, photon_basis: BasisPhoton) -> spmatrix:
    """Image of the symmetric basis matrix by the lie algebra homomorphism."""
    dim = len(photon_basis)
    matrix = lil_matrix((dim, dim), dtype=np.complex128)  # * efficient format for loading data

    for col, fock_ in enumerate(photon_basis):
        if fock_[mode_1] != 0:
            fock, coef = annihilation(mode_1, fock_)
            fock, coef_ = creation(mode_2, fock)
            matrix[photon_basis.index(fock), col] = 0.5j * coef * coef_

        if fock_[mode_2] != 0:
            fock, coef = annihilation(mode_2, fock_)
            fock, coef_ = creation(mode_1, fock)
            matrix[photon_basis.index(fock), col] += 0.5j * coef * coef_

    return matrix.tocsr()

sym_matrix(mode_1, mode_2, dim)

Create the element of the algebra i/2(|j><j|).

Source code in qoptcraft/basis/algebra.py
81
82
83
84
85
86
def sym_matrix(mode_1: int, mode_2: int, dim: int) -> spmatrix:
    """Create the element of the algebra i/2(|j><k| + |k><j|)."""
    matrix = np.zeros((dim, dim), dtype=np.complex128)
    matrix[mode_1, mode_2] = 0.5j
    matrix[mode_2, mode_1] += 0.5j
    return matrix