def test_variance_bootstrap():
    qubits = [0, 1]
    qc = get_test_qc(n_qubits=len(qubits))
    state_prep = Program([H(q) for q in qubits])
    state_prep.inst(CZ(qubits[0], qubits[1]))
    tomo_expt = generate_state_tomography_experiment(state_prep, qubits)
    results = list(
        measure_observables(qc=qc, tomo_experiment=tomo_expt, n_shots=10_000))
    estimate, status = iterative_mle_state_estimate(results=results,
                                                    qubits=qubits,
                                                    dilution=0.5)
    rho_est = estimate.estimate.state_point_est
    purity = np.trace(rho_est @ rho_est)
    purity = np.real_if_close(purity)
    assert purity.imag == 0.0

    def my_mle_estimator(_r, _q):
        return iterative_mle_state_estimate(results=_r,
                                            qubits=_q,
                                            dilution=0.5,
                                            entropy_penalty=0.0,
                                            beta=0.0)[0]

    boot_purity, boot_var = estimate_variance(results=results,
                                              qubits=qubits,
                                              tomo_estimator=my_mle_estimator,
                                              functional=dm.purity,
                                              n_resamples=5,
                                              project_to_physical=False)

    np.testing.assert_allclose(purity,
                               boot_purity,
                               atol=2 * np.sqrt(boot_var),
                               rtol=0.01)
def test_two_qubit_mle(two_q_tomo_fixture):
    qubits = [0, 1]
    results, rho_true = two_q_tomo_fixture
    estimate, status = iterative_mle_state_estimate(results=results,
                                                    qubits=qubits,
                                                    dilution=0.5)
    rho_est = estimate.estimate.state_point_est
    np.testing.assert_allclose(rho_true, rho_est, atol=0.01)
def test_hedged_single_qubit(single_q_tomo_fixture):
    qubits = [0]
    results, rho_true = single_q_tomo_fixture
    estimate, status = iterative_mle_state_estimate(results=results,
                                                    qubits=qubits,
                                                    dilution=0.5,
                                                    beta=0.5)
    rho_est = estimate.estimate.state_point_est
    np.testing.assert_allclose(rho_true, rho_est, atol=0.01)
def test_maxent_single_qubit(single_q_tomo_fixture):
    qubits = [0]
    results, rho_true = single_q_tomo_fixture
    estimate, status = iterative_mle_state_estimate(results=results,
                                                    qubits=qubits,
                                                    dilution=0.5,
                                                    entropy_penalty=1.0)
    rho_est = estimate.estimate.state_point_est
    np.testing.assert_allclose(rho_true, rho_est, atol=0.01)
 def my_mle_estimator(_r, _q):
     return iterative_mle_state_estimate(results=_r,
                                         qubits=_q,
                                         dilution=0.5,
                                         entropy_penalty=0.0,
                                         beta=0.0)[0]