Formatted Formula View#

The Amplify SDK formulas are automatically rendered in LaTeX in IPython front-end environments that support LaTeX formula display, such as Jupyter Notebook and Visual Studio Code.

Polynomials, polynomial arrays, coefficient matrices, constraints, and models are displayed as mathematical expressions as follows.

Variable array#

import amplify

gen = amplify.VariableGenerator()
q = gen.array("Binary", shape=(4, 4))
q
\[\begin{split}\displaystyle \begin{aligned}&\left[\begin{matrix}q_{0,0}& q_{0,1}& q_{0,2}& q_{0,3}\\q_{1,0}& q_{1,1}& q_{1,2}& q_{1,3}\\q_{2,0}& q_{2,1}& q_{2,2}& q_{2,3}\\q_{3,0}& q_{3,1}& q_{3,2}& q_{3,3}\end{matrix}\right]\end{aligned}\end{split}\]

Polynomial#

p = 2 * (q[0] * q[1]).sum()
p
\[\displaystyle 2 q_{0,0} q_{1,0} + 2 q_{0,1} q_{1,1} + 2 q_{0,2} q_{1,2} + 2 q_{0,3} q_{1,3}\]

Polynomial array#

q[0:2] + q[2:]
\[\begin{split}\displaystyle \begin{aligned}&\left[\begin{matrix}q_{0,0} + q_{2,0}& q_{0,1} + q_{2,1}& q_{0,2} + q_{2,2}& q_{0,3} + q_{2,3}\\q_{1,0} + q_{3,0}& q_{1,1} + q_{3,1}& q_{1,2} + q_{3,2}& q_{1,3} + q_{3,3}\end{matrix}\right]\end{aligned}\end{split}\]

Constraint#

c1 = amplify.one_hot(q, axis=1)
c1
\[\begin{split}\displaystyle \begin{array}{l}q_{0,0} + q_{0,1} + q_{0,2} + q_{0,3} = 1\ (\text{weight}\colon\ 1)\\q_{1,0} + q_{1,1} + q_{1,2} + q_{1,3} = 1\ (\text{weight}\colon\ 1)\\q_{2,0} + q_{2,1} + q_{2,2} + q_{2,3} = 1\ (\text{weight}\colon\ 1)\\q_{3,0} + q_{3,1} + q_{3,2} + q_{3,3} = 1\ (\text{weight}\colon\ 1)\end{array}\end{split}\]

Coefficient matrix#

import numpy as np

m = gen.matrix("Binary", 4)
m.quadratic = np.array([[1, 2, 3, 4],
                        [0, 5, 6, 7],
                        [0, 0, 8, 9],
                        [0, 0, 0, 10]])
m.linear = np.array([1, 2, 3, 4])
c2 = amplify.one_hot(m.variable_array)
m
\[\begin{split}\displaystyle \begin{array}{l}x^\top Qx + p^\top x + c\\\text{where:}\\\quad\begin{aligned}x=&\left[\begin{matrix}q'_0& q'_1& q'_2& q'_3\end{matrix}\right],\\Q=&\left[\begin{matrix} 1.& 2.& 3.& 4.\\ 0.& 5.& 6.& 7.\\ 0.& 0.& 8.& 9.\\ 0.& 0.& 0.& 10.\end{matrix}\right],\\p=&\left[\begin{matrix} 1.& 2.& 3.& 4.\end{matrix}\right],\\c=&\ 0\end{aligned}\end{array}\end{split}\]

Model#

model = p + c1
model
\[\begin{split}\displaystyle \begin{array}{l}\text{minimize:}\\\quad 2 q_{0,0} q_{1,0} + 2 q_{0,1} q_{1,1} + 2 q_{0,2} q_{1,2} + 2 q_{0,3} q_{1,3}\\\text{subject to:}\\\quad \begin{array}{l}q_{0,0} + q_{0,1} + q_{0,2} + q_{0,3} = 1\ (\text{weight}\colon\ 1)\\q_{1,0} + q_{1,1} + q_{1,2} + q_{1,3} = 1\ (\text{weight}\colon\ 1)\\q_{2,0} + q_{2,1} + q_{2,2} + q_{2,3} = 1\ (\text{weight}\colon\ 1)\\q_{3,0} + q_{3,1} + q_{3,2} + q_{3,3} = 1\ (\text{weight}\colon\ 1)\end{array}\end{array}\end{split}\]
model = m + c2
model
\[\begin{split}\displaystyle \begin{array}{l}\text{minimize:}\\\quad \begin{array}{l}x^\top Qx + p^\top x + c\\\text{where:}\\\quad\begin{aligned}x=&\left[\begin{matrix}q'_0& q'_1& q'_2& q'_3\end{matrix}\right],\\Q=&\left[\begin{matrix} 1.& 2.& 3.& 4.\\ 0.& 5.& 6.& 7.\\ 0.& 0.& 8.& 9.\\ 0.& 0.& 0.& 10.\end{matrix}\right],\\p=&\left[\begin{matrix} 1.& 2.& 3.& 4.\end{matrix}\right],\\c=&\ 0\end{aligned}\end{array}\\\text{subject to:}\\\quad \begin{array}{l}q'_0 + q'_1 + q'_2 + q'_3 = 1\ (\text{weight}\colon\ 1)\end{array}\end{array}\end{split}\]