数式の整形表示¶
Jupyter Notebook, Visual Studio Code など、LaTeX による数式表示に対応した IPython のフロントエンド環境では Amplify SDK の数式は自動的に LaTeX で描画されます。
多項式、多項式配列、係数行列、制約条件、モデルは次のように数式として表示されます。
変数配列¶
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}\]
多項式¶
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}\]
多項式配列¶
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}\]
制約条件¶
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}\]
係数行列¶
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 = 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}\]