Ejemplo n.º 1
0
def QCBMCostFunction(
    ansatz: Ansatz,
    backend: QuantumBackend,
    distance_measure: Callable,
    distance_measure_parameters: dict,
    target_bitstring_distribution: BitstringDistribution,
    gradient_type: str = "finite_difference",
):
    """Cost function used for evaluating QCBM.

    Args:
        ansatz (zquantum.core.interfaces.ansatz.Ansatz): the ansatz used to construct the variational circuits
        backend (zquantum.core.interfaces.backend.QuantumBackend): backend used for QCBM evaluation
        distance_measure (callable): function used to calculate the distance measure
        distance_measure_parameters (dict): dictionary containing the relevant parameters for the chosen distance measure
        target_bitstring_distribution (zquantum.core.bitstring_distribution.BitstringDistribution): bistring distribution which QCBM aims to learn
        gradient_type (str): parameter indicating which type of gradient should be used.

    Returns:
        Callable that evaluates the parametrized circuit produced by the ansatz with the given parameters and returns
            the distance between the produced bitstring distribution and the target distribution
    """

    assert (int(target_bitstring_distribution.get_qubits_number()) ==
            ansatz.number_of_qubits)

    def cost_function(parameters: np.ndarray,
                      store_artifact: StoreArtifact = None) -> ValueEstimate:
        """
        Evaluates the value of the cost function for given parameters.

        Args:
            parameters: parameters for which the evaluation should occur.

        Returns:
            (float): cost function value for given parameters
            zquantum.core.bitstring_distribution.BitstringDistribution: distribution obtained
        """
        circuit = ansatz.get_executable_circuit(parameters)
        distribution = backend.get_bitstring_distribution(circuit)
        value = evaluate_distribution_distance(
            target_bitstring_distribution,
            distribution,
            distance_measure,
            distance_measure_parameters=distance_measure_parameters,
        )

        if store_artifact:
            store_artifact("bitstring_distribution", distribution)

        return ValueEstimate(value)

    if gradient_type == "finite_difference":
        cost_function = function_with_gradient(
            cost_function, finite_differences_gradient(cost_function))
    else:
        raise RuntimeError("Unsupported gradient type: ", gradient_type)

    return cost_function
Ejemplo n.º 2
0
def test_adding_gradient_to_function_not_storing_artifacts_makes_a_callable_not_storing_artifacts(
):
    def _test_function(params):
        return (params**2).sum()

    function = function_with_gradient(
        _test_function, finite_differences_gradient(_test_function))
    assert not has_store_artifact_param(function)
Ejemplo n.º 3
0
def test_adding_gradient_to_function_storing_artifacts_makes_a_callable_that_stores_artifacts(
):
    def _test_function(params, store_artifact=None):
        if store_artifact:
            store_artifact("x", params[0])
        return (params**2).sum()

    function = function_with_gradient(
        _test_function, finite_differences_gradient(_test_function))
    assert has_store_artifact_param(function)
Ejemplo n.º 4
0
def QCBMCostFunction(
    ansatz: Ansatz,
    backend: QuantumBackend,
    n_samples: int,
    distance_measure: DistanceMeasure,
    distance_measure_parameters: dict,
    target_bitstring_distribution: BitstringDistribution,
    gradient_type: str = "finite_difference",
    gradient_kwargs: dict = None,
) -> CostFunction:
    """Cost function used for evaluating QCBM.

    Args:
        ansatz: the ansatz used to construct the variational circuits
        backend: backend used for QCBM evaluation
        distance_measure: function used to calculate the distance measure
        distance_measure_parameters: dictionary containing the relevant parameters for the chosen distance measure
        target_bitstring_distribution: bistring distribution which QCBM aims to learn
        gradient_type: parameter indicating which type of gradient should be used.

    Returns:
        Callable CostFunction object that evaluates the parametrized circuit produced by the ansatz with the given
        parameters and returns the distance between the produced bitstring distribution and the target distribution
    """

    warnings.warn(
        "QCBMCostFunction is deprecated in favour of create_QCBM_cost_function.",
        DeprecationWarning,
    )

    cost_function = _create_QCBM_cost_function(
        ansatz,
        backend,
        n_samples,
        distance_measure,
        distance_measure_parameters,
        target_bitstring_distribution,
    )

    if gradient_kwargs is None:
        gradient_kwargs = {}

    if gradient_type == "finite_difference":
        cost_function = function_with_gradient(
            cost_function,
            finite_differences_gradient(cost_function, **gradient_kwargs))
    else:
        raise RuntimeError("Unsupported gradient type: ", gradient_type)

    return cost_function
Ejemplo n.º 5
0
def create_QCBM_cost_function(
    ansatz: Ansatz,
    backend: QuantumBackend,
    n_samples: int,
    distance_measure: DistanceMeasure,
    distance_measure_parameters: dict,
    target_bitstring_distribution: BitstringDistribution,
    gradient_function: GradientFactory = finite_differences_gradient,
) -> CostFunction:
    """Cost function used for evaluating QCBM.

    Args:
        ansatz: the ansatz used to construct the variational circuits
        backend: backend used for QCBM evaluation
        distance_measure: function used to calculate the distance measure
        distance_measure_parameters: dictionary containing the relevant parameters for the chosen distance measure
        target_bitstring_distribution: bistring distribution which QCBM aims to learn
        gradient_function: a function which returns a function used to compute the gradient of the cost function
            (see zquantum.core.gradients.finite_differences_gradient for reference)

    Returns:
        Callable CostFunction object that evaluates the parametrized circuit produced by the ansatz with the given
        parameters and returns the distance between the produced bitstring distribution and the target distribution
    """

    cost_function = _create_QCBM_cost_function(
        ansatz,
        backend,
        n_samples,
        distance_measure,
        distance_measure_parameters,
        target_bitstring_distribution,
    )

    return function_with_gradient(cost_function,
                                  gradient_function(cost_function))
Ejemplo n.º 6
0
    def sum_x_squared(self):
        def _sum_x_squared(x):
            return sum(x**2)

        return function_with_gradient(
            _sum_x_squared, finite_differences_gradient(_sum_x_squared))