paddle_quantum.qinfo

The library of functions in quantum information.

paddle_quantum.qinfo.partial_trace(state, dim1, dim2, A_or_B)

Calculate the partial trace of the quantum state.

Parameters:
  • state (ndarray | Tensor | State) – Input quantum state.

  • dim1 (int) – The dimension of system A.

  • dim2 (int) – The dimension of system B.

  • A_or_B (int) – 1 or 2. 1 means to calculate partial trace on system A; 2 means to calculate partial trace on system B.

Returns:

Partial trace of the input quantum state.

Return type:

ndarray | Tensor | State

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

paddle_quantum.qinfo.trace_distance(rho, sigma)

Calculate the trace distance of two quantum states.

\[D(\rho, \sigma) = 1 / 2 * \text{tr}|\rho-\sigma|\]
Parameters:
  • rho (ndarray | Tensor | State) – a quantum state.

  • sigma (ndarray | Tensor | State) – a quantum state.

Returns:

The trace distance between the input quantum states.

Return type:

ndarray | Tensor

paddle_quantum.qinfo.state_fidelity(rho, sigma)

Calculate the fidelity of two quantum states.

\[F(\rho, \sigma) = \text{tr}(\sqrt{\sqrt{\rho}\sigma\sqrt{\rho}})\]
Parameters:
  • rho (ndarray | Tensor | State) – a quantum state.

  • sigma (ndarray | Tensor | State) – a quantum state.

Returns:

The fidelity between the input quantum states.

Return type:

ndarray | Tensor

paddle_quantum.qinfo.gate_fidelity(U, V)

calculate the fidelity between gates

\[F(U, V) = |\text{tr}(UV^\dagger)|/2^n\]

\(U\) is a \(2^n\times 2^n\) unitary gate

Parameters:
  • U (ndarray | Tensor) – quantum gate \(U\) in matrix form

  • V (ndarray | Tensor) – quantum gate \(V\) in matrix form

Returns:

fidelity between gates

Return type:

ndarray | Tensor

paddle_quantum.qinfo.purity(rho)

Calculate the purity of a quantum state.

\[P = \text{tr}(\rho^2)\]
Parameters:

rho (ndarray | Tensor | State) – Density matrix form of the quantum state.

Returns:

The purity of the input quantum state.

Return type:

ndarray | Tensor

paddle_quantum.qinfo.von_neumann_entropy(rho, base=2)

Calculate the von Neumann entropy of a quantum state.

\[S = -\text{tr}(\rho \log(\rho))\]
Parameters:
  • rho (ndarray | Tensor | State) – Density matrix form of the quantum state.

  • base (int | None) – The base of logarithm. Defaults to 2.

Returns:

The von Neumann entropy of the input quantum state.

Return type:

ndarray | Tensor

paddle_quantum.qinfo.relative_entropy(rho, sig, base=2)

Calculate the relative entropy of two quantum states.

\[S(\rho \| \sigma)=\text{tr} \rho(\log \rho-\log \sigma)\]
Parameters:
  • rho (ndarray | Tensor | State) – Density matrix form of the quantum state.

  • sig (ndarray | Tensor | State) – Density matrix form of the quantum state.

  • base (int | None) – The base of logarithm. Defaults to 2.

Returns:

Relative entropy between input quantum states.

Return type:

ndarray | Tensor

paddle_quantum.qinfo.random_pauli_str_generator(num_qubits, terms=3)

Generate a random observable in list form.

An observable \(O=0.3X\otimes I\otimes I+0.5Y\otimes I\otimes Z\)’s list form is [[0.3, 'x0'], [0.5, 'y0,z2']]. Such an observable is generated by random_pauli_str_generator(3, terms=2)

Parameters:
  • num_qubits (int) – Number of qubits.

  • terms (int | None) – Number of terms in the observable. Defaults to 3.

Returns:

The Hamiltonian of randomly generated observable.

Return type:

List

paddle_quantum.qinfo.pauli_str_convertor(observable)

Concatenate the input observable with coefficient 1.

For example, if the input observable is [['z0,x1'], ['z1']], then this function returns the observable [[1, 'z0,x1'], [1, 'z1']].

Parameters:

observable (List) – The observable to be concatenated with coefficient 1.

Returns:

The observable with coefficient 1

Return type:

List

paddle_quantum.qinfo.random_hamiltonian_generator(num_qubits, terms=3)

