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.
- class LSE(A, b)[source]¶
Bases:
NamedTuple
A
namedtuple
representing a linear system of equations.\[A x = b\]Create new instance of LSE(A, b)
- count(value, /)¶
Return number of occurrences of value.
- index(value, start=0, stop=9223372036854775807, /)¶
Return first index of value.
Raises ValueError if the value is not present.
- solve()[source]¶
Return the solution to this LSE: \(x=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 \(\sum_i x_i == 1\) which is a requirement for valid MPF coefficients.
- Return type:
Optimization problem constructors¶
- setup_exact_problem(lse)[source]¶
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
:\[\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:
References
- [1]: A. Carrera Vazquez et al., Quantum 7, 1067 (2023).
- setup_sum_of_squares_problem(lse, *, max_l1_norm=10.0)[source]¶
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 theLSE
:\[\sum_i \left( \sum_j A_{ij} x_j - b_i \right)^2\]two constraints are set:
the variables must sum to 1: \(\sum_i x_i == 1\)
the L1-norm (
norm1
) of the variables is bounded bymax_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:
- Returns:
The optimization problem and coefficients variable.
- Return type:
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(lse, *, max_l1_norm=10.0)[source]¶
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 + 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 bysetup_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 + \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:
the variables must sum to 1: \(\sum_i x_i == 1\)
the L1-norm (
norm1
) of the variables is bounded bymax_l1_norm
Below is an example which uses the
lse
object constructed in the example forsetup_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:
- Returns:
The optimization problem and coefficients variable.
- Return type:
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).