matrix_calcution

This module contains matrix calculation in classical shadow.

Post Processing - Classical Shadow - Matrix Calculation (qurry.process.classical_shadow.matrix_calcution)

qurry.process.classical_shadow.matrix_calcution.AllTraceRhoMethod

The method to calculate the all trace of Rho square.

  • “einsum_aij_bji_to_ab_numpy”:

    Use np.einsum(“aij,bji->ab”, rho_m_list, rho_m_list) to calculate the trace. This is the fastest implementation to calculate the trace of Rho if JAX is not available.

  • “einsum_aij_bji_to_ab_jax”:

    Use jnp.einsum(“aij,bji->ab”, rho_m_list, rho_m_list) to calculate the trace. This is the fastest implementation to calculate the trace of Rho.

alias of Literal[‘einsum_aij_bji_to_ab_numpy’, ‘einsum_aij_bji_to_ab_jax’] | str

qurry.process.classical_shadow.matrix_calcution.ClassicalShadowPythonMethod

The method to use for the calculation of classical shadow. It can be either “jax” or “numpy”. - “jax”: Use JAX to calculate the Kronecker product. - “numpy”: Use Numpy to calculate the Kronecker product.

alias of Literal[‘jax’, ‘numpy’]

qurry.process.classical_shadow.matrix_calcution.DEFAULT_PYTHON_METHOD: Literal['jax', 'numpy'] = 'jax'

The default backend to use for the calculation of classical shadow.

It can be either “jax” or “numpy”. - “jax”: Use JAX to calculate the Kronecker product. - “numpy”: Use Numpy to calculate the Kronecker product.

qurry.process.classical_shadow.matrix_calcution.SingleTraceRhoMethod

The method to calculate the trace of single Rho square.

  • “trace_of_matmul”: Use

    np.trace(np.matmul(rho_m1, rho_m2)) to calculate the trace.

  • “quick_trace_of_matmul” or “einsum_ij_ji”:

    Use np.einsum(“ij,ji”, rho_m1, rho_m2) to calculate the trace.

alias of Literal[‘trace_of_matmul’, ‘quick_trace_of_matmul’, ‘einsum_ij_ji’] | str

qurry.process.classical_shadow.matrix_calcution.all_trace_rho_by_einsum_aij_bji_to_ab_jax(rho_m_array: ndarray[tuple[int, int, int], dtype[complex128]]) complex128[source]

The trace of Rho by einsum_aij_bji_to_ab by JAX.

This is the fastest implementation to calculate the trace of Rho.

Parameters:

rho_m_array (np.ndarray[tuple[int, int, int], np.dtype[np.complex128]]) – The Rho M array.

Returns:

The trace of Rho.

Return type:

np.complex128

qurry.process.classical_shadow.matrix_calcution.all_trace_rho_by_einsum_aij_bji_to_ab_numpy(rho_m_array: ndarray[tuple[int, int, int], dtype[complex128]]) complex128[source]

The trace of Rho by einsum_aij_bji_to_ab.

This is the fastest implementation to calculate the trace of Rho.

Parameters:

rho_m_array (np.ndarray[tuple[int, int, int], np.dtype[np.complex128]]) – The Rho M array.

Returns:

The trace of Rho.

Return type:

np.complex128

qurry.process.classical_shadow.matrix_calcution.prediction_einsum_aij_bji_to_ab_jax(given_operators: ndarray[tuple[int, int, int], dtype[complex128]], estimators: ndarray[tuple[int, int, int], dtype[complex128]]) tuple[list[complex128], list[ndarray[tuple[int, int], dtype[complex128]]]][source]

Calculate the prediction of given operators by einsum_aij_bji_to_ab_jax.

Args:
given_operators (np.ndarray[tuple[int, int, int], np.dtype[np.complex128]]):

The given operators.

estimators (np.ndarray[tuple[int, int, int], np.dtype[np.complex128]]):

The estimators.

Returns:

A tuple containing: - A list of median values for each given operator. - A list of the corresponding median estimators for each given operator.

Return type:

tuple[list[np.complex128], list[np.ndarray[tuple[int, int], np.dtype[np.complex128]]]]

qurry.process.classical_shadow.matrix_calcution.prediction_einsum_aij_bji_to_ab_numpy(given_operators: ndarray[tuple[int, int, int], dtype[complex128]], estimators: ndarray[tuple[int, int, int], dtype[complex128]]) tuple[list[complex128], list[ndarray[tuple[int, int], dtype[complex128]]]][source]

Calculate the prediction of given operators by einsum_aij_bji_to_ab_numpy.

Parameters:
  • given_operators (np.ndarray[tuple[int, int, int], np.dtype[np.complex128]]) – The given operators.

  • estimators (np.ndarray[tuple[int, int, int], np.dtype[np.complex128]]) – The estimators.

Returns:

A tuple containing: - A list of median values for each given operator. - A list of the corresponding median estimators for each given operator.

Return type:

tuple[list[np.complex128], list[np.ndarray[tuple[int, int], np.dtype[np.complex128]]]]

qurry.process.classical_shadow.matrix_calcution.rho_mki_kronecker_product_numpy(key_list_of_precomputed: list[tuple[int, str]]) ndarray[tuple[int, int], dtype[complex128]][source]

Kronecker product for \(\rho_{mki}\) by Numpy.

Parameters:

