QAOA#

class iqm.qaoa.generic_qaoa.QAOA(problem, num_layers, *, betas=None, gammas=None, initial_angles=None)[source]#

Bases: ABC

The most generic QAOA abstract base class.

This abstract base class contains methods such as _internal_angle_logic() or linear_ramp_schedule() that can be used by any type / flavor of QAOA.

Parameters:
  • problem (ProblemInstance) – The ProblemInstance to be solved by the QAOA.

  • num_layers (int) – The number of QAOA layers.

  • betas (Sequence[float] | ndarray | None) – An optional list of the initial beta angles of QAOA. Has to be provided together with gammas.

  • gammas (Sequence[float] | ndarray | None) – An optional list of the initial gamma angles of QAOA. Has to be provided together with betas.

  • initial_angles (Sequence[float] | ndarray | None) – An optional list of the initial QAOA angles as one variable. Shouldn’t be provided together with either betas or gammas.

Attributes

angles

The angles in the QAOA, including betas and gammas in one ndarray.

betas

The beta angles in the QAOA, controlling the mixer Hamiltonian terms.

gammas

The gamma angles in the QAOA, controlling the problem Hamiltonian terms.

num_layers

The number of QAOA layers.

num_qubits

The number of qubits, equal to the number of problem variables if no special encoding is used.

problem

The problem instance associated with the QAOA.

trained

A boolean flag indicating whether the QAOA has been trained at all or not.

Methods

_internal_angle_logic([betas, gammas, ...])

Internal method to guarantee that angles are assigned correctly.

estimate(estimator)

The method for taking estimates of the expected value of the Hamiltonian from the QAOA circuit.

linear_ramp_schedule(delta_beta, delta_gamma)

The "linear ramp schedule" for setting the QAOA angles.

sample(sampler[, shots])

The method for taking samples (i.e., measurement results) from the QAOA circuit.

train()

The function that performs the training of the angles.

property trained: bool#

A boolean flag indicating whether the QAOA has been trained at all or not.

property num_layers: int#

The number of QAOA layers.

At first this is set to the value given at initialization, but it may be modified later (which has an effect on angles).

property problem: ProblemInstance#

The problem instance associated with the QAOA.

property num_qubits: int#

The number of qubits, equal to the number of problem variables if no special encoding is used.

property betas: ndarray#

The beta angles in the QAOA, controlling the mixer Hamiltonian terms.

property gammas: ndarray#

The gamma angles in the QAOA, controlling the problem Hamiltonian terms.

property angles: ndarray#

The angles in the QAOA, including betas and gammas in one ndarray.

sample(sampler, shots=20000)[source]#

The method for taking samples (i.e., measurement results) from the QAOA circuit.

Takes a SamplerBackend and uses it to get shots samples. The backend is responsible for building the quantum circuit and taking the measurements (or obtaining the samples some other way), using information from the QAOA object that is passed to its method sample().

Parameters:
  • sampler (SamplerBackend) – The sampler to use to generate samples. The sampler is an instance of a subclass of SamplerBackend with a sample() method of the appropriate signature.

  • shots (int) – The number of shots to be taken.

Returns:

A dictionary whose keys are bitstrings representing the samples and whose values are their respective frequencies, so that the sum of the values of the dictionary equals to shots.

Return type:

dict[str, int]

estimate(estimator)[source]#

The method for taking estimates of the expected value of the Hamiltonian from the QAOA circuit.

Takes a EstimatorBackend and uses it to get estimates of the expected value. The backend takes all the necessary information from the QAOA object that is passed to its method estimate().

Parameters:

estimator (EstimatorBackend) – The estimator used to get the expected value. The estimator is an instance of a subclass of EstimatorBackend with a method estimate() of the appropriate signature.

Returns:

An estimate of the expectation value fo the Hamiltonian. Not normalized in any way.

Return type:

float

linear_ramp_schedule(delta_beta, delta_gamma)[source]#

The “linear ramp schedule” for setting the QAOA angles.

Formulas adapted from [3]. It can be used either instead of training the QAOA or as a starting set of angles. The above work uses delta_beta and delta_gamma values around 0.5, but the best choice for these values depends on the problem Hamiltonian.

Parameters:
  • delta_beta (float) – The maximum beta angle.

  • delta_gamma (float) – The maximum gamma angle.

Return type:

None

abstract train()[source]#

The function that performs the training of the angles.

Return type:

None