def test_purity_standard(): rho0 = np.diag([1, 0]) rho1 = np.diag([0.9, 0.1]) rho2 = np.diag([0.5, 0.5]) assert dm.purity(rho0) == 1.0 assert np.allclose(dm.purity(rho1), 0.82) # by hand calc assert dm.purity(rho2) == 0.5 rho_qutrit = np.diag([1.0, 1.0, 1.0]) / 3 assert dm.purity(rho_qutrit) == 1 / 3
def test_purity_renorm(): D = 2 rho0 = np.diag([1, 0]) rho1 = np.diag([0.9, 0.1]) rho2 = np.diag([0.5, 0.5]) assert dm.purity(rho0, dim_renorm=True) == 1.0 assert np.allclose(dm.purity(rho1, dim_renorm=True), (D / (D - 1)) * (0.82 - 1 / D)) # by hand calc assert dm.purity(rho2, dim_renorm=True) == 0.0 rho_qutrit = np.diag([1.0, 1.0, 1.0]) / 3 assert dm.purity(rho_qutrit, dim_renorm=True) == 0.0
def estimate_variance( results: List[ExperimentResult], qubits: List[int], tomo_estimator: Callable, functional: Callable, target_state=None, n_resamples: int = 40, project_to_physical: bool = False) -> Tuple[float, float]: """ Use a simple bootstrap-like method to return an errorbar on some functional of the quantum state. :param results: Measured results from a state tomography experiment :param qubits: Qubits that were tomographized. :param tomo_estimator: takes in ``results, qubits`` and returns a corresponding estimate of the state rho, e.g. ``linear_inv_state_estimate`` :param functional: Which functional to find variance, e.g. ``dm.purity``. :param target_state: A density matrix of the state with respect to which the distance functional is measured. Not applicable if functional is ``dm.purity``. :param n_resamples: The number of times to resample. :param project_to_physical: Whether to project the estimated state to a physical one with :py:func:`project_density_matrix`. """ if functional != dm.purity: if target_state is None: raise ValueError("You're not using the `purity` functional. " "Please specify a target state.") sample_estimate = [] for _ in range(n_resamples): resampled_results = _resample_expectations_with_beta(results) estimate = tomo_estimator(resampled_results, qubits) # TODO: Shim! over different return values between linear inv. and mle if isinstance(estimate, np.ndarray): rho = estimate else: rho = estimate.estimate.state_point_est if project_to_physical: rho = project_density_matrix(rho) # Calculate functional of the state if functional == dm.purity: sample_estimate.append(np.real(dm.purity(rho, dim_renorm=False))) else: sample_estimate.append(np.real(functional(target_state, rho))) return np.mean(sample_estimate), np.var(sample_estimate)