QAOA Parametrisations¶

We currently offer 7 different parametrisations for QAOA, which can be found in the entropica_qaoa.qaoa.parameters module. They fall broadly into three categories: The Standard classes are parametrisations that have the $$\gamma$$ ‘s and $$\beta$$ ‘s as free parameters, as defined in the seminal paper by Farhi et al in A Quantum Approximate Optimization Algorithm. The Fourier classes have the discrete cosine and sine transforms of the $$\gamma$$ ‘s respective $$\beta$$’s as free parameters, as proposed by Zhou et al. in Quantum Approximate Optimization Algorithm: Performance, Mechanism, and Implementation on Near-Term Devices. Finally, the Annealing class is based on the idea of QAOA being a form of discretised, adiabatic annealing. Here the function values $$s(t_i)$$ at equally spaced times $$t_i$$ are the free parameters.

Except for the Annealing parameters, each class also comes in three levels of detail: StandardParams and FourierParams offer the $$\gamma$$ ‘s and $$\beta$$’s as proposed in above papers. StandardWithBiasParams and FourierWithBiasParams allow for extra $$\gamma$$’s for possible single-qubit bias terms, resp. their discrete sine transform. Lastly, ExtendedParams and FourierExtendedParams offer full control by having a seperate set of rotation angles for each term in the cost and mixer Hamiltonians, respective having a seperate set of Fourier coefficients for each term.

You can always convert parametrisations with fewer degrees of freedom to ones with more using the .from_other_parameters() classmethod. The full type tree is shown below, where the arrows mark possible conversions:

   ExtendedParams   <--------- FourierExtendedParams
^                         ^
|                         |
StandardWithBiasParams <------ FourierWithBiasParams
^                         ^
|                         |
StandardParams  <----------- FourierParams
^
|
AnnealingParams


Standard Parameters¶

class qaoa.parameters.StandardParams(hyperparameters, parameters)

QAOA parameters that implement a state preparation circuit with

$e^{-i \beta_p H_0} e^{-i \gamma_p H_c} \cdots e^{-i \beta_0 H_0} e^{-i \gamma_0 H_c}$

