qml.math.expand_matrix¶
- expand_matrix(mat, wires, wire_order=None, sparse_format='csr')[source]¶
Re-express a matrix acting on a subspace defined by a set of wire labels according to a global wire order.
- Parameters
mat (tensor_like) – matrix to expand
wires (Iterable) – wires determining the subspace that
mat
acts on; a matrix of dimension \(D^n\) acts on a subspace of \(n\) wires, where \(D\) is the qudit dimension (2).wire_order (Iterable) – global wire order, which has to contain all wire labels in
wires
, but can also contain additional labelssparse_format (str) – if
mat
is a SciPy sparse matrix then this is the string representing the preferred scipy sparse matrix format to cast the expanded matrix too
- Returns
expanded matrix
- Return type
tensor_like
Example
If the wire order is
None
or identical towires
, the original matrix gets returned:>>> matrix = np.array([[1, 2, 3, 4], ... [5, 6, 7, 8], ... [9, 10, 11, 12], ... [13, 14, 15, 16]]) >>> print(expand_matrix(matrix, wires=[0, 2], wire_order=[0, 2])) [[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12] [13 14 15 16]] >>> print(expand_matrix(matrix, wires=[0, 2])) [[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12] [13 14 15 16]]
If the wire order is a permutation of
wires
, the entries of the matrix get permuted:>>> print(expand_matrix(matrix, wires=[0, 2], wire_order=[2, 0])) [[ 1 3 2 4] [ 9 11 10 12] [ 5 7 6 8] [13 15 14 16]]
If the wire order contains wire labels not found in
wires
, the matrix gets expanded:>>> print(expand_matrix(matrix, wires=[0, 2], wire_order=[0, 1, 2])) [[ 1 2 0 0 3 4 0 0] [ 5 6 0 0 7 8 0 0] [ 0 0 1 2 0 0 3 4] [ 0 0 5 6 0 0 7 8] [ 9 10 0 0 11 12 0 0] [13 14 0 0 15 16 0 0] [ 0 0 9 10 0 0 11 12] [ 0 0 13 14 0 0 15 16]]
The method works with tensors from all autodifferentiation frameworks, for example:
>>> matrix_torch = torch.tensor([[1., 2.], ... [3., 4.]], requires_grad=True) >>> res = expand_matrix(matrix_torch, wires=["b"], wire_order=["a", "b"]) >>> type(res) torch.Tensor >>> res.requires_grad True
The method works with scipy sparse matrices, for example:
>>> from scipy import sparse >>> mat = sparse.csr_matrix([[0, 1], [1, 0]]) >>> qml.math.expand_matrix(mat, wires=[1], wire_order=[0,1]).toarray() array([[0., 1., 0., 0.], [1., 0., 0., 0.], [0., 0., 0., 1.], [0., 0., 1., 0.]])