key_list_of_precomputed (list[tuple[int, str]]) – The list of the keys of the precomputed \(\rho_{mki}\).

Returns:

The Kronecker product of the \(\rho_{mki}\).

Return type:

NDArray[np.complex128]

qurry.process.classical_shadow.matrix_calcution.rho_mki_kronecker_product_numpy_2(key_list_of_precomputed: Iterable[int]) ndarray[tuple[int, int], dtype[complex128]][source]

Kronecker product for \(\rho_{mki}\) by Numpy.

Parameters:

key_list_of_precomputed (Iterable[int]) – The list of the keys of the precomputed \(\rho_{mki}\).

Returns:

The Kronecker product of the \(\rho_{mki}\).

Return type:

NDArray[np.complex128]

qurry.process.classical_shadow.matrix_calcution.select_all_trace_rho_by_einsum_aij_bji_to_ab(method: Literal['einsum_aij_bji_to_ab_numpy', 'einsum_aij_bji_to_ab_jax'] | str = 'einsum_aij_bji_to_ab_jax') Callable[[ndarray[tuple[int, int, int], dtype[complex128]]], complex128][source]

Select the method to calculate the trace of Rho square.

Parameters:

method (AllTraceRhoMethod, optional) –

The method to use for the calculation. Defaults to DEFAULT_ALL_TRACE_RHO_METHOD. It can be either “einsum_aij_bji_to_ab_numpy” or “einsum_aij_bji_to_ab_jax”. - “einsum_aij_bji_to_ab_numpy”:

Use np.einsum(“aij,bji->ab”, rho_m_list, rho_m_list) to calculate the trace.

  • ”einsum_aij_bji_to_ab_jax”:

    Use jnp.einsum(“aij,bji->ab”, rho_m_list, rho_m_list) to calculate the trace.

This is the fastest implementation to calculate the trace of Rho.

Returns:

The function to calculate the trace of Rho.

Return type:

Callable[[np.ndarray[tuple[int, int, int], np.dtype[np.complex128]]], np.complex128]

qurry.process.classical_shadow.matrix_calcution.select_prediction_einsum_aij_bji_to_ab(method: Literal['einsum_aij_bji_to_ab_numpy', 'einsum_aij_bji_to_ab_jax'] | str = 'einsum_aij_bji_to_ab_jax') Callable[[ndarray[tuple[int, int, int], dtype[complex128]], ndarray[tuple[int, int, int], dtype[complex128]]], tuple[list[complex128], list[ndarray[tuple[int, int], dtype[complex128]]]]][source]

Select the method to calculate the prediction of given operators.

Parameters:

method (AllTraceRhoMethod, optional) – The method to use for the calculation. Defaults to DEFAULT_ALL_TRACE_RHO_METHOD It can be either “jax” or “numpy”.

Returns:

Callable[[

np.ndarray[tuple[int, int, int], np.dtype[np.complex128]], np.ndarray[tuple[int, int, int], np.dtype[np.complex128]]

], tuple[list[np.complex128], list[np.ndarray[tuple[int, int], np.dtype[np.complex128]]]]]:

The function to calculate the prediction of given operators.

qurry.process.classical_shadow.matrix_calcution.select_single_trace_rho_method(method: Literal['trace_of_matmul', 'quick_trace_of_matmul', 'einsum_ij_ji'] | str = 'quick_trace_of_matmul') Callable[[tuple[ndarray[tuple[int, int], dtype[complex128]], ndarray[tuple[int, int], dtype[complex128]]]], complex128][source]

Select the method to calculate the trace of Rho square.

Parameters:

method (str) – The method to use for the calculation.

Returns:

Callable[[tuple[

np.ndarray[tuple[int, int], np.dtype[np.complex128]], np.ndarray[tuple[int, int], np.dtype[np.complex128]],

]], np.complex128]:

The function to calculate the trace of Rho.

qurry.process.classical_shadow.matrix_calcution.set_cpu_only()[source]

Set JAX to use CPU only.

qurry.process.classical_shadow.matrix_calcution.single_trace_rho_by_einsum_ij_ji(rho_m1_and_rho_m2: tuple[ndarray[tuple[int, int], dtype[complex128]], ndarray[tuple[int, int], dtype[complex128]]]) complex128[source]

The single trace of Rho by einsum_ij_ji by Numpy.

Parameters:
  • (tupletuple[ (rho_m1_and_rho_m2)

  • np.ndarray[tuple[int

  • int]

  • np.dtype[np.complex128]]

:param : :param np.ndarray[tuple[int: :param int]: :param np.dtype[np.complex128]]: :param :

]):

The tuple of rho_m1 and rho_m2.

Returns:

The trace of Rho.

Return type:

np.complex128

qurry.process.classical_shadow.matrix_calcution.single_trace_rho_by_trace_of_matmul(rho_m1_and_rho_m2: tuple[ndarray[tuple[int, int], dtype[complex128]], ndarray[tuple[int, int], dtype[complex128]]]) complex128[source]

The single trace of Rho by trace of matmul.

Parameters:
  • (tuple[ (rho_m1_and_rho_m2) – np.ndarray[tuple[int, int], np.dtype[np.complex128]], np.ndarray[tuple[int, int], np.dtype[np.complex128]],

  • ]) – The tuple of rho_m1 and rho_m2.

Returns:

The trace of Rho.

Return type:

np.complex128