Exemplo n.º 1
0
def interpolate_qubit_operators(reference_qubit_operator,
                                target_qubit_operator,
                                epsilon=0.5):
    reference_qubit_operator = load_qubit_operator(reference_qubit_operator)
    target_qubit_operator = load_qubit_operator(target_qubit_operator)

    if epsilon > 1.0 or epsilon < 0.0:
        raise ValueError("epsilon must be in the range [0.0, 1.0]")

    output_qubit_operator = (epsilon * target_qubit_operator +
                             (1.0 - epsilon) * reference_qubit_operator)

    save_qubit_operator(output_qubit_operator, "qubit-operator.json")
Exemplo n.º 2
0
def evaluate_operator_for_parameter_grid(
    ansatz_specs,
    backend_specs,
    grid,
    operator,
    fixed_parameters="None",
):

    ansatz = create_object(json.loads(ansatz_specs))
    backend = create_object(json.loads(backend_specs))

    grid = load_parameter_grid(grid)
    operator = load_qubit_operator(operator)

    if fixed_parameters != "None":
        if type(fixed_parameters) == str:
            if os.path.exists(fixed_parameters):
                fixed_parameters = load_circuit_template_params(fixed_parameters)
    else:
        fixed_parameters = []

    (
        parameter_grid_evaluation,
        optimal_parameters,
    ) = _evaluate_operator_for_parameter_grid(
        ansatz, grid, backend, operator, previous_layer_params=fixed_parameters
    )

    save_parameter_grid_evaluation(
        parameter_grid_evaluation, "parameter-grid-evaluation.json"
    )
    save_circuit_template_params(optimal_parameters, "/app/optimal-parameters.json")
Exemplo n.º 3
0
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')
Exemplo n.º 4
0
def jw_get_ground_state_at_particle_number(particle_number, qubit_operator):
    qubit_operator = load_qubit_operator(qubit_operator)
    sparse_matrix = qubit_operator_sparse(qubit_operator)

    ground_energy, ground_state_amplitudes = _jw_get_ground_state_at_particle_number(
        sparse_matrix, particle_number
    )
    ground_state = Wavefunction(ground_state_amplitudes)
    value_estimate = ValueEstimate(ground_energy)

    save_wavefunction(ground_state, "ground-state.json")
    save_value_estimate(value_estimate, "value-estimate.json")
Exemplo n.º 5
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')
Exemplo n.º 6
0
def evaluate_ansatz_based_cost_function(
    ansatz_specs: str,
    backend_specs: str,
    cost_function_specs: str,
    ansatz_parameters: str,
    qubit_operator: str,
    noise_model="None",
    device_connectivity="None",
):
    ansatz_parameters = load_circuit_template_params(ansatz_parameters)
    # Load qubit op
    operator = load_qubit_operator(qubit_operator)
    ansatz_specs = json.loads(ansatz_specs)
    if ansatz_specs["function_name"] == "QAOAFarhiAnsatz":
        ansatz = create_object(ansatz_specs, cost_hamiltonian=operator)
    else:
        ansatz = create_object(ansatz_specs)

    backend_specs = json.loads(backend_specs)
    if noise_model != "None":
        backend_specs["noise_model"] = load_noise_model(noise_model)
    if device_connectivity != "None":
        backend_specs["device_connectivity"] = load_circuit_connectivity(
            device_connectivity)

    backend = create_object(backend_specs)
    cost_function_specs = json.loads(cost_function_specs)
    estimator_specs = cost_function_specs.pop("estimator-specs", None)
    if estimator_specs is not None:
        cost_function_specs["estimator"] = create_object(estimator_specs)
    cost_function_specs["target_operator"] = operator
    cost_function_specs["ansatz"] = ansatz
    cost_function_specs["backend"] = backend
    cost_function = create_object(cost_function_specs)

    value_estimate = cost_function(ansatz_parameters)

    save_value_estimate(value_estimate, "value_estimate.json")
Exemplo n.º 7
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")