Generate a random Hamiltonian.

Parameters:
  • num_qubits (int) – Number of qubits.

  • terms (int | None) – Number of terms in the Hamiltonian. Defaults to 3.

Returns:

The randomly generated Hamiltonian.

Return type:

List

paddle_quantum.qinfo.pauli_str_to_matrix(pauli_str, n)

Convert the input list form of an observable to its matrix form.

For example, if the input pauli_str is [[0.7, 'z0,x1'], [0.2, 'z1']] and n=3, then this function returns the observable \(0.7Z\otimes X\otimes I+0.2I\otimes Z\otimes I\) in matrix form.

Parameters:
  • pauli_str (list) – A list form of an observable.

  • n (int) – Number of qubits.

Raises:

ValueError – Only Pauli operator “I” can be accepted without specifying its position.

Returns:

The matrix form of the input observable.

Return type:

Tensor

paddle_quantum.qinfo.partial_transpose_2(density_op, sub_system=None)

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

Parameters:
  • density_op (ndarray | Tensor | State) – Density matrix form of the quantum state.

  • sub_system (int | None) – 1 or 2. 1 means to perform partial transpose on system A; 2 means to perform partial transpose on system B. Default is 2.

Returns:

The partial transpose of the input quantum state.

Return type:

ndarray | Tensor

Example:

import paddle
from paddle_quantum.qinfo import partial_transpose_2

rho_test = paddle.arange(1,17).reshape([4,4])
partial_transpose_2(rho_test, sub_system=1)
[[ 1,  2,  9, 10],
 [ 5,  6, 13, 14],
 [ 3,  4, 11, 12],
 [ 7,  8, 15, 16]]
paddle_quantum.qinfo.partial_transpose(density_op, n)

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

Parameters:
  • density_op (ndarray | Tensor | State) – Density matrix form of the quantum state.

  • n (int) – Number of qubits of subsystem A, with qubit indices as [0, 1, …, n-1]

Returns:

The partial transpose of the input quantum state.

Return type:

ndarray | Tensor

paddle_quantum.qinfo.permute_systems(mat, perm_list, dim_list)

Permute quantum system based on a permute list

Parameters:
  • mat (ndarray | Tensor | State) – A given matrix representation which is usually a quantum state.

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

  • dim – A list of dimension sizes of each subsystem.

Returns:

The permuted matrix

Return type:

ndarray | Tensor | State

paddle_quantum.qinfo.negativity(density_op)

Compute the Negativity \(N = ||\frac{\rho^{T_A}-1}{2}||\) of the input quantum state.

Parameters:

density_op (ndarray | Tensor | State) – Density matrix form of the quantum state.

Returns:

The Negativity of the input quantum state.

Return type:

ndarray | Tensor

paddle_quantum.qinfo.logarithmic_negativity(density_op)

Calculate the Logarithmic Negativity \(E_N = ||\rho^{T_A}||\) of the input quantum state.

Parameters:

density_op (ndarray | Tensor | State) – Density matrix form of the quantum state.

Returns:

The Logarithmic Negativity of the input quantum state.

Return type:

ndarray | Tensor

paddle_quantum.qinfo.is_ppt(density_op)

Check if the input quantum state is PPT.

Parameters:

density_op (ndarray | Tensor | State) – Density matrix form of the quantum state.

Returns:

Whether the input quantum state is PPT.

Return type:

bool

paddle_quantum.qinfo.is_choi(op)

Check if the input op is a Choi operator of a quantum operation.

Parameters:

op (ndarray | Tensor) – matrix form of the linear operation.

Returns:

Whether the input op is a valid quantum operation Choi operator.

Return type:

bool

Note

The operation op is (default) applied to the second system.

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

paddle_quantum.qinfo.image_to_density_matrix(image_filepath)

Encode image to density matrix

Parameters:

image_filepath (str) – Path to the image file.

Returns:

The density matrix obtained by encoding

Return type:

State

paddle_quantum.qinfo.shadow_trace(state, hamiltonian, sample_shots, method='CS')

Estimate the expectation value \(\text{trace}(H\rho)\) of an observable \(H\).

Parameters:
  • state (State) – Quantum state.

  • hamiltonian (Hamiltonian) – Observable.

  • sample_shots (int) – Number of samples.

  • method (str | None) – Method used to, which should be one of “CS”, “LBCS”, and “APS”. Default is “CS”.

Raises:

