qml.math.partial_trace¶
- partial_trace(matrix, indices, c_dtype='complex128')[source]¶
Compute the reduced density matrix by tracing out the provided indices.
- Parameters
matrix (tensor_like) – 2D or 3D density matrix tensor. For a 2D tensor, the size is assumed to be
(2**n, 2**n)
, for some integer number of wiresn
. For a 3D tensor, the first dimension is assumed to be the batch dimension,(batch_dim, 2**N, 2**N)
.indices (list(int)) – List of indices to be traced.
- Returns
(reduced) Density matrix of size
(2**len(wires), 2**len(wires))
- Return type
tensor_like
Example
We can compute the partial trace of the matrix
x
with respect to its 0th index.>>> x = np.array([[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) >>> partial_trace(x, indices=[0]) array([[1.+0.j, 0.+0.j], [0.+0.j, 0.+0.j]])
We can also pass a batch of matrices
x
to the function and return the partial trace of each matrix with respect to each matrix’s 0th index.>>> x = np.array([ ... [[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], ... [[0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] ... ]) >>> partial_trace(x, indices=[0]) array([[[1.+0.j, 0.+0.j], [0.+0.j, 0.+0.j]], [[0.+0.j, 0.+0.j], [0.+0.j, 1.+0.j]]])
The partial trace can also be computed with respect to multiple indices within different frameworks such as TensorFlow.
>>> x = tf.Variable([[[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], ... [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0]]], dtype=tf.complex128) >>> partial_trace(x, indices=[1]) <tf.Tensor: shape=(2, 2, 2), dtype=complex128, numpy= array([[[1.+0.j, 0.+0.j], [0.+0.j, 0.+0.j]], [[0.+0.j, 0.+0.j], [0.+0.j, 1.+0.j]]])>