def test_save_nmeas_estimate(self):
     K_coeff = 0.5646124437984263
     nterms = 14
     frame_meas = np.array(
         [0.03362557, 0.03362557, 0.03362557, 0.03362557, 0.43011016])
     save_nmeas_estimate(
         nmeas=K_coeff,
         nterms=nterms,
         filename="hamiltonian_analysis.json",
         frame_meas=frame_meas,
     )
     K_coeff_, nterms_, frame_meas_ = load_nmeas_estimate(
         "hamiltonian_analysis.json")
     assert K_coeff == K_coeff_
     assert nterms == nterms_
     assert frame_meas.tolist() == frame_meas_.tolist()
     remove_file_if_exists("hamiltonian_analysis.json")
Example #2
0
def grouped_hamiltonian_analysis(
    groups: str,
    expectation_values: Optional[str] = None,
):
    """Calculates the number of measurements required for computing
    the expectation value of a qubit hamiltonian, where co-measurable terms
    are grouped as a list of QubitOperators.

    We are assuming the exact expectation values are provided
    (i.e. infinite number of measurements or simulations without noise)
    M ~ (\sum_{i} prec(H_i)) ** 2.0 / (epsilon ** 2.0)
    where prec(H_i) is the precision (square root of the variance)
    for each group of co-measurable terms H_{i}. It is computed as
    prec(H_{i}) = \sum{ab} |h_{a}^{i}||h_{b}^{i}| cov(O_{a}^{i}, O_{b}^{i})
    where h_{a}^{i} is the coefficient of the a-th operator, O_{a}^{i}, in the
    i-th group. Covariances are assumed to be zero for a != b:
    cov(O_{a}^{i}, O_{b}^{i}) = <O_{a}^{i} O_{b}^{i}> - <O_{a}^{i}> <O_{b}^{i}> = 0

    ARGS:
        groups (str): The name of a file containing a list of QubitOperator objects,
            where each element in the list is a group of co-measurable terms.
        expectation_values (str): The name of a file containing a single ExpectationValues
            object with all expectation values of the operators contained in groups.
            If absent, variances are assumed to be maximal, i.e. 1.
            NOTE: YOU HAVE TO MAKE SURE THAT THE ORDER OF EXPECTATION VALUES MATCHES
            THE ORDER OF THE TERMS IN THE *GROUPED* TARGET QUBIT OPERATOR,
            OTHERWISE THIS FUNCTION WILL NOT RETURN THE CORRECT RESULT.
    """

    grouped_operator = load_qubit_operator_set(groups)

    if expectation_values is not None:
        expecval = load_expectation_values(expectation_values)
    else:
        expecval = None

    K_coeff, nterms, frame_meas = estimate_nmeas_for_frames(
        grouped_operator, expecval)

    save_nmeas_estimate(
        nmeas=K_coeff,
        nterms=nterms,
        frame_meas=frame_meas,
        filename="hamiltonian_analysis.json",
    )
Example #3
0
def hamiltonian_analysis(
    qubit_operator: str,
    decomposition_method: str = "greedy",
    expectation_values: Optional[str] = None,
):
    operator = load_qubit_operator(qubit_operator)
    if expectation_values is not None:
        expecval = load_expectation_values(expectation_values)
    else:
        expecval = None

    K_coeff, nterms, frame_meas = estimate_nmeas_for_operator(
        operator, decomposition_method, expecval)
    save_nmeas_estimate(
        nmeas=K_coeff,
        nterms=nterms,
        frame_meas=frame_meas,
        filename="hamiltonian_analysis.json",
    )
Example #4
0
def hamiltonian_analysis(
    qubit_operator: str,
    decomposition_method: str = "greedy",
    expectation_values: str = "None",
):
    operator = load_qubit_operator(qubit_operator)
    if decomposition_method != "greedy-sorted" and decomposition_method != "greedy":
        raise ValueError(
            f'Decomposition method {decomposition_method} is not supported')
    if expectation_values != "None":
        expecval = load_expectation_values(expectation_values)
    else:
        expecval = None

    K_coeff, nterms, frame_meas = estimate_nmeas(operator,
                                                 decomposition_method,
                                                 expecval)
    save_nmeas_estimate(nmeas=K_coeff,
                        nterms=nterms,
                        frame_meas=frame_meas,
                        filename='hamiltonian_analysis.json')