量子会計士

先輩、それ本気でノイマン型でやるんですか?!と言われないためにあがく。

Q# python

Q#の紹介ページを見ているとPythonからも使えるとのこと。では、試してみましょう。
(2018年2月のアップデートからのようです。)

こちらは、ほとんど、Githubにあるサンプルの説明をそのまま実行するのみです。

github.com

1. nb_condaのインストール

まずは、MSにすすめられるまま、nb_condaをインストール

conda install nb_conda

ついでに、condaのアップデートも実行されました。

2. サンプル実行用のPython環境を構築

Githubからcloneした一式の中に含まれる Quantum/Samples/PythonInterop フォルダでコマンドプロンプトを開いて、

conda env create -f environment.yml

すると、qsharp-samples という名前で、必要なソフトが入った環境ができました。なんと便利なことでしょう。

一昔前なら一つずつインストールして、途中で色々エラー出て泣いてた。

なお、nb_condaとnoseを加えたcondaのenvironment.ymlをgistに置いておきました。 gist7331087aa54f202ae373c09f1940f1a1

activate qsharp-samples

で、作成された環境 qsharp-samples を有効化

3. Jupyter notebookを起動

jupyter notebook

ブラウザで、Jupyter notebookが立ち上がる。

なお、別の環境で試した際は、qsharp-sample環境からJupyter notebokを立ち上げたのに、Kernelが通常のPython3のままとなり、うまくいかないことがありました。 その際は、

conda install notebook ipythonkernel
ipython kernel install —user —name qsharp-sample —display-name qsharp-python

として、明示的にJupyter notebookのカーネルを追加してから、Jupyter notebook上で、Kernel→Change kernel→qsharp-pythonを選択したら動きました。  

4. Jupyter notebookで実行

あとは、サンプルを実行していくのですが、以下のコードを実行したところエラー発生。

plt.figure(figsize=(10, 10))
estimation_results['posterior'].plot_covariance()
plt.xticks(rotation=90)
    ---------------------------------------------------------------------------

    AttributeError                            Traceback (most recent call last)

    <ipython-input-16-d936f679b251> in <module>
          1 plt.figure(figsize=(10, 10))
    ----> 2 estimation_results['posterior'].plot_covariance()
          3 plt.xticks(rotation=90)
    

    ~\Anaconda3\envs\qsharp-samples\lib\site-packages\qinfer\smc.py in plot_covariance(self, corr, param_slice, tick_labels, tick_params)
       1159         cov = self.est_covariance_mtx(corr=corr)[param_slice, param_slice]
       1160 
    -> 1161         retval = mpls.hinton(cov)
       1162         plt.xticks(*tick_labels, **(tick_params if tick_params is not None else {}))
       1163         plt.yticks(*tick_labels, **(tick_params if tick_params is not None else {}))
    

    ~\Anaconda3\envs\qsharp-samples\lib\site-packages\mpltools\special\hinton.py in hinton(inarray, max_value, use_default_ticks)
         54 
         55     ax = plt.gca()
    ---> 56     ax.set_axis_bgcolor('gray')
         57     # make sure we're working with a numpy array, not a numpy matrix
         58     inarray = np.asarray(inarray)
    

    AttributeError: 'AxesSubplot' object has no attribute 'set_axis_bgcolor'

結果的には、以下のページを参考に、

'AxesSubplot' object has no attribute 'set_axis_bgcolor' · Issue #10762 · scikit-learn/scikit-learn · GitHub

エラーの中に出てくる ~\Anaconda3\envs\qsharp-samples\lib\site-packages\mpltools\special\hinton.py 内の、

ax.set_axis_bgcolor('gray')

ax.set_facecolor('gray')

としたら動作しました。

なお、Jupyterあるあるなのかもしれませんが、hinton.pyを変更した後、そのまま実行すると、以下のように、示される該当箇所は変更後のものになっているのに前と同じエラーがでました。
これは、KernelをRestartしたところ解消しました。

~\Anaconda3\envs\qsharp-samples\lib\site-packages\mpltools\special\hinton.py in hinton(inarray, max_value, use_default_ticks)
     54 
     55     ax = plt.gca()
---> 56     ax.set_facecolor('gray')
     57     # make sure we're working with a numpy array, not a numpy matrix
     58     inarray = np.asarray(inarray)

AttributeError: 'AxesSubplot' object has no attribute 'set_axis_bgcolor'