qcp.matrices package¶
Submodules¶
qcp.matrices.dense_matrix module¶
- class qcp.matrices.dense_matrix.DenseMatrix(state: qcp.matrices.types.MATRIX)[source]¶
Bases:
qcp.matrices.matrix.Matrix
Implementation of a Dense Matrix class, where each matrix entry is stored in memory, including entries of value zero.
- __add__(other: qcp.matrices.matrix.Matrix) qcp.matrices.matrix.Matrix [source]¶
Add the matching elements of the other matrix to the entries in this matrix, and return a new matrix containing the resultant values.
- __getitem__(i: int) qcp.matrices.types.VECTOR [source]¶
Get the List representation of the row of index i.
- Parameters
i (int) – The row index to get.
- Returns
List representation of the row.
- Return type
- __init__(state: qcp.matrices.types.MATRIX)[source]¶
Initialise the dense matrix, using the given nested list as our matrix content
- Parameters
state (MATRIX) – A nested list containing values for each entry in the matrix.
- __len__() int [source]¶
Return the horizontal size of the DenseMatrix.
- Returns
The number of columns in the DenseMatrix
- Return type
int
- __mul__(other: Union[SCALARS, Matrix]) Matrix [source]¶
Override of the behaviour of the * multiplication operator.
If the operator is applied using a scalar value to this matrix, the matrix elements are each scaled by this number and a new matrix is returned containing these scaled elements.
If a
Matrix
object is applied using the * operator, the dot product of the two matrices is calculated, with the current matrix being the first Matrix, and the applied one the second, as order matters for matrix multiplication.
- __setitem__(i: int, v: qcp.matrices.types.VECTOR)[source]¶
Set the given row inplace to the new row values in the given list.
- Parameters
i (int) – The row index to modify
v (VECTOR) – The list of values to set the row to
- __str__() str [source]¶
Create a string representation of the Matrix, with rows separated by newlines, and bounded by ‘[‘/’]’ characters.
- Returns
A string representation of the matrix suitable for printing.
- Return type
str
- __sub__(other: qcp.matrices.matrix.Matrix) qcp.matrices.matrix.Matrix [source]¶
Subtract the matching elements of the other matrix to the entries in this matrix, and return a new matrix containing the resultant values.
- _abc_impl = <_abc._abc_data object>¶
- _dot(other: qcp.matrices.matrix.Matrix) qcp.matrices.matrix.Matrix [source]¶
Calculate the dot product between this Matrix, and another Matrix.
- columns() qcp.matrices.types.MATRIX [source]¶
The transpose of the matrix as a nested list
- Returns
A nested list of the matrix values transposed, indexed by column/row.
- Return type
- conjugate() qcp.matrices.dense_matrix.DenseMatrix [source]¶
Create a new
qcp.matrices.dense_matrix.DenseMatrix
where each value in the matrix is the complex conjugate of the current matrix values.- Returns
A DenseMatrix object of the same dimensions of the current matrix, with each value conjugated in place.
- Return type
- get_state() qcp.matrices.types.MATRIX [source]¶
Return the matrix values as a nested list
- Returns
A nested list of the matrix values indexed by row/column
- Return type
- static identity(n: int) qcp.matrices.dense_matrix.DenseMatrix [source]¶
Create the identity matrix with the given dimensions
- Parameters
n (int) – The matrix dimension
- Returns
The identity matrix of given dimension
- Return type
- property num_columns: int¶
Return the number of columns in the DenseMatrix.
- Returns
The number of columns.
- Return type
int
- property num_rows: int¶
Return the number of rows in the DenseMatrix.
- Returns
The number of rows
- Return type
int
- rows() qcp.matrices.types.MATRIX [source]¶
Equivalent to
qcp.matrices.dense_matrix.DenseMatrix.get_state()
.- Returns
A nested list of the matrix values indexed by row/column
- Return type
- trace() qcp.matrices.types.SCALARS [source]¶
Calculate the sum of the diagonal elements of the matrix
- Returns
The sum of all diagonal elements, with type determined by the value types.
- Return type
- transpose() qcp.matrices.dense_matrix.DenseMatrix [source]¶
Flips the matrix elements along the diagonal, and return a new
qcp.matrices.dense_matrix.DenseMatrix
containing these values.- Returns
The transpose of the current matrix.
- Return type
- property unitary: bool¶
Check if matrix is Unitary (can be shifted to gates.py)
- Parameters
Matrix – input: n x n matrix
- Returns
Whether the matrix is unitary
- Return type
bool
- static zeros(nrow: int, ncol: int = 1) qcp.matrices.dense_matrix.DenseMatrix [source]¶
Create a DenseMatrix of given dimensions, where each value of the matrix is zero.
- Parameters
nrow (int) – The row dimension of the DenseMatrix
ncol (int) – The (optional) column dimenion of the DenseMatrix defaults to 1, to be a column vector.
- Returns
The matrix object of our given size.
- Return type
qcp.matrices.matrix module¶
- class qcp.matrices.matrix.Matrix(state: qcp.matrices.types.MATRIX)[source]¶
Bases:
abc.ABC
Method stubs for an implementation of a matrix.
- __add__(other: qcp.matrices.matrix.Matrix) qcp.matrices.matrix.Matrix [source]¶
Add the matching elements of the other matrix to the entries in this matrix, and return a new matrix containing the resultant values.
- __getitem__(i: int) qcp.matrices.types.VECTOR [source]¶
Return the row elements for the given row index in a list representation
- Parameters
i (int) – The index of the row to get the elements of
- Returns
A list containing the row elements
- Return type
- __init__(state: qcp.matrices.types.MATRIX)[source]¶
Initialise the Matrix object, setting the matrix elements based off of the given nested list, and inferring the matrix dimensions from the list dimensions
- Parameters
state (MATRIX) – The nested list containing the matrix elements
- __len__() int [source]¶
Return the horizontal width of the matrix.
- Returns
The width of the matrix
- Return type
int
- __mul__(other: Union[SCALARS, Matrix]) Matrix [source]¶
Override of the behaviour of the * multiplication operator.
If the operator is applied using a scalar value to this matrix, the matrix elements are each scaled by this number and a new matrix is returned containing these scaled elements.
If a
Matrix
object is applied using the * operator, the dot product of the two matrices is calculated, with the current matrix being the first Matrix, and the applied one the second, as order matters for matrix multiplication.
- __rmul__(s: qcp.matrices.types.SCALARS) qcp.matrices.matrix.Matrix [source]¶
Shim layer for the
__mul__()
operation, so that we can pre-mulitply matrices with a scalar and get the expected behaviour.- Parameters
other (SCALARS) – The scalar to multiply against the matrix
- Returns
A new matrix where the elements are scaled by other
- Return type
- __setitem__(i: int, v: qcp.matrices.types.VECTOR)[source]¶
Set the given row in the matrix to the given elements in the list
- Parameters
i (int) – The index of the row to modify
v (VECTOR) – The new elements as a list to set the row to.
- __str__() str [source]¶
Create a string representation of the Matrix, with rows separated by newlines, and bounded by ‘[‘/’]’ characters.
- Returns
A string representation of the matrix suitable for printing.
- Return type
str
- __sub__(other: qcp.matrices.matrix.Matrix) qcp.matrices.matrix.Matrix [source]¶
Subtract the matching elements of the other matrix to the entries in this matrix, and return a new matrix containing the resultant values.
- _abc_impl = <_abc._abc_data object>¶
- _optional_newline(i: int, N: int) str [source]¶
Helper function for
__str__()
to determine which rows require a newline character appended to them.- Parameters
i (int) – The current row index under consideration.
N (int) – The total number of rows.
- Returns
A newline character, or empty string depending on the condition.
- Return type
str
- adjoint() qcp.matrices.matrix.Matrix [source]¶
Shortcut operation to calculate the transpose and conjugate of the current matrix.
- Returns
The new matrix that is transposed and then conjugated of the current matrix.
- Return type
- columns() qcp.matrices.types.MATRIX [source]¶
Return the transpose of the matrix as a nested list
- Returns
The nested list of matrix elements, indexed by column/row.
- Return type
- conjugate() qcp.matrices.matrix.Matrix [source]¶
Calculate the complex conjugate of each matrix element and return a new matrix of these conjugated elements
- Returns
The matrix with each element conjugates of the current
qcp.matrices.matrix.Matrix
- Return type
- get_state() qcp.matrices.types.MATRIX [source]¶
Returns the matrix values as a nested list, indexed by the row/column indices.
- Returns
A nested list of the matrix values
- Return type
- property num_columns: int¶
The number of columns in the Matrix
- Returns
The number of columns in the matrix
- Return type
int
- property num_rows: int¶
The number of rows in the Matrix
- Returns
The number of rows in the matrix
- Return type
int
- property square: bool¶
Whether the matrix is square or not.
- Returns
Whether the matrix row/column dimensions match.
- Return type
bool
- trace() qcp.matrices.types.SCALARS [source]¶
The sum of the diagonal elements of a square matrix
- Returns
The sum of the diagonal of the matrix
- Return type
- transpose() qcp.matrices.matrix.Matrix [source]¶
Mirror a square matrix along it’s diagonal, and return a new matrix of these elements
- Returns
The transpose of the current matrix.
- Return type
- property unitary: bool¶
Check if matrix is Unitary (can be shifted to gates.py)
- Parameters
Matrix – input: n x n matrix
- Returns
Whether the matrix is unitary
- Return type
bool
qcp.matrices.sparse_matrix module¶
- class qcp.matrices.sparse_matrix.SparseMatrix(state: Union[MATRIX, SPARSE], w: int = - 1, h: int = - 1)[source]¶
Bases:
qcp.matrices.matrix.Matrix
Implementation of a Sparse Matrix object, where only the non-zero matrix elements are stored in memory, and if a matrix element is not indexed, it is taken to be zero.
- __add__(other: qcp.matrices.matrix.Matrix) qcp.matrices.matrix.Matrix [source]¶
Add the matching elements of the other matrix to the entries in this matrix, and return a new matrix containing the resultant values.
- __getitem__(i: int) qcp.matrices.sparse_matrix.SparseVector [source]¶
Return the row elements for the given row index in a list representation
- Parameters
i (int) – The index of the row to get the elements of
- Returns
A list containing the row elements
- Return type
- __init__(state: Union[MATRIX, SPARSE], w: int = - 1, h: int = - 1)[source]¶
Initialise a SparseMatrix, using either a List[List[]] object, or a pre-indexed dictionary mapping indices to non-zero values.
- Parameters
state (Union[MATRIX, SPARSE]) – object containing the matrix elements, either by determining the row/column indices from the list, or using the given row/column index mapping.
w (int) – Optional overload of the Matrix width dimension
h (int) – Optional overload of the Matrix height dimension
- __len__() int [source]¶
Return the horizontal size of the SparseMatrix.
- Returns
The number of columns in the SparseMatrix
- Return type
int
- __mul__(other: Union[SCALARS, Matrix]) Matrix [source]¶
Override of the behaviour of the * multiplication operator.
If the operator is applied using a scalar value to this matrix, the matrix elements are each scaled by this number and a new matrix is returned containing these scaled elements.
If a
Matrix
object is applied using the * operator, the dot product of the two matrices is calculated, with the current matrix being the first Matrix, and the applied one the second, as order matters for matrix multiplication.
- __setitem__(i: int, v: Union[SparseVector, List[SCALARS], Dict[int, SCALARS]])[source]¶
Set the given row in the matrix to the given elements in the list
- Parameters
i (int) – The index of the row to modify
v (VECTOR) – The new elements as a list to set the row to.
- __str__() str [source]¶
Create a string representation of the Matrix, with rows separated by newlines, and bounded by ‘[‘/’]’ characters.
- Returns
A string representation of the matrix suitable for printing.
- Return type
str
- __sub__(other: qcp.matrices.matrix.Matrix) qcp.matrices.matrix.Matrix [source]¶
Subtract the matching elements of the other matrix to the entries in this matrix, and return a new matrix containing the resultant values.
- _abc_impl = <_abc._abc_data object>¶
- _as_list() qcp.matrices.types.MATRIX [source]¶
Equivalent to
get_state()
- _dot(other: qcp.matrices.matrix.Matrix) qcp.matrices.matrix.Matrix [source]¶
Calculate the dot product between this Matrix, and another Matrix.
- _dot_sparse(other: qcp.matrices.sparse_matrix.SparseMatrix) qcp.matrices.sparse_matrix.SparseMatrix [source]¶
Optimisation of the
_dot()
method in the case where both Matrices are SparseMatrix objects, in which case we only need to consider the non-zero entries of the matrices- Parameters
other (SparseMatrix) – The matrix to dot product with this one.
- Returns
A new matrix that conforms to the rules of matrix dot producting.
- Return type
- _get_row(i: int) qcp.matrices.sparse_matrix.SparseVector [source]¶
Equivalent to
__getitem__()
- columns() qcp.matrices.types.MATRIX [source]¶
The transpose of the matrix as a nested list
- Returns
A nested list of the matrix values transposed, indexed by column/row
- Return type
- conjugate() qcp.matrices.sparse_matrix.SparseMatrix [source]¶
Create a new SparseMatrix where each value in the matrix is the complex conjugate of the current matrix values.
- Returns
A SparseMatrix object of the same dimensions of the current matrix, with each value conjugated in place.
- Return type
- get_state() qcp.matrices.types.MATRIX [source]¶
Return the matrix values as a nested list
- Returns
A nested list of the matrix values indexed by row/column
- Return type
- static identity(n: int) qcp.matrices.sparse_matrix.SparseMatrix [source]¶
Create the identity matrix with the given dimensions
- Parameters
n (int) – The matrix dimension
- Returns
The SparseMatrix identity matrix object.
- Return type
- property num_columns: int¶
Return the number of columns in the SparseMatrix.
- Returns
The number of columns.
- Return type
int
- property num_rows: int¶
Return the number of rows in the SparseMatrix.
- Returns
The number of rows
- Return type
int
- rows() qcp.matrices.types.MATRIX [source]¶
Equivalent to get_state().
- Returns
A nested list of the matrix values indexed by row/column
- Return type
- trace() qcp.matrices.types.SCALARS [source]¶
Calculate the sum of the diagonal elements of the matrix
- Returns
The sum of all diagonal elements, with type determined by the value types.
- Return type
- transpose() qcp.matrices.sparse_matrix.SparseMatrix [source]¶
Flips the matrix elements along the diagonal, and return a new SparseMatrix containing these values.
- Returns
The transpose of the current matrix.
- Return type
- property unitary: bool¶
Check if matrix is Unitary (can be shifted to gates.py)
- Parameters
Matrix – input: n x n matrix
- Returns
Whether the matrix is unitary
- Return type
bool
- static zeros(nrow: int, ncol: int = 1) qcp.matrices.sparse_matrix.SparseMatrix [source]¶
Create a SparseMatrix of given dimensions, where each value of the matrix is zero.
- Parameters
nrow (int) – The row dimension of the SparseMatrix
ncol (int) – The (optional) column dimenion of the SparseMatrix defaults to 1, to be a column vector.
- Returns
The matrix object of our given size.
- Return type
- class qcp.matrices.sparse_matrix.SparseVector(entries: Union[List[SCALARS], Dict[int, SCALARS]], size: int)[source]¶
Bases:
object
Sparse implementation of a row vector, where only the non-zero elements are stored in memory, and any non saved index is taken to be zero.
- __getitem__(i: int) qcp.matrices.types.SCALARS [source]¶
Get the item at the given index in the SparseVector.
- Parameters
i (int) – The index of the value to read
- Returns
The indexed value.
- Return type
- __init__(entries: Union[List[SCALARS], Dict[int, SCALARS]], size: int)[source]¶
Create a new SparseVector object, where the values are allocated either directly, by providing a dictionary mapping the indices to the values, or inferred by reading the given list and storing any non-zero list values in a dictionary mapping.
When the dictionary is provided, the vector size is inferred from the highest index in the dictionary, unless the value is overwritten with the size parameter.
- Parameters
entries (Union[List[SCALARS], Dict[int, SCALARS]]) – The object containing the vector values to use.
size (int) – The size of the SparseVector
- qcp.matrices.sparse_matrix._list_to_dict()[source]¶
Convert a given list of values into a dictionary mapping indices to non-zero values
- Parameters
list (vals) – List of vector values.
int (limit) – Optional iteration limit for fixed sized rows.
- Returns
dict of key/value pairs for non-zero entries in the list.
- Return type
Dict[int,
SCALARS
]
qcp.matrices.types module¶
- qcp.matrices.types.MATRIX¶
Type Alias for the type of the matrix content, a 2D nested list of allowed matrix element types
alias of
List
[List
[Union
[complex
,float
,int
]]]
- qcp.matrices.types.SCALARS¶
Type Alias for the allowed types for the matrix elements
alias of
Union
[complex
,float
,int
]
- qcp.matrices.types.SCALARS_T = (<class 'complex'>, <class 'float'>, <class 'int'>)¶
Typle Alias for the allowed matrix element types as a tuple
- qcp.matrices.types.SPARSE¶
Type Alias for the storage method used in SparseMatrix, the entries of the matrix are stored in a nested dictionary, where values are looked up by row/column index
alias of
Dict
[int
,Dict
[int
,Union
[complex
,float
,int
]]]
- qcp.matrices.types.VECTOR¶
Type Alias for the type of a matrix row, as a list representation
alias of
List
[Union
[complex
,float
,int
]]
Module contents¶
Module containing pure Python implementations of matrices.
- qcp.matrices.DefaultMatrix¶
Quick type referencing for the preferred Matrix class to use, so that if we want to change it later, all we have to do is modify this line