QAOA#

Module: iqm.qaoa.generic_qaoa

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

Bases: ABC, Generic[ProblemType]

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 (ProblemType) – The problem to be solved by the QAOA, its type narrowed-down by the specific type of QAOA used.

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

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

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

  • initial_angles (Sequence[float] | np.ndarray | None) – An optional list of the initial QAOA angles as one variable. Shouldn’t be provided together with either betas or gammas. The gamma and beta angles are interleaved, so that the first pair of entries corresponds to \(\gamma_1\) and \(\beta_1\) (the angles of the first QAOA layer). The second pair of entries corresponds to \(\gamma_2\) and \(\beta_2\), etc. …

Raises:
  • TypeError – If the provided num_layers is not an integer.

  • ValueError – If the provided num_layers is not positive.

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

estimate

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

linear_ramp_schedule

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

sample

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: ProblemType#

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.

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

train(estimator=None, **kwargs)[source]#

The function that performs the training of the angles.

The training modifies angles in-place using the minimize() function from scipy. The training uses the provided estimator or a default one (implemented by QAOA’s subclasses).

Parameters:
  • estimator (EstimatorBackend | None) – An estimator EstimatorBackend to be used to calculate expectation values for the minimization. If None, a default one is picked, but this is depends on the QAOA subclass.

  • **kwargs (Any) – The keyword arguments to pass to the inner minimization scipy.optimize.minimize().

Return type:

None

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.

  • 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

Inheritance

Inheritance diagram of iqm.qaoa.generic_qaoa.QAOA