qolumbina.utils.data_conversion module#
A collection of utility functions that can be used for preprocessing and postprocessing of the test process.
- counts_to_expectation(counts, observable)[source]#
Convert counts to expectation value of a given observable.
- Parameters:
counts (dict[str, int]) – Measurement counts as a dictionary mapping bitstrings to their counts
observable (dict[str, float]) – A dictionary mapping bitstrings to their observable values
- Returns:
The expectation value as a float.
- Return type:
float
Example
>>> counts_to_expectation({'00': 500, '01': 300, '10': 100, '11': 100}, ... {'00': 1.0, '01': -1.0, '10': -1.0, '11': 1.0}) 0.2 # (0.5*1.0 + 0.3*(-1.0) + 0.1*(-1.0) + 0.1*1.0 = 0.2)
- counts_to_probabilities(counts)[source]#
Convert counts to probabilities.
- Parameters:
counts (dict[str, int]) – Measurement counts as a dictionary mapping bitstrings to their counts
- Returns:
A dictionary mapping bitstrings to their probabilities.
- Return type:
dict[str, float]
Example
>>> counts_to_probabilities({'00': 500, '01': 300, '10': 100, '11': 100}) {'00': 0.5, '01': 0.3, '10': 0.1, '11': 0.1}
- decompose_joint_state(statevector, target_subsystem, atol=1e-10)[source]#
Return the reduced density matrix of the given subsystem.
- Parameters:
statevector (Statevector) – Joint pure state \(\ket{\psi_{AB}}\) of the entire system consisting of subsystems \(A\) and \(B\), represented as a Statevector object.
target_subsystem (list[int]) – List of qubit indices for the target subsystem \(A\)
atol (float) – Absolute tolerance for numerical operations
- Returns:
- A tuple containing a string indicating the type (
'pure_state' or
'mixed_state') and the correspondingStatevectororDensityMatrixof the target subsystem \(A\).
- A tuple containing a string indicating the type (
- Raises:
ValueError – if
target_subsystemindices exceed the number of qubits in statevector- Return type:
tuple[str, Statevector | DensityMatrix]
Example
>>> from qiskit.quantum_info import Statevector >>> import numpy as np >>> psi_AB = Statevector.from_label('00') + Statevector.from_label('11') >>> psi_AB = psi_AB / np.linalg.norm(psi_AB.data) # Normalize >>> decompose_joint_state(psi_AB, target_subsystem=[0]) ('mixed_state', DensityMatrix([[0.5+0.j, 0. +0.j], [0. +0.j, 0.5+0.j]], dims=(2,)))
- frac_to_gate_list(value, n_bits, tol=1e-09)[source]#
Convert a fractional decimal number to a quantum gate list using fixed-point binary representation, with precision check.
- Parameters:
value (float) – Fractional decimal number to convert (must be in \([0, 1)\))
n_bits (int) – Number of bits in the output gate list (must be positive)
tol (float) – Tolerance for precision check (default: 1e-9)
- Returns:
Representation of a gate list
- Raises:
ValueError – if
valueis not in \([0, 1)\), or ifn_bitsis not positiveTypeError – if
valueis not a float, or ifn_bitsis not an integer
- Return type:
list[str]
Example
>>> frac_to_gate_list(0.375, 4) # 0.25 + 0.125 = 0.011 in binary ['i', 'x', 'x', 'i']
- int_to_gate_list(value, n_bits)[source]#
Convert a decimal integer to gate list representation of a quantum state using binary encoding.
- Parameters:
value (int) – Non-negative integer to convert
n_bits (int) – Number of bits in the output gate list
- Returns:
Representation of a gate list
- Raises:
ValueError – if
valueis negative, or ifn_bitsis not positiveTypeError – if
valueis not an integer, or ifn_bitsis not an integer
- Return type:
list[str]
Example
>>> int_to_gate_list(3, 4) ['x', 'x', 'i', 'i'] # 3 = 0b0011 # (q0, q1, q2, q3) -> ('x', 'x', 'i', 'i')