2021-04-08 11:29:37 -04:00
%root: main.tex
\begin { algorithm} [t]
\caption { \sampmon (\circuit )}
\label { alg:sample}
\begin { algorithmic} [1]
2021-04-09 11:48:10 -04:00
\Require \circuit : Circuit
2021-04-08 11:29:37 -04:00
\Ensure \vari { vars} : TreeSet
\Ensure \vari { sgn} $ \in \{ - 1 , 1 \} $
\Comment { \Cref { alg:one-pass-iter} should have been run before this one} % algorithm ~\ref{alg:sample}}
\State $ \vari { vars } \gets \emptyset $ \label { alg:sample-global1}
\If { $ \circuit . \type = + $ } \Comment { Sample at every $ + $ node}
\State $ \circuit _ { \vari { samp } } \gets $ Sample from left input ($ \circuit _ { \linput } $ ) and right input ($ \circuit _ { \rinput } $ ) w.p. $ \circuit . \vari { Lweight } $ and $ \circuit . \vari { Rweight } $ . \label { alg:sample-plus-bsamp} \Comment { Each call to \sampmon uses fresh randomness}
\State $ ( \vari { v } , \vari { s } ) \gets \sampmon ( \circuit _ { \vari { samp } } ) $ \label { alg:sample-plus-traversal}
\State $ \Return ~ ( \vari { v } , \vari { s } ) $
\ElsIf { $ \circuit . \type = \times $ } \Comment { Multiply the sampled values of all inputs}
\State $ \vari { sgn } \gets 1 $ \label { alg:sample-global2}
\For { $ input $ in $ \circuit . \vari { input } $ } \label { alg:sample-times-for-loop}
\State $ ( \vari { v } , \vari { s } ) \gets \sampmon ( input ) $
\State $ \vari { vars } \gets \vari { vars } \cup \{ \vari { v } \} $ \label { alg:sample-times-union}
\State $ \vari { sgn } \gets \vari { sgn } \times \vari { s } $ \label { alg:sample-times-product}
\EndFor
\State $ \Return ~ ( \vari { vars } , \vari { sgn } ) $
\ElsIf { $ \circuit . \type = numeric $ } \Comment { The leaf is a coefficient}
%\State $\vari{sgn} \gets \vari{sgn} \times sign(\circuit.\val)$
\State $ \Return ~ \left ( \{ \} , sign ( \circuit . \val ) \right ) $ \label { alg:sample-num-return}
\ElsIf { $ \circuit . \type = var $ }
%\State $\vari{vars} \gets \vari{vars} \; \cup \; \{\;\circuit.\val\;\}\label{alg:sample-var-union}$\Comment{Add the variable to the set}
\State $ \Return ~ \left ( \{ \circuit . \val \} , 1 \right ) $ \label { alg:sample-var-return}
\EndIf
\end { algorithmic}
\end { algorithm}