Python 量子運算(三):量子位元基底
2022/11/22
-----
Fig. 3.1. Qubit.
-----
一、安裝 LaTex
「
!sudo apt-get update
!sudo apt-get install texlive-latex-recommended
!sudo apt-get install dvipng texlive-latex-extra texlive-fonts-recommended
!wget http://mirrors.ctan.org/macros/latex/contrib/type1cm.zip
!unzip type1cm.zip -d /tmp/type1cm
!cd /tmp/type1cm/type1cm/ && sudo latex type1cm.ins
!sudo mkdir /usr/share/texmf/tex/latex/type1cm
!sudo cp /tmp/type1cm/type1cm/type1cm.sty /usr/share/texmf/tex/latex/type1cm
!sudo texhash
!apt install cm-super
」[1], [2]。
以上整合了參考文件的說明 [1], [2]。「sudo」是 Linux 作業系統的指令,用來取得較高的權限,以更動系統的部分設定 [3]。
比起「\rangle」等,較複雜的 LaTex 語法如「bmatrix」需要先在 Linux 中安裝 LaTex [1] - [4]。再於 Matplotlib 指定使用 LaTeX,以及使用 amsmath 此套件 [4]。
「
mpl.rcParams['text.usetex'] = True
mpl.rcParams['text.latex.preamble'] = r'\usepackage{{amsmath}}'
」[4]。
-----
代碼 3.1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | # Program 3.1:qubit(quantum bit) import matplotlib as mpl import matplotlib.pyplot as plt mpl.rcParams['text.usetex'] = True mpl.rcParams['text.latex.preamble'] = r'\usepackage{{amsmath}}' mpl.rcParams['font.size'] = 80 fig, ax = plt.subplots(figsize=(16, 16)) ax.text(0.35, 0.65, r"$\vert0\rangle\equiv\begin{bmatrix}1\\0\end{bmatrix}$") ax.text(0.35, 0.30, r"$\vert1\rangle\equiv\begin{bmatrix}0\\1\end{bmatrix}$") ax.set_axis_off() # plt.savefig('/content/drive/My Drive/pqc/0003_001.png') plt.show() |
解說:
-----
References
# LaTex
[1] Latex + matplotlib + google colab – learnsharewithdp
https://learnsharewithdp.wordpress.com/2020/05/08/latex-matplotlib-google-colab/
[2] (29) Make Latex Work with Matplotlib on Google Colab - YouTube
https://www.youtube.com/watch?v=yySQ-6mh8yo
# sudo
[3] Linux 的 su 與 sudo 指令教學與範例 - G. T. Wang
https://blog.gtwang.org/linux/sudo-su-command-tutorial-examples/
# matrix
[4] python - How to render a latex matrix by using matplotlib - Stack Overflow
https://stackoverflow.com/questions/55427836/how-to-render-a-latex-matrix-by-using-matplotlib
# math equations
[5] Rendering math equations using TeX — Matplotlib 3.6.0 documentation
https://matplotlib.org/stable/gallery/text_labels_and_annotations/tex_demo.html
# quantum bit
[6] 量子電腦設計原理
https://blog.heron.me/quantum-computing-c7633efa9030
# mathtext(another system)
[7] Mathtext Examples — Matplotlib 3.4.3 documentation
https://matplotlib.org/3.4.3/gallery/text_labels_and_annotations/mathtext_examples.html
-----
Python 量子運算(目錄)
https://mandhistory.blogspot.com/2022/01/quantum-computing.html
-----
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。