This corresponds to the parametrization used by Farhi in his original paper [https://arxiv.org/abs/1411.4028]

Variables
• betas (np.array) – 1D array with the betas from above

• gammas (np.array) – 1D array with the gamma from above

Parameters
• hyperparameters (Tuple[PauliSum, int]) – The hyperparameters containing the hamiltonian and the number of steps hyperparameters = (hamiltonian, n_steps)

• parameters (Tuple) – Tuple containing (betas, gammas) with dimensions (n_steps, n_steps)

classmethod empty(hyperparameters)

Alternative to __init__ that only takes hyperparameters and fills parameters via np.empty

Parameters

hyperparameters – Same as the hyperparameters argument for cls.__init__()

Returns

A Parameter object with the parameters filled by np.empty

Return type

Type[AbstractParams]

classmethod from_AbstractParameters(abstract_params, parameters)

Alternative to __init__ that takes hyperparameters from an existing AbstractQAOAParameter object.

Create a ConcreteQAOAParameters instance from an AbstractQAOAParameters instance with hyperparameters from abstract_params and normal parameters from parameters

Parameters
• abstract_params (AbstractParams) – An AbstractQAOAParameters instance to which to add the parameters

• parameter – A tuple containing the parameters. Must be the same as in __init__

Returns

A StandardParams object with the hyperparameters taken from abstract_params and the normal parameters from parameters

Return type

StandardParams

classmethod from_other_parameters(params)

Alternative to __init__ that takes parameters with less degrees of freedom as the input.

Parameters

params (Type[AbstractParams]) – The input parameters

Returns

The converted paramters s.t. all the rotation angles of the in and output parameters are the same.

Return type

Type[AbstractParams]

Note

If all conversion functions for your parametrization are in qaoa._parameter_conversions.py and correctly registered for converter you don’t need to override this function in its child classes

classmethod linear_ramp_from_hamiltonian(hamiltonian, n_steps, time=None)

Alternative to __init__ that already fills parameters.

Calculate initial parameters from a hamiltonian corresponding to a linear ramp annealing schedule and return a QAOAParams object.

Parameters
• hamiltonian (PauliSum) – hamiltonian for which to calculate the initial QAOA parameters.

• n_steps (int) – Number of timesteps.

• time (Optional[float]) – Total annealing time. Defaults to 0.7*n_steps.

Returns

A StandardParams object with parameters according to a linear ramp schedule for hamiltonian

Return type

StandardParams

plot(ax=None, **kwargs)

Plots self in a sensible way to the canvas ax, if provided.

Parameters
• ax (matplotlib.axes._subplots.AxesSubplot) – The canvas to plot itself on

• kwargs – All remaining keyword arguments are passed forward to the plot function

raw()

Return the parameters in a 1D array.

This 1D array is needed by scipy.optimize.minimize which expects the parameters that need to be optimized to be a 1D array.

Returns

The parameters in a 1D array. Has the same output format as the expected input of self.update_from_raw. Hence corresponds to the flattened parameters in __init__()

Return type

np.array

raw_rotation_angles()

Flat array of the rotation angles for the memory map for the parametric circuit.

Returns

Returns all single rotation angles in the ordering (x_rotation_angles, gamma_singles, zz_rotation_angles) where x_rotation_angles = (beta_q0_t0, beta_q1_t0, ... , beta_qn_tp) and the same for z_rotation_angles and zz_rotation_angles

Return type

np.array

update_from_raw(new_values)

Update all the parameters from a 1D array.

The input has the same format as the output of self.raw(). This is useful for scipy.optimize.minimize which expects the parameters that need to be optimized to be a 1D array.

Parameters

new_values – A 1D array with the new parameters. Must have length len(self) and the ordering of the flattend parameters in __init__().

property x_rotation_angles

2D array with the X-rotation angles.

1st index goes over n_steps and the 2nd index over the qubits to apply X-rotations on. These are needed by qaoa.cost_function.make_qaoa_memory_map

property z_rotation_angles

2D array with the ZZ-rotation angles.

1st index goes over the n_steps and the 2nd index over the qubit pairs, to apply ZZ-rotations on. These are needed by qaoa.cost_function.make_qaoa_memory_map

property zz_rotation_angles

2D array with Z-rotation angles.

1st index goes over the n_steps and the 2nd index over the qubit pairs, to apply Z-rotations on. These are needed by qaoa.cost_function.make_qaoa_memory_map

class qaoa.parameters.StandardWithBiasParams(hyperparameters, parameters)

QAOA parameters that implement a state preparation circuit with

$e^{-i \beta_p H_0} e^{-i \gamma_{\textrm{singles}, p} H_{c, \textrm{singles}}} e^{-i \gamma_{\textrm{pairs}, p} H_{c, \textrm{pairs}}} \cdots e^{-i \beta_0 H_0} e^{-i \gamma_{\textrm{singles}, 0} H_{c, \textrm{singles}}} e^{-i \gamma_{\textrm{pairs}, 0} H_{c, \textrm{pairs}}}$

where the cost hamiltonian is split into $$H_{c, \textrm{singles}}$$ the bias terms, that act on only one qubit, and $$H_{c, \textrm{pairs}}$$ the coupling terms, that act on two qubits.

Variables
• betas (np.array) – A 1D array containing the betas from above for each timestep

• gammas_pairs (np.array) – A 1D array containing the gammas_singles from above for each timestep

• gammas_singles (np.array) – A 1D array containing the gammas_pairs from above for each timestep

Parameters
• hyperparameters (Tuple[PauliSum, int]) – The hyperparameters containing the hamiltonian and the number of steps hyperparameters = (hamiltonian, n_steps)

• parameters (Tuple) – Tuple containing (betas, gammas_singles, gammas_pairs) with dimensions (n_steps, n_steps, n_steps)

classmethod empty(hyperparameters)

Alternative to __init__ that only takes hyperparameters and fills parameters via np.empty

Parameters

hyperparameters – Same as the hyperparameters argument for cls.__init__()

Returns

A Parameter object with the parameters filled by np.empty

Return type

Type[AbstractParams]

classmethod from_AbstractParameters(abstract_params, parameters)

Alternative to __init__ that takes hyperparameters from an existing AbstractQAOAParameter object.

Create a ConcreteQAOAParameters instance from an AbstractQAOAParameters instance with hyperparameters from abstract_params and normal parameters from parameters

Parameters
• abstract_params (AbstractParams) – An AbstractQAOAParameters instance to which to add the parameters

• parameter – A tuple containing the parameters. Must be the same as in __init__

Returns

An AlternatatingOperatorsQAOAParameters object with the hyperparameters taken from abstract_params and the normal parameters from parameters

Return type

StandardWithBiasParams

classmethod from_other_parameters(params)

Alternative to __init__ that takes parameters with less degrees of freedom as the input.

Parameters

params (Type[AbstractParams]) – The input parameters

Returns

The converted paramters s.t. all the rotation angles of the in and output parameters are the same.

Return type

Type[AbstractParams]

Note

If all conversion functions for your parametrization are in qaoa._parameter_conversions.py and correctly registered for converter you don’t need to override this function in its child classes

classmethod linear_ramp_from_hamiltonian(hamiltonian, n_steps, time=None)

Alternative to __init__ that already fills parameters.

Calculate initial parameters from a hamiltonian corresponding to a linear ramp annealing schedule and return a QAOAParams object.

Parameters
• hamiltonian (PauliSum) – hamiltonian for which to calculate the initial QAOA parameters.

• n_steps (int) – Number of timesteps.

• time (Optional[float]) – Total annealing time. Defaults to 0.7*n_steps.

Returns

An AlternatingOperatorsQAOAParameters object holding all the parameters

Return type

StandardWithBiasParams

plot(ax=None, **kwargs)

Plots self in a sensible way to the canvas ax, if provided.

Parameters
• ax (matplotlib.axes._subplots.AxesSubplot) – The canvas to plot itself on

• kwargs – All remaining keyword arguments are passed forward to the plot function

raw()

Return the parameters in a 1D array.

This 1D array is needed by scipy.optimize.minimize which expects the parameters that need to be optimized to be a 1D array.

Returns

The parameters in a 1D array. Has the same output format as the expected input of self.update_from_raw. Hence corresponds to the flattened parameters in __init__()

Return type

np.array

raw_rotation_angles()

Flat array of the rotation angles for the memory map for the parametric circuit.

Returns

Returns all single rotation angles in the ordering (x_rotation_angles, gamma_singles, zz_rotation_angles) where x_rotation_angles = (beta_q0_t0, beta_q1_t0, ... , beta_qn_tp) and the same for z_rotation_angles and zz_rotation_angles

Return type

np.array

update_from_raw(new_values)

Update all the parameters from a 1D array.

The input has the same format as the output of self.raw(). This is useful for scipy.optimize.minimize which expects the parameters that need to be optimized to be a 1D array.

Parameters

new_values – A 1D array with the new parameters. Must have length len(self) and the ordering of the flattend parameters in __init__().

property x_rotation_angles

2D array with the X-rotation angles.

1st index goes over n_steps and the 2nd index over the qubits to apply X-rotations on. These are needed by qaoa.cost_function.make_qaoa_memory_map

property z_rotation_angles

2D array with the ZZ-rotation angles.

1st index goes over the n_steps and the 2nd index over the qubit pairs, to apply ZZ-rotations on. These are needed by qaoa.cost_function.make_qaoa_memory_map

property zz_rotation_angles

2D array with Z-rotation angles.

1st index goes over the n_steps and the 2nd index over the qubit pairs, to apply Z-rotations on. These are needed by qaoa.cost_function.make_qaoa_memory_map

class qaoa.parameters.ExtendedParams(hyperparameters, parameters)

QAOA parameters in their most general form with different angles for each operator.

This means, that at the i-th timestep the evolution hamiltonian is given by

$H(t_i) = \sum_{\textrm{qubits } j} \beta_{ij} X_j + \sum_{\textrm{qubits } j} \gamma_{\textrm{single } ij} Z_j + \sum_{\textrm{qubit pairs} (jk)} \gamma_{\textrm{pair } i(jk)} Z_j Z_k$

and the complete circuit is then

$U = e^{-i H(t_p)} \cdots e^{-iH(t_1)}.$
Variables
• betas (np.array) – 2D array with the gammas from above for each timestep and qubit. 1st index goes over the timesteps, 2nd over the qubits.

• gammas_pairs (np.array) – 2D array with the gammas_pairs from above for each timestep and coupled pair. 1st index goes over timesteps, 2nd over pairs.

• gammas_singles (np.array) – 2D array with the gammas_singles from above for each timestep and bias term. 1st index goes over timesteps, 2nd over bias terms.

Parameters
• hyperparameters (Tuple[PauliSum, int]) – The hyperparameters containing the hamiltonian and the number of steps hyperparameters = (hamiltonian, n_steps)

• parameters (Tuple) – Tuple containing (betas, gammas_singles, gammas_pairs) with dimensions ((n_steps x nqubits), (n_steps x nsingle_terms), (n_steps x npair_terms))

classmethod empty(hyperparameters)

Alternative to __init__ that only takes hyperparameters and fills parameters via np.empty

Parameters

hyperparameters – Same as the hyperparameters argument for cls.__init__()

Returns

A Parameter object with the parameters filled by np.empty

Return type

Type[AbstractParams]

classmethod from_AbstractParameters(abstract_params, parameters)

Alternative to __init__ that takes hyperparameters from an existing AbstractQAOAParameter object.

Create a ConcreteQAOAParameters instance from an AbstractQAOAParameters instance with hyperparameters from abstract_params and normal parameters from parameters

Parameters
• abstract_params (AbstractParams) – An AbstractQAOAParameters instance to which to add the parameters

• parameter – A tuple containing the parameters. Must be the same as in __init__

Returns

A GeneralQAOAParameters object with the hyperparameters taken from abstract_params and the normal parameters from parameters

Return type

ExtendedParams

classmethod from_other_parameters(params)

Alternative to __init__ that takes parameters with less degrees of freedom as the input.

Parameters

params (Type[AbstractParams]) – The input parameters

Returns

The converted paramters s.t. all the rotation angles of the in and output parameters are the same.

Return type

Type[AbstractParams]

Note

If all conversion functions for your parametrization are in qaoa._parameter_conversions.py and correctly registered for converter you don’t need to override this function in its child classes

get_constraints()

Constraints on the parameters for constrained parameters.

Returns

A list of tuples (0, upper_boundary) of constraints on the parameters s.t. we are exploiting the periodicity of the cost function. Useful for constrained optimizers.

Return type

List[Tuple]

classmethod linear_ramp_from_hamiltonian(hamiltonian, n_steps, time=None)

Alternative to __init__ that already fills parameters.

Calculate initial parameters from a hamiltonian corresponding to a linear ramp annealing schedule and return a QAOAParams object.

Parameters
• hamiltonian (PauliSum) – hamiltonian for which to calculate the initial QAOA parameters.

• n_steps (int) – Number of timesteps.

• time (Optional[float]) – Total annealing time. Defaults to 0.7*n_steps.

Returns

The initial parameters according to a linear ramp for hamiltonian.

Return type

ExtendedParams

Todo

Refactor this s.t. it supers from __init__

plot(ax=None, **kwargs)

Plots self in a sensible way to the canvas ax, if provided.

Parameters
• ax (matplotlib.axes._subplots.AxesSubplot) – The canvas to plot itself on

• kwargs – All remaining keyword arguments are passed forward to the plot function

raw()

Return the parameters in a 1D array.

This 1D array is needed by scipy.optimize.minimize which expects the parameters that need to be optimized to be a 1D array.

Returns

The parameters in a 1D array. Has the same output format as the expected input of self.update_from_raw. Hence corresponds to the flattened parameters in __init__()

Return type

np.array

raw_rotation_angles()

Flat array of the rotation angles for the memory map for the parametric circuit.

Returns

Returns all single rotation angles in the ordering (x_rotation_angles, gamma_singles, zz_rotation_angles) where x_rotation_angles = (beta_q0_t0, beta_q1_t0, ... , beta_qn_tp) and the same for z_rotation_angles and zz_rotation_angles

Return type

np.array

update_from_raw(new_values)

Update all the parameters from a 1D array.

The input has the same format as the output of self.raw(). This is useful for scipy.optimize.minimize which expects the parameters that need to be optimized to be a 1D array.

Parameters

new_values – A 1D array with the new parameters. Must have length len(self) and the ordering of the flattend parameters in __init__().

property x_rotation_angles

2D array with the X-rotation angles.

1st index goes over n_steps and the 2nd index over the qubits to apply X-rotations on. These are needed by qaoa.cost_function.make_qaoa_memory_map

property z_rotation_angles

2D array with the ZZ-rotation angles.

1st index goes over the n_steps and the 2nd index over the qubit pairs, to apply ZZ-rotations on. These are needed by qaoa.cost_function.make_qaoa_memory_map

property zz_rotation_angles

2D array with Z-rotation angles.

1st index goes over the n_steps and the 2nd index over the qubit pairs, to apply Z-rotations on. These are needed by qaoa.cost_function.make_qaoa_memory_map

Fourier Parameters¶

class qaoa.parameters.FourierParams(hyperparameters, parameters)

The QAOA parameters as the sine/cosine transform of the original gammas and x_rotation_angles. See “Quantum Approximate Optimization Algorithm: Performance, Mechanism, and Implementation on Near-Term Devices” [https://arxiv.org/abs/1812.01041] for a detailed description.

Variables
• q (int) – The number of coefficients for the discrete sine and cosine transforms below

• u (np.array) – The discrete sine transform of the gammas in StandardParams

• v (np.array) – The discrete cosine transform of the betas in StandardParams

Parameters
• hyperparameters (Tuple[PauliSum, int, float]) – The hyperparameters containing the hamiltonian, the number of steps and the total annealing time hyperparameters = (hamiltonian, n_steps, q). q is the number of fourier coefficients. For q == n_steps we have the full expressivity of StandardParams. More are redundant.

• parameters (Tuple) – Tuple containing (v, u) with dimensions (q, q)

classmethod empty(hyperparameters)

Alternative to __init__ that only takes hyperparameters and fills parameters via np.empty

Parameters

hyperparameters – Same as the hyperparameters argument for cls.__init__()

Returns

A Parameter object with the parameters filled by np.empty

Return type

Type[AbstractParams]

classmethod from_AbstractParameters(abstract_params, parameters, q=4)

Alternative to __init__ that takes hyperparameters from an existing AbstractQAOAParameter object.

Create a ConcreteQAOAParameters instance from an AbstractQAOAParameters instance with hyperparameters from abstract_params and normal parameters from parameters

Parameters
• abstract_params (AbstractParams) – An AbstractQAOAParameters instance to which to add the parameters

• parameters (Tuple) – Same as parameters in .__init__()

• q (int) – Number of fourier coefficients. Defaults to 4

Returns

A FourierParams object with the hyperparameters taken from abstract_params and the normal parameters from parameters

Return type

FourierParams

classmethod from_other_parameters(params)

Alternative to __init__ that takes parameters with less degrees of freedom as the input.

Parameters

params (Type[AbstractParams]) – The input parameters

Returns

The converted paramters s.t. all the rotation angles of the in and output parameters are the same.

Return type

Type[AbstractParams]

Note

If all conversion functions for your parametrization are in qaoa._parameter_conversions.py and correctly registered for converter you don’t need to override this function in its child classes

classmethod linear_ramp_from_hamiltonian(hamiltonian, n_steps, q=4, time=None)

NOTE: rather than implement an exact linear schedule, this instead implements the lowest frequency component, i.e. a sine curve for gammas, and a cosine for betas.

Parameters
• hamiltonian (PauliSum) – The hamiltonian

• n_steps (int) – number of timesteps

• q (int) – Number of Fourier coefficients. Defaults to 4

• time (Optional[float]) – total time. Set to 0.7*n_steps if None is passed.

Returns

A FourierParams object with initial parameters corresponding to a the 0th order Fourier component (a sine curve for gammas, cosine for betas)

Return type

FourierParams

Todo

Make a more informed choice of the default value for q. Probably depending on n_qubits

plot(ax=None, **kwargs)

Plots self in a sensible way to the canvas ax, if provided.

Parameters
• ax (matplotlib.axes._subplots.AxesSubplot) – The canvas to plot itself on

• kwargs – All remaining keyword arguments are passed forward to the plot function

raw()

Return the parameters in a 1D array.

This 1D array is needed by scipy.optimize.minimize which expects the parameters that need to be optimized to be a 1D array.

Returns

The parameters in a 1D array. Has the same output format as the expected input of self.update_from_raw. Hence corresponds to the flattened parameters in __init__()

Return type

np.array

raw_rotation_angles()

Flat array of the rotation angles for the memory map for the parametric circuit.

Returns

Returns all single rotation angles in the ordering (x_rotation_angles, gamma_singles, zz_rotation_angles) where x_rotation_angles = (beta_q0_t0, beta_q1_t0, ... , beta_qn_tp) and the same for z_rotation_angles and zz_rotation_angles

Return type

np.array

update_from_raw(new_values)

Update all the parameters from a 1D array.

The input has the same format as the output of self.raw(). This is useful for scipy.optimize.minimize which expects the parameters that need to be optimized to be a 1D array.

Parameters

new_values – A 1D array with the new parameters. Must have length len(self) and the ordering of the flattend parameters in __init__().

property x_rotation_angles

2D array with the X-rotation angles.

1st index goes over n_steps and the 2nd index over the qubits to apply X-rotations on. These are needed by qaoa.cost_function.make_qaoa_memory_map

property z_rotation_angles

2D array with the ZZ-rotation angles.

1st index goes over the n_steps and the 2nd index over the qubit pairs, to apply ZZ-rotations on. These are needed by qaoa.cost_function.make_qaoa_memory_map

property zz_rotation_angles

2D array with Z-rotation angles.

1st index goes over the n_steps and the 2nd index over the qubit pairs, to apply Z-rotations on. These are needed by qaoa.cost_function.make_qaoa_memory_map

class qaoa.parameters.FourierWithBiasParams(hyperparameters, parameters)

The QAOA parameters as the sine/cosine transform of the original gammas and x_rotation_angles. See “Quantum Approximate Optimization Algorithm: Performance, Mechanism, and Implementation on Near-Term Devices” [https://arxiv.org/abs/1812.01041] for a detailed description.

Variables
• q (int) – The number of coefficients for the discrete sine and cosine transforms below

• u_pairs (np.array) – The discrete sine transform of the gammas_pairs in StandardWithBiasParams

• u_singles (np.array) – The discrete sine transform of the gammas_singles in StandardWithBiasParams

• v (np.array) – The discrete cosine transform of the betas in StandardWithBiasParams

Parameters
• hyperparameters (Tuple[PauliSum, int, float]) – The hyperparameters containing the hamiltonian, the number of steps and the total annealing time hyperparameters = (hamiltonian, n_steps, q). q is the number of fourier coefficients. For q == n_steps we have the full expressivity of StandardWithBiasParams. More are redundant.

• parameters (Tuple) – Tuple containing (v, u_singles, u_pairs) with dimensions (q, q, q)

classmethod empty(hyperparameters)

Alternative to __init__ that only takes hyperparameters and fills parameters via np.empty

Parameters

hyperparameters – Same as the hyperparameters argument for cls.__init__()

Returns

A Parameter object with the parameters filled by np.empty

Return type

Type[AbstractParams]

classmethod from_AbstractParameters(abstract_params, parameters, q=4)

Alternative to __init__ that takes hyperparameters from an existing AbstractQAOAParameter object.

Create a ConcreteQAOAParameters instance from an AbstractQAOAParameters instance with hyperparameters from abstract_params and normal parameters from parameters

Parameters
• abstract_params (AbstractParams) – An AbstractQAOAParameters instance to which to add the parameters

• parameters (Tuple) – Same as parameters in .__init__()

• q (int) – Number of fourier coefficients. Defaults to 4

Returns

A FourierWithBiasParams object with the hyperparameters taken from abstract_params and the normal parameters from parameters

Return type

FourierWithBiasParams

classmethod from_other_parameters(params)

Alternative to __init__ that takes parameters with less degrees of freedom as the input.

Parameters

params (Type[AbstractParams]) – The input parameters

Returns

The converted paramters s.t. all the rotation angles of the in and output parameters are the same.

Return type

Type[AbstractParams]

Note

If all conversion functions for your parametrization are in qaoa._parameter_conversions.py and correctly registered for converter you don’t need to override this function in its child classes

classmethod linear_ramp_from_hamiltonian(hamiltonian, n_steps, q=4, time=None)

Alternative to __init__ that already fills parameters.

Calculate initial parameters from a hamiltonian corresponding to a linear ramp annealing schedule and return a QAOAParams object.

Parameters
• hamiltonian (PauliSum) – The hamiltonian

• n_steps (int) – number of timesteps

• q (int) – Number of Fourier coefficients. Defaults to 4

• time (Optional[float]) – total time. Set to 0.7*n_steps if None is passed.

Returns

A FourierWithBiasParams object with initial parameters corresponding to a linear ramp annealing schedule

Return type

FourierWithBiasParams

Todo

Make a more informed choice of the default value for q. Probably depending on n_qubits

plot(ax=None, **kwargs)

Plots self in a sensible way to the canvas ax, if provided.

Parameters
• ax (matplotlib.axes._subplots.AxesSubplot) – The canvas to plot itself on

• kwargs – All remaining keyword arguments are passed forward to the plot function

raw()

Return the parameters in a 1D array.

This 1D array is needed by scipy.optimize.minimize which expects the parameters that need to be optimized to be a 1D array.

Returns

The parameters in a 1D array. Has the same output format as the expected input of self.update_from_raw. Hence corresponds to the flattened parameters in __init__()

Return type

np.array

raw_rotation_angles()

Flat array of the rotation angles for the memory map for the parametric circuit.

Returns

Returns all single rotation angles in the ordering (x_rotation_angles, gamma_singles, zz_rotation_angles) where x_rotation_angles = (beta_q0_t0, beta_q1_t0, ... , beta_qn_tp) and the same for z_rotation_angles and zz_rotation_angles

Return type

np.array

update_from_raw(new_values)

Update all the parameters from a 1D array.

The input has the same format as the output of self.raw(). This is useful for scipy.optimize.minimize which expects the parameters that need to be optimized to be a 1D array.

Parameters

new_values – A 1D array with the new parameters. Must have length len(self) and the ordering of the flattend parameters in __init__().

property x_rotation_angles

2D array with the X-rotation angles.

1st index goes over n_steps and the 2nd index over the qubits to apply X-rotations on. These are needed by qaoa.cost_function.make_qaoa_memory_map

property z_rotation_angles

2D array with the ZZ-rotation angles.

1st index goes over the n_steps and the 2nd index over the qubit pairs, to apply ZZ-rotations on. These are needed by qaoa.cost_function.make_qaoa_memory_map

property zz_rotation_angles

2D array with Z-rotation angles.

1st index goes over the n_steps and the 2nd index over the qubit pairs, to apply Z-rotations on. These are needed by qaoa.cost_function.make_qaoa_memory_map

class qaoa.parameters.FourierExtendedParams(hyperparameters, parameters)

The Fourier pendant to ExtendedParams.

Variables
• q (int) – The number of coefficients for the discrete sine and cosine transforms below

• v (np.array) – The discrete cosine transform of the betas in ExtendedParams

• u_singles (np.array) – The discrete sine transform of the gammas_singles in ExtendedParams

• u_pairs (np.array) – The discrete sine transform of the gammas_pairs in ExtendedParams

Parameters
• hyperparameters (Tuple[PauliSum, int]) – The hyperparameters containing the hamiltonian and the number of steps and the number of fourier coefficients hyperparameters = (hamiltonian, n_steps, q)

• parameters (Tuple) – Tuple containing (v, u_singles, u_pairs) with dimensions ((q x nqubits), (q x nsingle_terms), (q x npair_terms))

classmethod empty(hyperparameters)

Alternative to __init__ that only takes hyperparameters and fills parameters via np.empty

Parameters

hyperparameters – Same as the hyperparameters argument for cls.__init__()

Returns

A Parameter object with the parameters filled by np.empty

Return type

Type[AbstractParams]

classmethod from_AbstractParameters(abstract_params, parameters, q=4)

Alternative to __init__ that takes hyperparameters from an existing AbstractQAOAParameter object.

Create a ConcreteQAOAParameters instance from an AbstractQAOAParameters instance with hyperparameters from abstract_params and normal parameters from parameters

Parameters
• abstract_params (AbstractParams) – An AbstractQAOAParameters instance to which to add the parameters

• parameter – A tuple containing the parameters. Must be the same as in __init__

Returns

A FourierExtendedParams object with the hyperparameters taken from abstract_params and the normal parameters from parameters

Return type

FourierExtendedParams

classmethod from_other_parameters(params)

Alternative to __init__ that takes parameters with less degrees of freedom as the input.

Parameters

params (Type[AbstractParams]) – The input parameters

Returns

The converted paramters s.t. all the rotation angles of the in and output parameters are the same.

Return type

Type[AbstractParams]

Note

If all conversion functions for your parametrization are in qaoa._parameter_conversions.py and correctly registered for converter you don’t need to override this function in its child classes

classmethod linear_ramp_from_hamiltonian(hamiltonian, n_steps, q=4, time=None)

Alternative to __init__ that already fills parameters.

Calculate initial parameters from a hamiltonian corresponding to a linear ramp annealing schedule and return a QAOAParams object.

Parameters
• hamiltonian (PauliSum) – hamiltonian for which to calculate the initial QAOA parameters.

• n_steps (int) – Number of timesteps.

• time (Optional[float]) – Total annealing time. Defaults to 0.7*n_steps.

Returns

The initial parameters according to a linear ramp for hamiltonian.

Return type

FourierExtendedParams

plot(ax=None, **kwargs)

Plots self in a sensible way to the canvas ax, if provided.

Parameters
• ax (matplotlib.axes._subplots.AxesSubplot) – The canvas to plot itself on

• kwargs – All remaining keyword arguments are passed forward to the plot function

raw()

Return the parameters in a 1D array.

This 1D array is needed by scipy.optimize.minimize which expects the parameters that need to be optimized to be a 1D array.

Returns

The parameters in a 1D array. Has the same output format as the expected input of self.update_from_raw. Hence corresponds to the flattened parameters in __init__()

Return type

np.array

raw_rotation_angles()

Flat array of the rotation angles for the memory map for the parametric circuit.

Returns

Returns all single rotation angles in the ordering (x_rotation_angles, gamma_singles, zz_rotation_angles) where x_rotation_angles = (beta_q0_t0, beta_q1_t0, ... , beta_qn_tp) and the same for z_rotation_angles and zz_rotation_angles

Return type

np.array

update_from_raw(new_values)

Update all the parameters from a 1D array.

The input has the same format as the output of self.raw(). This is useful for scipy.optimize.minimize which expects the parameters that need to be optimized to be a 1D array.

Parameters

new_values – A 1D array with the new parameters. Must have length len(self) and the ordering of the flattend parameters in __init__().

property x_rotation_angles

2D array with the X-rotation angles.

1st index goes over n_steps and the 2nd index over the qubits to apply X-rotations on. These are needed by qaoa.cost_function.make_qaoa_memory_map

property z_rotation_angles

2D array with the ZZ-rotation angles.

1st index goes over the n_steps and the 2nd index over the qubit pairs, to apply ZZ-rotations on. These are needed by qaoa.cost_function.make_qaoa_memory_map

property zz_rotation_angles

2D array with Z-rotation angles.

1st index goes over the n_steps and the 2nd index over the qubit pairs, to apply Z-rotations on. These are needed by qaoa.cost_function.make_qaoa_memory_map

Annealing Parameters¶

class qaoa.parameters.AnnealingParams(hyperparameters, parameters)

QAOA parameters that implement a state preparation circuit of the form

$U = e^{-i (1-s(t_p)) H_M \Delta t} e^{-i s(t_p) H_C \Delta t} \cdots e^{-i(1-s(t_1)H_M \Delta t} e^{-i s(t_1) H_C \Delta t}$

where the $$s(t_i) =: s_i$$ are the variable parameters and $$\Delta t= \frac{T}{N}$$. So the schedule is specified by specifying s(t) at evenly spaced timesteps.

Variables

schedule (np.array) – An 1D array holding the values of the schedule function at each timestep.

Parameters
• hyperparameters (Tuple[PauliSum, int, float]) – The hyperparameters containing the hamiltonian, the number of steps and the total annealing time hyperparameters = (hamiltonian, n_steps, time)

• parameters (Tuple) – Tuple containing (schedule values) of length n_steps

classmethod empty(hyperparameters)

Alternative to __init__ that only takes hyperparameters and fills parameters via np.empty

Parameters

hyperparameters – Same as the hyperparameters argument for cls.__init__()

Returns

A Parameter object with the parameters filled by np.empty

Return type

Type[AbstractParams]

classmethod from_AbstractParameters(abstract_params, parameters, time=None)

Alternative to __init__ that takes hyperparameters from an existing AbstractQAOAParameter object.

Create a ConcreteQAOAParameters instance from an AbstractQAOAParameters instance with hyperparameters from abstract_params and normal parameters from parameters

Parameters
• abstract_params (AbstractParams) – An AbstractQAOAParameters instance to which to add the parameters

• parameter – A tuple containing the parameters. Must be the same as in __init__

Returns

An AnnealingParams object with the hyperparameters taken from abstract_params and the normal parameters from parameters

Return type

AnnealingParams

classmethod from_other_parameters(params)

Alternative to __init__ that takes parameters with less degrees of freedom as the input.

Parameters

params (Type[AbstractParams]) – The input parameters

Returns

The converted paramters s.t. all the rotation angles of the in and output parameters are the same.

Return type

Type[AbstractParams]

Note

If all conversion functions for your parametrization are in qaoa._parameter_conversions.py and correctly registered for converter you don’t need to override this function in its child classes

classmethod linear_ramp_from_hamiltonian(hamiltonian, n_steps, time=None)

Alternative to __init__ that already fills parameters.

Calculate initial parameters from a hamiltonian corresponding to a linear ramp annealing schedule and return a QAOAParams object.

Parameters
• hamiltonian (PauliSum) – hamiltonian for which to calculate the initial QAOA parameters.

• n_steps (int) – Number of timesteps.

• time (Optional[float]) – Total annealing time. Defaults to 0.7*n_steps.

Returns

An AnnealingParams object corresponding to a linear ramp schedule.

Return type

AnnealingParams

plot(ax=None, **kwargs)

Plots self in a sensible way to the canvas ax, if provided.

Parameters
• ax (matplotlib.axes._subplots.AxesSubplot) – The canvas to plot itself on

• kwargs – All remaining keyword arguments are passed forward to the plot function

raw()

Return the parameters in a 1D array.

This 1D array is needed by scipy.optimize.minimize which expects the parameters that need to be optimized to be a 1D array.

Returns

The parameters in a 1D array. Has the same output format as the expected input of self.update_from_raw. Hence corresponds to the flattened parameters in __init__()

Return type

np.array

raw_rotation_angles()

Flat array of the rotation angles for the memory map for the parametric circuit.

Returns

Returns all single rotation angles in the ordering (x_rotation_angles, gamma_singles, zz_rotation_angles) where x_rotation_angles = (beta_q0_t0, beta_q1_t0, ... , beta_qn_tp) and the same for z_rotation_angles and zz_rotation_angles

Return type

np.array

update_from_raw(new_values)

Update all the parameters from a 1D array.

The input has the same format as the output of self.raw(). This is useful for scipy.optimize.minimize which expects the parameters that need to be optimized to be a 1D array.

Parameters

new_values – A 1D array with the new parameters. Must have length len(self) and the ordering of the flattend parameters in __init__().

property x_rotation_angles

2D array with the X-rotation angles.

1st index goes over n_steps and the 2nd index over the qubits to apply X-rotations on. These are needed by qaoa.cost_function.make_qaoa_memory_map

property z_rotation_angles

2D array with the ZZ-rotation angles.

1st index goes over the n_steps and the 2nd index over the qubit pairs, to apply ZZ-rotations on. These are needed by qaoa.cost_function.make_qaoa_memory_map

property zz_rotation_angles

2D array with Z-rotation angles.

1st index goes over the n_steps and the 2nd index over the qubit pairs, to apply Z-rotations on. These are needed by qaoa.cost_function.make_qaoa_memory_map

Parameter Iterators¶

To facilitate iterating one or more of the free parameters over a given range (e.g. for landscape plots) we also provide a way to build Iterables that leave all parameters except one fixed. The one is iterated over a range specified by the user.

class qaoa.parameters.QAOAParameterIterator(qaoa_params, the_parameter, the_range)

An iterator to sweep one parameter over a range in a QAOAParameter object.

Parameters
• qaoa_params (Type[AbstractParams]) – The initial QAOA parameters, where one of them is swept over

• the_parameter (str) – A string specifying, which parameter should be varied. It has to be of the form <attr_name>[i] where <attr_name> is the name of the _internal_ list and i the index, at which it sits. E.g. if qaoa_params is of type AnnealingParams and we want to vary over the second timestep, it is the_parameter = "times[1]".

• the_range (Iterable[float]) – The range, that the_parameter should be varied over

Todo

• Add checks, that the number of indices in the_parameter matches the dimensions of the_parameter

• Add checks, that the index is not too large

Example

Assume qaoa_params is of type StandardWithBiasParams and has n_steps >= 2. Then the following code produces a loop that sweeps gammas_singles[1] over the range (0, 1) in 4 steps:

the_range = np.arange(0, 1, 0.4)
the_parameter = "gammas_singles[1]"
param_iterator = QAOAParameterIterator(qaoa_params, the_parameter, the_range)
for params in param_iterator:
# do what ever needs to be done.
# we have type(params) == type(qaoa_params)