quairkit.qinfo.linalg

quairkit.qinfo.linalg.abs_norm(mat)

tool for calculation of matrix norm

Parameters:
mat : ndarray | Tensor | State

matrix

Returns:

norm of input matrix

Return type:

float

quairkit.qinfo.linalg.block_enc_herm(mat, num_block_qubits=1)

generate a (qubitized) block encoding of hermitian mat

Parameters:
mat : ndarray | Tensor

matrix to be block encoded

num_block_qubits : int

ancilla qubits used in block encoding

Returns:

a unitary that is a block encoding of mat

Return type:

ndarray | Tensor

quairkit.qinfo.linalg.create_matrix(linear_map, input_dim, input_dtype=None)

Create a matrix representation of a linear map without needing to specify the output dimension.

This function constructs a matrix representation for a given linear map and input dimension.

Parameters:
linear_map : Callable[[Tensor | ndarray], Tensor | ndarray]

A function representing the linear map, which takes an input_dim-dimensional vector and returns a vector.

input_dim : int

The dimension of the input space.

input_dtype : dtype | None

The dtype of the input. Defaults to None.

Returns:

A matrix representing the linear map.

Raises:

RuntimeWarning – the input`linear_map` may not be linear.

Return type:

Tensor | ndarray

quairkit.qinfo.linalg.dagger(mat)

tool for calculation of matrix dagger

Parameters:
mat : ndarray | Tensor

matrix

Returns:

The dagger of matrix

Return type:

ndarray | Tensor

quairkit.qinfo.linalg.direct_sum(A, B)

calculate the direct sum of A and B

Parameters:
A : ndarray | Tensor

\(m \times n\) matrix

B : ndarray | Tensor

\(p \times q\) matrix

Returns:

a direct sum of A and B, with shape \((m + p) \times (n + q)\)

Return type:

ndarray | Tensor

quairkit.qinfo.linalg.gradient(loss_function, var, n)

Computes the gradient of a given loss function with respect to its input variable.

Parameters:
loss_function : Callable[[Tensor], Tensor]

A loss function to compute the gradient.

var : Tensor | ndarray

A vector of shape (m,1) as the input variables for the loss function.

n : int

The number of iterations for gradient computation.

Returns:

The gradient vector of shape (m,1).

Return type:

torch.Tensor

quairkit.qinfo.linalg.hessian(loss_function, var)

Computes the Hessian matrix of a given loss function with respect to its input variables.

Parameters:
loss_function : Callable[[Tensor], Tensor]

The loss function to compute the Hessian.

var : Tensor | ndarray

A matrix of shape (n, m) as input variables for the loss function.

Returns:

Hessian matrix of shape (m, n, n).

Return type:

torch.Tensor

quairkit.qinfo.linalg.herm_transform(fcn, mat, ignore_zero=False)

function transformation for Hermitian matrix

Parameters:
fcn : Callable[[float], float]

function \(f\) that can be expanded by Taylor series

mat : Tensor | ndarray | State

hermitian matrix \(H\)

ignore_zero : bool | None

whether ignore eigenspaces with zero eigenvalue, defaults to be False

Returns

\(f(H)\)

quairkit.qinfo.linalg.logm(mat)

Calculate log of a matrix

Parameters:
mat : ndarray | Tensor | State

Input matrix.

Returns:

The matrix of natural base logarithms

Return type:

ndarray | Tensor

quairkit.qinfo.linalg.NKron(matrix_A, matrix_B, *args)

calculate Kronecker product of at least two density matrices

Parameters:
matrix_A : Tensor | ndarray

density matrix

matrix_B : Tensor | ndarray

density matrix

args : Tensor | ndarray

other density matrices

Returns:

Kronecker product of matrices

Return type:

Tensor | ndarray

from quairkit.state import density_op_random
from quairkit.linalg import NKron
A = density_op_random(2)
B = density_op_random(2)
C = density_op_random(2)
result = NKron(A, B, C)

Note

result from above code block should be A otimes B otimes C

quairkit.qinfo.linalg.p_norm(mat, p)

tool for calculation of Schatten p-norm

Parameters:
mat : ndarray | Tensor | State

matrix

p : ndarray | Tensor | float

p-norm parameter

Returns:

p-norm of input matrix

Return type:

ndarray | Tensor

quairkit.qinfo.linalg.partial_trace(state, trace_idx, system_dim=2)

Calculate the partial trace of the quantum state

Parameters:
state : ndarray | Tensor | State

Input quantum state.

trace_idx : List[int] | int

The system indices to be traced out.

system_dim : List[int] | int

The dimension of all systems. Defaults to be the qubit case.

Returns:

Partial trace of the quantum state with arbitrarily selected subsystem.

Return type:

ndarray | Tensor | State

