Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
BasicGates: Add new task for adding global phase -1 (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
adutchengineer authored and tcNickolas committed Oct 5, 2019
1 parent 47dfa99 commit 8621795
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 38 deletions.
61 changes: 44 additions & 17 deletions BasicGates/BasicGates.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
"metadata": {},
"outputs": [],
"source": [
"%kata T11_StateFlip_Test \n",
"%kata T101_StateFlip_Test \n",
"\n",
"operation StateFlip (q : Qubit) : Unit is Adj+Ctl {\n",
" // The Pauli X gate will change the |0⟩ state to the |1⟩ state and vice versa.\n",
Expand Down Expand Up @@ -142,7 +142,7 @@
"metadata": {},
"outputs": [],
"source": [
"%kata T12_BasisChange_Test \n",
"%kata T102_BasisChange_Test \n",
"\n",
"operation BasisChange (q : Qubit) : Unit is Adj+Ctl {\n",
" // ...\n",
Expand All @@ -166,7 +166,7 @@
"metadata": {},
"outputs": [],
"source": [
"%kata T13_SignFlip_Test \n",
"%kata T103_SignFlip_Test \n",
"\n",
"operation SignFlip (q : Qubit) : Unit is Adj+Ctl {\n",
" // ...\n",
Expand Down Expand Up @@ -199,7 +199,7 @@
"metadata": {},
"outputs": [],
"source": [
"%kata T14_AmplitudeChange_Test\n",
"%kata T104_AmplitudeChange_Test\n",
"\n",
"operation AmplitudeChange (alpha : Double, q : Qubit) : Unit is Adj+Ctl {\n",
" // ...\n",
Expand All @@ -223,7 +223,7 @@
"metadata": {},
"outputs": [],
"source": [
"%kata T15_PhaseFlip_Test\n",
"%kata T105_PhaseFlip_Test\n",
"\n",
"operation PhaseFlip (q : Qubit) : Unit is Adj+Ctl {\n",
" // ...\n",
Expand Down Expand Up @@ -253,7 +253,7 @@
"metadata": {},
"outputs": [],
"source": [
"%kata T16_PhaseChange_Test\n",
"%kata T106_PhaseChange_Test\n",
"\n",
"operation PhaseChange (alpha : Double, q : Qubit) : Unit is Adj+Ctl {\n",
" // ...\n",
Expand All @@ -264,7 +264,34 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Task 1.7. Bell state change - 1\n",
"### Task 1.7. Global phase change\n",
"**Input:** A qubit in state $|\\psi\\rangle = \\beta |0\\rangle + \\gamma |1\\rangle$.\n",
"\n",
"**Goal**: Change the state of the qubit to $- \\beta |0\\rangle - \\gamma |1\\rangle$.\n",
"\n",
"> Note: this change on its own is not observable - there is no experiment you can do on a standalone qubit to figure out whether it acquired the global phase or not. \n",
"> However, you can use a controlled version of this operation to observe the global phase it introduces. \n",
"> This is used in later katas as part of more complicated tasks."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T107_GlobalPhaseChange_Test\n",
"\n",
"operation GlobalPhaseChange (q : Qubit) : Unit is Adj+Ctl {\n",
" // ...\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Task 1.8. Bell state change - 1\n",
"\n",
"**Input:** Two entangled qubits in Bell state $|\\Phi^{+}\\rangle = \\frac{1}{\\sqrt{2}} \\big(|00\\rangle + |11\\rangle\\big)$.\n",
"\n",
Expand All @@ -277,7 +304,7 @@
"metadata": {},
"outputs": [],
"source": [
"%kata T17_BellStateChange1_Test\n",
"%kata T108_BellStateChange1_Test\n",
"\n",
"operation BellStateChange1 (qs : Qubit[]) : Unit is Adj+Ctl {\n",
" // ...\n",
Expand All @@ -288,7 +315,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Task 1.8. Bell state change - 2\n",
"### Task 1.9. Bell state change - 2\n",
"\n",
"**Input:** Two entangled qubits in Bell state $|\\Phi^{+}\\rangle = \\frac{1}{\\sqrt{2}} \\big(|00\\rangle + |11\\rangle\\big)$.\n",
"\n",
Expand All @@ -301,7 +328,7 @@
"metadata": {},
"outputs": [],
"source": [
"%kata T18_BellStateChange2_Test\n",
"%kata T109_BellStateChange2_Test\n",
"\n",
"operation BellStateChange2 (qs : Qubit[]) : Unit is Adj+Ctl {\n",
" // ...\n",
Expand All @@ -312,7 +339,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Task 1.9. Bell state change - 3\n",
"### Task 1.10. Bell state change - 3\n",
"\n",
"**Input:** Two entangled qubits in Bell state $|\\Phi^{+}\\rangle = \\frac{1}{\\sqrt{2}} \\big(|00\\rangle + |11\\rangle\\big)$.\n",
"\n",
Expand All @@ -325,7 +352,7 @@
"metadata": {},
"outputs": [],
"source": [
"%kata T19_BellStateChange3_Test\n",
"%kata T110_BellStateChange3_Test\n",
"\n",
"operation BellStateChange3 (qs : Qubit[]) : Unit is Adj+Ctl {\n",
" // ...\n",
Expand Down Expand Up @@ -367,7 +394,7 @@
"metadata": {},
"outputs": [],
"source": [
"%kata T21_TwoQubitGate1_Test\n",
"%kata T201_TwoQubitGate1_Test\n",
"\n",
"operation TwoQubitGate1 (qs : Qubit[]) : Unit is Adj {\n",
" // ...\n",
Expand Down Expand Up @@ -395,7 +422,7 @@
"metadata": {},
"outputs": [],
"source": [
"%kata T22_TwoQubitGate2_Test\n",
"%kata T202_TwoQubitGate2_Test\n",
"\n",
"operation TwoQubitGate2 (qs : Qubit[]) : Unit is Adj {\n",
" // ...\n",
Expand All @@ -422,7 +449,7 @@
"metadata": {},
"outputs": [],
"source": [
"%kata T23_TwoQubitGate3_Test\n",
"%kata T203_TwoQubitGate3_Test\n",
"\n",
"operation TwoQubitGate3 (qs : Qubit[]) : Unit is Adj {\n",
" // ...\n",
Expand All @@ -447,7 +474,7 @@
"metadata": {},
"outputs": [],
"source": [
"%kata T24_ToffoliGate_Test\n",
"%kata T204_ToffoliGate_Test\n",
"\n",
"operation ToffoliGate (qs : Qubit[]) : Unit is Adj {\n",
" // ...\n",
Expand All @@ -472,7 +499,7 @@
"metadata": {},
"outputs": [],
"source": [
"%kata T25_FredkinGate_Test\n",
"%kata T205_FredkinGate_Test\n",
"\n",
"operation FredkinGate (qs : Qubit[]) : Unit is Adj {\n",
" // ...\n",
Expand Down
13 changes: 10 additions & 3 deletions BasicGates/ReferenceImplementation.qs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Quantum.Kata.BasicGates {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Math;


//////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -81,8 +82,14 @@ namespace Quantum.Kata.BasicGates {
R1(alpha, q);
}

// Task 1.7. Global Phase Change
// Input: A qubit in state β|0⟩ + γ|1⟩.
// Goal: Change the state of the qubit to - β|0⟩ - γ|1⟩.
operation GlobalPhaseChange_Reference (q: Qubit) : Unit is Adj+Ctl {
R(PauliI, 2.0 * PI(), q);
}

// Task 1.7. Bell state change - 1
// Task 1.8. Bell state change - 1
// Input: Two entangled qubits in Bell state |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2).
// Goal: Change the two-qubit state to |Φ⁻⟩ = (|00⟩ - |11⟩) / sqrt(2).
operation BellStateChange1_Reference (qs : Qubit[]) : Unit is Adj+Ctl {
Expand All @@ -91,7 +98,7 @@ namespace Quantum.Kata.BasicGates {
}


// Task 1.8. Bell state change - 2
// Task 1.9. Bell state change - 2
// Input: Two entangled qubits in Bell state |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2).
// Goal: Change the two-qubit state to |Ψ⁺⟩ = (|01⟩ + |10⟩) / sqrt(2).
operation BellStateChange2_Reference (qs : Qubit[]) : Unit is Adj+Ctl {
Expand All @@ -100,7 +107,7 @@ namespace Quantum.Kata.BasicGates {
}


// Task 1.9. Bell state change - 3
// Task 1.10. Bell state change - 3
// Input: Two entangled qubits in Bell state |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2).
// Goal: Change the two-qubit state to |Ψ⁻⟩ = (|01⟩ - |10⟩) / sqrt(2).
operation BellStateChange3_Reference (qs : Qubit[]) : Unit is Adj+Ctl {
Expand Down
20 changes: 17 additions & 3 deletions BasicGates/Tasks.qs
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,38 @@ namespace Quantum.Kata.BasicGates {
// ...
}

// Task 1.7. Global phase change
// Input: A qubit in state β|0⟩ + γ|1⟩.
// Goal: Change the state of the qubit to - β|0⟩ - γ|1⟩.
//
// Note: This change on its own is not observable -
// there is no experiment you can do on a standalone qubit
// to figure out whether it acquired the global phase or not.
// However, you can use a controlled version of this operation
// to observe the global phase it introduces. This is used
// in later katas as part of more complicated tasks.
operation GlobalPhaseChange (q: Qubit) : Unit is Adj+Ctl {
// ...
}


// Task 1.7. Bell state change - 1
// Task 1.8. Bell state change - 1
// Input: Two entangled qubits in Bell state |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2).
// Goal: Change the two-qubit state to |Φ⁻⟩ = (|00⟩ - |11⟩) / sqrt(2).
operation BellStateChange1 (qs : Qubit[]) : Unit is Adj+Ctl {
// ...
}


// Task 1.8. Bell state change - 2
// Task 1.9. Bell state change - 2
// Input: Two entangled qubits in Bell state |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2).
// Goal: Change the two-qubit state to |Ψ⁺⟩ = (|01⟩ + |10⟩) / sqrt(2).
operation BellStateChange2 (qs : Qubit[]) : Unit is Adj+Ctl {
// ...
}


// Task 1.9. Bell state change - 3
// Task 1.10. Bell state change - 3
// Input: Two entangled qubits in Bell state |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2).
// Goal: Change the two-qubit state to |Ψ⁻⟩ = (|01⟩ - |10⟩) / sqrt(2).
operation BellStateChange3 (qs : Qubit[]) : Unit is Adj+Ctl {
Expand Down
37 changes: 22 additions & 15 deletions BasicGates/Tests.qs
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,25 @@ namespace Quantum.Kata.BasicGates {


// ------------------------------------------------------
operation T11_StateFlip_Test () : Unit {
operation T101_StateFlip_Test () : Unit {
AssertOperationsEqualReferenced(2, ArrayWrapperOperation(StateFlip, _), ArrayWrapperOperation(StateFlip_Reference, _));
}


// ------------------------------------------------------
operation T12_BasisChange_Test () : Unit {
operation T102_BasisChange_Test () : Unit {
AssertOperationsEqualReferenced(2, ArrayWrapperOperation(BasisChange, _), ArrayWrapperOperation(BasisChange_Reference, _));
}


// ------------------------------------------------------
operation T13_SignFlip_Test () : Unit {
operation T103_SignFlip_Test () : Unit {
AssertOperationsEqualReferenced(2, ArrayWrapperOperation(SignFlip, _), ArrayWrapperOperation(SignFlip_Reference, _));
}


// ------------------------------------------------------
operation T14_AmplitudeChange_Test () : Unit {
operation T104_AmplitudeChange_Test () : Unit {
for (i in 0 .. 36) {
let alpha = ((2.0 * PI()) * IntAsDouble(i)) / 36.0;
AssertOperationsEqualReferenced(2, ArrayWrapperOperation(AmplitudeChange(alpha, _), _), ArrayWrapperOperation(AmplitudeChange_Reference(alpha, _), _));
Expand All @@ -62,20 +62,26 @@ namespace Quantum.Kata.BasicGates {


// ------------------------------------------------------
operation T15_PhaseFlip_Test () : Unit {
operation T105_PhaseFlip_Test () : Unit {
AssertOperationsEqualReferenced(2, ArrayWrapperOperation(PhaseFlip, _), ArrayWrapperOperation(PhaseFlip_Reference, _));
}


// ------------------------------------------------------
operation T16_PhaseChange_Test () : Unit {
operation T106_PhaseChange_Test () : Unit {
for (i in 0 .. 36) {
let alpha = ((2.0 * PI()) * IntAsDouble(i)) / 36.0;
AssertOperationsEqualReferenced(2, ArrayWrapperOperation(PhaseChange(alpha, _), _), ArrayWrapperOperation(PhaseChange_Reference(alpha, _), _));
}
}


// ------------------------------------------------------
operation T107_GlobalPhaseChange_Test () : Unit {
AssertOperationsEqualReferenced(2, ArrayWrapperOperation(GlobalPhaseChange, _), ArrayWrapperOperation(GlobalPhaseChange_Reference, _));
}


// ------------------------------------------------------
// 0 - |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2)
// 1 - |Φ⁻⟩ = (|00⟩ - |11⟩) / sqrt(2)
Expand Down Expand Up @@ -119,22 +125,23 @@ namespace Quantum.Kata.BasicGates {
AssertAllZero(qs);
}
}



// ------------------------------------------------------
operation T17_BellStateChange1_Test () : Unit {
operation T108_BellStateChange1_Test () : Unit {
VerifyBellStateConversion(BellStateChange1, 0, 1);
}


// ------------------------------------------------------
operation T18_BellStateChange2_Test () : Unit {
operation T109_BellStateChange2_Test () : Unit {
VerifyBellStateConversion(BellStateChange2, 0, 2);
}


// ------------------------------------------------------
operation T19_BellStateChange3_Test () : Unit {
operation T110_BellStateChange3_Test () : Unit {
VerifyBellStateConversion(BellStateChange3, 0, 3);
}

Expand All @@ -147,7 +154,7 @@ namespace Quantum.Kata.BasicGates {


// ------------------------------------------------------
operation T21_TwoQubitGate1_Test () : Unit {
operation T201_TwoQubitGate1_Test () : Unit {

// Note that the way the problem is formulated, we can't just compare two unitaries,
// we need to create an input state |A⟩ and check that the output state is correct
Expand Down Expand Up @@ -180,7 +187,7 @@ namespace Quantum.Kata.BasicGates {


// ------------------------------------------------------
operation T22_TwoQubitGate2_Test () : Unit {
operation T202_TwoQubitGate2_Test () : Unit {
using (qs = Qubit[2]) {
// prepare |+⟩ ⊗ |+⟩ state
StatePrep_PlusPlus(qs);
Expand All @@ -204,20 +211,20 @@ namespace Quantum.Kata.BasicGates {
}


operation T23_TwoQubitGate3_Test () : Unit {
operation T203_TwoQubitGate3_Test () : Unit {
AssertOperationsEqualReferenced(2, SwapWrapper, TwoQubitGate3_Reference);
AssertOperationsEqualReferenced(2, TwoQubitGate3, TwoQubitGate3_Reference);
}


// ------------------------------------------------------
operation T24_ToffoliGate_Test () : Unit {
operation T204_ToffoliGate_Test () : Unit {
AssertOperationsEqualReferenced(3, ToffoliGate, ToffoliGate_Reference);
}


// ------------------------------------------------------
operation T25_FredkinGate_Test () : Unit {
operation T205_FredkinGate_Test () : Unit {
AssertOperationsEqualReferenced(3, FredkinGate, FredkinGate_Reference);
}

Expand Down

0 comments on commit 8621795

Please sign in to comment.