Skip to content

Tomography

Minimal single-qubit tomography helpers for MSD postselection demos.

TomographyResult dataclass

TomographyResult(shots_by_basis: Mapping[str, ndarray])

Point-estimate single-qubit tomography result.

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/gemini/decoding/tomography.py
 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 __init__(
    self,
    shots_by_basis: Mapping[str, np.ndarray],
) -> 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.
    """
    zero_counts: dict[str, int] = {}
    one_counts: dict[str, int] = {}
    totals: dict[str, int] = {}
    bloch: dict[str, float] = {}
    for basis in shots_by_basis:
        shots = np.asarray(shots_by_basis[basis], dtype=np.uint8)
        if shots.ndim != 2 or shots.shape[1] != 1:
            raise ValueError(
                "TomographyResult expects each basis to have shape (shots, 1)."
            )
        zero_counts[basis] = int(np.count_nonzero(shots[:, 0] == 0))
        one_counts[basis] = int(np.count_nonzero(shots[:, 0] == 1))
        totals[basis] = max(zero_counts[basis] + one_counts[basis], 1)
        bloch[basis] = (zero_counts[basis] - one_counts[basis]) / totals[basis]

    object.__setattr__(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/gemini/decoding/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)