Skip to content

Commit 00331ff

Browse files
committed
update week5
1 parent 1d3b6b3 commit 00331ff

20 files changed

+8958
-504
lines changed

doc/pub/week5/html/week5-bs.html

Lines changed: 1613 additions & 113 deletions
Large diffs are not rendered by default.

doc/pub/week5/html/week5-reveal.html

Lines changed: 1525 additions & 83 deletions
Large diffs are not rendered by default.

doc/pub/week5/html/week5-solarized.html

Lines changed: 1536 additions & 92 deletions
Large diffs are not rendered by default.

doc/pub/week5/html/week5.html

Lines changed: 1536 additions & 92 deletions
Large diffs are not rendered by default.
151 KB
Binary file not shown.

doc/pub/week5/ipynb/week5.ipynb

Lines changed: 2320 additions & 123 deletions
Large diffs are not rendered by default.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
\begin{MintedVerbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYG{c+c1}{\PYGZsh{} Now rewrite it in terms of the identity matrix and the Pauli matrix X and Z}
3+
\PYG{n}{X} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{array}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{,}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)}
4+
\PYG{n}{Y} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{array}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{n}{j}\PYG{p}{]}\PYG{p}{,}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{n}{j}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)}
5+
\PYG{n}{Z} \PYG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p}{)}\PYG{o}{*}\PYG{n}{Z}\PYG{o}{+}\PYG{n}{omega\PYGZus{}x}\PYG{o}{*}\PYG{n}{X}
12+
\PYG{n}{EigValues}\PYG{p}{,} \PYG{n}{EigVectors} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{linalg}\PYG{o}{.}\PYG{n}{eig}\PYG{p}{(}\PYG{n}{Hamiltonian}\PYG{p}{)}
13+
\PYG{n}{permute} \PYG{o}{=} \PYG{n}{EigValues}\PYG{o}{.}\PYG{n}{argsort}\PYG{p}{(}\PYG{p}{)}
14+
\PYG{n}{EigValues} \PYG{o}{=} \PYG{n}{EigValues}\PYG{p}{[}\PYG{n}{permute}\PYG{p}{]}
15+
\PYG{c+c1}{\PYGZsh{} print only the lowest eigenvalue}
16+
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{EigValues}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)}
17+
18+
\end{MintedVerbatim}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
\begin{MintedVerbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYG{k}{def}\PYG{+w}{ }\PYG{n+nf}{minimize\PYGZus{}energy}\PYG{p}{(}\PYG{n}{lmb}\PYG{p}{,} \PYG{n}{number\PYGZus{}shots}\PYG{p}{,} \PYG{n}{angles\PYGZus{}0}\PYG{p}{,} \PYG{n}{learning\PYGZus{}rate}\PYG{p}{,} \PYG{n}{max\PYGZus{}epochs}\PYG{p}{)}\PYG{p}{:}
3+
\PYG{c+c1}{\PYGZsh{} angles = np.random.uniform(low = 0, high = np.pi, size = 2)}
4+
\PYG{n}{angles} \PYG{o}{=} \PYG{n}{angles\PYGZus{}0} \PYG{c+c1}{\PYGZsh{}lmb*np.array([np.pi, np.pi])}
5+
\PYG{n}{epoch} \PYG{o}{=} \PYG{l+m+mi}{0}
6+
\PYG{n}{delta\PYGZus{}energy} \PYG{o}{=} \PYG{l+m+mi}{1}
7+
\PYG{n}{energy} \PYG{o}{=} \PYG{n}{get\PYGZus{}energy}\PYG{p}{(}\PYG{n}{angles}\PYG{p}{,} \PYG{n}{lmb}\PYG{p}{,} \PYG{n}{number\PYGZus{}shots}\PYG{p}{)}
8+
\PYG{k}{while} \PYG{p}{(}\PYG{n}{epoch} \PYG{o}{\PYGZlt{}} \PYG{n}{max\PYGZus{}epochs}\PYG{p}{)} \PYG{o+ow}{and} \PYG{p}{(}\PYG{n}{delta\PYGZus{}energy} \PYG{o}{\PYGZgt{}} \PYG{l+m+mf}{1e\PYGZhy{}4}\PYG{p}{)}\PYG{p}{:}
9+
\PYG{n}{grad} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros\PYGZus{}like}\PYG{p}{(}\PYG{n}{angles}\PYG{p}{)}
10+
\PYG{k}{for} \PYG{n}{idx} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{angles}\PYG{o}{.}\PYG{n}{shape}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)}\PYG{p}{:}
11+
\PYG{n}{angles\PYGZus{}temp} \PYG{o}{=} \PYG{n}{angles}\PYG{o}{.}\PYG{n}{copy}\PYG{p}{(}\PYG{p}{)}
12+
\PYG{n}{angles\PYGZus{}temp}\PYG{p}{[}\PYG{n}{idx}\PYG{p}{]} \PYG{o}{+}\PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{pi}\PYG{o}{/}\PYG{l+m+mi}{2}
13+
\PYG{n}{E\PYGZus{}plus} \PYG{o}{=} \PYG{n}{get\PYGZus{}energy}\PYG{p}{(}\PYG{n}{angles\PYGZus{}temp}\PYG{p}{,} \PYG{n}{lmb}\PYG{p}{,} \PYG{n}{number\PYGZus{}shots}\PYG{p}{)}
14+
\PYG{n}{angles\PYGZus{}temp}\PYG{p}{[}\PYG{n}{idx}\PYG{p}{]} \PYG{o}{\PYGZhy{}}\PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{pi}
15+
\PYG{n}{E\PYGZus{}minus} \PYG{o}{=} \PYG{n}{get\PYGZus{}energy}\PYG{p}{(}\PYG{n}{angles\PYGZus{}temp}\PYG{p}{,} \PYG{n}{lmb}\PYG{p}{,} \PYG{n}{number\PYGZus{}shots}\PYG{p}{)}
16+
\PYG{n}{grad}\PYG{p}{[}\PYG{n}{idx}\PYG{p}{]} \PYG{o}{=} \PYG{p}{(}\PYG{n}{E\PYGZus{}plus} \PYG{o}{\PYGZhy{}} \PYG{n}{E\PYGZus{}minus}\PYG{p}{)}\PYG{o}{/}\PYG{l+m+mi}{2}
17+
\PYG{n}{angles} \PYG{o}{\PYGZhy{}}\PYG{o}{=} \PYG{n}{learning\PYGZus{}rate}\PYG{o}{*}\PYG{n}{grad}
18+
\PYG{n}{new\PYGZus{}energy} \PYG{o}{=} \PYG{n}{get\PYGZus{}energy}\PYG{p}{(}\PYG{n}{angles}\PYG{p}{,} \PYG{n}{lmb}\PYG{p}{,} \PYG{n}{number\PYGZus{}shots}\PYG{p}{)}
19+
\PYG{n}{delta\PYGZus{}energy} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{abs}\PYG{p}{(}\PYG{n}{new\PYGZus{}energy} \PYG{o}{\PYGZhy{}} \PYG{n}{energy}\PYG{p}{)}
20+
\PYG{n}{energy} \PYG{o}{=} \PYG{n}{new\PYGZus{}energy}
21+
\PYG{n}{epoch} \PYG{o}{+}\PYG{o}{=} \PYG{l+m+mi}{1}
22+
\PYG{k}{return} \PYG{n}{angles}\PYG{p}{,} \PYG{n}{epoch}\PYG{p}{,} \PYG{p}{(}\PYG{n}{epoch} \PYG{o}{\PYGZlt{}} \PYG{n}{max\PYGZus{}epochs}\PYG{p}{)}\PYG{p}{,} \PYG{n}{energy}\PYG{p}{,} \PYG{n}{delta\PYGZus{}energy}
23+
24+
25+
\end{MintedVerbatim}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
\begin{MintedVerbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYG{n}{number\PYGZus{}shots\PYGZus{}search} \PYG{o}{=} \PYG{l+m+mi}{10\PYGZus{}000}
3+
\PYG{n}{number\PYGZus{}shots} \PYG{o}{=} \PYG{l+m+mi}{10\PYGZus{}000}
4+
\PYG{n}{learning\PYGZus{}rate} \PYG{o}{=} \PYG{l+m+mf}{0.3}
5+
\PYG{n}{max\PYGZus{}epochs} \PYG{o}{=} \PYG{l+m+mi}{400}
6+
\PYG{n}{lmbvalues} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{linspace}\PYG{p}{(}\PYG{l+m+mf}{0.0}\PYG{p}{,} \PYG{l+m+mf}{1.0}\PYG{p}{,} \PYG{l+m+mi}{30}\PYG{p}{)}
7+
\PYG{n}{min\PYGZus{}energy} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{lmbvalues}\PYG{p}{)}\PYG{p}{)}
8+
\PYG{n}{epochs} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{lmbvalues}\PYG{p}{)}\PYG{p}{)}
9+
\PYG{k}{for} \PYG{n}{index}\PYG{p}{,} \PYG{n}{lmb} \PYG{o+ow}{in} \PYG{n+nb}{enumerate}\PYG{p}{(}\PYG{n}{tqdm}\PYG{p}{(}\PYG{n}{lmbvalues}\PYG{p}{)}\PYG{p}{)}\PYG{p}{:}
10+
\PYG{n}{memory} \PYG{o}{=} \PYG{l+m+mi}{0}
11+
\PYG{n}{angles\PYGZus{}0} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{uniform}\PYG{p}{(}\PYG{n}{low} \PYG{o}{=} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{n}{high} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{pi}\PYG{p}{,} \PYG{n}{size} \PYG{o}{=} \PYG{l+m+mi}{2}\PYG{p}{)}
12+
\PYG{n}{angles}\PYG{p}{,} \PYG{n}{epochs}\PYG{p}{[}\PYG{n}{index}\PYG{p}{]}\PYG{p}{,} \PYG{n}{converged}\PYG{p}{,} \PYG{n}{energy}\PYG{p}{,} \PYG{n}{delta\PYGZus{}energy} \PYG{o}{=} \PYG{n}{minimize\PYGZus{}energy}\PYG{p}{(}\PYG{n}{lmb}\PYG{p}{,} \PYG{n}{number\PYGZus{}shots\PYGZus{}search}\PYG{p}{,} \PYG{n}{angles\PYGZus{}0}\PYG{p}{,} \PYG{n}{learning\PYGZus{}rate}\PYG{p}{,} \PYG{n}{max\PYGZus{}epochs}\PYG{p}{)}
13+
\PYG{k}{if} \PYG{n}{epochs}\PYG{p}{[}\PYG{n}{index}\PYG{p}{]} \PYG{o}{\PYGZlt{}} \PYG{p}{(}\PYG{n}{epochs}\PYG{p}{[}\PYG{n}{index}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{]} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{5}\PYG{p}{)}\PYG{p}{:}
14+
\PYG{n}{angles\PYGZus{}0} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{uniform}\PYG{p}{(}\PYG{n}{low} \PYG{o}{=} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{n}{high} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{pi}\PYG{p}{,} \PYG{n}{size} \PYG{o}{=} \PYG{l+m+mi}{2}\PYG{p}{)}
15+
\PYG{n}{angles}\PYG{p}{,} \PYG{n}{epochs}\PYG{p}{[}\PYG{n}{index}\PYG{p}{]}\PYG{p}{,} \PYG{n}{converged}\PYG{p}{,} \PYG{n}{energy}\PYG{p}{,} \PYG{n}{delta\PYGZus{}energy} \PYG{o}{=} \PYG{n}{minimize\PYGZus{}energy}\PYG{p}{(}\PYG{n}{lmb}\PYG{p}{,} \PYG{n}{number\PYGZus{}shots\PYGZus{}search}\PYG{p}{,} \PYG{n}{angles\PYGZus{}0}\PYG{p}{,} \PYG{n}{learning\PYGZus{}rate}\PYG{p}{,} \PYG{n}{max\PYGZus{}epochs}\PYG{p}{)}
16+
\PYG{n}{min\PYGZus{}energy}\PYG{p}{[}\PYG{n}{index}\PYG{p}{]} \PYG{o}{=} \PYG{n}{get\PYGZus{}energy}\PYG{p}{(}\PYG{n}{angles}\PYG{p}{,} \PYG{n}{lmb}\PYG{p}{,} \PYG{n}{number\PYGZus{}shots}\PYG{p}{)}
17+
18+
\end{MintedVerbatim}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
\begin{MintedVerbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYG{c+c1}{\PYGZsh{} define the rotation matrices}
3+
\PYG{c+c1}{\PYGZsh{} Define angles theta and phi}
4+
\PYG{n}{theta} \PYG{o}{=} \PYG{l+m+mf}{0.5}\PYG{o}{*}\PYG{n}{np}\PYG{o}{.}\PYG{n}{pi}\PYG{p}{;} \PYG{n}{phi} \PYG{o}{=} \PYG{l+m+mf}{0.2}\PYG{o}{*}\PYG{n}{np}\PYG{o}{.}\PYG{n}{pi}
5+
\PYG{n}{Rx} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{theta}\PYG{o}{*}\PYG{l+m+mf}{0.5}\PYG{p}{)}\PYG{o}{*}\PYG{n}{I}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{n}{j}\PYG{o}{*}\PYG{n}{np}\PYG{o}{.}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{theta}\PYG{o}{*}\PYG{l+m+mf}{0.5}\PYG{p}{)}\PYG{o}{*}\PYG{n}{X}
6+
\PYG{n}{Ry} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{phi}\PYG{o}{*}\PYG{l+m+mf}{0.5}\PYG{p}{)}\PYG{o}{*}\PYG{n}{I}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{n}{j}\PYG{o}{*}\PYG{n}{np}\PYG{o}{.}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{phi}\PYG{o}{*}\PYG{l+m+mf}{0.5}\PYG{p}{)}\PYG{o}{*}\PYG{n}{Y}
7+
\PYG{c+c1}{\PYGZsh{}define basis states}
8+
\PYG{n}{basis0} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{array}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)}
9+
\PYG{n}{basis1} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{array}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)}
10+
11+
\PYG{n}{NewBasis} \PYG{o}{=} \PYG{n}{Ry} \PYG{o}{@} \PYG{n}{Rx} \PYG{o}{@} \PYG{n}{basis0}
12+
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{NewBasis}\PYG{p}{)}
13+
\PYG{c+c1}{\PYGZsh{} Compute the expectation value}
14+
\PYG{c+c1}{\PYGZsh{}Note hermitian conjugation}
15+
\PYG{n}{Energy} \PYG{o}{=} \PYG{n}{NewBasis}\PYG{o}{.}\PYG{n}{conj}\PYG{p}{(}\PYG{p}{)}\PYG{o}{.}\PYG{n}{T} \PYG{o}{@} \PYG{n}{Hamiltonian} \PYG{o}{@} \PYG{n}{NewBasis}
16+
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{Energy}\PYG{p}{)}
17+
18+
\end{MintedVerbatim}

0 commit comments

Comments
 (0)