quairkit.qinfo.linalg.partial_trace_discontiguous(state, preserve_qubits=None)

Calculate the partial trace of the quantum state with arbitrarily selected subsystem

Parameters:
state : ndarray | Tensor | State

Input quantum state.

preserve_qubits : List[int] | None

Remaining qubits, default is None, indicate all qubits remain.

Returns:

Partial trace of the quantum state with arbitrarily selected subsystem.

Return type:

ndarray | Tensor | State

Note

suitable only when the systems are qubits.

quairkit.qinfo.linalg.partial_transpose(state, transpose_idx, system_dim=2)

Calculate the partial transpose \(\rho^{T_A}\) of the input quantum state.

Parameters:
state : ndarray | Tensor | State

input quantum state.

transpose_idx : List[int] | int

The system indices to be transposed.

system_dim : List[int] | int

The dimension of all systems. Defaults to be the qubit case.

Returns:

The partial transpose of the input quantum state.

Return type:

ndarray | Tensor

quairkit.qinfo.linalg.pauli_decomposition(mat)

Decompose the matrix by the Pauli basis.

Parameters:
mat : ndarray | Tensor

the matrix to be decomposed

Returns:

The list of coefficients corresponding to Pauli basis.

Return type:

ndarray | Tensor

quairkit.qinfo.linalg.permute_systems(state, perm_list, system_dim=2)

Permute quantum system based on a permute list

Parameters:
mat

A given matrix representation which is usually a quantum state.

perm_list : List[int]

The permute list. e.g. input [0,2,1,3] will permute the 2nd and 3rd subsystems.

system_dim : List[int] | int

A list of dimension sizes of each subsystem.

Returns:

The permuted matrix

Return type:

ndarray | Tensor | State

quairkit.qinfo.linalg.prob_sample(distribution, shots=1024, binary=True, proportional=False)

Sample from a probability distribution.

Parameters:
distribution : Tensor

The probability distribution.

shots : int

The number of shots. Defaults to 1024.

binary : bool

Whether the sampled result is recorded as binary. Defaults to True.

proportional : bool

Whether the counts are shown in proportion

Returns:

A dictionary containing the ordered sampled results and their counts.

Return type:

Dict[str, int | float]

quairkit.qinfo.linalg.schmidt_decompose(psi, sys_A=None)

Calculate the Schmidt decomposition of a quantum state \(\lvert\psi\rangle=\sum_ic_i\lvert i_A\rangle\otimes\lvert i_B \rangle\).

Parameters:
psi : ndarray | Tensor | State

State vector form of the quantum state, with shape (2**n)

sys_A : List[int] | None

Qubit indices to be included in subsystem A (other qubits are included in subsystem B), default are the first half qubits of \(\lvert \psi\rangle\)

Returns:

contains elements

  • A one dimensional array composed of Schmidt coefficients, with shape (k)

  • A high dimensional array composed of bases for subsystem A \(\lvert i_A\rangle\), with shape (k, 2**m, 1)

  • A high dimensional array composed of bases for subsystem B \(\lvert i_B\rangle\) , with shape (k, 2**m, 1)

Return type:

Tuple[Tensor, Tensor, Tensor] | Tuple[ndarray, ndarray, ndarray]

quairkit.qinfo.linalg.sqrtm(mat)

Calculate square root of a matrix

Parameters:
mat : ndarray | Tensor | State

Input matrix.

Returns:

The square root of the matrix

Return type:

ndarray | Tensor

quairkit.qinfo.linalg.trace(mat, axis1=-2, axis2=-1)

Return the sum along diagonals of the tensor.

If \(mat\) is 2-D tensor, the sum along its diagonal is returned.

If \(mat\) has more than two dimensions, then the axes specified by axis1 and axis2 are used to determine the 2-D sub-tensors whose traces are returned. The shape of the resulting tensor is the same as the shape of \(mat\) with axis1 and axis2 removed.

Parameters:
mat : ndarray | Tensor | State

Input tensor, from which the diagonals are taken.

axis1 : int | None

The first axis of the 2-D sub-tensors along which the diagonals should be taken. Defaults to -2.

axis2 : int | None

The second axis of the 2-D sub-tensors along which the diagonals should be taken. Defaults to -1.

Returns:

The sum along the diagonals. If \(mat\) is 2-D tensor, the sum along its diagonal is returned. If \(mat\) has larger dimensions, then a tensor of sums along diagonals is returned.

Raises:

ValueError – The 2-D tensor from which the diagonals should be taken is not square.

Return type:

ndarray | Tensor

Note

The 2-D tensor/array from which the diagonals is taken should be square.

quairkit.qinfo.linalg.trace_norm(mat)

tool for calculation of trace norm

Parameters:
mat : ndarray | Tensor | State

matrix

Returns:

trace norm of input matrix

Return type:

ndarray | Tensor