qsharp-lang 1.8.1-dev → 1.8.2-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.
Files changed (272) hide show
  1. package/dist/compiler/worker-browser.d.ts +1 -1
  2. package/dist/debug-service/worker-browser.d.ts +1 -1
  3. package/dist/katas-content.generated.js +73 -73
  4. package/dist/katas-content.generated.md.js +80 -80
  5. package/dist/language-service/worker-browser.d.ts +1 -1
  6. package/dist/samples.generated.js +3 -3
  7. package/dist/workers/common.js +1 -2
  8. package/dist/workers/node.js +1 -1
  9. package/docs/Microsoft.Quantum.Arrays/All.md +1 -1
  10. package/docs/Microsoft.Quantum.Arrays/Any.md +1 -1
  11. package/docs/Microsoft.Quantum.Arrays/Chunks.md +1 -1
  12. package/docs/Microsoft.Quantum.Arrays/CircularlyShifted.md +1 -1
  13. package/docs/Microsoft.Quantum.Arrays/ColumnAt.md +1 -1
  14. package/docs/Microsoft.Quantum.Arrays/Count.md +1 -1
  15. package/docs/Microsoft.Quantum.Arrays/Diagonal.md +1 -1
  16. package/docs/Microsoft.Quantum.Arrays/DrawMany.md +1 -1
  17. package/docs/Microsoft.Quantum.Arrays/Enumerated.md +1 -1
  18. package/docs/Microsoft.Quantum.Arrays/Excluding.md +1 -1
  19. package/docs/Microsoft.Quantum.Arrays/Filtered.md +1 -1
  20. package/docs/Microsoft.Quantum.Arrays/FlatMapped.md +1 -1
  21. package/docs/Microsoft.Quantum.Arrays/Flattened.md +1 -1
  22. package/docs/Microsoft.Quantum.Arrays/Fold.md +1 -1
  23. package/docs/Microsoft.Quantum.Arrays/ForEach.md +1 -1
  24. package/docs/Microsoft.Quantum.Arrays/Head.md +1 -1
  25. package/docs/Microsoft.Quantum.Arrays/HeadAndRest.md +1 -1
  26. package/docs/Microsoft.Quantum.Arrays/IndexOf.md +1 -1
  27. package/docs/Microsoft.Quantum.Arrays/IndexRange.md +1 -1
  28. package/docs/Microsoft.Quantum.Arrays/Interleaved.md +1 -1
  29. package/docs/Microsoft.Quantum.Arrays/IsEmpty.md +1 -1
  30. package/docs/Microsoft.Quantum.Arrays/IsRectangularArray.md +1 -1
  31. package/docs/Microsoft.Quantum.Arrays/IsSorted.md +1 -1
  32. package/docs/Microsoft.Quantum.Arrays/IsSquareArray.md +1 -1
  33. package/docs/Microsoft.Quantum.Arrays/Mapped.md +1 -1
  34. package/docs/Microsoft.Quantum.Arrays/MappedByIndex.md +1 -1
  35. package/docs/Microsoft.Quantum.Arrays/MappedOverRange.md +1 -1
  36. package/docs/Microsoft.Quantum.Arrays/Most.md +1 -1
  37. package/docs/Microsoft.Quantum.Arrays/MostAndTail.md +1 -1
  38. package/docs/Microsoft.Quantum.Arrays/Padded.md +1 -1
  39. package/docs/Microsoft.Quantum.Arrays/Partitioned.md +1 -1
  40. package/docs/Microsoft.Quantum.Arrays/Rest.md +1 -1
  41. package/docs/Microsoft.Quantum.Arrays/Reversed.md +1 -1
  42. package/docs/Microsoft.Quantum.Arrays/SequenceI.md +1 -1
  43. package/docs/Microsoft.Quantum.Arrays/SequenceL.md +1 -1
  44. package/docs/Microsoft.Quantum.Arrays/Sorted.md +1 -1
  45. package/docs/Microsoft.Quantum.Arrays/Subarray.md +1 -1
  46. package/docs/Microsoft.Quantum.Arrays/Swapped.md +1 -1
  47. package/docs/Microsoft.Quantum.Arrays/Tail.md +1 -1
  48. package/docs/Microsoft.Quantum.Arrays/Transposed.md +1 -1
  49. package/docs/Microsoft.Quantum.Arrays/Unzipped.md +1 -1
  50. package/docs/Microsoft.Quantum.Arrays/Where.md +1 -1
  51. package/docs/Microsoft.Quantum.Arrays/Windows.md +1 -1
  52. package/docs/Microsoft.Quantum.Arrays/Zipped.md +1 -1
  53. package/docs/Microsoft.Quantum.Canon/ApplyCNOTChain.md +1 -1
  54. package/docs/Microsoft.Quantum.Canon/ApplyControlledOnBitString.md +1 -1
  55. package/docs/Microsoft.Quantum.Canon/ApplyControlledOnInt.md +1 -1
  56. package/docs/Microsoft.Quantum.Canon/ApplyP.md +1 -1
  57. package/docs/Microsoft.Quantum.Canon/ApplyPauli.md +1 -1
  58. package/docs/Microsoft.Quantum.Canon/ApplyPauliFromBitString.md +1 -1
  59. package/docs/Microsoft.Quantum.Canon/ApplyPauliFromInt.md +1 -1
  60. package/docs/Microsoft.Quantum.Canon/ApplyQFT.md +1 -1
  61. package/docs/Microsoft.Quantum.Canon/ApplyToEach.md +1 -1
  62. package/docs/Microsoft.Quantum.Canon/ApplyToEachA.md +1 -1
  63. package/docs/Microsoft.Quantum.Canon/ApplyToEachC.md +1 -1
  64. package/docs/Microsoft.Quantum.Canon/ApplyToEachCA.md +1 -1
  65. package/docs/Microsoft.Quantum.Canon/ApplyXorInPlace.md +1 -1
  66. package/docs/Microsoft.Quantum.Canon/ApplyXorInPlaceL.md +1 -1
  67. package/docs/Microsoft.Quantum.Canon/CX.md +1 -1
  68. package/docs/Microsoft.Quantum.Canon/CY.md +1 -1
  69. package/docs/Microsoft.Quantum.Canon/CZ.md +1 -1
  70. package/docs/Microsoft.Quantum.Canon/Fst.md +1 -1
  71. package/docs/Microsoft.Quantum.Canon/Snd.md +1 -1
  72. package/docs/Microsoft.Quantum.Canon/SwapReverseRegister.md +1 -1
  73. package/docs/Microsoft.Quantum.Convert/BigIntAsBoolArray.md +1 -1
  74. package/docs/Microsoft.Quantum.Convert/BoolArrayAsBigInt.md +1 -1
  75. package/docs/Microsoft.Quantum.Convert/BoolArrayAsInt.md +1 -1
  76. package/docs/Microsoft.Quantum.Convert/BoolArrayAsResultArray.md +1 -1
  77. package/docs/Microsoft.Quantum.Convert/BoolAsResult.md +1 -1
  78. package/docs/Microsoft.Quantum.Convert/ComplexAsComplexPolar.md +1 -1
  79. package/docs/Microsoft.Quantum.Convert/ComplexPolarAsComplex.md +1 -1
  80. package/docs/Microsoft.Quantum.Convert/DoubleAsStringWithPrecision.md +1 -1
  81. package/docs/Microsoft.Quantum.Convert/IntAsBigInt.md +1 -1
  82. package/docs/Microsoft.Quantum.Convert/IntAsBoolArray.md +1 -1
  83. package/docs/Microsoft.Quantum.Convert/IntAsDouble.md +1 -1
  84. package/docs/Microsoft.Quantum.Convert/ResultArrayAsBoolArray.md +1 -1
  85. package/docs/Microsoft.Quantum.Convert/ResultArrayAsInt.md +1 -1
  86. package/docs/Microsoft.Quantum.Convert/ResultAsBool.md +1 -1
  87. package/docs/Microsoft.Quantum.Core/IsRangeEmpty.md +1 -1
  88. package/docs/Microsoft.Quantum.Core/Length.md +1 -1
  89. package/docs/Microsoft.Quantum.Core/RangeEnd.md +1 -1
  90. package/docs/Microsoft.Quantum.Core/RangeReverse.md +1 -1
  91. package/docs/Microsoft.Quantum.Core/RangeStart.md +1 -1
  92. package/docs/Microsoft.Quantum.Core/RangeStep.md +1 -1
  93. package/docs/Microsoft.Quantum.Core/Repeated.md +1 -1
  94. package/docs/Microsoft.Quantum.Diagnostics/DumpMachine.md +1 -1
  95. package/docs/Microsoft.Quantum.Diagnostics/DumpRegister.md +1 -1
  96. package/docs/Microsoft.Quantum.Diagnostics/Fact.md +1 -1
  97. package/docs/Microsoft.Quantum.Intrinsic/AND.md +1 -1
  98. package/docs/Microsoft.Quantum.Intrinsic/CCNOT.md +1 -1
  99. package/docs/Microsoft.Quantum.Intrinsic/CNOT.md +1 -1
  100. package/docs/Microsoft.Quantum.Intrinsic/Exp.md +1 -1
  101. package/docs/Microsoft.Quantum.Intrinsic/H.md +1 -1
  102. package/docs/Microsoft.Quantum.Intrinsic/I.md +1 -1
  103. package/docs/Microsoft.Quantum.Intrinsic/M.md +1 -1
  104. package/docs/Microsoft.Quantum.Intrinsic/Measure.md +1 -1
  105. package/docs/Microsoft.Quantum.Intrinsic/Message.md +1 -1
  106. package/docs/Microsoft.Quantum.Intrinsic/R.md +1 -1
  107. package/docs/Microsoft.Quantum.Intrinsic/R1.md +1 -1
  108. package/docs/Microsoft.Quantum.Intrinsic/R1Frac.md +1 -1
  109. package/docs/Microsoft.Quantum.Intrinsic/RFrac.md +1 -1
  110. package/docs/Microsoft.Quantum.Intrinsic/Reset.md +1 -1
  111. package/docs/Microsoft.Quantum.Intrinsic/ResetAll.md +1 -1
  112. package/docs/Microsoft.Quantum.Intrinsic/Rx.md +1 -1
  113. package/docs/Microsoft.Quantum.Intrinsic/Rxx.md +1 -1
  114. package/docs/Microsoft.Quantum.Intrinsic/Ry.md +1 -1
  115. package/docs/Microsoft.Quantum.Intrinsic/Ryy.md +1 -1
  116. package/docs/Microsoft.Quantum.Intrinsic/Rz.md +1 -1
  117. package/docs/Microsoft.Quantum.Intrinsic/Rzz.md +1 -1
  118. package/docs/Microsoft.Quantum.Intrinsic/S.md +1 -1
  119. package/docs/Microsoft.Quantum.Intrinsic/SWAP.md +1 -1
  120. package/docs/Microsoft.Quantum.Intrinsic/T.md +1 -1
  121. package/docs/Microsoft.Quantum.Intrinsic/X.md +1 -1
  122. package/docs/Microsoft.Quantum.Intrinsic/Y.md +1 -1
  123. package/docs/Microsoft.Quantum.Intrinsic/Z.md +1 -1
  124. package/docs/Microsoft.Quantum.Logical/Xor.md +1 -1
  125. package/docs/Microsoft.Quantum.Math/AbsComplex.md +1 -1
  126. package/docs/Microsoft.Quantum.Math/AbsComplexPolar.md +1 -1
  127. package/docs/Microsoft.Quantum.Math/AbsD.md +1 -1
  128. package/docs/Microsoft.Quantum.Math/AbsI.md +1 -1
  129. package/docs/Microsoft.Quantum.Math/AbsL.md +1 -1
  130. package/docs/Microsoft.Quantum.Math/AbsSquaredComplex.md +1 -1
  131. package/docs/Microsoft.Quantum.Math/AbsSquaredComplexPolar.md +1 -1
  132. package/docs/Microsoft.Quantum.Math/ApproximateFactorial.md +1 -1
  133. package/docs/Microsoft.Quantum.Math/ArcCos.md +1 -1
  134. package/docs/Microsoft.Quantum.Math/ArcCosh.md +1 -1
  135. package/docs/Microsoft.Quantum.Math/ArcSin.md +1 -1
  136. package/docs/Microsoft.Quantum.Math/ArcSinh.md +1 -1
  137. package/docs/Microsoft.Quantum.Math/ArcTan.md +1 -1
  138. package/docs/Microsoft.Quantum.Math/ArcTan2.md +1 -1
  139. package/docs/Microsoft.Quantum.Math/ArcTanh.md +1 -1
  140. package/docs/Microsoft.Quantum.Math/ArgComplex.md +1 -1
  141. package/docs/Microsoft.Quantum.Math/ArgComplexPolar.md +1 -1
  142. package/docs/Microsoft.Quantum.Math/Binom.md +1 -1
  143. package/docs/Microsoft.Quantum.Math/BitSizeI.md +1 -1
  144. package/docs/Microsoft.Quantum.Math/BitSizeL.md +1 -1
  145. package/docs/Microsoft.Quantum.Math/Ceiling.md +1 -1
  146. package/docs/Microsoft.Quantum.Math/Complex.md +1 -1
  147. package/docs/Microsoft.Quantum.Math/ComplexPolar.md +1 -1
  148. package/docs/Microsoft.Quantum.Math/ContinuedFractionConvergentI.md +1 -1
  149. package/docs/Microsoft.Quantum.Math/ContinuedFractionConvergentL.md +1 -1
  150. package/docs/Microsoft.Quantum.Math/Cos.md +1 -1
  151. package/docs/Microsoft.Quantum.Math/Cosh.md +1 -1
  152. package/docs/Microsoft.Quantum.Math/DivRemI.md +1 -1
  153. package/docs/Microsoft.Quantum.Math/DivRemL.md +1 -1
  154. package/docs/Microsoft.Quantum.Math/DividedByC.md +1 -1
  155. package/docs/Microsoft.Quantum.Math/DividedByCP.md +1 -1
  156. package/docs/Microsoft.Quantum.Math/E.md +1 -1
  157. package/docs/Microsoft.Quantum.Math/ExpModI.md +1 -1
  158. package/docs/Microsoft.Quantum.Math/ExpModL.md +1 -1
  159. package/docs/Microsoft.Quantum.Math/ExtendedGreatestCommonDivisorI.md +1 -1
  160. package/docs/Microsoft.Quantum.Math/ExtendedGreatestCommonDivisorL.md +1 -1
  161. package/docs/Microsoft.Quantum.Math/FactorialI.md +1 -1
  162. package/docs/Microsoft.Quantum.Math/FactorialL.md +1 -1
  163. package/docs/Microsoft.Quantum.Math/Floor.md +1 -1
  164. package/docs/Microsoft.Quantum.Math/GreatestCommonDivisorI.md +1 -1
  165. package/docs/Microsoft.Quantum.Math/GreatestCommonDivisorL.md +1 -1
  166. package/docs/Microsoft.Quantum.Math/HammingWeightI.md +1 -1
  167. package/docs/Microsoft.Quantum.Math/InverseModI.md +1 -1
  168. package/docs/Microsoft.Quantum.Math/InverseModL.md +1 -1
  169. package/docs/Microsoft.Quantum.Math/IsCoprimeI.md +1 -1
  170. package/docs/Microsoft.Quantum.Math/IsCoprimeL.md +1 -1
  171. package/docs/Microsoft.Quantum.Math/IsInfinite.md +1 -1
  172. package/docs/Microsoft.Quantum.Math/IsNaN.md +1 -1
  173. package/docs/Microsoft.Quantum.Math/LargestFixedPoint.md +1 -1
  174. package/docs/Microsoft.Quantum.Math/Lg.md +1 -1
  175. package/docs/Microsoft.Quantum.Math/Log.md +1 -1
  176. package/docs/Microsoft.Quantum.Math/Log10.md +1 -1
  177. package/docs/Microsoft.Quantum.Math/LogFactorialD.md +1 -1
  178. package/docs/Microsoft.Quantum.Math/LogGammaD.md +1 -1
  179. package/docs/Microsoft.Quantum.Math/LogOf2.md +1 -1
  180. package/docs/Microsoft.Quantum.Math/Max.md +1 -1
  181. package/docs/Microsoft.Quantum.Math/MaxD.md +1 -1
  182. package/docs/Microsoft.Quantum.Math/MaxI.md +1 -1
  183. package/docs/Microsoft.Quantum.Math/MaxL.md +1 -1
  184. package/docs/Microsoft.Quantum.Math/Min.md +1 -1
  185. package/docs/Microsoft.Quantum.Math/MinD.md +1 -1
  186. package/docs/Microsoft.Quantum.Math/MinI.md +1 -1
  187. package/docs/Microsoft.Quantum.Math/MinL.md +1 -1
  188. package/docs/Microsoft.Quantum.Math/MinusC.md +1 -1
  189. package/docs/Microsoft.Quantum.Math/MinusCP.md +1 -1
  190. package/docs/Microsoft.Quantum.Math/ModulusI.md +1 -1
  191. package/docs/Microsoft.Quantum.Math/ModulusL.md +1 -1
  192. package/docs/Microsoft.Quantum.Math/NegationC.md +1 -1
  193. package/docs/Microsoft.Quantum.Math/NegationCP.md +1 -1
  194. package/docs/Microsoft.Quantum.Math/PI.md +1 -1
  195. package/docs/Microsoft.Quantum.Math/PNorm.md +1 -1
  196. package/docs/Microsoft.Quantum.Math/PNormalized.md +1 -1
  197. package/docs/Microsoft.Quantum.Math/PlusC.md +1 -1
  198. package/docs/Microsoft.Quantum.Math/PlusCP.md +1 -1
  199. package/docs/Microsoft.Quantum.Math/PowC.md +1 -1
  200. package/docs/Microsoft.Quantum.Math/PowCP.md +1 -1
  201. package/docs/Microsoft.Quantum.Math/RealMod.md +1 -1
  202. package/docs/Microsoft.Quantum.Math/Round.md +1 -1
  203. package/docs/Microsoft.Quantum.Math/SignD.md +1 -1
  204. package/docs/Microsoft.Quantum.Math/SignI.md +1 -1
  205. package/docs/Microsoft.Quantum.Math/SignL.md +1 -1
  206. package/docs/Microsoft.Quantum.Math/Sin.md +1 -1
  207. package/docs/Microsoft.Quantum.Math/Sinh.md +1 -1
  208. package/docs/Microsoft.Quantum.Math/SmallestFixedPoint.md +1 -1
  209. package/docs/Microsoft.Quantum.Math/Sqrt.md +1 -1
  210. package/docs/Microsoft.Quantum.Math/SquaredNorm.md +1 -1
  211. package/docs/Microsoft.Quantum.Math/Tan.md +1 -1
  212. package/docs/Microsoft.Quantum.Math/Tanh.md +1 -1
  213. package/docs/Microsoft.Quantum.Math/TimesC.md +1 -1
  214. package/docs/Microsoft.Quantum.Math/TimesCP.md +1 -1
  215. package/docs/Microsoft.Quantum.Math/TrailingZeroCountI.md +1 -1
  216. package/docs/Microsoft.Quantum.Math/TrailingZeroCountL.md +1 -1
  217. package/docs/Microsoft.Quantum.Math/Truncate.md +1 -1
  218. package/docs/Microsoft.Quantum.Measurement/MResetEachZ.md +1 -1
  219. package/docs/Microsoft.Quantum.Measurement/MResetX.md +1 -1
  220. package/docs/Microsoft.Quantum.Measurement/MResetY.md +1 -1
  221. package/docs/Microsoft.Quantum.Measurement/MResetZ.md +1 -1
  222. package/docs/Microsoft.Quantum.Measurement/MeasureAllZ.md +1 -1
  223. package/docs/Microsoft.Quantum.Measurement/MeasureEachZ.md +1 -1
  224. package/docs/Microsoft.Quantum.Measurement/MeasureInteger.md +1 -1
  225. package/docs/Microsoft.Quantum.ResourceEstimation/AccountForEstimates.md +1 -1
  226. package/docs/Microsoft.Quantum.ResourceEstimation/AuxQubitCount.md +1 -1
  227. package/docs/Microsoft.Quantum.ResourceEstimation/BeginEstimateCaching.md +1 -1
  228. package/docs/Microsoft.Quantum.ResourceEstimation/BeginRepeatEstimates.md +1 -1
  229. package/docs/Microsoft.Quantum.ResourceEstimation/CczCount.md +1 -1
  230. package/docs/Microsoft.Quantum.ResourceEstimation/EndEstimateCaching.md +1 -1
  231. package/docs/Microsoft.Quantum.ResourceEstimation/EndRepeatEstimates.md +1 -1
  232. package/docs/Microsoft.Quantum.ResourceEstimation/MeasurementCount.md +1 -1
  233. package/docs/Microsoft.Quantum.ResourceEstimation/PSSPCLayout.md +1 -1
  234. package/docs/Microsoft.Quantum.ResourceEstimation/RepeatEstimates.md +1 -1
  235. package/docs/Microsoft.Quantum.ResourceEstimation/RotationCount.md +1 -1
  236. package/docs/Microsoft.Quantum.ResourceEstimation/RotationDepth.md +1 -1
  237. package/docs/Microsoft.Quantum.ResourceEstimation/SingleVariant.md +1 -1
  238. package/docs/Microsoft.Quantum.ResourceEstimation/TCount.md +1 -1
  239. package/docs/Microsoft.Quantum.Unstable.Arithmetic/AddLE.md +1 -1
  240. package/docs/Microsoft.Quantum.Unstable.Arithmetic/ApplyIfEqualL.md +1 -1
  241. package/docs/Microsoft.Quantum.Unstable.Arithmetic/ApplyIfEqualLE.md +1 -1
  242. package/docs/Microsoft.Quantum.Unstable.Arithmetic/ApplyIfGreaterL.md +1 -1
  243. package/docs/Microsoft.Quantum.Unstable.Arithmetic/ApplyIfGreaterLE.md +1 -1
  244. package/docs/Microsoft.Quantum.Unstable.Arithmetic/ApplyIfGreaterOrEqualL.md +1 -1
  245. package/docs/Microsoft.Quantum.Unstable.Arithmetic/ApplyIfGreaterOrEqualLE.md +1 -1
  246. package/docs/Microsoft.Quantum.Unstable.Arithmetic/ApplyIfLessL.md +1 -1
  247. package/docs/Microsoft.Quantum.Unstable.Arithmetic/ApplyIfLessLE.md +1 -1
  248. package/docs/Microsoft.Quantum.Unstable.Arithmetic/ApplyIfLessOrEqualL.md +1 -1
  249. package/docs/Microsoft.Quantum.Unstable.Arithmetic/ApplyIfLessOrEqualLE.md +1 -1
  250. package/docs/Microsoft.Quantum.Unstable.Arithmetic/FourierTDIncByLE.md +1 -1
  251. package/docs/Microsoft.Quantum.Unstable.Arithmetic/IncByI.md +1 -1
  252. package/docs/Microsoft.Quantum.Unstable.Arithmetic/IncByIUsingIncByLE.md +1 -1
  253. package/docs/Microsoft.Quantum.Unstable.Arithmetic/IncByL.md +1 -1
  254. package/docs/Microsoft.Quantum.Unstable.Arithmetic/IncByLE.md +1 -1
  255. package/docs/Microsoft.Quantum.Unstable.Arithmetic/IncByLEUsingAddLE.md +1 -1
  256. package/docs/Microsoft.Quantum.Unstable.Arithmetic/IncByLUsingIncByLE.md +1 -1
  257. package/docs/Microsoft.Quantum.Unstable.Arithmetic/LookAheadDKRSAddLE.md +1 -1
  258. package/docs/Microsoft.Quantum.Unstable.Arithmetic/MAJ.md +1 -1
  259. package/docs/Microsoft.Quantum.Unstable.Arithmetic/ReflectAboutInteger.md +1 -1
  260. package/docs/Microsoft.Quantum.Unstable.Arithmetic/RippleCarryCGAddLE.md +1 -1
  261. package/docs/Microsoft.Quantum.Unstable.Arithmetic/RippleCarryCGIncByLE.md +1 -1
  262. package/docs/Microsoft.Quantum.Unstable.Arithmetic/RippleCarryTTKIncByLE.md +1 -1
  263. package/docs/Microsoft.Quantum.Unstable.StatePreparation/ApproximatelyPreparePureStateCP.md +1 -1
  264. package/docs/Microsoft.Quantum.Unstable.StatePreparation/PreparePureStateD.md +1 -1
  265. package/docs/Microsoft.Quantum.Unstable.TableLookup/Select.md +1 -1
  266. package/lib/node/qsc_wasm.cjs +150 -165
  267. package/lib/node/qsc_wasm_bg.wasm +0 -0
  268. package/lib/web/qsc_wasm.d.ts +7 -7
  269. package/lib/web/qsc_wasm.js +173 -178
  270. package/lib/web/qsc_wasm_bg.wasm +0 -0
  271. package/package.json +1 -1
  272. package/ux/histogram.tsx +2 -3
@@ -108,7 +108,7 @@ export default {
108
108
  "items": [
109
109
  {
110
110
  "type": "text-content",
111
- "content": "<p>Adding imaginary numbers to each other is quite simple, but what happens when you add a real number to an imaginary number? The result of that addition will be partly real and partly imaginary, otherwise known as a <strong>complex number</strong>. A complex number is simply the real part and the imaginary part being treated as a single number. Complex numbers are generally written as the sum of their two parts $a$ and $bi$, where both $a$ and $b$ are real numbers:</p>\n<p>$$a+bi$$</p>\n<p>For example, $3+4i$ or $-5-7i$ are valid complex numbers. Note that purely real or purely imaginary numbers can also be written as complex numbers: $2$ is $2+0i$, and $-3i$ is $0-3i$.</p>\n<p>When performing operations on complex numbers, it's often helpful to treat them as polynomials in terms of $i$.\nLet's see how to do the main arithmetic operations on complex numbers.</p>\n<blockquote>\n<p>In Q#, complex numbers are represented as user-defined type <code>Complex</code> from the <code>Microsoft.Quantum.Math</code> namespace.</p>\n<p>You can convert a complex number $x = a + bi$ into a tuple of two <code>Double</code> numbers using unwrap operator and tuple deconstruction: <code>let (a, b) = x!;</code>,\nor access its real and imaginary parts using their names: <code>let (a, b) = (x::Real, x::Imag);</code>.</p>\n<p>You can construct a complex number from its real and imaginary parts as follows: <code>let x = Complex(a, b);</code>.</p>\n</blockquote>\n"
111
+ "content": "<p>Adding imaginary numbers to each other is quite simple, but what happens when you add a real number to an imaginary number? The result of that addition will be partly real and partly imaginary, otherwise known as a <strong>complex number</strong>. A complex number is simply the real part and the imaginary part being treated as a single number. Complex numbers are generally written as the sum of their two parts $a$ and $bi$, where both $a$ and $b$ are real numbers:</p>\n<p>$$a+bi$$</p>\n<p>For example, $3+4i$ or $-5-7i$ are valid complex numbers. Note that purely real or purely imaginary numbers can also be written as complex numbers: $2$ is $2+0i$, and $-3i$ is $0-3i$.</p>\n<p>When performing operations on complex numbers, it's often helpful to treat them as polynomials in terms of $i$.\nLet's see how to do the main arithmetic operations on complex numbers.</p>\n<blockquote>\n<p>In Q#, complex numbers are represented as user-defined struct type <code>Complex</code> from the <code>Microsoft.Quantum.Math</code> namespace.</p>\n<p>Given a complex number $x = a + bi$, you can access its real and imaginary parts using their names: <code>let (a, b) = (x.Real, x.Imag);</code>.</p>\n<p>You can construct a complex number from its real and imaginary parts as follows: <code>let x = Complex(a, b);</code>.</p>\n</blockquote>\n"
112
112
  }
113
113
  ]
114
114
  },
@@ -125,7 +125,7 @@ export default {
125
125
  "KatasLibrary.qs",
126
126
  "complex_arithmetic__Common.qs"
127
127
  ],
