Skip to main contentIBM Quantum Documentation

Cost Functions

qiskit_addon_mpf.costs

Cost functions for MPF coefficients.

This module provides a number of optimization problem generator functions, each implementing a different cost function as the problem’s target objective. All of the functions provided by this module take a linear system of equations (LSE) encoding the parameters of the optimization problem as their first argument.

LSE

class LSE(A, b)

GitHub

Bases: NamedTuple

A namedtuple representing a linear system of equations.

Ax=bA x = b

Create new instance of LSE(A, b)

Parameters

A

Type: ndarray

The left hand side of the LSE.

b

Type: ndarray

The right hand side of the LSE.

count

count(value, /)

Return number of occurrences of value.

index

index(value, start=0, stop=9223372036854775807, /)

Return first index of value.

Raises ValueError if the value is not present.

solve

solve()

GitHub

Return the solution to this LSE: x=A1bx=A^{-1}b.

Returns

The solution to this LSE.

Raises

  • ValueError – if this LSE is parameterized with unassigned values.
  • ValueError – if this LSE does not include a row ensuring that ixi==1\sum_i x_i == 1 which is a requirement for valid MPF coefficients.

Return type

ndarray

x

Type: Variable

Returns the $x$ Variable.


Optimization problem constructors

setup_exact_problem

setup_exact_problem(lse)

GitHub

Construct a cvxpy.Problem for finding the exact MPF coefficients.

Note

The coefficients found via this optimization problem will be identical to the analytical ones obtained from the LSE.solve() method. This additional interface exists to highlight the parallel to the other cost functions provided by this module. It also serves educational purposes for how to approach optimization problems targeting MPF coefficients.

The optimization problem constructed by this function is defined as follows:

  • the cost function minimizes the L1-norm (norm1) of the variables (LSE.x)

  • the constraints correspond to each equation of the LSE:

jAijxj=bi\sum_j A_{ij} x_j = b_i

Here is an example:

>>> from qiskit_addon_mpf.costs import setup_exact_problem
>>> from qiskit_addon_mpf.static import setup_static_lse
>>> lse = setup_static_lse([1,2,3], order=2, symmetric=True)
>>> problem, coeffs = setup_exact_problem(lse)
>>> print(problem)
minimize norm1(x)
subject to Sum([1. 1. 1.] @ x, None, False) == 1.0
           Sum([1. 0.25   0.11111111] @ x, None, False) == 0.0
           Sum([1. 0.0625 0.01234568] @ x, None, False) == 0.0

You can then solve the problem and access the expansion coefficients like so:

>>> final_cost = problem.solve()
>>> print(coeffs.value)  
[ 0.04166667 -1.06666667  2.025     ]

Parameters

lse (LSE) – the linear system of equations from which to build the model.

Returns

The optimization problem and coefficients variable.

Return type

tuple[Problem, Variable]

References

[1]: A. Carrera Vazquez et al., Quantum 7, 1067 (2023).

https://quantum-journal.org/papers/q-2023-07-25-1067/

setup_sum_of_squares_problem

setup_sum_of_squares_problem(lse, *, max_l1_norm=10.0)

GitHub

Construct a cvxpy.Problem for finding approximate MPF coefficients.

The optimization problem constructed by this function is defined as follows:

  • the cost function minimizes the sum of squares (sum_squares()) of the distances to an exact solution for all equations of the LSE:
i(jAijxjbi)2\sum_i \left( \sum_j A_{ij} x_j - b_i \right)^2
  • two constraints are set:

    1. the variables must sum to 1: ixi==1\sum_i x_i == 1
    2. the L1-norm (norm1) of the variables is bounded by max_l1_norm

Here is an example:

>>> from qiskit_addon_mpf.costs import setup_sum_of_squares_problem
>>> from qiskit_addon_mpf.static import setup_static_lse
>>> lse = setup_static_lse([1,2,3], order=2, symmetric=True)
>>> problem, coeffs = setup_sum_of_squares_problem(lse, max_l1_norm=3.0)
>>> print(problem)  
minimize quad_over_lin(Vstack([1. 1.     1.]         @ x + -1.0,
                              [1. 0.25   0.11111111] @ x + -0.0,
                              [1. 0.0625 0.01234568] @ x + -0.0), 1.0)
subject to Sum(x, None, False) == 1.0
           norm1(x) <= 3.0

You can then solve the problem and access the expansion coefficients like so:

>>> final_cost = problem.solve()
>>> print(coeffs.value)  
[ 0.03513467 -1.          1.96486533]

Parameters

  • lse (LSE) – the linear system of equations from which to build the model.
  • max_l1_norm (float) – the upper limit to use for the constrain of the L1-norm of the variables.

Returns

The optimization problem and coefficients variable.

Return type

tuple[Problem, Variable]

References

[1]: S. Zhuk et al., Phys. Rev. Research 6, 033309 (2024).

https://journals.aps.org/prresearch/abstract/10.1103/PhysRevResearch.6.033309

setup_frobenius_problem

setup_frobenius_problem(lse, *, max_l1_norm=10.0)

GitHub

Construct a cvxpy.Problem for finding approximate MPF coefficients.

The optimization problem constructed by this function is defined as follows:

  • the cost function minimizes the following quadratic expression:
1+xTAx2xTb1 + x^T A x - 2 x^T b

As shown in [1] and [2], this expression arises from the Frobenius norm of the error between an exact time evolution state and a dynamic MPF. As such, taking the LSE constructed by setup_dynamic_lse() and plugging it into this function will yield Eq. (20) of [1] (which is identical to Eq. (2) of [2]), which we repeat below

1+i,jAij(t)xi(t)xj(t)2ibi(t)xi(t),1 + \sum_{i,j} A_{ij}(t) x_i(t) x_j(t) - 2 \sum_i b_i(t) x_i(t) \, ,

where $A$ and $b$ of our LSE correspond to the Gram matrix ($M$ in [1] and [2]) and the overlap vector ($L$ in [1] and [2]), respectively. Additionally, we use $x(t)$ to denote the MPF variables (or coefficients) rather than $c$ in [1] and [2].

  • two constraints are set:

    1. the variables must sum to 1: ixi==1\sum_i x_i == 1
    2. the L1-norm (norm1) of the variables is bounded by max_l1_norm

Below is an example which uses the lse object constructed in the example for setup_dynamic_lse().

>>> from qiskit_addon_mpf.costs import setup_frobenius_problem
>>> problem, coeffs = setup_frobenius_problem(lse, max_l1_norm=3.0)
>>> print(problem)  
minimize 1.0 + QuadForm(x, [[1.00 1.00]
                            [1.00 1.00]]) + -[2.00003171 1.99997911] @ x
subject to Sum(x, None, False) == 1.0
           norm1(x) <= 3.0

You can then solve the problem and access the expansion coefficients like so:

>>> final_cost = problem.solve()
>>> print(coeffs.value)  
[0.50596416 0.49403584]

Parameters

  • lse (LSE) – the linear system of equations from which to build the model.
  • max_l1_norm (float) – the upper limit to use for the constrain of the L1-norm of the variables.

Returns

The optimization problem and coefficients variable.

Return type

tuple[Problem, Variable]

References

[1]: S. Zhuk et al., Phys. Rev. Research 6, 033309 (2024).

https://journals.aps.org/prresearch/abstract/10.1103/PhysRevResearch.6.033309

[2]: N. Robertson et al., arXiv:2407.17405v2 (2024).

https://arxiv.org/abs/2407.17405v2

Was this page helpful?
Report a bug or request content on GitHub.