LoginSignup
193
111

More than 5 years have passed since last update.

新言語「Q#」を動かしてみる

Last updated at Posted at 2017-12-23

皆さん、こんにちは。戸倉彩です。

今回は先日、2017年12月11日にマイクロソフトが公開した新しいプログラミング言語「Q#」について、まずはいち早くサンプルを動かしてみたい!という方を対象に、マイクロソフト社がYouTubeで公開したMicrosoft Quantum Development Kit: Introduction and step-by-step demo(英語)の動画で取り扱っている「量子テレポーテーション」のサンプルを動かす方法をご紹介します。
Microsoft Quantum Development Kit: Introduction and step-by-step demo

Q#とは (読み方:キューシャープ)

マイクロソフト社が公開した量子コンピュータ向けプログラミング言語です。現時点では、Windows版 Visual Studio 2017の開発環境に統合され、30理論量子ビットのローカル量子シュミレーターを使うことでローカルのマシン上でデバッグ実行が可能です。
※マイクロソフトのクラウド、Microsoft Azure(読み方:アジュール)を利用することで、さらに大きい40以上の論理量子ビットをシュミレートすることができます。

量子コンピュータとは (Quantum Computer)

1ビットを基本単位として「0」または「1」のいづれかの値で計算する従来のコンピュータに対して、量子コンピュータでは量子力学の特徴である量子の重ね合わせを用いて「00」「01」「10」「11」の重ね合わせで計算を行います。すなわち同じ1ビットの中で「0」と「1」が同時に存在することができます。
※より詳しい技術情報については、「量子」や「量子コンピュータ」等のキーワードで検索して参考にしてみてください。

事前準備

  1. システム要件の確認
  2. Windows版 Visual Studio 2017のインストール 
  3. 開発キット「Microsoft Quantum Development Kit」のインストール

1. システム要件

