MacにQ#開発環境を作る

先日、Microsoftが開発している量子プログラミング言語Q#がMac, Linuxに対応したと[アナウンスされました](https://www.infoq.com/news/2018/02/microsoft-quantum-kit-mac-linux)
Q#がずっと気になっていた私はすぐに飛びついて、インストールと動作確認までやってみたので手順をまとめます

Q#そのものについての解説は、@__pandaman64__さんの記事をみてもらえればと思います


# 環境
- Mac High Sierra 10.13.3
- Visual Studio Code 1.20.1

VSCodeで使えるのは結構うれしい

# 必要なもの
macOS(Linuxでは検証してませんが、おそらく同様の手順で動くはず)

- [.NET CORE SDK](https://www.microsoft.com/net/learn/get-started/macos) 2.0以降

- (推奨)[Visual Studio Code](https://code.visualstudio.com/)

- (推奨)[Microsoft Quantum Development Kit for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=quantum.quantum-devkit-vscode)

# Q#開発環境をインストールする

- デベロッパーキットのインストール

[ Microsoft Quantum Development Kit for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=quantum.quantum-devkit-vscode)をインストールします(VSCodeで拡張機能を検索しても出てきます)

- プロジェクトテンプレートのインストール

Q# Development Kit project templatesをインストールします

Bashなどのコマンドライン上で

`$ dotnet new -i "Microsoft.Quantum.ProjectTemplates::0.2-*"`

# 正常にインストールできてるか検証

- サンプルをclone

お好みのディレクトリで

```
$ git clone https://github.com/Microsoft/Quantum.git
$ cd Quantum
$ code .
```

- サンプルを実行

量子テレポーテーションのサンプルを動かしてみます

```
$ cd Samples/Teleportation/
$ dotnet run
```

以下のような出力がされたらQ#で開発するための環境が整っています

```
Round 0:        Sent True,      got True.
Teleportation successful!!
Round 1:        Sent False,     got False.
Teleportation successful!!
...
Round 6:        Sent True,      got True.
Teleportation successful!!
Round 7:        Sent False,     got False.
Teleportation successful!!
```

データベース検索らしきサンプルも動かしてみました

```
$ cd ../DatabaseSearch/
$ dotnet run
```

古典的なアルゴリズムと量子アルゴリズムを比較しているのかな？

```
Classical random search for marked element in database.
  Database size: 8.
  Success probability:  0.125

Attempt 99. Success: Zero,  Probability: 0.22 Found database index One, One, Zero
Attempt 199. Success: Zero,  Probability: 0.175 Found database index One, Zero, One
Attempt 299. Success: Zero,  Probability: 0.183 Found database index Zero, One, One
Attempt 399. Success: Zero,  Probability: 0.162 Found database index Zero, One, One
Attempt 499. Success: Zero,  Probability: 0.152 Found database index Zero, One, Zero
Attempt 599. Success: One,  Probability: 0.15 Found database index One, One, One
Attempt 699. Success: Zero,  Probability: 0.146 Found database index Zero, One, Zero
Attempt 799. Success: Zero,  Probability: 0.138 Found database index Zero, One, One
Attempt 899. Success: Zero,  Probability: 0.132 Found database index Zero, One, Zero
Attempt 999. Success: Zero,  Probability: 0.132 Found database index Zero, Zero, Zero


Press any key to continue...





Quantum search for marked element in database.
  Database size: 64.
  Classical success probability: 0.015625
  Queries per search: 7
  Quantum success probability: 0.591380150057375

Attempt 9. Success: One,  Probability: 0.6 Speedup: 5.486 Found database index One, One, One, One, One, One
Attempt 19. Success: Zero,  Probability: 0.6 Speedup: 5.486 Found database index One, One, Zero, Zero, One, One
Attempt 29. Success: Zero,  Probability: 0.567 Speedup: 5.184 Found database index One, Zero, Zero, One, One, Zero
Attempt 39. Success: One,  Probability: 0.625 Speedup: 5.714 Found database index One, One, One, One, One, One
Attempt 49. Success: One,  Probability: 0.66 Speedup: 6.034 Found database index One, One, One, One, One, One
Attempt 59. Success: One,  Probability: 0.633 Speedup: 5.787 Found database index One, One, One, One, One, One
Attempt 69. Success: One,  Probability: 0.643 Speedup: 5.879 Found database index One, One, One, One, One, One
Attempt 79. Success: Zero,  Probability: 0.662 Speedup: 6.053 Found database index Zero, Zero, Zero, Zero, One, Zero
Attempt 89. Success: Zero,  Probability: 0.656 Speedup: 5.998 Found database index Zero, One, One, One, One, One
Attempt 99. Success: Zero,  Probability: 0.63 Speedup: 5.76 Found database index One, Zero, One, One, One, One


Press any key to continue...





Quantum search for marked element in database.
  Database size: 256.
  Marked elements: 0,39,101,234  Classical success probability: 0.015625
  Queries per search: 7
  Quantum success probability: 0.591380150057375

Attempt 0. Success: One,  Probability: 1 Speedup: 9.143 Found database index 0
Attempt 1. Success: Zero,  Probability: 0.5 Speedup: 4.571 Found database index 68
Attempt 2. Success: One,  Probability: 0.667 Speedup: 6.098 Found database index 101
Attempt 3. Success: One,  Probability: 0.75 Speedup: 6.857 Found database index 101
Attempt 4. Success: Zero,  Probability: 0.6 Speedup: 5.486 Found database index 165
Attempt 5. Success: Zero,  Probability: 0.5 Speedup: 4.571 Found database index 33
Attempt 6. Success: One,  Probability: 0.571 Speedup: 5.221 Found database index 234
Attempt 7. Success: Zero,  Probability: 0.5 Speedup: 4.571 Found database index 118
Attempt 8. Success: Zero,  Probability: 0.444 Speedup: 4.059 Found database index 80
Attempt 9. Success: One,  Probability: 0.5 Speedup: 4.571 Found database index 101


Press any key to continue...
```

# おわりに

すごく簡単に開発環境を構築できました

サンプルが沢山用意されていたので、まずはその辺を色々動かしてみようかと思います
公式のドキュメントがかなり充実しているので、一通り読んでみるといいかもです