qrunch.quantum.algorithms.callbacks.callbacks

Module with callbacks.

Functions

merge_parameter_dicts(dicts)

Merge a list of parameter-value dicts, keeping values from later dicts.

Classes

BeastToFastCircuitCallback

Convenience callback function that tracks the final operator and circuit.

BeastToFastCircuitProtocol

Define the minimal interface for 'beast-to-fast' circuit callbacks.

FinalOperatorCallback

Convenience callback function that tracks the final operator.

ObjectiveHistoryTracker

Convenience callback function that tracks sampler calls, adaptive iterations and objective function history.

class BeastToFastCircuitCallback

Bases: BeastToFastCircuitProtocol

Convenience callback function that tracks the final operator and circuit.

__init__() None

Initialize the final operator, circuit, and tracking containers.

Return type:

None

property estimated_value: ExpectationValue

Get the estimated value.

property final_parameters: dict[Parameter, float]

Get the final parameters.

property last_specified_circuit: Circuit

Get the latest specified minimized circuit.

record_adaptive_iteration(adaptive_iteration: int, unoptimized_circuit: Circuit, minimized_parameters: dict[Parameter, float], expectation_value: ExpectationValue, n_estimator_shots: int, operator: FermionHermitianSumProtocol | PairedHardcoreBosonHermitianSumProtocol | None = None) None

Call the callback function with the indicated parameters from the finished adaptive iteration.

Record the final operator.

Parameters:
Return type:

None

to_second_quantization_adaptive_vqe_result() SecondQuantizationAdaptiveVqeResult

Return the final result as a SecondQuantizationAdaptiveVqeResult.

Return type:

SecondQuantizationAdaptiveVqeResult

class BeastToFastCircuitProtocol

Bases: AdaptiveIterationCallback, Protocol

Define the minimal interface for ‘beast-to-fast’ circuit callbacks.

Provide access to the latest specified circuit, the running estimate history, and a conversion helper to a standard Adaptive VQE result object.

__init__(*args, **kwargs)
property estimated_value: ExpectationValue

Return the most recent expectation value estimate.

property last_specified_circuit: Circuit

Return the latest circuit with parameters specified.

Raise a ValueError if no circuit or parameters have been recorded yet.

record_adaptive_iteration(adaptive_iteration: int, unoptimized_circuit: Circuit, minimized_parameters: dict[Parameter, float], expectation_value: ExpectationValue, n_estimator_shots: int, operator: FermionHermitianSumProtocol | PairedHardcoreBosonHermitianSumProtocol | None = None) None

Call the callback function with the indicated parameters from the finished adaptive iteration.

Note that the adaptive_iteration integer starts at 0 and is incremented with 1 each iteration, except for when reminimizing, then a special value of -1 is given.

Parameters:
Return type:

None

to_second_quantization_adaptive_vqe_result() SecondQuantizationAdaptiveVqeResult

Build a SecondQuantizationAdaptiveVqeResult from the recorded state.

Return type:

SecondQuantizationAdaptiveVqeResult

class FinalOperatorCallback

Bases: AdaptiveIterationCallback

Convenience callback function that tracks the final operator.

__init__() None

Initialize the final_operator.

Return type:

None

record_adaptive_iteration(adaptive_iteration: int, unoptimized_circuit: Circuit, minimized_parameters: dict[Parameter, float], expectation_value: ExpectationValue, n_estimator_shots: int, operator: FermionHermitianSumProtocol | PairedHardcoreBosonHermitianSumProtocol | None = None) None

Call the callback function with the indicated parameters from the finished adaptive iteration.

Record the final operator.

Parameters:
Return type:

None

class ObjectiveHistoryTracker

Bases: AdaptiveIterationCallback

Convenience callback function that tracks sampler calls, adaptive iterations and objective function history.

__init__() None

Initialize the history.

Return type:

None

property last_specified_circuit: Circuit

Get the latest specified minimized circuit.

property minimized_circuits: list[Circuit]

Circuit ansatz with the optimal parameters.

record_adaptive_iteration(adaptive_iteration: int, unoptimized_circuit: Circuit, minimized_parameters: dict[Parameter, float], expectation_value: ExpectationValue, n_estimator_shots: int, operator: FermionHermitianSumProtocol | PairedHardcoreBosonHermitianSumProtocol | None = None) None

Call the callback function with the indicated parameters from the finished adaptive iteration.

Record the objective value at each adaptive iteration.

Note that the adaptive_iteration integer starts at 0 and is incremented with 1 each iteration, except for when reminimizing, then a special value of -1 is given.

Parameters:
Return type:

None

merge_parameter_dicts(dicts: list[dict[Parameter, float]]) dict[Parameter, float]

Merge a list of parameter-value dicts, keeping values from later dicts.

Parameters:

dicts (list[dict[Parameter, float]])

Return type:

dict[Parameter, float]