Quantum KatasのQ&A
今までのQ#のプログラミングコンテストを参考にして,知識の整理もかねて記事を書いていこうとと思います.(自分のための備忘録としてが強いのでご注意ください.間違えていた場合は教えてくださるとうれしいです.)
https://qiita.com/tehishik/items/195b93a194c24ba5fb73
まずはこの記事をみて基本的なゲートの処理を毎回確認するようにすればだいぶわかってきます.
直感的な理解だけでなく,いろいろ数式による理解の方法などもわかってくる部分が出てきたので,なるべく複数のアプローチで説明できるように心がけています.(躓いたところの説明が多いです.)
Superposition
adjoint self/auto/invert ってなんや?
quantum katasの解答をみると,qubitのもろもろの計算のあとに,
adjoint self
adjoint auto
adjoint invert
などが解答として書かれているんですね.superpositionの解答のところでは,adjoint invertとなっていたので,2qubitのときはそれらの直積を計算することになるので,それを計算しているのだと思われます.
(これがわからなかったのでどこで直積計算してるんだ...とだいぶ躓いてしまいました.superpositionのタスクでこれを知らないと答えを間違えてしまうというところがあると思います.)
// Task 4. Superposition of all basis vectors on two qubits
operation AllBasisVectors_TwoQubits_Reference (qs : Qubit[]) : Unit {
body (...) {
// Since a Hadamard gate will change |0⟩ into |+⟩ = (|0⟩ + |1⟩)/sqrt(2)
// And the desired state is just a tensor product |+⟩|+⟩, we can apply
// a Hadamard transformation to each qubit.
H(qs[0]);
H(qs[1]);
}
adjoint invert;
}
Measurement
H⊗Hを作用させた2量子ビット?(Measurement)
measurement_task1.11がわからん.
// Task 1.11*. Distinguish four orthogonal 2-qubit states
// Input: two qubits (stored in an array) which are guaranteed to be in one of the four orthogonal states:
// |S0⟩ = (|00⟩ + |01⟩ + |10⟩ + |11⟩) / 2
// |S1⟩ = (|00⟩ - |01⟩ + |10⟩ - |11⟩) / 2
// |S2⟩ = (|00⟩ + |01⟩ - |10⟩ - |11⟩) / 2
// |S3⟩ = (|00⟩ - |01⟩ - |10⟩ + |11⟩) / 2
// Output: 0 if qubits were in |S0⟩ state,
// 1 if they were in |S1⟩ state,
// 2 if they were in |S2⟩ state,
// 3 if they were in |S3⟩ state.
// The state of the qubits at the end of the operation does not matter.
operation TwoQubitState (qs : Qubit[]) : Int {
// ...
return -1;
}
//解答
// Task 1.11*. Distinguish four orthogonal 2-qubit states
// Input: two qubits (stored in an array) which are guaranteed to be in one of the four orthogonal states:
// |S0⟩ = (|00⟩ + |01⟩ + |10⟩ + |11⟩) / 2
// |S1⟩ = (|00⟩ - |01⟩ + |10⟩ - |11⟩) / 2
// |S2⟩ = (|00⟩ + |01⟩ - |10⟩ - |11⟩) / 2
// |S3⟩ = (|00⟩ - |01⟩ - |10⟩ + |11⟩) / 2
// Output: 0 if qubits were in |S0⟩ state,
// 1 if they were in |S1⟩ state,
// 2 if they were in |S2⟩ state,
// 3 if they were in |S3⟩ state.
// The state of the qubits at the end of the operation does not matter.
operation TwoQubitState_Reference (qs : Qubit[]) : Int {
// These states are produced by H ⊗ H, applied to four basis states.
// To measure them, apply H ⊗ H followed by basis state measurement
// implemented in BasisStateMeasurement_Reference.
H(qs[0]);
H(qs[1]);
return BasisStateMeasurement_Reference(qs);
}
例えば,$|S_0>$は$$|S_0>= \frac{|0>+|1>}{\sqrt{2}} ⊗\frac{|0>+|1>}{\sqrt{2}}$$なので,1量子目も2量子目も同じ$\frac{|0>+|1>}{\sqrt{2}}$であり,これらに対してそれぞれアダマール変換すると,まず1量子目が$|0>$になることはわかるし,2量子目もおなじ$|0>$になることもわかる.このようにしてそれぞれの量子ビットをアダマール変換させたものを,それぞれの量子ビットごとに測定してしまえば,ほかの$|S_1>$なども測定できてしまうよね,ということなのをやっと理解できました.
これの特徴なのが,$|S_0>$などが,エンタングルメントされた状態のままで測定するのではなく,Q#上ではそれぞれのビットごとに扱えてしまう,というプログラミング上の特徴によって実現できてしまっている,という点なのかなと考えています.(本来エンタングルメント状態の測定は2量子ビットで構成されたベル基底状態でしか測定はできないのではないか,と思ったのですがどうなのでしょう?)
解答で疑問に思ったのが,H⊗Hを作用させたものを考えればいい,というように書いてある点です.これについては2つの計算方法があります.例えば,(本当は|00>を2量子ビットのすべての重ね合わせ状態のものに置き換えて計算する)
$$(H⊗H)|00>= (H |0>)⊗(H|0>)$$として計算するか,
H⊗H = \frac{1}{\sqrt{2}}\begin{pmatrix}
1 & 1 \\
1 & -1
\end{pmatrix} ⊗\frac{1}{\sqrt{2}}\begin{pmatrix}
1 & 1 \\
1 & -1 \end{pmatrix} = \frac{1}{2} \begin{pmatrix}
1 & 1 & 1 & 1 \\
1 & -1 & 1 & -1 \\
1 & 1 & -1 & -1 \\
1 & -1 & -1 & 1 \end{pmatrix}
なので,これに
(H⊗H)|00> = (H⊗H)\begin{pmatrix}1\\0\\0\\0 \end{pmatrix}
を行列計算してあげれば,あまりきれいではない計算結果を得ることができると思います.今回の問題がうまくいくのは,やはり前者の方法において,$H⊗(\frac{|0>+|1>}{\sqrt{2}})$などの計算が簡単にいくのがきれいな点なんだろうと思います.
2ビット目に先にアダマール?(Measurement)
measurement_task1.12がわからん.
// Task 1.12**. Distinguish four orthogonal 2-qubit states, part two
// Input: two qubits (stored in an array) which are guaranteed to be in one of the four orthogonal states:
// |S0⟩ = ( |00⟩ - |01⟩ - |10⟩ - |11⟩) / 2
// |S1⟩ = (-|00⟩ + |01⟩ - |10⟩ - |11⟩) / 2
// |S2⟩ = (-|00⟩ - |01⟩ + |10⟩ - |11⟩) / 2
// |S3⟩ = (-|00⟩ - |01⟩ - |10⟩ + |11⟩) / 2
// Output: 0 if qubits were in |S0⟩ state,
// 1 if they were in |S1⟩ state,
// 2 if they were in |S2⟩ state,
// 3 if they were in |S3⟩ state.
// The state of the qubits at the end of the operation does not matter.
operation TwoQubitStatePartTwo (qs : Qubit[]) : Int {
// ...
return -1;
}
実際に計算してみる.
2量子ビット目に先にアダマール変換して,$|S_0> = ( |00⟩ - |01⟩ - |10⟩ - |11⟩) / 2$を計算してみる.
|00>は$(|01>+|00>)/\sqrt{2}$となり,
|01>は$(|00>-|01>)/\sqrt{2}$,
|10>は$(|10>+|11>)/\sqrt{2}$,
|11>は$(|10>-|11>)/\sqrt{2}$となる.
この4つから$|S_0>$を計算すると,たしかにベル基底状態のひとつである$(|01> -|10>)/\sqrt{2}$がでてくる.
(これ実際に計算しないと全然わからなかった.)
これで一度ベル基底状態の4つがつくれるらしく,そこからCNOTで位相反転させたりしていけばうまく|00>みたいなものだけにできるので判定ができるようになるらしい.ある程度納得できた.
operation TwoQubitStatePartTwo_Reference (qs : Qubit[]) : Int {
// Try this!
H(qs[1]);
// Now, each of the four input states has been converted to a Bell
// state:
// |S0⟩ ↦ |Ψ⁻⟩ = (|01⟩ - |10⟩) / sqrt(2) これが実際に今計算したやつ
// |S1⟩ ↦ |Ψ⁺⟩ = (|01⟩ + |10⟩) / sqrt(2)
// |S2⟩ ↦ |Φ⁻⟩ = (|00⟩ - |11⟩) / sqrt(2)
// |S3⟩ ↦ |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2)
// We can refer now to task 1.10 (what follows is an alternate solution
// to 1.10, with some tweaks to reorder the output values).
CNOT(qs[0], qs[1]);
H(qs[0]);
mutable m1 = 1;
if (M(qs[0]) == One) {
set m1 = 0;
}
mutable m2 = 1;
if (M(qs[1]) == One) {
set m2 = 0;
}
return m2 * 2 + m1;
}
Teleportation
量子プログラミングにおけるTeleportationがわからん(Teleportation)
https://qiita.com/tehishik/items/8d1d9bae93a722ed24df
量子テレポーテーションに関するtaskについてはある程度こちらの記事でも解説されています.こちらで触れていないことやわからなかったところを以下に書いていきます.
Teleportation_task1.2について,送信者とメッセージの二つの量子ビットに対して行う処理が公式マニュアルにはのっているのですが,これがそもそもどうしてそうするのか腑に落ちません.
// Task 1.2. Send the message (Alice's task)
// Entangle the message qubit with Alice's qubit
// and extract two classical bits to be sent to Bob.
// Inputs:
// 1) Alice's part of the entangled pair of qubits qAlice.
// 2) The message qubit qMessage.
// Output:
// Two classical bits Alice will send to Bob via classical channel as a tuple of Bool values.
// The first bit in the tuple should hold the result of measurement of the message qubit,
// the second bit - the result of measurement of Alice's qubit.
// Represent measurement result 'One' as 'True' and 'Zero' as 'False'.
// The state of the qubits in the end of the operation doesn't matter.
operation SendMessage (qAlice : Qubit, qMessage : Qubit) : (Bool, Bool) {
// ...
return (false, false);
}
// Task 1.2. Send the message (Alice's task)
operation SendMessage_Reference (qAlice : Qubit, qMessage : Qubit) : (Bool, Bool) {
CNOT(qMessage, qAlice);
H(qMessage); //このあたりの処理が腑に落ちない
return (M(qMessage) == One, M(qAlice) == One);
}
AliceとBobでエンタングル状態にある量子ビットのうち,さらに,AliceとMessageのqubitとをエンタングルさせたとき, Aliceとmessageをそれぞれ測定して,それらが|0>か|1>かによるbool値を返す,ということをすればよいようです.
量子テレポーテーションについては,いろいろ誤解されて一般的には理解されているところも多いような気もしますが,https://docs.microsoft.com/en-us/quantum/techniques/putting-it-all-together?view=qsharp-preview#step-2-send-the-message では,量子テレポーテーションに必要なプロセスが順番にtaskとしてQ#で記述できるように構成されていてとても理解の助けになるな,と思いました.
これも実際に計算して確かめてしまえば理解はできるようになると思われます.
まず,エンタングル状態にあるAliceとBobの2量子ビットはたとえばどちらも|0>である状態でエンタングルさせると,(これがteleportation_task1.1でやっていることですが,)
$$|\phi ^+>=(|00>+|11>)/\sqrt{2}$$となっています.(これはベル基底状態のひとつです.)ここで,messageのビットが
$$|\psi> = \alpha|0>+\beta|1>$$とかけるとき,AliceがMessageとエンタングルした状態は,
$$|\psi>|\phi^+> = (\alpha/\sqrt{2})(|000>+|011>)+(\beta/\sqrt{2})(|100>+|111>)$$と書くことができます.ここまでの計算はゲート処理に関係がない計算です.|i j k>において,iがmessageのビット,jがAliceのビット,kがBobのビットになっていることに注意が必要です.
コードをみると,先にmessageを制御ビットにして,AliceとのCNOTゲートを通していることがわかります.これをすると実際には(1量子目が制御ビットになって,2量子目のAliceのビットが変換されることになるので)
$$|\psi>|\phi^+> = (\alpha/\sqrt{2})(|000>+|011>)+(\beta/\sqrt{2})(|110>+|101>)$$
となることがわかると思います.
では,次にコードではmessageに対してHadamard gateを通していることがわかります.これは,1量子目のビットに注目して,Hadamard gateを通したものを代入して計算してしまえばいいことがわかります.
これの処理については,上の式をみてもらうと,2ビット目と3ビット目で00>,11>,10>,01>になっており,Hadamard gateでは
|0>は$(|0>+|1>)/\sqrt{2}$に,
|1>は$(|0>-|1>)/\sqrt{2}$になることを思い出せば,|i j k>であらわされる$2^3$個の状態がすべてでてくる計算になることがイメージできると思います.あとは対称性をイメージしてもらえれば整った計算結果が見えてくると思います.
ここまでのCNOTとHの操作によって,エンタングルされた量子状態をつくることができ,次のプロセスの土台をつくることができました.
ここで作った量子状態の,|i j k>のうちiとjを測定することによって$2^3$個のうちの状態を2個の状態に収束させることができる,というのがこのプロセスでのポイントとなります.
(たとえば,00が測定されたら,Bobの状態は$\alpha|0>+\beta|1>$に収束します)
(計算ではたしかにエンタングル状態になっているな,とはわかるのですが,自分で考えるのは難しいようにおもいます.)
ここまでの計算はたしかにそうなるな,という確認になってしまいましたが,本来のここで送信者が行うべき操作は,1.messageとAliceの2量子ビットでつくられる状態を測定することで,2.4つのベル基底状態のうちのどの状態になっているか測定して(ベル状態測定),3.それによって副次的にBobの量子ビットの状態を収束させて,4.ベル状態測定の結果を送信者が古典通信によってBobに送信して,5.それの結果によってBobの量子ビットに適切な変換をすることによってmessageと同じ情報をつくってしまう,という量子テレポーテーションの流れのうちの1と2(および3)をやることになります.
ベル状態測定の部分が,ここでみたようにCNOTとHの操作によって|i j k>のi,jの測定によって(本来のベル状態測定では2量子ビットの重ね合わせ状態なので,たとえば|00>と|11>の重ね合わせ状態なので,単純にiとjを一度だけ測定したのでは状態を決定できない.)
Bobの量子ビットの状態を収束できてしまう,というのが特徴的な点だと思います.
// Task 1.3. Reconstruct the message (Bob's task)
// Transform Bob's qubit into the required state using the two classical bits
// received from Alice.
// Inputs:
// 1) Bob's part of the entangled pair of qubits qBob.
// 2) The tuple of classical bits received from Alice,
// in the format used in task 1.2.
// Goal: transform Bob's qubit qBob into the state in which the message qubit had been originally.
operation ReconstructMessage (qBob : Qubit, (b1 : Bool, b2 : Bool)) : Unit {
// ...
//解答
// Task 1.3. Reconstruct the message (Bob's task)
operation ReconstructMessage_Reference (qBob : Qubit, (b1 : Bool, b2 : Bool)) : Unit {
if (b1) {
Z(qBob); //ビット反転
}
if (b2) {
X(qBob); //位相反転
}
}
}
続いてこのtask1.3では,前述の4,5の操作を実行していることになります.前述したように古典通信による2ビットの結果をもとにして,Bobの量子ビットにおいて,もとのmessageの状態$\alpha|0>+\beta|1>$を再現してあげればいいだけです.Zが位相反転,Xがビット反転であることに注意すれば,このように記述できることがわかると思います.
task1.1,task1.2,task1.3を順番に実行してあげれば,先ほど述べた量子テレポーテーションのプロセスをすべて実行することができます.
ここで注意すべきなのは最初のエンタングル状態が$(|00>+|11>)/\sqrt{2}$であったのでこの部分を変えたものがtask2.1以降で聞かれている,ということです.
Eigenstate(固有状態)のboolとは?(Teleportation)
// Task 1.5. Prepare a state and send it as a message (Alice's task)
// Given a Pauli basis along with a state 'True' as 'One' or 'False'
// as 'Zero' prepare a message qubit, entangle it with Alice's qubit,
// and extract two classical bits to be sent to Bob.
// Inputs:
// 1) Alice's part of the entangled pair of qubits qAlice.
// 2) A PauliX, PauliY, or PauliZ basis in which the message
// qubit should be prepared
// 3) A Bool indicating the eigenstate in which the message
// qubit should be prepared
// Output:
// Two classical bits Alice will send to Bob via classical channel as a tuple of Bool values.
// The first bit in the tuple should hold the result of measurement of the message qubit,
// the second bit - the result of measurement of Alice's qubit.
// Represent measurement result 'One' as 'True' and 'Zero' as 'False'.
// The state of the qubit qAlice in the end of the operation doesn't matter.
operation PrepareAndSendMessage (qAlice : Qubit, basis : Pauli, state : Bool) : (Bool, Bool) {
// ...
return (false, false);
}
上記のtaskの,A bool indicating the eigenstate in which the message qubit should be preparedの部分の意味がよくわかりませんでした.
これを考える前にpauli gateの復習をすると,
\sigma_X = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}\\
\sigma_Z = \begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix}\\
\sigma_Y ( = \sigma_Z \sigma_X / i) = \begin{pmatrix} 0 & -i \\ i & 0 \end{pmatrix}
と定義されています.これらの行列の固有値を計算すると,固有値は+1か-1になることがわかります.これをboolで表現しなさい,と言っているのではないかと思います.
stateのboolは最初の|0>か|1>のどちらの固有状態であるかを言っているだけで,それに対して,pauli行列をかけることによって,さっきのtask1.3ではmessageの状態は$\alpha$と$\beta$が未定のままだったのが,具体的な重ね合わせ状態として記述できて,それを送信するよ,ということをやっているtaskなだけだったようです.
// Task 1.5. Prepare the message specified and send it (Alice's task)
operation PrepareAndSendMessage_Reference (qAlice : Qubit, basis : Pauli, state : Bool) : (Bool, Bool) {
mutable classicalBits = (false, false);
using (qs = Qubit[1]) {
if (state) {
X(qs[0]);//0なら|0>で,1なら|1>にする処理 ビット反転
}
PrepareQubit(basis, qs[0]); //|0>か|1>に対して,basisで与えられるpauli行列のいずれかをかける処理
set classicalBits = SendMessage_Reference(qAlice, qs[0]);//ここまでの処理によってつくられたmessageを送信する処理 task1.3
Reset(qs[0]);
}
return classicalBits;
}
// Task 1.6. Reconstruct and measure the message state (Bob's task)
// Transform Bob's qubit into the required state using the two classical bits
// received from Alice and measure it in the same basis in which she prepared the message.
// Inputs:
// 1) Bob's part of the entangled pair of qubits qBob.
// 2) The tuple of classical bits received from Alice,
// in the format used in task 1.5.
// 3) The PauliX, PauliY, or PauliZ basis in which the
// message qubit was originally prepared
// Output:
// A Bool indicating the eigenstate in which the message qubit was prepared, 'One' as
// 'True' and 'Zero' as 'False'.
// Goal: transform Bob's qubit qBob into the state in which the message qubit was originally
// prepared, then measure it. The state of the qubit qBob in the end of the operation doesn't matter.
operation ReconstructAndMeasureMessage (qBob : Qubit, (b1 : Bool, b2 : Bool), basis : Pauli) : Bool {
// ...
return false;
}
次のこのtask1.6は,要は「さっきAliceが具体的に送ったmessageって,もともとはstateのboolから作ってたと思うけど,そのboolをBobが出力できるようにしてね」というtaskです.
// Task 1.6. Reconstruct the message and measure it (Bob's task)
operation ReconstructAndMeasureMessage_Reference (qBob : Qubit, (b1 : Bool, b2 : Bool), basis : Pauli) : Bool {
ReconstructMessage_Reference(qBob, (b1, b2)); //さっきやったtask1.3です
return Measure([basis], [qBob]) == One;
}
ReconstructMessage_Referenceによって再現された,送信時のmessageと同じqBobの状態を,pauliの基底によって測定すれば,もとのboolがわかるので,|0>だったのか|1>だったのかが最終的にわかる,という流れになります.(送信するmessageを古典の1ビットから量子的な1量子ビットをつくっていく,というプロセスを踏んでいるのは面白いですね.)
M関数とMeasure関数の区別
ここでM関数とMeasure関数の違いに注意する必要があります.https://docs.microsoft.com/en-us/qsharp/api/prelude/microsoft.quantum.primitive.m?view=qsharp-preview
Measure関数で行っていることが,いわゆる基底測定と呼ばれるものです.では基底測定はどうやって計算するのか具体的にみていきましょう.基底測定は確率の測定ともいえます.この確率を考えるには2つあって,1つ目がBloch球によって測定する基底の平面に射影する方法と,2つ目が地道に複素数を計算する方法です.
2つ目の地道に計算する方法を説明します.例えば,Measure([basis],[qBob])において,測定される対象であるqBobが$$|\psi>=\alpha|0>+\beta|1> =: (\alpha,\beta)^T=(1/\sqrt{3},\sqrt{2}/\sqrt{3})^T$$
とかけるとしましょう.これを$\sigma_X$の固有状態である$$<+|=(<0|+<1|)/\sqrt{2}=(1/\sqrt{2},1/\sqrt{2})$$で測定した場合,$$< +||\psi>= 1/\sqrt{6}+\sqrt{2}/\sqrt{6}$$
なので,測定値がZeroである確率は,$$|< +|\psi>|^2=|1/\sqrt{6}+\sqrt{2}/\sqrt{6}|^2$$を計算することになります.1と出力される確率は1.0からこの確率を引けばいいです.(ここで,絶対値の2乗は複素数で計算しなければいけないことに注意.)
練習としてqBobが$$|\psi>=(1/\sqrt{2},-1/\sqrt{2})^T$$のときは,
$$<+||\psi>=1/2 - 1/2= 0$$となるので,Measure([basis],[qBob])が[basis]=<+|,[qBob]=|->のときZeroと出力される確率が0.0でOneと出力される確率が1.0であることがわかります.よってこのときはもとのboolで送信者が送信していたのは|1>となる1であった,ということがわかります.このようにして,[basis],[qBob]が与えられたときに,上記のように計算していけば確率が計算できて,Zeroと出力される確率とOneと出力される確率がわかる,というわけです.
(関数の出力結果が確率的だ,というのが量子プログラミングの面白いところですね.)
そしてM(qubit:Qubit)は<0|によるqubitの状態測定をするという定義なので,Measure([PauliZ],[qubit])と同じ計算をしていることがわかります.
Task2.
Task2.1以降は,AliceとBobのエンタングル状態が今までと異なっているのと,Messageとの重ね合わせ状態について,CNOTとHゲートの操作をした結果を実際に書き出してあげれば,$$|\psi> = \alpha|0> + \beta|1>$$をつくるために,あとはパズル的にXとZゲートをかける対象を考えていけば解けます.
AliceとBobのエンタングル状態が異なっていれば,それに応じた古典2ビットの通信結果から加える変換が異なるというのは特筆すべき点ですね.
Principle of deferred measurement(遅延測定の原理)とは?
Task3.1ではPrinciple of deferred measurementがテーマになっているようです.
//////////////////////////////////////////////////////////////////
// Part III. Principle of deferred measurement
//////////////////////////////////////////////////////////////////
// The principle of deferred measurement claims that measurements can be moved
// from an intermediate stage of a quantum circuit to the end of the circuit.
// If the measurement results are used to perform classically controlled operations,
// they can be replaced by controlled quantum operations.
// In this task we will apply this principle to the teleportation circuit.
// Task 3.1. Measurement-free teleportation.
// Inputs:
// 1) The two qubits qAlice and qBob in |Φ⁺⟩ state.
// 2) The message qubit qMessage in the state |ψ⟩ to be teleported.
// Goal: transform Bob's qubit qBob into the state |ψ⟩ using no measurements.
// At the end of the operation qubits qAlice and qMessage should not be entangled with qBob.
operation MeasurementFreeTeleport (qAlice : Qubit, qBob : Qubit, qMessage : Qubit) : Unit {
// ...
}
//Answer
// Task 3.1. Measurement-free teleportation.
operation MeasurementFreeTeleport_Reference (qAlice : Qubit, qBob : Qubit, qMessage : Qubit) : Unit {
// The first part of the circuit is similar to Alice's part, but without measurements.
CNOT(qMessage, qAlice);
H(qMessage); //ここまでは今までと同じ
// Classically controlled gates applied by Bob are replaced by controlled gates
Controlled Z([qMessage], qBob);
//古典的な制御演算(今までだとif文を使っていた)を
//このような制御ビットを用いた条件付き量子演算に書き換えることができる
Controlled X([qAlice], qBob);
}
要は,回路の途中にある測定は,回路の最後に測定することができるよ,っていう原理のことらしいです.
Teleportation With Three entangled qubits?
3量子ビットでエンタングルさせた状態で情報を伝達する場合どうすればいいのでしょうか.
//////////////////////////////////////////////////////////////////
// Part IV. Teleportation with three entangled qubits
//////////////////////////////////////////////////////////////////
// Quantum teleportation using entangled states other than Bell pairs is also feasible.
// Here we look at just one of many possible schemes - in it a state is transferred from
// Alice to a third participant Charlie, but this may only be accomplished if Charlie
// has the trust of the second participant Bob.
// Task 4.1*. Entangled trio
// Input: three qubits qAlice, qBob, and qCharlie, each in |0⟩ state.
// Goal: create an entangled state |Ψ³⟩ = (|000⟩ + |011⟩ + |101⟩ + |110⟩) / 2 on these qubits.
//
// In the context of the quantum teleportation protocol, this is the preparation step:
// qubits qAlice, qBob, and qCharlie will be sent to Alice, Bob, and Charlie respectively.
operation EntangleThreeQubits (qAlice : Qubit, qBob : Qubit, qCharlie : Qubit) : Unit {
// ...
}
// Task 4.2*. Reconstruct the message (Charlie's task)
// Alice has a message qubit in the state |ψ⟩ to be teleported, she has entangled it with
// her own qubit from |Ψ³⟩ in the same manner as task 1.2 and extracted two classical bits
// in order to send them to Charlie. Bob has also measured his own qubit from |Ψ³⟩ and sent
// Charlie the result.
//
// Transform Charlie's qubit into the required state using the two classical bits
// received from Alice, and the one classical bit received from Bob.
// Inputs:
// 1) Charlie's part of the entangled trio of qubits qCharlie.
// 2) The tuple of classical bits received from Alice,
// in the format used in task 1.2.
// 3) A classical bit resulting from the measurement of Bob's qubit.
// Goal: transform Charlie's qubit qCharlie into the state in which the message qubit had been originally.
operation ReconstructMessageWhenThreeEntangledQubits (qCharlie : Qubit, (b1 : Bool, b2 : Bool), b3 : Bool) : Unit {
// ...
//Answer
// Task 4.1. Entangled trio
operation EntangleThreeQubits_Reference (qAlice : Qubit, qBob : Qubit, qCharlie : Qubit) : Unit {
body (...) {
H(qAlice);
H(qBob);
X(qCharlie);
CCNOT(qAlice, qBob, qCharlie);
X(qAlice);
X(qBob);
CCNOT(qAlice, qBob, qCharlie);
X(qAlice);
X(qBob);
}
adjoint invert;
}
// Task 4.2. Reconstruct the message (Charlie's task)
operation ReconstructMessageWhenThreeEntangledQubits_Reference (qCharlie : Qubit, (b1 : Bool, b2 : Bool), b3 : Bool) : Unit {
if (b1) {
Z(qCharlie);
}
if (b2) {
Z(qCharlie);
Y(qCharlie);
}
if (b3) {
X(qCharlie);
}
}
(追記します)
Superdense Coding
超稠密符号と呼ばれているものに関するtaskについてです.
量子テレポーテーションではエンタングル状態の2量子ビットに対して,古典2ビットを送信して1量子ビットを再現していたのに対し,
超稠密符号ではエンタングル状態の2量子ビットに対して,1量子ビットを送信して古典2ビットを再現するというものです(実際には,送信先にすでに1量子ビットを送っていることになるので,2量子ビットをおくることになっている.).
(しかし,量子ビットの送信は現在の技術的には誤りを生じてしまう確率が高い,というのは注意が必要です.)
前者の利点は,長距離の通信でも量子ビットの情報を伝えることができるという点(長距離だと量子ビットを送るのは難しい)にあるのに対し,
後者の利点は,先にエンタングル状態にある受信者の1量子ビットを保持させておいて,再現したい古典2ビットに応じて変換させた送信者の1量子ビットを送ることで情報を伝達することになるので,実質2倍の速度で情報通信することができる,
という違いがあります.
具体的なtaskを見ていくことにしましょう.
Task1 エンタングル状態をつくる
// Task 1. Entangled pair
// Input: An array of two qubits in the |00⟩ state.
// Goal: Create a Bell state |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2) on these qubits.
operation CreateEntangledPair (qs : Qubit[]) : Unit {
// The following lines enforce the constraints on the input that you are given.
// You don't need to modify them. Feel free to remove them, this won't cause your code to fail.
AssertIntEqual(Length(qs), 2, "The array should have exactly 2 qubits.");
// ...
}
//Answer
// Task 1. Entangled pair
operation CreateEntangledPair_Reference (qs : Qubit[]) : Unit {
body (...) {
H(qs[0]);
CNOT(qs[0], qs[1]);
// |00⟩から |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2)をつくるのはこれらの操作で十分です
//その他のベル状態をつくるには工夫が必要です
}
adjoint invert; //qs[0]とqs[1]の直積をつくるのでここも必要
}
Task2 古典2ビットに応じてAliceの量子ビットを変化させる
1.送信者が送りたい古典2ビットの情報に応じた量子ビットに送信者の量子ビットを変化させてからその量子ビットを送信すれば,2.受信者は送られた量子ビットを測定することによって古典2ビットがわかる,という流れのうちの1がこのTaskになります.
// Task 2. Send the message (Alice's task)
operation EncodeMessageInQubit_Reference (qAlice : Qubit, message : Bool[]) : Unit {
// We are starting this step with the entangled pair in state |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2).
// By doing operations on one of those qubits,
// we can encode each of the values as a transformation:
// "00" as I and |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2)
// "01" as X and |Ψ⁺⟩ = (|01⟩ + |10⟩) / sqrt(2) //2ビット目がtrueならビット反転(X)
// "10" as Z and |Φ⁻⟩ = (|00⟩ - |11⟩) / sqrt(2)
// "11" as Y and |Ψ⁻⟩ = (|01⟩ - |10⟩) / sqrt(2)//1ビット目がtrueなら位相反転(Z)
// Also, since Y(q) = iX(Z(q)), we can express this shorter:
if (message[0]) {
Z(qAlice);
}
if (message[1]) {
X(qAlice);
}
}
Task3
前述したうちの2のプロセスをやることになります.ここで考えるべきなのはベル状態の4つのうちのどれかを判定する方法だけです.
// Task 3. Decode the message (Bob's task)
operation DecodeMessageFromQubits_Reference (qBob : Qubit, qAlice : Qubit) : Bool[] {
// Declare a Bool array in which the result will be stored;
// the array has to be mutable to allow updating its elements.
mutable decoded_bits = new Bool[2]; //Bobが再現することになる古典2ビット
// Time to get our state back, by performing transformations as follows.
// Notice that it's important to keep the order right. The qubits that are
// subject to the Hadamard transform and the CNOT gate in the preparation
// of the pair have to match the operations below, or the order of the data
// bits will get flipped.
CNOT(qAlice, qBob);
H(qAlice);
// What is the outcome of this transformation, assuming each of the possible
// quantum states after the encoding step?
// |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2) ---> |00⟩
// |Ψ⁺⟩ = (|01⟩ + |10⟩) / sqrt(2) ---> |01⟩
// |Φ⁻⟩ = (|00⟩ - |11⟩) / sqrt(2) ---> |10⟩
// |Ψ⁻⟩ = (|01⟩ - |10⟩) / sqrt(2) ---> |11⟩
// So we can retrieve the encoded bits just by measuring.
set decoded_bits[0] = M(qAlice) == One; //Measure([PauliZ],qAlice)==Oneでもよい
set decoded_bits[1] = M(qBob) == One;
return decoded_bits;
これはmeasurementのtask1.9のdistinguish four bell statesでやったことから,CNOTとHを使うことで重ね合わせのない状態にできることを利用します.
$|\phi>$か$|\psi>$かがわかっていれば,Measure関数を使ってベル状態を判定できますが,それを使った別解も考えられそうです.
以上のTask1-3を使えば超稠密符号を行うことができます.
Joint Measurement
Joint Measurementでは1量子ビットずつ測定すると状態が変わってしまうので,2量子ビットを測定できるようにMeasure関数を使うことで状態を収束させてしまうことを避けることができるようです.
しかし,Joint Measurementでの測定結果のzeroとoneが出る確率をちゃんと計算できないのかな,と疑問に思いました.
Joint Measurementのやりやすそうな計算方法?
Joint Measurementで使う,Measure([PauliZ,PauliZ],qubit)などの関数をどうやって計算するのかがわからなかったのですが,説明できそうな計算方法がありそうなので以下で説明しようと思います.
Task5のparity measurement in different basisを題材に計算方法を考えていこうと思います.
// Task 5*. Parity measurement in different basis
// Input: Two qubits (stored in an array) which are guaranteed to be
// either in superposition α|00⟩ + β|01⟩ + β|10⟩ + α|11⟩
// or in superposition α|00⟩ - β|01⟩ + β|10⟩ - α|11⟩.
// Output: 0 if qubits were in the first superposition,
// 1 if they were in the second superposition.
// The state of the qubits at the end of the operation should be the same as the starting state.
operation DifferentBasis (qs : Qubit[]) : Int {
// ...
return -1;
}
//Answer
// Task 5*. Parity measurement in different basis
operation DifferentBasis_Reference (qs : Qubit[]) : Int {
// The first state is a superposition of the states |++⟩ and |--⟩,
// which belong to the +1 eigenspace of the operator X ⊗ X;
// the second one is a superposition of |+-⟩ and |-+⟩, which belong to the -1 eigenspace.
return Measure([PauliX, PauliX], qs) == Zero ? 0 | 1;
}
この解答だと,Xのパウリ行列2つのテンソル積による物理量を測定していることになります.
固有射影演算子を利用する
Bornの確率規則において,固有射影演算子を計算するのは,測定するときに使っているパウリ行列に対してです.(測定しているときの状態自体の固有射影演算子を計算するのだと思ってしまっていました...)測定するパウリ行列の固有ベクトルを計算できることがここで一番大事なことです.
固有ベクトル$<\phi|$が計算できれば,$<\phi|\psi>$を計算して,それの絶対値を2乗すれば,固有ベクトルに対応する固有値が測定結果としてでてくる確率を計算することができます.まあ,固有射影演算子というよくわからない名前がでていますが,大事なのは測定しているパウリ行列に対する固有ベクトルです.
具体的にみると,
$$\sigma_X ⊗\sigma_X = \begin{pmatrix} 0 & 0 & 0 & 1 \\
0 & 0 & 1 & 0 \\
0 & 1 & 0 & 0 \\
1 & 0 & 0 & 0\end{pmatrix}$$
の固有ベクトルは,
固有値1のとき,$|\phi> = \frac{1}{2(\alpha^2+\beta^2)}(\alpha,\beta,\beta,\alpha)^T$
固有値-1のとき,$|\phi> = \frac{1}{2(\alpha^2+\beta^2)}(\alpha,-\beta,-\beta,\alpha)^T$
となるようです.(計算は省略)
よって,$|\psi>$がα|00⟩ + β|01⟩ + β|10⟩ + α|11⟩とα|00⟩ - β|01⟩ + β|10⟩ - α|11⟩のとき,
前者においては,固有値1が結果として出てくる確率は,
$$<\phi|\psi>=\frac{\alpha^2 + \beta^2 + \beta^2 + \alpha^2}{2(\alpha^2+\beta^2)}= 1 \ \ \ (\because 2(\alpha^2 + \beta^2 ) = 1)$$より,1の確率で固有値1が結果として出力されます.
後者においては,固有値1が結果として出てくる確率は,
$$<\phi|\psi>=\frac{\alpha^2 + \beta^2 -\beta^2 -\alpha^2}{2(\alpha^2+\beta^2)}=0$$なので,0の確率で固有値1が結果として出力される,つまり1の確率で固有値-1が出力されることがわかります.
2量子ゲートの誤り率を低くできていない,とは
2量子ゲートを使う場合,ゲートの誤り率が現状低くできていないために,1量子ビットのゲートで代用できるようにする,というtask6が用意されています.では,2量子ゲートの誤り率は現状どれくらいになっているのでしょうか.(追記します)
Task7をうまく理解する方法
(追記します)
Quantum error-correction
Task1のParity Measurementsとは
同じパリティ(1の個数が偶数か奇数か,と考えればいい:偶数のときはパリティ0で,奇数の時はパリティ1)の状態にある3量子ビットの状態の重ね合わせ状態が起きている時を考える.
// Task 1. Parity Measurements
operation MeasureParity_Reference (register : Qubit[]) : Result {
return Measure([PauliZ, PauliZ, PauliZ], register);
}
このような解答になるようですが,どんな計算をしているのかがわかりません.
先ほどの固有射影演算子を計算するために,パウリ行列$\sigma_Z$の三つのテンソル積を考えてみると,
$$\sigma_Z ⊗\sigma_Z ⊗\sigma_Z=\begin{pmatrix} 1 &0&0& 0& 0 & 0 & 0 & 0 \\
0 & -1 & 0 & 0 & 0 & 0 & 0 & 0\\
0 &0&-1& 0& 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\
0 &0&0& 0& -1 & 0 & 0 & 0 \\
0 &0 & 0 & 0 & 0 & 1 & 0 & 0\\
0 &0&0& 0& 0 & 0 & 1 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & -1\\
\end{pmatrix}$$
書き出してしまえば,+1の固有値に対応する固有ベクトルが$\frac{1}{2}(1,0,0,1,0,1,1,0)^T$なのも想像しやすいと思います.
こちらのtask4での固有ベクトルの計算の記述がとてもわかりやすかったです.
これも,Joint Measurementのときのように,固有ベクトルによる固有値1を結果として出力する確率の計算することができますが,今回はBornの確率規則の別の定義によって計算してみます.
パリティが0の時の重ね合わせ状態は,
$$|\psi> = \alpha|000>+\beta|011>+\gamma|101>+\delta|110>=(\alpha,0,0,\beta,0,\gamma,\delta,0)^T\ \\ただし\ \alpha^2+\beta^2+\gamma^2+\delta^2 = 1$$
固有射影演算子は
$$A:= \frac{1}{2}(1,0,0,1,0,1,1,0)^T\frac{1}{2}(1,0,0,1,0,1,1,0)\\
=\frac{1}{4} \begin{pmatrix} 1 & 0 & 0 & 1 & 0 & 1 & 1 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
1 & 0 & 0 & 1 & 0 & 1 & 1 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
1 & 0 & 0 & 1 & 0 & 1 & 1 & 0 \\
1 & 0 & 0 & 1 & 0 & 1 & 1 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
\end{pmatrix} $$
よって,
$$<\psi|A|\psi>=<\psi|(\alpha,0,0,\beta,0,\gamma,\delta,0)^T = \alpha^2+\beta^2+\gamma^2+\delta^2 = 1 $$
(計算が合っていません)
Phase Estimation
(随時更新します)