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)
Bases: NamedTuple
A namedtuple
representing a linear system of equations.
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()
Return the solution to this LSE: .
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 which is a requirement for valid MPF coefficients.
Return type
x
Type: Variable
Returns the $x$ Variable
.
Optimization problem constructors
setup_exact_problem
setup_exact_problem(lse)
Construct a cvxpy.Problem
for finding the exact MPF coefficients.
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
:
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
setup_sum_of_squares_problem(lse, *, max_l1_norm=10.0)
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
:
-
two constraints are set:
- the variables must sum to 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
- 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
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)
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:
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
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:
- 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 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
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).