Skip to content

Commit

Permalink
[Superposition] Add post-selection workbook solution for task 2.7 (mi…
Browse files Browse the repository at this point in the history
  • Loading branch information
vivanwin authored Jun 13, 2020
1 parent 56c63e8 commit e9f775b
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 26 deletions.
34 changes: 12 additions & 22 deletions Superposition/ReferenceImplementation.qs
Original file line number Diff line number Diff line change
Expand Up @@ -463,31 +463,21 @@ namespace Quantum.Kata.Superposition {
}
}

if (P == N) {
// prepare as a power of 2 (previous task)
WState_PowerOfTwo_Reference(qs);
} else {
// allocate extra qubits
using (anc = Qubit[P - N]) {
let all_qubits = qs + anc;

repeat {
// prepare state W_P on original + ancilla qubits
WState_PowerOfTwo_Reference(all_qubits);

// measure ancilla qubits; if all of the results are Zero, we get the right state on main qubits
mutable allZeros = true;
for (i in 0 .. (P - N) - 1) {
if (not IsResultZero(M(anc[i]))) {
set allZeros = false;
}
// allocate extra qubits (might be 0 qubits if N is a power of 2)
using (anc = Qubit[P - N]) {
repeat {
// prepare state W_P on original + ancilla qubits
WState_PowerOfTwo(qs + anc);

// measure extra qubits; if all of the results are Zero, we got the right state on main qubits
mutable allZeros = true;
for (i in 0 .. (P - N) - 1) {
if (MResetZ(anc[i]) == One) {
set allZeros = false;
}
}
until (allZeros)
fixup {
ResetAll(anc);
}
}
until (allZeros);
}
}
}
Expand Down
79 changes: 75 additions & 4 deletions Superposition/Workbook_Superposition_Part2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"source": [
"# Superposition Kata Workbook, Part 2\n",
"\n",
"The [Superposition Kata Workbook, Part 1](./Workbook_Superposition.ipynb) includes the solutions of kata tasks 1 - 7. Part 2 continues the explanations for the rest of the tasks."
"The [Superposition Kata Workbook, Part 1](./Workbook_Superposition.ipynb) includes the solutions of kata tasks 1.1 - 1.7. Part 2 continues the explanations for the rest of the tasks."
]
},
{
Expand Down Expand Up @@ -1422,11 +1422,82 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The last approach we will describe uses a technique that is completely different from the ones you've seen before in this kata: postselection. \n",
"#### Post-selection\n",
"\n",
"> Note that this approach requires familiarity with measurements of quantum systems, in particular with the effect of partial measurement on a multi-qubit system; you might want to return to it after you've covered the relevant tutorials and katas.\n",
"This solution follows the similar approach to the one used to solve [task 2.3](#Task-2.3*.-$\\frac{1}{\\sqrt{3}}-\\big(|00\\rangle-+-|01\\rangle-+-|10\\rangle\\big)$-state.), which is called post-selection. \n",
"\n",
"*Coming up soon...*"
"Let's assume that we know how to prepare the $W$ state for $N = 2^k$ (we've discussed this in [task 2.6](#Task-2.6**.-W-state-on-$2^k$-qubits.)), and figure out how to use this knowledge as a building block for solving this task.\n",
"\n",
"Let's look at the smallest possible case for which $N \\neq 2^k$: $N = 3$ (we'll be able to generalize our solution for this case to an arbitrary number of qubits). The target $W$ state looks like this: \n",
"\n",
"$$|W_3\\rangle = \\frac{1}{3}\\big(|100\\rangle + |010\\rangle + |001\\rangle\\big)$$\n",
"\n",
"We will start by finding the smallest power of 2 $P$ which is greater than or equal to $N$; for our case $N = 3$ this power will be $P = 4$. We will allocate an extra $P - N$ qubits and use the solution of [task 2.6](#Task-2.6**.-W-state-on-$2^k$-qubits.) to prepare the $W_P$ state that looks as follows (with the state of the extra qubit highlighted in bold): \n",
"\n",
"$$|W_4\\rangle = \\frac{1}{2}\\big |100\\textbf{0}\\rangle + |010\\textbf{0}\\rangle + |001\\textbf{0}\\rangle + |000\\textbf{1}\\rangle \\big) = \\\\\n",
"= \\frac{\\sqrt3}{2} \\cdot \\frac{1}{\\sqrt3}\\big(|100\\rangle + |010\\rangle + |001\\rangle \\big) \\otimes |\\textbf{0}\\rangle + \\frac{1}{2}|000\\rangle \\otimes |\\textbf{1}\\rangle = \\\\\n",
"= \\frac{\\sqrt3}{2} |W_3\\rangle \\otimes |\\textbf{0}\\rangle + \\frac{1}{2}|000\\rangle \\otimes |\\textbf{1}\\rangle$$\n",
"\n",
"As we can see, if the extra qubit is in the $|0\\rangle$ state, the main 3 qubits that we are concerned about are in the right $|W_3\\rangle$ state. \n",
"\n",
"What happens if we measure just the extra qubit? This causes a partial collapse of the system to the state defined by the measurement result:\n",
"* If the result is $|0\\rangle$, the system collapses to the $|W_3\\rangle$ state - which is exactly what we wanted to achieve.\n",
"* If the result is $|1\\rangle$, the system collapses to a state $|000\\rangle$, so our goal is not achieved. The good thing is, this only happens in 25% of the cases, and we can just try again.\n",
"\n",
"If we generalize this approach to an arbitrary $N$, we'll have \n",
"\n",
"$$|W_P\\rangle = \\frac{\\sqrt{N}}{\\sqrt{P}} |W_N\\rangle \\otimes |\\textbf{0}\\rangle^{\\otimes P-N} + \\frac{\\sqrt{P-N}}{\\sqrt{P}} |0\\rangle^{\\otimes N} \\otimes |W_{P-N}\\rangle$$\n",
"\n",
"Measuring the extra $P-N$ qubits gives us two possibilities:\n",
"* All the extra qubits are in the $|0\\rangle$ state; this means the main qubits collapse to the $|W_N\\rangle$ state. \n",
"* One of the extra qubits is in the $|1\\rangle$ state; this means that the main qubits collapse to the $|0\\rangle^{\\otimes N}$ state, which is **not** the desired state. In this case we will reset and try again until all the extra qubits are in the $|0\\rangle$ state.\n",
"\n",
"> If you get the `No identifier with the name \\\"WState_PowerOfTwo\\\" exists` error, you need to run the code cell with the solution to [task 2.6](#Task-2.6**.-W-state-on-$2^k$-qubits.) first to make sure the `WState_PowerOfTwo` operation is defined."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T207_WState_Arbitrary_Test\n",
"\n",
"open Microsoft.Quantum.Measurement;\n",
"\n",
"operation WState_Arbitrary (qs : Qubit[]) : Unit {\n",
" let N = Length(qs);\n",
" if (N == 1) {\n",
" // base case of recursion: |1⟩\n",
" X(qs[0]);\n",
" } else {\n",
" // find the smallest power of 2 which is greater than or equal to N\n",
" // as a hack, we know we're not doing it on more than 64 qubits\n",
" mutable P = 1;\n",
" for (i in 1 .. 6) {\n",
" if (P < N) {\n",
" set P *= 2;\n",
" }\n",
" }\n",
"\n",
" // allocate extra qubits (might be 0 qubits if N is a power of 2)\n",
" using (anc = Qubit[P - N]) {\n",
" repeat {\n",
" // prepare state W_P on original + ancilla qubits\n",
" WState_PowerOfTwo(qs + anc);\n",
"\n",
" // measure extra qubits; if all of the results are Zero, we got the right state on main qubits\n",
" mutable allZeros = true;\n",
" for (i in 0 .. (P - N) - 1) {\n",
" if (MResetZ(anc[i]) == One) {\n",
" set allZeros = false;\n",
" }\n",
" }\n",
" }\n",
" until (allZeros);\n",
" }\n",
" }\n",
"}"
]
},
{
Expand Down

0 comments on commit e9f775b

Please sign in to comment.