ValueError – Hamiltonian has a bad form

Returns:

The estimated expectation value for the hamiltonian.

Return type:

float

paddle_quantum.qinfo.tensor_state(state_a, state_b, *args)

calculate tensor product (kronecker product) between at least two state. This function automatically returns State instance

Parameters:
  • state_a (State) – State

  • state_b (State) – State

  • *args (State) – other states

Returns:

tensor product state of input states

Return type:

State

Note

Need to be careful with the backend of states; Use paddle_quantum.linalg.NKron if the input datatype is paddle.Tensor or numpy.ndarray.

paddle_quantum.qinfo.diamond_norm(channel_repr, dim_io=None, **kwargs)

Calculate the diamond norm of input.

Parameters:
  • channel_repr (Channel | Tensor) – A Channel or a paddle.Tensor instance.

  • dim_io (int | Tuple[int, int] | None) – The input and output dimensions.

  • **kwargs – Parameters to set cvx.

Raises:
  • RuntimeErrorchannel_repr must be Channel or paddle.Tensor.

  • TypeError – “dim_io” should be “int” or “tuple”.

Warning

channel_repr is not in Choi representaiton, and is converted into ChoiRepr.

Returns:

Its diamond norm.

Return type:

float

Reference:

Khatri, Sumeet, and Mark M. Wilde. “Principles of quantum communication theory: A modern approach.” arXiv preprint arXiv:2011.04672 (2020). Watrous, J. . “Semidefinite Programs for Completely Bounded Norms.” Theory of Computing 5.1(2009):217-238.

paddle_quantum.qinfo.channel_repr_convert(representation, source, target, tol=1e-06)

convert the given representation of a channel to the target implementation

Parameters:
  • representation (Tensor | ndarray | List[Tensor] | List[ndarray]) – input representation

  • source (str) – input form, should be 'Choi', 'Kraus' or 'Stinespring'

  • target (str) – target form, should be 'Choi', 'Kraus' or 'Stinespring'

  • tol (float) – error tolerance for the conversion from Choi, \(10^{-6}\) by default

Raises:

ValueError – Unsupported channel representation: require Choi, Kraus or Stinespring.

Returns:

quantum channel by the target implementation

Return type:

Tensor | ndarray | List[Tensor] | List[ndarray]

Note

choi -> kraus currently has the error of order 1e-6 caused by eigh

Raises:

NotImplementedError – does not support the conversion of input data type

paddle_quantum.qinfo.random_channel(num_qubits, rank=None, target='Kraus')

Generate a random channel from its Stinespring representation

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

  • rank (int | None) – rank of this Channel. Defaults to be random sampled from \([0, 2^n]\)

  • target (str) – target representation, should to be 'Choi', 'Kraus' or 'Stinespring'

Returns:

the target representation of a random channel.

Return type:

Tensor | List[Tensor]

paddle_quantum.qinfo.kraus_unitary_random(num_qubits, num_oper)

randomly generate a set of unitaries as kraus operators for a quantum channel

Parameters:
  • num_qubits (int) – The amount of qubits of quantum channel.

  • num_oper (int) – The amount of unitaries to be generated.

Returns:

a list of kraus operators

Return type:

list

paddle_quantum.qinfo.grover_generation(oracle)

Construct the Grover operator based on oracle.

Parameters:

oracle (ndarray | Tensor) – the input oracle \(A\) to be rotated.

Returns:

Grover operator in form

Return type:

ndarray | Tensor

\[G = A (2 |0^n \rangle\langle 0^n| - I^n) A^\dagger \cdot (I - 2|1 \rangle\langle 1|) \otimes I^{n-1}\]
paddle_quantum.qinfo.qft_generation(num_qubits)

Construct the quantum fourier transpose (QFT) gate.

Parameters:

num_qubits (int) – number of qubits \(n\) st. \(N = 2^n\).

Returns:

a gate in below matrix form, here \(\omega_N = \text{exp}(\frac{2 \pi i}{N})\)

Return type:

Tensor

\[\begin{split}\begin{align} QFT = \frac{1}{\sqrt{N}} \begin{bmatrix} 1 & 1 & .. & 1 \\ 1 & \omega_N & .. & \omega_N^{N-1} \\ .. & .. & .. & .. \\ 1 & \omega_N^{N-1} & .. & \omega_N^{(N-1)^2} \end{bmatrix} \end{align}\end{split}\]