Basic Linear Algebra (quanguru.QuantumToolbox.linearAlgebra
)#
Contains some basic linear algebra methods for scipy.sparse and np.ndarray types.
Functions#
|
Hermitian conjugate \(M^{\dagger} := (M^{*})^{T}\) of a matrix \(M\), where * is complex conjugation, and T is transposition. |
|
Computes the inner product \(\langle ket2 | ket1 \rangle\) of a ket vector with itself or with another, where \(\langle ket2| := |ket2 \rangle^{\dagger}\). |
|
Norm \(\sqrt{\langle ket | ket \rangle}\) of a ket state \(|ket \rangle\), where \(\langle ket| := |ket \rangle^{\dagger}\). |
|
Computes the outer product \(|ket2 \rangle\langle ket1|\) of a ket vector with itself or with another, where \(\langle ket2| := |ket2 \rangle^{\dagger}\). |
|
Function to calculate tensor product \(\otimes_{i} M_{i}\) of given (any number i of) states (\(\{M_{i}\}\) in the given order). |
|
Trace \(Tr(M) := \sum_{i} M_{ii}\) of a matrix M. |
|
Calculates the partial trace of a density matrix of composite state. |
|
Calculates the matrix multiplication of the given arbitrary number of inputs in the given order. |
|
A recursive function to raise the given matrix to a power, ie for given matrix \(M\) and power \(n\), this returns \(\overbrace{M @ M @ \cdots @ M}^{n times}\), where \(@\) is the matrix multiplication. |
Function Name |
Docstrings |
Examples |
Unit Tests |
Tutorials |
---|---|---|---|---|
hc |
✅ |
✅ |
✅ |
❌ |
innerProd |
✅ |
✅ |
✅ |
❌ |
norm |
✅ |
✅ |
✅ |
❌ |
outerProd |
✅ |
✅ |
✅ |
❌ |
tensorProd |
✅ |
✅ |
✅ |
❌ |
trace |
✅ |
✅ |
✅ |
❌ |
partialTrace |
✅ |
✅ |
✅ |
❌ |
_matMulInputs |
✅ |
✅ |
✅ |
❌ |
_matPower |
✅ |
✅ |
✅ |
❌ |
- hc(matrix: spmatrix | ndarray) spmatrix | ndarray [source]#
Hermitian conjugate \(M^{\dagger} := (M^{*})^{T}\) of a matrix \(M\), where * is complex conjugation, and T is transposition.
- Parameters:
matrix (Matrix) – a matrix
- Returns:
Hermitian conjugate of the given matrix
- Return type:
Matrix
Examples
>>> operEx1 = np.array([[1+1j, 2+2j], >>> [3+3j, 4+4j]]) >>> hc(operEx1) array([[1.-1.j, 3.-3.j], [2.-2.j, 4.-4.j]])
>>> operEx2 = np.array([[0, 1], >>> [1, 0]]) >>> hc(operEx2) array([[0, 1], [1, 0]])
>>> operEx3 = np.array([[1, 0, 0], >>> [0, 1, 0], >>> [1j, 0, 1]]) >>> hc(operEx3) array([[1, 0, -1j], [0, 1, 0], [0, 0, 1]])
- innerProd(ket1: spmatrix | ndarray, ket2: spmatrix | ndarray = None) float [source]#
Computes the inner product \(\langle ket2 | ket1 \rangle\) of a ket vector with itself or with another, where \(\langle ket2| := |ket2 \rangle^{\dagger}\).
- Parameters:
ket1 (Matrix) – 1st ket state
ket2 (Matrix) – 2nd ket state
- Returns:
inner product
- Return type:
float
Examples
>>> cMatEx1 = np.array([[1], >>> [0]]) >>> innerProd(cMatEx1) 1
>>> cMatEx2 = np.array([[0], >>> [1]]) >>> innerProd(cMatEx2) 1
>>> cMatEx3 = (1/5)*np.array([[3], >>> [4j]]) >>> innerProd(cMatEx3) 1
>>> cMatEx4 = np.array([[1], >>> [1j]]) >>> innerProd(cMatEx4) 2
- norm(ket: spmatrix | ndarray) float [source]#
Norm \(\sqrt{\langle ket | ket \rangle}\) of a ket state \(|ket \rangle\), where \(\langle ket| := |ket \rangle^{\dagger}\). This function simply returns the square root of
innerProd
- Parameters:
ket (Matrix) – a ket state
- Returns:
norm of the state
- Return type:
float
Examples
>>> cMatEx1 = np.array([[1], >>> [0]]) >>> norm(cMatEx1) 1
>>> cMatEx2 = np.array([[3], >>> [4j]]) >>> norm(cMatEx2) 5+0j
>>> cMatEx3 = np.array([[1], >>> [1j]]) >>> norm(cMatEx3) 1.4142135623730951+0j
- outerProd(ket1: spmatrix | ndarray, ket2: spmatrix | ndarray = None) spmatrix | ndarray [source]#
Computes the outer product \(|ket2 \rangle\langle ket1|\) of a ket vector with itself or with another, where \(\langle ket2| := |ket2 \rangle^{\dagger}\).
- Parameters:
ket1 (Matrix) – 1st ket state
ket2 (Matrix) – 2nd ket state
- Returns:
operator in square matrix form resulting from the computed outer product
- Return type:
Matrix
Examples
>>> cMatEx1 = np.array([[1], >>> [0]]) >>> outerProd(cMatEx1) array([[1, 0], [0, 0]])
>>> cMatEx2 = np.array([[0], >>> [1]]) >>> outerProd(cMatEx2) array([[0, 0], [0, 1]])
>>> cMatEx3 = (1/5)*np.array([[3], >>> [4j]]) >>> outerProd(cMatEx3) array([[0.36+0.j , 0. -0.48j], [0. +0.48j, 0.64+0.j ]])
>>> cMatEx4 = np.array([[1], >>> [1j]]) >>> outerProd(cMatEx4) array([[1+0.j , 0. -1j], [0. +1j, 1+0.j ]])
- tensorProd(*args: spmatrix | ndarray) spmatrix | ndarray [source]#
Function to calculate tensor product \(\otimes_{i} M_{i}\) of given (any number i of) states (\(\{M_{i}\}\) in the given order).
- Parameters:
*args (Matrix) – state matrices (arbitrary number of them)
- Returns:
tensor product of given states (in the given order)
- Return type:
Matrix
Examples
>>> cMatEx1 = np.array([[1], >>> [0]]) >>> cMatEx2 = np.array([[0], >>> [1]]) >>> tensorProd(cMatEx1, cMatEx2) array([[0], [1], [0], [0]], dtype=int64) >>> tensorProd(cMatEx2, cMatEx1) array([[0], [0], [1], [0]], dtype=int64)
>>> operEx1 = np.array([[0, 1], >>> [1, 0]]) >>> operEx2 = np.array([[1, 0, 0], >>> [0, 1, 0], >>> [1j, 0, 1]]) >>> tensorProd(operEx1, operEx2) array([[0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0]], dtype=int64) >>> tensorProd(operEx2, operEx1) array([[0, 1, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 1, 0]], dtype=int64)
- trace(matrix: spmatrix | ndarray) float [source]#
Trace \(Tr(M) := \sum_{i} M_{ii}\) of a matrix M.
- Parameters:
matrix (Matrix) – a matrix
- Returns:
trace of the given matrix
- Return type:
float
Examples
>>> cMatEx1 = np.array([[1], >>> [0]]) >>> trace(outerProd(cMatEx1)) 1
>>> cMatEx2 = np.array([[0], >>> [1]]) >>> trace(outerProd(cMatEx2)) 1
- partialTrace(keep: ndarray | List[int], dims: ndarray | List[int], state: spmatrix | ndarray) ndarray [source]#
Calculates the partial trace of a density matrix of composite state.
- Parameters:
keep (ndOrListInt) – An array of indices of the spaces to keep after being traced. For instance, if the space is A x B x C x D and we want to trace out B and D, keep = [0,2]
dims (ndOrListInt) – An array of the dimensions of each space. For instance, if the space is A x B x C x D, dims = [dim_A, dim_B, dim_C, dim_D]
state (Matrix) – Matrix to trace
- Returns:
Traced matrix
- Return type:
Matrix
Examples
>>> cMatEx1 = np.array([[1], >>> [0]]) >>> cMatEx2 = np.array([[0], >>> [1]]) >>> partialTrace([0], [2, 2], outerProd(tensorProd(cMatEx1, cMatEx2))) array([[1, 0], [0, 0]], dtype=int64) >>> partialTrace([1], [2, 2], outerProd(tensorProd(cMatEx1, cMatEx2))) array([[0, 0], [0, 1]], dtype=int64) >>> partialTrace([0], [2, 2], outerProd(tensorProd(cMatEx2, cMatEx1))) array([[0, 0], [0, 1]], dtype=int64) >>> partialTrace([1], [2, 2], outerProd(tensorProd(cMatEx2, cMatEx1))) array([[1, 0], [0, 0]], dtype=int64)
- _matMulInputs(*args: spmatrix | ndarray | List[spmatrix | ndarray]) spmatrix | ndarray [source]#
Calculates the matrix multiplication of the given arbitrary number of inputs in the given order. It does not check the correctness of the shapes until the matrix multiplication operator (@) itself gives an error.
- Parameters:
*args (matrixOrMatrixList)
- Returns:
Given Matrices multiplied
- Return type:
Matrix
Examples
>>> cMatEx1 = np.array([[1, 1], >>> [1, 1]]) >>> cMatEx2 = np.array([[1, 0], >>> [0, 0]]) >>> qg.linearAlgebra._matMulInputs(cMatEx1,cMatEx2) array([[1, 0], [1, 0]])
>>> cMatEx1 = np.array([[1, 1], >>> [1, 1]]) >>> cMatEx2 = np.array([[1, 0], >>> [0, 0]]) >>> cMatEx3 = np.array([[1, 1], >>> [0, 0]]) >>> qg.linearAlgebra._matMulInputs(cMatEx1,cMatEx2,cMatEx3) array([[1, 1], [1, 1]])
- _matPower(matrix: spmatrix | ndarray, power: int) spmatrix | ndarray [source]#
A recursive function to raise the given matrix to a power, ie for given matrix \(M\) and power \(n\), this returns \(\overbrace{M @ M @ \cdots @ M}^{n times}\), where \(@\) is the matrix multiplication.
- Parameters:
matrix (Matrix) – The matrix to raise to a power
power (int) – Power to raise
- Returns:
Given matrix raised to given power
- Return type:
Matrix
Examples
>>> cMatEx1 = np.array([[1, 1], >>> [1, 1]]) >>> qg.linearAlgebra._matMulInputs(cMatEx1, 2) array([[2, 2], [2, 2]])
>>> cMatEx1 = np.array([[1, 1], >>> [1, 1]]) >>> qg.linearAlgebra._matMulInputs(cMatEx1, 5) array([[16, 16], [16, 16]])
>>> cMatEx2 = np.array([[1, 1], >>> [0, 0]]) >>> qg.linearAlgebra._matMulInputs(cMatEx1, 3) array([[1, 1], [0, 0]])