Skip to content

Tomography

Minimal single-qubit tomography helpers.

TomographyResult dataclass

TomographyResult(shots_by_basis: Mapping[str, ndarray])

Point-estimate single-qubit tomography result.

__post_init__

__post_init__() -> None

Create a tomography result by computing the density matrix from the shots per basis.

Parameters:

Name Type Description Default
shots_by_basis Mapping[str, ndarray]

A mapping of each basis to an array of shots (0/1's) in each basis.

required
Source code in .venv/lib/python3.12/site-packages/bloqade/analysis/tomography/tomography.py
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
def __post_init__(
    self,
) -> None:
    """
    Create a tomography result by computing the density matrix from the shots per basis.

    Args:
        shots_by_basis (Mapping[str, np.ndarray]): A mapping of each basis to an array of shots (0/1's) in each basis.
    """
    if set(self.shots_by_basis) != BASES:
        raise ValueError("Single-qubit tomography requires X, Y, and Z keys.")

    bloch: dict[str, float] = {}
    for basis in BASES:
        shots = np.asarray(self.shots_by_basis[basis])
        if shots.ndim != 1:
            raise ValueError(
                "TomographyResult expects each basis to have shape (shots,)."
            )
        if shots.size == 0:
            raise ValueError(f"{basis}-basis shots cannot be empty.")
        if not np.all((shots == 0) | (shots == 1)):
            raise ValueError("Tomography shots must contain only zero or one.")

        shots = shots.astype(np.uint8, copy=False)
        prob_meas_one = float(np.mean(shots))
        bloch[basis] = 1.0 - 2.0 * prob_meas_one

    self.density_matrix = _density_matrix_from_bloch(bloch)

fidelity_bloch

fidelity_bloch(
    target_bloch: ndarray | Sequence[float],
    tol: float = 1e-10,
) -> float

Return the fidelity to a target state from its Bloch vector.

Source code in .venv/lib/python3.12/site-packages/bloqade/analysis/tomography/tomography.py
108
109
110
111
112
113
114
115
116
117
118
def fidelity_bloch(
    self,
    target_bloch: np.ndarray | Sequence[float],
    tol: float = 1e-10,
) -> float:
    """Return the fidelity to a target state from its Bloch vector."""

    target_density_matrix = _density_matrix_from_bloch(
        _validate_single_qubit_bloch_vector(target_bloch, tol=tol)
    )
    return _single_qubit_fidelity(self.density_matrix, target_density_matrix)