Example #1
0
def get_expectation_values_for_qubit_operator(
    backend_specs: Specs,
    circuit: Union[str, Circuit],
    qubit_operator: Union[str, SymbolicOperator],
):
    """Measure the expection values of the terms in an input operator with respect to the state prepared by the input
    circuit on the backend described by the backend_specs. The results are serialized into a JSON under the
    file: "expectation-values.json"

    ARGS:
        backend_specs (Union[dict, str]): The backend on which to run the quantum circuit
        circuit (Union[str, Circuit]): The circuit that prepares the state to be measured
        qubit_operator (Union[str, SymbolicOperator]): The operator to measure
    """
    if isinstance(circuit, str):
        circuit = load_circuit(circuit)
    if isinstance(qubit_operator, str):
        qubit_operator = load_qubit_operator(qubit_operator)
    if isinstance(backend_specs, str):
        backend_specs = json.loads(backend_specs)
    backend = create_object(backend_specs)

    expectation_values = backend.get_expectation_values(
        circuit, qubit_operator)
    save_expectation_values(expectation_values, "expectation-values.json")
def get_expectation_values_for_qubit_operator(
    backend_specs: Specs,
    circuit: Union[str, Circuit, Dict],
    qubit_operator: Union[str, SymbolicOperator, Dict],
):
    """Measure the expectation values of the terms in an input operator with respect to
    the state prepared by the input circuit on the backend described by the
    `backend_specs`. The results are serialized into a JSON under the file:
    "expectation-values.json"

    Args:
        backend_specs: The backend on which to run the quantum circuit
        circuit: The circuit that prepares the state to be measured
        qubit_operator: The operator to measure
    """
    if isinstance(circuit, str):
        circuit = load_circuit(circuit)
    elif isinstance(circuit, dict):
        circuit = circuit_from_dict(circuit)
    if isinstance(qubit_operator, str):
        qubit_operator = load_qubit_operator(qubit_operator)
    elif isinstance(qubit_operator, dict):
        qubit_operator = convert_dict_to_qubitop(qubit_operator)
    if isinstance(backend_specs, str):
        backend_specs = json.loads(backend_specs)
    backend = cast(QuantumBackend, create_object(backend_specs))

    estimation_tasks = [
        EstimationTask(qubit_operator, circuit, backend.n_samples)
    ]

    expectation_values = estimate_expectation_values_by_averaging(
        backend, estimation_tasks)

    save_expectation_values(expectation_values[0], "expectation-values.json")
def expectation_values_from_rdms(
    interactionrdm: str,
    qubit_operator: str,
    sort_terms: bool = False,
):
    operator = load_qubit_operator(qubit_operator)
    rdms = load_interaction_rdm(interactionrdm)
    expecval = get_expectation_values_from_rdms(rdms, operator, sort_terms=sort_terms)
    save_expectation_values(expecval, "expectation_values.json")
def test_expectation_values_io():
    expectation_values = np.array([0.0, 0.0, -1.0])
    correlations = []
    correlations.append(np.array([[1.0, -1.0], [-1.0, 1.0]]))
    correlations.append(np.array([[1.0]]))

    estimator_covariances = []
    estimator_covariances.append(np.array([[0.1, -0.1], [-0.1, 0.1]]))
    estimator_covariances.append(np.array([[0.1]]))

    expectation_values_object = ExpectationValues(expectation_values,
                                                  correlations,
                                                  estimator_covariances)

    save_expectation_values(expectation_values_object,
                            "expectation_values.json")
    expectation_values_object_loaded = load_expectation_values(
        "expectation_values.json")

    assert np.allclose(
        expectation_values_object.values,
        expectation_values_object_loaded.values,
    )
    assert len(expectation_values_object.correlations) == len(
        expectation_values_object_loaded.correlations)
    assert len(expectation_values_object.estimator_covariances) == len(
        expectation_values_object_loaded.estimator_covariances)
    for i in range(len(expectation_values_object.correlations)):
        assert np.allclose(
            expectation_values_object.correlations[i],
            expectation_values_object_loaded.correlations[i],
        )
    for i in range(len(expectation_values_object.estimator_covariances)):
        assert np.allclose(
            expectation_values_object.estimator_covariances[i],
            expectation_values_object_loaded.estimator_covariances[i],
        )

    remove_file_if_exists("expectation_values.json")
Example #5
0
def expectation_values_from_rdms_for_qubitoperator_list(
        interactionrdm: str,
        qubit_operator_list: str,
        sort_terms: bool = False):
    """Computes expectation values of Pauli strings in a list of QubitOperator given a
       fermionic InteractionRDM from OpenFermion. All the expectation values for the
       operators in the list are written to file in a single ExpectationValues object in the
       same order the operators came in.

    ARGS:
        interactionrdm (str): The name of the file containing the interaction RDM to
            use for the expectation values computation
        qubitoperator_list (str): The name of the file containing the list of qubit operators
            to compute the expectation values for in the form of OpenFermion QubitOperator objects
        sort_terms (bool): whether or not each input qubit operator needs to be sorted before
            calculating expectations
    """

    operator_list = load_qubit_operator_set(qubit_operator_list)
    rdms = load_interaction_rdm(interactionrdm)
    expecval = get_expectation_values_from_rdms_for_qubitoperator_list(
        rdms, operator_list, sort_terms=sort_terms)
    save_expectation_values(expecval, "expectation_values.json")
Example #6
0
def get_expectation_values_for_qubit_operator(backend_specs, circuit, qubit_operator):
    circuit = load_circuit(circuit)
    qubit_operator = load_qubit_operator(qubit_operator)
    backend = create_object(json.loads(backend_specs))
    expectation_values = backend.get_expectation_values(circuit, qubit_operator)
    save_expectation_values(expectation_values, "expectation-values.json")