Beispiel #1
0
def get_expectation_values_for_qubit_operator(
    backend_specs: Specs,
    circuit: Union[str, Circuit, Dict],
    qubit_operator: Union[str, SymbolicOperator, Dict],
):
    """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)
    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 = create_object(backend_specs)

    expectation_values = backend.get_expectation_values(
        circuit, qubit_operator)
    save_expectation_values(expectation_values, "expectation-values.json")
Beispiel #2
0
def run_circuit_and_measure(
    backend_specs: Specs,
    circuit: Union[str, Dict],
    n_samples: Optional[int] = None,
    noise_model: Optional[str] = None,
    device_connectivity: Optional[str] = None,
):
    if isinstance(backend_specs, str):
        backend_specs = json.loads(backend_specs)
    if noise_model is not None:
        backend_specs["noise_model"] = load_noise_model(noise_model)
    if device_connectivity is not None:
        backend_specs["device_connectivity"] = load_circuit_connectivity(
            device_connectivity)

    backend = create_object(backend_specs)
    if isinstance(circuit, str):
        circuit = load_circuit(circuit)
    else:
        circuit = Circuit.from_dict(circuit)

    measurements = backend.run_circuit_and_measure(circuit,
                                                   n_samples=n_samples)
    measurements.save("measurements.json")