paddle_quantum.linalg

The library of functions in linear algebra.

paddle_quantum.linalg.abs_norm(mat)

tool for calculation of matrix norm

Parameters:

mat (ndarray | Tensor | State) – matrix

Returns:

norm of input matrix

Return type:

float

paddle_quantum.linalg.dagger(mat)

tool for calculation of matrix dagger

Parameters:

mat (ndarray | Tensor) – matrix

Returns:

The dagger of matrix

Return type:

ndarray | Tensor

paddle_quantum.linalg.is_hermitian(mat, eps=1e-06)

verify whether mat is Hermitian

Parameters:
  • mat (ndarray | Tensor) – hermitian candidate \(P\)

  • eps (float | None) – tolerance of error

Returns:

determine whether \(P - P^\dagger = 0\)

Return type:

bool

paddle_quantum.linalg.is_positive(mat, eps=1e-06)

verify whether mat is a positive semi-definite matrix.

Parameters:
  • mat (ndarray | Tensor) – positive operator candidate \(P\)

  • eps (float | None) – tolerance of error

Returns:

determine whether \(P\) is Hermitian and eigenvalues are non-negative

Return type:

bool

paddle_quantum.linalg.is_state_vector(vec, eps=None)

verify whether vec is a legal quantum state vector

Parameters:
  • vec (ndarray | Tensor) – state vector candidate \(x\)

  • eps (float | None) – tolerance of error, default to be None i.e. no testing for data correctness

Returns:

determine whether \(x^\dagger x = 1\), and return the number of qubits or an error message

Return type:

Tuple[bool, int]

Note

error message is: * -1 if the above equation does not hold * -2 if the dimension of vec is not a power of 2 * -3 if vec is not a vector

paddle_quantum.linalg.is_density_matrix(rho, eps=None)

verify whether rho is a legal quantum density matrix

Parameters:
  • rho (ndarray | Tensor) – density matrix candidate

  • eps (float | None) – tolerance of error, default to be None i.e. no testing for data correctness

Returns:

determine whether rho is a PSD matrix with trace 1 and return the number of qubits or an error message.

Return type:

Tuple[bool, int]

Note

error message is: * -1 if rho is not PSD * -2 if the trace of rho is not 1 * -3 if the dimension of rho is not a power of 2 * -4 if rho is not a square matrix

paddle_quantum.linalg.is_projector(mat, eps=1e-06)

verify whether mat is a projector

Parameters:
  • mat (ndarray | Tensor) – projector candidate \(P\)

  • eps (float | None) – tolerance of error

Returns:

determine whether \(PP - P = 0\)

Return type:

bool

paddle_quantum.linalg.is_unitary(mat, eps=0.0001)

verify whether mat is a unitary

Parameters:
  • mat (ndarray | Tensor) – unitary candidate \(P\)

  • eps (float | None) – tolerance of error

Returns:

determine whether \(PP^\dagger - I = 0\)

Return type:

bool

paddle_quantum.linalg.hermitian_random(num_qubits)

randomly generate a \(2^n \times 2^n\) hermitian matrix

Parameters:

num_qubits (int) – number of qubits \(n\)

Returns:

a \(2^n \times 2^n\) hermitian matrix

Return type:

Tensor

paddle_quantum.linalg.orthogonal_projection_random(num_qubits)

randomly generate a \(2^n \times 2^n\) rank-1 orthogonal projector

Parameters:

num_qubits (int) – number of qubits \(n\)

Returns:

a \(2^n \times 2^n\) orthogonal projector

Return type:

Tensor

paddle_quantum.linalg.density_matrix_random(num_qubits)

randomly generate an num_qubits-qubit state in density matrix form

Parameters:

num_qubits (int) – number of qubits \(n\)

Returns:

a \(2^n \times 2^n\) density matrix

Return type:

Tensor

paddle_quantum.linalg.unitary_random(num_qubits)

randomly generate a \(2^n \times 2^n\) unitary

Parameters:

num_qubits (int) – number of qubits \(n\)

Returns:

a \(2^n \times 2^n\) unitary matrix

Return type:

Tensor