・Windows 64ビット環境
・AVX(Advanced Vector Extension)対応CPU
・Visual Studio 2017のシステム要件 (https://www.visualstudio.com/ja-jp/productinfo/vs2017-system-requirements-vs)

2. Visual Studio 2017 のインストール

Visual Studio 2017はマイクロソフトが提供する開発ツールです。学生さんや個人の開発者向けからあらゆる規模の企業プロジェクトの開発者向けのエディションが用意されています。ご自身の立場に近いものをご利用ください。
※本内容は個人の開発者向けに無料で提供されているVisual Studio Community 2017でもお試しいただくことが可能です。
※エディションによって利用できる機能が異なりますのでご注意ください。

初めての方は、Visual Studio 2017 ダウンロードサイトより最新版を入手してインストールしてください。

3. 「Microsoft Quantum Development Kit」のインストール

Microsoft Quantum Development Kitにアクセスするためには、最新情報を提供するためのコンタクト先等についてフォーム入力する必要があります。

  1. ブラウザからマイクロソフト社のQuantum Development Kit サイトへアクセスする。

  2. 「Download now」ボタンをクリックする。
     

  3. 各項目を入力(英語)し、「Download now」ボタンをクリックする。
    ➀ First name (名)
    ➁ Last name (姓)
    ➂ Email address (メールアドレス)
    ➃ Company name (会社名) *企業に所属してない場合はnoneと適当に入力
    ➄ Country (国) *ドロップダウンからJapanを選択
    ➅ Company size (従業員数) *企業に所属してない場合は1を選択
    ➆ Job role (職業) *ドロップダウンから選択。企業に所属していない場合や該当する項目が無い場合にはotherを選択
    ➇ Phone (電話番号) *国番号+81に続いて、市外局番先頭の0を外して電話番号を入力
     

  4. Welcome to the Microsoft Quantum Development Kit preview サイトが表示されたら「Download」ボタンをクリックし、ダウンロードを開始する。
     

  5. 「プログラムで開く」または「ファイルを保存する」を選択する。今回は「プログラムで開く」を選択。
     

  6. VSIXインストーラ画面が表示されたら、拡張機能をインストールする製品の対象として「Visual Studio 2017」にチェックが入っていることを確認し、「インストール」をクリックする。

  7. インストールが完了したら「閉じる」をクリックして画面を閉じる。

サンプルコードの実行方法

  1. サンプルコードを入手する
  2. プロジェクトを開く
  3. デバッグ実行する

1. サンプルコードを入手

  1. GitHubのMicrosoft/QuantumリポジトリをクローンまたはZipファイルをダウンロードし展開する。
  2. リポジトリにはドキュメントやサンプルコード、Readmeファイル等が含まれていることを確認する。

2. プロジェクトを開く

今回は冒頭でもお伝えしたように、マイクロソフト社がYouTubeで公開したMicrosoft Quantum Development Kit: Introduction and step-by-step demo(英語)の動画で取り扱っている「量子テレポーテーション」のサンプルを動かしてみたいと思います。
1. Visual Studio 2017を起動する
2. 「ファイル」→「開く」→「フォルダ」メニューを選択する

3. 「Quantum-master」→「Samples」→「Teleportation」の「TeleportationSample.csproj」を選択する
4. ソシューションエクスプローラで「Sample」→「0.Introduction」→「TeleportationSample」を選択し、右クリックして「スタートアッププロジェクトに設定する」を選択する。

3. デバッグ実行する

今回のサンプルでは、ロジック周りを記述したC#(Program.cs)に基づいて8回、Q#(TeleportationSample.qs)で書いた量子プログラムをシミュレーションし、量子ビットにテレポートしたかどうかを検証します。

Program.cs
using Microsoft.Quantum.Simulation.Simulators;
using System.Linq;

namespace Microsoft.Quantum.Examples.Teleportation {
    class Program
    {
        static void Main(string[] args)
        {
            var sim = new QuantumSimulator();
            var rand = new System.Random();

            foreach (var idxRun in Enumerable.Range(0, 8)) {
                var sent = rand.Next(2) == 0;
                var received = TeleportClassicalMessage.Run(sim, sent).Result;
                System.Console.WriteLine($"Round {idxRun}:\tSent {sent},\tgot {received}.");
                System.Console.WriteLine(sent == received ? "Teleportation successful!!\n" : "\n");
            }

            System.Console.WriteLine("\n\nPress Enter to exit...\n\n");
            System.Console.ReadLine();

        }
    }
}

TeleportationSample.qs
namespace Microsoft.Quantum.Examples.Teleportation {
    open Microsoft.Quantum.Primitive;
    open Microsoft.Quantum.Canon;

        operation Teleport(msg : Qubit, there : Qubit) : () {
        body {

            using (register = Qubit[1]) {
                // Ask for an auxillary qubit that we can use to prepare
                // for teleportation.
                let here = register[0];

                // Create some entanglement that we can use to send our message.
                H(here);
                CNOT(here, there);

                // Move our message into the entangled pair.
                CNOT(msg, here);
                H(msg);

                // Measure out the entanglement.
                if (M(msg) == One)  { Z(there); }
                if (M(here) == One) { X(there); }

                // Reset our "here" qubit before releasing it.
                Reset(here);
            }

        }
    }

    operation TeleportClassicalMessage(message : Bool) : Bool {
        body {
            mutable measurement = false;

            using (register = Qubit[2]) {
                // Ask for some qubits that we can use to teleport.
                let msg = register[0];
                let there = register[1];

                // Encode the message we want to send.
                if (message) { X(msg); }

                // Use the operation we defined above.
                Teleport(msg, there);

                // Check what message was sent.
                if (M(there) == One) { set measurement = true; }

                // Reset all of the qubits that we used before releasing
                // them.
                ResetAll(register);
            }

            return measurement;
        }
    }

   }

  1. 「開始」ボタンまたは「F5」で実行を開始する。
  2. デバッグが開始され、コマンドプロンプトが起動し実行画面が表示されば成功です。 ※TrueまたはFalseはランダムとなるため、環境やタイミングによって表示される結果が異なります。

いかがでしたでしょうか。もしよければこちらの記事を読んだ後、ページ内のいいね!を押していただけると今後の励みになります。また近いうちにお会いしましょう。

参考URL

・ニュースリリース(日本語) 「未来は量子コンピューティング:無償の量子コンピューティング開発キットをリリース」
・ニュースリリース(英語) 「The future is quantum: Microsoft releases free preview of Quantum Development Kit」
・Microsoft 量子コンピュータ全般(英語) 「Quantum computing | Microsoft」
・Microsoft 量子ブログサイト(英語) 「Microsoft Quantum」

Have a nice Geek Life♪

※Twitterで最新情報発信中 @ayatokura

193
111
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
193
111