128
- "placeholderCode": "namespace Kata { \n open Microsoft.Quantum.Math;\n \n function ComplexAdd(x : Complex, y : Complex) : Complex { \n // Extract real and imaginary components of the inputs.\n let (a, b) = x!;\n let (c, d) = (y::Real, y::Imag);\n // Implement your solution here...\n return Complex(0., 0.);\n }\n}\n",
128
+ "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function ComplexAdd(x : Complex, y : Complex) : Complex {\n // Extract real and imaginary components of the inputs.\n let (a, b) = (x.Real, x.Imag);\n // Implement your solution here...\n return Complex(0., 0.);\n }\n}\n",
129
129
  "explainedSolution": {
130
130
  "type": "explained-solution",
131
131
  "items": [
@@ -136,7 +136,7 @@ export default {
136
136
  {
137
137
  "type": "solution",
138
138
  "id": "complex_arithmetic__complex_addition_solution",
139
- "code": "namespace Kata { \n open Microsoft.Quantum.Math;\n \n function ComplexAdd(x : Complex, y: Complex) : Complex { \n let (a, b) = x!;\n let (c, d) = y!;\n return Complex(a + c, b + d);\n }\n}\n"
139
+ "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function ComplexAdd(x : Complex, y : Complex) : Complex {\n Complex(x.Real + y.Real, x.Imag + y.Imag)\n }\n}\n"
140
140
  }
141
141
  ]
142
142
  }
@@ -154,7 +154,7 @@ export default {
154
154
  "KatasLibrary.qs",
155
155
  "complex_arithmetic__Common.qs"
156
156
  ],
157
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n \n function ComplexMult(x : Complex, y: Complex) : Complex {\n // Implement your solution here...\n return Complex(0., 0.);\n }\n}\n",
157
+ "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function ComplexMult(x : Complex, y : Complex) : Complex {\n // Implement your solution here...\n return Complex(0., 0.);\n }\n}\n",
158
158
  "explainedSolution": {
159
159
  "type": "explained-solution",
160
160
  "items": [
@@ -165,7 +165,7 @@ export default {
165
165
  {
166
166
  "type": "solution",
167
167
  "id": "complex_arithmetic__complex_multiplication_solution",
168
- "code": "namespace Kata { \n open Microsoft.Quantum.Math;\n \n function ComplexMult(x : Complex, y: Complex) : Complex {\n let (a, b) = x!;\n let (c, d) = y!;\n return Complex(a * c - b * d, a * d + b * c);\n }\n}\n"
168
+ "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function ComplexMult(x : Complex, y : Complex) : Complex {\n let (a, b) = (x.Real, x.Imag);\n let (c, d) = (y.Real, y.Imag);\n return Complex(a * c - b * d, a * d + b * c);\n }\n}\n"
169
169
  }
170
170
  ]
171
171
  }
@@ -194,7 +194,7 @@ export default {
194
194
  "KatasLibrary.qs",
195
195
  "complex_arithmetic__Common.qs"
196
196
  ],
197
- "placeholderCode": "namespace Kata { \n open Microsoft.Quantum.Math;\n \n function ComplexConjugate(x : Complex) : Complex { \n // Implement your solution here...\n return Complex(0., 0.);\n }\n}\n",
197
+ "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function ComplexConjugate(x : Complex) : Complex {\n // Implement your solution here...\n return Complex(0., 0.);\n }\n}\n",
198
198
  "explainedSolution": {
199
199
  "type": "explained-solution",
200
200
  "items": [
@@ -205,7 +205,7 @@ export default {
205
205
  {
206
206
  "type": "solution",
207
207
  "id": "complex_arithmetic__complex_conjugate_solution",
208
- "code": "namespace Kata { \n open Microsoft.Quantum.Math; \n\n operation ComplexConjugate(x : Complex) : Complex {\n return Complex(x::Real, -x::Imag);\n }\n}\n"
208
+ "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function ComplexConjugate(x : Complex) : Complex {\n Complex(x.Real, -x.Imag)\n }\n}\n"
209
209
  }
210
210
  ]
211
211
  }
@@ -234,7 +234,7 @@ export default {
234
234
  "KatasLibrary.qs",
235
235
  "complex_arithmetic__Common.qs"
236
236
  ],
237
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math; \n \n function ComplexDiv(x : Complex, y : Complex) : Complex {\n // Implement your solution here...\n return Complex(0., 0.);\n }\n}\n",
237
+ "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function ComplexDiv(x : Complex, y : Complex) : Complex {\n // Implement your solution here...\n return Complex(0., 0.);\n }\n}\n",
238
238
  "explainedSolution": {
239
239
  "type": "explained-solution",
240
240
  "items": [
@@ -245,7 +245,7 @@ export default {
245
245
  {
246
246
  "type": "solution",
247
247
  "id": "complex_arithmetic__complex_division_solution",
248
- "code": "namespace Kata { \n open Microsoft.Quantum.Math;\n \n function ComplexDiv(x : Complex, y: Complex) : Complex {\n let (a, b) = x!;\n let (c, d) = y!;\n let denominator = c * c + d * d;\n let real = (a * c + b * d) / denominator;\n let imag = (- a * d + b * c) / denominator;\n return Complex(real, imag);\n }\n}"
248
+ "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function ComplexDiv(x : Complex, y : Complex) : Complex {\n let (a, b) = (x.Real, x.Imag);\n let (c, d) = (y.Real, y.Imag);\n let denominator = c * c + d * d;\n let real = (a * c + b * d) / denominator;\n let imag = (- a * d + b * c) / denominator;\n return Complex(real, imag);\n }\n}\n"
249
249
  }
250
250
  ]
251
251
  }
@@ -285,7 +285,7 @@ export default {
285
285
  {
286
286
  "type": "solution",
287
287
  "id": "complex_arithmetic__complex_modulus_solution",
288
- "code": "namespace Kata { \n open Microsoft.Quantum.Math;\n\n function ComplexModulus(x : Complex) : Double {\n let (a, b) = x!;\n return Sqrt(a * a + b * b);\n }\n}\n"
288
+ "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function ComplexModulus(x : Complex) : Double {\n let (a, b) = (x.Real, x.Imag);\n return Sqrt(a * a + b * b);\n }\n}\n"
289
289
  }
290
290
  ]
291
291
  }
@@ -314,18 +314,18 @@ export default {
314
314
  "KatasLibrary.qs",
315
315
  "complex_arithmetic__Common.qs"
316
316
  ],
317
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n \n function ComplexExponent(x : Complex) : Complex {\n // Implement your solution here...\n return Complex(0.0, 0.0);\n }\n}\n",
317
+ "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function ComplexExponent(x : Complex) : Complex {\n // Implement your solution here...\n return Complex(0., 0.);\n }\n}\n",
318
318
  "explainedSolution": {
319
319
  "type": "explained-solution",
320
320
  "items": [
321
321
  {
322
322
  "type": "text-content",
323
- "content": "<p>To start, you'll rewrite the expression $e^{a + bi}$ as a product of two simpler expressions: $ e^a \\cdot\\ e^{bi} $.\nThe first part is a real number.\nThe second part can be expressed using the formula $e^{i\\theta} = \\cos \\theta + i\\sin \\theta$.\nSubstituting this into our expression gives:\n$$ e^a(\\cos b + i\\sin b) = \\underset{real}{\\underbrace{e^a \\cos b}} + \\underset{imaginary}{\\underbrace{e^a \\sin b}}i $$</p>\n"
323
+ "content": "<p>To start, you'll rewrite the expression $e^{a + bi}$ as a product of two simpler expressions: $ e^a \\cdot\\ e^{bi} $.\nThe first part is a real number.\nThe second part can be expressed using the formula $e^{i\\theta} = \\cos \\theta + i\\sin \\theta$.\nSubstituting this into the expression gives:\n$$ e^a(\\cos b + i\\sin b) = \\underset{real}{\\underbrace{e^a \\cos b}} + \\underset{imaginary}{\\underbrace{e^a \\sin b}}i $$</p>\n"
324
324
  },
325
325
  {
326
326
  "type": "solution",
327
327
  "id": "complex_arithmetic__complex_exponents_solution",
328
- "code": "namespace Kata { \n open Microsoft.Quantum.Math;\n\n function ComplexExponent (x : Complex) : Complex {\n let (a, b) = x!;\n return Complex(E()^a * Cos(b), E()^a * Sin(b));\n }\n}\n"
328
+ "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function ComplexExponent(x : Complex) : Complex {\n Complex(E()^x.Real * Cos(x.Imag), E()^x.Real * Sin(x.Imag))\n }\n}\n"
329
329
  }
330
330
  ]
331
331
  }
@@ -354,7 +354,7 @@ export default {
354
354
  {
355
355
  "type": "solution",
356
356
  "id": "complex_arithmetic__complex_powers_real_solution",
357
- "code": "namespace Kata { \n open Microsoft.Quantum.Math;\n\n function ComplexExpReal(r : Double, x : Complex) : Complex {\n if AbsD(r) < 1e-9 {\n return Complex(0.0, 0.0);\n }\n \n let (a, b) = x!; \n let ra = r ^ a;\n let lnr = Log(r);\n return Complex(ra * Cos(b * lnr), ra * Sin(b * lnr));\n }\n}\n"
357
+ "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function ComplexExpReal(r : Double, x : Complex) : Complex {\n if AbsD(r) < 1e-9 {\n return Complex(0., 0.);\n }\n\n let ra = r^x.Real;\n let lnr = Log(r);\n return Complex(ra * Cos(x.Imag * lnr), ra * Sin(x.Imag * lnr));\n }\n}\n"
358
358
  }
359
359
  ]
360
360
  }
@@ -366,7 +366,7 @@ export default {
366
366
  "items": [
367
367
  {
368
368
  "type": "text-content",
369
- "content": "<p>Consider the expression $e^{i\\theta} = \\cos\\theta + i\\sin\\theta$. Notice that if you map this number onto the complex plane, it'll land on a <strong>unit circle</strong> around $0 + 0i$. This means that its modulus is always $1$. You can also verify this algebraically: $\\cos^2\\theta + \\sin^2\\theta = 1$.</p>\n<p>Using this fact you can represent complex numbers using <strong>polar coordinates</strong>. In a polar coordinate system, a point is represented by two numbers: its direction from origin, represented by an angle from the $x$ axis, and how far away it is in that direction.</p>\n<p>Another way to think about this is that you're taking a point that is $1$ unit away (which is on the unit circle) in the specified direction, and multiplying it by the desired distance. And to get the point on the unit circle, you can use $e^{i\\theta}$.</p>\n<p>A complex number of the format $r \\cdot e^{i\\theta}$ will be represented by a point which is $r$ units away from the origin, in the direction specified by the angle $\\theta$.\nSometimes $\\theta$ will be referred to as the number's <strong>argument</strong> or <strong>phase</strong>.</p>\n<blockquote>\n<p>In Q#, complex numbers in polar form are represented as user-defined type <code>ComplexPolar</code> from the <code>Microsoft.Quantum.Math</code> namespace.</p>\n<p>You can convert a complex number $x = r \\cdot e^{i\\theta}$ into a tuple of two <code>Double</code> numbers using unwrap operator and tuple deconstruction: <code>let (r, theta) = x!;</code>,\nor access its magnitude and phase using their names: <code>let (r, theta) = (x::Magnitude, x::Argument);</code>.</p>\n<p>You can construct a complex number from its magnitude and phase as follows: <code>let x = ComplexPolar(r, theta);</code>.</p>\n</blockquote>\n"
369
+ "content": "<p>Consider the expression $e^{i\\theta} = \\cos\\theta + i\\sin\\theta$. Notice that if you map this number onto the complex plane, it'll land on a <strong>unit circle</strong> around $0 + 0i$. This means that its modulus is always $1$. You can also verify this algebraically: $\\cos^2\\theta + \\sin^2\\theta = 1$.</p>\n<p>Using this fact you can represent complex numbers using <strong>polar coordinates</strong>. In a polar coordinate system, a point is represented by two numbers: its direction from origin, represented by an angle from the $x$ axis, and how far away it is in that direction.</p>\n<p>Another way to think about this is that you're taking a point that is $1$ unit away (which is on the unit circle) in the specified direction, and multiplying it by the desired distance. And to get the point on the unit circle, you can use $e^{i\\theta}$.</p>\n<p>A complex number of the format $r \\cdot e^{i\\theta}$ will be represented by a point which is $r$ units away from the origin, in the direction specified by the angle $\\theta$.\nSometimes $\\theta$ will be referred to as the number's <strong>argument</strong> or <strong>phase</strong>.</p>\n<blockquote>\n<p>In Q#, complex numbers in polar form are represented as user-defined struct type <code>ComplexPolar</code> from the <code>Microsoft.Quantum.Math</code> namespace.</p>\n<p>Given a complex number $x = r \\cdot e^{i\\theta}$, you can access its magnitude and phase using their names: <code>let r = x.Magnitude;</code> and <code>let theta = x.Argument;</code>.</p>\n<p>You can construct a complex number from its magnitude and phase as follows: <code>let x = ComplexPolar(r, theta);</code>.</p>\n</blockquote>\n"
370
370
  }
371
371
  ]
372
372
  },
@@ -394,7 +394,7 @@ export default {
394
394
  {
395
395
  "type": "solution",
396
396
  "id": "complex_arithmetic__cartesian_to_polar_solution",
397
- "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n \n function ComplexToComplexPolar(x : Complex) : ComplexPolar {\n let (a, b) = x!;\n return ComplexPolar(Sqrt(a * a + b * b), ArcTan2(b, a));\n }\n}\n"
397
+ "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function ComplexToComplexPolar(x : Complex) : ComplexPolar {\n let (a, b) = (x.Real, x.Imag);\n return ComplexPolar(Sqrt(a * a + b * b), ArcTan2(b, a));\n }\n}\n"
398
398
  }
399
399
  ]
400
400
  }
@@ -412,7 +412,7 @@ export default {
412
412
  "KatasLibrary.qs",
413
413
  "complex_arithmetic__Common.qs"
414
414
  ],
415
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n \n function ComplexPolarToComplex(x : ComplexPolar) : Complex {\n // Implement your solution here...\n return Complex(0., 0.);\n }\n}\n",
415
+ "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function ComplexPolarToComplex(x : ComplexPolar) : Complex {\n // Implement your solution here...\n return Complex(0., 0.);\n }\n}\n",
416
416
  "explainedSolution": {
417
417
  "type": "explained-solution",
418
418
  "items": [
@@ -423,7 +423,7 @@ export default {
423
423
  {
424
424
  "type": "solution",
425
425
  "id": "complex_arithmetic__polar_to_cartesian_solution",
426
- "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n \n function ComplexPolarToComplex(x : ComplexPolar) : Complex {\n let (r, theta) = x!;\n return Complex(r * Cos(theta), r * Sin(theta));\n }\n}\n"
426
+ "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function ComplexPolarToComplex(x : ComplexPolar) : Complex {\n let (r, theta) = (x.Magnitude, x.Argument);\n return Complex(r * Cos(theta), r * Sin(theta));\n }\n}\n"
427
427
  }
428
428
  ]
429
429
  }
@@ -441,7 +441,7 @@ export default {
441
441
  "KatasLibrary.qs",
442
442
  "complex_arithmetic__Common.qs"
443
443
  ],
444
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n \n function ComplexPolarMult(x : ComplexPolar, y: ComplexPolar) : ComplexPolar {\n // Implement your solution here...\n return ComplexPolar(0., 0.);\n }\n}\n",
444
+ "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n \n function ComplexPolarMult(x : ComplexPolar, y : ComplexPolar) : ComplexPolar {\n // Implement your solution here...\n return ComplexPolar(0., 0.);\n }\n}\n",
445
445
  "explainedSolution": {
446
446
  "type": "explained-solution",
447
447
  "items": [
@@ -452,7 +452,7 @@ export default {
452
452
  {
453
453
  "type": "solution",
454
454
  "id": "complex_arithmetic__polar_multiplication_solution",
455
- "code": "namespace Kata { \n open Microsoft.Quantum.Math;\n \n function ComplexPolarMult(x : ComplexPolar, y: ComplexPolar) : ComplexPolar {\n let (r1, theta1) = x!;\n let (r2, theta2) = y!;\n mutable theta3 = theta1 + theta2;\n if theta3 > PI() {\n set theta3 -= 2.0 * PI();\n }\n if theta3 < -PI() {\n set theta3 += 2.0 * PI();\n }\n return ComplexPolar(r1 * r2, theta3);\n }\n}\n"
455
+ "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function ComplexPolarMult(x : ComplexPolar, y : ComplexPolar) : ComplexPolar {\n mutable theta = x.Argument + y.Argument;\n if theta > PI() {\n set theta -= 2.0 * PI();\n }\n if theta < -PI() {\n set theta += 2.0 * PI();\n }\n return ComplexPolar(x.Magnitude * y.Magnitude, theta);\n }\n}\n"
456
456
  }
457
457
  ]
458
458
  }
@@ -795,7 +795,7 @@ export default {
795
795
  "items": [
796
796
  {
797
797
  "type": "text-content",
798
- "content": "<p>The <strong>inner product</strong> is yet another important matrix operation that is only applied to vectors. Given two vectors $V$ and $W$ of the same size, their inner product $\\langle V , W \\rangle$ is defined as a product of matrices $V^\\dagger$ and $W$:</p>\n<p>$$\\langle V , W \\rangle = V^\\dagger W$$</p>\n<p>Let's break this down so it's a bit easier to understand. A $1 \\times n$ matrix (the adjoint of an $n \\times 1$ vector) multiplied by an $n \\times 1$ vector results in a $1 \\times 1$ matrix, which is equivalent to a scalar. The result of an inner product is that scalar.</p>\n<p>That is, to calculate the inner product of two vectors, take the corresponding elements $V_k$ and $W_k$, multiply the complex conjugate of $V_k$ by $W_k$, and add up those products:</p>\n<p>$$\\langle V , W \\rangle = \\sum_{k=0}^{n-1}\\overline{V_k}W_k$$</p>\n<p>If you're familiar with the <strong>dot product</strong>, you'll notice that it's equivalent to inner product for real-numbered vectors.</p>\n<blockquote>\n<p>We use our definition for these tutorials because it matches the notation used in quantum computing. You might encounter other sources which define the inner product a little differently: $\\langle V , W \\rangle = W^\\dagger V = V^T\\overline{W}$, in contrast to the $V^\\dagger W$ that is used here. These definitions are almost equivalent, with some differences in the scalar multiplication by a complex number.</p>\n</blockquote>\n<p>An immediate application for the inner product is computing the <strong>vector norm</strong>. The norm of vector $V$ is defined as $||V|| = \\sqrt{\\langle V , V \\rangle}$. This condenses the vector down to a single non-negative real value. If the vector represents coordinates in space, the norm happens to be the length of the vector. A vector is called <strong>normalized</strong> if its norm is equal to $1$.</p>\n<p>The inner product has the following properties:</p>\n<ul>\n<li>Distributivity over addition: $\\langle V + W , X \\rangle = \\langle V , X \\rangle + \\langle W , X \\rangle$ and $\\langle V , W + X \\rangle = \\langle V , W \\rangle + \\langle V , X \\rangle$</li>\n<li>Partial associativity with scalar multiplication: $x \\cdot \\langle V , W \\rangle = \\langle \\overline{x}V , W \\rangle = \\langle V , xW \\rangle$</li>\n<li>Skew symmetry: $\\langle V , W \\rangle = \\overline{\\langle W , V \\rangle}$</li>\n<li>Multiplying a vector by a unitary matrix <strong>preserves the vector's inner product with itself</strong> (and therefore the vector's norm): $\\langle UV , UV \\rangle = \\langle V , V \\rangle$</li>\n</ul>\n<blockquote>\n<p>Note that just like matrix multiplication, the inner product <strong>isn't commutative</strong>: $\\langle V , W \\rangle$ won't always equal $\\langle W , V \\rangle$.</p>\n</blockquote>\n"
798
+ "content": "<p>The <strong>inner product</strong> is yet another important matrix operation that is only applied to vectors. Given two vectors $V$ and $W$ of the same size, their inner product $\\langle V , W \\rangle$ is defined as a product of matrices $V^\\dagger$ and $W$:</p>\n<p>$$\\langle V , W \\rangle = V^\\dagger W$$</p>\n<p>Let's break this down so it's a bit easier to understand. A $1 \\times n$ matrix (the adjoint of an $n \\times 1$ vector) multiplied by an $n \\times 1$ vector results in a $1 \\times 1$ matrix, which is equivalent to a scalar. The result of an inner product is that scalar.</p>\n<p>That is, to calculate the inner product of two vectors, take the corresponding elements $V_k$ and $W_k$, multiply the complex conjugate of $V_k$ by $W_k$, and add up those products:</p>\n<p>$$\\langle V , W \\rangle = \\sum_{k=0}^{n-1}\\overline{V_k}W_k$$</p>\n<p>If you're familiar with the <strong>dot product</strong>, you'll notice that it's equivalent to inner product for real-numbered vectors.</p>\n<blockquote>\n<p>You might encounter other sources which define the inner product a little differently: $\\langle V , W \\rangle = W^\\dagger V = V^T\\overline{W}$, in contrast to the $V^\\dagger W$ that is used here. These definitions are almost equivalent, with some differences in the scalar multiplication by a complex number.</p>\n</blockquote>\n<p>An immediate application for the inner product is computing the <strong>vector norm</strong>. The norm of vector $V$ is defined as $||V|| = \\sqrt{\\langle V , V \\rangle}$. This condenses the vector down to a single non-negative real value. If the vector represents coordinates in space, the norm happens to be the length of the vector. A vector is called <strong>normalized</strong> if its norm is equal to $1$.</p>\n<p>The inner product has the following properties:</p>\n<ul>\n<li>Distributivity over addition: $\\langle V + W , X \\rangle = \\langle V , X \\rangle + \\langle W , X \\rangle$ and $\\langle V , W + X \\rangle = \\langle V , W \\rangle + \\langle V , X \\rangle$</li>\n<li>Partial associativity with scalar multiplication: $x \\cdot \\langle V , W \\rangle = \\langle \\overline{x}V , W \\rangle = \\langle V , xW \\rangle$</li>\n<li>Skew symmetry: $\\langle V , W \\rangle = \\overline{\\langle W , V \\rangle}$</li>\n<li>Multiplying a vector by a unitary matrix <strong>preserves the vector's inner product with itself</strong> (and therefore the vector's norm): $\\langle UV , UV \\rangle = \\langle V , V \\rangle$</li>\n</ul>\n<blockquote>\n<p>Note that just like matrix multiplication, the inner product <strong>isn't commutative</strong>: $\\langle V , W \\rangle$ won't always equal $\\langle W , V \\rangle$.</p>\n</blockquote>\n"
799
799
  }
800
800
  ]
801
801
  },
@@ -1006,7 +1006,7 @@ export default {
1006
1006
  "items": [
1007
1007
  {
1008
1008
  "type": "text-content",
1009
- "content": "<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>Qubits aren't an ordinary data type, so the variables of this type have to be declared and initialized (&quot;allocated&quot;) 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 $\\ket{0}$, 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 $\\ket{0}$, it will result in a runtime error. You will see why it is important later, when you 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\\ket{0} + \\beta\\ket{1}$$</p>\n<p>If this program runs on a physical quantum system, there's 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 can't write any non-physical code in Q#!</p>\n</blockquote>\n"
1009
+ "content": "<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>Qubits aren't an ordinary data type, so the variables of this type have to be declared and initialized (&quot;allocated&quot;) 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 $\\ket{0}$, 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 $\\ket{0}$, it will result in a runtime error. You will see why it is important later, when you look at multi-qubit systems.</p>\n<h2>Visualizing Quantum State</h2>\n<p>Before continuing, let's learn some techniques to visualize the quantum state of the 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\\ket{0} + \\beta\\ket{1}$$</p>\n<p>If this program runs on a physical quantum system, there's 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 can't write any non-physical code in Q#!</p>\n</blockquote>\n"
1010
1010
  },
1011
1011
  {
1012
1012
  "type": "example",
@@ -1083,7 +1083,7 @@ export default {
1083
1083
  "items": [
1084
1084
  {
1085
1085
  "type": "text-content",
1086
- "content": "<p>There are certain properties common to all quantum gates. This section introduces 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 &amp; 1 \\\\ 1 &amp; 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\\ket{0} =\n\\begin{bmatrix} 0 &amp; 1 \\\\ 1 &amp; 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\\ket{1} =\n\\begin{bmatrix} 0 &amp; 1 \\\\ 1 &amp; 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>$$\\ket{\\psi} = \\alpha\\ket{0} + \\beta\\ket{1}$$</p>\n<p>$$\nX\\ket{\\psi} =\n\\begin{bmatrix} 0 &amp; 1 \\\\ 1 &amp; 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 $\\ket{0}$, $\\ket{1}$, and $\\ket{\\psi}$ mean, you can review the section on Dirac notation in &quot;The Qubit&quot; 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 &quot;gate&quot; and &quot;gate matrix&quot; 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 $\\ket{\\psi}$, the result of applying $A$ followed by $B$ to that qubit would be $B\\big(A\\ket{\\psi}\\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)\\ket{\\psi}$.</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, that is, there exists 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 $\\ket{0}$ and $\\ket{1}$. Consider an arbitrary gate:</p>\n<p>$$A = \\begin{bmatrix} \\epsilon &amp; \\zeta \\\\ \\eta &amp; \\mu \\end{bmatrix}$$</p>\n<p>Watch what happens when applying $A$ to these states:</p>\n<p>$$\nA\\ket{0} =\n\\begin{bmatrix} \\epsilon &amp; \\zeta \\\\ \\eta &amp; \\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\\ket{0} + \\eta\\ket{1}\n$$</p>\n<p>$$\nA\\ket{1} =\n\\begin{bmatrix} \\epsilon &amp; \\zeta \\\\ \\eta &amp; \\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\\ket{0} + \\mu\\ket{1}\n$$</p>\n<p>Notice that applying the $A$ gate to the $\\ket{0}$ state transforms it into the state written as the first column of the gate's matrix. Likewise, applying the $A$ gate to the $\\ket{1}$ 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 &amp; 1 \\\\ 1 &amp; 0 \\end{bmatrix}$$</p>\n<p>$$X\\ket{0} = \\begin{bmatrix} 0 \\\\ 1 \\end{bmatrix} = \\ket{1}$$</p>\n<p>$$X\\ket{1} = \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} = \\ket{0}$$</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>$$\\ket{\\psi} = \\begin{bmatrix} \\alpha \\\\ \\beta \\end{bmatrix} = \\alpha\\ket{0} + \\beta\\ket{1}$$</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\\ket{\\psi} = X\\big(\\alpha\\ket{0} + \\beta\\ket{1}\\big) = X\\big(\\alpha\\ket{0}\\big) + X\\big(\\beta\\ket{1}\\big) = \\alpha X\\ket{0} + \\beta X\\ket{1} = \\alpha\\ket{1} + \\beta\\ket{0}$$</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"
1086
+ "content": "<p>There are certain properties common to all quantum gates. This section introduces 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.\nThe example for this section, the $X$ gate, is represented by the following matrix:</p>\n<p>$$\\begin{bmatrix} 0 &amp; 1 \\\\ 1 &amp; 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\\ket{0} =\n\\begin{bmatrix} 0 &amp; 1 \\\\ 1 &amp; 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\\ket{1} =\n\\begin{bmatrix} 0 &amp; 1 \\\\ 1 &amp; 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>$$\\ket{\\psi} = \\alpha\\ket{0} + \\beta\\ket{1}$$</p>\n<p>$$\nX\\ket{\\psi} =\n\\begin{bmatrix} 0 &amp; 1 \\\\ 1 &amp; 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 $\\ket{0}$, $\\ket{1}$, and $\\ket{\\psi}$ mean, you can review the section on Dirac notation in &quot;The Qubit&quot; 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 &quot;gate&quot; and &quot;gate matrix&quot; 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 $\\ket{\\psi}$, the result of applying $A$ followed by $B$ to that qubit would be $B\\big(A\\ket{\\psi}\\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)\\ket{\\psi}$.</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, that is, there exists 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 $\\ket{0}$ and $\\ket{1}$. Consider an arbitrary gate:</p>\n<p>$$A = \\begin{bmatrix} \\epsilon &amp; \\zeta \\\\ \\eta &amp; \\mu \\end{bmatrix}$$</p>\n<p>Watch what happens when applying $A$ to these states:</p>\n<p>$$\nA\\ket{0} =\n\\begin{bmatrix} \\epsilon &amp; \\zeta \\\\ \\eta &amp; \\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\\ket{0} + \\eta\\ket{1}\n$$</p>\n<p>$$\nA\\ket{1} =\n\\begin{bmatrix} \\epsilon &amp; \\zeta \\\\ \\eta &amp; \\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\\ket{0} + \\mu\\ket{1}\n$$</p>\n<p>Notice that applying the $A$ gate to the $\\ket{0}$ state transforms it into the state written as the first column of the gate's matrix. Likewise, applying the $A$ gate to the $\\ket{1}$ 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 &amp; 1 \\\\ 1 &amp; 0 \\end{bmatrix}$$</p>\n<p>$$X\\ket{0} = \\begin{bmatrix} 0 \\\\ 1 \\end{bmatrix} = \\ket{1}$$</p>\n<p>$$X\\ket{1} = \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} = \\ket{0}$$</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>$$\\ket{\\psi} = \\begin{bmatrix} \\alpha \\\\ \\beta \\end{bmatrix} = \\alpha\\ket{0} + \\beta\\ket{1}$$</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\\ket{\\psi} = X\\big(\\alpha\\ket{0} + \\beta\\ket{1}\\big) = X\\big(\\alpha\\ket{0}\\big) + X\\big(\\beta\\ket{1}\\big) = \\alpha X\\ket{0} + \\beta X\\ket{1} = \\alpha\\ket{1} + \\beta\\ket{0}$$</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"
1087
1087
  }
1088
1088
  ]
1089
1089
  },
@@ -1105,7 +1105,7 @@ export default {
1105
1105
  "items": [
1106
1106
  {
1107
1107
  "type": "text-content",
1108
- "content": "<p>This section describes a more formal process of finding the ket-bra decompositions of quantum gates. This section isn't 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 <em>eigenvalues</em> and <em>eigenvectors</em> to find the ket-bra decomposition of any gate. Given a gate $A$ and the orthogonal vectors $\\ket{\\phi}$ and $\\ket{\\psi}$, if:</p>\n<p>$$A\\ket{\\phi} = x_\\phi\\ket{\\phi}$$\n$$A\\ket{\\psi} = x_\\psi\\ket{\\psi}$$</p>\n<p>Real numbers $x_\\phi$ and $x_\\psi$ are called eigenvalues and $\\ket{\\phi}$ and $\\ket{\\psi}$ are eigenvectors of $A$. Then:</p>\n<p>$$A = x_\\phi\\ket{\\phi}\\bra{\\phi} + x_\\psi\\ket{\\psi}\\bra{\\psi}$$</p>\n<p>Let's use our $X$ gate as a simple example. The $X$ gate has two eigenvectors: $\\ket{+} = \\frac{1}{\\sqrt{2}}\\big(\\ket{0} + \\ket{1}\\big)$ and $\\ket{-} = \\frac{1}{\\sqrt{2}}\\big(\\ket{0} - \\ket{1}\\big)$. Their eigenvalues are $1$ and $-1$ respectively:</p>\n<p>$$X\\ket{+} = \\ket{+}$$\n$$X\\ket{-} = -\\ket{-}$$</p>\n<p>Here's what the decomposition looks like:\n$$X = \\ket{+}\\bra{+} - \\ket{-}\\bra{-} =$$\n$$= \\frac{1}{2}\\big[\\big(\\ket{0} + \\ket{1}\\big)\\big(\\bra{0} + \\bra{1}\\big) - \\big(\\ket{0} - \\ket{1}\\big)\\big(\\bra{0} - \\bra{1}\\big)\\big] =$$\n$$= \\frac{1}{2}\\big(\\ket{0}\\bra{0} + \\ket{0}\\bra{1} + \\ket{1}\\bra{0} + \\ket{1}\\bra{1} - \\ket{0}\\bra{0} + \\ket{0}\\bra{1} + \\ket{1}\\bra{0} - \\ket{1}\\bra{1}\\big) =$$\n$$= \\frac{1}{2}\\big(2\\ket{0}\\bra{1} + 2\\ket{1}\\bra{0}\\big) =$$\n$$= \\ket{0}\\bra{1} + \\ket{1}\\bra{0}$$</p>\n"
1108
+ "content": "<p>This section describes a more formal process of finding the ket-bra decompositions of quantum gates. This section isn't 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 <em>eigenvalues</em> and <em>eigenvectors</em> to find the ket-bra decomposition of any gate. Given a gate $A$ and the orthogonal vectors $\\ket{\\phi}$ and $\\ket{\\psi}$, if:</p>\n<p>$$A\\ket{\\phi} = x_\\phi\\ket{\\phi}$$\n$$A\\ket{\\psi} = x_\\psi\\ket{\\psi}$$</p>\n<p>Real numbers $x_\\phi$ and $x_\\psi$ are called eigenvalues and $\\ket{\\phi}$ and $\\ket{\\psi}$ are eigenvectors of $A$. Then:</p>\n<p>$$A = x_\\phi\\ket{\\phi}\\bra{\\phi} + x_\\psi\\ket{\\psi}\\bra{\\psi}$$</p>\n<p>Let's use the $X$ gate as a simple example. The $X$ gate has two eigenvectors: $\\ket{+} = \\frac{1}{\\sqrt{2}}\\big(\\ket{0} + \\ket{1}\\big)$ and $\\ket{-} = \\frac{1}{\\sqrt{2}}\\big(\\ket{0} - \\ket{1}\\big)$. Their eigenvalues are $1$ and $-1$ respectively:</p>\n<p>$$X\\ket{+} = \\ket{+}$$\n$$X\\ket{-} = -\\ket{-}$$</p>\n<p>Here's what the decomposition looks like:\n$$X = \\ket{+}\\bra{+} - \\ket{-}\\bra{-} =$$\n$$= \\frac{1}{2}\\big[\\big(\\ket{0} + \\ket{1}\\big)\\big(\\bra{0} + \\bra{1}\\big) - \\big(\\ket{0} - \\ket{1}\\big)\\big(\\bra{0} - \\bra{1}\\big)\\big] =$$\n$$= \\frac{1}{2}\\big(\\ket{0}\\bra{0} + \\ket{0}\\bra{1} + \\ket{1}\\bra{0} + \\ket{1}\\bra{1} - \\ket{0}\\bra{0} + \\ket{0}\\bra{1} + \\ket{1}\\bra{0} - \\ket{1}\\bra{1}\\big) =$$\n$$= \\frac{1}{2}\\big(2\\ket{0}\\bra{1} + 2\\ket{1}\\bra{0}\\big) =$$\n$$= \\ket{0}\\bra{1} + \\ket{1}\\bra{0}$$</p>\n"
1109
1109
  }
1110
1110
  ]
1111
1111
  },
@@ -1266,7 +1266,7 @@ export default {
1266
1266
  "items": [
1267
1267
  {
1268
1268
  "type": "text-content",
1269
- "content": "<p>A global phase is a phase factor that multiplies the entire quantum state. It isn't observable when measuring the qubit's state, as the probabilities remain unchanged. However, it's significant when considering quantum state transformations.</p>\n<p>Our task is to implement an operation that transforms the given qubit state from $\\ket{\\psi} = \\beta \\ket{0} + \\gamma \\ket{1}$ to $- \\beta \\ket{0} - \\gamma \\ket{1}$.</p>\n<p>To do that, you use a sequence of gates. The Pauli Z gate followed by the Pauli X gate can be used to achieve this effect when applied in succession twice.</p>\n<ol>\n<li>\n<p><strong>Apply the Pauli Z gate</strong>: The Z gate multiplies the $\\ket{1}$ state by $-1$ but doen't change the $\\ket{0}$ state, converting our state $\\beta \\ket{0} + \\gamma \\ket{1}$ to $\\beta \\ket{0} - \\gamma \\ket{1}$.</p>\n<p>The matrix representation of the Z gate is:</p>\n<p>$$\nZ =\n\\begin{bmatrix}1 &amp; 0 \\\\ 0 &amp; -1 \\end{bmatrix}\n$$</p>\n</li>\n<li>\n<p><strong>Apply the Pauli X gate</strong>: The X gate flips the $\\ket{0}$ and $\\ket{1}$ basis states, converting $\\beta \\ket{0} - \\gamma \\ket{1}$ state to $\\beta \\ket{1} - \\gamma \\ket{0}$.</p>\n<p>The matrix representation of the X gate is:</p>\n<p>$$\nX =\n\\begin{bmatrix}0 &amp; 1 \\\\ 1 &amp; 0\\end{bmatrix}\n$$</p>\n</li>\n<li>\n<p><strong>Repeat the Z and X gates</strong>: Applying the Z gate again will multiply the $\\ket{1}$ state (that used to be $\\ket{0}$), converting the state $\\beta \\ket{1} - \\gamma \\ket{0}$ to $- \\beta \\ket{1} - \\gamma \\ket{0}$. Finally, the second X gate will restore the original basis states, but now with both amplitudes having acquired an additional phase of $-1$. This means the state has been multiplied by $-1$, achieving the required global phase change.</p>\n</li>\n</ol>\n"
1269
+ "content": "<p>A global phase is a phase factor that multiplies the entire quantum state. It isn't observable when measuring the qubit's state, as the probabilities remain unchanged. However, it's significant when considering quantum state transformations.</p>\n<p>Your task is to implement an operation that transforms the given qubit state from $\\ket{\\psi} = \\beta \\ket{0} + \\gamma \\ket{1}$ to $- \\beta \\ket{0} - \\gamma \\ket{1}$.</p>\n<p>To do that, you use a sequence of gates. The Pauli Z gate followed by the Pauli X gate can be used to achieve this effect when applied in succession twice.</p>\n<ol>\n<li>\n<p><strong>Apply the Pauli Z gate</strong>: The Z gate multiplies the $\\ket{1}$ state by $-1$ but doen't change the $\\ket{0}$ state, converting the state $\\beta \\ket{0} + \\gamma \\ket{1}$ to $\\beta \\ket{0} - \\gamma \\ket{1}$.</p>\n<p>The matrix representation of the Z gate is:</p>\n<p>$$\nZ =\n\\begin{bmatrix}1 &amp; 0 \\\\ 0 &amp; -1 \\end{bmatrix}\n$$</p>\n</li>\n<li>\n<p><strong>Apply the Pauli X gate</strong>: The X gate flips the $\\ket{0}$ and $\\ket{1}$ basis states, converting $\\beta \\ket{0} - \\gamma \\ket{1}$ state to $\\beta \\ket{1} - \\gamma \\ket{0}$.</p>\n<p>The matrix representation of the X gate is:</p>\n<p>$$\nX =\n\\begin{bmatrix}0 &amp; 1 \\\\ 1 &amp; 0\\end{bmatrix}\n$$</p>\n</li>\n<li>\n<p><strong>Repeat the Z and X gates</strong>: Applying the Z gate again will multiply the $\\ket{1}$ state (that used to be $\\ket{0}$), converting the state $\\beta \\ket{1} - \\gamma \\ket{0}$ to $- \\beta \\ket{1} - \\gamma \\ket{0}$. Finally, the second X gate will restore the original basis states, but now with both amplitudes having acquired an additional phase of $-1$. This means the state has been multiplied by $-1$, achieving the required global phase change.</p>\n</li>\n</ol>\n"
1270
1270
  },
1271
1271
  {
1272
1272
  "type": "solution",
@@ -2400,7 +2400,7 @@ export default {
2400
2400
  "items": [
2401
2401
  {
2402
2402
  "type": "text-content",
2403
- "content": "<p>This kata is designed to get you familiar with quantum state preparation and to help you practice using gates to change quantum states.</p>\n<p><strong>This kata covers the following topics:</strong></p>\n<ul>\n<li>Using basic single-qubit and multi-qubit gates to prepare quantum states</li>\n<li>Manipulating superposition states</li>\n<li>Flow control and recursion in Q#</li>\n</ul>\n<p><strong>What you should know to start working on this kata:</strong></p>\n<ul>\n<li>Dirac notation for single-qubit and multi-qubit quantum systems</li>\n<li>Basic single-qubit and multi-qubit gates</li>\n</ul>\n"
2403
+ "content": "<p>This kata is designed to get you familiar with quantum state preparation and to help you practice using gates to change quantum states.</p>\n<p><strong>This kata covers the following topics:</strong></p>\n<ul>\n<li>Using basic single-qubit and multi-qubit gates to prepare quantum states</li>\n<li>Manipulating superposition states</li>\n<li>Flow control and recursion in Q#</li>\n</ul>\n<p><strong>What you should know to start working on this kata:</strong></p>\n<ul>\n<li>Dirac notation for single-qubit and multi-qubit quantum systems</li>\n<li>Basic single-qubit and multi-qubit gates</li>\n</ul>\n<p>If you need a refresher on these topics, you can check out the previous katas.</p>\n"
2404
2404
  }
2405
2405
  ]
2406
2406
  },
@@ -2421,7 +2421,7 @@ export default {
2421
2421
  "title": "Plus State",
2422
2422
  "description": {
2423
2423
  "type": "text-content",
2424
- "content": "<p><strong>Input:</strong> A qubit in the $\\ket{0}$ state.</p>\n<p><strong>Goal:</strong> Change the state of the qubit to $\\ket{+} = \\frac{1}{\\sqrt{2}} \\big(\\ket{0} + \\ket{1}\\big)$.</p>\n<details>\n <summary><b>Need a hint?</b></summary>\n Hadamard gate H will convert $\\ket{0}$ state to $\\ket{+}$ state.\n</details>\n"
2424
+ "content": "<p><strong>Input:</strong> A qubit in the $\\ket{0}$ state.</p>\n<p><strong>Goal:</strong> Change the state of the qubit to $\\ket{+} = \\frac{1}{\\sqrt{2}} \\big(\\ket{0} + \\ket{1}\\big)$.</p>\n<details>\n <summary><b>Need a hint?</b></summary>\n Hadamard gate $H$ will convert $\\ket{0}$ state to $\\ket{+}$ state.\n</details>\n"
2425
2425
  },
2426
2426
  "sourceIds": [
2427
2427
  "preparing_states__plus_state__Verification.qs",
@@ -2434,7 +2434,7 @@ export default {
2434
2434
  "items": [
2435
2435
  {
2436
2436
  "type": "text-content",
2437
- "content": "<p>When you start learning the basic quantum gates, one of the first gates described will be the Hadamard gate:</p>\n<p>$$H = \\frac{1}{\\sqrt2} \\begin{bmatrix} 1 &amp; 1 \\\\ 1 &amp; -1 \\end{bmatrix}$$</p>\n<p>This gate converts $\\ket{0}$ into $\\ket{+} = \\frac{1}{\\sqrt{2}} \\big(\\ket{0} + \\ket{1}\\big)$ and $\\ket{1}$ into $\\ket{−} = \\frac{1}{\\sqrt{2}} \\big(\\ket{0} - \\ket{1}\\big)$. The first of these transformations is exactly the one we're looking for!</p>\n<p>Hadamard gate is one of the built-in gates in Q#, available in the <code>Microsoft.Quantum.Intrinsic</code> namespace.\nIt is open in any Q# source files by default, so you can use it right away.</p>\n"
2437
+ "content": "<p>When you start learning the basic quantum gates, one of the first gates described will be the Hadamard gate:</p>\n<p>$$H = \\frac{1}{\\sqrt2} \\begin{bmatrix} 1 &amp; 1 \\\\ 1 &amp; -1 \\end{bmatrix}$$</p>\n<p>This gate converts $\\ket{0}$ into $\\ket{+} = \\frac{1}{\\sqrt{2}} \\big(\\ket{0} + \\ket{1}\\big)$ and $\\ket{1}$ into $\\ket{−} = \\frac{1}{\\sqrt{2}} \\big(\\ket{0} - \\ket{1}\\big)$. The first of these transformations is exactly the one you're looking for!</p>\n<p>Hadamard gate is one of the built-in gates in Q#, available in the <code>Microsoft.Quantum.Intrinsic</code> namespace.\nIt's open in any Q# source files by default, so you can use it right away.</p>\n"
2438
2438
  },
2439
2439
  {
2440
2440
  "type": "solution",
@@ -2463,7 +2463,7 @@ export default {
2463
2463
  "items": [
2464
2464
  {
2465
2465
  "type": "text-content",
2466
- "content": "<p>As we've seen in the previous task, the Hadamard gate maps the basis state $\\ket{0}$ to $\\frac{1}{\\sqrt2}\\big(\\ket{0} + \\ket{1}\\big)$ and $\\ket{1}$ to $\\frac{1}{\\sqrt2}\\big(\\ket{0} - \\ket{1}\\big)$.\nIf our qubit was already in the $\\ket{1}$ state, we would simply apply the Hadamard gate to prepare the required $\\ket{-}$ state.\nFortunately, there is another operation we can use to change the state $\\ket{0}$ to $\\ket{1}$, namely the X gate:</p>\n<p>$$X = \\begin{bmatrix} 0 &amp; 1 \\\\ 1 &amp; 0 \\end{bmatrix}$$</p>\n<p>This gate transforms $\\ket{0} \\longmapsto \\ket{1}$ and $\\ket{1} \\longmapsto \\ket{0}$.\nX is another one of the built-in gates in Q# from the <code>Microsoft.Quantum.Intrinsic</code> namespace.</p>\n<p>Thus, our solution should apply the X gate to our qubit, followed by the Hadamard gate.</p>\n"
2466
+ "content": "<p>As you've seen in the previous task, the Hadamard gate maps the basis state $\\ket{0}$ to $\\frac{1}{\\sqrt2}\\big(\\ket{0} + \\ket{1}\\big)$ and $\\ket{1}$ to $\\frac{1}{\\sqrt2}\\big(\\ket{0} - \\ket{1}\\big)$.\nIf your qubit was already in the $\\ket{1}$ state, you would simply apply the Hadamard gate to prepare the required $\\ket{-}$ state.\nFortunately, there is another operation you can use to change the state $\\ket{0}$ to $\\ket{1}$, namely the $X$ gate:</p>\n<p>$$X = \\begin{bmatrix} 0 &amp; 1 \\\\ 1 &amp; 0 \\end{bmatrix}$$</p>\n<p>This gate transforms $\\ket{0} \\longmapsto \\ket{1}$ and $\\ket{1} \\longmapsto \\ket{0}$.\n$X$ is another one of the built-in gates in Q# from the <code>Microsoft.Quantum.Intrinsic</code> namespace.</p>\n<p>Thus, your solution should apply the $X$ gate to your qubit, followed by the Hadamard gate.</p>\n"
2467
2467
  },
2468
2468
  {
2469
2469
  "type": "solution",
@@ -2492,7 +2492,7 @@ export default {
2492
2492
  "items": [
2493
2493
  {
2494
2494
  "type": "text-content",
2495
- "content": "<p>We know that the Hadamard gate maps the basis state $\\ket{0}$ to $\\frac{1}{\\sqrt2}(\\ket{0} + \\ket{1})$, so it is a logical starting point for solving this problem.</p>\n<p>Next, we see that the final state has a $\\frac{1}{2}$ term hinting that we might be applying two operations involving a $\\frac{1}{\\sqrt{2}}$ term.</p>\n<p>Now, how do we get the $\\ket{00} + \\ket{01} + \\ket{10} + \\ket{11}$ expression? Let's see what does multiplying the expression $\\ket{0} + \\ket{1}$ by itself look like:</p>\n<p>$$\\big(\\ket{0} + \\ket{1}\\big) \\otimes \\big(\\ket{0} + \\ket{1}\\big) = \\ket{0}\\ket{0} + \\ket{0}\\ket{1} + \\ket{1}\\ket{0} + \\ket{1}\\ket{1}$$</p>\n<p>Thus, applying the Hadamard gate to each qubit in isolation will deliver the desired final result:</p>\n<p>$$H\\ket{0} \\otimes H\\ket{0} = \\frac{1}{\\sqrt2} \\big(\\ket{0} + \\ket{1}\\big) \\otimes \\frac{1}{\\sqrt2}\\big(\\ket{0} + \\ket{1}\\big)\n= \\frac{1}{2} (\\ket{00} + \\ket{01} + \\ket{10} + \\ket{11})$$</p>\n<p>Q# arrays are similar to arrays in other languages: you can access the $i$-th element of the array <code>qs</code> as <code>qs[i]</code> (indices are 0-based).</p>\n"
2495
+ "content": "<p>You know that the Hadamard gate maps the basis state $\\ket{0}$ to $\\frac{1}{\\sqrt2}(\\ket{0} + \\ket{1})$, so it's a logical starting point for solving this problem.</p>\n<p>Next, you see that the final state has a $\\frac{1}{2}$ term hinting that you might be applying two operations involving a $\\frac{1}{\\sqrt{2}}$ term.</p>\n<p>Now, how do you get the $\\ket{00} + \\ket{01} + \\ket{10} + \\ket{11}$ expression? Let's see what does multiplying the expression $\\ket{0} + \\ket{1}$ by itself look like:</p>\n<p>$$\\big(\\ket{0} + \\ket{1}\\big) \\otimes \\big(\\ket{0} + \\ket{1}\\big) = \\ket{0}\\ket{0} + \\ket{0}\\ket{1} + \\ket{1}\\ket{0} + \\ket{1}\\ket{1}$$</p>\n<p>Thus, applying the Hadamard gate to each qubit in isolation will deliver the desired final result:</p>\n<p>$$H\\ket{0} \\otimes H\\ket{0} = \\frac{1}{\\sqrt2} \\big(\\ket{0} + \\ket{1}\\big) \\otimes \\frac{1}{\\sqrt2}\\big(\\ket{0} + \\ket{1}\\big)\n= \\frac{1}{2} (\\ket{00} + \\ket{01} + \\ket{10} + \\ket{11})$$</p>\n<p>Q# arrays are similar to arrays in other languages: you can access the $i$-th element of the array <code>qs</code> as <code>qs[i]</code> (indices are 0-based).</p>\n"
2496
2496
  },
2497
2497
  {
2498
2498
  "type": "solution",
@@ -2521,7 +2521,7 @@ export default {
2521
2521
  "items": [
2522
2522
  {
2523
2523
  "type": "text-content",
2524
- "content": "<p>Here we start with the end state of the previous task $\\frac{1}{2} \\big(\\ket{00} + \\ket{01} + \\ket{10} + \\ket{11}\\big)$. Looking at the desired state, the phase of the $\\ket{11}$ state is flipped ($+$ changed to a $-$).</p>\n<p>A regular phase flip on one qubit can be done using a Z gate:\n$$\\begin{bmatrix} 1 &amp; 0 \\\\ 0 &amp; -1 \\end{bmatrix}$$\nThis gate will perform a phase flip only on the $\\ket{1}$ state:</p>\n<p>$$Z(\\alpha\\ket{0} + \\beta\\ket{1}) = \\alpha\\ket{0} - \\beta\\ket{1}$$</p>\n<p>In our case we only want to flip the phase of the $\\ket{11}$ state and not the $\\ket{01}$ state. To accomplish this, we can use a controlled Z gate; this will make sure that the $Z$ gate is only applied if the control bit is in the $\\ket{1}$ state, and the $\\ket{01}$ state will not change.</p>\n<blockquote>\n<p>In Q# we can apply a controlled gate by using the <code>Controlled</code> keyword before the gate. The controlled gate will take two parameters; the first parameter is an array of control qubits (you can have multiple qubits used as a control), and the second parameter is a tuple of parameters passed to the original gate (in this case it's just the qubit to which you want to apply the gate if the control bit is $\\ket{1}$).</p>\n</blockquote>\n"
2524
+ "content": "<p>Here you start with the end state of the previous task $\\frac{1}{2} \\big(\\ket{00} + \\ket{01} + \\ket{10} + \\ket{11}\\big)$. Looking at the desired state, the phase of the $\\ket{11}$ state is flipped ($+$ changed to a $-$).</p>\n<p>A regular phase flip on one qubit can be done using a $Z$ gate:\n$$\\begin{bmatrix} 1 &amp; 0 \\\\ 0 &amp; -1 \\end{bmatrix}$$\nThis gate will perform a phase flip only on the $\\ket{1}$ state:</p>\n<p>$$Z(\\alpha\\ket{0} + \\beta\\ket{1}) = \\alpha\\ket{0} - \\beta\\ket{1}$$</p>\n<p>In this case, you only want to flip the phase of the $\\ket{11}$ state and not the $\\ket{01}$ state. To accomplish this, you can use a controlled $Z$ gate; this will make sure that the $Z$ gate is only applied if the control bit is in the $\\ket{1}$ state, and the $\\ket{01}$ state won't change.</p>\n<blockquote>\n<p>In Q#, you can apply a controlled gate by using the <code>Controlled</code> keyword before the gate. The controlled gate will take two parameters; the first parameter is an array of control qubits (you can have multiple qubits used as a control), and the second parameter is a tuple of parameters passed to the original gate (in this case it's just the qubit to which you want to apply the gate if the control bit is $\\ket{1}$).</p>\n</blockquote>\n"
2525
2525
  },
2526
2526
  {
2527
2527
  "type": "solution",
@@ -2550,7 +2550,7 @@ export default {
2550
2550
  "items": [
2551
2551
  {
2552
2552
  "type": "text-content",
2553
- "content": "<p>We will start approaching the problem from the desired end result. Let’s see if we can factor any expressions out of $\\big(\\ket{00} + i\\ket{01} - \\ket{10} - i\\ket{11}\\big)$:</p>\n<p>$$\n\\ket{00} + i\\ket{01} - \\ket{10} - i\\ket{11}\n= \\ket{00} + \\big(\\ket{0} - \\ket{1}\\big) i\\ket{1} - \\ket{10} =\n$$\n$$\n= \\big(\\ket{0} - \\ket{1}\\big) \\ket{0} + \\big(\\ket{0} - \\ket{1}\\big) i\\ket{1}\n= \\big(\\ket{0} - \\ket{1}\\big) \\otimes \\big(\\ket{0} + i\\ket{1}\\big)\n$$</p>\n<p>The fact that we were able to factor out the state into a tensor product of two terms means the state is separable.</p>\n<p>This is looking promising. Now let’s try to approach the problem from the other end, i.e. from the starting state of $\\ket{00}$.\nAs we've seen in the previous task, applying a Hadamard operation to each $\\ket{0}$ gets us closer to the factored-out expression:</p>\n<p>$$\nH\\ket{0} \\otimes H\\ket{0} = \\frac{1}{\\sqrt2} \\big(\\ket{0} + \\ket{1}\\big) \\otimes \\frac{1}{\\sqrt2} \\big(\\ket{0} + \\ket{1}\\big)\n=\\frac{1}{2} \\big(\\ket{0} + \\ket{1}\\big) \\otimes \\big(\\ket{0} + \\ket{1}\\big) \n$$</p>\n<p>If we compare these two equations (while ignoring the $\\frac{1}{2}$ term in the second equation), we end up with the following transformations that we need to perform on the individual qubits:</p>\n<p>$$\n\\ket{0} + \\ket{1} \\overset{???}\\rightarrow \\ket{0} - \\ket{1}\n$$</p>\n<p>$$\n\\ket{0} + \\ket{1} \\overset{???}\\rightarrow \\ket{0} + i\\ket{1}\n$$</p>\n<p>Next lets take a look at our basic gates, in particular the Pauli $Z$ gate:</p>\n<p>$$Z = \\begin{bmatrix} 1 &amp; 0 \\\\ 0 &amp; -1 \\end{bmatrix}$$</p>\n<p>If it is applied to the state $\\frac{1}{\\sqrt2} \\big(\\ket{0} + \\ket{1}\\big)$, it will leave the basis state $\\ket{0}$ unchanged and will map $\\ket{1}$ to $-\\ket{1}$. Thus,</p>\n<p>$$Z\\frac{1}{\\sqrt2} \\big(\\ket{0} + \\ket{1}\\big) = \\frac{1}{\\sqrt2} \\big(\\ket{0} - \\ket{1}\\big)$$</p>\n<p>So the $Z$ gate is the answer to the question of how to do the first transformation.</p>\n<p>Looking for another gate to address the second transformation, we find the $S$ gate:</p>\n<p>$$S = \\begin{bmatrix} 1 &amp; 0 \\\\ 0 &amp; i \\end{bmatrix}$$</p>\n<p>If it is applied to the state $\\frac{1}{\\sqrt2} \\big(\\ket{0} + \\ket{1}\\big)$, it will leave the basis state $\\ket{0}$ unchanged and will map $\\ket{1}$ to $i\\ket{1}$. Thus,</p>\n<p>$$S\\frac{1}{\\sqrt2} \\big(\\ket{0} + \\ket{1}\\big) = \\frac{1}{\\sqrt2} \\big(\\ket{0} + i\\ket{1}\\big)$$</p>\n<p>So the $S$ gate now answers the question of how to do the second transformation.</p>\n<p>To summarize, the state we need to prepare can be represented as follows:\n$$ZH\\ket{0} \\otimes SH\\ket{0}$$</p>\n<p>Remember that in Q# the gates have to be applied in reverse order compared to the mathematical notation - the gate closest to the ket symbol is applied first.</p>\n"
2553
+ "content": "<p>You'll start approaching the problem from the desired end result. Let’s see if you can factor any expressions out of $\\big(\\ket{00} + i\\ket{01} - \\ket{10} - i\\ket{11}\\big)$:</p>\n<p>$$\n\\ket{00} + i\\ket{01} - \\ket{10} - i\\ket{11}\n= \\ket{00} + \\big(\\ket{0} - \\ket{1}\\big) i\\ket{1} - \\ket{10} =\n$$\n$$\n= \\big(\\ket{0} - \\ket{1}\\big) \\ket{0} + \\big(\\ket{0} - \\ket{1}\\big) i\\ket{1}\n= \\big(\\ket{0} - \\ket{1}\\big) \\otimes \\big(\\ket{0} + i\\ket{1}\\big)\n$$</p>\n<p>The fact that you were able to factor out the state into a tensor product of two terms means the state is separable.</p>\n<p>This is looking promising. Now let’s try to approach the problem from the other end, that is, from the starting state of $\\ket{00}$.\nAs you've seen in the previous task, applying a Hadamard operation to each $\\ket{0}$ gets you closer to the factored-out expression:</p>\n<p>$$\nH\\ket{0} \\otimes H\\ket{0} = \\frac{1}{\\sqrt2} \\big(\\ket{0} + \\ket{1}\\big) \\otimes \\frac{1}{\\sqrt2} \\big(\\ket{0} + \\ket{1}\\big)\n=\\frac{1}{2} \\big(\\ket{0} + \\ket{1}\\big) \\otimes \\big(\\ket{0} + \\ket{1}\\big) \n$$</p>\n<p>If you compare these two equations (while ignoring the $\\frac{1}{2}$ term in the second equation), you end up with the following transformations that you need to perform on the individual qubits:</p>\n<p>$$\n\\ket{0} + \\ket{1} \\overset{???}\\rightarrow \\ket{0} - \\ket{1}\n$$</p>\n<p>$$\n\\ket{0} + \\ket{1} \\overset{???}\\rightarrow \\ket{0} + i\\ket{1}\n$$</p>\n<p>Next, let's take a look at the basic gates, in particular the Pauli Z gate:</p>\n<p>$$Z = \\begin{bmatrix} 1 &amp; 0 \\\\ 0 &amp; -1 \\end{bmatrix}$$</p>\n<p>If it's applied to the state $\\frac{1}{\\sqrt2} \\big(\\ket{0} + \\ket{1}\\big)$, it'll leave the basis state $\\ket{0}$ unchanged and will map $\\ket{1}$ to $-\\ket{1}$. Thus,</p>\n<p>$$Z\\frac{1}{\\sqrt2} \\big(\\ket{0} + \\ket{1}\\big) = \\frac{1}{\\sqrt2} \\big(\\ket{0} - \\ket{1}\\big)$$</p>\n<p>So the $Z$ gate is the answer to the question of how to do the first transformation.</p>\n<p>Looking for another gate to address the second transformation, you find the $S$ gate:</p>\n<p>$$S = \\begin{bmatrix} 1 &amp; 0 \\\\ 0 &amp; i \\end{bmatrix}$$</p>\n<p>If it's applied to the state $\\frac{1}{\\sqrt2} \\big(\\ket{0} + \\ket{1}\\big)$, it'll leave the basis state $\\ket{0}$ unchanged and will map $\\ket{1}$ to $i\\ket{1}$. Thus,</p>\n<p>$$S\\frac{1}{\\sqrt2} \\big(\\ket{0} + \\ket{1}\\big) = \\frac{1}{\\sqrt2} \\big(\\ket{0} + i\\ket{1}\\big)$$</p>\n<p>So the $S$ gate now answers the question of how to do the second transformation.</p>\n<p>To summarize, the state you need to prepare can be represented as follows:\n$$ZH\\ket{0} \\otimes SH\\ket{0}$$</p>\n<p>Remember that in Q# the gates have to be applied in reverse order compared to the mathematical notation - the gate closest to the ket symbol is applied first.</p>\n"
2554
2554
  },
2555
2555
  {
2556
2556
  "type": "solution",
@@ -2579,7 +2579,7 @@ export default {
2579
2579
  "items": [
2580
2580
  {
2581
2581
  "type": "text-content",
2582
- "content": "<p>The first thing we notice is that, unlike in the previous task, we cannot represent this state as a tensor product of two individual qubit states - this goal state is NOT separable.</p>\n<blockquote>\n<p>How can we see this? Let's assume that this state can be represented as a tensor product of two qubit states:</p>\n<p>$$\\ket{\\psi_1} \\otimes \\ket{\\psi_2} = (\\alpha_1\\ket{0} + \\beta_1\\ket{1}) \\otimes (\\alpha_2\\ket{0} + \\beta_2\\ket{1}) = \\alpha_1\\alpha_2\\ket{00} + \\alpha_1\\beta_2\\ket{01} + \\beta_1\\alpha_2\\ket{10} + \\beta_1\\beta_2\\ket{11}$$</p>\n<p>In order for this state to be equal to $\\frac{1}{\\sqrt2}\\big(\\ket{00} + \\ket{11}\\big)$, we need to have $\\alpha_1\\alpha_2 = \\beta_1\\beta_2 = \\frac{1}{\\sqrt2}$ and at the same time $\\alpha_1\\beta_2 = \\beta_1\\alpha_2 = 0$, which is impossible.</p>\n<p>This is the phenomena called <strong>entanglement</strong>, in which the states of the qubits are linked together and can not be considered individually.</p>\n</blockquote>\n<p>Let's see what steps we can take to prepare this state without factoring it into states of individual qubits.</p>\n<hr />\n<p>First, we notice that we should end with a superposition of two of the four computational basis for two qubits: $\\ket{00}, \\ket{01}, \\ket{10}, \\ket{11}$.</p>\n<p>This gives us a hint that we should start by preparing a superposition on at least one of the qubits. Let’s try creating a superposition on the first qubit with a Hadamard gate:</p>\n<p>$$H\\ket{0} \\otimes \\ket{0} = \\frac{1}{\\sqrt2} (\\ket{0} + \\ket{1}) \\otimes \\ket{0} = \\frac{1}{\\sqrt2} (\\ket{00} + \\ket{10})$$</p>\n<p>Well, we got pretty close, except we need to transform the $\\ket{10}$ state to $\\ket{11}$.\nHow can we do this?</p>\n<p>We can take advantage of controlled gates, specifically the <a href=\"https://en.wikipedia.org/wiki/Controlled_NOT_gate\">controlled NOT gate</a>, also referred to as $CNOT$. This gate acts on two qubits, hence it is represented as a $4 \\times 4$ unitary matrix. The $CNOT$ gate changes the target qubit from state $\\ket{0}$ to $\\ket{1}$ and vice versa when the control qubit is $\\ket{1}$ and does nothing to the target qubit when the control qubit is $\\ket{0}$. The control qubit always remains unchanged.</p>\n<p>$$\\text{CNOT} = \\begin{bmatrix} 1 &amp; 0 &amp; 0 &amp; 0 \\\\ 0 &amp; 1 &amp; 0 &amp; 0 \\\\ 0 &amp; 0 &amp; 0 &amp; 1 \\\\ 0 &amp; 0 &amp; 1 &amp; 0 \\end{bmatrix}$$</p>\n<p>If we apply the CNOT gate to the state $\\frac{1}{\\sqrt2} (\\ket{00} + \\ket{10})$, taking the first qubit as the control and the second one as target, we'll get exactly the desired goal state.</p>\n<p>Steps required to reach goal state:</p>\n<ol>\n<li>Apply a Hadamard gate to the first qubit.</li>\n<li>Applying a CNOT with first qubit as control and second qubit as target.</li>\n</ol>\n<p>In matrix representation we can represent this operation as a product of two $4 \\times 4$ matrices, with the matrix corresponding to the first step being the tensor product of a Hadamard gate on the first qubit and identity gate on the second qubit.</p>\n<p>$$H \\otimes I = \\frac{1}{\\sqrt2} \\begin{bmatrix} 1 &amp; 1 \\\\ 1 &amp; -1 \\end{bmatrix} \\otimes \\begin{bmatrix} 1 &amp; 0 \\\\ 0 &amp; 1 \\end{bmatrix} = \n\\frac{1}{\\sqrt2}\\begin{bmatrix} 1 &amp; 0 &amp; 1 &amp; 0 \\\\ 0 &amp; 1 &amp; 0 &amp; 1 \\\\ 1 &amp; 0 &amp; -1 &amp; 0 \\\\ 0 &amp; 1 &amp; 0 &amp; -1 \\end{bmatrix}$$</p>\n<p>$$\\underset{\\text{CNOT}}{\\underbrace{\\begin{bmatrix} 1 &amp; 0 &amp; 0 &amp; 0 \\\\ 0 &amp; 1 &amp; 0 &amp; 0 \\\\ 0 &amp; 0 &amp; 0 &amp; 1 \\\\ 0 &amp; 0 &amp; 1 &amp; 0 \\end{bmatrix}}} \n\\cdot \n\\underset{H \\otimes I}{\\underbrace{\\frac{1}{\\sqrt2} \\begin{bmatrix} 1 &amp; 0 &amp; 1 &amp; 0 \\\\ 0 &amp; 1 &amp; 0 &amp; 1 \\\\ 1 &amp; 0 &amp; -1 &amp; 0 \\\\ 0 &amp; 1 &amp; 0 &amp; -1 \\end{bmatrix}}}\n\\cdot\n\\underset{\\ket{0}}{\\underbrace{ \\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ 0 \\end{bmatrix}}}\n= \\frac{1}{\\sqrt2} \\begin{bmatrix} 1 &amp; 0 &amp; 1 &amp; 0 \\\\ 0 &amp; 1 &amp; 0 &amp; 1 \\\\ 0 &amp; 1 &amp; 0 &amp; -1 \\\\ 1 &amp; 0 &amp; -1 &amp; 0 \\end{bmatrix}\n\\cdot\n\\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ 0 \\end{bmatrix}\n= \\underset{goal}{\\underbrace{ \\frac{1}{\\sqrt2} \\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ 1 \\end{bmatrix}}}\n$$</p>\n<p>Note that in the matrix representation and in Dirac notation the gates are applied from right to left (the rightmost operation happens firts), while in circuit notation the operations are applied from left to right (the leftmost operation happens first).</p>\n"
2582
+ "content": "<p>The first thing you notice is that, unlike in the previous task, you can't represent this state as a tensor product of two individual qubit states - this goal state is <strong>not</strong> separable.</p>\n<blockquote>\n<p>How can you see this? Let's assume that this state can be represented as a tensor product of two qubit states:</p>\n<p>$$\\ket{\\psi_1} \\otimes \\ket{\\psi_2} = (\\alpha_1\\ket{0} + \\beta_1\\ket{1}) \\otimes (\\alpha_2\\ket{0} + \\beta_2\\ket{1}) = \\alpha_1\\alpha_2\\ket{00} + \\alpha_1\\beta_2\\ket{01} + \\beta_1\\alpha_2\\ket{10} + \\beta_1\\beta_2\\ket{11}$$</p>\n<p>In order for this state to be equal to $\\frac{1}{\\sqrt2}\\big(\\ket{00} + \\ket{11}\\big)$, you need to have $\\alpha_1\\alpha_2 = \\beta_1\\beta_2 = \\frac{1}{\\sqrt2}$ and at the same time $\\alpha_1\\beta_2 = \\beta_1\\alpha_2 = 0$, which is impossible.</p>\n<p>This is the phenomena called <strong>entanglement</strong>, in which the states of the qubits are linked together and can't be considered individually.</p>\n</blockquote>\n<p>Let's see what steps you can take to prepare this state without factoring it into states of individual qubits.</p>\n<hr />\n<p>First, notice that you should end with a superposition of two of the four computational basis for two qubits: $\\ket{00}, \\ket{01}, \\ket{10}, \\ket{11}$.</p>\n<p>This gives you a hint that you should start by preparing a superposition on at least one of the qubits. Let’s try creating a superposition on the first qubit with a Hadamard gate:</p>\n<p>$$H\\ket{0} \\otimes \\ket{0} = \\frac{1}{\\sqrt2} (\\ket{0} + \\ket{1}) \\otimes \\ket{0} = \\frac{1}{\\sqrt2} (\\ket{00} + \\ket{10})$$</p>\n<p>Well, you got pretty close, except you need to transform the $\\ket{10}$ state to $\\ket{11}$.\nHow can you do this?</p>\n<p>You can take advantage of controlled gates, specifically the <a href=\"https://en.wikipedia.org/wiki/Controlled_NOT_gate\">controlled NOT gate</a>, also referred to as $CNOT$. This gate acts on two qubits, hence it's represented as a $4 \\times 4$ unitary matrix. The $CNOT$ gate changes the target qubit from state $\\ket{0}$ to $\\ket{1}$ and vice versa when the control qubit is $\\ket{1}$ and does nothing to the target qubit when the control qubit is $\\ket{0}$. The control qubit always remains unchanged.</p>\n<p>$$\\text{CNOT} = \\begin{bmatrix} 1 &amp; 0 &amp; 0 &amp; 0 \\\\ 0 &amp; 1 &amp; 0 &amp; 0 \\\\ 0 &amp; 0 &amp; 0 &amp; 1 \\\\ 0 &amp; 0 &amp; 1 &amp; 0 \\end{bmatrix}$$</p>\n<p>If you apply the $CNOT$ gate to the state $\\frac{1}{\\sqrt2} (\\ket{00} + \\ket{10})$, taking the first qubit as the control and the second one as target, you'll get exactly the desired goal state.</p>\n<p>Steps required to reach goal state:</p>\n<ol>\n<li>Apply a Hadamard gate to the first qubit.</li>\n<li>Applying a $CNOT$ gate with first qubit as control and second qubit as target.</li>\n</ol>\n<p>In matrix representation, you can represent this operation as a product of two $4 \\times 4$ matrices, with the matrix corresponding to the first step being the tensor product of a Hadamard gate on the first qubit and identity gate on the second qubit.</p>\n<p>$$H \\otimes I = \\frac{1}{\\sqrt2} \\begin{bmatrix} 1 &amp; 1 \\\\ 1 &amp; -1 \\end{bmatrix} \\otimes \\begin{bmatrix} 1 &amp; 0 \\\\ 0 &amp; 1 \\end{bmatrix} = \n\\frac{1}{\\sqrt2}\\begin{bmatrix} 1 &amp; 0 &amp; 1 &amp; 0 \\\\ 0 &amp; 1 &amp; 0 &amp; 1 \\\\ 1 &amp; 0 &amp; -1 &amp; 0 \\\\ 0 &amp; 1 &amp; 0 &amp; -1 \\end{bmatrix}$$</p>\n<p>$$\\underset{\\text{CNOT}}{\\underbrace{\\begin{bmatrix} 1 &amp; 0 &amp; 0 &amp; 0 \\\\ 0 &amp; 1 &amp; 0 &amp; 0 \\\\ 0 &amp; 0 &amp; 0 &amp; 1 \\\\ 0 &amp; 0 &amp; 1 &amp; 0 \\end{bmatrix}}} \n\\cdot \n\\underset{H \\otimes I}{\\underbrace{\\frac{1}{\\sqrt2} \\begin{bmatrix} 1 &amp; 0 &amp; 1 &amp; 0 \\\\ 0 &amp; 1 &amp; 0 &amp; 1 \\\\ 1 &amp; 0 &amp; -1 &amp; 0 \\\\ 0 &amp; 1 &amp; 0 &amp; -1 \\end{bmatrix}}}\n\\cdot\n\\underset{\\ket{0}}{\\underbrace{ \\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ 0 \\end{bmatrix}}}\n= \\frac{1}{\\sqrt2} \\begin{bmatrix} 1 &amp; 0 &amp; 1 &amp; 0 \\\\ 0 &amp; 1 &amp; 0 &amp; 1 \\\\ 0 &amp; 1 &amp; 0 &amp; -1 \\\\ 1 &amp; 0 &amp; -1 &amp; 0 \\end{bmatrix}\n\\cdot\n\\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ 0 \\end{bmatrix}\n= \\underset{goal}{\\underbrace{ \\frac{1}{\\sqrt2} \\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ 1 \\end{bmatrix}}}\n$$</p>\n<p>Note that in the matrix representation and in Dirac notation the gates are applied from right to left (the rightmost operation happens firts), while in circuit notation the operations are applied from left to right (the leftmost operation happens first).</p>\n"
2583
2583
  },
2584
2584
  {
2585
2585
  "type": "solution",
@@ -2608,7 +2608,7 @@ export default {
2608
2608
  "items": [
2609
2609
  {
2610
2610
  "type": "text-content",
2611
- "content": "<blockquote>\n<p>The <a href=\"https://en.wikipedia.org/wiki/Bell_state\">Bell states</a> form an orthonormal basis in the 4-dimensional space that describes the states of a 2-qubit system.\nYou can check that the norm of each of these states is 1, and their inner product of each pair of states is 0.</p>\n</blockquote>\n<p>The goal is to transform the $\\ket{00}$ basis state into one of the Bell basis states, depending on the value of <code>index</code> given as an input.</p>\n<p>We will describe two solutions, one of which will be based on the previous task, and the second one will help us understand the unitary transformation that converts the computational basis into the Bell basis.</p>\n<h4>Solution 1</h4>\n<p>Let's use the first Bell state we prepared in the previous task and transform it according to the value of <code>index</code>.</p>\n<p>$$\\frac{1}{\\sqrt2} \\big(\\ket{00} + \\ket{11}\\big)$$</p>\n<p>What transformation do we need to apply to get to the final state?</p>\n<ul>\n<li>\n<p>If <code>index = 0</code>, we do nothing - the prepared state is already $\\ket{\\Phi^{+}}$.</p>\n</li>\n<li>\n<p>If <code>index = 1</code>, we need to add a relative phase of $-1$ to the $\\ket{11}$ term. Remember that $Z$ gate does exactly that with a qubit:</p>\n<p>$$Z(H\\ket{0}) \\otimes \\ket{0} = \\frac{1}{\\sqrt2} \\big(\\ket{0} - \\ket{1}\\big) \\otimes \\ket{0} = \\frac{1}{\\sqrt2} \\big(\\ket{00} - \\ket{10}\\big)$$</p>\n<p>If we now apply the $CNOT$ as before, we will have:</p>\n<p>$$\\frac{1}{\\sqrt2} \\big(\\ket{00} - \\ket{\\overset{\\curvearrowright}{10}}\\big) \\underset{\\text{CNOT}}{\\Longrightarrow} \\frac{1}{\\sqrt2} \\big(\\ket{00} - \\ket{11}\\big) = \\ket{\\Phi^{-}}$$</p>\n</li>\n<li>\n<p>If <code>index = 2</code>, we need to change the second qubit in both $\\ket{00}$ and $\\ket{11}$ terms, which can be done applying an $X$ gate:</p>\n<p>$$H\\ket{0} \\otimes X\\ket{0} = H\\ket{0} \\otimes \\ket{1} = \\frac{1}{\\sqrt2} \\big(\\ket{0} + \\ket{1}\\big) \\otimes \\ket{1} = \\frac{1}{\\sqrt2} \\big(\\ket{01} + \\ket{11}\\big)$$</p>\n<p>If we now apply the $CNOT$ as before, we will have:</p>\n<p>$$\\frac{1}{\\sqrt2} \\big(\\ket{01} + \\ket{\\overset{\\curvearrowright}{11}}\\big) \\underset{\\text{CNOT}}{\\Longrightarrow} \\frac{1}{\\sqrt2} \\big(\\ket{01} + \\ket{10}\\big) = \\ket{\\Psi^{+}}$$</p>\n</li>\n<li>\n<p>If <code>index = 3</code>, we use the same logic to realize that we need to apply both the $Z$ and $X$ corrections to get $\\ket{\\Psi^{-}}$ state.</p>\n</li>\n</ul>\n<p>The final sequence of steps is as follows:</p>\n<ol>\n<li>Apply the $H$ gate to the first qubit.</li>\n<li>Apply the $Z$ gate to the first qubit if <code>index == 1</code> or <code>index == 3</code>.</li>\n<li>Apply the $X$ gate to the second qubit if <code>index == 2</code> or <code>index == 3</code>.</li>\n<li>Apply the $CNOT$ gate with the first qubit as control and the second qubit as target.</li>\n</ol>\n"
2611
+ "content": "<blockquote>\n<p>The <a href=\"https://en.wikipedia.org/wiki/Bell_state\">Bell states</a> form an orthonormal basis in the 4-dimensional space that describes the states of a 2-qubit system.\nYou can check that the norm of each of these states is 1, and their inner product of each pair of states is 0.</p>\n</blockquote>\n<p>The goal is to transform the $\\ket{00}$ basis state into one of the Bell basis states, depending on the value of <code>index</code> given as an input.</p>\n<p>This exercise provides two solutions, one of which is based on the previous task, and the second one helps you understand the unitary transformation that converts the computational basis into the Bell basis.</p>\n<h4>Solution 1</h4>\n<p>Let's use the first Bell state you prepared in the previous task and transform it according to the value of <code>index</code>.</p>\n<p>$$\\frac{1}{\\sqrt2} \\big(\\ket{00} + \\ket{11}\\big)$$</p>\n<p>What transformation do you need to apply to get to the final state?</p>\n<ul>\n<li>\n<p>If <code>index = 0</code>, you do nothing - the prepared state is already $\\ket{\\Phi^{+}}$.</p>\n</li>\n<li>\n<p>If <code>index = 1</code>, you need to add a relative phase of $-1$ to the $\\ket{11}$ term. Remember that $Z$ gate does exactly that with a qubit:</p>\n<p>$$Z(H\\ket{0}) \\otimes \\ket{0} = \\frac{1}{\\sqrt2} \\big(\\ket{0} - \\ket{1}\\big) \\otimes \\ket{0} = \\frac{1}{\\sqrt2} \\big(\\ket{00} - \\ket{10}\\big)$$</p>\n<p>If you now apply the $CNOT$ as before, you'll have:</p>\n<p>$$\\frac{1}{\\sqrt2} \\big(\\ket{00} - \\ket{\\overset{\\curvearrowright}{10}}\\big) \\underset{\\text{CNOT}}{\\Longrightarrow} \\frac{1}{\\sqrt2} \\big(\\ket{00} - \\ket{11}\\big) = \\ket{\\Phi^{-}}$$</p>\n</li>\n<li>\n<p>If <code>index = 2</code>, you need to change the second qubit in both $\\ket{00}$ and $\\ket{11}$ terms, which can be done applying an $X$ gate:</p>\n<p>$$H\\ket{0} \\otimes X\\ket{0} = H\\ket{0} \\otimes \\ket{1} = \\frac{1}{\\sqrt2} \\big(\\ket{0} + \\ket{1}\\big) \\otimes \\ket{1} = \\frac{1}{\\sqrt2} \\big(\\ket{01} + \\ket{11}\\big)$$</p>\n<p>If you now apply the $CNOT$ as before, you'll have:</p>\n<p>$$\\frac{1}{\\sqrt2} \\big(\\ket{01} + \\ket{\\overset{\\curvearrowright}{11}}\\big) \\underset{\\text{CNOT}}{\\Longrightarrow} \\frac{1}{\\sqrt2} \\big(\\ket{01} + \\ket{10}\\big) = \\ket{\\Psi^{+}}$$</p>\n</li>\n<li>\n<p>If <code>index = 3</code>, you use the same logic to realize that you need to apply both the $Z$ and $X$ corrections to get $\\ket{\\Psi^{-}}$ state.</p>\n</li>\n</ul>\n<p>The final sequence of steps is as follows:</p>\n<ol>\n<li>Apply the $H$ gate to the first qubit.</li>\n<li>Apply the $Z$ gate to the first qubit if <code>index == 1</code> or <code>index == 3</code>.</li>\n<li>Apply the $X$ gate to the second qubit if <code>index == 2</code> or <code>index == 3</code>.</li>\n<li>Apply the $CNOT$ gate with the first qubit as control and the second qubit as target.</li>\n</ol>\n"
2612
2612
  },
2613
2613
  {
2614
2614
  "type": "solution",
@@ -2617,7 +2617,7 @@ export default {
2617
2617
  },
2618
2618
  {
2619
2619
  "type": "text-content",
2620
- "content": "<h4>Solution 2</h4>\n<p>Let's take a closer look at the unitary transformation $\\text{CNOT}\\cdot(H \\otimes I)$ discussed in the previous task.</p>\n<p>$$\\frac{1}{\\sqrt2} \\begin{bmatrix} 1 &amp; 0 &amp; 1 &amp; 0 \\\\ 0 &amp; 1 &amp; 0 &amp; 1 \\\\ 0 &amp; 1 &amp; 0 &amp; -1 \\\\ \\underset{\\ket{\\Phi^{+}}}{\\underbrace{1}} &amp; \\underset{\\ket{\\Psi^{+}}}{\\underbrace{0}} &amp; \\underset{\\ket{\\Phi^{-}}}{\\underbrace{-1}} &amp; \\underset{\\ket{\\Psi^{-}}}{\\underbrace{0}} \\end{bmatrix}$$</p>\n<p>Notice that each of the columns in the unitary matrix corresponds to one of the Bell states.\nThis unitary transformation transforms the computational basis into the Bell basis, which is exactly what the task asks us to do.</p>\n<p>We see that this transformation converts $\\ket{00}$ into the first Bell state, $\\ket{01}$ into the second Bell state, etc.\nWe just need to make sure we set the qubits to the correct state before applying this transformation, using $X$ gates to change the initial $\\ket{0}$ states to $\\ket{1}$ if needed.</p>\n<p>In Q#, we can use the <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.convert/intasboolarray\">IntAsBoolArray</a> function to convert the input <code>index</code> to the right bit pattern.</p>\n"
2620
+ "content": "<h4>Solution 2</h4>\n<p>Let's take a closer look at the unitary transformation $\\text{CNOT}\\cdot(H \\otimes I)$ discussed in the previous task.</p>\n<p>$$\\frac{1}{\\sqrt2} \\begin{bmatrix} 1 &amp; 0 &amp; 1 &amp; 0 \\\\ 0 &amp; 1 &amp; 0 &amp; 1 \\\\ 0 &amp; 1 &amp; 0 &amp; -1 \\\\ \\underset{\\ket{\\Phi^{+}}}{\\underbrace{1}} &amp; \\underset{\\ket{\\Psi^{+}}}{\\underbrace{0}} &amp; \\underset{\\ket{\\Phi^{-}}}{\\underbrace{-1}} &amp; \\underset{\\ket{\\Psi^{-}}}{\\underbrace{0}} \\end{bmatrix}$$</p>\n<p>Notice that each of the columns in the unitary matrix corresponds to one of the Bell states.\nThis unitary transformation transforms the computational basis into the Bell basis, which is exactly what the task asks you to do.</p>\n<p>You see that this transformation converts $\\ket{00}$ into the first Bell state, $\\ket{01}$ into the second Bell state, etc.\nYou just need to make sure you set the qubits to the correct state before applying this transformation, using $X$ gates to change the initial $\\ket{0}$ states to $\\ket{1}$ if needed.</p>\n<p>In Q#, you can use the <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.convert/intasboolarray\">IntAsBoolArray</a> function to convert the input <code>index</code> to the right bit pattern.</p>\n"
2621
2621
  },
2622
2622
  {
2623
2623
  "type": "solution",
@@ -2646,7 +2646,7 @@ export default {
2646
2646
  "items": [
2647
2647
  {
2648
2648
  "type": "text-content",
2649
- "content": "<p>The single-qubit GHZ state is the plus state $\\frac{1}{\\sqrt{2}} \\big (\\ket{0} + \\ket{1}\\big)$ that we've discussed in the first task. As a reminder, that state is prepared by applying a Hadamard gate.</p>\n<p>The 2-qubit GHZ state is the Bell state $\\frac{1}{\\sqrt{2}} \\big (\\ket{00} + \\ket{11}\\big)$ that we've discussed in the two previous tasks.</p>\n<p>The next one is the 3-qubit GHZ state:\n$$\\ket{GHZ} = \\frac{1}{\\sqrt{2}} \\big (\\ket{000} + \\ket{111}\\big)$$</p>\n<p>Let's use the 2-qubit state as a building block to construct the state for 3 qubits. First, let's add a third qubit to the above state (on the right from the first two qubits).\nComparing this state with the desired end state, we see that they differ only in the third (rightmost) qubit:</p>\n<p>$$\\ket{\\Phi^+} \\ket{0} = \\frac{1}{\\sqrt{2}} \\big (\\ket{000} + \\ket{11\\textbf{0}}\\big)$$\n$$\\ket{GHZ} = \\frac{1}{\\sqrt{2}} \\big (\\ket{000} + \\ket{11\\textbf{1}}\\big)$$</p>\n<p>Applying a controlled NOT operation using the first (leftmost) qubit as the control bit and the third (rightmost) qubit as the target qubit allows us to fix this difference.</p>\n<p>Thus we can come to the general solution: apply Hadamard gate to the first qubit and do a series of CNOT gates with the first qubit as control and each of the other qubits as targets.</p>\n"
2649
+ "content": "<p>The single-qubit GHZ state is the plus state $\\frac{1}{\\sqrt{2}} \\big (\\ket{0} + \\ket{1}\\big)$ that you've discussed in the first task. As a reminder, that state is prepared by applying a Hadamard gate.</p>\n<p>The 2-qubit GHZ state is the Bell state $\\frac{1}{\\sqrt{2}} \\big (\\ket{00} + \\ket{11}\\big)$ that we've discussed in the two previous tasks.</p>\n<p>The next one is the 3-qubit GHZ state:\n$$\\ket{GHZ} = \\frac{1}{\\sqrt{2}} \\big (\\ket{000} + \\ket{111}\\big)$$</p>\n<p>Let's use the 2-qubit state as a building block to construct the state for 3 qubits. First, let's add a third qubit to the above state (on the right from the first two qubits).\nComparing this state with the desired end state, you see that they differ only in the third (rightmost) qubit:</p>\n<p>$$\\ket{\\Phi^+} \\ket{0} = \\frac{1}{\\sqrt{2}} \\big (\\ket{000} + \\ket{11\\textbf{0}}\\big)$$\n$$\\ket{GHZ} = \\frac{1}{\\sqrt{2}} \\big (\\ket{000} + \\ket{11\\textbf{1}}\\big)$$</p>\n<p>Applying a controlled $NOT$ operation using the first (leftmost) qubit as the control bit and the third (rightmost) qubit as the target qubit allows you to fix this difference.</p>\n<p>Thus, you can come to the general solution: apply Hadamard gate to the first qubit and do a series of $CNOT$ gates with the first qubit as control and each of the other qubits as targets.</p>\n"
2650
2650
  },
2651
2651
  {
2652
2652
  "type": "solution",
@@ -2675,7 +2675,7 @@ export default {
2675
2675
  "items": [
2676
2676
  {
2677
2677
  "type": "text-content",
2678
- "content": "<p>As we've seen in the 'Superposition of all basis vectors on two qubits' task, to prepare a superposition of all basis vectors on 2 qubits we need to apply a Hadamard gate to each of the qubits.</p>\n<p>It seems that the solution for the general case might be to apply a Hadamard gate to every qubit as well. Let's check the first few examples:</p>\n<p>$$\\begin{align*}\n H\\ket{0} &amp;= \\frac{1}{\\sqrt2}\\big(\\ket{0} + \\ket{1}\\big) \\\\\n H\\ket{0} \\otimes H\\ket{0} &amp;= \\frac{1}{\\sqrt2} \\big(\\ket{0} + \\ket{1}\\big) \\otimes \\frac{1}{\\sqrt2} \\big(\\ket{0} + \\ket{1}\\big) \\\\ \n &amp;= \\frac{1}{\\sqrt{2^2}}\\big(\\ket{00} + \\ket{01}+ \\ket{10}+ \\ket{11}\\big) \\\\\n H\\ket{0} \\otimes H\\ket{0} \\otimes H\\ket{0} &amp;= \\frac{1}{\\sqrt{2^2}}\\big(\\ket{00} + \\ket{01}+ \\ket{10}+ \\ket{11}\\big) \\otimes \\frac{1}{\\sqrt2}\\big(\\ket{0} + \\ket{1}\\big) \\\\\n &amp;= \\frac{1}{\\sqrt{2^3}}\\big(\\ket{000} + \\ket{001} + \\ket{010}+ \\ket{100}+ \\ket{110} + \\ket{101}+ \\ket{011}+ \\ket{111}\\big) \\\\\n \\underset{N}{\\underbrace{H\\ket{0} \\otimes \\dots \\otimes H\\ket{0}}} \n &amp;= \\frac{1}{\\sqrt{2^{N-1}}} \\big( \\ket{\\underset{N-1}{\\underbrace{0 \\cdots 0}}} + \\cdots + \\ket{\\underset{N-1}{\\underbrace{1 \\cdots 1}}} \\big) \\otimes \\frac{1}{\\sqrt2}\\big(\\ket{0} + \\ket{1}\\big) = \\\\\n &amp;= \\frac{1}{\\sqrt{2^N}} \\big( \\ket{\\underset{N}{\\underbrace{0 \\cdots 0}}} + \\cdots + \\ket{\\underset{N}{\\underbrace{1 \\cdots 1}}} \\big) \n\\end{align*}$$</p>\n<p>Thus, the solution requires us to iterate over the qubit array and to apply the Hadamard gate to each element.</p>\n"
2678
+ "content": "<p>As you've seen in the 'Superposition of all basis vectors on two qubits' task, to prepare a superposition of all basis vectors on 2 qubits you need to apply a Hadamard gate to each of the qubits.</p>\n<p>It seems that the solution for the general case might be to apply a Hadamard gate to every qubit as well. Let's check the first few examples:</p>\n<p>$$\\begin{align*}\n H\\ket{0} &amp;= \\frac{1}{\\sqrt2}\\big(\\ket{0} + \\ket{1}\\big) \\\\\n H\\ket{0} \\otimes H\\ket{0} &amp;= \\frac{1}{\\sqrt2} \\big(\\ket{0} + \\ket{1}\\big) \\otimes \\frac{1}{\\sqrt2} \\big(\\ket{0} + \\ket{1}\\big) \\\\ \n &amp;= \\frac{1}{\\sqrt{2^2}}\\big(\\ket{00} + \\ket{01}+ \\ket{10}+ \\ket{11}\\big) \\\\\n H\\ket{0} \\otimes H\\ket{0} \\otimes H\\ket{0} &amp;= \\frac{1}{\\sqrt{2^2}}\\big(\\ket{00} + \\ket{01}+ \\ket{10}+ \\ket{11}\\big) \\otimes \\frac{1}{\\sqrt2}\\big(\\ket{0} + \\ket{1}\\big) \\\\\n &amp;= \\frac{1}{\\sqrt{2^3}}\\big(\\ket{000} + \\ket{001} + \\ket{010}+ \\ket{100}+ \\ket{110} + \\ket{101}+ \\ket{011}+ \\ket{111}\\big) \\\\\n \\underset{N}{\\underbrace{H\\ket{0} \\otimes \\dots \\otimes H\\ket{0}}} \n &amp;= \\frac{1}{\\sqrt{2^{N-1}}} \\big( \\ket{\\underset{N-1}{\\underbrace{0 \\cdots 0}}} + \\cdots + \\ket{\\underset{N-1}{\\underbrace{1 \\cdots 1}}} \\big) \\otimes \\frac{1}{\\sqrt2}\\big(\\ket{0} + \\ket{1}\\big) = \\\\\n &amp;= \\frac{1}{\\sqrt{2^N}} \\big( \\ket{\\underset{N}{\\underbrace{0 \\cdots 0}}} + \\cdots + \\ket{\\underset{N}{\\underbrace{1 \\cdots 1}}} \\big) \n\\end{align*}$$</p>\n<p>Thus, the solution requires you to iterate over the qubit array and to apply the Hadamard gate to each element.</p>\n"
2679
2679
  },
2680
2680
  {
2681
2681
  "type": "solution",
@@ -2704,7 +2704,7 @@ export default {
2704
2704
  "items": [
2705
2705
  {
2706
2706
  "type": "text-content",
2707
- "content": "<p>Let’s look at some examples of basis states to illustrate the binary numbering system.</p>\n<p>The 4 basis states on $N = 2$ qubits can be split in two columns, where the left column represents the basis states that form the required superposition state for <code>isEven = true</code> and the right column - the basis states that form the required superposition state for <code>isEven = false</code>.</p>\n<table>\n <tr><th>even</th><th>odd</th></tr>\n <tr><td> <b>0</b>0 </td><td> <b>0</b>1 </td></tr>\n <tr><td> <b>1</b>0 </td><td> <b>1</b>1 </td></tr>\n</table>\n<p>If we do the same basis state split for $N = 3$ qubits, the pattern becomes more obvious.</p>\n<table>\n <tr><th>even</th><th>odd</th></tr>\n <tr><td> <b>00</b>0 </td><td> <b>00</b>1</td></tr>\n <tr><td> <b>01</b>0 </td><td> <b>01</b>1</td></tr>\n <tr><td> <b>10</b>0 </td><td> <b>10</b>1</td></tr>\n <tr><td> <b>11</b>0 </td><td> <b>11</b>1</td></tr>\n</table>\n<p>The two leftmost qubits go through all possible basis states for <code>isEven = true</code> and for <code>isEven = false</code>, and the rightmost qubit stays in the $\\ket{0}$ state for <code>isEven = true</code> and in the $\\ket{1}$ state for <code>isEven = false</code>.</p>\n<p>A quick sanity check for $N = 4$ qubits re-confirms the pattern.</p>\n<table>\n <tr>\n <th>even</th>\n <th>odd</th>\n </tr>\n <tr><td> <b>000</b>0 </td><td> <b>000</b>1</td></tr>\n <tr><td> <b>001</b>0 </td><td> <b>001</b>1</td></tr>\n <tr><td> <b>010</b>0 </td><td> <b>010</b>1</td></tr>\n <tr><td> <b>011</b>0 </td><td> <b>011</b>1</td></tr>\n <tr><td> <b>100</b>0 </td><td> <b>100</b>1</td></tr>\n <tr><td> <b>101</b>0 </td><td> <b>101</b>1</td></tr>\n <tr><td> <b>110</b>0 </td><td> <b>110</b>1</td></tr>\n <tr><td> <b>111</b>0 </td><td> <b>111</b>1</td></tr>\n</table>\n<p>Again, the three leftmost qubits go through all possible basis states in both columns, and the rightmost qubit stays in the same state in each column.</p>\n<p>The solution is to put all qubits except the rightmost one into an equal superposition (similar to what we did in the 'Superposition of all basis vectors' task) and to set the rightmost qubit to $\\ket{0}$ or $\\ket{1}$ depending on the <code>isEven</code> flag, using the X operator to convert $\\ket{0}$ to $\\ket{1}$ if <code>isEven = false</code>.</p>\n"
2707
+ "content": "<p>Let’s look at some examples of basis states to illustrate the binary numbering system.</p>\n<p>The 4 basis states on $N = 2$ qubits can be split in two columns, where the left column represents the basis states that form the required superposition state for <code>isEven = true</code> and the right column - the basis states that form the required superposition state for <code>isEven = false</code>.</p>\n<table>\n <tr><th>even</th><th>odd</th></tr>\n <tr><td> <b>0</b>0 </td><td> <b>0</b>1 </td></tr>\n <tr><td> <b>1</b>0 </td><td> <b>1</b>1 </td></tr>\n</table>\n<p>If you do the same basis state split for $N = 3$ qubits, the pattern becomes more obvious.</p>\n<table>\n <tr><th>even</th><th>odd</th></tr>\n <tr><td> <b>00</b>0 </td><td> <b>00</b>1</td></tr>\n <tr><td> <b>01</b>0 </td><td> <b>01</b>1</td></tr>\n <tr><td> <b>10</b>0 </td><td> <b>10</b>1</td></tr>\n <tr><td> <b>11</b>0 </td><td> <b>11</b>1</td></tr>\n</table>\n<p>The two leftmost qubits go through all possible basis states for <code>isEven = true</code> and for <code>isEven = false</code>, and the rightmost qubit stays in the $\\ket{0}$ state for <code>isEven = true</code> and in the $\\ket{1}$ state for <code>isEven = false</code>.</p>\n<p>A quick sanity check for $N = 4$ qubits re-confirms the pattern.</p>\n<table>\n <tr>\n <th>even</th>\n <th>odd</th>\n </tr>\n <tr><td> <b>000</b>0 </td><td> <b>000</b>1</td></tr>\n <tr><td> <b>001</b>0 </td><td> <b>001</b>1</td></tr>\n <tr><td> <b>010</b>0 </td><td> <b>010</b>1</td></tr>\n <tr><td> <b>011</b>0 </td><td> <b>011</b>1</td></tr>\n <tr><td> <b>100</b>0 </td><td> <b>100</b>1</td></tr>\n <tr><td> <b>101</b>0 </td><td> <b>101</b>1</td></tr>\n <tr><td> <b>110</b>0 </td><td> <b>110</b>1</td></tr>\n <tr><td> <b>111</b>0 </td><td> <b>111</b>1</td></tr>\n</table>\n<p>Again, the three leftmost qubits go through all possible basis states in both columns, and the rightmost qubit stays in the same state in each column.</p>\n<p>The solution is to put all qubits except the rightmost one into an equal superposition (similar to what you did in the 'Superposition of all basis vectors' task) and to set the rightmost qubit to $\\ket{0}$ or $\\ket{1}$ depending on the <code>isEven</code> flag, using the $X$ operator to convert $\\ket{0}$ to $\\ket{1}$ if <code>isEven = false</code>.</p>\n"
2708
2708
  },
2709
2709
  {
2710
2710
  "type": "solution",
@@ -2720,7 +2720,7 @@ export default {
2720
2720
  "title": "Zero and Bitstring",
2721
2721
  "description": {
2722
2722
  "type": "text-content",
2723
- "content": "<p><strong>Inputs:</strong></p>\n<ol>\n<li>$N$ ($N \\ge 1$) qubits in the $\\ket{0 \\dots 0}$ state.</li>\n<li>A bit string of length $N$ represented as <code>Bool[]</code>. Bit values <code>false</code> and <code>true</code> correspond to $\\ket{0}$ and $\\ket{1}$ states. You are guaranteed that the first bit of the bit string is <code>true</code>.</li>\n</ol>\n<p><strong>Goal:</strong> Change the state of the qubits to an equal superposition of $\\ket{0 \\dots 0}$ and the basis state given by the bit string.</p>\n<blockquote>\n<p>For example, for the bit string <code>[true, false]</code> the state required is $\\frac{1}{\\sqrt{2}}\\big(\\ket{00} + \\ket{10}\\big)$.</p>\n</blockquote>\n"
2723
+ "content": "<p><strong>Inputs:</strong></p>\n<ol>\n<li>$N$ ($N \\ge 1$) qubits in the $\\ket{0 \\dots 0}$ state.</li>\n<li>A bit string of length $N$ represented as <code>Bool[]</code>. Bit values <code>false</code> and <code>true</code> correspond to $\\ket{0}$ and $\\ket{1}$ states. You're guaranteed that the first bit of the bit string is <code>true</code>.</li>\n</ol>\n<p><strong>Goal:</strong> Change the state of the qubits to an equal superposition of $\\ket{0 \\dots 0}$ and the basis state given by the bit string.</p>\n<blockquote>\n<p>For example, for the bit string <code>[true, false]</code> the state required is $\\frac{1}{\\sqrt{2}}\\big(\\ket{00} + \\ket{10}\\big)$.</p>\n</blockquote>\n"
2724
2724
  },
2725
2725
  "sourceIds": [
2726
2726
  "preparing_states__zero_and_bitstring__Verification.qs",
@@ -2733,7 +2733,7 @@ export default {
2733
2733
  "items": [
2734
2734
  {
2735
2735
  "type": "text-content",
2736
- "content": "<blockquote>\n<p>A common strategy for preparing a superposition state in a qubit register is using an auxiliary qubit (or several, for more complicated states). The auxiliary qubit can be put into a superposition state through the usual means of applying a Hadamard gate (or a rotation about the Y axis for an uneven superposition).\nThen the basis states of the desired superposition are prepared individually based on the auxiliary qubit state by using it as the control qubit for a $CNOT$ gate. One of the basis states will be prepared controlled on the $\\ket{0}$ component of the auxiliary state, and the other - controlled on the $\\ket{1}$ component.\nFinally, you have to return the auxiliary qubit to the $\\ket{0}$ state by uncomputing it, i.e., by using the basis state prepared from the $\\ket{1}$ component as the control qubits for a $CNOT$ gate with the auxiliary qubit as the target.</p>\n<p>More details on using this approach can be found in the solution to &quot;Superposition of Four Bitstrings&quot; and &quot;W State on $2^k$ Qubits&quot; tasks. However, for this task we can come up with a simpler solution.\nInstead of allocating a new qubit to use as the auxiliary, we can use the first qubit in the register for this purpose, because we are guaranteed that the first bit in the two basis vectors that comprise the required superposition is different.\nThis saves us the need to allocate a new qubit and lets us skip the uncomputing step, as the qubit acting as the control for the next preparation steps is part of the desired result.</p>\n</blockquote>\n<p>Consider the earlier tasks in this kata that asked to prepare Bell states and GHZ state; the structure of the superposition state in this task is a more general case of those scenarios: all of them ask to prepare an equal superposition of two different basis states.</p>\n<p>The first step of the solution is the same as in those tasks: put the first qubit in the register into an equal superposition of $\\ket{0}$ and $\\ket{1}$ using the H gate to get the following state:</p>\n<p>$$\\frac{1}{\\sqrt2} (\\ket{0} + \\ket{1}) \\otimes \\ket{0 \\dots 0} = \\frac{1}{\\sqrt2} (\\ket{00 \\dots 0} + \\ket{10 \\dots 0})$$</p>\n<p>The first term of the superposition already matches the desired state, so we need to fix the second term.\nTo do that, we will walk through the remaining qubits in the register, checking if the bit in the corresponding position of the bit string <code>bits</code> is <code>true</code>.\nIf it is, that qubit's state needs to be adjusted from $0$ to $1$ in the second term of our superposition (and left unchanged in the first term).\nWe can do this change using the $CNOT$ gate with the first qubit as the control and the current qubit as the target.\nWhen we have finished walking through the register like this, the register will be in the desired superposition.</p>\n"
2736
+ "content": "<blockquote>\n<p>A common strategy for preparing a superposition state in a qubit register is using an auxiliary qubit (or several, for more complicated states). The auxiliary qubit can be put into a superposition state through the usual means of applying a Hadamard gate (or a rotation about the $Y$ axis for an uneven superposition).\nThen the basis states of the desired superposition are prepared individually based on the auxiliary qubit state by using it as the control qubit for a $CNOT$ gate. One of the basis states will be prepared controlled on the $\\ket{0}$ component of the auxiliary state, and the other - controlled on the $\\ket{1}$ component.\nFinally, you have to return the auxiliary qubit to the $\\ket{0}$ state by uncomputing it, that is, by using the basis state prepared from the $\\ket{1}$ component as the control qubits for a $CNOT$ gate with the auxiliary qubit as the target.</p>\n<p>More details on using this approach can be found in the solution to &quot;Superposition of Four Bitstrings&quot; and &quot;W State on $2^k$ Qubits&quot; tasks. However, for this task you can come up with a simpler solution.\nInstead of allocating a new qubit to use as the auxiliary, you can use the first qubit in the register for this purpose, because you're guaranteed that the first bit in the two basis vectors that comprise the required superposition is different.\nThis saves you the need to allocate a new qubit and lets us skip the uncomputing step, as the qubit acting as the control for the next preparation steps is part of the desired result.</p>\n</blockquote>\n<p>Consider the earlier tasks in this kata that asked to prepare Bell states and GHZ state; the structure of the superposition state in this task is a more general case of those scenarios: all of them ask to prepare an equal superposition of two different basis states.</p>\n<p>The first step of the solution is the same as in those tasks: put the first qubit in the register into an equal superposition of $\\ket{0}$ and $\\ket{1}$ using the $H$ gate to get the following state:</p>\n<p>$$\\frac{1}{\\sqrt2} (\\ket{0} + \\ket{1}) \\otimes \\ket{0 \\dots 0} = \\frac{1}{\\sqrt2} (\\ket{00 \\dots 0} + \\ket{10 \\dots 0})$$</p>\n<p>The first term of the superposition already matches the desired state, so you need to fix the second term.\nTo do that, you'll walk through the remaining qubits in the register, checking if the bit in the corresponding position of the bit string <code>bits</code> is <code>true</code>.\nIf it is, that qubit's state needs to be adjusted from $0$ to $1$ in the second term of the superposition (and left unchanged in the first term).\nYou can do this change using the $CNOT$ gate with the first qubit as the control and the current qubit as the target.\nWhen you have finished walking through the register like this, the register will be in the desired superposition.</p>\n"
2737
2737
  },
2738
2738
  {
2739
2739
  "type": "solution",
@@ -2749,7 +2749,7 @@ export default {
2749
2749
  "title": "Two Bitstrings",
2750
2750
  "description": {
2751
2751
  "type": "text-content",
2752
- "content": "<p><strong>Inputs:</strong></p>\n<ol>\n<li>$N$ ($N \\ge 1$) qubits in the $\\ket{0 \\dots 0}$ state.</li>\n<li>Two bit strings of length $N$ represented as <code>Bool[]</code>s. Bit values <code>false</code> and <code>true</code> correspond to $\\ket{0}$ and $\\ket{1}$ states. You are guaranteed that the two bit strings differ in at least one bit.</li>\n</ol>\n<p><strong>Goal:</strong> Change the state of the qubits to an equal superposition of the basis states given by the bit strings.</p>\n<blockquote>\n<p>For example, for bit strings <code>[false, true, false]</code> and <code>[false, false, true]</code> the state required is $\\frac{1}{\\sqrt{2}}\\big(\\ket{010} + \\ket{001}\\big)$.</p>\n</blockquote>\n"
2752
+ "content": "<p><strong>Inputs:</strong></p>\n<ol>\n<li>$N$ ($N \\ge 1$) qubits in the $\\ket{0 \\dots 0}$ state.</li>\n<li>Two bit strings of length $N$ represented as <code>Bool[]</code>s. Bit values <code>false</code> and <code>true</code> correspond to $\\ket{0}$ and $\\ket{1}$ states. You're guaranteed that the two bit strings differ in at least one bit.</li>\n</ol>\n<p><strong>Goal:</strong> Change the state of the qubits to an equal superposition of the basis states given by the bit strings.</p>\n<blockquote>\n<p>For example, for bit strings <code>[false, true, false]</code> and <code>[false, false, true]</code> the state required is $\\frac{1}{\\sqrt{2}}\\big(\\ket{010} + \\ket{001}\\big)$.</p>\n</blockquote>\n"
2753
2753
  },
2754
2754
  "sourceIds": [
2755
2755
  "preparing_states__two_bitstrings__Verification.qs",
@@ -2762,7 +2762,7 @@ export default {
2762
2762
  "items": [
2763
2763
  {
2764
2764
  "type": "text-content",
2765
- "content": "<p>The strategy of using an auxiliary qubit to control the preparation process described in the previous task can be applied to this task as well.</p>\n<p>We will start by allocating an auxiliary qubit and preparing it in the $\\frac{1}{\\sqrt2} (\\ket{0} + \\ket{1})$ state using the $H$ gate. The overall state of the system will be</p>\n<p>$$\\frac{1}{\\sqrt2} (\\ket{0} + \\ket{1})_a \\otimes \\ket{0 \\dots 0}_r = \\frac{1}{\\sqrt2} (\\ket{0}_a \\otimes \\ket{0 \\dots 0}_r + \\ket{1}_a \\otimes \\ket{0 \\dots 0}_r)$$</p>\n<p>At this point, we can prepare the two basis states of the target state separately, bit by bit, controlling the preparation of one of them on the $\\ket{0}$ state of the auxiliary qubit and the preparation of the other one - on the $\\ket{1}$ state.\nIf a bit in one of the bit strings is <code>true</code>, we will apply a controlled $X$ gate with the auxiliary qubit as control, the qubit in the corresponding position of the register as target, and control it on the $\\ket{0}$ or the $\\ket{1}$ state depending on which bit string we are considering at the moment.\nSuch controlled gate can be implemented using <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.canon/applycontrolledonint\"><code>ApplyControlledOnInt</code></a> library function.</p>\n<p>After this the state of the system will be\n$$\\frac{1}{\\sqrt2} (\\ket{0}_a \\otimes \\ket{bits_1}_r + \\ket{1}_a \\otimes \\ket{bits_2}_r)$$</p>\n<p>Finally, we will uncompute the auxiliary qubit by using <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.canon/applycontrolledonbitstring\"><code>ApplyControlledOnBitString</code></a> library function with the second bit string and the <code>X</code> operation as arguments, the quantum register as the control, and the auxiliary qubit as the target.\nThis will affect only the $\\ket{1}_a \\otimes \\ket{bits_2}_r$ term, flipping the state of the auxiliary qubit in it and bringing the system to its final state:</p>\n<p>$$\\ket{0}_a \\otimes \\frac{1}{\\sqrt2} (\\ket{bits_1} + \\ket{bits_2})_r$$</p>\n"
2765
+ "content": "<p>The strategy of using an auxiliary qubit to control the preparation process described in the previous task can be applied to this task as well.</p>\n<p>You'll start by allocating an auxiliary qubit and preparing it in the $\\frac{1}{\\sqrt2} (\\ket{0} + \\ket{1})$ state using the $H$ gate. The overall state of the system will be</p>\n<p>$$\\frac{1}{\\sqrt2} (\\ket{0} + \\ket{1})_a \\otimes \\ket{0 \\dots 0}_r = \\frac{1}{\\sqrt2} (\\ket{0}_a \\otimes \\ket{0 \\dots 0}_r + \\ket{1}_a \\otimes \\ket{0 \\dots 0}_r)$$</p>\n<p>At this point, you can prepare the two basis states of the target state separately, bit by bit, controlling the preparation of one of them on the $\\ket{0}$ state of the auxiliary qubit and the preparation of the other one - on the $\\ket{1}$ state.\nIf a bit in one of the bit strings is <code>true</code>, you'll apply a controlled $X$ gate with the auxiliary qubit as control, the qubit in the corresponding position of the register as target, and control it on the $\\ket{0}$ or the $\\ket{1}$ state depending on which bit string you're considering at the moment.\nSuch controlled gate can be implemented using the <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.canon/applycontrolledonint\"><code>ApplyControlledOnInt</code></a> library function.</p>\n<p>After this the state of the system will be\n$$\\frac{1}{\\sqrt2} (\\ket{0}_a \\otimes \\ket{bits_1}_r + \\ket{1}_a \\otimes \\ket{bits_2}_r)$$</p>\n<p>Finally, you'll uncompute the auxiliary qubit by using <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.canon/applycontrolledonbitstring\"><code>ApplyControlledOnBitString</code></a> library function with the second bit string and the <code>X</code> operation as arguments, the quantum register as the control, and the auxiliary qubit as the target.\nThis will affect only the $\\ket{1}_a \\otimes \\ket{bits_2}_r$ term, flipping the state of the auxiliary qubit in it and bringing the system to its final state:</p>\n<p>$$\\ket{0}_a \\otimes \\frac{1}{\\sqrt2} (\\ket{bits_1} + \\ket{bits_2})_r$$</p>\n"
2766
2766
  },
2767
2767
  {
2768
2768
  "type": "solution",
@@ -2771,7 +2771,7 @@ export default {
2771
2771
  },
2772
2772
  {
2773
2773
  "type": "text-content",
2774
- "content": "<p>It is also possible to solve the task without using an extra qubit, if instead we use one of the qubits in the register in this role.\nWhile walking through the register and bit strings, the first time the bit strings disagreed, the qubit in the corresponding position would take on the role of the auxiliary qubit; we would put it in superposition using the $H$ gate and perform all subsequent bit flips using that qubit as the control.</p>\n<p>This saves us an additional qubit and allows to skip the uncomputing step, though the code becomes less elegant.\nWe will move the classical logic of comparing two bit strings to find the first position in which they differ to a function <code>FindFirstDiff</code>.</p>\n"
2774
+ "content": "<p>It's also possible to solve the task without using an extra qubit, if instead you use one of the qubits in the register in this role.\nWhile walking through the register and bit strings, the first time the bit strings disagreed, the qubit in the corresponding position would take on the role of the auxiliary qubit; you'd put it in superposition using the $H$ gate and perform all subsequent bit flips using that qubit as the control.</p>\n<p>This saves you an additional qubit and allows to skip the uncomputing step, though the code becomes less elegant.\nYou'll move the classical logic of comparing two bit strings to find the first position in which they differ to a function <code>FindFirstDiff</code>.</p>\n"
2775
2775
  },
2776
2776
  {
2777
2777
  "type": "solution",
@@ -2800,7 +2800,7 @@ export default {
2800
2800
  "items": [
2801
2801
  {
2802
2802
  "type": "text-content",
2803
- "content": "<p>We are going to use the same trick of auxiliary qubits that we used in the previous task.</p>\n<p>Since the desired superposition has 4 basis states with equal amplitudes, we are going to need two qubits to define a unique basis to control preparation of each of the basis states in the superposition.</p>\n<p>We start by allocating two extra qubits and preparing an equal superposition of all 2-qubit states on them by applying an H gate to each of them:</p>\n<p>$$\\frac12 (\\ket{00} + \\ket{01} + \\ket{10} + \\ket{11})_a \\otimes \\ket{0 \\dots 0}_r$$</p>\n<p>Then, for each of the four given bit strings, we walk through it and prepare the matching basis state on the main register of qubits, using controlled X gates with the corresponding basis state of the auxiliary qubits as control.</p>\n<p>For example, when preparing the bit string <code>bits[0]</code>, we apply X gates controlled on the basis state $\\ket{00}$; when preparing the bit string <code>bits[1]</code>, we apply X gates controlled on $\\ket{10}$, and so on.</p>\n<blockquote>\n<p>We can choose an arbitrary matching of the 2-qubit basis states used as controls and the bit strings prepared on the main register.\nSince all amplitudes are the same, the result does not depend on which state controlled which bit string preparation.\nIt can be convenient to use indices of the bit strings, converted to little-endian, to control preparation of the bit strings.\nQ# library function <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.canon/applycontrolledonint\"><code>ApplyControlledOnInt</code></a> does exactly that.</p>\n</blockquote>\n<p>After this the system will be in the state</p>\n<p>$$\\frac12 (\\ket{00}_a \\ket{bits_0}_r + \\ket{10}_a \\ket{bits_1}_r + \\ket{01}_a \\ket{bits_2}_r + \\ket{11}_a \\ket{bits_3}_r)$$</p>\n<p>As the last step, we must uncompute the auxiliary qubits, i.e., return them to the $\\ket{00}$ state to unentangle them from the main register.</p>\n<p>Same as we did in the previous task, we will use <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.canon/applycontrolledonbitstring\"><code>ApplyControlledOnBitString</code></a> with the corresponding bit string and the X operation as arguments, the quantum register as the control, and the auxiliary qubits as the target.</p>\n<p>We will uncompute each of them separately, so one of the auxiliary qubits will be uncomputed with the <code>bits[1]</code> and <code>bits[3]</code> bit strings as controls, and the other - with the <code>bits[2]</code> and <code>bits[3]</code>.</p>\n"
2803
+ "content": "<p>You're going to use the same trick of auxiliary qubits that you used in the previous task.</p>\n<p>Since the desired superposition has 4 basis states with equal amplitudes, you're going to need two qubits to define a unique basis to control preparation of each of the basis states in the superposition.</p>\n<p>You start by allocating two extra qubits and preparing an equal superposition of all 2-qubit states on them by applying an $H$ gate to each of them:</p>\n<p>$$\\frac12 (\\ket{00} + \\ket{01} + \\ket{10} + \\ket{11})_a \\otimes \\ket{0 \\dots 0}_r$$</p>\n<p>Then, for each of the four given bit strings, you walk through it and prepare the matching basis state on the main register of qubits, using controlled $X$ gates with the corresponding basis state of the auxiliary qubits as control.</p>\n<p>For example, when preparing the bit string <code>bits[0]</code>, you apply $X$ gates controlled on the basis state $\\ket{00}$; when preparing the bit string <code>bits[1]</code>, you apply $X$ gates controlled on $\\ket{10}$, and so on.</p>\n<blockquote>\n<p>You can choose an arbitrary matching of the 2-qubit basis states used as controls and the bit strings prepared on the main register.\nSince all amplitudes are the same, the result doesn't depend on which state controlled which bit string preparation.\nIt can be convenient to use indices of the bit strings, converted to little-endian, to control preparation of the bit strings.\nQ# library function <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.canon/applycontrolledonint\"><code>ApplyControlledOnInt</code></a> does exactly that.</p>\n</blockquote>\n<p>After this, the system will be in the state</p>\n<p>$$\\frac12 (\\ket{00}_a \\ket{bits_0}_r + \\ket{10}_a \\ket{bits_1}_r + \\ket{01}_a \\ket{bits_2}_r + \\ket{11}_a \\ket{bits_3}_r)$$</p>\n<p>As the last step, you must uncompute the auxiliary qubits, that is, return them to the $\\ket{00}$ state to unentangle them from the main register.</p>\n<p>Same as you did in the previous task, you'll use <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.canon/applycontrolledonbitstring\"><code>ApplyControlledOnBitString</code></a> with the corresponding bit string and the <code>X</code> operation as arguments, the quantum register as the control, and the auxiliary qubits as the target.</p>\n<p>You'll uncompute each of them separately, so one of the auxiliary qubits will be uncomputed with the <code>bits[1]</code> and <code>bits[3]</code> bit strings as controls, and the other - with the <code>bits[2]</code> and <code>bits[3]</code>.</p>\n"
2804
2804
  },
2805
2805
  {
2806
2806
  "type": "solution",
@@ -2809,7 +2809,7 @@ export default {
2809
2809
  },
2810
2810
  {
2811
2811
  "type": "text-content",
2812
- "content": "<p>Alternatively, we can leverage the recursion abilities of Q# to create a superposition of the four bit strings. This solution also extends to an arbitrary number of bit strings with no code changes.</p>\n<p>For this process we will look at the first bits of each string and adjust the probability of measuring a $\\ket{0}$ or $\\ket{1}$ accordingly on the first qubit of our answer. We will then recursively call (as needed) the process again to adjust the probabilities of measurement on the second bit depending on the first bit. This process recurses until no more input bits are provided.</p>\n<p>Consider, for example, the following four bit strings on which to create a superposition: $\\ket{001}, \\ket{101}, \\ket{111}, \\ket{110}$.</p>\n<p>We can rewrite the superposition state we need to prepare as</p>\n<p>$$\\frac12 \\big(\\ket{001} + \\ket{101} + \\ket{111} + \\ket{110} \\big) = \\frac12 \\ket{0} \\otimes \\ket{01} + \\frac{\\sqrt3}{2} \\ket{1} \\otimes \\frac{1}{\\sqrt3} \\big(\\ket{10} + \\ket{11} + \\ket{10} \\big)$$</p>\n<p>As the first step of the solution, we need to prepare a state $\\frac12 \\ket{0} + \\frac{\\sqrt3}{2} \\ket{1}$ on the first qubit (to measure $\\ket{0}$ with $\\frac14$ probability and to measure $\\ket{1}$ with $\\frac34$ probability). To do this, we will apply an <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.intrinsic/ry\"><code>Ry</code></a> rotation to the first qubit.</p>\n<p>After this, we'll need to prepare the rest of the qubits in appropriate states depending on the state of the first qubit - state $\\ket{01}$ if the first qubit is in state $\\ket{0}$ and state $\\frac{1}{\\sqrt3} \\big(\\ket{10} + \\ket{11} + \\ket{10} \\big)$ if the first qubit is in state $\\ket{1}$. We can do this recursively using the same logic. Let's finish walking through this example in detail.</p>\n<p>The second qubit of the recursion follows similarly but depends on the first qubit. If the first qubit measures $\\ket{0}$, then we want the second qubit to measure $\\ket{0}$ with 100% probability, but if it measures $\\ket{1}$, we want it to measure $\\ket{0}$ with $\\frac13$ probability and $\\ket{1}$ with $\\frac23$ probability. For this, we can do a controlled <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.intrinsic/ry\"><code>Ry</code></a> rotation on the second qubit with the first qubit as control.</p>\n<p>The third qubit in this example will have three cases because it depends on the first two qubits; this follows naturally from the recursion.</p>\n<ol>\n<li>If the first two qubits measure $\\ket{00}$, then we need the third qubit to measure $\\ket{0}$ with 100% probability.</li>\n<li>If the first two qubits measure $\\ket{10}$, then we need the third qubit to measure $\\ket{1}$ with 100% probability.</li>\n<li>If the first two qubits measure $\\ket{11}$, then we need the third qubit to measure $\\ket{0}$ with $\\frac12$ probability and $\\ket{1}$ with $\\frac12$ probability. Just as with the second qubit, a controlled <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.intrinsic/ry\"><code>Ry</code></a> rotation on the third qubit will accomplish this goal.</li>\n</ol>\n<blockquote>\n<p>We will use <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.canon/applycontrolledonbitstring\"><code>ApplyControlledOnBitString</code></a> operation to perform rotations depending on the state of several previous qubits.</p>\n</blockquote>\n"
2812
+ "content": "<p>Alternatively, you can leverage the recursion abilities of Q# to create a superposition of the four bit strings. This solution also extends to an arbitrary number of bit strings with no code changes.</p>\n<p>For this process, you'll look at the first bits of each string and adjust the probability of measuring a $\\ket{0}$ or $\\ket{1}$ accordingly on the first qubit of your answer. You'll then recursively call (as needed) the process again to adjust the probabilities of measurement on the second bit depending on the first bit. This process recurses until no more input bits are provided.</p>\n<p>Consider, for example, the following four bit strings on which to create a superposition: $\\ket{001}, \\ket{101}, \\ket{111}, \\ket{110}$.</p>\n<p>You can rewrite the superposition state you need to prepare as</p>\n<p>$$\\frac12 \\big(\\ket{001} + \\ket{101} + \\ket{111} + \\ket{110} \\big) = \\frac12 \\ket{0} \\otimes \\ket{01} + \\frac{\\sqrt3}{2} \\ket{1} \\otimes \\frac{1}{\\sqrt3} \\big(\\ket{10} + \\ket{11} + \\ket{10} \\big)$$</p>\n<p>As the first step of the solution, you need to prepare a state $\\frac12 \\ket{0} + \\frac{\\sqrt3}{2} \\ket{1}$ on the first qubit (to measure $\\ket{0}$ with $\\frac14$ probability and to measure $\\ket{1}$ with $\\frac34$ probability). To do this, you will apply an <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.intrinsic/ry\"><code>Ry</code></a> rotation to the first qubit.</p>\n<p>After this, you'll need to prepare the rest of the qubits in appropriate states depending on the state of the first qubit - state $\\ket{01}$ if the first qubit is in state $\\ket{0}$ and state $\\frac{1}{\\sqrt3} \\big(\\ket{10} + \\ket{11} + \\ket{10} \\big)$ if the first qubit is in state $\\ket{1}$. You can do this recursively using the same logic. Let's finish walking through this example in detail.</p>\n<p>The second qubit of the recursion follows similarly but depends on the first qubit. If the first qubit measures $\\ket{0}$, then you want the second qubit to measure $\\ket{0}$ with $100\\%$ probability, but if it measures $\\ket{1}$, you want it to measure $\\ket{0}$ with $\\frac13$ probability and $\\ket{1}$ with $\\frac23$ probability. For this, you can do a controlled <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.intrinsic/ry\"><code>Ry</code></a> rotation on the second qubit with the first qubit as control.</p>\n<p>The third qubit in this example will have three cases because it depends on the first two qubits; this follows naturally from the recursion.</p>\n<ol>\n<li>If the first two qubits measure $\\ket{00}$, then you need the third qubit to measure $\\ket{0}$ with $100\\%$ probability.</li>\n<li>If the first two qubits measure $\\ket{10}$, then you need the third qubit to measure $\\ket{1}$ with $100\\%$ probability.</li>\n<li>If the first two qubits measure $\\ket{11}$, then you need the third qubit to measure $\\ket{0}$ with $\\frac12$ probability and $\\ket{1}$ with $\\frac12$ probability. Just as with the second qubit, a controlled <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.intrinsic/ry\"><code>Ry</code></a> rotation on the third qubit will accomplish this goal.</li>\n</ol>\n<blockquote>\n<p>You'll use <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.canon/applycontrolledonbitstring\"><code>ApplyControlledOnBitString</code></a> operation to perform rotations depending on the state of several previous qubits.</p>\n</blockquote>\n"
2813
2813
  },
2814
2814
  {
2815
2815
  "type": "solution",
@@ -2838,7 +2838,7 @@ export default {
2838
2838
  "items": [
2839
2839
  {
2840
2840
  "type": "text-content",
2841
- "content": "<p>There are multiple ways to approach this problem. In our first solution, we are going to use a recursive approach that was hinted at in the task.</p>\n<p>Let's denote the required state on $N$ qubits as $\\ket{P_{N,0}}$ for <code>parity = 0</code> and $\\ket{P_{N,1}}$ for <code>parity = 1</code>. We can group the $2^{N-1}$ basis states included in the state $\\ket{P_{N,0}}$ by their first bit ($0$ or $1$) and write the state as follows:</p>\n<p>$$\\ket{P_{N,0}} = \\frac{1}{\\sqrt{2^{N-1}}} \\sum_{k : k \\text{ has parity }0} \\ket{k}_N = $$\n$$= \\frac{1}{\\sqrt{2^{N-1}}} \\big( \\ket{0} \\otimes \\sum_{k&#039; : k&#039; \\text{ has parity }0} \\ket{k&#039;}_{N-1} + \\ket{1} \\otimes \\sum_{k&#039;&#039; : k&#039;&#039; \\text{ has parity }1} \\ket{k&#039;&#039;}_{N-1} \\big) = $$\n$$= \\frac{1}{\\sqrt{2}} \\big( \\ket{0} \\otimes \\ket{P_{N-1,0}} + \\ket{1} \\otimes \\ket{P_{N-1,1}} \\big)$$\nWe can consider the expression for $\\ket{P_{N,1}}$ in a similar manner, and get a unified expression for both states:\n$$\\ket{P_{N,p}} = \\frac{1}{\\sqrt{2}} \\big( \\ket{0} \\otimes \\ket{P_{N-1,p}} + \\ket{1} \\otimes \\ket{P_{N-1,1-p}} \\big)$$</p>\n<p>Now we can use this expression to prepare the state using a recursive approach we've seen before:</p>\n<ol>\n<li>Apply $H$ gate to the first qubit to prepare state $\\frac{1}{\\sqrt{2}} ( \\ket{0} + \\ket{1} ) \\otimes \\ket{0}_{N-1}$.</li>\n<li>Apply the controlled variant of procedure of preparing $\\ket{P_{N-1,p}}$ on the last $N-1$ qubits, with the first qubit in the $\\ket{0}$ state as the control.</li>\n<li>Apply the controlled variant of procedure of preparing $\\ket{P_{N-1,1-p}}$ on the last $N-1$ qubits, with the first qubit in the $\\ket{1}$ state as the control.</li>\n</ol>\n<blockquote>\n<p>Q# library function <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.canon/applycontrolledonint\"><code>ApplyControlledOnInt</code></a> allows to do that easily.</p>\n</blockquote>\n<ol start=\"4\">\n<li>The base of recursion is preparing the states for $N = 1$:</li>\n</ol>\n<ul>\n<li>For <code>parity = 0</code>, there is one single-qubit state with this parity: $\\ket{0}$ (no action required to prepare).</li>\n<li>For <code>parity = 1</code>, there is one single-qubit state with this parity: $\\ket{1}$ (apply $X$ gate to prepare).</li>\n</ul>\n"
2841
+ "content": "<p>There are multiple ways to approach this problem. The first solution uses the recursive approach that was hinted at in the task.</p>\n<p>Let's denote the required state on $N$ qubits as $\\ket{P_{N,0}}$ for <code>parity = 0</code> and $\\ket{P_{N,1}}$ for <code>parity = 1</code>. You can group the $2^{N-1}$ basis states included in the state $\\ket{P_{N,0}}$ by their first bit ($0$ or $1$) and write the state as follows:</p>\n<p>$$\\ket{P_{N,0}} = \\frac{1}{\\sqrt{2^{N-1}}} \\sum_{k : k \\text{ has parity }0} \\ket{k}_N = $$\n$$= \\frac{1}{\\sqrt{2^{N-1}}} \\big( \\ket{0} \\otimes \\sum_{k&#039; : k&#039; \\text{ has parity }0} \\ket{k&#039;}_{N-1} + \\ket{1} \\otimes \\sum_{k&#039;&#039; : k&#039;&#039; \\text{ has parity }1} \\ket{k&#039;&#039;}_{N-1} \\big) = $$\n$$= \\frac{1}{\\sqrt{2}} \\big( \\ket{0} \\otimes \\ket{P_{N-1,0}} + \\ket{1} \\otimes \\ket{P_{N-1,1}} \\big)$$\nYou can consider the expression for $\\ket{P_{N,1}}$ in a similar manner, and get a unified expression for both states:\n$$\\ket{P_{N,p}} = \\frac{1}{\\sqrt{2}} \\big( \\ket{0} \\otimes \\ket{P_{N-1,p}} + \\ket{1} \\otimes \\ket{P_{N-1,1-p}} \\big)$$</p>\n<p>Now you can use this expression to prepare the state using a recursive approach you've seen before:</p>\n<ol>\n<li>Apply $H$ gate to the first qubit to prepare state $\\frac{1}{\\sqrt{2}} ( \\ket{0} + \\ket{1} ) \\otimes \\ket{0}_{N-1}$.</li>\n<li>Apply the controlled variant of procedure of preparing $\\ket{P_{N-1,p}}$ on the last $N-1$ qubits, with the first qubit in the $\\ket{0}$ state as the control.</li>\n<li>Apply the controlled variant of procedure of preparing $\\ket{P_{N-1,1-p}}$ on the last $N-1$ qubits, with the first qubit in the $\\ket{1}$ state as the control.</li>\n</ol>\n<blockquote>\n<p>Q# library function <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.canon/applycontrolledonint\"><code>ApplyControlledOnInt</code></a> allows you to do that easily.</p>\n</blockquote>\n<ol start=\"4\">\n<li>The base of recursion is preparing the states for $N = 1$:</li>\n</ol>\n<ul>\n<li>For <code>parity = 0</code>, there is one single-qubit state with this parity: $\\ket{0}$ (no action required to prepare).</li>\n<li>For <code>parity = 1</code>, there is one single-qubit state with this parity: $\\ket{1}$ (apply $X$ gate to prepare).</li>\n</ul>\n"
2842
2842
  },
2843
2843
  {
2844
2844
  "type": "solution",
@@ -2847,7 +2847,7 @@ export default {
2847
2847
  },
2848
2848
  {
2849
2849
  "type": "text-content",
2850
- "content": "<p>In the second solution, we'll use post-selection. We start by preparing an equal superposition of all basis states and allocating an extra qubit.</p>\n<p>This time we use the extra qubit to calculate the parity of the input state: applying a series of CNOT gates, each one with one of the input qubits as control and the extra qubit as a target will compute the parity of the state.</p>\n<p>Now we measure the extra qubit: if the measurement result matches our parity, we're done — the input qubits collapsed to an equal superposition of all states that have this parity. If the measurement result is the opposite, we can retry the whole process.</p>\n<p>We can avoid retrying the state preparation if our measurement result doesn't match the required parity: notice that applying an X gate to any one of the qubits changes the parity of each basis state to the opposite one, and thus converts the state we got to the state we need.</p>\n"
2850
+ "content": "<p>The second solution uses post-selection. You start by preparing an equal superposition of all basis states and allocating an extra qubit.</p>\n<p>This time you use the extra qubit to calculate the parity of the input state: applying a series of $CNOT$ gates, each one with one of the input qubits as control and the extra qubit as a target will compute the parity of the state.</p>\n<p>Now, you measure the extra qubit: if the measurement result matches the parity, you're done — the input qubits collapsed to an equal superposition of all states that have this parity. If the measurement result is the opposite, you can retry the whole process.</p>\n<p>You can avoid retrying the state preparation if your measurement result doesn't match the required parity: notice that applying an $X$ gate to any one of the qubits changes the parity of each basis state to the opposite one, and thus converts the state you got to the state you need.</p>\n"
2851
2851
  },
2852
2852
  {
2853
2853
  "type": "solution",
@@ -2856,7 +2856,7 @@ export default {
2856
2856
  },
2857
2857
  {
2858
2858
  "type": "text-content",
2859
- "content": "<p>Yet another way of getting the desired superposition could be preparing the mix of all possible basis states for all qubits iteratively, keeping the parity on each step.</p>\n<p>We start by preparing a superposition of all basis states with parity $0$. To achieve that, we loop through all the qubits except the first one and prepare all of them in equal superposition. In order to maintain the parity of the basis states involved, we use the first qubit and conditionally flip its state using a CNOT gate with each next qubit as the control, so that every time there is a $\\ket{1}$ state in the chain, we get back to an even number of $\\ket{1}$ states in that basis state.</p>\n<p>For example, after the first loop iteration we get the state $\\frac12(\\ket{00} + \\ket{11})$. After the second iteration we get the state</p>\n<p>$$CNOT_{2,0} \\frac1{\\sqrt2}(\\ket{00} + \\ket{11}) \\otimes \\frac1{\\sqrt2}(\\ket{0} + \\ket{1}) = $$\n$$= CNOT_{2,0} \\frac12(\\ket{000} + \\ket{\\textbf{0}0\\textbf{1}} + \\ket{110} + \\ket{\\textbf{1}1\\textbf{1}}) = $$\n$$= \\frac12(\\ket{000} + \\ket{101} + \\ket{110} + \\ket{011})$$</p>\n<p>After the loop we will have a superposition of all possible basis states with even number of $\\ket{1}$s.\nThen, if `parity` is equal to 1 and we want an odd number of $\\ket{1}$s, we just flip the state of the first qubit again (or any qubit).</p>\n<p>For example, if the input has 3 qubits, after the for loop we will have a superposition of 4 basis states:</p>\n<p>$$\\frac12(\\ket{000} + \\ket{101} + \\ket{110} + \\ket{011})$$</p>\n<p>If <code>parity = 0</code>, we are done, having even numbers of $\\ket{1}$s. If <code>parity = 1</code>, we flip the state of the first qubit, getting the desired result:</p>\n<p>$$\\frac12(\\ket{100} + \\ket{001} + \\ket{010} + \\ket{111})$$</p>\n"
2859
+ "content": "<p>Yet another way of getting the desired superposition could be preparing the mix of all possible basis states for all qubits iteratively, keeping the parity on each step.</p>\n<p>You start by preparing a superposition of all basis states with parity $0$. To achieve that, you loop through all the qubits except the first one and prepare all of them in equal superposition. In order to maintain the parity of the basis states involved, use the first qubit and conditionally flip its state using a $CNOT$ gate with each next qubit as the control, so that every time there is a $\\ket{1}$ state in the chain, you get back to an even number of $\\ket{1}$ states in that basis state.</p>\n<p>For example, after the first loop iteration, you get the state $\\frac12(\\ket{00} + \\ket{11})$. After the second iteration, you get the state</p>\n<p>$$CNOT_{2,0} \\frac1{\\sqrt2}(\\ket{00} + \\ket{11}) \\otimes \\frac1{\\sqrt2}(\\ket{0} + \\ket{1}) = $$\n$$= CNOT_{2,0} \\frac12(\\ket{000} + \\ket{\\textbf{0}0\\textbf{1}} + \\ket{110} + \\ket{\\textbf{1}1\\textbf{1}}) = $$\n$$= \\frac12(\\ket{000} + \\ket{101} + \\ket{110} + \\ket{011})$$</p>\n<p>After the loop you'll have a superposition of all possible basis states with even number of $\\ket{1}$s.\nThen, if `parity` is equal to 1 and we want an odd number of $\\ket{1}$s, you just flip the state of the first qubit again (or any qubit).</p>\n<p>For example, if the input has 3 qubits, after the for loop you'll have a superposition of 4 basis states:</p>\n<p>$$\\frac12(\\ket{000} + \\ket{101} + \\ket{110} + \\ket{011})$$</p>\n<p>If <code>parity = 0</code>, you're done, having even numbers of $\\ket{1}$s. If <code>parity = 1</code>, you flip the state of the first qubit, getting the desired result:</p>\n<p>$$\\frac12(\\ket{100} + \\ket{001} + \\ket{010} + \\ket{111})$$</p>\n"
2860
2860
  },
2861
2861
  {
2862
2862
  "type": "solution",
@@ -2896,7 +2896,7 @@ export default {
2896
2896
  "items": [
2897
2897
  {
2898
2898
  "type": "text-content",
2899
- "content": "<p>We want to convert the $\\ket{0}$ state to a parameterized superposition of $\\ket{0}$ and $\\ket{1}$, which suggests\nthat we are looking for some kind of a rotation operation. There are three main gates that implement rotations around various axes of the Bloch Sphere:</p>\n<ul>\n<li>$R_x(\\theta) = \\begin{bmatrix} \\cos\\frac{\\theta}{2} &amp; -i\\sin\\frac{\\theta}{2} \\\\ -i\\sin\\frac{\\theta}{2} &amp; \\cos\\frac{\\theta}{2} \\end{bmatrix}$</li>\n<li>$R_y(\\theta) = \\begin{bmatrix} \\cos\\frac{\\theta}{2} &amp; -\\sin\\frac{\\theta}{2} \\\\ \\sin\\frac{\\theta}{2} &amp; \\cos\\frac{\\theta}{2} \\end{bmatrix}$</li>\n<li>$R_z(\\theta) = \\begin{bmatrix} e^{-i\\theta/2} &amp; 0 \\\\ 0 &amp; e^{i\\theta/2} \\end{bmatrix}$</li>\n</ul>\n<p>If we were to apply the $R_x$ gate to a qubit in the $\\ket{0}$ state, we would introduce complex coefficients to the amplitudes, which is clearly not what we're looking for. Similarly, the $R_z$ gate introduces only a global phase when applied to $\\ket{0}$ state, so we can rule it out as well. This leaves only the $R_y$ as a starting point for the solution.</p>\n<p>Applying the $R_y$ gate to the $\\ket{0}$ state, we get:\n$$R_y(\\theta) \\ket{0} = \n\\begin{bmatrix} \\cos\\frac{\\theta}{2} &amp; -\\sin\\frac{\\theta}{2} \\\\ \\sin\\frac{\\theta}{2} &amp; \\cos\\frac{\\theta}{2} \\end{bmatrix} \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} = \n\\begin{bmatrix} \\cos\\frac{\\theta}{2} \\\\ \\sin\\frac{\\theta}{2} \\end{bmatrix} = \\cos\\frac{\\theta}{2}\\ket{0} + \\sin\\frac{\\theta}{2}\\ket{1}$$</p>\n<p>Therefore, applying the $R_y(2\\alpha)$ gate to $\\ket{0}$ is the solution to our problem.</p>\n"
2899
+ "content": "<p>You want to convert the $\\ket{0}$ state to a parameterized superposition of $\\ket{0}$ and $\\ket{1}$, which suggests\nthat you're looking for some kind of a rotation operation. There are three main gates that implement rotations around various axes of the Bloch Sphere:</p>\n<ul>\n<li>$R_x(\\theta) = \\begin{bmatrix} \\cos\\frac{\\theta}{2} &amp; -i\\sin\\frac{\\theta}{2} \\\\ -i\\sin\\frac{\\theta}{2} &amp; \\cos\\frac{\\theta}{2} \\end{bmatrix}$</li>\n<li>$R_y(\\theta) = \\begin{bmatrix} \\cos\\frac{\\theta}{2} &amp; -\\sin\\frac{\\theta}{2} \\\\ \\sin\\frac{\\theta}{2} &amp; \\cos\\frac{\\theta}{2} \\end{bmatrix}$</li>\n<li>$R_z(\\theta) = \\begin{bmatrix} e^{-i\\theta/2} &amp; 0 \\\\ 0 &amp; e^{i\\theta/2} \\end{bmatrix}$</li>\n</ul>\n<p>If you were to apply the $R_x$ gate to a qubit in the $\\ket{0}$ state, you'd introduce complex coefficients to the amplitudes, which is clearly not what you're looking for. Similarly, the $R_z$ gate introduces only a global phase when applied to $\\ket{0}$ state, so you can rule it out as well. This leaves only the $R_y$ as a starting point for the solution.</p>\n<p>Applying the $R_y$ gate to the $\\ket{0}$ state, you get:\n$$R_y(\\theta) \\ket{0} = \n\\begin{bmatrix} \\cos\\frac{\\theta}{2} &amp; -\\sin\\frac{\\theta}{2} \\\\ \\sin\\frac{\\theta}{2} &amp; \\cos\\frac{\\theta}{2} \\end{bmatrix} \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} = \n\\begin{bmatrix} \\cos\\frac{\\theta}{2} \\\\ \\sin\\frac{\\theta}{2} \\end{bmatrix} = \\cos\\frac{\\theta}{2}\\ket{0} + \\sin\\frac{\\theta}{2}\\ket{1}$$</p>\n<p>Therefore, applying the $R_y(2\\alpha)$ gate to $\\ket{0}$ is the solution to this problem.</p>\n"
2900
2900
  },
2901
2901
  {
2902
2902
  "type": "solution",
@@ -2925,7 +2925,7 @@ export default {
2925
2925
  "items": [
2926
2926
  {
2927
2927
  "type": "text-content",
2928
- "content": "<p>We will start by putting the first qubit in the state $\\alpha\\ket{0} + \\beta\\ket{1}$, where $\\alpha$ and $\\beta$ are the square roots of relative weights of all basis states which start with 0 and with 1, respectively.</p>\n<p>In this case, our state can be represented as $\\frac{1}{\\sqrt{2}} \\big( \\ket{0} \\otimes \\ket{0} + \\ket{1} \\otimes \\frac{1}{\\sqrt2}(\\ket{0}+\\ket{1}) \\big)$.</p>\n<p>We see that the relative weights of $\\ket{0}$ and $\\ket{1}$ states of the first qubit are both $\\frac12$ (the squares of their amplitudes in the decomposition above). This means that we can do the first step by applying the $H$ gate to the first qubit, which gives the $\\frac{1}{\\sqrt{2}}\\ket{00} + \\frac{1}{\\sqrt{2}}\\ket{10}$ state. In matrix form this will look as follows:</p>\n<p>$$ H \\otimes I = \\frac{1}{\\sqrt{2}} \\begin{bmatrix}1 &amp; 0 &amp; 1 &amp; 0 \\\\ 0 &amp; 1 &amp; 0 &amp; 1 \\\\ 1 &amp; 0 &amp; -1 &amp; 0 \\\\ 0 &amp; 1 &amp; 0 &amp; -1 \\end{bmatrix} \\cdot\n\\begin{bmatrix} 1 \\\\ 0 \\\\0 \\\\ 0 \\end{bmatrix} = \\frac{1}{\\sqrt{2}} \\begin{bmatrix} 1 \\\\ 0 \\\\ 1 \\\\0 \\end{bmatrix} = \\frac{1}{\\sqrt{2}} \\big( \\ket{0} + \\ket{1} \\big) \\otimes \\ket{0}$$</p>\n<p>Now the first term of the state $\\frac{1}{\\sqrt2}\\ket{00}$ matches that of the goal state, and we need to convert the second term $\\frac{1}{\\sqrt2}\\ket{10}$ to $\\ket{1} \\otimes \\frac{1}{\\sqrt2}(\\ket{0}+\\ket{1})$.</p>\n<p>To do this, we use the controlled $H$ gate. The matrix representation of the controlled $H$ gate is similar to the $CNOT$ gate, however the bottom right block of the matrix is not an $X$ gate but the $H$ gate:</p>\n<p>$$\\text{Controlled } H = \\begin{bmatrix}1 &amp; 0 &amp; 0 &amp; 0 \\\\ 0 &amp; 1 &amp; 0 &amp; 0 \\\\ 0 &amp; 0 &amp; \\frac{1}{\\sqrt{2}} &amp; \\frac{1}{\\sqrt{2}} \\\\ 0 &amp; 0 &amp; \\frac{1}{\\sqrt{2}} &amp; -\\frac{1}{\\sqrt{2}} \\end{bmatrix}$$</p>\n<p>When this is applied to our current state, we get our goal state:</p>\n<p>$$\\text{Controlled } H \\cdot \\frac{1}{\\sqrt{2}} \\begin{bmatrix} 1 \\\\ 0 \\\\ 1 \\\\0 \\end{bmatrix} = \\begin{bmatrix}\\frac{1}{\\sqrt{2}} \\\\ 0 \\\\ \\frac{1}{2} \\\\ \\frac{1}{2} \\end{bmatrix} = \\frac{1}{\\sqrt{2}}\\ket{00}+\\frac{1}{2}\\ket{10}+\\frac{1}{2}\\ket{11} $$</p>\n"
2928
+ "content": "<p>You'll start by putting the first qubit in the state $\\alpha\\ket{0} + \\beta\\ket{1}$, where $\\alpha$ and $\\beta$ are the square roots of relative weights of all basis states which start with 0 and with 1, respectively.</p>\n<p>In this case, the state can be represented as $\\frac{1}{\\sqrt{2}} \\big( \\ket{0} \\otimes \\ket{0} + \\ket{1} \\otimes \\frac{1}{\\sqrt2}(\\ket{0}+\\ket{1}) \\big)$.</p>\n<p>You see that the relative weights of $\\ket{0}$ and $\\ket{1}$ states of the first qubit are both $\\frac12$ (the squares of their amplitudes in the decomposition above). This means that you can do the first step by applying the $H$ gate to the first qubit, which gives the $\\frac{1}{\\sqrt{2}}\\ket{00} + \\frac{1}{\\sqrt{2}}\\ket{10}$ state. In matrix form this will look as follows:</p>\n<p>$$ H \\otimes I = \\frac{1}{\\sqrt{2}} \\begin{bmatrix}1 &amp; 0 &amp; 1 &amp; 0 \\\\ 0 &amp; 1 &amp; 0 &amp; 1 \\\\ 1 &amp; 0 &amp; -1 &amp; 0 \\\\ 0 &amp; 1 &amp; 0 &amp; -1 \\end{bmatrix} \\cdot\n\\begin{bmatrix} 1 \\\\ 0 \\\\0 \\\\ 0 \\end{bmatrix} = \\frac{1}{\\sqrt{2}} \\begin{bmatrix} 1 \\\\ 0 \\\\ 1 \\\\0 \\end{bmatrix} = \\frac{1}{\\sqrt{2}} \\big( \\ket{0} + \\ket{1} \\big) \\otimes \\ket{0}$$</p>\n<p>Now the first term of the state $\\frac{1}{\\sqrt2}\\ket{00}$ matches that of the goal state, and you need to convert the second term $\\frac{1}{\\sqrt2}\\ket{10}$ to $\\ket{1} \\otimes \\frac{1}{\\sqrt2}(\\ket{0}+\\ket{1})$.</p>\n<p>To do this, you use the controlled $H$ gate. The matrix representation of the controlled $H$ gate is similar to the $CNOT$ gate, however the bottom right block of the matrix is not an $X$ gate but the $H$ gate:</p>\n<p>$$\\text{Controlled } H = \\begin{bmatrix}1 &amp; 0 &amp; 0 &amp; 0 \\\\ 0 &amp; 1 &amp; 0 &amp; 0 \\\\ 0 &amp; 0 &amp; \\frac{1}{\\sqrt{2}} &amp; \\frac{1}{\\sqrt{2}} \\\\ 0 &amp; 0 &amp; \\frac{1}{\\sqrt{2}} &amp; -\\frac{1}{\\sqrt{2}} \\end{bmatrix}$$</p>\n<p>When this is applied to the current state, you get your goal state:</p>\n<p>$$\\text{Controlled } H \\cdot \\frac{1}{\\sqrt{2}} \\begin{bmatrix} 1 \\\\ 0 \\\\ 1 \\\\0 \\end{bmatrix} = \\begin{bmatrix}\\frac{1}{\\sqrt{2}} \\\\ 0 \\\\ \\frac{1}{2} \\\\ \\frac{1}{2} \\end{bmatrix} = \\frac{1}{\\sqrt{2}}\\ket{00}+\\frac{1}{2}\\ket{10}+\\frac{1}{2}\\ket{11} $$</p>\n"
2929
2929
  },
2930
2930
  {
2931
2931
  "type": "solution",
@@ -2954,7 +2954,7 @@ export default {
2954
2954
  "items": [
2955
2955
  {
2956
2956
  "type": "text-content",
2957
- "content": "<p>There are multiple ways to prepare this state; the first solution described will focus on preparing this state without using arbitrary rotation gates.</p>\n<p>Initially we will prepare an equal superposition of all basis states on the two starting qubits by applying the $H$ gate to each of them:\n$$\\frac{1}{2} \\big(\\ket{00} + \\ket{01} + \\ket{10} + \\ket{11}\\big)$$</p>\n<p>This state is a superposition of the state we want to prepare, and the $\\ket{11}$ state that we would like to discard.\nWe can do exactly that by performing the right measurement on the system. To do this, we allocate an extra qubit (sometimes referred to as an <em>ancilla</em> qubit). With this extra qubit the new state becomes:\n$$\\frac{1}{2} \\big(\\ket{00\\textbf{0}} + \\ket{01\\textbf{0}} + \\ket{10\\textbf{0}} + \\ket{11\\textbf{0}}\\big)$$</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 $\\ket{0}$ for the basis states that we want to keep, and switch it to $\\ket{1}$ 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 $\\ket{1}$ if both control qubits are in the $\\ket{11}$ state, which marks exactly the state that we want to discard:</p>\n<p>$$CCNOT\\frac{1}{2} \\big(\\ket{00\\textbf{0}} + \\ket{01\\textbf{0}} + \\ket{10\\textbf{0}} + \\ket{11\\textbf{0}}\\big) = \n\\frac{1}{2}\\big(\\ket{00} + \\ket{01} + \\ket{10} \\big) \\otimes \\ket{\\textbf{0}} + \\frac{1}{2}\\ket{11} \\otimes \\ket{\\textbf{1}} $$</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 $\\ket{0}$, the system collapses to a state that is a linear combination of basis states which had the extra qubit in state $\\ket{0}$, i.e., the two qubits end up in the target state $\\frac{1}{\\sqrt3}\\big(\\ket{00} + \\ket{01} + \\ket{10}\\big)$.</li>\n<li>If the result is $\\ket{1}$, the system collapses to a state $\\ket{11}$, 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 $\\ket{00}$ state and try again.</li>\n</ul>\n<blockquote>\n<p>Q# has a built-in <a href=\"https://learn.microsoft.com/azure/quantum/user-guide/language/expressions/conditionalloops#repeat-expression\">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 $H$ and $CCNOT$ 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 $\\ket{00}$ state.</li>\n</ul>\n</blockquote>\n<p>This technique is sometimes called post-selection.</p>\n"
2957
+ "content": "<p>There are multiple ways to prepare this state; the first solution described will focus on preparing this state without using arbitrary rotation gates.</p>\n<p>Initially, you'll prepare an equal superposition of all basis states on the two starting qubits by applying the $H$ gate to each of them:\n$$\\frac{1}{2} \\big(\\ket{00} + \\ket{01} + \\ket{10} + \\ket{11}\\big)$$</p>\n<p>This state is a superposition of the state you want to prepare, and the $\\ket{11}$ state that you'd like to discard.\nYou can do exactly that by performing the right measurement on the system. To do this, you allocate an extra qubit (sometimes referred to as an <em>ancilla</em> qubit). With this extra qubit, the new state becomes:\n$$\\frac{1}{2} \\big(\\ket{00\\textbf{0}} + \\ket{01\\textbf{0}} + \\ket{10\\textbf{0}} + \\ket{11\\textbf{0}}\\big)$$</p>\n<p>Now, you want to separate the first three basis states from the last one and to store this separation in the extra qubit.\nFor example, you can keep the state of the extra qubit $\\ket{0}$ for the basis states that you want to keep, and switch it to $\\ket{1}$ for the basis states that you'd 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 $\\ket{1}$ if both control qubits are in the $\\ket{11}$ state, which marks exactly the state that you want to discard:</p>\n<p>$$CCNOT\\frac{1}{2} \\big(\\ket{00\\textbf{0}} + \\ket{01\\textbf{0}} + \\ket{10\\textbf{0}} + \\ket{11\\textbf{0}}\\big) = \n\\frac{1}{2}\\big(\\ket{00} + \\ket{01} + \\ket{10} \\big) \\otimes \\ket{\\textbf{0}} + \\frac{1}{2}\\ket{11} \\otimes \\ket{\\textbf{1}} $$</p>\n<p>Finally, you 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 $\\ket{0}$, the system collapses to a state that is a linear combination of basis states which had the extra qubit in state $\\ket{0}$, that is, the two qubits end up in the target state $\\frac{1}{\\sqrt3}\\big(\\ket{00} + \\ket{01} + \\ket{10}\\big)$.</li>\n<li>If the result is $\\ket{1}$, the system collapses to a state $\\ket{11}$, so your goal isn't achieved. The good thing is, this only happens in $25\\%$ of the cases, and you can just reset your qubits to the $\\ket{00}$ state and try again.</li>\n</ul>\n<blockquote>\n<p>Q# has a built-in <a href=\"https://learn.microsoft.com/azure/quantum/user-guide/language/expressions/conditionalloops#repeat-expression\">repeat-until-success (RUS) loop</a>, which comes in handy in this case.</p>\n<ul>\n<li>You'll describe the main operations (applying $H$ and $CCNOT$ 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 your success.</li>\n<li>Finally, the <code>fixup</code> section allows you to clean up the results of the loop body execution before trying again if the success criteria isn't met. In this case, you reset the first two qubits back to the $\\ket{00}$ state.</li>\n</ul>\n</blockquote>\n<p>This technique is sometimes called <em>post-selection</em>.</p>\n"
2958
2958
  },
2959
2959
  {
2960
2960
  "type": "solution",
@@ -2963,7 +2963,7 @@ export default {
2963
2963
  },
2964
2964
  {
2965
2965
  "type": "text-content",
2966
- "content": "<p>Alternatively, this state can be prepared using arbitrary rotations (the $R_y$ gate).</p>\n<p>To start, we will try to find a decomposition of the target state that makes it easier to see how to prepare the state.<br />\nKnowing that $\\ket{+} = \\frac{1}{\\sqrt{2}}\\big(\\ket{0}+\\ket{1}\\big)$, we can represent the state as follows:</p>\n<p>$$ \\frac{1}{\\sqrt{3}} \\big(\\ket{00} + \\ket{01} + \\ket{10}\\big) = \\frac{\\sqrt{2}}{\\sqrt{3}}\\ket{0} \\otimes \\ket{+} + \\frac{1}{\\sqrt{3}}\\ket{1} \\otimes \\ket{0} $$</p>\n<p>To prepare this state, we first want to prepare the first qubit in the state $ \\frac{\\sqrt{2}}{\\sqrt{3}}\\ket{0} + \\frac{1}{\\sqrt{3}}\\ket{1} $, while leaving the second qubit unchanged.\nTo do this, we can use a rotation gate $R_y$ (see Single Qubit Gates kata) which will perform the following transformation:\n$$ R_y\\ket{0} = \\cos\\frac{\\theta}{2}\\ket{0} + \\sin\\frac{\\theta}{2}\\ket{1} $$\nWe need to find a value of $\\theta$ which satisfies both:\n$$\\cos\\frac{\\theta}{2} = \\frac{\\sqrt{2}}{\\sqrt{3}} \\text{ and } \\sin\\frac{\\theta}{2} = \\frac{1}{\\sqrt{3}}$$</p>\n<p>Solving the last equation for $\\theta$ gives us $\\frac{\\theta}{2} = \\arcsin\\frac{1}{\\sqrt{3}}$, or $\\theta = 2 \\arcsin\\frac{1}{\\sqrt{3}}$.</p>\n<p>When we apply this to our first qubit, we will get our desired intermediary state:\n$$ R_y(2 \\arcsin\\frac{1}{\\sqrt{3}})\\ket{0} \\otimes \\ket{0} = \n\\left(\\frac{\\sqrt{2}}{\\sqrt{3}}\\ket{0} + \\frac{1}{\\sqrt{3}}\\ket{1} \\right) \\otimes \\ket{0} = \n\\frac{\\sqrt{2}}{\\sqrt{3}}\\ket{0} \\otimes \\ket{0} + \\frac{1}{\\sqrt{3}}\\ket{1} \\otimes \\ket{0}$$</p>\n<p>Now, the second term of this state already matches our final goal, so we need to adjust the first term:\nprepare the $\\ket{+}$ state on the second qubit only if the first qubit is in the $\\ket{0}$ state.\nTo do this, we apply a conditional $H$ gate to the second qubit, if the first qubit is in the $\\ket{0}$ state, this will give our desired state:\n$$ \\frac{\\sqrt{2}}{\\sqrt{3}}\\ket{0} \\otimes \\ket{+} + \\frac{1}{\\sqrt{3}}\\ket{1} \\otimes \\ket{0}$$</p>\n<blockquote>\n<p>In Q# we can apply a conditional gate with arbitrary controls using the <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.canon/applycontrolledonint\"><code>ApplyControlledOnInt</code> operation</a>.\nIn this case we want the $H$ gate to be applied if the control qubit is in the $\\ket{0}$ state, so we will use <code>ApplyControlledOnInt(0, H)</code> gate.</p>\n</blockquote>\n"
2966
+ "content": "<p>Alternatively, this state can be prepared using arbitrary rotations (the $R_y$ gate).</p>\n<p>To start, you'll try to find a decomposition of the target state that makes it easier to see how to prepare the state.<br />\nKnowing that $\\ket{+} = \\frac{1}{\\sqrt{2}}\\big(\\ket{0}+\\ket{1}\\big)$, you can represent the state as follows:</p>\n<p>$$ \\frac{1}{\\sqrt{3}} \\big(\\ket{00} + \\ket{01} + \\ket{10}\\big) = \\frac{\\sqrt{2}}{\\sqrt{3}}\\ket{0} \\otimes \\ket{+} + \\frac{1}{\\sqrt{3}}\\ket{1} \\otimes \\ket{0} $$</p>\n<p>To prepare this state, you first want to prepare the first qubit in the state $ \\frac{\\sqrt{2}}{\\sqrt{3}}\\ket{0} + \\frac{1}{\\sqrt{3}}\\ket{1} $, while leaving the second qubit unchanged.\nTo do this, you can use a rotation gate $R_y$ (see Single Qubit Gates kata) which will perform the following transformation:\n$$ R_y\\ket{0} = \\cos\\frac{\\theta}{2}\\ket{0} + \\sin\\frac{\\theta}{2}\\ket{1} $$\nYou need to find a value of $\\theta$ which satisfies both:\n$$\\cos\\frac{\\theta}{2} = \\frac{\\sqrt{2}}{\\sqrt{3}} \\text{ and } \\sin\\frac{\\theta}{2} = \\frac{1}{\\sqrt{3}}$$</p>\n<p>Solving the last equation for $\\theta$ gives you $\\frac{\\theta}{2} = \\arcsin\\frac{1}{\\sqrt{3}}$, or $\\theta = 2 \\arcsin\\frac{1}{\\sqrt{3}}$.</p>\n<p>When you apply this to your first qubit, you'll get your desired intermediary state:\n$$ R_y(2 \\arcsin\\frac{1}{\\sqrt{3}})\\ket{0} \\otimes \\ket{0} = \n\\left(\\frac{\\sqrt{2}}{\\sqrt{3}}\\ket{0} + \\frac{1}{\\sqrt{3}}\\ket{1} \\right) \\otimes \\ket{0} = \n\\frac{\\sqrt{2}}{\\sqrt{3}}\\ket{0} \\otimes \\ket{0} + \\frac{1}{\\sqrt{3}}\\ket{1} \\otimes \\ket{0}$$</p>\n<p>Now, the second term of this state already matches your final goal, so you need to adjust the first term:\nprepare the $\\ket{+}$ state on the second qubit only if the first qubit is in the $\\ket{0}$ state.\nTo do this, you apply a conditional $H$ gate to the second qubit, if the first qubit is in the $\\ket{0}$ state, this will give your desired state:\n$$ \\frac{\\sqrt{2}}{\\sqrt{3}}\\ket{0} \\otimes \\ket{+} + \\frac{1}{\\sqrt{3}}\\ket{1} \\otimes \\ket{0}$$</p>\n<blockquote>\n<p>In Q#, you can apply a conditional gate with arbitrary controls using the <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.canon/applycontrolledonint\"><code>ApplyControlledOnInt</code> operation</a>.\nIn this case you want the $H$ gate to be applied if the control qubit is in the $\\ket{0}$ state, so you'll use the <code>ApplyControlledOnInt(0, H)</code> gate.</p>\n</blockquote>\n"
2967
2967
  },
2968
2968
  {
2969
2969
  "type": "solution",
@@ -2992,7 +2992,7 @@ export default {
2992
2992
  "items": [
2993
2993
  {
2994
2994
  "type": "text-content",
2995
- "content": "<p>To start, we will prepare the $\\frac{1}{\\sqrt{3}} \\big( \\ket{00} + \\ket{01} + \\ket{10} \\big)$ state using the solution to the previous task. To get to the final state, we need to add the relative phases to both $\\ket{01}$ and $\\ket{10}$ basis states without changing the $\\ket{00}$ state.</p>\n<p>First, we want to transform the $\\ket{01}$ state to the $\\omega \\ket{01} = e^{2\\pi i/3} \\ket{01}$ state, while not changing the other states.\nUsing the <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.intrinsic/r1\">$R_1$</a> gate, we can change a qubit state from $\\ket{1}$ to $e^{i\\theta}\\ket{1}$ without changing the $\\ket{0}$ state.\nIndeed, here is the effect of the $R_1$ gate on the $\\ket{0}$ and the $\\ket{1}$ states:</p>\n<p>$$ R_1 \\ket{0} = \\begin{bmatrix} 1 &amp; 0 \\\\ 0 &amp; e^{i\\theta} \\end{bmatrix} \\cdot \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} = \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} = \\ket{0} $$</p>\n<p>$$ R_1 \\ket{1} = \\begin{bmatrix} 1 &amp; 0 \\\\ 0 &amp; e^{i\\theta} \\end{bmatrix} \\cdot \\begin{bmatrix} 0 \\\\ 1 \\end{bmatrix} = \\begin{bmatrix} 0 \\\\ e^{i\\theta} \\end{bmatrix} = e^{i\\theta}\\ket{1} $$</p>\n<p>When we apply the $R_1$ gate to the second qubit, this will only affect the $\\ket{01}$ term, which is exactly what we want. Knowing this, we just need the right value for $\\theta$, which in this case is $\\frac{2\\pi}{3}$.</p>\n<p>We use the same approach to change $\\ket{10}$ term to $\\omega^2 \\ket{10}$. By applying the $R_1$ gate to the first qubit we will only change the $\\ket{10}$ term. In this case the right $\\theta$ will be $\\frac{4\\pi}{3}$.</p>\n"
2995
+ "content": "<p>To start, you'll prepare the $\\frac{1}{\\sqrt{3}} \\big( \\ket{00} + \\ket{01} + \\ket{10} \\big)$ state using the solution to the previous task. To get to the final state, you need to add the relative phases to both $\\ket{01}$ and $\\ket{10}$ basis states without changing the $\\ket{00}$ state.</p>\n<p>First, you want to transform the $\\ket{01}$ state to the $\\omega \\ket{01} = e^{2\\pi i/3} \\ket{01}$ state, while not changing the other states.\nUsing the <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.intrinsic/r1\">$R_1$</a> gate, you can change a qubit state from $\\ket{1}$ to $e^{i\\theta}\\ket{1}$ without changing the $\\ket{0}$ state.\nIndeed, here is the effect of the $R_1$ gate on the $\\ket{0}$ and the $\\ket{1}$ states:</p>\n<p>$$ R_1 \\ket{0} = \\begin{bmatrix} 1 &amp; 0 \\\\ 0 &amp; e^{i\\theta} \\end{bmatrix} \\cdot \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} = \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} = \\ket{0} $$</p>\n<p>$$ R_1 \\ket{1} = \\begin{bmatrix} 1 &amp; 0 \\\\ 0 &amp; e^{i\\theta} \\end{bmatrix} \\cdot \\begin{bmatrix} 0 \\\\ 1 \\end{bmatrix} = \\begin{bmatrix} 0 \\\\ e^{i\\theta} \\end{bmatrix} = e^{i\\theta}\\ket{1} $$</p>\n<p>When you apply the $R_1$ gate to the second qubit, this will only affect the $\\ket{01}$ term, which is exactly what you want. Knowing this, you just need the right value for $\\theta$, which in this case is $\\frac{2\\pi}{3}$.</p>\n<p>You use the same approach to change $\\ket{10}$ term to $\\omega^2 \\ket{10}$. By applying the $R_1$ gate to the first qubit you'll only change the $\\ket{10}$ term. In this case the right $\\theta$ will be $\\frac{4\\pi}{3}$.</p>\n"
2996
2996
  },
2997
2997
  {
2998
2998
  "type": "solution",
@@ -3021,7 +3021,7 @@ export default {
3021
3021
  "items": [
3022
3022
  {
3023
3023
  "type": "text-content",
3024
- "content": "<p>We start with the state $\\ket{\\psi_0}=\\ket{00}$.</p>\n<p><strong>Step 1:</strong> Start by putting the first qubit in the state $\\alpha\\ket{0} + \\beta\\ket{1}$, where $\\alpha$ and $\\beta$ are the square roots of relative weights of all basis states which start with 0 and with 1, respectively.</p>\n<p>In case of Hardy state, two basis states start with 0: $\\frac{1}{\\sqrt{12}} \\big (3\\ket{00} + \\ket{01}\\big )$ and two basis states start with 1: $\\frac{1}{\\sqrt{12}} \\big (\\ket{10} + \\ket{11}\\big )$.\nThe relative weights of 0 and 1 are just the sums of squares of their amplitudes:\n$\\alpha^2 = \\frac{9}{12} + \\frac{1}{12} = \\frac{10}{12}$ and $\\beta^2 = \\frac{1}{12} + \\frac{1}{12} = \\frac{2}{12}$, respectively.\nSo we'll need to put the first qubit in the state $\\sqrt{\\frac{10}{12}}\\ket{0} + \\sqrt{\\frac{2}{12}}\\ket{1}$ using the $R_y$ gate:</p>\n<p>$$\\ket{00} \\overset{R_{y_1}}\\rightarrow \\big (\\sqrt{\\frac{10}{12}}\\ket{0} + \\sqrt{\\frac{2}{12}}\\ket{1} \\big ) \\otimes \\ket{0} =: \\ket{\\psi_1}$$</p>\n<p>Here $R_{y_1} := R_y(2\\arccos \\sqrt{\\frac{10}{12}}) \\otimes I$.</p>\n<p><strong>Step 2:</strong> Finish the preparation by putting the second qubit in the right state, applying controlled Ry gates with the first qubit as the control.</p>\n<p>To get the first two terms right, you need to convert the terms</p>\n<p>$$\\big (\\sqrt{\\frac{10}{12}}\\ket{0} \\big) \\otimes \\ket{0} \\overset{R_{y_2}}\\rightarrow \\frac{1}{\\sqrt{12}} \\big (3\\ket{00} + \\ket{01} \\big) \\text{ (2.a) }$$\nand\n$$\\big (\\sqrt{\\frac{2}{12}}\\ket{1} \\big) \\otimes \\ket{0} \\overset{R_{y_3}}\\rightarrow \\frac{1}{\\sqrt{12}} \\big (\\ket{10}+\\ket{11} \\big) \\text{ (2.b) }$$</p>\n<p><strong>Step 2.a:</strong> The transformation\n$$\\big (\\sqrt{\\frac{10}{12}}\\ket{0} \\big) \\otimes \\ket{0} \\overset{R_{y_2}}\\rightarrow \\frac{1}{\\sqrt{12}} \\big (3\\ket{00} + \\ket{01} \\big)$$</p>\n<p>is equivalent to the following transformation (to see this, just multiply both sides by $\\sqrt{\\frac{12}{10}}$):</p>\n<p>$$\\ket{0} \\otimes \\ket{0} \\overset{R_{y_2}}\\rightarrow \\frac{1}{\\sqrt{10}} \\big (3\\ket{00} + \\ket{01} \\big) = \\ket{0} \\otimes \\frac{1}{\\sqrt{10}} \\big (3\\ket{0} + \\ket{1} \\big)$$</p>\n<p>This rotation should only be applied if the first qubit is in state $\\ket{0}$, i.e., we need a conditional-on-zero rotation. The rotation angle can be determined by $\\cos\\frac{\\theta_2}{2} = \\frac{3}{\\sqrt{10}}$ and $\\sin\\frac{\\theta_2}{2} = \\frac{1}{\\sqrt{10}}$.</p>\n<p><strong>Step 2.b:</strong> Similarly, the transformation</p>\n<p>$$\\big (\\sqrt{\\frac{2}{12}}\\ket{1} \\big) \\otimes \\ket{0} \\overset{R_{y_3}}\\rightarrow \\frac{1}{\\sqrt{12}} \\big (\\ket{10}+\\ket{11} \\big)$$</p>\n<p>is equivalent to</p>\n<p>$$\\ket{1} \\otimes \\ket{0} \\overset{R_{y_3}}\\rightarrow \\frac{1}{\\sqrt{2}} \\big (\\ket{10}+\\ket{11} \\big)$$</p>\n<p>and can be done using a controlled rotation, applied if first qubit is $\\ket{1}$, i.e., a conditional-on-one rotation. The rotation angle can be determined by $\\cos\\frac{\\theta_3}{2} = \\frac{1}{\\sqrt{2}}$ and $\\sin\\frac{\\theta_3}{2} = \\frac{1}{\\sqrt{2}}$.</p>\n"
3024
+ "content": "<p>You start with the state $\\ket{\\psi_0}=\\ket{00}$.</p>\n<p><strong>Step 1:</strong> Start by putting the first qubit in the state $\\alpha\\ket{0} + \\beta\\ket{1}$, where $\\alpha$ and $\\beta$ are the square roots of relative weights of all basis states which start with 0 and with 1, respectively.</p>\n<p>In case of Hardy state, two basis states start with 0: $\\frac{1}{\\sqrt{12}} \\big (3\\ket{00} + \\ket{01}\\big )$ and two basis states start with 1: $\\frac{1}{\\sqrt{12}} \\big (\\ket{10} + \\ket{11}\\big )$.\nThe relative weights of 0 and 1 are just the sums of squares of their amplitudes:\n$\\alpha^2 = \\frac{9}{12} + \\frac{1}{12} = \\frac{10}{12}$ and $\\beta^2 = \\frac{1}{12} + \\frac{1}{12} = \\frac{2}{12}$, respectively.\nSo you'll need to put the first qubit in the state $\\sqrt{\\frac{10}{12}}\\ket{0} + \\sqrt{\\frac{2}{12}}\\ket{1}$ using the $R_y$ gate:</p>\n<p>$$\\ket{00} \\overset{R_{y_1}}\\rightarrow \\big (\\sqrt{\\frac{10}{12}}\\ket{0} + \\sqrt{\\frac{2}{12}}\\ket{1} \\big ) \\otimes \\ket{0} =: \\ket{\\psi_1}$$</p>\n<p>Here $R_{y_1} := R_y(2\\arccos \\sqrt{\\frac{10}{12}}) \\otimes I$.</p>\n<p><strong>Step 2:</strong> Finish the preparation by putting the second qubit in the right state, applying controlled $R_y$ gates with the first qubit as the control.</p>\n<p>To get the first two terms right, you need to convert the terms</p>\n<p>$$\\big (\\sqrt{\\frac{10}{12}}\\ket{0} \\big) \\otimes \\ket{0} \\overset{R_{y_2}}\\rightarrow \\frac{1}{\\sqrt{12}} \\big (3\\ket{00} + \\ket{01} \\big) \\text{ (2.a) }$$\nand\n$$\\big (\\sqrt{\\frac{2}{12}}\\ket{1} \\big) \\otimes \\ket{0} \\overset{R_{y_3}}\\rightarrow \\frac{1}{\\sqrt{12}} \\big (\\ket{10}+\\ket{11} \\big) \\text{ (2.b) }$$</p>\n<p><strong>Step 2.a:</strong> The transformation\n$$\\big (\\sqrt{\\frac{10}{12}}\\ket{0} \\big) \\otimes \\ket{0} \\overset{R_{y_2}}\\rightarrow \\frac{1}{\\sqrt{12}} \\big (3\\ket{00} + \\ket{01} \\big)$$</p>\n<p>is equivalent to the following transformation (to see this, just multiply both sides by $\\sqrt{\\frac{12}{10}}$):</p>\n<p>$$\\ket{0} \\otimes \\ket{0} \\overset{R_{y_2}}\\rightarrow \\frac{1}{\\sqrt{10}} \\big (3\\ket{00} + \\ket{01} \\big) = \\ket{0} \\otimes \\frac{1}{\\sqrt{10}} \\big (3\\ket{0} + \\ket{1} \\big)$$</p>\n<p>This rotation should only be applied if the first qubit is in state $\\ket{0}$, that is, you need a conditional-on-zero rotation. The rotation angle can be determined by $\\cos\\frac{\\theta_2}{2} = \\frac{3}{\\sqrt{10}}$ and $\\sin\\frac{\\theta_2}{2} = \\frac{1}{\\sqrt{10}}$.</p>\n<p><strong>Step 2.b:</strong> Similarly, the transformation</p>\n<p>$$\\big (\\sqrt{\\frac{2}{12}}\\ket{1} \\big) \\otimes \\ket{0} \\overset{R_{y_3}}\\rightarrow \\frac{1}{\\sqrt{12}} \\big (\\ket{10}+\\ket{11} \\big)$$</p>\n<p>is equivalent to</p>\n<p>$$\\ket{1} \\otimes \\ket{0} \\overset{R_{y_3}}\\rightarrow \\frac{1}{\\sqrt{2}} \\big (\\ket{10}+\\ket{11} \\big)$$</p>\n<p>and can be done using a controlled rotation, applied if first qubit is $\\ket{1}$, that is, a conditional-on-one rotation. The rotation angle can be determined by $\\cos\\frac{\\theta_3}{2} = \\frac{1}{\\sqrt{2}}$ and $\\sin\\frac{\\theta_3}{2} = \\frac{1}{\\sqrt{2}}$.</p>\n"
3025
3025
  },
3026
3026
  {
3027
3027
  "type": "solution",
@@ -3050,7 +3050,7 @@ export default {
3050
3050
  "items": [
3051
3051
  {
3052
3052
  "type": "text-content",
3053
- "content": "<p>The problem becomes more manageable if broken down into the simplest cases and built up from there.</p>\n<ol>\n<li>\n<p>The smallest instance of the problem, $N = 1$, requires preparing $\\ket{W_1} = \\ket{1}$; this can be done trivially using an X gate.</p>\n</li>\n<li>\n<p>The next instance, $N = 2$, requires preparing $\\ket{W_2} = \\frac{1}{\\sqrt2}\\big(\\ket{10} + \\ket{01}\\big)$. It matches one of the Bell states we've seen earlier, but preparing it will be more interesting (and more useful for the next steps!) if we think of it in recursive terms. Let's see how to express $\\ket{W_2}$ in terms of $\\ket{W_1}$:</p>\n</li>\n</ol>\n<p>$$\\ket{W_2} = \\frac{1}{\\sqrt2}\\big(\\ket{10} + \\ket{01}\\big) = \\frac{1}{\\sqrt2}\\big(\\ket{W_1} \\otimes \\ket{0} + \\ket{0} \\otimes \\ket{W_1}\\big)$$</p>\n<p>This representation suggests us a solution: &quot;split&quot; the starting state $\\ket{00}$ in two terms, prepare $\\ket{W_1}$ on the first qubit for the first term and on the second qubit - for the second term.\nTo do this, we can again use an auxiliary qubit prepared in the $\\ket{+}$ state and control the preparation of $\\ket{W_1}$ state on the first or the second qubit based on the state of the auxiliary qubit:</p>\n<p>$$\\ket{0}_{aux} \\ket{00}_{reg} \\overset{H}{\\longrightarrow} \\frac{1}{\\sqrt2}(\\ket{0}_{aux} + \\ket{1}_{aux}) \\otimes \\ket{00}_{reg} = \\frac{1}{\\sqrt2}(\\ket{0}_{aux} \\ket{00}_{reg} + \\ket{1}_{aux} \\ket{00}_{reg})\\overset{CNOT_0}{\\longrightarrow}$$</p>\n<p>$${\\longrightarrow}\\frac{1}{\\sqrt2}(\\ket{0}_{aux} \\ket{W_1}\\ket{0}_{reg} + \\ket{1}_{aux} \\ket{00}_{reg})\\overset{CNOT_1}{\\longrightarrow}$$</p>\n<p>$${\\longrightarrow}\\frac{1}{\\sqrt2}(\\ket{0}_{aux} \\ket{W_1}\\ket{0}_{reg} + \\ket{1}_{aux} \\ket{0}\\ket{W_1}_{reg})$$</p>\n<blockquote>\n<p>The auxiliary qubit is now entangled with the rest of the qubits, so we can't simply reset it without it affecting the superposition we have prepared using it.</p>\n</blockquote>\n<p>The last step to bring the register to the desired state is to uncompute the auxiliary qubit for the term $\\ket{1}_{aux} \\ket{0}\\ket{W_1}_{reg}$ (the other term already has it in state $\\ket{0}$).</p>\n<p>To do this, we need to consider the explicit expression of the state $\\ket{0}\\ket{W_1} = \\ket{01}$. Similarly to the previous tasks, we'll uncompute the auxiliary qubit for this term by using a controlled X gate, with the auxiliary qubit as the target and the main register in the $\\ket{01}$ state as a control. This will make sure that the gate is applied only for this term and not for any others.</p>\n<p>The last step can be simplified to use fewer qubits as controls: we can use just the second qubit of the main register in state $\\ket{1}$ as control, since we know that if the second qubit is in state $\\ket{1}$, the first one has to be in state $\\ket{0}$ (we don't need to use both of them as the control pattern).</p>\n<ol start=\"3\">\n<li>If we take this one step further, to $N = 4$, we'll see that the same recursive logic can be applied to the larger and larger sizes of the problem. Indeed,</li>\n</ol>\n<p>$$\\ket{W_4} = \\frac{1}{2}\\big(\\ket{1000} + \\ket{0100} + \\ket{0010} + \\ket{0001}\\big) = $$\n$$= \\frac{1}{\\sqrt2} \\big(\\frac{1}{\\sqrt2}(\\ket{10} + \\ket{01}) \\otimes \\ket{00} + \\ket{00} \\otimes \\frac{1}{\\sqrt2}(\\ket{10} + \\ket{01}) \\big) = $$\n$$= \\frac{1}{\\sqrt2} \\big(\\ket{W_2} \\otimes \\ket{00} + \\ket{00} \\otimes \\ket{W_2}\\big)$$</p>\n<p>We can use the same approach for this case: prepare an auxiliary qubit in $\\ket{+}$ state and use it to control preparation of $W_2$ state on the first and the second half of the register. The last step will be uncomputing the $\\ket{1}$ state of the auxiliary qubit using two controlled X gates with each of the qubits of the second half of the register in state $\\ket{1}$ as controls.</p>\n<p>The same recursive approach can be generalized for arbitrary powers of 2 as the register size.</p>\n"
3053
+ "content": "<p>The problem becomes more manageable if broken down into the simplest cases and built up from there.</p>\n<ol>\n<li>\n<p>The smallest instance of the problem, $N = 1$, requires preparing $\\ket{W_1} = \\ket{1}$; this can be done trivially using an $X$ gate.</p>\n</li>\n<li>\n<p>The next instance, $N = 2$, requires preparing $\\ket{W_2} = \\frac{1}{\\sqrt2}\\big(\\ket{10} + \\ket{01}\\big)$. It matches one of the Bell states you've seen earlier, but preparing it will be more interesting (and more useful for the next steps!) if you think of it in recursive terms. Let's see how to express $\\ket{W_2}$ in terms of $\\ket{W_1}$:</p>\n</li>\n</ol>\n<p>$$\\ket{W_2} = \\frac{1}{\\sqrt2}\\big(\\ket{10} + \\ket{01}\\big) = \\frac{1}{\\sqrt2}\\big(\\ket{W_1} \\otimes \\ket{0} + \\ket{0} \\otimes \\ket{W_1}\\big)$$</p>\n<p>This representation suggests you a solution: &quot;split&quot; the starting state $\\ket{00}$ in two terms, prepare $\\ket{W_1}$ on the first qubit for the first term and on the second qubit - for the second term.\nTo do this, you can again use an auxiliary qubit prepared in the $\\ket{+}$ state and control the preparation of $\\ket{W_1}$ state on the first or the second qubit based on the state of the auxiliary qubit:</p>\n<p>$$\\ket{0}_{aux} \\ket{00}_{reg} \\overset{H}{\\longrightarrow} \\frac{1}{\\sqrt2}(\\ket{0}_{aux} + \\ket{1}_{aux}) \\otimes \\ket{00}_{reg} = \\frac{1}{\\sqrt2}(\\ket{0}_{aux} \\ket{00}_{reg} + \\ket{1}_{aux} \\ket{00}_{reg})\\overset{CNOT_0}{\\longrightarrow}$$</p>\n<p>$${\\longrightarrow}\\frac{1}{\\sqrt2}(\\ket{0}_{aux} \\ket{W_1}\\ket{0}_{reg} + \\ket{1}_{aux} \\ket{00}_{reg})\\overset{CNOT_1}{\\longrightarrow}$$</p>\n<p>$${\\longrightarrow}\\frac{1}{\\sqrt2}(\\ket{0}_{aux} \\ket{W_1}\\ket{0}_{reg} + \\ket{1}_{aux} \\ket{0}\\ket{W_1}_{reg})$$</p>\n<blockquote>\n<p>The auxiliary qubit is now entangled with the rest of the qubits, so you can't simply reset it without it affecting the superposition you have prepared using it.</p>\n</blockquote>\n<p>The last step to bring the register to the desired state is to uncompute the auxiliary qubit for the term $\\ket{1}_{aux} \\ket{0}\\ket{W_1}_{reg}$ (the other term already has it in state $\\ket{0}$).</p>\n<p>To do this, you need to consider the explicit expression of the state $\\ket{0}\\ket{W_1} = \\ket{01}$. Similarly to the previous tasks, you'll uncompute the auxiliary qubit for this term by using a controlled $X$ gate, with the auxiliary qubit as the target and the main register in the $\\ket{01}$ state as a control. This will make sure that the gate is applied only for this term and not for any others.</p>\n<p>The last step can be simplified to use fewer qubits as controls: you can use just the second qubit of the main register in state $\\ket{1}$ as control, since you know that if the second qubit is in state $\\ket{1}$, the first one has to be in state $\\ket{0}$ (you don't need to use both of them as the control pattern).</p>\n<ol start=\"3\">\n<li>If you take this one step further, to $N = 4$, you'll see that the same recursive logic can be applied to the larger and larger sizes of the problem. Indeed,</li>\n</ol>\n<p>$$\\ket{W_4} = \\frac{1}{2}\\big(\\ket{1000} + \\ket{0100} + \\ket{0010} + \\ket{0001}\\big) = $$\n$$= \\frac{1}{\\sqrt2} \\big(\\frac{1}{\\sqrt2}(\\ket{10} + \\ket{01}) \\otimes \\ket{00} + \\ket{00} \\otimes \\frac{1}{\\sqrt2}(\\ket{10} + \\ket{01}) \\big) = $$\n$$= \\frac{1}{\\sqrt2} \\big(\\ket{W_2} \\otimes \\ket{00} + \\ket{00} \\otimes \\ket{W_2}\\big)$$</p>\n<p>You can use the same approach for this case: prepare an auxiliary qubit in $\\ket{+}$ state and use it to control preparation of $W_2$ state on the first and the second half of the register. The last step will be uncomputing the $\\ket{1}$ state of the auxiliary qubit using two controlled $X$ gates with each of the qubits of the second half of the register in state $\\ket{1}$ as controls.</p>\n<p>The same recursive approach can be generalized for arbitrary powers of 2 as the register size.</p>\n"
3054
3054
  },
3055
3055
  {
3056
3056
  "type": "solution",
@@ -3059,7 +3059,7 @@ export default {
3059
3059
  },
3060
3060
  {
3061
3061
  "type": "text-content",
3062
- "content": "<p>This implementation of the recursion requires $\\log_2 N$ extra qubits allocated for controlling the preparation (one per level of recursion). We can modify our approach to use just one extra qubit at a time.</p>\n<p>To do this, let's notice that to prepare $\\ket{W_{N}}$ we need to prepare the $\\ket{W_{N-1}}$ state on half of the qubits for both states of the auxiliary qubit, the difference is just in which half of the register we're using. This means that we can prepare the $\\ket{W_{N-1}}$ state on the first half of the qubits, and use an auxiliary qubit in superposition to control swapping the first half of the register with the second half. The uncomputation of the auxiliary qubit happens in the same way as in the first approach.</p>\n"
3062
+ "content": "<p>This implementation of the recursion requires $\\log_2 N$ extra qubits allocated for controlling the preparation (one per level of recursion). You can modify your approach to use just one extra qubit at a time.</p>\n<p>To do this, let's notice that to prepare $\\ket{W_{N}}$ you need to prepare the $\\ket{W_{N-1}}$ state on half of the qubits for both states of the auxiliary qubit, the difference is just in which half of the register you're using. This means that you can prepare the $\\ket{W_{N-1}}$ state on the first half of the qubits, and use an auxiliary qubit in superposition to control swapping the first half of the register with the second half. The uncomputation of the auxiliary qubit happens in the same way as in the first approach.</p>\n"
3063
3063
  },
3064
3064
  {
3065
3065
  "type": "solution",
@@ -3075,7 +3075,7 @@ export default {
3075
3075
  "title": "W State on Arbitrary Number of Qubits",
3076
3076
  "description": {
3077
3077
  "type": "text-content",
3078
- "content": "<p><strong>Input:</strong> $N$ qubits in the $\\ket{0 \\dots 0}$ state ($N$ is not necessarily a power of two).</p>\n<p><strong>Goal:</strong> Change the state of the qubits to the <a href=\"https://en.wikipedia.org/wiki/W_state\">W state</a> - an equal superposition of $N$ basis states on $N$ qubits which have Hamming weight of 1.</p>\n<p>For example, for $N = 3$ the required state is $\\frac{1}{\\sqrt{3}}\\big(\\ket{100} + \\ket{010} + \\ket{001}\\big)$.</p>\n<details>\n <summary><b>Need a hint?</b></summary>\n You can modify the signature of the given operation to specify its controlled specialization.\n</details>\n"
3078
+ "content": "<p><strong>Input:</strong> $N$ qubits in the $\\ket{0 \\dots 0}$ state ($N$ isn't necessarily a power of two).</p>\n<p><strong>Goal:</strong> Change the state of the qubits to the <a href=\"https://en.wikipedia.org/wiki/W_state\">W state</a> - an equal superposition of $N$ basis states on $N$ qubits which have Hamming weight of 1.</p>\n<p>For example, for $N = 3$ the required state is $\\frac{1}{\\sqrt{3}}\\big(\\ket{100} + \\ket{010} + \\ket{001}\\big)$.</p>\n<details>\n <summary><b>Need a hint?</b></summary>\n You can modify the signature of the given operation to specify its controlled specialization.\n</details>\n"
3079
3079
  },
3080
3080
  "sourceIds": [
3081
3081
  "preparing_states__wstate_arbitrary__Verification.qs",
@@ -3088,7 +3088,7 @@ export default {
3088
3088
  "items": [
3089
3089
  {
3090
3090
  "type": "text-content",
3091
- "content": "<p>This problem allows a variety of solutions that rely on techniques from arbitrary rotations to recursion to postselection.</p>\n<h3>Iterative Solution</h3>\n<p>The first approach we will describe relies on performing a sequence of controlled rotations.</p>\n<p>To prepare a weighted superposition $\\cos \\theta \\ket{0} + \\sin \\theta \\ket{1}$ on a single qubit, we need to start with the $\\ket{0}$ state and apply the $R_y$ gate to it with the angle parameter equal to $2 \\theta$.\nWe'll apply the $R_y$ gate with angle $2 \\theta_1 = 2\\arcsin \\frac{1}{\\sqrt{N}}$ to the first qubit of the register to prepare the following state:</p>\n<p>$$(\\cos \\theta_1 \\ket{0} + \\sin \\theta_1 \\ket{1}) \\otimes \\ket{0 \\dots 0} = \\frac{1}{\\sqrt{N}}\\ket{10 \\dots 0} + \\frac{\\sqrt{N-1}}{\\sqrt{N}}\\ket{00 \\dots 0} $$</p>\n<p>The first term $\\frac{1}{\\sqrt{N}}\\ket{10 \\dots 0}$ already matches the first term of the $\\ket{W_N}$ state; now we need to convert the second term $\\frac{\\sqrt{N-1}}{\\sqrt{N}}\\ket{00 \\dots 0}$ into the rest of the $\\ket{W_N}$ terms.</p>\n<p>To prepare a term that matches the second term of the $\\ket{W_N}$ state, we can apply another $R_y$ gate to the term $\\ket{00 \\dots 0}$, this time to the second qubit, with an angle $2 \\theta_2 = 2\\arcsin \\frac{1}{\\sqrt{N-1}}$.\nTo make sure it doesn't affect the term that we're already happy with, we will apply a controlled version of the $R_y$ gate, with the first qubit of the register in state $\\ket{0}$ as control.\nThis will change our state to</p>\n<p>$$\\frac{1}{\\sqrt{N}}\\ket{10 \\dots 0} + \\frac{\\sqrt{N-1}}{\\sqrt{N}} \\ket{0} \\otimes (\\cos \\theta_2 \\ket{0} + \\sin \\theta_2 \\ket{1}) \\otimes \\ket{0 \\dots 0} = $$\n$$= \\frac{1}{\\sqrt{N}}\\ket{10 \\dots 0} + \\frac{\\sqrt{N-1}}{\\sqrt{N}} \\frac{1}{\\sqrt{N-1}} \\ket{010 \\dots 0} + \\frac{\\sqrt{N-1}}{\\sqrt{N}} \\frac{\\sqrt{N-2}}{\\sqrt{N-1}} \\ket{000 \\dots 0}$$</p>\n<p>Now we have two terms that match the terms of the $\\ket{W_N}$ state, and need to convert the third term $\\frac{\\sqrt{N-2}}{\\sqrt{N}}\\ket{00 \\dots 0}$ into the rest of terms.</p>\n<p>We will keep going like this, preparing one term of the $\\ket{W_N}$ state at a time, until the rotation on the last qubit will be an $X$ gate, controlled on all previous qubits being in the $\\ket{0 \\dots 0}$ state.</p>\n"
3091
+ "content": "<p>This problem allows a variety of solutions that rely on techniques from arbitrary rotations to recursion to postselection.</p>\n<h3>Iterative Solution</h3>\n<p>The first approach relies on performing a sequence of controlled rotations.</p>\n<p>To prepare a weighted superposition $\\cos \\theta \\ket{0} + \\sin \\theta \\ket{1}$ on a single qubit, you need to start with the $\\ket{0}$ state and apply the $R_y$ gate to it with the angle parameter equal to $2 \\theta$.\nYou'll apply the $R_y$ gate with angle $2 \\theta_1 = 2\\arcsin \\frac{1}{\\sqrt{N}}$ to the first qubit of the register to prepare the following state:</p>\n<p>$$(\\cos \\theta_1 \\ket{0} + \\sin \\theta_1 \\ket{1}) \\otimes \\ket{0 \\dots 0} = \\frac{1}{\\sqrt{N}}\\ket{10 \\dots 0} + \\frac{\\sqrt{N-1}}{\\sqrt{N}}\\ket{00 \\dots 0} $$</p>\n<p>The first term $\\frac{1}{\\sqrt{N}}\\ket{10 \\dots 0}$ already matches the first term of the $\\ket{W_N}$ state; now you need to convert the second term $\\frac{\\sqrt{N-1}}{\\sqrt{N}}\\ket{00 \\dots 0}$ into the rest of the $\\ket{W_N}$ terms.</p>\n<p>To prepare a term that matches the second term of the $\\ket{W_N}$ state, you can apply another $R_y$ gate to the term $\\ket{00 \\dots 0}$, this time to the second qubit, with an angle $2 \\theta_2 = 2\\arcsin \\frac{1}{\\sqrt{N-1}}$.\nTo make sure it doesn't affect the term that you're already happy with, you'll apply a controlled version of the $R_y$ gate, with the first qubit of the register in state $\\ket{0}$ as control.\nThis will change the state to</p>\n<p>$$\\frac{1}{\\sqrt{N}}\\ket{10 \\dots 0} + \\frac{\\sqrt{N-1}}{\\sqrt{N}} \\ket{0} \\otimes (\\cos \\theta_2 \\ket{0} + \\sin \\theta_2 \\ket{1}) \\otimes \\ket{0 \\dots 0} = $$\n$$= \\frac{1}{\\sqrt{N}}\\ket{10 \\dots 0} + \\frac{\\sqrt{N-1}}{\\sqrt{N}} \\frac{1}{\\sqrt{N-1}} \\ket{010 \\dots 0} + \\frac{\\sqrt{N-1}}{\\sqrt{N}} \\frac{\\sqrt{N-2}}{\\sqrt{N-1}} \\ket{000 \\dots 0}$$</p>\n<p>Now you have two terms that match the terms of the $\\ket{W_N}$ state, and need to convert the third term $\\frac{\\sqrt{N-2}}{\\sqrt{N}}\\ket{00 \\dots 0}$ into the rest of terms.</p>\n<p>You'll keep going like this, preparing one term of the $\\ket{W_N}$ state at a time, until the rotation on the last qubit will be an $X$ gate, controlled on all previous qubits being in the $\\ket{0 \\dots 0}$ state.</p>\n"
3092
3092
  },
3093
3093
  {
3094
3094
  "type": "solution",
@@ -3097,7 +3097,7 @@ export default {
3097
3097
  },
3098
3098
  {
3099
3099
  "type": "text-content",
3100
- "content": "<h3>Recursive Solution</h3>\n<p>We can express the same sequence of gates using recursion, if we notice that</p>\n<p>$$\\ket{W_N} = \\frac{1}{\\sqrt{N}}\\ket{10 \\dots 0} + \\frac{\\sqrt{N-1}}{\\sqrt{N}}\\ket{0} \\otimes \\ket{W_{N-1}}$$</p>\n<p>The first step of the solution would still be applying the $R_y$ gate with angle $2 \\theta_1 = 2\\arcsin \\frac{1}{\\sqrt{N}}$ to the first qubit of the register to prepare the following state:</p>\n<p>$$\\frac{1}{\\sqrt{N}}\\ket{10 \\dots 0} + \\frac{\\sqrt{N-1}}{\\sqrt{N}}\\ket{00 \\dots 0} $$</p>\n<p>But we would express the rest of the controlled rotations as the operation that prepares the $\\ket{W_{N-1}}$ state, controlled on the $\\ket{0}$ state of the first qubit.</p>\n<blockquote>\n<p>Note that you don't have to implement the controlled version of this operation yourself; it is sufficient to add <code>is Adj + Ctl</code> to the signature of the operation <code>WState_Arbitrary</code> to specify that controlled variant has to be generated automatically.</p>\n</blockquote>\n"
3100
+ "content": "<h3>Recursive Solution</h3>\n<p>You can express the same sequence of gates using recursion, if you notice that</p>\n<p>$$\\ket{W_N} = \\frac{1}{\\sqrt{N}}\\ket{10 \\dots 0} + \\frac{\\sqrt{N-1}}{\\sqrt{N}}\\ket{0} \\otimes \\ket{W_{N-1}}$$</p>\n<p>The first step of the solution would still be applying the $R_y$ gate with angle $2 \\theta_1 = 2\\arcsin \\frac{1}{\\sqrt{N}}$ to the first qubit of the register to prepare the following state:</p>\n<p>$$\\frac{1}{\\sqrt{N}}\\ket{10 \\dots 0} + \\frac{\\sqrt{N-1}}{\\sqrt{N}}\\ket{00 \\dots 0} $$</p>\n<p>But you'd express the rest of the controlled rotations as the operation that prepares the $\\ket{W_{N-1}}$ state, controlled on the $\\ket{0}$ state of the first qubit.</p>\n<blockquote>\n<p>Note that you don't have to implement the controlled version of this operation yourself; it's sufficient to add <code>is Adj + Ctl</code> to the signature of the operation <code>WState_Arbitrary</code> to specify that controlled variant has to be generated automatically.</p>\n</blockquote>\n"
3101
3101
  },
3102
3102
  {
3103
3103
  "type": "solution",
@@ -3106,7 +3106,7 @@ export default {
3106
3106
  },
3107
3107
  {
3108
3108
  "type": "text-content",
3109
- "content": "<h3>Post-selection Solution</h3>\n<p>Let's assume that we know how to prepare the $W$ state for $N = 2^k$ (we've discussed this in the previous task), and figure out how to use this knowledge as a building block for solving this task.</p>\n<p>Let's look at the smallest possible case for which $N \\neq 2^k$: $N = 3$ (we'll be able to generalize our solution for this case to an arbitrary number of qubits). The target $W$ state looks like this:</p>\n<p>$$\\ket{W_3} = \\frac{1}{3}\\big(\\ket{100} + \\ket{010} + \\ket{001}\\big)$$</p>\n<p>We will start by finding the smallest power of 2 $P$ which is greater than or equal to $N$; for our case $N = 3$ this power will be $P = 4$. We will allocate an extra $P - N$ qubits and use the solution of the previous task to prepare the $W_P$ state that looks as follows (with the state of the extra qubit highlighted in bold):</p>\n<p>$$\\ket{W_4} = \\frac{1}{2}\\big( \\ket{100\\textbf{0}} + \\ket{010\\textbf{0}} + \\ket{001\\textbf{0}} + \\ket{000\\textbf{1}} \\big) = $$\n$$= \\frac{\\sqrt3}{2} \\cdot \\frac{1}{\\sqrt3}\\big(\\ket{100} + \\ket{010} + \\ket{001} \\big) \\otimes \\ket{\\textbf{0}} + \\frac{1}{2}\\ket{000} \\otimes \\ket{\\textbf{1}} = $$\n$$= \\frac{\\sqrt3}{2} \\ket{W_3} \\otimes \\ket{\\textbf{0}} + \\frac{1}{2}\\ket{000} \\otimes \\ket{\\textbf{1}}$$</p>\n<p>As we can see, if the extra qubit is in the $\\ket{0}$ state, the main 3 qubits that we are concerned about are in the right $\\ket{W_3}$ state.</p>\n<p>What happens if we measure just the extra qubit? This causes a partial collapse of the system to the state defined by the measurement result:</p>\n<ul>\n<li>If the result is $\\ket{0}$, the system collapses to the $\\ket{W_3}$ state - which is exactly what we wanted to achieve.</li>\n<li>If the result is $\\ket{1}$, the system collapses to a state $\\ket{000}$, so our goal is not achieved. The good thing is, this only happens in 25% of the cases, and we can just try again.</li>\n</ul>\n<p>If we generalize this approach to an arbitrary $N$, we'll have</p>\n<p>$$\\ket{W_P} = \\frac{\\sqrt{N}}{\\sqrt{P}} \\ket{W_N} \\otimes \\ket{\\textbf{0}}^{\\otimes P-N} + \\frac{\\sqrt{P-N}}{\\sqrt{P}} \\ket{0}^{\\otimes N} \\otimes \\ket{W_{P-N}}$$</p>\n<p>Measuring the extra $P-N$ qubits gives us two possibilities:</p>\n<ul>\n<li>All the extra qubits are in the $\\ket{0}$ state; this means the main qubits collapse to the $\\ket{W_N}$ state.</li>\n<li>One of the extra qubits is in the $\\ket{1}$ state; this means that the main qubits collapse to the $\\ket{0}^{\\otimes N}$ state, which is <strong>not</strong> the desired state. In this case we will reset and try again until all the extra qubits are in the $\\ket{0}$ state.</li>\n</ul>\n"
3109
+ "content": "<h3>Post-selection Solution</h3>\n<p>Let's assume that you know how to prepare the $W$ state for $N = 2^k$ (see the previous task), and figure out how to use this knowledge as a building block for solving this task.</p>\n<p>Let's look at the smallest possible case for which $N \\neq 2^k$: $N = 3$ (you'll be able to generalize the solution for this case to an arbitrary number of qubits). The target $W$ state looks like this:</p>\n<p>$$\\ket{W_3} = \\frac{1}{3}\\big(\\ket{100} + \\ket{010} + \\ket{001}\\big)$$</p>\n<p>You'll start by finding the smallest power of 2 $P$ which is greater than or equal to $N$; for this case $N = 3$, so this power will be $P = 4$. You'll allocate an extra $P - N$ qubits and use the solution of the previous task to prepare the $W_P$ state that looks as follows (with the state of the extra qubit highlighted in bold):</p>\n<p>$$\\ket{W_4} = \\frac{1}{2}\\big( \\ket{100\\textbf{0}} + \\ket{010\\textbf{0}} + \\ket{001\\textbf{0}} + \\ket{000\\textbf{1}} \\big) = $$\n$$= \\frac{\\sqrt3}{2} \\cdot \\frac{1}{\\sqrt3}\\big(\\ket{100} + \\ket{010} + \\ket{001} \\big) \\otimes \\ket{\\textbf{0}} + \\frac{1}{2}\\ket{000} \\otimes \\ket{\\textbf{1}} = $$\n$$= \\frac{\\sqrt3}{2} \\ket{W_3} \\otimes \\ket{\\textbf{0}} + \\frac{1}{2}\\ket{000} \\otimes \\ket{\\textbf{1}}$$</p>\n<p>As you can see, if the extra qubit is in the $\\ket{0}$ state, the main 3 qubits that you're concerned about are in the right $\\ket{W_3}$ state.</p>\n<p>What happens if you 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 $\\ket{0}$, the system collapses to the $\\ket{W_3}$ state - which is exactly what you wanted to achieve.</li>\n<li>If the result is $\\ket{1}$, the system collapses to a state $\\ket{000}$, so your goal is not achieved. The good thing is, this only happens in $25\\%$ of the cases, and you can just try again.</li>\n</ul>\n<p>If you generalize this approach to an arbitrary $N$, you'll have</p>\n<p>$$\\ket{W_P} = \\frac{\\sqrt{N}}{\\sqrt{P}} \\ket{W_N} \\otimes \\ket{\\textbf{0}}^{\\otimes P-N} + \\frac{\\sqrt{P-N}}{\\sqrt{P}} \\ket{0}^{\\otimes N} \\otimes \\ket{W_{P-N}}$$</p>\n<p>Measuring the extra $P-N$ qubits gives you two possibilities:</p>\n<ul>\n<li>All the extra qubits are in the $\\ket{0}$ state; this means the main qubits collapse to the $\\ket{W_N}$ state.</li>\n<li>One of the extra qubits is in the $\\ket{1}$ state; this means that the main qubits collapse to the $\\ket{0}^{\\otimes N}$ state, which is <strong>not</strong> the desired state. In this case, you'll reset and try again until all the extra qubits are in the $\\ket{0}$ state.</li>\n</ul>\n"
3110
3110
  },
3111
3111
  {
3112
3112
  "type": "solution",
@@ -6596,7 +6596,7 @@ export default {
6596
6596
  "items": [
6597
6597
  {
6598
6598
  "type": "text-content",
6599
- "content": "<p>In the quantum scenario, the classical function you're working with is implemented as a quantum oracle - a &quot;black box&quot; operation used as input to another algorithm. This operation is implemented in a way which allows to perform calculations not only on individual inputs, but also on superpositions of inputs.</p>\n<p>The oracle has to act on quantum states instead of classical values.\nTo enable this, integer input $x$ is represented as a qubit state $\\ket{x}$.</p>\n<p>The type of oracles used in this tutorial are called <em>phase oracles</em>. A phase oracle $U_f$ encodes the value of the classical function $f$ it implements in the phase of the qubit state as follows:</p>\n<p>$$U_f \\ket{x} = (-1)^{f(x)} \\ket{x}$$</p>\n<p>In our case $f$ can return only two values, 0 or 1, which result in no phase change or multiplication by a relative phase $-1$, respectively.</p>\n<p>The effect of such an oracle on any single basis state isn't particularly interesting: it just adds a global phase which isn't 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>There are only four single-bit functions, so you can see how to implement them all as phase oracles in Q#.</p>\n<ol>\n<li>$f(x) \\equiv 0$</li>\n</ol>\n<p>This is the easiest function to implement: if $f(x) \\equiv 0$,</p>\n<p>$$U_f \\ket{x} \\equiv (-1)^0 \\ket{x} = \\ket{x}$$</p>\n<p>This means that $U_f$ is an identity - a transformation which does absolutely nothing!</p>\n<ol start=\"2\">\n<li>$f(x) \\equiv 1$</li>\n</ol>\n<p>The second constant function is slightly trickier: if $f(x) \\equiv 1$</p>\n<p>$$U_f \\ket{x} \\equiv (-1)^1 \\ket{x} = - \\ket{x}$$</p>\n<p>Now $U_f$ is a negative identity, that is, a transformation which applies a global phase of $-1$ to the state.\nA lot of algorithms just ignore the global phase accumulated in them, since it isn't observable.\nHowever, if you want to be really meticulous, you can use the $R$ gate which performs a given rotation around the given axis.\nWhen called with <code>PauliI</code> axis, this operation applies a global phase to the given qubit.</p>\n<ol start=\"3\">\n<li>$f(x) = x$</li>\n</ol>\n<p>$$U_f \\ket{x} = (-1)^{f(x)} \\ket{x} = (-1)^{x} \\ket{x}$$</p>\n<p>This means that you don't need to do anything if the qubit is in the $\\ket{0}$ state, and apply a phase of $-1$ if it is in the $\\ket{1}$ state. This is exactly the effect of the $Z$ gate!</p>\n<p>In the following demo, you'll see how to implement the first three one-bit functions as quantum oracles, and their effect on a qubit state.\nAfter that, you'll try to implement the oracle for the fourth function on your own!</p>\n"
6599
+ "content": "<p>In the quantum scenario, the classical function you're working with is implemented as a quantum oracle - a &quot;black box&quot; operation used as input to another algorithm. This operation is implemented in a way which allows to perform calculations not only on individual inputs, but also on superpositions of inputs.</p>\n<p>The oracle has to act on quantum states instead of classical values.\nTo enable this, integer input $x$ is represented as a qubit state $\\ket{x}$.</p>\n<p>The type of oracles used in this tutorial are called <em>phase oracles</em>. A phase oracle $U_f$ encodes the value of the classical function $f$ it implements in the phase of the qubit state as follows:</p>\n<p>$$U_f \\ket{x} = (-1)^{f(x)} \\ket{x}$$</p>\n<p>In this case $f$ can return only two values, 0 or 1, which result in no phase change or multiplication by a relative phase $-1$, respectively.</p>\n<p>The effect of such an oracle on any single basis state isn't particularly interesting: it just adds a global phase which isn't 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>There are only four single-bit functions, so you can see how to implement them all as phase oracles in Q#.</p>\n<ol>\n<li>$f(x) \\equiv 0$</li>\n</ol>\n<p>This is the easiest function to implement: if $f(x) \\equiv 0$,</p>\n<p>$$U_f \\ket{x} \\equiv (-1)^0 \\ket{x} = \\ket{x}$$</p>\n<p>This means that $U_f$ is an identity - a transformation which does absolutely nothing!</p>\n<ol start=\"2\">\n<li>$f(x) \\equiv 1$</li>\n</ol>\n<p>The second constant function is slightly trickier: if $f(x) \\equiv 1$</p>\n<p>$$U_f \\ket{x} \\equiv (-1)^1 \\ket{x} = - \\ket{x}$$</p>\n<p>Now $U_f$ is a negative identity, that is, a transformation which applies a global phase of $-1$ to the state.\nA lot of algorithms just ignore the global phase accumulated in them, since it isn't observable.\nHowever, if you want to be really meticulous, you can use the $R$ gate which performs a given rotation around the given axis.\nWhen called with <code>PauliI</code> axis, this operation applies a global phase to the given qubit.</p>\n<ol start=\"3\">\n<li>$f(x) = x$</li>\n</ol>\n<p>$$U_f \\ket{x} = (-1)^{f(x)} \\ket{x} = (-1)^{x} \\ket{x}$$</p>\n<p>This means that you don't need to do anything if the qubit is in the $\\ket{0}$ state, and apply a phase of $-1$ if it is in the $\\ket{1}$ state. This is exactly the effect of the $Z$ gate!</p>\n<p>In the following demo, you'll see how to implement the first three one-bit functions as quantum oracles, and their effect on a qubit state.\nAfter that, you'll try to implement the oracle for the fourth function on your own!</p>\n"
6600
6600
  },
6601
6601
  {
6602
6602
  "type": "example",
@@ -6873,7 +6873,7 @@ export default {
6873
6873
  "items": [
6874
6874
  {
6875
6875
  "type": "text-content",
6876
- "content": "<p>To wrap up our discussion in this kata, let's take a look at a problem solved using a similar approach - the Bernstein-Vazirani algorithm.\nIn this problem, you are also given an oracle implementing an $N$-bit function $f(x): \\{0, 1\\}^N \\to \\{0, 1\\}$.\nHowever, this time the function is guaranteed to be a <em>scalar product function</em>, that is, there exists an $N$-bit string $s$\nthat allows the following representation ($\\cdot$ is bitwise inner product of integers modulo $2$):</p>\n<p>$$f(x) = x \\cdot s = \\sum_{k=0}^{N-1} x_k s_k \\bmod 2$$</p>\n<p>The task is to recover the hidden bit string $s$.</p>\n<p><strong>Examples</strong></p>\n<ul>\n<li>$f(x) \\equiv 0$ is an example of such a function with $s = 0, \\dots, 0$.</li>\n<li>$f(x) = 1 \\text{ if x has odd number of 1s, and } 0 \\text{ otherwise }$ is another example of such a function, with $s = 1, \\dots, 1$.</li>\n</ul>\n<p>If you solve this problem classically, how many calls to the given function will you need?\nYou'd need to use one query to recover each bit of $s$ (the query for $k$-th bit can be a bit string with $1$ in the $k$-th bit and zeros in all other positions), for a total of $N$ queries.</p>\n<p>What about the quantum scenario?\nIt turns out that the algorithm that allows you to solve this problem looks just like Deutsch-Jozsa algorithm,\nexcept for the way you interpret the measurement results on the last step. To see this, you'll need to take another look\nat the math involved in applying Hadamard gates to multiple qubits.</p>\n<h3>Apply Hadamard transform to each qubit: a different view</h3>\n<p>When you apply an $H$ gate to a single qubit in the basis state $\\ket{x}$, you can write the result as the following sum:</p>\n<p>$$H\\ket{x} = \\frac1{\\sqrt2} (\\ket{0} + (-1)^{x} \\ket{1}) = \\frac1{\\sqrt2} \\sum_{z \\in {0, 1}} (-1)^{x \\cdot z} \\ket{z}$$</p>\n<p>If you use this representation to spell out the result of applying an $H$ gate to each qubit of an $N$-qubit basis state\n$\\ket{x} = \\ket{x_0}\\ket{x_1} \\dots \\ket{x_{N-1}}$, you get:</p>\n<p>$$H\\ket{x} = \\frac1{\\sqrt{2^N}} \\sum_{z_k \\in {0, 1}} (-1)^{x_0z_0 + \\dots + x_{N-1}z_{N-1}} \\ket{z_0}\\ket{z_1} \\dots \\ket{z_{N-1}} =$$</p>\n<p>$$= \\frac1{\\sqrt{2^N}} \\sum_{z = 0}^{2^N-1} (-1)^{x \\cdot z} \\ket{z}$$</p>\n<p>With this in mind, let's revisit the algorithm and see how you can write the exact quantum state after it.</p>\n<h3>Bernstein-Vazirani algorithm</h3>\n<p>Bernstein-Vazirani algorithm follows the same outline as Deutsch-Jozsa algorithm:</p>\n<ol>\n<li>Apply the $H$ gate to each qubit.</li>\n<li>Apply the oracle.</li>\n<li>Apply the $H$ gate to each qubit again.</li>\n<li>Measure all qubits.</li>\n</ol>\n<p>You know that after the second step the qubits end up in the following state:</p>\n<p>$$\\frac{1}{\\sqrt{2^N}} \\sum_{x=0}^{2^N-1} (-1)^{f(x)} \\ket{x}$$</p>\n<p>Now, once you apply the Hadamard gates to each qubit, the system state becomes:</p>\n<p>$$\\frac{1}{\\sqrt{2^N}} \\sum_{x=0}^{2^N-1} \\sum_{z=0}^{2^N-1} (-1)^{f(x) + x \\cdot z} \\ket{z}$$</p>\n<blockquote>\n<p>In Deutsch-Jozsa algorithm, you looked at the amplitude of the $\\ket{0}$ state in this expression, which was\n$\\frac{1}{\\sqrt{2^N}} \\sum_{x=0}^{2^N-1} (-1)^{f(x)}$.</p>\n</blockquote>\n<p>Now, let's take a look at the amplitude of the $\\ket{s}$ state - the state that encodes the hidden bit string you're looking for.\nThis amplitude is</p>\n<p>$$\\frac{1}{\\sqrt{2^N}} \\sum_{x=0}^{2^N-1} (-1)^{f(x) + x \\cdot s}$$</p>\n<p>Since $f(x) = x \\cdot s$, for all values of $x$ $f(x) + x \\cdot s = 2 x \\cdot s$, and $(-1)^{f(x) + x \\cdot s} = 1$.\nOverall the amplitude of $\\ket{s}$ is</p>\n<p>$$\\frac{1}{\\sqrt{2^N}} \\sum_{x=0}^{2^N-1} 1 = \\frac{1}{\\sqrt{2^N}} 2^N = 1$$</p>\n<p>This means that the state after applying the Hadamard gates is just $\\ket{s}$, and measuring it gives you the bit string $s$!\nAnd, same as Deutsch-Jozsa algorithm, Bernstein-Vazirani algorithm takes only one oracle call.</p>\n"
6876
+ "content": "<p>To wrap up the discussion in this kata, let's take a look at a problem solved using a similar approach - the Bernstein-Vazirani algorithm.\nIn this problem, you are also given an oracle implementing an $N$-bit function $f(x): \\{0, 1\\}^N \\to \\{0, 1\\}$.\nHowever, this time the function is guaranteed to be a <em>scalar product function</em>, that is, there exists an $N$-bit string $s$\nthat allows the following representation ($\\cdot$ is bitwise inner product of integers modulo $2$):</p>\n<p>$$f(x) = x \\cdot s = \\sum_{k=0}^{N-1} x_k s_k \\bmod 2$$</p>\n<p>The task is to recover the hidden bit string $s$.</p>\n<p><strong>Examples</strong></p>\n<ul>\n<li>$f(x) \\equiv 0$ is an example of such a function with $s = 0, \\dots, 0$.</li>\n<li>$f(x) = 1 \\text{ if x has odd number of 1s, and } 0 \\text{ otherwise }$ is another example of such a function, with $s = 1, \\dots, 1$.</li>\n</ul>\n<p>If you solve this problem classically, how many calls to the given function will you need?\nYou'd need to use one query to recover each bit of $s$ (the query for $k$-th bit can be a bit string with $1$ in the $k$-th bit and zeros in all other positions), for a total of $N$ queries.</p>\n<p>What about the quantum scenario?\nIt turns out that the algorithm that allows you to solve this problem looks just like Deutsch-Jozsa algorithm,\nexcept for the way you interpret the measurement results on the last step. To see this, you'll need to take another look\nat the math involved in applying Hadamard gates to multiple qubits.</p>\n<h3>Apply Hadamard transform to each qubit: a different view</h3>\n<p>When you apply an $H$ gate to a single qubit in the basis state $\\ket{x}$, you can write the result as the following sum:</p>\n<p>$$H\\ket{x} = \\frac1{\\sqrt2} (\\ket{0} + (-1)^{x} \\ket{1}) = \\frac1{\\sqrt2} \\sum_{z \\in {0, 1}} (-1)^{x \\cdot z} \\ket{z}$$</p>\n<p>If you use this representation to spell out the result of applying an $H$ gate to each qubit of an $N$-qubit basis state\n$\\ket{x} = \\ket{x_0}\\ket{x_1} \\dots \\ket{x_{N-1}}$, you get:</p>\n<p>$$H\\ket{x} = \\frac1{\\sqrt{2^N}} \\sum_{z_k \\in {0, 1}} (-1)^{x_0z_0 + \\dots + x_{N-1}z_{N-1}} \\ket{z_0}\\ket{z_1} \\dots \\ket{z_{N-1}} =$$</p>\n<p>$$= \\frac1{\\sqrt{2^N}} \\sum_{z = 0}^{2^N-1} (-1)^{x \\cdot z} \\ket{z}$$</p>\n<p>With this in mind, let's revisit the algorithm and see how you can write the exact quantum state after it.</p>\n<h3>Bernstein-Vazirani algorithm</h3>\n<p>Bernstein-Vazirani algorithm follows the same outline as Deutsch-Jozsa algorithm:</p>\n<ol>\n<li>Apply the $H$ gate to each qubit.</li>\n<li>Apply the oracle.</li>\n<li>Apply the $H$ gate to each qubit again.</li>\n<li>Measure all qubits.</li>\n</ol>\n<p>You know that after the second step the qubits end up in the following state:</p>\n<p>$$\\frac{1}{\\sqrt{2^N}} \\sum_{x=0}^{2^N-1} (-1)^{f(x)} \\ket{x}$$</p>\n<p>Now, once you apply the Hadamard gates to each qubit, the system state becomes:</p>\n<p>$$\\frac{1}{\\sqrt{2^N}} \\sum_{x=0}^{2^N-1} \\sum_{z=0}^{2^N-1} (-1)^{f(x) + x \\cdot z} \\ket{z}$$</p>\n<blockquote>\n<p>In Deutsch-Jozsa algorithm, you looked at the amplitude of the $\\ket{0}$ state in this expression, which was\n$\\frac{1}{\\sqrt{2^N}} \\sum_{x=0}^{2^N-1} (-1)^{f(x)}$.</p>\n</blockquote>\n<p>Now, let's take a look at the amplitude of the $\\ket{s}$ state - the state that encodes the hidden bit string you're looking for.\nThis amplitude is</p>\n<p>$$\\frac{1}{\\sqrt{2^N}} \\sum_{x=0}^{2^N-1} (-1)^{f(x) + x \\cdot s}$$</p>\n<p>Since $f(x) = x \\cdot s$, for all values of $x$ $f(x) + x \\cdot s = 2 x \\cdot s$, and $(-1)^{f(x) + x \\cdot s} = 1$.\nOverall the amplitude of $\\ket{s}$ is</p>\n<p>$$\\frac{1}{\\sqrt{2^N}} \\sum_{x=0}^{2^N-1} 1 = \\frac{1}{\\sqrt{2^N}} 2^N = 1$$</p>\n<p>This means that the state after applying the Hadamard gates is just $\\ket{s}$, and measuring it gives you the bit string $s$!\nAnd, same as Deutsch-Jozsa algorithm, Bernstein-Vazirani algorithm takes only one oracle call.</p>\n"
6877
6877
  }
6878
6878
  ]
6879
6879
  },
@@ -8558,7 +8558,7 @@ export default {
8558
8558
  "items": [
8559
8559
  {
8560
8560
  "type": "text-content",
8561
- "content": "<p>The key idea that enables quantum error correction is discretization of quantum errors: being able to correct any kind of error using a code that explicitly allows us to correct only a discrete set of Pauli errors.</p>\n<blockquote>\n<p>The high-level mathematical reasoning behind our ability to discretize quantum errors is as follows.</p>\n<p>Let's consider a single-qubit error acting on one of the qubits of the encoded state, converting the entire state from $\\ket{\\psi_L}$ to $E\\ket{\\psi_L}$.\nYou can represent the state with this error as some superposition of four orthogonal states: $\\ket{\\psi_L}$, $X\\ket{\\psi_L}$, $Z\\ket{\\psi_L}$, and $XZ\\ket{\\psi_L}$ (this is possible because the Pauli matrices $I, X, Y, Z$ form a basis for $2 \\times 2$ Hermitian matrices, so the operator representing the error can be decomposed as their linear combination).</p>\n<p>Doing the set of parity measurements to detect $X$ and $Z$ errors will collapse the state $E\\ket{\\psi_L}$ into other of these four states, yielding measurement results that match that state. For example, if parity measurements yield a set of results indicating that an $X$ error happened, the state $E\\ket{\\psi_L}$ will collapse to $X\\ket{\\psi_L}$.</p>\n<p>Then, you can use the results of parity measurements to correct the error that they indicate, bringing our state back to the error-free state $\\ket{\\psi_L}$.</p>\n</blockquote>\n<p>Does Shor code indeed correct all errors, and not just the set of Pauli errors $X$, $Y$, and $Z$?\nLet's try it out!</p>\n<p>The following demo puts together the steps of error correction using Shor code: it encodes a given logical state into multiple qubits, introduces an arbitrary error, runs the error detection code and applies error correction if necessary, and checks that the result is an accurate encoding of the starting logical state. Experiment with applying different errors to different qubits of the code - and not just the Pauli errors but any single-qubit rotations too. You can even use a measurement!</p>\n"
8561
+ "content": "<p>The key idea that enables quantum error correction is discretization of quantum errors: being able to correct any kind of error using a code that explicitly allows us to correct only a discrete set of Pauli errors.</p>\n<blockquote>\n<p>The high-level mathematical reasoning behind the ability to discretize quantum errors is as follows.</p>\n<p>Let's consider a single-qubit error acting on one of the qubits of the encoded state, converting the entire state from $\\ket{\\psi_L}$ to $E\\ket{\\psi_L}$.\nYou can represent the state with this error as some superposition of four orthogonal states: $\\ket{\\psi_L}$, $X\\ket{\\psi_L}$, $Z\\ket{\\psi_L}$, and $XZ\\ket{\\psi_L}$ (this is possible because the Pauli matrices $I, X, Y, Z$ form a basis for $2 \\times 2$ Hermitian matrices, so the operator representing the error can be decomposed as their linear combination).</p>\n<p>Doing the set of parity measurements to detect $X$ and $Z$ errors will collapse the state $E\\ket{\\psi_L}$ into other of these four states, yielding measurement results that match that state. For example, if parity measurements yield a set of results indicating that an $X$ error happened, the state $E\\ket{\\psi_L}$ will collapse to $X\\ket{\\psi_L}$.</p>\n<p>Then, you can use the results of parity measurements to correct the error that they indicate, bringing the state back to the error-free state $\\ket{\\psi_L}$.</p>\n</blockquote>\n<p>Does Shor code indeed correct all errors, and not just the set of Pauli errors $X$, $Y$, and $Z$?\nLet's try it out!</p>\n<p>The following demo puts together the steps of error correction using Shor code: it encodes a given logical state into multiple qubits, introduces an arbitrary error, runs the error detection code and applies error correction if necessary, and checks that the result is an accurate encoding of the starting logical state. Experiment with applying different errors to different qubits of the code - and not just the Pauli errors but any single-qubit rotations too. You can even use a measurement!</p>\n"
8562
8562
  },
8563
8563
  {
8564
8564
  "type": "example",
@@ -8871,7 +8871,7 @@ export default {
8871
8871
  },
8872
8872
  {
8873
8873
  "id": "complex_arithmetic__Common.qs",
8874
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Random; \n open Microsoft.Quantum.Convert;\n\n operation DrawRandomComplex() : Complex {\n // Generates a random complex number. \n let real = DrawRandomDouble(-10., 10.);\n let imag = DrawRandomDouble(-10., 10.);\n return Complex(real, imag);\n }\n\n function ComplexAsString(x : Complex) : String {\n if x::Imag < 0.0 {\n $\"{x::Real} - {AbsD(x::Imag)}i\"\n } else {\n $\"{x::Real} + {x::Imag}i\"\n }\n }\n\n function ComplexPolarAsString(x : ComplexPolar) : String {\n $\"{x::Magnitude} * exp({x::Argument}i)\"\n }\n\n operation CheckTwoComplexOpsAreSame(sol : (Complex, Complex) -> Complex, ref : (Complex, Complex) -> Complex) : Bool {\n for _ in 0 .. 24 {\n let x = DrawRandomComplex();\n let y = DrawRandomComplex();\n\n let expected = ref(x, y);\n let actual = sol(x, y);\n \n if not ComplexEqual(expected, actual) {\n Message(\"Incorrect\");\n Message($\"For x = {ComplexAsString(x)}, y = {ComplexAsString(y)} expected return {ComplexAsString(expected)}, actual return {ComplexAsString(actual)}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n\n function ComplexEqual(x : Complex, y : Complex) : Bool { \n // Tests two complex numbers for equality.\n AbsD(x::Real - y::Real) <= 0.001 and AbsD(x::Imag - y::Imag) <= 0.001\n }\n\n function ComplexPolarEqual(x : ComplexPolar, y : ComplexPolar) : Bool { \n // Tests two complex polar numbers for equality.\n AbsD(x::Magnitude - y::Magnitude) <= 0.001 and AbsD(x::Argument - y::Argument) <= 0.001\n }\n}\n"
8874
+ "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Random;\n open Microsoft.Quantum.Convert;\n\n operation DrawRandomComplex() : Complex {\n // Generates a random complex number.\n let real = DrawRandomDouble(-10., 10.);\n let imag = DrawRandomDouble(-10., 10.);\n return Complex(real, imag);\n }\n\n function ComplexAsString(x : Complex) : String {\n let precision = 3;\n if x.Imag < 0.0 {\n $\"{DoubleAsStringWithPrecision(x.Real, precision)} - {DoubleAsStringWithPrecision(AbsD(x.Imag), precision)}i\"\n } else {\n $\"{DoubleAsStringWithPrecision(x.Real, precision)} + {DoubleAsStringWithPrecision(x.Imag, precision)}i\"\n }\n }\n\n function ComplexPolarAsString(x : ComplexPolar) : String {\n let precision = 3;\n $\"{DoubleAsStringWithPrecision(x.Magnitude, precision)} * exp({DoubleAsStringWithPrecision(x.Argument, precision)}i)\"\n }\n\n operation CheckTwoComplexOpsAreSame(sol : (Complex, Complex) -> Complex, ref : (Complex, Complex) -> Complex) : Bool {\n for _ in 0..24 {\n let x = DrawRandomComplex();\n let y = DrawRandomComplex();\n\n let expected = ref(x, y);\n let actual = sol(x, y);\n\n if not ComplexEqual(expected, actual) {\n Message(\"Incorrect\");\n Message($\"For x = {ComplexAsString(x)}, y = {ComplexAsString(y)} expected return {ComplexAsString(expected)}, actual return {ComplexAsString(actual)}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n\n function ComplexEqual(x : Complex, y : Complex) : Bool {\n // Tests two complex numbers for equality.\n AbsD(x.Real - y.Real) <= 0.001 and AbsD(x.Imag - y.Imag) <= 0.001\n }\n\n function ComplexPolarEqual(x : ComplexPolar, y : ComplexPolar) : Bool {\n // Tests two complex polar numbers for equality.\n AbsD(x.Magnitude - y.Magnitude) <= 0.001 and AbsD(x.Argument - y.Argument) <= 0.001\n }\n}\n"
8875
8875
  },
8876
8876
  {
8877
8877
  "id": "complex_arithmetic__complex_addition__Verification.qs",
@@ -8883,7 +8883,7 @@ export default {
8883
8883
  },
8884
8884
  {
8885
8885
  "id": "complex_arithmetic__complex_conjugate__Verification.qs",
8886
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n\n function ComplexConjugate_Reference(x : Complex) : Complex {\n // Return the complex conjugate \n Complex(x::Real, -x::Imag)\n } \n\n @EntryPoint()\n operation CheckSolution() : Bool { \n for _ in 0 .. 24 {\n let x = DrawRandomComplex();\n\n let expected = ComplexConjugate_Reference(x);\n let actual = Kata.ComplexConjugate(x);\n \n if not ComplexEqual(expected, actual) {\n Message(\"Incorrect\");\n Message($\"For x = {ComplexAsString(x)} expected return {ComplexAsString(expected)}, actual return {ComplexAsString(actual)}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}"
8886
+ "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n\n function ComplexConjugate_Reference(x : Complex) : Complex {\n // Return the complex conjugate\n Complex(x.Real, -x.Imag)\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for _ in 0..24 {\n let x = DrawRandomComplex();\n\n let expected = ComplexConjugate_Reference(x);\n let actual = Kata.ComplexConjugate(x);\n\n if not ComplexEqual(expected, actual) {\n Message(\"Incorrect\");\n Message($\"For x = {ComplexAsString(x)} expected return {ComplexAsString(expected)}, actual return {ComplexAsString(actual)}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
8887
8887
  },
8888
8888
  {
8889
8889
  "id": "complex_arithmetic__complex_division__Verification.qs",
@@ -8891,15 +8891,15 @@ export default {
8891
8891
  },
8892
8892
  {
8893
8893
  "id": "complex_arithmetic__complex_modulus__Verification.qs",
8894
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for _ in 0 .. 24 {\n let x = DrawRandomComplex();\n\n let expected = AbsComplex(x);\n let actual = Kata.ComplexModulus(x);\n \n if AbsD(expected - actual) > 1e-6 {\n Message(\"Incorrect\");\n Message($\"For x = {ComplexAsString(x)} expected return {expected}, actual return {actual}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
8894
+ "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Math;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for _ in 0 .. 24 {\n let x = DrawRandomComplex();\n\n let expected = AbsComplex(x);\n let actual = Kata.ComplexModulus(x);\n \n if AbsD(expected - actual) > 1e-6 {\n let precision = 3;\n Message(\"Incorrect\");\n Message($\"For x = {ComplexAsString(x)} expected return {DoubleAsStringWithPrecision(expected, precision)}, actual return {DoubleAsStringWithPrecision(actual, precision)}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
8895
8895
  },
8896
8896
  {
8897
8897
  "id": "complex_arithmetic__complex_exponents__Verification.qs",
8898
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Random; \n \n function ComplexExponent_Reference(x : Complex) : Complex {\n let expa = E() ^ x::Real;\n return Complex(expa * Cos(x::Imag), expa * Sin(x::Imag));\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for _ in 0 .. 24 {\n let x = DrawRandomComplex();\n\n let expected = ComplexExponent_Reference(x);\n let actual = Kata.ComplexExponent(x);\n \n if not ComplexEqual(expected, actual) {\n Message(\"Incorrect\");\n Message($\"For x = {ComplexAsString(x)} expected return {ComplexAsString(expected)}, actual return {ComplexAsString(actual)}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
8898
+ "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Random;\n\n function ComplexExponent_Reference(x : Complex) : Complex {\n let expa = E()^x.Real;\n return Complex(expa * Cos(x.Imag), expa * Sin(x.Imag));\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for _ in 0..24 {\n let x = DrawRandomComplex();\n\n let expected = ComplexExponent_Reference(x);\n let actual = Kata.ComplexExponent(x);\n\n if not ComplexEqual(expected, actual) {\n Message(\"Incorrect\");\n Message($\"For x = {ComplexAsString(x)} expected return {ComplexAsString(expected)}, actual return {ComplexAsString(actual)}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
8899
8899
  },
8900
8900
  {
8901
8901
  "id": "complex_arithmetic__complex_powers_real__Verification.qs",
8902
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Random; \n \n function ComplexExpReal_Reference(r : Double, x : Complex) : Complex {\n if AbsD(r) < 1e-9 {\n return Complex(0.0, 0.0);\n }\n let real = r ^ x::Real * Cos(x::Imag * Log(r));\n let imaginary = r ^ x::Real * Sin(x::Imag * Log(r));\n return Complex(real, imaginary);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for ind in 0 .. 24 {\n let x = DrawRandomComplex();\n let r = ind == 0 ? 0.0 | DrawRandomDouble(0., 10.); \n\n let expected = ComplexExpReal_Reference(r, x); \n let actual = Kata.ComplexExpReal(r, x); \n \n if not ComplexEqual(expected, actual) {\n Message(\"Incorrect\");\n Message($\"For x = {ComplexAsString(x)} and r = {r} expected return {ComplexAsString(expected)}, actual return {ComplexAsString(actual)}.\");\n return false;\n } \n } \n\n Message(\"Correct!\");\n return true; \n }\n}\n"
8902
+ "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Random;\n\n function ComplexExpReal_Reference(r : Double, x : Complex) : Complex {\n if AbsD(r) < 1e-9 {\n return Complex(0., 0.);\n }\n let real = r^x.Real * Cos(x.Imag * Log(r));\n let imaginary = r^x.Real * Sin(x.Imag * Log(r));\n return Complex(real, imaginary);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for ind in 0..24 {\n let x = DrawRandomComplex();\n let r = ind == 0 ? 0.0 | DrawRandomDouble(0., 10.);\n\n let expected = ComplexExpReal_Reference(r, x);\n let actual = Kata.ComplexExpReal(r, x);\n\n if not ComplexEqual(expected, actual) {\n let precision = 3; \n Message(\"Incorrect\");\n Message($\"For x = {ComplexAsString(x)} and r = {DoubleAsStringWithPrecision(r, precision)} expected return {ComplexAsString(expected)}, actual return {ComplexAsString(actual)}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
8903
8903
  },
8904
8904
  {
8905
8905
  "id": "complex_arithmetic__cartesian_to_polar__Verification.qs",
@@ -8919,7 +8919,7 @@ export default {
8919
8919
  },
8920
8920
  {
8921
8921
  "id": "linear_algebra__Common.qs",
8922
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n\n function ArraysEqualD(actual : Double[][], expected : Double[][]) : Bool {\n if Length(actual) != Length(expected) {\n Message(\"Incorrect\");\n Message($\"Expected number of rows {Length(expected)}, actual {Length(actual)}\");\n return false;\n }\n for i in 0 .. Length(actual) - 1 {\n if Length(actual[i]) != Length(expected[i]) {\n Message(\"Incorrect\");\n Message($\"For row {i}, expected number of columns {Length(expected[i])}, actual {Length(actual[i])}\");\n return false;\n }\n\n for j in 0 .. Length(actual[i]) - 1 {\n if AbsD(actual[i][j] - expected[i][j]) > 1e-9 {\n Message(\"Incorrect\");\n Message($\"For element in row {i}, column {j}, expected {expected[i][j]}, actual {actual[i][j]}\");\n return false;\n }\n }\n }\n \n Message(\"Correct!\");\n return true;\n }\n\n function ComplexAsString(x : Complex) : String {\n if x::Imag < 0.0 {\n $\"{x::Real} - {AbsD(x::Imag)}i\"\n } else {\n $\"{x::Real} + {x::Imag}i\"\n }\n }\n\n function ArraysEqualC(actual : Complex[][], expected : Complex[][]) : Bool {\n if Length(actual) != Length(expected) {\n Message(\"Incorrect\");\n Message($\"Expected number of rows {Length(expected)}, actual {Length(actual)}\");\n return false;\n }\n for i in 0 .. Length(actual) - 1 {\n if Length(actual[i]) != Length(expected[i]) {\n Message(\"Incorrect\");\n Message($\"For row {i}, expected number of columns {Length(expected[i])}, actual {Length(actual[i])}\");\n return false;\n }\n\n for j in 0 .. Length(actual[i]) - 1 {\n if AbsComplex(MinusC(actual[i][j], expected[i][j])) > 1e-9 {\n Message(\"Incorrect\");\n Message($\"For element in row {i}, column {j}, expected {ComplexAsString(expected[i][j])}, actual {ComplexAsString(actual[i][j])}\");\n return false;\n }\n }\n }\n \n Message(\"Correct!\");\n return true;\n }\n}\n"
8922
+ "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n\n function ArraysEqualD(actual : Double[][], expected : Double[][]) : Bool {\n if Length(actual) != Length(expected) {\n Message(\"Incorrect\");\n Message($\"Expected number of rows {Length(expected)}, actual {Length(actual)}\");\n return false;\n }\n for i in 0..Length(actual) - 1 {\n if Length(actual[i]) != Length(expected[i]) {\n Message(\"Incorrect\");\n Message($\"For row {i}, expected number of columns {Length(expected[i])}, actual {Length(actual[i])}\");\n return false;\n }\n\n for j in 0..Length(actual[i]) - 1 {\n if AbsD(actual[i][j] - expected[i][j]) > 1e-9 {\n Message(\"Incorrect\");\n Message($\"For element in row {i}, column {j}, expected {expected[i][j]}, actual {actual[i][j]}\");\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n\n function ComplexAsString(x : Complex) : String {\n if x.Imag < 0.0 {\n $\"{x.Real} - {AbsD(x.Imag)}i\"\n } else {\n $\"{x.Real} + {x.Imag}i\"\n }\n }\n\n function ArraysEqualC(actual : Complex[][], expected : Complex[][]) : Bool {\n if Length(actual) != Length(expected) {\n Message(\"Incorrect\");\n Message($\"Expected number of rows {Length(expected)}, actual {Length(actual)}\");\n return false;\n }\n for i in 0..Length(actual) - 1 {\n if Length(actual[i]) != Length(expected[i]) {\n Message(\"Incorrect\");\n Message($\"For row {i}, expected number of columns {Length(expected[i])}, actual {Length(actual[i])}\");\n return false;\n }\n\n for j in 0..Length(actual[i]) - 1 {\n if AbsComplex(MinusC(actual[i][j], expected[i][j])) > 1e-9 {\n Message(\"Incorrect\");\n Message($\"For element in row {i}, column {j}, expected {ComplexAsString(expected[i][j])}, actual {ComplexAsString(actual[i][j])}\");\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
8923
8923
  },
8924
8924
  {
8925
8925
  "id": "linear_algebra__scalar_multiplication__Verification.qs",
@@ -9007,19 +9007,19 @@ export default {
9007
9007
  },
9008
9008
  {
9009
9009
  "id": "single_qubit_gates__complex_phase__Verification.qs",
9010
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Convert;\n\n operation PhaseChange (alpha : Double, q : Qubit) : Unit is Adj+Ctl {\n R1(alpha, q);\n }\n\n operation CheckSolution() : Bool {\n for i in 0 .. 36 {\n let alpha = ((2.0 * PI()) * IntAsDouble(i)) / 36.0;\n let solution = register => Kata.PhaseChange(alpha, register[0]);\n let reference = register => PhaseChange(alpha, register[0]);\n let isCorrect = CheckOperationsAreEqualStrict(1, solution, reference);\n if not isCorrect {\n Message(\"Incorrect.\");\n Message($\"The solution was incorrect for the test case alpha = {alpha}.\");\n Message(\"Hint: examine the effect your solution has on the state 0.6|0〉 + 0.8|1〉 and compare it with the effect it \" +\n \"is expected to have.\");\n ShowQuantumStateComparison(1, qs => Ry(ArcTan2(0.8, 0.6) * 2.0, qs[0]), solution, reference);\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}"
9010
+ "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Convert;\n\n operation PhaseChange (alpha : Double, q : Qubit) : Unit is Adj+Ctl {\n R1(alpha, q);\n }\n\n operation CheckSolution() : Bool {\n for i in 0 .. 36 {\n let alpha = ((2.0 * PI()) * IntAsDouble(i)) / 36.0;\n let solution = register => Kata.PhaseChange(alpha, register[0]);\n let reference = register => PhaseChange(alpha, register[0]);\n let isCorrect = CheckOperationsAreEqualStrict(1, solution, reference);\n if not isCorrect {\n let precision = 3;\n Message(\"Incorrect.\");\n Message($\"The solution was incorrect for the test case alpha = {DoubleAsStringWithPrecision(alpha, precision)}.\");\n Message(\"Hint: examine the effect your solution has on the state 0.6|0〉 + 0.8|1〉 and compare it with the effect it \" +\n \"is expected to have.\");\n ShowQuantumStateComparison(1, qs => Ry(ArcTan2(0.8, 0.6) * 2.0, qs[0]), solution, reference);\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}"
9011
9011
  },
9012
9012
  {
9013
9013
  "id": "single_qubit_gates__amplitude_change__Verification.qs",
9014
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Convert;\n\n operation AmplitudeChange (alpha : Double, q : Qubit) : Unit is Adj+Ctl {\n Ry(2.0 * alpha, q);\n }\n\n operation CheckSolution() : Bool {\n for i in 0 .. 36 {\n let alpha = ((2.0 * PI()) * IntAsDouble(i)) / 36.0;\n let solution = register => Kata.AmplitudeChange(alpha, register[0]);\n let reference = register => AmplitudeChange(alpha, register[0]);\n let isCorrect = CheckOperationsAreEqualStrict(1, solution, reference);\n if not isCorrect {\n Message(\"Incorrect.\");\n Message($\"The solution was incorrect for the test case alpha = {alpha}.\");\n Message(\"Hint: examine the effect your solution has on the state 0.6|0〉 + 0.8|1〉 and compare it with the effect it \" +\n \"is expected to have.\");\n ShowQuantumStateComparison(1, qs => Ry(ArcTan2(0.8, 0.6) * 2.0, qs[0]), solution, reference);\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9014
+ "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Convert;\n\n operation AmplitudeChange (alpha : Double, q : Qubit) : Unit is Adj+Ctl {\n Ry(2.0 * alpha, q);\n }\n\n operation CheckSolution() : Bool {\n for i in 0 .. 36 {\n let alpha = ((2.0 * PI()) * IntAsDouble(i)) / 36.0;\n let solution = register => Kata.AmplitudeChange(alpha, register[0]);\n let reference = register => AmplitudeChange(alpha, register[0]);\n let isCorrect = CheckOperationsAreEqualStrict(1, solution, reference);\n if not isCorrect {\n let precision = 3;\n Message(\"Incorrect.\");\n Message($\"The solution was incorrect for the test case alpha = {DoubleAsStringWithPrecision(alpha, precision)}.\");\n Message(\"Hint: examine the effect your solution has on the state 0.6|0〉 + 0.8|1〉 and compare it with the effect it \" +\n \"is expected to have.\");\n ShowQuantumStateComparison(1, qs => Ry(ArcTan2(0.8, 0.6) * 2.0, qs[0]), solution, reference);\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9015
9015
  },
9016
9016
  {
9017
9017
  "id": "single_qubit_gates__prepare_rotated_state__Verification.qs",
9018
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation PrepareRotatedState (alpha : Double, beta : Double, q : Qubit) : Unit is Adj+Ctl {\n let phi = ArcTan2(beta, alpha);\n Rx(2.0 * phi, 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 solution = register => Kata.PrepareRotatedState(alpha, beta, register[0]);\n let reference = register => PrepareRotatedState(alpha, beta, 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}.\");\n Message(\"Hint: examine the state prepared by your solution and compare it with the state it \" +\n \"is expected to prepare.\");\n ShowQuantumStateComparison(1, qs => (), solution, reference);\n return false;\n }\n }\n\n Message(\"Correct!\");\n Message(\"The solution was correct for all test cases.\");\n true\n }\n}\n"
9018
+ "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation PrepareRotatedState (alpha : Double, beta : Double, q : Qubit) : Unit is Adj+Ctl {\n let phi = ArcTan2(beta, alpha);\n Rx(2.0 * phi, 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 solution = register => Kata.PrepareRotatedState(alpha, beta, register[0]);\n let reference = register => PrepareRotatedState(alpha, beta, register[0]);\n let isCorrect = CheckOperationsEquivalenceOnZeroStateStrict(solution, reference, 1);\n if not isCorrect {\n let precision = 3;\n Message(\"Incorrect.\");\n Message($\"The solution was incorrect for the test case alpha = {DoubleAsStringWithPrecision(alpha, precision)}, beta = {DoubleAsStringWithPrecision(beta, precision)}.\");\n Message(\"Hint: examine the state prepared by your solution and compare it with the state it \" +\n \"is expected to prepare.\");\n ShowQuantumStateComparison(1, qs => (), solution, reference);\n return false;\n }\n }\n\n Message(\"Correct!\");\n Message(\"The solution was correct for all test cases.\");\n true\n }\n}\n"
9019
9019
  },
9020
9020
  {
9021
9021
  "id": "single_qubit_gates__prepare_arbitrary_state__Verification.qs",
9022
- "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 ShowQuantumStateComparison(1, qs => (), solution, reference);\n return false;\n }\n }\n\n Message(\"Correct!\");\n Message(\"The solution was correct for all test cases.\");\n true\n }\n}\n"
9022
+ "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 let precision = 3;\n Message(\"Incorrect.\");\n Message($\"The solution was incorrect for the test case alpha = {DoubleAsStringWithPrecision(alpha, precision)}, beta = {DoubleAsStringWithPrecision(beta, precision)}, theta = {DoubleAsStringWithPrecision(theta, precision)}.\");\n Message(\"Hint: examine the state prepared by your solution and compare it with the state it \" +\n \"is expected to prepare.\");\n ShowQuantumStateComparison(1, qs => (), solution, reference);\n return false;\n }\n }\n\n Message(\"Correct!\");\n Message(\"The solution was correct for all test cases.\");\n true\n }\n}\n"
9023
9023
  },
9024
9024
  {
9025
9025
  "id": "multi_qubit_systems__learn_basis_state_amplitudes__Verification.qs",