paddle_quantum.linalg.unitary_hermitian_random(num_qubits)

randomly generate a \(2^n \times 2^n\) hermitian unitary

Parameters:

num_qubits (int) – number of qubits \(n\)

Returns:

a \(2^n \times 2^n\) hermitian unitary matrix

Return type:

Tensor

paddle_quantum.linalg.unitary_random_with_hermitian_block(num_qubits, is_unitary=False)

randomly generate a unitary \(2^n \times 2^n\) matrix that is a block encoding of a \(2^{n/2} \times 2^{n/2}\) Hermitian matrix

Parameters:
  • num_qubits (int) – number of qubits \(n\)

  • is_unitary (bool) – whether the hermitian block is a unitary divided by 2 (for tutorial only)

Returns:

a \(2^n \times 2^n\) unitary matrix that its upper-left block is a Hermitian matrix

Return type:

Tensor

paddle_quantum.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

paddle_quantum.linalg.haar_orthogonal(num_qubits)

randomly generate an orthogonal matrix following Haar random, referenced by arXiv:math-ph/0609050v2

Parameters:

num_qubits (int) – number of qubits \(n\)

Returns:

a \(2^n \times 2^n\) orthogonal matrix

Return type:

Tensor

paddle_quantum.linalg.haar_unitary(num_qubits)

randomly generate a unitary following Haar random, referenced by arXiv:math-ph/0609050v2

Parameters:

num_qubits (int) – number of qubits \(n\)

Returns:

a \(2^n \times 2^n\) unitary

Return type:

Tensor

paddle_quantum.linalg.haar_state_vector(num_qubits, is_real=False)

randomly generate a state vector following Haar random

Parameters:
  • num_qubits (int) – number of qubits \(n\)

  • is_real (bool | None) – whether the vector is real, default to be False

Returns:

a \(2^n \times 1\) state vector

Return type:

Tensor

paddle_quantum.linalg.haar_density_operator(num_qubits, rank=None, is_real=False)

randomly generate a density matrix following Haar random

Parameters:
  • num_qubits (int) – number of qubits \(n\)

  • rank (int | None) – rank of density matrix, default to be None refering to full ranks

  • is_real (bool | None) – whether the density matrix is real, default to be False

Returns:

a \(2^n \times 2^n\) density matrix

Return type:

Tensor

paddle_quantum.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

paddle_quantum.linalg.NKron(matrix_A, matrix_B, *args)

calculate Kronecker product of at least two matrices

Parameters:
  • matrix_A (Tensor | ndarray) – matrix, as paddle.Tensor or numpy.ndarray

  • matrix_B (Tensor | ndarray) – matrix, as paddle.Tensor or numpy.ndarray

  • *args (Tensor | ndarray) – other matrices, as paddle.Tensor or numpy.ndarray

Returns:

Kronecker product of matrices, determined by input type of matrix_A

Return type:

Tensor | ndarray

from paddle_quantum.state import density_op_random
from paddle_quantum.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

paddle_quantum.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)\)

paddle_quantum.linalg.pauli_basis_generation(num_qubits)

Generate a Pauli basis.

Parameters:

num_qubits (int) – the number of qubits \(n\).

Returns:

The Pauli basis of \(\mathbb{C}^{2^n \times 2^n}\).

Return type:

List[Tensor]

paddle_quantum.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

paddle_quantum.linalg.subsystem_decomposition(mat, first_basis, second_basis, inner_prod)

Decompose the input matrix by two given bases in two subsystems.

Parameters:
  • mat (ndarray | Tensor) – the matrix \(w\) to be decomposed

  • first_basis (List[ndarray] | List[Tensor]) – a basis \(\{e_i\}_i\) from the first space

  • second_basis (List[ndarray] | List[Tensor]) – a basis \(\{f_j\}_j\) from the second space

  • inner_prod (Callable[[ndarray, ndarray], ndarray] | Callable[[Tensor, Tensor], Tensor]) – the inner product of these subspaces

Returns:

a coefficient matrix \([\beta_{ij}]\) such that \(w = \sum_{i, j} \beta_{ij} e_i \otimes f_j\).

Return type:

ndarray | Tensor