qsharp-lang 1.1.4-dev → 1.1.5-dev
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser.d.ts +3 -3
- package/dist/browser.js +12 -58
- package/dist/compiler/compiler.d.ts +4 -2
- package/dist/compiler/compiler.js +14 -1
- package/dist/compiler/events.d.ts +1 -1
- package/dist/compiler/worker-browser.d.ts +1 -1
- package/dist/compiler/worker-browser.js +3 -40
- package/dist/compiler/worker-node.js +3 -39
- package/dist/debug-service/debug-service.d.ts +5 -3
- package/dist/debug-service/debug-service.js +18 -1
- package/dist/debug-service/worker-browser.d.ts +1 -1
- package/dist/debug-service/worker-browser.js +3 -29
- package/dist/debug-service/worker-node.js +3 -28
- package/dist/katas-content.generated.js +308 -308
- package/dist/language-service/language-service.d.ts +7 -1
- package/dist/language-service/language-service.js +27 -0
- package/dist/language-service/worker-browser.d.ts +1 -1
- package/dist/language-service/worker-browser.js +3 -29
- package/dist/language-service/worker-node.js +3 -28
- package/dist/main.d.ts +2 -2
- package/dist/main.js +7 -42
- package/dist/samples.generated.js +1 -1
- package/dist/workers/browser.d.ts +21 -0
- package/dist/workers/browser.js +67 -0
- package/dist/{worker-proxy.d.ts → workers/common.d.ts} +26 -9
- package/dist/{worker-proxy.js → workers/common.js} +33 -4
- package/dist/workers/node.d.ts +20 -0
- package/dist/workers/node.js +55 -0
- package/docs/Microsoft.Quantum.Arrays/All.md +4 -6
- package/docs/Microsoft.Quantum.Arrays/Any.md +4 -6
- package/docs/Microsoft.Quantum.Arrays/Chunks.md +3 -3
- package/docs/Microsoft.Quantum.Arrays/CircularlyShifted.md +3 -3
- package/docs/Microsoft.Quantum.Arrays/ColumnAt.md +3 -3
- package/docs/Microsoft.Quantum.Arrays/Count.md +4 -6
- package/docs/Microsoft.Quantum.Arrays/Diagonal.md +3 -3
- package/docs/Microsoft.Quantum.Arrays/DrawMany.md +5 -6
- package/docs/Microsoft.Quantum.Arrays/Enumerated.md +4 -5
- package/docs/Microsoft.Quantum.Arrays/Excluding.md +4 -5
- package/docs/Microsoft.Quantum.Arrays/Filtered.md +4 -6
- package/docs/Microsoft.Quantum.Arrays/FlatMapped.md +4 -5
- package/docs/Microsoft.Quantum.Arrays/Flattened.md +3 -3
- package/docs/Microsoft.Quantum.Arrays/Fold.md +4 -5
- package/docs/Microsoft.Quantum.Arrays/ForEach.md +5 -7
- package/docs/Microsoft.Quantum.Arrays/Head.md +3 -3
- package/docs/Microsoft.Quantum.Arrays/HeadAndRest.md +3 -3
- package/docs/Microsoft.Quantum.Arrays/IndexOf.md +4 -5
- package/docs/Microsoft.Quantum.Arrays/IndexRange.md +4 -5
- package/docs/Microsoft.Quantum.Arrays/Interleaved.md +3 -3
- package/docs/Microsoft.Quantum.Arrays/IsEmpty.md +3 -3
- package/docs/Microsoft.Quantum.Arrays/IsRectangularArray.md +3 -3
- package/docs/Microsoft.Quantum.Arrays/IsSorted.md +4 -5
- package/docs/Microsoft.Quantum.Arrays/IsSquareArray.md +3 -3
- package/docs/Microsoft.Quantum.Arrays/Mapped.md +4 -6
- package/docs/Microsoft.Quantum.Arrays/MappedByIndex.md +4 -6
- package/docs/Microsoft.Quantum.Arrays/MappedOverRange.md +4 -6
- package/docs/Microsoft.Quantum.Arrays/Most.md +4 -5
- package/docs/Microsoft.Quantum.Arrays/MostAndTail.md +3 -3
- package/docs/Microsoft.Quantum.Arrays/Padded.md +4 -5
- package/docs/Microsoft.Quantum.Arrays/Partitioned.md +3 -3
- package/docs/Microsoft.Quantum.Arrays/Rest.md +4 -5
- package/docs/Microsoft.Quantum.Arrays/Reversed.md +4 -5
- package/docs/Microsoft.Quantum.Arrays/SequenceI.md +3 -3
- package/docs/Microsoft.Quantum.Arrays/SequenceL.md +3 -3
- package/docs/Microsoft.Quantum.Arrays/Sorted.md +4 -5
- package/docs/Microsoft.Quantum.Arrays/Subarray.md +4 -6
- package/docs/Microsoft.Quantum.Arrays/Swapped.md +3 -3
- package/docs/Microsoft.Quantum.Arrays/Tail.md +3 -3
- package/docs/Microsoft.Quantum.Arrays/Transposed.md +4 -5
- package/docs/Microsoft.Quantum.Arrays/Unzipped.md +4 -5
- package/docs/Microsoft.Quantum.Arrays/Where.md +4 -5
- package/docs/Microsoft.Quantum.Arrays/Windows.md +3 -3
- package/docs/Microsoft.Quantum.Arrays/Zipped.md +4 -5
- package/docs/Microsoft.Quantum.Canon/ApplyCNOTChain.md +4 -4
- package/docs/Microsoft.Quantum.Canon/ApplyControlledOnBitString.md +5 -6
- package/docs/Microsoft.Quantum.Canon/ApplyControlledOnInt.md +5 -6
- package/docs/Microsoft.Quantum.Canon/ApplyP.md +5 -6
- package/docs/Microsoft.Quantum.Canon/ApplyPauli.md +5 -6
- package/docs/Microsoft.Quantum.Canon/ApplyPauliFromBitString.md +5 -6
- package/docs/Microsoft.Quantum.Canon/ApplyPauliFromInt.md +5 -6
- package/docs/Microsoft.Quantum.Canon/ApplyQFT.md +4 -4
- package/docs/Microsoft.Quantum.Canon/ApplyToEach.md +4 -4
- package/docs/Microsoft.Quantum.Canon/ApplyToEachA.md +5 -6
- package/docs/Microsoft.Quantum.Canon/ApplyToEachC.md +5 -6
- package/docs/Microsoft.Quantum.Canon/ApplyToEachCA.md +5 -6
- package/docs/Microsoft.Quantum.Canon/ApplyXorInPlace.md +5 -6
- package/docs/Microsoft.Quantum.Canon/ApplyXorInPlaceL.md +5 -6
- package/docs/Microsoft.Quantum.Canon/CX.md +4 -4
- package/docs/Microsoft.Quantum.Canon/CY.md +4 -4
- package/docs/Microsoft.Quantum.Canon/CZ.md +4 -4
- package/docs/Microsoft.Quantum.Canon/Fst.md +3 -3
- package/docs/Microsoft.Quantum.Canon/Snd.md +3 -3
- package/docs/Microsoft.Quantum.Canon/SwapReverseRegister.md +4 -4
- package/docs/Microsoft.Quantum.Convert/BigIntAsBoolArray.md +4 -5
- package/docs/Microsoft.Quantum.Convert/BoolArrayAsBigInt.md +4 -5
- package/docs/Microsoft.Quantum.Convert/BoolArrayAsInt.md +3 -3
- package/docs/Microsoft.Quantum.Convert/BoolArrayAsResultArray.md +4 -5
- package/docs/Microsoft.Quantum.Convert/BoolAsResult.md +4 -5
- package/docs/Microsoft.Quantum.Convert/ComplexAsComplexPolar.md +4 -5
- package/docs/Microsoft.Quantum.Convert/ComplexPolarAsComplex.md +4 -5
- package/docs/Microsoft.Quantum.Convert/IntAsBigInt.md +3 -3
- package/docs/Microsoft.Quantum.Convert/IntAsBoolArray.md +4 -5
- package/docs/Microsoft.Quantum.Convert/IntAsDouble.md +3 -3
- package/docs/Microsoft.Quantum.Convert/ResultArrayAsBoolArray.md +4 -5
- package/docs/Microsoft.Quantum.Convert/ResultArrayAsInt.md +3 -3
- package/docs/Microsoft.Quantum.Convert/ResultAsBool.md +4 -5
- package/docs/Microsoft.Quantum.Core/IsRangeEmpty.md +3 -3
- package/docs/Microsoft.Quantum.Core/Length.md +3 -3
- package/docs/Microsoft.Quantum.Core/RangeEnd.md +4 -5
- package/docs/Microsoft.Quantum.Core/RangeReverse.md +3 -3
- package/docs/Microsoft.Quantum.Core/RangeStart.md +3 -3
- package/docs/Microsoft.Quantum.Core/RangeStep.md +3 -3
- package/docs/Microsoft.Quantum.Core/Repeated.md +3 -3
- package/docs/Microsoft.Quantum.Diagnostics/CheckAllZero.md +4 -4
- package/docs/Microsoft.Quantum.Diagnostics/CheckOperationsAreEqual.md +4 -4
- package/docs/Microsoft.Quantum.Diagnostics/CheckZero.md +4 -4
- package/docs/Microsoft.Quantum.Diagnostics/DumpMachine.md +3 -3
- package/docs/Microsoft.Quantum.Diagnostics/Fact.md +3 -3
- package/docs/Microsoft.Quantum.Intrinsic/CCNOT.md +4 -4
- package/docs/Microsoft.Quantum.Intrinsic/CNOT.md +4 -4
- package/docs/Microsoft.Quantum.Intrinsic/Exp.md +4 -4
- package/docs/Microsoft.Quantum.Intrinsic/H.md +4 -4
- package/docs/Microsoft.Quantum.Intrinsic/I.md +4 -4
- package/docs/Microsoft.Quantum.Intrinsic/M.md +5 -6
- package/docs/Microsoft.Quantum.Intrinsic/Measure.md +5 -6
- package/docs/Microsoft.Quantum.Intrinsic/Message.md +3 -3
- package/docs/Microsoft.Quantum.Intrinsic/R.md +4 -4
- package/docs/Microsoft.Quantum.Intrinsic/R1.md +4 -4
- package/docs/Microsoft.Quantum.Intrinsic/R1Frac.md +5 -11
- package/docs/Microsoft.Quantum.Intrinsic/RFrac.md +5 -10
- package/docs/Microsoft.Quantum.Intrinsic/Reset.md +5 -6
- package/docs/Microsoft.Quantum.Intrinsic/ResetAll.md +5 -6
- package/docs/Microsoft.Quantum.Intrinsic/Rx.md +4 -4
- package/docs/Microsoft.Quantum.Intrinsic/Rxx.md +4 -4
- package/docs/Microsoft.Quantum.Intrinsic/Ry.md +4 -4
- package/docs/Microsoft.Quantum.Intrinsic/Ryy.md +4 -4
- package/docs/Microsoft.Quantum.Intrinsic/Rz.md +4 -4
- package/docs/Microsoft.Quantum.Intrinsic/Rzz.md +4 -4
- package/docs/Microsoft.Quantum.Intrinsic/S.md +4 -4
- package/docs/Microsoft.Quantum.Intrinsic/SWAP.md +4 -4
- package/docs/Microsoft.Quantum.Intrinsic/T.md +4 -4
- package/docs/Microsoft.Quantum.Intrinsic/X.md +4 -4
- package/docs/Microsoft.Quantum.Intrinsic/Y.md +4 -4
- package/docs/Microsoft.Quantum.Intrinsic/Z.md +4 -4
- package/docs/Microsoft.Quantum.Logical/Xor.md +3 -3
- package/docs/Microsoft.Quantum.Math/AbsComplex.md +4 -5
- package/docs/Microsoft.Quantum.Math/AbsComplexPolar.md +4 -5
- package/docs/Microsoft.Quantum.Math/AbsD.md +3 -3
- package/docs/Microsoft.Quantum.Math/AbsI.md +3 -3
- package/docs/Microsoft.Quantum.Math/AbsL.md +3 -3
- package/docs/Microsoft.Quantum.Math/AbsSquaredComplex.md +4 -5
- package/docs/Microsoft.Quantum.Math/AbsSquaredComplexPolar.md +4 -5
- package/docs/Microsoft.Quantum.Math/ApproximateFactorial.md +3 -3
- package/docs/Microsoft.Quantum.Math/ArcCos.md +3 -3
- package/docs/Microsoft.Quantum.Math/ArcCosh.md +3 -3
- package/docs/Microsoft.Quantum.Math/ArcSin.md +3 -3
- package/docs/Microsoft.Quantum.Math/ArcSinh.md +3 -3
- package/docs/Microsoft.Quantum.Math/ArcTan.md +3 -3
- package/docs/Microsoft.Quantum.Math/ArcTan2.md +3 -3
- package/docs/Microsoft.Quantum.Math/ArcTanh.md +3 -3
- package/docs/Microsoft.Quantum.Math/ArgComplex.md +4 -5
- package/docs/Microsoft.Quantum.Math/ArgComplexPolar.md +3 -3
- package/docs/Microsoft.Quantum.Math/Binom.md +3 -3
- package/docs/Microsoft.Quantum.Math/BitSizeI.md +4 -5
- package/docs/Microsoft.Quantum.Math/BitSizeL.md +4 -5
- package/docs/Microsoft.Quantum.Math/Ceiling.md +4 -5
- package/docs/Microsoft.Quantum.Math/Complex.md +4 -6
- package/docs/Microsoft.Quantum.Math/ComplexPolar.md +4 -5
- package/docs/Microsoft.Quantum.Math/ContinuedFractionConvergentI.md +4 -6
- package/docs/Microsoft.Quantum.Math/ContinuedFractionConvergentL.md +4 -6
- package/docs/Microsoft.Quantum.Math/Cos.md +3 -3
- package/docs/Microsoft.Quantum.Math/Cosh.md +3 -3
- package/docs/Microsoft.Quantum.Math/DivRemI.md +3 -3
- package/docs/Microsoft.Quantum.Math/DivRemL.md +3 -3
- package/docs/Microsoft.Quantum.Math/DividedByC.md +3 -3
- package/docs/Microsoft.Quantum.Math/DividedByCP.md +3 -3
- package/docs/Microsoft.Quantum.Math/E.md +3 -3
- package/docs/Microsoft.Quantum.Math/ExpModI.md +4 -5
- package/docs/Microsoft.Quantum.Math/ExpModL.md +4 -5
- package/docs/Microsoft.Quantum.Math/ExtendedGreatestCommonDivisorI.md +4 -5
- package/docs/Microsoft.Quantum.Math/ExtendedGreatestCommonDivisorL.md +4 -5
- package/docs/Microsoft.Quantum.Math/FactorialI.md +3 -3
- package/docs/Microsoft.Quantum.Math/FactorialL.md +3 -3
- package/docs/Microsoft.Quantum.Math/Floor.md +4 -5
- package/docs/Microsoft.Quantum.Math/GreatestCommonDivisorI.md +4 -5
- package/docs/Microsoft.Quantum.Math/GreatestCommonDivisorL.md +4 -5
- package/docs/Microsoft.Quantum.Math/HammingWeightI.md +3 -3
- package/docs/Microsoft.Quantum.Math/InverseModI.md +3 -3
- package/docs/Microsoft.Quantum.Math/InverseModL.md +3 -3
- package/docs/Microsoft.Quantum.Math/IsCoprimeI.md +3 -3
- package/docs/Microsoft.Quantum.Math/IsCoprimeL.md +3 -3
- package/docs/Microsoft.Quantum.Math/IsInfinite.md +4 -5
- package/docs/Microsoft.Quantum.Math/IsNaN.md +4 -5
- package/docs/Microsoft.Quantum.Math/LargestFixedPoint.md +3 -3
- package/docs/Microsoft.Quantum.Math/Lg.md +3 -3
- package/docs/Microsoft.Quantum.Math/Log.md +3 -3
- package/docs/Microsoft.Quantum.Math/Log10.md +3 -3
- package/docs/Microsoft.Quantum.Math/LogFactorialD.md +4 -5
- package/docs/Microsoft.Quantum.Math/LogGammaD.md +4 -5
- package/docs/Microsoft.Quantum.Math/LogOf2.md +3 -3
- package/docs/Microsoft.Quantum.Math/Max.md +3 -3
- package/docs/Microsoft.Quantum.Math/MaxD.md +3 -3
- package/docs/Microsoft.Quantum.Math/MaxI.md +3 -3
- package/docs/Microsoft.Quantum.Math/MaxL.md +3 -3
- package/docs/Microsoft.Quantum.Math/Min.md +3 -3
- package/docs/Microsoft.Quantum.Math/MinD.md +3 -3
- package/docs/Microsoft.Quantum.Math/MinI.md +3 -3
- package/docs/Microsoft.Quantum.Math/MinL.md +3 -3
- package/docs/Microsoft.Quantum.Math/MinusC.md +3 -3
- package/docs/Microsoft.Quantum.Math/MinusCP.md +3 -3
- package/docs/Microsoft.Quantum.Math/ModulusI.md +4 -5
- package/docs/Microsoft.Quantum.Math/ModulusL.md +4 -5
- package/docs/Microsoft.Quantum.Math/NegationC.md +3 -3
- package/docs/Microsoft.Quantum.Math/NegationCP.md +3 -3
- package/docs/Microsoft.Quantum.Math/PI.md +3 -3
- package/docs/Microsoft.Quantum.Math/PNorm.md +4 -7
- package/docs/Microsoft.Quantum.Math/PNormalized.md +4 -8
- package/docs/Microsoft.Quantum.Math/PlusC.md +3 -3
- package/docs/Microsoft.Quantum.Math/PlusCP.md +3 -3
- package/docs/Microsoft.Quantum.Math/PowC.md +4 -5
- package/docs/Microsoft.Quantum.Math/PowCP.md +4 -5
- package/docs/Microsoft.Quantum.Math/RealMod.md +3 -3
- package/docs/Microsoft.Quantum.Math/Round.md +4 -5
- package/docs/Microsoft.Quantum.Math/SignD.md +3 -3
- package/docs/Microsoft.Quantum.Math/SignI.md +3 -3
- package/docs/Microsoft.Quantum.Math/SignL.md +3 -3
- package/docs/Microsoft.Quantum.Math/Sin.md +3 -3
- package/docs/Microsoft.Quantum.Math/Sinh.md +3 -3
- package/docs/Microsoft.Quantum.Math/SmallestFixedPoint.md +3 -3
- package/docs/Microsoft.Quantum.Math/Sqrt.md +3 -3
- package/docs/Microsoft.Quantum.Math/SquaredNorm.md +3 -3
- package/docs/Microsoft.Quantum.Math/Tan.md +3 -3
- package/docs/Microsoft.Quantum.Math/Tanh.md +3 -3
- package/docs/Microsoft.Quantum.Math/TimesC.md +3 -3
- package/docs/Microsoft.Quantum.Math/TimesCP.md +3 -3
- package/docs/Microsoft.Quantum.Math/TrailingZeroCountI.md +4 -5
- package/docs/Microsoft.Quantum.Math/TrailingZeroCountL.md +4 -5
- package/docs/Microsoft.Quantum.Math/Truncate.md +4 -5
- package/docs/Microsoft.Quantum.Measurement/MResetEachZ.md +5 -6
- package/docs/Microsoft.Quantum.Measurement/MResetX.md +5 -7
- package/docs/Microsoft.Quantum.Measurement/MResetY.md +5 -7
- package/docs/Microsoft.Quantum.Measurement/MResetZ.md +5 -7
- package/docs/Microsoft.Quantum.Measurement/MeasureAllZ.md +4 -4
- package/docs/Microsoft.Quantum.Measurement/MeasureEachZ.md +4 -4
- package/docs/Microsoft.Quantum.Measurement/MeasureInteger.md +5 -7
- package/docs/Microsoft.Quantum.Random/DrawRandomDouble.md +4 -4
- package/docs/Microsoft.Quantum.Random/DrawRandomInt.md +4 -4
- package/docs/Microsoft.Quantum.ResourceEstimation/AccountForEstimates.md +5 -7
- package/docs/Microsoft.Quantum.ResourceEstimation/AuxQubitCount.md +4 -5
- package/docs/Microsoft.Quantum.ResourceEstimation/BeginEstimateCaching.md +4 -6
- package/docs/Microsoft.Quantum.ResourceEstimation/BeginRepeatEstimates.md +5 -10
- package/docs/Microsoft.Quantum.ResourceEstimation/CczCount.md +4 -5
- package/docs/Microsoft.Quantum.ResourceEstimation/EndEstimateCaching.md +4 -6
- package/docs/Microsoft.Quantum.ResourceEstimation/EndRepeatEstimates.md +4 -4
- package/docs/Microsoft.Quantum.ResourceEstimation/MeasurementCount.md +4 -5
- package/docs/Microsoft.Quantum.ResourceEstimation/PSSPCLayout.md +4 -6
- package/docs/Microsoft.Quantum.ResourceEstimation/RepeatEstimates.md +5 -8
- package/docs/Microsoft.Quantum.ResourceEstimation/RotationCount.md +4 -5
- package/docs/Microsoft.Quantum.ResourceEstimation/RotationDepth.md +4 -5
- package/docs/Microsoft.Quantum.ResourceEstimation/SingleVariant.md +4 -5
- package/docs/Microsoft.Quantum.ResourceEstimation/TCount.md +4 -5
- package/docs/Microsoft.Quantum.Unstable.Arithmetic/AddLE.md +5 -6
- package/docs/Microsoft.Quantum.Unstable.Arithmetic/ApplyIfEqualL.md +5 -6
- package/docs/Microsoft.Quantum.Unstable.Arithmetic/ApplyIfEqualLE.md +5 -7
- package/docs/Microsoft.Quantum.Unstable.Arithmetic/ApplyIfGreaterL.md +5 -6
- package/docs/Microsoft.Quantum.Unstable.Arithmetic/ApplyIfGreaterLE.md +5 -7
- package/docs/Microsoft.Quantum.Unstable.Arithmetic/ApplyIfGreaterOrEqualL.md +5 -6
- package/docs/Microsoft.Quantum.Unstable.Arithmetic/ApplyIfGreaterOrEqualLE.md +5 -7
- package/docs/Microsoft.Quantum.Unstable.Arithmetic/ApplyIfLessL.md +5 -6
- package/docs/Microsoft.Quantum.Unstable.Arithmetic/ApplyIfLessLE.md +5 -7
- package/docs/Microsoft.Quantum.Unstable.Arithmetic/ApplyIfLessOrEqualL.md +5 -6
- package/docs/Microsoft.Quantum.Unstable.Arithmetic/ApplyIfLessOrEqualLE.md +5 -7
- package/docs/Microsoft.Quantum.Unstable.Arithmetic/FourierTDIncByLE.md +5 -6
- package/docs/Microsoft.Quantum.Unstable.Arithmetic/IncByI.md +4 -4
- package/docs/Microsoft.Quantum.Unstable.Arithmetic/IncByIUsingIncByLE.md +5 -6
- package/docs/Microsoft.Quantum.Unstable.Arithmetic/IncByL.md +4 -4
- package/docs/Microsoft.Quantum.Unstable.Arithmetic/IncByLE.md +4 -4
- package/docs/Microsoft.Quantum.Unstable.Arithmetic/IncByLEUsingAddLE.md +4 -4
- package/docs/Microsoft.Quantum.Unstable.Arithmetic/IncByLUsingIncByLE.md +5 -6
- package/docs/Microsoft.Quantum.Unstable.Arithmetic/LookAheadDKRSAddLE.md +5 -6
- package/docs/Microsoft.Quantum.Unstable.Arithmetic/MAJ.md +4 -4
- package/docs/Microsoft.Quantum.Unstable.Arithmetic/ReflectAboutInteger.md +4 -4
- package/docs/Microsoft.Quantum.Unstable.Arithmetic/RippleCarryCGAddLE.md +5 -6
- package/docs/Microsoft.Quantum.Unstable.Arithmetic/RippleCarryCGIncByLE.md +5 -6
- package/docs/Microsoft.Quantum.Unstable.Arithmetic/RippleCarryTTKIncByLE.md +4 -4
- package/docs/Microsoft.Quantum.Unstable.StatePreparation/ApproximatelyPreparePureStateCP.md +5 -7
- package/docs/Microsoft.Quantum.Unstable.StatePreparation/PreparePureStateD.md +5 -6
- package/docs/Microsoft.Quantum.Unstable.TableLookup/Select.md +4 -4
- package/docs/toc.yml +286 -284
- package/lib/node/qsc_wasm_bg.wasm +0 -0
- package/lib/web/qsc_wasm_bg.wasm +0 -0
- package/package.json +1 -1
- package/dist/compiler/worker-proxy.d.ts +0 -7
- package/dist/compiler/worker-proxy.js +0 -18
- package/dist/debug-service/worker-proxy.d.ts +0 -7
- package/dist/debug-service/worker-proxy.js +0 -22
- package/dist/language-service/worker-proxy.d.ts +0 -6
- package/dist/language-service/worker-proxy.js +0 -28
|
@@ -26,8 +26,8 @@ export default {
|
|
|
26
26
|
"asMarkdown": "Exercises are problems that you have to solve by writing Q# code. In each exercise, you will be presented with placeholder code that you will have to fill in with the correct solution.\n\n**Input:** A qubit in the $|0\\rangle$ state.\n\n**Goal:** Apply the X gate to the qubit, i.e., perform a \"bit flip\" to set the qubit into the $|1\\rangle$ state.\n\n<details>\n<summary><strong>Need a hint?</strong></summary>\nFor some problems a hint will be provided to help you if you are stuck. For this exercise, read line number 3 in the code below.\n</details>\n"
|
|
27
27
|
},
|
|
28
28
|
"sourceIds": [
|
|
29
|
-
"
|
|
30
|
-
"
|
|
29
|
+
"getting_started__flip_qubit__Verification.qs",
|
|
30
|
+
"KatasLibrary.qs"
|
|
31
31
|
],
|
|
32
32
|
"placeholderCode": "namespace Kata {\n operation FlipQubit(q : Qubit) : Unit is Adj + Ctl {\n // Uncomment the following line to solve this exercise.\n //X(q);\n }\n}\n",
|
|
33
33
|
"explainedSolution": {
|
|
@@ -95,8 +95,8 @@ export default {
|
|
|
95
95
|
"items": [
|
|
96
96
|
{
|
|
97
97
|
"type": "text-content",
|
|
98
|
-
"asHtml": "<p>Complex numbers have a parameter called the phase. If a complex number $z = x + iy$ is written in polar form $z = re^{i\\theta}$, its phase is $\\theta$, where $\\theta = \\atan2(y, x)$.</p>\n<blockquote>\n<p><code>atan2</code> is a useful function available in most programming languages. It takes two arguments and returns an angle $\\theta$\nbetween $-\\pi$ and $\\pi$ that has $\\cos \\theta = x$ and $\\sin \\theta = y$. Unlike using $\\tan^{-1}(\\frac{y}{x})$, <code>atan2</code> computes
|
|
99
|
-
"asMarkdown": "\nComplex numbers have a parameter called the phase. If a complex number $z = x + iy$ is written in polar form $z = re^{i\\theta}$, its phase is $\\theta$, where $\\theta = \\atan2(y, x)$.\n\n> `atan2` is a useful function available in most programming languages. It takes two arguments and returns an angle $\\theta$\n> between $-\\pi$ and $\\pi$ that has $\\cos \\theta = x$ and $\\sin \\theta = y$. Unlike using $\\tan^{-1}(\\frac{y}{x})$, `atan2` computes
|
|
98
|
+
"asHtml": "<p>Complex numbers have a parameter called the phase. If a complex number $z = x + iy$ is written in polar form $z = re^{i\\theta}$, its phase is $\\theta$, where $\\theta = \\atan2(y, x)$.</p>\n<blockquote>\n<p><code>atan2</code> is a useful function available in most programming languages. It takes two arguments and returns an angle $\\theta$\nbetween $-\\pi$ and $\\pi$ that has $\\cos \\theta = x$ and $\\sin \\theta = y$. Unlike using $\\tan^{-1}(\\frac{y}{x})$, <code>atan2</code> computes\nthe correct quadrant for the angle, since it preserves information about the signs of both sine and cosine of the angle.</p>\n</blockquote>\n<p>The probability amplitudes $\\alpha$ and $\\beta$ are complex numbers, therefore $\\alpha$ and $\\beta$ have a phase. For example, consider a qubit in state $\\frac{1 + i}{2}|0\\rangle + \\frac{1 - i}{2}|1\\rangle$. If you do the math, you see that the phase of $|0\\rangle$ is $\\atan2(\\frac12, \\frac12) = \\frac{\\pi}{4}$, and the phase of $|1\\rangle$ is $\\atan2(\\frac12, -\\frac12) = -\\frac{\\pi}{4}$. The difference between these two phases is known as <strong>relative phase</strong>.</p>\n<p>Multiplying the state of the entire system by $e^{i\\theta}$ doesn't affect the relative phase: $\\alpha|0\\rangle + \\beta|1\\rangle$ has the same relative phase as $e^{i\\theta}\\big(\\alpha|0\\rangle + \\beta|1\\rangle\\big)$. In the second expression, $\\theta$ is known as the system's <strong>global phase</strong>.</p>\n<p>The state of a qubit (or, more generally, the state of a quantum system) is defined by its relative phase - global phase arises as a consequence of using linear algebra to represent qubits, and has no physical meaning. That is, applying a phase to the entire state of a system (multiplying the entire vector by $e^{i\\theta}$ for any real $\\theta$) doesn't actually affect the state of the system. Because of this, global phase is sometimes known as <strong>unobservable phase</strong> or <strong>hidden phase</strong>.</p>\n",
|
|
99
|
+
"asMarkdown": "\nComplex numbers have a parameter called the phase. If a complex number $z = x + iy$ is written in polar form $z = re^{i\\theta}$, its phase is $\\theta$, where $\\theta = \\atan2(y, x)$.\n\n> `atan2` is a useful function available in most programming languages. It takes two arguments and returns an angle $\\theta$\n> between $-\\pi$ and $\\pi$ that has $\\cos \\theta = x$ and $\\sin \\theta = y$. Unlike using $\\tan^{-1}(\\frac{y}{x})$, `atan2` computes\n> the correct quadrant for the angle, since it preserves information about the signs of both sine and cosine of the angle.\n\nThe probability amplitudes $\\alpha$ and $\\beta$ are complex numbers, therefore $\\alpha$ and $\\beta$ have a phase. For example, consider a qubit in state $\\frac{1 + i}{2}|0\\rangle + \\frac{1 - i}{2}|1\\rangle$. If you do the math, you see that the phase of $|0\\rangle$ is $\\atan2(\\frac12, \\frac12) = \\frac{\\pi}{4}$, and the phase of $|1\\rangle$ is $\\atan2(\\frac12, -\\frac12) = -\\frac{\\pi}{4}$. The difference between these two phases is known as **relative phase**.\n\nMultiplying the state of the entire system by $e^{i\\theta}$ doesn't affect the relative phase: $\\alpha|0\\rangle + \\beta|1\\rangle$ has the same relative phase as $e^{i\\theta}\\big(\\alpha|0\\rangle + \\beta|1\\rangle\\big)$. In the second expression, $\\theta$ is known as the system's **global phase**.\n\nThe state of a qubit (or, more generally, the state of a quantum system) is defined by its relative phase - global phase arises as a consequence of using linear algebra to represent qubits, and has no physical meaning. That is, applying a phase to the entire state of a system (multiplying the entire vector by $e^{i\\theta}$ for any real $\\theta$) doesn't actually affect the state of the system. Because of this, global phase is sometimes known as **unobservable phase** or **hidden phase**."
|
|
100
100
|
}
|
|
101
101
|
]
|
|
102
102
|
},
|
|
@@ -107,8 +107,8 @@ export default {
|
|
|
107
107
|
"items": [
|
|
108
108
|
{
|
|
109
109
|
"type": "text-content",
|
|
110
|
-
"asHtml": "<p>In Q#, qubits are represented by the <code>Qubit</code> data type. On a physical quantum computer, it's impossible to directly access the state of a qubit, whether to read its exact state, or to set it to a desired state, and this data type reflects that. Instead, you can change the state of a qubit using quantum gates, and extract information about the state of the system using measurements.</p>\n<p>That being said, when you run Q# code on a quantum simulator instead of a physical quantum computer, you can use diagnostic functions that allow you to peek at the state of the quantum system. This is very useful both for learning and for debugging small Q# programs.</p>\n<p>The qubits aren't an ordinary data type, so the variables of this type have to be declared and initialized ("allocated") a little differently. The <code>use</code> statement allocates a qubit (or multiple) that can be used until the end of the scope in which the statement was used: <code>use q = Qubit();</code> allocates a qubit and binds it to the variable <code>q</code>.</p>\n<p>Freshly allocated qubits start out in state $|0\\rangle$, and have to be returned to that state by the time they are released. If you attempt to release a qubit in any state other than $|0\\rangle$, it will result in a runtime error. We will see why it is important later, when we look at multi-qubit systems.</p>\n<h2>Visualizing Quantum State</h2>\n<p>Before we continue, let's learn some techniques to visualize the quantum state of our qubits.</p>\n<h3>Display the Quantum State of a Single-Qubit Program</h3>\n<p>Let's start with a simple scenario: a program that acts on a single qubit
|
|
111
|
-
"asMarkdown": "\nIn Q#, qubits are represented by the `Qubit` data type. On a physical quantum computer, it's impossible to directly access the state of a qubit, whether to read its exact state, or to set it to a desired state, and this data type reflects that. Instead, you can change the state of a qubit using quantum gates, and extract information about the state of the system using measurements.\n\nThat being said, when you run Q# code on a quantum simulator instead of a physical quantum computer, you can use diagnostic functions that allow you to peek at the state of the quantum system. This is very useful both for learning and for debugging small Q# programs.\n\nThe qubits aren't an ordinary data type, so the variables of this type have to be declared and initialized (\"allocated\") a little differently. The `use` statement allocates a qubit (or multiple) that can be used until the end of the scope in which the statement was used: `use q = Qubit();` allocates a qubit and binds it to the variable `q`.\n\nFreshly allocated qubits start out in state $|0\\rangle$, and have to be returned to that state by the time they are released. If you attempt to release a qubit in any state other than $|0\\rangle$, it will result in a runtime error. We will see why it is important later, when we look at multi-qubit systems.\n\n## Visualizing Quantum State\n\nBefore we continue, let's learn some techniques to visualize the quantum state of our qubits.\n\n### Display the Quantum State of a Single-Qubit Program\n\nLet's start with a simple scenario: a program that acts on a single qubit
|
|
110
|
+
"asHtml": "<p>In Q#, qubits are represented by the <code>Qubit</code> data type. On a physical quantum computer, it's impossible to directly access the state of a qubit, whether to read its exact state, or to set it to a desired state, and this data type reflects that. Instead, you can change the state of a qubit using quantum gates, and extract information about the state of the system using measurements.</p>\n<p>That being said, when you run Q# code on a quantum simulator instead of a physical quantum computer, you can use diagnostic functions that allow you to peek at the state of the quantum system. This is very useful both for learning and for debugging small Q# programs.</p>\n<p>The qubits aren't an ordinary data type, so the variables of this type have to be declared and initialized ("allocated") a little differently. The <code>use</code> statement allocates a qubit (or multiple) that can be used until the end of the scope in which the statement was used: <code>use q = Qubit();</code> allocates a qubit and binds it to the variable <code>q</code>.</p>\n<p>Freshly allocated qubits start out in state $|0\\rangle$, and have to be returned to that state by the time they are released. If you attempt to release a qubit in any state other than $|0\\rangle$, it will result in a runtime error. We will see why it is important later, when we look at multi-qubit systems.</p>\n<h2>Visualizing Quantum State</h2>\n<p>Before we continue, let's learn some techniques to visualize the quantum state of our qubits.</p>\n<h3>Display the Quantum State of a Single-Qubit Program</h3>\n<p>Let's start with a simple scenario: a program that acts on a single qubit.\nThe state of the quantum system used by this program can be represented as a complex vector of length 2, or, using Dirac notation,</p>\n<p>$$\\begin{bmatrix} \\alpha \\\\ \\beta \\end{bmatrix} = \\alpha|0\\rangle + \\beta|1\\rangle$$</p>\n<p>If this program runs on a physical quantum system, there is no way to get the information about the values of $\\alpha$ and $\\beta$ at a certain point of the program execution from a single observation.\nYou would need to run the program repeatedly up to this point, perform a measurement on the system, and aggregate the results of multiple measurements to estimate $\\alpha$ and $\\beta$.</p>\n<p>However, at the early stages of quantum program development the program typically runs on a simulator - a classical program which simulates the behavior of a small quantum system while having complete information about its internal state.\nYou can take advantage of this to do some non-physical things, such as peeking at the internals of the quantum system to observe its exact state without disturbing it!</p>\n<p>The <code>DumpMachine</code> function from the <code>Microsoft.Quantum.Diagnostics</code> namespace allows you to do exactly that. The output of <code>DumpMachine</code> is accurate up to a global phase, and remember that global phase does not have any physical meaning. When using <code>DumpMachine</code>, you may see that all probability amplitudes are multiplied by some complex number compared to the state you're expecting.</p>\n<h3>Demo: DumpMachine For Single-Qubit Systems</h3>\n<p>The following demo shows how to allocate a qubit and examine its state in Q#. You'll use <code>DumpMachine</code> to output the state of the system at any point in the program without affecting the state.</p>\n<blockquote>\n<p>Note that the Q# code doesn't have access to the output of <code>DumpMachine</code>, so you cannot write any non-physical code in Q#!</p>\n</blockquote>\n",
|
|
111
|
+
"asMarkdown": "\nIn Q#, qubits are represented by the `Qubit` data type. On a physical quantum computer, it's impossible to directly access the state of a qubit, whether to read its exact state, or to set it to a desired state, and this data type reflects that. Instead, you can change the state of a qubit using quantum gates, and extract information about the state of the system using measurements.\n\nThat being said, when you run Q# code on a quantum simulator instead of a physical quantum computer, you can use diagnostic functions that allow you to peek at the state of the quantum system. This is very useful both for learning and for debugging small Q# programs.\n\nThe qubits aren't an ordinary data type, so the variables of this type have to be declared and initialized (\"allocated\") a little differently. The `use` statement allocates a qubit (or multiple) that can be used until the end of the scope in which the statement was used: `use q = Qubit();` allocates a qubit and binds it to the variable `q`.\n\nFreshly allocated qubits start out in state $|0\\rangle$, and have to be returned to that state by the time they are released. If you attempt to release a qubit in any state other than $|0\\rangle$, it will result in a runtime error. We will see why it is important later, when we look at multi-qubit systems.\n\n## Visualizing Quantum State\n\nBefore we continue, let's learn some techniques to visualize the quantum state of our qubits.\n\n### Display the Quantum State of a Single-Qubit Program\n\nLet's start with a simple scenario: a program that acts on a single qubit.\nThe state of the quantum system used by this program can be represented as a complex vector of length 2, or, using Dirac notation,\n\n$$\\begin{bmatrix} \\alpha \\\\\\ \\beta \\end{bmatrix} = \\alpha|0\\rangle + \\beta|1\\rangle$$\n\nIf this program runs on a physical quantum system, there is no way to get the information about the values of $\\alpha$ and $\\beta$ at a certain point of the program execution from a single observation.\nYou would need to run the program repeatedly up to this point, perform a measurement on the system, and aggregate the results of multiple measurements to estimate $\\alpha$ and $\\beta$.\n\nHowever, at the early stages of quantum program development the program typically runs on a simulator - a classical program which simulates the behavior of a small quantum system while having complete information about its internal state.\nYou can take advantage of this to do some non-physical things, such as peeking at the internals of the quantum system to observe its exact state without disturbing it!\n\nThe `DumpMachine` function from the `Microsoft.Quantum.Diagnostics` namespace allows you to do exactly that. The output of `DumpMachine` is accurate up to a global phase, and remember that global phase does not have any physical meaning. When using `DumpMachine`, you may see that all probability amplitudes are multiplied by some complex number compared to the state you're expecting.\n\n### Demo: DumpMachine For Single-Qubit Systems\n\nThe following demo shows how to allocate a qubit and examine its state in Q#. You'll use `DumpMachine` to output the state of the system at any point in the program without affecting the state.\n\n> Note that the Q# code doesn't have access to the output of `DumpMachine`, so you cannot write any non-physical code in Q#!"
|
|
112
112
|
},
|
|
113
113
|
{
|
|
114
114
|
"type": "example",
|
|
@@ -132,8 +132,8 @@ export default {
|
|
|
132
132
|
"asMarkdown": "\n**Input:** A qubit in an unknown state $|\\psi\\rangle = \\alpha|0\\rangle + \\beta|1\\rangle$. The amplitudes $\\alpha$ and $\\beta$ will be real and non-negative.\n\n**Output:** A tuple of two numbers $(\\alpha', \\beta')$ - your estimates of the amplitudes $\\alpha$ and $\\beta$.\nThe absolute errors $|\\alpha - \\alpha'|$ and $|\\beta - \\beta'|$ should be less than or equal to 0.001.\n\nPlease note that the state parameter is guaranteed to be the same\nif you run the code several times. Your operation will be called\nonce for every run.\n\n<details>\n <summary><b>Need a hint?</b></summary>\n On a physical quantum system, there would be no way to obtain the values of $\\alpha$ and $\\beta$ from a single observation. Since this program runs on a simulator, we can use <code>DumpMachine</code> to inspect the qubit and take a note of its state. Furthermore, the problem statement guarantees, that the state will be the same from invocation to invocation. So we can update the code to return the amplitudes that we've taken note of. Then run the code again.\n</details>\n"
|
|
133
133
|
},
|
|
134
134
|
"sourceIds": [
|
|
135
|
-
"
|
|
136
|
-
"
|
|
135
|
+
"qubit__learn_single_qubit_state__Verification.qs",
|
|
136
|
+
"KatasLibrary.qs"
|
|
137
137
|
],
|
|
138
138
|
"placeholderCode": "namespace Kata {\n operation LearnSingleQubitState(q : Qubit) : (Double, Double) {\n // Implement your solution here...\n\n return (0.0, 0.0);\n }\n}\n",
|
|
139
139
|
"explainedSolution": {
|
|
@@ -160,7 +160,7 @@ export default {
|
|
|
160
160
|
{
|
|
161
161
|
"type": "text-content",
|
|
162
162
|
"asHtml": "<p>Congratulations! In this kata you learned the basics of qubits and qubit states. Here are a few key concepts to keep in mind:</p>\n<ul>\n<li>A qubit is a basic unit of quantum information, analogous to a bit in classical computing.</li>\n<li>Superposition is a quantum phenomenon where a qubit is in a combination of both 0 and 1 states. When measured, a qubit goes from being in superposition to one of the classical states.</li>\n<li>A qubit can be represented as $|\\psi\\rangle = \\alpha|0\\rangle + \\beta|1\\rangle$, where $\\alpha$ and $\\beta$ are complex numbers and state vectors $|0\\rangle$ and $|1\\rangle$ are $0$ and $1$ states respectively.</li>\n<li>In Q#, qubits are represented by the <code>Qubit</code> data type. When simulating a quantum program, you can use <code>DumpMachine</code> to inspect the state of a qubit without disturbing it.</li>\n</ul>\n",
|
|
163
|
-
"asMarkdown": "\nCongratulations! In this kata you learned the basics of qubits and qubit states. Here are a few key concepts to keep in mind:\n
|
|
163
|
+
"asMarkdown": "\nCongratulations! In this kata you learned the basics of qubits and qubit states. Here are a few key concepts to keep in mind:\n\n- A qubit is a basic unit of quantum information, analogous to a bit in classical computing.\n- Superposition is a quantum phenomenon where a qubit is in a combination of both 0 and 1 states. When measured, a qubit goes from being in superposition to one of the classical states.\n- A qubit can be represented as $|\\psi\\rangle = \\alpha|0\\rangle + \\beta|1\\rangle$, where $\\alpha$ and $\\beta$ are complex numbers and state vectors $|0\\rangle$ and $|1\\rangle$ are $0$ and $1$ states respectively.\n- In Q#, qubits are represented by the `Qubit` data type. When simulating a quantum program, you can use `DumpMachine` to inspect the state of a qubit without disturbing it."
|
|
164
164
|
}
|
|
165
165
|
]
|
|
166
166
|
}
|
|
@@ -189,8 +189,8 @@ export default {
|
|
|
189
189
|
"items": [
|
|
190
190
|
{
|
|
191
191
|
"type": "text-content",
|
|
192
|
-
"asHtml": "<p>There are certain properties common to all quantum gates. This section will introduce those properties, using the $X$ gate as an example.</p>\n<h2>Matrix Representation</h2>\n<p>Quantum gates are represented as $2^N \\times 2^N$ unitary matrices, where $N$ is the number of qubits the gate operates on
|
|
193
|
-
"asMarkdown": "\nThere are certain properties common to all quantum gates. This section will introduce those properties, using the $X$ gate as an example.\n\n## Matrix Representation\n\nQuantum gates are represented as $2^N \\times 2^N$ unitary matrices, where $N$ is the number of qubits the gate operates on
|
|
192
|
+
"asHtml": "<p>There are certain properties common to all quantum gates. This section will introduce those properties, using the $X$ gate as an example.</p>\n<h2>Matrix Representation</h2>\n<p>Quantum gates are represented as $2^N \\times 2^N$ unitary matrices, where $N$ is the number of qubits the gate operates on.\nAs a quick reminder, a unitary matrix is a square matrix whose inverse is its adjoint, thus $U^* U = UU^* = UU^{-1} = \\mathbb{I}$.\nSingle-qubit gates are represented by $2 \\times 2$ matrices.\nOur example for this section, the $X$ gate, is represented by the following matrix:</p>\n<p>$$\\begin{bmatrix} 0 & 1 \\\\ 1 & 0 \\end{bmatrix}$$</p>\n<p>You may recall that the state of a qubit is represented by a vector of size $2$. You can apply a gate to a qubit by multiplying the gate's matrix by the qubit's state vector. The result will be another vector, representing the new state of the qubit. For example, applying the $X$ gate to the computational basis states looks like this:</p>\n<p>$$\nX|0\\rangle =\n\\begin{bmatrix} 0 & 1 \\\\ 1 & 0 \\end{bmatrix}\n\\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} =\n\\begin{bmatrix} 0 \\cdot 1 + 1 \\cdot 0 \\\\ 1 \\cdot 1 + 0 \\cdot 0 \\end{bmatrix} =\n\\begin{bmatrix} 0 \\\\ 1 \\end{bmatrix}\n$$</p>\n<p>$$\nX|1\\rangle =\n\\begin{bmatrix} 0 & 1 \\\\ 1 & 0 \\end{bmatrix}\n\\begin{bmatrix} 0 \\\\ 1 \\end{bmatrix} =\n\\begin{bmatrix} 0 \\cdot 0 + 1 \\cdot 1 \\\\ 1 \\cdot 0 + 0 \\cdot 1 \\end{bmatrix} =\n\\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix}\n$$</p>\n<p>The general case:</p>\n<p>$$|\\psi\\rangle = \\alpha|0\\rangle + \\beta|1\\rangle$$</p>\n<p>$$\nX|\\psi\\rangle =\n\\begin{bmatrix} 0 & 1 \\\\ 1 & 0 \\end{bmatrix}\n\\begin{bmatrix} \\alpha \\\\ \\beta \\end{bmatrix} =\n\\begin{bmatrix} 0 \\cdot \\alpha + 1 \\cdot \\beta \\\\ 1 \\cdot \\alpha + 0 \\cdot \\beta \\end{bmatrix} =\n\\begin{bmatrix} \\beta \\\\ \\alpha \\end{bmatrix}\n$$</p>\n<blockquote>\n<p>If you need a reminder of what $|0\\rangle$, $|1\\rangle$, and $|\\psi\\rangle$ mean, you can review the section on Dirac notation in "The Qubit" kata.</p>\n</blockquote>\n<p>Quantum gates are represented by matrices, just like quantum states are represented by vectors. Because this is the most common way to represent quantum gates, the terms "gate" and "gate matrix" will be used interchangeably in this kata.</p>\n<p>Applying several quantum gates in sequence is equivalent to performing several of these multiplications.\nFor example, if you have gates $A$ and $B$ and a qubit in state $|\\psi\\rangle$, the result of applying $A$ followed by $B$ to that qubit would be $B\\big(A|\\psi\\rangle\\big)$ (the gate closest to the qubit state gets applied first).\nMatrix multiplication is associative, so this is equivalent to multiplying the $B$ matrix by the $A$ matrix, producing a compound gate of the two, and then applying that to the qubit: $\\big(BA\\big)|\\psi\\rangle$.</p>\n<blockquote>\n<p>Note that matrix multiplication isn’t commutative, thus $(BA) \\neq (AB)$.</p>\n</blockquote>\n<p>All quantum gates are reversible - there is another gate which will undo any given gate's transformation, returning the qubit to its original state.\nThis means that when dealing with quantum gates, information about qubit states is never lost, as opposed to classical logic gates, some of which destroy information.\nQuantum gates are represented by unitary matrices, so the inverse of a gate is its adjoint; these terms are also used interchangeably in quantum computing.</p>\n<h2>Effects on Basis States</h2>\n<p>There is a simple way to find out what a gate does to the two computational basis states $|0\\rangle$ and $|1\\rangle$. Consider an arbitrary gate:</p>\n<p>$$A = \\begin{bmatrix} \\epsilon & \\zeta \\\\ \\eta & \\mu \\end{bmatrix}$$</p>\n<p>Watch what happens when we apply it to these states:</p>\n<p>$$\nA|0\\rangle =\n\\begin{bmatrix} \\epsilon & \\zeta \\\\ \\eta & \\mu \\end{bmatrix}\n\\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} =\n\\begin{bmatrix} \\epsilon \\cdot 1 + \\zeta \\cdot 0 \\\\ \\eta \\cdot 1 + \\mu \\cdot 0 \\end{bmatrix} =\n\\begin{bmatrix} \\epsilon \\\\ \\eta \\end{bmatrix} = \\epsilon|0\\rangle + \\eta|1\\rangle\n$$</p>\n<p>$$\nA|1\\rangle =\n\\begin{bmatrix} \\epsilon & \\zeta \\\\ \\eta & \\mu \\end{bmatrix}\n\\begin{bmatrix} 0 \\\\ 1 \\end{bmatrix} =\n\\begin{bmatrix} \\epsilon \\cdot 0 + \\zeta \\cdot 1 \\\\ \\eta \\cdot 0 + \\mu \\cdot 1 \\end{bmatrix} =\n\\begin{bmatrix} \\zeta \\\\ \\mu \\end{bmatrix} = \\zeta|0\\rangle + \\mu|1\\rangle\n$$</p>\n<p>Notice that applying the gate to the $|0\\rangle$ state transforms it into the state written as the first column of the gate's matrix. Likewise, applying the gate to the $|1\\rangle$ state transforms it into the state written as the second column. This holds true for any quantum gate, including, of course, the $X$ gate:</p>\n<p>$$X = \\begin{bmatrix} 0 & 1 \\\\ 1 & 0 \\end{bmatrix}$$</p>\n<p>$$X|0\\rangle = \\begin{bmatrix} 0 \\\\ 1 \\end{bmatrix} = |1\\rangle$$</p>\n<p>$$X|1\\rangle = \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} = |0\\rangle$$</p>\n<p>Once you understand how a gate affects the computational basis states, you can easily find how it affects any state.\nRecall that any qubit state vector can be written as a linear combination of the basis states:</p>\n<p>$$|\\psi\\rangle = \\begin{bmatrix} \\alpha \\\\ \\beta \\end{bmatrix} = \\alpha|0\\rangle + \\beta|1\\rangle$$</p>\n<p>Because matrix multiplication distributes over addition, once you know how a gate affects those two basis states, you can calculate how it affects any state:</p>\n<p>$$X|\\psi\\rangle = X\\big(\\alpha|0\\rangle + \\beta|1\\rangle\\big) = X\\big(\\alpha|0\\rangle\\big) + X\\big(\\beta|1\\rangle\\big) = \\alpha X|0\\rangle + \\beta X|1\\rangle = \\alpha|1\\rangle + \\beta|0\\rangle$$</p>\n<p>That is, applying a gate to a qubit in superposition is equivalent to applying that gate to the basis states that make up that superposition and adding the results with appropriate weights.</p>\n",
|
|
193
|
+
"asMarkdown": "\nThere are certain properties common to all quantum gates. This section will introduce those properties, using the $X$ gate as an example.\n\n## Matrix Representation\n\nQuantum gates are represented as $2^N \\times 2^N$ unitary matrices, where $N$ is the number of qubits the gate operates on.\nAs a quick reminder, a unitary matrix is a square matrix whose inverse is its adjoint, thus $U^* U = UU^* = UU^{-1} = \\mathbb{I}$.\nSingle-qubit gates are represented by $2 \\times 2$ matrices.\nOur example for this section, the $X$ gate, is represented by the following matrix:\n\n$$\\begin{bmatrix} 0 & 1 \\\\\\ 1 & 0 \\end{bmatrix}$$\n\nYou may recall that the state of a qubit is represented by a vector of size $2$. You can apply a gate to a qubit by multiplying the gate's matrix by the qubit's state vector. The result will be another vector, representing the new state of the qubit. For example, applying the $X$ gate to the computational basis states looks like this:\n\n$$\nX|0\\rangle =\n\\begin{bmatrix} 0 & 1 \\\\\\ 1 & 0 \\end{bmatrix}\n\\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix} =\n\\begin{bmatrix} 0 \\cdot 1 + 1 \\cdot 0 \\\\\\ 1 \\cdot 1 + 0 \\cdot 0 \\end{bmatrix} =\n\\begin{bmatrix} 0 \\\\\\ 1 \\end{bmatrix}\n$$\n\n$$\nX|1\\rangle =\n\\begin{bmatrix} 0 & 1 \\\\\\ 1 & 0 \\end{bmatrix}\n\\begin{bmatrix} 0 \\\\\\ 1 \\end{bmatrix} =\n\\begin{bmatrix} 0 \\cdot 0 + 1 \\cdot 1 \\\\\\ 1 \\cdot 0 + 0 \\cdot 1 \\end{bmatrix} =\n\\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix}\n$$\n\nThe general case:\n\n$$|\\psi\\rangle = \\alpha|0\\rangle + \\beta|1\\rangle$$\n\n$$\nX|\\psi\\rangle =\n\\begin{bmatrix} 0 & 1 \\\\\\ 1 & 0 \\end{bmatrix}\n\\begin{bmatrix} \\alpha \\\\\\ \\beta \\end{bmatrix} =\n\\begin{bmatrix} 0 \\cdot \\alpha + 1 \\cdot \\beta \\\\\\ 1 \\cdot \\alpha + 0 \\cdot \\beta \\end{bmatrix} =\n\\begin{bmatrix} \\beta \\\\\\ \\alpha \\end{bmatrix}\n$$\n\n> If you need a reminder of what $|0\\rangle$, $|1\\rangle$, and $|\\psi\\rangle$ mean, you can review the section on Dirac notation in \"The Qubit\" kata.\n\nQuantum gates are represented by matrices, just like quantum states are represented by vectors. Because this is the most common way to represent quantum gates, the terms \"gate\" and \"gate matrix\" will be used interchangeably in this kata.\n\nApplying several quantum gates in sequence is equivalent to performing several of these multiplications.\nFor example, if you have gates $A$ and $B$ and a qubit in state $|\\psi\\rangle$, the result of applying $A$ followed by $B$ to that qubit would be $B\\big(A|\\psi\\rangle\\big)$ (the gate closest to the qubit state gets applied first).\nMatrix multiplication is associative, so this is equivalent to multiplying the $B$ matrix by the $A$ matrix, producing a compound gate of the two, and then applying that to the qubit: $\\big(BA\\big)|\\psi\\rangle$.\n\n>Note that matrix multiplication isn’t commutative, thus $(BA) \\neq \\(AB)$.\n\nAll quantum gates are reversible - there is another gate which will undo any given gate's transformation, returning the qubit to its original state.\nThis means that when dealing with quantum gates, information about qubit states is never lost, as opposed to classical logic gates, some of which destroy information.\nQuantum gates are represented by unitary matrices, so the inverse of a gate is its adjoint; these terms are also used interchangeably in quantum computing.\n\n## Effects on Basis States\n\nThere is a simple way to find out what a gate does to the two computational basis states $|0\\rangle$ and $|1\\rangle$. Consider an arbitrary gate:\n\n$$A = \\begin{bmatrix} \\epsilon & \\zeta \\\\\\ \\eta & \\mu \\end{bmatrix}$$\n\nWatch what happens when we apply it to these states:\n\n$$\nA|0\\rangle =\n\\begin{bmatrix} \\epsilon & \\zeta \\\\\\ \\eta & \\mu \\end{bmatrix}\n\\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix} =\n\\begin{bmatrix} \\epsilon \\cdot 1 + \\zeta \\cdot 0 \\\\\\ \\eta \\cdot 1 + \\mu \\cdot 0 \\end{bmatrix} =\n\\begin{bmatrix} \\epsilon \\\\\\ \\eta \\end{bmatrix} = \\epsilon|0\\rangle + \\eta|1\\rangle\n$$\n\n$$\nA|1\\rangle =\n\\begin{bmatrix} \\epsilon & \\zeta \\\\\\ \\eta & \\mu \\end{bmatrix}\n\\begin{bmatrix} 0 \\\\\\ 1 \\end{bmatrix} =\n\\begin{bmatrix} \\epsilon \\cdot 0 + \\zeta \\cdot 1 \\\\\\ \\eta \\cdot 0 + \\mu \\cdot 1 \\end{bmatrix} =\n\\begin{bmatrix} \\zeta \\\\\\ \\mu \\end{bmatrix} = \\zeta|0\\rangle + \\mu|1\\rangle\n$$\n\nNotice that applying the gate to the $|0\\rangle$ state transforms it into the state written as the first column of the gate's matrix. Likewise, applying the gate to the $|1\\rangle$ state transforms it into the state written as the second column. This holds true for any quantum gate, including, of course, the $X$ gate:\n\n$$X = \\begin{bmatrix} 0 & 1 \\\\\\ 1 & 0 \\end{bmatrix}$$\n\n$$X|0\\rangle = \\begin{bmatrix} 0 \\\\\\ 1 \\end{bmatrix} = |1\\rangle$$\n\n$$X|1\\rangle = \\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix} = |0\\rangle$$\n\nOnce you understand how a gate affects the computational basis states, you can easily find how it affects any state.\nRecall that any qubit state vector can be written as a linear combination of the basis states:\n\n$$|\\psi\\rangle = \\begin{bmatrix} \\alpha \\\\\\ \\beta \\end{bmatrix} = \\alpha|0\\rangle + \\beta|1\\rangle$$\n\nBecause matrix multiplication distributes over addition, once you know how a gate affects those two basis states, you can calculate how it affects any state:\n\n$$X|\\psi\\rangle = X\\big(\\alpha|0\\rangle + \\beta|1\\rangle\\big) = X\\big(\\alpha|0\\rangle\\big) + X\\big(\\beta|1\\rangle\\big) = \\alpha X|0\\rangle + \\beta X|1\\rangle = \\alpha|1\\rangle + \\beta|0\\rangle$$\n\nThat is, applying a gate to a qubit in superposition is equivalent to applying that gate to the basis states that make up that superposition and adding the results with appropriate weights."
|
|
194
194
|
}
|
|
195
195
|
]
|
|
196
196
|
},
|
|
@@ -201,8 +201,8 @@ export default {
|
|
|
201
201
|
"items": [
|
|
202
202
|
{
|
|
203
203
|
"type": "text-content",
|
|
204
|
-
"asHtml": "<p>There is another way to represent quantum gates, this time using Dirac notation. However, the kets we've been using aren't enough to represent arbitrary matrices. We need to introduce another piece of notation: the <strong>bra</strong> (this is why Dirac notation is sometimes called <strong>bra-ket notation</strong>).</p>\n<p>Recall that kets represent column vectors; a bra is a ket's row vector counterpart. For any ket $|\\psi\\rangle$, the corresponding bra is its adjoint (conjugate transpose): $\\langle\\psi| = |\\psi\\rangle^\\dagger$.</p>\n<p>Some examples:</p>\n<table>\n <tr>\n <th>Ket</th>\n <th>Bra</th>\n </tr>\n <tr>\n <td>$|0\\rangle = \\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix}$</td>\n <td>$\\langle0| = \\begin{bmatrix} 1 & 0 \\end{bmatrix}$</td>\n </tr>\n <tr>\n <td>$|1\\rangle = \\begin{bmatrix} 0 \\\\\\ 1 \\end{bmatrix}$</td>\n <td>$\\langle1| = \\begin{bmatrix} 0 & 1 \\end{bmatrix}$</td>\n </tr>\n <tr>\n <td>$|i\\rangle = \\begin{bmatrix} \\frac{1}{\\sqrt{2}} \\\\\\ \\frac{i}{\\sqrt{2}} \\end{bmatrix}$</td>\n <td>$\\langle i| = \\begin{bmatrix} \\frac{1}{\\sqrt{2}} & -\\frac{i}{\\sqrt{2}} \\end{bmatrix}$</td>\n </tr>\n <tr>\n <td>$|\\psi\\rangle = \\begin{bmatrix} \\alpha \\\\\\ \\beta \\end{bmatrix}$</td>\n <td>$\\langle\\psi| = \\begin{bmatrix} \\overline{\\alpha} & \\overline{\\beta} \\end{bmatrix}$</td>\n </tr>\n <tr>\n <td>$|\\psi\\rangle = \\alpha|0\\rangle + \\beta|1\\rangle$</td>\n <td>$\\langle\\psi| = \\overline{\\alpha}\\langle0| + \\overline{\\beta}\\langle1|$</td>\n </tr>\n</table>\n\n<p>Kets and bras give us a neat way to express inner and outer products. The inner product of $|\\phi\\rangle$ and $|\\psi\\rangle$ is the matrix product of $\\langle\\phi|$ and $|\\psi\\rangle$, denoted as $\\langle\\phi|\\psi\\rangle$, and their outer product is the matrix product of $|\\phi\\rangle$ and $\\langle\\psi|$, denoted as $|\\phi\\rangle\\langle\\psi|$. Notice that the norm of $|\\psi\\rangle$ is $\\sqrt{\\langle\\psi|\\psi\\rangle}$.</p>\n<p>This brings us to representing matrices. Recall that the outer product of two vectors of the same size produces a square matrix. We can use a linear combination of several outer products of simple vectors (such as basis vectors) to express any square matrix. For example, the $X$ gate can be expressed as follows:</p>\n<p>$$X = |0\\rangle\\langle1| + |1\\rangle\\langle0|$$</p>\n<p>$$\n|0\\rangle\\langle1| + |1\\rangle\\langle0| =\n\\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix}\\begin{bmatrix} 0 & 1 \\end{bmatrix} +\n\\begin{bmatrix} 0 \\\\ 1 \\end{bmatrix}\\begin{bmatrix} 1 & 0 \\end{bmatrix} =\n\\begin{bmatrix} 0 & 1 \\\\ 0 & 0 \\end{bmatrix} + \\begin{bmatrix} 0 & 0 \\\\ 1 & 0 \\end{bmatrix} =\n\\begin{bmatrix} 0 & 1 \\\\ 1 & 0 \\end{bmatrix}\n$$</p>\n<p>This representation can be used to carry out calculations in Dirac notation without ever switching back to matrix representation:</p>\n<p>$$X|0\\rangle = \\big(|0\\rangle\\langle1| + |1\\rangle\\langle0|\\big)|0\\rangle = |0\\rangle\\langle1|0\\rangle + |1\\rangle\\langle0|0\\rangle = |0\\rangle\\big(\\langle1|0\\rangle\\big) + |1\\rangle\\big(\\langle0|0\\rangle\\big) = |0\\rangle(0) + |1\\rangle(1) = |1\\rangle$$</p>\n<blockquote>\n<p>That last step may seem a bit confusing. Recall that $|0\\rangle$ and $|1\\rangle$ form an <strong>orthonormal basis</strong>. That is, they are both normalized, and they are orthogonal to each other.</p>\n<p>A vector is normalized if its norm is equal to $1$, which only happens if its inner product with itself is equal to $1$. This means that $\\langle0|0\\rangle = \\langle1|1\\rangle = 1$</p>\n<p>Two vectors are orthogonal to each other if their inner product equals $0$. This means that $\\langle0|1\\rangle = \\langle 1|0\\rangle = 0$.</p>\n</blockquote>\n<p>In general case, a matrix
|
|
205
|
-
"asMarkdown": "\nThere is another way to represent quantum gates, this time using Dirac notation. However, the kets we've been using aren't enough to represent arbitrary matrices. We need to introduce another piece of notation: the **bra** (this is why Dirac notation is sometimes called **bra-ket notation**).\n\nRecall that kets represent column vectors; a bra is a ket's row vector counterpart. For any ket $|\\psi\\rangle$, the corresponding bra is its adjoint (conjugate transpose): $\\langle\\psi| = |\\psi\\rangle^\\dagger$.\n\nSome examples:\n\n<table>\n <tr>\n <th>Ket</th>\n <th>Bra</th>\n </tr>\n <tr>\n <td>$|0\\rangle = \\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix}$</td>\n <td>$\\langle0| = \\begin{bmatrix} 1 & 0 \\end{bmatrix}$</td>\n </tr>\n <tr>\n <td>$|1\\rangle = \\begin{bmatrix} 0 \\\\\\ 1 \\end{bmatrix}$</td>\n <td>$\\langle1| = \\begin{bmatrix} 0 & 1 \\end{bmatrix}$</td>\n </tr>\n <tr>\n <td>$|i\\rangle = \\begin{bmatrix} \\frac{1}{\\sqrt{2}} \\\\\\ \\frac{i}{\\sqrt{2}} \\end{bmatrix}$</td>\n <td>$\\langle i| = \\begin{bmatrix} \\frac{1}{\\sqrt{2}} & -\\frac{i}{\\sqrt{2}} \\end{bmatrix}$</td>\n </tr>\n <tr>\n <td>$|\\psi\\rangle = \\begin{bmatrix} \\alpha \\\\\\ \\beta \\end{bmatrix}$</td>\n <td>$\\langle\\psi| = \\begin{bmatrix} \\overline{\\alpha} & \\overline{\\beta} \\end{bmatrix}$</td>\n </tr>\n <tr>\n <td>$|\\psi\\rangle = \\alpha|0\\rangle + \\beta|1\\rangle$</td>\n <td>$\\langle\\psi| = \\overline{\\alpha}\\langle0| + \\overline{\\beta}\\langle1|$</td>\n </tr>\n</table>\n\nKets and bras give us a neat way to express inner and outer products. The inner product of $|\\phi\\rangle$ and $|\\psi\\rangle$ is the matrix product of $\\langle\\phi|$ and $|\\psi\\rangle$, denoted as $\\langle\\phi|\\psi\\rangle$, and their outer product is the matrix product of $|\\phi\\rangle$ and $\\langle\\psi|$, denoted as $|\\phi\\rangle\\langle\\psi|$. Notice that the norm of $|\\psi\\rangle$ is $\\sqrt{\\langle\\psi|\\psi\\rangle}$.\n\nThis brings us to representing matrices. Recall that the outer product of two vectors of the same size produces a square matrix. We can use a linear combination of several outer products of simple vectors (such as basis vectors) to express any square matrix. For example, the $X$ gate can be expressed as follows:\n\n$$X = |0\\rangle\\langle1| + |1\\rangle\\langle0|$$\n\n$$\n|0\\rangle\\langle1| + |1\\rangle\\langle0| =\n\\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix}\\begin{bmatrix} 0 & 1 \\end{bmatrix} +\n\\begin{bmatrix} 0 \\\\\\ 1 \\end{bmatrix}\\begin{bmatrix} 1 & 0 \\end{bmatrix} =\n\\begin{bmatrix} 0 & 1 \\\\\\ 0 & 0 \\end{bmatrix} + \\begin{bmatrix} 0 & 0 \\\\\\ 1 & 0 \\end{bmatrix} =\n\\begin{bmatrix} 0 & 1 \\\\\\ 1 & 0 \\end{bmatrix}\n$$\n\nThis representation can be used to carry out calculations in Dirac notation without ever switching back to matrix representation:\n\n$$X|0\\rangle = \\big(|0\\rangle\\langle1| + |1\\rangle\\langle0|\\big)|0\\rangle = |0\\rangle\\langle1|0\\rangle + |1\\rangle\\langle0|0\\rangle = |0\\rangle\\big(\\langle1|0\\rangle\\big) + |1\\rangle\\big(\\langle0|0\\rangle\\big) = |0\\rangle(0) + |1\\rangle(1) = |1\\rangle$$\n\n> That last step may seem a bit confusing. Recall that $|0\\rangle$ and $|1\\rangle$ form an **orthonormal basis**. That is, they are both normalized, and they are orthogonal to each other.\n>\n> A vector is normalized if its norm is equal to $1$, which only happens if its inner product with itself is equal to $1$. This means that $\\langle0|0\\rangle = \\langle1|1\\rangle = 1$\n>\n> Two vectors are orthogonal to each other if their inner product equals $0$. This means that $\\langle0|1\\rangle = \\langle 1|0\\rangle = 0$.\n\nIn general case, a matrix
|
|
204
|
+
"asHtml": "<p>There is another way to represent quantum gates, this time using Dirac notation. However, the kets we've been using aren't enough to represent arbitrary matrices. We need to introduce another piece of notation: the <strong>bra</strong> (this is why Dirac notation is sometimes called <strong>bra-ket notation</strong>).</p>\n<p>Recall that kets represent column vectors; a bra is a ket's row vector counterpart. For any ket $|\\psi\\rangle$, the corresponding bra is its adjoint (conjugate transpose): $\\langle\\psi| = |\\psi\\rangle^\\dagger$.</p>\n<p>Some examples:</p>\n<table>\n <tr>\n <th>Ket</th>\n <th>Bra</th>\n </tr>\n <tr>\n <td>$|0\\rangle = \\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix}$</td>\n <td>$\\langle0| = \\begin{bmatrix} 1 & 0 \\end{bmatrix}$</td>\n </tr>\n <tr>\n <td>$|1\\rangle = \\begin{bmatrix} 0 \\\\\\ 1 \\end{bmatrix}$</td>\n <td>$\\langle1| = \\begin{bmatrix} 0 & 1 \\end{bmatrix}$</td>\n </tr>\n <tr>\n <td>$|i\\rangle = \\begin{bmatrix} \\frac{1}{\\sqrt{2}} \\\\\\ \\frac{i}{\\sqrt{2}} \\end{bmatrix}$</td>\n <td>$\\langle i| = \\begin{bmatrix} \\frac{1}{\\sqrt{2}} & -\\frac{i}{\\sqrt{2}} \\end{bmatrix}$</td>\n </tr>\n <tr>\n <td>$|\\psi\\rangle = \\begin{bmatrix} \\alpha \\\\\\ \\beta \\end{bmatrix}$</td>\n <td>$\\langle\\psi| = \\begin{bmatrix} \\overline{\\alpha} & \\overline{\\beta} \\end{bmatrix}$</td>\n </tr>\n <tr>\n <td>$|\\psi\\rangle = \\alpha|0\\rangle + \\beta|1\\rangle$</td>\n <td>$\\langle\\psi| = \\overline{\\alpha}\\langle0| + \\overline{\\beta}\\langle1|$</td>\n </tr>\n</table>\n\n<p>Kets and bras give us a neat way to express inner and outer products. The inner product of $|\\phi\\rangle$ and $|\\psi\\rangle$ is the matrix product of $\\langle\\phi|$ and $|\\psi\\rangle$, denoted as $\\langle\\phi|\\psi\\rangle$, and their outer product is the matrix product of $|\\phi\\rangle$ and $\\langle\\psi|$, denoted as $|\\phi\\rangle\\langle\\psi|$. Notice that the norm of $|\\psi\\rangle$ is $\\sqrt{\\langle\\psi|\\psi\\rangle}$.</p>\n<p>This brings us to representing matrices. Recall that the outer product of two vectors of the same size produces a square matrix. We can use a linear combination of several outer products of simple vectors (such as basis vectors) to express any square matrix. For example, the $X$ gate can be expressed as follows:</p>\n<p>$$X = |0\\rangle\\langle1| + |1\\rangle\\langle0|$$</p>\n<p>$$\n|0\\rangle\\langle1| + |1\\rangle\\langle0| =\n\\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix}\\begin{bmatrix} 0 & 1 \\end{bmatrix} +\n\\begin{bmatrix} 0 \\\\ 1 \\end{bmatrix}\\begin{bmatrix} 1 & 0 \\end{bmatrix} =\n\\begin{bmatrix} 0 & 1 \\\\ 0 & 0 \\end{bmatrix} + \\begin{bmatrix} 0 & 0 \\\\ 1 & 0 \\end{bmatrix} =\n\\begin{bmatrix} 0 & 1 \\\\ 1 & 0 \\end{bmatrix}\n$$</p>\n<p>This representation can be used to carry out calculations in Dirac notation without ever switching back to matrix representation:</p>\n<p>$$X|0\\rangle = \\big(|0\\rangle\\langle1| + |1\\rangle\\langle0|\\big)|0\\rangle = |0\\rangle\\langle1|0\\rangle + |1\\rangle\\langle0|0\\rangle = |0\\rangle\\big(\\langle1|0\\rangle\\big) + |1\\rangle\\big(\\langle0|0\\rangle\\big) = |0\\rangle(0) + |1\\rangle(1) = |1\\rangle$$</p>\n<blockquote>\n<p>That last step may seem a bit confusing. Recall that $|0\\rangle$ and $|1\\rangle$ form an <strong>orthonormal basis</strong>. That is, they are both normalized, and they are orthogonal to each other.</p>\n<p>A vector is normalized if its norm is equal to $1$, which only happens if its inner product with itself is equal to $1$. This means that $\\langle0|0\\rangle = \\langle1|1\\rangle = 1$</p>\n<p>Two vectors are orthogonal to each other if their inner product equals $0$. This means that $\\langle0|1\\rangle = \\langle 1|0\\rangle = 0$.</p>\n</blockquote>\n<p>In general case, a matrix\n$$A = \\begin{bmatrix} a_{00} & a_{01} \\\\ a_{10} & a_{11} \\end{bmatrix}$$\nwill have the following ket-bra representation:\n$$A = a_{00} |0\\rangle\\langle0| + a_{01} |0\\rangle\\langle1| + a_{10} |1\\rangle\\langle0| + a_{11} |1\\rangle\\langle1|$$</p>\n",
|
|
205
|
+
"asMarkdown": "\nThere is another way to represent quantum gates, this time using Dirac notation. However, the kets we've been using aren't enough to represent arbitrary matrices. We need to introduce another piece of notation: the **bra** (this is why Dirac notation is sometimes called **bra-ket notation**).\n\nRecall that kets represent column vectors; a bra is a ket's row vector counterpart. For any ket $|\\psi\\rangle$, the corresponding bra is its adjoint (conjugate transpose): $\\langle\\psi| = |\\psi\\rangle^\\dagger$.\n\nSome examples:\n\n<table>\n <tr>\n <th>Ket</th>\n <th>Bra</th>\n </tr>\n <tr>\n <td>$|0\\rangle = \\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix}$</td>\n <td>$\\langle0| = \\begin{bmatrix} 1 & 0 \\end{bmatrix}$</td>\n </tr>\n <tr>\n <td>$|1\\rangle = \\begin{bmatrix} 0 \\\\\\ 1 \\end{bmatrix}$</td>\n <td>$\\langle1| = \\begin{bmatrix} 0 & 1 \\end{bmatrix}$</td>\n </tr>\n <tr>\n <td>$|i\\rangle = \\begin{bmatrix} \\frac{1}{\\sqrt{2}} \\\\\\ \\frac{i}{\\sqrt{2}} \\end{bmatrix}$</td>\n <td>$\\langle i| = \\begin{bmatrix} \\frac{1}{\\sqrt{2}} & -\\frac{i}{\\sqrt{2}} \\end{bmatrix}$</td>\n </tr>\n <tr>\n <td>$|\\psi\\rangle = \\begin{bmatrix} \\alpha \\\\\\ \\beta \\end{bmatrix}$</td>\n <td>$\\langle\\psi| = \\begin{bmatrix} \\overline{\\alpha} & \\overline{\\beta} \\end{bmatrix}$</td>\n </tr>\n <tr>\n <td>$|\\psi\\rangle = \\alpha|0\\rangle + \\beta|1\\rangle$</td>\n <td>$\\langle\\psi| = \\overline{\\alpha}\\langle0| + \\overline{\\beta}\\langle1|$</td>\n </tr>\n</table>\n\nKets and bras give us a neat way to express inner and outer products. The inner product of $|\\phi\\rangle$ and $|\\psi\\rangle$ is the matrix product of $\\langle\\phi|$ and $|\\psi\\rangle$, denoted as $\\langle\\phi|\\psi\\rangle$, and their outer product is the matrix product of $|\\phi\\rangle$ and $\\langle\\psi|$, denoted as $|\\phi\\rangle\\langle\\psi|$. Notice that the norm of $|\\psi\\rangle$ is $\\sqrt{\\langle\\psi|\\psi\\rangle}$.\n\nThis brings us to representing matrices. Recall that the outer product of two vectors of the same size produces a square matrix. We can use a linear combination of several outer products of simple vectors (such as basis vectors) to express any square matrix. For example, the $X$ gate can be expressed as follows:\n\n$$X = |0\\rangle\\langle1| + |1\\rangle\\langle0|$$\n\n$$\n|0\\rangle\\langle1| + |1\\rangle\\langle0| =\n\\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix}\\begin{bmatrix} 0 & 1 \\end{bmatrix} +\n\\begin{bmatrix} 0 \\\\\\ 1 \\end{bmatrix}\\begin{bmatrix} 1 & 0 \\end{bmatrix} =\n\\begin{bmatrix} 0 & 1 \\\\\\ 0 & 0 \\end{bmatrix} + \\begin{bmatrix} 0 & 0 \\\\\\ 1 & 0 \\end{bmatrix} =\n\\begin{bmatrix} 0 & 1 \\\\\\ 1 & 0 \\end{bmatrix}\n$$\n\nThis representation can be used to carry out calculations in Dirac notation without ever switching back to matrix representation:\n\n$$X|0\\rangle = \\big(|0\\rangle\\langle1| + |1\\rangle\\langle0|\\big)|0\\rangle = |0\\rangle\\langle1|0\\rangle + |1\\rangle\\langle0|0\\rangle = |0\\rangle\\big(\\langle1|0\\rangle\\big) + |1\\rangle\\big(\\langle0|0\\rangle\\big) = |0\\rangle(0) + |1\\rangle(1) = |1\\rangle$$\n\n> That last step may seem a bit confusing. Recall that $|0\\rangle$ and $|1\\rangle$ form an **orthonormal basis**. That is, they are both normalized, and they are orthogonal to each other.\n>\n> A vector is normalized if its norm is equal to $1$, which only happens if its inner product with itself is equal to $1$. This means that $\\langle0|0\\rangle = \\langle1|1\\rangle = 1$\n>\n> Two vectors are orthogonal to each other if their inner product equals $0$. This means that $\\langle0|1\\rangle = \\langle 1|0\\rangle = 0$.\n\nIn general case, a matrix\n$$A = \\begin{bmatrix} a_{00} & a_{01} \\\\\\ a_{10} & a_{11} \\end{bmatrix}$$\nwill have the following ket-bra representation:\n$$A = a_{00} |0\\rangle\\langle0| + a_{01} |0\\rangle\\langle1| + a_{10} |1\\rangle\\langle0| + a_{11} |1\\rangle\\langle1|$$"
|
|
206
206
|
}
|
|
207
207
|
]
|
|
208
208
|
},
|
|
@@ -225,8 +225,8 @@ export default {
|
|
|
225
225
|
"items": [
|
|
226
226
|
{
|
|
227
227
|
"type": "text-content",
|
|
228
|
-
"asHtml": "<p>This section introduces some of the common single-qubit gates, including their matrix form, their ket-bra decomposition, and a brief "cheatsheet" listing their effect on some common qubit states.</p>\n<p>You can use a tool called <a href=\"https://algassert.com/quirk\" target=\"_blank\">Quirk</a> to visualize how these gates interact with various qubit states.</p>\n<p>This section relies on the following notation:</p>\n<table>\n <tr>\n <td>$|+\\rangle = \\frac{1}{\\sqrt{2}}\\big(|0\\rangle + |1\\rangle\\big)$</td>\n <td>$|-\\rangle = \\frac{1}{\\sqrt{2}}\\big(|0\\rangle - |1\\rangle\\big)$</td>\n </tr>\n <tr>\n <td>$|i\\rangle = \\frac{1}{\\sqrt{2}}\\big(|0\\rangle + i|1\\rangle\\big)$</td>\n <td>$|-i\\rangle = \\frac{1}{\\sqrt{2}}\\big(|0\\rangle - i|1\\rangle\\big)$</td>\n </tr>\n</table>\n\n<p>The Pauli gates, named after <a href=\"https://en.wikipedia.org/wiki/Wolfgang_Pauli\" target=\"_blank\">Wolfgang Pauli</a>, are based on the so-called <strong>Pauli matrices</strong>, $X$, $Y$ and $Z$. All three Pauli gates are <strong>self-adjoint</strong>, meaning that each one is its own inverse, $XX = \\mathbb{I}$.</p>\n<table>\n <tr>\n <th>Gate</th>\n <th>Matrix</th>\n <th>Ket-Bra</th>\n <th>Applying to $|\\psi\\rangle = \\alpha|0\\rangle + \\beta|1\\rangle$</th>\n <th>Applying to basis states</th>\n </tr>\n <tr>\n <td>$X$</td>\n <td>$\\begin{bmatrix} 0 & 1 \\\\\\ 1 & 0 \\end{bmatrix}$</td>\n <td>$|0\\rangle\\langle1| + |1\\rangle\\langle0|$</td>\n <td>$X|\\psi\\rangle = \\alpha|1\\rangle + \\beta|0\\rangle$</td>\n <td>\n $X|0\\rangle = |1\\rangle$<br>\n $X|1\\rangle = |0\\rangle$<br>\n $X|+\\rangle = |+\\rangle$<br>\n $X|-\\rangle = -|-\\rangle$<br>\n $X|i\\rangle = i|-i\\rangle$<br>\n $X|-i\\rangle = -i|i\\rangle$\n </td>\n </tr>\n <tr>\n <td>$Y$</td>\n <td>$\\begin{bmatrix} 0 & -i \\\\\\ i & 0 \\end{bmatrix}$</td>\n <td>$i(|1\\rangle\\langle0| - |0\\rangle\\langle1|)$</td>\n <td>$Y|\\psi\\rangle = i\\big(\\alpha|1\\rangle - \\beta|0\\rangle\\big)$</td>\n <td>\n $Y|0\\rangle = i|1\\rangle$<br>\n $Y|1\\rangle = -i|0\\rangle$<br>\n $Y|+\\rangle = -i|-\\rangle$<br>\n $Y|-\\rangle = i|+\\rangle$<br>\n $Y|i\\rangle = |i\\rangle$<br>\n $Y|-i\\rangle = -|-i\\rangle$<br>\n </td>\n </tr>\n <tr>\n <td>$Z$</td>\n <td>$\\begin{bmatrix} 1 & 0 \\\\\\ 0 & -1 \\end{bmatrix}$</td>\n <td>$|0\\rangle\\langle0| - |1\\rangle\\langle1|$</td>\n <td>$Z|\\psi\\rangle = \\alpha|0\\rangle - \\beta|1\\rangle$</td>\n <td>\n $Z|0\\rangle = |0\\rangle$<br>\n $Z|1\\rangle = -|1\\rangle$<br>\n $Z|+\\rangle = |-\\rangle$<br>\n $Z|-\\rangle = |+\\rangle$<br>\n $Z|i\\rangle = |-i\\rangle$<br>\n $Z|-i\\rangle = |i\\rangle$<br>\n </td>\n </tr>\n</table>\n\n<blockquote>\n<p>The $X$ gate is sometimes referred to as the <strong>bit flip</strong> gate, or the <strong>NOT</strong> gate, because it acts like the classical NOT gate on the computational basis.</p>\n<p>The $Z$ gate is sometimes referred to as the <strong>phase flip</strong> gate.</p>\n</blockquote>\n<p>Here are several properties of the Pauli gates that are easy to verify and convenient to remember:</p>\n<ul>\n<li>Different Pauli gates <em>anti-commute</em>:\n$$XZ = -ZX, XY = -YX, YZ = -ZY$$</li>\n<li>A product of any two Pauli gates equals the third gate, with an extra $i$ (or $-i$) phase:\n$$XY = iZ, YZ = iX, ZX = iY
|
|
229
|
-
"asMarkdown": "\nThis section introduces some of the common single-qubit gates, including their matrix form, their ket-bra decomposition, and a brief \"cheatsheet\" listing their effect on some common qubit states.\n\nYou can use a tool called <a href=\"https://algassert.com/quirk\" target=\"_blank\">Quirk</a> to visualize how these gates interact with various qubit states.\n\nThis section relies on the following notation:\n\n<table>\n <tr>\n <td>$|+\\rangle = \\frac{1}{\\sqrt{2}}\\big(|0\\rangle + |1\\rangle\\big)$</td>\n <td>$|-\\rangle = \\frac{1}{\\sqrt{2}}\\big(|0\\rangle - |1\\rangle\\big)$</td>\n </tr>\n <tr>\n <td>$|i\\rangle = \\frac{1}{\\sqrt{2}}\\big(|0\\rangle + i|1\\rangle\\big)$</td>\n <td>$|-i\\rangle = \\frac{1}{\\sqrt{2}}\\big(|0\\rangle - i|1\\rangle\\big)$</td>\n </tr>\n</table>\n\nThe Pauli gates, named after <a href=\"https://en.wikipedia.org/wiki/Wolfgang_Pauli\" target=\"_blank\">Wolfgang Pauli</a>, are based on the so-called **Pauli matrices**, $X$, $Y$ and $Z$. All three Pauli gates are **self-adjoint**, meaning that each one is its own inverse, $XX = \\mathbb{I}$.\n\n<table>\n <tr>\n <th>Gate</th>\n <th>Matrix</th>\n <th>Ket-Bra</th>\n <th>Applying to $|\\psi\\rangle = \\alpha|0\\rangle + \\beta|1\\rangle$</th>\n <th>Applying to basis states</th>\n </tr>\n <tr>\n <td>$X$</td>\n <td>$\\begin{bmatrix} 0 & 1 \\\\\\ 1 & 0 \\end{bmatrix}$</td>\n <td>$|0\\rangle\\langle1| + |1\\rangle\\langle0|$</td>\n <td>$X|\\psi\\rangle = \\alpha|1\\rangle + \\beta|0\\rangle$</td>\n <td>\n $X|0\\rangle = |1\\rangle$<br>\n $X|1\\rangle = |0\\rangle$<br>\n $X|+\\rangle = |+\\rangle$<br>\n $X|-\\rangle = -|-\\rangle$<br>\n $X|i\\rangle = i|-i\\rangle$<br>\n $X|-i\\rangle = -i|i\\rangle$\n </td>\n </tr>\n <tr>\n <td>$Y$</td>\n <td>$\\begin{bmatrix} 0 & -i \\\\\\ i & 0 \\end{bmatrix}$</td>\n <td>$i(|1\\rangle\\langle0| - |0\\rangle\\langle1|)$</td>\n <td>$Y|\\psi\\rangle = i\\big(\\alpha|1\\rangle - \\beta|0\\rangle\\big)$</td>\n <td>\n $Y|0\\rangle = i|1\\rangle$<br>\n $Y|1\\rangle = -i|0\\rangle$<br>\n $Y|+\\rangle = -i|-\\rangle$<br>\n $Y|-\\rangle = i|+\\rangle$<br>\n $Y|i\\rangle = |i\\rangle$<br>\n $Y|-i\\rangle = -|-i\\rangle$<br>\n </td>\n </tr>\n <tr>\n <td>$Z$</td>\n <td>$\\begin{bmatrix} 1 & 0 \\\\\\ 0 & -1 \\end{bmatrix}$</td>\n <td>$|0\\rangle\\langle0| - |1\\rangle\\langle1|$</td>\n <td>$Z|\\psi\\rangle = \\alpha|0\\rangle - \\beta|1\\rangle$</td>\n <td>\n $Z|0\\rangle = |0\\rangle$<br>\n $Z|1\\rangle = -|1\\rangle$<br>\n $Z|+\\rangle = |-\\rangle$<br>\n $Z|-\\rangle = |+\\rangle$<br>\n $Z|i\\rangle = |-i\\rangle$<br>\n $Z|-i\\rangle = |i\\rangle$<br>\n </td>\n </tr>\n</table>\n\n> The $X$ gate is sometimes referred to as the **bit flip** gate, or the **NOT** gate, because it acts like the classical NOT gate on the computational basis.\n>\n> The $Z$ gate is sometimes referred to as the **phase flip** gate.\n\nHere are several properties of the Pauli gates that are easy to verify and convenient to remember:\n\n- Different Pauli gates
|
|
228
|
+
"asHtml": "<p>This section introduces some of the common single-qubit gates, including their matrix form, their ket-bra decomposition, and a brief "cheatsheet" listing their effect on some common qubit states.</p>\n<p>You can use a tool called <a href=\"https://algassert.com/quirk\" target=\"_blank\">Quirk</a> to visualize how these gates interact with various qubit states.</p>\n<p>This section relies on the following notation:</p>\n<table>\n <tr>\n <td>$|+\\rangle = \\frac{1}{\\sqrt{2}}\\big(|0\\rangle + |1\\rangle\\big)$</td>\n <td>$|-\\rangle = \\frac{1}{\\sqrt{2}}\\big(|0\\rangle - |1\\rangle\\big)$</td>\n </tr>\n <tr>\n <td>$|i\\rangle = \\frac{1}{\\sqrt{2}}\\big(|0\\rangle + i|1\\rangle\\big)$</td>\n <td>$|-i\\rangle = \\frac{1}{\\sqrt{2}}\\big(|0\\rangle - i|1\\rangle\\big)$</td>\n </tr>\n</table>\n\n<p>The Pauli gates, named after <a href=\"https://en.wikipedia.org/wiki/Wolfgang_Pauli\" target=\"_blank\">Wolfgang Pauli</a>, are based on the so-called <strong>Pauli matrices</strong>, $X$, $Y$ and $Z$. All three Pauli gates are <strong>self-adjoint</strong>, meaning that each one is its own inverse, $XX = \\mathbb{I}$.</p>\n<table>\n <tr>\n <th>Gate</th>\n <th>Matrix</th>\n <th>Ket-Bra</th>\n <th>Applying to $|\\psi\\rangle = \\alpha|0\\rangle + \\beta|1\\rangle$</th>\n <th>Applying to basis states</th>\n </tr>\n <tr>\n <td>$X$</td>\n <td>$\\begin{bmatrix} 0 & 1 \\\\\\ 1 & 0 \\end{bmatrix}$</td>\n <td>$|0\\rangle\\langle1| + |1\\rangle\\langle0|$</td>\n <td>$X|\\psi\\rangle = \\alpha|1\\rangle + \\beta|0\\rangle$</td>\n <td>\n $X|0\\rangle = |1\\rangle$<br>\n $X|1\\rangle = |0\\rangle$<br>\n $X|+\\rangle = |+\\rangle$<br>\n $X|-\\rangle = -|-\\rangle$<br>\n $X|i\\rangle = i|-i\\rangle$<br>\n $X|-i\\rangle = -i|i\\rangle$\n </td>\n </tr>\n <tr>\n <td>$Y$</td>\n <td>$\\begin{bmatrix} 0 & -i \\\\\\ i & 0 \\end{bmatrix}$</td>\n <td>$i(|1\\rangle\\langle0| - |0\\rangle\\langle1|)$</td>\n <td>$Y|\\psi\\rangle = i\\big(\\alpha|1\\rangle - \\beta|0\\rangle\\big)$</td>\n <td>\n $Y|0\\rangle = i|1\\rangle$<br>\n $Y|1\\rangle = -i|0\\rangle$<br>\n $Y|+\\rangle = -i|-\\rangle$<br>\n $Y|-\\rangle = i|+\\rangle$<br>\n $Y|i\\rangle = |i\\rangle$<br>\n $Y|-i\\rangle = -|-i\\rangle$<br>\n </td>\n </tr>\n <tr>\n <td>$Z$</td>\n <td>$\\begin{bmatrix} 1 & 0 \\\\\\ 0 & -1 \\end{bmatrix}$</td>\n <td>$|0\\rangle\\langle0| - |1\\rangle\\langle1|$</td>\n <td>$Z|\\psi\\rangle = \\alpha|0\\rangle - \\beta|1\\rangle$</td>\n <td>\n $Z|0\\rangle = |0\\rangle$<br>\n $Z|1\\rangle = -|1\\rangle$<br>\n $Z|+\\rangle = |-\\rangle$<br>\n $Z|-\\rangle = |+\\rangle$<br>\n $Z|i\\rangle = |-i\\rangle$<br>\n $Z|-i\\rangle = |i\\rangle$<br>\n </td>\n </tr>\n</table>\n\n<blockquote>\n<p>The $X$ gate is sometimes referred to as the <strong>bit flip</strong> gate, or the <strong>NOT</strong> gate, because it acts like the classical NOT gate on the computational basis.</p>\n<p>The $Z$ gate is sometimes referred to as the <strong>phase flip</strong> gate.</p>\n</blockquote>\n<p>Here are several properties of the Pauli gates that are easy to verify and convenient to remember:</p>\n<ul>\n<li>Different Pauli gates <em>anti-commute</em>:\n$$XZ = -ZX, XY = -YX, YZ = -ZY$$</li>\n<li>A product of any two Pauli gates equals the third gate, with an extra $i$ (or $-i$) phase:\n$$XY = iZ, YZ = iX, ZX = iY$$</li>\n<li>A product of all three Pauli gates equals identity (with an extra $i$ phase):\n$$XYZ = iI$$</li>\n</ul>\n",
|
|
229
|
+
"asMarkdown": "\nThis section introduces some of the common single-qubit gates, including their matrix form, their ket-bra decomposition, and a brief \"cheatsheet\" listing their effect on some common qubit states.\n\nYou can use a tool called <a href=\"https://algassert.com/quirk\" target=\"_blank\">Quirk</a> to visualize how these gates interact with various qubit states.\n\nThis section relies on the following notation:\n\n<table>\n <tr>\n <td>$|+\\rangle = \\frac{1}{\\sqrt{2}}\\big(|0\\rangle + |1\\rangle\\big)$</td>\n <td>$|-\\rangle = \\frac{1}{\\sqrt{2}}\\big(|0\\rangle - |1\\rangle\\big)$</td>\n </tr>\n <tr>\n <td>$|i\\rangle = \\frac{1}{\\sqrt{2}}\\big(|0\\rangle + i|1\\rangle\\big)$</td>\n <td>$|-i\\rangle = \\frac{1}{\\sqrt{2}}\\big(|0\\rangle - i|1\\rangle\\big)$</td>\n </tr>\n</table>\n\nThe Pauli gates, named after <a href=\"https://en.wikipedia.org/wiki/Wolfgang_Pauli\" target=\"_blank\">Wolfgang Pauli</a>, are based on the so-called **Pauli matrices**, $X$, $Y$ and $Z$. All three Pauli gates are **self-adjoint**, meaning that each one is its own inverse, $XX = \\mathbb{I}$.\n\n<table>\n <tr>\n <th>Gate</th>\n <th>Matrix</th>\n <th>Ket-Bra</th>\n <th>Applying to $|\\psi\\rangle = \\alpha|0\\rangle + \\beta|1\\rangle$</th>\n <th>Applying to basis states</th>\n </tr>\n <tr>\n <td>$X$</td>\n <td>$\\begin{bmatrix} 0 & 1 \\\\\\ 1 & 0 \\end{bmatrix}$</td>\n <td>$|0\\rangle\\langle1| + |1\\rangle\\langle0|$</td>\n <td>$X|\\psi\\rangle = \\alpha|1\\rangle + \\beta|0\\rangle$</td>\n <td>\n $X|0\\rangle = |1\\rangle$<br>\n $X|1\\rangle = |0\\rangle$<br>\n $X|+\\rangle = |+\\rangle$<br>\n $X|-\\rangle = -|-\\rangle$<br>\n $X|i\\rangle = i|-i\\rangle$<br>\n $X|-i\\rangle = -i|i\\rangle$\n </td>\n </tr>\n <tr>\n <td>$Y$</td>\n <td>$\\begin{bmatrix} 0 & -i \\\\\\ i & 0 \\end{bmatrix}$</td>\n <td>$i(|1\\rangle\\langle0| - |0\\rangle\\langle1|)$</td>\n <td>$Y|\\psi\\rangle = i\\big(\\alpha|1\\rangle - \\beta|0\\rangle\\big)$</td>\n <td>\n $Y|0\\rangle = i|1\\rangle$<br>\n $Y|1\\rangle = -i|0\\rangle$<br>\n $Y|+\\rangle = -i|-\\rangle$<br>\n $Y|-\\rangle = i|+\\rangle$<br>\n $Y|i\\rangle = |i\\rangle$<br>\n $Y|-i\\rangle = -|-i\\rangle$<br>\n </td>\n </tr>\n <tr>\n <td>$Z$</td>\n <td>$\\begin{bmatrix} 1 & 0 \\\\\\ 0 & -1 \\end{bmatrix}$</td>\n <td>$|0\\rangle\\langle0| - |1\\rangle\\langle1|$</td>\n <td>$Z|\\psi\\rangle = \\alpha|0\\rangle - \\beta|1\\rangle$</td>\n <td>\n $Z|0\\rangle = |0\\rangle$<br>\n $Z|1\\rangle = -|1\\rangle$<br>\n $Z|+\\rangle = |-\\rangle$<br>\n $Z|-\\rangle = |+\\rangle$<br>\n $Z|i\\rangle = |-i\\rangle$<br>\n $Z|-i\\rangle = |i\\rangle$<br>\n </td>\n </tr>\n</table>\n\n> The $X$ gate is sometimes referred to as the **bit flip** gate, or the **NOT** gate, because it acts like the classical NOT gate on the computational basis.\n>\n> The $Z$ gate is sometimes referred to as the **phase flip** gate.\n\nHere are several properties of the Pauli gates that are easy to verify and convenient to remember:\n\n- Different Pauli gates _anti-commute_:\n $$XZ = -ZX, XY = -YX, YZ = -ZY$$\n- A product of any two Pauli gates equals the third gate, with an extra $i$ (or $-i$) phase:\n $$XY = iZ, YZ = iX, ZX = iY$$\n- A product of all three Pauli gates equals identity (with an extra $i$ phase):\n $$XYZ = iI$$"
|
|
230
230
|
}
|
|
231
231
|
]
|
|
232
232
|
},
|
|
@@ -257,8 +257,8 @@ export default {
|
|
|
257
257
|
"asMarkdown": "**Input:** A qubit in an arbitrary state $|\\\\psi\\\\rangle = \\\\alpha|0\\\\rangle + \\\\beta|1\\\\rangle$.\n\n**Goal:** Apply the Y gate to the qubit, i.e., transform the given state into $i\\\\alpha|1\\\\rangle - i\\\\beta|0\\\\rangle$."
|
|
258
258
|
},
|
|
259
259
|
"sourceIds": [
|
|
260
|
-
"
|
|
261
|
-
"
|
|
260
|
+
"single_qubit_gates__y_gate__Verification.qs",
|
|
261
|
+
"KatasLibrary.qs"
|
|
262
262
|
],
|
|
263
263
|
"placeholderCode": "namespace Kata {\n operation ApplyY(q : Qubit) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}",
|
|
264
264
|
"explainedSolution": {
|
|
@@ -287,8 +287,8 @@ export default {
|
|
|
287
287
|
"asMarkdown": "**Input:** A qubit in an arbitrary state $|\\\\psi\\\\rangle = \\\\alpha|0\\\\rangle + \\\\beta|1\\\\rangle$.\n\n**Goal:** Use several Pauli gates to change the qubit state to $i|\\\\psi\\\\rangle = i\\\\alpha|0\\\\rangle + i\\\\beta|1\\\\rangle$."
|
|
288
288
|
},
|
|
289
289
|
"sourceIds": [
|
|
290
|
-
"
|
|
291
|
-
"
|
|
290
|
+
"single_qubit_gates__global_phase_i__Verification.qs",
|
|
291
|
+
"KatasLibrary.qs"
|
|
292
292
|
],
|
|
293
293
|
"placeholderCode": "namespace Kata {\n operation GlobalPhaseI(q : Qubit) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}",
|
|
294
294
|
"explainedSolution": {
|
|
@@ -317,8 +317,8 @@ export default {
|
|
|
317
317
|
"asMarkdown": "**Input:** A qubit in an arbitrary state $|\\psi\\rangle = \\alpha|0\\rangle + \\beta|1\\rangle$.\n\n**Goal:** Use several Pauli gates to change the qubit state to $- \\alpha|0\\rangle + \\beta|1\\rangle$, i.e., apply the transformation represented by the following matrix:\n\n$$\\begin{bmatrix} -1 & 0 \\\\\\ 0 & 1 \\end{bmatrix}$$\n"
|
|
318
318
|
},
|
|
319
319
|
"sourceIds": [
|
|
320
|
-
"
|
|
321
|
-
"
|
|
320
|
+
"single_qubit_gates__sign_flip_on_zero__Verification.qs",
|
|
321
|
+
"KatasLibrary.qs"
|
|
322
322
|
],
|
|
323
323
|
"placeholderCode": "namespace Kata {\n operation SignFlipOnZero (q : Qubit) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}\n",
|
|
324
324
|
"explainedSolution": {
|
|
@@ -371,8 +371,8 @@ export default {
|
|
|
371
371
|
"asMarkdown": "**Input:** A qubit in state $|0\\rangle$.\n\n**Goal:** Transform the qubit into state $|-\\rangle$."
|
|
372
372
|
},
|
|
373
373
|
"sourceIds": [
|
|
374
|
-
"
|
|
375
|
-
"
|
|
374
|
+
"single_qubit_gates__prepare_minus__Verification.qs",
|
|
375
|
+
"KatasLibrary.qs"
|
|
376
376
|
],
|
|
377
377
|
"placeholderCode": "namespace Kata {\n operation PrepareMinus(q : Qubit) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}\n",
|
|
378
378
|
"explainedSolution": {
|
|
@@ -398,8 +398,8 @@ export default {
|
|
|
398
398
|
"items": [
|
|
399
399
|
{
|
|
400
400
|
"type": "text-content",
|
|
401
|
-
"asHtml": "<p>The next two gates are known as phase shift gates. They apply a phase to the $|1\\rangle$ state, and leave the $|0\\rangle$ state unchanged.</p>\n<table>\n <tr>\n <th>Gate</th>\n <th>Matrix</th>\n <th>Ket-Bra</th>\n <th>Applying to $|\\psi\\rangle = \\alpha|0\\rangle + \\beta|1\\rangle$</th>\n <th>Applying to basis states</th>\n </tr>\n <tr>\n <td>$S$</td>\n <td>$\\begin{bmatrix} 1 & 0 \\\\ 0 & i \\end{bmatrix}$</td>\n <td>$|0\\rangle\\langle0| + i|1\\rangle\\langle1|$</td>\n <td>$S|\\psi\\rangle = \\alpha|0\\rangle + i\\beta|1\\rangle$</td>\n <td>\n $S|0\\rangle = |0\\rangle$<br>\n $S|1\\rangle = i|1\\rangle$<br>\n $S|+\\rangle = |i\\rangle$<br>\n $S|-\\rangle = |-i\\rangle$<br>\n $S|i\\rangle = |-\\rangle$<br>\n $S|-i\\rangle = |+\\rangle$<br>\n </td>\n </tr>\n <tr>\n <td>$T$</td>\n <td>$\\begin{bmatrix} 1 & 0 \\\\\\ 0 & e^{i\\pi/4} \\end{bmatrix}$</td>\n <td>$|0\\rangle\\langle0| + e^{i\\pi/4}|1\\rangle$$\\langle1|$</td>\n <td>$T|\\psi\\rangle = \\alpha|0\\rangle + e^{i\\pi/4} \\beta |1\\rangle$</td>\n <td>\n $T|0\\rangle = |0\\rangle$<br>\n $T|1\\rangle = e^{i\\pi/4}|1\\rangle$\n </td>\n </tr>\n</table>\n\n<blockquote>\n<p>Notice that applying the $T$ gate twice is equivalent to applying the $S$ gate, and applying the $S$ gate twice is equivalent to applying the $Z$ gate
|
|
402
|
-
"asMarkdown": "\nThe next two gates are known as phase shift gates. They apply a phase to the $|1\\rangle$ state, and leave the $|0\\rangle$ state unchanged.\n\n<table>\n <tr>\n <th>Gate</th>\n <th>Matrix</th>\n <th>Ket-Bra</th>\n <th>Applying to $|\\psi\\rangle = \\alpha|0\\rangle + \\beta|1\\rangle$</th>\n <th>Applying to basis states</th>\n </tr>\n <tr>\n <td>$S$</td>\n <td>$\\begin{bmatrix} 1 & 0 \\\\ 0 & i \\end{bmatrix}$</td>\n <td>$|0\\rangle\\langle0| + i|1\\rangle\\langle1|$</td>\n <td>$S|\\psi\\rangle = \\alpha|0\\rangle + i\\beta|1\\rangle$</td>\n <td>\n $S|0\\rangle = |0\\rangle$<br>\n $S|1\\rangle = i|1\\rangle$<br>\n $S|+\\rangle = |i\\rangle$<br>\n $S|-\\rangle = |-i\\rangle$<br>\n $S|i\\rangle = |-\\rangle$<br>\n $S|-i\\rangle = |+\\rangle$<br>\n </td>\n </tr>\n <tr>\n <td>$T$</td>\n <td>$\\begin{bmatrix} 1 & 0 \\\\\\ 0 & e^{i\\pi/4} \\end{bmatrix}$</td>\n <td>$|0\\rangle\\langle0| + e^{i\\pi/4}|1\\rangle$$\\langle1|$</td>\n <td>$T|\\psi\\rangle = \\alpha|0\\rangle + e^{i\\pi/4} \\beta |1\\rangle$</td>\n <td>\n $T|0\\rangle = |0\\rangle$<br>\n $T|1\\rangle = e^{i\\pi/4}|1\\rangle$\n </td>\n </tr>\n</table>\n\n> Notice that applying the $T$ gate twice is equivalent to applying the $S$ gate, and applying the $S$ gate twice is equivalent to applying the $Z$ gate
|
|
401
|
+
"asHtml": "<p>The next two gates are known as phase shift gates. They apply a phase to the $|1\\rangle$ state, and leave the $|0\\rangle$ state unchanged.</p>\n<table>\n <tr>\n <th>Gate</th>\n <th>Matrix</th>\n <th>Ket-Bra</th>\n <th>Applying to $|\\psi\\rangle = \\alpha|0\\rangle + \\beta|1\\rangle$</th>\n <th>Applying to basis states</th>\n </tr>\n <tr>\n <td>$S$</td>\n <td>$\\begin{bmatrix} 1 & 0 \\\\ 0 & i \\end{bmatrix}$</td>\n <td>$|0\\rangle\\langle0| + i|1\\rangle\\langle1|$</td>\n <td>$S|\\psi\\rangle = \\alpha|0\\rangle + i\\beta|1\\rangle$</td>\n <td>\n $S|0\\rangle = |0\\rangle$<br>\n $S|1\\rangle = i|1\\rangle$<br>\n $S|+\\rangle = |i\\rangle$<br>\n $S|-\\rangle = |-i\\rangle$<br>\n $S|i\\rangle = |-\\rangle$<br>\n $S|-i\\rangle = |+\\rangle$<br>\n </td>\n </tr>\n <tr>\n <td>$T$</td>\n <td>$\\begin{bmatrix} 1 & 0 \\\\\\ 0 & e^{i\\pi/4} \\end{bmatrix}$</td>\n <td>$|0\\rangle\\langle0| + e^{i\\pi/4}|1\\rangle$$\\langle1|$</td>\n <td>$T|\\psi\\rangle = \\alpha|0\\rangle + e^{i\\pi/4} \\beta |1\\rangle$</td>\n <td>\n $T|0\\rangle = |0\\rangle$<br>\n $T|1\\rangle = e^{i\\pi/4}|1\\rangle$\n </td>\n </tr>\n</table>\n\n<blockquote>\n<p>Notice that applying the $T$ gate twice is equivalent to applying the $S$ gate, and applying the $S$ gate twice is equivalent to applying the $Z$ gate:\n$$T^2 = S, S^2 = Z$$</p>\n</blockquote>\n",
|
|
402
|
+
"asMarkdown": "\nThe next two gates are known as phase shift gates. They apply a phase to the $|1\\rangle$ state, and leave the $|0\\rangle$ state unchanged.\n\n<table>\n <tr>\n <th>Gate</th>\n <th>Matrix</th>\n <th>Ket-Bra</th>\n <th>Applying to $|\\psi\\rangle = \\alpha|0\\rangle + \\beta|1\\rangle$</th>\n <th>Applying to basis states</th>\n </tr>\n <tr>\n <td>$S$</td>\n <td>$\\begin{bmatrix} 1 & 0 \\\\ 0 & i \\end{bmatrix}$</td>\n <td>$|0\\rangle\\langle0| + i|1\\rangle\\langle1|$</td>\n <td>$S|\\psi\\rangle = \\alpha|0\\rangle + i\\beta|1\\rangle$</td>\n <td>\n $S|0\\rangle = |0\\rangle$<br>\n $S|1\\rangle = i|1\\rangle$<br>\n $S|+\\rangle = |i\\rangle$<br>\n $S|-\\rangle = |-i\\rangle$<br>\n $S|i\\rangle = |-\\rangle$<br>\n $S|-i\\rangle = |+\\rangle$<br>\n </td>\n </tr>\n <tr>\n <td>$T$</td>\n <td>$\\begin{bmatrix} 1 & 0 \\\\\\ 0 & e^{i\\pi/4} \\end{bmatrix}$</td>\n <td>$|0\\rangle\\langle0| + e^{i\\pi/4}|1\\rangle$$\\langle1|$</td>\n <td>$T|\\psi\\rangle = \\alpha|0\\rangle + e^{i\\pi/4} \\beta |1\\rangle$</td>\n <td>\n $T|0\\rangle = |0\\rangle$<br>\n $T|1\\rangle = e^{i\\pi/4}|1\\rangle$\n </td>\n </tr>\n</table>\n\n> Notice that applying the $T$ gate twice is equivalent to applying the $S$ gate, and applying the $S$ gate twice is equivalent to applying the $Z$ gate:\n$$T^2 = S, S^2 = Z$$"
|
|
403
403
|
}
|
|
404
404
|
]
|
|
405
405
|
},
|
|
@@ -413,8 +413,8 @@ export default {
|
|
|
413
413
|
"asMarkdown": "**Input:** A qubit in an arbitrary state $|\\psi\\rangle = \\alpha|0\\rangle + \\beta|1\\rangle$.\n\n**Goal:** Use several phase shift gates to apply the transformation represented by the following matrix to the given qubit:\n\n$$\\begin{bmatrix} 1 & 0 \\\\\\ 0 & e^{3i\\pi/4} \\end{bmatrix}$$"
|
|
414
414
|
},
|
|
415
415
|
"sourceIds": [
|
|
416
|
-
"
|
|
417
|
-
"
|
|
416
|
+
"single_qubit_gates__three_quarters_pi_phase__Verification.qs",
|
|
417
|
+
"KatasLibrary.qs"
|
|
418
418
|
],
|
|
419
419
|
"placeholderCode": "namespace Kata {\n operation ThreeQuartersPiPhase (q : Qubit) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}\n",
|
|
420
420
|
"explainedSolution": {
|
|
@@ -440,8 +440,8 @@ export default {
|
|
|
440
440
|
"items": [
|
|
441
441
|
{
|
|
442
442
|
"type": "text-content",
|
|
443
|
-
"asHtml": "<p>The next few gates are parametrized: their exact behavior depends on a numeric parameter - an angle $\\theta$, given in radians
|
|
444
|
-
"asMarkdown": "\nThe next few gates are parametrized: their exact behavior depends on a numeric parameter - an angle $\\theta$, given in radians
|
|
443
|
+
"asHtml": "<p>The next few gates are parametrized: their exact behavior depends on a numeric parameter - an angle $\\theta$, given in radians.\nThese gates are the $X$ rotation gate $R_x(\\theta)$, $Y$ rotation gate $R_y(\\theta)$, $Z$ rotation gate $R_z(\\theta)$, and the arbitrary phase gate $R_1(\\theta)$.\nNote that for the first three gates the parameter $\\theta$ is multiplied by $\\frac{1}{2}$ within the gate's matrix.</p>\n<blockquote>\n<p>These gates are known as rotation gates, because they represent rotations around various axes on the Bloch sphere. The Bloch sphere is a way of representing the qubit states visually, mapping them onto the surface of a sphere.\nUnfortunately, this visualization isn't very useful beyond single-qubit states, which is why we have opted not to go into details in this kata.\nIf you are curious about it, you can learn more in <a href=\"https://en.wikipedia.org/wiki/Bloch_sphere\" target=\"_blank\">this Wikipedia article</a>.</p>\n</blockquote>\n<table>\n <tr>\n <th>Gate</th>\n <th>Matrix</th>\n <th>Applying to $|\\psi\\rangle = \\alpha|0\\rangle + \\beta|1\\rangle$</th>\n <th>Applying to basis states</th>\n </tr>\n <tr>\n <td>$R_x(\\theta)$</td>\n <td>\n $$\n \\begin{bmatrix} \\cos\\frac{\\theta}{2} & -i\\sin\\frac{\\theta}{2} \\\\\\ -i\\sin\\frac{\\theta}{2} & \\cos\\frac{\\theta}{2} \\end{bmatrix}\n $$\n </td>\n <td>$R_x(\\theta)|\\psi\\rangle = (\\alpha\\cos\\frac{\\theta}{2} - i\\beta\\sin\\frac{\\theta}{2})|0\\rangle + (\\beta\\cos\\frac{\\theta}{2} - i\\alpha\\sin\\frac{\\theta}{2})|1\\rangle$</td>\n <td>\n $R_x(\\theta)|0\\rangle = \\cos\\frac{\\theta}{2}|0\\rangle - i\\sin\\frac{\\theta}{2}|1\\rangle$<br>\n $R_x(\\theta)|1\\rangle = \\cos\\frac{\\theta}{2}|1\\rangle - i\\sin\\frac{\\theta}{2}|0\\rangle$\n </td>\n </tr>\n <tr>\n <td>$R_y(\\theta)$</td>\n <td>$\\begin{bmatrix} \\cos\\frac{\\theta}{2} & -\\sin\\frac{\\theta}{2} \\\\\\ \\sin\\frac{\\theta}{2} & \\cos\\frac{\\theta}{2} \\end{bmatrix}$</td>\n <td>$R_y(\\theta)|\\psi\\rangle = (\\alpha\\cos\\frac{\\theta}{2} - \\beta\\sin\\frac{\\theta}{2})|0\\rangle + (\\beta\\cos\\frac{\\theta}{2} + \\alpha\\sin\\frac{\\theta}{2})|1\\rangle$</td>\n <td>\n $R_y(\\theta)|0\\rangle = \\cos\\frac{\\theta}{2}|0\\rangle + \\sin\\frac{\\theta}{2}|1\\rangle$<br>\n $R_y(\\theta)|1\\rangle = \\cos\\frac{\\theta}{2}|1\\rangle - \\sin\\frac{\\theta}{2}|0\\rangle$\n </td>\n </tr>\n <tr>\n <td>$R_z(\\theta)$</td>\n <td>$\\begin{bmatrix} e^{-i\\theta/2} & 0 \\\\\\ 0 & e^{i\\theta/2} \\end{bmatrix}$</td>\n <td>$R_z(\\theta)|\\psi\\rangle = \\alpha e^{-i\\theta/2}|0\\rangle + \\beta e^{i\\theta/2}|1\\rangle$</td>\n <td>\n $R_z(\\theta)|0\\rangle = e^{-i\\theta/2}|0\\rangle$<br>\n $R_z(\\theta)|1\\rangle = e^{i\\theta/2}|1\\rangle$\n </td>\n </tr>\n <tr>\n <td>$R_1(\\theta)$</td>\n <td>$\\begin{bmatrix} 1 & 0 \\\\\\ 0 & e^{i\\theta} \\end{bmatrix}$</td>\n <td>$R_1(\\theta)|\\psi\\rangle = \\alpha|0\\rangle + \\beta e^{i\\theta}|1\\rangle$</td>\n <td>\n $R_1(\\theta)|0\\rangle = |0\\rangle$<br>\n $R_1(\\theta)|1\\rangle = e^{i\\theta}|1\\rangle$\n </td> \n </tr>\n</table>\n\n<p>You have already encountered some special cases of the $R_1$ gate:</p>\n<p>$$T = R_1(\\frac{\\pi}{4}), S = R_1(\\frac{\\pi}{2}), Z = R_1(\\pi)$$</p>\n<p>In addition, this gate is closely related to the $R_z$ gate: applying $R_1$ gate is equivalent to applying the $R_z$ gate, and then applying a global phase:</p>\n<p>$$R_1(\\theta) = e^{i\\theta/2}R_z(\\theta)$$</p>\n<p>In addition, the rotation gates are very closely related to their respective Pauli gates:</p>\n<p>$$X = iR_x(\\pi), Y = iR_y(\\pi), Z = iR_z(\\pi)$$</p>\n",
|
|
444
|
+
"asMarkdown": "\nThe next few gates are parametrized: their exact behavior depends on a numeric parameter - an angle $\\theta$, given in radians.\nThese gates are the $X$ rotation gate $R_x(\\theta)$, $Y$ rotation gate $R_y(\\theta)$, $Z$ rotation gate $R_z(\\theta)$, and the arbitrary phase gate $R_1(\\theta)$.\nNote that for the first three gates the parameter $\\theta$ is multiplied by $\\frac{1}{2}$ within the gate's matrix.\n\n> These gates are known as rotation gates, because they represent rotations around various axes on the Bloch sphere. The Bloch sphere is a way of representing the qubit states visually, mapping them onto the surface of a sphere.\n> Unfortunately, this visualization isn't very useful beyond single-qubit states, which is why we have opted not to go into details in this kata.\n> If you are curious about it, you can learn more in <a href=\"https://en.wikipedia.org/wiki/Bloch_sphere\" target=\"_blank\">this Wikipedia article</a>.\n\n<table>\n <tr>\n <th>Gate</th>\n <th>Matrix</th>\n <th>Applying to $|\\psi\\rangle = \\alpha|0\\rangle + \\beta|1\\rangle$</th>\n <th>Applying to basis states</th>\n </tr>\n <tr>\n <td>$R_x(\\theta)$</td>\n <td>\n $$\n \\begin{bmatrix} \\cos\\frac{\\theta}{2} & -i\\sin\\frac{\\theta}{2} \\\\\\ -i\\sin\\frac{\\theta}{2} & \\cos\\frac{\\theta}{2} \\end{bmatrix}\n $$\n </td>\n <td>$R_x(\\theta)|\\psi\\rangle = (\\alpha\\cos\\frac{\\theta}{2} - i\\beta\\sin\\frac{\\theta}{2})|0\\rangle + (\\beta\\cos\\frac{\\theta}{2} - i\\alpha\\sin\\frac{\\theta}{2})|1\\rangle$</td>\n <td>\n $R_x(\\theta)|0\\rangle = \\cos\\frac{\\theta}{2}|0\\rangle - i\\sin\\frac{\\theta}{2}|1\\rangle$<br>\n $R_x(\\theta)|1\\rangle = \\cos\\frac{\\theta}{2}|1\\rangle - i\\sin\\frac{\\theta}{2}|0\\rangle$\n </td>\n </tr>\n <tr>\n <td>$R_y(\\theta)$</td>\n <td>$\\begin{bmatrix} \\cos\\frac{\\theta}{2} & -\\sin\\frac{\\theta}{2} \\\\\\ \\sin\\frac{\\theta}{2} & \\cos\\frac{\\theta}{2} \\end{bmatrix}$</td>\n <td>$R_y(\\theta)|\\psi\\rangle = (\\alpha\\cos\\frac{\\theta}{2} - \\beta\\sin\\frac{\\theta}{2})|0\\rangle + (\\beta\\cos\\frac{\\theta}{2} + \\alpha\\sin\\frac{\\theta}{2})|1\\rangle$</td>\n <td>\n $R_y(\\theta)|0\\rangle = \\cos\\frac{\\theta}{2}|0\\rangle + \\sin\\frac{\\theta}{2}|1\\rangle$<br>\n $R_y(\\theta)|1\\rangle = \\cos\\frac{\\theta}{2}|1\\rangle - \\sin\\frac{\\theta}{2}|0\\rangle$\n </td>\n </tr>\n <tr>\n <td>$R_z(\\theta)$</td>\n <td>$\\begin{bmatrix} e^{-i\\theta/2} & 0 \\\\\\ 0 & e^{i\\theta/2} \\end{bmatrix}$</td>\n <td>$R_z(\\theta)|\\psi\\rangle = \\alpha e^{-i\\theta/2}|0\\rangle + \\beta e^{i\\theta/2}|1\\rangle$</td>\n <td>\n $R_z(\\theta)|0\\rangle = e^{-i\\theta/2}|0\\rangle$<br>\n $R_z(\\theta)|1\\rangle = e^{i\\theta/2}|1\\rangle$\n </td>\n </tr>\n <tr>\n <td>$R_1(\\theta)$</td>\n <td>$\\begin{bmatrix} 1 & 0 \\\\\\ 0 & e^{i\\theta} \\end{bmatrix}$</td>\n <td>$R_1(\\theta)|\\psi\\rangle = \\alpha|0\\rangle + \\beta e^{i\\theta}|1\\rangle$</td>\n <td>\n $R_1(\\theta)|0\\rangle = |0\\rangle$<br>\n $R_1(\\theta)|1\\rangle = e^{i\\theta}|1\\rangle$\n </td> \n </tr>\n</table>\n\nYou have already encountered some special cases of the $R_1$ gate:\n\n$$T = R_1(\\frac{\\pi}{4}), S = R_1(\\frac{\\pi}{2}), Z = R_1(\\pi)$$\n\nIn addition, this gate is closely related to the $R_z$ gate: applying $R_1$ gate is equivalent to applying the $R_z$ gate, and then applying a global phase:\n\n$$R_1(\\theta) = e^{i\\theta/2}R_z(\\theta)$$\n\nIn addition, the rotation gates are very closely related to their respective Pauli gates:\n\n$$X = iR_x(\\pi), Y = iR_y(\\pi), Z = iR_z(\\pi)$$"
|
|
445
445
|
}
|
|
446
446
|
]
|
|
447
447
|
},
|
|
@@ -455,8 +455,8 @@ export default {
|
|
|
455
455
|
"asMarkdown": "**Inputs:**\n\n1. Real numbers $\\alpha$ and $\\beta$ such that $\\alpha^2 + \\beta^2 = 1$.\n2. A qubit in state $|0\\rangle$.\n\n**Goal:** Use a rotation gate to transform the qubit into state $\\alpha|0\\rangle -i\\beta|1\\rangle$.\n\n> You will probably need functions from the <a href=\"https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.math\" target=\"_blank\">Math</a> namespace, specifically <a href=\"https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.math.arctan2\" target=\"_blank\">ArcTan2</a>.\n>\n> You can assign variables in Q# by using the `let` keyword: `let num = 3;` or `let result = Function(input);`"
|
|
456
456
|
},
|
|
457
457
|
"sourceIds": [
|
|
458
|
-
"
|
|
459
|
-
"
|
|
458
|
+
"single_qubit_gates__prepare_rotated_state__Verification.qs",
|
|
459
|
+
"KatasLibrary.qs"
|
|
460
460
|
],
|
|
461
461
|
"placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n operation PrepareRotatedState(alpha : Double, beta : Double, q : Qubit)\n : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}\n",
|
|
462
462
|
"explainedSolution": {
|
|
@@ -485,8 +485,8 @@ export default {
|
|
|
485
485
|
"asMarkdown": "**Inputs:**\n\n1. A non-negative real number $\\alpha$.\n2. A non-negative real number $\\beta = \\sqrt{1 - \\alpha^2}$.\n3. A real number $\\theta$.\n4. A qubit in state $|0\\rangle$.\n\n**Goal:** Transform the qubit into state $\\alpha|0\\rangle + e^{i\\theta}\\beta|1\\rangle$.\n\n> Since only the relative amplitudes and relative phase have any physical meaning, this allows us to prepare any single-qubit quantum state we want to."
|
|
486
486
|
},
|
|
487
487
|
"sourceIds": [
|
|
488
|
-
"
|
|
489
|
-
"
|
|
488
|
+
"single_qubit_gates__prepare_arbitrary_state__Verification.qs",
|
|
489
|
+
"KatasLibrary.qs"
|
|
490
490
|
],
|
|
491
491
|
"placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n operation PrepareArbitraryState(\n alpha : Double,beta : Double, theta : Double, q : Qubit)\n : Unit is Adj+Ctl {\n // Implement your solution here...\n\n }\n}\n",
|
|
492
492
|
"explainedSolution": {
|
|
@@ -513,7 +513,7 @@ export default {
|
|
|
513
513
|
{
|
|
514
514
|
"type": "text-content",
|
|
515
515
|
"asHtml": "<p>Congratulations! In this kata you learned the matrix and the ket-bra representation of quantum gates. Here are a few key concepts to keep in mind:</p>\n<ul>\n<li>Single-qubit gates act on individual qubits and are represented by $2 \\times 2$ unitary matrices.</li>\n<li>The effect of a gate applied to a qubit can be calculated by multiplying the corresponding matrix by the state vector of the qubit.</li>\n<li>Applying several quantum gates in sequence is equivalent to performing several matrix multiplications.</li>\n<li>Any square matrix can be represented as a linear combination of the outer products of vectors. The outer product is the matrix product of $|\\phi\\rangle$ and $\\langle\\psi|$, denoted as $|\\phi\\rangle\\langle\\psi|$.</li>\n<li>Pauli gates, identity and Hadamard gates, phase shift gates, and rotation gates are examples of single-qubit gates. All of them are available in Q#.</li>\n</ul>\n<p>Next, you will learn about multi-qubit systems in the “Multi-Qubit Systems” kata.</p>\n",
|
|
516
|
-
"asMarkdown": "\nCongratulations! In this kata you learned the matrix and the ket-bra representation of quantum gates. Here are a few key concepts to keep in mind:\n
|
|
516
|
+
"asMarkdown": "\nCongratulations! In this kata you learned the matrix and the ket-bra representation of quantum gates. Here are a few key concepts to keep in mind:\n\n- Single-qubit gates act on individual qubits and are represented by $2 \\times 2$ unitary matrices.\n- The effect of a gate applied to a qubit can be calculated by multiplying the corresponding matrix by the state vector of the qubit.\n- Applying several quantum gates in sequence is equivalent to performing several matrix multiplications.\n- Any square matrix can be represented as a linear combination of the outer products of vectors. The outer product is the matrix product of $|\\phi\\rangle$ and $\\langle\\psi|$, denoted as $|\\phi\\rangle\\langle\\psi|$.\n- Pauli gates, identity and Hadamard gates, phase shift gates, and rotation gates are examples of single-qubit gates. All of them are available in Q#.\n\nNext, you will learn about multi-qubit systems in the “Multi-Qubit Systems” kata."
|
|
517
517
|
}
|
|
518
518
|
]
|
|
519
519
|
}
|
|
@@ -542,8 +542,8 @@ export default {
|
|
|
542
542
|
"items": [
|
|
543
543
|
{
|
|
544
544
|
"type": "text-content",
|
|
545
|
-
"asHtml": "<p>In The Qubit kata we discussed the concept of a qubit - the basic building block of a quantum computer.\nA multi-qubit system is a collection of multiple qubits, treated as a single system.</p>\n<p>Let's start by examining a system of two classical bits. Each bit can be in two states: $0$ and $1$. Therefore, a system of two bits can be in four different states: $00$, $01$, $10$, and $11$. Generally, a system of $N$ classical bits can be in any of the $2^N$ states.</p>\n<p>A system of $N$ qubits can also be in any of the $2^N$ classical states, but, unlike the classical bits, it can also be in a <strong>superposition</strong> of all these states.</p>\n<p>Similarly to single-qubit systems, a state of an $N$-qubit system can be represented as a complex vector of size $2^N$:\n$$\\begin{bmatrix} x_0 \\\\ x_1 \\\\ \\vdots \\\\ x_{2^N-1}\\end{bmatrix}$$</p>\n<h2>Basis States</h2>\n<p>Similarly to single-qubit systems, multi-qubit systems have their own sets of basis states.\nThe computational basis for an $N$-qubit system is a set of $2^N$ vectors, in each of which with one element equals $1$, and the other elements equal $0$.</p>\n<p>For example, this is the <strong>computational basis</strong> for a two-qubit system:</p>\n<p>$$\\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ 0 \\end{bmatrix},\n\\begin{bmatrix} 0 \\\\ 1 \\\\ 0 \\\\ 0 \\end{bmatrix},\n\\begin{bmatrix} 0 \\\\ 0 \\\\ 1 \\\\ 0 \\end{bmatrix},\n\\begin{bmatrix} 0 \\\\ 0 \\\\ 0 \\\\ 1 \\end{bmatrix}$$</p>\n<p>It is easy to see that these vectors form an orthonormal basis. Note that each of these basis states can be represented as a tensor product of some combination of single-qubit basis states:</p>\n<table>\n <tr>\n <td>$\\begin{bmatrix} 1 \\\\\\ 0 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix} =\n\\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix} \\otimes \\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix}$</td>\n <td>$\\begin{bmatrix} 0 \\\\\\ 1 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix} =\n\\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix} \\otimes \\begin{bmatrix} 0 \\\\\\ 1 \\end{bmatrix}$</td>\n <td>$\\begin{bmatrix} 0 \\\\\\ 0 \\\\\\ 1 \\\\\\ 0 \\end{bmatrix} =\n\\begin{bmatrix} 0 \\\\\\ 1 \\end{bmatrix} \\otimes \\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix}$</td>\n <td>$\\begin{bmatrix} 0 \\\\\\ 0 \\\\\\ 0 \\\\\\ 1 \\end{bmatrix} =\n\\begin{bmatrix} 0 \\\\\\ 1 \\end{bmatrix} \\otimes \\begin{bmatrix} 0 \\\\\\ 1 \\end{bmatrix}$</td>\n </tr>\n</table>\n\n<p>Any two-qubit system can be expressed as some linear combination of those tensor products of single-qubit basis states.</p>\n<p>Similar logic applies to systems of more than two qubits. In general case,</p>\n<p>$$\n\\begin{bmatrix} x_0 \\\\ x_1 \\\\ \\vdots \\\\ x_{2^N-1} \\end{bmatrix} =\nx_0 \\begin{bmatrix} 1 \\\\ 0 \\\\ \\vdots \\\\ 0 \\end{bmatrix} +\nx_1 \\begin{bmatrix} 0 \\\\ 1 \\\\ \\vdots \\\\ 0 \\end{bmatrix} + \\dotsb +\nx_{2^N-1} \\begin{bmatrix} 0 \\\\ 0 \\\\ \\vdots \\\\ 1 \\end{bmatrix}\n$$</p>\n<p>The coefficients of the basis vectors define how "close" is the system state to the corresponding basis vector.</p>\n<blockquote>\n<p>Just like with single-qubit systems, there exist other orthonormal bases states for multi-qubit systems. An example for a two-qubit system is the <strong>Bell basis</strong>:</p>\n<p>$$\\frac{1}{\\sqrt{2}}\\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ 1 \\end{bmatrix}
|
|
546
|
-
"asMarkdown": "\nIn The Qubit kata we discussed the concept of a qubit - the basic building block of a quantum computer.\nA multi-qubit system is a collection of multiple qubits, treated as a single system.\n\nLet's start by examining a system of two classical bits. Each bit can be in two states: $0$ and $1$. Therefore, a system of two bits can be in four different states: $00$, $01$, $10$, and $11$. Generally, a system of $N$ classical bits can be in any of the $2^N$ states.\n\nA system of $N$ qubits can also be in any of the $2^N$ classical states, but, unlike the classical bits, it can also be in a **superposition** of all these states.\n\nSimilarly to single-qubit systems, a state of an $N$-qubit system can be represented as a complex vector of size $2^N$:\n$$\\begin{bmatrix} x_0 \\\\\\ x_1 \\\\\\ \\vdots \\\\\\ x_{2^N-1}\\end{bmatrix}$$\n\n## Basis States\n\nSimilarly to single-qubit systems, multi-qubit systems have their own sets of basis states.\nThe computational basis for an $N$-qubit system is a set of $2^N$ vectors, in each of which with one element equals $1$, and the other elements equal $0$.\n\nFor example, this is the **computational basis** for a two-qubit system:\n\n$$\\begin{bmatrix} 1 \\\\\\ 0 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix},\n\\begin{bmatrix} 0 \\\\\\ 1 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix},\n\\begin{bmatrix} 0 \\\\\\ 0 \\\\\\ 1 \\\\\\ 0 \\end{bmatrix},\n\\begin{bmatrix} 0 \\\\\\ 0 \\\\\\ 0 \\\\\\ 1 \\end{bmatrix}$$\n\nIt is easy to see that these vectors form an orthonormal basis. Note that each of these basis states can be represented as a tensor product of some combination of single-qubit basis states:\n\n<table>\n <tr>\n <td>$\\begin{bmatrix} 1 \\\\\\ 0 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix} =\n\\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix} \\otimes \\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix}$</td>\n <td>$\\begin{bmatrix} 0 \\\\\\ 1 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix} =\n\\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix} \\otimes \\begin{bmatrix} 0 \\\\\\ 1 \\end{bmatrix}$</td>\n <td>$\\begin{bmatrix} 0 \\\\\\ 0 \\\\\\ 1 \\\\\\ 0 \\end{bmatrix} =\n\\begin{bmatrix} 0 \\\\\\ 1 \\end{bmatrix} \\otimes \\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix}$</td>\n <td>$\\begin{bmatrix} 0 \\\\\\ 0 \\\\\\ 0 \\\\\\ 1 \\end{bmatrix} =\n\\begin{bmatrix} 0 \\\\\\ 1 \\end{bmatrix} \\otimes \\begin{bmatrix} 0 \\\\\\ 1 \\end{bmatrix}$</td>\n </tr>\n</table>\n\nAny two-qubit system can be expressed as some linear combination of those tensor products of single-qubit basis states.\n\nSimilar logic applies to systems of more than two qubits. In general case,\n\n$$\n\\begin{bmatrix} x_0 \\\\\\ x_1 \\\\\\ \\vdots \\\\\\ x_{2^N-1} \\end{bmatrix} =\nx_0 \\begin{bmatrix} 1 \\\\\\ 0 \\\\\\ \\vdots \\\\\\ 0 \\end{bmatrix} +\nx_1 \\begin{bmatrix} 0 \\\\\\ 1 \\\\\\ \\vdots \\\\\\ 0 \\end{bmatrix} + \\dotsb +\nx_{2^N-1} \\begin{bmatrix} 0 \\\\\\ 0 \\\\\\ \\vdots \\\\\\ 1 \\end{bmatrix}\n$$\n\nThe coefficients of the basis vectors define how \"close\" is the system state to the corresponding basis vector.\n\n> Just like with single-qubit systems, there exist other orthonormal bases states for multi-qubit systems. An example for a two-qubit system is the **Bell basis**:\n>\n> $$\\frac{1}{\\sqrt{2}}\\begin{bmatrix} 1 \\\\\\ 0 \\\\\\ 0 \\\\\\ 1 \\end{bmatrix}
|
|
545
|
+
"asHtml": "<p>In The Qubit kata we discussed the concept of a qubit - the basic building block of a quantum computer.\nA multi-qubit system is a collection of multiple qubits, treated as a single system.</p>\n<p>Let's start by examining a system of two classical bits. Each bit can be in two states: $0$ and $1$. Therefore, a system of two bits can be in four different states: $00$, $01$, $10$, and $11$. Generally, a system of $N$ classical bits can be in any of the $2^N$ states.</p>\n<p>A system of $N$ qubits can also be in any of the $2^N$ classical states, but, unlike the classical bits, it can also be in a <strong>superposition</strong> of all these states.</p>\n<p>Similarly to single-qubit systems, a state of an $N$-qubit system can be represented as a complex vector of size $2^N$:\n$$\\begin{bmatrix} x_0 \\\\ x_1 \\\\ \\vdots \\\\ x_{2^N-1}\\end{bmatrix}$$</p>\n<h2>Basis States</h2>\n<p>Similarly to single-qubit systems, multi-qubit systems have their own sets of basis states.\nThe computational basis for an $N$-qubit system is a set of $2^N$ vectors, in each of which with one element equals $1$, and the other elements equal $0$.</p>\n<p>For example, this is the <strong>computational basis</strong> for a two-qubit system:</p>\n<p>$$\\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ 0 \\end{bmatrix},\n\\begin{bmatrix} 0 \\\\ 1 \\\\ 0 \\\\ 0 \\end{bmatrix},\n\\begin{bmatrix} 0 \\\\ 0 \\\\ 1 \\\\ 0 \\end{bmatrix},\n\\begin{bmatrix} 0 \\\\ 0 \\\\ 0 \\\\ 1 \\end{bmatrix}$$</p>\n<p>It is easy to see that these vectors form an orthonormal basis. Note that each of these basis states can be represented as a tensor product of some combination of single-qubit basis states:</p>\n<table>\n <tr>\n <td>$\\begin{bmatrix} 1 \\\\\\ 0 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix} =\n\\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix} \\otimes \\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix}$</td>\n <td>$\\begin{bmatrix} 0 \\\\\\ 1 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix} =\n\\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix} \\otimes \\begin{bmatrix} 0 \\\\\\ 1 \\end{bmatrix}$</td>\n <td>$\\begin{bmatrix} 0 \\\\\\ 0 \\\\\\ 1 \\\\\\ 0 \\end{bmatrix} =\n\\begin{bmatrix} 0 \\\\\\ 1 \\end{bmatrix} \\otimes \\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix}$</td>\n <td>$\\begin{bmatrix} 0 \\\\\\ 0 \\\\\\ 0 \\\\\\ 1 \\end{bmatrix} =\n\\begin{bmatrix} 0 \\\\\\ 1 \\end{bmatrix} \\otimes \\begin{bmatrix} 0 \\\\\\ 1 \\end{bmatrix}$</td>\n </tr>\n</table>\n\n<p>Any two-qubit system can be expressed as some linear combination of those tensor products of single-qubit basis states.</p>\n<p>Similar logic applies to systems of more than two qubits. In general case,</p>\n<p>$$\n\\begin{bmatrix} x_0 \\\\ x_1 \\\\ \\vdots \\\\ x_{2^N-1} \\end{bmatrix} =\nx_0 \\begin{bmatrix} 1 \\\\ 0 \\\\ \\vdots \\\\ 0 \\end{bmatrix} +\nx_1 \\begin{bmatrix} 0 \\\\ 1 \\\\ \\vdots \\\\ 0 \\end{bmatrix} + \\dotsb +\nx_{2^N-1} \\begin{bmatrix} 0 \\\\ 0 \\\\ \\vdots \\\\ 1 \\end{bmatrix}\n$$</p>\n<p>The coefficients of the basis vectors define how "close" is the system state to the corresponding basis vector.</p>\n<blockquote>\n<p>Just like with single-qubit systems, there exist other orthonormal bases states for multi-qubit systems. An example for a two-qubit system is the <strong>Bell basis</strong>:</p>\n<p>$$\\frac{1}{\\sqrt{2}}\\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ 1 \\end{bmatrix},\n\\frac{1}{\\sqrt{2}}\\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ -1 \\end{bmatrix},\n\\frac{1}{\\sqrt{2}}\\begin{bmatrix} 0 \\\\ 1 \\\\ 1 \\\\ 0 \\end{bmatrix},\n\\frac{1}{\\sqrt{2}}\\begin{bmatrix} 0 \\\\ 1 \\\\ -1 \\\\ 0 \\end{bmatrix}$$</p>\n<p>You can check that these vectors are normalized, and orthogonal to each other, and that any two-qubit state can be expressed as a linear combination of these vectors. The vectors of Bell basis, however, can not be represented as tensor products of single-qubit basis states.</p>\n</blockquote>\n",
|
|
546
|
+
"asMarkdown": "\nIn The Qubit kata we discussed the concept of a qubit - the basic building block of a quantum computer.\nA multi-qubit system is a collection of multiple qubits, treated as a single system.\n\nLet's start by examining a system of two classical bits. Each bit can be in two states: $0$ and $1$. Therefore, a system of two bits can be in four different states: $00$, $01$, $10$, and $11$. Generally, a system of $N$ classical bits can be in any of the $2^N$ states.\n\nA system of $N$ qubits can also be in any of the $2^N$ classical states, but, unlike the classical bits, it can also be in a **superposition** of all these states.\n\nSimilarly to single-qubit systems, a state of an $N$-qubit system can be represented as a complex vector of size $2^N$:\n$$\\begin{bmatrix} x_0 \\\\\\ x_1 \\\\\\ \\vdots \\\\\\ x_{2^N-1}\\end{bmatrix}$$\n\n## Basis States\n\nSimilarly to single-qubit systems, multi-qubit systems have their own sets of basis states.\nThe computational basis for an $N$-qubit system is a set of $2^N$ vectors, in each of which with one element equals $1$, and the other elements equal $0$.\n\nFor example, this is the **computational basis** for a two-qubit system:\n\n$$\\begin{bmatrix} 1 \\\\\\ 0 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix},\n\\begin{bmatrix} 0 \\\\\\ 1 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix},\n\\begin{bmatrix} 0 \\\\\\ 0 \\\\\\ 1 \\\\\\ 0 \\end{bmatrix},\n\\begin{bmatrix} 0 \\\\\\ 0 \\\\\\ 0 \\\\\\ 1 \\end{bmatrix}$$\n\nIt is easy to see that these vectors form an orthonormal basis. Note that each of these basis states can be represented as a tensor product of some combination of single-qubit basis states:\n\n<table>\n <tr>\n <td>$\\begin{bmatrix} 1 \\\\\\ 0 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix} =\n\\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix} \\otimes \\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix}$</td>\n <td>$\\begin{bmatrix} 0 \\\\\\ 1 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix} =\n\\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix} \\otimes \\begin{bmatrix} 0 \\\\\\ 1 \\end{bmatrix}$</td>\n <td>$\\begin{bmatrix} 0 \\\\\\ 0 \\\\\\ 1 \\\\\\ 0 \\end{bmatrix} =\n\\begin{bmatrix} 0 \\\\\\ 1 \\end{bmatrix} \\otimes \\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix}$</td>\n <td>$\\begin{bmatrix} 0 \\\\\\ 0 \\\\\\ 0 \\\\\\ 1 \\end{bmatrix} =\n\\begin{bmatrix} 0 \\\\\\ 1 \\end{bmatrix} \\otimes \\begin{bmatrix} 0 \\\\\\ 1 \\end{bmatrix}$</td>\n </tr>\n</table>\n\nAny two-qubit system can be expressed as some linear combination of those tensor products of single-qubit basis states.\n\nSimilar logic applies to systems of more than two qubits. In general case,\n\n$$\n\\begin{bmatrix} x_0 \\\\\\ x_1 \\\\\\ \\vdots \\\\\\ x_{2^N-1} \\end{bmatrix} =\nx_0 \\begin{bmatrix} 1 \\\\\\ 0 \\\\\\ \\vdots \\\\\\ 0 \\end{bmatrix} +\nx_1 \\begin{bmatrix} 0 \\\\\\ 1 \\\\\\ \\vdots \\\\\\ 0 \\end{bmatrix} + \\dotsb +\nx_{2^N-1} \\begin{bmatrix} 0 \\\\\\ 0 \\\\\\ \\vdots \\\\\\ 1 \\end{bmatrix}\n$$\n\nThe coefficients of the basis vectors define how \"close\" is the system state to the corresponding basis vector.\n\n> Just like with single-qubit systems, there exist other orthonormal bases states for multi-qubit systems. An example for a two-qubit system is the **Bell basis**:\n>\n> $$\\frac{1}{\\sqrt{2}}\\begin{bmatrix} 1 \\\\\\ 0 \\\\\\ 0 \\\\\\ 1 \\end{bmatrix},\n\\frac{1}{\\sqrt{2}}\\begin{bmatrix} 1 \\\\\\ 0 \\\\\\ 0 \\\\\\ -1 \\end{bmatrix},\n\\frac{1}{\\sqrt{2}}\\begin{bmatrix} 0 \\\\\\ 1 \\\\\\ 1 \\\\\\ 0 \\end{bmatrix},\n\\frac{1}{\\sqrt{2}}\\begin{bmatrix} 0 \\\\\\ 1 \\\\\\ -1 \\\\\\ 0 \\end{bmatrix}$$\n>\n> You can check that these vectors are normalized, and orthogonal to each other, and that any two-qubit state can be expressed as a linear combination of these vectors. The vectors of Bell basis, however, can not be represented as tensor products of single-qubit basis states."
|
|
547
547
|
}
|
|
548
548
|
]
|
|
549
549
|
},
|
|
@@ -554,8 +554,8 @@ export default {
|
|
|
554
554
|
"items": [
|
|
555
555
|
{
|
|
556
556
|
"type": "text-content",
|
|
557
|
-
"asHtml": "<p>Sometimes the global state of a multi-qubit system can be separated into the states of individual qubits or subsystems. To do this, you would express the vector state of the global system as a tensor product of the vectors representing each individual qubit/subsystem. Here is an example of a two-qubit state:</p>\n<p>$$\n\\begin{bmatrix} \\frac{1}{\\sqrt{2}} \\\\ 0 \\\\ \\frac{1}{\\sqrt{2}} \\\\ 0 \\end{bmatrix} =\n\\begin{bmatrix} \\frac{1}{\\sqrt{2}} \\\\ \\frac{1}{\\sqrt{2}} \\end{bmatrix} \\otimes \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix}\n$$</p>\n<p>You can see that the first qubit is in state $\\frac{1}{\\sqrt{2}}\\big(|0\\rangle + |1\\rangle\\big)$ and the second qubit is in state $|0\\rangle$. The multi-qubit states that allow such representation are known as <strong>separable states</strong>, or product states, because you can separate the global state into the tensor product of individual subsystems.</p>\n<h2>🔎 Analyze</h2>\n<p>Show that the state is separable:\n$$\n\\frac{1}{2} \\begin{bmatrix} 1 \\\\ i \\\\ -i \\\\ 1 \\end{bmatrix} =\n\\begin{bmatrix} ? \\\\ ? \\end{bmatrix} \\otimes \\begin{bmatrix} ? \\\\ ? \\end{bmatrix}\n$$</p>\n<details>\n<summary><b>Solution</b></summary>\nTo separate the state into a tensor product of two single-qubit states, we need to represent it in the following way:\n\n<p>$$\n\\begin{bmatrix} \\alpha \\gamma \\\\ \\alpha \\delta \\\\ \\beta \\gamma \\\\ \\beta \\delta \\end{bmatrix}
|
|
558
|
-
"asMarkdown": "\nSometimes the global state of a multi-qubit system can be separated into the states of individual qubits or subsystems. To do this, you would express the vector state of the global system as a tensor product of the vectors representing each individual qubit/subsystem. Here is an example of a two-qubit state:\n\n$$\n\\begin{bmatrix} \\frac{1}{\\sqrt{2}} \\\\\\ 0 \\\\\\ \\frac{1}{\\sqrt{2}} \\\\\\ 0 \\end{bmatrix} =\n\\begin{bmatrix} \\frac{1}{\\sqrt{2}} \\\\\\ \\frac{1}{\\sqrt{2}} \\end{bmatrix} \\otimes \\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix}\n$$\n\nYou can see that the first qubit is in state $\\frac{1}{\\sqrt{2}}\\big(|0\\rangle + |1\\rangle\\big)$ and the second qubit is in state $|0\\rangle$. The multi-qubit states that allow such representation are known as **separable states**, or product states, because you can separate the global state into the tensor product of individual subsystems.\n\n
|
|
557
|
+
"asHtml": "<p>Sometimes the global state of a multi-qubit system can be separated into the states of individual qubits or subsystems. To do this, you would express the vector state of the global system as a tensor product of the vectors representing each individual qubit/subsystem. Here is an example of a two-qubit state:</p>\n<p>$$\n\\begin{bmatrix} \\frac{1}{\\sqrt{2}} \\\\ 0 \\\\ \\frac{1}{\\sqrt{2}} \\\\ 0 \\end{bmatrix} =\n\\begin{bmatrix} \\frac{1}{\\sqrt{2}} \\\\ \\frac{1}{\\sqrt{2}} \\end{bmatrix} \\otimes \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix}\n$$</p>\n<p>You can see that the first qubit is in state $\\frac{1}{\\sqrt{2}}\\big(|0\\rangle + |1\\rangle\\big)$ and the second qubit is in state $|0\\rangle$. The multi-qubit states that allow such representation are known as <strong>separable states</strong>, or product states, because you can separate the global state into the tensor product of individual subsystems.</p>\n<h2>🔎 Analyze</h2>\n<p>Show that the state is separable:\n$$\n\\frac{1}{2} \\begin{bmatrix} 1 \\\\ i \\\\ -i \\\\ 1 \\end{bmatrix} =\n\\begin{bmatrix} ? \\\\ ? \\end{bmatrix} \\otimes \\begin{bmatrix} ? \\\\ ? \\end{bmatrix}\n$$</p>\n<details>\n<summary><b>Solution</b></summary>\nTo separate the state into a tensor product of two single-qubit states, we need to represent it in the following way:\n\n<p>$$\n\\begin{bmatrix} \\alpha \\gamma \\\\ \\alpha \\delta \\\\ \\beta \\gamma \\\\ \\beta \\delta \\end{bmatrix} =\n\\begin{bmatrix} \\alpha \\\\ \\beta \\end{bmatrix} \\otimes \\begin{bmatrix} \\gamma \\\\ \\delta \\end{bmatrix}\n$$</p>\n<p>This brings us to a system of equations:</p>\n<p>$$\n\\begin{cases}\n\\alpha\\gamma = \\frac{1}{2} \\\\ \\alpha\\delta = \\frac{i}{2} \\\\ \\beta \\gamma = \\frac{-i}{2} \\\\ \\beta \\delta = \\frac{1}{2}\n\\end{cases}\n$$</p>\n<p>Solving this system of equations gives us the answer:</p>\n<p>$$\\alpha = \\frac{1}{\\sqrt2}, \\beta = \\frac{-i}{\\sqrt2}, \\gamma = \\frac{1}{\\sqrt2}, \\delta = \\frac{i}{\\sqrt2}$$</p>\n<p>$$\n\\frac{1}{2} \\begin{bmatrix} 1 \\\\ i \\\\ -i \\\\ 1 \\end{bmatrix} =\n\\frac{1}{\\sqrt2} \\begin{bmatrix} 1 \\\\ -i \\end{bmatrix} \\otimes \\frac{1}{\\sqrt2} \\begin{bmatrix} 1 \\\\ i \\end{bmatrix}\n$$</p>\n<p>Note that finding such representation is not always possible, as you will see in the next exercise.</p>\n</details>\n\n<h2>🔎 Analyze</h2>\n<p>Is this state separable?</p>\n<p>$$\\frac{1}{\\sqrt{2}}\\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ 1 \\end{bmatrix}$$</p>\n<details>\n<summary><b>Solution</b></summary>\nLet's assume that this state is separable and write down the system of equations to determine the coefficients of individual qubit states in the tensor product, similar to what we did in the previous exercise:\n\n<p>$$\n\\begin{cases}\n\\alpha\\gamma = \\frac{1}{\\sqrt2} \\\\ \\alpha\\delta = 0 \\\\ \\beta \\gamma = 0 \\\\ \\beta \\delta = \\frac{1}{\\sqrt2}\n\\end{cases}\n$$</p>\n<p>Now let's multiply the first and the last equations, and the second and the third equations:</p>\n<p>$$\n\\begin{cases}\n\\alpha\\beta\\gamma\\delta = \\frac{1}{2} \\\\ \\alpha\\beta\\gamma\\delta = 0\n\\end{cases}\n$$</p>\n<p>We can see that this system of equations doesn't have a solution, which means that this state is <b>not separable</b>.</p>\n</details>",
|
|
558
|
+
"asMarkdown": "\nSometimes the global state of a multi-qubit system can be separated into the states of individual qubits or subsystems. To do this, you would express the vector state of the global system as a tensor product of the vectors representing each individual qubit/subsystem. Here is an example of a two-qubit state:\n\n$$\n\\begin{bmatrix} \\frac{1}{\\sqrt{2}} \\\\\\ 0 \\\\\\ \\frac{1}{\\sqrt{2}} \\\\\\ 0 \\end{bmatrix} =\n\\begin{bmatrix} \\frac{1}{\\sqrt{2}} \\\\\\ \\frac{1}{\\sqrt{2}} \\end{bmatrix} \\otimes \\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix}\n$$\n\nYou can see that the first qubit is in state $\\frac{1}{\\sqrt{2}}\\big(|0\\rangle + |1\\rangle\\big)$ and the second qubit is in state $|0\\rangle$. The multi-qubit states that allow such representation are known as **separable states**, or product states, because you can separate the global state into the tensor product of individual subsystems.\n\n## 🔎 Analyze\n\nShow that the state is separable:\n$$\n\\frac{1}{2} \\begin{bmatrix} 1 \\\\\\ i \\\\\\ -i \\\\\\ 1 \\end{bmatrix} =\n\\begin{bmatrix} ? \\\\\\ ? \\end{bmatrix} \\otimes \\begin{bmatrix} ? \\\\\\ ? \\end{bmatrix}\n$$\n\n<details>\n<summary><b>Solution</b></summary>\nTo separate the state into a tensor product of two single-qubit states, we need to represent it in the following way:\n\n$$\n\\begin{bmatrix} \\alpha \\gamma \\\\\\ \\alpha \\delta \\\\\\ \\beta \\gamma \\\\\\ \\beta \\delta \\end{bmatrix} =\n\\begin{bmatrix} \\alpha \\\\\\ \\beta \\end{bmatrix} \\otimes \\begin{bmatrix} \\gamma \\\\\\ \\delta \\end{bmatrix}\n$$\n\nThis brings us to a system of equations:\n\n$$\n\\begin{cases}\n\\alpha\\gamma = \\frac{1}{2} \\\\\\ \\alpha\\delta = \\frac{i}{2} \\\\\\ \\beta \\gamma = \\frac{-i}{2} \\\\\\ \\beta \\delta = \\frac{1}{2}\n\\end{cases}\n$$\n\nSolving this system of equations gives us the answer:\n\n$$\\alpha = \\frac{1}{\\sqrt2}, \\beta = \\frac{-i}{\\sqrt2}, \\gamma = \\frac{1}{\\sqrt2}, \\delta = \\frac{i}{\\sqrt2}$$\n\n$$\n\\frac{1}{2} \\begin{bmatrix} 1 \\\\\\ i \\\\\\ -i \\\\\\ 1 \\end{bmatrix} =\n\\frac{1}{\\sqrt2} \\begin{bmatrix} 1 \\\\\\ -i \\end{bmatrix} \\otimes \\frac{1}{\\sqrt2} \\begin{bmatrix} 1 \\\\\\ i \\end{bmatrix}\n$$\n\nNote that finding such representation is not always possible, as you will see in the next exercise.\n</details>\n\n## 🔎 Analyze\n\nIs this state separable?\n\n$$\\frac{1}{\\sqrt{2}}\\begin{bmatrix} 1 \\\\\\ 0 \\\\\\ 0 \\\\\\ 1 \\end{bmatrix}$$\n\n<details>\n<summary><b>Solution</b></summary>\nLet's assume that this state is separable and write down the system of equations to determine the coefficients of individual qubit states in the tensor product, similar to what we did in the previous exercise:\n\n$$\n\\begin{cases}\n\\alpha\\gamma = \\frac{1}{\\sqrt2} \\\\\\ \\alpha\\delta = 0 \\\\\\ \\beta \\gamma = 0 \\\\\\ \\beta \\delta = \\frac{1}{\\sqrt2}\n\\end{cases}\n$$\n\nNow let's multiply the first and the last equations, and the second and the third equations:\n\n$$\n\\begin{cases}\n\\alpha\\beta\\gamma\\delta = \\frac{1}{2} \\\\\\ \\alpha\\beta\\gamma\\delta = 0\n\\end{cases}\n$$\n\nWe can see that this system of equations doesn't have a solution, which means that this state is <b>not separable</b>.\n</details>"
|
|
559
559
|
}
|
|
560
560
|
]
|
|
561
561
|
},
|
|
@@ -579,7 +579,7 @@ export default {
|
|
|
579
579
|
{
|
|
580
580
|
"type": "text-content",
|
|
581
581
|
"asHtml": "<p>Just like with single qubits, Dirac notation provides a useful shorthand for writing down states of multi-qubit systems.</p>\n<p>As we've seen earlier, multi-qubit systems have their own canonical bases, and the basis states can be represented as tensor products of single-qubit basis states. Any multi-qubit system can be represented as a linear combination of these basis states:</p>\n<p>$$\n\\begin{bmatrix} x_0 \\\\ x_1 \\\\ x_2 \\\\ x_3 \\end{bmatrix} =\nx_0\\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ 0 \\end{bmatrix} +\nx_1\\begin{bmatrix} 0 \\\\ 1 \\\\ 0 \\\\ 0 \\end{bmatrix} +\nx_2\\begin{bmatrix} 0 \\\\ 0 \\\\ 1 \\\\ 0 \\end{bmatrix} +\nx_3\\begin{bmatrix} 0 \\\\ 0 \\\\ 0 \\\\ 1 \\end{bmatrix} =\nx_0|0\\rangle \\otimes |0\\rangle +\nx_1|0\\rangle \\otimes |1\\rangle +\nx_2|1\\rangle \\otimes |0\\rangle +\nx_3|1\\rangle \\otimes |1\\rangle\n$$</p>\n<p>To simplify this, tensor products of basis states have their own notation:</p>\n<p>$$|0\\rangle \\otimes |0\\rangle = |00\\rangle$$\n$$|0\\rangle \\otimes |1\\rangle = |01\\rangle$$\n$$|1\\rangle \\otimes |0\\rangle = |10\\rangle$$\n$$|1\\rangle \\otimes |1\\rangle = |11\\rangle$$</p>\n<p>$$|0\\rangle \\otimes |0\\rangle \\otimes |0\\rangle = |000\\rangle$$</p>\n<p>And so on.</p>\n<p>Or, more generally:</p>\n<p>$$|i_0\\rangle \\otimes |i_1\\rangle \\otimes \\dotsb \\otimes |i_n\\rangle = |i_0i_1...i_n\\rangle$$</p>\n<p>Using this notation simplifies our example:</p>\n<p>$$\n\\begin{bmatrix} x_0 \\\\ x_1 \\\\ x_2 \\\\ x_3 \\end{bmatrix} =\nx_0|00\\rangle + x_1|01\\rangle + x_2|10\\rangle + x_3|11\\rangle\n$$</p>\n<p>Just like with single qubits, we can put arbitrary symbols within the kets the same way variables are used in algebra.\nWhether a ket represents a single qubit or an entire system depends on the context.\nSome ket symbols have a commonly accepted usage, such as the symbols for the Bell basis:</p>\n<p>$$|\\phi^+\\rangle = \\frac{1}{\\sqrt{2}}\\big(|00\\rangle + |11\\rangle\\big)$$\n$$|\\phi^-\\rangle = \\frac{1}{\\sqrt{2}}\\big(|00\\rangle - |11\\rangle\\big)$$\n$$|\\psi^+\\rangle = \\frac{1}{\\sqrt{2}}\\big(|01\\rangle + |10\\rangle\\big)$$\n$$|\\psi^-\\rangle = \\frac{1}{\\sqrt{2}}\\big(|01\\rangle - |10\\rangle\\big)$$</p>\n<blockquote>\n<h2>Endianness</h2>\n<p>In classical computing, endianness refers to the order of bits (or bytes) when representing numbers in binary. You're probably familiar with the typical way of writing numbers in binary: $0 = 0_2$, $1 = 1_2$, $2 = 10_2$, $3 = 11_2$, $4 = 100_2$, $5 = 101_2$, $6 = 110_2$, etc. This is known as <strong>big-endian format</strong>. In big-endian format, the <em>most significant</em> bits come first. For example: $110_2 = 1 \\cdot 4 + 1 \\cdot 2 + 0 \\cdot 1 = 4 + 2 = 6$.</p>\n<p>There is an alternate way of writing binary numbers - <strong>little-endian format</strong>. In little-endian format, the <em>least significant</em> bits come first. For example, $2$ would be written as $01$, $4$ as $001$, and $6$ as $011$. To put it another way, in little endian format, the number is written backwards compared to the big-endian format.</p>\n<p>In Dirac notation for multi-qubit systems, it's common to see integer numbers within the kets instead of bit sequences. What those numbers mean depends on the context - whether the notation used is big-endian or little-endian.</p>\n<p>Examples with a 3 qubit system:</p>\n<table>\n <tr>\n <th>Integer Ket</th>\n <td>$|0\\rangle$</td>\n <td>$|1\\rangle$</td>\n <td>$|2\\rangle$</td>\n <td>$|3\\rangle$</td>\n <td>$|4\\rangle$</td>\n <td>$|5\\rangle$</td>\n <td>$|6\\rangle$</td>\n <td>$|7\\rangle$</td>\n </tr>\n <tr>\n <th>Big-endian</th>\n <td>$|000\\rangle$</td>\n <td>$|001\\rangle$</td>\n <td>$|010\\rangle$</td>\n <td>$|011\\rangle$</td>\n <td>$|100\\rangle$</td>\n <td>$|101\\rangle$</td>\n <td>$|110\\rangle$</td>\n <td>$|111\\rangle$</td>\n </tr>\n <tr>\n <th>Little-endian</th>\n <td>$|000\\rangle$</td>\n <td>$|100\\rangle$</td>\n <td>$|010\\rangle$</td>\n <td>$|110\\rangle$</td>\n <td>$|001\\rangle$</td>\n <td>$|101\\rangle$</td>\n <td>$|011\\rangle$</td>\n <td>$|111\\rangle$</td>\n </tr>\n</table>\n\n<p>Multi-qubit quantum systems that store superpositions of numbers are often referred to as <strong>quantum registers</strong>.</p>\n</blockquote>\n",
|
|
582
|
-
"asMarkdown": "\nJust like with single qubits, Dirac notation provides a useful shorthand for writing down states of multi-qubit systems.\n\nAs we've seen earlier, multi-qubit systems have their own canonical bases, and the basis states can be represented as tensor products of single-qubit basis states. Any multi-qubit system can be represented as a linear combination of these basis states:\n\n$$\n\\begin{bmatrix} x_0 \\\\\\ x_1 \\\\\\ x_2 \\\\\\ x_3 \\end{bmatrix} =\nx_0\\begin{bmatrix} 1 \\\\\\ 0 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix} +\nx_1\\begin{bmatrix} 0 \\\\\\ 1 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix} +\nx_2\\begin{bmatrix} 0 \\\\\\ 0 \\\\\\ 1 \\\\\\ 0 \\end{bmatrix} +\nx_3\\begin{bmatrix} 0 \\\\\\ 0 \\\\\\ 0 \\\\\\ 1 \\end{bmatrix} =\nx_0|0\\rangle \\otimes |0\\rangle +\nx_1|0\\rangle \\otimes |1\\rangle +\nx_2|1\\rangle \\otimes |0\\rangle +\nx_3|1\\rangle \\otimes |1\\rangle\n$$\n\nTo simplify this, tensor products of basis states have their own notation:\n\n$$|0\\rangle \\otimes |0\\rangle = |00\\rangle$$\n$$|0\\rangle \\otimes |1\\rangle = |01\\rangle$$\n$$|1\\rangle \\otimes |0\\rangle = |10\\rangle$$\n$$|1\\rangle \\otimes |1\\rangle = |11\\rangle$$\n\n$$|0\\rangle \\otimes |0\\rangle \\otimes |0\\rangle = |000\\rangle$$\n\nAnd so on.\n\nOr, more generally:\n\n$$|i_0\\rangle \\otimes |i_1\\rangle \\otimes \\dotsb \\otimes |i_n\\rangle = |i_0i_1...i_n\\rangle$$\n\nUsing this notation simplifies our example:\n\n$$\n\\begin{bmatrix} x_0 \\\\\\ x_1 \\\\\\ x_2 \\\\\\ x_3 \\end{bmatrix} =\nx_0|00\\rangle + x_1|01\\rangle + x_2|10\\rangle + x_3|11\\rangle\n$$\n\nJust like with single qubits, we can put arbitrary symbols within the kets the same way variables are used in algebra.\nWhether a ket represents a single qubit or an entire system depends on the context.\nSome ket symbols have a commonly accepted usage, such as the symbols for the Bell basis:\n\n$$|\\phi^+\\rangle = \\frac{1}{\\sqrt{2}}\\big(|00\\rangle + |11\\rangle\\big)$$\n$$|\\phi^-\\rangle = \\frac{1}{\\sqrt{2}}\\big(|00\\rangle - |11\\rangle\\big)$$\n$$|\\psi^+\\rangle = \\frac{1}{\\sqrt{2}}\\big(|01\\rangle + |10\\rangle\\big)$$\n$$|\\psi^-\\rangle = \\frac{1}{\\sqrt{2}}\\big(|01\\rangle - |10\\rangle\\big)$$\n\n
|
|
582
|
+
"asMarkdown": "\nJust like with single qubits, Dirac notation provides a useful shorthand for writing down states of multi-qubit systems.\n\nAs we've seen earlier, multi-qubit systems have their own canonical bases, and the basis states can be represented as tensor products of single-qubit basis states. Any multi-qubit system can be represented as a linear combination of these basis states:\n\n$$\n\\begin{bmatrix} x_0 \\\\\\ x_1 \\\\\\ x_2 \\\\\\ x_3 \\end{bmatrix} =\nx_0\\begin{bmatrix} 1 \\\\\\ 0 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix} +\nx_1\\begin{bmatrix} 0 \\\\\\ 1 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix} +\nx_2\\begin{bmatrix} 0 \\\\\\ 0 \\\\\\ 1 \\\\\\ 0 \\end{bmatrix} +\nx_3\\begin{bmatrix} 0 \\\\\\ 0 \\\\\\ 0 \\\\\\ 1 \\end{bmatrix} =\nx_0|0\\rangle \\otimes |0\\rangle +\nx_1|0\\rangle \\otimes |1\\rangle +\nx_2|1\\rangle \\otimes |0\\rangle +\nx_3|1\\rangle \\otimes |1\\rangle\n$$\n\nTo simplify this, tensor products of basis states have their own notation:\n\n$$|0\\rangle \\otimes |0\\rangle = |00\\rangle$$\n$$|0\\rangle \\otimes |1\\rangle = |01\\rangle$$\n$$|1\\rangle \\otimes |0\\rangle = |10\\rangle$$\n$$|1\\rangle \\otimes |1\\rangle = |11\\rangle$$\n\n$$|0\\rangle \\otimes |0\\rangle \\otimes |0\\rangle = |000\\rangle$$\n\nAnd so on.\n\nOr, more generally:\n\n$$|i_0\\rangle \\otimes |i_1\\rangle \\otimes \\dotsb \\otimes |i_n\\rangle = |i_0i_1...i_n\\rangle$$\n\nUsing this notation simplifies our example:\n\n$$\n\\begin{bmatrix} x_0 \\\\\\ x_1 \\\\\\ x_2 \\\\\\ x_3 \\end{bmatrix} =\nx_0|00\\rangle + x_1|01\\rangle + x_2|10\\rangle + x_3|11\\rangle\n$$\n\nJust like with single qubits, we can put arbitrary symbols within the kets the same way variables are used in algebra.\nWhether a ket represents a single qubit or an entire system depends on the context.\nSome ket symbols have a commonly accepted usage, such as the symbols for the Bell basis:\n\n$$|\\phi^+\\rangle = \\frac{1}{\\sqrt{2}}\\big(|00\\rangle + |11\\rangle\\big)$$\n$$|\\phi^-\\rangle = \\frac{1}{\\sqrt{2}}\\big(|00\\rangle - |11\\rangle\\big)$$\n$$|\\psi^+\\rangle = \\frac{1}{\\sqrt{2}}\\big(|01\\rangle + |10\\rangle\\big)$$\n$$|\\psi^-\\rangle = \\frac{1}{\\sqrt{2}}\\big(|01\\rangle - |10\\rangle\\big)$$\n\n>## Endianness\n>\n> In classical computing, endianness refers to the order of bits (or bytes) when representing numbers in binary. You're probably familiar with the typical way of writing numbers in binary: $0 = 0_2$, $1 = 1_2$, $2 = 10_2$, $3 = 11_2$, $4 = 100_2$, $5 = 101_2$, $6 = 110_2$, etc. This is known as **big-endian format**. In big-endian format, the *most significant* bits come first. For example: $110_2 = 1 \\cdot 4 + 1 \\cdot 2 + 0 \\cdot 1 = 4 + 2 = 6$.\n>\n> There is an alternate way of writing binary numbers - **little-endian format**. In little-endian format, the *least significant* bits come first. For example, $2$ would be written as $01$, $4$ as $001$, and $6$ as $011$. To put it another way, in little endian format, the number is written backwards compared to the big-endian format.\n>\n> In Dirac notation for multi-qubit systems, it's common to see integer numbers within the kets instead of bit sequences. What those numbers mean depends on the context - whether the notation used is big-endian or little-endian.\n>\n> Examples with a 3 qubit system:\n>\n> <table>\n> <tr>\n> <th>Integer Ket</th>\n> <td>$|0\\rangle$</td>\n> <td>$|1\\rangle$</td>\n> <td>$|2\\rangle$</td>\n> <td>$|3\\rangle$</td>\n> <td>$|4\\rangle$</td>\n> <td>$|5\\rangle$</td>\n> <td>$|6\\rangle$</td>\n> <td>$|7\\rangle$</td>\n> </tr>\n> <tr>\n> <th>Big-endian</th>\n> <td>$|000\\rangle$</td>\n> <td>$|001\\rangle$</td>\n> <td>$|010\\rangle$</td>\n> <td>$|011\\rangle$</td>\n> <td>$|100\\rangle$</td>\n> <td>$|101\\rangle$</td>\n> <td>$|110\\rangle$</td>\n> <td>$|111\\rangle$</td>\n> </tr>\n> <tr>\n> <th>Little-endian</th>\n> <td>$|000\\rangle$</td>\n> <td>$|100\\rangle$</td>\n> <td>$|010\\rangle$</td>\n> <td>$|110\\rangle$</td>\n> <td>$|001\\rangle$</td>\n> <td>$|101\\rangle$</td>\n> <td>$|011\\rangle$</td>\n> <td>$|111\\rangle$</td>\n> </tr>\n></table>\n>\n> Multi-qubit quantum systems that store superpositions of numbers are often referred to as **quantum registers**."
|
|
583
583
|
}
|
|
584
584
|
]
|
|
585
585
|
},
|
|
@@ -590,8 +590,8 @@ export default {
|
|
|
590
590
|
"items": [
|
|
591
591
|
{
|
|
592
592
|
"type": "text-content",
|
|
593
|
-
"asHtml": "<p>This demo shows you how to allocate multiple qubits in Q# and examine their joint state. It uses single-qubit gates for manipulating the individual qubit states - if you need a refresher on them, please review the Single-Qubit Gates kata.</p>\n<p>These demos use the function <code>DumpMachine</code> to print the state of the quantum simulator.\nWhen dealing with multi-qubit systems, <code>DumpMachine</code> prints information about each basis state that has a non-zero amplitude, one basis state per row, the same as it does for single-qubit systems
|
|
594
|
-
"asMarkdown": "\nThis demo shows you how to allocate multiple qubits in Q# and examine their joint state. It uses single-qubit gates for manipulating the individual qubit states - if you need a refresher on them, please review the Single-Qubit Gates kata.\n\nThese demos use the function `DumpMachine` to print the state of the quantum simulator.\nWhen dealing with multi-qubit systems, `DumpMachine` prints information about each basis state that has a non-zero amplitude, one basis state per row, the same as it does for single-qubit systems
|
|
593
|
+
"asHtml": "<p>This demo shows you how to allocate multiple qubits in Q# and examine their joint state. It uses single-qubit gates for manipulating the individual qubit states - if you need a refresher on them, please review the Single-Qubit Gates kata.</p>\n<p>These demos use the function <code>DumpMachine</code> to print the state of the quantum simulator.\nWhen dealing with multi-qubit systems, <code>DumpMachine</code> prints information about each basis state that has a non-zero amplitude, one basis state per row, the same as it does for single-qubit systems.\nThe basis states are represented as bit strings, one bit per the qubit allocated, with the leftmost bit corresponding\nto the qubit that was allocated the earliest. (If the qubits were allocated at once as an array, the leftmost bit corresponds\nto the first element of the array.)</p>\n",
|
|
594
|
+
"asMarkdown": "\nThis demo shows you how to allocate multiple qubits in Q# and examine their joint state. It uses single-qubit gates for manipulating the individual qubit states - if you need a refresher on them, please review the Single-Qubit Gates kata.\n\nThese demos use the function `DumpMachine` to print the state of the quantum simulator.\nWhen dealing with multi-qubit systems, `DumpMachine` prints information about each basis state that has a non-zero amplitude, one basis state per row, the same as it does for single-qubit systems.\nThe basis states are represented as bit strings, one bit per the qubit allocated, with the leftmost bit corresponding\nto the qubit that was allocated the earliest. (If the qubits were allocated at once as an array, the leftmost bit corresponds\nto the first element of the array.)"
|
|
595
595
|
},
|
|
596
596
|
{
|
|
597
597
|
"type": "example",
|
|
@@ -615,8 +615,8 @@ export default {
|
|
|
615
615
|
"asMarkdown": "\n**Input:** 2 qubits in an unknown state $|\\psi\\rangle = \\sum_{k = 0}^{3} x_k |k\\rangle$. The amplitudes $x_k$ will be real and non-negative.\n\n**Output:** A tuple of two numbers $(x_1', x_2')$ - your estimates of the amplitudes of the state $|01\\rangle$ and $|10\\rangle$, respectively.\nThe absolute errors $|x_1 - x_1'|$ and $|x_2 - x_2'|$ should be less than or equal to 0.001.\n\nPlease note that the state parameter is guaranteed to be the same\nif you run the code several times. Your operation will be called\nonce for every run.\n\n<details>\n <summary><b>Need a hint?</b></summary>\n On a physical quantum system, there would be no way to obtain these values from a single observation. Since this program runs on a simulator, we can use <code>DumpMachine</code> to inspect the qubits and take a note of their state. Furthermore, the problem statement guarantees, that the state will be the same from invocation to invocation. So we can update the code to return the amplitudes that we've taken note of. Then run the code again.\n</details>\n"
|
|
616
616
|
},
|
|
617
617
|
"sourceIds": [
|
|
618
|
-
"
|
|
619
|
-
"
|
|
618
|
+
"multi_qubit_systems__learn_basis_state_amplitudes__Verification.qs",
|
|
619
|
+
"KatasLibrary.qs"
|
|
620
620
|
],
|
|
621
621
|
"placeholderCode": "namespace Kata {\n operation LearnBasisStateAmplitudes(qs : Qubit[]) : (Double, Double) {\n // Implement your solution here...\n\n return (0.0, 0.0);\n }\n}\n",
|
|
622
622
|
"explainedSolution": {
|
|
@@ -657,8 +657,8 @@ export default {
|
|
|
657
657
|
"asMarkdown": "**Input:** A two-qubit system in the basis state $|00\\rangle = \\begin{bmatrix} 1 \\\\\\ 0 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix}$.\n\n**Goal:** Transform the system into the basis state $|11\\rangle = \\begin{bmatrix} 0 \\\\\\ 0 \\\\\\ 0 \\\\\\ 1 \\end{bmatrix}$.\n"
|
|
658
658
|
},
|
|
659
659
|
"sourceIds": [
|
|
660
|
-
"
|
|
661
|
-
"
|
|
660
|
+
"multi_qubit_systems__prepare_basis_state__Verification.qs",
|
|
661
|
+
"KatasLibrary.qs"
|
|
662
662
|
],
|
|
663
663
|
"placeholderCode": "namespace Kata {\n operation PrepareBasisState(qs : Qubit[]) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}",
|
|
664
664
|
"explainedSolution": {
|
|
@@ -687,8 +687,8 @@ export default {
|
|
|
687
687
|
"asMarkdown": "**Input:** A two-qubit system in the basis state $|00\\rangle = \\begin{bmatrix} 1 \\\\\\ 0 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix}$.\n\n**Goal:** Transform the system into the state $\\frac{1}{\\sqrt2}\\big(|00\\rangle - |01\\rangle\\big) = \\frac{1}{\\sqrt2}\\begin{bmatrix} 1 \\\\\\ -1 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix}$.\n\n<details>\n <summary><b>Need a hint?</b></summary>\n Represent the target state as a tensor product $|0\\rangle \\otimes \\frac{1}{\\sqrt2}\\big(|0\\rangle - |1\\rangle\\big) = \\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix} \\otimes \\frac{1}{\\sqrt2}\\begin{bmatrix} 1 \\\\\\ -1 \\end{bmatrix}$.\n</details>\n"
|
|
688
688
|
},
|
|
689
689
|
"sourceIds": [
|
|
690
|
-
"
|
|
691
|
-
"
|
|
690
|
+
"multi_qubit_systems__prepare_superposition__Verification.qs",
|
|
691
|
+
"KatasLibrary.qs"
|
|
692
692
|
],
|
|
693
693
|
"placeholderCode": "namespace Kata {\n operation PrepareSuperposition(qs : Qubit[]) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}",
|
|
694
694
|
"explainedSolution": {
|
|
@@ -717,8 +717,8 @@ export default {
|
|
|
717
717
|
"asMarkdown": "\n**Input:** A two-qubit system in the basis state $|00\\rangle = \\begin{bmatrix} 1 \\\\\\ 0 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix}$.\n\n**Goal:** Transform the system into the state $\\frac{1}{2}\\big(|00\\rangle - |01\\rangle + |10\\rangle - |11\\rangle\\big) = \\frac{1}{2}\\begin{bmatrix} 1 \\\\\\ -1 \\\\\\ 1 \\\\\\ -1 \\end{bmatrix}$.\n\n<details>\n <summary><b>Need a hint?</b></summary>\n Represent the target state as a tensor product $\\frac{1}{\\sqrt2}\\big(|0\\rangle + |1\\rangle\\big) \\otimes \\frac{1}{\\sqrt2}\\big(|0\\rangle - |1\\rangle\\big) = \\frac{1}{\\sqrt2} \\begin{bmatrix} 1 \\\\\\ 1 \\end{bmatrix} \\otimes \\frac{1}{\\sqrt2}\\begin{bmatrix} 1 \\\\\\ -1 \\end{bmatrix}$.\n</details>\n"
|
|
718
718
|
},
|
|
719
719
|
"sourceIds": [
|
|
720
|
-
"
|
|
721
|
-
"
|
|
720
|
+
"multi_qubit_systems__prepare_with_real__Verification.qs",
|
|
721
|
+
"KatasLibrary.qs"
|
|
722
722
|
],
|
|
723
723
|
"placeholderCode": "namespace Kata {\n operation PrepareWithReal(qs : Qubit[]) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}",
|
|
724
724
|
"explainedSolution": {
|
|
@@ -747,8 +747,8 @@ export default {
|
|
|
747
747
|
"asMarkdown": "\n**Input:** A two-qubit system in the basis state $|00\\rangle = \\begin{bmatrix} 1 \\\\\\ 0 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix}$.\n\n**Goal:** Transform the system into the state $\\frac{1}{2}\\big(|00\\rangle + e^{i\\pi/4}|01\\rangle + e^{i\\pi/2}|10\\rangle + e^{3i\\pi/4}|11\\rangle\\big) = \\frac{1}{2}\\begin{bmatrix} 1 \\\\\\ e^{i\\pi/4} \\\\\\ e^{i\\pi/2} \\\\\\ e^{3i\\pi/4} \\end{bmatrix}$.\n\n<details>\n <summary><b>Need a hint?</b></summary>\n Represent the target state as a tensor product $\\frac{1}{\\sqrt2}\\big(|0\\rangle + e^{i\\pi/2}|1\\rangle\\big) \\otimes \\frac{1}{\\sqrt2}\\big(|0\\rangle + e^{i\\pi/4}|1\\rangle\\big) = \\frac{1}{\\sqrt2} \\begin{bmatrix} 1 \\\\\\ e^{i\\pi/2} \\end{bmatrix} \\otimes \\frac{1}{\\sqrt2}\\begin{bmatrix} 1 \\\\\\ e^{i\\pi/4} \\end{bmatrix}$.\n</details>\n"
|
|
748
748
|
},
|
|
749
749
|
"sourceIds": [
|
|
750
|
-
"
|
|
751
|
-
"
|
|
750
|
+
"multi_qubit_systems__prepare_with_complex__Verification.qs",
|
|
751
|
+
"KatasLibrary.qs"
|
|
752
752
|
],
|
|
753
753
|
"placeholderCode": "namespace Kata {\n operation PrepareWithComplex(qs : Qubit[]) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}",
|
|
754
754
|
"explainedSolution": {
|
|
@@ -792,8 +792,8 @@ export default {
|
|
|
792
792
|
"items": [
|
|
793
793
|
{
|
|
794
794
|
"type": "text-content",
|
|
795
|
-
"asHtml": "<p>This kata continues the introduction to quantum gates, focusing on applying quantum gates to multi-qubit systems.</p>\n<p><strong>This kata covers the following topics:</strong></p>\n<ul>\n<li>Applying quantum gates to a part of the system</li>\n<li
|
|
796
|
-
"asMarkdown": "\nThis kata continues the introduction to quantum gates, focusing on applying quantum gates to multi-qubit systems.\n\n**This kata covers the following topics:**\n- Applying quantum gates to a part of the system\n-
|
|
795
|
+
"asHtml": "<p>This kata continues the introduction to quantum gates, focusing on applying quantum gates to multi-qubit systems.</p>\n<p><strong>This kata covers the following topics:</strong></p>\n<ul>\n<li>Applying quantum gates to a part of the system</li>\n<li>$CNOT$, $CCNOT$, and $SWAP$ gates</li>\n<li>Controlled gates</li>\n</ul>\n<p><strong>What you should know to start working on this kata:</strong></p>\n<ul>\n<li>Basic linear algebra</li>\n<li>The concept of qubit and multi-qubit systems</li>\n<li>Single-qubit and multi-qubit quantum gates</li>\n</ul>\n",
|
|
796
|
+
"asMarkdown": "\nThis kata continues the introduction to quantum gates, focusing on applying quantum gates to multi-qubit systems.\n\n**This kata covers the following topics:**\n\n- Applying quantum gates to a part of the system\n- $CNOT$, $CCNOT$, and $SWAP$ gates\n- Controlled gates\n\n**What you should know to start working on this kata:**\n\n- Basic linear algebra\n- The concept of qubit and multi-qubit systems\n- Single-qubit and multi-qubit quantum gates"
|
|
797
797
|
}
|
|
798
798
|
]
|
|
799
799
|
},
|
|
@@ -804,8 +804,8 @@ export default {
|
|
|
804
804
|
"items": [
|
|
805
805
|
{
|
|
806
806
|
"type": "text-content",
|
|
807
|
-
"asHtml": "<p>As a reminder, single-qubit gates are represented by $2\\times2$ unitary matrices
|
|
808
|
-
"asMarkdown": "\nAs a reminder, single-qubit gates are represented by $2\\times2$ unitary matrices.\
|
|
807
|
+
"asHtml": "<p>As a reminder, single-qubit gates are represented by $2\\times2$ unitary matrices.\nThe effect of a gate applied to a qubit can be calculated by multiplying the corresponding matrix by the state vector of the qubit to get the resulting state vector.</p>\n<p>Multi-qubit gates are represented by $2^N\\times2^N$ matrices, where $N$ is the number of qubits the gate operates on. To apply this gate, you multiply the matrix by the state vector of the $N$-qubit quantum system.</p>\n<h2>Applying Gates to a Part of the System</h2>\n<p>The simplest thing we can do with multi-qubit systems is to apply gates to only a subset of qubits in the system.\nSimilar to how it is sometimes possible to represent the state of a multi-qubit system as a tensor product of single-qubit states, you can construct gates that modify the state of a multi-qubit system as tensor products of gates that affect parts of the system.</p>\n<p>Let's consider an example of applying a single-qubit gate to one of the qubits of a two-qubit system.\nIf you want to apply an $X$ gate to the first qubit of the system and do nothing to the second qubit, the resulting gate will be represented as a tensor product of an $X$ gate and the identity gate $I$ which corresponds to doing nothing:</p>\n<p>$$\nX \\otimes I =\n\\begin{bmatrix} 0 & 1 \\\\ 1 & 0 \\end{bmatrix} \\otimes \\begin{bmatrix} 1 & 0 \\\\ 0 & 1 \\end{bmatrix} =\n\\begin{bmatrix}\n 0 & 0 & 1 & 0 \\\\ \n 0 & 0 & 0 & 1 \\\\ \n 1 & 0 & 0 & 0 \\\\ \n 0 & 1 & 0 & 0\n\\end{bmatrix}\n$$</p>\n<p>You can use the same approach when applying several gates to independent parts of the system at the same time.\nFor example, applying the $X$ gate to the first qubit and the $H$ gate to the second qubit would be represented as follows:</p>\n<p>$$\nX \\otimes H =\n\\begin{bmatrix} 0 & 1 \\\\ 1 & 0 \\end{bmatrix} \\otimes \\frac{1}{\\sqrt{2}}\\begin{bmatrix} 1 & 1 \\\\ 1 & -1 \\end{bmatrix} =\n\\frac{1}{\\sqrt{2}}\\begin{bmatrix}\n 0 & 0 & 1 & 1 \\\\ \n 0 & 0 & 1 & -1 \\\\ \n 1 & 1 & 0 & 0 \\\\ \n 1 & -1 & 0 & 0\n\\end{bmatrix}\n$$</p>\n<blockquote>\n<p>Note that we can use mixed-multiplication property of tensor product to see that this is equivalent to applying $X$ gate to the first qubit and applying $H$ gate to the second qubit, in either order:</p>\n<p>$$X \\otimes H = (I X) \\otimes (H I) = (I \\otimes H) (X \\otimes I)$$\n$$X \\otimes H = (X I) \\otimes (I H) = (X \\otimes I) (I \\otimes H)$$</p>\n</blockquote>\n<p>This approach can be generalized to larger systems and gates that act on multiple qubits as well.\nIt can be less straightforward when a multi-qubit gate is applied to a subset of qubits that are not "adjacent" to each other in the tensor product; we'll see an example later in this kata.</p>\n",
|
|
808
|
+
"asMarkdown": "\nAs a reminder, single-qubit gates are represented by $2\\times2$ unitary matrices.\nThe effect of a gate applied to a qubit can be calculated by multiplying the corresponding matrix by the state vector of the qubit to get the resulting state vector.\n\nMulti-qubit gates are represented by $2^N\\\\times2^N$ matrices, where $N$ is the number of qubits the gate operates on. To apply this gate, you multiply the matrix by the state vector of the $N$-qubit quantum system.\n\n## Applying Gates to a Part of the System\n\nThe simplest thing we can do with multi-qubit systems is to apply gates to only a subset of qubits in the system.\nSimilar to how it is sometimes possible to represent the state of a multi-qubit system as a tensor product of single-qubit states, you can construct gates that modify the state of a multi-qubit system as tensor products of gates that affect parts of the system.\n\nLet's consider an example of applying a single-qubit gate to one of the qubits of a two-qubit system.\nIf you want to apply an $X$ gate to the first qubit of the system and do nothing to the second qubit, the resulting gate will be represented as a tensor product of an $X$ gate and the identity gate $I$ which corresponds to doing nothing:\n\n$$\nX \\otimes I =\n\\begin{bmatrix} 0 & 1 \\\\\\ 1 & 0 \\end{bmatrix} \\otimes \\begin{bmatrix} 1 & 0 \\\\\\ 0 & 1 \\end{bmatrix} =\n\\begin{bmatrix}\n 0 & 0 & 1 & 0 \\\\\\ \n 0 & 0 & 0 & 1 \\\\\\ \n 1 & 0 & 0 & 0 \\\\\\ \n 0 & 1 & 0 & 0\n\\end{bmatrix}\n$$\n\nYou can use the same approach when applying several gates to independent parts of the system at the same time.\nFor example, applying the $X$ gate to the first qubit and the $H$ gate to the second qubit would be represented as follows:\n\n$$\nX \\otimes H =\n\\begin{bmatrix} 0 & 1 \\\\\\ 1 & 0 \\end{bmatrix} \\otimes \\frac{1}{\\sqrt{2}}\\begin{bmatrix} 1 & 1 \\\\\\ 1 & -1 \\end{bmatrix} =\n\\frac{1}{\\sqrt{2}}\\begin{bmatrix}\n 0 & 0 & 1 & 1 \\\\\\ \n 0 & 0 & 1 & -1 \\\\\\ \n 1 & 1 & 0 & 0 \\\\\\ \n 1 & -1 & 0 & 0\n\\end{bmatrix}\n$$\n\n> Note that we can use mixed-multiplication property of tensor product to see that this is equivalent to applying $X$ gate to the first qubit and applying $H$ gate to the second qubit, in either order:\n>\n> $$X \\otimes H = (I X) \\otimes (H I) = (I \\otimes H) (X \\otimes I)$$\n> $$X \\otimes H = (X I) \\otimes (I H) = (X \\otimes I) (I \\otimes H)$$\n\nThis approach can be generalized to larger systems and gates that act on multiple qubits as well.\nIt can be less straightforward when a multi-qubit gate is applied to a subset of qubits that are not \"adjacent\" to each other in the tensor product; we'll see an example later in this kata."
|
|
809
809
|
}
|
|
810
810
|
]
|
|
811
811
|
},
|
|
@@ -815,12 +815,12 @@ export default {
|
|
|
815
815
|
"title": "Compound Gate",
|
|
816
816
|
"description": {
|
|
817
817
|
"type": "text-content",
|
|
818
|
-
"asHtml": "<p><strong>Inputs:</strong> $3$ qubits in an arbitrary superposition state $|\\psi\\rangle$, stored in an array of length 3
|
|
819
|
-
"asMarkdown": "**Inputs:** $3$ qubits in an arbitrary superposition state $|\\\\psi\\\\rangle$, stored in an array of length 3
|
|
818
|
+
"asHtml": "<p><strong>Inputs:</strong> $3$ qubits in an arbitrary superposition state $|\\psi\\rangle$, stored in an array of length $3$.</p>\n<p><strong>Goal:</strong> Apply the following matrix to the system. This matrix can be represented as applying $3$ single-qubit gates.</p>\n<p>$$\nQ =\n\\begin{bmatrix}\n0 & -i & 0 & 0 & 0 & 0 & 0 & 0 \\\\ \ni & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\\\ \n0 & 0 & 0 & -i & 0 & 0 & 0 & 0 \\\\ \n0 & 0 & i & 0 & 0 & 0 & 0 & 0 \\\\ \n0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\\\ \n0 & 0 & 0 & 0 & -1 & 0 & 0 & 0 \\\\ \n0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\\\ \n0 & 0 & 0 & 0 & 0 & 0 & -1 & 0\n\\end{bmatrix}\n$$</p>\n<blockquote>\n<p>We recommend to keep a list of common quantum gates on hand.</p>\n</blockquote>\n<details>\n <summary><b>Need a hint?</b></summary>\n <p>Start by noticing that the top right and bottom left quadrants of the matrix are filled with $0$s, and the bottom right quadrant equals to the top left one, multiplied by $i$. Does this look like a tensor product of a 1-qubit and 2-qubit matrices? Which ones?</p>\n</details>",
|
|
819
|
+
"asMarkdown": "**Inputs:** $3$ qubits in an arbitrary superposition state $|\\\\psi\\\\rangle$, stored in an array of length $3$.\n\n**Goal:** Apply the following matrix to the system. This matrix can be represented as applying $3$ single-qubit gates.\n\n$$\nQ =\n\\begin{bmatrix}\n0 & -i & 0 & 0 & 0 & 0 & 0 & 0 \\\\\\ \ni & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\\\\\ \n0 & 0 & 0 & -i & 0 & 0 & 0 & 0 \\\\\\ \n0 & 0 & i & 0 & 0 & 0 & 0 & 0 \\\\\\ \n0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\\\\\ \n0 & 0 & 0 & 0 & -1 & 0 & 0 & 0 \\\\\\ \n0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\\\\\ \n0 & 0 & 0 & 0 & 0 & 0 & -1 & 0\n\\end{bmatrix}\n$$\n\n> We recommend to keep a list of common quantum gates on hand.\n\n<details>\n <summary><b>Need a hint?</b></summary>\n <p>Start by noticing that the top right and bottom left quadrants of the matrix are filled with $0$s, and the bottom right quadrant equals to the top left one, multiplied by $i$. Does this look like a tensor product of a 1-qubit and 2-qubit matrices? Which ones?</p>\n</details>"
|
|
820
820
|
},
|
|
821
821
|
"sourceIds": [
|
|
822
|
-
"
|
|
823
|
-
"
|
|
822
|
+
"multi_qubit_gates__compound_gate__Verification.qs",
|
|
823
|
+
"KatasLibrary.qs"
|
|
824
824
|
],
|
|
825
825
|
"placeholderCode": "namespace Kata {\n operation CompoundGate(qs : Qubit[]) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}",
|
|
826
826
|
"explainedSolution": {
|
|
@@ -828,8 +828,8 @@ export default {
|
|
|
828
828
|
"items": [
|
|
829
829
|
{
|
|
830
830
|
"type": "text-content",
|
|
831
|
-
"asHtml": "<p>One way to represent a multi-qubit transformation is to use the tensor product of gates acting on subsets of qubits. For example, if you have 2 qubits, applying the
|
|
832
|
-
"asMarkdown": "\nOne way to represent a multi-qubit transformation is to use the tensor product of gates acting on subsets of qubits. For example, if you have 2 qubits, applying the
|
|
831
|
+
"asHtml": "<p>One way to represent a multi-qubit transformation is to use the tensor product of gates acting on subsets of qubits. For example, if you have 2 qubits, applying the $Z$ gate on the first qubit and the $X$ gate on the second qubit will create this matrix:</p>\n<p>$$\nZ \\otimes X =\n\\begin{bmatrix} 1 & 0 \\\\ 0 & -1 \\end{bmatrix} \\otimes \\begin{bmatrix} 0 & 1 \\\\ 1 & 0 \\end{bmatrix} =\n\\begin{bmatrix} 0 & 1 & 0 & 0 \\\\ 1 & 0 & 0 & 0 \\\\ 0 & 0 & 0 & -1 \\\\ 0 & 0 & -1 & 0 \\end{bmatrix}\n$$</p>\n<p>With this in mind, let's see how to reverse engineer the target matrix above to find the 3 gates which, acting on individual qubits, together form the target transformation.</p>\n<p>Start by noticing that the top right and bottom left quadrants of the target matrix are filled with $0$'s, and the bottom right quadrant equals to the top left one, multiplied by $i$. This hints at applying the $S$ gate to the first qubit:</p>\n<p>$$\nQ =\n\\begin{bmatrix} 1 & 0 \\\\ 0 & i \\end{bmatrix} \\otimes\n\\begin{bmatrix}\n 0 & -i & 0 & 0 \\\\ \n i & 0 & 0 & 0 \\\\ \n 0 & 0 & 0 & -i \\\\ \n 0 & 0 & i & 0 \n\\end{bmatrix} =\n\\begin{bmatrix}\n 0 & -i & 0 & 0 & 0 & 0 & 0 & 0 \\\\ \n i & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\\\ \n 0 & 0 & 0 & -i & 0 & 0 & 0 & 0 \\\\ \n 0 & 0 & i & 0 & 0 & 0 & 0 & 0 \\\\ \n 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\\\ \n 0 & 0 & 0 & 0 & -1 & 0 & 0 & 0 \\\\ \n 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\\\ \n 0 & 0 & 0 & 0 & 0 & 0 & -1 & 0\n\\end{bmatrix}\n$$</p>\n<p>Now the $4 \\times 4$ matrix has all $0$s in the top right and bottom left quadrants, and the bottom right quadrant equals to the top left one. This means the second qubit has the $I$ gate applied to it, and the third qubit - the $Y$ gate:</p>\n<p>$$\nQ =\n\\begin{bmatrix} 1 & 0 \\\\ 0 & i \\end{bmatrix} \\otimes \\begin{bmatrix} 1 & 0 \\\\ 0 & 1 \\end{bmatrix} \\otimes\n\\begin{bmatrix} 0 & -i \\\\ i & 0 \\end{bmatrix} = S \\otimes I \\otimes Y\n$$</p>\n",
|
|
832
|
+
"asMarkdown": "\nOne way to represent a multi-qubit transformation is to use the tensor product of gates acting on subsets of qubits. For example, if you have 2 qubits, applying the $Z$ gate on the first qubit and the $X$ gate on the second qubit will create this matrix:\n\n$$\nZ \\otimes X =\n\\begin{bmatrix} 1 & 0 \\\\\\ 0 & -1 \\end{bmatrix} \\otimes \\begin{bmatrix} 0 & 1 \\\\\\ 1 & 0 \\end{bmatrix} =\n\\begin{bmatrix} 0 & 1 & 0 & 0 \\\\\\ 1 & 0 & 0 & 0 \\\\\\ 0 & 0 & 0 & -1 \\\\\\ 0 & 0 & -1 & 0 \\end{bmatrix}\n$$\n\nWith this in mind, let's see how to reverse engineer the target matrix above to find the 3 gates which, acting on individual qubits, together form the target transformation.\n\nStart by noticing that the top right and bottom left quadrants of the target matrix are filled with $0$'s, and the bottom right quadrant equals to the top left one, multiplied by $i$. This hints at applying the $S$ gate to the first qubit:\n\n$$\nQ =\n\\begin{bmatrix} 1 & 0 \\\\\\ 0 & i \\end{bmatrix} \\otimes\n\\begin{bmatrix}\n 0 & -i & 0 & 0 \\\\\\ \n i & 0 & 0 & 0 \\\\\\ \n 0 & 0 & 0 & -i \\\\\\ \n 0 & 0 & i & 0 \n\\end{bmatrix} =\n\\begin{bmatrix}\n 0 & -i & 0 & 0 & 0 & 0 & 0 & 0 \\\\\\ \n i & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\\\\\ \n 0 & 0 & 0 & -i & 0 & 0 & 0 & 0 \\\\\\ \n 0 & 0 & i & 0 & 0 & 0 & 0 & 0 \\\\\\ \n 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\\\\\ \n 0 & 0 & 0 & 0 & -1 & 0 & 0 & 0 \\\\\\ \n 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\\\\\ \n 0 & 0 & 0 & 0 & 0 & 0 & -1 & 0\n\\end{bmatrix}\n$$\n\nNow the $4 \\times 4$ matrix has all $0$s in the top right and bottom left quadrants, and the bottom right quadrant equals to the top left one. This means the second qubit has the $I$ gate applied to it, and the third qubit - the $Y$ gate:\n\n$$\nQ =\n\\begin{bmatrix} 1 & 0 \\\\\\ 0 & i \\end{bmatrix} \\otimes \\begin{bmatrix} 1 & 0 \\\\\\ 0 & 1 \\end{bmatrix} \\otimes\n\\begin{bmatrix} 0 & -i \\\\\\ i & 0 \\end{bmatrix} = S \\otimes I \\otimes Y\n$$"
|
|
833
833
|
},
|
|
834
834
|
{
|
|
835
835
|
"type": "solution",
|
|
@@ -846,8 +846,8 @@ export default {
|
|
|
846
846
|
"items": [
|
|
847
847
|
{
|
|
848
848
|
"type": "text-content",
|
|
849
|
-
"asHtml": "<p>Our first proper multi-qubit gate is the
|
|
850
|
-
"asMarkdown": "\nOur first proper multi-qubit gate is the
|
|
849
|
+
"asHtml": "<p>Our first proper multi-qubit gate is the $CNOT$ ("controlled NOT") gate. The $CNOT$ gate is a two-qubit gate, with one qubit referred to as the <strong>control</strong> qubit, and the other qubit as the <strong>target</strong> qubit (usually the first qubit is the control, and the second qubit is the target).</p>\n<p>$CNOT$ acts as a conditional gate of sorts: if the control qubit is in state $|1\\rangle$, it applies the $X$ gate to the target qubit, otherwise it does nothing.</p>\n<blockquote>\n<p>If the system is in a superposition of several basis states, the effects of the gate will be a linear combination of the effects of it acting separately on each of the basis states.\nThis will be the case for all quantum gates you'll encounter later that are specified in terms of basis states: since all unitary gates are linear, it is sufficient to define their effect on the basis states, and use linearity to figure out their effect on any state.</p>\n</blockquote>\n<table>\n <tr>\n <th>Gate</th>\n <th>Matrix</th>\n <th>Applying to $|\\psi\\rangle = \\alpha|00\\rangle + \\beta|01\\rangle + \\gamma|10\\rangle + \\delta|11\\rangle$</th>\n <th>Applying to basis states</th>\n </tr>\n <tr>\n <td>$CNOT$</td>\n <td>$\\begin{bmatrix} 1 & 0 & 0 & 0 \\\\\\ 0 & 1 & 0 & 0 \\\\\\ 0 & 0 & 0 & 1 \\\\\\ 0 & 0 & 1 & 0 \\end{bmatrix}$</td>\n <td>$CNOT|\\psi\\rangle = \\alpha|00\\rangle + \\beta|01\\rangle + \\delta|10\\rangle + \\gamma|11\\rangle$</td>\n <td>\n $$CNOT|00\\rangle = |00\\rangle$$\n $$CNOT|01\\rangle = |01\\rangle$$\n $$CNOT|10\\rangle = |11\\rangle$$\n $$CNOT|11\\rangle = |10\\rangle$$\n </td>\n </tr>\n</table>\n\n<p>The $CNOT$ gate is particularly useful for preparing entangled states. Consider the following separable state:</p>\n<p>$$\\big(\\alpha|0\\rangle + \\beta|1\\rangle\\big) \\otimes |0\\rangle = \\alpha|00\\rangle + \\beta|10\\rangle$$</p>\n<p>If we apply the $CNOT$ gate to it, with the first qubit as the control, and the second as the target, we get the following state, which is not separable any longer:</p>\n<p>$$\\alpha|00\\rangle + \\beta|11\\rangle$$</p>\n<p>The $CNOT$ gate is self-adjoint: applying it for the second time reverses its effect.</p>\n",
|
|
850
|
+
"asMarkdown": "\nOur first proper multi-qubit gate is the $CNOT$ (\"controlled NOT\") gate. The $CNOT$ gate is a two-qubit gate, with one qubit referred to as the **control** qubit, and the other qubit as the **target** qubit (usually the first qubit is the control, and the second qubit is the target).\n\n$CNOT$ acts as a conditional gate of sorts: if the control qubit is in state $|1\\\\rangle$, it applies the $X$ gate to the target qubit, otherwise it does nothing.\n\n> If the system is in a superposition of several basis states, the effects of the gate will be a linear combination of the effects of it acting separately on each of the basis states.\n> This will be the case for all quantum gates you'll encounter later that are specified in terms of basis states: since all unitary gates are linear, it is sufficient to define their effect on the basis states, and use linearity to figure out their effect on any state.\n\n<table>\n <tr>\n <th>Gate</th>\n <th>Matrix</th>\n <th>Applying to $|\\psi\\rangle = \\alpha|00\\rangle + \\beta|01\\rangle + \\gamma|10\\rangle + \\delta|11\\rangle$</th>\n <th>Applying to basis states</th>\n </tr>\n <tr>\n <td>$CNOT$</td>\n <td>$\\begin{bmatrix} 1 & 0 & 0 & 0 \\\\\\ 0 & 1 & 0 & 0 \\\\\\ 0 & 0 & 0 & 1 \\\\\\ 0 & 0 & 1 & 0 \\end{bmatrix}$</td>\n <td>$CNOT|\\psi\\rangle = \\alpha|00\\rangle + \\beta|01\\rangle + \\delta|10\\rangle + \\gamma|11\\rangle$</td>\n <td>\n $$CNOT|00\\rangle = |00\\rangle$$\n $$CNOT|01\\rangle = |01\\rangle$$\n $$CNOT|10\\rangle = |11\\rangle$$\n $$CNOT|11\\rangle = |10\\rangle$$\n </td>\n </tr>\n</table>\n\nThe $CNOT$ gate is particularly useful for preparing entangled states. Consider the following separable state:\n\n$$\\big(\\alpha|0\\rangle + \\beta|1\\rangle\\big) \\otimes |0\\rangle = \\alpha|00\\rangle + \\beta|10\\rangle$$\n\nIf we apply the $CNOT$ gate to it, with the first qubit as the control, and the second as the target, we get the following state, which is not separable any longer:\n\n$$\\alpha|00\\rangle + \\beta|11\\rangle$$\n\nThe $CNOT$ gate is self-adjoint: applying it for the second time reverses its effect."
|
|
851
851
|
}
|
|
852
852
|
]
|
|
853
853
|
},
|
|
@@ -861,8 +861,8 @@ export default {
|
|
|
861
861
|
"asMarkdown": "**Input:** Two qubits in state $|00\\\\rangle$, stored in an array of length 2.\n\n**Goal:** Transform the system into the Bell state $\\\\Phi^+ = \\\\frac{1}{\\\\sqrt{2}}\\\\big(|00\\\\rangle + |11\\\\rangle\\\\big)$."
|
|
862
862
|
},
|
|
863
863
|
"sourceIds": [
|
|
864
|
-
"
|
|
865
|
-
"
|
|
864
|
+
"multi_qubit_gates__preparing_bell_state__Verification.qs",
|
|
865
|
+
"KatasLibrary.qs"
|
|
866
866
|
],
|
|
867
867
|
"placeholderCode": "namespace Kata {\n operation BellState (qs : Qubit[]) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}",
|
|
868
868
|
"explainedSolution": {
|
|
@@ -870,8 +870,8 @@ export default {
|
|
|
870
870
|
"items": [
|
|
871
871
|
{
|
|
872
872
|
"type": "text-content",
|
|
873
|
-
"asHtml": "<p>We've seen this state before in the
|
|
874
|
-
"asMarkdown": "\nWe've seen this state before in the
|
|
873
|
+
"asHtml": "<p>We've seen this state before in the Multi-Qubit Systems kata, where we established that this state is not separable, i.e., it can not be prepared using just the single-qubit gates. To prepare it, we need to use $CNOT$ gate.</p>\n<p>Let's look at the effect of the $CNOT$ gate on a separable state described in the tutorial:\n$$CNOT_{1,2}\\big(\\alpha|0\\rangle + \\beta|1\\rangle\\big) \\otimes |0\\rangle = CNOT_{1,2}(\\alpha|00\\rangle + \\beta|10\\rangle) = \\alpha|00\\rangle + \\beta|11\\rangle$$</p>\n<p>This resulting state is exactly the state we need to prepare, with $\\alpha = \\beta = \\frac{1}{\\sqrt{2}}$!</p>\n<p>The solution takes two steps:</p>\n<ol>\n<li>Prepare a state $\\big(\\frac{1}{\\sqrt{2}}|0\\rangle + \\frac{1}{\\sqrt{2}}|1\\rangle\\big) \\otimes |0\\rangle$.\nWe can use the Hadamard gate to do this.</li>\n<li>Apply a $CNOT$ gate with the first qubit as the control and the second qubit as the target.</li>\n</ol>\n",
|
|
874
|
+
"asMarkdown": "\nWe've seen this state before in the Multi-Qubit Systems kata, where we established that this state is not separable, i.e., it can not be prepared using just the single-qubit gates. To prepare it, we need to use $CNOT$ gate.\n\nLet's look at the effect of the $CNOT$ gate on a separable state described in the tutorial:\n$$CNOT_{1,2}\\big(\\alpha|0\\rangle + \\beta|1\\rangle\\big) \\otimes |0\\rangle = CNOT_{1,2}(\\alpha|00\\rangle + \\beta|10\\rangle) = \\alpha|00\\rangle + \\beta|11\\rangle$$\n\nThis resulting state is exactly the state we need to prepare, with $\\alpha = \\beta = \\frac{1}{\\sqrt{2}}$!\n\nThe solution takes two steps:\n1. Prepare a state $\\big(\\frac{1}{\\sqrt{2}}|0\\rangle + \\frac{1}{\\sqrt{2}}|1\\rangle\\big) \\otimes |0\\rangle$.\nWe can use the Hadamard gate to do this.\n2. Apply a $CNOT$ gate with the first qubit as the control and the second qubit as the target."
|
|
875
875
|
},
|
|
876
876
|
{
|
|
877
877
|
"type": "solution",
|
|
@@ -888,8 +888,8 @@ export default {
|
|
|
888
888
|
"items": [
|
|
889
889
|
{
|
|
890
890
|
"type": "text-content",
|
|
891
|
-
"asHtml": "<p>Same as in the case of single-qubit gates, we can represent multi-qubit gates using Dirac notation.</p>\n<blockquote>\n<p>Recall that kets represent column vectors and bras represent row vectors. For any ket $|\\psi\\rangle$, the corresponding bra is its adjoint (conjugate transpose): $\\langle\\psi| = |\\psi\\rangle^\\dagger$.</p>\n<p>Kets and bras are used to express inner and outer products. The inner product of $|\\phi\\rangle$ and $|\\psi\\rangle$ is the matrix product of $\\langle\\phi|$ and $|\\psi\\rangle$, denoted as $\\langle\\phi|\\psi\\rangle$, and their outer product is the matrix product of $|\\phi\\rangle$ and $\\langle\\psi|$, denoted as $|\\phi\\rangle\\langle\\psi|$.</p>\n<p>As we've seen in the
|
|
892
|
-
"asMarkdown": "\nSame as in the case of single-qubit gates, we can represent multi-qubit gates using Dirac notation.\n\n> Recall that kets represent column vectors and bras represent row vectors. For any ket $|\\psi\\rangle$, the corresponding bra is its adjoint (conjugate transpose): $\\langle\\psi| = |\\psi\\rangle^\\dagger$.\n>\n> Kets and bras are used to express inner and outer products. The inner product of $|\\phi\\rangle$ and $|\\psi\\rangle$ is the matrix product of $\\langle\\phi|$ and $|\\psi\\rangle$, denoted as $\\langle\\phi|\\psi\\rangle$, and their outer product is the matrix product of $|\\phi\\rangle$ and $\\langle\\psi|$, denoted as $|\\phi\\rangle\\langle\\psi|$.\n>\n> As we've seen in the
|
|
891
|
+
"asHtml": "<p>Same as in the case of single-qubit gates, we can represent multi-qubit gates using Dirac notation.</p>\n<blockquote>\n<p>Recall that kets represent column vectors and bras represent row vectors. For any ket $|\\psi\\rangle$, the corresponding bra is its adjoint (conjugate transpose): $\\langle\\psi| = |\\psi\\rangle^\\dagger$.</p>\n<p>Kets and bras are used to express inner and outer products. The inner product of $|\\phi\\rangle$ and $|\\psi\\rangle$ is the matrix product of $\\langle\\phi|$ and $|\\psi\\rangle$, denoted as $\\langle\\phi|\\psi\\rangle$, and their outer product is the matrix product of $|\\phi\\rangle$ and $\\langle\\psi|$, denoted as $|\\phi\\rangle\\langle\\psi|$.</p>\n<p>As we've seen in the Single-Qubit Gates kata, kets and bras can be used to represent matrices. The outer product of two vectors of the same size produces a square matrix. We can use a linear combination of several outer products of simple vectors (such as basis vectors) to express any square matrix.</p>\n</blockquote>\n<p>Let's consider ket-bra representation of the $CNOT$ gate:</p>\n<p>$$CNOT =$$\n$$= |00\\rangle\\langle00| + |01\\rangle\\langle01| + |10\\rangle\\langle11| + |11\\rangle\\langle10| =$$\n$$=\n\\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ 0 \\end{bmatrix}\\begin{bmatrix} 1 & 0 & 0 & 0 \\end{bmatrix} +\n\\begin{bmatrix} 0 \\\\ 1 \\\\ 0 \\\\ 0 \\end{bmatrix}\\begin{bmatrix} 0 & 1 & 0 & 0 \\end{bmatrix} +\n\\begin{bmatrix} 0 \\\\ 0 \\\\ 1 \\\\ 0 \\end{bmatrix}\\begin{bmatrix} 0 & 0 & 0 & 1 \\end{bmatrix} +\n\\begin{bmatrix} 0 \\\\ 0 \\\\ 0 \\\\ 1 \\end{bmatrix}\\begin{bmatrix} 0 & 0 & 1 & 0 \\end{bmatrix} =\n$$\n$$=\n\\begin{bmatrix} 1 & 0 & 0 & 0 \\\\ 0 & 0 & 0 & 0 \\\\ 0 & 0 & 0 & 0 \\\\ 0 & 0 & 0 & 0 \\\\ \\end{bmatrix} +\n\\begin{bmatrix} 0 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 0 \\\\ 0 & 0 & 0 & 0 \\\\ \\end{bmatrix} +\n\\begin{bmatrix} 0 & 0 & 0 & 0 \\\\ 0 & 0 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 0 & 0 \\\\ \\end{bmatrix} +\n\\begin{bmatrix} 0 & 0 & 0 & 0 \\\\ 0 & 0 & 0 & 0 \\\\ 0 & 0 & 0 & 0 \\\\ 0 & 0 & 1 & 0 \\\\ \\end{bmatrix} =\n$$\n$$=\\begin{bmatrix} 1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0 \\\\ \\end{bmatrix}$$</p>\n<p>This representation can be used to carry out calculations in Dirac notation without ever switching back to matrix representation:</p>\n<p>$$\nCNOT|10\\rangle =\n\\big(|00\\rangle\\langle00| + |01\\rangle\\langle01| + |10\\rangle\\langle11| + |11\\rangle\\langle10|\\big)|10\\rangle =$$\n$$=|00\\rangle\\langle00|10\\rangle + |01\\rangle\\langle01|10\\rangle + |10\\rangle\\langle11|10\\rangle + |11\\rangle\\langle10|10\\rangle =$$\n$$=|00\\rangle\\big(\\langle00|10\\rangle\\big) + |01\\rangle\\big(\\langle01|10\\rangle\\big) + |10\\rangle\\big(\\langle11|10\\rangle\\big) + |11\\rangle\\big(\\langle10|10\\rangle\\big) =$$\n$$=|00\\rangle(0) + |01\\rangle(0) + |10\\rangle(0) + |11\\rangle(1) = |11\\rangle$$</p>\n<blockquote>\n<p>Notice how a lot of the inner product terms turn out to equal 0, and our expression is easily simplified. We have expressed the $CNOT$ gate in terms of outer product of computational basis states, which are orthonormal, and apply it to another computational basis state, so the individual inner products are going to always be 0 or 1.</p>\n</blockquote>\n<p>In general case, a $4\\times4$ matrix that describes a 2-qubit gate\n$$A =\n\\begin{bmatrix}\n a_{00} & a_{01} & a_{02} & a_{03} \\\\ \n a_{10} & a_{11} & a_{12} & a_{13} \\\\ \n a_{20} & a_{21} & a_{22} & a_{23} \\\\ \n a_{30} & a_{31} & a_{32} & a_{33} \\\\ \n\\end{bmatrix}\n$$</p>\n<p>will have the following ket-bra representation:\n$$A =$$\n$$=a_{00} |00\\rangle\\langle00| + a_{01} |00\\rangle\\langle01| + a_{02} |00\\rangle\\langle10| + a_{03} |00\\rangle\\langle11| +$$\n$$=a_{10} |01\\rangle\\langle00| + a_{11} |01\\rangle\\langle01| + a_{12} |01\\rangle\\langle10| + a_{13} |01\\rangle\\langle11| +$$\n$$=a_{20} |10\\rangle\\langle00| + a_{21} |10\\rangle\\langle01| + a_{22} |10\\rangle\\langle10| + a_{23} |10\\rangle\\langle11| +$$\n$$=a_{30} |11\\rangle\\langle00| + a_{31} |11\\rangle\\langle01| + a_{32} |11\\rangle\\langle10| + a_{33} |11\\rangle\\langle11|$$</p>\n<p>A similar expression can be extended for matrices that describe $N$-qubit gates, where $N > 2$:</p>\n<p>$$A = \\sum_{i=0}^{2^N-1} \\sum_{j=0}^{2^N-1} a_{ij} |i\\rangle\\langle j|$$</p>\n<p>Dirac notation is particularly useful for expressing sparse matrices - matrices that have few non-zero elements. Indeed, consider the $CNOT$ gate again: it is a $4 \\times 4$ matrix described with 16 elements, but its Dirac notation has only 4 terms, one for each non-zero element of the matrix.</p>\n<p>With enough practice you'll be able to perform computations in Dirac notation without spelling out all the bra-ket terms explicitly!</p>\n",
|
|
892
|
+
"asMarkdown": "\nSame as in the case of single-qubit gates, we can represent multi-qubit gates using Dirac notation.\n\n> Recall that kets represent column vectors and bras represent row vectors. For any ket $|\\psi\\rangle$, the corresponding bra is its adjoint (conjugate transpose): $\\langle\\psi| = |\\psi\\rangle^\\dagger$.\n>\n> Kets and bras are used to express inner and outer products. The inner product of $|\\phi\\rangle$ and $|\\psi\\rangle$ is the matrix product of $\\langle\\phi|$ and $|\\psi\\rangle$, denoted as $\\langle\\phi|\\psi\\rangle$, and their outer product is the matrix product of $|\\phi\\rangle$ and $\\langle\\psi|$, denoted as $|\\phi\\rangle\\langle\\psi|$.\n>\n> As we've seen in the Single-Qubit Gates kata, kets and bras can be used to represent matrices. The outer product of two vectors of the same size produces a square matrix. We can use a linear combination of several outer products of simple vectors (such as basis vectors) to express any square matrix.\n\nLet's consider ket-bra representation of the $CNOT$ gate:\n\n$$CNOT =$$\n$$= |00\\rangle\\langle00| + |01\\rangle\\langle01| + |10\\rangle\\langle11| + |11\\rangle\\langle10| =$$\n$$=\n\\begin{bmatrix} 1 \\\\\\ 0 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix}\\begin{bmatrix} 1 & 0 & 0 & 0 \\end{bmatrix} +\n\\begin{bmatrix} 0 \\\\\\ 1 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix}\\begin{bmatrix} 0 & 1 & 0 & 0 \\end{bmatrix} +\n\\begin{bmatrix} 0 \\\\\\ 0 \\\\\\ 1 \\\\\\ 0 \\end{bmatrix}\\begin{bmatrix} 0 & 0 & 0 & 1 \\end{bmatrix} +\n\\begin{bmatrix} 0 \\\\\\ 0 \\\\\\ 0 \\\\\\ 1 \\end{bmatrix}\\begin{bmatrix} 0 & 0 & 1 & 0 \\end{bmatrix} =\n$$\n$$=\n\\begin{bmatrix} 1 & 0 & 0 & 0 \\\\\\ 0 & 0 & 0 & 0 \\\\\\ 0 & 0 & 0 & 0 \\\\\\ 0 & 0 & 0 & 0 \\\\\\ \\end{bmatrix} +\n\\begin{bmatrix} 0 & 0 & 0 & 0 \\\\\\ 0 & 1 & 0 & 0 \\\\\\ 0 & 0 & 0 & 0 \\\\\\ 0 & 0 & 0 & 0 \\\\\\ \\end{bmatrix} +\n\\begin{bmatrix} 0 & 0 & 0 & 0 \\\\\\ 0 & 0 & 0 & 0 \\\\\\ 0 & 0 & 0 & 1 \\\\\\ 0 & 0 & 0 & 0 \\\\\\ \\end{bmatrix} +\n\\begin{bmatrix} 0 & 0 & 0 & 0 \\\\\\ 0 & 0 & 0 & 0 \\\\\\ 0 & 0 & 0 & 0 \\\\\\ 0 & 0 & 1 & 0 \\\\\\ \\end{bmatrix} =\n$$\n$$=\\begin{bmatrix} 1 & 0 & 0 & 0 \\\\\\ 0 & 1 & 0 & 0 \\\\\\ 0 & 0 & 0 & 1 \\\\\\ 0 & 0 & 1 & 0 \\\\\\ \\end{bmatrix}$$\n\nThis representation can be used to carry out calculations in Dirac notation without ever switching back to matrix representation:\n\n$$\nCNOT|10\\rangle =\n\\big(|00\\rangle\\langle00| + |01\\rangle\\langle01| + |10\\rangle\\langle11| + |11\\rangle\\langle10|\\big)|10\\rangle =$$\n$$=|00\\rangle\\langle00|10\\rangle + |01\\rangle\\langle01|10\\rangle + |10\\rangle\\langle11|10\\rangle + |11\\rangle\\langle10|10\\rangle =$$\n$$=|00\\rangle\\big(\\langle00|10\\rangle\\big) + |01\\rangle\\big(\\langle01|10\\rangle\\big) + |10\\rangle\\big(\\langle11|10\\rangle\\big) + |11\\rangle\\big(\\langle10|10\\rangle\\big) =$$\n$$=|00\\rangle(0) + |01\\rangle(0) + |10\\rangle(0) + |11\\rangle(1) = |11\\rangle$$\n\n> Notice how a lot of the inner product terms turn out to equal 0, and our expression is easily simplified. We have expressed the $CNOT$ gate in terms of outer product of computational basis states, which are orthonormal, and apply it to another computational basis state, so the individual inner products are going to always be 0 or 1.\n\nIn general case, a $4\\\\times4$ matrix that describes a 2-qubit gate\n$$A =\n\\begin{bmatrix}\n a_{00} & a_{01} & a_{02} & a_{03} \\\\\\ \n a_{10} & a_{11} & a_{12} & a_{13} \\\\\\ \n a_{20} & a_{21} & a_{22} & a_{23} \\\\\\ \n a_{30} & a_{31} & a_{32} & a_{33} \\\\\\ \n\\end{bmatrix}\n$$\n\nwill have the following ket-bra representation:\n$$A =$$\n$$=a_{00} |00\\rangle\\langle00| + a_{01} |00\\rangle\\langle01| + a_{02} |00\\rangle\\langle10| + a_{03} |00\\rangle\\langle11| +$$\n$$=a_{10} |01\\rangle\\langle00| + a_{11} |01\\rangle\\langle01| + a_{12} |01\\rangle\\langle10| + a_{13} |01\\rangle\\langle11| +$$\n$$=a_{20} |10\\rangle\\langle00| + a_{21} |10\\rangle\\langle01| + a_{22} |10\\rangle\\langle10| + a_{23} |10\\rangle\\langle11| +$$\n$$=a_{30} |11\\rangle\\langle00| + a_{31} |11\\rangle\\langle01| + a_{32} |11\\rangle\\langle10| + a_{33} |11\\rangle\\langle11|$$\n\nA similar expression can be extended for matrices that describe $N$-qubit gates, where $N > 2$:\n\n$$A = \\sum_{i=0}^{2^N-1} \\sum_{j=0}^{2^N-1} a_{ij} |i\\rangle\\langle j|$$\n\nDirac notation is particularly useful for expressing sparse matrices - matrices that have few non-zero elements. Indeed, consider the $CNOT$ gate again: it is a $4 \\times 4$ matrix described with 16 elements, but its Dirac notation has only 4 terms, one for each non-zero element of the matrix.\n\nWith enough practice you'll be able to perform computations in Dirac notation without spelling out all the bra-ket terms explicitly!"
|
|
893
893
|
}
|
|
894
894
|
]
|
|
895
895
|
},
|
|
@@ -900,8 +900,8 @@ export default {
|
|
|
900
900
|
"items": [
|
|
901
901
|
{
|
|
902
902
|
"type": "text-content",
|
|
903
|
-
"asHtml": "<p>This section describes a more formal process of finding the ket-bra decompositions of multi-qubit quantum gates.\nThis section is not necessary to start working with quantum gates, so feel free to skip it for now, and come back to it later.</p>\n<p>You can use the properties of eigenvalues and eigenvectors to find the ket-bra decomposition of any gate. Consider an $N$-qubit gate $A$; the matrix representation of the gate is a square matrix of size $2^N$. Therefore it also has $2^N$ orthogonal eigenvectors $|\\psi_i\\rangle$</p>\n<p>$$A|\\psi_i\\rangle = x_i|\\psi_i\\rangle, 0 \\leq i \\leq 2^N -1$$</p>\n<p>Then its ket-bra decomposition is:</p>\n<p>$$A = \\sum_{i=0}^{2^N-1} x_i|\\psi_i\\rangle\\langle\\psi_i|$$</p>\n<p>Let's use our
|
|
904
|
-
"asMarkdown": "\nThis section describes a more formal process of finding the ket-bra decompositions of multi-qubit quantum gates.\nThis section is not necessary to start working with quantum gates, so feel free to skip it for now, and come back to it later.\n\nYou can use the properties of eigenvalues and eigenvectors to find the ket-bra decomposition of any gate. Consider an $N$-qubit gate $A$; the matrix representation of the gate is a square matrix of size $2^N$. Therefore it also has $2^N$ orthogonal eigenvectors $|\\psi_i\\rangle$\n\n$$A|\\psi_i\\rangle = x_i|\\psi_i\\rangle, 0 \\leq i \\leq 2^N -1$$\n\nThen its ket-bra decomposition is:\n\n$$A = \\sum_{i=0}^{2^N-1} x_i|\\psi_i\\rangle\\langle\\psi_i|$$\n\nLet's use our
|
|
903
|
+
"asHtml": "<p>This section describes a more formal process of finding the ket-bra decompositions of multi-qubit quantum gates.\nThis section is not necessary to start working with quantum gates, so feel free to skip it for now, and come back to it later.</p>\n<p>You can use the properties of eigenvalues and eigenvectors to find the ket-bra decomposition of any gate. Consider an $N$-qubit gate $A$; the matrix representation of the gate is a square matrix of size $2^N$. Therefore it also has $2^N$ orthogonal eigenvectors $|\\psi_i\\rangle$</p>\n<p>$$A|\\psi_i\\rangle = x_i|\\psi_i\\rangle, 0 \\leq i \\leq 2^N -1$$</p>\n<p>Then its ket-bra decomposition is:</p>\n<p>$$A = \\sum_{i=0}^{2^N-1} x_i|\\psi_i\\rangle\\langle\\psi_i|$$</p>\n<p>Let's use our $CNOT$ gate as a simple example.\nThe $CNOT$ gate has four eigenvectors.</p>\n<ul>\n<li>Two, as we can clearly see, are computational basis states $|00\\rangle$ and $|01\\rangle$ with eigen values $1$ and $1$, respectively (the basis states that are not affected by the gate).</li>\n<li>The other two are $|1\\rangle \\otimes |+\\rangle = \\frac{1}{\\sqrt{2}}\\big(|10\\rangle + |11\\rangle\\big)$ and $|1\\rangle \\otimes |-\\rangle = \\frac{1}{\\sqrt{2}}\\big(|10\\rangle - |11\\rangle\\big)$ with eigenvalues $1$ and $-1$, respectively:</li>\n</ul>\n<p>$$CNOT|00\\rangle = |00\\rangle$$\n$$CNOT|01\\rangle = |01\\rangle$$\n$$CNOT|1+\\rangle = |1+\\rangle$$\n$$CNOT|1-\\rangle = -|1-\\rangle$$</p>\n<p>Here's what the decomposition looks like:</p>\n<p>$$CNOT =$$\n$$=|00\\rangle\\langle00| + |01\\rangle\\langle01| + |1+\\rangle\\langle1+| - |1-\\rangle\\langle1-| =$$\n$$=|00\\rangle\\langle00| + |01\\rangle\\langle01| + \\frac{1}{2}\\big[\\big(|10\\rangle + |11\\rangle\\big)\\big(\\langle10| + \\langle11|\\big) - \\big(|10\\rangle - |11\\rangle\\big)\\big(\\langle10| - \\langle11|\\big)\\big] =$$\n$$=|00\\rangle\\langle00| + |01\\rangle\\langle01| + \\frac{1}{2}\\big(|10\\rangle\\langle10| + |10\\rangle\\langle11| + |11\\rangle\\langle10| + |11\\rangle\\langle11| - |10\\rangle\\langle10| + |10\\rangle\\langle11| + |11\\rangle\\langle10| - |11\\rangle\\langle11|\\big) =$$\n$$=|00\\rangle\\langle00| + |01\\rangle\\langle01| + \\frac{1}{2}\\big(2|10\\rangle\\langle11| + 2|11\\rangle\\langle10|\\big) =$$\n$$=|00\\rangle\\langle00| + |01\\rangle\\langle01| + |10\\rangle\\langle11| + |11\\rangle\\langle10|$$</p>\n",
|
|
904
|
+
"asMarkdown": "\nThis section describes a more formal process of finding the ket-bra decompositions of multi-qubit quantum gates.\nThis section is not necessary to start working with quantum gates, so feel free to skip it for now, and come back to it later.\n\nYou can use the properties of eigenvalues and eigenvectors to find the ket-bra decomposition of any gate. Consider an $N$-qubit gate $A$; the matrix representation of the gate is a square matrix of size $2^N$. Therefore it also has $2^N$ orthogonal eigenvectors $|\\psi_i\\rangle$\n\n$$A|\\psi_i\\rangle = x_i|\\psi_i\\rangle, 0 \\leq i \\leq 2^N -1$$\n\nThen its ket-bra decomposition is:\n\n$$A = \\sum_{i=0}^{2^N-1} x_i|\\psi_i\\rangle\\langle\\psi_i|$$\n\nLet's use our $CNOT$ gate as a simple example.\nThe $CNOT$ gate has four eigenvectors.\n\n- Two, as we can clearly see, are computational basis states $|00\\rangle$ and $|01\\rangle$ with eigen values $1$ and $1$, respectively (the basis states that are not affected by the gate).\n- The other two are $|1\\rangle \\otimes |+\\rangle = \\frac{1}{\\sqrt{2}}\\big(|10\\rangle + |11\\rangle\\big)$ and $|1\\rangle \\otimes |-\\rangle = \\frac{1}{\\sqrt{2}}\\big(|10\\rangle - |11\\rangle\\big)$ with eigenvalues $1$ and $-1$, respectively:\n\n$$CNOT|00\\rangle = |00\\rangle$$\n$$CNOT|01\\rangle = |01\\rangle$$\n$$CNOT|1+\\rangle = |1+\\rangle$$\n$$CNOT|1-\\rangle = -|1-\\rangle$$\n\nHere's what the decomposition looks like:\n\n$$CNOT =$$\n$$=|00\\rangle\\langle00| + |01\\rangle\\langle01| + |1+\\rangle\\langle1+| - |1-\\rangle\\langle1-| =$$\n$$=|00\\rangle\\langle00| + |01\\rangle\\langle01| + \\frac{1}{2}\\big[\\big(|10\\rangle + |11\\rangle\\big)\\big(\\langle10| + \\langle11|\\big) - \\big(|10\\rangle - |11\\rangle\\big)\\big(\\langle10| - \\langle11|\\big)\\big] =$$\n$$=|00\\rangle\\langle00| + |01\\rangle\\langle01| + \\frac{1}{2}\\big(|10\\rangle\\langle10| + |10\\rangle\\langle11| + |11\\rangle\\langle10| + |11\\rangle\\langle11| - |10\\rangle\\langle10| + |10\\rangle\\langle11| + |11\\rangle\\langle10| - |11\\rangle\\langle11|\\big) =$$\n$$=|00\\rangle\\langle00| + |01\\rangle\\langle01| + \\frac{1}{2}\\big(2|10\\rangle\\langle11| + 2|11\\rangle\\langle10|\\big) =$$\n$$=|00\\rangle\\langle00| + |01\\rangle\\langle01| + |10\\rangle\\langle11| + |11\\rangle\\langle10|$$"
|
|
905
905
|
}
|
|
906
906
|
]
|
|
907
907
|
},
|
|
@@ -912,8 +912,8 @@ export default {
|
|
|
912
912
|
"items": [
|
|
913
913
|
{
|
|
914
914
|
"type": "text-content",
|
|
915
|
-
"asHtml": "<p>The
|
|
916
|
-
"asMarkdown": "\nThe
|
|
915
|
+
"asHtml": "<p>The $SWAP$ gate acts on two qubits, and, as the name implies, swaps their quantum states.</p>\n<table>\n <tr>\n <th>Gate</th>\n <th>Matrix</th>\n <th>Applying to $|\\psi\\rangle = \\alpha|00\\rangle + \\beta|01\\rangle + \\gamma|10\\rangle + \\delta|11\\rangle$</th>\n <th>Applying to basis states</th>\n </tr>\n <tr>\n <td>$SWAP$</td>\n <td>$\\begin{bmatrix} 1 & 0 & 0 & 0 \\\\\\ 0 & 0 & 1 & 0 \\\\\\ 0 & 1 & 0 & 0 \\\\\\ 0 & 0 & 0 & 1 \\end{bmatrix}$</td>\n <td>$SWAP|\\psi\\rangle = \\alpha|00\\rangle + \\gamma|01\\rangle + \\beta|10\\rangle + \\delta|11\\rangle$</td>\n <td>\n $$SWAP|00\\rangle = |00\\rangle$$\n $$SWAP|01\\rangle = |10\\rangle$$\n $$SWAP|10\\rangle = |01\\rangle$$\n $$SWAP|11\\rangle = |11\\rangle$$\n </td>\n </tr>\n</table>",
|
|
916
|
+
"asMarkdown": "\nThe $SWAP$ gate acts on two qubits, and, as the name implies, swaps their quantum states.\n\n<table>\n <tr>\n <th>Gate</th>\n <th>Matrix</th>\n <th>Applying to $|\\psi\\rangle = \\alpha|00\\rangle + \\beta|01\\rangle + \\gamma|10\\rangle + \\delta|11\\rangle$</th>\n <th>Applying to basis states</th>\n </tr>\n <tr>\n <td>$SWAP$</td>\n <td>$\\begin{bmatrix} 1 & 0 & 0 & 0 \\\\\\ 0 & 0 & 1 & 0 \\\\\\ 0 & 1 & 0 & 0 \\\\\\ 0 & 0 & 0 & 1 \\end{bmatrix}$</td>\n <td>$SWAP|\\psi\\rangle = \\alpha|00\\rangle + \\gamma|01\\rangle + \\beta|10\\rangle + \\delta|11\\rangle$</td>\n <td>\n $$SWAP|00\\rangle = |00\\rangle$$\n $$SWAP|01\\rangle = |10\\rangle$$\n $$SWAP|10\\rangle = |01\\rangle$$\n $$SWAP|11\\rangle = |11\\rangle$$\n </td>\n </tr>\n</table>"
|
|
917
917
|
}
|
|
918
918
|
]
|
|
919
919
|
},
|
|
@@ -927,8 +927,8 @@ export default {
|
|
|
927
927
|
"asMarkdown": "**Inputs:**\n\n1. $N$ qubits in an arbitrary state $|\\\\psi\\\\rangle$, stored in an array of length $N$.\n2. Integers `index1` and `index2` such that $0 \\le \\text{index1} < \\text{index2} \\le N - 1$.\n\n**Goal:** Swap the states of the qubits at the given indices."
|
|
928
928
|
},
|
|
929
929
|
"sourceIds": [
|
|
930
|
-
"
|
|
931
|
-
"
|
|
930
|
+
"multi_qubit_gates__qubit_swap__Verification.qs",
|
|
931
|
+
"KatasLibrary.qs"
|
|
932
932
|
],
|
|
933
933
|
"placeholderCode": "namespace Kata {\n operation QubitSwap (qs : Qubit[], index1 : Int, index2 : Int)\n : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}",
|
|
934
934
|
"explainedSolution": {
|
|
@@ -936,8 +936,8 @@ export default {
|
|
|
936
936
|
"items": [
|
|
937
937
|
{
|
|
938
938
|
"type": "text-content",
|
|
939
|
-
"asHtml": "<p>The SWAP gate allows us to swap the state of any two qubits. In this exercise you need to apply it to two qubits given by their indices in an array of qubits; you can access individual qubits of the array as <code>qs[index of a qubit]</code>.</p>\n",
|
|
940
|
-
"asMarkdown": "\nThe SWAP gate allows us to swap the state of any two qubits. In this exercise you need to apply it to two qubits given by their indices in an array of qubits; you can access individual qubits of the array as `qs[index of a qubit]`."
|
|
939
|
+
"asHtml": "<p>The $SWAP$ gate allows us to swap the state of any two qubits. In this exercise you need to apply it to two qubits given by their indices in an array of qubits; you can access individual qubits of the array as <code>qs[index of a qubit]</code>.</p>\n",
|
|
940
|
+
"asMarkdown": "\nThe $SWAP$ gate allows us to swap the state of any two qubits. In this exercise you need to apply it to two qubits given by their indices in an array of qubits; you can access individual qubits of the array as `qs[index of a qubit]`."
|
|
941
941
|
},
|
|
942
942
|
{
|
|
943
943
|
"type": "solution",
|
|
@@ -954,8 +954,8 @@ export default {
|
|
|
954
954
|
"items": [
|
|
955
955
|
{
|
|
956
956
|
"type": "text-content",
|
|
957
|
-
"asHtml": "<p>In the above examples the
|
|
958
|
-
"asMarkdown": "\nIn the above examples the
|
|
957
|
+
"asHtml": "<p>In the above examples the $CNOT$ gate acted on two adjacent qubits. However, multi-qubit gates can act on non-adjacent qubits as well. Let's see how to work out the math of the system state change in this case.</p>\n<p>Take 3 qubits in an arbitrary state $|\\psi\\rangle = x_{000} |000\\rangle + x_{001}|001\\rangle + x_{010}|010\\rangle + x_{011}|011\\rangle + x_{100}|100\\rangle + x_{101}|101\\rangle + x_{110}|110\\rangle + x_{111}|111\\rangle $.</p>\n<p>We can apply the $CNOT$ gate on 1st and 3rd qubits, with the 1st qubit as control and the 3rd qubit as target. Let's label the 3-qubit gate that describes the effect of this on the whole system as $CINOT$. The $CINOT$ ignores the 2nd qubit (leaves it unchanged) and applies the $CNOT$ gate as specified above.</p>\n<h2>Q#</h2>\n<p>In Q# we describe the operation as the sequence of gates that are applied to the qubits, regardless of whether the qubits are adjacent or not.</p>\n<pre><code class=\"language-qsharp\">operation CINOT (qs: Qubit[]) : Unit {\n CNOT(qs[0], qs[2]); // Length of qs is assumed to be 3\n}\n</code></pre>\n<h2>Dirac Notation</h2>\n<p>In Dirac notation we can consider the effect of the gate on each basis vector separately: each basis vector $|a_1a_2a_3\\rangle$ remains unchanged if $a_1 = 0$, and becomes $|a_1a_2(\\neg a_3)\\rangle$ if $a_1 = 1$. The full effect on the state becomes:</p>\n<p>$$CINOT|\\psi\\rangle = x_{000} CINOT|000\\rangle + x_{001} CINOT|001\\rangle + x_{010} CINOT|010\\rangle + x_{011} CINOT|011\\rangle+$$\n$$+x_{100} CINOT|100\\rangle + x_{101} CINOT|101\\rangle + x_{110} CINOT|110\\rangle + x_{111} CINOT|111\\rangle =$$\n$$= x_{000}|000\\rangle + x_{001}|001\\rangle + x_{010}|010\\rangle + x_{011}|011\\rangle + x_{101}|100\\rangle + x_{100}|101\\rangle + x_{111}|110\\rangle + x_{110}|111\\rangle $$</p>\n<h2>Matrix Form</h2>\n<p>$CINOT$ can also be represented in matrix form as a $2^3 \\times 2^3$ matrix:\n$$\n\\begin{bmatrix}\n 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\\\ \n 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\\\ \n 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\\\ \n 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\\\ \n 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\\\ \n 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\\\ \n 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\\\ \n 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\n\\end{bmatrix}\n$$</p>\n<p>Applying $CINOT$ to $|\\psi\\rangle$ gives us\n$$\nCINOT \\begin{bmatrix}\n 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\\\ \n 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\\\ \n 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\\\ \n 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\\\ \n 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\\\ \n 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\\\ \n 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\\\ \n 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\n\\end{bmatrix}\n\\begin{bmatrix}\n x_{000} \\\\ x_{001} \\\\ x_{010} \\\\ x_{011} \\\\ x_{100} \\\\ x_{101} \\\\ x_{110} \\\\ x_{111}\n\\end{bmatrix} =\n\\begin{bmatrix}\n x_{000} \\\\ x_{001} \\\\ x_{010} \\\\ x_{011} \\\\ x_{101} \\\\ x_{100} \\\\ x_{111} \\\\ x_{110}\n\\end{bmatrix}\n$$</p>\n<p>However, as $N$ gets larger, creating a full size matrix can be extremely unwieldy. To express the matrix without spelling out its elements, we can use the following trick:</p>\n<ol>\n<li>Apply the $SWAP$ gate on the 1st and 2nd qubits.\nThis will bring the qubits on which the $CNOT$ gate acts next to each other, without any extra qubits between them.</li>\n<li>Apply the $CNOT$ on 2nd and 3rd qubits.\nSince now the gate acts on adjacent qubits, this can be represented as a tensor product of the gate we're applying and <code>I</code> gates.</li>\n<li>Apply the $SWAP$ gate on the 1st and 2nd qubits again.</li>\n</ol>\n<p>These can be represented as applying the following gates on the 3 qubits.</p>\n<ol>\n<li>$SWAP \\otimes I$</li>\n</ol>\n<p>$$\nx_{000}|000\\rangle + x_{001}|001\\rangle + x_{100}|010\\rangle + x_{101}|011\\rangle +\nx_{010}|100\\rangle + x_{011}|101\\rangle + x_{110}|110\\rangle + x_{111}|111\\rangle\n$$</p>\n<ol start=\"2\">\n<li>$I \\otimes CNOT$</li>\n</ol>\n<p>$$\nx_{000}|000\\rangle + x_{001}|001\\rangle + x_{101}|010\\rangle + x_{100}|011\\rangle +\nx_{010}|100\\rangle + x_{011}|101\\rangle + x_{111}|110\\rangle + x_{110}|111\\rangle\n$$</p>\n<ol start=\"3\">\n<li>$SWAP \\otimes I$</li>\n</ol>\n<p>$$\nx_{000}|000\\rangle + x_{001}|001\\rangle + x_{010}|010\\rangle + x_{011}|011\\rangle +\nx_{101}|100\\rangle + x_{100}|101\\rangle + x_{111}|110\\rangle + x_{110}|111\\rangle\n$$</p>\n<p>The result is the the $CINOT$ gate as we intended; so we can write</p>\n<p>$$CINOT = (SWAP \\otimes I)(I \\otimes CNOT)(SWAP \\otimes I)$$</p>\n<blockquote>\n<p>Note that in matrix notation we always apply a gate to the complete system, so we must apply $SWAP \\otimes I$, spelling the identity gate explicitly.\nHowever, when implementing the unitary $SWAP \\otimes I$ in Q#, we need only to call <code>SWAP(qs[0], qs[1])</code> - the remaining qubit <code>qs[2]</code> will not change, which is equivalent to applying an implicit identity gate.</p>\n<p>We can also spell out all gates applied explicitly (this makes for a much longer code, though):</p>\n<pre><code class=\"language-qsharp\">operation CINOT (qs: Qubit[]) : Unit {\n // First step\n SWAP(qs[0], qs[1]);\n I(qs[2]);\n // Second step\n I(qs[0]);\n CNOT(qs[1], qs[2]);\n // Third step\n SWAP(qs[0], qs[1]);\n I(qs[2]);\n}\n</code></pre>\n</blockquote>\n",
|
|
958
|
+
"asMarkdown": "\nIn the above examples the $CNOT$ gate acted on two adjacent qubits. However, multi-qubit gates can act on non-adjacent qubits as well. Let's see how to work out the math of the system state change in this case.\n\nTake 3 qubits in an arbitrary state $|\\psi\\rangle = x_{000} |000\\rangle + x_{001}|001\\rangle + x_{010}|010\\rangle + x_{011}|011\\rangle + x_{100}|100\\rangle + x_{101}|101\\rangle + x_{110}|110\\rangle + x_{111}|111\\rangle $.\n\nWe can apply the $CNOT$ gate on 1st and 3rd qubits, with the 1st qubit as control and the 3rd qubit as target. Let's label the 3-qubit gate that describes the effect of this on the whole system as $CINOT$. The $CINOT$ ignores the 2nd qubit (leaves it unchanged) and applies the $CNOT$ gate as specified above.\n\n## Q#\n\nIn Q# we describe the operation as the sequence of gates that are applied to the qubits, regardless of whether the qubits are adjacent or not.\n\n```qsharp\noperation CINOT (qs: Qubit[]) : Unit {\n CNOT(qs[0], qs[2]); // Length of qs is assumed to be 3\n}\n```\n\n## Dirac Notation\n\nIn Dirac notation we can consider the effect of the gate on each basis vector separately: each basis vector $|a_1a_2a_3\\rangle$ remains unchanged if $a_1 = 0$, and becomes $|a_1a_2(\\neg a_3)\\rangle$ if $a_1 = 1$. The full effect on the state becomes:\n\n$$CINOT|\\psi\\rangle = x_{000} CINOT|000\\rangle + x_{001} CINOT|001\\rangle + x_{010} CINOT|010\\rangle + x_{011} CINOT|011\\rangle+$$\n$$+x_{100} CINOT|100\\rangle + x_{101} CINOT|101\\rangle + x_{110} CINOT|110\\rangle + x_{111} CINOT|111\\rangle =$$\n$$= x_{000}|000\\rangle + x_{001}|001\\rangle + x_{010}|010\\rangle + x_{011}|011\\rangle + x_{101}|100\\rangle + x_{100}|101\\rangle + x_{111}|110\\rangle + x_{110}|111\\rangle $$\n\n## Matrix Form\n\n$CINOT$ can also be represented in matrix form as a $2^3 \\times 2^3$ matrix:\n$$\n\\begin{bmatrix}\n 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\\\\\ \n 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\\\\\ \n 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\\\\\ \n 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\\\\\ \n 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\\\\\ \n 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\\\\\ \n 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\\\\\ \n 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\n\\end{bmatrix}\n$$\n\nApplying $CINOT$ to $|\\psi\\rangle$ gives us\n$$\nCINOT \\begin{bmatrix}\n 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\\\\\ \n 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\\\\\ \n 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\\\\\ \n 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\\\\\ \n 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\\\\\ \n 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\\\\\ \n 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\\\\\ \n 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\n\\end{bmatrix}\n\\begin{bmatrix}\n x_{000} \\\\\\ x_{001} \\\\\\ x_{010} \\\\\\ x_{011} \\\\\\ x_{100} \\\\\\ x_{101} \\\\\\ x_{110} \\\\\\ x_{111}\n\\end{bmatrix} =\n\\begin{bmatrix}\n x_{000} \\\\\\ x_{001} \\\\\\ x_{010} \\\\\\ x_{011} \\\\\\ x_{101} \\\\\\ x_{100} \\\\\\ x_{111} \\\\\\ x_{110}\n\\end{bmatrix}\n$$\n\nHowever, as $N$ gets larger, creating a full size matrix can be extremely unwieldy. To express the matrix without spelling out its elements, we can use the following trick:\n\n1. Apply the $SWAP$ gate on the 1st and 2nd qubits.\n This will bring the qubits on which the $CNOT$ gate acts next to each other, without any extra qubits between them.\n2. Apply the $CNOT$ on 2nd and 3rd qubits.\n Since now the gate acts on adjacent qubits, this can be represented as a tensor product of the gate we're applying and `I` gates.\n3. Apply the $SWAP$ gate on the 1st and 2nd qubits again.\n\nThese can be represented as applying the following gates on the 3 qubits.\n\n1. $SWAP \\otimes I$\n\n$$\nx_{000}|000\\rangle + x_{001}|001\\rangle + x_{100}|010\\rangle + x_{101}|011\\rangle +\nx_{010}|100\\rangle + x_{011}|101\\rangle + x_{110}|110\\rangle + x_{111}|111\\rangle\n$$\n\n2. $I \\otimes CNOT$\n\n$$\nx_{000}|000\\rangle + x_{001}|001\\rangle + x_{101}|010\\rangle + x_{100}|011\\rangle +\nx_{010}|100\\\\rangle + x_{011}|101\\rangle + x_{111}|110\\rangle + x_{110}|111\\rangle\n$$\n\n3. $SWAP \\otimes I$\n\n$$\nx_{000}|000\\rangle + x_{001}|001\\rangle + x_{010}|010\\rangle + x_{011}|011\\rangle +\nx_{101}|100\\rangle + x_{100}|101\\rangle + x_{111}|110\\rangle + x_{110}|111\\rangle\n$$\n\nThe result is the the $CINOT$ gate as we intended; so we can write\n\n$$CINOT = (SWAP \\otimes I)(I \\otimes CNOT)(SWAP \\otimes I)$$\n\n> Note that in matrix notation we always apply a gate to the complete system, so we must apply $SWAP \\otimes I$, spelling the identity gate explicitly.\n> However, when implementing the unitary $SWAP \\otimes I$ in Q#, we need only to call `SWAP(qs[0], qs[1])` - the remaining qubit `qs[2]` will not change, which is equivalent to applying an implicit identity gate.\n>\n> We can also spell out all gates applied explicitly (this makes for a much longer code, though):\n>\n> ```qsharp\noperation CINOT (qs: Qubit[]) : Unit {\n // First step\n SWAP(qs[0], qs[1]);\n I(qs[2]);\n // Second step\n I(qs[0]);\n CNOT(qs[1], qs[2]);\n // Third step\n SWAP(qs[0], qs[1]);\n I(qs[2]);\n}\n```"
|
|
959
959
|
}
|
|
960
960
|
]
|
|
961
961
|
},
|
|
@@ -966,8 +966,8 @@ export default {
|
|
|
966
966
|
"items": [
|
|
967
967
|
{
|
|
968
968
|
"type": "text-content",
|
|
969
|
-
"asHtml": "<p><strong>Controlled gates</strong> are a class of gates derived from other gates as follows: they act on a control qubit and a target qubit, just like the
|
|
970
|
-
"asMarkdown": "\n**Controlled gates** are a class of gates derived from other gates as follows: they act on a control qubit and a target qubit, just like the
|
|
969
|
+
"asHtml": "<p><strong>Controlled gates</strong> are a class of gates derived from other gates as follows: they act on a control qubit and a target qubit, just like the $CNOT$ gate.\nA controlled-$U$ gate applies the $U$ gate to the target qubit if the control qubit is in state $|1\\rangle$, and does nothing otherwise.</p>\n<p>Given a gate $U = \\begin{bmatrix} \\alpha & \\beta \\\\ \\gamma & \\delta \\end{bmatrix}$, its controlled version looks like this:</p>\n<table>\n <tr>\n <th>Gate</th>\n <th>Matrix</th>\n <th>Q# Documentation</th>\n </tr>\n <tr>\n <td>$\\text{Controlled }U$</td>\n <td>\n $$\n \\begin{bmatrix}\n 1 & 0 & 0 & 0 \\\\\\ \n 0 & 1 & 0 & 0 \\\\\\ \n 0 & 0 & \\alpha & \\beta \\\\\\ \n 0 & 0 & \\gamma & \\delta\n \\end{bmatrix}\n $$\n </td>\n <td><a href=\"https://learn.microsoft.com/azure/quantum/user-guide/language/expressions/functorapplication#controlled-functor\" target=\"_blank\">Controlled functor</a></td>\n </tr>\n</table>\n\n<blockquote>\n<p>The $CNOT$ gate is en example of a controlled gate, which is why it is also known as the controlled $NOT$ or controlled $X$ gate.</p>\n</blockquote>\n<p>The concept of controlled gates can be generalized beyond controlling single-qubit gates.\nFor any multi-qubit gate, its controlled version will have an identity matrix in the top left quadrant, the gate itself in the bottom right, and $0$ everywhere else.\nHere, for example, is the Controlled $SWAP$, or <strong>Fredkin gate</strong>:</p>\n<p>$$\n\\begin{bmatrix}\n 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\\\ \n 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\\\ \n 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\\\ \n 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\\\ \n 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\\\ \n 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\\\ \n 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\\\ \n 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\n\\end{bmatrix}\n$$</p>\n<p>In Q#, controlled gates are applied using the <a href=\"https://learn.microsoft.com/azure/quantum/user-guide/language/expressions/functorapplication#controlled-functor\" target=\"_blank\"><code>Controlled</code></a> functor.\nThe controlled version of a gate accepts an array of control qubits (in this case an array of a single qubit), followed by the arguments to the original gate.\nFor example, these two lines are equivalent:</p>\n<pre><code class=\"language-qsharp\">Controlled X([control], target);\nCNOT(control, target);\n</code></pre>\n<p>If the original gate was implemented as an operation with multiple parameters, the controlled version of this gate will take those parameters as a tuple. For example, to apply Fredkin gate, you'd have to call:</p>\n<pre><code class=\"language-qsharp\">Controlled SWAP([control], (q1, q2));\n</code></pre>\n<p>You can use the controlled version of a Q# operation only if that operation has a controlled version defined.\nThe Q# compiler will often be able to generate a controlled version of the operation automatically if you put <code>is Ctl</code> after the operation's return type.\nIn other cases, you'll need to define the controlled version of an operation manually.</p>\n",
|
|
970
|
+
"asMarkdown": "\n**Controlled gates** are a class of gates derived from other gates as follows: they act on a control qubit and a target qubit, just like the $CNOT$ gate.\nA controlled-$U$ gate applies the $U$ gate to the target qubit if the control qubit is in state $|1\\rangle$, and does nothing otherwise.\n\nGiven a gate $U = \\begin{bmatrix} \\alpha & \\beta \\\\\\ \\gamma & \\delta \\end{bmatrix}$, its controlled version looks like this:\n\n<table>\n <tr>\n <th>Gate</th>\n <th>Matrix</th>\n <th>Q# Documentation</th>\n </tr>\n <tr>\n <td>$\\text{Controlled }U$</td>\n <td>\n $$\n \\begin{bmatrix}\n 1 & 0 & 0 & 0 \\\\\\ \n 0 & 1 & 0 & 0 \\\\\\ \n 0 & 0 & \\alpha & \\beta \\\\\\ \n 0 & 0 & \\gamma & \\delta\n \\end{bmatrix}\n $$\n </td>\n <td><a href=\"https://learn.microsoft.com/azure/quantum/user-guide/language/expressions/functorapplication#controlled-functor\" target=\"_blank\">Controlled functor</a></td>\n </tr>\n</table>\n\n> The $CNOT$ gate is en example of a controlled gate, which is why it is also known as the controlled $NOT$ or controlled $X$ gate.\n\nThe concept of controlled gates can be generalized beyond controlling single-qubit gates.\nFor any multi-qubit gate, its controlled version will have an identity matrix in the top left quadrant, the gate itself in the bottom right, and $0$ everywhere else.\nHere, for example, is the Controlled $SWAP$, or **Fredkin gate**:\n\n$$\n\\begin{bmatrix}\n 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\\\\\ \n 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\\\\\ \n 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\\\\\ \n 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\\\\\ \n 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\\\\\ \n 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\\\\\ \n 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\\\\\ \n 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\n\\end{bmatrix}\n$$\n\nIn Q#, controlled gates are applied using the <a href=\"https://learn.microsoft.com/azure/quantum/user-guide/language/expressions/functorapplication#controlled-functor\" target=\"_blank\">`Controlled`</a> functor.\nThe controlled version of a gate accepts an array of control qubits (in this case an array of a single qubit), followed by the arguments to the original gate.\nFor example, these two lines are equivalent:\n\n```qsharp\nControlled X([control], target);\nCNOT(control, target);\n```\n\nIf the original gate was implemented as an operation with multiple parameters, the controlled version of this gate will take those parameters as a tuple. For example, to apply Fredkin gate, you'd have to call:\n\n```qsharp\nControlled SWAP([control], (q1, q2));\n```\n\nYou can use the controlled version of a Q# operation only if that operation has a controlled version defined.\nThe Q# compiler will often be able to generate a controlled version of the operation automatically if you put `is Ctl` after the operation's return type.\nIn other cases, you'll need to define the controlled version of an operation manually."
|
|
971
971
|
}
|
|
972
972
|
]
|
|
973
973
|
},
|
|
@@ -977,12 +977,12 @@ export default {
|
|
|
977
977
|
"title": "Controlled Rotation",
|
|
978
978
|
"description": {
|
|
979
979
|
"type": "text-content",
|
|
980
|
-
"asHtml": "<p><strong>Inputs:</strong></p>\n<ol>\n<li>Two qubits in an arbitrary state $|\\phi\\rangle$, stored as an array of length 2.</li>\n<li>An angle $\\theta$: $-\\pi < \\theta \\leq \\pi$.</li>\n</ol>\n<p><strong>Goal:</strong> Apply a controlled $R_x$ gate, using the first qubit as control and the second qubit as target, with $\\theta$ as the angle argument for the gate.</p>\n<details>\n <summary><b>Need a hint?</b></summary>\n <p>If you were to apply a regular version of $R_x$ gate, it would take two parameters - angle
|
|
981
|
-
"asMarkdown": "**Inputs:**\n\n1. Two qubits in an arbitrary state $|\\phi\\rangle$, stored as an array of length 2.\n2. An angle $\\theta$: $-\\pi < \\theta \\leq \\pi$.\n\n**Goal:** Apply a controlled $R_x$ gate, using the first qubit as control and the second qubit as target, with $\\theta$ as the angle argument for the gate.\n\n<details>\n <summary><b>Need a hint?</b></summary>\n <p>If you were to apply a regular version of $R_x$ gate, it would take two parameters - angle
|
|
980
|
+
"asHtml": "<p><strong>Inputs:</strong></p>\n<ol>\n<li>Two qubits in an arbitrary state $|\\phi\\rangle$, stored as an array of length 2.</li>\n<li>An angle $\\theta$: $-\\pi < \\theta \\leq \\pi$.</li>\n</ol>\n<p><strong>Goal:</strong> Apply a controlled $R_x$ gate, using the first qubit as control and the second qubit as target, with $\\theta$ as the angle argument for the gate.</p>\n<details>\n <summary><b>Need a hint?</b></summary>\n <p>If you were to apply a regular version of $R_x$ gate, it would take two parameters - angle $\\theta$ as the first parameter and the target qubit as the second parameter.</p>\n</details>",
|
|
981
|
+
"asMarkdown": "**Inputs:**\n\n1. Two qubits in an arbitrary state $|\\phi\\rangle$, stored as an array of length 2.\n2. An angle $\\theta$: $-\\pi < \\theta \\leq \\pi$.\n\n**Goal:** Apply a controlled $R_x$ gate, using the first qubit as control and the second qubit as target, with $\\theta$ as the angle argument for the gate.\n\n<details>\n <summary><b>Need a hint?</b></summary>\n <p>If you were to apply a regular version of $R_x$ gate, it would take two parameters - angle $\\theta$ as the first parameter and the target qubit as the second parameter.</p>\n</details>"
|
|
982
982
|
},
|
|
983
983
|
"sourceIds": [
|
|
984
|
-
"
|
|
985
|
-
"
|
|
984
|
+
"multi_qubit_gates__controlled_rotation__Verification.qs",
|
|
985
|
+
"KatasLibrary.qs"
|
|
986
986
|
],
|
|
987
987
|
"placeholderCode": "namespace Kata {\n operation ControlledRotation(qs : Qubit[], theta : Double)\n : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}",
|
|
988
988
|
"explainedSolution": {
|
|
@@ -990,13 +990,13 @@ export default {
|
|
|
990
990
|
"items": [
|
|
991
991
|
{
|
|
992
992
|
"type": "text-content",
|
|
993
|
-
"asHtml": "<p>In Q# the <
|
|
994
|
-
"asMarkdown": "\nIn Q# the
|
|
993
|
+
"asHtml": "<p>In Q# the <code>Rx</code> intrinsic gate takes the angle $\\theta$ and the target qubit as inputs. To create a controlled version of this gate, we can use the <code>Controlled</code> functor.</p>\n<p>A matrix representation of this operation would be:</p>\n<p>$$\n\\begin{bmatrix} 1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & \\cos\\frac{\\theta}{2} & -i\\sin\\frac{\\theta}{2} \\\\ 0 & 0 & -i\\sin\\frac{\\theta}{2} & \\cos\\frac{\\theta}{2} \\end{bmatrix}\n$$</p>\n<p>The parameters of the new gate are changed a bit:</p>\n<ul>\n<li>The first parameter has to be the array of control qubits; the <code>Rx</code> gate will be applied to the target only if these are all in the $|1\\rangle$ state. Note that this parameter has to be an array, even if there is just one control qubit!</li>\n<li>The second parameter is a tuple with the parameters that you would've passed to the original <code>Rx</code> gate. You can create a tuple of values by putting round brackets around them.</li>\n</ul>\n<blockquote>\n<p>The <code>Controlled</code> functor can be used before any single-qubit gate to make it a controlled gate. The first argument will be an array of qubits even if you are using a single control qubit, like in the $CNOT$ gate. The second argument is a tuple <code>()</code> with the parameters of the gate. For example, these two gates are equivalent: <code>CNOT(qs[0], qs[1])</code> and <code>Controlled X([qs[0]], (qs[1]));</code></p>\n</blockquote>\n",
|
|
994
|
+
"asMarkdown": "\nIn Q# the `Rx` intrinsic gate takes the angle $\\theta$ and the target qubit as inputs. To create a controlled version of this gate, we can use the `Controlled` functor.\n\nA matrix representation of this operation would be:\n\n$$\n\\begin{bmatrix} 1 & 0 & 0 & 0 \\\\\\ 0 & 1 & 0 & 0 \\\\\\ 0 & 0 & \\cos\\frac{\\theta}{2} & -i\\sin\\frac{\\theta}{2} \\\\\\ 0 & 0 & -i\\sin\\frac{\\theta}{2} & \\cos\\frac{\\theta}{2} \\end{bmatrix}\n$$\n\nThe parameters of the new gate are changed a bit:\n\n* The first parameter has to be the array of control qubits; the `Rx` gate will be applied to the target only if these are all in the $|1\\rangle$ state. Note that this parameter has to be an array, even if there is just one control qubit!\n* The second parameter is a tuple with the parameters that you would've passed to the original `Rx` gate. You can create a tuple of values by putting round brackets around them.\n\n> The `Controlled` functor can be used before any single-qubit gate to make it a controlled gate. The first argument will be an array of qubits even if you are using a single control qubit, like in the $CNOT$ gate. The second argument is a tuple `()` with the parameters of the gate. For example, these two gates are equivalent: `CNOT(qs[0], qs[1])` and `Controlled X([qs[0]], (qs[1]));`"
|
|
995
995
|
},
|
|
996
996
|
{
|
|
997
997
|
"type": "solution",
|
|
998
998
|
"id": "multi_qubit_gates__controlled_rotation_solution",
|
|
999
|
-
"code": "namespace Kata {\n operation ControlledRotation (qs : Qubit[], theta : Double) : Unit is Adj + Ctl {\n let
|
|
999
|
+
"code": "namespace Kata {\n operation ControlledRotation (qs : Qubit[], theta : Double) : Unit is Adj + Ctl {\n let control = qs[0];\n let target = qs[1];\n Controlled Rx([control], (theta, target));\n }\n}"
|
|
1000
1000
|
}
|
|
1001
1001
|
]
|
|
1002
1002
|
}
|
|
@@ -1008,8 +1008,8 @@ export default {
|
|
|
1008
1008
|
"items": [
|
|
1009
1009
|
{
|
|
1010
1010
|
"type": "text-content",
|
|
1011
|
-
"asHtml": "<p>Controlled gates can have multiple control qubits; in this case the gate $U$ is applied only if all control qubits are in the $|1\\rangle$ states.\nYou can think of it as constructing a controlled version of a gate that is already controlled.</p>\n<p>The simplest example of this is the <strong>Toffoli gate</strong>, or
|
|
1012
|
-
"asMarkdown": "\nControlled gates can have multiple control qubits; in this case the gate $U$ is applied only if all control qubits are in the $|1\\rangle$ states.\nYou can think of it as constructing a controlled version of a gate that is already controlled.\n\nThe simplest example of this is the **Toffoli gate**, or
|
|
1011
|
+
"asHtml": "<p>Controlled gates can have multiple control qubits; in this case the gate $U$ is applied only if all control qubits are in the $|1\\rangle$ states.\nYou can think of it as constructing a controlled version of a gate that is already controlled.</p>\n<p>The simplest example of this is the <strong>Toffoli gate</strong>, or $CCNOT$ (controlled controlled $NOT$) gate, which applies the $X$ gate to the last qubit only if the first two qubits are in the $|11\\rangle$ state:</p>\n<p>$$\n\\begin{bmatrix}\n 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\\\ \n 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\\\ \n 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\\\ \n 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\\\ \n 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\\\ \n 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\\\ \n 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\\\ \n 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\n\\end{bmatrix}\n$$</p>\n<p>To construct a multi-controlled version of an operation in Q#, you can use the Controlled functor as well, passing all control qubits as an array that is the first parameter.</p>\n",
|
|
1012
|
+
"asMarkdown": "\nControlled gates can have multiple control qubits; in this case the gate $U$ is applied only if all control qubits are in the $|1\\rangle$ states.\nYou can think of it as constructing a controlled version of a gate that is already controlled.\n\nThe simplest example of this is the **Toffoli gate**, or $CCNOT$ (controlled controlled $NOT$) gate, which applies the $X$ gate to the last qubit only if the first two qubits are in the $|11\\rangle$ state:\n\n$$\n\\begin{bmatrix}\n 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\\\\\ \n 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\\\\\ \n 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\\\\\ \n 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\\\\\ \n 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\\\\\ \n 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\\\\\ \n 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\\\\\ \n 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\n\\end{bmatrix}\n$$\n\nTo construct a multi-controlled version of an operation in Q#, you can use the Controlled functor as well, passing all control qubits as an array that is the first parameter."
|
|
1013
1013
|
}
|
|
1014
1014
|
]
|
|
1015
1015
|
},
|
|
@@ -1020,8 +1020,8 @@ export default {
|
|
|
1020
1020
|
"items": [
|
|
1021
1021
|
{
|
|
1022
1022
|
"type": "text-content",
|
|
1023
|
-
"asHtml": "<p>Typically, the term "controlled
|
|
1024
|
-
"asMarkdown": "\nTypically, the term \"controlled
|
|
1023
|
+
"asHtml": "<p>Typically, the term "controlled $U$ gate" refers to the type of gate we've described previously, which applies the gate $U$ only if the control qubit(s) are in the $|1\\rangle$ state.</p>\n<p>It is possible, however, to define variants of controlled gates that use different states as control states.\nFor example, an <strong>anti-controlled</strong> $U$ gate (sometimes called <strong>zero-controlled</strong>) applies a gate only if the control qubit is in the $|0\\rangle$ state.\nIt is also possible to define control conditions in other bases, for example, applying the gate if the control qubit is in the $|+\\rangle$ state.</p>\n<p>All the variants of controlled gates can be expressed in terms of the controls described in previous sections, using the following sequence of steps:</p>\n<ul>\n<li>First, apply a transformation on control qubits that will transform the state you want to use as control into the $|1...1\\rangle$ state.</li>\n<li>Apply the regular controlled version of the gate.</li>\n<li>Finally, undo the transformation on control qubits from the first step using the adjoint version of it.</li>\n</ul>\n<p>Why do we need this last step? Remember that controlled gates are defined in terms of their effect on the basis states:\nwe apply the gate on the target qubit if and only if the control qubit is in the state we want to control on, and we don't change the state of the control qubit at all.\nIf we don't undo the transformation we did on the first step, applying our gate to a basis state will modify not only the state of the target qubit but also the state of the control qubit, which is not what we're looking for.</p>\n<p>For example, consider an anti-controlled $X$ gate - a gate that should apply an $X$ gate to the second qubit if the first qubit is in the $|0\\rangle$ state.\nHere is the effect we expect this gate to have on each of the 2-qubit basis states:</p>\n<table>\n <tr>\n <th>Input state</th>\n <th>Output state</th>\n </tr>\n <tr>\n <td>$|00\\rangle$</td>\n <td>$|01\\rangle$</td>\n </tr>\n <tr>\n <td>$|01\\rangle$</td>\n <td>$|00\\rangle$</td>\n </tr>\n <tr>\n <td>$|10\\rangle$</td>\n <td>$|10\\rangle$</td>\n </tr>\n <tr>\n <td>$|11\\rangle$</td>\n <td>$|11\\rangle$</td>\n </tr>\n</table>\n\n<p>Let's apply the anti-controlled $X$ gate to the $|00\\rangle$ state step by step:</p>\n<ol>\n<li>Transform the state of the control qubit to $|1\\rangle$: we can do that by applying the $X$ gate to the first qubit:\n$$|00\\rangle \\rightarrow |10\\rangle$$</li>\n<li>Apply the regular $CNOT$ gate:\n$$|10\\rangle \\rightarrow |11\\rangle$$</li>\n<li>Now, if we don't undo the change we did on the first step, we'll end up with a gate that transforms $|00\\rangle$ into $|11\\rangle$, which is not the transformation we're trying to implement.\nHowever, if we undo it by applying the $X$ gate to the first qubit again, we'll get the correct state:\n$$|11\\rangle \\rightarrow |01\\rangle$$</li>\n</ol>\n<p>You can check that getting the right behavior of the operation on the rest of the basis states also requires that last step.</p>\n<p>Finally, let's take a look at a very useful operation <code>ApplyControlledOnBitString</code> provided by the Q# standard library.\nIt applies a variant of a gate controlled on a basis state specified by a bit mask; for example, bit mask <code>[true, false]</code> means that the gate should be applied only if the two control qubits are in the $|10\\rangle$ state.\nThis operation takes four arguments: the control bit mask as a Boolean array, the gate $U$ that needs its controlled variant defined, the array of control qubits, and the arguments to the $U$ gate (the target qubit(s) and any additional parameters it takes).</p>\n<p>The sequence of steps that implement this variant are:</p>\n<ol>\n<li>Apply the $X$ gate to each control qubit that corresponds to a <code>false</code> element of the bit mask (in the example, that's just the second qubit). After this, if the control qubits started in the $|10\\rangle$ state, they'll end up in the $|11\\rangle$ state, and if they started in any other state, they'll end up in any state but $|11\\rangle$.</li>\n<li>Apply the regular controlled version of the gate.</li>\n<li>Apply the $X$ gate to the same qubits to return them to their original state.</li>\n</ol>\n",
|
|
1024
|
+
"asMarkdown": "\nTypically, the term \"controlled $U$ gate\" refers to the type of gate we've described previously, which applies the gate $U$ only if the control qubit(s) are in the $|1\\rangle$ state.\n\nIt is possible, however, to define variants of controlled gates that use different states as control states.\nFor example, an **anti-controlled** $U$ gate (sometimes called **zero-controlled**) applies a gate only if the control qubit is in the $|0\\rangle$ state.\nIt is also possible to define control conditions in other bases, for example, applying the gate if the control qubit is in the $|+\\rangle$ state.\n\nAll the variants of controlled gates can be expressed in terms of the controls described in previous sections, using the following sequence of steps:\n\n- First, apply a transformation on control qubits that will transform the state you want to use as control into the $|1...1\\rangle$ state.\n- Apply the regular controlled version of the gate.\n- Finally, undo the transformation on control qubits from the first step using the adjoint version of it.\n\nWhy do we need this last step? Remember that controlled gates are defined in terms of their effect on the basis states:\nwe apply the gate on the target qubit if and only if the control qubit is in the state we want to control on, and we don't change the state of the control qubit at all.\nIf we don't undo the transformation we did on the first step, applying our gate to a basis state will modify not only the state of the target qubit but also the state of the control qubit, which is not what we're looking for.\n\nFor example, consider an anti-controlled $X$ gate - a gate that should apply an $X$ gate to the second qubit if the first qubit is in the $|0\\rangle$ state.\nHere is the effect we expect this gate to have on each of the 2-qubit basis states:\n\n<table>\n <tr>\n <th>Input state</th>\n <th>Output state</th>\n </tr>\n <tr>\n <td>$|00\\rangle$</td>\n <td>$|01\\rangle$</td>\n </tr>\n <tr>\n <td>$|01\\rangle$</td>\n <td>$|00\\rangle$</td>\n </tr>\n <tr>\n <td>$|10\\rangle$</td>\n <td>$|10\\rangle$</td>\n </tr>\n <tr>\n <td>$|11\\rangle$</td>\n <td>$|11\\rangle$</td>\n </tr>\n</table>\n\nLet's apply the anti-controlled $X$ gate to the $|00\\rangle$ state step by step:\n\n1. Transform the state of the control qubit to $|1\\rangle$: we can do that by applying the $X$ gate to the first qubit:\n$$|00\\rangle \\rightarrow |10\\rangle$$\n2. Apply the regular $CNOT$ gate:\n$$|10\\rangle \\rightarrow |11\\rangle$$\n3. Now, if we don't undo the change we did on the first step, we'll end up with a gate that transforms $|00\\rangle$ into $|11\\rangle$, which is not the transformation we're trying to implement.\nHowever, if we undo it by applying the $X$ gate to the first qubit again, we'll get the correct state:\n$$|11\\rangle \\rightarrow |01\\rangle$$\n\nYou can check that getting the right behavior of the operation on the rest of the basis states also requires that last step.\n\nFinally, let's take a look at a very useful operation `ApplyControlledOnBitString` provided by the Q# standard library.\nIt applies a variant of a gate controlled on a basis state specified by a bit mask; for example, bit mask `[true, false]` means that the gate should be applied only if the two control qubits are in the $|10\\rangle$ state.\nThis operation takes four arguments: the control bit mask as a Boolean array, the gate $U$ that needs its controlled variant defined, the array of control qubits, and the arguments to the $U$ gate (the target qubit(s) and any additional parameters it takes).\n\nThe sequence of steps that implement this variant are:\n\n1. Apply the $X$ gate to each control qubit that corresponds to a `false` element of the bit mask (in the example, that's just the second qubit). After this, if the control qubits started in the $|10\\rangle$ state, they'll end up in the $|11\\rangle$ state, and if they started in any other state, they'll end up in any state but $|11\\rangle$.\n2. Apply the regular controlled version of the gate.\n3. Apply the $X$ gate to the same qubits to return them to their original state."
|
|
1025
1025
|
}
|
|
1026
1026
|
]
|
|
1027
1027
|
},
|
|
@@ -1031,12 +1031,12 @@ export default {
|
|
|
1031
1031
|
"title": "Arbitrary Controls",
|
|
1032
1032
|
"description": {
|
|
1033
1033
|
"type": "text-content",
|
|
1034
|
-
"asHtml": "<p><strong>Input:</strong></p>\n<ol>\n<li><code>controls</code> - a register of $N$ qubits in an arbitrary state $|\\phi\\rangle$.</li>\n<li><code>target</code> - a qubit in an arbitrary state $|\\psi\\rangle$.</li>\n<li><code>controlBits</code> - an array of $N$ booleans, specifying what state each control qubit should be in order to apply the gate.</li>\n</ol>\n<p><strong>Goal:</strong> Apply the controlled
|
|
1035
|
-
"asMarkdown": "**Input:**\n\n1. `controls` - a register of $N$ qubits in an arbitrary state $|\\phi\\rangle$.\n2. `target` - a qubit in an arbitrary state $|\\psi\\rangle$.\n3. `controlBits` - an array of $N$ booleans, specifying what state each control qubit should be in order to apply the gate.\n\n**Goal:** Apply the controlled
|
|
1034
|
+
"asHtml": "<p><strong>Input:</strong></p>\n<ol>\n<li><code>controls</code> - a register of $N$ qubits in an arbitrary state $|\\phi\\rangle$.</li>\n<li><code>target</code> - a qubit in an arbitrary state $|\\psi\\rangle$.</li>\n<li><code>controlBits</code> - an array of $N$ booleans, specifying what state each control qubit should be in order to apply the gate.</li>\n</ol>\n<p><strong>Goal:</strong> Apply the controlled $X$ gate with the <code>controls</code> as control qubits and <code>target</code> as target, with the state specified by <code>controlBits</code> as controls. If the element of the array is <code>true</code>, the corresponding qubit is a regular control (should be in state $|1\\rangle$), and if it is <code>false</code>, the corresponding qubit is an anti-control (should be in state $|0\\rangle$).</p>\n<blockquote>\n<p>For example, if <code>controlBits = [true, false, true]</code>, the controlled $X$ gate should only be applied if the control qubits are in state $|101\\rangle$.</p>\n</blockquote>\n<details>\n <summary><strong>Need a hint?</strong></summary>\n <p>Consider using a library operation for this task. If you want to do it without a library operation, don't forget to reset the qubits back to the state they were originally in.</p>\n</details>\n",
|
|
1035
|
+
"asMarkdown": "**Input:**\n\n1. `controls` - a register of $N$ qubits in an arbitrary state $|\\phi\\rangle$.\n2. `target` - a qubit in an arbitrary state $|\\psi\\rangle$.\n3. `controlBits` - an array of $N$ booleans, specifying what state each control qubit should be in order to apply the gate.\n\n**Goal:** Apply the controlled $X$ gate with the `controls` as control qubits and `target` as target, with the state specified by `controlBits` as controls. If the element of the array is `true`, the corresponding qubit is a regular control (should be in state $|1\\rangle$), and if it is `false`, the corresponding qubit is an anti-control (should be in state $|0\\rangle$).\n\n> For example, if `controlBits = [true, false, true]`, the controlled $X$ gate should only be applied if the control qubits are in state $|101\\rangle$.\n\n<details>\n <summary><strong>Need a hint?</strong></summary>\n <p>Consider using a library operation for this task. If you want to do it without a library operation, don't forget to reset the qubits back to the state they were originally in.</p>\n</details>\n"
|
|
1036
1036
|
},
|
|
1037
1037
|
"sourceIds": [
|
|
1038
|
-
"
|
|
1039
|
-
"
|
|
1038
|
+
"multi_qubit_gates__arbitrary_controls__Verification.qs",
|
|
1039
|
+
"KatasLibrary.qs"
|
|
1040
1040
|
],
|
|
1041
1041
|
"placeholderCode": "namespace Kata {\n operation MultiControls(\n controls : Qubit[], target : Qubit, controlBits : Bool[])\n : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}",
|
|
1042
1042
|
"explainedSolution": {
|
|
@@ -1044,8 +1044,8 @@ export default {
|
|
|
1044
1044
|
"items": [
|
|
1045
1045
|
{
|
|
1046
1046
|
"type": "text-content",
|
|
1047
|
-
"asHtml": "<p>We are asked to perform
|
|
1048
|
-
"asMarkdown": "\nWe are asked to perform
|
|
1047
|
+
"asHtml": "<p>We are asked to perform an $X$ gate on the <code>target</code> qubit controlled by the state of <code>controls</code> qubits; this state should correspond to the mask given by <code>controlBits</code>.</p>\n<p>If the <code>controlBits</code> mask consists of all <code>true</code> values, we can use a familiar <code>Controlled X</code> gate. What can we do if the mask has some <code>false</code> values in it?</p>\n<p>Turns out we can transform the state of the control qubits depending on the corresponding elements of <code>controlBits</code>: if the element is <code>false</code>, we apply an $X$ gate to the corresponding qubit in the <code>controls</code> array. After this, <code>Controlled X</code> gate will apply an $X$ gate in the exact case that we want.\nFinally, we'll need to remember to undo ("uncompute") the first step, otherwise our controlled gate will affect the state of the control qubits as well as the state of the target.</p>\n<p>As you can see in the first cell below, this can take quite some coding.</p>\n",
|
|
1048
|
+
"asMarkdown": "\nWe are asked to perform an $X$ gate on the `target` qubit controlled by the state of `controls` qubits; this state should correspond to the mask given by `controlBits`.\n\nIf the `controlBits` mask consists of all `true` values, we can use a familiar `Controlled X` gate. What can we do if the mask has some `false` values in it?\n\nTurns out we can transform the state of the control qubits depending on the corresponding elements of `controlBits`: if the element is `false`, we apply an $X$ gate to the corresponding qubit in the `controls` array. After this, `Controlled X` gate will apply an $X$ gate in the exact case that we want.\nFinally, we'll need to remember to undo (\\\"uncompute\\\") the first step, otherwise our controlled gate will affect the state of the control qubits as well as the state of the target.\n\nAs you can see in the first cell below, this can take quite some coding."
|
|
1049
1049
|
},
|
|
1050
1050
|
{
|
|
1051
1051
|
"type": "solution",
|
|
@@ -1064,8 +1064,8 @@ export default {
|
|
|
1064
1064
|
},
|
|
1065
1065
|
{
|
|
1066
1066
|
"type": "text-content",
|
|
1067
|
-
"asHtml": "<p>Finally, here is how the exact same task could be realized using the library function <
|
|
1068
|
-
"asMarkdown": "\nFinally, here is how the exact same task could be realized using the library function
|
|
1067
|
+
"asHtml": "<p>Finally, here is how the exact same task could be realized using the library function <code>ApplyControlledOnBitString</code>.</p>\n",
|
|
1068
|
+
"asMarkdown": "\nFinally, here is how the exact same task could be realized using the library function `ApplyControlledOnBitString`."
|
|
1069
1069
|
},
|
|
1070
1070
|
{
|
|
1071
1071
|
"type": "solution",
|
|
@@ -1082,8 +1082,8 @@ export default {
|
|
|
1082
1082
|
"items": [
|
|
1083
1083
|
{
|
|
1084
1084
|
"type": "text-content",
|
|
1085
|
-
"asHtml": "<p>Congratulations!
|
|
1086
|
-
"asMarkdown": "\nCongratulations!
|
|
1085
|
+
"asHtml": "<p>Congratulations! In this kata you learned how to apply multi-qubit quantum gates to multi-qubit systems. Here are a few key concepts to keep in mind:</p>\n<ul>\n<li>Applying single-qubit gates to a quantum system can be described as applying a larger multi-qubit gate to the whole system. In this case, this multi-qubit gate can be represented as a tensor product of single-qubit gates.</li>\n<li>$CNOT$ gate is a type of controlled gate that acts on two qubits. It applies the $X$ gate to the target qubit if the control qubit is in state $\\ket{1}$, otherwise it does nothing.</li>\n<li>$SWAP$ gate is a gate that acts on two qubits, swapping their states.</li>\n<li>In Q#, controlled gates are applied using the <code>Controlled</code> functor.</li>\n<li><code>ApplyControlledOnBitString</code> operation allows us to construct multi-qubit controlled gates with different control patterns.</li>\n</ul>\n<p>Next, you will learn about quantum measurements in the "Measurements in Single-Qubit Systems" kata.</p>\n",
|
|
1086
|
+
"asMarkdown": "\nCongratulations! In this kata you learned how to apply multi-qubit quantum gates to multi-qubit systems. Here are a few key concepts to keep in mind:\n\n- Applying single-qubit gates to a quantum system can be described as applying a larger multi-qubit gate to the whole system. In this case, this multi-qubit gate can be represented as a tensor product of single-qubit gates.\n- $CNOT$ gate is a type of controlled gate that acts on two qubits. It applies the $X$ gate to the target qubit if the control qubit is in state $\\ket{1}$, otherwise it does nothing.\n- $SWAP$ gate is a gate that acts on two qubits, swapping their states.\n- In Q#, controlled gates are applied using the `Controlled` functor.\n- `ApplyControlledOnBitString` operation allows us to construct multi-qubit controlled gates with different control patterns.\n\nNext, you will learn about quantum measurements in the \"Measurements in Single-Qubit Systems\" kata."
|
|
1087
1087
|
}
|
|
1088
1088
|
]
|
|
1089
1089
|
}
|
|
@@ -1112,25 +1112,25 @@ export default {
|
|
|
1112
1112
|
"items": [
|
|
1113
1113
|
{
|
|
1114
1114
|
"type": "text-content",
|
|
1115
|
-
"asHtml": "<p>In this section, we will discuss the simplest type of qubit measurements - measurements in the computational basis. This is the "default" type of measurements. Unless otherwise specified, "measurement" refers to this type.</p>\n<p>The state $\\ket{\\psi}$ of a single qubit can always be expressed in Dirac notation as:\n$$\\ket{\\psi} = \\alpha \\ket{0} + \\beta \\ket{1}$$\nwhere $\\alpha$ and $\\beta$ are complex numbers, and the state is normalized, $|\\alpha|^2 + |\\beta|^2 = 1$.</p>\n<p>To get some information about the qubit state, we can measure the qubit. Similar to the classical case of measuring a bit, the outcome of a measurement can be $0$ or $1$. However, unlike the classical case, quantum measurement is a probabilistic process.</p>\n<p>The probabilities of the measurement outcomes being $0$ and $1$ are $|\\alpha|^2$ and $|\\beta|^2$, respectively. Additionally, the state of the qubit is modified by the measurement - if the outcome of the measurement is $0$, then the post-measurement state of the qubit is $\\ket{0}$, and if the outcome is $1$, the state is $\\ket{1}$. In quantum mechanics, this is referred to as the <a href=\"https://en.wikipedia.org/wiki/Wave_function_collapse\" target=\"_blank\">collapse of the wave function</a>.</p>\n<p>The outcomes of computational basis measurements and their probabilities are summarized in the table below:</p>\n<table>\n <tr>\n <th>Measurement outcome</th>\n <th>Probability of outcome</th>\n <th>State after measurement</th>\n </tr>\n <tr>\n <td>$0$</td>\n <td>$|\\alpha|^2$</td>\n <td>$\\ket 0$</td>\n </tr>\n <tr>\n <td>$1$</td>\n <td>$|\\beta|^2$</td>\n <td>$\\ket 1$</td>\n </tr>\n</table>\n\n<blockquote>\n<p>Unlike quantum gates which are unitary and reversible operations, measurements are neither unitary nor reversible. Since the outcomes of a measurement are probabilistic, any two isolated qubits which are initially prepared in identical superposition states are in general not guaranteed to have the same measurement outcomes after each qubit has been measured separately. As we will see below, measurements are modeled by projection operators instead of unitary operators.</p>\n<p>Additionally, the assumption of the wave function being <strong>normalized</strong> is important, since the probability outcomes must sum up to $1$. If the wave function is not normalized, it is important to normalize it first in order to obtain the correct measurement probabilities.</p>\n</blockquote>\n<h2>🔎 Analyze</h2>\n<p>The qubit is in the following state:\n$$\\ket \\psi = 0.6 \\ket 0 + 0.8 \\ket 1
|
|
1116
|
-
"asMarkdown": "\nIn this section, we will discuss the simplest type of qubit measurements - measurements in the computational basis. This is the \"default\" type of measurements. Unless otherwise specified, \"measurement\" refers to this type.\n\nThe state $\\ket{\\psi}$ of a single qubit can always be expressed in Dirac notation as:\n$$\\ket{\\psi} = \\alpha \\ket{0} + \\beta \\ket{1}$$\nwhere $\\alpha$ and $\\beta$ are complex numbers, and the state is normalized, $|\\alpha|^2 + |\\beta|^2 = 1$.\n\nTo get some information about the qubit state, we can measure the qubit. Similar to the classical case of measuring a bit, the outcome of a measurement can be $0$ or $1$. However, unlike the classical case, quantum measurement is a probabilistic process.\n\nThe probabilities of the measurement outcomes being $0$ and $1$ are $|\\alpha|^2$ and $|\\beta|^2$, respectively. Additionally, the state of the qubit is modified by the measurement - if the outcome of the measurement is $0$, then the post-measurement state of the qubit is $\\ket{0}$, and if the outcome is $1$, the state is $\\ket{1}$. In quantum mechanics, this is referred to as the <a href=\"https://en.wikipedia.org/wiki/Wave_function_collapse\" target=\"_blank\">collapse of the wave function</a>.\n\nThe outcomes of computational basis measurements and their probabilities are summarized in the table below:\n<table>\n <tr>\n <th>Measurement outcome</th>\n <th>Probability of outcome</th>\n <th>State after measurement</th>\n </tr>\n <tr>\n <td>$0$</td>\n <td>$|\\alpha|^2$</td>\n <td>$\\ket 0$</td>\n </tr>\n <tr>\n <td>$1$</td>\n <td>$|\\beta|^2$</td>\n <td>$\\ket 1$</td>\n </tr>\n</table>\n\n>Unlike quantum gates which are unitary and reversible operations, measurements are neither unitary nor reversible. Since the outcomes of a measurement are probabilistic, any two isolated qubits which are initially prepared in identical superposition states are in general not guaranteed to have the same measurement outcomes after each qubit has been measured separately. As we will see below, measurements are modeled by projection operators instead of unitary operators.\n>\n>Additionally, the assumption of the wave function being **normalized** is important, since the probability outcomes must sum up to $1$. If the wave function is not normalized, it is important to normalize it first in order to obtain the correct measurement probabilities.\n\n## 🔎 Analyze\n\nThe qubit is in the following state:\n$$\\ket \\psi = 0.6 \\ket 0 + 0.8 \\ket 1
|
|
1115
|
+
"asHtml": "<p>In this section, we will discuss the simplest type of qubit measurements - measurements in the computational basis. This is the "default" type of measurements. Unless otherwise specified, "measurement" refers to this type.</p>\n<p>The state $\\ket{\\psi}$ of a single qubit can always be expressed in Dirac notation as:\n$$\\ket{\\psi} = \\alpha \\ket{0} + \\beta \\ket{1}$$\nwhere $\\alpha$ and $\\beta$ are complex numbers, and the state is normalized, $|\\alpha|^2 + |\\beta|^2 = 1$.</p>\n<p>To get some information about the qubit state, we can measure the qubit. Similar to the classical case of measuring a bit, the outcome of a measurement can be $0$ or $1$. However, unlike the classical case, quantum measurement is a probabilistic process.</p>\n<p>The probabilities of the measurement outcomes being $0$ and $1$ are $|\\alpha|^2$ and $|\\beta|^2$, respectively. Additionally, the state of the qubit is modified by the measurement - if the outcome of the measurement is $0$, then the post-measurement state of the qubit is $\\ket{0}$, and if the outcome is $1$, the state is $\\ket{1}$. In quantum mechanics, this is referred to as the <a href=\"https://en.wikipedia.org/wiki/Wave_function_collapse\" target=\"_blank\">collapse of the wave function</a>.</p>\n<p>The outcomes of computational basis measurements and their probabilities are summarized in the table below:</p>\n<table>\n <tr>\n <th>Measurement outcome</th>\n <th>Probability of outcome</th>\n <th>State after measurement</th>\n </tr>\n <tr>\n <td>$0$</td>\n <td>$|\\alpha|^2$</td>\n <td>$\\ket 0$</td>\n </tr>\n <tr>\n <td>$1$</td>\n <td>$|\\beta|^2$</td>\n <td>$\\ket 1$</td>\n </tr>\n</table>\n\n<blockquote>\n<p>Unlike quantum gates, which are unitary and reversible operations, measurements are neither unitary nor reversible. Since the outcomes of a measurement are probabilistic, any two isolated qubits which are initially prepared in identical superposition states are in general not guaranteed to have the same measurement outcomes after each qubit has been measured separately. As we will see below, measurements are modeled by projection operators instead of unitary operators.</p>\n<p>Additionally, the assumption of the wave function being <strong>normalized</strong> is important, since the probability outcomes must sum up to $1$. If the wave function is not normalized, it is important to normalize it first in order to obtain the correct measurement probabilities.</p>\n</blockquote>\n<h2>🔎 Analyze</h2>\n<p>The qubit is in the following state:\n$$\\ket \\psi = 0.6 \\ket 0 + 0.8 \\ket 1 = \\begin{bmatrix} 0.6 \\\\ 0.8\\end{bmatrix}$$</p>\n<p>If this qubit is measured in the computational basis, what are the outcome probabilities?</p>\n<details>\n<summary><b>Solution</b></summary>\nThe given state $\\ket \\psi$ is normalized, since $0.6^2 + 0.8^2 = 1$. Hence, the probability of measuring $0$ is $|0.6|^2 = 0.36$, and the probability of measuring $1$ is $|0.8|^2 = 0.64$.\n</details>",
|
|
1116
|
+
"asMarkdown": "\nIn this section, we will discuss the simplest type of qubit measurements - measurements in the computational basis. This is the \"default\" type of measurements. Unless otherwise specified, \"measurement\" refers to this type.\n\nThe state $\\ket{\\psi}$ of a single qubit can always be expressed in Dirac notation as:\n$$\\ket{\\psi} = \\alpha \\ket{0} + \\beta \\ket{1}$$\nwhere $\\alpha$ and $\\beta$ are complex numbers, and the state is normalized, $|\\alpha|^2 + |\\beta|^2 = 1$.\n\nTo get some information about the qubit state, we can measure the qubit. Similar to the classical case of measuring a bit, the outcome of a measurement can be $0$ or $1$. However, unlike the classical case, quantum measurement is a probabilistic process.\n\nThe probabilities of the measurement outcomes being $0$ and $1$ are $|\\alpha|^2$ and $|\\beta|^2$, respectively. Additionally, the state of the qubit is modified by the measurement - if the outcome of the measurement is $0$, then the post-measurement state of the qubit is $\\ket{0}$, and if the outcome is $1$, the state is $\\ket{1}$. In quantum mechanics, this is referred to as the <a href=\"https://en.wikipedia.org/wiki/Wave_function_collapse\" target=\"_blank\">collapse of the wave function</a>.\n\nThe outcomes of computational basis measurements and their probabilities are summarized in the table below:\n<table>\n <tr>\n <th>Measurement outcome</th>\n <th>Probability of outcome</th>\n <th>State after measurement</th>\n </tr>\n <tr>\n <td>$0$</td>\n <td>$|\\alpha|^2$</td>\n <td>$\\ket 0$</td>\n </tr>\n <tr>\n <td>$1$</td>\n <td>$|\\beta|^2$</td>\n <td>$\\ket 1$</td>\n </tr>\n</table>\n\n>Unlike quantum gates, which are unitary and reversible operations, measurements are neither unitary nor reversible. Since the outcomes of a measurement are probabilistic, any two isolated qubits which are initially prepared in identical superposition states are in general not guaranteed to have the same measurement outcomes after each qubit has been measured separately. As we will see below, measurements are modeled by projection operators instead of unitary operators.\n>\n>Additionally, the assumption of the wave function being **normalized** is important, since the probability outcomes must sum up to $1$. If the wave function is not normalized, it is important to normalize it first in order to obtain the correct measurement probabilities.\n\n## 🔎 Analyze\n\nThe qubit is in the following state:\n$$\\ket \\psi = 0.6 \\ket 0 + 0.8 \\ket 1 = \\begin{bmatrix} 0.6 \\\\\\ 0.8\\end{bmatrix}$$\n\nIf this qubit is measured in the computational basis, what are the outcome probabilities?\n\n<details>\n<summary><b>Solution</b></summary>\nThe given state $\\ket \\psi$ is normalized, since $0.6^2 + 0.8^2 = 1$. Hence, the probability of measuring $0$ is $|0.6|^2 = 0.36$, and the probability of measuring $1$ is $|0.8|^2 = 0.64$.\n</details>"
|
|
1117
1117
|
}
|
|
1118
1118
|
]
|
|
1119
1119
|
},
|
|
1120
1120
|
{
|
|
1121
1121
|
"type": "lesson",
|
|
1122
1122
|
"id": "single_qubit_measurements__implementing_measurement",
|
|
1123
|
-
"title": "Implementing Measurement
|
|
1123
|
+
"title": "Implementing Measurement in Q# Using Operations M and MResetZ",
|
|
1124
1124
|
"items": [
|
|
1125
1125
|
{
|
|
1126
1126
|
"type": "text-content",
|
|
1127
|
-
"asHtml": "<p>In this demo, we prepare a qubit in the state $0.6|0\\rangle + 0.8|1\\rangle$, and then measure it in the computational basis. In Q#, single-qubit measurements in the computational basis can be implemented using the <
|
|
1128
|
-
"asMarkdown": "\nIn this demo, we prepare a qubit in the state $0.6|0\\rangle + 0.8|1\\rangle$, and then measure it in the computational basis. In Q#, single-qubit measurements in the computational basis can be implemented using the
|
|
1127
|
+
"asHtml": "<p>In this demo, we prepare a qubit in the state $0.6|0\\rangle + 0.8|1\\rangle$, and then measure it in the computational basis. In Q#, single-qubit measurements in the computational basis can be implemented using the <code>M</code> operation. It will return the constant <code>Zero</code> if measurement result was $0$ or the constant <code>One</code> if the measurement result was $1$. <code>Zero</code> and <code>One</code> are constants of type <code>Result</code>.</p>\n<blockquote>\n<p>If you run this code multiple times, you will notice that whenever the measurement outcome is $1$, the post-measurement state of the qubit is $\\ket 1$, and similarly for outcome $0$ the final state is $\\ket{0}$. This is in line with our expectation that after the measurement the wave function 'collapses' to the corresponding state.</p>\n</blockquote>\n<p>An alternative way to implement a computational basis measurement is using the <code>MResetZ</code> operation that measures the qubit and resets it to the $\\ket{0}$ state. Remember that Q# requires you to reset all qubits to $\\ket{0}$ before releasing them, so if you don't need to use the qubit after you measure it, it is convenient to use <code>MResetZ</code> right away rather than do the measurement using <code>M</code> and follow it with a separate call to <code>Reset</code>.</p>\n",
|
|
1128
|
+
"asMarkdown": "\nIn this demo, we prepare a qubit in the state $0.6|0\\rangle + 0.8|1\\rangle$, and then measure it in the computational basis. In Q#, single-qubit measurements in the computational basis can be implemented using the `M` operation. It will return the constant `Zero` if measurement result was $0$ or the constant `One` if the measurement result was $1$. `Zero` and `One` are constants of type `Result`.\n\n> If you run this code multiple times, you will notice that whenever the measurement outcome is $1$, the post-measurement state of the qubit is $\\ket 1$, and similarly for outcome $0$ the final state is $\\ket{0}$. This is in line with our expectation that after the measurement the wave function 'collapses' to the corresponding state.\n\nAn alternative way to implement a computational basis measurement is using the `MResetZ` operation that measures the qubit and resets it to the $\\ket{0}$ state. Remember that Q# requires you to reset all qubits to $\\ket{0}$ before releasing them, so if you don't need to use the qubit after you measure it, it is convenient to use `MResetZ` right away rather than do the measurement using `M` and follow it with a separate call to `Reset`."
|
|
1129
1129
|
},
|
|
1130
1130
|
{
|
|
1131
1131
|
"type": "example",
|
|
1132
1132
|
"id": "single_qubit_measurements__implementing_measurement_demo",
|
|
1133
|
-
"code": "namespace Kata {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Math;\n\n @EntryPoint()\n operation SimpleMeasurementDemo() : Unit {\n use q = Qubit();\n // Prepare the qubit in the superposition state
|
|
1133
|
+
"code": "namespace Kata {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Math;\n\n @EntryPoint()\n operation SimpleMeasurementDemo() : Unit {\n use q = Qubit();\n // Prepare the qubit in the superposition state |𝜓❭ = 0.6 |0❭ + 0.8 |1❭\n Ry(2.0 * ArcTan2(0.8, 0.6), q);\n Message(\"Qubit in state |𝜓❭:\");\n DumpMachine();\n\n let outcome = M(q) == One ? 1 | 0;\n Message($\"The measurement outcome is {outcome}.\");\n Message(\"Post-measurement state of the qubit:\");\n DumpMachine();\n Reset(q);\n }\n}\n"
|
|
1134
1134
|
}
|
|
1135
1135
|
]
|
|
1136
1136
|
},
|
|
@@ -1141,18 +1141,18 @@ export default {
|
|
|
1141
1141
|
"items": [
|
|
1142
1142
|
{
|
|
1143
1143
|
"type": "text-content",
|
|
1144
|
-
"asHtml": "<p>The following code demonstrates that the theoretical and experimental values of the probability outcomes indeed match with each other. We repeatedly prepare the same state $\\ket \\psi = 0.6 \\ket 0 + 0.8 \\ket 1$ and measure it in the computational basis $100$ times. At the end, we expect 0 to be measured approximately $|0.6 |^2 \\cdot 100= 36$ times, and 1 to be measured approximately $|0.8|^2 \\cdot 100= 64$ times. Note that since measurements are probabilistic, we do not expect the results to match these values exactly. As we repeat the measurements, the resulting distribution will align with the theoretical probabilities.</p>\n",
|
|
1145
|
-
"asMarkdown": "\nThe following code demonstrates that the theoretical and experimental values of the probability outcomes indeed match with each other. We repeatedly prepare the same state $\\ket \\psi = 0.6 \\ket 0 + 0.8 \\ket 1$ and measure it in the computational basis $100$ times. At the end, we expect 0 to be measured approximately $|0.6 |^2 \\cdot 100= 36$ times, and 1 to be measured approximately $|0.8|^2 \\cdot 100= 64$ times. Note that since measurements are probabilistic, we do not expect the results to match these values exactly. As we repeat the measurements, the resulting distribution will align with the theoretical probabilities."
|
|
1144
|
+
"asHtml": "<p>The following code demonstrates that the theoretical and experimental values of the probability outcomes indeed match with each other. We repeatedly prepare the same state $\\ket \\psi = 0.6 \\ket 0 + 0.8 \\ket 1$ and measure it in the computational basis $100$ times. At the end, we expect $0$ to be measured approximately $|0.6 |^2 \\cdot 100= 36$ times, and $1$ to be measured approximately $|0.8|^2 \\cdot 100= 64$ times. Note that since measurements are probabilistic, we do not expect the results to match these values exactly. As we repeat the measurements, the resulting distribution will align with the theoretical probabilities.</p>\n",
|
|
1145
|
+
"asMarkdown": "\nThe following code demonstrates that the theoretical and experimental values of the probability outcomes indeed match with each other. We repeatedly prepare the same state $\\ket \\psi = 0.6 \\ket 0 + 0.8 \\ket 1$ and measure it in the computational basis $100$ times. At the end, we expect $0$ to be measured approximately $|0.6 |^2 \\cdot 100= 36$ times, and $1$ to be measured approximately $|0.8|^2 \\cdot 100= 64$ times. Note that since measurements are probabilistic, we do not expect the results to match these values exactly. As we repeat the measurements, the resulting distribution will align with the theoretical probabilities."
|
|
1146
1146
|
},
|
|
1147
1147
|
{
|
|
1148
1148
|
"type": "example",
|
|
1149
1149
|
"id": "single_qubit_measurements__measurement_statistics_demo",
|
|
1150
|
-
"code": "namespace Kata {\n
|
|
1150
|
+
"code": "namespace Kata {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Measurement;\n\n @EntryPoint()\n operation MeasumentStatisticsDemo() : Unit {\n mutable countZero = 0;\n let numRuns = 100;\n use q = Qubit();\n for i in 1 .. numRuns {\n // Prepare the qubit in the superposition state |𝜓❭ = 0.6 |0❭ + 0.8 |1❭\n Ry(2.0 * ArcTan2(0.8, 0.6), q);\n\n // Measure and update the counts according to the outcomes\n if MResetZ(q) == Zero {\n set countZero += 1;\n }\n }\n let countOne = numRuns - countZero;\n\n Message($\"Simulated probability of measuring 0 is 0.{countZero}.\");\n Message($\"Theoretical probability of measuring 0 is 0.36.\");\n Message($\"Simulated probability of measuring 1 is 0.{countOne}.\");\n Message($\"Theoretical probability of measuring 1 is 0.64.\");\n }\n}\n"
|
|
1151
1151
|
},
|
|
1152
1152
|
{
|
|
1153
1153
|
"type": "text-content",
|
|
1154
|
-
"asHtml": "<p>Measurements can be used to distinguish orthogonal states. We start with an exercise for distinguishing between the computational basis states and discuss the general case of arbitrary basis measurements later in the kata.</p>\n",
|
|
1155
|
-
"asMarkdown": "\nMeasurements can be used to distinguish orthogonal states. We start with an exercise for distinguishing between the computational basis states and discuss the general case of arbitrary basis measurements later in the kata."
|
|
1154
|
+
"asHtml": "<p>Measurements can be used to distinguish orthogonal states. We start with an exercise for distinguishing between the computational basis states, and discuss the general case of arbitrary basis measurements later in the kata.</p>\n",
|
|
1155
|
+
"asMarkdown": "\nMeasurements can be used to distinguish orthogonal states. We start with an exercise for distinguishing between the computational basis states, and discuss the general case of arbitrary basis measurements later in the kata."
|
|
1156
1156
|
}
|
|
1157
1157
|
]
|
|
1158
1158
|
},
|
|
@@ -1166,23 +1166,23 @@ export default {
|
|
|
1166
1166
|
"asMarkdown": "**Input:** A qubit which is guaranteed to be in either the $|0\\rangle$ or the $|1\\rangle$ state.\n\n**Output:** `true` if the qubit was in the $|0\\rangle$ state, or `false` if it was in the $|1\\rangle$ state. The state of the qubit at the end of the operation does not matter.\n"
|
|
1167
1167
|
},
|
|
1168
1168
|
"sourceIds": [
|
|
1169
|
+
"single_qubit_measurements__distinguish_0_and_1__Verification.qs",
|
|
1169
1170
|
"KatasLibrary.qs",
|
|
1170
|
-
"
|
|
1171
|
-
"single_qubit_measurements__distinguish_0_and_1__verification.qs"
|
|
1171
|
+
"single_qubit_measurements__Common.qs"
|
|
1172
1172
|
],
|
|
1173
|
-
"placeholderCode": "namespace Kata {\n
|
|
1173
|
+
"placeholderCode": "namespace Kata {\n operation IsQubitZero(q : Qubit) : Bool {\n // Implement your solution here...\n\n return false;\n }\n}\n",
|
|
1174
1174
|
"explainedSolution": {
|
|
1175
1175
|
"type": "explained-solution",
|
|
1176
1176
|
"items": [
|
|
1177
1177
|
{
|
|
1178
1178
|
"type": "text-content",
|
|
1179
|
-
"asHtml": "<p>The input qubit is guaranteed to be either in basis state $|0\\rangle$ or $|1\\rangle$. This means that when measuring the qubit in the computational basis, the measurement will report the input state without any doubt.</p>\n<p>In Q# the operation <
|
|
1180
|
-
"asMarkdown": "\nThe input qubit is guaranteed to be either in basis state $|0\\rangle$ or $|1\\rangle$. This means that when measuring the qubit in the computational basis, the measurement will report the input state without any doubt.\n\nIn Q# the operation
|
|
1179
|
+
"asHtml": "<p>The input qubit is guaranteed to be either in basis state $|0\\rangle$ or $|1\\rangle$. This means that when measuring the qubit in the computational basis, the measurement will report the input state without any doubt.</p>\n<p>In Q# the operation <code>M</code> can be used to measure a single qubit in the computational basis. The measurement result is a value of type <code>Result</code> - the operation <code>M</code> will return <code>One</code> if the input qubit was in the $|1\\rangle$ state and <code>Zero</code> if the input qubit was in the $|0\\rangle$ state. Since we need to encode the first case as <code>false</code> and the second one as <code>true</code>, we can return the result of equality comparison between measurement result and <code>Zero</code>.</p>\n",
|
|
1180
|
+
"asMarkdown": "\nThe input qubit is guaranteed to be either in basis state $|0\\rangle$ or $|1\\rangle$. This means that when measuring the qubit in the computational basis, the measurement will report the input state without any doubt.\n\nIn Q# the operation `M` can be used to measure a single qubit in the computational basis. The measurement result is a value of type `Result` - the operation `M` will return `One` if the input qubit was in the $|1\\rangle$ state and `Zero` if the input qubit was in the $|0\\rangle$ state. Since we need to encode the first case as `false` and the second one as `true`, we can return the result of equality comparison between measurement result and `Zero`."
|
|
1181
1181
|
},
|
|
1182
1182
|
{
|
|
1183
1183
|
"type": "solution",
|
|
1184
1184
|
"id": "single_qubit_measurements__distinguish_0_and_1_solution",
|
|
1185
|
-
"code": "namespace Kata {\n operation IsQubitZero(q : Qubit) : Bool {\n return M(q) == Zero;\n }\n
|
|
1185
|
+
"code": "namespace Kata {\n operation IsQubitZero(q : Qubit) : Bool {\n return M(q) == Zero;\n }\n}\n"
|
|
1186
1186
|
}
|
|
1187
1187
|
]
|
|
1188
1188
|
}
|
|
@@ -1194,8 +1194,8 @@ export default {
|
|
|
1194
1194
|
"items": [
|
|
1195
1195
|
{
|
|
1196
1196
|
"type": "text-content",
|
|
1197
|
-
"asHtml": "<p>So far, we have discussed measurements done in the computational basis, that is, the ${ \\ket 0, \\ket 1}$ basis.</p>\n<p>It is also possible to implement measurements in other orthogonal bases, such as the Pauli X basis, which consists of the two vectors $\\ket + = \\frac1{\\sqrt2} \\big(\\ket 0 +\\ket 1\\big)$, and $\\ket - = \\frac1{\\sqrt2} \\big(\\ket 0 -\\ket 1\\big)$. Q# has a built-in operation <
|
|
1198
|
-
"asMarkdown": "\nSo far, we have discussed measurements done in the computational basis, that is, the $\\{ \\ket 0, \\ket 1\\}$ basis.\n\nIt is also possible to implement measurements in other orthogonal bases, such as the Pauli X basis, which consists of the two vectors $\\ket + = \\frac1{\\sqrt2} \\big(\\ket 0 +\\ket 1\\big)$, and $\\ket - = \\frac1{\\sqrt2} \\big(\\ket 0 -\\ket 1\\big)$. Q# has a built-in operation
|
|
1197
|
+
"asHtml": "<p>So far, we have discussed measurements done in the computational basis, that is, the ${ \\ket 0, \\ket 1}$ basis.</p>\n<p>It is also possible to implement measurements in other orthogonal bases, such as the Pauli X basis, which consists of the two vectors $\\ket + = \\frac1{\\sqrt2} \\big(\\ket 0 +\\ket 1\\big)$, and $\\ket - = \\frac1{\\sqrt2} \\big(\\ket 0 -\\ket 1\\big)$. Q# has a built-in operation <code>Measure</code> for measurements in the Pauli bases.</p>\n<blockquote>\n<p>The <code>Measure</code> operation can also be used for measuring multiple qubits in a multi-qubit system; however, in this kata we only consider measurements for single-qubit systems.</p>\n</blockquote>\n<p>The eigenvalues of a Pauli matrix are $\\pm 1$, with one eigenvector corresponding to each eigenvalue. For any chosen Pauli basis, the <code>Measure</code> operation returns <code>Zero</code> if the measurement outcome corresponds to the eigenvalue $+1$, and returns <code>One</code> if the measurement outcome corresponds to the eigenvalue $-1$. As in the case of the computational basis measurements, the wave function of the qubit collapses to the corresponding state after the measurement is executed.</p>\n<p>The probabilities of the outcomes are defined using a similar rule: to measure a state $\\ket \\psi$ in a Pauli basis $\\{ \\ket {b_0}, \\ket {b_1} \\}$, we represent it as a linear combination of the basis vectors\n$$\\ket \\psi = c_0 \\ket {b_0} + c_1 \\ket {b_1}$$</p>\n<p>The probabilities of outcomes $\\ket{b_0}$ and $\\ket{b_1}$ will be defined as $|c_0|^2$, and $|c_1|^2$, respectively.</p>\n<blockquote>\n<p>Computational basis measurement is often referred to as measurement in Pauli Z basis. Indeed, the eigenvectors of the Z gate are $\\ket 0$ and $\\ket 1$, with eigenvalues $+1$ and $-1$, respectively.</p>\n</blockquote>\n",
|
|
1198
|
+
"asMarkdown": "\nSo far, we have discussed measurements done in the computational basis, that is, the $\\{ \\ket 0, \\ket 1\\}$ basis.\n\nIt is also possible to implement measurements in other orthogonal bases, such as the Pauli X basis, which consists of the two vectors $\\ket + = \\frac1{\\sqrt2} \\big(\\ket 0 +\\ket 1\\big)$, and $\\ket - = \\frac1{\\sqrt2} \\big(\\ket 0 -\\ket 1\\big)$. Q# has a built-in operation `Measure` for measurements in the Pauli bases.\n\n> The `Measure` operation can also be used for measuring multiple qubits in a multi-qubit system; however, in this kata we only consider measurements for single-qubit systems.\n\nThe eigenvalues of a Pauli matrix are $\\pm 1$, with one eigenvector corresponding to each eigenvalue. For any chosen Pauli basis, the `Measure` operation returns `Zero` if the measurement outcome corresponds to the eigenvalue $+1$, and returns `One` if the measurement outcome corresponds to the eigenvalue $-1$. As in the case of the computational basis measurements, the wave function of the qubit collapses to the corresponding state after the measurement is executed.\n\nThe probabilities of the outcomes are defined using a similar rule: to measure a state $\\ket \\psi$ in a Pauli basis $\\\\{ \\ket {b_0}, \\ket {b_1} \\\\}$, we represent it as a linear combination of the basis vectors\n$$\\ket \\psi = c_0 \\ket {b_0} + c_1 \\ket {b_1}$$\n\nThe probabilities of outcomes $\\ket{b_0}$ and $\\ket{b_1}$ will be defined as $|c_0|^2$, and $|c_1|^2$, respectively.\n\n> Computational basis measurement is often referred to as measurement in Pauli Z basis. Indeed, the eigenvectors of the Z gate are $\\ket 0$ and $\\ket 1$, with eigenvalues $+1$ and $-1$, respectively."
|
|
1199
1199
|
}
|
|
1200
1200
|
]
|
|
1201
1201
|
},
|
|
@@ -1205,27 +1205,27 @@ export default {
|
|
|
1205
1205
|
"title": "Distinguish |+〉 and |-〉",
|
|
1206
1206
|
"description": {
|
|
1207
1207
|
"type": "text-content",
|
|
1208
|
-
"asHtml": "<p><strong>Input</strong>: A qubit which is guaranteed to be in either the $\\ket +$ state, or the $\\ket -$ state.</p>\n<p><strong>Output</strong>: <code>true</code> if the qubit is in the $\\ket -$ state, or <code>false</code> if it was in the $\\ket +$ state.</p>\n<blockquote>\n<p>To perform a single-qubit measurement in a certain Pauli basis using the <code>Measure</code> operation,\nyou need to pass it two parameters: first, an array of one <code>Pauli</code> constant (<code>PauliX</code>, <code>PauliY</code> or <code>PauliZ</code>), and second, an array of one qubit you want to measure.</p>\n</blockquote>\n",
|
|
1209
|
-
"asMarkdown": "**Input**: A qubit which is guaranteed to be in either the $\\ket +$ state, or the $\\ket -$ state.\n\n**Output**: `true` if the qubit is in the $\\ket -$ state, or `false` if it was in the $\\ket +$ state.\n\n> To perform a single-qubit measurement in a certain Pauli basis using the `Measure` operation,\n> you need to pass it two parameters: first, an array of one `Pauli` constant (`PauliX`, `PauliY` or `PauliZ`), and second, an array of one qubit you want to measure.\n"
|
|
1208
|
+
"asHtml": "<p><strong>Input</strong>: A qubit which is guaranteed to be in either the $\\ket +$ state, or the $\\ket -$ state.</p>\n<p><strong>Output</strong>: <code>true</code> if the qubit is in the $\\ket -$ state, or <code>false</code> if it was in the $\\ket +$ state. The state of the qubit at the end of the operation does not matter.</p>\n<blockquote>\n<p>To perform a single-qubit measurement in a certain Pauli basis using the <code>Measure</code> operation,\nyou need to pass it two parameters: first, an array of one <code>Pauli</code> constant (<code>PauliX</code>, <code>PauliY</code> or <code>PauliZ</code>), and second, an array of one qubit you want to measure.</p>\n</blockquote>\n",
|
|
1209
|
+
"asMarkdown": "**Input**: A qubit which is guaranteed to be in either the $\\ket +$ state, or the $\\ket -$ state.\n\n**Output**: `true` if the qubit is in the $\\ket -$ state, or `false` if it was in the $\\ket +$ state. The state of the qubit at the end of the operation does not matter.\n\n> To perform a single-qubit measurement in a certain Pauli basis using the `Measure` operation,\n> you need to pass it two parameters: first, an array of one `Pauli` constant (`PauliX`, `PauliY` or `PauliZ`), and second, an array of one qubit you want to measure.\n"
|
|
1210
1210
|
},
|
|
1211
1211
|
"sourceIds": [
|
|
1212
|
+
"single_qubit_measurements__distinguish_plus_and_minus__Verification.qs",
|
|
1212
1213
|
"KatasLibrary.qs",
|
|
1213
|
-
"
|
|
1214
|
-
"single_qubit_measurements__distinguish_plus_and_minus__verification.qs"
|
|
1214
|
+
"single_qubit_measurements__Common.qs"
|
|
1215
1215
|
],
|
|
1216
|
-
"placeholderCode": "namespace Kata {\n
|
|
1216
|
+
"placeholderCode": "namespace Kata {\n operation IsQubitMinus(q : Qubit) : Bool {\n // Implement your solution here...\n\n return false;\n }\n}\n",
|
|
1217
1217
|
"explainedSolution": {
|
|
1218
1218
|
"type": "explained-solution",
|
|
1219
1219
|
"items": [
|
|
1220
1220
|
{
|
|
1221
1221
|
"type": "text-content",
|
|
1222
|
-
"asHtml": "<p>The input qubit is guaranteed to be either in basis state $|+\\rangle$ or $|-\\rangle$. This means that when measuring the qubit in the Pauli X basis, the measurement will report the input state without any doubt. (Recall that these states are eigenstates for the Pauli X matrix).</p>\n<p>In Q# the operation <
|
|
1223
|
-
"asMarkdown": "\nThe input qubit is guaranteed to be either in basis state $|+\\rangle$ or $|-\\rangle$. This means that when measuring the qubit in the Pauli X basis, the measurement will report the input state without any doubt. (Recall that these states are eigenstates for the Pauli X matrix).\n\nIn Q# the operation
|
|
1222
|
+
"asHtml": "<p>The input qubit is guaranteed to be either in basis state $|+\\rangle$ or $|-\\rangle$. This means that when measuring the qubit in the Pauli X basis, the measurement will report the input state without any doubt. (Recall that these states are eigenstates for the Pauli X matrix).</p>\n<p>In Q# the operation <code>Measure</code> can be used to measure a qubit in Pauli basis of the user's choice. The operation returns a value of type <code>Result</code> and is <code>Zero</code> if the measured state corresponds to the eigenvalue $+1$, and <code>One</code> if it corresponds to the eigenvalue $-1$ of the Pauli operator.</p>\n<p>Since the states $\\ket +$ and $\\ket -$ correspond to the eigenvalues $+1$ and $-1$ of the Pauli X operator, we can return the result of equality comparison between the measurement result and <code>One</code>.\nNote that since <code>Measure</code> operation generally works with multiple qubits to perform multi-qubit measurements, it takes array parameters. To do a single-qubit measurement, you need to pass two arrays of one element, <code>[PauliX]</code> and <code>[q]</code>, rather than individual values.</p>\n",
|
|
1223
|
+
"asMarkdown": "\nThe input qubit is guaranteed to be either in basis state $|+\\rangle$ or $|-\\rangle$. This means that when measuring the qubit in the Pauli X basis, the measurement will report the input state without any doubt. (Recall that these states are eigenstates for the Pauli X matrix).\n\nIn Q# the operation `Measure` can be used to measure a qubit in Pauli basis of the user's choice. The operation returns a value of type `Result` and is `Zero` if the measured state corresponds to the eigenvalue $+1$, and `One` if it corresponds to the eigenvalue $-1$ of the Pauli operator.\n\nSince the states $\\ket +$ and $\\ket -$ correspond to the eigenvalues $+1$ and $-1$ of the Pauli X operator, we can return the result of equality comparison between the measurement result and `One`.\nNote that since `Measure` operation generally works with multiple qubits to perform multi-qubit measurements, it takes array parameters. To do a single-qubit measurement, you need to pass two arrays of one element, `[PauliX]` and `[q]`, rather than individual values."
|
|
1224
1224
|
},
|
|
1225
1225
|
{
|
|
1226
1226
|
"type": "solution",
|
|
1227
1227
|
"id": "single_qubit_measurements__distinguish_plus_and_minus_solution",
|
|
1228
|
-
"code": "namespace Kata {\n operation IsQubitMinus(q : Qubit) : Bool {\n return Measure([PauliX], [q]) == One;\n }\n
|
|
1228
|
+
"code": "namespace Kata {\n operation IsQubitMinus(q : Qubit) : Bool {\n return Measure([PauliX], [q]) == One;\n }\n}\n"
|
|
1229
1229
|
}
|
|
1230
1230
|
]
|
|
1231
1231
|
}
|
|
@@ -1237,8 +1237,8 @@ export default {
|
|
|
1237
1237
|
"items": [
|
|
1238
1238
|
{
|
|
1239
1239
|
"type": "text-content",
|
|
1240
|
-
"asHtml": "<p>It is possible to measure a qubit in orthogonal bases other than the Pauli bases. Suppose one wants to measure a qubit in an orthonormal basis $\\ket {b_0}$ and $\\ket {b_1}$. Let the state of the qubit be represented by the normalized vector $\\ket \\psi$. Then, one can always express the state in terms of the basis vectors $\\ket{b_0}$ and $\\ket{b_1}$, i.e., there exist complex numbers $c_0, c_1$, such that\n$$\n\\ket \\psi = c_0 \\ket {b_0} + c_1 \\ket {b_1}.\n$$\nThe rule for obtaining the probabilities of measurement outcomes is exactly the same as that for the
|
|
1241
|
-
"asMarkdown": "\nIt is possible to measure a qubit in orthogonal bases other than the Pauli bases. Suppose one wants to measure a qubit in an orthonormal basis $\\ket {b_0}$ and $\\ket {b_1}$. Let the state of the qubit be represented by the normalized vector $\\ket \\psi$. Then, one can always express the state in terms of the basis vectors $\\ket{b_0}$ and $\\ket{b_1}$, i.e., there exist complex numbers $c_0, c_1$, such that\n$$\n\\ket \\psi = c_0 \\ket {b_0} + c_1 \\ket {b_1}.\n$$\nThe rule for obtaining the probabilities of measurement outcomes is exactly the same as that for the
|
|
1240
|
+
"asHtml": "<p>It is possible to measure a qubit in orthogonal bases other than the Pauli bases. Suppose one wants to measure a qubit in an orthonormal basis $\\ket {b_0}$ and $\\ket {b_1}$. Let the state of the qubit be represented by the normalized vector $\\ket \\psi$. Then, one can always express the state in terms of the basis vectors $\\ket{b_0}$ and $\\ket{b_1}$, i.e., there exist complex numbers $c_0, c_1$, such that\n$$\n\\ket \\psi = c_0 \\ket {b_0} + c_1 \\ket {b_1}.\n$$\nThe rule for obtaining the probabilities of measurement outcomes is exactly the same as that for the computational basis measurement. For a measurement in a $\\{ b_0, b_1\\}$ basis we get</p>\n<ul>\n<li>Outcome $b_0$ with probability $|c_0|^2$ and the post-measurement qubit state of $\\ket {b_0}$</li>\n<li>Outcome $b_1$ with probability $|c_1|^2$ and the post-measurement qubit state of $\\ket {b_1}$</li>\n</ul>\n<p>This can be summarized in the following table:</p>\n<table>\n <tr>\n <th>Measurement outcome</th>\n <th>Probability of outcome</th>\n <th>State after measurement</th>\n </tr>\n <tr>\n <td>$b_0$</td>\n <td>$|c_0|^2$</td>\n <td>$\\ket{b_0}$</td>\n </tr>\n <tr>\n <td>$b_1$</td>\n <td>$|c_1|^2$</td>\n <td>$\\ket{b_1}$</td>\n </tr>\n</table>\n\n<p>As before, the assumption of $\\ket \\psi$ being normalized is important, since it guarantees that the two probabilities add to $1$.</p>\n<blockquote>\n<p>As you may recall, a global phase is said to be hidden or unobservable.\nThis is explained by the fact that global phases have no impact on quantum measurements. For example, consider two isolated qubits which are in (normalized) states $\\ket \\psi$ and $e^{i\\theta}\\ket \\psi$.\nIf both are measured in an orthogonal basis $\\{ \\ket{b_0},\\ket{b_1}\\}$, the probabilities of measuring $b_0$ or $b_1$ are identical in both cases, since $|\\bra{b_i}\\ket{\\psi}|^2 = |\\bra{b_i}e^{i\\theta}\\ket{\\psi}|^2 $.\nSimilarly, for either qubit, if $b_i$ is the measurement outcome, the post-measurement state of the qubit is $\\ket{b_i}$. Hence, the measurements are independent of the global phase $\\theta$.</p>\n</blockquote>\n<h2>Measurements as Projection Operations</h2>\n<p>Just as qubits are represented by vectors and quantum gates are represented by matrices, quantum measurements are represented by orthogonal projection operators. An orthogonal projection operator is a matrix $P$ which satisfies the following property:\n$$\nP^2 = P^\\dagger = P.\n$$\n(As usual, the $\\dagger$ symbol denotes conjugate transposition.)</p>\n<p>As the name implies, orthogonal projection operators project the state of the qubit onto an orthogonal subspace. Using the ket-bra representation, one can represent a projection matrix in the Dirac notation.\nFor example, one may construct a projector onto the $\\ket{0}$ subspace as:\n$$\nP = \\ket 0 \\bra 0 \\equiv \\begin{bmatrix} 1 & 0 \\\\ 0 & 0\\end{bmatrix}.\n$$</p>\n<p>A measurement in an orthogonal basis $\\{ \\ket{b_0}, \\ket{b_1}\\}$ is described by a pair of projectors $P_0 = \\ket{b_0}\\bra{b_0}$ and $P_1 = \\ket{b_1}\\bra{b_1}$. Since $\\ket{b_0}$ and $\\ket{b_1}$ are orthogonal, their projectors are also orthogonal, i.e., $P_0 P_1 = P_1 P_0 = 0$. The rules for measurements in this basis can then be summarized as follows:</p>\n<ul>\n<li>Measuring a qubit in a state $\\ket \\psi$ is done by picking one of these projection operators at random.</li>\n<li>Projection $P_0$ is chosen with probability $|P_0 \\ket{\\psi}|^2$, and the projector $P_1$ is chosen with probability $|P_1\\ket{\\psi}|^2.$</li>\n<li>If projector $P_0$ is chosen, the post-measurement state of the qubit is given by\n $$\n \\frac1{|P_0 \\ket{\\psi}|}P_0 \\ket\\psi,\n $$\n and similarly for $P_1$.</li>\n</ul>\n<p>Although this formalism looks different from the previous sections, it is in fact equivalent. If $\\ket \\psi = c_0 \\ket{b_0} + c_1 \\ket{b_1}$, we have\n$$\nP_0 \\ket \\psi = c_0 \\ket{b_0}, \\text{so that } | P_0\\ket \\psi| = c_0,\n$$\nand similarly,\n$$\nP_1 \\ket \\psi = c_1 \\ket{b_1}, \\text{so that } |P_1\\ket \\psi| = c_1.\n$$</p>\n<p>Thus, as before, the probability of measuring $b_0$ is $|P_0\\ket\\psi|^2 = |c_0|^2$, and the probability of measuring $b_1$ is $|P_1\\ket\\psi|^2 = |c_1|^2$. Similarly, one can verify that the post-measurement outcomes are also $\\ket{b_0}$ and $\\ket{b_1}$, respectively (up to unobservable global phases).</p>\n<p>Although the projector formalism for single-qubit systems may seem superfluous, its importance will become clear later, when we consider measurements for multi-qubit systems.</p>\n<h2>Arbitrary Basis Measurements Implementation</h2>\n<p>In the previous section, we discussed measurements in Pauli bases using the built-in <code>Measure</code> operation. We will now show that it is always possible to measure a qubit in any orthogonal basis using just unitary rotation matrices and computation basis measurements.</p>\n<p>Consider a state $ \\ket \\psi = c_0 \\ket {b_0} + c_1 \\ket {b_1} $ which we would like to measure in an orthonormal basis $\\{ \\ket{b_0}, \\ket{b_1}\\}$. First, we construct the following unitary matrix:\n$$\nU = \\ket{0} \\bra{b_0} + \\ket{1} \\bra{b_1}\n$$</p>\n<p>The conjugate transpose of this unitary is the operator\n$$\nU^\\dagger = \\ket{b_0} \\bra{0} + \\ket{b_1} \\bra{1}\n$$</p>\n<p>(One may verify that $U$ is indeed a unitary matrix, by checking that $U^\\dagger U = U U^\\dagger = I$)</p>\n<p>Note that the effect of these matrices on the two bases is the following:\n$$U\\ket{b_0} = \\ket{0}$$\n$$U\\ket{b_1} = \\ket{1}$$\n$$U^\\dagger \\ket{0} = \\ket{b_0}$$\n$$U^\\dagger \\ket{1} = \\ket{b_1}$$</p>\n<p>In order to implement a measurement in the $\\{ \\ket{b_0}, \\ket{b_1} \\}$ basis, we do the following:</p>\n<ol>\n<li>Apply $U$ to $\\ket \\psi$.<br>The resulting state is $U\\ket \\psi = c_0 \\ket 0 + c_1 \\ket 1 $.</li>\n<li>Measure the state $U\\ket{\\psi}$ in the computational basis.<br>The outcomes $0$ and $1$ occur with probabilities $|c_0|^2$ and $|c_1|^2$.</li>\n<li>Apply $U^\\dagger$ to the post-measurement state.<br>This transforms the states $\\ket 0$ and $\\ket 1$ to the states $\\ket{b_0}$ and $\\ket{b_1}$, respectively.</li>\n</ol>\n<p>Thus, $b_0$ and $b_1$ are measured with probabilities $|c_0|^2$ and $|c_1|^2$, respectively, with the end state being $\\ket{b_0}$ and $\\ket{b_1}$ - which is exactly the measurement we want to implement.</p>\n<p>This procedure can be used to distinguish arbitrary orthogonal states as well, as will become clear from the following exercises.</p>\n<h2>🔎 Analyze</h2>\n<p><strong>The outcome probabilities for a measurement in a specified basis</strong></p>\n<ol>\n<li><p>What are the outcome probabilities of measuring a qubit in the $\\ket{0}$ state in the Pauli X basis, i.e., the ${ \\ket +, \\ket -}$ basis?</p>\n</li>\n<li><p>What are the outcome probabilities of measuring a qubit in the $0.6\\ket{0} + 0.8 \\ket{1}$ state in the Pauli Y basis, i.e., the ${ \\ket i, \\ket{-i}}$ basis?</p>\n</li>\n</ol>\n<details>\n<summary><b>Solution</b></summary>\n\n<ol>\n<li><p>To find the probabilities of measuring $+$ and $-$, we first need to express the state $\\ket 0$ in terms of $\\ket +$ and $\\ket -$. Using the fact that $\\ket{\\pm} = \\frac{1}{\\sqrt{2}} (\\ket{0} \\pm \\ket{1})$, we can show that\n $$\n \\ket 0 = \\frac{1}{\\sqrt{2}} \\ket{+} + \\frac{1}{\\sqrt{2}} \\ket{-}.\n $$\n Thus, the probability of measuring $+$ is $|\\frac1{\\sqrt2}|^2 = 0.5$, and similarly, the probability of measuring $-$ is $0.5$.</p>\n</li>\n<li><p>Similar to the first part, we need to express the state $\\ket \\psi = 0.6 \\ket 0 + 0.8 \\ket 1$ in the $\\ket{\\pm i}$ basis. For this calculation, we use the projection matrix approach.</p>\n<p> First, we recall that the states $\\ket{\\pm i}$ are given by\n $$\n \\ket{\\pm i} = \\frac1{\\sqrt2} (\\ket 0 \\pm i \\ket 1).\n $$</p>\n<p> We can now construct the two projectors $P_{\\pm i}$ onto states $\\ket {\\pm i}$ as follows:\n $$P_{i} = \\ket{i}\\bra{i} = \\frac{1}{2} \\begin{bmatrix} 1 \\\\ i \\end{bmatrix} \\begin{bmatrix} 1 & -i \\end{bmatrix} = \\frac{1}{2} \\begin{bmatrix}1 & -i \\\\ i & 1\\end{bmatrix},$$\n $$P_{-i} = \\ket{-i}\\bra{-i} = \\frac{1}{2} \\begin{bmatrix} 1 \\\\ -i \\end{bmatrix} \\begin{bmatrix} 1 & i \\end{bmatrix} = \\frac{1}{2} \\begin{bmatrix}1 & i \\\\ -i & 1\\end{bmatrix}.$$</p>\n<p> Recalling that the probabilities of measuring $\\pm i$ are equal to the norm of the vectors $P_{\\pm i}\\ket \\psi$, we now apply $P_{\\pm i}$ to $\\ket \\psi$:\n $$P_{+i} \\ket \\psi = \\frac{1}{2} \\begin{bmatrix}1 & -i \\\\ i & 1\\end{bmatrix} \\begin{bmatrix} 0.6 \\\\ 0.8 \\end{bmatrix} = \\frac{1}{2} \\begin{bmatrix} 0.6 - 0.8i \\\\ 0.8 + 0.6i \\end{bmatrix},$$\n $$P_{-i} \\ket \\psi = \\frac{1}{2} \\begin{bmatrix}1 & i \\\\ -i & 1\\end{bmatrix} \\begin{bmatrix} 0.6 \\\\ 0.8 \\end{bmatrix} = \\frac{1}{2} \\begin{bmatrix} 0.6 + 0.8i \\\\ 0.8 - 0.6i \\end{bmatrix}.$$</p>\n<p> Hence, the probabilities of measuring $\\pm i$, which we denote by $p(\\pm i)$, are:\n $$p(+i) = |P_{+i} \\ket \\psi|^2 = \\frac{1}{4}(|0.6 - 0.8i|^2 + |0.8 + 0.6i|^2) = \\frac{1}{2},$$\n $$p(-i) = |P_{-i} \\ket \\psi|^2 = \\frac{1}{4}(|0.6 + 0.8i|^2 + |0.8 - 0.6i|^2) = \\frac{1}{2}.$$</p>\n</li>\n</ol>\n</details>",
|
|
1241
|
+
"asMarkdown": "\nIt is possible to measure a qubit in orthogonal bases other than the Pauli bases. Suppose one wants to measure a qubit in an orthonormal basis $\\ket {b_0}$ and $\\ket {b_1}$. Let the state of the qubit be represented by the normalized vector $\\ket \\psi$. Then, one can always express the state in terms of the basis vectors $\\ket{b_0}$ and $\\ket{b_1}$, i.e., there exist complex numbers $c_0, c_1$, such that\n$$\n\\ket \\psi = c_0 \\ket {b_0} + c_1 \\ket {b_1}.\n$$\nThe rule for obtaining the probabilities of measurement outcomes is exactly the same as that for the computational basis measurement. For a measurement in a $\\\\{ b_0, b_1\\\\}$ basis we get\n\n- Outcome $b_0$ with probability $|c_0|^2$ and the post-measurement qubit state of $\\ket {b_0}$\n- Outcome $b_1$ with probability $|c_1|^2$ and the post-measurement qubit state of $\\ket {b_1}$\n\nThis can be summarized in the following table:\n<table>\n <tr>\n <th>Measurement outcome</th>\n <th>Probability of outcome</th>\n <th>State after measurement</th>\n </tr>\n <tr>\n <td>$b_0$</td>\n <td>$|c_0|^2$</td>\n <td>$\\ket{b_0}$</td>\n </tr>\n <tr>\n <td>$b_1$</td>\n <td>$|c_1|^2$</td>\n <td>$\\ket{b_1}$</td>\n </tr>\n</table>\n\nAs before, the assumption of $\\ket \\psi$ being normalized is important, since it guarantees that the two probabilities add to $1$.\n\n> As you may recall, a global phase is said to be hidden or unobservable.\nThis is explained by the fact that global phases have no impact on quantum measurements. For example, consider two isolated qubits which are in (normalized) states $\\ket \\psi$ and $e^{i\\theta}\\ket \\psi$.\nIf both are measured in an orthogonal basis $\\\\{ \\ket{b_0},\\ket{b_1}\\\\}$, the probabilities of measuring $b_0$ or $b_1$ are identical in both cases, since $|\\bra{b_i}\\ket{\\psi}|^2 = |\\bra{b_i}e^{i\\theta}\\ket{\\psi}|^2 $.\nSimilarly, for either qubit, if $b_i$ is the measurement outcome, the post-measurement state of the qubit is $\\ket{b_i}$. Hence, the measurements are independent of the global phase $\\theta$.\n\n## Measurements as Projection Operations\n\nJust as qubits are represented by vectors and quantum gates are represented by matrices, quantum measurements are represented by orthogonal projection operators. An orthogonal projection operator is a matrix $P$ which satisfies the following property:\n$$\nP^2 = P^\\dagger = P.\n$$\n(As usual, the $\\dagger$ symbol denotes conjugate transposition.)\n\nAs the name implies, orthogonal projection operators project the state of the qubit onto an orthogonal subspace. Using the ket-bra representation, one can represent a projection matrix in the Dirac notation.\nFor example, one may construct a projector onto the $\\ket{0}$ subspace as:\n$$\nP = \\ket 0 \\bra 0 \\equiv \\begin{bmatrix} 1 & 0 \\\\\\ 0 & 0\\end{bmatrix}.\n$$\n\nA measurement in an orthogonal basis $\\\\{ \\ket{b_0}, \\ket{b_1}\\\\}$ is described by a pair of projectors $P_0 = \\ket{b_0}\\bra{b_0}$ and $P_1 = \\ket{b_1}\\bra{b_1}$. Since $\\ket{b_0}$ and $\\ket{b_1}$ are orthogonal, their projectors are also orthogonal, i.e., $P_0 P_1 = P_1 P_0 = 0$. The rules for measurements in this basis can then be summarized as follows:\n\n- Measuring a qubit in a state $\\ket \\psi$ is done by picking one of these projection operators at random.\n- Projection $P_0$ is chosen with probability $|P_0 \\ket{\\psi}|^2$, and the projector $P_1$ is chosen with probability $|P_1\\ket{\\psi}|^2.$\n- If projector $P_0$ is chosen, the post-measurement state of the qubit is given by\n $$\n \\frac1{|P_0 \\ket{\\psi}|}P_0 \\ket\\psi,\n $$\n and similarly for $P_1$.\n\nAlthough this formalism looks different from the previous sections, it is in fact equivalent. If $\\ket \\psi = c_0 \\ket{b_0} + c_1 \\ket{b_1}$, we have\n$$\nP_0 \\ket \\psi = c_0 \\ket{b_0}, \\text{so that } | P_0\\ket \\psi| = c_0,\n$$\nand similarly,\n$$\nP_1 \\ket \\psi = c_1 \\ket{b_1}, \\text{so that } |P_1\\ket \\psi| = c_1.\n$$\n\nThus, as before, the probability of measuring $b_0$ is $|P_0\\ket\\psi|^2 = |c_0|^2$, and the probability of measuring $b_1$ is $|P_1\\ket\\psi|^2 = |c_1|^2$. Similarly, one can verify that the post-measurement outcomes are also $\\ket{b_0}$ and $\\ket{b_1}$, respectively (up to unobservable global phases).\n\nAlthough the projector formalism for single-qubit systems may seem superfluous, its importance will become clear later, when we consider measurements for multi-qubit systems.\n\n## Arbitrary Basis Measurements Implementation\n\nIn the previous section, we discussed measurements in Pauli bases using the built-in `Measure` operation. We will now show that it is always possible to measure a qubit in any orthogonal basis using just unitary rotation matrices and computation basis measurements.\n\nConsider a state $ \\ket \\psi = c_0 \\ket {b_0} + c_1 \\ket {b_1} $ which we would like to measure in an orthonormal basis $\\\\{ \\ket{b_0}, \\ket{b_1}\\\\}$. First, we construct the following unitary matrix:\n$$\nU = \\ket{0} \\bra{b_0} + \\ket{1} \\bra{b_1}\n$$\n\nThe conjugate transpose of this unitary is the operator\n$$\nU^\\dagger = \\ket{b_0} \\bra{0} + \\ket{b_1} \\bra{1}\n$$\n\n(One may verify that $U$ is indeed a unitary matrix, by checking that $U^\\dagger U = U U^\\dagger = I$)\n\nNote that the effect of these matrices on the two bases is the following:\n$$U\\ket{b_0} = \\ket{0}$$\n$$U\\ket{b_1} = \\ket{1}$$\n$$U^\\dagger \\ket{0} = \\ket{b_0}$$\n$$U^\\dagger \\ket{1} = \\ket{b_1}$$\n\nIn order to implement a measurement in the $\\\\{ \\ket{b_0}, \\ket{b_1} \\\\}$ basis, we do the following:\n\n1. Apply $U$ to $\\ket \\psi$. \n The resulting state is $U\\ket \\psi = c_0 \\ket 0 + c_1 \\ket 1 $.\n2. Measure the state $U\\ket{\\psi}$ in the computational basis. \n The outcomes $0$ and $1$ occur with probabilities $|c_0|^2$ and $|c_1|^2$.\n3. Apply $U^\\dagger$ to the post-measurement state. \n This transforms the states $\\ket 0$ and $\\ket 1$ to the states $\\ket{b_0}$ and $\\ket{b_1}$, respectively.\n\nThus, $b_0$ and $b_1$ are measured with probabilities $|c_0|^2$ and $|c_1|^2$, respectively, with the end state being $\\ket{b_0}$ and $\\ket{b_1}$ - which is exactly the measurement we want to implement.\n\nThis procedure can be used to distinguish arbitrary orthogonal states as well, as will become clear from the following exercises.\n\n## 🔎 Analyze\n\n**The outcome probabilities for a measurement in a specified basis**\n\n1. What are the outcome probabilities of measuring a qubit in the $\\ket{0}$ state in the Pauli X basis, i.e., the $\\{ \\ket +, \\ket -\\}$ basis?\n\n2. What are the outcome probabilities of measuring a qubit in the $0.6\\ket{0} + 0.8 \\ket{1}$ state in the Pauli Y basis, i.e., the $\\{ \\ket i, \\ket{-i}\\}$ basis?\n\n<details>\n<summary><b>Solution</b></summary>\n\n1. To find the probabilities of measuring $+$ and $-$, we first need to express the state $\\ket 0$ in terms of $\\ket +$ and $\\ket -$. Using the fact that $\\ket{\\pm} = \\frac{1}{\\sqrt{2}} (\\ket{0} \\pm \\ket{1})$, we can show that\n $$\n \\ket 0 = \\frac{1}{\\sqrt{2}} \\ket{+} + \\frac{1}{\\sqrt{2}} \\ket{-}.\n $$\n Thus, the probability of measuring $+$ is $|\\frac1{\\sqrt2}|^2 = 0.5$, and similarly, the probability of measuring $-$ is $0.5$.\n\n2. Similar to the first part, we need to express the state $\\ket \\psi = 0.6 \\ket 0 + 0.8 \\ket 1$ in the $\\ket{\\pm i}$ basis. For this calculation, we use the projection matrix approach.\n\n First, we recall that the states $\\ket{\\pm i}$ are given by\n $$\n \\ket{\\pm i} = \\frac1{\\sqrt2} (\\ket 0 \\pm i \\ket 1).\n $$\n\n We can now construct the two projectors $P_{\\pm i}$ onto states $\\ket {\\pm i}$ as follows:\n $$P_{i} = \\ket{i}\\bra{i} = \\frac{1}{2} \\begin{bmatrix} 1 \\\\\\\\ i \\end{bmatrix} \\begin{bmatrix} 1 & -i \\end{bmatrix} = \\frac{1}{2} \\begin{bmatrix}1 & -i \\\\\\\\ i & 1\\end{bmatrix},$$\n $$P_{-i} = \\ket{-i}\\bra{-i} = \\frac{1}{2} \\begin{bmatrix} 1 \\\\\\\\ -i \\end{bmatrix} \\begin{bmatrix} 1 & i \\end{bmatrix} = \\frac{1}{2} \\begin{bmatrix}1 & i \\\\\\\\ -i & 1\\end{bmatrix}.$$\n\n Recalling that the probabilities of measuring $\\pm i$ are equal to the norm of the vectors $P_{\\pm i}\\ket \\psi$, we now apply $P_{\\pm i}$ to $\\ket \\psi$:\n $$P_{+i} \\ket \\psi = \\frac{1}{2} \\begin{bmatrix}1 & -i \\\\\\\\ i & 1\\end{bmatrix} \\begin{bmatrix} 0.6 \\\\\\\\ 0.8 \\end{bmatrix} = \\frac{1}{2} \\begin{bmatrix} 0.6 - 0.8i \\\\\\\\ 0.8 + 0.6i \\end{bmatrix},$$\n $$P_{-i} \\ket \\psi = \\frac{1}{2} \\begin{bmatrix}1 & i \\\\\\\\ -i & 1\\end{bmatrix} \\begin{bmatrix} 0.6 \\\\\\\\ 0.8 \\end{bmatrix} = \\frac{1}{2} \\begin{bmatrix} 0.6 + 0.8i \\\\\\\\ 0.8 - 0.6i \\end{bmatrix}.$$\n\n Hence, the probabilities of measuring $\\pm i$, which we denote by $p(\\pm i)$, are:\n $$p(+i) = |P_{+i} \\ket \\psi|^2 = \\frac{1}{4}(|0.6 - 0.8i|^2 + |0.8 + 0.6i|^2) = \\frac{1}{2},$$\n $$p(-i) = |P_{-i} \\ket \\psi|^2 = \\frac{1}{4}(|0.6 + 0.8i|^2 + |0.8 - 0.6i|^2) = \\frac{1}{2}.$$\n\n</details>"
|
|
1242
1242
|
}
|
|
1243
1243
|
]
|
|
1244
1244
|
},
|
|
@@ -1248,15 +1248,15 @@ export default {
|
|
|
1248
1248
|
"title": "Distinguishing Orthogonal States: 1",
|
|
1249
1249
|
"description": {
|
|
1250
1250
|
"type": "text-content",
|
|
1251
|
-
"asHtml": "<p><strong>Input:</strong> A qubit which is guaranteed to be in either the $\\ket {\\psi_+}$ or the $\\ket{\\psi_-} $ state, where $\\ket {\\psi_+} = 0.6\\ket 0 + 0.8 \\ket 1 $ and $\\ket {\\psi_-} = -0.8\\ket 0 + 0.6 \\ket 1$.</p>\n<p><strong>Output:</strong> <code>true</code> if the qubit was in the $\\ket {\\psi_+}$ state, or <code>false</code> if it was in the $\\ket{\\psi_-} $ state. The state of the qubit at the end of the operation does not matter.</p>\n<details>\n<summary><strong>Need a hint?</strong></summary>\nA suitable $R_y$ rotation can be used to go from the computational basis
|
|
1252
|
-
"asMarkdown": "**Input:** A qubit which is guaranteed to be in either the $\\ket {\\psi_+}$ or the $\\ket{\\psi_-} $ state, where $\\ket {\\psi_+} = 0.6\\ket 0 + 0.8 \\ket 1 $ and $\\ket {\\psi_-} = -0.8\\ket 0 + 0.6 \\ket 1$.\n\n**Output:** `true` if the qubit was in the $\\ket {\\psi_+}$ state, or `false` if it was in the $\\ket{\\psi_-} $ state. The state of the qubit at the end of the operation does not matter.\n\n<details>\n<summary><strong>Need a hint?</strong></summary>\nA suitable $R_y$ rotation can be used to go from the computational basis
|
|
1251
|
+
"asHtml": "<p><strong>Input:</strong> A qubit which is guaranteed to be in either the $\\ket {\\psi_+}$ or the $\\ket{\\psi_-} $ state, where $\\ket {\\psi_+} = 0.6\\ket 0 + 0.8 \\ket 1 $ and $\\ket {\\psi_-} = -0.8\\ket 0 + 0.6 \\ket 1$.</p>\n<p><strong>Output:</strong> <code>true</code> if the qubit was in the $\\ket {\\psi_+}$ state, or <code>false</code> if it was in the $\\ket{\\psi_-} $ state. The state of the qubit at the end of the operation does not matter.</p>\n<details>\n<summary><strong>Need a hint?</strong></summary>\nA suitable $R_y$ rotation can be used to go from the computational basis $\\\\{ \\ket 0, \\ket 1 \\\\}$ to the $\\\\{ \\ket{\\psi_+}, \\ket{\\psi_-} \\\\}$ basis and vice versa.\n</details>\n",
|
|
1252
|
+
"asMarkdown": "**Input:** A qubit which is guaranteed to be in either the $\\ket {\\psi_+}$ or the $\\ket{\\psi_-} $ state, where $\\ket {\\psi_+} = 0.6\\ket 0 + 0.8 \\ket 1 $ and $\\ket {\\psi_-} = -0.8\\ket 0 + 0.6 \\ket 1$.\n\n**Output:** `true` if the qubit was in the $\\ket {\\psi_+}$ state, or `false` if it was in the $\\ket{\\psi_-} $ state. The state of the qubit at the end of the operation does not matter.\n\n<details>\n<summary><strong>Need a hint?</strong></summary>\nA suitable $R_y$ rotation can be used to go from the computational basis $\\\\{ \\ket 0, \\ket 1 \\\\}$ to the $\\\\{ \\ket{\\psi_+}, \\ket{\\psi_-} \\\\}$ basis and vice versa.\n</details>\n"
|
|
1253
1253
|
},
|
|
1254
1254
|
"sourceIds": [
|
|
1255
|
+
"single_qubit_measurements__distinguish_orthogonal_states_1__Verification.qs",
|
|
1255
1256
|
"KatasLibrary.qs",
|
|
1256
|
-
"
|
|
1257
|
-
"single_qubit_measurements__distinguish_orthogonal_states_1__verification.qs"
|
|
1257
|
+
"single_qubit_measurements__Common.qs"
|
|
1258
1258
|
],
|
|
1259
|
-
"placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n operation IsQubitPsiPlus(q : Qubit) : Bool {\n // Implement your solution here...\n\n return false;\n }\n
|
|
1259
|
+
"placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n operation IsQubitPsiPlus(q : Qubit) : Bool {\n // Implement your solution here...\n\n return false;\n }\n}\n",
|
|
1260
1260
|
"explainedSolution": {
|
|
1261
1261
|
"type": "explained-solution",
|
|
1262
1262
|
"items": [
|
|
@@ -1268,7 +1268,7 @@ export default {
|
|
|
1268
1268
|
{
|
|
1269
1269
|
"type": "solution",
|
|
1270
1270
|
"id": "single_qubit_measurements__distinguish_orthogonal_states_1_solution",
|
|
1271
|
-
"code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n operation IsQubitPsiPlus(q : Qubit) : Bool {\n Ry(-2.0 * ArcTan2(0.8, 0.6), q);\n return M(q) == Zero;\n }\n
|
|
1271
|
+
"code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n operation IsQubitPsiPlus(q : Qubit) : Bool {\n Ry(-2.0 * ArcTan2(0.8, 0.6), q);\n return M(q) == Zero;\n }\n}\n"
|
|
1272
1272
|
}
|
|
1273
1273
|
]
|
|
1274
1274
|
}
|
|
@@ -1283,23 +1283,23 @@ export default {
|
|
|
1283
1283
|
"asMarkdown": "**Inputs:**\n\n1. Angle $\\alpha$, in radians, represented as a `Double`.\n2. A qubit which is guaranteed to be in either the $|A\\rangle$ or the $|B\\rangle$ state, where $|A\\rangle = \\cos \\alpha |0\\rangle - i \\sin \\alpha |1\\rangle$ and $|B\\rangle = - i \\sin \\alpha |0\\rangle + \\cos \\alpha |1\\rangle$.\n\n**Output:** `true` if the qubit was in the $|A\\rangle$ state, or `false` if it was in the $|B\\rangle$ state. The state of the qubit at the end of the operation does not matter.\n\n<details>\n <summary><strong>Need a hint?</strong></summary>\n <p>An $R_x$ rotation can be used to go from the computational basis $\\{ \\ket 0, \\ket 1 \\}$ to the $\\{ \\ket{A}, \\ket{B} \\}$ basis and vice versa.</p>\n</details>\n"
|
|
1284
1284
|
},
|
|
1285
1285
|
"sourceIds": [
|
|
1286
|
+
"single_qubit_measurements__distinguish_orthogonal_states_2__Verification.qs",
|
|
1286
1287
|
"KatasLibrary.qs",
|
|
1287
|
-
"
|
|
1288
|
-
"single_qubit_measurements__distinguish_orthogonal_states_2__verification.qs"
|
|
1288
|
+
"single_qubit_measurements__Common.qs"
|
|
1289
1289
|
],
|
|
1290
|
-
"placeholderCode": "namespace Kata {\n
|
|
1290
|
+
"placeholderCode": "namespace Kata {\n operation IsQubitA(alpha : Double, q : Qubit) : Bool {\n // Implement your solution here...\n\n return false;\n }\n}\n",
|
|
1291
1291
|
"explainedSolution": {
|
|
1292
1292
|
"type": "explained-solution",
|
|
1293
1293
|
"items": [
|
|
1294
1294
|
{
|
|
1295
1295
|
"type": "text-content",
|
|
1296
|
-
"asHtml": "<p>We can distinguish between the states $\\ket{A}$ and $\\ket B$ if we implement a measurement in the
|
|
1297
|
-
"asMarkdown": "\nWe can distinguish between the states $\\ket{A}$ and $\\ket B$ if we implement a measurement in the
|
|
1296
|
+
"asHtml": "<p>We can distinguish between the states $\\ket{A}$ and $\\ket B$ if we implement a measurement in the $\\{ \\ket{A}, \\ket{B}\\}$ basis.</p>\n<p>We can notice that the $R_x$ rotation gate with $\\theta = 2 \\alpha$ is an appropriate transformation which maps the $\\ket 0 $ state to the $\\ket A$ state, and the $\\ket 1$ state to the $\\ket B$ state:</p>\n<p>$$R_x(\\theta) \\ket 0 = \\cos \\alpha \\ket 0 -i \\sin \\alpha \\ket 1 = \\ket {A},$$\n$$R_x(\\theta) \\ket 1 = -i \\sin \\alpha \\ket 0 + \\cos \\alpha \\ket 1 = \\ket{B}.$$</p>\n<p>Thus, the inverse transformation $R_x(-\\theta)$ maps the $A/B$ basis to the $0/1$ basis.</p>\n<p>Therefore, if we apply $R_x(-\\theta)$ to the qubit and measure it using <code>M</code>, a measurement result of <code>Zero</code> will correspond to the qubit's original state being $\\ket{A}$, while a result of <code>One</code> will correspond to the qubit's original state being $\\ket B$.</p>\n",
|
|
1297
|
+
"asMarkdown": "\nWe can distinguish between the states $\\ket{A}$ and $\\ket B$ if we implement a measurement in the $\\\\{ \\ket{A}, \\ket{B}\\\\}$ basis.\n\nWe can notice that the $R_x$ rotation gate with $\\theta = 2 \\alpha$ is an appropriate transformation which maps the $\\ket 0 $ state to the $\\ket A$ state, and the $\\ket 1$ state to the $\\ket B$ state:\n\n$$R_x(\\theta) \\ket 0 = \\cos \\alpha \\ket 0 -i \\sin \\alpha \\ket 1 = \\ket {A},$$\n$$R_x(\\theta) \\ket 1 = -i \\sin \\alpha \\ket 0 + \\cos \\alpha \\ket 1 = \\ket{B}.$$\n\nThus, the inverse transformation $R_x(-\\theta)$ maps the $A/B$ basis to the $0/1$ basis.\n\nTherefore, if we apply $R_x(-\\theta)$ to the qubit and measure it using `M`, a measurement result of `Zero` will correspond to the qubit's original state being $\\ket{A}$, while a result of `One` will correspond to the qubit's original state being $\\ket B$."
|
|
1298
1298
|
},
|
|
1299
1299
|
{
|
|
1300
1300
|
"type": "solution",
|
|
1301
1301
|
"id": "single_qubit_measurements__distinguish_orthogonal_states_2_solution",
|
|
1302
|
-
"code": "namespace Kata {\n
|
|
1302
|
+
"code": "namespace Kata {\n operation IsQubitA(alpha : Double, q : Qubit) : Bool {\n Rx(-2.0 * alpha, q);\n return M(q) == Zero;\n }\n}\n"
|
|
1303
1303
|
}
|
|
1304
1304
|
]
|
|
1305
1305
|
}
|
|
@@ -1314,11 +1314,11 @@ export default {
|
|
|
1314
1314
|
"asMarkdown": "**Inputs:**\n\n1. Angle $\\alpha$, in radians, represented as a `Double`.\n1. A qubit in some unknown state.\n\n**Output:** Implement a measurement in the $\\{\\ket A, \\ket B\\}$ basis. Same as in the previous exercise, $|A\\rangle = \\cos \\alpha |0\\rangle - i \\sin \\alpha |1\\rangle$ and $|B\\rangle = - i \\sin \\alpha |0\\rangle + \\cos \\alpha |1\\rangle$. Return `Zero` if the measurement outcome is $A$, and `One` if the outcome is $B$.\nThe state of the qubit after the measurement should correspond to the measurement result.\n\n<details>\n <summary><strong>Need a hint?</strong></summary>\n <p>An $R_x$ rotation can be used to go from the computational basis $\\{ \\ket 0, \\ket 1 \\}$ to the $\\{ \\ket{A}, \\ket{B} \\}$ basis and vice versa.</p>\n</details>\n"
|
|
1315
1315
|
},
|
|
1316
1316
|
"sourceIds": [
|
|
1317
|
+
"single_qubit_measurements__a_b_basis_measurements__Verification.qs",
|
|
1317
1318
|
"KatasLibrary.qs",
|
|
1318
|
-
"
|
|
1319
|
-
"single_qubit_measurements__a_b_basis_measurements__verification.qs"
|
|
1319
|
+
"single_qubit_measurements__Common.qs"
|
|
1320
1320
|
],
|
|
1321
|
-
"placeholderCode": "namespace Kata {\n
|
|
1321
|
+
"placeholderCode": "namespace Kata {\n operation MeasureInABBasis(alpha : Double, q : Qubit) : Result {\n // Implement your solution here...\n\n return Zero;\n }\n}\n",
|
|
1322
1322
|
"explainedSolution": {
|
|
1323
1323
|
"type": "explained-solution",
|
|
1324
1324
|
"items": [
|
|
@@ -1330,7 +1330,7 @@ export default {
|
|
|
1330
1330
|
{
|
|
1331
1331
|
"type": "solution",
|
|
1332
1332
|
"id": "single_qubit_measurements__a_b_basis_measurements_solution",
|
|
1333
|
-
"code": "namespace Kata {\n
|
|
1333
|
+
"code": "namespace Kata {\n operation MeasureInABBasis(alpha : Double, q : Qubit) : Result {\n Rx(-2.0 * alpha, q);\n let measurementResult = M(q);\n Rx(2.0 * alpha, q);\n return measurementResult;\n }\n}\n"
|
|
1334
1334
|
}
|
|
1335
1335
|
]
|
|
1336
1336
|
}
|
|
@@ -1342,8 +1342,8 @@ export default {
|
|
|
1342
1342
|
"items": [
|
|
1343
1343
|
{
|
|
1344
1344
|
"type": "text-content",
|
|
1345
|
-
"asHtml": "<p>Congratulations! In this kata you learned how to apply measurements on single-qubit systems. Here are a few key concepts to keep in mind:</p>\n<ul>\n<li>Measurements are always done in an orthogonal basis. By default, we choose the computational basis
|
|
1346
|
-
"asMarkdown": "\nCongratulations! In this kata you learned how to apply measurements on single-qubit systems. Here are a few key concepts to keep in mind:\n
|
|
1345
|
+
"asHtml": "<p>Congratulations! In this kata you learned how to apply measurements on single-qubit systems. Here are a few key concepts to keep in mind:</p>\n<ul>\n<li>Unlike quantum gates, measurements are neither unitary nor reversible. When we measure a qubit, the state of the qubit collapses to one of the basis states, and the initial state is lost.</li>\n<li>Measurements are always done in an orthogonal basis. By default, we choose the computational basis ${{ \\ket{0}, \\ket{1} }}$.</li>\n<li>Measurements are represented as sets of projector operators, which are matrices.</li>\n<li>In Q#, you can implement measurements in the computational basis using the <code>M</code> operation, and in the Pauli basis using the <code>Measure</code> operation. You can also use <code>MResetZ</code> to measure a qubit in the computational basis and reset it to $\\ket{0}$ right away.</li>\n</ul>\n<p>Next, you will learn about measurements in multi-qubit systems in the "Measurements in Multi-Qubit Systems" kata.</p>\n",
|
|
1346
|
+
"asMarkdown": "\nCongratulations! In this kata you learned how to apply measurements on single-qubit systems. Here are a few key concepts to keep in mind:\n\n- Unlike quantum gates, measurements are neither unitary nor reversible. When we measure a qubit, the state of the qubit collapses to one of the basis states, and the initial state is lost.\n- Measurements are always done in an orthogonal basis. By default, we choose the computational basis ${{ \\ket{0}, \\ket{1} }}$.\n- Measurements are represented as sets of projector operators, which are matrices.\n- In Q#, you can implement measurements in the computational basis using the `M` operation, and in the Pauli basis using the `Measure` operation. You can also use `MResetZ` to measure a qubit in the computational basis and reset it to $\\ket{0}$ right away.\n\nNext, you will learn about measurements in multi-qubit systems in the \"Measurements in Multi-Qubit Systems\" kata."
|
|
1347
1347
|
}
|
|
1348
1348
|
]
|
|
1349
1349
|
}
|
|
@@ -1372,8 +1372,8 @@ export default {
|
|
|
1372
1372
|
"items": [
|
|
1373
1373
|
{
|
|
1374
1374
|
"type": "text-content",
|
|
1375
|
-
"asHtml": "<p>There are several types of measurements you can perform on an $n$-qubit system ($n>1$):</p>\n<ul>\n<li>Measuring all the qubits simultaneously in an orthogonal basis ($2^n$ possible outcomes). As we shall see below, this is a direct generalization of orthogonal basis measurements done in single-qubit systems introduced in the previous kata.</li>\n<li>Partial measurement: measuring $m$ qubits out of $n$, for $m<n$ ($2^m$ possible outcomes). Partial measurements involve a partial collapse of the system's wave function, since only some of the qubits are measured.</li>\n<li>Joint measurement: measuring a joint property of all $n$ qubits ($2$ possible outcomes).</li>\n</ul>\n<p>We will discuss these concepts in the same order as in the list above.</p>\n<h2>Full Measurements: Measurements in Multi-Qubit Bases</h2>\n<p>Consider a system consisting of $n\\geq1$ qubits. The wave function of such a system belongs to a vector space of dimension $2^n$. Thus, the vector space is spanned by an orthogonal basis, such as the computational basis which consists of the vectors $|0\\dotsc0\\rangle, \\dotsc, |1\\dotsc 1\\rangle$. For generality, we consider an arbitrary orthonormal basis, which we denote by ${ |b_0\\rangle, |b_1\\rangle, \\dotsc, |b_{2^n-1}\\rangle }$.</p>\n<p>Then, the state $|\\psi\\rangle$ of the multi-qubit system can be expressed as a linear combination of the $2^n$ basis vectors $|b_i\\rangle$. That is, there exist complex numbers $c_0,c_1,\\dotsc, c_{2^n-1}$ such that</p>\n<p>$$\n|\\psi\\rangle = \\sum_{i=0}^{2^n-1} c_i|b_i\\rangle \\equiv \\begin{pmatrix} c_0 \\\\ c_1 \\\\ \\vdots \\\\ c_{2^n-1} \\end{pmatrix}\n$$</p>\n<p>In line with the usual convention, we choose the wave function to be normalized, so that $|c_0|^2 + \\dotsc + |c_{2^n-1}|^2 =1$. Then, a quantum measurement in the ${ |b_0\\rangle, |b_1\\rangle, \\dotsc, |b_{2^n-1}\\rangle }$ basis satisfies the following rules:</p>\n<ul>\n<li>The measurement outcome $b_i$ occurs with probability $|c_i|^2$.</li>\n<li>Whenever the measurement outcome is $b_i$, the wave function collapses to the state $|b_i\\rangle$. That is, the post-measurement state of the system is equal to $|b_i\\rangle$.</li>\n</ul>\n<p>This can be summarized in the following table:</p>\n<table>\n <tr>\n <th>Measurement outcome</th>\n <th>Probability of outcome</th>\n <th>State after measurement</th>\n </tr>\n <tr>\n <td>$b_i$</td>\n <td>$|c_i|^2$</td>\n <td>$\\ket{b_i}$</td>\n </tr>\n</table>\n\n<blockquote>\n<p>Similar to measurements in single-qubit systems, the assumption of normalization of the original wave function is required in order to ensure that the sum of all the outcome probabilities is 1.</p>\n</blockquote>\n<h2>Multi-Qubit Measurement Outcome Probabilities I</h2>\n<p>Suppose that a two-qubit system is known to be in the following state:\n$$\\ket \\psi = \\frac{1}{3}\\ket {00} + \\frac{2}{3} \\ket {01} + \\frac{2}{3}\\ket {11}$$</p>\n<p>If all the qubits are measured simultaneously in the computational basis, what are the outcome probabilities?</p>\n<p>The wave function $|\\psi\\rangle$ is normalized, since $\\left(\\frac{1}{3}\\right)^2 + \\left(\\frac{2}{3}\\right)^2 + \\left(\\frac{2}{3}\\right)^2 = 1$. Hence, the probabilities of measuring each of the computational basis states is simply the square of the absolute value of the corresponding coefficients. That is, the probabilities of measuring $00$, $01$ and $11$ are $\\frac{1}{9}$, $\\frac{4}{9}$ and $\\frac{4}{9}$, respectively, and the probability of measuring the basis state $10$ that is not part of the superposition is $0$:</p>\n<table>\n <tr>\n <th>Measurement outcome</th>\n <th>Probability of outcome</th>\n </tr>\n <tr>\n <td>$00$</td>\n <td>$\\left( \\frac{1}{3}\\right)^2 = \\frac{1}{9}$</td>\n </tr
|
|
1376
|
-
"asMarkdown": "\nThere are several types of measurements you can perform on an $n$-qubit system ($n>1$):\n\n- Measuring all the qubits simultaneously in an orthogonal basis ($2^n$ possible outcomes). As we shall see below, this is a direct generalization of orthogonal basis measurements done in single-qubit systems introduced in the previous kata.\n- Partial measurement: measuring $m$ qubits out of $n$, for $m<n$ ($2^m$ possible outcomes). Partial measurements involve a partial collapse of the system's wave function, since only some of the qubits are measured.\n- Joint measurement: measuring a joint property of all $n$ qubits ($2$ possible outcomes).\n\nWe will discuss these concepts in the same order as in the list above.\n\n## Full Measurements: Measurements in Multi-Qubit Bases\n\nConsider a system consisting of $n\\geq1$ qubits. The wave function of such a system belongs to a vector space of dimension $2^n$. Thus, the vector space is spanned by an orthogonal basis, such as the computational basis which consists of the vectors $|0\\dotsc0\\rangle, \\dotsc, |1\\dotsc 1\\rangle$. For generality, we consider an arbitrary orthonormal basis, which we denote by $\\{ |b_0\\rangle, |b_1\\rangle, \\dotsc, |b_{2^n-1}\\rangle \\}$.\n\nThen, the state $|\\psi\\rangle$ of the multi-qubit system can be expressed as a linear combination of the $2^n$ basis vectors $|b_i\\rangle$. That is, there exist complex numbers $c_0,c_1,\\dotsc, c_{2^n-1}$ such that\n\n$$\n|\\psi\\rangle = \\sum_{i=0}^{2^n-1} c_i|b_i\\rangle \\equiv \\begin{pmatrix} c_0 \\\\\\ c_1 \\\\\\ \\vdots \\\\\\ c_{2^n-1} \\end{pmatrix}\n$$\n\nIn line with the usual convention, we choose the wave function to be normalized, so that $|c_0|^2 + \\dotsc + |c_{2^n-1}|^2 =1$. Then, a quantum measurement in the $\\{ |b_0\\rangle, |b_1\\rangle, \\dotsc, |b_{2^n-1}\\rangle \\}$ basis satisfies the following rules:\n\n- The measurement outcome $b_i$ occurs with probability $|c_i|^2$.\n- Whenever the measurement outcome is $b_i$, the wave function collapses to the state $|b_i\\rangle$. That is, the post-measurement state of the system is equal to $|b_i\\rangle$.\n\nThis can be summarized in the following table:\n\n<table>\n <tr>\n <th>Measurement outcome</th>\n <th>Probability of outcome</th>\n <th>State after measurement</th>\n </tr>\n <tr>\n <td>$b_i$</td>\n <td>$|c_i|^2$</td>\n <td>$\\ket{b_i}$</td>\n </tr>\n</table>\n\n> Similar to measurements in single-qubit systems, the assumption of normalization of the original wave function is required in order to ensure that the sum of all the outcome probabilities is 1.\n\n## Multi-Qubit Measurement Outcome Probabilities I\n\nSuppose that a two-qubit system is known to be in the following state:\n$$\\ket \\psi = \\frac{1}{3}\\ket {00} + \\frac{2}{3} \\ket {01} + \\frac{2}{3}\\ket {11}$$\n\nIf all the qubits are measured simultaneously in the computational basis, what are the outcome probabilities?\n\nThe wave function $|\\psi\\rangle$ is normalized, since $\\left(\\frac{1}{3}\\right)^2 + \\left(\\frac{2}{3}\\right)^2 + \\left(\\frac{2}{3}\\right)^2 = 1$. Hence, the probabilities of measuring each of the computational basis states is simply the square of the absolute value of the corresponding coefficients. That is, the probabilities of measuring $00$, $01$ and $11$ are $\\frac{1}{9}$, $\\frac{4}{9}$ and $\\frac{4}{9}$, respectively, and the probability of measuring the basis state $10$ that is not part of the superposition is $0$:\n\n<table>\n <tr>\n <th>Measurement outcome</th>\n <th>Probability of outcome</th>\n </tr>\n <tr>\n <td>$00$</td>\n <td>$\\left( \\frac{1}{3}\\right)^2 = \\frac{1}{9}$</td>\n </tr
|
|
1375
|
+
"asHtml": "<p>There are several types of measurements you can perform on an $n$-qubit system ($n>1$):</p>\n<ul>\n<li>Measuring all the qubits simultaneously in an orthogonal basis ($2^n$ possible outcomes). As we shall see below, this is a direct generalization of orthogonal basis measurements done in single-qubit systems introduced in the previous kata.</li>\n<li>Partial measurement: measuring $m$ qubits out of $n$, for $m<n$ ($2^m$ possible outcomes). Partial measurements involve a partial collapse of the system's wave function, since only some of the qubits are measured.</li>\n<li>Joint measurement: measuring a joint property of all $n$ qubits ($2$ possible outcomes).</li>\n</ul>\n<p>We will discuss these concepts in the same order as in the list above.</p>\n<h2>Full Measurements: Measurements in Multi-Qubit Bases</h2>\n<p>Consider a system consisting of $n\\geq1$ qubits. The wave function of such a system belongs to a vector space of dimension $2^n$. Thus, the vector space is spanned by an orthogonal basis, such as the computational basis which consists of the vectors $|0\\dotsc0\\rangle, \\dotsc, |1\\dotsc 1\\rangle$. For generality, we consider an arbitrary orthonormal basis, which we denote by ${ |b_0\\rangle, |b_1\\rangle, \\dotsc, |b_{2^n-1}\\rangle }$.</p>\n<p>Then, the state $|\\psi\\rangle$ of the multi-qubit system can be expressed as a linear combination of the $2^n$ basis vectors $|b_i\\rangle$. That is, there exist complex numbers $c_0,c_1,\\dotsc, c_{2^n-1}$ such that</p>\n<p>$$\n|\\psi\\rangle = \\sum_{i=0}^{2^n-1} c_i|b_i\\rangle \\equiv \\begin{pmatrix} c_0 \\\\ c_1 \\\\ \\vdots \\\\ c_{2^n-1} \\end{pmatrix}\n$$</p>\n<p>In line with the usual convention, we choose the wave function to be normalized, so that $|c_0|^2 + \\dotsc + |c_{2^n-1}|^2 =1$. Then, a quantum measurement in the ${ |b_0\\rangle, |b_1\\rangle, \\dotsc, |b_{2^n-1}\\rangle }$ basis satisfies the following rules:</p>\n<ul>\n<li>The measurement outcome $b_i$ occurs with probability $|c_i|^2$.</li>\n<li>Whenever the measurement outcome is $b_i$, the wave function collapses to the state $|b_i\\rangle$. That is, the post-measurement state of the system is equal to $|b_i\\rangle$.</li>\n</ul>\n<p>This can be summarized in the following table:</p>\n<table>\n <tr>\n <th>Measurement outcome</th>\n <th>Probability of outcome</th>\n <th>State after measurement</th>\n </tr>\n <tr>\n <td>$b_i$</td>\n <td>$|c_i|^2$</td>\n <td>$\\ket{b_i}$</td>\n </tr>\n</table>\n\n<blockquote>\n<p>Similar to measurements in single-qubit systems, the assumption of normalization of the original wave function is required in order to ensure that the sum of all the outcome probabilities is 1.</p>\n</blockquote>\n<h2>Multi-Qubit Measurement Outcome Probabilities I</h2>\n<p>Suppose that a two-qubit system is known to be in the following state:\n$$\\ket \\psi = \\frac{1}{3}\\ket {00} + \\frac{2}{3} \\ket {01} + \\frac{2}{3}\\ket {11}$$</p>\n<p>If all the qubits are measured simultaneously in the computational basis, what are the outcome probabilities?</p>\n<p>The wave function $|\\psi\\rangle$ is normalized, since $\\left(\\frac{1}{3}\\right)^2 + \\left(\\frac{2}{3}\\right)^2 + \\left(\\frac{2}{3}\\right)^2 = 1$. Hence, the probabilities of measuring each of the computational basis states is simply the square of the absolute value of the corresponding coefficients. That is, the probabilities of measuring $00$, $01$ and $11$ are $\\frac{1}{9}$, $\\frac{4}{9}$ and $\\frac{4}{9}$, respectively, and the probability of measuring the basis state $10$ that is not part of the superposition is $0$:</p>\n<table>\n <tr>\n <th>Measurement outcome</th>\n <th>Probability of outcome</th>\n </tr>\n <tr>\n <td>$00$</td>\n <td>$\\left( \\frac{1}{3}\\right)^2 = \\frac{1}{9}$</td>\n </tr>\n <tr>\n <td>$01$</td>\n <td>$\\left( \\frac{2}{3}\\right)^2 = \\frac{4}{9}$</td>\n </tr>\n <tr>\n <td>$10$</td>\n <td>$\\left( 0\\right)^2 = 0$</td>\n </tr>\n <tr>\n <td>$11$</td>\n <td>$\\left( \\frac{2}{3}\\right)^2 = \\frac{4}{9}$</td>\n </tr>\n</table>\n</details>\n\n<h2>Multi-Qubit Measurement Outcome Probabilities II</h2>\n<p>Suppose that a two-qubit system is known to be in the following state:\n$$\\ket \\psi = \\frac{2}{3}\\ket {00} + \\frac{1}{3} \\ket {01} + \\frac{2}{3}\\ket {11}$$</p>\n<p>If all the qubits are measured simultaneously in the Pauli X basis, that is, in the ${ \\ket{++}, \\ket{+-}, \\ket{-+}, \\ket{--}}$ basis, what are the outcome probabilities?</p>\n<h3>Analytical Solution</h3>\n<p>Using the expressions $|0\\rangle = \\frac{1}{\\sqrt{2}} \\big( |+\\rangle + |-\\rangle \\big)$ and $|1\\rangle = \\frac{1}{\\sqrt{2}} \\big( |+\\rangle - |-\\rangle \\big)$, we first express $|\\psi\\rangle$ in the Pauli X basis. This gives us\n$$\\ket \\psi = \\frac{2}{3}\\ket {00} + \\frac{1}{3} \\ket {01} + \\frac{2}{3}\\ket {11}$$</p>\n<p>$$= \\frac{2}{3} \\big[ \\frac{1}{\\sqrt{2}}\\big(\\ket{+} + \\ket{-}\\big) \\otimes \\frac{1}{\\sqrt{2}} \\big(\\ket{+} + \\ket{-}\\big) \\big] + \\frac{1}{3} \\big[ \\frac{1}{\\sqrt{2}}\\big(\\ket{+} + \\ket{-}\\big) \\otimes \\frac{1}{\\sqrt{2}} \\big(\\ket{+} - \\ket{-}\\big) \\big] + \\frac{2}{3} \\big[ \\frac{1}{\\sqrt{2}}\\big(\\ket{+} - \\ket{-}\\big) \\otimes \\frac{1}{\\sqrt{2}} \\big(\\ket{+} - \\ket{-}\\big) \\big]$$</p>\n<p>$$= \\frac{1}{3} \\big[ \\big(\\ket{+} + \\ket{-}\\big) \\otimes \\big(\\ket{+} + \\ket{-}\\big) \\big] + \\frac{1}{6} \\big[ \\big(\\ket{+} + \\ket{-}\\big) \\otimes \\big(\\ket{+} - \\ket{-}\\big) \\big] + \\frac{1}{3} \\big[ \\big(\\ket{+} - \\ket{-}\\big) \\otimes \\big(\\ket{+} - \\ket{-}\\big) \\big]$$</p>\n<p>$$= \\frac{1}{3} \\big[ \\ket{++} + \\ket{+-} + \\ket{-+} + \\ket{--} \\big] + \\frac{1}{6} \\big[ \\ket{++} - \\ket{+-} + \\ket{-+} - \\ket{--} \\big] + \\frac{1}{3} \\big[ \\ket{++} - \\ket{+-} - \\ket{-+} + \\ket{--} \\big]$$</p>\n<p>$$= (\\frac{1}{3} + \\frac{1}{6} + \\frac{1}{3})\\ket{++} + (\\frac{1}{3} - \\frac{1}{6} - \\frac{1}{3})\\ket{+-} + (\\frac{1}{3} + \\frac{1}{6} - \\frac{1}{3})\\ket{-+} + (\\frac{1}{3} - \\frac{1}{6} + \\frac{1}{3})\\ket{--}$$</p>\n<p>$$= \\frac{5}{6}\\ket{++} - \\frac{1}{6}\\ket{+-} + \\frac{1}{6}\\ket{-+} + \\frac{1}{2}\\ket{--}$$</p>\n<p>After this, the probabilities of measuring each of the four basis vectors is given by the square of the absolute value of its amplitude in the superposition:</p>\n<table>\n <tr>\n <th>Measurement outcome</th>\n <th>Probability of outcome</th>\n </tr>\n <tr>\n <td>$++$</td>\n <td>$\\left( \\frac{5}{6}\\right)^2 = \\frac{25}{36}$</td>\n </tr>\n <tr>\n <td>$+-$</td>\n <td>$\\left( -\\frac{1}{6}\\right)^2 = \\frac{1}{36}$</td>\n </tr>\n <tr>\n <td>$-+$</td>\n <td>$\\left( \\frac{1}{6}\\right)^2 = \\frac{1}{36}$</td>\n </tr>\n <tr>\n <td>$--$</td>\n <td>$\\left( \\frac{1}{2}\\right)^2 = \\frac{1}{4}$</td>\n </tr>\n</table>\n\n<h3>Code-Based Solution</h3>\n<p>We can also use Q# to solve this problem. It can be achieved in three steps:</p>\n<ol>\n<li>Prepare the state $\\ket \\psi$.</li>\n<li>Apply a transformation that maps the 2-qubit Pauli X basis into the 2-qubit computational basis. This transformation just applies a Hadamard gate to each of the qubits.</li>\n<li>View probabilities of each basis state with the <code>DumpMachine</code> function. Thanks to the previous step, the following state equivalence holds:</li>\n</ol>\n<table>\n <tr>\n <th>Before basis transformation</th>\n <th>After basis transformation</th>\n </tr>\n <tr>\n <td>$\\ket {++}$</td>\n <td>$\\ket {00}$</td>\n </tr>\n <tr>\n <td>$\\ket {+-}$</td>\n <td>$\\ket {01}$</td>\n </tr>\n <tr>\n <td>$\\ket {-+}$</td>\n <td>$\\ket {10}$</td>\n </tr>\n <tr>\n <td>$\\ket {--}$</td>\n <td>$\\ket {11}$</td>\n </tr>\n</table>\n\n<p>The amplitudes of the computational basis states after the transformation are the same as the amplitudes of the basis states of the Pauli X basis before the transformation!</p>\n<blockquote>\n<p>To implement the first step, we can represent $\\ket \\psi$ as<br>$$\\frac 2 3 \\ket {00} + {\\big (} \\frac 1 {\\sqrt 5} \\ket {0} + \\frac 2 {\\sqrt 5} \\ket {1} {\\big )} \\frac {\\sqrt 5} 3 \\ket {1}$$\nThis representation tells us how we should rotate individual qubits.</p>\n<p>Notice that we start by rotating the second qubit, as this gives a simpler implementation. If we started by rotating the first qubit, we would need to use a CNOT gate and a controlled $R_y$ gate to achieve the same result.</p>\n</blockquote>\n",
|
|
1376
|
+
"asMarkdown": "\nThere are several types of measurements you can perform on an $n$-qubit system ($n>1$):\n\n- Measuring all the qubits simultaneously in an orthogonal basis ($2^n$ possible outcomes). As we shall see below, this is a direct generalization of orthogonal basis measurements done in single-qubit systems introduced in the previous kata.\n- Partial measurement: measuring $m$ qubits out of $n$, for $m<n$ ($2^m$ possible outcomes). Partial measurements involve a partial collapse of the system's wave function, since only some of the qubits are measured.\n- Joint measurement: measuring a joint property of all $n$ qubits ($2$ possible outcomes).\n\nWe will discuss these concepts in the same order as in the list above.\n\n## Full Measurements: Measurements in Multi-Qubit Bases\n\nConsider a system consisting of $n\\geq1$ qubits. The wave function of such a system belongs to a vector space of dimension $2^n$. Thus, the vector space is spanned by an orthogonal basis, such as the computational basis which consists of the vectors $|0\\dotsc0\\rangle, \\dotsc, |1\\dotsc 1\\rangle$. For generality, we consider an arbitrary orthonormal basis, which we denote by $\\{ |b_0\\rangle, |b_1\\rangle, \\dotsc, |b_{2^n-1}\\rangle \\}$.\n\nThen, the state $|\\psi\\rangle$ of the multi-qubit system can be expressed as a linear combination of the $2^n$ basis vectors $|b_i\\rangle$. That is, there exist complex numbers $c_0,c_1,\\dotsc, c_{2^n-1}$ such that\n\n$$\n|\\psi\\rangle = \\sum_{i=0}^{2^n-1} c_i|b_i\\rangle \\equiv \\begin{pmatrix} c_0 \\\\\\ c_1 \\\\\\ \\vdots \\\\\\ c_{2^n-1} \\end{pmatrix}\n$$\n\nIn line with the usual convention, we choose the wave function to be normalized, so that $|c_0|^2 + \\dotsc + |c_{2^n-1}|^2 =1$. Then, a quantum measurement in the $\\{ |b_0\\rangle, |b_1\\rangle, \\dotsc, |b_{2^n-1}\\rangle \\}$ basis satisfies the following rules:\n\n- The measurement outcome $b_i$ occurs with probability $|c_i|^2$.\n- Whenever the measurement outcome is $b_i$, the wave function collapses to the state $|b_i\\rangle$. That is, the post-measurement state of the system is equal to $|b_i\\rangle$.\n\nThis can be summarized in the following table:\n\n<table>\n <tr>\n <th>Measurement outcome</th>\n <th>Probability of outcome</th>\n <th>State after measurement</th>\n </tr>\n <tr>\n <td>$b_i$</td>\n <td>$|c_i|^2$</td>\n <td>$\\ket{b_i}$</td>\n </tr>\n</table>\n\n> Similar to measurements in single-qubit systems, the assumption of normalization of the original wave function is required in order to ensure that the sum of all the outcome probabilities is 1.\n\n## Multi-Qubit Measurement Outcome Probabilities I\n\nSuppose that a two-qubit system is known to be in the following state:\n$$\\ket \\psi = \\frac{1}{3}\\ket {00} + \\frac{2}{3} \\ket {01} + \\frac{2}{3}\\ket {11}$$\n\nIf all the qubits are measured simultaneously in the computational basis, what are the outcome probabilities?\n\nThe wave function $|\\psi\\rangle$ is normalized, since $\\left(\\frac{1}{3}\\right)^2 + \\left(\\frac{2}{3}\\right)^2 + \\left(\\frac{2}{3}\\right)^2 = 1$. Hence, the probabilities of measuring each of the computational basis states is simply the square of the absolute value of the corresponding coefficients. That is, the probabilities of measuring $00$, $01$ and $11$ are $\\frac{1}{9}$, $\\frac{4}{9}$ and $\\frac{4}{9}$, respectively, and the probability of measuring the basis state $10$ that is not part of the superposition is $0$:\n\n<table>\n <tr>\n <th>Measurement outcome</th>\n <th>Probability of outcome</th>\n </tr>\n <tr>\n <td>$00$</td>\n <td>$\\left( \\frac{1}{3}\\right)^2 = \\frac{1}{9}$</td>\n </tr>\n <tr>\n <td>$01$</td>\n <td>$\\left( \\frac{2}{3}\\right)^2 = \\frac{4}{9}$</td>\n </tr>\n <tr>\n <td>$10$</td>\n <td>$\\left( 0\\right)^2 = 0$</td>\n </tr>\n <tr>\n <td>$11$</td>\n <td>$\\left( \\frac{2}{3}\\right)^2 = \\frac{4}{9}$</td>\n </tr>\n</table>\n</details>\n\n## Multi-Qubit Measurement Outcome Probabilities II\n\nSuppose that a two-qubit system is known to be in the following state:\n$$\\ket \\psi = \\frac{2}{3}\\ket {00} + \\frac{1}{3} \\ket {01} + \\frac{2}{3}\\ket {11}$$\n\nIf all the qubits are measured simultaneously in the Pauli X basis, that is, in the $\\{ \\ket{++}, \\ket{+-}, \\ket{-+}, \\ket{--}\\}$ basis, what are the outcome probabilities?\n\n### Analytical Solution\n\nUsing the expressions $|0\\rangle = \\frac{1}{\\sqrt{2}} \\big( |+\\rangle + |-\\rangle \\big)$ and $|1\\rangle = \\frac{1}{\\sqrt{2}} \\big( |+\\rangle - |-\\rangle \\big)$, we first express $|\\psi\\rangle$ in the Pauli X basis. This gives us\n$$\\ket \\psi = \\frac{2}{3}\\ket {00} + \\frac{1}{3} \\ket {01} + \\frac{2}{3}\\ket {11}$$\n\n$$= \\frac{2}{3} \\big[ \\frac{1}{\\sqrt{2}}\\big(\\ket{+} + \\ket{-}\\big) \\otimes \\frac{1}{\\sqrt{2}} \\big(\\ket{+} + \\ket{-}\\big) \\big] + \\frac{1}{3} \\big[ \\frac{1}{\\sqrt{2}}\\big(\\ket{+} + \\ket{-}\\big) \\otimes \\frac{1}{\\sqrt{2}} \\big(\\ket{+} - \\ket{-}\\big) \\big] + \\frac{2}{3} \\big[ \\frac{1}{\\sqrt{2}}\\big(\\ket{+} - \\ket{-}\\big) \\otimes \\frac{1}{\\sqrt{2}} \\big(\\ket{+} - \\ket{-}\\big) \\big]$$\n\n$$= \\frac{1}{3} \\big[ \\big(\\ket{+} + \\ket{-}\\big) \\otimes \\big(\\ket{+} + \\ket{-}\\big) \\big] + \\frac{1}{6} \\big[ \\big(\\ket{+} + \\ket{-}\\big) \\otimes \\big(\\ket{+} - \\ket{-}\\big) \\big] + \\frac{1}{3} \\big[ \\big(\\ket{+} - \\ket{-}\\big) \\otimes \\big(\\ket{+} - \\ket{-}\\big) \\big]$$\n\n$$= \\frac{1}{3} \\big[ \\ket{++} + \\ket{+-} + \\ket{-+} + \\ket{--} \\big] + \\frac{1}{6} \\big[ \\ket{++} - \\ket{+-} + \\ket{-+} - \\ket{--} \\big] + \\frac{1}{3} \\big[ \\ket{++} - \\ket{+-} - \\ket{-+} + \\ket{--} \\big]$$\n\n$$= (\\frac{1}{3} + \\frac{1}{6} + \\frac{1}{3})\\ket{++} + (\\frac{1}{3} - \\frac{1}{6} - \\frac{1}{3})\\ket{+-} + (\\frac{1}{3} + \\frac{1}{6} - \\frac{1}{3})\\ket{-+} + (\\frac{1}{3} - \\frac{1}{6} + \\frac{1}{3})\\ket{--}$$\n\n$$= \\frac{5}{6}\\ket{++} - \\frac{1}{6}\\ket{+-} + \\frac{1}{6}\\ket{-+} + \\frac{1}{2}\\ket{--}$$\n\nAfter this, the probabilities of measuring each of the four basis vectors is given by the square of the absolute value of its amplitude in the superposition:\n<table>\n <tr>\n <th>Measurement outcome</th>\n <th>Probability of outcome</th>\n </tr>\n <tr>\n <td>$++$</td>\n <td>$\\left( \\frac{5}{6}\\right)^2 = \\frac{25}{36}$</td>\n </tr>\n <tr>\n <td>$+-$</td>\n <td>$\\left( -\\frac{1}{6}\\right)^2 = \\frac{1}{36}$</td>\n </tr>\n <tr>\n <td>$-+$</td>\n <td>$\\left( \\frac{1}{6}\\right)^2 = \\frac{1}{36}$</td>\n </tr>\n <tr>\n <td>$--$</td>\n <td>$\\left( \\frac{1}{2}\\right)^2 = \\frac{1}{4}$</td>\n </tr>\n</table>\n\n### Code-Based Solution\n\nWe can also use Q# to solve this problem. It can be achieved in three steps:\n\n1. Prepare the state $\\ket \\psi$.\n2. Apply a transformation that maps the 2-qubit Pauli X basis into the 2-qubit computational basis. This transformation just applies a Hadamard gate to each of the qubits.\n3. View probabilities of each basis state with the `DumpMachine` function. Thanks to the previous step, the following state equivalence holds:\n\n<table>\n <tr>\n <th>Before basis transformation</th>\n <th>After basis transformation</th>\n </tr>\n <tr>\n <td>$\\ket {++}$</td>\n <td>$\\ket {00}$</td>\n </tr>\n <tr>\n <td>$\\ket {+-}$</td>\n <td>$\\ket {01}$</td>\n </tr>\n <tr>\n <td>$\\ket {-+}$</td>\n <td>$\\ket {10}$</td>\n </tr>\n <tr>\n <td>$\\ket {--}$</td>\n <td>$\\ket {11}$</td>\n </tr>\n</table>\n\nThe amplitudes of the computational basis states after the transformation are the same as the amplitudes of the basis states of the Pauli X basis before the transformation!\n\n>To implement the first step, we can represent $\\ket \\psi$ as \n>$$\\frac 2 3 \\ket {00} + {\\big (} \\frac 1 {\\sqrt 5} \\ket {0} + \\frac 2 {\\sqrt 5} \\ket {1} {\\big )} \\frac {\\sqrt 5} 3 \\ket {1}$$\n>This representation tells us how we should rotate individual qubits.\n>\n>Notice that we start by rotating the second qubit, as this gives a simpler implementation. If we started by rotating the first qubit, we would need to use a CNOT gate and a controlled $R_y$ gate to achieve the same result."
|
|
1377
1377
|
},
|
|
1378
1378
|
{
|
|
1379
1379
|
"type": "example",
|
|
@@ -1419,8 +1419,8 @@ export default {
|
|
|
1419
1419
|
"asMarkdown": "**Input:** Two qubits (stored in an array of length 2) which are guaranteed to be in one of the four basis states ($|00\\rangle$, $|01\\rangle$, $|10\\rangle$, or $|11\\rangle$).\n\n**Output:**\n\n* 0 if the qubits were in the $|00\\rangle$ state,\n* 1 if they were in the $|01\\rangle$ state, \n* 2 if they were in the $|10\\rangle$ state, \n* 3 if they were in the $|11\\rangle$ state.\n\nIn this task and the subsequent ones the order of qubit states in task description matches the order of qubits in the array (i.e., $|10\\rangle$ state corresponds to `qs[0]` in state $|1\\rangle$ and `qs[1]` in state $|0\\rangle$).\n\nThe state of the qubits at the end of the operation does not matter.\n"
|
|
1420
1420
|
},
|
|
1421
1421
|
"sourceIds": [
|
|
1422
|
-
"
|
|
1423
|
-
"
|
|
1422
|
+
"multi_qubit_measurements__full_measurements__Verification.qs",
|
|
1423
|
+
"multi_qubit_measurements__Common.qs",
|
|
1424
1424
|
"KatasLibrary.qs"
|
|
1425
1425
|
],
|
|
1426
1426
|
"placeholderCode": "namespace Kata {\n operation BasisStateMeasurement(qs : Qubit[]) : Int {\n // Implement your solution here...\n\n return -1;\n }\n\n}\n",
|
|
@@ -1430,7 +1430,7 @@ export default {
|
|
|
1430
1430
|
{
|
|
1431
1431
|
"type": "text-content",
|
|
1432
1432
|
"asHtml": "<p>First, we measure both qubits in the input array and store the result in <code>m1</code> and <code>m2</code>. We can decode these results like this: </p>\n<ul>\n<li><code>m1</code> is $|0\\rangle$ and <code>m2</code> is $|0\\rangle$: we return $0\\cdot2+0 = 0$</li>\n<li><code>m1</code> is $|0\\rangle$ and <code>m2</code> is $|1\\rangle$: we return $0\\cdot2+1 = 1$</li>\n<li><code>m1</code> is $|1\\rangle$ and <code>m2</code> is $|0\\rangle$: we return $1\\cdot2+0 = 2$</li>\n<li><code>m1</code> is $|1\\rangle$ and <code>m2</code> is $|1\\rangle$: we return $1\\cdot2+1 = 3$</li>\n</ul>\n<p>In other words, we treat the measurement results as the binary notation of the return value in big endian notation.</p>\n",
|
|
1433
|
-
"asMarkdown": "\nFirst, we measure both qubits in the input array and store the result in `m1` and `m2`. We can decode these results like this: \n- `m1` is $|0\\rangle$ and `m2` is $|0\\rangle$: we return $0\\cdot2+0 = 0$\n- `m1` is $|0\\rangle$ and `m2` is $|1\\rangle$: we return $0\\cdot2+1 = 1$\n- `m1` is $|1\\rangle$ and `m2` is $|0\\rangle$: we return $1\\cdot2+0 = 2$\n- `m1` is $|1\\rangle$ and `m2` is $|1\\rangle$: we return $1\\cdot2+1 = 3$\n\nIn other words, we treat the measurement results as the binary notation of the return value in big endian notation."
|
|
1433
|
+
"asMarkdown": "\nFirst, we measure both qubits in the input array and store the result in `m1` and `m2`. We can decode these results like this: \n\n- `m1` is $|0\\rangle$ and `m2` is $|0\\rangle$: we return $0\\cdot2+0 = 0$\n- `m1` is $|0\\rangle$ and `m2` is $|1\\rangle$: we return $0\\cdot2+1 = 1$\n- `m1` is $|1\\rangle$ and `m2` is $|0\\rangle$: we return $1\\cdot2+0 = 2$\n- `m1` is $|1\\rangle$ and `m2` is $|1\\rangle$: we return $1\\cdot2+1 = 3$\n\nIn other words, we treat the measurement results as the binary notation of the return value in big endian notation."
|
|
1434
1434
|
},
|
|
1435
1435
|
{
|
|
1436
1436
|
"type": "solution",
|
|
@@ -1440,7 +1440,7 @@ export default {
|
|
|
1440
1440
|
{
|
|
1441
1441
|
"type": "text-content",
|
|
1442
1442
|
"asHtml": "<p>We can generalize this code to read out an integer in big endian notation from a qubit array of arbitrary length using several library operations and functions:</p>\n<ul>\n<li><code>MeasureEachZ</code> measures each of the qubits in the array in the computational basis and returns an array of <code>Result</code> data type.</li>\n<li><code>Reversed</code> reverses the given array.</li>\n<li><code>ResultArrayAsInt</code> converts an array of bits given as <code>Result</code> to an integer, assuming little-endian notation (that's why we have to reverse the array before converting it).</li>\n</ul>\n",
|
|
1443
|
-
"asMarkdown": "\nWe can generalize this code to read out an integer in big endian notation from a qubit array of arbitrary length using several library operations and functions:\n\n
|
|
1443
|
+
"asMarkdown": "\nWe can generalize this code to read out an integer in big endian notation from a qubit array of arbitrary length using several library operations and functions:\n\n- `MeasureEachZ` measures each of the qubits in the array in the computational basis and returns an array of `Result` data type.\n- `Reversed` reverses the given array.\n- `ResultArrayAsInt` converts an array of bits given as `Result` to an integer, assuming little-endian notation (that's why we have to reverse the array before converting it)."
|
|
1444
1444
|
},
|
|
1445
1445
|
{
|
|
1446
1446
|
"type": "solution",
|
|
@@ -1457,8 +1457,8 @@ export default {
|
|
|
1457
1457
|
"items": [
|
|
1458
1458
|
{
|
|
1459
1459
|
"type": "text-content",
|
|
1460
|
-
"asHtml": "<p>For a system with $n>1$ qubits, it is possible to measure $m<n$ qubits one after another. The number of measurement outcomes is then $2^m$ instead of $2^n$. The probabilities of each of the outcomes and the post-measurement states of the qubits can be found using the projection formalism for measurements.</p>\n<p>First, we recall the concept of projection operators introduced in the single-qubit systems measurements kata. Measurements are modeled by orthogonal projection operators - matrices that satisfy\n$$P^2 = P^\\dagger = P$$\nConsider an $n$-qubit system in a state $|\\psi\\rangle$, for which the first $m<n$ qubits are measured in an orthogonal basis ${ |b_0\\rangle , |b_1\\rangle, \\dotsc, |b_{2^m-1}\\rangle}$ corresponding to the $m$ qubits being measured. Then we define $2^m$ projectors corresponding to each of the $|b_i\\rangle$ states as</p>\n<p>$$P_i = |b_i\\rangle \\langle b_i| \\otimes \\mathbb{1}_{n-m} $$</p>\n<p>where $\\mathbb{1}_{n-m}$ is the identity operator over the remaining $(n-m)$ qubits.</p>\n<p>The symbol $\\otimes$ represents the tensor product or the Kronecker product of two matrices. It is different from the usual matrix multiplication.\nIn the current context, $|b_i\\rangle \\langle b_i| \\otimes \\mathbb{1}_{n-m}$ simply means that
|
|
1461
|
-
"asMarkdown": "\nFor a system with $n>1$ qubits, it is possible to measure $m<n$ qubits one after another. The number of measurement outcomes is then $2^m$ instead of $2^n$. The probabilities of each of the outcomes and the post-measurement states of the qubits can be found using the projection formalism for measurements.\n\nFirst, we recall the concept of projection operators introduced in the single-qubit systems measurements kata. Measurements are modeled by orthogonal projection operators - matrices that satisfy\n$$P^2 = P^\\dagger = P$$\nConsider an $n$-qubit system in a state $|\\psi\\rangle$, for which the first $m<n$ qubits are measured in an orthogonal basis $\\{ |b_0\\rangle , |b_1\\rangle, \\dotsc, |b_{2^m-1}\\rangle\\}$ corresponding to the $m$ qubits being measured. Then we define $2^m$ projectors corresponding to each of the $|b_i\\rangle$ states as\n\n$$P_i = |b_i\\rangle \\langle b_i| \\otimes \\mathbb{1}_{n-m} $$\n\nwhere $\\mathbb{1}_{n-m}$ is the identity operator over the remaining $(n-m)$ qubits.\n\nThe symbol $\\otimes$ represents the tensor product or the Kronecker product of two matrices. It is different from the usual matrix multiplication.\nIn the current context, $|b_i\\rangle \\langle b_i| \\otimes \\mathbb{1}_{n-m}$ simply means that
|
|
1460
|
+
"asHtml": "<p>For a system with $n>1$ qubits, it is possible to measure $m<n$ qubits one after another. The number of measurement outcomes is then $2^m$ instead of $2^n$. The probabilities of each of the outcomes and the post-measurement states of the qubits can be found using the projection formalism for measurements.</p>\n<p>First, we recall the concept of projection operators introduced in the single-qubit systems measurements kata. Measurements are modeled by orthogonal projection operators - matrices that satisfy\n$$P^2 = P^\\dagger = P$$\nConsider an $n$-qubit system in a state $|\\psi\\rangle$, for which the first $m<n$ qubits are measured in an orthogonal basis ${ |b_0\\rangle , |b_1\\rangle, \\dotsc, |b_{2^m-1}\\rangle}$ corresponding to the $m$ qubits being measured. Then we define $2^m$ projectors corresponding to each of the $|b_i\\rangle$ states as</p>\n<p>$$P_i = |b_i\\rangle \\langle b_i| \\otimes \\mathbb{1}_{n-m} $$</p>\n<p>where $\\mathbb{1}_{n-m}$ is the identity operator over the remaining $(n-m)$ qubits.</p>\n<p>The symbol $\\otimes$ represents the tensor product or the Kronecker product of two matrices. It is different from the usual matrix multiplication.\nIn the current context, $|b_i\\rangle \\langle b_i| \\otimes \\mathbb{1}_{n-m}$ simply means that</p>\n<ul>\n<li>The operator $|b_i\\rangle \\langle b_i|$ acts only on the $m$ qubits being measured.</li>\n<li>The effect of $P_i$ on the remaining qubits is $\\mathbb{1}_{n-m} $, i.e., the identity operator.</li>\n</ul>\n<p>Analogous to the case for measurements for single-qubit systems, the rules for partial measurement probabilities and outcomes can be summarized as follows:</p>\n<ul>\n<li>When a measurement is done, one of these projectors is chosen randomly. The probability of choosing projector $P_i$ is $\\big|P_i|\\psi\\rangle\\big|^2$.</li>\n<li>If the projector $P_i$ is chosen, the measurement outcome is $b_i$, and the state of the system after the measurement is given by</li>\n</ul>\n<p>$$\n\\frac{P_i |\\psi\\rangle}{\\big|P_i |\\psi\\rangle\\big|}.\n$$</p>\n<p>For example, consider a two-qubit system in the state $\\ket \\psi = \\frac{1}{\\sqrt{2}}\\ket{01} - \\frac{1}{\\sqrt 2}\\ket{10}$. Consider a measurement of the first qubit in the computational basis, i.e., in the ${\\ket 0 , \\ket 1 }$ basis. Then, we have two projectors that represent this measurement:\n$$P_0 = \\ket 0\\bra 0 \\otimes \\mathbb{1}$$\n$$P_1 = \\ket 1 \\bra 1 \\otimes \\mathbb{1}$$</p>\n<p>The action of $P_0$ on $\\ket \\psi$ is</p>\n<p>$$P_0 \\ket \\psi$$</p>\n<p>$$= \\left(\\ket 0\\bra 0 \\otimes \\mathbb{1}\\right) \\frac{1}{\\sqrt 2}\\big(\\ket{01} - \\ket{10}\\big)$$</p>\n<p>$$= \\frac{1}{\\sqrt 2} \\big( \\ket 0\\bra 0 0\\rangle \\otimes \\mathbb{1} \\ket{1} - \\ket 0 \\bra 0 1\\rangle \\otimes \\mathbb{1} \\ket 0 \\big)$$</p>\n<p>$$= \\frac{1}{\\sqrt 2} \\ket{01}$$</p>\n<p>Similarly, we obtain\n$$P_1 \\ket\\psi = -\\frac{1}{\\sqrt 2} \\ket{10}$$</p>\n<p>Clearly, we have $\\big|P_0 \\ket \\psi\\big| = \\big|P_1 \\ket \\psi\\big| = \\frac{1}{2}$ in this case. Thus, the probabilities of measuring $0$ and $1$ are both $0.5$, with the post-measurement states of system being $\\ket{01}$ and $\\ket{10}$, respectively.</p>\n<blockquote>\n<p>Similar to the case of single-qubit system measurements, the applicability of the formalism above requires the state of the multi-qubit system, $\\ket \\psi$, to be normalized. This is required to ensure that all the probabilities of individual outcomes add up to 1.</p>\n</blockquote>\n<h2>🔎 Analyze</h2>\n<p><strong>Partial measurement probabilities for the Hardy state</strong></p>\n<p>Consider a 2-qubit system in the state $\\ket \\psi = \\frac{1}{\\sqrt{12}} \\big(3|00\\rangle + |01\\rangle + |10\\rangle + |11\\rangle\\big)$.</p>\n<p>If only the first qubit is measured in the computational basis, what are the probabilities of the outcomes, and the post-measurement states of the system?</p>\n<details>\n<summary><b>Solution</b></summary>\nA measurement outcome of $0$ on the first qubit corresponds to the projection operator $P_0 = |0\\rangle\\langle 0| \\otimes \\mathbb{1}$. Applying it to the state $\\ket \\psi$ gives us\n$$\\big|P_0 \\ket{\\psi}\\big|^2 = \\big|\\frac{1}{\\sqrt{12}} \\left(3\\ket {00} + \\ket{01}\\right) \\big|^2 = \\frac{5}{6}$$\nand\n$$\\frac{P_0 \\ket{\\psi}}{\\big|P_0 \\ket{\\psi}\\big|} = \\frac{1}{\\sqrt{10}} \\left( 3\\ket{00} + \\ket{01}\\right)$$\n\n<p>Similarly, $P_1 = |1\\rangle \\langle 1 | \\otimes \\mathbb{1}$ is the projector corresponding to a measurement outcome of $1$ on the first qubit. Applying $P_1$ on $\\ket{\\psi}$ gives us $\\big|P_1 \\ket{\\psi}\\big|^2 = \\frac{1}{6}$ and</p>\n<p>$$\\frac{P_1 \\ket{\\psi}}{\\big|P_1 \\ket{\\psi}\\big|} = \\frac{1}{\\sqrt{2}} \\left(\\ket{10} + \\ket{11}\\right)$$</p>\n<table>\n <tr>\n <th>Measurement outcome</th>\n <th>Probability of outcome</th>\n <th>Post-measurement state</th>\n </tr>\n <tr>\n <td>$0$</td>\n <td>$\\frac{5}{6}$</td>\n <td>$\\frac{1}{\\sqrt{10}} \\left( 3\\ket{00} + \\ket{01}\\right)$</td>\n </tr>\n <tr>\n <td>$1$</td>\n <td>$\\frac{1}{6}$</td>\n <td>$\\frac{1}{\\sqrt{2}} \\left(\\ket{10} + \\ket{11}\\right)$</td>\n </tr>\n</table>\n</details>",
|
|
1461
|
+
"asMarkdown": "\nFor a system with $n>1$ qubits, it is possible to measure $m<n$ qubits one after another. The number of measurement outcomes is then $2^m$ instead of $2^n$. The probabilities of each of the outcomes and the post-measurement states of the qubits can be found using the projection formalism for measurements.\n\nFirst, we recall the concept of projection operators introduced in the single-qubit systems measurements kata. Measurements are modeled by orthogonal projection operators - matrices that satisfy\n$$P^2 = P^\\dagger = P$$\nConsider an $n$-qubit system in a state $|\\psi\\rangle$, for which the first $m<n$ qubits are measured in an orthogonal basis $\\{ |b_0\\rangle , |b_1\\rangle, \\dotsc, |b_{2^m-1}\\rangle\\}$ corresponding to the $m$ qubits being measured. Then we define $2^m$ projectors corresponding to each of the $|b_i\\rangle$ states as\n\n$$P_i = |b_i\\rangle \\langle b_i| \\otimes \\mathbb{1}_{n-m} $$\n\nwhere $\\mathbb{1}_{n-m}$ is the identity operator over the remaining $(n-m)$ qubits.\n\nThe symbol $\\otimes$ represents the tensor product or the Kronecker product of two matrices. It is different from the usual matrix multiplication.\nIn the current context, $|b_i\\rangle \\langle b_i| \\otimes \\mathbb{1}_{n-m}$ simply means that\n\n- The operator $|b_i\\rangle \\langle b_i|$ acts only on the $m$ qubits being measured.\n- The effect of $P_i$ on the remaining qubits is $\\mathbb{1}_{n-m} $, i.e., the identity operator.\n\nAnalogous to the case for measurements for single-qubit systems, the rules for partial measurement probabilities and outcomes can be summarized as follows:\n\n- When a measurement is done, one of these projectors is chosen randomly. The probability of choosing projector $P_i$ is $\\big|P_i|\\psi\\rangle\\big|^2$.\n- If the projector $P_i$ is chosen, the measurement outcome is $b_i$, and the state of the system after the measurement is given by\n\n$$\n\\frac{P_i |\\psi\\rangle}{\\big|P_i |\\psi\\rangle\\big|}.\n$$\n\nFor example, consider a two-qubit system in the state $\\ket \\psi = \\frac{1}{\\sqrt{2}}\\ket{01} - \\frac{1}{\\sqrt 2}\\ket{10}$. Consider a measurement of the first qubit in the computational basis, i.e., in the $\\{\\ket 0 , \\ket 1 \\}$ basis. Then, we have two projectors that represent this measurement:\n$$P_0 = \\ket 0\\bra 0 \\otimes \\mathbb{1}$$\n$$P_1 = \\ket 1 \\bra 1 \\otimes \\mathbb{1}$$\n\nThe action of $P_0$ on $\\ket \\psi$ is\n\n$$P_0 \\ket \\psi$$\n\n$$= \\left(\\ket 0\\bra 0 \\otimes \\mathbb{1}\\right) \\frac{1}{\\sqrt 2}\\big(\\ket{01} - \\ket{10}\\big)$$\n\n$$= \\frac{1}{\\sqrt 2} \\big( \\ket 0\\bra 0 0\\rangle \\otimes \\mathbb{1} \\ket{1} - \\ket 0 \\bra 0 1\\rangle \\otimes \\mathbb{1} \\ket 0 \\big)$$\n\n$$= \\frac{1}{\\sqrt 2} \\ket{01}$$\n\nSimilarly, we obtain\n$$P_1 \\ket\\psi = -\\frac{1}{\\sqrt 2} \\ket{10}$$\n\nClearly, we have $\\big|P_0 \\ket \\psi\\big| = \\big|P_1 \\ket \\psi\\big| = \\frac{1}{2}$ in this case. Thus, the probabilities of measuring $0$ and $1$ are both $0.5$, with the post-measurement states of system being $\\ket{01}$ and $\\ket{10}$, respectively.\n\n> Similar to the case of single-qubit system measurements, the applicability of the formalism above requires the state of the multi-qubit system, $\\ket \\psi$, to be normalized. This is required to ensure that all the probabilities of individual outcomes add up to 1.\n\n## 🔎 Analyze\n\n**Partial measurement probabilities for the Hardy state**\n\nConsider a 2-qubit system in the state $\\ket \\psi = \\frac{1}{\\sqrt{12}} \\big(3|00\\rangle + |01\\rangle + |10\\rangle + |11\\rangle\\big)$.\n\nIf only the first qubit is measured in the computational basis, what are the probabilities of the outcomes, and the post-measurement states of the system?\n\n<details>\n<summary><b>Solution</b></summary>\nA measurement outcome of $0$ on the first qubit corresponds to the projection operator $P_0 = |0\\rangle\\langle 0| \\otimes \\mathbb{1}$. Applying it to the state $\\ket \\psi$ gives us\n$$\\big|P_0 \\ket{\\psi}\\big|^2 = \\big|\\frac{1}{\\sqrt{12}} \\left(3\\ket {00} + \\ket{01}\\right) \\big|^2 = \\frac{5}{6}$$\nand\n$$\\frac{P_0 \\ket{\\psi}}{\\big|P_0 \\ket{\\psi}\\big|} = \\frac{1}{\\sqrt{10}} \\left( 3\\ket{00} + \\ket{01}\\right)$$\n\nSimilarly, $P_1 = |1\\rangle \\langle 1 | \\otimes \\mathbb{1}$ is the projector corresponding to a measurement outcome of $1$ on the first qubit. Applying $P_1$ on $\\ket{\\psi}$ gives us $\\big|P_1 \\ket{\\psi}\\big|^2 = \\frac{1}{6}$ and\n\n$$\\frac{P_1 \\ket{\\psi}}{\\big|P_1 \\ket{\\psi}\\big|} = \\frac{1}{\\sqrt{2}} \\left(\\ket{10} + \\ket{11}\\right)$$\n\n<table>\n <tr>\n <th>Measurement outcome</th>\n <th>Probability of outcome</th>\n <th>Post-measurement state</th>\n </tr>\n <tr>\n <td>$0$</td>\n <td>$\\frac{5}{6}$</td>\n <td>$\\frac{1}{\\sqrt{10}} \\left( 3\\ket{00} + \\ket{01}\\right)$</td>\n </tr>\n <tr>\n <td>$1$</td>\n <td>$\\frac{1}{6}$</td>\n <td>$\\frac{1}{\\sqrt{2}} \\left(\\ket{10} + \\ket{11}\\right)$</td>\n </tr>\n</table>\n</details>"
|
|
1462
1462
|
}
|
|
1463
1463
|
]
|
|
1464
1464
|
},
|
|
@@ -1494,8 +1494,8 @@ export default {
|
|
|
1494
1494
|
"asMarkdown": "**Input**: A 3-qubit system which is guaranteed to be in either the $\\ket{++-}$ state, or the $\\ket{---}$ state.\n\n**Output**: \n- 0 if the qubit is in the $\\ket{++-}$ state,\n- 1 if the qubit is in the $\\ket{---}$ state.\n\nThe state of the qubits at the end of the operation should be the same as the initial state.\n"
|
|
1495
1495
|
},
|
|
1496
1496
|
"sourceIds": [
|
|
1497
|
-
"
|
|
1498
|
-
"
|
|
1497
|
+
"multi_qubit_measurements__partial_measurements_for_system__Verification.qs",
|
|
1498
|
+
"multi_qubit_measurements__Common.qs",
|
|
1499
1499
|
"KatasLibrary.qs"
|
|
1500
1500
|
],
|
|
1501
1501
|
"placeholderCode": "namespace Kata {\n operation IsPlusPlusMinus(qs : Qubit[]) : Int {\n // Implement your solution here...\n\n return -1;\n }\n\n}\n",
|
|
@@ -1522,8 +1522,8 @@ export default {
|
|
|
1522
1522
|
"items": [
|
|
1523
1523
|
{
|
|
1524
1524
|
"type": "text-content",
|
|
1525
|
-
"asHtml": "<p>Entanglement has an effect on the measurement statistics of the system. If two qubits are entangled, then their measurement outcomes will be correlated, while separable states (which are by definition not entangled) have uncorrelated measurement outcomes.</p>\n<blockquote>\n<p>It is useful to revisit the concepts of entanglement and separable states, which were introduced in the kata on multi-qubit systems. Consider a system of $n>1$ number of qubits, which we divide into two parts: A, consisting of $m$ qubits, and B, consisting of the remaining $n-m$ qubits. We say that the state $\\ket \\psi$ of the entire system is separable if it can be expressed as a tensor product of the states of parts A and B:\n$$\n\\ket \\psi = \\ket {\\phi_A} \\otimes \\ket{\\phi_B}\n$$\nwhere $\\ket{\\phi_A}$ and $\\ket{\\phi_B}$ are wave functions that describe parts $A$ and $B$, respectively. If it is not possible to express $\\ket \\psi$ in such a form, then we say that system A is entangled with system B.</p>\n</blockquote>\n<p>Consider a measurement on the subsystem $A$ of a separable state. Let the measurement be done in a basis ${ \\ket{b_0},\\dotsc,\\ket{b_{2^m-1}}}$. According to the projection formalism, a projection operator $P_i = \\ket{b_i}\\bra{b_i} \\otimes \\mathbb{1}$ is chosen randomly. The corresponding post-measurement state of the system is then given by</p>\n<p>$$\\ket{\\psi}_{i} \\equiv \\frac{P_i \\ket{\\psi}}{\\big|P_i \\ket{\\psi}\\big|}$$</p>\n<p>$$= \\frac{\\ket{b_i}\\bra{b_i}\\phi_A\\rangle \\otimes \\ket {\\phi_B}}{\\big|\\ket{b_i}\\bra{b_i}\\phi_A\\rangle \\otimes \\ket {\\phi_B}\\big|}$$</p>\n<p>$$= \\frac{\\bra{b_i}\\phi_A\\rangle \\cdot \\ket{b_i} \\otimes \\ket {\\phi_B}}{\\big|\\ket{b_i}\\big| \\cdot \\bra{b_i}\\phi_A\\rangle \\cdot \\big| \\ket {\\phi_B}\\big|}$$</p>\n<p>$$= \\ket{b_i} \\otimes \\ket{\\phi_B}$$</p>\n<p>Thus, the state of subsystem $B$ after the measurement is $\\ket{\\phi_B}$ independently of the outcome $i$ of the measurement on the first qubit. The results of a subsequent measurement on subsystem $B$, including outcome probabilities, will be independent of the result of the first measurement. In other words, the outcomes of the two measurements will be uncorrelated.</p>\n<p>On the other hand, if the system is entangled, then the measurement outcomes will be correlated, in a manner dictated by the bases chosen for the measurements on the two subsystems. The following exercise illustrates this phenomenon.</p>\n<h2>🔎 Analyze</h2>\n<p><strong>Sequential measurements on an entangled state and a separable state</strong></p>\n<p>Consider two two-qubit states:</p>\n<ul>\n<li>The Bell state $|\\Phi^{+}\\rangle = \\frac{1}{\\sqrt{2}} \\big (|00\\rangle + |11\\rangle\\big)$.</li>\n<li>A state $\\ket \\Theta = \\frac{1}{2} \\big( \\ket{00} + \\ket{01} + \\ket{10} + \\ket{11} \\big)$.</li>\n</ul>\n<p>For both states, consider a measurement on the first qubit, followed by a measurement on the second qubit, both done in the computational basis. For which state can we expect the measurement outcomes to be correlated? Verify by calculating the sequential measurement probabilities explicitly for both states
|
|
1526
|
-
"asMarkdown": "\nEntanglement has an effect on the measurement statistics of the system. If two qubits are entangled, then their measurement outcomes will be correlated, while separable states (which are by definition not entangled) have uncorrelated measurement outcomes.\n\n> It is useful to revisit the concepts of entanglement and separable states, which were introduced in the kata on multi-qubit systems. Consider a system of $n>1$ number of qubits, which we divide into two parts: A, consisting of $m$ qubits, and B, consisting of the remaining $n-m$ qubits. We say that the state $\\ket \\psi$ of the entire system is separable if it can be expressed as a tensor product of the states of parts A and B:\n$$\n\\ket \\psi = \\ket {\\phi_A} \\otimes \\ket{\\phi_B}\n$$\nwhere $\\ket{\\phi_A}$ and $\\ket{\\phi_B}$ are wave functions that describe parts $A$ and $B$, respectively. If it is not possible to express $\\ket \\psi$ in such a form, then we say that system A is entangled with system B.\n\nConsider a measurement on the subsystem $A$ of a separable state. Let the measurement be done in a basis $\\{ \\ket{b_0},\\dotsc,\\ket{b_{2^m-1}}\\}$. According to the projection formalism, a projection operator $P_i = \\ket{b_i}\\bra{b_i} \\otimes \\mathbb{1}$ is chosen randomly. The corresponding post-measurement state of the system is then given by\n\n$$\\ket{\\psi}_{i} \\equiv \\frac{P_i \\ket{\\psi}}{\\big|P_i \\ket{\\psi}\\big|}$$\n\n$$= \\frac{\\ket{b_i}\\bra{b_i}\\phi_A\\rangle \\otimes \\ket {\\phi_B}}{\\big|\\ket{b_i}\\bra{b_i}\\phi_A\\rangle \\otimes \\ket {\\phi_B}\\big|}$$\n\n$$= \\frac{\\bra{b_i}\\phi_A\\rangle \\cdot \\ket{b_i} \\otimes \\ket {\\phi_B}}{\\big|\\ket{b_i}\\big| \\cdot \\bra{b_i}\\phi_A\\rangle \\cdot \\big| \\ket {\\phi_B}\\big|}$$\n\n$$= \\ket{b_i} \\otimes \\ket{\\phi_B}$$\n\nThus, the state of subsystem $B$ after the measurement is $\\ket{\\phi_B}$ independently of the outcome $i$ of the measurement on the first qubit. The results of a subsequent measurement on subsystem $B$, including outcome probabilities, will be independent of the result of the first measurement. In other words, the outcomes of the two measurements will be uncorrelated.\n\nOn the other hand, if the system is entangled, then the measurement outcomes will be correlated, in a manner dictated by the bases chosen for the measurements on the two subsystems. The following exercise illustrates this phenomenon.\n\n## 🔎 Analyze\n\n**Sequential measurements on an entangled state and a separable state**\n\nConsider two two-qubit states:\n- The Bell state $|\\Phi^{+}\\rangle = \\frac{1}{\\sqrt{2}} \\big (|00\\rangle + |11\\rangle\\big)$.\n- A state $\\ket \\Theta = \\frac{1}{2} \\big( \\ket{00} + \\ket{01} + \\ket{10} + \\ket{11} \\big)$.\n\nFor both states, consider a measurement on the first qubit, followed by a measurement on the second qubit, both done in the computational basis. For which state can we expect the measurement outcomes to be correlated? Verify by calculating the sequential measurement probabilities explicitly for both states
|
|
1525
|
+
"asHtml": "<p>Entanglement has an effect on the measurement statistics of the system. If two qubits are entangled, then their measurement outcomes will be correlated, while separable states (which are by definition not entangled) have uncorrelated measurement outcomes.</p>\n<blockquote>\n<p>It is useful to revisit the concepts of entanglement and separable states, which were introduced in the kata on multi-qubit systems. Consider a system of $n>1$ number of qubits, which we divide into two parts: A, consisting of $m$ qubits, and B, consisting of the remaining $n-m$ qubits. We say that the state $\\ket \\psi$ of the entire system is separable if it can be expressed as a tensor product of the states of parts A and B:\n$$\n\\ket \\psi = \\ket {\\phi_A} \\otimes \\ket{\\phi_B}\n$$\nwhere $\\ket{\\phi_A}$ and $\\ket{\\phi_B}$ are wave functions that describe parts $A$ and $B$, respectively. If it is not possible to express $\\ket \\psi$ in such a form, then we say that system A is entangled with system B.</p>\n</blockquote>\n<p>Consider a measurement on the subsystem $A$ of a separable state. Let the measurement be done in a basis ${ \\ket{b_0},\\dotsc,\\ket{b_{2^m-1}}}$. According to the projection formalism, a projection operator $P_i = \\ket{b_i}\\bra{b_i} \\otimes \\mathbb{1}$ is chosen randomly. The corresponding post-measurement state of the system is then given by</p>\n<p>$$\\ket{\\psi}_{i} \\equiv \\frac{P_i \\ket{\\psi}}{\\big|P_i \\ket{\\psi}\\big|}$$</p>\n<p>$$= \\frac{\\ket{b_i}\\bra{b_i}\\phi_A\\rangle \\otimes \\ket {\\phi_B}}{\\big|\\ket{b_i}\\bra{b_i}\\phi_A\\rangle \\otimes \\ket {\\phi_B}\\big|}$$</p>\n<p>$$= \\frac{\\bra{b_i}\\phi_A\\rangle \\cdot \\ket{b_i} \\otimes \\ket {\\phi_B}}{\\big|\\ket{b_i}\\big| \\cdot \\bra{b_i}\\phi_A\\rangle \\cdot \\big| \\ket {\\phi_B}\\big|}$$</p>\n<p>$$= \\ket{b_i} \\otimes \\ket{\\phi_B}$$</p>\n<p>Thus, the state of subsystem $B$ after the measurement is $\\ket{\\phi_B}$ independently of the outcome $i$ of the measurement on the first qubit. The results of a subsequent measurement on subsystem $B$, including outcome probabilities, will be independent of the result of the first measurement. In other words, the outcomes of the two measurements will be uncorrelated.</p>\n<p>On the other hand, if the system is entangled, then the measurement outcomes will be correlated, in a manner dictated by the bases chosen for the measurements on the two subsystems. The following exercise illustrates this phenomenon.</p>\n<h2>🔎 Analyze</h2>\n<p><strong>Sequential measurements on an entangled state and a separable state</strong></p>\n<p>Consider two two-qubit states:</p>\n<ul>\n<li>The Bell state $|\\Phi^{+}\\rangle = \\frac{1}{\\sqrt{2}} \\big (|00\\rangle + |11\\rangle\\big)$.</li>\n<li>A state $\\ket \\Theta = \\frac{1}{2} \\big( \\ket{00} + \\ket{01} + \\ket{10} + \\ket{11} \\big)$.</li>\n</ul>\n<p>For both states, consider a measurement on the first qubit, followed by a measurement on the second qubit, both done in the computational basis. For which state can we expect the measurement outcomes to be correlated? Verify by calculating the sequential measurement probabilities explicitly for both states.</p>\n<details>\n<summary><b>Solution</b></summary>\n<p><b>The Bell state</b>: If the measurement outcome on the first qubit is $0$, a subsequent measurement on the second qubit *always* results in an outcome of $0$, with probability $1$. Similarly, if the measurement outcome on the first qubit is $1$, then the second qubit measurement always results in $1$. Thus, sequential measurements are perfectly *correlated*.</p>\n\n<p><b>Separable state $\\ket \\theta$</b>: Irrespective of whether the first qubit measurement outcome is $0$ of $1$ (each of which occurs with a probability of $0.5$), a subsequent measurement on the second qubit results in an outcome of $0$ or $1$ (both with a probability of $0.5$). Thus, sequential measurements are perfectly <b>uncorrelated</b>.</p>\n\n<p>This aligns with the fact that the Bell state is entangled, while the $\\ket{\\theta}$ is separable and can be expressed as $\\ket \\theta = \\ket + \\otimes \\ket +$.</p>\n</details>\n\n<h2>State Modification Using Partial Measurements</h2>\n<p>For certain multi-qubit systems prepared in a superposition state, it is possible to use partial measurements to collapse a part of the system to some desired state.</p>\n",
|
|
1526
|
+
"asMarkdown": "\nEntanglement has an effect on the measurement statistics of the system. If two qubits are entangled, then their measurement outcomes will be correlated, while separable states (which are by definition not entangled) have uncorrelated measurement outcomes.\n\n> It is useful to revisit the concepts of entanglement and separable states, which were introduced in the kata on multi-qubit systems. Consider a system of $n>1$ number of qubits, which we divide into two parts: A, consisting of $m$ qubits, and B, consisting of the remaining $n-m$ qubits. We say that the state $\\ket \\psi$ of the entire system is separable if it can be expressed as a tensor product of the states of parts A and B:\n$$\n\\ket \\psi = \\ket {\\phi_A} \\otimes \\ket{\\phi_B}\n$$\nwhere $\\ket{\\phi_A}$ and $\\ket{\\phi_B}$ are wave functions that describe parts $A$ and $B$, respectively. If it is not possible to express $\\ket \\psi$ in such a form, then we say that system A is entangled with system B.\n\nConsider a measurement on the subsystem $A$ of a separable state. Let the measurement be done in a basis $\\{ \\ket{b_0},\\dotsc,\\ket{b_{2^m-1}}\\}$. According to the projection formalism, a projection operator $P_i = \\ket{b_i}\\bra{b_i} \\otimes \\mathbb{1}$ is chosen randomly. The corresponding post-measurement state of the system is then given by\n\n$$\\ket{\\psi}_{i} \\equiv \\frac{P_i \\ket{\\psi}}{\\big|P_i \\ket{\\psi}\\big|}$$\n\n$$= \\frac{\\ket{b_i}\\bra{b_i}\\phi_A\\rangle \\otimes \\ket {\\phi_B}}{\\big|\\ket{b_i}\\bra{b_i}\\phi_A\\rangle \\otimes \\ket {\\phi_B}\\big|}$$\n\n$$= \\frac{\\bra{b_i}\\phi_A\\rangle \\cdot \\ket{b_i} \\otimes \\ket {\\phi_B}}{\\big|\\ket{b_i}\\big| \\cdot \\bra{b_i}\\phi_A\\rangle \\cdot \\big| \\ket {\\phi_B}\\big|}$$\n\n$$= \\ket{b_i} \\otimes \\ket{\\phi_B}$$\n\nThus, the state of subsystem $B$ after the measurement is $\\ket{\\phi_B}$ independently of the outcome $i$ of the measurement on the first qubit. The results of a subsequent measurement on subsystem $B$, including outcome probabilities, will be independent of the result of the first measurement. In other words, the outcomes of the two measurements will be uncorrelated.\n\nOn the other hand, if the system is entangled, then the measurement outcomes will be correlated, in a manner dictated by the bases chosen for the measurements on the two subsystems. The following exercise illustrates this phenomenon.\n\n## 🔎 Analyze\n\n**Sequential measurements on an entangled state and a separable state**\n\nConsider two two-qubit states:\n\n- The Bell state $|\\Phi^{+}\\rangle = \\frac{1}{\\sqrt{2}} \\big (|00\\rangle + |11\\rangle\\big)$.\n- A state $\\ket \\Theta = \\frac{1}{2} \\big( \\ket{00} + \\ket{01} + \\ket{10} + \\ket{11} \\big)$.\n\nFor both states, consider a measurement on the first qubit, followed by a measurement on the second qubit, both done in the computational basis. For which state can we expect the measurement outcomes to be correlated? Verify by calculating the sequential measurement probabilities explicitly for both states.\n\n<details>\n<summary><b>Solution</b></summary>\n<p><b>The Bell state</b>: If the measurement outcome on the first qubit is $0$, a subsequent measurement on the second qubit *always* results in an outcome of $0$, with probability $1$. Similarly, if the measurement outcome on the first qubit is $1$, then the second qubit measurement always results in $1$. Thus, sequential measurements are perfectly *correlated*.</p>\n\n<p><b>Separable state $\\ket \\theta$</b>: Irrespective of whether the first qubit measurement outcome is $0$ of $1$ (each of which occurs with a probability of $0.5$), a subsequent measurement on the second qubit results in an outcome of $0$ or $1$ (both with a probability of $0.5$). Thus, sequential measurements are perfectly <b>uncorrelated</b>.</p>\n\n<p>This aligns with the fact that the Bell state is entangled, while the $\\ket{\\theta}$ is separable and can be expressed as $\\ket \\theta = \\ket + \\otimes \\ket +$.</p>\n</details>\n\n## State Modification Using Partial Measurements\n\nFor certain multi-qubit systems prepared in a superposition state, it is possible to use partial measurements to collapse a part of the system to some desired state."
|
|
1527
1527
|
}
|
|
1528
1528
|
]
|
|
1529
1529
|
},
|
|
@@ -1537,8 +1537,8 @@ export default {
|
|
|
1537
1537
|
"asMarkdown": "**Input**: \n1. A 2-qubit system in the state\n$$\n|\\psi\\rangle = \\frac{1}{\\sqrt{2}} |0\\rangle \\otimes ( a |0\\rangle + b|1\\rangle) + \\frac{1}{\\sqrt{2}} |1\\rangle \\otimes (b|0\\rangle + a |1\\rangle),\n$$\nwhere the constants $a$ and $b$ satisfying $|a|^2 + |b|^2 = 1$ are unknown.\n2. An integer $ind$ which is either $0$ or $1$.\n\n**Goal**: \n- If $ind$ equals 0, the state of the second qubit should be $a|0\\rangle + b|1\\rangle$\n- If $ind$ equals 1, the state of the second qubit should be $b|0\\rangle + a |1\\rangle$. \n\nThe state of the first qubit at the end does not matter.\n"
|
|
1538
1538
|
},
|
|
1539
1539
|
"sourceIds": [
|
|
1540
|
-
"
|
|
1541
|
-
"
|
|
1540
|
+
"multi_qubit_measurements__state_modification__Verification.qs",
|
|
1541
|
+
"multi_qubit_measurements__Common.qs",
|
|
1542
1542
|
"KatasLibrary.qs"
|
|
1543
1543
|
],
|
|
1544
1544
|
"placeholderCode": "namespace Kata {\n operation StateSelction(qs : Qubit[], ind : Int) : Unit {\n // Implement your solution here...\n\n }\n}\n",
|
|
@@ -1580,8 +1580,8 @@ export default {
|
|
|
1580
1580
|
"asMarkdown": "**Input:** Two qubits (in an array) which are in the state $\\ket{00}$.\n\n**Goal:** Modify the qubits to the state $\\frac{1}{\\sqrt{3}} \\big(|00\\rangle + |01\\rangle + |10\\rangle\\big)$ using post-selection.\n\n<br/>\n<details>\n <summary><b>Need a hint?</b></summary>\n Consider a 3-qubit state $\\frac{1}{2}(|00\\rangle + |01\\rangle + |11\\rangle) \\otimes |0\\rangle + \\frac{1}{2} |11\\rangle \\otimes |1\\rangle$.\n What happens when one measures the third qubit?\n</details>\n"
|
|
1581
1581
|
},
|
|
1582
1582
|
"sourceIds": [
|
|
1583
|
-
"
|
|
1584
|
-
"
|
|
1583
|
+
"multi_qubit_measurements__state_preparation__Verification.qs",
|
|
1584
|
+
"multi_qubit_measurements__Common.qs",
|
|
1585
1585
|
"KatasLibrary.qs"
|
|
1586
1586
|
],
|
|
1587
1587
|
"placeholderCode": "namespace Kata {\n operation PostSelection(qs : Qubit[]) : Unit {\n // Implement your solution here...\n\n }\n}\n",
|
|
@@ -1590,8 +1590,8 @@ export default {
|
|
|
1590
1590
|
"items": [
|
|
1591
1591
|
{
|
|
1592
1592
|
"type": "text-content",
|
|
1593
|
-
"asHtml": "<p>While it is possible to prepare the state $\\ket \\psi$ directly using unitary rotations, it is simpler to use post-selection for preparing it. Here, we describe the procedure in more detail below.</p>\n<p>Initially we will prepare an equal superposition of all basis states corresponding to the first two qubits by applying the <strong>H</strong> gate to each of them
|
|
1594
|
-
"asMarkdown": "\nWhile it is possible to prepare the state $\\ket \\psi$ directly using unitary rotations, it is simpler to use post-selection for preparing it. Here, we describe the procedure in more detail below.\n\nInitially we will prepare an equal superposition of all basis states corresponding to the first two qubits by applying the **H** gate to each of them
|
|
1593
|
+
"asHtml": "<p>While it is possible to prepare the state $\\ket \\psi$ directly using unitary rotations, it is simpler to use post-selection for preparing it. Here, we describe the procedure in more detail below.</p>\n<p>Initially we will prepare an equal superposition of all basis states corresponding to the first two qubits by applying the <strong>H</strong> gate to each of them:\n$$\\frac{1}{2} \\big(|00\\rangle + |01\\rangle + |10\\rangle + |11\\rangle\\big) \\otimes \\ket 0$$</p>\n<p>This state is a superposition of the state we want to prepare, and the $|11\\rangle$ state that we would like to discard.</p>\n<p>Now, we want to separate the first three basis states from the last one and to store this separation in the extra qubit.\nFor example, we can keep the state of the extra qubit $|0\\rangle$ for the basis states that we want to keep, and switch it to $|1\\rangle$ for the basis states that we would like to discard.\nA <strong>CCNOT</strong> gate can be used to accomplish this, with the first two qubits used as control qubits and the extra qubit as target.\nWhen the gate is applied, the state of the extra qubit will only change to $|1\\rangle$ if both control qubits are in the $|11\\rangle$ state, which marks exactly the state that we want to discard:</p>\n<p>$$\\text{CCNOT}\\frac{1}{2} \\big(|00\\textbf{0}\\rangle + |01\\textbf{0}\\rangle + |10\\textbf{0}\\rangle + |11\\textbf{0}\\rangle\\big) =\n\\frac{1}{2}\\big(|00\\rangle + |01\\rangle + |10\\rangle \\big) \\otimes |\\textbf{0}\\rangle + \\frac{1}{2}|11\\rangle \\otimes |\\textbf{1}\\rangle $$</p>\n<p>Finally we measure just the extra qubit; this causes a partial collapse of the system to the state defined by the measurement result:</p>\n<ul>\n<li>If the result is $|0\\rangle$, the system collapses to a state that is a linear combination of basis states which had the extra qubit in state $|0\\rangle$, i.e., the two qubits end up in the target state $\\frac{1}{\\sqrt3}\\big(|00\\rangle + |01\\rangle + |10\\rangle\\big)$.</li>\n<li>If the result is $|1\\rangle$, the system collapses to a state $|11\\rangle$, so our goal is not achieved. The good thing is, this only happens in 25% of the cases, and we can just reset our qubits to the $|00\\rangle$ state and try again.</li>\n</ul>\n<blockquote>\n<p>Q# has a built-in <a href=\"https://docs.microsoft.com/en-us/quantum/user-guide/using-qsharp/control-flow#repeat-until-success-loop\" target=\"_blank\">repeat-until-success (RUS) loop</a>, which comes in handy in this case.</p>\n<ul>\n<li>We will describe the main operations (applying <strong>H</strong> and <strong>CCNOT</strong> gates and the measurement) in the <code>repeat</code> part of the loop, which specifies its body. </li>\n<li><code>until</code> section specifies the condition which will break the loop. In this case the result of the measurement needs to be <code>Zero</code> to indicate our success. </li>\n<li>Finally, the <code>fixup</code> section allows us to clean up the results of the loop body execution before trying again if the success criteria is not met. In this case we reset the first two qubits back to the $|00\\rangle$ state.</li>\n</ul>\n</blockquote>\n<p>This technique is sometimes called post-selection.</p>\n",
|
|
1594
|
+
"asMarkdown": "\nWhile it is possible to prepare the state $\\ket \\psi$ directly using unitary rotations, it is simpler to use post-selection for preparing it. Here, we describe the procedure in more detail below.\n\nInitially we will prepare an equal superposition of all basis states corresponding to the first two qubits by applying the **H** gate to each of them:\n$$\\frac{1}{2} \\big(|00\\rangle + |01\\rangle + |10\\rangle + |11\\rangle\\big) \\otimes \\ket 0$$\n\nThis state is a superposition of the state we want to prepare, and the $|11\\rangle$ state that we would like to discard.\n\nNow, we want to separate the first three basis states from the last one and to store this separation in the extra qubit.\nFor example, we can keep the state of the extra qubit $|0\\rangle$ for the basis states that we want to keep, and switch it to $|1\\rangle$ for the basis states that we would like to discard.\nA **CCNOT** gate can be used to accomplish this, with the first two qubits used as control qubits and the extra qubit as target.\nWhen the gate is applied, the state of the extra qubit will only change to $|1\\rangle$ if both control qubits are in the $|11\\rangle$ state, which marks exactly the state that we want to discard:\n\n$$\\text{CCNOT}\\frac{1}{2} \\big(|00\\textbf{0}\\rangle + |01\\textbf{0}\\rangle + |10\\textbf{0}\\rangle + |11\\textbf{0}\\rangle\\big) =\n\\frac{1}{2}\\big(|00\\rangle + |01\\rangle + |10\\rangle \\big) \\otimes |\\textbf{0}\\rangle + \\frac{1}{2}|11\\rangle \\otimes |\\textbf{1}\\rangle $$\n\nFinally 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 a state that is a linear combination of basis states which had the extra qubit in state $|0\\rangle$, i.e., the two qubits end up in the target state $\\frac{1}{\\sqrt3}\\big(|00\\rangle + |01\\rangle + |10\\rangle\\big)$.\n* If the result is $|1\\rangle$, the system collapses to a state $|11\\rangle$, so our goal is not achieved. The good thing is, this only happens in 25% of the cases, and we can just reset our qubits to the $|00\\rangle$ state and try again.\n\n> Q# has a built-in <a href=\"https://docs.microsoft.com/en-us/quantum/user-guide/using-qsharp/control-flow#repeat-until-success-loop\" target=\"_blank\">repeat-until-success (RUS) loop</a>, which comes in handy in this case.\n> * We will describe the main operations (applying **H** and **CCNOT** gates and the measurement) in the `repeat` part of the loop, which specifies its body. \n> * `until` section specifies the condition which will break the loop. In this case the result of the measurement needs to be `Zero` to indicate our success. \n> * Finally, the `fixup` section allows us to clean up the results of the loop body execution before trying again if the success criteria is not met. In this case we reset the first two qubits back to the $|00\\rangle$ state.\n\nThis technique is sometimes called post-selection."
|
|
1595
1595
|
},
|
|
1596
1596
|
{
|
|
1597
1597
|
"type": "solution",
|
|
@@ -1609,7 +1609,7 @@ export default {
|
|
|
1609
1609
|
{
|
|
1610
1610
|
"type": "text-content",
|
|
1611
1611
|
"asHtml": "<p>Joint measurements, also known as Pauli measurements, are a generalization of 2-outcome measurements to multiple qubits and other bases. In Q#, joint measurements in Pauli bases are implemented using the <a href=\"https://docs.microsoft.com/en-us/qsharp/api/qsharp/microsoft.quantum.intrinsic.measure\" target=\"_blank\"><code>Measure</code></a> operation. Let's review single-qubit measurements in a different light before discussing joint measurements.</p>\n<h2>Single-Qubit Pauli Measurement</h2>\n<p>For single-qubit systems, any measurement corresponding to an orthogonal basis can be associated with a Hermitian matrix with eigenvalues $\\pm 1$. The possible measurement outcomes (represented as <code>Result</code> in Q#) are the eigenvalues of the Hermitian matrix, and the corresponding projection matrices for the measurement are the projection operators onto the <em>eigenspaces</em> corresponding to the eigenvalues.</p>\n<p>For example, consider the computational basis measurement, which can result in outcomes <code>Zero</code> or <code>One</code> corresponding to states $\\ket 0$ and $\\ket 1$. This measurement is associated with the Pauli Z operator, which is given by</p>\n<p>$$Z = \\begin{pmatrix} 1 & 0 \\\\ 0 & -1\\end{pmatrix} = \\ket{0}\\bra{0} - \\ket{1}\\bra{1}$$</p>\n<p>The $Z$ operator has two eigenvalues, $1$ and $-1$, with corresponding eigenvectors $\\ket{0}$ and $\\ket{1}$. A $Z$-measurement is then a measurement in the $\\{\\ket{0},\\ket{1}\\}$ basis, with the measurement outcomes being $1$ and $-1$ respectively. In Q#, by convention, an eigenvalue of $1$ corresponds to a <code>Result</code> of <code>Zero</code>, while an eigenvalue of $-1$ corresponds to a <code>Result</code> of <code>One</code>.</p>\n<p>Similarly, one can implement measurements corresponding to the Pauli X and Y operators. We summarize the various properties below:</p>\n<table>\n <tr>\n <th>Pauli Operator</th>\n <th>Matrix</th>\n <th>Eigenvalue</th>\n <th>Eigenvector/post-measurement state</th>\n <th>Measurement Result in Q#</th>\n </tr>\n <tr>\n <td rowspan=\"2\">$X$</td>\n <td rowspan=\"2\">$\\begin{bmatrix} 0 & 1 \\\\ 1 & 0 \\end{bmatrix}$</td>\n <td>+1</td>\n <td>$\\ket{+}$</td>\n <td>Zero</td>\n </tr><tr>\n <td>-1</td>\n <td>$\\ket{-}$</td>\n <td>One</td>\n </tr>\n <tr>\n <td rowspan=\"2\">$Y$</td>\n <td rowspan=\"2\">$\\begin{bmatrix} 0 & -i \\\\ i & 0 \\end{bmatrix}$</td>\n <td>+1</td>\n <td>$\\ket{i}$</td>\n <td>Zero</td>\n </tr><tr>\n <td>-1</td>\n <td>$\\ket{-i}$</td>\n <td>One</td>\n </tr>\n <tr>\n <td rowspan=\"2\">$Z$</td>\n <td rowspan=\"2\">$\\begin{bmatrix} 1 & 0 \\\\ 0 & -1 \\end{bmatrix}$</td>\n <td>+1</td>\n <td>$\\ket{0}$</td>\n <td>Zero</td>\n </tr><tr>\n <td>-1</td>\n <td>$\\ket{1}$</td>\n <td>One</td>\n </tr>\n</table>\n\n<p>In general, any measurement on a single qubit which results in two outcomes corresponds to the Hermitian operator $U Z U^\\dagger$, for some $2\\times 2$ unitary matrix $U$.</p>\n<p>Joint measurements are a generalization of this principle for multi-qubit matrices.</p>\n<h2>Parity Measurements</h2>\n<p>The simplest joint measurement is a parity measurement. A parity measurement treats computational basis vectors differently depending on whether the number of 1's in the basis vector is even or odd.</p>\n<p>For example, the operator $Z\\otimes Z$, or $ZZ$ in short, is the parity measurement operator for a two-qubit system. The eigenvalues $1$ and $-1$ correspond to the subspaces spanned by basis vectors ${ |00\\rangle, |11\\rangle }$ and ${ |01\\rangle, |10\\rangle }$, respectively. That is, when a $ZZ$ measurement results in a <code>Zero</code> (i.e. the eigenvalue $+1$), the post-measurement state is a superposition of only those computational basis vectors which have an even number of $1$'s. On the other hand, a result of <code>One</code> corresponds to a post-measurement state with only odd parity computational basis vectors.</p>\n<p>Let's see what happens to various two-qubit states after the parity measurement. The $Z \\otimes Z$ matrix for two qubits is:</p>\n<p>$$\nZ \\otimes Z =\n\\begin{bmatrix}\n 1 & 0 & 0 & 0 \\\\ \n 0 & -1 & 0 & 0 \\\\ \n 0 & 0 & -1 & 0 \\\\ \n 0 & 0 & 0 & 1 \\\\ \n\\end{bmatrix}\n$$</p>\n<p>When this transformation is applied to a basis state $|00\\rangle$, we get</p>\n<p>$$\n\\begin{bmatrix}\n 1 & 0 & 0 & 0 \\\\ \n 0 & -1 & 0 & 0 \\\\ \n 0 & 0 & -1 & 0 \\\\ \n 0 & 0 & 0 & 1 \\\\ \n\\end{bmatrix}\n\\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ 0 \\end{bmatrix} =\n\\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ 0 \\end{bmatrix}\n$$</p>\n<p>Comparing this to the characteristic equation for eigenvectors of $Z \\otimes Z$ given by\n$ Z \\otimes Z |\\psi\\rangle = \\lambda |\\psi\\rangle$,\nit is easy to see that $|00\\rangle$ belongs to the $+1$ eigenspace, hence the $Z \\otimes Z$ measurement will return <code>Zero</code> and leave the state unchanged.</p>\n<p>Similarly, it can easily be verified that $|11\\rangle$ also belongs to $+1$ eigenspace, while $|01\\rangle$ and $|10\\rangle$ belong to the $-1$ eigenspace.</p>\n<p>Now, what happens if we apply a $Z \\otimes Z$ measurement to a superposition state $\\alpha |00\\rangle + \\beta |11\\rangle$? We can see that</p>\n<p>$$\n\\begin{bmatrix}\n 1 & 0 & 0 & 0 \\\\ \n 0 & -1 & 0 & 0 \\\\ \n 0 & 0 & -1 & 0 \\\\ \n 0 & 0 & 0 & 1 \\\\ \n\\end{bmatrix}\n\\begin{bmatrix} \\alpha \\\\ 0 \\\\ 0 \\\\ \\beta \\end{bmatrix} =\n\\begin{bmatrix} \\alpha \\\\ 0 \\\\ 0 \\\\ \\beta \\end{bmatrix}\n$$</p>\n<p>So this state also belongs to the $+1$ eigenspace, and measuring it will return <code>Zero</code> and leave the state unchanged. Similarly, we can verify that an $\\alpha |01\\rangle + \\beta |10\\rangle$ state belongs to the $-1$ eigenspace, and measuring it will return <code>One</code> without changing the state.</p>\n<p>Similarly, a parity measurement on a higher number of qubits can be implemented using a $Z \\otimes \\dotsc \\otimes Z$ measurement.</p>\n",
|
|
1612
|
-
"asMarkdown": "\nJoint measurements, also known as Pauli measurements, are a generalization of 2-outcome measurements to multiple qubits and other bases. In Q#, joint measurements in Pauli bases are implemented using the <a href=\"https://docs.microsoft.com/en-us/qsharp/api/qsharp/microsoft.quantum.intrinsic.measure\" target=\"_blank\">`Measure`</a> operation. Let's review single-qubit measurements in a different light before discussing joint measurements.\n\n## Single-Qubit Pauli Measurement\nFor single-qubit systems, any measurement corresponding to an orthogonal basis can be associated with a Hermitian matrix with eigenvalues $\\pm 1$. The possible measurement outcomes (represented as `Result` in Q#) are the eigenvalues of the Hermitian matrix, and the corresponding projection matrices for the measurement are the projection operators onto the *eigenspaces* corresponding to the eigenvalues.\n\nFor example, consider the computational basis measurement, which can result in outcomes `Zero` or `One` corresponding to states $\\ket 0$ and $\\ket 1$. This measurement is associated with the Pauli Z operator, which is given by\n\n$$Z = \\begin{pmatrix} 1 & 0 \\\\\\ 0 & -1\\end{pmatrix} = \\ket{0}\\bra{0} - \\ket{1}\\bra{1}$$\n\nThe $Z$ operator has two eigenvalues, $1$ and $-1$, with corresponding eigenvectors $\\ket{0}$ and $\\ket{1}$. A $Z$-measurement is then a measurement in the $\\\\{\\ket{0},\\ket{1}\\\\}$ basis, with the measurement outcomes being $1$ and $-1$ respectively. In Q#, by convention, an eigenvalue of $1$ corresponds to a `Result` of `Zero`, while an eigenvalue of $-1$ corresponds to a `Result` of `One`.\n\nSimilarly, one can implement measurements corresponding to the Pauli X and Y operators. We summarize the various properties below:\n<table>\n <tr>\n <th>Pauli Operator</th>\n <th>Matrix</th>\n <th>Eigenvalue</th>\n <th>Eigenvector/post-measurement state</th>\n <th>Measurement Result in Q#</th>\n </tr>\n <tr>\n <td rowspan=\"2\">$X$</td>\n <td rowspan=\"2\">$\\begin{bmatrix} 0 & 1 \\\\ 1 & 0 \\end{bmatrix}$</td>\n <td>+1</td>\n <td>$\\ket{+}$</td>\n <td>Zero</td>\n </tr><tr>\n <td>-1</td>\n <td>$\\ket{-}$</td>\n <td>One</td>\n </tr>\n <tr>\n <td rowspan=\"2\">$Y$</td>\n <td rowspan=\"2\">$\\begin{bmatrix} 0 & -i \\\\ i & 0 \\end{bmatrix}$</td>\n <td>+1</td>\n <td>$\\ket{i}$</td>\n <td>Zero</td>\n </tr><tr>\n <td>-1</td>\n <td>$\\ket{-i}$</td>\n <td>One</td>\n </tr>\n <tr>\n <td rowspan=\"2\">$Z$</td>\n <td rowspan=\"2\">$\\begin{bmatrix} 1 & 0 \\\\ 0 & -1 \\end{bmatrix}$</td>\n <td>+1</td>\n <td>$\\ket{0}$</td>\n <td>Zero</td>\n </tr><tr>\n <td>-1</td>\n <td>$\\ket{1}$</td>\n <td>One</td>\n </tr>\n</table>\n\nIn general, any measurement on a single qubit which results in two outcomes corresponds to the Hermitian operator $U Z U^\\dagger$, for some $2\\times 2$ unitary matrix $U$.\n\nJoint measurements are a generalization of this principle for multi-qubit matrices.\n\n## Parity Measurements\nThe simplest joint measurement is a parity measurement. A parity measurement treats computational basis vectors differently depending on whether the number of 1's in the basis vector is even or odd.\n\nFor example, the operator $Z\\otimes Z$, or $ZZ$ in short, is the parity measurement operator for a two-qubit system. The eigenvalues $1$ and $-1$ correspond to the subspaces spanned by basis vectors $\\{ |00\\rangle, |11\\rangle \\}$ and $\\{ |01\\rangle, |10\\rangle \\}$, respectively. That is, when a $ZZ$ measurement results in a `Zero` (i.e. the eigenvalue $+1$), the post-measurement state is a superposition of only those computational basis vectors which have an even number of $1$'s. On the other hand, a result of `One` corresponds to a post-measurement state with only odd parity computational basis vectors.\n\nLet's see what happens to various two-qubit states after the parity measurement. The $Z \\otimes Z$ matrix for two qubits is:\n\n$$\nZ \\otimes Z =\n\\begin{bmatrix}\n 1 & 0 & 0 & 0 \\\\\\ \n 0 & -1 & 0 & 0 \\\\\\ \n 0 & 0 & -1 & 0 \\\\\\ \n 0 & 0 & 0 & 1 \\\\\\ \n\\end{bmatrix}\n$$\n\nWhen this transformation is applied to a basis state $|00\\rangle$, we get\n\n$$\n\\begin{bmatrix}\n 1 & 0 & 0 & 0 \\\\\\ \n 0 & -1 & 0 & 0 \\\\\\ \n 0 & 0 & -1 & 0 \\\\\\ \n 0 & 0 & 0 & 1 \\\\\\ \n\\end{bmatrix}\n\\begin{bmatrix} 1 \\\\\\ 0 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix} =\n\\begin{bmatrix} 1 \\\\\\ 0 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix}\n$$\n\nComparing this to the characteristic equation for eigenvectors of $Z \\otimes Z$ given by\n$ Z \\otimes Z |\\psi\\rangle = \\lambda |\\psi\\rangle$,\nit is easy to see that $|00\\rangle$ belongs to the $+1$ eigenspace, hence the $Z \\otimes Z$ measurement will return `Zero` and leave the state unchanged.\n\nSimilarly, it can easily be verified that $|11\\rangle$ also belongs to $+1$ eigenspace, while $|01\\rangle$ and $|10\\rangle$ belong to the $-1$ eigenspace.\n\nNow, what happens if we apply a $Z \\otimes Z$ measurement to a superposition state $\\alpha |00\\rangle + \\beta |11\\rangle$? We can see that\n\n$$\n\\begin{bmatrix}\n 1 & 0 & 0 & 0 \\\\\\ \n 0 & -1 & 0 & 0 \\\\\\ \n 0 & 0 & -1 & 0 \\\\\\ \n 0 & 0 & 0 & 1 \\\\\\ \n\\end{bmatrix}\n\\begin{bmatrix} \\alpha \\\\\\ 0 \\\\\\ 0 \\\\\\ \\beta \\end{bmatrix} =\n\\begin{bmatrix} \\alpha \\\\\\ 0 \\\\\\ 0 \\\\\\ \\beta \\end{bmatrix}\n$$\n\nSo this state also belongs to the $+1$ eigenspace, and measuring it will return `Zero` and leave the state unchanged. Similarly, we can verify that an $\\alpha |01\\rangle + \\beta |10\\rangle$ state belongs to the $-1$ eigenspace, and measuring it will return `One` without changing the state.\n\nSimilarly, a parity measurement on a higher number of qubits can be implemented using a $Z \\otimes \\dotsc \\otimes Z$ measurement."
|
|
1612
|
+
"asMarkdown": "\nJoint measurements, also known as Pauli measurements, are a generalization of 2-outcome measurements to multiple qubits and other bases. In Q#, joint measurements in Pauli bases are implemented using the <a href=\"https://docs.microsoft.com/en-us/qsharp/api/qsharp/microsoft.quantum.intrinsic.measure\" target=\"_blank\">`Measure`</a> operation. Let's review single-qubit measurements in a different light before discussing joint measurements.\n\n## Single-Qubit Pauli Measurement\n\nFor single-qubit systems, any measurement corresponding to an orthogonal basis can be associated with a Hermitian matrix with eigenvalues $\\pm 1$. The possible measurement outcomes (represented as `Result` in Q#) are the eigenvalues of the Hermitian matrix, and the corresponding projection matrices for the measurement are the projection operators onto the *eigenspaces* corresponding to the eigenvalues.\n\nFor example, consider the computational basis measurement, which can result in outcomes `Zero` or `One` corresponding to states $\\ket 0$ and $\\ket 1$. This measurement is associated with the Pauli Z operator, which is given by\n\n$$Z = \\begin{pmatrix} 1 & 0 \\\\\\ 0 & -1\\end{pmatrix} = \\ket{0}\\bra{0} - \\ket{1}\\bra{1}$$\n\nThe $Z$ operator has two eigenvalues, $1$ and $-1$, with corresponding eigenvectors $\\ket{0}$ and $\\ket{1}$. A $Z$-measurement is then a measurement in the $\\\\{\\ket{0},\\ket{1}\\\\}$ basis, with the measurement outcomes being $1$ and $-1$ respectively. In Q#, by convention, an eigenvalue of $1$ corresponds to a `Result` of `Zero`, while an eigenvalue of $-1$ corresponds to a `Result` of `One`.\n\nSimilarly, one can implement measurements corresponding to the Pauli X and Y operators. We summarize the various properties below:\n<table>\n <tr>\n <th>Pauli Operator</th>\n <th>Matrix</th>\n <th>Eigenvalue</th>\n <th>Eigenvector/post-measurement state</th>\n <th>Measurement Result in Q#</th>\n </tr>\n <tr>\n <td rowspan=\"2\">$X$</td>\n <td rowspan=\"2\">$\\begin{bmatrix} 0 & 1 \\\\ 1 & 0 \\end{bmatrix}$</td>\n <td>+1</td>\n <td>$\\ket{+}$</td>\n <td>Zero</td>\n </tr><tr>\n <td>-1</td>\n <td>$\\ket{-}$</td>\n <td>One</td>\n </tr>\n <tr>\n <td rowspan=\"2\">$Y$</td>\n <td rowspan=\"2\">$\\begin{bmatrix} 0 & -i \\\\ i & 0 \\end{bmatrix}$</td>\n <td>+1</td>\n <td>$\\ket{i}$</td>\n <td>Zero</td>\n </tr><tr>\n <td>-1</td>\n <td>$\\ket{-i}$</td>\n <td>One</td>\n </tr>\n <tr>\n <td rowspan=\"2\">$Z$</td>\n <td rowspan=\"2\">$\\begin{bmatrix} 1 & 0 \\\\ 0 & -1 \\end{bmatrix}$</td>\n <td>+1</td>\n <td>$\\ket{0}$</td>\n <td>Zero</td>\n </tr><tr>\n <td>-1</td>\n <td>$\\ket{1}$</td>\n <td>One</td>\n </tr>\n</table>\n\nIn general, any measurement on a single qubit which results in two outcomes corresponds to the Hermitian operator $U Z U^\\dagger$, for some $2\\times 2$ unitary matrix $U$.\n\nJoint measurements are a generalization of this principle for multi-qubit matrices.\n\n## Parity Measurements\n\nThe simplest joint measurement is a parity measurement. A parity measurement treats computational basis vectors differently depending on whether the number of 1's in the basis vector is even or odd.\n\nFor example, the operator $Z\\otimes Z$, or $ZZ$ in short, is the parity measurement operator for a two-qubit system. The eigenvalues $1$ and $-1$ correspond to the subspaces spanned by basis vectors $\\{ |00\\rangle, |11\\rangle \\}$ and $\\{ |01\\rangle, |10\\rangle \\}$, respectively. That is, when a $ZZ$ measurement results in a `Zero` (i.e. the eigenvalue $+1$), the post-measurement state is a superposition of only those computational basis vectors which have an even number of $1$'s. On the other hand, a result of `One` corresponds to a post-measurement state with only odd parity computational basis vectors.\n\nLet's see what happens to various two-qubit states after the parity measurement. The $Z \\otimes Z$ matrix for two qubits is:\n\n$$\nZ \\otimes Z =\n\\begin{bmatrix}\n 1 & 0 & 0 & 0 \\\\\\ \n 0 & -1 & 0 & 0 \\\\\\ \n 0 & 0 & -1 & 0 \\\\\\ \n 0 & 0 & 0 & 1 \\\\\\ \n\\end{bmatrix}\n$$\n\nWhen this transformation is applied to a basis state $|00\\rangle$, we get\n\n$$\n\\begin{bmatrix}\n 1 & 0 & 0 & 0 \\\\\\ \n 0 & -1 & 0 & 0 \\\\\\ \n 0 & 0 & -1 & 0 \\\\\\ \n 0 & 0 & 0 & 1 \\\\\\ \n\\end{bmatrix}\n\\begin{bmatrix} 1 \\\\\\ 0 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix} =\n\\begin{bmatrix} 1 \\\\\\ 0 \\\\\\ 0 \\\\\\ 0 \\end{bmatrix}\n$$\n\nComparing this to the characteristic equation for eigenvectors of $Z \\otimes Z$ given by\n$ Z \\otimes Z |\\psi\\rangle = \\lambda |\\psi\\rangle$,\nit is easy to see that $|00\\rangle$ belongs to the $+1$ eigenspace, hence the $Z \\otimes Z$ measurement will return `Zero` and leave the state unchanged.\n\nSimilarly, it can easily be verified that $|11\\rangle$ also belongs to $+1$ eigenspace, while $|01\\rangle$ and $|10\\rangle$ belong to the $-1$ eigenspace.\n\nNow, what happens if we apply a $Z \\otimes Z$ measurement to a superposition state $\\alpha |00\\rangle + \\beta |11\\rangle$? We can see that\n\n$$\n\\begin{bmatrix}\n 1 & 0 & 0 & 0 \\\\\\ \n 0 & -1 & 0 & 0 \\\\\\ \n 0 & 0 & -1 & 0 \\\\\\ \n 0 & 0 & 0 & 1 \\\\\\ \n\\end{bmatrix}\n\\begin{bmatrix} \\alpha \\\\\\ 0 \\\\\\ 0 \\\\\\ \\beta \\end{bmatrix} =\n\\begin{bmatrix} \\alpha \\\\\\ 0 \\\\\\ 0 \\\\\\ \\beta \\end{bmatrix}\n$$\n\nSo this state also belongs to the $+1$ eigenspace, and measuring it will return `Zero` and leave the state unchanged. Similarly, we can verify that an $\\alpha |01\\rangle + \\beta |10\\rangle$ state belongs to the $-1$ eigenspace, and measuring it will return `One` without changing the state.\n\nSimilarly, a parity measurement on a higher number of qubits can be implemented using a $Z \\otimes \\dotsc \\otimes Z$ measurement."
|
|
1613
1613
|
}
|
|
1614
1614
|
]
|
|
1615
1615
|
},
|
|
@@ -1623,8 +1623,8 @@ export default {
|
|
|
1623
1623
|
"asMarkdown": "**Inputs**: Two qubits stored in an array which are guaranteed to be either in a superposition of the states $|00\\rangle$ and $|11\\rangle$ or in a superposition of states $|01\\rangle$ and $|10\\rangle$.\n\n**Output**: 0 if qubits were in the first superposition, 1 if they were in the second superposition. \n*The state of the qubits at the end of the operation should be the same as the starting state.*\n"
|
|
1624
1624
|
},
|
|
1625
1625
|
"sourceIds": [
|
|
1626
|
-
"
|
|
1627
|
-
"
|
|
1626
|
+
"multi_qubit_measurements__joint_measurements__Verification.qs",
|
|
1627
|
+
"multi_qubit_measurements__Common.qs",
|
|
1628
1628
|
"KatasLibrary.qs"
|
|
1629
1629
|
],
|
|
1630
1630
|
"placeholderCode": "namespace Kata {\n operation ParityMeasurement(qs : Qubit[]) : Int {\n // Implement your solution here...\n\n return -1;\n }\n\n}\n",
|
|
@@ -1651,8 +1651,8 @@ export default {
|
|
|
1651
1651
|
"items": [
|
|
1652
1652
|
{
|
|
1653
1653
|
"type": "text-content",
|
|
1654
|
-
"asHtml": "<p>Joint measurement is a generalization of the measurement in the computational basis.\nPauli measurements can also be generalized to a larger number of qubits. A multi-qubit Pauli measurement corresponds to an operator $M_1 \\otimes \\dotsc \\otimes M_n$, with each $M_i$ being from the set of gates ${X,Y,Z,I}$. If at least one of the operators is not the identity matrix, then the measurement can result in two outcomes: a <code>Result</code> of <code>Zero</code> corresponding to eigenvalue $+1$ and a <code>Result</code> of <code>One</code> corresponding to the eigenvalue $-1$. The corresponding projection operators are the projections onto the corresponding eigenspaces.</p>\n<p>For example, a Pauli/joint measurement corresponding to the $X\\otimes Z$ operator can be characterized as follows:</p>\n<table>\n <tr>\n <th>Eigenvalue</th>\n <th>Measurement Result in Q#</th>\n <th>Eigenbasis</th>\n <th>Measurement Projector</th>\n </tr>\n <tr>\n <td>$+1$</td>\n <td>Zero</td>\n <td>$\\{ \\ket{0,+}, \\ket{1,-} \\}$</td>\n <td>$P_{+1} = \\ket{0,+}\\bra{0,+} + \\ket{1,-} \\bra{1,-}$</td>\n </tr>\n <tr>\n <td>$-1$</td>\n <td>One</td>\n <td>$\\{ \\ket{0,-}, \\ket{1,+} \\}$</td>\n <td>$P_{-1} = \\ket{0,-}\\bra{0,-} + \\ket{1,+} \\bra{1,+}$</td>\n </tr>\n </table>\n\n<p> The rules for measurements are then the same as those outlined in the partial measurements section, with the projection operators in the table.</p>\n<h2>🔎 Analyze</h2>\n<p><strong>Parity measurement in different basis</strong></p>\n<p>Consider a system which is in a state $\\alpha |00\\rangle + \\beta |01\\rangle + \\beta |10\\rangle + \\alpha |11\\rangle$.</p>\n<p>What are the possible outcomes and their associated probabilities, if a measurement in an $XX$ Pauli measurement is done?</p>\n<details>\n<summary><b>Solution</b></summary>\nThe first step towards identifying the outcomes and their probabilities for joint measurements is to identify the eigenvectors corresponding to eigenvalues $\\pm1$ of the Pauli operator. We note that since $X\\ket{\\pm}= \\pm\\ket{\\pm}$, we have\n\n<p>$$XX \\ket{++} = \\ket{++}$$\n$$XX \\ket{--} = \\ket{--}$$\n$$XX \\ket{+-} = -\\ket{+-}$$\n$$XX \\ket{-+} = -\\ket{-+}$$</p>\n<p>Thus, the $XX$ operator measures the parity in the Hadamard, or the $\\ket{\\pm}$ basis. That is, it distinguishes basis states with an even number of $+$'s from basis states which have an odd number of $+$'s.</p>\n<p>The projector corresponding to a result of <code>Zero</code> is given by $P_{+1} = \\ket{++}\\bra{++} + \\ket{--}\\bra{--}$, while the projector corresponding to a result of <code>One</code> is given by $P_{-1} = \\ket{+-}\\bra{+-} + \\ket{-+}\\bra{-+}$. Then, we note that $P_{+1}$ annihilates states with odd parity, while leaving states with even parity unaffected. That is, for any values of the constants
|
|
1655
|
-
"asMarkdown": "\nJoint measurement is a generalization of the measurement in the computational basis.\nPauli measurements can also be generalized to a larger number of qubits. A multi-qubit Pauli measurement corresponds to an operator $M_1 \\otimes \\dotsc \\otimes M_n$, with each $M_i$ being from the set of gates $\\{X,Y,Z,I\\}$. If at least one of the operators is not the identity matrix, then the measurement can result in two outcomes: a `Result` of `Zero` corresponding to eigenvalue $+1$ and a `Result` of `One` corresponding to the eigenvalue $-1$. The corresponding projection operators are the projections onto the corresponding eigenspaces.\n\nFor example, a Pauli/joint measurement corresponding to the $X\\otimes Z$ operator can be characterized as follows:\n<table>\n <tr>\n <th>Eigenvalue</th>\n <th>Measurement Result in Q#</th>\n <th>Eigenbasis</th>\n <th>Measurement Projector</th>\n </tr>\n <tr>\n <td>$+1$</td>\n <td>Zero</td>\n <td>$\\{ \\ket{0,+}, \\ket{1,-} \\}$</td>\n <td>$P_{+1} = \\ket{0,+}\\bra{0,+} + \\ket{1,-} \\bra{1,-}$</td>\n </tr>\n <tr>\n <td>$-1$</td>\n <td>One</td>\n <td>$\\{ \\ket{0,-}, \\ket{1,+} \\}$</td>\n <td>$P_{-1} = \\ket{0,-}\\bra{0,-} + \\ket{1,+} \\bra{1,+}$</td>\n </tr>\n </table>\n\n The rules for measurements are then the same as those outlined in the partial measurements section, with the projection operators in the table.\n\n## 🔎 Analyze\n\n**Parity measurement in different basis**\n\nConsider a system which is in a state $\\alpha |00\\rangle + \\beta |01\\rangle + \\beta |10\\rangle + \\alpha |11\\rangle$.\n\nWhat are the possible outcomes and their associated probabilities, if a measurement in an $XX$ Pauli measurement is done?\n\n<details>\n<summary><b>Solution</b></summary>\nThe first step towards identifying the outcomes and their probabilities for joint measurements is to identify the eigenvectors corresponding to eigenvalues $\\pm1$ of the Pauli operator. We note that since $X\\ket{\\pm}= \\pm\\ket{\\pm}$, we have\n\n$$XX \\ket{++} = \\ket{++}$$\n$$XX \\ket{--} = \\ket{--}$$\n$$XX \\ket{+-} = -\\ket{+-}$$\n$$XX \\ket{-+} = -\\ket{-+}$$\n\nThus, the $XX$ operator measures the parity in the Hadamard, or the $\\ket{\\pm}$ basis. That is, it distinguishes basis states with an even number of $+$'s from basis states which have an odd number of $+$'s.\n\nThe projector corresponding to a result of `Zero` is given by $P_{+1} = \\ket{++}\\bra{++} + \\ket{--}\\bra{--}$, while the projector corresponding to a result of `One` is given by $P_{-1} = \\ket{+-}\\bra{+-} + \\ket{-+}\\bra{-+}$. Then, we note that $P_{+1}$ annihilates states with odd parity, while leaving states with even parity unaffected. That is, for any values of the constants
|
|
1654
|
+
"asHtml": "<p>Joint measurement is a generalization of the measurement in the computational basis.\nPauli measurements can also be generalized to a larger number of qubits. A multi-qubit Pauli measurement corresponds to an operator $M_1 \\otimes \\dotsc \\otimes M_n$, with each $M_i$ being from the set of gates ${X,Y,Z,I}$. If at least one of the operators is not the identity matrix, then the measurement can result in two outcomes: a <code>Result</code> of <code>Zero</code> corresponding to eigenvalue $+1$ and a <code>Result</code> of <code>One</code> corresponding to the eigenvalue $-1$. The corresponding projection operators are the projections onto the corresponding eigenspaces.</p>\n<p>For example, a Pauli/joint measurement corresponding to the $X\\otimes Z$ operator can be characterized as follows:</p>\n<table>\n <tr>\n <th>Eigenvalue</th>\n <th>Measurement Result in Q#</th>\n <th>Eigenbasis</th>\n <th>Measurement Projector</th>\n </tr>\n <tr>\n <td>$+1$</td>\n <td>Zero</td>\n <td>$\\{ \\ket{0,+}, \\ket{1,-} \\}$</td>\n <td>$P_{+1} = \\ket{0,+}\\bra{0,+} + \\ket{1,-} \\bra{1,-}$</td>\n </tr>\n <tr>\n <td>$-1$</td>\n <td>One</td>\n <td>$\\{ \\ket{0,-}, \\ket{1,+} \\}$</td>\n <td>$P_{-1} = \\ket{0,-}\\bra{0,-} + \\ket{1,+} \\bra{1,+}$</td>\n </tr>\n </table>\n\n<p> The rules for measurements are then the same as those outlined in the partial measurements section, with the projection operators in the table.</p>\n<h2>🔎 Analyze</h2>\n<p><strong>Parity measurement in different basis</strong></p>\n<p>Consider a system which is in a state $\\alpha |00\\rangle + \\beta |01\\rangle + \\beta |10\\rangle + \\alpha |11\\rangle$.</p>\n<p>What are the possible outcomes and their associated probabilities, if a measurement in an $XX$ Pauli measurement is done?</p>\n<details>\n<summary><b>Solution</b></summary>\nThe first step towards identifying the outcomes and their probabilities for joint measurements is to identify the eigenvectors corresponding to eigenvalues $\\pm1$ of the Pauli operator. We note that since $X\\ket{\\pm}= \\pm\\ket{\\pm}$, we have\n\n<p>$$XX \\ket{++} = \\ket{++}$$\n$$XX \\ket{--} = \\ket{--}$$\n$$XX \\ket{+-} = -\\ket{+-}$$\n$$XX \\ket{-+} = -\\ket{-+}$$</p>\n<p>Thus, the $XX$ operator measures the parity in the Hadamard, or the $\\ket{\\pm}$ basis. That is, it distinguishes basis states with an even number of $+$'s from basis states which have an odd number of $+$'s.</p>\n<p>The projector corresponding to a result of <code>Zero</code> is given by $P_{+1} = \\ket{++}\\bra{++} + \\ket{--}\\bra{--}$, while the projector corresponding to a result of <code>One</code> is given by $P_{-1} = \\ket{+-}\\bra{+-} + \\ket{-+}\\bra{-+}$. Then, we note that $P_{+1}$ annihilates states with odd parity, while leaving states with even parity unaffected. That is, for any values of the constants\n$$P_{+1} ( \\gamma \\ket{++} + \\delta \\ket{--} ) =$$\n$$( \\gamma \\ket{++} + \\delta \\ket{--} )P_{+1} ( \\mu \\ket{-+} + \\nu \\ket{+-} ) = 0$$</p>\n<p>Similarly, $P_{-1}$ annihilates states with even parity, while leaving states with odd parity unaffected.</p>\n<p>Now we express the given state in the Hadamard basis. We note that it is possible to go from the computational basis to the Hadamard basis using the following relations:\n$$\\ket{0} = \\frac{1}{\\sqrt{2}} \\left( \\ket{+} + \\ket{-} \\right)$$\n$$\\ket{1} = \\frac{1}{\\sqrt{2}} \\left( \\ket{+} - \\ket{-} \\right)$$</p>\n<p>Using these, we obtain\n$$ \\alpha |00\\rangle + \\beta |01\\rangle + \\beta |10\\rangle + \\alpha |11\\rangle = (\\alpha + \\beta) |++\\rangle + (\\alpha - \\beta) |--\\rangle$$\nThus, this state has an even parity in the Hadamard basis. It follows that an $XX$ Pauli measurement will result in the outcome <code>Zero</code> with probability 1, leaving the state unchanged after the measurement.</p>\n</details>",
|
|
1655
|
+
"asMarkdown": "\nJoint measurement is a generalization of the measurement in the computational basis.\nPauli measurements can also be generalized to a larger number of qubits. A multi-qubit Pauli measurement corresponds to an operator $M_1 \\otimes \\dotsc \\otimes M_n$, with each $M_i$ being from the set of gates $\\{X,Y,Z,I\\}$. If at least one of the operators is not the identity matrix, then the measurement can result in two outcomes: a `Result` of `Zero` corresponding to eigenvalue $+1$ and a `Result` of `One` corresponding to the eigenvalue $-1$. The corresponding projection operators are the projections onto the corresponding eigenspaces.\n\nFor example, a Pauli/joint measurement corresponding to the $X\\otimes Z$ operator can be characterized as follows:\n<table>\n <tr>\n <th>Eigenvalue</th>\n <th>Measurement Result in Q#</th>\n <th>Eigenbasis</th>\n <th>Measurement Projector</th>\n </tr>\n <tr>\n <td>$+1$</td>\n <td>Zero</td>\n <td>$\\{ \\ket{0,+}, \\ket{1,-} \\}$</td>\n <td>$P_{+1} = \\ket{0,+}\\bra{0,+} + \\ket{1,-} \\bra{1,-}$</td>\n </tr>\n <tr>\n <td>$-1$</td>\n <td>One</td>\n <td>$\\{ \\ket{0,-}, \\ket{1,+} \\}$</td>\n <td>$P_{-1} = \\ket{0,-}\\bra{0,-} + \\ket{1,+} \\bra{1,+}$</td>\n </tr>\n </table>\n\n The rules for measurements are then the same as those outlined in the partial measurements section, with the projection operators in the table.\n\n## 🔎 Analyze\n\n**Parity measurement in different basis**\n\nConsider a system which is in a state $\\alpha |00\\rangle + \\beta |01\\rangle + \\beta |10\\rangle + \\alpha |11\\rangle$.\n\nWhat are the possible outcomes and their associated probabilities, if a measurement in an $XX$ Pauli measurement is done?\n\n<details>\n<summary><b>Solution</b></summary>\nThe first step towards identifying the outcomes and their probabilities for joint measurements is to identify the eigenvectors corresponding to eigenvalues $\\pm1$ of the Pauli operator. We note that since $X\\ket{\\pm}= \\pm\\ket{\\pm}$, we have\n\n$$XX \\ket{++} = \\ket{++}$$\n$$XX \\ket{--} = \\ket{--}$$\n$$XX \\ket{+-} = -\\ket{+-}$$\n$$XX \\ket{-+} = -\\ket{-+}$$\n\nThus, the $XX$ operator measures the parity in the Hadamard, or the $\\ket{\\pm}$ basis. That is, it distinguishes basis states with an even number of $+$'s from basis states which have an odd number of $+$'s.\n\nThe projector corresponding to a result of `Zero` is given by $P_{+1} = \\ket{++}\\bra{++} + \\ket{--}\\bra{--}$, while the projector corresponding to a result of `One` is given by $P_{-1} = \\ket{+-}\\bra{+-} + \\ket{-+}\\bra{-+}$. Then, we note that $P_{+1}$ annihilates states with odd parity, while leaving states with even parity unaffected. That is, for any values of the constants\n$$P_{+1} ( \\gamma \\ket{++} + \\delta \\ket{--} ) =$$\n$$( \\gamma \\ket{++} + \\delta \\ket{--} )P_{+1} ( \\mu \\ket{-+} + \\nu \\ket{+-} ) = 0$$\n\nSimilarly, $P_{-1}$ annihilates states with even parity, while leaving states with odd parity unaffected.\n\nNow we express the given state in the Hadamard basis. We note that it is possible to go from the computational basis to the Hadamard basis using the following relations:\n$$\\ket{0} = \\frac{1}{\\sqrt{2}} \\left( \\ket{+} + \\ket{-} \\right)$$\n$$\\ket{1} = \\frac{1}{\\sqrt{2}} \\left( \\ket{+} - \\ket{-} \\right)$$\n\nUsing these, we obtain\n$$ \\alpha |00\\rangle + \\beta |01\\rangle + \\beta |10\\rangle + \\alpha |11\\rangle = (\\alpha + \\beta) |++\\rangle + (\\alpha - \\beta) |--\\rangle$$\nThus, this state has an even parity in the Hadamard basis. It follows that an $XX$ Pauli measurement will result in the outcome `Zero` with probability 1, leaving the state unchanged after the measurement.\n</details>"
|
|
1656
1656
|
}
|
|
1657
1657
|
]
|
|
1658
1658
|
},
|
|
@@ -1664,7 +1664,7 @@ export default {
|
|
|
1664
1664
|
{
|
|
1665
1665
|
"type": "text-content",
|
|
1666
1666
|
"asHtml": "<p>Congratulations! In this kata you learned how to apply measurements on multi-qubit systems. Here are a few key concepts to keep in mind:</p>\n<ul>\n<li>Full measurements: you measure all the qubits simultaneously in an orthogonal basis ($2^n$ possible outcomes).</li>\n<li>Partial measurements: you measure $m$ qubits out of $n$, for $m< n$ ($2^m$ possible outcomes).</li>\n<li>Joint measurement: Pauli measurement of all $n$ qubits ($2$ possible outcomes).</li>\n</ul>\n<p>Next, you will implement a quantum algorithm to generate random numbers in "Quantum Random Number Generation" kata.</p>\n",
|
|
1667
|
-
"asMarkdown": "\nCongratulations! In this kata you learned how to apply measurements on multi-qubit systems. Here are a few key concepts to keep in mind:\n
|
|
1667
|
+
"asMarkdown": "\nCongratulations! In this kata you learned how to apply measurements on multi-qubit systems. Here are a few key concepts to keep in mind:\n\n- Full measurements: you measure all the qubits simultaneously in an orthogonal basis ($2^n$ possible outcomes).\n- Partial measurements: you measure $m$ qubits out of $n$, for $m< n$ ($2^m$ possible outcomes).\n- Joint measurement: Pauli measurement of all $n$ qubits ($2$ possible outcomes).\n\nNext, you will implement a quantum algorithm to generate random numbers in \"Quantum Random Number Generation\" kata."
|
|
1668
1668
|
}
|
|
1669
1669
|
]
|
|
1670
1670
|
}
|
|
@@ -1681,8 +1681,8 @@ export default {
|
|
|
1681
1681
|
"items": [
|
|
1682
1682
|
{
|
|
1683
1683
|
"type": "text-content",
|
|
1684
|
-
"asHtml": "<p>True random number generation is a notoriously difficult problem. Many "random" generators today are actually pseudo-random, using a starting seed to spawn seemingly-random numbers that are actually a repeatable function of that seed. Most true random number generators are based on measurements of some natural phenomenon, such as atmospheric noise or atomic decay. You can read more about it <a href=\"https://en.wikipedia.org/wiki/Random_number_generation\" target=\"_blank\">here</a
|
|
1685
|
-
"asMarkdown": "\nTrue random number generation is a notoriously difficult problem. Many \"random\" generators today are actually pseudo-random, using a starting seed to spawn seemingly-random numbers that are actually a repeatable function of that seed. Most true random number generators are based on measurements of some natural phenomenon, such as atmospheric noise or atomic decay. You can read more about it <a href=\"https://en.wikipedia.org/wiki/Random_number_generation\" target=\"_blank\">here</a
|
|
1684
|
+
"asHtml": "<p>True random number generation is a notoriously difficult problem. Many "random" generators today are actually pseudo-random, using a starting seed to spawn seemingly-random numbers that are actually a repeatable function of that seed. Most true random number generators are based on measurements of some natural phenomenon, such as atmospheric noise or atomic decay. You can read more about it <a href=\"https://en.wikipedia.org/wiki/Random_number_generation\" target=\"_blank\">here</a>.</p>\n<p>Quantum random number generators (QRNGs) are truly random. Of course, this only applies to the case when they run on a real quantum device, relying on the randomness of the quantum state collapse during measurement to produce the random numbers. When QRNGs run on a simulator, the source of randomness is the same as for other classical programs, so the generated numbers are pseudo-random.</p>\n<p>The quantum algorithm for random number generation is one of the simplest applications of quantum computing principles, requiring very few qubits to run.</p>\n<p><strong>This kata covers the following topics:</strong></p>\n<ul>\n<li>Quantum random number generation and the principles behind it</li>\n<li>Implementation of a variety of QRNGs with equal probability of any given number</li>\n<li>Implementation a single-bit QRNG with weighted probabilities of generated bits</li>\n</ul>\n<p><strong>What you should know to start working on this kata:</strong></p>\n<ul>\n<li>The concept of qubit and measurement</li>\n<li>Single-qubit gates</li>\n</ul>\n",
|
|
1685
|
+
"asMarkdown": "\nTrue random number generation is a notoriously difficult problem. Many \"random\" generators today are actually pseudo-random, using a starting seed to spawn seemingly-random numbers that are actually a repeatable function of that seed. Most true random number generators are based on measurements of some natural phenomenon, such as atmospheric noise or atomic decay. You can read more about it <a href=\"https://en.wikipedia.org/wiki/Random_number_generation\" target=\"_blank\">here</a>.\n\nQuantum random number generators (QRNGs) are truly random. Of course, this only applies to the case when they run on a real quantum device, relying on the randomness of the quantum state collapse during measurement to produce the random numbers. When QRNGs run on a simulator, the source of randomness is the same as for other classical programs, so the generated numbers are pseudo-random.\n\nThe quantum algorithm for random number generation is one of the simplest applications of quantum computing principles, requiring very few qubits to run.\n\n**This kata covers the following topics:**\n\n- Quantum random number generation and the principles behind it\n- Implementation of a variety of QRNGs with equal probability of any given number\n- Implementation a single-bit QRNG with weighted probabilities of generated bits\n\n**What you should know to start working on this kata:**\n\n- The concept of qubit and measurement\n- Single-qubit gates"
|
|
1686
1686
|
}
|
|
1687
1687
|
]
|
|
1688
1688
|
},
|
|
@@ -1693,8 +1693,8 @@ export default {
|
|
|
1693
1693
|
"items": [
|
|
1694
1694
|
{
|
|
1695
1695
|
"type": "text-content",
|
|
1696
|
-
"asHtml": "<p>Recall from the Qubit kata that a qubit state $|\\psi\\rangle$ is defined via the basis states $|0\\rangle$ and $|1\\rangle$ as $|\\psi\\rangle = \\begin{bmatrix} \\alpha
|
|
1697
|
-
"asMarkdown": "\nRecall from the Qubit kata that a qubit state $|\\psi\\rangle$ is defined via the basis states $|0\\rangle$ and $|1\\rangle$ as $|\\psi\\rangle = \\begin{bmatrix} \\alpha
|
|
1696
|
+
"asHtml": "<p>Recall from the Qubit kata that a qubit state $|\\psi\\rangle$ is defined via the basis states $|0\\rangle$ and $|1\\rangle$ as $|\\psi\\rangle = \\begin{bmatrix} \\alpha \\\\ \\beta \\end{bmatrix} = \\alpha|0\\rangle + \\beta|1\\rangle$, where $|\\alpha|^2 + |\\beta|^2 = 1$.</p>\n<p>We call $\\alpha$ and $\\beta$ the probability amplitudes of states $|0\\rangle$ and $|1\\rangle$, respectively. When $|\\psi\\rangle$ is measured in the $\\{|0\\rangle, |1\\rangle\\}$ basis (the computational basis), the probabilities of the outcomes are defined based on the state amplitudes: there is a $|\\alpha|^2$ probability that the measurement result will be $0$, and a $|\\beta|^2$ probability that the measurement result will be $1$.</p>\n<blockquote>\n<p>For example, a qubit in state $\\begin{bmatrix} \\frac{1}{\\sqrt{2}} \\\\ \\frac{1}{\\sqrt{2}} \\end{bmatrix}$ will yield measurement results $0$ or $1$ with equal probability, while a qubit in state $\\begin{bmatrix} \\frac{1}{2} \\\\ \\frac{\\sqrt3}{2} \\end{bmatrix}$ will yield measurement result $0$ only 25% of the time, and $1$ 75% of the time.</p>\n</blockquote>\n<p>This knowledge is sufficient to implement a simple random number generator!</p>\n<blockquote>\n<p>Remember that you can refer to the Single-Qubit Gates kata if you need a refresher on the various quantum gates and their usage in Q#.</p>\n</blockquote>\n",
|
|
1697
|
+
"asMarkdown": "\nRecall from the Qubit kata that a qubit state $|\\psi\\rangle$ is defined via the basis states $|0\\rangle$ and $|1\\rangle$ as $|\\psi\\rangle = \\begin{bmatrix} \\alpha \\\\\\ \\beta \\end{bmatrix} = \\alpha|0\\rangle + \\beta|1\\rangle$, where $|\\alpha|^2 + |\\beta|^2 = 1$.\n\nWe call $\\alpha$ and $\\beta$ the probability amplitudes of states $|0\\rangle$ and $|1\\rangle$, respectively. When $|\\psi\\rangle$ is measured in the $\\\\{|0\\rangle, |1\\rangle\\\\}$ basis (the computational basis), the probabilities of the outcomes are defined based on the state amplitudes: there is a $|\\alpha|^2$ probability that the measurement result will be $0$, and a $|\\beta|^2$ probability that the measurement result will be $1$.\n\n> For example, a qubit in state $\\begin{bmatrix} \\frac{1}{\\sqrt{2}} \\\\\\ \\frac{1}{\\sqrt{2}} \\end{bmatrix}$ will yield measurement results $0$ or $1$ with equal probability, while a qubit in state $\\begin{bmatrix} \\frac{1}{2} \\\\\\ \\frac{\\sqrt3}{2} \\end{bmatrix}$ will yield measurement result $0$ only 25% of the time, and $1$ 75% of the time.\n\nThis knowledge is sufficient to implement a simple random number generator!\n\n> Remember that you can refer to the Single-Qubit Gates kata if you need a refresher on the various quantum gates and their usage in Q#."
|
|
1698
1698
|
}
|
|
1699
1699
|
]
|
|
1700
1700
|
},
|
|
@@ -1708,9 +1708,9 @@ export default {
|
|
|
1708
1708
|
"asMarkdown": "**Input:** None.\n\n**Goal:** Generate a $0$ or $1$ with equal probability.\n<details>\n <summary><b>Need a hint?</b></summary>\n Use the allocated qubit, apply a quantum gate to it, measure it and use the result to return a $0$ or $1$.\n</details>\n\n**Stretch goal:** Can you find a different way to implement this operation?\n<details>\n <summary><b>Need a hint?</b></summary>\n What are the different quantum states that produce $0$ and $1$ measurement results with the same probability? How would measuring the qubit in a different basis change the result?\n</details>\n"
|
|
1709
1709
|
},
|
|
1710
1710
|
"sourceIds": [
|
|
1711
|
-
"
|
|
1711
|
+
"random_numbers__random_bit__Verification.qs",
|
|
1712
1712
|
"KatasLibrary.qs",
|
|
1713
|
-
"
|
|
1713
|
+
"random_numbers__Common.qs"
|
|
1714
1714
|
],
|
|
1715
1715
|
"placeholderCode": "namespace Kata {\n operation RandomBit() : Int {\n use q = Qubit();\n // Implement your solution here...\n\n return -1;\n }\n\n}\n",
|
|
1716
1716
|
"explainedSolution": {
|
|
@@ -1718,13 +1718,13 @@ export default {
|
|
|
1718
1718
|
"items": [
|
|
1719
1719
|
{
|
|
1720
1720
|
"type": "text-content",
|
|
1721
|
-
"asHtml": "<p>The state of single qubit can be represented as a two-dimensional column vector $\\begin{bmatrix} \\alpha
|
|
1722
|
-
"asMarkdown": "\nThe state of single qubit can be represented as a two-dimensional column vector $\\begin{bmatrix} \\alpha
|
|
1721
|
+
"asHtml": "<p>The state of single qubit can be represented as a two-dimensional column vector $\\begin{bmatrix} \\alpha \\\\ \\beta \\end{bmatrix}$, where $\\alpha$ and $\\beta$ are complex numbers that satisfy $|\\alpha|^2 + |\\beta|^2 = 1$. When we measure the qubit, we get either 0 with probability $|\\alpha|^2$ or 1 with probability $|\\beta|^2$. Essentially we can control probablity of measurement outcome by setting the right amplitudes of basis states. </p>\n<p>When we allocate the qubit in Q#, amplitudes $\\alpha$ and $\\beta$ are 1 and 0, respectively. Now our goal is set equal amplitudes for $\\alpha$ and $\\beta$ for absolute randomness. We can achieve that by simply applying Hadamard gate to the initial state $|0\\rangle$:</p>\n<p>$$\nH|0\\rangle =\n\\frac{1}{\\sqrt{2}}\n\\begin{bmatrix} 1 & 1 \\\\ 1 & -1 \\end{bmatrix}\n\\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} =\n\\frac{1}{\\sqrt{2}}\n\\begin{bmatrix} 1 \\cdot 1 + 1 \\cdot 0 \\\\ 1 \\cdot 1 + (-1) \\cdot 0 \\end{bmatrix} =\n\\frac{1}{\\sqrt{2}}\n\\begin{bmatrix} 1 \\\\ 1 \\end{bmatrix}\n$$</p>\n<p>Now, both 0 and 1 measurement outcomes occur with equal probablity of $|\\frac{1}{\\sqrt{2}}|^2 = \\frac{1}{2}$.</p>\n<blockquote>\n<p>Since the outcome probability is the square of the absolute value of amplitude, we will get the same distribution of outcomes by measuring the $|-\\rangle$ state, which we can prepare by applying the Hadamard gate to the basis state $|1\\rangle$. Try it out to achieve the stretch goal!</p>\n</blockquote>\n",
|
|
1722
|
+
"asMarkdown": "\nThe state of single qubit can be represented as a two-dimensional column vector $\\begin{bmatrix} \\alpha \\\\\\ \\beta \\end{bmatrix}$, where $\\alpha$ and $\\beta$ are complex numbers that satisfy $|\\alpha|^2 + |\\beta|^2 = 1$. When we measure the qubit, we get either 0 with probability $|\\alpha|^2$ or 1 with probability $|\\beta|^2$. Essentially we can control probablity of measurement outcome by setting the right amplitudes of basis states. \n\nWhen we allocate the qubit in Q#, amplitudes $\\alpha$ and $\\beta$ are 1 and 0, respectively. Now our goal is set equal amplitudes for $\\alpha$ and $\\beta$ for absolute randomness. We can achieve that by simply applying Hadamard gate to the initial state $|0\\rangle$:\n\n$$\nH|0\\rangle =\n\\frac{1}{\\sqrt{2}}\n\\begin{bmatrix} 1 & 1 \\\\\\ 1 & -1 \\end{bmatrix}\n\\begin{bmatrix} 1 \\\\\\ 0 \\end{bmatrix} =\n\\frac{1}{\\sqrt{2}}\n\\begin{bmatrix} 1 \\cdot 1 + 1 \\cdot 0 \\\\\\ 1 \\cdot 1 + (-1) \\cdot 0 \\end{bmatrix} =\n\\frac{1}{\\sqrt{2}}\n\\begin{bmatrix} 1 \\\\\\ 1 \\end{bmatrix}\n$$\n\nNow, both 0 and 1 measurement outcomes occur with equal probablity of $|\\frac{1}{\\sqrt{2}}|^2 = \\frac{1}{2}$.\n\n> Since the outcome probability is the square of the absolute value of amplitude, we will get the same distribution of outcomes by measuring the $|-\\rangle$ state, which we can prepare by applying the Hadamard gate to the basis state $|1\\rangle$. Try it out to achieve the stretch goal!"
|
|
1723
1723
|
},
|
|
1724
1724
|
{
|
|
1725
1725
|
"type": "solution",
|
|
1726
1726
|
"id": "random_numbers__random_bit_solution",
|
|
1727
|
-
"code": "namespace Kata {\n operation RandomBit() : Int {\n // Allocate single qubit.\n use q = Qubit();\n\n //
|
|
1727
|
+
"code": "namespace Kata {\n open Microsoft.Quantum.Measurement;\n operation RandomBit() : Int {\n // Allocate single qubit.\n use q = Qubit();\n\n // Convert the qubit state to |+>.\n H(q);\n\n // Measure the qubit and reset it to |0>.\n let result = MResetZ(q);\n\n // Return integer value of result.\n if result == One {\n return 1;\n }\n return 0;\n }\n}\n"
|
|
1728
1728
|
}
|
|
1729
1729
|
]
|
|
1730
1730
|
}
|
|
@@ -1739,23 +1739,23 @@ export default {
|
|
|
1739
1739
|
"asMarkdown": "\nNow that you can generate a single random bit, you can use that logic to create random multi-bit numbers. Let's try first to make a two-bit number by combining two randomly generated bits.\n\n**Input:** None.\n\n**Goal:** Generate a random number in the range $[0, 3]$ with an equal probability of getting each of the four numbers.\n\n**Stretch goal:** Can you do this without allocating qubits in this operation?\n<details>\n <summary><b>Need a hint?</b></summary>\n Remember that you can use previously defined operations to implement your solution. For convenience, the <b>RandomBit</b> operation is already available for you to use in this exercise.\n</details>\n"
|
|
1740
1740
|
},
|
|
1741
1741
|
"sourceIds": [
|
|
1742
|
-
"
|
|
1742
|
+
"random_numbers__random_two_bits__Verification.qs",
|
|
1743
1743
|
"KatasLibrary.qs",
|
|
1744
|
-
"
|
|
1744
|
+
"random_numbers__Common.qs"
|
|
1745
1745
|
],
|
|
1746
|
-
"placeholderCode": "namespace Kata {\n operation RandomTwoBits() : Int {\n // Implement your solution here...\n\n return -1;\n }\n\n // You can use
|
|
1746
|
+
"placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Measurement;\n operation RandomTwoBits() : Int {\n // Implement your solution here...\n\n return -1;\n }\n\n // You can use the operation defined in the previous exercise to implement your solution.\n operation RandomBit() : Int {\n use q = Qubit();\n H(q);\n return MResetZ(q) == Zero ? 0 | 1;\n }\n}\n",
|
|
1747
1747
|
"explainedSolution": {
|
|
1748
1748
|
"type": "explained-solution",
|
|
1749
1749
|
"items": [
|
|
1750
1750
|
{
|
|
1751
1751
|
"type": "text-content",
|
|
1752
|
-
"asHtml": "<p>Let's reuse the <code>RandomBit</code> operation from the "Generate A Single Random Bit" exercise.\nWe can generate two random bits by calling the <code>RandomBit</code> operation twice, multiply the most significant bit by 2 and add the
|
|
1753
|
-
"asMarkdown": "\nLet's reuse the `RandomBit` operation from the \"Generate A Single Random Bit\" exercise.\nWe can generate two random bits by calling the `RandomBit` operation twice, multiply the most significant bit by 2 and add the
|
|
1752
|
+
"asHtml": "<p>Let's reuse the <code>RandomBit</code> operation from the "Generate A Single Random Bit" exercise.\nWe can generate two random bits by calling the <code>RandomBit</code> operation twice, multiply the most significant bit by 2 and add the least significant bit to generate a random two-bit number.</p>\n",
|
|
1753
|
+
"asMarkdown": "\nLet's reuse the `RandomBit` operation from the \"Generate A Single Random Bit\" exercise.\nWe can generate two random bits by calling the `RandomBit` operation twice, multiply the most significant bit by 2 and add the least significant bit to generate a random two-bit number."
|
|
1754
1754
|
},
|
|
1755
1755
|
{
|
|
1756
1756
|
"type": "solution",
|
|
1757
1757
|
"id": "random_numbers__random_two_bits_solution",
|
|
1758
|
-
"code": "namespace Kata {\n operation RandomTwoBits() : Int {\n return 2 * RandomBit() + RandomBit();\n }\n\n operation RandomBit() : Int {\n
|
|
1758
|
+
"code": "namespace Kata {\n open Microsoft.Quantum.Measurement;\n operation RandomTwoBits() : Int {\n return 2 * RandomBit() + RandomBit();\n }\n\n operation RandomBit() : Int {\n use q = Qubit();\n H(q);\n return MResetZ(q) == Zero ? 0 | 1;\n }\n}\n"
|
|
1759
1759
|
}
|
|
1760
1760
|
]
|
|
1761
1761
|
}
|
|
@@ -1766,27 +1766,27 @@ export default {
|
|
|
1766
1766
|
"title": "Generate a Number of Arbitrary Size",
|
|
1767
1767
|
"description": {
|
|
1768
1768
|
"type": "text-content",
|
|
1769
|
-
"asHtml": "<p>Let's take it a step further and generate an $N$-bit number.</p>\n<p><strong>Input:</strong> An integer $N$ ($1 \\le N \\le 10$).</p>\n<p><strong>Goal:</strong> Generate a random number in the range $[0, 2^N - 1]$ with an equal probability of getting each of the numbers in this range.</p>\n<blockquote>\n<p>Useful Q# documentation:</p>\n<ul>\n<li><a href=\"https://docs.microsoft.com/azure/quantum/user-guide/language/statements/iterations\" target=\"_blank\">for loops</a></li>\n<li><a href=\"https://docs.microsoft.com/azure/quantum/user-guide/language/typesystem/immutability\" target=\"_blank\">mutable variables</a></li>\n
|
|
1770
|
-
"asMarkdown": "Let's take it a step further and generate an $N$-bit number.\n\n**Input:** An integer $N$ ($1 \\le N \\le 10$).\n\n**Goal:** Generate a random number in the range $[0, 2^N - 1]$ with an equal probability of getting each of the numbers in this range.\n\n> Useful Q# documentation:\n>\n> * <a href=\"https://docs.microsoft.com/azure/quantum/user-guide/language/statements/iterations\" target=\"_blank\">for loops</a>\n> * <a href=\"https://docs.microsoft.com/azure/quantum/user-guide/language/typesystem/immutability\" target=\"_blank\">mutable variables</a>\n
|
|
1769
|
+
"asHtml": "<p>Let's take it a step further and generate an $N$-bit number.</p>\n<p><strong>Input:</strong> An integer $N$ ($1 \\le N \\le 10$).</p>\n<p><strong>Goal:</strong> Generate a random number in the range $[0, 2^N - 1]$ with an equal probability of getting each of the numbers in this range.</p>\n<blockquote>\n<p>Useful Q# documentation:</p>\n<ul>\n<li><a href=\"https://docs.microsoft.com/azure/quantum/user-guide/language/statements/iterations\" target=\"_blank\">for loops</a></li>\n<li><a href=\"https://docs.microsoft.com/azure/quantum/user-guide/language/typesystem/immutability\" target=\"_blank\">mutable variables</a></li>\n</ul>\n</blockquote>\n<details>\n <summary><b>Need a hint?</b></summary>\n Remember that you can use previously defined operations to implement your solution. For convenience, the <b>RandomBit</b> operation is already available for you to use in this exercise.\n</details>\n",
|
|
1770
|
+
"asMarkdown": "Let's take it a step further and generate an $N$-bit number.\n\n**Input:** An integer $N$ ($1 \\le N \\le 10$).\n\n**Goal:** Generate a random number in the range $[0, 2^N - 1]$ with an equal probability of getting each of the numbers in this range.\n\n> Useful Q# documentation:\n>\n> * <a href=\"https://docs.microsoft.com/azure/quantum/user-guide/language/statements/iterations\" target=\"_blank\">for loops</a>\n> * <a href=\"https://docs.microsoft.com/azure/quantum/user-guide/language/typesystem/immutability\" target=\"_blank\">mutable variables</a>\n\n<details>\n <summary><b>Need a hint?</b></summary>\n Remember that you can use previously defined operations to implement your solution. For convenience, the <b>RandomBit</b> operation is already available for you to use in this exercise.\n</details>\n"
|
|
1771
1771
|
},
|
|
1772
1772
|
"sourceIds": [
|
|
1773
|
-
"
|
|
1773
|
+
"random_numbers__random_n_bits__Verification.qs",
|
|
1774
1774
|
"KatasLibrary.qs",
|
|
1775
|
-
"
|
|
1775
|
+
"random_numbers__Common.qs"
|
|
1776
1776
|
],
|
|
1777
|
-
"placeholderCode": "namespace Kata {\n operation RandomNBits(N : Int) : Int {\n // Implement your solution here...\n\n return -1;\n }\n\n // You can use
|
|
1777
|
+
"placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Measurement;\n operation RandomNBits(N : Int) : Int {\n // Implement your solution here...\n\n return -1;\n }\n\n // You can use the operation defined in the first exercise to implement your solution.\n operation RandomBit() : Int {\n use q = Qubit();\n H(q);\n return MResetZ(q) == Zero ? 0 | 1;\n }\n}\n",
|
|
1778
1778
|
"explainedSolution": {
|
|
1779
1779
|
"type": "explained-solution",
|
|
1780
1780
|
"items": [
|
|
1781
1781
|
{
|
|
1782
1782
|
"type": "text-content",
|
|
1783
|
-
"asHtml": "<p>Let's reuse the <code>RandomBit</code> operation from the "Generate A Single Random Bit" exercise again.\nWe'll generate N random bits by calling <code>RandomBit</code> operation N times, and treat the result as a binary notation
|
|
1784
|
-
"asMarkdown": "\nLet's reuse the `RandomBit` operation from the \"Generate A Single Random Bit\" exercise again.\nWe'll generate N random bits by calling `RandomBit` operation N times, and treat the result as a binary notation
|
|
1783
|
+
"asHtml": "<p>Let's reuse the <code>RandomBit</code> operation from the "Generate A Single Random Bit" exercise again.\nWe'll generate $N$ random bits by calling <code>RandomBit</code> operation $N$ times, and treat the result as a binary notation of the integer we're looking for.\nSince the maximum value of the number written with $N$ bits is $2^N - 1$, we don't need to do any extra checks to ensure that the result is within the given range.</p>\n",
|
|
1784
|
+
"asMarkdown": "\nLet's reuse the `RandomBit` operation from the \"Generate A Single Random Bit\" exercise again.\nWe'll generate $N$ random bits by calling `RandomBit` operation $N$ times, and treat the result as a binary notation of the integer we're looking for.\nSince the maximum value of the number written with $N$ bits is $2^N - 1$, we don't need to do any extra checks to ensure that the result is within the given range."
|
|
1785
1785
|
},
|
|
1786
1786
|
{
|
|
1787
1787
|
"type": "solution",
|
|
1788
1788
|
"id": "random_numbers__random_n_bits_solution",
|
|
1789
|
-
"code": "namespace Kata {\n operation RandomNBits(N : Int) : Int {\n mutable result = 0;\n for i in 0..
|
|
1789
|
+
"code": "namespace Kata {\n open Microsoft.Quantum.Measurement;\n operation RandomNBits(N : Int) : Int {\n mutable result = 0;\n for i in 0 .. N - 1 {\n set result = result * 2 + RandomBit();\n }\n return result;\n }\n\n // You can use the operation defined in the first exercise to implement your solution.\n operation RandomBit() : Int {\n use q = Qubit();\n H(q);\n return MResetZ(q) == Zero ? 0 | 1;\n }\n}\n"
|
|
1790
1790
|
}
|
|
1791
1791
|
]
|
|
1792
1792
|
}
|
|
@@ -1797,27 +1797,27 @@ export default {
|
|
|
1797
1797
|
"title": "Generate a Weighted Bit",
|
|
1798
1798
|
"description": {
|
|
1799
1799
|
"type": "text-content",
|
|
1800
|
-
"asHtml": "<p>In each of the above exercises, all generated numbers were equally likely. Now let's create an operation that will return a random bit with different probabilities of outcomes. </p>\n<blockquote>\n<p>Remember that by setting the amplitudes of basis states $\\alpha$ and $\\beta$, we can control the probability of getting measurement outcomes $0$ and $1$ when the qubit is measured.</p>\n</blockquote>\n<p><strong>Input:</strong> \nA floating-point number $x$, $0 \\le x \\le 1$. </p>\n<p><strong>Goal:</strong> Generate $0$ or $1$ with probability of $0$ equal to $x$ and probability of $1$ equal to $1 - x$.</p>\n<blockquote>\n<p>
|
|
1801
|
-
"asMarkdown": "In each of the above exercises, all generated numbers were equally likely. Now let's create an operation that will return a random bit with different probabilities of outcomes. \n\n> Remember that by setting the amplitudes of basis states $\\alpha$ and $\\beta$, we can control the probability of getting measurement outcomes $0$ and $1$ when the qubit is measured.\n\n**Input:** \nA floating-point number $x$, $0 \\le x \\le 1$. \n\n**Goal:** Generate $0$ or $1$ with probability of $0$ equal to $x$ and probability of $1$ equal to $1 - x$.\n\n>
|
|
1800
|
+
"asHtml": "<p>In each of the above exercises, all generated numbers were equally likely. Now let's create an operation that will return a random bit with different probabilities of outcomes. </p>\n<blockquote>\n<p>Remember that by setting the amplitudes of basis states $\\alpha$ and $\\beta$, we can control the probability of getting measurement outcomes $0$ and $1$ when the qubit is measured.</p>\n</blockquote>\n<p><strong>Input:</strong> \nA floating-point number $x$, $0 \\le x \\le 1$. </p>\n<p><strong>Goal:</strong> Generate $0$ or $1$ with probability of $0$ equal to $x$ and probability of $1$ equal to $1 - x$.</p>\n<blockquote>\n<p>Q# namespace <code>Microsoft.Quantum.Math</code> includes useful functions <code>Sqrt</code>, <code>ArcCos</code>, and <code>ArcSin</code>.</p>\n</blockquote>\n",
|
|
1801
|
+
"asMarkdown": "In each of the above exercises, all generated numbers were equally likely. Now let's create an operation that will return a random bit with different probabilities of outcomes. \n\n> Remember that by setting the amplitudes of basis states $\\alpha$ and $\\beta$, we can control the probability of getting measurement outcomes $0$ and $1$ when the qubit is measured.\n\n**Input:** \nA floating-point number $x$, $0 \\le x \\le 1$. \n\n**Goal:** Generate $0$ or $1$ with probability of $0$ equal to $x$ and probability of $1$ equal to $1 - x$.\n\n> Q# namespace `Microsoft.Quantum.Math` includes useful functions `Sqrt`, `ArcCos`, and `ArcSin`.\n"
|
|
1802
1802
|
},
|
|
1803
1803
|
"sourceIds": [
|
|
1804
|
-
"
|
|
1804
|
+
"random_numbers__weighted_random_bit__Verification.qs",
|
|
1805
1805
|
"KatasLibrary.qs",
|
|
1806
|
-
"
|
|
1806
|
+
"random_numbers__Common.qs"
|
|
1807
1807
|
],
|
|
1808
|
-
"placeholderCode": "namespace Kata {\n operation WeightedRandomBit(x : Double) : Int {\n // Implement your solution here...\n\n return -1;\n }\n
|
|
1808
|
+
"placeholderCode": "namespace Kata {\n operation WeightedRandomBit(x : Double) : Int {\n // Implement your solution here...\n\n return -1;\n }\n}\n",
|
|
1809
1809
|
"explainedSolution": {
|
|
1810
1810
|
"type": "explained-solution",
|
|
1811
1811
|
"items": [
|
|
1812
1812
|
{
|
|
1813
1813
|
"type": "text-content",
|
|
1814
|
-
"asHtml": "<p>An arbitrary single-qubit state can be written as:</p>\n<p>$$\n|\\psi\\rangle =\n \\cos \\frac{\\theta}{2} |0 \\rangle
|
|
1815
|
-
"asMarkdown": "\nAn arbitrary single-qubit state can be written as:\n\n$$\n|\\psi\\rangle =\n \\cos \\frac{\\theta}{2} |0 \\rangle
|
|
1814
|
+
"asHtml": "<p>An arbitrary single-qubit state can be written as:</p>\n<p>$$\n|\\psi\\rangle =\n \\cos \\frac{\\theta}{2} |0 \\rangle + e^{i\\phi} \\sin \\frac{\\theta}{2} |1\\rangle\n$$</p>\n<p>Here, $\\theta$ is the angle between the state vector and the $Z$-axis, and $\\phi$ is the longitude angle with respect to the $X$-axis on the Bloch sphere.</p>\n<p>Our goal is to generate $0$ or $1$ with the probability of generating a $0$ equal to $x$ and the probability of generating a $1$ equal to $1 - x$. This means that the qubit state should look like</p>\n<p>$$\n|\\psi\\rangle =\n \\sqrt x |0 \\rangle + \\sqrt{1 - x} |1\\rangle\n$$</p>\n<p>Comparing the amplitudes of the state $|0 \\rangle$ in the two equations, we get</p>\n<p>$$\n\\sqrt x = \\cos \\frac{\\theta}{2} \\Rightarrow \\theta = 2 \\arccos\\sqrt x\n$$</p>\n<p>Since $\\theta$ is the angle between the state vector and the $Z$-axis, we need to apply the $Ry$ gate with the calculated $\\theta$ to the starting state $|0 \\rangle$ to get the desired qubit state.</p>\n<p>The $Ry$ gate applies a given rotation about the $Y$-axis (i.e., in the $ZX$-plane). Hence, $\\phi$ (longitude angle with respect to $X$-axis) is always equal to $0^{\\circ}$, which means that the relative phase $e^{i\\phi}$ doesn't have any impact on the resulting qubit state.</p>\n<blockquote>\n<p>We can also calculate ${\\theta}$ by comparing the amplitudes of the state $|1 \\rangle$ in the two equations, which is $2 \\arcsin\\sqrt{1.0 - x}$. The results will be equivalent.</p>\n</blockquote>\n",
|
|
1815
|
+
"asMarkdown": "\nAn arbitrary single-qubit state can be written as:\n\n$$\n|\\psi\\rangle =\n \\cos \\frac{\\theta}{2} |0 \\rangle + e^{i\\phi} \\sin \\frac{\\theta}{2} |1\\rangle\n$$\n\nHere, $\\theta$ is the angle between the state vector and the $Z$-axis, and $\\phi$ is the longitude angle with respect to the $X$-axis on the Bloch sphere.\n\nOur goal is to generate $0$ or $1$ with the probability of generating a $0$ equal to $x$ and the probability of generating a $1$ equal to $1 - x$. This means that the qubit state should look like\n\n$$\n|\\psi\\rangle =\n \\sqrt x |0 \\rangle + \\sqrt{1 - x} |1\\rangle\n$$\n\nComparing the amplitudes of the state $|0 \\rangle$ in the two equations, we get\n\n$$\n\\sqrt x = \\cos \\frac{\\theta}{2} \\Rightarrow \\theta = 2 \\arccos\\sqrt x\n$$\n\nSince $\\theta$ is the angle between the state vector and the $Z$-axis, we need to apply the $Ry$ gate with the calculated $\\theta$ to the starting state $|0 \\rangle$ to get the desired qubit state.\n\nThe $Ry$ gate applies a given rotation about the $Y$-axis (i.e., in the $ZX$-plane). Hence, $\\phi$ (longitude angle with respect to $X$-axis) is always equal to $0^{\\circ}$, which means that the relative phase $e^{i\\phi}$ doesn't have any impact on the resulting qubit state.\n\n> We can also calculate ${\\theta}$ by comparing the amplitudes of the state $|1 \\rangle$ in the two equations, which is $2 \\arcsin\\sqrt{1.0 - x}$. The results will be equivalent."
|
|
1816
1816
|
},
|
|
1817
1817
|
{
|
|
1818
1818
|
"type": "solution",
|
|
1819
1819
|
"id": "random_numbers__weighted_random_bit_solution",
|
|
1820
|
-
"code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n operation WeightedRandomBit(x : Double) : Int {\n
|
|
1820
|
+
"code": "namespace Kata {\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Measurement;\n\n operation WeightedRandomBit(x : Double) : Int {\n let theta = 2.0 * ArcCos(Sqrt(x)); // (or) 2.0 * ArcSin(Sqrt(1.0 - x));\n\n use q = Qubit();\n Ry(theta, q);\n\n let result = MResetZ(q);\n return result == Zero ? 0 | 1;\n }\n}\n"
|
|
1821
1821
|
}
|
|
1822
1822
|
]
|
|
1823
1823
|
}
|
|
@@ -1828,27 +1828,27 @@ export default {
|
|
|
1828
1828
|
"title": "Generate a Random Number Between Min and Max",
|
|
1829
1829
|
"description": {
|
|
1830
1830
|
"type": "text-content",
|
|
1831
|
-
"asHtml": "<p>In the "Generate A Number Of Arbitrary Size" exercise, we generated numbers in the range $[0, 2^N-1]$ $(1 \\leq N \\leq 10)$. Now let's create an operation that will return a random number in the range $[min, max]$. </p>\n<p><strong>Input:</strong> \nTwo integers $min$ and $max$ ($0 \\leq min \\leq max \\leq 2^{10}-1$).</p>\n<p><strong>Goal:</strong> Generate a random number in the range $[min, max]$ with an equal probability of getting each of the numbers in this range.</p>\n<blockquote>\n<p>
|
|
1832
|
-
"asMarkdown": "In the \"Generate A Number Of Arbitrary Size\" exercise, we generated numbers in the range $[0, 2^N-1]$ $(1 \\leq N \\leq 10)$. Now let's create an operation that will return a random number in the range $[min, max]$. \n\n**Input:** \nTwo integers $min$ and $max$ ($0 \\leq min \\leq max \\leq 2^{10}-1$).\n\n**Goal:** Generate a random number in the range $[min, max]$ with an equal probability of getting each of the numbers in this range.\n\n>
|
|
1831
|
+
"asHtml": "<p>In the "Generate A Number Of Arbitrary Size" exercise, we generated numbers in the range $[0, 2^N-1]$ $(1 \\leq N \\leq 10)$. Now let's create an operation that will return a random number in the range $[min, max]$. </p>\n<p><strong>Input:</strong> \nTwo integers $min$ and $max$ ($0 \\leq min \\leq max \\leq 2^{10}-1$).</p>\n<p><strong>Goal:</strong> Generate a random number in the range $[min, max]$ with an equal probability of getting each of the numbers in this range.</p>\n<blockquote>\n<p>Q# namespace <code>Microsoft.Quantum.Math</code> includes useful function <code>BitSizeI</code> that calculates the number of bits in the binary representation of the given number.</p>\n</blockquote>\n",
|
|
1832
|
+
"asMarkdown": "In the \"Generate A Number Of Arbitrary Size\" exercise, we generated numbers in the range $[0, 2^N-1]$ $(1 \\leq N \\leq 10)$. Now let's create an operation that will return a random number in the range $[min, max]$. \n\n**Input:** \nTwo integers $min$ and $max$ ($0 \\leq min \\leq max \\leq 2^{10}-1$).\n\n**Goal:** Generate a random number in the range $[min, max]$ with an equal probability of getting each of the numbers in this range.\n\n> Q# namespace `Microsoft.Quantum.Math` includes useful function `BitSizeI` that calculates the number of bits in the binary representation of the given number.\n"
|
|
1833
1833
|
},
|
|
1834
1834
|
"sourceIds": [
|
|
1835
|
-
"
|
|
1835
|
+
"random_numbers__random_number__Verification.qs",
|
|
1836
1836
|
"KatasLibrary.qs",
|
|
1837
|
-
"
|
|
1837
|
+
"random_numbers__Common.qs"
|
|
1838
1838
|
],
|
|
1839
|
-
"placeholderCode": "namespace Kata {\n operation RandomNumberInRange(min : Int, max : Int) : Int {\n // Implement your solution here...\n\n return -1;\n }\n\n // You can use
|
|
1839
|
+
"placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Measurement;\n\n operation RandomNumberInRange(min : Int, max : Int) : Int {\n // Implement your solution here...\n\n return -1;\n }\n\n // You can use the operations defined in the earlier exercises to implement your solution.\n operation RandomBit() : Int {\n use q = Qubit();\n H(q);\n return MResetZ(q) == Zero ? 0 | 1;\n }\n\n operation RandomNBits(N : Int) : Int {\n mutable result = 0;\n for i in 0 .. N - 1 {\n set result = result * 2 + RandomBit();\n }\n return result;\n }\n}\n",
|
|
1840
1840
|
"explainedSolution": {
|
|
1841
1841
|
"type": "explained-solution",
|
|
1842
1842
|
"items": [
|
|
1843
1843
|
{
|
|
1844
1844
|
"type": "text-content",
|
|
1845
|
-
"asHtml": "<p>We can reuse the <code>RandomBit</code>
|
|
1846
|
-
"asMarkdown": "\nWe can reuse the `RandomBit`
|
|
1845
|
+
"asHtml": "<p>We can reuse the <code>RandomBit</code> and <code>RandomNBits</code> operations from earlier exercises.</p>\n<p>We'll generate an $N$-bit random number by calling the <code>RandomNBits</code> operation, where $N$ is the bitsize of $max - min$. We can repeat this process until the result is less than or equal than $max - min$, and return that number plus $min$.</p>\n",
|
|
1846
|
+
"asMarkdown": "\nWe can reuse the `RandomBit` and `RandomNBits` operations from earlier exercises.\n\nWe'll generate an $N$-bit random number by calling the `RandomNBits` operation, where $N$ is the bitsize of $max - min$. We can repeat this process until the result is less than or equal than $max - min$, and return that number plus $min$."
|
|
1847
1847
|
},
|
|
1848
1848
|
{
|
|
1849
1849
|
"type": "solution",
|
|
1850
1850
|
"id": "random_numbers__random_number_solution",
|
|
1851
|
-
"code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n operation RandomNumberInRange(min : Int, max : Int) : Int {\n let nBits = BitSizeI(max - min);\n mutable output = 0;\n repeat {\n set output = RandomNBits(nBits);\n } until output <= max - min;\n return output + min;\n }\n\n operation RandomNBits(N : Int) : Int {\n mutable result = 0;\n for i in 0..
|
|
1851
|
+
"code": "namespace Kata {\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Measurement;\n\n operation RandomNumberInRange(min : Int, max : Int) : Int {\n let nBits = BitSizeI(max - min);\n mutable output = 0;\n repeat {\n set output = RandomNBits(nBits);\n } until output <= max - min;\n return output + min;\n }\n\n operation RandomNBits(N : Int) : Int {\n mutable result = 0;\n for i in 0 .. N - 1 {\n set result = result * 2 + RandomBit();\n }\n return result;\n }\n\n operation RandomBit() : Int {\n use q = Qubit();\n H(q);\n return MResetZ(q) == Zero ? 0 | 1;\n }\n}\n"
|
|
1852
1852
|
}
|
|
1853
1853
|
]
|
|
1854
1854
|
}
|
|
@@ -1860,8 +1860,8 @@ export default {
|
|
|
1860
1860
|
"items": [
|
|
1861
1861
|
{
|
|
1862
1862
|
"type": "text-content",
|
|
1863
|
-
"asHtml": "<p>Congratulations! In this kata you have created a random number generator. Here are a few key concepts to keep in mind:</p>\n<ul>\n<li>This code will generate truly random numbers when executed on a true quantum computer. Random numbers obtained when executing on a simulator are only as good as the source of randomness used by the simulator.</li>\n<li>You can generate a random bit by
|
|
1864
|
-
"asMarkdown": "\nCongratulations! In this kata you have created a random number generator. Here are a few key concepts to keep in mind:\n
|
|
1863
|
+
"asHtml": "<p>Congratulations! In this kata you have created a random number generator. Here are a few key concepts to keep in mind:</p>\n<ul>\n<li>This code will generate truly random numbers when executed on a true quantum computer. Random numbers obtained when executing on a simulator are only as good as the source of randomness used by the simulator.</li>\n<li>You can generate a random bit by preparing a qubit in superposition and then measuring it in the computational basis.\nThe amplitudes of the basis states will define the probability distribution of the generated bits.</li>\n<li>The Q# library function <code>BitSizeI</code> returns the number of bits in the binary representation of an integer.</li>\n</ul>\n",
|
|
1864
|
+
"asMarkdown": "\nCongratulations! In this kata you have created a random number generator. Here are a few key concepts to keep in mind:\n\n- This code will generate truly random numbers when executed on a true quantum computer. Random numbers obtained when executing on a simulator are only as good as the source of randomness used by the simulator.\n- You can generate a random bit by preparing a qubit in superposition and then measuring it in the computational basis.\n The amplitudes of the basis states will define the probability distribution of the generated bits.\n- The Q# library function `BitSizeI` returns the number of bits in the binary representation of an integer."
|
|
1865
1865
|
}
|
|
1866
1866
|
]
|
|
1867
1867
|
}
|
|
@@ -1905,9 +1905,9 @@ export default {
|
|
|
1905
1905
|
"asMarkdown": "**Input:** \n A bit vector of length 3 represented as a `Bool[]` - a binary representation of a number.\n\n**Output:**\n Return `true` if the input array represents the number $7$, and `false` otherwise.\n\n**Examples:**\n\n* If the input array is `[true, true, true]`, return `true`.\n* If the input array is `[true, true, false]`, return `false`.\n"
|
|
1906
1906
|
},
|
|
1907
1907
|
"sourceIds": [
|
|
1908
|
+
"oracles__classical_oracles__Verification.qs",
|
|
1908
1909
|
"KatasLibrary.qs",
|
|
1909
|
-
"
|
|
1910
|
-
"oracles__classical_oracles__verification.qs"
|
|
1910
|
+
"oracles__Common.qs"
|
|
1911
1911
|
],
|
|
1912
1912
|
"placeholderCode": "namespace Kata {\n function IsSeven(x : Bool[]) : Bool {\n // Implement your solution here...\n\n return false;\n }\n}\n",
|
|
1913
1913
|
"explainedSolution": {
|
|
@@ -1928,8 +1928,8 @@ export default {
|
|
|
1928
1928
|
"items": [
|
|
1929
1929
|
{
|
|
1930
1930
|
"type": "text-content",
|
|
1931
|
-
"asHtml": "<p>An oracle in the quantum world is a "black box" operation that is used as input to an algorithm (such as Deutsch-Jozsa algorithm or Grover's search algorithm)
|
|
1932
|
-
"asMarkdown": "\nAn oracle in the quantum world is a \"black box\" operation that is used as input to an algorithm (such as Deutsch-Jozsa algorithm or Grover's search algorithm)
|
|
1931
|
+
"asHtml": "<p>An oracle in the quantum world is a "black box" operation that is used as input to an algorithm (such as Deutsch-Jozsa algorithm or Grover's search algorithm).\nMany quantum algorithms assume an oracle implementation of some classical function as input, but this is a very strong assumption - sometimes implementing the oracle for a function is a lot more complex than the algorithm that will use this oracle!<br>In this kata, you will learn the properties of quantum oracles and how to implement them.</p>\n<p>A quantum oracle implements a function $f: \\{0,1\\}^n \\rightarrow \\{0,1\\}^m$, where the input is $n$-bits of the form $x = (x_{0}, x_{1}, \\dots, x_{n-1})$. In most commonly used cases $m=1$, that is, the function can return values $0$ or $1$. In this kata, we will focus on this class of functions.</p>\n<p>Quantum oracles operate on qubit arrays (and can take classical parameters as well). The classical input is encoded into the state of an $n$-qubit register:<br>$$|x\\rangle = |x_0\\rangle \\otimes |x_1\\rangle \\otimes ... \\otimes |x_{n-1}\\rangle,$$\nwhere $|x_i\\rangle$ represents the state of the $i$-th qubit. </p>\n<p>Oracles must be unitary transformations, and follow the same rules of linear algebra as other quantum operations.\nThis allows us to define quantum oracles based on their effect on the basis states - tensor products of single-qubit basis states $|0\\rangle$ and $|1\\rangle$.</p>\n<blockquote>\n<p>For example, an oracle that implements a function that takes 2 bits of input will be defined using its effect on basis states $|00\\rangle$, $|01\\rangle$, $|10\\rangle$, and $|11\\rangle$. </p>\n</blockquote>\n<p>There are two types of quantum oracles: phase oracles and marking oracles. Let's take a closer look at them.</p>\n<h2>Phase Oracles</h2>\n<p>For a function $f: \\{0,1\\}^n \\rightarrow \\{0,1\\}$, the phase oracle $U_{\\text{phase}}$ encodes the the values $f(0)$ and $f(1)$ in the relative phases of basis states $\\ket{0}$ and $\\ket{1}$, respectively.</p>\n<p>$$U_{phase} |\\vec{x}\\rangle = (-1)^{f(x)}|\\vec{x}\\rangle$$</p>\n<p>Thus, the phase oracle $U_{\\text{phase}}$ doesn't change the phase of the basis states for which $f(x)=0$, but multiplies the phase of the basis states for which $f(x)=1$ by $-1$.</p>\n<p>The effect of such an oracle on any single basis state is not particularly interesting: it just adds a global phase which is not something you can observe. However, if you apply this oracle to a <em>superposition</em> of basis states, its effect becomes noticeable.\nRemember that quantum operations are linear: if you define the effect of an operation on the basis states, you'll be able to deduce its effect on superposition states (which are just linear combinations of the basis states) using its linearity.</p>\n<p>A phase oracle doesn't have an "output", unlike the function it implements; the effect of the oracle application is the change in the state of the system.</p>\n",
|
|
1932
|
+
"asMarkdown": "\nAn oracle in the quantum world is a \"black box\" operation that is used as input to an algorithm (such as Deutsch-Jozsa algorithm or Grover's search algorithm).\nMany quantum algorithms assume an oracle implementation of some classical function as input, but this is a very strong assumption - sometimes implementing the oracle for a function is a lot more complex than the algorithm that will use this oracle! \nIn this kata, you will learn the properties of quantum oracles and how to implement them.\n\nA quantum oracle implements a function $f: \\\\{0,1\\\\}^n \\rightarrow \\\\{0,1\\\\}^m$, where the input is $n$-bits of the form $x = (x_{0}, x_{1}, \\dots, x_{n-1})$. In most commonly used cases $m=1$, that is, the function can return values $0$ or $1$. In this kata, we will focus on this class of functions.\n\nQuantum oracles operate on qubit arrays (and can take classical parameters as well). The classical input is encoded into the state of an $n$-qubit register: \n$$|x\\rangle = |x_0\\rangle \\otimes |x_1\\rangle \\otimes ... \\otimes |x_{n-1}\\rangle,$$\nwhere $|x_i\\rangle$ represents the state of the $i$-th qubit. \n\nOracles must be unitary transformations, and follow the same rules of linear algebra as other quantum operations.\nThis allows us to define quantum oracles based on their effect on the basis states - tensor products of single-qubit basis states $|0\\rangle$ and $|1\\rangle$.\n\n> For example, an oracle that implements a function that takes 2 bits of input will be defined using its effect on basis states $|00\\rangle$, $|01\\rangle$, $|10\\rangle$, and $|11\\rangle$. \n\nThere are two types of quantum oracles: phase oracles and marking oracles. Let's take a closer look at them.\n\n## Phase Oracles\n\nFor a function $f: \\\\{0,1\\\\}^n \\rightarrow \\\\{0,1\\\\}$, the phase oracle $U_{\\text{phase}}$ encodes the the values $f(0)$ and $f(1)$ in the relative phases of basis states $\\ket{0}$ and $\\ket{1}$, respectively.\n\n$$U_{phase} |\\vec{x}\\rangle = (-1)^{f(x)}|\\vec{x}\\rangle$$\n\nThus, the phase oracle $U_{\\text{phase}}$ doesn't change the phase of the basis states for which $f(x)=0$, but multiplies the phase of the basis states for which $f(x)=1$ by $-1$.\n\nThe effect of such an oracle on any single basis state is not particularly interesting: it just adds a global phase which is not something you can observe. However, if you apply this oracle to a *superposition* of basis states, its effect becomes noticeable.\nRemember that quantum operations are linear: if you define the effect of an operation on the basis states, you'll be able to deduce its effect on superposition states (which are just linear combinations of the basis states) using its linearity.\n\nA phase oracle doesn't have an \"output\", unlike the function it implements; the effect of the oracle application is the change in the state of the system."
|
|
1933
1933
|
}
|
|
1934
1934
|
]
|
|
1935
1935
|
},
|
|
@@ -1950,8 +1950,8 @@ export default {
|
|
|
1950
1950
|
},
|
|
1951
1951
|
{
|
|
1952
1952
|
"type": "text-content",
|
|
1953
|
-
"asHtml": "<p>We introduced the function <a href=\"https://learn.microsoft.com/en-us/qsharp/api/qsharp/microsoft.quantum.canon.applycontrolledonbitstring\" target=\"_blank\"><code>ApplyControlledOnBitString</code></a> provided by the Q# Standard library.\nIt defines a variant of a gate controlled on a state specified by a bit mask; for example, bit mask <code>[true, false]</code> means that the gate should be applied only if the two control qubits are in the $|10\\rangle$ state.</p>\n<p>The sequence of steps that implement this variant are:</p>\n<ol>\n<li>Apply the $X$ gate to each control qubit that corresponds to a <code>false</code> element of the bit mask. After this, if the control qubits started in the $|10\\rangle$ state, they'll end up in the $|11\\rangle$ state, and if they started in any other state, they'll end up in any state but $|11\\rangle$.</li>\n<li>Apply the regular controlled version of the gate.</li>\n<li>Apply the $X$ gate to the same qubits to return them to their original state.</li>\n</ol>\n<p>Due to this <a href=\"https://learn.microsoft.com/en-us/azure/quantum/user-guide/language/statements/conjugations\" target=\"_blank\">conjugation pattern</a>, the time complexity of this function is $2N$, where N is the number of control qubits.</p>\n<blockquote>\n<p>Notice that the input state in the demo above is an equal superposition of all basis states
|
|
1954
|
-
"asMarkdown": "\nWe introduced the function <a href=\"https://learn.microsoft.com/en-us/qsharp/api/qsharp/microsoft.quantum.canon.applycontrolledonbitstring\" target=\"_blank\">`ApplyControlledOnBitString`</a> provided by the Q# Standard library.\nIt defines a variant of a gate controlled on a state specified by a bit mask; for example, bit mask `[true, false]` means that the gate should be applied only if the two control qubits are in the $|10\\rangle$ state.\n
|
|
1953
|
+
"asHtml": "<p>We introduced the function <a href=\"https://learn.microsoft.com/en-us/qsharp/api/qsharp/microsoft.quantum.canon.applycontrolledonbitstring\" target=\"_blank\"><code>ApplyControlledOnBitString</code></a> provided by the Q# Standard library.\nIt defines a variant of a gate controlled on a state specified by a bit mask; for example, bit mask <code>[true, false]</code> means that the gate should be applied only if the two control qubits are in the $|10\\rangle$ state.</p>\n<p>The sequence of steps that implement this variant are:</p>\n<ol>\n<li>Apply the $X$ gate to each control qubit that corresponds to a <code>false</code> element of the bit mask. After this, if the control qubits started in the $|10\\rangle$ state, they'll end up in the $|11\\rangle$ state, and if they started in any other state, they'll end up in any state but $|11\\rangle$.</li>\n<li>Apply the regular controlled version of the gate.</li>\n<li>Apply the $X$ gate to the same qubits to return them to their original state.</li>\n</ol>\n<p>Due to this <a href=\"https://learn.microsoft.com/en-us/azure/quantum/user-guide/language/statements/conjugations\" target=\"_blank\">conjugation pattern</a>, the time complexity of this function is $2N$, where N is the number of control qubits.</p>\n<blockquote>\n<p>Notice that the input state in the demo above is an equal superposition of all basis states.\nAfter applying the oracle the absolute values of all amplitudes are the same, but the states $|010\\rangle$ and $|101\\rangle$ had their phase flipped to negative!<br>Recall that these two states are exactly the inputs for which $f(x) = 1$, thus they are exactly the two states we expect to experience a phase flip!</p>\n</blockquote>\n<p>In the next exercise you will implement the classical oracle that you've implemented in the first exercise, this time as a quantum phase oracle $U_{7,\\text{phase}}$ that encodes the number 7.</p>\n",
|
|
1954
|
+
"asMarkdown": "\nWe introduced the function <a href=\"https://learn.microsoft.com/en-us/qsharp/api/qsharp/microsoft.quantum.canon.applycontrolledonbitstring\" target=\"_blank\">`ApplyControlledOnBitString`</a> provided by the Q# Standard library.\nIt defines a variant of a gate controlled on a state specified by a bit mask; for example, bit mask `[true, false]` means that the gate should be applied only if the two control qubits are in the $|10\\rangle$ state.\n\nThe sequence of steps that implement this variant are:\n\n1. Apply the $X$ gate to each control qubit that corresponds to a `false` element of the bit mask. After this, if the control qubits started in the $|10\\rangle$ state, they'll end up in the $|11\\rangle$ state, and if they started in any other state, they'll end up in any state but $|11\\rangle$.\n2. Apply the regular controlled version of the gate.\n3. Apply the $X$ gate to the same qubits to return them to their original state.\n\nDue to this <a href=\"https://learn.microsoft.com/en-us/azure/quantum/user-guide/language/statements/conjugations\" target=\"_blank\">conjugation pattern</a>, the time complexity of this function is $2N$, where N is the number of control qubits.\n\n> Notice that the input state in the demo above is an equal superposition of all basis states.\nAfter applying the oracle the absolute values of all amplitudes are the same, but the states $|010\\rangle$ and $|101\\rangle$ had their phase flipped to negative! \n> Recall that these two states are exactly the inputs for which $f(x) = 1$, thus they are exactly the two states we expect to experience a phase flip!\n\nIn the next exercise you will implement the classical oracle that you've implemented in the first exercise, this time as a quantum phase oracle $U_{7,\\text{phase}}$ that encodes the number 7."
|
|
1955
1955
|
}
|
|
1956
1956
|
]
|
|
1957
1957
|
},
|
|
@@ -1965,9 +1965,9 @@ export default {
|
|
|
1965
1965
|
"asMarkdown": "**Input:**\n 3 qubits in an arbitrary state $|x\\rangle$ (input/query register).\n\n**Goal:**\n\nFlip the sign of the input state $|x\\rangle$ if the input register is in\nthe state $|111\\rangle$ (encoding the integer $7$), and leave the input register unchanged otherwise. \nDon't allocate extra qubits to perform this operation.\n\n**Examples:**\n\n* If the query register is in the state $|111\\rangle$, flip its sign.\n* If the query register is in the state $|010\\rangle$ or $|101\\rangle$, do nothing.\n\n<details>\n <summary><b>Need a hint?</b></summary>\n To solve this problem, you need to find a gate that will only flip the sign of the $|111\\rangle$ basis state. Which single-qubit gate flips the sign of the basis state $|1\\rangle$ but not $|0\\rangle$? How can you modify this gate to solve this problem?\n</details>\n"
|
|
1966
1966
|
},
|
|
1967
1967
|
"sourceIds": [
|
|
1968
|
+
"oracles__phase_oracle_seven__Verification.qs",
|
|
1968
1969
|
"KatasLibrary.qs",
|
|
1969
|
-
"
|
|
1970
|
-
"oracles__phase_oracle_seven__verification.qs"
|
|
1970
|
+
"oracles__Common.qs"
|
|
1971
1971
|
],
|
|
1972
1972
|
"placeholderCode": "namespace Kata {\n operation IsSeven_PhaseOracle(x : Qubit[]) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}\n",
|
|
1973
1973
|
"explainedSolution": {
|
|
@@ -1993,8 +1993,8 @@ export default {
|
|
|
1993
1993
|
"items": [
|
|
1994
1994
|
{
|
|
1995
1995
|
"type": "text-content",
|
|
1996
|
-
"asHtml": "<p>A marking oracle $U_{mark}$ is an oracle that encodes the value of the classical function $f$ it implements in the <em>amplitude</em> of the qubit state. When provided an input array of qubits in the basis state $|\\vec{x}\\rangle$ and an output qubit in the basis state $|y\\rangle$, it flips the state of the output qubit if $f(x)=1$. (You can also represent this as addition modulo 2 between $f(x)$ and $y$.) Hence $U_{mark}$ is an operator that performs the following operation:</p>\n<p>$$U_{mark}|\\vec{x}\\rangle |y\\rangle = U_{mark}\\big(|\\vec{x}\\rangle \\otimes |y\\rangle\\big) = |\\vec{x}\\rangle \\otimes |y \\oplus f(x)\\rangle$$</p>\n<p>Again, since all quantum operations are linear, you can figure out the effect of this operation on superposition state knowing its effect on the basis states using its linearity
|
|
1997
|
-
"asMarkdown": "\nA marking oracle $U_{mark}$ is an oracle that encodes the value of the classical function $f$ it implements in the *amplitude* of the qubit state. When provided an input array of qubits in the basis state $|\\vec{x}\\rangle$ and an output qubit in the basis state $|y\\rangle$, it flips the state of the output qubit if $f(x)=1$. (You can also represent this as addition modulo 2 between $f(x)$ and $y$.) Hence $U_{mark}$ is an operator that performs the following operation:\n\n$$U_{mark}|\\vec{x}\\rangle |y\\rangle = U_{mark}\\big(|\\vec{x}\\rangle \\otimes |y\\rangle\\big) = |\\vec{x}\\rangle \\otimes |y \\oplus f(x)\\rangle$$\n\nAgain, since all quantum operations are linear, you can figure out the effect of this operation on superposition state knowing its effect on the basis states using its linearity
|
|
1996
|
+
"asHtml": "<p>A marking oracle $U_{mark}$ is an oracle that encodes the value of the classical function $f$ it implements in the <em>amplitude</em> of the qubit state. When provided an input array of qubits in the basis state $|\\vec{x}\\rangle$ and an output qubit in the basis state $|y\\rangle$, it flips the state of the output qubit if $f(x)=1$. (You can also represent this as addition modulo 2 between $f(x)$ and $y$.) Hence $U_{mark}$ is an operator that performs the following operation:</p>\n<p>$$U_{mark}|\\vec{x}\\rangle |y\\rangle = U_{mark}\\big(|\\vec{x}\\rangle \\otimes |y\\rangle\\big) = |\\vec{x}\\rangle \\otimes |y \\oplus f(x)\\rangle$$</p>\n<p>Again, since all quantum operations are linear, you can figure out the effect of this operation on superposition state knowing its effect on the basis states using its linearity.</p>\n<p>A marking oracle has distinct "input" and "output" qubits, but in general the effect of the oracle application is the change in the state of the whole system rather than of the "output" qubits only. We will look at this closer in a moment.</p>\n<h2>Marking Oracle for Alternating Bit Pattern Function</h2>\n<p>Consider the function $f(x)$ that takes $3$ bits of input and returns $1$ if $x=101$ or $x=010$, and $0$ otherwise (it is the same function we've seen in the demo "Phase oracle for alternating bit pattern function").</p>\n<p>The marking oracle that implements this function will take an array of 3 qubits as an "input" register and an "output" qubit, and will flip the state of the output qubit if the input qubit was in basis state $|101\\rangle$ or $|010\\rangle$, and do nothing otherwise. Let's see the effect of this oracle on a superposition state.</p>\n",
|
|
1997
|
+
"asMarkdown": "\nA marking oracle $U_{mark}$ is an oracle that encodes the value of the classical function $f$ it implements in the *amplitude* of the qubit state. When provided an input array of qubits in the basis state $|\\vec{x}\\rangle$ and an output qubit in the basis state $|y\\rangle$, it flips the state of the output qubit if $f(x)=1$. (You can also represent this as addition modulo 2 between $f(x)$ and $y$.) Hence $U_{mark}$ is an operator that performs the following operation:\n\n$$U_{mark}|\\vec{x}\\rangle |y\\rangle = U_{mark}\\big(|\\vec{x}\\rangle \\otimes |y\\rangle\\big) = |\\vec{x}\\rangle \\otimes |y \\oplus f(x)\\rangle$$\n\nAgain, since all quantum operations are linear, you can figure out the effect of this operation on superposition state knowing its effect on the basis states using its linearity.\n\nA marking oracle has distinct \"input\" and \"output\" qubits, but in general the effect of the oracle application is the change in the state of the whole system rather than of the \"output\" qubits only. We will look at this closer in a moment.\n\n## Marking Oracle for Alternating Bit Pattern Function\n\nConsider the function $f(x)$ that takes $3$ bits of input and returns $1$ if $x=101$ or $x=010$, and $0$ otherwise (it is the same function we've seen in the demo \"Phase oracle for alternating bit pattern function\").\n\nThe marking oracle that implements this function will take an array of 3 qubits as an \"input\" register and an \"output\" qubit, and will flip the state of the output qubit if the input qubit was in basis state $|101\\rangle$ or $|010\\rangle$, and do nothing otherwise. Let's see the effect of this oracle on a superposition state."
|
|
1998
1998
|
},
|
|
1999
1999
|
{
|
|
2000
2000
|
"type": "example",
|
|
@@ -2003,8 +2003,8 @@ export default {
|
|
|
2003
2003
|
},
|
|
2004
2004
|
{
|
|
2005
2005
|
"type": "text-content",
|
|
2006
|
-
"asHtml": "<blockquote>\n<p>Let's compare the initial state to the final state from the above demo
|
|
2007
|
-
"asMarkdown": "\n> Let's compare the initial state to the final state from the above demo
|
|
2006
|
+
"asHtml": "<blockquote>\n<p>Let's compare the initial state to the final state from the above demo.\nIn the initial state we had a tensor product of an equal superposition of all 3-qubit basis states and the state $|0\\rangle$. In the final state, this is no longer the case.\nThe basis states $|010\\rangle \\otimes |0\\rangle$ and $|101\\rangle \\otimes |0\\rangle$ no longer have non-zero amplitudes, and instead $|010\\rangle \\otimes |1\\rangle$ and $|101\\rangle \\otimes |1\\rangle$ have non-zero amplitudes.</p>\n<p>This is exactly the result that we expect. Recall our function $f(x)$: $f(x)=1$ if and only if $x=010$ or $x=101$. The first three qubits (variable <code>x</code>) represent the input state $|x\\rangle$, and the last qubit (variable <code>y</code>) represents the output state $|y\\rangle$. Thus when we have the two basis states, $|x\\rangle=|010\\rangle$ or $|x\\rangle=|101\\rangle$, we will flip the state of the qubit $|y\\rangle$, causing these two initial states to be tensored with $|1\\rangle$ in the final state where originally they were tensored with $|0\\rangle$.</p>\n<p>Since the rest of the basis states correspond to $f(x) = 0$, all other basis states in the initial superposition remain unchanged.</p>\n</blockquote>\n<p>Now you will implement the same function you've seen in the first two exercises as a marking oracle $U_{7,mark}$.</p>\n",
|
|
2007
|
+
"asMarkdown": "\n> Let's compare the initial state to the final state from the above demo.\nIn the initial state we had a tensor product of an equal superposition of all 3-qubit basis states and the state $|0\\rangle$. In the final state, this is no longer the case.\nThe basis states $|010\\rangle \\otimes |0\\rangle$ and $|101\\rangle \\otimes |0\\rangle$ no longer have non-zero amplitudes, and instead $|010\\rangle \\otimes |1\\rangle$ and $|101\\rangle \\otimes |1\\rangle$ have non-zero amplitudes.\n>\n> This is exactly the result that we expect. Recall our function $f(x)$: $f(x)=1$ if and only if $x=010$ or $x=101$. The first three qubits (variable `x`) represent the input state $|x\\rangle$, and the last qubit (variable `y`) represents the output state $|y\\rangle$. Thus when we have the two basis states, $|x\\rangle=|010\\rangle$ or $|x\\rangle=|101\\rangle$, we will flip the state of the qubit $|y\\rangle$, causing these two initial states to be tensored with $|1\\rangle$ in the final state where originally they were tensored with $|0\\rangle$.\n>\n> Since the rest of the basis states correspond to $f(x) = 0$, all other basis states in the initial superposition remain unchanged.\n\nNow you will implement the same function you've seen in the first two exercises as a marking oracle $U_{7,mark}$."
|
|
2008
2008
|
}
|
|
2009
2009
|
]
|
|
2010
2010
|
},
|
|
@@ -2018,9 +2018,9 @@ export default {
|
|
|
2018
2018
|
"asMarkdown": "**Inputs:**\n\n 1. 3 qubits in an arbitrary state $|x\\rangle$ (input/query register)\n \n 2. A qubit in an arbitrary state $|y\\rangle$ (target qubit)\n\n**Goal:**\n\nFlip the state of $|y\\rangle$ if the input register is in the \nstate $|111\\rangle$, and leave the state $|y\\rangle$ unchanged otherwise.\n\n**Examples:**\n\n* If the query register is in the state $|111\\rangle$, flip the state of the target qubit $|y\\rangle$.\n* If the query register is in the state $|010\\rangle$ or $|101\\rangle$, do nothing.\n"
|
|
2019
2019
|
},
|
|
2020
2020
|
"sourceIds": [
|
|
2021
|
+
"oracles__marking_oracle_seven__Verification.qs",
|
|
2021
2022
|
"KatasLibrary.qs",
|
|
2022
|
-
"
|
|
2023
|
-
"oracles__marking_oracle_seven__verification.qs"
|
|
2023
|
+
"oracles__Common.qs"
|
|
2024
2024
|
],
|
|
2025
2025
|
"placeholderCode": "namespace Kata {\n operation IsSeven_MarkingOracle(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}\n",
|
|
2026
2026
|
"explainedSolution": {
|
|
@@ -2046,8 +2046,8 @@ export default {
|
|
|
2046
2046
|
"items": [
|
|
2047
2047
|
{
|
|
2048
2048
|
"type": "text-content",
|
|
2049
|
-
"asHtml": "<p>Previously we considered applying marking oracles when the register $|x\\rangle$ was in a basis state or a superposition state, and the target qubit $|y\\rangle$ in a basis state. How might the effect of applying marking oracles change if the target is also in a superposition state? In this case we will observe <strong>phase kickback</strong> - the relative phase from the target qubit affecting ("kicked back" into) the state of the input qubits.</p>\n<p>In order to observe phase kickback, we use the target qubit $|y\\rangle=|-\\rangle$.</p>\n<blockquote>\n<p>This is the standard choice for two reasons
|
|
2050
|
-
"asMarkdown": "\nPreviously we considered applying marking oracles when the register $|x\\rangle$ was in a basis state or a superposition state, and the target qubit $|y\\rangle$ in a basis state. How might the effect of applying marking oracles change if the target is also in a superposition state? In this case we will observe **phase kickback** - the relative phase from the target qubit affecting (\"kicked back\" into) the state of the input qubits.\n\nIn order to observe phase kickback, we use the target qubit $|y\\rangle=|-\\rangle$.\n\n> This is the standard choice for two reasons
|
|
2049
|
+
"asHtml": "<p>Previously we considered applying marking oracles when the register $|x\\rangle$ was in a basis state or a superposition state, and the target qubit $|y\\rangle$ in a basis state. How might the effect of applying marking oracles change if the target is also in a superposition state? In this case we will observe <strong>phase kickback</strong> - the relative phase from the target qubit affecting ("kicked back" into) the state of the input qubits.</p>\n<p>In order to observe phase kickback, we use the target qubit $|y\\rangle=|-\\rangle$.</p>\n<blockquote>\n<p>This is the standard choice for two reasons.\nFirst, for phase kickback to occur, the target qubit must have a difference in relative phase between the basis states $|0\\rangle$ and $|1\\rangle$.\nSecond, the target qubit must be in an equal superposition, otherwise it will become entangled with the input register.</p>\n</blockquote>\n<p>Let's see the results of applying a marking oracle $U_{mark}$ which implements the function $f(x)$ to the input register $|x\\rangle$ and the target qubit in state $|-\\rangle$:</p>\n<ul>\n<li>If the input register $|x\\rangle$ is in a basis state:</li>\n</ul>\n<p>$$U_{mark} |x\\rangle |-\\rangle = \\frac1{\\sqrt2} \\big(U_{mark}|x\\rangle|0\\rangle - U_{mark}|x\\rangle |1\\rangle\\big)$$</p>\n<p>$$= \\frac1{\\sqrt2} \\big(|x\\rangle|0\\oplus f(x)\\rangle - |x\\rangle |1\\oplus f(x)\\rangle\\big)$$</p>\n<p>$$=\n\\begin{cases}\n\\frac1{\\sqrt2} \\big(|x\\rangle|0\\rangle - |x\\rangle |1\\rangle\\big) = |x\\rangle|-\\rangle \\text{ if } f(x) = 0 \\<br>\\frac1{\\sqrt2} \\big(|x\\rangle|1\\rangle - |x\\rangle |0\\rangle\\big) = -|x\\rangle|-\\rangle \\text{ if } f(x) = 1\n\\end{cases}\n$$</p>\n<p>$$= (-1)^{f(x)}|x\\rangle |-\\rangle$$</p>\n<ul>\n<li>If the input register is in a superposition state, say $|x\\rangle = \\frac1{\\sqrt2} \\big(|b_1\\rangle + |b_2\\rangle\\big)$, where $|b_1\\rangle$ and $|b_2\\rangle$ are basis states:</li>\n</ul>\n<p>$$U_{mark} |x\\rangle |-\\rangle = U_{mark} \\frac{1}{\\sqrt{2}} \\big(|b_1\\rangle + |b_2\\rangle\\big) |-\\rangle$$</p>\n<p>$$= \\frac{1}{\\sqrt{2}} \\big( U_{mark}|b_1\\rangle|-\\rangle + U_{mark}|b_2\\rangle|-\\rangle\\big)$$</p>\n<p>$$= \\frac{1}{\\sqrt{2}} \\big( (-1)^{f(b_1)}|b_1\\rangle + (-1)^{f(b_2)}|b_2\\rangle\\big) |-\\rangle$$</p>\n<p>We see that in both cases applying $U_{mark}$ does not change the state of the target qubit, but it does change the state of the input register.\nThus we can drop the target qubit without any repercussions after the application of the oracle.\nNotice that the input register is now in the following state:\n$$|\\psi\\rangle = \\frac{1}{\\sqrt{2}} \\big( (-1)^{f(b_1)}|b_1\\rangle + (-1)^{f(b_2)}|b_2\\rangle\\big),$$</p>\n<p>which looks exactly as if we applied a phase oracle to $|x\\rangle$ instead of applying a marking oracle to $|x\\rangle|-\\rangle$! This is a very important application of phase kickback: it allows to convert a marking oracle into a phase oracle - which you will implement in the next task.</p>\n<blockquote>\n<p>Another important application of this effect is <strong>phase estimation</strong> algorithm, which allows to estimate an eigenvalue of an eigenvector.</p>\n</blockquote>\n<p>Consider the following example using the $U_{7,mark}$ oracle. Let's begin with $|x\\rangle$ as an equal superposition of the $6$ and $7$ basis states and $|y\\rangle=|-\\rangle$, the overall state is:</p>\n<p>$$|\\eta\\rangle = \\Big[\\frac{1}{\\sqrt{2}}\\big(|110\\rangle + |111\\rangle\\big)\\Big] \\otimes \\frac{1}{\\sqrt{2}}\\big(|0\\rangle - |1\\rangle\\big)$$</p>\n<p>$$= \\frac{1}{2} \\big(|110\\rangle|0\\rangle + |111\\rangle|0\\rangle - |110\\rangle|1\\rangle - |111\\rangle|1\\rangle\\big)$$</p>\n<p>How does $U_{7,mark}$ act on this state?</p>\n<p>$$U_{7,mark}|\\eta\\rangle = U_{7,mark} \\frac{1}{2} \\big(|110\\rangle|0\\rangle + |111\\rangle|0\\rangle - |110\\rangle|1\\rangle - |111\\rangle|1\\rangle \\big)$$</p>\n<p>$$= \\frac{1}{2} \\big( U_{7,mark}|110\\rangle|0\\rangle + U_{7,mark}|111\\rangle|0\\rangle - U_{7,mark}|110\\rangle|1\\rangle - U_{7,mark}|111\\rangle|1\\rangle \\big)$$</p>\n<p>$$= \\frac{1}{2} \\big(|110\\rangle|0\\rangle + |111\\rangle|1\\rangle - |110\\rangle|1\\rangle - |111\\rangle|0\\rangle \\big) := |\\xi\\rangle$$</p>\n<p>Now we would like to observe how our input state $|\\eta\\rangle$ was modified by the oracle. Let's simplify the resulting state $|\\xi\\rangle$:</p>\n<p>$$|\\xi\\rangle = \\frac{1}{2} \\big(|110\\rangle|0\\rangle + |111\\rangle|1\\rangle - |110\\rangle|1\\rangle - |111\\rangle|0\\rangle\\big)$$</p>\n<p>$$= \\frac{1}{2} \\big(|110\\rangle|0\\rangle - |110\\rangle|1\\rangle - |111\\rangle|0\\rangle + |111\\rangle|1\\rangle \\big)$$</p>\n<p>$$= \\frac{1}{2} \\Big[|110\\rangle \\otimes \\big(|0\\rangle - |1\\rangle \\big) + |111\\rangle \\otimes \\big(|1\\rangle - |0\\rangle\\big)\\Big]$$</p>\n<p>$$= \\Big[\\frac{1}{\\sqrt{2}} \\big( |110\\rangle - |111\\rangle \\big) \\Big] \\otimes \\Big[ \\frac{1}{\\sqrt{2}} \\big( |0\\rangle - |1\\rangle \\big) \\Big]$$</p>\n<p>$$= \\Big[\\frac{1}{\\sqrt{2}} \\big( |110\\rangle - |111\\rangle \\big) \\Big] \\otimes |-\\rangle$$</p>\n<p>Finally, let's compare $|\\eta\\rangle$ and $|\\xi\\rangle$; below are the final equations repeated for your convenience:\n$$|\\eta\\rangle = \\Big[\\frac{1}{\\sqrt{2}}\\big(|110\\rangle + |111\\rangle\\big)\\Big] \\otimes |-\\rangle$$\n$$|\\xi\\rangle = \\Big[\\frac{1}{\\sqrt{2}}\\big(|110\\rangle - |111\\rangle\\big)\\Big] \\otimes |-\\rangle$$</p>\n<p>We can see that these two equations are identical, except for the $-1$ phase that appeared on the $|111\\rangle$ basis state (representing $7$). This is a specific example of the phase kickback effect, as the phase from $|-\\rangle$ has been <em>kicked back</em> into $|x\\rangle$.</p>\n<h2>🔎 Analyze</h2>\n<p><strong>Distinguishing states</strong></p>\n<p>How could we distinguish the states $|\\eta\\rangle = |11+\\rangle |-\\rangle$ and $|\\xi\\rangle = |11-\\rangle |-\\rangle$? Take a moment to think.</p>\n<details>\n<summary><b>Solution</b></summary>\nRecall that we can only observe alterations to out input state by performing a measurement.\nIf we apply Hadamard gate to the third qubit, we will be able to distinguish between the input state and the output state.\n $$(I\\otimes I \\otimes H)|11+\\rangle = |110\\rangle \\\\ (I\\otimes I \\otimes H)|11-\\rangle = |111\\rangle$$\nNow if we were to measure the third qubit, we'll be able to distinguish the starting state and the state after phase kickback occurred.\n</details>",
|
|
2050
|
+
"asMarkdown": "\nPreviously we considered applying marking oracles when the register $|x\\rangle$ was in a basis state or a superposition state, and the target qubit $|y\\rangle$ in a basis state. How might the effect of applying marking oracles change if the target is also in a superposition state? In this case we will observe **phase kickback** - the relative phase from the target qubit affecting (\"kicked back\" into) the state of the input qubits.\n\nIn order to observe phase kickback, we use the target qubit $|y\\rangle=|-\\rangle$.\n\n> This is the standard choice for two reasons.\n> First, for phase kickback to occur, the target qubit must have a difference in relative phase between the basis states $|0\\rangle$ and $|1\\rangle$.\n> Second, the target qubit must be in an equal superposition, otherwise it will become entangled with the input register.\n\nLet's see the results of applying a marking oracle $U_{mark}$ which implements the function $f(x)$ to the input register $|x\\rangle$ and the target qubit in state $|-\\rangle$:\n\n- If the input register $|x\\rangle$ is in a basis state:\n\n$$U_{mark} |x\\rangle |-\\rangle = \\frac1{\\sqrt2} \\big(U_{mark}|x\\rangle|0\\rangle - U_{mark}|x\\rangle |1\\rangle\\big)$$\n\n$$= \\frac1{\\sqrt2} \\big(|x\\rangle|0\\oplus f(x)\\rangle - |x\\rangle |1\\oplus f(x)\\rangle\\big)$$\n\n$$=\n\\begin{cases}\n\\frac1{\\sqrt2} \\big(|x\\rangle|0\\rangle - |x\\rangle |1\\rangle\\big) = |x\\rangle|-\\rangle \\text{ if } f(x) = 0 \\\\\\\n\\frac1{\\sqrt2} \\big(|x\\rangle|1\\rangle - |x\\rangle |0\\rangle\\big) = -|x\\rangle|-\\rangle \\text{ if } f(x) = 1\n\\end{cases}\n$$\n\n$$= (-1)^{f(x)}|x\\rangle |-\\rangle$$\n\n- If the input register is in a superposition state, say $|x\\rangle = \\frac1{\\sqrt2} \\big(|b_1\\rangle + |b_2\\rangle\\big)$, where $|b_1\\rangle$ and $|b_2\\rangle$ are basis states:\n\n$$U_{mark} |x\\rangle |-\\rangle = U_{mark} \\frac{1}{\\sqrt{2}} \\big(|b_1\\rangle + |b_2\\rangle\\big) |-\\rangle$$\n\n$$= \\frac{1}{\\sqrt{2}} \\big( U_{mark}|b_1\\rangle|-\\rangle + U_{mark}|b_2\\rangle|-\\rangle\\big)$$\n\n$$= \\frac{1}{\\sqrt{2}} \\big( (-1)^{f(b_1)}|b_1\\rangle + (-1)^{f(b_2)}|b_2\\rangle\\big) |-\\rangle$$\n\nWe see that in both cases applying $U_{mark}$ does not change the state of the target qubit, but it does change the state of the input register.\nThus we can drop the target qubit without any repercussions after the application of the oracle.\nNotice that the input register is now in the following state:\n$$|\\psi\\rangle = \\frac{1}{\\sqrt{2}} \\big( (-1)^{f(b_1)}|b_1\\rangle + (-1)^{f(b_2)}|b_2\\rangle\\big),$$\n\nwhich looks exactly as if we applied a phase oracle to $|x\\rangle$ instead of applying a marking oracle to $|x\\rangle|-\\rangle$! This is a very important application of phase kickback: it allows to convert a marking oracle into a phase oracle - which you will implement in the next task.\n\n> Another important application of this effect is **phase estimation** algorithm, which allows to estimate an eigenvalue of an eigenvector.\n\nConsider the following example using the $U_{7,mark}$ oracle. Let's begin with $|x\\rangle$ as an equal superposition of the $6$ and $7$ basis states and $|y\\rangle=|-\\rangle$, the overall state is:\n\n$$|\\eta\\rangle = \\Big[\\frac{1}{\\sqrt{2}}\\big(|110\\rangle + |111\\rangle\\big)\\Big] \\otimes \\frac{1}{\\sqrt{2}}\\big(|0\\rangle - |1\\rangle\\big)$$\n\n$$= \\frac{1}{2} \\big(|110\\rangle|0\\rangle + |111\\rangle|0\\rangle - |110\\rangle|1\\rangle - |111\\rangle|1\\rangle\\big)$$\n\nHow does $U_{7,mark}$ act on this state?\n\n$$U_{7,mark}|\\eta\\rangle = U_{7,mark} \\frac{1}{2} \\big(|110\\rangle|0\\rangle + |111\\rangle|0\\rangle - |110\\rangle|1\\rangle - |111\\rangle|1\\rangle \\big)$$\n\n$$= \\frac{1}{2} \\big( U_{7,mark}|110\\rangle|0\\rangle + U_{7,mark}|111\\rangle|0\\rangle - U_{7,mark}|110\\rangle|1\\rangle - U_{7,mark}|111\\rangle|1\\rangle \\big)$$\n\n$$= \\frac{1}{2} \\big(|110\\rangle|0\\rangle + |111\\rangle|1\\rangle - |110\\rangle|1\\rangle - |111\\rangle|0\\rangle \\big) := |\\xi\\rangle$$\n\nNow we would like to observe how our input state $|\\eta\\rangle$ was modified by the oracle. Let's simplify the resulting state $|\\xi\\rangle$:\n\n$$|\\xi\\rangle = \\frac{1}{2} \\big(|110\\rangle|0\\rangle + |111\\rangle|1\\rangle - |110\\rangle|1\\rangle - |111\\rangle|0\\rangle\\big)$$\n\n$$= \\frac{1}{2} \\big(|110\\rangle|0\\rangle - |110\\rangle|1\\rangle - |111\\rangle|0\\rangle + |111\\rangle|1\\rangle \\big)$$\n\n$$= \\frac{1}{2} \\Big[|110\\rangle \\otimes \\big(|0\\rangle - |1\\rangle \\big) + |111\\rangle \\otimes \\big(|1\\rangle - |0\\rangle\\big)\\Big]$$\n\n$$= \\Big[\\frac{1}{\\sqrt{2}} \\big( |110\\rangle - |111\\rangle \\big) \\Big] \\otimes \\Big[ \\frac{1}{\\sqrt{2}} \\big( |0\\rangle - |1\\rangle \\big) \\Big]$$\n\n$$= \\Big[\\frac{1}{\\sqrt{2}} \\big( |110\\rangle - |111\\rangle \\big) \\Big] \\otimes |-\\rangle$$\n\nFinally, let's compare $|\\eta\\rangle$ and $|\\xi\\rangle$; below are the final equations repeated for your convenience:\n$$|\\eta\\rangle = \\Big[\\frac{1}{\\sqrt{2}}\\big(|110\\rangle + |111\\rangle\\big)\\Big] \\otimes |-\\rangle$$\n$$|\\xi\\rangle = \\Big[\\frac{1}{\\sqrt{2}}\\big(|110\\rangle - |111\\rangle\\big)\\Big] \\otimes |-\\rangle$$\n\nWe can see that these two equations are identical, except for the $-1$ phase that appeared on the $|111\\rangle$ basis state (representing $7$). This is a specific example of the phase kickback effect, as the phase from $|-\\rangle$ has been *kicked back* into $|x\\rangle$.\n\n## 🔎 Analyze\n\n**Distinguishing states**\n\nHow could we distinguish the states $|\\eta\\rangle = |11+\\rangle |-\\rangle$ and $|\\xi\\rangle = |11-\\rangle |-\\rangle$? Take a moment to think.\n\n<details>\n<summary><b>Solution</b></summary>\nRecall that we can only observe alterations to out input state by performing a measurement.\nIf we apply Hadamard gate to the third qubit, we will be able to distinguish between the input state and the output state.\n $$(I\\otimes I \\otimes H)|11+\\rangle = |110\\rangle \\\\ (I\\otimes I \\otimes H)|11-\\rangle = |111\\rangle$$\nNow if we were to measure the third qubit, we'll be able to distinguish the starting state and the state after phase kickback occurred.\n</details>"
|
|
2051
2051
|
}
|
|
2052
2052
|
]
|
|
2053
2053
|
},
|
|
@@ -2061,9 +2061,9 @@ export default {
|
|
|
2061
2061
|
"asMarkdown": "**Inputs:**\n\n 1. A marking oracle implementing an unknown $N$-bit function $f(x)$.\n 2. $N$ qubits in an arbitrary state (input/query register).\n \n**Goal:**\n\nFlip the phase of each basis state $|x\\rangle$ for which $f(x) = 1$. You can only access $f(x)$ via the marking oracle you are given.\n\n<br/>\n<details>\n <summary><b>Need a hint?</b></summary>\n Recall that you can allocate extra qubits to assist in this operation. Is there a state that you could prepare with an auxiliary qubit which would help you to convert the marking oracle to a phase oracle?\n</details>\n"
|
|
2062
2062
|
},
|
|
2063
2063
|
"sourceIds": [
|
|
2064
|
+
"oracles__marking_oracle_as_phase__Verification.qs",
|
|
2064
2065
|
"KatasLibrary.qs",
|
|
2065
|
-
"
|
|
2066
|
-
"oracles__marking_oracle_as_phase__verification.qs"
|
|
2066
|
+
"oracles__Common.qs"
|
|
2067
2067
|
],
|
|
2068
2068
|
"placeholderCode": "namespace Kata {\n operation ApplyMarkingOracleAsPhaseOracle(\n markingOracle : ((Qubit[], Qubit) => Unit is Adj + Ctl),\n qubits : Qubit[])\n : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}\n",
|
|
2069
2069
|
"explainedSolution": {
|
|
@@ -2094,8 +2094,8 @@ export default {
|
|
|
2094
2094
|
},
|
|
2095
2095
|
{
|
|
2096
2096
|
"type": "text-content",
|
|
2097
|
-
"asHtml": "<blockquote>\n<p>Notice from the above demo that your phase oracle $U_{7,phase}$ behaves the same as the converted version of your marking oracle $U_{7,mark}$, both of which induce a phase flip on the basis state $|111\\rangle$!</p>\n</blockquote>\n<p>This way to convert a marking oracle to a phase oracle is useful because many quantum algorithms, such as Grover's search algorithm, rely on a phase oracle, but it is often easier to implement the function as a marking oracle
|
|
2098
|
-
"asMarkdown": "\n> Notice from the above demo that your phase oracle $U_{7,phase}$ behaves the same as the converted version of your marking oracle $U_{7,mark}$, both of which induce a phase flip on the basis state $|111\\rangle$!\n\nThis way to convert a marking oracle to a phase oracle is useful because many quantum algorithms, such as Grover's search algorithm, rely on a phase oracle, but it is often easier to implement the function as a marking oracle
|
|
2097
|
+
"asHtml": "<blockquote>\n<p>Notice from the above demo that your phase oracle $U_{7,phase}$ behaves the same as the converted version of your marking oracle $U_{7,mark}$, both of which induce a phase flip on the basis state $|111\\rangle$!</p>\n</blockquote>\n<p>This way to convert a marking oracle to a phase oracle is useful because many quantum algorithms, such as Grover's search algorithm, rely on a phase oracle, but it is often easier to implement the function as a marking oracle.\nThis converter provides a way to implement the function of interest as a marking oracle and then convert it into a phase oracle, which could then be leveraged in a quantum algorithm.</p>\n",
|
|
2098
|
+
"asMarkdown": "\n> Notice from the above demo that your phase oracle $U_{7,phase}$ behaves the same as the converted version of your marking oracle $U_{7,mark}$, both of which induce a phase flip on the basis state $|111\\rangle$!\n\nThis way to convert a marking oracle to a phase oracle is useful because many quantum algorithms, such as Grover's search algorithm, rely on a phase oracle, but it is often easier to implement the function as a marking oracle.\nThis converter provides a way to implement the function of interest as a marking oracle and then convert it into a phase oracle, which could then be leveraged in a quantum algorithm."
|
|
2099
2099
|
}
|
|
2100
2100
|
]
|
|
2101
2101
|
},
|
|
@@ -2106,8 +2106,8 @@ export default {
|
|
|
2106
2106
|
"items": [
|
|
2107
2107
|
{
|
|
2108
2108
|
"type": "text-content",
|
|
2109
|
-
"asHtml": "<p>In this section you will implement a few more complicated quantum oracles
|
|
2110
|
-
"asMarkdown": "\nIn this section you will implement a few more complicated quantum oracles
|
|
2109
|
+
"asHtml": "<p>In this section you will implement a few more complicated quantum oracles.</p>\n<blockquote>\n<p>Notice that the operation declarations below require adjoint and controlled variants of the oracle to be automatically generated. This is common practice that makes testing and reusing the code easier. Typically Q# compiler will easily generate these variants, as long as you don't use mutable variables or operations that don't support these functors.</p>\n</blockquote>\n",
|
|
2110
|
+
"asMarkdown": "\nIn this section you will implement a few more complicated quantum oracles.\n\n> Notice that the operation declarations below require adjoint and controlled variants of the oracle to be automatically generated. This is common practice that makes testing and reusing the code easier. Typically Q# compiler will easily generate these variants, as long as you don't use mutable variables or operations that don't support these functors."
|
|
2111
2111
|
}
|
|
2112
2112
|
]
|
|
2113
2113
|
},
|
|
@@ -2121,9 +2121,9 @@ export default {
|
|
|
2121
2121
|
"asMarkdown": "**Inputs:**\n\n 1. $N$ qubits in an arbitrary state $|x\\rangle$ (input/query register).\n 2. A qubit in an arbitrary state $|y\\rangle$ (target qubit).\n\n**Goal:**\n\nFlip the state of $|y\\rangle$ if the input register is in any basis state\nexcept for $|00...0\\rangle$ (the all zero state).\n\n**Examples:**\n\n* If the query register is in the state $|10000001\\rangle$, $|11101101\\rangle$ or $|0010101\\rangle$, flip the state $|y\\rangle$.\n* If the query register is in the state $|000\\rangle$, do nothing.\n\n<br/>\n<details>\n <summary><b>Before implementing this oracle, answer the question: are you implementing a marking or a phase oracle?</b></summary>\n This is a marking oracle, because we are flipping the state of the target qubit $|y\\rangle$ based on the state of the input $|x\\rangle$.\n</details>\n\n<br/>\n<details>\n <summary><b>Need a hint?</b></summary>\n You need to flip the state of $|y\\rangle$ for every input except $|00...0\\rangle$, or, alternatively, flip it unconditionally and then flip it for the $|00...0\\rangle$ state. You may find the Q# library function <a href=\"https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.canon.controlledonint\" target=\"_blank\">ControlledOnInt</a> useful in your implementation.\n</details>\n"
|
|
2122
2122
|
},
|
|
2123
2123
|
"sourceIds": [
|
|
2124
|
+
"oracles__or_oracle__Verification.qs",
|
|
2124
2125
|
"KatasLibrary.qs",
|
|
2125
|
-
"
|
|
2126
|
-
"oracles__or_oracle__verification.qs"
|
|
2126
|
+
"oracles__Common.qs"
|
|
2127
2127
|
],
|
|
2128
2128
|
"placeholderCode": "namespace Kata {\n operation Or_Oracle(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}\n",
|
|
2129
2129
|
"explainedSolution": {
|
|
@@ -2152,9 +2152,9 @@ export default {
|
|
|
2152
2152
|
"asMarkdown": "**Inputs:**\n\n 1. $N$ qubits in an arbitrary state $|x\\rangle$ (input/query register).\n 2. An integer $k$ such that $0 \\leq k < N$.\n\n**Goal:**\n\nFlip the sign of the input state $|x\\rangle$ if the $k$-th bit of $x$ is $1$. \n**Implement this oracle without using auxiliary qubits.**\n\n**Examples:**\n\n* If the query register is in the state $|010\\rangle$ and $k=0$, do nothing.\n* If the query register is in the state $|010\\rangle$ and $k=1$, flip the sign of the basis state.\n\n<br/>\n<details>\n <summary><b>Before implementing this oracle, answer the question: are you implementing a marking or a phase oracle?</b></summary>\n This is a phase oracle, because we are changing the phase of the input state $|x\\rangle$ based on the value of the function $f(x)$.\n</details>\n"
|
|
2153
2153
|
},
|
|
2154
2154
|
"sourceIds": [
|
|
2155
|
+
"oracles__kth_bit_oracle__Verification.qs",
|
|
2155
2156
|
"KatasLibrary.qs",
|
|
2156
|
-
"
|
|
2157
|
-
"oracles__kth_bit_oracle__verification.qs"
|
|
2157
|
+
"oracles__Common.qs"
|
|
2158
2158
|
],
|
|
2159
2159
|
"placeholderCode": "namespace Kata {\n operation KthBit_Oracle(x : Qubit[], k : Int) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}\n",
|
|
2160
2160
|
"explainedSolution": {
|
|
@@ -2183,9 +2183,9 @@ export default {
|
|
|
2183
2183
|
"asMarkdown": "**Inputs:**\n\n 1. $N$ qubits in an arbitrary state $|x\\rangle$ (input/query register).\n 2. An integer $k$ such that $0 \\leq k < N$.\n\n**Goal:**\n\nFlip the sign of the basis state $|x\\rangle$ if any of the bits of $x$ (not considering the $k$-th bit) are $1$ in input register. In other words, the input register with the $k$-th qubit excluded should not be in the all zero state to flip the sign of the input register. The state of the $k$-th qubit does not affect the result.\n\nFeel free to explore implementing this operation with or without auxiliary qubits.\n\n**Examples:**\n\n* If the query register is in the state $|010\\rangle$ and $k=0$, flip the sign of the register.\n* If the query register is in the state $|010\\rangle$ and $k=1$, do nothing.\n\n<details>\n <summary><b>Before implementing this oracle, answer the question: are you implementing a marking or a phase oracle?</b></summary>\n This is a phase oracle, because we are changing the phase of the input state $|x\\rangle$ based on the value of the function $f(x)$.\n</details>\n\n<details>\n <summary><b>Need a hint?</b></summary>\n You can use the previously implemented oracles if needed by copying the code.\n <br/>\n You can use <a href=\"https://docs.microsoft.com/en-us/azure/quantum/user-guide/language/expressions/itemaccessexpressions\" target=\"_blank\">array slicing</a> to get parts of the array before and after the $k$-th element.\n</details>\n"
|
|
2184
2184
|
},
|
|
2185
2185
|
"sourceIds": [
|
|
2186
|
+
"oracles__or_but_kth_oracle__Verification.qs",
|
|
2186
2187
|
"KatasLibrary.qs",
|
|
2187
|
-
"
|
|
2188
|
-
"oracles__or_but_kth_oracle__verification.qs"
|
|
2188
|
+
"oracles__Common.qs"
|
|
2189
2189
|
],
|
|
2190
2190
|
"placeholderCode": "namespace Kata {\n operation OrOfBitsExceptKth_Oracle(x : Qubit[], k : Int)\n : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}\n",
|
|
2191
2191
|
"explainedSolution": {
|
|
@@ -2209,9 +2209,9 @@ export default {
|
|
|
2209
2209
|
"asMarkdown": "**Inputs:**\n\n 1. $N$ qubits in an arbitrary state $|x\\rangle$ (input/query register).\n 2. A qubit in an arbitrary state $|y\\rangle$ (target qubit).\n 3. A boolean array of length $N$ `pattern` representing a basis state; `true` and `false` elements correspond to $|1\\rangle$ and $|0\\rangle$, respectively.\n\n**Goal:**\n\nFlip the state of $|y\\rangle$ if the input register matches the basis state\nrepresented by `pattern`. \n\n**Examples:**\n\n* If the query register is in the state $|010\\rangle$ and `pattern = [false, true, false]`, flip the state $|y\\rangle$.\n* If the query register is in the state $|1001\\rangle$ and `pattern = [false, true, true, false]`, do nothing.\n \n<br/>\n<details>\n <summary><b>Before implementing this oracle, answer the question: are you implementing a marking or a phase oracle?</b></summary>\n This is a marking oracle, because we are flipping the state of the target qubit $|y\\rangle$ based on the state of the input $|x\\rangle$.\n</details>\n\n<br/>\n<details>\n <summary><b>Need a hint?</b></summary>\n You need to flip the state of $|y\\rangle$ if $|x\\rangle$ matches the given pattern. You may find the Q# library function <a href=\"https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.canon.controlledonbitstring\" target=\"_blank\">ControlledOnBitString</a> useful in your implementation.\n</details>\n"
|
|
2210
2210
|
},
|
|
2211
2211
|
"sourceIds": [
|
|
2212
|
+
"oracles__bit_pattern_oracle__Verification.qs",
|
|
2212
2213
|
"KatasLibrary.qs",
|
|
2213
|
-
"
|
|
2214
|
-
"oracles__bit_pattern_oracle__verification.qs"
|
|
2214
|
+
"oracles__Common.qs"
|
|
2215
2215
|
],
|
|
2216
2216
|
"placeholderCode": "namespace Kata {\n operation ArbitraryBitPattern_Oracle(x : Qubit[], y : Qubit, pattern : Bool[])\n : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}\n",
|
|
2217
2217
|
"explainedSolution": {
|
|
@@ -2235,9 +2235,9 @@ export default {
|
|
|
2235
2235
|
"asMarkdown": "**Inputs:**\n\n 1. $N$ qubits in an arbitrary state $|x\\rangle$ (input/query register).\n 2. A boolean array of length $N$ `pattern` representing a basis state; `true` and `false` elements correspond to $|1\\rangle$ and $|0\\rangle$, respectively.\n\n**Goal:**\n \nFlip the sign of the input state $|x\\rangle$ if the input register matches the basis state\nrepresented by `pattern`. \n**Implement this oracle without using auxiliary qubits**\n\n**Examples:**\n\n * If the query register is in the state $|010\\rangle$ and `pattern = [false, true, false]`, flip the sign of the input register.\n * If the query register is in the state $|1001\\rangle$ and `pattern = [false, true, true, false]`, do nothing.\n \n<br/>\n<details>\n <summary><b>Before implementing this oracle, answer the question: are you implementing a marking or a phase oracle?</b></summary>\n This is a phase oracle, because we are changing the phase of the input state $|x\\rangle$ based on the value of the function $f(x)$.\n</details>\n\n<br/>\n<details>\n <summary><b>Need a hint?</b></summary>\n Can you transform the state of the input register based on the <code>pattern</code> value so as to have to flip the phase only for the $|1...1\\rangle$ state?\n</details>\n"
|
|
2236
2236
|
},
|
|
2237
2237
|
"sourceIds": [
|
|
2238
|
+
"oracles__bit_pattern_challenge__Verification.qs",
|
|
2238
2239
|
"KatasLibrary.qs",
|
|
2239
|
-
"
|
|
2240
|
-
"oracles__bit_pattern_challenge__verification.qs"
|
|
2240
|
+
"oracles__Common.qs"
|
|
2241
2241
|
],
|
|
2242
2242
|
"placeholderCode": "namespace Kata {\n operation ArbitraryBitPattern_Oracle_Challenge(x : Qubit[], pattern : Bool[])\n : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}\n",
|
|
2243
2243
|
"explainedSolution": {
|
|
@@ -2261,9 +2261,9 @@ export default {
|
|
|
2261
2261
|
"asMarkdown": "Suppose that you would like to schedule a meeting with your co-worker Jasmine.\nYou both work five day workweeks, and $|x\\rangle$ and $|jasmine\\rangle$ are 5-bit states representing your and Jasmine's schedules.\nThe schedules are indicators of a person being busy on that day: a $1$ bit means that person is busy on that day, and $0$ means they're free for a meeting that day. Implement a function that determines if you and Jasmine can schedule a meeting during the week, i.e., whether there is a day when both schedules have a $0$ simultaneously.\n\n**Inputs:**\n\n 1. 5 qubits in an arbitrary state $|x\\rangle$ representing your schedule for the week (input/query register).\n 2. 5 qubits in an arbitrary state $|jasmine\\rangle$ representing Jasmine's schedule for the week (input/query register).\n 3. A qubit in an arbitrary state $|y\\rangle$ (target qubit).\n\n**Goal:**\n\nFlip the state of $|y\\rangle$ if you and Jasmine are both free on the same day for at least one day during the week. Recall that a $0$ means that a person is free on that day.\n\n**Examples:**\n\n* If $|x\\rangle=|10101\\rangle$ and $|jasmine\\rangle=|01010\\rangle$, do nothing (there is no day on which you both are free).\n* If $|x\\rangle=|10001\\rangle$ and $|jasmine\\rangle=|01010\\rangle$, flip the state $|y\\rangle$ (you are both free on Wednesday).\n* If $|x\\rangle=|00000\\rangle$ and $|jasmine\\rangle=|00000\\rangle$, flip the state $|y\\rangle$ (you are both free all week).\n* If $|x\\rangle=|11111\\rangle$ and $|jasmine\\rangle=|11111\\rangle$, do nothing (you are both busy all week).\n \n<br/>\n<details>\n <summary><b>Before implementing this oracle, answer the question: are you implementing a marking or a phase oracle?</b></summary>\n This is a marking oracle, because we are flipping the state of the target qubit $|y\\rangle$ based on the state of the inputs $|x\\rangle$ and $|jasmine\\rangle$. Notice that even though we do not have the typical single-input-register situation that we saw earlier, this is still a marking oracle.\n</details>\n"
|
|
2262
2262
|
},
|
|
2263
2263
|
"sourceIds": [
|
|
2264
|
+
"oracles__meeting_oracle__Verification.qs",
|
|
2264
2265
|
"KatasLibrary.qs",
|
|
2265
|
-
"
|
|
2266
|
-
"oracles__meeting_oracle__verification.qs"
|
|
2266
|
+
"oracles__Common.qs"
|
|
2267
2267
|
],
|
|
2268
2268
|
"placeholderCode": "namespace Kata {\n operation Meeting_Oracle(x : Qubit[], jasmine : Qubit[], y : Qubit)\n : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}\n",
|
|
2269
2269
|
"explainedSolution": {
|
|
@@ -2284,8 +2284,8 @@ export default {
|
|
|
2284
2284
|
"items": [
|
|
2285
2285
|
{
|
|
2286
2286
|
"type": "text-content",
|
|
2287
|
-
"asHtml": "<p>In this demo we show how you could test an oracle that you've implemented for your own problem
|
|
2288
|
-
"asMarkdown": "\nIn this demo we show how you could test an oracle that you've implemented for your own problem
|
|
2287
|
+
"asHtml": "<p>In this demo we show how you could test an oracle that you've implemented for your own problem.\nFor all of the previous oracles that you've implemented, we've been testing your oracle against a reference solution for that task.\nHowever, if you're designing an oracle for a new problem, you do not have a reference solution for it - if you did, there would be no point for you to program the oracle in the first place!</p>\n<p>A good way to test a quantum oracle of interest is to write a classical oracle that performs the same computation classically, and then compare the effect of your quantum oracle on the basis states with the output of the classical oracle for every input (or a lot of the inputs if you are constrained by runtime) to ensure that they match.</p>\n<p>Here we will compare the reference implementation of <code>Meeting_Oracle</code> to the classical code implementing the same function.</p>\n",
|
|
2288
|
+
"asMarkdown": "\nIn this demo we show how you could test an oracle that you've implemented for your own problem.\nFor all of the previous oracles that you've implemented, we've been testing your oracle against a reference solution for that task.\nHowever, if you're designing an oracle for a new problem, you do not have a reference solution for it - if you did, there would be no point for you to program the oracle in the first place!\n\nA good way to test a quantum oracle of interest is to write a classical oracle that performs the same computation classically, and then compare the effect of your quantum oracle on the basis states with the output of the classical oracle for every input (or a lot of the inputs if you are constrained by runtime) to ensure that they match.\n\nHere we will compare the reference implementation of `Meeting_Oracle` to the classical code implementing the same function."
|
|
2289
2289
|
},
|
|
2290
2290
|
{
|
|
2291
2291
|
"type": "example",
|
|
@@ -2302,7 +2302,7 @@ export default {
|
|
|
2302
2302
|
{
|
|
2303
2303
|
"type": "text-content",
|
|
2304
2304
|
"asHtml": "<p>Congratulations! In this kata you have learned to build quantum oracles. Here are a few key concepts to keep in mind:</p>\n<ul>\n<li>A quantum oracle is an "opaque box" operation that is used as input to another algorithm.</li>\n<li>Phase oracles encode the information in the relative phase of basis states. If $f(x)=0$, the oracle doesn't change the basis state $\\ket{x}$, and if $f(x)=1$ it multiplies the phase of the basis state $\\ket{x}$ by $-1$.</li>\n<li>Marking oracles use an extra qubit $\\ket{y}$ and encode the information in the state of that qubit. If $f(x)=0$, it doen't change the state of the qubit $\\ket{y}$ for the basis state $\\ket{x}$, and if $f(x)=1$ it flips the state of the qubit $\\ket{y}$ for the basis state $\\ket{x}$.</li>\n</ul>\n<p><strong>Next Steps</strong></p>\n<p>We hope you enjoyed this kata! If you're looking to learn more about quantum oracles and Q#, here are some suggestions:</p>\n<ul>\n<li>To learn about the Grover's algorithm, you can check <a href=\"https://learn.microsoft.com/en-us/training/modules/solve-graph-coloring-problems-grovers-search/\" target=\"_blank\">Microsoft Learn module "Solve graph coloring problems by using Grover's search"</a>.</li>\n<li>To learn more about the Q# libraries, you can check the <a href=\"https://learn.microsoft.com/en-us/azure/quantum/user-guide/libraries/?tabs=tabid-clivscode\" target=\"_blank\">The Q# user guide</a>.</li>\n</ul>\n",
|
|
2305
|
-
"asMarkdown": "\nCongratulations! In this kata you have learned to build quantum oracles. Here are a few key concepts to keep in mind:\n
|
|
2305
|
+
"asMarkdown": "\nCongratulations! In this kata you have learned to build quantum oracles. Here are a few key concepts to keep in mind:\n\n- A quantum oracle is an \"opaque box\" operation that is used as input to another algorithm.\n- Phase oracles encode the information in the relative phase of basis states. If $f(x)=0$, the oracle doesn't change the basis state $\\ket{x}$, and if $f(x)=1$ it multiplies the phase of the basis state $\\ket{x}$ by $-1$.\n- Marking oracles use an extra qubit $\\ket{y}$ and encode the information in the state of that qubit. If $f(x)=0$, it doen't change the state of the qubit $\\ket{y}$ for the basis state $\\ket{x}$, and if $f(x)=1$ it flips the state of the qubit $\\ket{y}$ for the basis state $\\ket{x}$.\n\n**Next Steps**\n\nWe hope you enjoyed this kata! If you're looking to learn more about quantum oracles and Q#, here are some suggestions:\n\n- To learn about the Grover's algorithm, you can check <a href=\"https://learn.microsoft.com/en-us/training/modules/solve-graph-coloring-problems-grovers-search/\" target=\"_blank\">Microsoft Learn module \"Solve graph coloring problems by using Grover's search\"</a>.\n- To learn more about the Q# libraries, you can check the <a href=\"https://learn.microsoft.com/en-us/azure/quantum/user-guide/libraries/?tabs=tabid-clivscode\" target=\"_blank\">The Q# user guide</a>."
|
|
2306
2306
|
}
|
|
2307
2307
|
]
|
|
2308
2308
|
}
|
|
@@ -2310,14 +2310,14 @@ export default {
|
|
|
2310
2310
|
}
|
|
2311
2311
|
],
|
|
2312
2312
|
"globalCodeSources": [
|
|
2313
|
-
{
|
|
2314
|
-
"id": "KatasLibrary.qs",
|
|
2315
|
-
"code": "// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nnamespace Microsoft.Quantum.Katas {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Intrinsic;\n\n /// # Summary\n /// Given two operations, checks whether they act identically for all input states.\n /// This operation is implemented by using the Choi–Jamiołkowski isomorphism.\n operation CheckOperationsEquivalence(\n op : (Qubit[] => Unit is Adj + Ctl),\n reference : (Qubit[] => Unit is Adj + Ctl),\n inputSize : Int)\n : Bool {\n Fact(inputSize > 0, \"`inputSize` must be positive\");\n use (control, target) = (Qubit[inputSize], Qubit[inputSize]);\n within {\n EntangleRegisters(control, target);\n }\n apply {\n op(target);\n Adjoint reference(target);\n }\n\n let areEquivalent = CheckAllZero(control + target);\n ResetAll(control + target);\n areEquivalent\n }\n\n /// # Summary\n /// Given two operations, checks whether they act identically (including global phase) for all input states.\n /// This is done through controlled versions of the operations instead of plain ones which convert the global phase\n /// into a relative phase that can be detected.\n operation CheckOperationsEquivalenceStrict(\n op : (Qubit[] => Unit is Adj + Ctl),\n reference : (Qubit[] => Unit is Adj + Ctl),\n inputSize : Int)\n : Bool {\n Fact(inputSize > 0, \"`inputSize` must be positive\");\n let controlledOp = register => Controlled op(register[...0], register[1...]);\n let controlledReference = register => Controlled reference(register[...0], register[1...]);\n let areEquivalent = CheckOperationsEquivalence(controlledOp, controlledReference, inputSize + 1);\n areEquivalent\n }\n\n /// # Summary\n /// Given two operations, checks whether they act identically on the zero state |0〉 ⊗ |0〉 ⊗ ... ⊗ |0〉 composed of\n /// `inputSize` qubits.\n operation CheckOperationsEquivalenceOnZeroState(\n op : (Qubit[] => Unit is Adj + Ctl),\n reference : (Qubit[] => Unit is Adj + Ctl),\n inputSize : Int)\n : Bool {\n Fact(inputSize > 0, \"`inputSize` must be positive\");\n use target = Qubit[inputSize];\n op(target);\n Adjoint reference(target);\n let isCorrect = CheckAllZero(target);\n ResetAll(target);\n isCorrect\n }\n\n /// # Summary\n /// Given two operations, checks whether they act identically on the zero state |0〉 ⊗ |0〉 ⊗ ... ⊗ |0〉 composed of\n /// `inputSize` qubits.\n /// This operation introduces a control qubit to convert a global phase into a relative phase to be able to detect\n /// it.\n operation CheckOperationsEquivalenceOnZeroStateStrict(\n op : (Qubit[] => Unit is Adj + Ctl),\n reference : (Qubit[] => Unit is Adj + Ctl),\n inputSize : Int)\n : Bool {\n Fact(inputSize > 0, \"`inputSize` must be positive\");\n use control = Qubit();\n use target = Qubit[inputSize];\n within {\n H(control);\n }\n apply {\n Controlled op([control], target);\n Adjoint Controlled reference([control], target);\n }\n\n let isCorrect = CheckAllZero([control] + target);\n ResetAll([control] + target);\n isCorrect\n }\n\n /// # Summary\n /// Shows the effect a quantum operation has on the quantum state.\n operation ShowEffectOnQuantumState(targetRegister : Qubit[], op : (Qubit[] => Unit is Adj + Ctl)) : Unit {\n Message(\"Quantum state before applying the operation:\");\n DumpMachine();\n\n // Apply the operation, dump the simulator state and \"undo\" the operation by applying the adjoint.\n Message(\"Quantum state after applying the operation:\");\n op(targetRegister);\n DumpMachine();\n Adjoint op(targetRegister);\n }\n\n /// # Summary\n /// Shows the comparison of the quantum state between a specific operation and a reference operation.\n operation ShowQuantumStateComparison(\n targetRegister : Qubit[],\n op : (Qubit[] => Unit is Adj + Ctl),\n reference : (Qubit[] => Unit is Adj + Ctl))\n : Unit {\n Message(\"Initial quantum state:\");\n DumpMachine();\n\n // Apply the reference operation, dump the simulator state and \"undo\" the operation by applying the adjoint.\n reference(targetRegister);\n Message(\"Expected quantum state after applying the operation:\");\n DumpMachine();\n Adjoint reference(targetRegister);\n\n // Apply the specific operation, dump the simulator state and \"undo\" the operation by applying the adjoint.\n op(targetRegister);\n Message(\"Actual quantum state after applying the operation:\");\n DumpMachine();\n Adjoint op(targetRegister);\n }\n\n /// # Summary\n /// Given two operations, checks whether they act identically on the zero state |0〉 ⊗ |0〉 ⊗ ... ⊗ |0〉 composed of\n /// `inputSize` qubits. If they don't, prints user feedback.\n operation CheckOperationsEquivalenceOnZeroStateWithFeedback(\n testImpl : (Qubit[] => Unit is Adj + Ctl),\n refImpl : (Qubit[] => Unit is Adj + Ctl),\n inputSize : Int\n ) : Bool {\n\n let isCorrect = CheckOperationsEquivalenceOnZeroState(testImpl, refImpl, inputSize);\n\n // Output different feedback to the user depending on whether the exercise was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n use target = Qubit[inputSize];\n ShowQuantumStateComparison(target, testImpl, refImpl);\n ResetAll(target);\n }\n isCorrect\n }\n\n\n internal operation EntangleRegisters(\n control : Qubit[],\n target : Qubit[]) : Unit is Adj + Ctl {\n Fact(\n Length(control) == Length(target),\n $\"The length of qubit registers must be the same.\");\n\n for index in IndexRange(control) {\n H(control[index]);\n CNOT(control[index], target[index]);\n }\n }\n}\n"
|
|
2316
|
-
},
|
|
2317
2313
|
{
|
|
2318
2314
|
"id": "getting_started__flip_qubit__Verification.qs",
|
|
2319
2315
|
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Intrinsic;\n open Microsoft.Quantum.Katas;\n\n operation FlipQubit(q : Qubit) : Unit is Adj + Ctl {\n X(q);\n }\n\n operation CheckSolution() : Bool {\n let solution = register => Kata.FlipQubit(register[0]);\n let reference = register => FlipQubit(register[0]);\n let isCorrect = CheckOperationsEquivalence(solution, reference, 1);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n Message(\"Congratulations! You have solved your first exercise.\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Look out for hints when your solution is incorrect.\");\n Message(\"Hint: examine the effect your solution has on the |0〉 state and compare it with the effect it \" +\n \"is expected to have.\");\n use target = Qubit[1]; // |0〉\n ShowQuantumStateComparison(target, solution, reference);\n ResetAll(target);\n }\n isCorrect\n }\n}\n"
|
|
2320
2316
|
},
|
|
2317
|
+
{
|
|
2318
|
+
"id": "KatasLibrary.qs",
|
|
2319
|
+
"code": "// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nnamespace Microsoft.Quantum.Katas {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Intrinsic;\n\n /// # Summary\n /// Given two operations, checks whether they act identically for all input states.\n /// This operation is implemented by using the Choi–Jamiołkowski isomorphism.\n operation CheckOperationsEquivalence(\n op : (Qubit[] => Unit is Adj + Ctl),\n reference : (Qubit[] => Unit is Adj + Ctl),\n inputSize : Int)\n : Bool {\n Fact(inputSize > 0, \"`inputSize` must be positive\");\n use (control, target) = (Qubit[inputSize], Qubit[inputSize]);\n within {\n EntangleRegisters(control, target);\n }\n apply {\n op(target);\n Adjoint reference(target);\n }\n\n let areEquivalent = CheckAllZero(control + target);\n ResetAll(control + target);\n areEquivalent\n }\n\n /// # Summary\n /// Given two operations, checks whether they act identically (including global phase) for all input states.\n /// This is done through controlled versions of the operations instead of plain ones which convert the global phase\n /// into a relative phase that can be detected.\n operation CheckOperationsEquivalenceStrict(\n op : (Qubit[] => Unit is Adj + Ctl),\n reference : (Qubit[] => Unit is Adj + Ctl),\n inputSize : Int)\n : Bool {\n Fact(inputSize > 0, \"`inputSize` must be positive\");\n let controlledOp = register => Controlled op(register[...0], register[1...]);\n let controlledReference = register => Controlled reference(register[...0], register[1...]);\n let areEquivalent = CheckOperationsEquivalence(controlledOp, controlledReference, inputSize + 1);\n areEquivalent\n }\n\n /// # Summary\n /// Given two operations, checks whether they act identically on the zero state |0〉 ⊗ |0〉 ⊗ ... ⊗ |0〉 composed of\n /// `inputSize` qubits.\n operation CheckOperationsEquivalenceOnZeroState(\n op : (Qubit[] => Unit is Adj + Ctl),\n reference : (Qubit[] => Unit is Adj + Ctl),\n inputSize : Int)\n : Bool {\n Fact(inputSize > 0, \"`inputSize` must be positive\");\n use target = Qubit[inputSize];\n op(target);\n Adjoint reference(target);\n let isCorrect = CheckAllZero(target);\n ResetAll(target);\n isCorrect\n }\n\n /// # Summary\n /// Given two operations, checks whether they act identically on the zero state |0〉 ⊗ |0〉 ⊗ ... ⊗ |0〉 composed of\n /// `inputSize` qubits.\n /// This operation introduces a control qubit to convert a global phase into a relative phase to be able to detect\n /// it.\n operation CheckOperationsEquivalenceOnZeroStateStrict(\n op : (Qubit[] => Unit is Adj + Ctl),\n reference : (Qubit[] => Unit is Adj + Ctl),\n inputSize : Int)\n : Bool {\n Fact(inputSize > 0, \"`inputSize` must be positive\");\n use control = Qubit();\n use target = Qubit[inputSize];\n within {\n H(control);\n }\n apply {\n Controlled op([control], target);\n Adjoint Controlled reference([control], target);\n }\n\n let isCorrect = CheckAllZero([control] + target);\n ResetAll([control] + target);\n isCorrect\n }\n\n /// # Summary\n /// Shows the effect a quantum operation has on the quantum state.\n operation ShowEffectOnQuantumState(targetRegister : Qubit[], op : (Qubit[] => Unit is Adj + Ctl)) : Unit {\n Message(\"Quantum state before applying the operation:\");\n DumpMachine();\n\n // Apply the operation, dump the simulator state and \"undo\" the operation by applying the adjoint.\n Message(\"Quantum state after applying the operation:\");\n op(targetRegister);\n DumpMachine();\n Adjoint op(targetRegister);\n }\n\n /// # Summary\n /// Shows the comparison of the quantum state between a specific operation and a reference operation.\n operation ShowQuantumStateComparison(\n targetRegister : Qubit[],\n op : (Qubit[] => Unit is Adj + Ctl),\n reference : (Qubit[] => Unit is Adj + Ctl))\n : Unit {\n Message(\"Initial quantum state:\");\n DumpMachine();\n\n // Apply the reference operation, dump the simulator state and \"undo\" the operation by applying the adjoint.\n reference(targetRegister);\n Message(\"Expected quantum state after applying the operation:\");\n DumpMachine();\n Adjoint reference(targetRegister);\n\n // Apply the specific operation, dump the simulator state and \"undo\" the operation by applying the adjoint.\n op(targetRegister);\n Message(\"Actual quantum state after applying the operation:\");\n DumpMachine();\n Adjoint op(targetRegister);\n }\n\n /// # Summary\n /// Given two operations, checks whether they act identically on the zero state |0〉 ⊗ |0〉 ⊗ ... ⊗ |0〉 composed of\n /// `inputSize` qubits. If they don't, prints user feedback.\n operation CheckOperationsEquivalenceOnZeroStateWithFeedback(\n testImpl : (Qubit[] => Unit is Adj + Ctl),\n refImpl : (Qubit[] => Unit is Adj + Ctl),\n inputSize : Int\n ) : Bool {\n\n let isCorrect = CheckOperationsEquivalenceOnZeroState(testImpl, refImpl, inputSize);\n\n // Output different feedback to the user depending on whether the exercise was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n use target = Qubit[inputSize];\n ShowQuantumStateComparison(target, testImpl, refImpl);\n ResetAll(target);\n }\n isCorrect\n }\n\n\n internal operation EntangleRegisters(\n control : Qubit[],\n target : Qubit[]) : Unit is Adj + Ctl {\n Fact(\n Length(control) == Length(target),\n $\"The length of qubit registers must be the same.\");\n\n for index in IndexRange(control) {\n H(control[index]);\n CNOT(control[index], target[index]);\n }\n }\n}\n"
|
|
2320
|
+
},
|
|
2321
2321
|
{
|
|
2322
2322
|
"id": "qubit__learn_single_qubit_state__Verification.qs",
|
|
2323
2323
|
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Intrinsic;\n open Microsoft.Quantum.Math;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n use q = Qubit();\n\n // Prepare the state that will be passed to the solution.\n let angle = 0.5;\n Ry(angle, q);\n\n // Call the solution and get the answer.\n let (a, b) = Kata.LearnSingleQubitState(q);\n\n // Calculate the expected values based on the rotation angle.\n let (a_exp, b_exp) = (Cos(0.5 * angle), Sin(0.5 * angle));\n\n Reset(q);\n\n let isCorrect =\n (AbsD(a - a_exp) <= 0.001) and\n (AbsD(b - b_exp) <= 0.001);\n\n // Output different feedback to the user depending on whether the exercise was correct.\n if isCorrect {\n Message(\"All tests passed.\");\n } else {\n Message(\"At least one of the amplitudes was too far from the expected value.\");\n }\n\n isCorrect\n }\n}\n"
|
|
@@ -2351,159 +2351,159 @@ export default {
|
|
|
2351
2351
|
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation PrepareArbitraryState (alpha : Double, beta : Double, theta : Double, q : Qubit) : Unit is Adj+Ctl {\n let phi = ArcTan2(beta, alpha);\n Ry(2.0 * phi, q);\n R1(theta, q);\n }\n\n operation CheckSolution() : Bool {\n for i in 0 .. 10 {\n let i = IntAsDouble(i);\n let alpha = Cos(i);\n let beta = Sin(i);\n let theta = Sin(i);\n let solution = register => Kata.PrepareArbitraryState(alpha, beta, theta, register[0]);\n let reference = register => PrepareArbitraryState(alpha, beta, theta, register[0]);\n let isCorrect = CheckOperationsEquivalenceOnZeroStateStrict(solution, reference, 1);\n if not isCorrect {\n Message(\"Incorrect.\");\n Message($\"The solution was incorrect for the test case alpha = {alpha}, beta = {beta}, theta = {theta}.\");\n Message(\"Hint: examine the state prepared by your solution and compare it with the state it \" +\n \"is expected to prepare.\");\n use initial = Qubit(); // |0〉\n ShowQuantumStateComparison([initial], solution, reference);\n Reset(initial);\n return false;\n }\n }\n\n Message(\"Correct!\");\n Message(\"The solution was correct for all test cases.\");\n true\n }\n}\n"
|
|
2352
2352
|
},
|
|
2353
2353
|
{
|
|
2354
|
-
"id": "
|
|
2354
|
+
"id": "multi_qubit_systems__learn_basis_state_amplitudes__Verification.qs",
|
|
2355
2355
|
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Intrinsic;\n open Microsoft.Quantum.Math;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n use qs = Qubit[2];\n\n // Prepare the state that will be passed to the solution.\n H(qs[0]);\n ApplyControlledOnInt(0, Ry(1.0, _), [qs[0]], qs[1]);\n ApplyControlledOnInt(1, Ry(2.0, _), [qs[0]], qs[1]);\n\n // Call the solution and get the answer.\n let (x1, x2) = Kata.LearnBasisStateAmplitudes(qs);\n\n // Calculate the expected values based on the rotation angle.\n // We convert |00⟩ + |10⟩ to |0⟩ Ry(1.0)|0⟩ + |1⟩ Ry(2.0)|0⟩, so\n // * the amplitude of |01⟩ is 2nd amp of Ry(1.0)|0⟩\n // * the amplitude of |10⟩ is 1st amp of Ry(2.0)|0⟩\n let (x1_exp, x2_exp) = (\n 1.0/Sqrt(2.0) * Sin(0.5 * 1.0),\n 1.0/Sqrt(2.0) * Cos(0.5 * 2.0));\n\n let isCorrect =\n (AbsD(x1 - x1_exp) <= 0.001) and\n (AbsD(x2 - x2_exp) <= 0.001);\n\n ResetAll(qs);\n\n // Output different feedback to the user depending on whether the exercise was correct.\n if isCorrect {\n Message(\"All tests passed.\");\n } else {\n Message(\"One of the amplitudes was too far from the expected value.\");\n }\n\n isCorrect\n }\n}\n"
|
|
2356
2356
|
},
|
|
2357
2357
|
{
|
|
2358
|
-
"id": "
|
|
2358
|
+
"id": "multi_qubit_systems__prepare_basis_state__Verification.qs",
|
|
2359
2359
|
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation PrepareBasisState_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n X(qs[0]);\n X(qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(Kata.PrepareBasisState, \n PrepareBasisState_Reference, 2)\n }\n}\n"
|
|
2360
2360
|
},
|
|
2361
2361
|
{
|
|
2362
|
-
"id": "
|
|
2362
|
+
"id": "multi_qubit_systems__prepare_superposition__Verification.qs",
|
|
2363
2363
|
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation PrepareSuperposition_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n X(qs[1]);\n H(qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(Kata.PrepareSuperposition, \n PrepareSuperposition_Reference, 2)\n }\n}\n"
|
|
2364
2364
|
},
|
|
2365
2365
|
{
|
|
2366
|
-
"id": "
|
|
2366
|
+
"id": "multi_qubit_systems__prepare_with_real__Verification.qs",
|
|
2367
2367
|
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation PrepareWithReal_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n X(qs[1]);\n H(qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(Kata.PrepareWithReal, \n PrepareWithReal_Reference, 2)\n }\n}\n"
|
|
2368
2368
|
},
|
|
2369
2369
|
{
|
|
2370
|
-
"id": "
|
|
2370
|
+
"id": "multi_qubit_systems__prepare_with_complex__Verification.qs",
|
|
2371
2371
|
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation PrepareWithComplex_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n H(qs[1]);\n S(qs[0]);\n T(qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(Kata.PrepareWithComplex, \n PrepareWithComplex_Reference, 2)\n }\n}\n"
|
|
2372
2372
|
},
|
|
2373
2373
|
{
|
|
2374
2374
|
"id": "multi_qubit_gates__compound_gate__Verification.qs",
|
|
2375
|
-
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.
|
|
2375
|
+
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation CompoundGate (qs : Qubit[]) : Unit is Adj + Ctl {\n S(qs[0]);\n I(qs[1]); // this line can be omitted, since it doesn't change the qubit state\n Y(qs[2]);\n }\n\n operation CheckSolution() : Bool {\n let solution = Kata.CompoundGate;\n let reference = CompoundGate;\n let isCorrect = CheckOperationsEquivalenceStrict(solution, reference, 3);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine how your solution transforms the given state and compare it with the expected \" +\n \"transformation\");\n use initial = Qubit[3]; // |000〉\n Ry(ArcTan2(0.8, 0.6) * 2.0, initial[0]);\n Ry(ArcTan2(0.7, 0.4) * 2.0, initial[1]);\n Ry(ArcTan2(0.6, 0.5) * 2.0, initial[2]);\n ShowQuantumStateComparison(initial, solution, reference);\n ResetAll(initial);\n }\n\n isCorrect\n }\n}"
|
|
2376
2376
|
},
|
|
2377
2377
|
{
|
|
2378
2378
|
"id": "multi_qubit_gates__preparing_bell_state__Verification.qs",
|
|
2379
|
-
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.
|
|
2379
|
+
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation BellState (qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n CNOT(qs[0], qs[1]);\n }\n\n operation CheckSolution() : Bool {\n let solution = Kata.BellState;\n let reference = BellState;\n let isCorrect = CheckOperationsEquivalenceOnZeroState(solution, reference, 2);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect :(\");\n Message(\"Hint: examine the state prepared by your solution and compare it with the state it \" +\n \"is expected to prepare.\");\n use initial = Qubit[2]; // |00〉\n ShowQuantumStateComparison(initial, solution, reference);\n ResetAll(initial);\n }\n\n isCorrect\n }\n}"
|
|
2380
2380
|
},
|
|
2381
2381
|
{
|
|
2382
2382
|
"id": "multi_qubit_gates__qubit_swap__Verification.qs",
|
|
2383
|
-
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Intrinsic;\n open Microsoft.Quantum.Katas;\n\n operation QubitSwap (qs : Qubit[], index1 : Int, index2 : Int) : Unit is Adj + Ctl {\n SWAP(qs[index1], qs[index2]);\n }\n\n operation CheckSolution() : Bool {\n for N in 2 .. 5 {\n for index1 in 0 .. N-2 {\n for index2 in index1+1 .. N-1 {\n let solution = register => Kata.QubitSwap(register, index1, index2);\n let reference = register => QubitSwap(register, index1, index2);\n if not CheckOperationsEquivalence(solution, reference, N) {\n Message(\"Incorrect.\");\n Message(
|
|
2383
|
+
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Intrinsic;\n open Microsoft.Quantum.Katas;\n\n operation QubitSwap (qs : Qubit[], index1 : Int, index2 : Int) : Unit is Adj + Ctl {\n SWAP(qs[index1], qs[index2]);\n }\n\n operation CheckSolution() : Bool {\n for N in 2 .. 5 {\n for index1 in 0 .. N-2 {\n for index2 in index1+1 .. N-1 {\n let solution = register => Kata.QubitSwap(register, index1, index2);\n let reference = register => QubitSwap(register, index1, index2);\n if not CheckOperationsEquivalence(solution, reference, N) {\n Message(\"Incorrect.\");\n Message($\"Swapping qubits {index1} and {index2} out of $N$ didn't have the expected effect.\");\n return false;\n }\n }\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}"
|
|
2384
2384
|
},
|
|
2385
2385
|
{
|
|
2386
2386
|
"id": "multi_qubit_gates__controlled_rotation__Verification.qs",
|
|
2387
|
-
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Katas;\n\n operation ControlledRotation (qs : Qubit[], theta : Double) : Unit is Adj + Ctl {\n let controll = qs[0];\n let target = qs[1];\n Controlled Rx([controll], (theta, target));\n }\n\n operation CheckSolution() : Bool {\n for i in 0 .. 20 {\n let angle = IntAsDouble(i) / 10.0;\n let solution = register => Kata.ControlledRotation(register, angle);\n let reference = register => ControlledRotation(register, angle);\n if not CheckOperationsEquivalence(solution, reference, 2) {\n Message(\"Incorrect.\");\n Message(
|
|
2387
|
+
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Katas;\n\n operation ControlledRotation (qs : Qubit[], theta : Double) : Unit is Adj + Ctl {\n let controll = qs[0];\n let target = qs[1];\n Controlled Rx([controll], (theta, target));\n }\n\n operation CheckSolution() : Bool {\n for i in 0 .. 20 {\n let angle = IntAsDouble(i) / 10.0;\n let solution = register => Kata.ControlledRotation(register, angle);\n let reference = register => ControlledRotation(register, angle);\n if not CheckOperationsEquivalence(solution, reference, 2) {\n Message(\"Incorrect.\");\n Message($\"The test case for theta={angle} did not pass\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}"
|
|
2388
2388
|
},
|
|
2389
2389
|
{
|
|
2390
2390
|
"id": "multi_qubit_gates__arbitrary_controls__Verification.qs",
|
|
2391
|
-
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Katas;\n\n operation MultiControls(controls : Qubit[], target : Qubit, controlBits : Bool[]) : Unit is Adj + Ctl {\n within {\n for index in 0 .. Length(controls) - 1 {\n if controlBits[index] == false {\n X(controls[index]);\n }\n }\n } apply {\n Controlled X(controls,target);\n }\n }\n\n operation CheckSolution() : Bool {\n for i in 0 .. (2 ^ 4) - 1 {\n let bits = IntAsBoolArray(i, 4);\n let solution = register => Kata.MultiControls(Most(register), Tail(register), bits);\n let reference = register => MultiControls(Most(register), Tail(register), bits);\n if not CheckOperationsEquivalence(solution, reference, 5) {\n Message(\"Incorrect.\");\n Message(
|
|
2391
|
+
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Katas;\n\n operation MultiControls(controls : Qubit[], target : Qubit, controlBits : Bool[]) : Unit is Adj + Ctl {\n within {\n for index in 0 .. Length(controls) - 1 {\n if controlBits[index] == false {\n X(controls[index]);\n }\n }\n } apply {\n Controlled X(controls,target);\n }\n }\n\n operation CheckSolution() : Bool {\n for i in 0 .. (2 ^ 4) - 1 {\n let bits = IntAsBoolArray(i, 4);\n let solution = register => Kata.MultiControls(Most(register), Tail(register), bits);\n let reference = register => MultiControls(Most(register), Tail(register), bits);\n if not CheckOperationsEquivalence(solution, reference, 5) {\n Message(\"Incorrect.\");\n Message($\"The test case for controlBits = {bits} did not pass.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}"
|
|
2392
2392
|
},
|
|
2393
2393
|
{
|
|
2394
|
-
"id": "
|
|
2395
|
-
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Intrinsic;\n
|
|
2394
|
+
"id": "single_qubit_measurements__distinguish_0_and_1__Verification.qs",
|
|
2395
|
+
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Intrinsic;\n\n operation StatePrep_IsQubitZero (q : Qubit, state : Int) : Unit is Adj {\n if state == 0 {\n // convert |0⟩ to |1⟩\n X(q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishTwoStates(\n StatePrep_IsQubitZero,\n Kata.IsQubitZero,\n [\"|1⟩\", \"|0⟩\"],\n false);\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n isCorrect\n }\n\n}\n"
|
|
2396
2396
|
},
|
|
2397
2397
|
{
|
|
2398
|
-
"id": "
|
|
2399
|
-
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Intrinsic;\n\n //
|
|
2398
|
+
"id": "single_qubit_measurements__Common.qs",
|
|
2399
|
+
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Intrinsic;\n open Microsoft.Quantum.Random;\n\n // \"Framework\" operation for testing single-qubit tasks for distinguishing states of one qubit\n // with Bool return\n operation DistinguishTwoStates(\n statePrep : ((Qubit, Int) => Unit is Adj),\n testImpl : (Qubit => Bool),\n stateName : String[],\n checkFinalState : Bool) : Bool {\n\n let nTotal = 100;\n let nStates = 2;\n mutable misclassifications = Repeated(0, nStates);\n\n use q = Qubit();\n for i in 1 .. nTotal {\n // get a random bit to define whether qubit will be in a state corresponding to true return (1) or to false one (0)\n // state = 0 false return\n // state = 1 true return\n let state = DrawRandomInt(0, 1);\n\n // do state prep: convert |0⟩ to outcome with false return or to outcome with true return depending on state\n statePrep(q, state);\n\n // get the solution's answer and verify if NOT a match, then differentiate what kind of mismatch\n let ans = testImpl(q);\n if ans != (state == 1) {\n set misclassifications w/= state <- misclassifications[state] + 1;\n }\n\n // If the final state is to be verified, check if it matches the measurement outcome\n if checkFinalState {\n Adjoint statePrep(q, state);\n Fact(CheckZero(q), \"Returned Bool value does not match the expected qubit state.\");\n } else {\n Reset(q);\n }\n }\n\n mutable totalMisclassifications = 0;\n for i in 0 .. nStates - 1 {\n if misclassifications[i] != 0 {\n set totalMisclassifications += misclassifications[i];\n Message($\"Misclassified {stateName[i]} as {stateName[1 - i]} in {misclassifications[i]} test runs.\");\n }\n }\n\n return totalMisclassifications == 0;\n }\n\n}\n"
|
|
2400
2400
|
},
|
|
2401
2401
|
{
|
|
2402
|
-
"id": "
|
|
2403
|
-
"code": "namespace Kata.Verification {\n\n // Distinguish |+❭ and |-❭ using Measure operation\n operation StatePrep_IsQubitMinus (q : Qubit, state : Int) : Unit is Adj {\n if state == 1 {\n // convert |0⟩ to |-⟩\n X(q);\n H(q);\n } else {\n // convert |0⟩ to |+⟩\n H(q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishTwoStates(\n StatePrep_IsQubitMinus,\n Kata.IsQubitMinus,\n [\"|+⟩\", \"|-⟩\"],\n false);\n if isCorrect {\n Message(\"
|
|
2402
|
+
"id": "single_qubit_measurements__distinguish_plus_and_minus__Verification.qs",
|
|
2403
|
+
"code": "namespace Kata.Verification {\n\n // Distinguish |+❭ and |-❭ using Measure operation\n operation StatePrep_IsQubitMinus (q : Qubit, state : Int) : Unit is Adj {\n if state == 1 {\n // convert |0⟩ to |-⟩\n X(q);\n H(q);\n } else {\n // convert |0⟩ to |+⟩\n H(q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishTwoStates(\n StatePrep_IsQubitMinus,\n Kata.IsQubitMinus,\n [\"|+⟩\", \"|-⟩\"],\n false);\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n isCorrect\n }\n\n}\n"
|
|
2404
2404
|
},
|
|
2405
2405
|
{
|
|
2406
|
-
"id": "
|
|
2407
|
-
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n\n // Distinguish specific orthogonal states\n // |ψ₊⟩ = 0.6 * |0⟩ + 0.8 * |1⟩,\n // |ψ₋⟩ = -0.8 * |0⟩ + 0.6 * |1⟩.\n operation StatePrep_IsQubitPsiPlus(q : Qubit, state : Int) : Unit is Adj {\n if state == 0 {\n // convert |0⟩ to |ψ₋⟩\n X(q);\n Ry(2.0 * ArcTan2(0.8, 0.6), q);\n } else {\n // convert |0⟩ to |ψ₊⟩\n Ry(2.0 * ArcTan2(0.8, 0.6), q);\n }\n }\n\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishTwoStates(\n StatePrep_IsQubitPsiPlus,\n Kata.IsQubitPsiPlus,\n [\"|ψ₋⟩\", \"|ψ₊⟩\"],\n false);\n if isCorrect {\n Message(\"
|
|
2406
|
+
"id": "single_qubit_measurements__distinguish_orthogonal_states_1__Verification.qs",
|
|
2407
|
+
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n\n // Distinguish specific orthogonal states\n // |ψ₊⟩ = 0.6 * |0⟩ + 0.8 * |1⟩,\n // |ψ₋⟩ = -0.8 * |0⟩ + 0.6 * |1⟩.\n operation StatePrep_IsQubitPsiPlus(q : Qubit, state : Int) : Unit is Adj {\n if state == 0 {\n // convert |0⟩ to |ψ₋⟩\n X(q);\n Ry(2.0 * ArcTan2(0.8, 0.6), q);\n } else {\n // convert |0⟩ to |ψ₊⟩\n Ry(2.0 * ArcTan2(0.8, 0.6), q);\n }\n }\n\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishTwoStates(\n StatePrep_IsQubitPsiPlus,\n Kata.IsQubitPsiPlus,\n [\"|ψ₋⟩\", \"|ψ₊⟩\"],\n false);\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n isCorrect\n }\n\n}\n"
|
|
2408
2408
|
},
|
|
2409
2409
|
{
|
|
2410
|
-
"id": "
|
|
2411
|
-
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Math;\n\n // Distinguish states |A❭ and |B❭\n // |A⟩ = cos(alpha) * |0⟩ - i sin(alpha) * |1⟩,\n // |B⟩ = - i sin(alpha) * |0⟩ + cos(alpha) * |1⟩.\n operation StatePrep_IsQubitA(alpha : Double, q : Qubit, state : Int) : Unit is Adj {\n if state == 0 {\n // convert |0⟩ to |B⟩\n X(q);\n Rx(2.0 * alpha, q);\n } else {\n // convert |0⟩ to |A⟩\n Rx(2.0 * alpha, q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for i in 0 .. 10 {\n let alpha = (PI() * IntAsDouble(i)) / 10.0;\n let isCorrect = DistinguishTwoStates(\n StatePrep_IsQubitA(alpha, _, _),\n Kata.IsQubitA(alpha, _),\n [$\"|B⟩ = -i sin({i}π/10)|0⟩ + cos({i}π/10)|1⟩\", $\"|A⟩ = cos({i}π/10)|0⟩ + i sin({i}π/10)|1⟩\"],\n false);\n if not isCorrect {\n return false;\n }\n }\n Message(\"
|
|
2410
|
+
"id": "single_qubit_measurements__distinguish_orthogonal_states_2__Verification.qs",
|
|
2411
|
+
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Math;\n\n // Distinguish states |A❭ and |B❭\n // |A⟩ = cos(alpha) * |0⟩ - i sin(alpha) * |1⟩,\n // |B⟩ = - i sin(alpha) * |0⟩ + cos(alpha) * |1⟩.\n operation StatePrep_IsQubitA(alpha : Double, q : Qubit, state : Int) : Unit is Adj {\n if state == 0 {\n // convert |0⟩ to |B⟩\n X(q);\n Rx(2.0 * alpha, q);\n } else {\n // convert |0⟩ to |A⟩\n Rx(2.0 * alpha, q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for i in 0 .. 10 {\n let alpha = (PI() * IntAsDouble(i)) / 10.0;\n let isCorrect = DistinguishTwoStates(\n StatePrep_IsQubitA(alpha, _, _),\n Kata.IsQubitA(alpha, _),\n [$\"|B⟩ = -i sin({i}π/10)|0⟩ + cos({i}π/10)|1⟩\", $\"|A⟩ = cos({i}π/10)|0⟩ + i sin({i}π/10)|1⟩\"],\n false);\n if not isCorrect {\n Message(\"Incorrect.\");\n return false;\n }\n }\n Message(\"Correct!\");\n true\n }\n\n}\n"
|
|
2412
2412
|
},
|
|
2413
2413
|
{
|
|
2414
|
-
"id": "
|
|
2415
|
-
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Math;\n\n // Measure state in {|A❭, |B❭} basis\n // |A⟩ = cos(alpha) * |0⟩ - i sin(alpha) * |1⟩,\n // |B⟩ = - i sin(alpha) * |0⟩ + cos(alpha) * |1⟩.\n
|
|
2414
|
+
"id": "single_qubit_measurements__a_b_basis_measurements__Verification.qs",
|
|
2415
|
+
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Math;\n\n // Measure state in {|A❭, |B❭} basis\n // |A⟩ = cos(alpha) * |0⟩ - i sin(alpha) * |1⟩,\n // |B⟩ = - i sin(alpha) * |0⟩ + cos(alpha) * |1⟩.\n operation StatePrep_IsQubitA (alpha : Double, q : Qubit, state : Int) : Unit is Adj {\n if state == 0 {\n // convert |0⟩ to |B⟩\n X(q);\n Rx(2.0 * alpha, q);\n } else {\n // convert |0⟩ to |A⟩\n Rx(2.0 * alpha, q);\n }\n }\n\n // We can use the StatePrep_IsQubitA operation for the testing\n operation CheckSolution() : Bool {\n for i in 0 .. 10 {\n let alpha = (PI() * IntAsDouble(i)) / 10.0;\n let isCorrect = DistinguishTwoStates(\n StatePrep_IsQubitA(alpha, _, _),\n q => Kata.MeasureInABBasis(alpha, q) == Zero,\n [$\"|B⟩=(-i sin({i}π/10)|0⟩ + cos({i}π/10)|1⟩)\", $\"|A⟩=(cos({i}π/10)|0⟩ + i sin({i}π/10)|1⟩)\"],\n true);\n if not isCorrect {\n Message($\"Test fails for alpha={alpha}\");\n return false;\n }\n }\n Message(\"Correct!\");\n true\n }\n\n}\n"
|
|
2416
2416
|
},
|
|
2417
2417
|
{
|
|
2418
|
-
"id": "
|
|
2418
|
+
"id": "multi_qubit_measurements__full_measurements__Verification.qs",
|
|
2419
2419
|
"code": "namespace Kata.Verification {\n\n // Distinguish four basis states\n operation StatePrep_BasisStateMeasurement(qs : Qubit[], state : Int, dummyVar : Double) : Unit is Adj {\n if state / 2 == 1 {\n // |10⟩ or |11⟩\n X(qs[0]);\n }\n if state % 2 == 1 {\n // |01⟩ or |11⟩\n X(qs[1]);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishStates_MultiQubit(\n 2,\n 4,\n StatePrep_BasisStateMeasurement,\n Kata.BasisStateMeasurement,\n false,\n [\"|00⟩\", \"|01⟩\", \"|10⟩\", \"|11⟩\"]);\n\n if (isCorrect) {\n Message(\"Correct!\");\n Message(\"All test cases passed.\");\n } else {\n Message(\"Incorrect!\");\n }\n\n isCorrect\n }\n}\n"
|
|
2420
2420
|
},
|
|
2421
2421
|
{
|
|
2422
|
-
"id": "
|
|
2422
|
+
"id": "multi_qubit_measurements__Common.qs",
|
|
2423
2423
|
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Intrinsic;\n open Microsoft.Quantum.Canon;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Measurement;\n open Microsoft.Quantum.Random;\n\n // \"Framework\" operation for testing multi-qubit tasks for distinguishing states of an array of qubits\n // with Int return\n operation DistinguishStates_MultiQubit(\n nQubits : Int,\n nStates : Int,\n statePrep : ((Qubit[], Int, Double) => Unit is Adj),\n testImpl : (Qubit[] => Int),\n preserveState : Bool,\n stateNames : String[]) : Bool {\n\n let nTotal = 100;\n // misclassifications will store the number of times state i has been classified as state j (dimension nStates^2)\n mutable misclassifications = [0, size = nStates * nStates];\n // unknownClassifications will store the number of times state i has been classified as some invalid state (index < 0 or >= nStates)\n mutable unknownClassifications = [0, size = nStates];\n\n use qs = Qubit[nQubits];\n for i in 1 .. nTotal {\n // get a random integer to define the state of the qubits\n let state = DrawRandomInt(0, nStates - 1);\n // get a random rotation angle to define the exact state of the qubits\n // for some exercises, this value might be a dummy variable which does not matter\n let alpha = DrawRandomDouble(0.0, 1.0) * PI();\n\n // do state prep: convert |0...0⟩ to outcome with return equal to state\n statePrep(qs, state, alpha);\n\n // get the solution's answer and verify that it's a match, if not, increase the exact mismatch count\n let ans = testImpl(qs);\n if ((ans >= 0) and (ans < nStates)) {\n // classification result is a valid state index - check if is it correct\n if ans != state {\n set misclassifications w/= ((state * nStates) + ans) <- (misclassifications[(state * nStates) + ans] + 1);\n }\n }\n else {\n // classification result is an invalid state index - file it separately\n set unknownClassifications w/= state <- (unknownClassifications[state] + 1);\n }\n\n if preserveState {\n // check that the state of the qubit after the operation is unchanged\n Adjoint statePrep(qs, state, alpha);\n if not CheckAllZero(qs) {\n Message(\"Test should preseve qubit state.\");\n ResetAll(qs);\n return false;\n }\n } else {\n // we're not checking the state of the qubit after the operation\n ResetAll(qs);\n }\n }\n ResetAll(qs);\n\n mutable totalMisclassifications = 0;\n for i in 0 .. nStates - 1 {\n for j in 0 .. nStates - 1 {\n if misclassifications[(i * nStates) + j] != 0 {\n set totalMisclassifications += misclassifications[i * nStates + j];\n Message($\"Misclassified {stateNames[i]} as {stateNames[j]} in {misclassifications[(i * nStates) + j]} test runs.\");\n }\n }\n if unknownClassifications[i] != 0 {\n set totalMisclassifications += unknownClassifications[i];\n Message($\"Misclassified {stateNames[i]} as Unknown State in {unknownClassifications[i]} test runs.\");\n }\n }\n totalMisclassifications == 0\n }\n}\n"
|
|
2424
2424
|
},
|
|
2425
2425
|
{
|
|
2426
|
-
"id": "
|
|
2426
|
+
"id": "multi_qubit_measurements__partial_measurements_for_system__Verification.qs",
|
|
2427
2427
|
"code": "namespace Kata.Verification {\n\n // Distinguish orthogonal states using partial measurements\n operation StatePrep_IsPlusPlusMinus(qs : Qubit[], state : Int, dummyVar : Double) : Unit is Adj {\n if state == 0 {\n // prepare the state |++-⟩\n H(qs[0]);\n H(qs[1]);\n X(qs[2]);\n H(qs[2]);\n } else {\n // prepare the state |---⟩\n X(qs[0]);\n H(qs[0]);\n X(qs[1]);\n H(qs[1]);\n X(qs[2]);\n H(qs[2]);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n return DistinguishStates_MultiQubit(\n 3,\n 2,\n StatePrep_IsPlusPlusMinus,\n Kata.IsPlusPlusMinus,\n false,\n [\"|++-⟩\", \"|---⟩\"]);\n }\n\n}\n"
|
|
2428
2428
|
},
|
|
2429
2429
|
{
|
|
2430
|
-
"id": "
|
|
2430
|
+
"id": "multi_qubit_measurements__state_modification__Verification.qs",
|
|
2431
2431
|
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Math;\n\n // State selection using partial measurements\n operation stateInitialize_StateSelction(alpha : Double, qs : Qubit[]) : Unit {\n // Prepare the state to be input to the testImplementation\n // set the second qubit in a superposition a |0⟩ + b|1⟩\n // with a = cos alpha, b = sin alpha\n Ry(2.0 * alpha, qs[1]);\n\n H(qs[0]);\n // Apply CX gate\n CX(qs[0], qs[1]);\n }\n\n operation statePrepare_StateSelction(alpha : Double, Choice : Int, qs : Qubit[]) : Unit is Adj {\n // The expected state of the second qubit for the exercise.\n\n // set the second qubit in a superposition a |0⟩ + b|1⟩\n // with a = cos alpha, b = sin alpha\n Ry(2.0 * alpha, qs[1]);\n if Choice == 1 {\n // if the Choice is 1, change the state to b|0⟩ + a|1⟩\n X(qs[1]);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n use qs = Qubit[2];\n for i in 0 .. 5 {\n let alpha = (PI() * IntAsDouble(i)) / 5.0;\n\n //for Choice = 0 and 1,\n for Choice in 0 .. 1 {\n // Prepare the state to be input to the testImplementation\n stateInitialize_StateSelction(alpha, qs);\n\n // operate testImplementation\n Kata.StateSelction(qs, Choice);\n // reset the first qubit, since its state does not matter\n Reset(qs[0]);\n\n // apply adjoint reference operation and check that the result is correct\n Adjoint statePrepare_StateSelction(alpha, Choice, qs);\n\n if not CheckAllZero(qs) {\n ResetAll(qs);\n return false;\n }\n ResetAll(qs);\n }\n }\n ResetAll(qs);\n true\n }\n\n}\n"
|
|
2432
2432
|
},
|
|
2433
2433
|
{
|
|
2434
|
-
"id": "
|
|
2434
|
+
"id": "multi_qubit_measurements__state_preparation__Verification.qs",
|
|
2435
2435
|
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Math;\n\n // State preparation using partial measurements\n operation RefImpl_T4(qs : Qubit[]) : Unit is Adj {\n // Rotate first qubit to (sqrt(2) |0⟩ + |1⟩) / sqrt(3)\n let theta = ArcSin(1.0 / Sqrt(3.0));\n Ry(2.0 * theta, qs[0]);\n\n // Split the state sqrt(2) |0⟩ ⊗ |0⟩ into |00⟩ + |01⟩\n ApplyControlledOnInt(0, H, [qs[0]], qs[1]);\n }\n\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n use qs = Qubit[2];\n\n // operate the test implementation\n Kata.PostSelection(qs);\n\n // apply adjoint reference operation and check that the result is |0⟩\n Adjoint RefImpl_T4(qs);\n let isCorrect = CheckAllZero(qs);\n ResetAll(qs);\n isCorrect\n }\n\n}\n"
|
|
2436
2436
|
},
|
|
2437
2437
|
{
|
|
2438
|
-
"id": "
|
|
2438
|
+
"id": "multi_qubit_measurements__joint_measurements__Verification.qs",
|
|
2439
2439
|
"code": "namespace Kata.Verification {\n\n // Two qubit parity Measurement\n operation StatePrep_ParityMeasurement(qs : Qubit[], state : Int, alpha : Double) : Unit is Adj {\n // prep cos(alpha) * |0..0⟩ + sin(alpha) * |1..1⟩\n Ry(2.0 * alpha, qs[0]);\n for i in 1 .. Length(qs) - 1 {\n CNOT(qs[0], qs[i]);\n }\n\n if state == 1 {\n // flip the state of the first half of the qubits\n for i in 0 .. Length(qs) / 2 - 1 {\n X(qs[i]);\n }\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n return DistinguishStates_MultiQubit(\n 2,\n 2,\n StatePrep_ParityMeasurement,\n Kata.ParityMeasurement,\n true,\n [\"α|00⟩ + β|11⟩\", \"α|01⟩ + β|10⟩\"]);\n }\n}\n"
|
|
2440
2440
|
},
|
|
2441
2441
|
{
|
|
2442
|
-
"id": "
|
|
2443
|
-
"code": "namespace Kata.Verification {\n @EntryPoint()\n operation CheckSolution() : Bool {\n
|
|
2442
|
+
"id": "random_numbers__random_bit__Verification.qs",
|
|
2443
|
+
"code": "namespace Kata.Verification {\n @EntryPoint()\n operation CheckSolution() : Bool {\n let randomnessVerifier = () => CheckUniformDistribution(Kata.RandomBit, 0, 1, 1000);\n let isCorrect = IsSufficientlyRandom(randomnessVerifier);\n if isCorrect {\n Message(\"Correct!\");\t\n\t }\n isCorrect\n }\n\n}\n"
|
|
2444
2444
|
},
|
|
2445
2445
|
{
|
|
2446
|
-
"id": "
|
|
2447
|
-
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Math;\n\n /// # Summary\n /// Helper operation that checks that the given RNG operation generates a uniform distribution.\n ///\n /// # Input\n /// ## randomGenerator\n /// Random number generation operation to be tested.\n /// The parameters to this operation are provided by the caller using Delay().\n /// ## min, max\n /// Minimal and maximal numbers in the range to be generated, inclusive.\n /// ## nRuns\n /// The number of random numbers to generate for test.\n ///\n /// # Output\n /// 0x0 if the generated distribution is uniform.\n /// 0x1 if a value was generated outside the specified range.\n /// 0x2 if the average of the distribution is outside the expected range.\n /// 0x3 if the median of the distribution is outside the expected range.\n /// 0x4 if the minimum count requirements were not met.\n operation CheckUniformDistribution (\n randomGenerator : (Unit => Int),\n min : Int,\n max : Int,\n nRuns : Int) \n : Int {\n let idealMean = 0.5 * IntAsDouble(max + min);\n let rangeDividedByTwo = 0.5 * IntAsDouble(max - min);\n // Variance = a*(a+1)/3, where a = (max-min)/2\n // For sample population : divide it by nRuns\n let varianceInSamplePopulation = (rangeDividedByTwo * (rangeDividedByTwo + 1.0)) / IntAsDouble(3 * nRuns);\n let standardDeviation = Sqrt(varianceInSamplePopulation);\n\n // lowRange : The lower bound of the median and average for generated dataset\n // highRange : The upper bound of the median and average for generated dataset\n // Set them with 3 units of std deviation for 99% accuracy.\n let lowRange = idealMean - 3.0 * standardDeviation;\n let highRange = idealMean + 3.0 * standardDeviation;\n\n let idealCopiesGenerated = IntAsDouble(nRuns) / IntAsDouble(max-min+1);\n let minimumCopiesGenerated = (0.8 * idealCopiesGenerated > 40.0) ? 0.8 * idealCopiesGenerated | 0.0;\n\n mutable counts = [0, size = max + 1];\n mutable average = 0.0;\n for i in 1..nRuns {\n let val = randomGenerator();\n if (val < min or val > max) {\n Message($\"Unexpected number generated. Expected values from {min} to {max}, generated {val}\");\n return 0x1;\n }\n set average += IntAsDouble(val);\n set counts w/= val <- counts[val] + 1;\n }\n\n set average = average / IntAsDouble(nRuns);\n if (average < lowRange or average > highRange) {\n Message($\"Unexpected average of generated numbers. Expected between {lowRange} and {highRange}, got {average}\");\n return 0x2;\n }\n\n let median = FindMedian (counts, max+1, nRuns);\n if (median < Floor(lowRange) or median > Ceiling(highRange)) {\n Message($\"Unexpected median of generated numbers. Expected between {Floor(lowRange)} and {Ceiling(highRange)}, got {median}.\");\n return 0x3;\n }\n\n for i in min..max {\n if counts[i] < Floor(minimumCopiesGenerated) {\n Message($\"Unexpectedly low number of {i}'s generated. Only {counts[i]} out of {nRuns} were {i}\");\n return 0x4;\n }\n }\n return 0x0;\n }\n\n operation FindMedian (counts : Int[], arrSize : Int, sampleSize : Int) : Int {\n mutable totalCount = 0;\n for i in 0 .. arrSize - 1 {\n set totalCount = totalCount + counts[i];\n if totalCount >= sampleSize / 2 {\n return i;\n }\n }\n return -1;\n }\n\n operation IsSufficientlyRandom(verifier : (Unit => Int)) : Bool {\n let results = RunRandomnessVerifier(verifier, 10);\n Tail(results) == 0x0\n }\n\n /// # Summary\n /// Helper operation that runs a randomness verifier up to a maximum number of times.\n /// A single run can fail with non-negligible probability even for a \"correct\" random generator.\n ///\n /// # Input\n /// ## verifier\n /// Operation which verifies the a random generator.\n /// ## maxAttempts\n /// Maximum number of times the verifier is run until a successful result occurs.\n ///\n /// # Output\n /// Array with the results of each verifier run.\n operation RunRandomnessVerifier(verifier : (Unit => Int), maxAttempts : Int) : Int[] {\n mutable attemptResults = [];\n mutable result = -1;\n repeat {\n set result = verifier();\n set attemptResults += [result];\n } until
|
|
2446
|
+
"id": "random_numbers__Common.qs",
|
|
2447
|
+
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Math;\n\n /// # Summary\n /// Helper operation that checks that the given RNG operation generates a uniform distribution.\n ///\n /// # Input\n /// ## randomGenerator\n /// Random number generation operation to be tested.\n /// The parameters to this operation are provided by the caller using Delay().\n /// ## min, max\n /// Minimal and maximal numbers in the range to be generated, inclusive.\n /// ## nRuns\n /// The number of random numbers to generate for test.\n ///\n /// # Output\n /// 0x0 if the generated distribution is uniform.\n /// 0x1 if a value was generated outside the specified range.\n /// 0x2 if the average of the distribution is outside the expected range.\n /// 0x3 if the median of the distribution is outside the expected range.\n /// 0x4 if the minimum count requirements were not met.\n operation CheckUniformDistribution (\n randomGenerator : (Unit => Int),\n min : Int,\n max : Int,\n nRuns : Int) \n : Int {\n let idealMean = 0.5 * IntAsDouble(max + min);\n let rangeDividedByTwo = 0.5 * IntAsDouble(max - min);\n // Variance = a*(a+1)/3, where a = (max-min)/2\n // For sample population : divide it by nRuns\n let varianceInSamplePopulation = (rangeDividedByTwo * (rangeDividedByTwo + 1.0)) / IntAsDouble(3 * nRuns);\n let standardDeviation = Sqrt(varianceInSamplePopulation);\n\n // lowRange : The lower bound of the median and average for generated dataset\n // highRange : The upper bound of the median and average for generated dataset\n // Set them with 3 units of std deviation for 99% accuracy.\n let lowRange = idealMean - 3.0 * standardDeviation;\n let highRange = idealMean + 3.0 * standardDeviation;\n\n let idealCopiesGenerated = IntAsDouble(nRuns) / IntAsDouble(max-min+1);\n let minimumCopiesGenerated = (0.8 * idealCopiesGenerated > 40.0) ? 0.8 * idealCopiesGenerated | 0.0;\n\n mutable counts = [0, size = max + 1];\n mutable average = 0.0;\n for i in 1..nRuns {\n let val = randomGenerator();\n if (val < min or val > max) {\n Message($\"Unexpected number generated. Expected values from {min} to {max}, generated {val}\");\n return 0x1;\n }\n set average += IntAsDouble(val);\n set counts w/= val <- counts[val] + 1;\n }\n\n set average = average / IntAsDouble(nRuns);\n if (average < lowRange or average > highRange) {\n Message($\"Unexpected average of generated numbers. Expected between {lowRange} and {highRange}, got {average}\");\n return 0x2;\n }\n\n let median = FindMedian (counts, max+1, nRuns);\n if (median < Floor(lowRange) or median > Ceiling(highRange)) {\n Message($\"Unexpected median of generated numbers. Expected between {Floor(lowRange)} and {Ceiling(highRange)}, got {median}.\");\n return 0x3;\n }\n\n for i in min..max {\n if counts[i] < Floor(minimumCopiesGenerated) {\n Message($\"Unexpectedly low number of {i}'s generated. Only {counts[i]} out of {nRuns} were {i}\");\n return 0x4;\n }\n }\n return 0x0;\n }\n\n operation FindMedian (counts : Int[], arrSize : Int, sampleSize : Int) : Int {\n mutable totalCount = 0;\n for i in 0 .. arrSize - 1 {\n set totalCount = totalCount + counts[i];\n if totalCount >= sampleSize / 2 {\n return i;\n }\n }\n return -1;\n }\n\n operation IsSufficientlyRandom(verifier : (Unit => Int)) : Bool {\n let results = RunRandomnessVerifier(verifier, 10);\n Tail(results) == 0x0\n }\n\n /// # Summary\n /// Helper operation that runs a randomness verifier up to a maximum number of times.\n /// A single run can fail with non-negligible probability even for a \"correct\" random generator.\n ///\n /// # Input\n /// ## verifier\n /// Operation which verifies the a random generator.\n /// ## maxAttempts\n /// Maximum number of times the verifier is run until a successful result occurs.\n ///\n /// # Output\n /// Array with the results of each verifier run.\n operation RunRandomnessVerifier(verifier : (Unit => Int), maxAttempts : Int) : Int[] {\n mutable attemptResults = [];\n mutable result = -1;\n repeat {\n set result = verifier();\n set attemptResults += [result];\n // If the result is 0x1, the generator returned an invalid result.\n // That's different from \"not random enough\" verdicts, so we break right away.\n } until result == 0 or result == 0x1 or Length(attemptResults) >= maxAttempts;\n\n attemptResults\n }\n}\n"
|
|
2448
2448
|
},
|
|
2449
2449
|
{
|
|
2450
|
-
"id": "
|
|
2451
|
-
"code": "namespace Kata.Verification {\n @EntryPoint()\n operation CheckSolution() : Bool {\n
|
|
2450
|
+
"id": "random_numbers__random_two_bits__Verification.qs",
|
|
2451
|
+
"code": "namespace Kata.Verification {\n @EntryPoint()\n operation CheckSolution() : Bool {\n let randomnessVerifier = () => CheckUniformDistribution(Kata.RandomTwoBits, 0, 3, 1000);\n let isCorrect = IsSufficientlyRandom(randomnessVerifier);\n if isCorrect {\n Message(\"All tests passed.\");\n }\n isCorrect\n }\n\n}\n"
|
|
2452
2452
|
},
|
|
2453
2453
|
{
|
|
2454
|
-
"id": "
|
|
2455
|
-
"code": "namespace Kata.Verification {\n @EntryPoint()\n operation CheckSolution() : Bool {\n // Test random number generation for 1, 2, 3, 10 bits\n let testCases = [(1, 1000), (2, 1000), (3, 1000), (10, 10000)];\n for (N, runs) in testCases {\n Message($\"Testing N = {N}...\");\n let max = (1 <<< N) - 1;\n let randomnessVerifier = () => CheckUniformDistribution(() =>\n Kata.RandomNBits(N), 0, max, runs);\n let isCorrect = IsSufficientlyRandom(randomnessVerifier);\n if not isCorrect {\n return false;\n }\n Message($\"Test passed for N = {N}\");\n }\n Message(\"
|
|
2454
|
+
"id": "random_numbers__random_n_bits__Verification.qs",
|
|
2455
|
+
"code": "namespace Kata.Verification {\n @EntryPoint()\n operation CheckSolution() : Bool {\n // Test random number generation for 1, 2, 3, 10 bits\n let testCases = [(1, 1000), (2, 1000), (3, 1000), (10, 10000)];\n for (N, runs) in testCases {\n Message($\"Testing N = {N}...\");\n let max = (1 <<< N) - 1;\n let randomnessVerifier = () => CheckUniformDistribution(() =>\n Kata.RandomNBits(N), 0, max, runs);\n let isCorrect = IsSufficientlyRandom(randomnessVerifier);\n if not isCorrect {\n return false;\n }\n Message($\"Test passed for N = {N}\");\n }\n Message(\"Correct!\");\n true\n }\n\n}\n"
|
|
2456
2456
|
},
|
|
2457
2457
|
{
|
|
2458
|
-
"id": "
|
|
2459
|
-
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Convert;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for x in [0.0, 0.25, 0.5, 0.75, 1.0] {\n Message($\"Testing generating zero with {x*100.0}% probability...\");\n let randomnessVerifier = () => CheckXPercentZero(() => Kata.WeightedRandomBit(x), x);\n let isCorrect = IsSufficientlyRandom(randomnessVerifier);\n if not isCorrect {\n return false;\n }\n
|
|
2458
|
+
"id": "random_numbers__weighted_random_bit__Verification.qs",
|
|
2459
|
+
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Convert;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for x in [0.0, 0.25, 0.5, 0.75, 1.0] {\n Message($\"Testing generating zero with {x*100.0}% probability...\");\n let randomnessVerifier = () => CheckXPercentZero(() => Kata.WeightedRandomBit(x), x);\n let isCorrect = IsSufficientlyRandom(randomnessVerifier);\n if not isCorrect {\n return false;\n }\n }\n Message(\"Correct!\");\n true\n }\n\n /// # Summary\n /// Helper operation that checks that the given RNG operation generates zero with x percent probability\n /// # Input\n /// ## op\n /// Random number generation operation to be tested.\n /// ## x\n /// Probability of generating zero\n operation CheckXPercentZero (op : (Unit => Int), x : Double) : Int {\n mutable oneCount = 0;\n let nRuns = 1000;\n for N in 1..nRuns {\n let val = op();\n if (val < 0 or val > 1) {\n Message($\"Unexpected number generated. Expected 0 or 1, instead generated {val}\");\n return 0x1;\n }\n set oneCount += val;\n }\n\n let zeroCount = nRuns - oneCount;\n let goalZeroCount = (x == 0.0) ? 0 | (x == 1.0) ? nRuns | Floor(x * IntAsDouble(nRuns));\n // We don't have tests with probabilities near 0.0 or 1.0, so for those the matching has to be exact\n if (goalZeroCount == 0 or goalZeroCount == nRuns) {\n if zeroCount != goalZeroCount {\n Message($\"Expected {x * 100.0}% 0's, instead got {zeroCount} 0's out of {nRuns}\");\n return 0x2;\n }\n } else {\n if zeroCount < goalZeroCount - 4 * nRuns / 100 {\n Message($\"Unexpectedly low number of 0's generated: expected around {x * IntAsDouble(nRuns)} 0's, got {zeroCount} out of {nRuns}\");\n return 0x3;\n } elif zeroCount > goalZeroCount + 4 * nRuns / 100 {\n Message($\"Unexpectedly high number of 0's generated: expected around {x * IntAsDouble(nRuns)} 0's, got {zeroCount} out of {nRuns}\");\n return 0x4;\n }\n }\n return 0x0;\n }\n\n}\n"
|
|
2460
2460
|
},
|
|
2461
2461
|
{
|
|
2462
|
-
"id": "
|
|
2463
|
-
"code": "namespace Kata.Verification {\n @EntryPoint()\n operation CheckSolution() : Bool {\n let testCases = [(1, 3, 1000), (27, 312, 5000), (0, 3, 1000), (0, 1023, 10000)];\n for (min, max, runs) in testCases {\n Message($\"Testing for min = {min} and max = {max}...\");\n let randomnessVerifier = () => CheckUniformDistribution(() =>\n Kata.RandomNumberInRange(min, max), min, max, runs);\n let isCorrect = IsSufficientlyRandom(randomnessVerifier);\n if not isCorrect {\n return false;\n }\n
|
|
2462
|
+
"id": "random_numbers__random_number__Verification.qs",
|
|
2463
|
+
"code": "namespace Kata.Verification {\n @EntryPoint()\n operation CheckSolution() : Bool {\n let testCases = [(1, 3, 1000), (27, 312, 5000), (0, 3, 1000), (0, 1023, 10000)];\n for (min, max, runs) in testCases {\n Message($\"Testing for min = {min} and max = {max}...\");\n let randomnessVerifier = () => CheckUniformDistribution(() =>\n Kata.RandomNumberInRange(min, max), min, max, runs);\n let isCorrect = IsSufficientlyRandom(randomnessVerifier);\n if not isCorrect {\n return false;\n }\n }\n Message(\"Correct!\");\n true\n }\n}\n"
|
|
2464
2464
|
},
|
|
2465
2465
|
{
|
|
2466
|
-
"id": "
|
|
2467
|
-
"code": "namespace Kata.Verification {\n
|
|
2466
|
+
"id": "oracles__classical_oracles__Verification.qs",
|
|
2467
|
+
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n\n function IsSeven_Reference(x : Bool[]) : Bool {\n return BoolArrayAsInt(x) == 7;\n }\n\n @EntryPoint()\n function CheckSolution() : Bool {\n let N = 3;\n for k in 0..((2^N)-1) {\n let x = IntAsBoolArray(k, N);\n\n let actual = Kata.IsSeven(x);\n let expected = IsSeven_Reference(x);\n\n if actual != expected {\n Message($\"Failed on test case x = {x}: got {actual}, expected {expected}\");\n return false;\n }\n }\n Message(\"All tests passed.\");\n true\n }\n}\n"
|
|
2468
2468
|
},
|
|
2469
2469
|
{
|
|
2470
|
-
"id": "
|
|
2471
|
-
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.
|
|
2470
|
+
"id": "oracles__Common.qs",
|
|
2471
|
+
"code": "namespace Kata.Verification {\n\n open Microsoft.Quantum.Diagnostics;\n\n /// # Summary\n /// Given two registers, prepares the maximally entangled state \n /// between each pair of qubits on the respective registers.\n /// All qubits must start in the |0⟩ state.\n ///\n /// The result is that corresponding pairs of qubits from each register are in the\n /// $\\bra{\\beta_{00}}\\ket{\\beta_{00}}$.\n ///\n /// # Input\n /// ## left\n /// A qubit array in the $\\ket{0\\cdots 0}$ state\n /// ## right\n /// A qubit array in the $\\ket{0\\cdots 0}$ state\n internal operation PrepareEntangledState (left : Qubit[], right : Qubit[]) : Unit\n is Adj + Ctl {\n\n for idxQubit in 0 .. Length(left) - 1\n {\n H(left[idxQubit]);\n Controlled X([left[idxQubit]], right[idxQubit]);\n }\n }\n \n // TODO: Maybe move this to Katas library (or reuse if there's a similar one)\n \n /// # Summary\n /// Given two operations, asserts that they act identically for all input states.\n ///\n /// This assertion is implemented by using the Choi–Jamiołkowski isomorphism to reduce\n /// the assertion to one of a qubit state assertion on two entangled registers.\n /// Thus, this operation needs only a single call to each operation being tested,\n /// but requires twice as many qubits to be allocated.\n /// This assertion can be used to ensure, for instance, that an optimized version of an\n /// operation acts identically to its naïve implementation, or that an operation\n /// which acts on a range of non-quantum inputs agrees with known cases.\n ///\n /// # Remarks\n /// This operation requires that the operation modeling the expected behavior is\n /// adjointable, so that the inverse can be performed on the target register alone.\n /// Formally, one can specify a transpose operation, which relaxes this requirement,\n /// but the transpose operation is not in general physically realizable for arbitrary\n /// quantum operations and thus is not included here as an option.\n ///\n /// # Input\n /// ## nQubits\n /// Number of qubits to pass to each operation.\n /// ## actual\n /// Operation to be tested.\n /// ## expected\n /// Operation defining the expected behavior for the operation under test.\n operation CheckOperationsEqualReferenced (nQubits : Int, actual : (Qubit[] => Unit), expected : (Qubit[] => Unit is Adj)) : Bool {\n // Prepare a reference register entangled with the target register.\n use (reference, target) = (Qubit[nQubits], Qubit[nQubits]) {\n PrepareEntangledState(reference, target);\n actual(target);\n Adjoint expected(target);\n Adjoint PrepareEntangledState(reference, target);\n let isCorrect = CheckAllZero(reference + target);\n ResetAll(target);\n ResetAll(reference);\n isCorrect\n }\n }\n\n // ------------------------------------------------------\n // Helper functions\n operation ApplyOracle (qs : Qubit[], oracle : ((Qubit[], Qubit) => Unit is Adj + Ctl)) : Unit is Adj + Ctl {\n let N = Length(qs);\n oracle(qs[0 .. N - 2], qs[N - 1]);\n }\n\n operation CheckTwoOraclesAreEqual (nQubits : Range,\n oracle1 : ((Qubit[], Qubit) => Unit is Adj + Ctl),\n oracle2 : ((Qubit[], Qubit) => Unit is Adj + Ctl)) : Bool {\n let sol = ApplyOracle(_, oracle1);\n let refSol = ApplyOracle(_, oracle2);\n\n for i in nQubits {\n if not CheckOperationsEqualReferenced(i + 1, sol, refSol) {\n return false;\n }\n }\n true\n }\n\n}\n"
|
|
2472
2472
|
},
|
|
2473
2473
|
{
|
|
2474
|
-
"id": "
|
|
2474
|
+
"id": "oracles__phase_oracle_seven__Verification.qs",
|
|
2475
2475
|
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n\n operation IsSeven_PhaseOracle_Reference(x : Qubit[]) : Unit is Adj + Ctl {\n Controlled Z(Most(x), Tail(x));\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let N = 3;\n let isCorrect = CheckOperationsEqualReferenced(\n N,\n Kata.IsSeven_PhaseOracle,\n IsSeven_PhaseOracle_Reference);\n if isCorrect {\n Message(\"All tests passed.\");\n } else {\n Message(\"Test failed: Operation is not the same as the reference operation.\");\n }\n isCorrect\n }\n}\n"
|
|
2476
2476
|
},
|
|
2477
2477
|
{
|
|
2478
|
-
"id": "
|
|
2478
|
+
"id": "oracles__marking_oracle_seven__Verification.qs",
|
|
2479
2479
|
"code": "namespace Kata.Verification {\n\n operation IsSeven_MarkingOracle_Reference(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n Controlled X(x, y);\n }\n\n // ------------------------------------------------------\n @EntryPoint()\n operation CheckSolution () : Bool {\n let isCorrect = CheckTwoOraclesAreEqual(\n 3..3,\n Kata.IsSeven_MarkingOracle,\n IsSeven_MarkingOracle_Reference);\n if isCorrect {\n Message(\"All tests passed.\");\n } else {\n Message(\"Test failed: Operation is not the same as the reference operation.\");\n }\n isCorrect\n }\n\n}\n"
|
|
2480
2480
|
},
|
|
2481
2481
|
{
|
|
2482
|
-
"id": "
|
|
2482
|
+
"id": "oracles__marking_oracle_as_phase__Verification.qs",
|
|
2483
2483
|
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n\n operation ApplyMarkingOracleAsPhaseOracle_Reference(\n markingOracle : ((Qubit[], Qubit) => Unit is Adj + Ctl),\n qubits : Qubit[]) : Unit is Adj + Ctl {\n\n use minus = Qubit();\n within {\n X(minus);\n H(minus);\n } apply {\n markingOracle(qubits, minus);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 1..5 {\n for k in 0..(2^N-1) {\n let pattern = IntAsBoolArray(k, N);\n\n let isCorrect = CheckOperationsEqualReferenced(\n N,\n qubits => Kata.ApplyMarkingOracleAsPhaseOracle(\n ApplyControlledOnBitString(pattern, X, _, _),\n qubits),\n qubits => ApplyMarkingOracleAsPhaseOracle_Reference(\n ApplyControlledOnBitString(pattern, X, _, _),\n qubits));\n if not isCorrect {\n Message($\"Failed on test pattern {pattern} for a bit pattern oracle.\");\n return false;\n }\n }\n }\n Message(\"All tests passed.\");\n true\n }\n\n}\n"
|
|
2484
2484
|
},
|
|
2485
2485
|
{
|
|
2486
|
-
"id": "
|
|
2486
|
+
"id": "oracles__or_oracle__Verification.qs",
|
|
2487
2487
|
"code": "namespace Kata.Verification {\n operation Or_Oracle_Reference(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n X(y);\n ApplyControlledOnInt(0, X, x, y);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = CheckTwoOraclesAreEqual(1..7, Kata.Or_Oracle, Or_Oracle_Reference);\n if (isCorrect) {\n Message(\"All tests passed.\");\n } else {\n Message(\"Test failed: Operation is not the same as the reference operation.\");\n }\n isCorrect\n }\n}\n"
|
|
2488
2488
|
},
|
|
2489
2489
|
{
|
|
2490
|
-
"id": "
|
|
2490
|
+
"id": "oracles__kth_bit_oracle__Verification.qs",
|
|
2491
2491
|
"code": "namespace Kata.Verification {\n operation KthBit_Oracle_Reference(x : Qubit[], k : Int) : Unit is Adj + Ctl {\n Z(x[k]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 1..5 {\n for k in 0..(N-1) {\n let isCorrect = CheckOperationsEqualReferenced(\n N,\n Kata.KthBit_Oracle(_, k),\n KthBit_Oracle_Reference(_, k));\n if not isCorrect {\n Message($\"Failed on test case for NumberOfQubits = {N}, k = {k}.\");\n return false;\n }\n }\n }\n Message(\"All tests passed.\");\n true\n }\n\n}\n"
|
|
2492
2492
|
},
|
|
2493
2493
|
{
|
|
2494
|
-
"id": "
|
|
2494
|
+
"id": "oracles__or_but_kth_oracle__Verification.qs",
|
|
2495
2495
|
"code": "namespace Kata.Verification {\n\n operation Or_Oracle_Reference(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n X(y);\n ApplyControlledOnInt(0, X, x, y);\n }\n\n operation OrOfBitsExceptKth_Oracle_Reference(x : Qubit[], k : Int) : Unit is Adj + Ctl {\n use minus = Qubit();\n within {\n X(minus);\n H(minus);\n } apply {\n Or_Oracle_Reference(x[...k-1] + x[k+1...], minus);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 1..5 {\n for k in 0..(N-1) {\n let isCorrect = CheckOperationsEqualReferenced(\n N,\n Kata.OrOfBitsExceptKth_Oracle(_, k),\n OrOfBitsExceptKth_Oracle_Reference(_, k));\n if not isCorrect {\n Message($\"Failed on test case for NumberOfQubits = {N}, k = {k}.\");\n return false;\n }\n }\n }\n Message(\"All tests passed.\");\n true\n }\n\n}\n"
|
|
2496
2496
|
},
|
|
2497
2497
|
{
|
|
2498
|
-
"id": "
|
|
2498
|
+
"id": "oracles__bit_pattern_oracle__Verification.qs",
|
|
2499
2499
|
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n\n operation ArbitraryBitPattern_Oracle_Reference(x : Qubit[], y : Qubit, pattern : Bool[]) : Unit is Adj + Ctl {\n ApplyControlledOnBitString(pattern, X, x, y);\n }\n\n // ------------------------------------------------------\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 1..4 {\n for k in 0..((2^N)-1) {\n let pattern = IntAsBoolArray(k, N);\n\n let isCorrect = CheckTwoOraclesAreEqual(\n N..N,\n Kata.ArbitraryBitPattern_Oracle(_, _, pattern),\n ArbitraryBitPattern_Oracle_Reference(_, _, pattern));\n if not isCorrect {\n Message($\"Failed on pattern {pattern}.\");\n return false;\n }\n }\n }\n Message(\"All tests passed.\");\n true\n }\n\n}\n"
|
|
2500
2500
|
},
|
|
2501
2501
|
{
|
|
2502
|
-
"id": "
|
|
2502
|
+
"id": "oracles__bit_pattern_challenge__Verification.qs",
|
|
2503
2503
|
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Arrays;\n\n operation ArbitraryBitPattern_Oracle_Challenge_Reference(x : Qubit[], pattern : Bool[]) : Unit is Adj + Ctl {\n within {\n for i in IndexRange(x) {\n if not pattern[i] {\n X(x[i]);\n }\n }\n } apply {\n Controlled Z(Most(x), Tail(x));\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 1..4 {\n for k in 0..((2^N)-1) {\n let pattern = IntAsBoolArray(k, N);\n\n let isCorrect = CheckOperationsEqualReferenced(\n N,\n Kata.ArbitraryBitPattern_Oracle_Challenge(_, pattern),\n ArbitraryBitPattern_Oracle_Challenge_Reference(_, pattern));\n if not isCorrect {\n Message($\"Failed on pattern {pattern}.\");\n return false;\n }\n }\n }\n Message(\"All tests passed.\");\n true\n }\n\n}\n"
|
|
2504
2504
|
},
|
|
2505
2505
|
{
|
|
2506
|
-
"id": "
|
|
2506
|
+
"id": "oracles__meeting_oracle__Verification.qs",
|
|
2507
2507
|
"code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Convert;\n\n operation Or_Oracle_Reference(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n X(y);\n ApplyControlledOnInt(0, X, x, y);\n }\n\n operation Meeting_Oracle_Reference(x : Qubit[], jasmine : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n use q = Qubit[Length(x)];\n within {\n for i in IndexRange(q) {\n // flip q[i] if both x and jasmine are free on the given day\n X(x[i]);\n X(jasmine[i]);\n CCNOT(x[i], jasmine[i], q[i]);\n }\n } apply {\n Or_Oracle_Reference(q, y);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 1..4 {\n use jasmine = Qubit[N];\n for k in 0..(2^N-1) {\n let binaryJasmine = IntAsBoolArray(k, N);\n mutable isCorrect = false;\n within {\n ApplyPauliFromBitString(PauliX, true, binaryJasmine, jasmine);\n } apply {\n set isCorrect = CheckTwoOraclesAreEqual(\n 1..N,\n Kata.Meeting_Oracle(_, jasmine, _),\n Meeting_Oracle_Reference(_, jasmine, _));\n }\n if not isCorrect {\n Message($\"Failed on test case for N = {N}, k = {k}.\");\n return false;\n }\n }\n }\n Message(\"All tests passed.\");\n true\n }\n\n}\n"
|
|
2508
2508
|
}
|
|
2509
2509
|
]
|