Exemple #1
0
def test_R_operator_with_hand_calc_example_1_qubit():
    # This example was worked out by hand
    rho = ID / 2
    obs_freqs = [3, 7]
    my_by_hand_calc_ans_Z = ((3 / 0.5) * PROJ_ZERO +
                             (7 / 0.5) * PROJ_ONE) / sum(obs_freqs)
    my_by_hand_calc_ans_X = ((3 / 0.5) * PROJ_PLUS +
                             (7 / 0.5) * PROJ_MINUS) / sum(obs_freqs)

    qubits = [Q0]
    exp = (obs_freqs[0] - obs_freqs[1]) / sum(obs_freqs)
    zplus_result = ExperimentResult(setting=Z_SETTING,
                                    expectation=exp,
                                    total_counts=sum(obs_freqs))
    xplus_result = ExperimentResult(setting=X_SETTING,
                                    expectation=exp,
                                    total_counts=sum(obs_freqs))

    z_results = [zplus_result]
    x_results = [xplus_result]

    # Z basis test
    np.testing.assert_allclose(_R(rho, z_results, qubits),
                               my_by_hand_calc_ans_Z,
                               atol=1e-12)
    # X basis test
    np.testing.assert_allclose(_R(rho, x_results, qubits),
                               my_by_hand_calc_ans_X,
                               atol=1e-12)
Exemple #2
0
def test_R_operator_fixed_point_1_qubit():
    # Check fixed point of operator. See Eq. 5 in Řeháček et al., PRA 75, 042108 (2007).
    qubits = [Q0]

    id_result = ExperimentResult(setting=ID_SETTING,
                                 expectation=1,
                                 total_counts=1)
    zplus_result = ExperimentResult(setting=Z_SETTING,
                                    expectation=1,
                                    total_counts=1)
    xplus_result = ExperimentResult(setting=X_SETTING,
                                    expectation=1,
                                    total_counts=1)

    z_results = [id_result, zplus_result]
    x_results = [id_result, xplus_result]

    def test_trace(rho, results):
        return _R(rho, results, qubits) @ rho @ _R(rho, results, qubits)

    np.testing.assert_allclose(test_trace(PROJ_ZERO, z_results),
                               PROJ_ZERO,
                               atol=1e-12)
    np.testing.assert_allclose(test_trace(PROJ_PLUS, x_results),
                               PROJ_PLUS,
                               atol=1e-12)
Exemple #3
0
def _resample_expectations_with_beta(results, prior_counts=1):
    """Resample expectation values by constructing a beta distribution and sampling from it.

    Used by :py:func:`estimate_variance`.

    :param results: A list of ExperimentResults
    :param prior_counts: Number of "counts" to add to alpha and beta for the beta distribution
        from which we sample.
    :return: A new list of ``results`` where each ExperimentResult's ``expectation`` field
        contained a resampled expectation value
    """
    resampled_results = []
    for result in results:
        # reconstruct the raw counts of observations from the pauli observable mean
        num_plus = ((result.expectation + 1) / 2) * result.total_counts
        num_minus = result.total_counts - num_plus

        # We resample this data assuming it was from a beta distribution,
        # with additive smoothing
        alpha = num_plus + prior_counts
        beta = num_minus + prior_counts

        # transform bit bias back to pauli expectation value
        resampled_expect = 2 * np.random.beta(alpha, beta) - 1
        resampled_results += [ExperimentResult(
            setting=result.setting,
            expectation=resampled_expect,
            std_err=result.std_err,
            total_counts=result.total_counts,
        )]
    return resampled_results
Exemple #4
0
def test_experiment_result():
    er = ExperimentResult(
        setting=ExperimentSetting(sX(0), sZ(0)),
        expectation=0.9,
        stddev=0.05,
    )
    assert str(er) == '(1+0j)*X0→(1+0j)*Z0: 0.9 +- 0.05'
Exemple #5
0
def test_experiment_result():
    er = ExperimentResult(
        setting=ExperimentSetting(plusX(0), sZ(0)),
        expectation=0.9,
        stddev=0.05,
        total_counts=100,
    )
    assert str(er) == 'X0_0→(1+0j)*Z0: 0.9 +- 0.05'
Exemple #6
0
def test_R_operator_fixed_point_2_qubit():
    # Check fixed point of operator. See Eq. 5 in Řeháček et al., PRA 75, 042108 (2007).
    qubits = [0, 1]
    id_setting = ExperimentSetting(in_state=zeros_state(qubits),
                                   out_operator=sI(qubits[0]) * sI(qubits[1]))
    zz_setting = ExperimentSetting(in_state=zeros_state(qubits),
                                   out_operator=sZ(qubits[0]) * sI(qubits[1]))

    id_result = ExperimentResult(setting=id_setting,
                                 expectation=1,
                                 total_counts=1)
    zzplus_result = ExperimentResult(setting=zz_setting,
                                     expectation=1,
                                     total_counts=1)

    zz_results = [id_result, zzplus_result]

    # Z basis test
    r = _R(P00, zz_results, qubits)
    actual = r @ P00 @ r
    np.testing.assert_allclose(actual, P00, atol=1e-12)
def wfn_measure_observables(n_qubits, tomo_expt: TomographyExperiment):
    if len(tomo_expt.program.defined_gates) > 0:
        raise pytest.skip("Can't do wfn on defined gates yet")
    wfn = NumpyWavefunctionSimulator(n_qubits)
    for settings in tomo_expt:
        for setting in settings:
            prog = Program()
            for oneq_state in setting.in_state.states:
                prog += _one_q_state_prep(oneq_state)
            prog += tomo_expt.program

            yield ExperimentResult(
                setting=setting,
                expectation=wfn.reset().do_program(prog).expectation(setting.out_operator),
                stddev=0.,
                total_counts=1,  # don't set to zero unless you want nans
            )