qsharp-lang 1.10.3-dev → 1.11.1

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 (302) hide show
  1. package/dist/katas-content.generated.js +377 -377
  2. package/dist/katas-content.generated.md.js +377 -377
  3. package/dist/samples.generated.js +12 -12
  4. package/docs/Microsoft.Quantum.Core/Length.md +4 -4
  5. package/docs/Microsoft.Quantum.Core/Repeated.md +4 -4
  6. package/docs/Microsoft.Quantum.Core/index.md +3 -3
  7. package/docs/Std.Arithmetic/AddLE.md +1 -1
  8. package/docs/Std.Arithmetic/ApplyIfEqualL.md +1 -1
  9. package/docs/Std.Arithmetic/ApplyIfEqualLE.md +1 -1
  10. package/docs/Std.Arithmetic/ApplyIfGreaterL.md +1 -1
  11. package/docs/Std.Arithmetic/ApplyIfGreaterLE.md +1 -1
  12. package/docs/Std.Arithmetic/ApplyIfGreaterOrEqualL.md +1 -1
  13. package/docs/Std.Arithmetic/ApplyIfGreaterOrEqualLE.md +1 -1
  14. package/docs/Std.Arithmetic/ApplyIfLessL.md +1 -1
  15. package/docs/Std.Arithmetic/ApplyIfLessLE.md +1 -1
  16. package/docs/Std.Arithmetic/ApplyIfLessOrEqualL.md +1 -1
  17. package/docs/Std.Arithmetic/ApplyIfLessOrEqualLE.md +1 -1
  18. package/docs/Std.Arithmetic/FourierTDIncByLE.md +1 -1
  19. package/docs/Std.Arithmetic/IncByI.md +1 -1
  20. package/docs/Std.Arithmetic/IncByIUsingIncByLE.md +1 -1
  21. package/docs/Std.Arithmetic/IncByL.md +1 -1
  22. package/docs/Std.Arithmetic/IncByLE.md +1 -1
  23. package/docs/Std.Arithmetic/IncByLEUsingAddLE.md +1 -1
  24. package/docs/Std.Arithmetic/IncByLUsingIncByLE.md +1 -1
  25. package/docs/Std.Arithmetic/LookAheadDKRSAddLE.md +1 -1
  26. package/docs/Std.Arithmetic/MAJ.md +1 -1
  27. package/docs/Std.Arithmetic/ReflectAboutInteger.md +1 -1
  28. package/docs/Std.Arithmetic/RippleCarryCGAddLE.md +1 -1
  29. package/docs/Std.Arithmetic/RippleCarryCGIncByLE.md +1 -1
  30. package/docs/Std.Arithmetic/RippleCarryTTKIncByLE.md +1 -1
  31. package/docs/Std.Arithmetic/index.md +1 -1
  32. package/docs/Std.Arrays/All.md +1 -1
  33. package/docs/Std.Arrays/Any.md +1 -1
  34. package/docs/Std.Arrays/Chunks.md +1 -1
  35. package/docs/Std.Arrays/CircularlyShifted.md +1 -1
  36. package/docs/Std.Arrays/ColumnAt.md +3 -3
  37. package/docs/Std.Arrays/Count.md +1 -1
  38. package/docs/Std.Arrays/Diagonal.md +2 -2
  39. package/docs/Std.Arrays/DrawMany.md +2 -2
  40. package/docs/Std.Arrays/Enumerated.md +1 -1
  41. package/docs/Std.Arrays/Excluding.md +1 -1
  42. package/docs/Std.Arrays/Filtered.md +1 -1
  43. package/docs/Std.Arrays/FlatMapped.md +1 -1
  44. package/docs/Std.Arrays/Flattened.md +1 -1
  45. package/docs/Std.Arrays/Fold.md +1 -1
  46. package/docs/Std.Arrays/ForEach.md +2 -2
  47. package/docs/Std.Arrays/Head.md +1 -1
  48. package/docs/Std.Arrays/HeadAndRest.md +1 -1
  49. package/docs/Std.Arrays/IndexOf.md +1 -1
  50. package/docs/Std.Arrays/IndexRange.md +1 -1
  51. package/docs/Std.Arrays/Interleaved.md +1 -1
  52. package/docs/Std.Arrays/IsEmpty.md +1 -1
  53. package/docs/Std.Arrays/IsRectangularArray.md +2 -2
  54. package/docs/Std.Arrays/IsSorted.md +1 -1
  55. package/docs/Std.Arrays/IsSquareArray.md +2 -2
  56. package/docs/Std.Arrays/Mapped.md +2 -2
  57. package/docs/Std.Arrays/MappedByIndex.md +2 -2
  58. package/docs/Std.Arrays/MappedOverRange.md +2 -2
  59. package/docs/Std.Arrays/Most.md +1 -1
  60. package/docs/Std.Arrays/MostAndTail.md +1 -1
  61. package/docs/Std.Arrays/Padded.md +1 -1
  62. package/docs/Std.Arrays/Partitioned.md +1 -1
  63. package/docs/Std.Arrays/Rest.md +1 -1
  64. package/docs/Std.Arrays/Reversed.md +1 -1
  65. package/docs/Std.Arrays/SequenceI.md +1 -1
  66. package/docs/Std.Arrays/SequenceL.md +1 -1
  67. package/docs/Std.Arrays/Sorted.md +1 -1
  68. package/docs/Std.Arrays/Subarray.md +1 -1
  69. package/docs/Std.Arrays/Swapped.md +1 -1
  70. package/docs/Std.Arrays/Tail.md +1 -1
  71. package/docs/Std.Arrays/Transposed.md +1 -1
  72. package/docs/Std.Arrays/Unzipped.md +2 -2
  73. package/docs/Std.Arrays/Where.md +1 -1
  74. package/docs/Std.Arrays/Windows.md +1 -1
  75. package/docs/Std.Arrays/Zipped.md +2 -2
  76. package/docs/Std.Arrays/index.md +1 -1
  77. package/docs/Std.Canon/ApplyCNOTChain.md +1 -1
  78. package/docs/Std.Canon/ApplyControlledOnBitString.md +1 -1
  79. package/docs/Std.Canon/ApplyControlledOnInt.md +1 -1
  80. package/docs/Std.Canon/ApplyP.md +1 -1
  81. package/docs/Std.Canon/ApplyPauli.md +1 -1
  82. package/docs/Std.Canon/ApplyPauliFromBitString.md +1 -1
  83. package/docs/Std.Canon/ApplyPauliFromInt.md +1 -1
  84. package/docs/Std.Canon/ApplyQFT.md +1 -1
  85. package/docs/Std.Canon/ApplyToEach.md +1 -1
  86. package/docs/Std.Canon/ApplyToEachA.md +2 -2
  87. package/docs/Std.Canon/ApplyToEachC.md +2 -2
  88. package/docs/Std.Canon/ApplyToEachCA.md +2 -2
  89. package/docs/Std.Canon/ApplyXorInPlace.md +1 -1
  90. package/docs/Std.Canon/ApplyXorInPlaceL.md +1 -1
  91. package/docs/Std.Canon/CX.md +1 -1
  92. package/docs/Std.Canon/CY.md +1 -1
  93. package/docs/Std.Canon/CZ.md +1 -1
  94. package/docs/Std.Canon/Fst.md +1 -1
  95. package/docs/Std.Canon/Relabel.md +1 -1
  96. package/docs/Std.Canon/Snd.md +1 -1
  97. package/docs/Std.Canon/SwapReverseRegister.md +1 -1
  98. package/docs/Std.Canon/index.md +1 -1
  99. package/docs/Std.Convert/BigIntAsBoolArray.md +1 -1
  100. package/docs/Std.Convert/BoolArrayAsBigInt.md +1 -1
  101. package/docs/Std.Convert/BoolArrayAsInt.md +1 -1
  102. package/docs/Std.Convert/BoolArrayAsResultArray.md +1 -1
  103. package/docs/Std.Convert/BoolAsResult.md +1 -1
  104. package/docs/Std.Convert/ComplexAsComplexPolar.md +1 -1
  105. package/docs/Std.Convert/ComplexPolarAsComplex.md +1 -1
  106. package/docs/Std.Convert/DoubleAsStringWithPrecision.md +1 -1
  107. package/docs/Std.Convert/IntAsBigInt.md +1 -1
  108. package/docs/Std.Convert/IntAsBoolArray.md +1 -1
  109. package/docs/Std.Convert/IntAsDouble.md +1 -1
  110. package/docs/Std.Convert/ResultArrayAsBoolArray.md +1 -1
  111. package/docs/Std.Convert/ResultArrayAsInt.md +1 -1
  112. package/docs/Std.Convert/ResultAsBool.md +1 -1
  113. package/docs/Std.Convert/index.md +1 -1
  114. package/docs/Std.Core/Length.md +1 -1
  115. package/docs/Std.Core/Repeated.md +1 -1
  116. package/docs/Std.Core/index.md +1 -1
  117. package/docs/Std.Diagnostics/ApplyIdleNoise.md +1 -1
  118. package/docs/Std.Diagnostics/BitFlipNoise.md +1 -1
  119. package/docs/Std.Diagnostics/CheckAllZero.md +1 -1
  120. package/docs/Std.Diagnostics/CheckOperationsAreEqual.md +1 -1
  121. package/docs/Std.Diagnostics/CheckZero.md +1 -1
  122. package/docs/Std.Diagnostics/ConfigurePauliNoise.md +1 -1
  123. package/docs/Std.Diagnostics/DepolarizingNoise.md +1 -1
  124. package/docs/Std.Diagnostics/DumpMachine.md +1 -1
  125. package/docs/Std.Diagnostics/DumpOperation.md +1 -1
  126. package/docs/Std.Diagnostics/DumpRegister.md +1 -1
  127. package/docs/Std.Diagnostics/Fact.md +1 -1
  128. package/docs/Std.Diagnostics/NoNoise.md +1 -1
  129. package/docs/Std.Diagnostics/PhaseFlipNoise.md +1 -1
  130. package/docs/Std.Diagnostics/StartCountingFunction.md +1 -1
  131. package/docs/Std.Diagnostics/StartCountingOperation.md +1 -1
  132. package/docs/Std.Diagnostics/StartCountingQubits.md +1 -1
  133. package/docs/Std.Diagnostics/StopCountingFunction.md +1 -1
  134. package/docs/Std.Diagnostics/StopCountingOperation.md +1 -1
  135. package/docs/Std.Diagnostics/StopCountingQubits.md +1 -1
  136. package/docs/Std.Diagnostics/index.md +1 -1
  137. package/docs/Std.Intrinsic/AND.md +1 -1
  138. package/docs/Std.Intrinsic/CCNOT.md +1 -1
  139. package/docs/Std.Intrinsic/CNOT.md +1 -1
  140. package/docs/Std.Intrinsic/Exp.md +1 -1
  141. package/docs/Std.Intrinsic/H.md +1 -1
  142. package/docs/Std.Intrinsic/I.md +1 -1
  143. package/docs/Std.Intrinsic/M.md +1 -1
  144. package/docs/Std.Intrinsic/Measure.md +1 -1
  145. package/docs/Std.Intrinsic/Message.md +1 -1
  146. package/docs/Std.Intrinsic/R.md +1 -1
  147. package/docs/Std.Intrinsic/R1.md +1 -1
  148. package/docs/Std.Intrinsic/R1Frac.md +4 -4
  149. package/docs/Std.Intrinsic/RFrac.md +4 -4
  150. package/docs/Std.Intrinsic/Reset.md +1 -1
  151. package/docs/Std.Intrinsic/ResetAll.md +1 -1
  152. package/docs/Std.Intrinsic/Rx.md +1 -1
  153. package/docs/Std.Intrinsic/Rxx.md +1 -1
  154. package/docs/Std.Intrinsic/Ry.md +1 -1
  155. package/docs/Std.Intrinsic/Ryy.md +1 -1
  156. package/docs/Std.Intrinsic/Rz.md +1 -1
  157. package/docs/Std.Intrinsic/Rzz.md +1 -1
  158. package/docs/Std.Intrinsic/S.md +1 -1
  159. package/docs/Std.Intrinsic/SWAP.md +1 -1
  160. package/docs/Std.Intrinsic/T.md +1 -1
  161. package/docs/Std.Intrinsic/X.md +1 -1
  162. package/docs/Std.Intrinsic/Y.md +1 -1
  163. package/docs/Std.Intrinsic/Z.md +1 -1
  164. package/docs/Std.Intrinsic/index.md +3 -3
  165. package/docs/Std.Logical/Xor.md +1 -1
  166. package/docs/Std.Logical/index.md +1 -1
  167. package/docs/Std.Math/AbsComplex.md +1 -1
  168. package/docs/Std.Math/AbsComplexPolar.md +1 -1
  169. package/docs/Std.Math/AbsD.md +1 -1
  170. package/docs/Std.Math/AbsI.md +1 -1
  171. package/docs/Std.Math/AbsL.md +1 -1
  172. package/docs/Std.Math/AbsSquaredComplex.md +1 -1
  173. package/docs/Std.Math/AbsSquaredComplexPolar.md +1 -1
  174. package/docs/Std.Math/ApproximateFactorial.md +3 -3
  175. package/docs/Std.Math/ArcCos.md +1 -1
  176. package/docs/Std.Math/ArcCosh.md +1 -1
  177. package/docs/Std.Math/ArcSin.md +1 -1
  178. package/docs/Std.Math/ArcSinh.md +1 -1
  179. package/docs/Std.Math/ArcTan.md +1 -1
  180. package/docs/Std.Math/ArcTan2.md +1 -1
  181. package/docs/Std.Math/ArcTanh.md +1 -1
  182. package/docs/Std.Math/ArgComplex.md +1 -1
  183. package/docs/Std.Math/ArgComplexPolar.md +1 -1
  184. package/docs/Std.Math/Binom.md +1 -1
  185. package/docs/Std.Math/BitSizeI.md +1 -1
  186. package/docs/Std.Math/BitSizeL.md +1 -1
  187. package/docs/Std.Math/Ceiling.md +1 -1
  188. package/docs/Std.Math/Complex.md +1 -1
  189. package/docs/Std.Math/ComplexPolar.md +1 -1
  190. package/docs/Std.Math/ContinuedFractionConvergentI.md +1 -1
  191. package/docs/Std.Math/ContinuedFractionConvergentL.md +1 -1
  192. package/docs/Std.Math/Cos.md +1 -1
  193. package/docs/Std.Math/Cosh.md +1 -1
  194. package/docs/Std.Math/DivRemI.md +1 -1
  195. package/docs/Std.Math/DivRemL.md +1 -1
  196. package/docs/Std.Math/DividedByC.md +1 -1
  197. package/docs/Std.Math/DividedByCP.md +1 -1
  198. package/docs/Std.Math/E.md +2 -2
  199. package/docs/Std.Math/ExpModI.md +1 -1
  200. package/docs/Std.Math/ExpModL.md +1 -1
  201. package/docs/Std.Math/ExtendedGreatestCommonDivisorI.md +1 -1
  202. package/docs/Std.Math/ExtendedGreatestCommonDivisorL.md +1 -1
  203. package/docs/Std.Math/FactorialI.md +4 -4
  204. package/docs/Std.Math/FactorialL.md +3 -3
  205. package/docs/Std.Math/Floor.md +1 -1
  206. package/docs/Std.Math/GreatestCommonDivisorI.md +1 -1
  207. package/docs/Std.Math/GreatestCommonDivisorL.md +1 -1
  208. package/docs/Std.Math/HammingWeightI.md +1 -1
  209. package/docs/Std.Math/InverseModI.md +1 -1
  210. package/docs/Std.Math/InverseModL.md +1 -1
  211. package/docs/Std.Math/IsCoprimeI.md +1 -1
  212. package/docs/Std.Math/IsCoprimeL.md +1 -1
  213. package/docs/Std.Math/IsInfinite.md +2 -2
  214. package/docs/Std.Math/IsNaN.md +1 -1
  215. package/docs/Std.Math/LargestFixedPoint.md +1 -1
  216. package/docs/Std.Math/Lg.md +1 -1
  217. package/docs/Std.Math/Log.md +1 -1
  218. package/docs/Std.Math/Log10.md +1 -1
  219. package/docs/Std.Math/LogFactorialD.md +4 -4
  220. package/docs/Std.Math/LogGammaD.md +1 -1
  221. package/docs/Std.Math/LogOf2.md +1 -1
  222. package/docs/Std.Math/Max.md +1 -1
  223. package/docs/Std.Math/MaxD.md +1 -1
  224. package/docs/Std.Math/MaxI.md +1 -1
  225. package/docs/Std.Math/MaxL.md +1 -1
  226. package/docs/Std.Math/Min.md +1 -1
  227. package/docs/Std.Math/MinD.md +1 -1
  228. package/docs/Std.Math/MinI.md +1 -1
  229. package/docs/Std.Math/MinL.md +1 -1
  230. package/docs/Std.Math/MinusC.md +1 -1
  231. package/docs/Std.Math/MinusCP.md +1 -1
  232. package/docs/Std.Math/ModulusI.md +1 -1
  233. package/docs/Std.Math/ModulusL.md +1 -1
  234. package/docs/Std.Math/NegationC.md +1 -1
  235. package/docs/Std.Math/NegationCP.md +1 -1
  236. package/docs/Std.Math/PI.md +2 -2
  237. package/docs/Std.Math/PNorm.md +1 -1
  238. package/docs/Std.Math/PNormalized.md +1 -1
  239. package/docs/Std.Math/PlusC.md +1 -1
  240. package/docs/Std.Math/PlusCP.md +1 -1
  241. package/docs/Std.Math/PowC.md +1 -1
  242. package/docs/Std.Math/PowCP.md +1 -1
  243. package/docs/Std.Math/RealMod.md +1 -1
  244. package/docs/Std.Math/Round.md +1 -1
  245. package/docs/Std.Math/SignD.md +1 -1
  246. package/docs/Std.Math/SignI.md +1 -1
  247. package/docs/Std.Math/SignL.md +1 -1
  248. package/docs/Std.Math/Sin.md +1 -1
  249. package/docs/Std.Math/Sinh.md +1 -1
  250. package/docs/Std.Math/SmallestFixedPoint.md +1 -1
  251. package/docs/Std.Math/Sqrt.md +1 -1
  252. package/docs/Std.Math/SquaredNorm.md +1 -1
  253. package/docs/Std.Math/Tan.md +1 -1
  254. package/docs/Std.Math/Tanh.md +1 -1
  255. package/docs/Std.Math/TimesC.md +1 -1
  256. package/docs/Std.Math/TimesCP.md +1 -1
  257. package/docs/Std.Math/TrailingZeroCountI.md +1 -1
  258. package/docs/Std.Math/TrailingZeroCountL.md +1 -1
  259. package/docs/Std.Math/Truncate.md +1 -1
  260. package/docs/Std.Math/index.md +1 -1
  261. package/docs/Std.Measurement/MResetEachZ.md +2 -2
  262. package/docs/Std.Measurement/MResetX.md +1 -1
  263. package/docs/Std.Measurement/MResetY.md +1 -1
  264. package/docs/Std.Measurement/MResetZ.md +1 -1
  265. package/docs/Std.Measurement/MeasureAllZ.md +2 -2
  266. package/docs/Std.Measurement/MeasureEachZ.md +3 -3
  267. package/docs/Std.Measurement/MeasureInteger.md +1 -1
  268. package/docs/Std.Measurement/index.md +1 -1
  269. package/docs/Std.Random/DrawRandomBool.md +1 -1
  270. package/docs/Std.Random/DrawRandomDouble.md +1 -1
  271. package/docs/Std.Random/DrawRandomInt.md +1 -1
  272. package/docs/Std.Random/index.md +1 -1
  273. package/docs/Std.Range/IsRangeEmpty.md +1 -1
  274. package/docs/Std.Range/RangeEnd.md +1 -1
  275. package/docs/Std.Range/RangeReverse.md +1 -1
  276. package/docs/Std.Range/RangeStart.md +1 -1
  277. package/docs/Std.Range/RangeStep.md +1 -1
  278. package/docs/Std.Range/index.md +1 -1
  279. package/docs/Std.ResourceEstimation/AccountForEstimates.md +1 -1
  280. package/docs/Std.ResourceEstimation/AuxQubitCount.md +1 -1
  281. package/docs/Std.ResourceEstimation/BeginEstimateCaching.md +1 -1
  282. package/docs/Std.ResourceEstimation/BeginRepeatEstimates.md +1 -1
  283. package/docs/Std.ResourceEstimation/CczCount.md +1 -1
  284. package/docs/Std.ResourceEstimation/EndEstimateCaching.md +1 -1
  285. package/docs/Std.ResourceEstimation/EndRepeatEstimates.md +1 -1
  286. package/docs/Std.ResourceEstimation/MeasurementCount.md +1 -1
  287. package/docs/Std.ResourceEstimation/PSSPCLayout.md +1 -1
  288. package/docs/Std.ResourceEstimation/RepeatEstimates.md +1 -1
  289. package/docs/Std.ResourceEstimation/RotationCount.md +1 -1
  290. package/docs/Std.ResourceEstimation/RotationDepth.md +1 -1
  291. package/docs/Std.ResourceEstimation/SingleVariant.md +1 -1
  292. package/docs/Std.ResourceEstimation/TCount.md +1 -1
  293. package/docs/Std.ResourceEstimation/index.md +1 -1
  294. package/docs/Std.StatePreparation/ApproximatelyPreparePureStateCP.md +1 -1
  295. package/docs/Std.StatePreparation/PreparePureStateD.md +2 -2
  296. package/docs/Std.StatePreparation/index.md +1 -1
  297. package/docs/Std.TableLookup/Select.md +1 -1
  298. package/docs/Std.TableLookup/index.md +1 -1
  299. package/docs/index.md +1 -1
  300. package/lib/node/qsc_wasm_bg.wasm +0 -0
  301. package/lib/web/qsc_wasm_bg.wasm +0 -0
  302. package/package.json +1 -1
@@ -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 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"
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>Std.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
  },
@@ -118,14 +118,14 @@ export default {
118
118
  "title": "Add Complex Numbers",
119
119
  "description": {
120
120
  "type": "text-content",
121
- "content": "<p><strong>Inputs:</strong></p>\n<ol>\n<li>A complex number $x = a + bi$.</li>\n<li>A complex number $y = c + di$.</li>\n</ol>\n<p><strong>Goal:</strong>\nReturn the sum of $x$ and $y$ as a complex number.</p>\n<details>\n <summary><b>Need a hint?</b></summary>\n<p>Adding complex numbers is just like adding polynomials. Add components of the same type - add the real part to the real part, add the imaginary part to the imaginary part.</p>\n<p>A video explanation of adding complex numbers can be found <a href=\"https://www.youtube.com/watch?v=SfbjqVyQljk\">here</a>.</p>\n</details>\n<blockquote>\n<p>Q# function <code>PlusC</code> from <code>Microsoft.Quantum.Math</code> namespace adds two complex numbers. For educational purposes, try to do this task by hand.</p>\n</blockquote>\n"
121
+ "content": "<p><strong>Inputs:</strong></p>\n<ol>\n<li>A complex number $x = a + bi$.</li>\n<li>A complex number $y = c + di$.</li>\n</ol>\n<p><strong>Goal:</strong>\nReturn the sum of $x$ and $y$ as a complex number.</p>\n<details>\n <summary><b>Need a hint?</b></summary>\n<p>Adding complex numbers is just like adding polynomials. Add components of the same type - add the real part to the real part, add the imaginary part to the imaginary part.</p>\n<p>A video explanation of adding complex numbers can be found <a href=\"https://www.youtube.com/watch?v=SfbjqVyQljk\">here</a>.</p>\n</details>\n<blockquote>\n<p>Q# function <code>PlusC</code> from <code>Std.Math</code> namespace adds two complex numbers. For educational purposes, try to do this task by hand.</p>\n</blockquote>\n"
122
122
  },
123
123
  "sourceIds": [
124
124
  "complex_arithmetic__complex_addition__Verification.qs",
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.Real, x.Imag);\n // Implement your solution here...\n return Complex(0., 0.);\n }\n}\n",
128
+ "placeholderCode": "namespace Kata {\n import Std.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 Complex(x.Real + y.Real, x.Imag + y.Imag)\n }\n}\n"
139
+ "code": "namespace Kata {\n import Std.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
  }
@@ -147,14 +147,14 @@ export default {
147
147
  "title": "Multiply Complex Numbers",
148
148
  "description": {
149
149
  "type": "text-content",
150
- "content": "<p><strong>Inputs:</strong></p>\n<ol>\n<li>A complex number $x = a + bi$.</li>\n<li>A complex number $y = c + di$.</li>\n</ol>\n<p><strong>Goal:</strong>\nReturn the product of $x$ and $y$ as a complex number.</p>\n<details>\n <summary><b>Need a hint?</b></summary>\n<p>Multiplying complex numbers is just like multiplying polynomials. Distribute one of the complex numbers:\n$$(a + bi)(c + di) = a(c + di) + bi(c + di)$$\nThen multiply through, keeping in mind that $i^2=-1$, and group the real and imaginary terms together.</p>\n<p>A video explanation of multiplying complex numbers can be found <a href=\"https://www.youtube.com/watch?v=cWn6g8Qqvs4\">here</a>.</p>\n</details>\n<blockquote>\n<p>Q# function <code>TimesC</code> from <code>Microsoft.Quantum.Math</code> namespace multiplies two complex numbers. For educational purposes, try to do this task by hand.</p>\n</blockquote>\n"
150
+ "content": "<p><strong>Inputs:</strong></p>\n<ol>\n<li>A complex number $x = a + bi$.</li>\n<li>A complex number $y = c + di$.</li>\n</ol>\n<p><strong>Goal:</strong>\nReturn the product of $x$ and $y$ as a complex number.</p>\n<details>\n <summary><b>Need a hint?</b></summary>\n<p>Multiplying complex numbers is just like multiplying polynomials. Distribute one of the complex numbers:\n$$(a + bi)(c + di) = a(c + di) + bi(c + di)$$\nThen multiply through, keeping in mind that $i^2=-1$, and group the real and imaginary terms together.</p>\n<p>A video explanation of multiplying complex numbers can be found <a href=\"https://www.youtube.com/watch?v=cWn6g8Qqvs4\">here</a>.</p>\n</details>\n<blockquote>\n<p>Q# function <code>TimesC</code> from <code>Std.Math</code> namespace multiplies two complex numbers. For educational purposes, try to do this task by hand.</p>\n</blockquote>\n"
151
151
  },
152
152
  "sourceIds": [
153
153
  "complex_arithmetic__complex_multiplication__Verification.qs",
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 import Std.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.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"
168
+ "code": "namespace Kata {\n import Std.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 import Std.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 function ComplexConjugate(x : Complex) : Complex {\n Complex(x.Real, -x.Imag)\n }\n}\n"
208
+ "code": "namespace Kata {\n import Std.Math.*;\n\n function ComplexConjugate(x : Complex) : Complex {\n Complex(x.Real, -x.Imag)\n }\n}\n"
209
209
  }
210
210
  ]
211
211
  }
@@ -227,14 +227,14 @@ export default {
227
227
  "title": "Divide Complex Numbers",
228
228
  "description": {
229
229
  "type": "text-content",
230
- "content": "<p><strong>Inputs:</strong></p>\n<ol>\n<li>A complex number $x = a + bi$.</li>\n<li>A complex number $y = c + di \\neq 0$.</li>\n</ol>\n<p><strong>Goal:</strong> Return the result of the division $\\frac{x}{y} = \\frac{a + bi}{c + di}$.</p>\n<details>\n <summary><b>Need a hint?</b></summary>\n<p>A video explanation of complex division can be found <a href=\"https://www.youtube.com/watch?v=Z8j5RDOibV4\">here</a>.</p>\n</details>\n<blockquote>\n<p>Q# function <code>DividedByC</code> from <code>Microsoft.Quantum.Math</code> namespace divides two complex numbers. For educational purposes, try to do this task by hand.</p>\n</blockquote>\n"
230
+ "content": "<p><strong>Inputs:</strong></p>\n<ol>\n<li>A complex number $x = a + bi$.</li>\n<li>A complex number $y = c + di \\neq 0$.</li>\n</ol>\n<p><strong>Goal:</strong> Return the result of the division $\\frac{x}{y} = \\frac{a + bi}{c + di}$.</p>\n<details>\n <summary><b>Need a hint?</b></summary>\n<p>A video explanation of complex division can be found <a href=\"https://www.youtube.com/watch?v=Z8j5RDOibV4\">here</a>.</p>\n</details>\n<blockquote>\n<p>Q# function <code>DividedByC</code> from <code>Std.Math</code> namespace divides two complex numbers. For educational purposes, try to do this task by hand.</p>\n</blockquote>\n"
231
231
  },
232
232
  "sourceIds": [
233
233
  "complex_arithmetic__complex_division__Verification.qs",
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 import Std.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.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"
248
+ "code": "namespace Kata {\n import Std.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
  }
@@ -267,14 +267,14 @@ export default {
267
267
  "title": "Find Modulus",
268
268
  "description": {
269
269
  "type": "text-content",
270
- "content": "<p><strong>Input:</strong> A complex number $x = a + bi$.</p>\n<p><strong>Goal:</strong> Return the modulus of this number, $|x|$.</p>\n<details>\n <summary><b>Need a hint?</b></summary>\n<p>A video explanation of absolute value and complex numbers can be found <a href=\"https://www.youtube.com/watch?v=FwuPXchH2rA\">here</a>.</p>\n</details>\n<blockquote>\n<p>Q# function <code>AbsComplex</code> from <code>Microsoft.Quantum.Math</code> namespace gets the absolute value of a complex number. For educational purposes, try to do this task by hand.</p>\n</blockquote>\n"
270
+ "content": "<p><strong>Input:</strong> A complex number $x = a + bi$.</p>\n<p><strong>Goal:</strong> Return the modulus of this number, $|x|$.</p>\n<details>\n <summary><b>Need a hint?</b></summary>\n<p>A video explanation of absolute value and complex numbers can be found <a href=\"https://www.youtube.com/watch?v=FwuPXchH2rA\">here</a>.</p>\n</details>\n<blockquote>\n<p>Q# function <code>AbsComplex</code> from <code>Std.Math</code> namespace gets the absolute value of a complex number. For educational purposes, try to do this task by hand.</p>\n</blockquote>\n"
271
271
  },
272
272
  "sourceIds": [
273
273
  "complex_arithmetic__complex_modulus__Verification.qs",
274
274
  "KatasLibrary.qs",
275
275
  "complex_arithmetic__Common.qs"
276
276
  ],
277
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n \n function ComplexModulus(x : Complex) : Double {\n // Implement your solution here...\n return 0.;\n }\n}\n",
277
+ "placeholderCode": "namespace Kata {\n import Std.Math.*;\n\n function ComplexModulus(x : Complex) : Double {\n // Implement your solution here...\n return 0.;\n }\n}\n",
278
278
  "explainedSolution": {
279
279
  "type": "explained-solution",
280
280
  "items": [
@@ -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.Real, x.Imag);\n return Sqrt(a * a + b * b);\n }\n}\n"
288
+ "code": "namespace Kata {\n import Std.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
  }
@@ -307,14 +307,14 @@ export default {
307
307
  "title": "Find Complex Exponent",
308
308
  "description": {
309
309
  "type": "text-content",
310
- "content": "<p><strong>Input:</strong> A complex number $x = a + bi$.</p>\n<p><strong>Goal:</strong> Return the complex number $e^x = e^{a + bi}$.</p>\n<details>\n <summary><b>Need a hint?</b></summary>\n Q# namespace <code>Microsoft.Quantum.Math</code> includes a function <code>E()</code> that returns the value of the constant $e$.\n</details>\n"
310
+ "content": "<p><strong>Input:</strong> A complex number $x = a + bi$.</p>\n<p><strong>Goal:</strong> Return the complex number $e^x = e^{a + bi}$.</p>\n<details>\n <summary><b>Need a hint?</b></summary>\n Q# namespace <code>Std.Math</code> includes a function <code>E()</code> that returns the value of the constant $e$.\n</details>\n"
311
311
  },
312
312
  "sourceIds": [
313
313
  "complex_arithmetic__complex_exponents__Verification.qs",
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.);\n }\n}\n",
317
+ "placeholderCode": "namespace Kata {\n import Std.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": [
@@ -325,7 +325,7 @@ export default {
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 Complex(E()^x.Real * Cos(x.Imag), E()^x.Real * Sin(x.Imag))\n }\n}\n"
328
+ "code": "namespace Kata {\n import Std.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
  }
@@ -336,14 +336,14 @@ export default {
336
336
  "title": "Find Complex Power of Real Number",
337
337
  "description": {
338
338
  "type": "text-content",
339
- "content": "<p><strong>Inputs:</strong></p>\n<ol>\n<li>A non-negative real number $r$.</li>\n<li>A complex number $x = a + bi$.</li>\n</ol>\n<p><strong>Goal:</strong> Return the complex number $r^x = r^{a + bi}$.</p>\n<details>\n <summary><b>Need a hint?</b></summary>\n You can use the fact that $r = e^{\\ln r}$ to convert exponent bases. Remember though, $\\ln r$ is only defined for positive numbers - make sure to check for $r = 0$ separately!\n<p>Q# namespace <code>Microsoft.Quantum.Math</code> includes useful functions <code>Log()</code>, <code>Sin()</code>, and <code>Cos()</code>.</p>\n</details>\n"
339
+ "content": "<p><strong>Inputs:</strong></p>\n<ol>\n<li>A non-negative real number $r$.</li>\n<li>A complex number $x = a + bi$.</li>\n</ol>\n<p><strong>Goal:</strong> Return the complex number $r^x = r^{a + bi}$.</p>\n<details>\n <summary><b>Need a hint?</b></summary>\n You can use the fact that $r = e^{\\ln r}$ to convert exponent bases. Remember though, $\\ln r$ is only defined for positive numbers - make sure to check for $r = 0$ separately!\n<p>Q# namespace <code>Std.Math</code> includes useful functions <code>Log()</code>, <code>Sin()</code>, and <code>Cos()</code>.</p>\n</details>\n"
340
340
  },
341
341
  "sourceIds": [
342
342
  "complex_arithmetic__complex_powers_real__Verification.qs",
343
343
  "KatasLibrary.qs",
344
344
  "complex_arithmetic__Common.qs"
345
345
  ],
346
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function ComplexExpReal (r : Double, x : Complex) : Complex {\n // Implement your solution here...\n return Complex(0.0, 0.0);\n }\n}\n",
346
+ "placeholderCode": "namespace Kata {\n import Std.Math.*;\n\n function ComplexExpReal(r : Double, x : Complex) : Complex {\n // Implement your solution here...\n return Complex(0.0, 0.0);\n }\n}\n",
347
347
  "explainedSolution": {
348
348
  "type": "explained-solution",
349
349
  "items": [
@@ -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.);\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"
357
+ "code": "namespace Kata {\n import Std.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 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"
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>Std.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
  },
@@ -376,14 +376,14 @@ export default {
376
376
  "title": "Convert Cartesian to Polar",
377
377
  "description": {
378
378
  "type": "text-content",
379
- "content": "<p><strong>Input:</strong>\nA complex number $x = a + bi$.</p>\n<p><strong>Goal:</strong>\nReturn the polar representation of $x = re^{i\\theta}$, that is, the distance from origin $r$ and phase $\\theta$ as a <code>ComplexPolar</code>.</p>\n<ul>\n<li>$r$ should be non-negative: $r \\geq 0$</li>\n<li>$\\theta$ should be between $-\\pi$ and $\\pi$: $-\\pi &lt; \\theta \\leq \\pi$</li>\n</ul>\n<details>\n <summary><b>Need a hint?</b></summary>\n<p>A video explanation of this conversion can be found <a href=\"https://www.youtube.com/watch?v=8RasCV_Lggg\">here</a>.</p>\n<p>Q# namespace <code>Microsoft.Quantum.Math</code> includes a useful function <code>ArcTan2()</code>.</p>\n</details>\n<blockquote>\n<p>Q# function <code>ComplexAsComplexPolar</code> from <code>Microsoft.Quantum.Math</code> namespace converts a complex number of type <code>Complex</code> to a complex number of type <code>ComplexPolar</code>. For educational purposes, try to do this task by hand.</p>\n</blockquote>\n"
379
+ "content": "<p><strong>Input:</strong>\nA complex number $x = a + bi$.</p>\n<p><strong>Goal:</strong>\nReturn the polar representation of $x = re^{i\\theta}$, that is, the distance from origin $r$ and phase $\\theta$ as a <code>ComplexPolar</code>.</p>\n<ul>\n<li>$r$ should be non-negative: $r \\geq 0$</li>\n<li>$\\theta$ should be between $-\\pi$ and $\\pi$: $-\\pi &lt; \\theta \\leq \\pi$</li>\n</ul>\n<details>\n <summary><b>Need a hint?</b></summary>\n<p>A video explanation of this conversion can be found <a href=\"https://www.youtube.com/watch?v=8RasCV_Lggg\">here</a>.</p>\n<p>Q# namespace <code>Std.Math</code> includes a useful function <code>ArcTan2()</code>.</p>\n</details>\n<blockquote>\n<p>Q# function <code>ComplexAsComplexPolar</code> from <code>Std.Math</code> namespace converts a complex number of type <code>Complex</code> to a complex number of type <code>ComplexPolar</code>. For educational purposes, try to do this task by hand.</p>\n</blockquote>\n"
380
380
  },
381
381
  "sourceIds": [
382
382
  "complex_arithmetic__cartesian_to_polar__Verification.qs",
383
383
  "KatasLibrary.qs",
384
384
  "complex_arithmetic__Common.qs"
385
385
  ],
386
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n \n function ComplexToComplexPolar(x : Complex) : ComplexPolar {\n // Implement your solution here...\n return ComplexPolar(0., 0.);\n }\n}\n",
386
+ "placeholderCode": "namespace Kata {\n import Std.Math.*;\n\n function ComplexToComplexPolar(x : Complex) : ComplexPolar {\n // Implement your solution here...\n return ComplexPolar(0., 0.);\n }\n}\n",
387
387
  "explainedSolution": {
388
388
  "type": "explained-solution",
389
389
  "items": [
@@ -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.Real, x.Imag);\n return ComplexPolar(Sqrt(a * a + b * b), ArcTan2(b, a));\n }\n}\n"
397
+ "code": "namespace Kata {\n import Std.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
  }
@@ -405,14 +405,14 @@ export default {
405
405
  "title": "Convert Polar to Cartesian",
406
406
  "description": {
407
407
  "type": "text-content",
408
- "content": "<p><strong>Input:</strong>\nA complex number in polar form $x = re^{i\\theta}$.</p>\n<p><strong>Goal:</strong>\nReturn the Cartesian representation of $x = a + bi$ as a <code>Complex</code>.</p>\n<details>\n <summary><b>Need a hint?</b></summary>\n<p>A video explanation of this conversion can be found <a href=\"https://www.youtube.com/watch?v=auywa7dydAk\">here</a>.</p>\n</details>\n<blockquote>\n<p>Q# function <code>ComplexPolarAsComplex</code> from <code>Microsoft.Quantum.Math</code> namespace converts a complex number of type <code>ComplexPolar</code> to a complex number of type <code>Complex</code>. For educational purposes, try to do this task by hand.</p>\n</blockquote>\n"
408
+ "content": "<p><strong>Input:</strong>\nA complex number in polar form $x = re^{i\\theta}$.</p>\n<p><strong>Goal:</strong>\nReturn the Cartesian representation of $x = a + bi$ as a <code>Complex</code>.</p>\n<details>\n <summary><b>Need a hint?</b></summary>\n<p>A video explanation of this conversion can be found <a href=\"https://www.youtube.com/watch?v=auywa7dydAk\">here</a>.</p>\n</details>\n<blockquote>\n<p>Q# function <code>ComplexPolarAsComplex</code> from <code>Std.Math</code> namespace converts a complex number of type <code>ComplexPolar</code> to a complex number of type <code>Complex</code>. For educational purposes, try to do this task by hand.</p>\n</blockquote>\n"
409
409
  },
410
410
  "sourceIds": [
411
411
  "complex_arithmetic__polar_to_cartesian__Verification.qs",
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 import Std.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.Magnitude, x.Argument);\n return Complex(r * Cos(theta), r * Sin(theta));\n }\n}\n"
426
+ "code": "namespace Kata {\n import Std.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
  }
@@ -434,14 +434,14 @@ export default {
434
434
  "title": "Multiply Polar Numbers",
435
435
  "description": {
436
436
  "type": "text-content",
437
- "content": "<p><strong>Inputs:</strong></p>\n<ol>\n<li>A complex polar number $x = r_{1}e^{i\\theta_1}$\nwhere $r_{1} \\geq 0$ and $-\\pi &lt; \\theta_1 \\leq \\pi$.</li>\n<li>A complex polar number $y = r_{2}e^{i\\theta_2}$\nwhere $r_{2} \\geq 0$ and $-\\pi &lt; \\theta_2 \\leq \\pi$.</li>\n</ol>\n<p><strong>Goal:</strong>\nReturn the product of $x$ and $y$ as a complex polar number $x \\cdot y = r_{3}e^{i\\theta_3}$.</p>\n<ul>\n<li>$r_3$ should be non-negative: $r_3 \\geq 0$</li>\n<li>$\\theta_3$ should be between $-\\pi$ and $\\pi$: $-\\pi &lt; \\theta_3 \\leq \\pi$</li>\n<li>Try to avoid converting the numbers into their Cartesian form.</li>\n</ul>\n<details>\n <summary><b>Need a hint?</b></summary>\n<p>Remember, a number written in polar form already involves multiplication. What is $r_1e^{i\\theta_1} \\cdot r_2e^{i\\theta_2}$?</p>\n<p>Is the value of $\\theta$ in the product incorrect? Remember you might have to check your boundaries and adjust it to be in the range requested.</p>\n</details>\n<blockquote>\n<p>Q# function <code>TimesCP</code> from <code>Microsoft.Quantum.Math</code> namespace multiplies two complex numbers, but it doesn't normalize the argument of the resulting number. For educational purposes, try to do this task by hand.</p>\n</blockquote>\n"
437
+ "content": "<p><strong>Inputs:</strong></p>\n<ol>\n<li>A complex polar number $x = r_{1}e^{i\\theta_1}$\nwhere $r_{1} \\geq 0$ and $-\\pi &lt; \\theta_1 \\leq \\pi$.</li>\n<li>A complex polar number $y = r_{2}e^{i\\theta_2}$\nwhere $r_{2} \\geq 0$ and $-\\pi &lt; \\theta_2 \\leq \\pi$.</li>\n</ol>\n<p><strong>Goal:</strong>\nReturn the product of $x$ and $y$ as a complex polar number $x \\cdot y = r_{3}e^{i\\theta_3}$.</p>\n<ul>\n<li>$r_3$ should be non-negative: $r_3 \\geq 0$</li>\n<li>$\\theta_3$ should be between $-\\pi$ and $\\pi$: $-\\pi &lt; \\theta_3 \\leq \\pi$</li>\n<li>Try to avoid converting the numbers into their Cartesian form.</li>\n</ul>\n<details>\n <summary><b>Need a hint?</b></summary>\n<p>Remember, a number written in polar form already involves multiplication. What is $r_1e^{i\\theta_1} \\cdot r_2e^{i\\theta_2}$?</p>\n<p>Is the value of $\\theta$ in the product incorrect? Remember you might have to check your boundaries and adjust it to be in the range requested.</p>\n</details>\n<blockquote>\n<p>Q# function <code>TimesCP</code> from <code>Std.Math</code> namespace multiplies two complex numbers, but it doesn't normalize the argument of the resulting number. For educational purposes, try to do this task by hand.</p>\n</blockquote>\n"
438
438
  },
439
439
  "sourceIds": [
440
440
  "complex_arithmetic__polar_multiplication__Verification.qs",
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 import Std.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 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"
455
+ "code": "namespace Kata {\n import Std.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
  }
@@ -721,7 +721,7 @@ export default {
721
721
  "KatasLibrary.qs",
722
722
  "linear_algebra__Common.qs"
723
723
  ],
724
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function Conjugate() : Complex[][] {\n // Replace the return value with correct answer.\n return [[Complex(0., 0.), Complex(0., 0.)],\n [Complex(0., 0.), Complex(0., 0.)]];\n }\n}\n",
724
+ "placeholderCode": "namespace Kata {\n import Std.Math.*;\n\n function Conjugate() : Complex[][] {\n // Replace the return value with correct answer.\n return [\n [Complex(0., 0.), Complex(0., 0.)],\n [Complex(0., 0.), Complex(0., 0.)]\n ];\n }\n}\n",
725
725
  "explainedSolution": {
726
726
  "type": "explained-solution",
727
727
  "items": [
@@ -732,7 +732,7 @@ export default {
732
732
  {
733
733
  "type": "solution",
734
734
  "id": "linear_algebra__conjugate_solution",
735
- "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function Conjugate() : Complex[][] {\n return [[Complex(1., -5.), Complex(2., 0.)],\n [Complex(3., 6.), Complex(0., -4.)]];\n }\n}\n"
735
+ "code": "namespace Kata {\n import Std.Math.*;\n\n function Conjugate() : Complex[][] {\n return [\n [Complex(1., -5.), Complex(2., 0.)],\n [Complex(3., 6.), Complex(0., -4.)]\n ];\n }\n}\n"
736
736
  }
737
737
  ]
738
738
  }
@@ -761,7 +761,7 @@ export default {
761
761
  "KatasLibrary.qs",
762
762
  "linear_algebra__Common.qs"
763
763
  ],
764
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function MatrixAdjoint() : Complex[][] {\n // Replace the return value with correct answer.\n return [[Complex(0., 0.), Complex(0., 0.)],\n [Complex(0., 0.), Complex(0., 0.)]];\n }\n}\n",
764
+ "placeholderCode": "namespace Kata {\n import Std.Math.*;\n\n function MatrixAdjoint() : Complex[][] {\n // Replace the return value with correct answer.\n return [\n [Complex(0., 0.), Complex(0., 0.)],\n [Complex(0., 0.), Complex(0., 0.)]\n ];\n }\n}\n",
765
765
  "explainedSolution": {
766
766
  "type": "explained-solution",
767
767
  "items": [
@@ -772,7 +772,7 @@ export default {
772
772
  {
773
773
  "type": "solution",
774
774
  "id": "linear_algebra__adjoint_solution",
775
- "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function MatrixAdjoint() : Complex[][] {\n return [[Complex(1., -5.), Complex(3., 6.)],\n [Complex(2., 0.), Complex(0., -4.)]];\n }\n}\n"
775
+ "code": "namespace Kata {\n import Std.Math.*;\n\n function MatrixAdjoint() : Complex[][] {\n return [\n [Complex(1., -5.), Complex(3., 6.)],\n [Complex(2., 0.), Complex(0., -4.)]\n ];\n }\n}\n"
776
776
  }
777
777
  ]
778
778
  }
@@ -812,7 +812,7 @@ export default {
812
812
  "KatasLibrary.qs",
813
813
  "linear_algebra__Common.qs"
814
814
  ],
815
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function InnerProduct() : Complex {\n // Replace the return value with correct answer.\n return Complex(0., 0.);\n }\n}\n",
815
+ "placeholderCode": "namespace Kata {\n import Std.Math.*;\n\n function InnerProduct() : Complex {\n // Replace the return value with correct answer.\n return Complex(0., 0.);\n }\n}\n",
816
816
  "explainedSolution": {
817
817
  "type": "explained-solution",
818
818
  "items": [
@@ -823,7 +823,7 @@ export default {
823
823
  {
824
824
  "type": "solution",
825
825
  "id": "linear_algebra__inner_product_solution",
826
- "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function InnerProduct() : Complex {\n return Complex(-18., 72.);\n }\n}\n"
826
+ "code": "namespace Kata {\n import Std.Math.*;\n\n function InnerProduct() : Complex {\n return Complex(-18., 72.);\n }\n}\n"
827
827
  }
828
828
  ]
829
829
  }
@@ -841,7 +841,7 @@ export default {
841
841
  "KatasLibrary.qs",
842
842
  "linear_algebra__Common.qs"
843
843
  ],
844
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function NormalizedVector() : Complex[][] {\n // Replace the return value with correct answer.\n return [[Complex(0., 0.)],\n [Complex(0., 0.)]];\n }\n}\n",
844
+ "placeholderCode": "namespace Kata {\n import Std.Math.*;\n\n function NormalizedVector() : Complex[][] {\n // Replace the return value with correct answer.\n return [\n [Complex(0., 0.)],\n [Complex(0., 0.)]\n ];\n }\n}\n",
845
845
  "explainedSolution": {
846
846
  "type": "explained-solution",
847
847
  "items": [
@@ -852,7 +852,7 @@ export default {
852
852
  {
853
853
  "type": "solution",
854
854
  "id": "linear_algebra__normalized_vector_solution",
855
- "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function NormalizedVector() : Complex[][] {\n return [[Complex(-0.6, 0.)],\n [Complex(0., 0.8)]];\n }\n}\n"
855
+ "code": "namespace Kata {\n import Std.Math.*;\n\n function NormalizedVector() : Complex[][] {\n return [\n [Complex(-0.6, 0.)],\n [Complex(0., 0.8)]\n ];\n }\n}\n"
856
856
  }
857
857
  ]
858
858
  }
@@ -881,7 +881,7 @@ export default {
881
881
  "KatasLibrary.qs",
882
882
  "linear_algebra__Common.qs"
883
883
  ],
884
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function OuterProduct() : Complex[][] {\n // Replace the return value with correct answer.\n return [[Complex(0., 0.), Complex(0., 0.)],\n [Complex(0., 0.), Complex(0., 0.)]];\n }\n}\n",
884
+ "placeholderCode": "namespace Kata {\n import Std.Math.*;\n\n function OuterProduct() : Complex[][] {\n // Replace the return value with correct answer.\n return [\n [Complex(0., 0.), Complex(0., 0.)],\n [Complex(0., 0.), Complex(0., 0.)]\n ];\n }\n}\n",
885
885
  "explainedSolution": {
886
886
  "type": "explained-solution",
887
887
  "items": [
@@ -892,7 +892,7 @@ export default {
892
892
  {
893
893
  "type": "solution",
894
894
  "id": "linear_algebra__outer_product_solution",
895
- "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function OuterProduct() : Complex[][] {\n return [[Complex(-27., 0.), Complex(0., -6.)],\n [Complex(0., -81.), Complex(18., 0.)]];\n }\n}\n"
895
+ "code": "namespace Kata {\n import Std.Math.*;\n\n function OuterProduct() : Complex[][] {\n return [\n [Complex(-27., 0.), Complex(0., -6.)],\n [Complex(0., -81.), Complex(18., 0.)]\n ];\n }\n}\n"
896
896
  }
897
897
  ]
898
898
  }
@@ -921,7 +921,7 @@ export default {
921
921
  "KatasLibrary.qs",
922
922
  "linear_algebra__Common.qs"
923
923
  ],
924
- "placeholderCode": "namespace Kata {\n function TensorProduct() : Double[][] {\n // Replace the return value with correct answer.\n return [[0., 0., 0., 0.],\n [0., 0., 0., 0.],\n [0., 0., 0., 0.],\n [0., 0., 0., 0.]];\n }\n}\n",
924
+ "placeholderCode": "namespace Kata {\n function TensorProduct() : Double[][] {\n // Replace the return value with correct answer.\n return [\n [0., 0., 0., 0.],\n [0., 0., 0., 0.],\n [0., 0., 0., 0.],\n [0., 0., 0., 0.]\n ];\n }\n}\n",
925
925
  "explainedSolution": {
926
926
  "type": "explained-solution",
927
927
  "items": [
@@ -932,7 +932,7 @@ export default {
932
932
  {
933
933
  "type": "solution",
934
934
  "id": "linear_algebra__tensor_product_solution",
935
- "code": "namespace Kata {\n function TensorProduct() : Double[][] {\n return [[5., 6., 10., 12.],\n [7., 8., 14., 16.],\n [15., 18., 20., 24.],\n [21., 24., 28., 32.]];\n }\n}\n"
935
+ "code": "namespace Kata {\n function TensorProduct() : Double[][] {\n return [\n [5., 6., 10., 12.],\n [7., 8., 14., 16.],\n [15., 18., 20., 24.],\n [21., 24., 28., 32.]\n ];\n }\n}\n"
936
936
  }
937
937
  ]
938
938
  }
@@ -1006,12 +1006,12 @@ 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 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"
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>Std.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",
1013
1013
  "id": "qubit__single_qubit_dump_machine_demo",
1014
- "code": "namespace Kata {\n open Microsoft.Quantum.Diagnostics;\n\n @EntryPoint()\n operation RunExample() : Unit {\n // This line allocates a qubit in state |0⟩.\n use q = Qubit();\n Message(\"State |0⟩:\");\n\n // This line prints out the state of the quantum computer.\n // Since only one qubit is allocated, only its state is printed.\n DumpMachine();\n\n // This line changes the qubit from state |0⟩ to state |1⟩.\n X(q);\n\n Message(\"State |1⟩:\");\n DumpMachine();\n\n // This line changes the qubit to state |-⟩ = (1/sqrt(2))(|0⟩ - |1⟩).\n // That is, this puts the qubit into a superposition where the absolute\n // value of the probability amplitudes is 1/sqrt(2), which is\n // approximately 0.707107.\n H(q);\n\n Message(\"State |-⟩:\");\n DumpMachine();\n\n // This line changes the qubit to state |-i⟩ = (1/sqrt(2))(|0⟩ - i|1⟩).\n S(q);\n\n Message(\"State |-i⟩:\");\n DumpMachine();\n\n // This will put the qubit into an uneven superposition, where the\n // amplitudes of |0⟩ and |1⟩ have different absolute values.\n Rx(2.0, q);\n Ry(1.0, q);\n\n Message(\"Uneven superposition state:\");\n DumpMachine();\n\n // This line returns the qubit to state |0⟩, which must be done before\n // the qubit is released or otherwise a runtime error might occur.\n Reset(q);\n }\n}\n"
1014
+ "code": "namespace Kata {\n import Std.Diagnostics.*;\n\n @EntryPoint()\n operation RunExample() : Unit {\n // This line allocates a qubit in state |0⟩.\n use q = Qubit();\n Message(\"State |0⟩:\");\n\n // This line prints out the state of the quantum computer.\n // Since only one qubit is allocated, only its state is printed.\n DumpMachine();\n\n // This line changes the qubit from state |0⟩ to state |1⟩.\n X(q);\n\n Message(\"State |1⟩:\");\n DumpMachine();\n\n // This line changes the qubit to state |-⟩ = (1/sqrt(2))(|0⟩ - |1⟩).\n // That is, this puts the qubit into a superposition where the absolute\n // value of the probability amplitudes is 1/sqrt(2), which is\n // approximately 0.707107.\n H(q);\n\n Message(\"State |-⟩:\");\n DumpMachine();\n\n // This line changes the qubit to state |-i⟩ = (1/sqrt(2))(|0⟩ - i|1⟩).\n S(q);\n\n Message(\"State |-i⟩:\");\n DumpMachine();\n\n // This will put the qubit into an uneven superposition, where the\n // amplitudes of |0⟩ and |1⟩ have different absolute values.\n Rx(2.0, q);\n Ry(1.0, q);\n\n Message(\"Uneven superposition state:\");\n DumpMachine();\n\n // This line returns the qubit to state |0⟩, which must be done before\n // the qubit is released or otherwise a runtime error might occur.\n Reset(q);\n }\n}\n"
1015
1015
  },
1016
1016
  {
1017
1017
  "type": "text-content",
@@ -1042,7 +1042,7 @@ export default {
1042
1042
  {
1043
1043
  "type": "solution",
1044
1044
  "id": "qubit__learn_single_qubit_state_solution",
1045
- "code": "namespace Kata { \n open Microsoft.Quantum.Diagnostics;\n\n operation LearnSingleQubitState (q : Qubit) : (Double, Double) {\n DumpMachine(); // Only used to learn the amplitudes.\n return (0.9689, 0.2474);\n }\n}\n"
1045
+ "code": "namespace Kata {\n import Std.Diagnostics.*;\n\n operation LearnSingleQubitState(q : Qubit) : (Double, Double) {\n DumpMachine(); // Only used to learn the amplitudes.\n return (0.9689, 0.2474);\n }\n}\n"
1046
1046
  }
1047
1047
  ]
1048
1048
  }
@@ -1132,7 +1132,7 @@ export default {
1132
1132
  {
1133
1133
  "type": "example",
1134
1134
  "id": "single_qubit_gates__pauli_gates_in_qsharp_demo",
1135
- "code": "namespace Demo {\n // To use elements from a namespace, you need to use the `open` keyword to\n // access them.\n open Microsoft.Quantum.Diagnostics;\n\n @EntryPoint()\n operation PauliGatesUsage () : Unit {\n // This allocates a qubit for us to work with.\n use q = Qubit();\n\n // This will put the qubit into an uneven superposition |𝜓❭, where the\n // amplitudes of |0⟩ and |1⟩ have different absolute values.\n Ry(1.0, q);\n\n Message(\"Qubit in state |𝜓❭:\");\n DumpMachine();\n\n // Let's apply the X gate; notice how it swaps the amplitudes of the\n // |0❭ and |1❭ basis states.\n X(q);\n Message(\"Qubit in state X|𝜓❭:\");\n DumpMachine();\n\n // Applying the Z gate adds -1 relative phase to the |1❭ basis states.\n Z(q);\n Message(\"Qubit in state ZX|𝜓❭:\");\n DumpMachine();\n\n // Finally, applying the Y gate returns the qubit to its original state\n // |𝜓❭, with an extra global phase of i.\n Y(q);\n Message(\"Qubit in state YZX|𝜓❭:\");\n DumpMachine();\n\n // This returns the qubit to state |0❭.\n Reset(q);\n }\n}\n"
1135
+ "code": "namespace Demo {\n // To use elements from a namespace, you need to use the `import` keyword to\n // access them.\n import Std.Diagnostics.*;\n\n @EntryPoint()\n operation PauliGatesUsage() : Unit {\n // This allocates a qubit for us to work with.\n use q = Qubit();\n\n // This will put the qubit into an uneven superposition |𝜓❭, where the\n // amplitudes of |0⟩ and |1⟩ have different absolute values.\n Ry(1.0, q);\n\n Message(\"Qubit in state |𝜓❭:\");\n DumpMachine();\n\n // Let's apply the X gate; notice how it swaps the amplitudes of the\n // |0❭ and |1❭ basis states.\n X(q);\n Message(\"Qubit in state X|𝜓❭:\");\n DumpMachine();\n\n // Applying the Z gate adds -1 relative phase to the |1❭ basis states.\n Z(q);\n Message(\"Qubit in state ZX|𝜓❭:\");\n DumpMachine();\n\n // Finally, applying the Y gate returns the qubit to its original state\n // |𝜓❭, with an extra global phase of i.\n Y(q);\n Message(\"Qubit in state YZX|𝜓❭:\");\n DumpMachine();\n\n // This returns the qubit to state |0❭.\n Reset(q);\n }\n}\n"
1136
1136
  }
1137
1137
  ]
1138
1138
  },
@@ -1425,7 +1425,7 @@ export default {
1425
1425
  {
1426
1426
  "type": "solution",
1427
1427
  "id": "single_qubit_gates_phase_i_solution_b",
1428
- "code": "namespace Kata {\n operation PhaseFlip (q : Qubit) : Unit is Adj + Ctl {\n open Microsoft.Quantum.Math;\n R1(0.5 * PI(), q);\n }\n}"
1428
+ "code": "namespace Kata {\n operation PhaseFlip(q : Qubit) : Unit is Adj + Ctl {\n import Std.Math.*;\n R1(0.5 * PI(), q);\n }\n}\n"
1429
1429
  }
1430
1430
  ]
1431
1431
  }
@@ -1531,24 +1531,24 @@ export default {
1531
1531
  "title": "Prepare Rotated State",
1532
1532
  "description": {
1533
1533
  "type": "text-content",
1534
- "content": "<p><strong>Inputs:</strong></p>\n<ol>\n<li>Real numbers $\\alpha$ and $\\beta$ such that $\\alpha^2 + \\beta^2 = 1$.</li>\n<li>A qubit in state $\\ket{0}$.</li>\n</ol>\n<p><strong>Goal:</strong> Use a rotation gate to transform the qubit into state $\\alpha\\ket{0} -i\\beta\\ket{1}$.</p>\n<blockquote>\n<p>You'll probably need functions from the <code>Microsoft.Quantum.Math</code> namespace, specifically <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.math/arctan2\" target=\"_blank\">ArcTan2</a>.</p>\n<p>You can assign variables in Q# by using the <code>let</code> keyword: <code>let num = 3;</code> or <code>let result = Function(input);</code></p>\n</blockquote>\n"
1534
+ "content": "<p><strong>Inputs:</strong></p>\n<ol>\n<li>Real numbers $\\alpha$ and $\\beta$ such that $\\alpha^2 + \\beta^2 = 1$.</li>\n<li>A qubit in state $\\ket{0}$.</li>\n</ol>\n<p><strong>Goal:</strong> Use a rotation gate to transform the qubit into state $\\alpha\\ket{0} -i\\beta\\ket{1}$.</p>\n<blockquote>\n<p>You'll probably need functions from the <code>Std.Math</code> namespace, specifically <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.math/arctan2\" target=\"_blank\">ArcTan2</a>.</p>\n<p>You can assign variables in Q# by using the <code>let</code> keyword: <code>let num = 3;</code> or <code>let result = Function(input);</code></p>\n</blockquote>\n"
1535
1535
  },
1536
1536
  "sourceIds": [
1537
1537
  "single_qubit_gates__prepare_rotated_state__Verification.qs",
1538
1538
  "KatasLibrary.qs"
1539
1539
  ],
1540
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n operation PrepareRotatedState(alpha : Double, beta : Double, q : Qubit)\n : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}\n",
1540
+ "placeholderCode": "namespace Kata {\n import Std.Math.*;\n\n operation PrepareRotatedState(alpha : Double, beta : Double, q : Qubit) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}\n",
1541
1541
  "explainedSolution": {
1542
1542
  "type": "explained-solution",
1543
1543
  "items": [
1544
1544
  {
1545
1545
  "type": "text-content",
1546
- "content": "<p>You use the rotation gate $R_x(\\theta)$. This gate turns the state $\\ket{0}$ into $R_x(\\theta)\\ket{0} = \\cos\\frac{\\theta}{2}\\ket{0} - i\\sin\\frac{\\theta}{2}\\ket{1}$.\nThis is similar to the state you need. You just need to find an angle $\\theta$ such that $\\cos\\frac{\\theta}{2}=\\alpha$ and $\\sin\\frac{\\theta}{2}=\\beta$. You can use these two equations to solve for $\\theta$: $\\theta = 2\\arctan\\frac{\\beta}{\\alpha}$. (<em>Note: It's given that $\\alpha^2 + \\beta^2=1$</em>).\nHence the required gate is $R_x(2\\arctan\\frac{\\beta}{\\alpha})$, which in matrix form is $\\begin{bmatrix} \\alpha &amp; -i\\beta \\\\ -i\\beta &amp; \\alpha \\end{bmatrix}$.\nThis gate turns $\\ket{0} = \\begin{bmatrix} 1 \\\\ 0\\end{bmatrix}$ into $\\begin{bmatrix} \\alpha &amp; -i\\beta \\\\ -i\\beta &amp; \\alpha \\end{bmatrix} \\begin{bmatrix} 1 \\\\ 0\\end{bmatrix} = \\begin{bmatrix} \\alpha \\\\ -i\\beta \\end{bmatrix} = \\alpha\\ket{0} -i\\beta\\ket{1}$.</p>\n<blockquote>\n<p>Trigonometric functions are available in Q# via the <code>Microsoft.Quantum.Math</code> namespace. In this case, you'll need <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.math/arctan2\" target=\"_blank\">ArcTan2</a>.</p>\n</blockquote>\n"
1546
+ "content": "<p>You use the rotation gate $R_x(\\theta)$. This gate turns the state $\\ket{0}$ into $R_x(\\theta)\\ket{0} = \\cos\\frac{\\theta}{2}\\ket{0} - i\\sin\\frac{\\theta}{2}\\ket{1}$.\nThis is similar to the state you need. You just need to find an angle $\\theta$ such that $\\cos\\frac{\\theta}{2}=\\alpha$ and $\\sin\\frac{\\theta}{2}=\\beta$. You can use these two equations to solve for $\\theta$: $\\theta = 2\\arctan\\frac{\\beta}{\\alpha}$. (<em>Note: It's given that $\\alpha^2 + \\beta^2=1$</em>).\nHence the required gate is $R_x(2\\arctan\\frac{\\beta}{\\alpha})$, which in matrix form is $\\begin{bmatrix} \\alpha &amp; -i\\beta \\\\ -i\\beta &amp; \\alpha \\end{bmatrix}$.\nThis gate turns $\\ket{0} = \\begin{bmatrix} 1 \\\\ 0\\end{bmatrix}$ into $\\begin{bmatrix} \\alpha &amp; -i\\beta \\\\ -i\\beta &amp; \\alpha \\end{bmatrix} \\begin{bmatrix} 1 \\\\ 0\\end{bmatrix} = \\begin{bmatrix} \\alpha \\\\ -i\\beta \\end{bmatrix} = \\alpha\\ket{0} -i\\beta\\ket{1}$.</p>\n<blockquote>\n<p>Trigonometric functions are available in Q# via the <code>Std.Math</code> namespace. In this case, you'll need <a href=\"https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.math/arctan2\" target=\"_blank\">ArcTan2</a>.</p>\n</blockquote>\n"
1547
1547
  },
1548
1548
  {
1549
1549
  "type": "solution",
1550
1550
  "id": "single_qubit_gates__prepare_rotated_state_solution",
1551
- "code": "namespace Kata {\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"
1551
+ "code": "namespace Kata {\n import Std.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"
1552
1552
  }
1553
1553
  ]
1554
1554
  }
@@ -1565,7 +1565,7 @@ export default {
1565
1565
  "single_qubit_gates__prepare_arbitrary_state__Verification.qs",
1566
1566
  "KatasLibrary.qs"
1567
1567
  ],
1568
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n operation PrepareArbitraryState(\n alpha : Double,beta : Double, theta : Double, q : Qubit)\n : Unit is Adj+Ctl {\n // Implement your solution here...\n\n }\n}\n",
1568
+ "placeholderCode": "namespace Kata {\n import Std.Math.*;\n\n operation PrepareArbitraryState(\n alpha : Double,\n beta : Double,\n theta : Double,\n q : Qubit\n ) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}\n",
1569
1569
  "explainedSolution": {
1570
1570
  "type": "explained-solution",
1571
1571
  "items": [
@@ -1576,7 +1576,7 @@ export default {
1576
1576
  {
1577
1577
  "type": "solution",
1578
1578
  "id": "single_qubit_gates__prepare_arbitrary_state_solution",
1579
- "code": "namespace Kata {\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"
1579
+ "code": "namespace Kata {\n import Std.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"
1580
1580
  }
1581
1581
  ]
1582
1582
  }
@@ -1666,7 +1666,7 @@ export default {
1666
1666
  {
1667
1667
  "type": "example",
1668
1668
  "id": "multi_qubit_systems__multi_qubit_systems_demo",
1669
- "code": "namespace Kata {\n open Microsoft.Quantum.Diagnostics;\n\n @EntryPoint()\n operation MultiQubitSystemsDemo () : Unit {\n // This allocates an array of 2 qubits, each of them in state |0⟩.\n // The overall state of the system is |00⟩.\n use qs = Qubit[2];\n // X gate changes the first qubit into state |1⟩.\n X(qs[0]);\n Message(\"The system in now in state |10⟩:\");\n DumpMachine();\n\n // This changes the second qubit into state |+⟩ = (1/sqrt(2))(|0⟩ + |1⟩).\n H(qs[1]);\n Message(\"The system in now in state (1/sqrt(2))(|10⟩ + |11⟩):\");\n DumpMachine();\n\n // This changes the first qubit into state |-⟩ = (1/sqrt(2))(|0⟩ - |1⟩)\n H(qs[0]);\n Message(\"The system in now in state 0.5(|00⟩ + |01⟩ - |10⟩ - |11⟩):\");\n DumpMachine();\n\n // The next lines entangle the qubits (don't worry about what exactly they do for now).\n H(qs[1]);\n CNOT(qs[0], qs[1]);\n Message(\"The system in now in entangled state 0.5(|00⟩ - |11⟩):\");\n DumpMachine();\n\n // This returns the system into state |00⟩.\n ResetAll(qs);\n }\n}"
1669
+ "code": "namespace Kata {\n import Std.Diagnostics.*;\n\n @EntryPoint()\n operation MultiQubitSystemsDemo() : Unit {\n // This allocates an array of 2 qubits, each of them in state |0⟩.\n // The overall state of the system is |00⟩.\n use qs = Qubit[2];\n // X gate changes the first qubit into state |1⟩.\n X(qs[0]);\n Message(\"The system in now in state |10⟩:\");\n DumpMachine();\n\n // This changes the second qubit into state |+⟩ = (1/sqrt(2))(|0⟩ + |1⟩).\n H(qs[1]);\n Message(\"The system in now in state (1/sqrt(2))(|10⟩ + |11⟩):\");\n DumpMachine();\n\n // This changes the first qubit into state |-⟩ = (1/sqrt(2))(|0⟩ - |1⟩)\n H(qs[0]);\n Message(\"The system in now in state 0.5(|00⟩ + |01⟩ - |10⟩ - |11⟩):\");\n DumpMachine();\n\n // The next lines entangle the qubits (don't worry about what exactly they do for now).\n H(qs[1]);\n CNOT(qs[0], qs[1]);\n Message(\"The system in now in entangled state 0.5(|00⟩ - |11⟩):\");\n DumpMachine();\n\n // This returns the system into state |00⟩.\n ResetAll(qs);\n }\n}\n"
1670
1670
  },
1671
1671
  {
1672
1672
  "type": "text-content",
@@ -1697,7 +1697,7 @@ export default {
1697
1697
  {
1698
1698
  "type": "solution",
1699
1699
  "id": "multi_qubit_systems__learn_basis_state_amplitudes_solution",
1700
- "code": "namespace Kata {\n open Microsoft.Quantum.Diagnostics;\n\n operation LearnBasisStateAmplitudes(qs : Qubit[]) : (Double, Double) {\n DumpMachine(); // Only used to learn the amplitudes.\n return (0.3390, 0.3821);\n }\n}\n"
1700
+ "code": "namespace Kata {\n import Std.Diagnostics.*;\n\n operation LearnBasisStateAmplitudes(qs : Qubit[]) : (Double, Double) {\n DumpMachine(); // Only used to learn the amplitudes.\n return (0.3390, 0.3821);\n }\n}\n"
1701
1701
  }
1702
1702
  ]
1703
1703
  }
@@ -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 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"
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>Std.Intrinsic</code> namespace.\nIt's imported 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 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"
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>Std.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",
@@ -2622,7 +2622,7 @@ export default {
2622
2622
  {
2623
2623
  "type": "solution",
2624
2624
  "id": "preparing_states__all_bell_states_solution_b",
2625
- "code": "namespace Kata {\n open Microsoft.Quantum.Convert;\n\n operation AllBellStates (qs : Qubit[], index : Int) : Unit is Adj + Ctl {\n let bitmask = IntAsBoolArray(index, 2);\n \n if bitmask[0] {\n X(qs[0]);\n }\n\n if bitmask[1] {\n X(qs[1]);\n }\n\n H(qs[0]);\n CNOT(qs[0], qs[1]);\n }\n}\n"
2625
+ "code": "namespace Kata {\n import Std.Convert.*;\n\n operation AllBellStates(qs : Qubit[], index : Int) : Unit is Adj + Ctl {\n let bitmask = IntAsBoolArray(index, 2);\n\n if bitmask[0] {\n X(qs[0]);\n }\n\n if bitmask[1] {\n X(qs[1]);\n }\n\n H(qs[0]);\n CNOT(qs[0], qs[1]);\n }\n}\n"
2626
2626
  }
2627
2627
  ]
2628
2628
  }
@@ -2640,7 +2640,7 @@ export default {
2640
2640
  "KatasLibrary.qs",
2641
2641
  "preparing_states__Common.qs"
2642
2642
  ],
2643
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n\n operation GHZ_State (qs : Qubit[]) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}\n",
2643
+ "placeholderCode": "namespace Kata {\n import Std.Arrays.*;\n\n operation GHZ_State(qs : Qubit[]) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}\n",
2644
2644
  "explainedSolution": {
2645
2645
  "type": "explained-solution",
2646
2646
  "items": [
@@ -2651,7 +2651,7 @@ export default {
2651
2651
  {
2652
2652
  "type": "solution",
2653
2653
  "id": "preparing_states__ghz_solution",
2654
- "code": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n \n operation GHZ_State (qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n \n // Library function Rest returns all array elements except for the first one\n for q in Rest(qs) {\n CNOT(qs[0], q);\n }\n }\n}\n"
2654
+ "code": "namespace Kata {\n import Std.Arrays.*;\n\n operation GHZ_State(qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n\n // Library function Rest returns all array elements except for the first one\n for q in Rest(qs) {\n CNOT(qs[0], q);\n }\n }\n}\n"
2655
2655
  }
2656
2656
  ]
2657
2657
  }
@@ -2814,7 +2814,7 @@ export default {
2814
2814
  {
2815
2815
  "type": "solution",
2816
2816
  "id": "preparing_states__four_bitstrings_solution_b",
2817
- "code": "namespace Kata {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Math;\n\n operation FourBitstringSuperposition (qs : Qubit[], bits : Bool[][]) : Unit {\n FourBitstringSuperposition_Recursive([], qs, bits);\n }\n\n operation FourBitstringSuperposition_Recursive (currentBitString : Bool[], qs : Qubit[], bits : Bool[][]) : Unit {\n // an array of bit strings whose columns we are considering begin with |0⟩\n mutable zeroLeads = [];\n // an array of bit strings whose columns we are considering begin with |1⟩\n mutable oneLeads = [];\n // the number of bit strings we're considering\n let rows = Length(bits);\n // the current position we're considering\n let currentIndex = Length(currentBitString);\n\n if rows >= 1 and currentIndex < Length(qs) {\n // figure out what percentage of the bits should be |0⟩\n for row in 0 .. rows - 1 {\n if bits[row][currentIndex] {\n set oneLeads = oneLeads + [bits[row]];\n } else {\n set zeroLeads = zeroLeads + [bits[row]];\n }\n }\n // rotate the qubit to adjust coefficients based on the previous bit string\n // for the first path through, when the bit string has zero length,\n // the Controlled version of the rotation will perform a regular rotation\n let theta = ArcCos(Sqrt(IntAsDouble(Length(zeroLeads)) / IntAsDouble(rows)));\n ApplyControlledOnBitString(currentBitString, Ry, qs[0 .. currentIndex - 1],\n (2.0 * theta, qs[currentIndex]));\n\n // call state preparation recursively based on the bit strings so far\n FourBitstringSuperposition_Recursive(currentBitString + [false], qs, zeroLeads);\n FourBitstringSuperposition_Recursive(currentBitString + [true], qs, oneLeads);\n }\n }\n}\n"
2817
+ "code": "namespace Kata {\n import Std.Convert.*;\n import Std.Math.*;\n\n operation FourBitstringSuperposition(qs : Qubit[], bits : Bool[][]) : Unit {\n FourBitstringSuperposition_Recursive([], qs, bits);\n }\n\n operation FourBitstringSuperposition_Recursive(currentBitString : Bool[], qs : Qubit[], bits : Bool[][]) : Unit {\n // an array of bit strings whose columns we are considering begin with |0⟩\n mutable zeroLeads = [];\n // an array of bit strings whose columns we are considering begin with |1⟩\n mutable oneLeads = [];\n // the number of bit strings we're considering\n let rows = Length(bits);\n // the current position we're considering\n let currentIndex = Length(currentBitString);\n\n if rows >= 1 and currentIndex < Length(qs) {\n // figure out what percentage of the bits should be |0⟩\n for row in 0..rows - 1 {\n if bits[row][currentIndex] {\n set oneLeads = oneLeads + [bits[row]];\n } else {\n set zeroLeads = zeroLeads + [bits[row]];\n }\n }\n // rotate the qubit to adjust coefficients based on the previous bit string\n // for the first path through, when the bit string has zero length,\n // the Controlled version of the rotation will perform a regular rotation\n let theta = ArcCos(Sqrt(IntAsDouble(Length(zeroLeads)) / IntAsDouble(rows)));\n ApplyControlledOnBitString(\n currentBitString,\n Ry,\n qs[0..currentIndex - 1],\n (2.0 * theta, qs[currentIndex])\n );\n\n // call state preparation recursively based on the bit strings so far\n FourBitstringSuperposition_Recursive(currentBitString + [false], qs, zeroLeads);\n FourBitstringSuperposition_Recursive(currentBitString + [true], qs, oneLeads);\n }\n }\n}\n"
2818
2818
  }
2819
2819
  ]
2820
2820
  }
@@ -2968,7 +2968,7 @@ export default {
2968
2968
  {
2969
2969
  "type": "solution",
2970
2970
  "id": "preparing_states__three_states_two_qubits_solution_b",
2971
- "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n operation ThreeStates_TwoQubits (qs : Qubit[]) : Unit is Adj + Ctl {\n let theta = ArcSin(1.0 / Sqrt(3.0));\n Ry(2.0 * theta, qs[0]);\n ApplyControlledOnInt(0, H,[qs[0]], qs[1]);\n }\n}\n"
2971
+ "code": "namespace Kata {\n import Std.Math.*;\n\n operation ThreeStates_TwoQubits(qs : Qubit[]) : Unit is Adj + Ctl {\n let theta = ArcSin(1.0 / Sqrt(3.0));\n Ry(2.0 * theta, qs[0]);\n ApplyControlledOnInt(0, H, [qs[0]], qs[1]);\n }\n}\n"
2972
2972
  }
2973
2973
  ]
2974
2974
  }
@@ -2986,7 +2986,7 @@ export default {
2986
2986
  "KatasLibrary.qs",
2987
2987
  "preparing_states__Common.qs"
2988
2988
  ],
2989
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n operation ThreeStates_TwoQubits_Phases (qs : Qubit[]) : Unit {\n // Implement your solution here...\n\n }\n\n // You might find this helper operation from an earlier task useful.\n operation ThreeStates_TwoQubits (qs : Qubit[]) : Unit is Adj {\n let theta = ArcSin(1.0 / Sqrt(3.0));\n Ry(2.0 * theta, qs[0]);\n ApplyControlledOnInt(0, H, [qs[0]], qs[1]);\n }\n}\n",
2989
+ "placeholderCode": "namespace Kata {\n import Std.Math.*;\n\n operation ThreeStates_TwoQubits_Phases(qs : Qubit[]) : Unit {\n // Implement your solution here...\n\n }\n\n // You might find this helper operation from an earlier task useful.\n operation ThreeStates_TwoQubits(qs : Qubit[]) : Unit is Adj {\n let theta = ArcSin(1.0 / Sqrt(3.0));\n Ry(2.0 * theta, qs[0]);\n ApplyControlledOnInt(0, H, [qs[0]], qs[1]);\n }\n}\n",
2990
2990
  "explainedSolution": {
2991
2991
  "type": "explained-solution",
2992
2992
  "items": [
@@ -2997,7 +2997,7 @@ export default {
2997
2997
  {
2998
2998
  "type": "solution",
2999
2999
  "id": "preparing_states__three_states_two_qubits_phases_solution",
3000
- "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n operation ThreeStates_TwoQubits_Phases (qs : Qubit[]) : Unit {\n // First create (|00⟩ + |01⟩ + |10⟩) / sqrt(3) state\n ThreeStates_TwoQubits(qs);\n\n R1(4.0 * PI() / 3.0, qs[0]);\n R1(2.0 * PI() / 3.0, qs[1]);\n }\n\n operation ThreeStates_TwoQubits (qs : Qubit[]) : Unit is Adj {\n let theta = ArcSin(1.0 / Sqrt(3.0));\n Ry(2.0 * theta, qs[0]);\n ApplyControlledOnInt(0, H, [qs[0]], qs[1]);\n }\n}\n"
3000
+ "code": "namespace Kata {\n import Std.Math.*;\n\n operation ThreeStates_TwoQubits_Phases(qs : Qubit[]) : Unit {\n // First create (|00⟩ + |01⟩ + |10⟩) / sqrt(3) state\n ThreeStates_TwoQubits(qs);\n\n R1(4.0 * PI() / 3.0, qs[0]);\n R1(2.0 * PI() / 3.0, qs[1]);\n }\n\n operation ThreeStates_TwoQubits(qs : Qubit[]) : Unit is Adj {\n let theta = ArcSin(1.0 / Sqrt(3.0));\n Ry(2.0 * theta, qs[0]);\n ApplyControlledOnInt(0, H, [qs[0]], qs[1]);\n }\n}\n"
3001
3001
  }
3002
3002
  ]
3003
3003
  }
@@ -3026,7 +3026,7 @@ export default {
3026
3026
  {
3027
3027
  "type": "solution",
3028
3028
  "id": "preparing_states__hardy_state_solution",
3029
- "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n operation Hardy_State (qs : Qubit[]) : Unit is Adj {\n let theta = ArcCos(Sqrt(10.0/12.0));\n Ry(2.0 * theta, qs[0]);\n\n ApplyControlledOnInt(0, Ry, [qs[0]], (2.0 * ArcCos(3.0 / Sqrt(10.0)), qs[1]));\n ApplyControlledOnInt(1, Ry, [qs[0]], (2.0 * PI() / 4.0, qs[1]));\n }\n}\n"
3029
+ "code": "namespace Kata {\n import Std.Math.*;\n\n operation Hardy_State(qs : Qubit[]) : Unit is Adj {\n let theta = ArcCos(Sqrt(10.0 / 12.0));\n Ry(2.0 * theta, qs[0]);\n\n ApplyControlledOnInt(0, Ry, [qs[0]], (2.0 * ArcCos(3.0 / Sqrt(10.0)), qs[1]));\n ApplyControlledOnInt(1, Ry, [qs[0]], (2.0 * PI() / 4.0, qs[1]));\n }\n}\n"
3030
3030
  }
3031
3031
  ]
3032
3032
  }
@@ -3093,7 +3093,7 @@ export default {
3093
3093
  {
3094
3094
  "type": "solution",
3095
3095
  "id": "preparing_states__wstate_arbitrary_solution_a",
3096
- "code": "namespace Kata {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Math;\n\n operation WState_Arbitrary (qs : Qubit[]) : Unit is Adj + Ctl {\n let N = Length(qs);\n Ry(2.0 * ArcSin(Sqrt(1.0 / IntAsDouble(N))), qs[0]);\n for i in 1 .. N - 1 {\n ApplyControlledOnInt(0, Ry(2.0 * ArcSin(Sqrt(1.0 / IntAsDouble(N - i))), _), \n qs[0 .. i - 1], qs[i]);\n }\n }\n}\n"
3096
+ "code": "namespace Kata {\n import Std.Convert.*;\n import Std.Math.*;\n\n operation WState_Arbitrary(qs : Qubit[]) : Unit is Adj + Ctl {\n let N = Length(qs);\n Ry(2.0 * ArcSin(Sqrt(1.0 / IntAsDouble(N))), qs[0]);\n for i in 1..N - 1 {\n ApplyControlledOnInt(\n 0,\n Ry(2.0 * ArcSin(Sqrt(1.0 / IntAsDouble(N - i))), _),\n qs[0..i - 1],\n qs[i]\n );\n }\n }\n}\n"
3097
3097
  },
3098
3098
  {
3099
3099
  "type": "text-content",
@@ -3102,7 +3102,7 @@ export default {
3102
3102
  {
3103
3103
  "type": "solution",
3104
3104
  "id": "preparing_states__wstate_arbitrary_solution_b",
3105
- "code": "namespace Kata {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Math;\n\n operation WState_Arbitrary (qs : Qubit[]) : Unit is Adj + Ctl {\n let N = Length(qs);\n Ry(2.0 * ArcSin(Sqrt(1.0 / IntAsDouble(N))), qs[0]);\n if N > 1 {\n ApplyControlledOnInt(0, WState_Arbitrary, [qs[0]], qs[1 ...]);\n }\n }\n}\n"
3105
+ "code": "namespace Kata {\n import Std.Convert.*;\n import Std.Math.*;\n\n operation WState_Arbitrary(qs : Qubit[]) : Unit is Adj + Ctl {\n let N = Length(qs);\n Ry(2.0 * ArcSin(Sqrt(1.0 / IntAsDouble(N))), qs[0]);\n if N > 1 {\n ApplyControlledOnInt(0, WState_Arbitrary, [qs[0]], qs[1...]);\n }\n }\n}\n"
3106
3106
  },
3107
3107
  {
3108
3108
  "type": "text-content",
@@ -3168,7 +3168,7 @@ export default {
3168
3168
  {
3169
3169
  "type": "example",
3170
3170
  "id": "single_qubit_measurements__implementing_measurement_demo",
3171
- "code": "namespace Kata {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Math;\n\n @EntryPoint()\n operation SimpleMeasurementDemo() : Unit {\n use q = Qubit();\n // Prepare the qubit in the superposition state |𝜓❭ = 0.6 |0❭ + 0.8 |1❭\n Ry(2.0 * ArcTan2(0.8, 0.6), q);\n Message(\"Qubit in state |𝜓❭:\");\n DumpMachine();\n\n let outcome = M(q) == One ? 1 | 0;\n Message($\"The measurement outcome is {outcome}.\");\n Message(\"Post-measurement state of the qubit:\");\n DumpMachine();\n Reset(q);\n }\n}\n"
3171
+ "code": "namespace Kata {\n import Std.Diagnostics.*;\n import Std.Math.*;\n\n @EntryPoint()\n operation SimpleMeasurementDemo() : Unit {\n use q = Qubit();\n // Prepare the qubit in the superposition state |𝜓❭ = 0.6 |0❭ + 0.8 |1❭\n Ry(2.0 * ArcTan2(0.8, 0.6), q);\n Message(\"Qubit in state |𝜓❭:\");\n DumpMachine();\n\n let outcome = M(q) == One ? 1 | 0;\n Message($\"The measurement outcome is {outcome}.\");\n Message(\"Post-measurement state of the qubit:\");\n DumpMachine();\n Reset(q);\n }\n}\n"
3172
3172
  }
3173
3173
  ]
3174
3174
  },
@@ -3184,7 +3184,7 @@ export default {
3184
3184
  {
3185
3185
  "type": "example",
3186
3186
  "id": "single_qubit_measurements__measurement_statistics_demo",
3187
- "code": "namespace Kata {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Math;\n\n @EntryPoint()\n operation MeasumentStatisticsDemo() : Unit {\n mutable countZero = 0;\n let numRuns = 100;\n use q = Qubit();\n for i in 1 .. numRuns {\n // Prepare the qubit in the superposition state |𝜓❭ = 0.6 |0❭ + 0.8 |1❭\n Ry(2.0 * ArcTan2(0.8, 0.6), q);\n\n // Measure and update the counts according to the outcomes\n if MResetZ(q) == Zero {\n set countZero += 1;\n }\n }\n let countOne = numRuns - countZero;\n\n Message($\"Simulated probability of measuring 0 is 0.{countZero}.\");\n Message($\"Theoretical probability of measuring 0 is 0.36.\");\n Message($\"Simulated probability of measuring 1 is 0.{countOne}.\");\n Message($\"Theoretical probability of measuring 1 is 0.64.\");\n }\n}\n"
3187
+ "code": "namespace Kata {\n import Std.Diagnostics.*;\n import Std.Math.*;\n\n @EntryPoint()\n operation MeasumentStatisticsDemo() : Unit {\n mutable countZero = 0;\n let numRuns = 100;\n use q = Qubit();\n for i in 1..numRuns {\n // Prepare the qubit in the superposition state |𝜓❭ = 0.6 |0❭ + 0.8 |1❭\n Ry(2.0 * ArcTan2(0.8, 0.6), q);\n\n // Measure and update the counts according to the outcomes\n if MResetZ(q) == Zero {\n set countZero += 1;\n }\n }\n let countOne = numRuns - countZero;\n\n Message($\"Simulated probability of measuring 0 is 0.{countZero}.\");\n Message($\"Theoretical probability of measuring 0 is 0.36.\");\n Message($\"Simulated probability of measuring 1 is 0.{countOne}.\");\n Message($\"Theoretical probability of measuring 1 is 0.64.\");\n }\n}\n"
3188
3188
  },
3189
3189
  {
3190
3190
  "type": "text-content",
@@ -3282,7 +3282,7 @@ export default {
3282
3282
  "single_qubit_measurements__distinguish_orthogonal_states_1__Verification.qs",
3283
3283
  "KatasLibrary.qs"
3284
3284
  ],
3285
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n operation IsQubitPsiPlus(q : Qubit) : Bool {\n // Implement your solution here...\n\n return false;\n }\n}\n",
3285
+ "placeholderCode": "namespace Kata {\n import Std.Math.*;\n\n operation IsQubitPsiPlus(q : Qubit) : Bool {\n // Implement your solution here...\n\n return false;\n }\n}\n",
3286
3286
  "explainedSolution": {
3287
3287
  "type": "explained-solution",
3288
3288
  "items": [
@@ -3293,7 +3293,7 @@ export default {
3293
3293
  {
3294
3294
  "type": "solution",
3295
3295
  "id": "single_qubit_measurements__distinguish_orthogonal_states_1_solution",
3296
- "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n operation IsQubitPsiPlus(q : Qubit) : Bool {\n Ry(-2.0 * ArcTan2(0.8, 0.6), q);\n return M(q) == Zero;\n }\n}\n"
3296
+ "code": "namespace Kata {\n import Std.Math.*;\n\n operation IsQubitPsiPlus(q : Qubit) : Bool {\n Ry(-2.0 * ArcTan2(0.8, 0.6), q);\n return M(q) == Zero;\n }\n}\n"
3297
3297
  }
3298
3298
  ]
3299
3299
  }
@@ -3395,7 +3395,7 @@ export default {
3395
3395
  {
3396
3396
  "type": "example",
3397
3397
  "id": "multi_qubit_measurements__multi_qubit_probabilities_example",
3398
- "code": "namespace Kata {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Math;\n\n @EntryPoint()\n operation CalculateProbabilities() : Unit {\n use qs = Qubit[2];\n\n Ry(2. * ArcCos(2. / 3.), qs[1]);\n Controlled Ry([qs[1]], (2. * ArcCos(1. / Sqrt(5.)), qs[0]));\n\n Message(\"The initial state of the system\");\n Message(\"(includes measurement outcome probabilities in computational basis):\");\n DumpMachine();\n\n // Change the basis\n H(qs[0]);\n H(qs[1]);\n\n Message(\"Final state of the two-qubit system:\");\n Message(\"(includes measurement outcome probabilities in Pauli X basis):\");\n DumpMachine();\n\n // Return the qubit array into state |00❭.\n ResetAll(qs);\n }\n}\n"
3398
+ "code": "namespace Kata {\n import Std.Diagnostics.*;\n import Std.Math.*;\n\n @EntryPoint()\n operation CalculateProbabilities() : Unit {\n use qs = Qubit[2];\n\n Ry(2. * ArcCos(2. / 3.), qs[1]);\n Controlled Ry([qs[1]], (2. * ArcCos(1. / Sqrt(5.)), qs[0]));\n\n Message(\"The initial state of the system\");\n Message(\"(includes measurement outcome probabilities in computational basis):\");\n DumpMachine();\n\n // Change the basis\n H(qs[0]);\n H(qs[1]);\n\n Message(\"Final state of the two-qubit system:\");\n Message(\"(includes measurement outcome probabilities in Pauli X basis):\");\n DumpMachine();\n\n // Return the qubit array into state |00❭.\n ResetAll(qs);\n }\n}\n"
3399
3399
  },
3400
3400
  {
3401
3401
  "type": "text-content",
@@ -3415,7 +3415,7 @@ export default {
3415
3415
  {
3416
3416
  "type": "example",
3417
3417
  "id": "multi_qubit_measurements__measuring_one_at_a_time",
3418
- "code": "namespace Kata {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Math;\n\n @EntryPoint()\n operation DemoBasisMeasurement() : Unit {\n let expected_probabilities = [1. / 9., 4. / 9., 0., 4. / 9.];\n\n // Set up counter array for tracking measurement outcomes.\n mutable countArray = [0, 0, 0, 0];\n\n use qs = Qubit[2];\n let numRuns = 1000;\n for i in 1 .. numRuns {\n // Prepare the starting state.\n Ry(2. * ArcCos(1. / 3.), qs[1]);\n Controlled H([qs[1]], qs[0]);\n if i == 1 {\n Message(\"The state of the system before measurement is:\");\n DumpMachine();\n }\n\n // Measure the first (most significant) qubit, then measure the second (least significant) qubit,\n // and convert the result to an integer, interpreting it as big endian binary notation.\n let result = (MResetZ(qs[0]) == One ? 1 | 0) * 2 + (MResetZ(qs[1]) == One ? 1 | 0);\n\n set countArray w/= result <- countArray[result] + 1;\n }\n\n // Obtain simulated probability of measurement for each outcome.\n mutable simulated_probabilities = [];\n for i in 0 .. 3 {\n set simulated_probabilities +=\n [IntAsDouble(countArray[i]) / IntAsDouble(numRuns)];\n }\n\n Message($\"Theoretical measurement probabilities are {expected_probabilities}\");\n Message($\"Simulated measurement probabilities are {simulated_probabilities}\");\n }\n}\n"
3418
+ "code": "namespace Kata {\n import Std.Convert.*;\n import Std.Diagnostics.*;\n import Std.Math.*;\n\n @EntryPoint()\n operation DemoBasisMeasurement() : Unit {\n let expected_probabilities = [1. / 9., 4. / 9., 0., 4. / 9.];\n\n // Set up counter array for tracking measurement outcomes.\n mutable countArray = [0, 0, 0, 0];\n\n use qs = Qubit[2];\n let numRuns = 1000;\n for i in 1..numRuns {\n // Prepare the starting state.\n Ry(2. * ArcCos(1. / 3.), qs[1]);\n Controlled H([qs[1]], qs[0]);\n if i == 1 {\n Message(\"The state of the system before measurement is:\");\n DumpMachine();\n }\n\n // Measure the first (most significant) qubit, then measure the second (least significant) qubit,\n // and convert the result to an integer, interpreting it as big endian binary notation.\n let result = (MResetZ(qs[0]) == One ? 1 | 0) * 2 + (MResetZ(qs[1]) == One ? 1 | 0);\n\n set countArray w/= result <- countArray[result] + 1;\n }\n\n // Obtain simulated probability of measurement for each outcome.\n mutable simulated_probabilities = [];\n for i in 0..3 {\n set simulated_probabilities += [IntAsDouble(countArray[i]) / IntAsDouble(numRuns)];\n }\n\n Message($\"Theoretical measurement probabilities are {expected_probabilities}\");\n Message($\"Simulated measurement probabilities are {simulated_probabilities}\");\n }\n}\n"
3419
3419
  },
3420
3420
  {
3421
3421
  "type": "text-content",
@@ -3455,7 +3455,7 @@ export default {
3455
3455
  {
3456
3456
  "type": "solution",
3457
3457
  "id": "multi_qubit_measurements__full_measurements_solution_alt",
3458
- "code": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Convert;\n\n operation BasisStateMeasurement(qs : Qubit[]) : Int {\n return ResultArrayAsInt(Reversed(MeasureEachZ(qs)));\n }\n}\n"
3458
+ "code": "namespace Kata {\n import Std.Arrays.*;\n import Std.Convert.*;\n\n operation BasisStateMeasurement(qs : Qubit[]) : Int {\n return ResultArrayAsInt(Reversed(MeasureEachZ(qs)));\n }\n}\n"
3459
3459
  }
3460
3460
  ]
3461
3461
  }
@@ -3483,7 +3483,7 @@ export default {
3483
3483
  {
3484
3484
  "type": "example",
3485
3485
  "id": "multi_qubit_measurements__partial_measurements_demo",
3486
- "code": "namespace Kata {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Math;\n\n @EntryPoint()\n operation DemoPartialMeasurement() : Unit {\n let expected_probabilities = [0.833, 0.167];\n\n let numRuns = 1000;\n mutable countArray = [0, 0];\n use qs = Qubit[2];\n for i in 1 .. numRuns {\n // Prepare the Hardy state |𝜓❭ = (1/√12)(3|00⟩ + |01⟩ + |10⟩ + |11⟩)\n Ry(2. * ArcCos(Sqrt(5. / 6.)), qs[0]);\n ApplyControlledOnInt(0, Ry, [qs[0]], (2. * ArcCos(3. / Sqrt(10.)), qs[1]));\n Controlled H([qs[0]], qs[1]);\n\n if i == 1 {\n Message(\"The state |𝜓❭ of the system before measurement is:\");\n DumpMachine();\n }\n\n // Measure the first qubit.\n let outcome = M(qs[0]) == Zero ? 0 | 1;\n set countArray w/= outcome <- countArray[outcome] + 1;\n\n if countArray[outcome] == 1 {\n // The first time the outcome is 0/1, print the system state afterwards.\n Message($\"For outcome {outcome}, the post-measurement state of the system is:\");\n DumpMachine();\n }\n ResetAll(qs);\n }\n\n // Obtain simulated probability of measurement for each outcome.\n mutable simulated_probabilities = [];\n for i in 0 .. 1 {\n set simulated_probabilities += [IntAsDouble(countArray[i]) / IntAsDouble(numRuns)];\n }\n\n Message($\"Theoretical measurement probabilities are {expected_probabilities}\");\n Message($\"Simulated measurement probabilities are {simulated_probabilities}\");\n }\n}\n"
3486
+ "code": "namespace Kata {\n import Std.Diagnostics.*;\n import Std.Convert.*;\n import Std.Math.*;\n\n @EntryPoint()\n operation DemoPartialMeasurement() : Unit {\n let expected_probabilities = [0.833, 0.167];\n\n let numRuns = 1000;\n mutable countArray = [0, 0];\n use qs = Qubit[2];\n for i in 1..numRuns {\n // Prepare the Hardy state |𝜓❭ = (1/√12)(3|00⟩ + |01⟩ + |10⟩ + |11⟩)\n Ry(2. * ArcCos(Sqrt(5. / 6.)), qs[0]);\n ApplyControlledOnInt(0, Ry, [qs[0]], (2. * ArcCos(3. / Sqrt(10.)), qs[1]));\n Controlled H([qs[0]], qs[1]);\n\n if i == 1 {\n Message(\"The state |𝜓❭ of the system before measurement is:\");\n DumpMachine();\n }\n\n // Measure the first qubit.\n let outcome = M(qs[0]) == Zero ? 0 | 1;\n set countArray w/= outcome <- countArray[outcome] + 1;\n\n if countArray[outcome] == 1 {\n // The first time the outcome is 0/1, print the system state afterwards.\n Message($\"For outcome {outcome}, the post-measurement state of the system is:\");\n DumpMachine();\n }\n ResetAll(qs);\n }\n\n // Obtain simulated probability of measurement for each outcome.\n mutable simulated_probabilities = [];\n for i in 0..1 {\n set simulated_probabilities += [IntAsDouble(countArray[i]) / IntAsDouble(numRuns)];\n }\n\n Message($\"Theoretical measurement probabilities are {expected_probabilities}\");\n Message($\"Simulated measurement probabilities are {simulated_probabilities}\");\n }\n}\n"
3487
3487
  },
3488
3488
  {
3489
3489
  "type": "text-content",
@@ -3883,7 +3883,7 @@ export default {
3883
3883
  "KatasLibrary.qs",
3884
3884
  "distinguishing_states__Common.qs"
3885
3885
  ],
3886
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Convert;\n\n operation SuperpositionOneMeasurement(qs : Qubit[], bits1 : Bool[][], bits2 : Bool[][]) : Int {\n // Implement your solution here...\n\n return -1;\n }\n}\n",
3886
+ "placeholderCode": "namespace Kata {\n import Std.Convert.*;\n\n operation SuperpositionOneMeasurement(qs : Qubit[], bits1 : Bool[][], bits2 : Bool[][]) : Int {\n // Implement your solution here...\n\n return -1;\n }\n}\n",
3887
3887
  "explainedSolution": {
3888
3888
  "type": "explained-solution",
3889
3889
  "items": [
@@ -3894,7 +3894,7 @@ export default {
3894
3894
  {
3895
3895
  "type": "solution",
3896
3896
  "id": "distinguishing_states__two_superposition_states_bit_strings_solution_one",
3897
- "code": "namespace Kata {\n open Microsoft.Quantum.Convert;\n\n function FindFirstSuperpositionDiff (bits1 : Bool[][], bits2 : Bool[][], nQubits : Int) : Int {\n for i in 0 .. nQubits - 1 {\n // count the number of 1s in i-th position in bit strings of both arrays\n mutable val1 = 0;\n mutable val2 = 0;\n for j in 0 .. Length(bits1) - 1 {\n if bits1[j][i] {\n set val1 += 1;\n }\n }\n for k in 0 .. Length(bits2) - 1 {\n if bits2[k][i] {\n set val2 += 1;\n }\n }\n if (val1 == Length(bits1) and val2 == 0) or (val1 == 0 and val2 == Length(bits2)) {\n return i;\n }\n }\n\n return -1;\n }\n\n operation SuperpositionOneMeasurement (qs : Qubit[], bits1 : Bool[][], bits2 : Bool[][]) : Int {\n let diff = FindFirstSuperpositionDiff(bits1, bits2, Length(qs));\n\n let res = ResultAsBool(M(qs[diff]));\n\n if res == bits1[0][diff] {\n return 0;\n } else {\n return 1;\n }\n }\n}\n"
3897
+ "code": "namespace Kata {\n import Std.Convert.*;\n\n function FindFirstSuperpositionDiff(bits1 : Bool[][], bits2 : Bool[][], nQubits : Int) : Int {\n for i in 0..nQubits - 1 {\n // count the number of 1s in i-th position in bit strings of both arrays\n mutable val1 = 0;\n mutable val2 = 0;\n for j in 0..Length(bits1) - 1 {\n if bits1[j][i] {\n set val1 += 1;\n }\n }\n for k in 0..Length(bits2) - 1 {\n if bits2[k][i] {\n set val2 += 1;\n }\n }\n if (val1 == Length(bits1) and val2 == 0) or (val1 == 0 and val2 == Length(bits2)) {\n return i;\n }\n }\n\n return -1;\n }\n\n operation SuperpositionOneMeasurement(qs : Qubit[], bits1 : Bool[][], bits2 : Bool[][]) : Int {\n let diff = FindFirstSuperpositionDiff(bits1, bits2, Length(qs));\n\n let res = ResultAsBool(M(qs[diff]));\n\n if res == bits1[0][diff] {\n return 0;\n } else {\n return 1;\n }\n }\n}\n"
3898
3898
  }
3899
3899
  ]
3900
3900
  }
@@ -3912,7 +3912,7 @@ export default {
3912
3912
  "KatasLibrary.qs",
3913
3913
  "distinguishing_states__Common.qs"
3914
3914
  ],
3915
- "placeholderCode": "namespace Kata{\n open Microsoft.Quantum.Convert;\n\n operation SuperpositionMeasurement (qs : Qubit[], bits1 : Bool[][], bits2 : Bool[][]) : Int {\n // Implement your solution here...\n\n return -1;\n }\n}\n",
3915
+ "placeholderCode": "namespace Kata {\n import Std.Convert.*;\n\n operation SuperpositionMeasurement(qs : Qubit[], bits1 : Bool[][], bits2 : Bool[][]) : Int {\n // Implement your solution here...\n\n return -1;\n }\n}\n",
3916
3916
  "explainedSolution": {
3917
3917
  "type": "explained-solution",
3918
3918
  "items": [
@@ -3923,7 +3923,7 @@ export default {
3923
3923
  {
3924
3924
  "type": "solution",
3925
3925
  "id": "distinguishing_states__two_superposition_states_bit_strings_solution",
3926
- "code": "namespace Kata{\n open Microsoft.Quantum.Convert;\n\n operation SuperpositionMeasurement (qs : Qubit[], bits1 : Bool[][], bits2 : Bool[][]) : Int {\n let measuredState = MeasureInteger(qs);\n for s in bits1 {\n if BoolArrayAsInt(s) == measuredState {\n return 0;\n }\n }\n\n return 1;\n }\n}\n"
3926
+ "code": "namespace Kata {\n import Std.Convert.*;\n\n operation SuperpositionMeasurement(qs : Qubit[], bits1 : Bool[][], bits2 : Bool[][]) : Int {\n let measuredState = MeasureInteger(qs);\n for s in bits1 {\n if BoolArrayAsInt(s) == measuredState {\n return 0;\n }\n }\n\n return 1;\n }\n}\n"
3927
3927
  }
3928
3928
  ]
3929
3929
  }
@@ -4095,7 +4095,7 @@ export default {
4095
4095
  "KatasLibrary.qs",
4096
4096
  "distinguishing_states__Common.qs"
4097
4097
  ],
4098
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Measurement;\n operation ThreeQubitMeasurement(qs : Qubit[]) : Int {\n // Implement your solution here...\n\n return -1;\n }\n\n // You might find this helper operation from the task\n // \"W State on Arbitrary Number of Qubits\" from the \"Preparing Quantum States\" kata useful.\n operation WState_Arbitrary(qs : Qubit[]) : Unit is Adj + Ctl {\n let N = Length(qs);\n\n if N == 1 {\n X(qs[0]);\n } else {\n let theta = ArcSin(1.0 / Sqrt(IntAsDouble(N)));\n Ry(2.0 * theta, qs[0]);\n X(qs[0]);\n Controlled WState_Arbitrary(qs[0..0], qs[1..N - 1]);\n X(qs[0]);\n }\n }\n}\n",
4098
+ "placeholderCode": "namespace Kata {\n import Std.Convert.*;\n import Std.Math.*;\n import Std.Measurement.*;\n operation ThreeQubitMeasurement(qs : Qubit[]) : Int {\n // Implement your solution here...\n\n return -1;\n }\n\n // You might find this helper operation from the task\n // \"W State on Arbitrary Number of Qubits\" from the \"Preparing Quantum States\" kata useful.\n operation WState_Arbitrary(qs : Qubit[]) : Unit is Adj + Ctl {\n let N = Length(qs);\n\n if N == 1 {\n X(qs[0]);\n } else {\n let theta = ArcSin(1.0 / Sqrt(IntAsDouble(N)));\n Ry(2.0 * theta, qs[0]);\n X(qs[0]);\n Controlled WState_Arbitrary(qs[0..0], qs[1..N - 1]);\n X(qs[0]);\n }\n }\n}\n",
4099
4099
  "explainedSolution": {
4100
4100
  "type": "explained-solution",
4101
4101
  "items": [
@@ -4106,7 +4106,7 @@ export default {
4106
4106
  {
4107
4107
  "type": "solution",
4108
4108
  "id": "distinguishing_states__two_orthogonal_three_qubit_solution",
4109
- "code": "namespace Kata {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Measurement;\n\n operation ThreeQubitMeasurement(qs : Qubit[]) : Int {\n R1(-2.0 * PI() / 3.0, qs[1]);\n R1(-4.0 * PI() / 3.0, qs[2]);\n\n Adjoint WState_Arbitrary(qs);\n\n return MeasureInteger(qs) == 0 ? 0 | 1;\n }\n\n operation WState_Arbitrary(qs : Qubit[]) : Unit is Adj + Ctl {\n let N = Length(qs);\n\n if N == 1 {\n X(qs[0]);\n } else {\n let theta = ArcSin(1.0 / Sqrt(IntAsDouble(N)));\n Ry(2.0 * theta, qs[0]);\n\n X(qs[0]);\n Controlled WState_Arbitrary(qs[0..0], qs[1..N - 1]);\n X(qs[0]);\n }\n }\n}\n"
4109
+ "code": "namespace Kata {\n import Std.Convert.*;\n import Std.Math.*;\n import Std.Measurement.*;\n\n operation ThreeQubitMeasurement(qs : Qubit[]) : Int {\n R1(-2.0 * PI() / 3.0, qs[1]);\n R1(-4.0 * PI() / 3.0, qs[2]);\n\n Adjoint WState_Arbitrary(qs);\n\n return MeasureInteger(qs) == 0 ? 0 | 1;\n }\n\n operation WState_Arbitrary(qs : Qubit[]) : Unit is Adj + Ctl {\n let N = Length(qs);\n\n if N == 1 {\n X(qs[0]);\n } else {\n let theta = ArcSin(1.0 / Sqrt(IntAsDouble(N)));\n Ry(2.0 * theta, qs[0]);\n\n X(qs[0]);\n Controlled WState_Arbitrary(qs[0..0], qs[1..N - 1]);\n X(qs[0]);\n }\n }\n}\n"
4110
4110
  }
4111
4111
  ]
4112
4112
  }
@@ -4146,7 +4146,7 @@ export default {
4146
4146
  {
4147
4147
  "type": "solution",
4148
4148
  "id": "measurements_nonorthogonal_states_solution",
4149
- "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n \n operation IsQubitZeroOrPlus (q : Qubit) : Bool {\n Ry(0.25 * PI(), q);\n return M(q) == Zero;\n }\n}\n"
4149
+ "code": "namespace Kata {\n import Std.Math.*;\n\n operation IsQubitZeroOrPlus(q : Qubit) : Bool {\n Ry(0.25 * PI(), q);\n return M(q) == Zero;\n }\n}\n"
4150
4150
  }
4151
4151
  ]
4152
4152
  }
@@ -4175,7 +4175,7 @@ export default {
4175
4175
  {
4176
4176
  "type": "solution",
4177
4177
  "id": "distinguishing_states__zero_plus_inc_solution",
4178
- "code": "namespace Kata {\n open Microsoft.Quantum.Random;\n operation IsQubitZeroPlusOrInconclusive(q : Qubit) : Int {\n // Pick a random basis\n let basis = DrawRandomInt(0, 1);\n if basis == 0 {\n // use standard basis\n let result = M(q);\n // result is One only if the state was |+⟩\n return result == One ? 1 | -1;\n }\n else {\n // use Hadamard basis\n H(q);\n let result = M(q);\n // result is One only if the state was |0⟩\n return result == One ? 0 | -1;\n }\n }\n}\n"
4178
+ "code": "namespace Kata {\n import Std.Random.*;\n operation IsQubitZeroPlusOrInconclusive(q : Qubit) : Int {\n // Pick a random basis\n let basis = DrawRandomInt(0, 1);\n if basis == 0 {\n // use standard basis\n let result = M(q);\n // result is One only if the state was |+⟩\n return result == One ? 1 | -1;\n } else {\n // use Hadamard basis\n H(q);\n let result = M(q);\n // result is One only if the state was |0⟩\n return result == One ? 0 | -1;\n }\n }\n}\n"
4179
4179
  }
4180
4180
  ]
4181
4181
  }
@@ -4204,7 +4204,7 @@ export default {
4204
4204
  {
4205
4205
  "type": "solution",
4206
4206
  "id": "distinguishing_states__peres_wooters_game_solution",
4207
- "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n operation IsQubitNotInABC (q : Qubit) : Int {\n let alpha = ArcCos(Sqrt(2.0 / 3.0));\n\n use a = Qubit();\n Z(q);\n CNOT(a, q);\n Controlled H([q], a);\n S(a);\n X(q);\n\n ApplyControlledOnInt(0, Ry, [a], (-2.0 * alpha, q));\n CNOT(a, q);\n Controlled H([q], a);\n CNOT(a, q);\n\n let res0 = MResetZ(a);\n let res1 = M(q);\n\n if (res0 == Zero and res1 == Zero) {\n return 0;\n }\n elif (res0 == One and res1 == Zero) {\n return 1;\n }\n elif (res0 == Zero and res1 == One) {\n return 2;\n }\n else {\n // this should never occur\n return 3;\n }\n }\n}"
4207
+ "code": "namespace Kata {\n import Std.Math.*;\n operation IsQubitNotInABC(q : Qubit) : Int {\n let alpha = ArcCos(Sqrt(2.0 / 3.0));\n\n use a = Qubit();\n Z(q);\n CNOT(a, q);\n Controlled H([q], a);\n S(a);\n X(q);\n\n ApplyControlledOnInt(0, Ry, [a], (-2.0 * alpha, q));\n CNOT(a, q);\n Controlled H([q], a);\n CNOT(a, q);\n\n let res0 = MResetZ(a);\n let res1 = M(q);\n\n if (res0 == Zero and res1 == Zero) {\n return 0;\n } elif (res0 == One and res1 == Zero) {\n return 1;\n } elif (res0 == Zero and res1 == One) {\n return 2;\n } else {\n // this should never occur\n return 3;\n }\n }\n}\n"
4208
4208
  }
4209
4209
  ]
4210
4210
  }
@@ -4407,7 +4407,7 @@ export default {
4407
4407
  "KatasLibrary.qs",
4408
4408
  "distinguishing_unitaries__Common.qs"
4409
4409
  ],
4410
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n \n operation DistinguishRzFromR1 (unitary : ((Double, Qubit) => Unit is Adj + Ctl)) : Int {\n // Implement your solution here...\n\n return -1;\n }\n}\n",
4410
+ "placeholderCode": "namespace Kata {\n import Std.Math.*;\n\n operation DistinguishRzFromR1(unitary : ((Double, Qubit) => Unit is Adj + Ctl)) : Int {\n // Implement your solution here...\n\n return -1;\n }\n}\n",
4411
4411
  "explainedSolution": {
4412
4412
  "type": "explained-solution",
4413
4413
  "items": [
@@ -4418,7 +4418,7 @@ export default {
4418
4418
  {
4419
4419
  "type": "solution",
4420
4420
  "id": "distinguishing_unitaries__rz_r1_solution",
4421
- "code": "namespace Kata {\n open Microsoft.Quantum.Math; \n operation DistinguishRzFromR1 (unitary : ((Double, Qubit) => Unit is Adj+Ctl)) : Int {\n use qs = Qubit[2];\n H(qs[0]);\n Controlled unitary(qs[0..0], (2.0 * PI(), qs[1]));\n return MResetX(qs[0]) == Zero ? 1 | 0;\n }\n}\n"
4421
+ "code": "namespace Kata {\n import Std.Math.*;\n operation DistinguishRzFromR1(unitary : ((Double, Qubit) => Unit is Adj + Ctl)) : Int {\n use qs = Qubit[2];\n H(qs[0]);\n Controlled unitary(qs[0..0], (2.0 * PI(), qs[1]));\n return MResetX(qs[0]) == Zero ? 1 | 0;\n }\n}\n"
4422
4422
  }
4423
4423
  ]
4424
4424
  }
@@ -4523,7 +4523,7 @@ export default {
4523
4523
  "KatasLibrary.qs",
4524
4524
  "distinguishing_unitaries__Common.qs"
4525
4525
  ],
4526
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Math;\n \n operation DistinguishRzFromRy (theta : Double, unitary : (Qubit => Unit is Adj + Ctl)) : Int {\n // Implement your solution here...\n\n return -1;\n }\n}\n",
4526
+ "placeholderCode": "namespace Kata {\n import Std.Convert.*;\n import Std.Math.*;\n\n operation DistinguishRzFromRy(theta : Double, unitary : (Qubit => Unit is Adj + Ctl)) : Int {\n // Implement your solution here...\n\n return -1;\n }\n}\n",
4527
4527
  "explainedSolution": {
4528
4528
  "type": "explained-solution",
4529
4529
  "items": [
@@ -4534,7 +4534,7 @@ export default {
4534
4534
  {
4535
4535
  "type": "solution",
4536
4536
  "id": "distinguishing_unitaries__rz_ry_solution",
4537
- "code": "namespace Kata {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Math;\n\n function ComputeRepetitions(angle : Double, offset : Int, accuracy : Double) : Int {\n mutable pifactor = 0;\n while (true) {\n let pimultiple = PI() * IntAsDouble(2 * pifactor + offset);\n let times = Round(pimultiple / angle);\n if AbsD(pimultiple - (IntAsDouble(times) * angle)) / PI() < accuracy {\n return times;\n }\n set pifactor += 1;\n }\n return 0;\n }\n\n operation DistinguishRzFromRy (theta : Double, unitary : (Qubit => Unit is Adj+Ctl)) : Int {\n use q = Qubit();\n let times = ComputeRepetitions(theta, 1, 0.1);\n mutable attempt = 1;\n mutable measuredOne = false;\n repeat {\n for _ in 1 .. times {\n unitary(q);\n }\n // for Rz, we'll never venture away from |0⟩ state, so as soon as we got |1⟩ we know it's not Rz\n if MResetZ(q) == One {\n set measuredOne = true;\n }\n // if we try several times and still only get |0⟩s, chances are that it is Rz\n } until (attempt == 4 or measuredOne) \n fixup {\n set attempt += 1;\n }\n return measuredOne ? 1 | 0;\n }\n}\n"
4537
+ "code": "namespace Kata {\n import Std.Convert.*;\n import Std.Math.*;\n\n function ComputeRepetitions(angle : Double, offset : Int, accuracy : Double) : Int {\n mutable pifactor = 0;\n while (true) {\n let pimultiple = PI() * IntAsDouble(2 * pifactor + offset);\n let times = Round(pimultiple / angle);\n if AbsD(pimultiple - (IntAsDouble(times) * angle)) / PI() < accuracy {\n return times;\n }\n set pifactor += 1;\n }\n return 0;\n }\n\n operation DistinguishRzFromRy(theta : Double, unitary : (Qubit => Unit is Adj + Ctl)) : Int {\n use q = Qubit();\n let times = ComputeRepetitions(theta, 1, 0.1);\n mutable attempt = 1;\n mutable measuredOne = false;\n repeat {\n for _ in 1..times {\n unitary(q);\n }\n // for Rz, we'll never venture away from |0⟩ state, so as soon as we got |1⟩ we know it's not Rz\n if MResetZ(q) == One {\n set measuredOne = true;\n }\n // if we try several times and still only get |0⟩s, chances are that it is Rz\n } until (attempt == 4 or measuredOne)\n fixup {\n set attempt += 1;\n }\n return measuredOne ? 1 | 0;\n }\n}\n"
4538
4538
  }
4539
4539
  ]
4540
4540
  }
@@ -4799,7 +4799,7 @@ export default {
4799
4799
  "title": "Generate a Weighted Bit",
4800
4800
  "description": {
4801
4801
  "type": "text-content",
4802
- "content": "<p>In each of the above exercises, all generated numbers were equally likely. Now let's create an operation that will return a random bit with different probabilities of outcomes.</p>\n<blockquote>\n<p>Remember that by setting the amplitudes of basis states $\\alpha$ and $\\beta$, you can control the probability of getting measurement outcomes $0$ and $1$ when the qubit is measured.</p>\n</blockquote>\n<p><strong>Input:</strong>\nA floating-point number $x$, $0 \\le x \\le 1$.</p>\n<p><strong>Goal:</strong> Generate $0$ or $1$ with probability of $0$ equal to $x$ and probability of $1$ equal to $1 - x$.</p>\n<blockquote>\n<p>Q# namespace <code>Microsoft.Quantum.Math</code> includes useful functions <code>Sqrt</code>, <code>ArcCos</code>, and <code>ArcSin</code>.</p>\n</blockquote>\n"
4802
+ "content": "<p>In each of the above exercises, all generated numbers were equally likely. Now let's create an operation that will return a random bit with different probabilities of outcomes.</p>\n<blockquote>\n<p>Remember that by setting the amplitudes of basis states $\\alpha$ and $\\beta$, you can control the probability of getting measurement outcomes $0$ and $1$ when the qubit is measured.</p>\n</blockquote>\n<p><strong>Input:</strong>\nA floating-point number $x$, $0 \\le x \\le 1$.</p>\n<p><strong>Goal:</strong> Generate $0$ or $1$ with probability of $0$ equal to $x$ and probability of $1$ equal to $1 - x$.</p>\n<blockquote>\n<p>Q# namespace <code>Std.Math</code> includes useful functions <code>Sqrt</code>, <code>ArcCos</code>, and <code>ArcSin</code>.</p>\n</blockquote>\n"
4803
4803
  },
4804
4804
  "sourceIds": [
4805
4805
  "random_numbers__weighted_random_bit__Verification.qs",
@@ -4817,7 +4817,7 @@ export default {
4817
4817
  {
4818
4818
  "type": "solution",
4819
4819
  "id": "random_numbers__weighted_random_bit_solution",
4820
- "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n operation WeightedRandomBit(x : Double) : Int {\n let theta = 2.0 * ArcCos(Sqrt(x)); // or 2.0 * ArcSin(Sqrt(1.0 - x));\n\n use q = Qubit();\n Ry(theta, q);\n\n let result = MResetZ(q);\n return result == Zero ? 0 | 1;\n }\n}\n"
4820
+ "code": "namespace Kata {\n import Std.Math.*;\n\n operation WeightedRandomBit(x : Double) : Int {\n let theta = 2.0 * ArcCos(Sqrt(x)); // or 2.0 * ArcSin(Sqrt(1.0 - x));\n\n use q = Qubit();\n Ry(theta, q);\n\n let result = MResetZ(q);\n return result == Zero ? 0 | 1;\n }\n}\n"
4821
4821
  }
4822
4822
  ]
4823
4823
  }
@@ -4828,7 +4828,7 @@ export default {
4828
4828
  "title": "Generate a Random Number Between Min and Max",
4829
4829
  "description": {
4830
4830
  "type": "text-content",
4831
- "content": "<p>In the &quot;Generate A Number Of Arbitrary Size&quot; exercise, you generated numbers in the range $[0, 2^N-1]$ $(1 \\leq N \\leq 10)$. Now let's create an operation that will return a random number in the range $[min, max]$.</p>\n<p><strong>Input:</strong>\nTwo integers $min$ and $max$ ($0 \\leq min \\leq max \\leq 2^{10}-1$).</p>\n<p><strong>Goal:</strong> Generate a random number in the range $[min, max]$ with an equal probability of getting each of the numbers in this range.</p>\n<blockquote>\n<p>Q# namespace <code>Microsoft.Quantum.Math</code> includes useful function <code>BitSizeI</code> that calculates the number of bits in the binary representation of the given number.</p>\n</blockquote>\n"
4831
+ "content": "<p>In the &quot;Generate A Number Of Arbitrary Size&quot; exercise, you generated numbers in the range $[0, 2^N-1]$ $(1 \\leq N \\leq 10)$. Now let's create an operation that will return a random number in the range $[min, max]$.</p>\n<p><strong>Input:</strong>\nTwo integers $min$ and $max$ ($0 \\leq min \\leq max \\leq 2^{10}-1$).</p>\n<p><strong>Goal:</strong> Generate a random number in the range $[min, max]$ with an equal probability of getting each of the numbers in this range.</p>\n<blockquote>\n<p>Q# namespace <code>Std.Math</code> includes useful function <code>BitSizeI</code> that calculates the number of bits in the binary representation of the given number.</p>\n</blockquote>\n"
4832
4832
  },
4833
4833
  "sourceIds": [
4834
4834
  "random_numbers__random_number__Verification.qs",
@@ -4846,7 +4846,7 @@ export default {
4846
4846
  {
4847
4847
  "type": "solution",
4848
4848
  "id": "random_numbers__random_number_solution",
4849
- "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n operation RandomNumberInRange(min : Int, max : Int) : Int {\n let nBits = BitSizeI(max - min);\n mutable output = 0;\n repeat {\n set output = RandomNBits(nBits);\n } until output <= max - min;\n return output + min;\n }\n\n operation RandomNBits(N : Int) : Int {\n mutable result = 0;\n for i in 0 .. N - 1 {\n set result = result * 2 + RandomBit();\n }\n return result;\n }\n\n operation RandomBit() : Int {\n use q = Qubit();\n H(q);\n return MResetZ(q) == Zero ? 0 | 1;\n }\n}\n"
4849
+ "code": "namespace Kata {\n import Std.Math.*;\n\n operation RandomNumberInRange(min : Int, max : Int) : Int {\n let nBits = BitSizeI(max - min);\n mutable output = 0;\n repeat {\n set output = RandomNBits(nBits);\n } until output <= max - min;\n return output + min;\n }\n\n operation RandomNBits(N : Int) : Int {\n mutable result = 0;\n for i in 0..N - 1 {\n set result = result * 2 + RandomBit();\n }\n return result;\n }\n\n operation RandomBit() : Int {\n use q = Qubit();\n H(q);\n return MResetZ(q) == Zero ? 0 | 1;\n }\n}\n"
4850
4850
  }
4851
4851
  ]
4852
4852
  }
@@ -4915,7 +4915,7 @@ export default {
4915
4915
  {
4916
4916
  "type": "solution",
4917
4917
  "id": "key_distribution__random_array_solution",
4918
- "code": "namespace Kata {\n open Microsoft.Quantum.Random;\n\n operation RandomArray(N : Int) : Bool[] {\n mutable array = [false, size = N];\n for i in 0 .. N - 1 {\n set array w/= i <- DrawRandomInt(0, 1) == 0;\n }\n return array;\n }\n}\n"
4918
+ "code": "namespace Kata {\n import Std.Random.*;\n\n operation RandomArray(N : Int) : Bool[] {\n mutable array = [false, size = N];\n for i in 0..N - 1 {\n set array w/= i <- DrawRandomInt(0, 1) == 0;\n }\n return array;\n }\n}\n"
4919
4919
  }
4920
4920
  ]
4921
4921
  }
@@ -4973,7 +4973,7 @@ export default {
4973
4973
  {
4974
4974
  "type": "solution",
4975
4975
  "id": "key_distribution__measure_qubits_solution",
4976
- "code": "namespace Kata {\n open Microsoft.Quantum.Convert;\n\n operation MeasureQubits(qs : Qubit[], bases : Bool[]) : Bool[] {\n for i in 0 .. Length(qs) - 1 {\n if bases[i] {\n H(qs[i]);\n }\n }\n return ResultArrayAsBoolArray(MeasureEachZ(qs)); \n }\n}\n"
4976
+ "code": "namespace Kata {\n import Std.Convert.*;\n\n operation MeasureQubits(qs : Qubit[], bases : Bool[]) : Bool[] {\n for i in 0..Length(qs) - 1 {\n if bases[i] {\n H(qs[i]);\n }\n }\n return ResultArrayAsBoolArray(MeasureEachZ(qs));\n }\n}\n"
4977
4977
  }
4978
4978
  ]
4979
4979
  }
@@ -5019,7 +5019,7 @@ export default {
5019
5019
  {
5020
5020
  "type": "example",
5021
5021
  "id": "key_distribution__bb84_demo",
5022
- "code": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Random;\n\n @EntryPoint()\n operation RunBB84Protocol() : Unit {\n // 1. Alice chooses a random set of bits to encode in her qubits \n // and a random set of bases to prepare her qubits in.\n // ...\n\n // 2. Alice allocates qubits, encodes them using her choices and sends them to Bob.\n // You can not express \"sending the qubits to Bob\" in Q#, \n // so Bob will just use the same qubits.\n // ...\n\n // 3. Bob chooses a random set of bases to measure Alice's qubits in.\n // ...\n\n // 4. Bob measures Alice's qubits in his chosen bases.\n // ...\n\n // 5. Alice and Bob compare their chosen bases and \n // use the bits in the matching positions to create a shared key.\n // ...\n\n // If you did everything correctly, the generated keys will always match, \n // since there is no eavesdropping going on.\n // In the next lesson we will discuss introducing eavesdropping.\n }\n\n // You might find these helper operations from earlier tasks useful.\n operation RandomArray(N : Int) : Bool[] {\n mutable array = [false, size = N];\n for i in 0 .. N - 1 {\n set array w/= i <- DrawRandomInt(0, 1) == 0;\n }\n return array;\n }\n\n operation PrepareQubits(qs : Qubit[], bases : Bool[], bits : Bool[]) : Unit {\n for i in 0 .. Length(qs) - 1 {\n if bits[i] {\n X(qs[i]);\n }\n if bases[i] {\n H(qs[i]);\n }\n }\n }\n\n operation MeasureQubits(qs : Qubit[], bases : Bool[]) : Bool[] {\n for i in 0 .. Length(qs) - 1 {\n if bases[i] {\n H(qs[i]);\n }\n }\n return ResultArrayAsBoolArray(MeasureEachZ(qs)); \n }\n\n function GenerateSharedKey(basesAlice : Bool[], basesBob : Bool[], bits : Bool[]) : Bool[] {\n mutable key = [];\n for i in 0 .. Length(bits) - 1 {\n if basesAlice[i] == basesBob[i] {\n set key += [bits[i]];\n }\n }\n return key;\n } \n}\n"
5022
+ "code": "namespace Kata {\n import Std.Arrays.*;\n import Std.Convert.*;\n import Std.Diagnostics.*;\n import Std.Random.*;\n\n @EntryPoint()\n operation RunBB84Protocol() : Unit {\n // 1. Alice chooses a random set of bits to encode in her qubits\n // and a random set of bases to prepare her qubits in.\n // ...\n\n // 2. Alice allocates qubits, encodes them using her choices and sends them to Bob.\n // You can not express \"sending the qubits to Bob\" in Q#,\n // so Bob will just use the same qubits.\n // ...\n\n // 3. Bob chooses a random set of bases to measure Alice's qubits in.\n // ...\n\n // 4. Bob measures Alice's qubits in his chosen bases.\n // ...\n\n // 5. Alice and Bob compare their chosen bases and\n // use the bits in the matching positions to create a shared key.\n // ...\n\n // If you did everything correctly, the generated keys will always match,\n // since there is no eavesdropping going on.\n // In the next lesson we will discuss introducing eavesdropping.\n }\n\n // You might find these helper operations from earlier tasks useful.\n operation RandomArray(N : Int) : Bool[] {\n mutable array = [false, size = N];\n for i in 0..N - 1 {\n set array w/= i <- DrawRandomInt(0, 1) == 0;\n }\n return array;\n }\n\n operation PrepareQubits(qs : Qubit[], bases : Bool[], bits : Bool[]) : Unit {\n for i in 0..Length(qs) - 1 {\n if bits[i] {\n X(qs[i]);\n }\n if bases[i] {\n H(qs[i]);\n }\n }\n }\n\n operation MeasureQubits(qs : Qubit[], bases : Bool[]) : Bool[] {\n for i in 0..Length(qs) - 1 {\n if bases[i] {\n H(qs[i]);\n }\n }\n return ResultArrayAsBoolArray(MeasureEachZ(qs));\n }\n\n function GenerateSharedKey(basesAlice : Bool[], basesBob : Bool[], bits : Bool[]) : Bool[] {\n mutable key = [];\n for i in 0..Length(bits) - 1 {\n if basesAlice[i] == basesBob[i] {\n set key += [bits[i]];\n }\n }\n return key;\n }\n}\n"
5023
5023
  }
5024
5024
  ]
5025
5025
  },
@@ -5260,7 +5260,7 @@ export default {
5260
5260
  {
5261
5261
  "type": "example",
5262
5262
  "id": "teleportation__testing_standard_teleportation_example",
5263
- "code": "namespace Kata {\n open Microsoft.Quantum.Diagnostics;\n\n @EntryPoint()\n operation TestTeleportation() : Unit {\n\n // To define the different states, let us make use of PauliX, PauliY and PauliZ basis\n let messages = [(PauliX, Zero, \"|+⟩\"), \n (PauliX, One, \"|-⟩\"), \n (PauliY, Zero, \"|i⟩\"), \n (PauliY, One, \"|-i⟩\"), \n (PauliZ, Zero, \"|0⟩\"), \n (PauliZ, One, \"|1⟩\")];\n\n // To effectively test the solution, experiment needs to be repeated multiple times\n let numRepetitions = 100;\n\n // Loop through all the states to test each one of them individually\n for (basis, sentState, stateName) in messages {\n\n // Loop through multiple iterations for each state\n for j in 1 .. numRepetitions {\n // 1. Initialize qubits for Alice and Bob\n // ..\n \n // 2. Prepare the entangled state between Alice and Bob\n // ..\n\n // 3. Prepare the Message qubit and send classical message\n // ..\n\n // 4. Reconstruct Bob's qubit using the classical message\n // ..\n\n // 5. Verify if the state was teleported correctly. If not, indicate failure\n // ..\n\n // 6. Reset the qubits\n // ..\n }\n }\n\n // 7. Indicate success if everything went well\n // ..\n }\n\n // You might find these helper operations from earlier tasks useful.\n operation PrepareAndSendMessage(qAlice : Qubit, basis : Pauli, state : Result) : (Bool, Bool) {\n use qMessage = Qubit();\n if state == One {\n X(qMessage);\n }\n if basis != PauliZ {\n H(qMessage);\n }\n if basis == PauliY {\n S(qMessage);\n }\n let classicalBits = SendMessage(qAlice, qMessage);\n Reset(qMessage);\n return classicalBits;\n }\n \n operation ReconstructAndMeasureMessage(qBob : Qubit, (b1 : Bool, b2 : Bool), basis : Pauli) : Result {\n ReconstructMessage(qBob, (b1, b2));\n return Measure([basis], [qBob]);\n }\n\n operation Entangle(qAlice : Qubit, qBob : Qubit) : Unit is Adj + Ctl {\n H(qAlice);\n CNOT(qAlice, qBob);\n }\n\n operation SendMessage(qAlice: Qubit, qMessage: Qubit) : (Bool, Bool) {\n CNOT(qMessage, qAlice);\n H(qMessage);\n return (M(qMessage) == One, M(qAlice) == One);\n }\n\n operation ReconstructMessage(qBob : Qubit, (b1 : Bool, b2 : Bool)) : Unit {\n if b1 {\n Z(qBob);\n }\n if b2 {\n X(qBob);\n }\n }\n}"
5263
+ "code": "namespace Kata {\n import Std.Diagnostics.*;\n\n @EntryPoint()\n operation TestTeleportation() : Unit {\n\n // To define the different states, let us make use of PauliX, PauliY and PauliZ basis\n let messages = [\n (PauliX, Zero, \"|+⟩\"),\n (PauliX, One, \"|-⟩\"),\n (PauliY, Zero, \"|i⟩\"),\n (PauliY, One, \"|-i⟩\"),\n (PauliZ, Zero, \"|0⟩\"),\n (PauliZ, One, \"|1⟩\")\n ];\n\n // To effectively test the solution, experiment needs to be repeated multiple times\n let numRepetitions = 100;\n\n // Loop through all the states to test each one of them individually\n for (basis, sentState, stateName) in messages {\n\n // Loop through multiple iterations for each state\n for j in 1..numRepetitions {\n // 1. Initialize qubits for Alice and Bob\n // ..\n\n // 2. Prepare the entangled state between Alice and Bob\n // ..\n\n // 3. Prepare the Message qubit and send classical message\n // ..\n\n // 4. Reconstruct Bob's qubit using the classical message\n // ..\n\n // 5. Verify if the state was teleported correctly. If not, indicate failure\n // ..\n\n // 6. Reset the qubits\n // ..\n }\n }\n\n // 7. Indicate success if everything went well\n // ..\n }\n\n // You might find these helper operations from earlier tasks useful.\n operation PrepareAndSendMessage(qAlice : Qubit, basis : Pauli, state : Result) : (Bool, Bool) {\n use qMessage = Qubit();\n if state == One {\n X(qMessage);\n }\n if basis != PauliZ {\n H(qMessage);\n }\n if basis == PauliY {\n S(qMessage);\n }\n let classicalBits = SendMessage(qAlice, qMessage);\n Reset(qMessage);\n return classicalBits;\n }\n\n operation ReconstructAndMeasureMessage(qBob : Qubit, (b1 : Bool, b2 : Bool), basis : Pauli) : Result {\n ReconstructMessage(qBob, (b1, b2));\n return Measure([basis], [qBob]);\n }\n\n operation Entangle(qAlice : Qubit, qBob : Qubit) : Unit is Adj + Ctl {\n H(qAlice);\n CNOT(qAlice, qBob);\n }\n\n operation SendMessage(qAlice : Qubit, qMessage : Qubit) : (Bool, Bool) {\n CNOT(qMessage, qAlice);\n H(qMessage);\n return (M(qMessage) == One, M(qAlice) == One);\n }\n\n operation ReconstructMessage(qBob : Qubit, (b1 : Bool, b2 : Bool)) : Unit {\n if b1 {\n Z(qBob);\n }\n if b2 {\n X(qBob);\n }\n }\n}\n"
5264
5264
  }
5265
5265
  ]
5266
5266
  },
@@ -5397,7 +5397,7 @@ export default {
5397
5397
  {
5398
5398
  "type": "solution",
5399
5399
  "id": "teleportation__entanglement_swapping_solution",
5400
- "code": "namespace Kata {\n open Microsoft.Quantum.Convert;\n\n operation EntanglementSwapping() : ((Qubit, Qubit) => Int, (Qubit, Int) => Unit) {\n return (SendMessageCharlie, ReconstructMessageBob);\n }\n\n operation SendMessageCharlie(qAlice1 : Qubit, qBob1 : Qubit) : Int {\n let (c1, c2) = SendMessage(qAlice1, qBob1);\n return BoolArrayAsInt([c1, c2]);\n } \n\n operation ReconstructMessageBob(qBob2 : Qubit, resultCharlie : Int) : Unit {\n let classicalBits = IntAsBoolArray(resultCharlie, 2); \n ReconstructMessage(qBob2, (classicalBits[0], classicalBits[1]));\n }\n\n operation SendMessage(qAlice: Qubit, qMessage: Qubit) : (Bool, Bool) {\n CNOT(qMessage, qAlice);\n H(qMessage);\n return (M(qMessage) == One, M(qAlice) == One);\n }\n\n operation ReconstructMessage(qBob : Qubit, (b1 : Bool, b2 : Bool)) : Unit {\n if b1 {\n Z(qBob);\n }\n if b2 {\n X(qBob);\n }\n }\n}"
5400
+ "code": "namespace Kata {\n import Std.Convert.*;\n\n operation EntanglementSwapping() : ((Qubit, Qubit) => Int, (Qubit, Int) => Unit) {\n return (SendMessageCharlie, ReconstructMessageBob);\n }\n\n operation SendMessageCharlie(qAlice1 : Qubit, qBob1 : Qubit) : Int {\n let (c1, c2) = SendMessage(qAlice1, qBob1);\n return BoolArrayAsInt([c1, c2]);\n }\n\n operation ReconstructMessageBob(qBob2 : Qubit, resultCharlie : Int) : Unit {\n let classicalBits = IntAsBoolArray(resultCharlie, 2);\n ReconstructMessage(qBob2, (classicalBits[0], classicalBits[1]));\n }\n\n operation SendMessage(qAlice : Qubit, qMessage : Qubit) : (Bool, Bool) {\n CNOT(qMessage, qAlice);\n H(qMessage);\n return (M(qMessage) == One, M(qAlice) == One);\n }\n\n operation ReconstructMessage(qBob : Qubit, (b1 : Bool, b2 : Bool)) : Unit {\n if b1 {\n Z(qBob);\n }\n if b2 {\n X(qBob);\n }\n }\n}\n"
5401
5401
  }
5402
5402
  ]
5403
5403
  }
@@ -5748,7 +5748,7 @@ export default {
5748
5748
  {
5749
5749
  "type": "example",
5750
5750
  "id": "oracles__phase_oracle_alt_bit",
5751
- "code": "namespace Kata {\n open Microsoft.Quantum.Diagnostics;\n\n // This operation implements the oracle; we will learn how to implement oracles later in the kata\n operation AlternatingBitPattern_PhaseOracle(x : Qubit[]) : Unit is Adj + Ctl {\n use q = Qubit();\n X(q);\n ApplyControlledOnBitString([false, true, false], Z, x, q);\n ApplyControlledOnBitString([true, false, true], Z, x, q);\n X(q);\n }\n\n @EntryPoint()\n operation PhaseOracle_Demo() : Unit {\n use q = Qubit[3];\n ApplyToEachA(H, q);\n\n Message(\"Starting state (equal superposition of all basis states):\");\n DumpMachine();\n\n // Apply the oracle\n AlternatingBitPattern_PhaseOracle(q);\n\n // Print the resulting state; notice which phases changed\n Message(\"State after applying the phase oracle:\");\n DumpMachine();\n\n ResetAll(q);\n }\n}\n"
5751
+ "code": "namespace Kata {\n import Std.Diagnostics.*;\n\n // This operation implements the oracle; we will learn how to implement oracles later in the kata\n operation AlternatingBitPattern_PhaseOracle(x : Qubit[]) : Unit is Adj + Ctl {\n use q = Qubit();\n X(q);\n ApplyControlledOnBitString([false, true, false], Z, x, q);\n ApplyControlledOnBitString([true, false, true], Z, x, q);\n X(q);\n }\n\n @EntryPoint()\n operation PhaseOracle_Demo() : Unit {\n use q = Qubit[3];\n ApplyToEachA(H, q);\n\n Message(\"Starting state (equal superposition of all basis states):\");\n DumpMachine();\n\n // Apply the oracle\n AlternatingBitPattern_PhaseOracle(q);\n\n // Print the resulting state; notice which phases changed\n Message(\"State after applying the phase oracle:\");\n DumpMachine();\n\n ResetAll(q);\n }\n}\n"
5752
5752
  },
5753
5753
  {
5754
5754
  "type": "text-content",
@@ -5780,7 +5780,7 @@ export default {
5780
5780
  {
5781
5781
  "type": "solution",
5782
5782
  "id": "oracles__phase_oracle_seven_solution",
5783
- "code": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n\n operation IsSeven_PhaseOracle(x : Qubit[]) : Unit is Adj + Ctl {\n Controlled Z(Most(x), Tail(x));\n }\n}\n"
5783
+ "code": "namespace Kata {\n import Std.Arrays.*;\n\n operation IsSeven_PhaseOracle(x : Qubit[]) : Unit is Adj + Ctl {\n Controlled Z(Most(x), Tail(x));\n }\n}\n"
5784
5784
  },
5785
5785
  {
5786
5786
  "type": "text-content",
@@ -5812,7 +5812,7 @@ export default {
5812
5812
  {
5813
5813
  "type": "example",
5814
5814
  "id": "oracles__marking_oracle_alt_bit",
5815
- "code": "namespace Kata {\n open Microsoft.Quantum.Diagnostics;\n\n // This operation implements the oracle; we will learn how to implement oracles later in the kata\n operation AlternatingBitPattern_MarkingOracle(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n ApplyControlledOnBitString([false, true, false], X, x, y);\n ApplyControlledOnBitString([true, false, true], X, x, y);\n }\n\n @EntryPoint()\n operation MarkingOracle_Demo() : Unit {\n use (x, y) = (Qubit[3], Qubit());\n ApplyToEachA(H, x);\n\n Message(\"Starting state (equal superposition of all basis states ⊗ |0⟩):\");\n DumpMachine();\n\n // Apply the oracle\n AlternatingBitPattern_MarkingOracle(x, y);\n\n // Print the resulting state; notice which amplitudes changed\n Message(\"State after applying the marking oracle:\");\n DumpMachine();\n\n ResetAll(x + [y]);\n }\n}\n"
5815
+ "code": "namespace Kata {\n import Std.Diagnostics.*;\n\n // This operation implements the oracle; we will learn how to implement oracles later in the kata\n operation AlternatingBitPattern_MarkingOracle(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n ApplyControlledOnBitString([false, true, false], X, x, y);\n ApplyControlledOnBitString([true, false, true], X, x, y);\n }\n\n @EntryPoint()\n operation MarkingOracle_Demo() : Unit {\n use (x, y) = (Qubit[3], Qubit());\n ApplyToEachA(H, x);\n\n Message(\"Starting state (equal superposition of all basis states ⊗ |0⟩):\");\n DumpMachine();\n\n // Apply the oracle\n AlternatingBitPattern_MarkingOracle(x, y);\n\n // Print the resulting state; notice which amplitudes changed\n Message(\"State after applying the marking oracle:\");\n DumpMachine();\n\n ResetAll(x + [y]);\n }\n}\n"
5816
5816
  },
5817
5817
  {
5818
5818
  "type": "text-content",
@@ -5905,7 +5905,7 @@ export default {
5905
5905
  {
5906
5906
  "type": "example",
5907
5907
  "id": "oracles__oracle_converter_demo",
5908
- "code": "namespace Kata {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Arrays;\n\n @EntryPoint()\n operation OracleConverterDemo () : Unit {\n use register = Qubit[3];\n ApplyToEachA(H, register);\n\n Message(\"The equal superposition state:\");\n DumpMachine();\n\n // Apply the phase oracle `IsSeven_PhaseOracle`\n IsSeven_PhaseOracle(register);\n\n Message(\"The state after applying the phase oracle IsSeven_PhaseOracle:\");\n DumpMachine();\n ResetAll(register);\n\n ApplyToEachA(H, register);\n\n // Apply the marking oracle `IsSeven_MarkingOracle` as a phase oracle\n ApplyMarkingOracleAsPhaseOracle(IsSeven_MarkingOracle, register);\n\n Message(\"The state after applying the converted marking oracle IsSeven_MarkingOracle:\");\n DumpMachine();\n ResetAll(register);\n }\n\n operation IsSeven_PhaseOracle(x : Qubit[]) : Unit is Adj + Ctl {\n Controlled Z(Most(x), Tail(x));\n }\n\n operation IsSeven_MarkingOracle(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n Controlled X(x, y);\n }\n\n operation ApplyMarkingOracleAsPhaseOracle(\n markingOracle : ((Qubit[], Qubit) => Unit is Adj + Ctl),\n qubits : Qubit[])\n : Unit is Adj + Ctl {\n use minus = Qubit();\n within {\n X(minus);\n H(minus);\n } apply {\n markingOracle(qubits, minus);\n }\n }\n}\n"
5908
+ "code": "namespace Kata {\n import Std.Diagnostics.*;\n import Std.Arrays.*;\n\n @EntryPoint()\n operation OracleConverterDemo() : Unit {\n use register = Qubit[3];\n ApplyToEachA(H, register);\n\n Message(\"The equal superposition state:\");\n DumpMachine();\n\n // Apply the phase oracle `IsSeven_PhaseOracle`\n IsSeven_PhaseOracle(register);\n\n Message(\"The state after applying the phase oracle IsSeven_PhaseOracle:\");\n DumpMachine();\n ResetAll(register);\n\n ApplyToEachA(H, register);\n\n // Apply the marking oracle `IsSeven_MarkingOracle` as a phase oracle\n ApplyMarkingOracleAsPhaseOracle(IsSeven_MarkingOracle, register);\n\n Message(\"The state after applying the converted marking oracle IsSeven_MarkingOracle:\");\n DumpMachine();\n ResetAll(register);\n }\n\n operation IsSeven_PhaseOracle(x : Qubit[]) : Unit is Adj + Ctl {\n Controlled Z(Most(x), Tail(x));\n }\n\n operation IsSeven_MarkingOracle(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n Controlled X(x, y);\n }\n\n operation ApplyMarkingOracleAsPhaseOracle(\n markingOracle : ((Qubit[], Qubit) => Unit is Adj + Ctl),\n qubits : Qubit[]\n ) : Unit is Adj + Ctl {\n use minus = Qubit();\n within {\n X(minus);\n H(minus);\n } apply {\n markingOracle(qubits, minus);\n }\n }\n}\n"
5909
5909
  },
5910
5910
  {
5911
5911
  "type": "text-content",
@@ -6064,7 +6064,7 @@ export default {
6064
6064
  {
6065
6065
  "type": "solution",
6066
6066
  "id": "oracles__bit_pattern_challenge_solution",
6067
- "code": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n\n operation ArbitraryBitPattern_Oracle_Challenge(\n x : Qubit[],\n pattern : Bool[])\n : Unit is Adj + Ctl {\n within {\n for i in IndexRange(x) {\n if not pattern[i] {\n X(x[i]);\n }\n }\n } apply {\n Controlled Z(Most(x), Tail(x));\n }\n }\n}\n"
6067
+ "code": "namespace Kata {\n import Std.Arrays.*;\n\n operation ArbitraryBitPattern_Oracle_Challenge(\n x : Qubit[],\n pattern : Bool[]\n ) : Unit is Adj + Ctl {\n within {\n for i in IndexRange(x) {\n if not pattern[i] {\n X(x[i]);\n }\n }\n } apply {\n Controlled Z(Most(x), Tail(x));\n }\n }\n}\n"
6068
6068
  }
6069
6069
  ]
6070
6070
  }
@@ -6093,7 +6093,7 @@ export default {
6093
6093
  {
6094
6094
  "type": "solution",
6095
6095
  "id": "oracles__meeting_oracle_solution",
6096
- "code": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n\n operation Or_Oracle(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n X(y);\n ApplyControlledOnInt(0, X, x, y);\n }\n\n operation Meeting_Oracle(x : Qubit[], jasmine : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n use qs = Qubit[Length(x)];\n within {\n for i in IndexRange(qs) {\n // flip q[i] if both x and jasmine are free on the given day\n X(x[i]);\n X(jasmine[i]);\n CCNOT(x[i], jasmine[i], qs[i]);\n }\n } apply {\n Or_Oracle(qs, y);\n }\n }\n}\n"
6096
+ "code": "namespace Kata {\n import Std.Arrays.*;\n\n operation Or_Oracle(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n X(y);\n ApplyControlledOnInt(0, X, x, y);\n }\n\n operation Meeting_Oracle(x : Qubit[], jasmine : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n use qs = Qubit[Length(x)];\n within {\n for i in IndexRange(qs) {\n // flip q[i] if both x and jasmine are free on the given day\n X(x[i]);\n X(jasmine[i]);\n CCNOT(x[i], jasmine[i], qs[i]);\n }\n } apply {\n Or_Oracle(qs, y);\n }\n }\n}\n"
6097
6097
  }
6098
6098
  ]
6099
6099
  }
@@ -6110,7 +6110,7 @@ export default {
6110
6110
  {
6111
6111
  "type": "example",
6112
6112
  "id": "oracles__test_meeting_oracle",
6113
- "code": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Convert;\n\n // The classical function to perform the same computation\n function Meeting_Classical(x : Bool[], jasmine : Bool[]) : Bool {\n for i in IndexRange(x) {\n if (not x[i]) and (not jasmine[i]) {\n // They have a day that they can both meet\n return true;\n }\n }\n return false;\n }\n\n operation Or_Oracle(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n X(y);\n ApplyControlledOnInt(0, X, x, y);\n }\n\n operation Meeting_Oracle(x : Qubit[], jasmine : Qubit[], z : Qubit) : Unit is Adj + Ctl {\n use qs = Qubit[Length(x)];\n within {\n for i in IndexRange(qs) {\n // Flip q[i] if both you and Jasmine are free on the given day\n X(x[i]);\n X(jasmine[i]);\n CCNOT(x[i], jasmine[i], qs[i]);\n }\n } apply {\n Or_Oracle(qs, z);\n }\n }\n\n @EntryPoint()\n operation Test_Meeting_Oracle() : Unit {\n // There are 2^5 ways to arrange each of the schedules - let's try all of them\n for k in 0 .. 2^5 - 1 {\n for j in 0 .. 2^5 - 1 {\n // Convert your and Jasmine's schedules to bit arrays\n let binaryX = IntAsBoolArray(k, 5);\n let binaryJasmine = IntAsBoolArray(j, 5);\n\n // Calculate the function classically\n let classicalResult = Meeting_Classical(binaryX, binaryJasmine);\n\n // Allocate qubits to represent your schedule, Jasmine's schedule, and the output\n use (x, jasmine, target) = (Qubit[5], Qubit[5], Qubit());\n // Prepare the quantum schedules in basis states matching the binary schedules\n ApplyPauliFromBitString(PauliX, true, binaryX, x);\n ApplyPauliFromBitString(PauliX, true, binaryJasmine, jasmine);\n\n // Apply the quantum oracle\n Meeting_Oracle(x, jasmine, target);\n\n // Compare the result of the quantum oracle with that of the classical oracle\n if CheckZero(target) == classicalResult {\n Message($\"Failed on test case k = {k}, j = {j}. Classical result is not the same as quantum.\");\n }\n\n // Undo the preparation of basis states x and jasmine\n ApplyPauliFromBitString(PauliX, true, binaryX, x);\n ApplyPauliFromBitString(PauliX, true, binaryJasmine, jasmine);\n\n // Check that the oracle did not change the input states\n if not CheckAllZero(x) {\n Message($\"Failed on test case k = {k}, j = {j}. Input state of x changed.\");\n }\n if not CheckAllZero(jasmine) {\n Message($\"Failed on test case k = {k}, j = {j}. Input state of jasmine changed.\");\n }\n\n Reset(target);\n }\n }\n Message(\"Success!\");\n }\n}\n"
6113
+ "code": "namespace Kata {\n import Std.Arrays.*;\n import Std.Diagnostics.*;\n import Std.Convert.*;\n\n // The classical function to perform the same computation\n function Meeting_Classical(x : Bool[], jasmine : Bool[]) : Bool {\n for i in IndexRange(x) {\n if (not x[i]) and (not jasmine[i]) {\n // They have a day that they can both meet\n return true;\n }\n }\n return false;\n }\n\n operation Or_Oracle(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n X(y);\n ApplyControlledOnInt(0, X, x, y);\n }\n\n operation Meeting_Oracle(x : Qubit[], jasmine : Qubit[], z : Qubit) : Unit is Adj + Ctl {\n use qs = Qubit[Length(x)];\n within {\n for i in IndexRange(qs) {\n // Flip q[i] if both you and Jasmine are free on the given day\n X(x[i]);\n X(jasmine[i]);\n CCNOT(x[i], jasmine[i], qs[i]);\n }\n } apply {\n Or_Oracle(qs, z);\n }\n }\n\n @EntryPoint()\n operation Test_Meeting_Oracle() : Unit {\n // There are 2^5 ways to arrange each of the schedules - let's try all of them\n for k in 0..2^5 - 1 {\n for j in 0..2^5 - 1 {\n // Convert your and Jasmine's schedules to bit arrays\n let binaryX = IntAsBoolArray(k, 5);\n let binaryJasmine = IntAsBoolArray(j, 5);\n\n // Calculate the function classically\n let classicalResult = Meeting_Classical(binaryX, binaryJasmine);\n\n // Allocate qubits to represent your schedule, Jasmine's schedule, and the output\n use (x, jasmine, target) = (Qubit[5], Qubit[5], Qubit());\n // Prepare the quantum schedules in basis states matching the binary schedules\n ApplyPauliFromBitString(PauliX, true, binaryX, x);\n ApplyPauliFromBitString(PauliX, true, binaryJasmine, jasmine);\n\n // Apply the quantum oracle\n Meeting_Oracle(x, jasmine, target);\n\n // Compare the result of the quantum oracle with that of the classical oracle\n if CheckZero(target) == classicalResult {\n Message($\"Failed on test case k = {k}, j = {j}. Classical result is not the same as quantum.\");\n }\n\n // Undo the preparation of basis states x and jasmine\n ApplyPauliFromBitString(PauliX, true, binaryX, x);\n ApplyPauliFromBitString(PauliX, true, binaryJasmine, jasmine);\n\n // Check that the oracle did not change the input states\n if not CheckAllZero(x) {\n Message($\"Failed on test case k = {k}, j = {j}. Input state of x changed.\");\n }\n if not CheckAllZero(jasmine) {\n Message($\"Failed on test case k = {k}, j = {j}. Input state of jasmine changed.\");\n }\n\n Reset(target);\n }\n }\n Message(\"Success!\");\n }\n}\n"
6114
6114
  }
6115
6115
  ]
6116
6116
  },
@@ -6388,18 +6388,18 @@ export default {
6388
6388
  "KatasLibrary.qs",
6389
6389
  "marking_oracles__Common.qs"
6390
6390
  ],
6391
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n\n operation Oracle_PatternMatching (x : Qubit[], y : Qubit, a : Int[], r : Bool[]) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n } \n}\n",
6391
+ "placeholderCode": "namespace Kata {\n import Std.Arrays.*;\n\n operation Oracle_PatternMatching(x : Qubit[], y : Qubit, a : Int[], r : Bool[]) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n}\n",
6392
6392
  "explainedSolution": {
6393
6393
  "type": "explained-solution",
6394
6394
  "items": [
6395
6395
  {
6396
6396
  "type": "text-content",
6397
- "content": "<p>This task is similar to the previous one: the function we're evaluating depends only on the states of a subset of qubits, so we can ignore the rest of them. The main difference here is getting the set of qubits we need to use as controls.</p>\n<p>Q# library function <code>Subarray</code> from the <code>Microsoft.Quantum.Arrays</code> namespace extracts the elements of the given array at the given indices, which is exactly what we need here.</p>\n"
6397
+ "content": "<p>This task is similar to the previous one: the function we're evaluating depends only on the states of a subset of qubits, so we can ignore the rest of them. The main difference here is getting the set of qubits we need to use as controls.</p>\n<p>Q# library function <code>Subarray</code> from the <code>Std.Arrays</code> namespace extracts the elements of the given array at the given indices, which is exactly what we need here.</p>\n"
6398
6398
  },
6399
6399
  {
6400
6400
  "type": "solution",
6401
6401
  "id": "marking_oracles__pattern_match_solution",
6402
- "code": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n\n operation Oracle_PatternMatching (x : Qubit[], y : Qubit, a : Int[], r : Bool[]) : Unit is Adj + Ctl {\n let ctrl = Subarray(a, x);\n ApplyControlledOnBitString(r, X, ctrl, y);\n }\n}\n"
6402
+ "code": "namespace Kata {\n import Std.Arrays.*;\n\n operation Oracle_PatternMatching(x : Qubit[], y : Qubit, a : Int[], r : Bool[]) : Unit is Adj + Ctl {\n let ctrl = Subarray(a, x);\n ApplyControlledOnBitString(r, X, ctrl, y);\n }\n}\n"
6403
6403
  }
6404
6404
  ]
6405
6405
  }
@@ -6457,7 +6457,7 @@ export default {
6457
6457
  {
6458
6458
  "type": "solution",
6459
6459
  "id": "marking_oracles__balanced_solution",
6460
- "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n \n operation Oracle_Balanced(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n let N = Length(x);\n let log = BitSizeI(N);\n use inc = Qubit[log];\n within {\n for q in x {\n Controlled Increment([q], inc);\n }\n } apply {\n ApplyControlledOnInt(N / 2, X, inc, y);\n }\n }\n\n operation Increment(register : Qubit[]) : Unit is Adj + Ctl {\n if Length(register) > 1 {\n Controlled Increment([register[0]], register[1 ...]);\n }\n X(register[0]);\n } \n}\n"
6460
+ "code": "namespace Kata {\n import Std.Math.*;\n\n operation Oracle_Balanced(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n let N = Length(x);\n let log = BitSizeI(N);\n use inc = Qubit[log];\n within {\n for q in x {\n Controlled Increment([q], inc);\n }\n } apply {\n ApplyControlledOnInt(N / 2, X, inc, y);\n }\n }\n\n operation Increment(register : Qubit[]) : Unit is Adj + Ctl {\n if Length(register) > 1 {\n Controlled Increment([register[0]], register[1...]);\n }\n X(register[0]);\n }\n}\n"
6461
6461
  }
6462
6462
  ]
6463
6463
  }
@@ -6486,7 +6486,7 @@ export default {
6486
6486
  {
6487
6487
  "type": "solution",
6488
6488
  "id": "marking_oracles__majority_solution",
6489
- "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n operation Oracle_Majority(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n let N = Length(x);\n let log = BitSizeI(N);\n use inc = Qubit[log];\n within {\n for q in x {\n Controlled Increment([q], inc);\n }\n } apply {\n CNOT(inc[log-1], y);\n if N == 5 {\n CCNOT(inc[0], inc[1], y);\n }\n }\n }\n\n operation Increment(register : Qubit[]) : Unit is Adj + Ctl {\n if Length(register) > 1 {\n Controlled Increment([register[0]], register[1 ...]);\n }\n X(register[0]);\n } \n}\n"
6489
+ "code": "namespace Kata {\n import Std.Math.*;\n\n operation Oracle_Majority(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n let N = Length(x);\n let log = BitSizeI(N);\n use inc = Qubit[log];\n within {\n for q in x {\n Controlled Increment([q], inc);\n }\n } apply {\n CNOT(inc[log-1], y);\n if N == 5 {\n CCNOT(inc[0], inc[1], y);\n }\n }\n }\n\n operation Increment(register : Qubit[]) : Unit is Adj + Ctl {\n if Length(register) > 1 {\n Controlled Increment([register[0]], register[1...]);\n }\n X(register[0]);\n }\n}\n"
6490
6490
  }
6491
6491
  ]
6492
6492
  }
@@ -6601,7 +6601,7 @@ export default {
6601
6601
  {
6602
6602
  "type": "example",
6603
6603
  "id": "deutsch_algo__oracle_implementations",
6604
- "code": "namespace Kata {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Math;\n\n operation PhaseOracle_Zero (x : Qubit) : Unit {\n // Do nothing...\n }\n\n operation PhaseOracle_One (x : Qubit) : Unit {\n // Apply a global phase of -1\n R(PauliI, 2.0 * PI(), x);\n }\n\n operation PhaseOracle_X (x : Qubit) : Unit {\n Z(x);\n }\n\n @EntryPoint()\n operation OracleImplementationDemo () : Unit {\n use q = Qubit();\n Ry(2.0 * ArcCos(0.6), q);\n Message(\"The qubit state before oracle application is 0.6|0⟩ + 0.8|0⟩:\");\n DumpMachine();\n\n // Apply the oracle.\n // Experiment with using other oracles to see their behavior!\n // (Note that the -1 global phase might not show up in simulation)\n PhaseOracle_X(q);\n\n Message(\"The qubit state after oracle application:\");\n DumpMachine();\n\n Reset(q);\n }\n}"
6604
+ "code": "namespace Kata {\n import Std.Diagnostics.*;\n import Std.Math.*;\n\n operation PhaseOracle_Zero(x : Qubit) : Unit {\n // Do nothing...\n }\n\n operation PhaseOracle_One(x : Qubit) : Unit {\n // Apply a global phase of -1\n R(PauliI, 2.0 * PI(), x);\n }\n\n operation PhaseOracle_X(x : Qubit) : Unit {\n Z(x);\n }\n\n @EntryPoint()\n operation OracleImplementationDemo() : Unit {\n use q = Qubit();\n Ry(2.0 * ArcCos(0.6), q);\n Message(\"The qubit state before oracle application is 0.6|0⟩ + 0.8|0⟩:\");\n DumpMachine();\n\n // Apply the oracle.\n // Experiment with using other oracles to see their behavior!\n // (Note that the -1 global phase might not show up in simulation)\n PhaseOracle_X(q);\n\n Message(\"The qubit state after oracle application:\");\n DumpMachine();\n\n Reset(q);\n }\n}\n"
6605
6605
  }
6606
6606
  ]
6607
6607
  },
@@ -6628,7 +6628,7 @@ export default {
6628
6628
  {
6629
6629
  "type": "solution",
6630
6630
  "id": "deutsch_algo__one_minus_x_oracle_solution",
6631
- "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n \n operation PhaseOracle_OneMinusX(x : Qubit) : Unit is Adj + Ctl {\n Z(x);\n R(PauliI, 2.0 * PI(), x);\n }\n}\n"
6631
+ "code": "namespace Kata {\n import Std.Math.*;\n\n operation PhaseOracle_OneMinusX(x : Qubit) : Unit is Adj + Ctl {\n Z(x);\n R(PauliI, 2.0 * PI(), x);\n }\n}\n"
6632
6632
  }
6633
6633
  ]
6634
6634
  }
@@ -6695,7 +6695,7 @@ export default {
6695
6695
  {
6696
6696
  "type": "example",
6697
6697
  "id": "deutsch_algo__e2edemo",
6698
- "code": "namespace Kata {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Math;\n\n @EntryPoint()\n operation DeutschAlgorithmDemo () : Unit {\n for (oracle, name) in [(PhaseOracle_Zero, \"f(x) = 0\"), \n (PhaseOracle_One, \"f(x) = 1\"),\n (PhaseOracle_X, \"f(x) = x\"), \n (PhaseOracle_OneMinusX, \"f(x) = 1-x\")] {\n let isConstant = DeutschAlgorithm(oracle);\n Message($\"{name} identified as {isConstant ? \"constant\" | \"variable\"}\");\n }\n }\n\n operation PhaseOracle_Zero (x : Qubit) : Unit {\n }\n\n operation PhaseOracle_One (x : Qubit) : Unit {\n R(PauliI, 2.0 * PI(), x);\n }\n\n operation PhaseOracle_X (x : Qubit) : Unit {\n Z(x);\n }\n\n operation PhaseOracle_OneMinusX(x : Qubit) : Unit {\n Z(x);\n R(PauliI, 2.0 * PI(), x);\n }\n\n operation DeutschAlgorithm (oracle : Qubit => Unit) : Bool {\n use x = Qubit();\n H(x);\n oracle(x);\n H(x);\n return MResetZ(x) == Zero;\n }\n}"
6698
+ "code": "namespace Kata {\n import Std.Diagnostics.*;\n import Std.Math.*;\n\n @EntryPoint()\n operation DeutschAlgorithmDemo() : Unit {\n for (oracle, name) in [\n (PhaseOracle_Zero, \"f(x) = 0\"),\n (PhaseOracle_One, \"f(x) = 1\"),\n (PhaseOracle_X, \"f(x) = x\"),\n (PhaseOracle_OneMinusX, \"f(x) = 1-x\")\n ] {\n let isConstant = DeutschAlgorithm(oracle);\n Message($\"{name} identified as {isConstant ? \"constant\" | \"variable\"}\");\n }\n }\n\n operation PhaseOracle_Zero(x : Qubit) : Unit {}\n\n operation PhaseOracle_One(x : Qubit) : Unit {\n R(PauliI, 2.0 * PI(), x);\n }\n\n operation PhaseOracle_X(x : Qubit) : Unit {\n Z(x);\n }\n\n operation PhaseOracle_OneMinusX(x : Qubit) : Unit {\n Z(x);\n R(PauliI, 2.0 * PI(), x);\n }\n\n operation DeutschAlgorithm(oracle : Qubit => Unit) : Bool {\n use x = Qubit();\n H(x);\n oracle(x);\n H(x);\n return MResetZ(x) == Zero;\n }\n}\n"
6699
6699
  }
6700
6700
  ]
6701
6701
  },
@@ -6751,7 +6751,7 @@ export default {
6751
6751
  {
6752
6752
  "type": "example",
6753
6753
  "id": "deutsch_jozsa__oracle_implementations",
6754
- "code": "namespace Kata {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Math;\n\n operation PhaseOracle_Zero (x : Qubit[]) : Unit {\n // Do nothing...\n }\n\n operation PhaseOracle_One (x : Qubit[]) : Unit {\n // Apply a global phase of -1\n R(PauliI, 2.0 * PI(), x[0]);\n }\n\n operation PhaseOracle_Xmod2 (x : Qubit[]) : Unit {\n let N = Length(x);\n // Array elements are indexed 0 through Length(x) - 1, inclusive.\n Z(x[N - 1]);\n }\n\n @EntryPoint()\n operation OracleImplementationDemo () : Unit {\n use qs = Qubit[2];\n Ry(2.0 * ArcCos(0.5), qs[0]);\n Ry(2.0 * ArcCos(0.6), qs[1]);\n Message(\"The state before oracle application:\");\n DumpMachine();\n\n // Apply the oracle.\n // Experiment with using other oracles to see their behavior!\n // (Note that the -1 global phase might not show up in simulation)\n PhaseOracle_Xmod2(qs);\n\n Message(\"The state after oracle application:\");\n DumpMachine();\n\n ResetAll(qs);\n }\n}"
6754
+ "code": "namespace Kata {\n import Std.Diagnostics.*;\n import Std.Math.*;\n\n operation PhaseOracle_Zero(x : Qubit[]) : Unit {\n // Do nothing...\n }\n\n operation PhaseOracle_One(x : Qubit[]) : Unit {\n // Apply a global phase of -1\n R(PauliI, 2.0 * PI(), x[0]);\n }\n\n operation PhaseOracle_Xmod2(x : Qubit[]) : Unit {\n let N = Length(x);\n // Array elements are indexed 0 through Length(x) - 1, inclusive.\n Z(x[N - 1]);\n }\n\n @EntryPoint()\n operation OracleImplementationDemo() : Unit {\n use qs = Qubit[2];\n Ry(2.0 * ArcCos(0.5), qs[0]);\n Ry(2.0 * ArcCos(0.6), qs[1]);\n Message(\"The state before oracle application:\");\n DumpMachine();\n\n // Apply the oracle.\n // Experiment with using other oracles to see their behavior!\n // (Note that the -1 global phase might not show up in simulation)\n PhaseOracle_Xmod2(qs);\n\n Message(\"The state after oracle application:\");\n DumpMachine();\n\n ResetAll(qs);\n }\n}\n"
6755
6755
  }
6756
6756
  ]
6757
6757
  },
@@ -6862,7 +6862,7 @@ export default {
6862
6862
  {
6863
6863
  "type": "example",
6864
6864
  "id": "deutsch_jozsa__e2edemo",
6865
- "code": "namespace Kata {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Math;\n\n @EntryPoint()\n operation DeutschJozsaAlgorithmDemo () : Unit {\n for (N, oracle, name) in [(2, qs => (), \"f(x) = 0\"), \n (2, qs => R(PauliI, 2.0 * PI(), qs[0]), \"f(x) = 1\"),\n (3, qs => Z(qs[Length(qs) - 1]), \"f(x) = x mod 2\"), \n (3, qs => Z(qs[0]), \"f(x) = most significant bit of x\"),\n (3, ApplyToEach(Z, _), \"f(x) = parity of x\")] {\n let isConstant = DeutschJozsaAlgorithm(N, oracle);\n Message($\"{name} identified as {isConstant ? \"constant\" | \"balanced\"}\");\n }\n }\n\n operation DeutschJozsaAlgorithm (N : Int, oracle : Qubit[] => Unit) : Bool {\n mutable isConstant = true;\n use x = Qubit[N];\n ApplyToEach(H, x);\n oracle(x);\n ApplyToEach(H, x);\n for xi in x {\n if MResetZ(xi) == One {\n set isConstant = false;\n }\n }\n return isConstant;\n }\n}"
6865
+ "code": "namespace Kata {\n import Std.Diagnostics.*;\n import Std.Math.*;\n\n @EntryPoint()\n operation DeutschJozsaAlgorithmDemo() : Unit {\n for (N, oracle, name) in [\n (2, qs => (), \"f(x) = 0\"),\n (2, qs => R(PauliI, 2.0 * PI(), qs[0]), \"f(x) = 1\"),\n (3, qs => Z(qs[Length(qs) - 1]), \"f(x) = x mod 2\"),\n (3, qs => Z(qs[0]), \"f(x) = most significant bit of x\"),\n (3, ApplyToEach(Z, _), \"f(x) = parity of x\")\n ] {\n let isConstant = DeutschJozsaAlgorithm(N, oracle);\n Message($\"{name} identified as {isConstant ? \"constant\" | \"balanced\"}\");\n }\n }\n\n operation DeutschJozsaAlgorithm(N : Int, oracle : Qubit[] => Unit) : Bool {\n mutable isConstant = true;\n use x = Qubit[N];\n ApplyToEach(H, x);\n oracle(x);\n ApplyToEach(H, x);\n for xi in x {\n if MResetZ(xi) == One {\n set isConstant = false;\n }\n }\n return isConstant;\n }\n}\n"
6866
6866
  }
6867
6867
  ]
6868
6868
  },
@@ -6941,7 +6941,7 @@ export default {
6941
6941
  "items": [
6942
6942
  {
6943
6943
  "type": "text-content",
6944
- "content": "<p>The problem solved by Grover's search algorithm is called the <em>search problem</em> and can be formulated as follows.\nYou are given a classical function that takes an $n$-bit input and returns a one-bit output $f(x): \\{0, 1\\}^n \\to \\{0, 1\\}$.\nThe task is to find an input $x_0$ for which $f(x_0) = 1$.</p>\n<p>Importantly, you don't have any information about the internal structure of the function $f$! It is given to you as a &quot;black box&quot; that allows you to evaluate the value of the function for any input you want, but not to learn anything about how it works.</p>\n<blockquote>\n<p>This problem is sometimes described as <em>function inversion</em>, since it tries to evaluate the inverse of the function $f$.</p>\n</blockquote>\n<h3>Classical solution</h3>\n<p>If you solve the search problem classically, how many calls to the given function will you need?</p>\n<p>Since you don't know anything about the internal structure of the function, you can't do better than the brute force approach.\nYou need to try evaluating the function on different inputs until you either hit the input which produces the desired output or run out of inputs to try and conclude that the desired input doesn't exist.\nThis requires $O(2^n)$ function evaluations, since in the worst case scenario you'll need to try all inputs.</p>\n<h3>Example problems</h3>\n<p>Any problem that allows you to check whether a given value of $x$ is a solution to it can be formulated as a search problem,\nif you define $f(x)$ as &quot;$1$ if $x$ is a solution to the problem, and $0$ otherwise&quot;.</p>\n<p>Some of the problems can be formulated as a search problem more naturally than the others.\nFor example:</p>\n<ul>\n<li>The Boolean satisfiability problem aims to find an assignment of variables in the given Boolean formula\nfor which the formula evaluates to true (or to decide that such assignment doesn't exist).\nThis is exactly the definition of the search problem, with the input $x$ defined as the set of variables used in the formula and $f(x)$ - as the formula itself.</li>\n<li>Vertex coloring problem aims to find an assignment of colors to the vertices of a given graph that would satisfy the given constraints. In this case, $x$ describes the colors assigned to the vertices, and $f(x)$ is $1$ if the constraints are satisfied or $0$ otherwise.</li>\n</ul>\n<p>You will learn more about appliyng Grover's search to solving specific problems in later katas.\nThis kata focuses on Grover's search algorithm itself rather than on applying it to a specific problem, so\nit uses a very simple function definition as an example.</p>\n<p>Let's define a fixed bit string $p$ of length $P$. The function $f(x)$ is $1$ if the bit string $x$ starts with $p$, and $0$ otherwise.</p>\n<p>This function has a very simple implementation and will allow you to experiment with instances of the search problem with different parameters easily.\nFor example, if the length of the bit string $p$ equals the length of the input, the function $f(x) = 1$ if $x = p$, and $0$ otherwise. In this case, the equation $f(x) = 1$ has exactly one solution, the bit string $p$ itself.</p>\n"
6944
+ "content": "<p>The problem solved by Grover's search algorithm is called the <em>search problem</em> and can be formulated as follows.\nYou are given a classical function that takes an $n$-bit input and returns a one-bit output $f(x): \\{0, 1\\}^n \\to \\{0, 1\\}$.\nThe task is to find an input $x_0$ for which $f(x_0) = 1$.</p>\n<p>Importantly, you don't have any information about the internal structure of the function $f$! It is given to you as a &quot;black box&quot; that allows you to evaluate the value of the function for any input you want, but not to learn anything about how it works.</p>\n<blockquote>\n<p>This problem is sometimes described as <em>function inversion</em>, since it tries to evaluate the inverse of the function $f$.</p>\n</blockquote>\n<h3>Classical solution</h3>\n<p>If you solve the search problem classically, how many calls to the given function will you need?</p>\n<p>Since you don't know anything about the internal structure of the function, you can't do better than the brute force approach.\nYou need to try evaluating the function on different inputs until you either hit the input which produces the desired output or run out of inputs to try and conclude that the desired input doesn't exist.\nThis requires $O(2^n)$ function evaluations, since in the worst case scenario you'll need to try all inputs.</p>\n<h3>Example problems</h3>\n<p>Any problem that allows you to check whether a given value of $x$ is a solution to it can be formulated as a search problem,\nif you define $f(x)$ as &quot;$1$ if $x$ is a solution to the problem, and $0$ otherwise&quot;.</p>\n<p>Some of the problems can be formulated as a search problem more naturally than the others.\nFor example:</p>\n<ul>\n<li>The Boolean satisfiability problem aims to find an assignment of variables in the given Boolean formula\nfor which the formula evaluates to true (or to decide that such assignment doesn't exist).\nThis is exactly the definition of the search problem, with the input $x$ defined as the set of variables used in the formula and $f(x)$ - as the formula itself.</li>\n<li>Vertex coloring problem aims to find an assignment of colors to the vertices of a given graph that would satisfy the given constraints. In this case, $x$ describes the colors assigned to the vertices, and $f(x)$ is $1$ if the constraints are satisfied or $0$ otherwise.</li>\n</ul>\n<p>You will learn more about applying Grover's search to solving specific problems in later katas.\nThis kata focuses on Grover's search algorithm itself rather than on applying it to a specific problem, so\nit uses a very simple function definition as an example.</p>\n<p>Let's define a fixed bit string $p$ of length $P$. The function $f(x)$ is $1$ if the bit string $x$ starts with $p$, and $0$ otherwise.</p>\n<p>This function has a very simple implementation and will allow you to experiment with instances of the search problem with different parameters easily.\nFor example, if the length of the bit string $p$ equals the length of the input, the function $f(x) = 1$ if $x = p$, and $0$ otherwise. In this case, the equation $f(x) = 1$ has exactly one solution, the bit string $p$ itself.</p>\n"
6945
6945
  }
6946
6946
  ]
6947
6947
  },
@@ -6980,7 +6980,7 @@ export default {
6980
6980
  "items": [
6981
6981
  {
6982
6982
  "type": "text-content",
6983
- "content": "<h3>Inputs</h3>\n<p>You are given the number of bits in the function input $n$ and the phase oracle for the problem we're solving - a &quot;black box&quot; quantum operation $U_f$ that implements a classical function $f(x)$.</p>\n<p>As usual, the phase oracle $U_f$ is defined by its effect on the individual values $x$ (represented as basis states $\\ket{x}$).\nIf the value of the function on the input $x$ $f(x) = 1$, the corresponding basis state $\\ket{x}$ is multiplied by $-1$; otherwise, the basis state is not changed.\nFormally, this can be written as follows:</p>\n<p>$$U_f \\ket{x} = (-1)^{f(x)} \\ket{x}$$</p>\n<blockquote>\n<p>Typically the oracle for Grover's search is implemented as a marking oracle and then converted into a phase oracle using the phase kickback trick.</p>\n</blockquote>\n<h3>Algorithm outline</h3>\n<p>The high-level outline of the algorithm is very simple:</p>\n<ol>\n<li>Initialize the quantum system to a well-known starting state.</li>\n<li>Apply a fixed sequence of &quot;Grover iterations&quot; several times. Each iteration is implemented as pair of operations that includes one call of the oracle &quot;black box&quot;.</li>\n<li>Finally, measuring all qubits will produce the desired output with high probability.</li>\n</ol>\n<p>Let's take a closer look at the algorithm.</p>\n<blockquote>\n<p>We will use a convenient visualization of the algorithm steps rather than mathematical derivation.\nThey are equivalent, but the visual representation is much easier to follow.</p>\n</blockquote>\n<h3>Initial state and definitions</h3>\n<p>Grover's search algorithm begins with a uniform superposition of all the states in the search space.\nTypically, the search space is defined as all $n$-bit bit strings, so this superposition is just an even superposition\nof all $N = 2^n$ basis states on $n$ qubits:\n$$\\ket{\\text{all}} = \\frac{1}{\\sqrt{N}}\\sum_{x=0}^{N-1}{\\ket{x}} $$</p>\n<p>When this superposition is considered in the context of the equation $f(x) = 1$,\nall the basis states can be split in two groups: &quot;good&quot; (solutions) and &quot;bad&quot; (non-solutions).\nIf the number of states for which $f(x)=1$ (the number of equation solutions) is $M$,\ntwo uniform superpositions of &quot;good&quot; and &quot;bad&quot; states can be defined as follows:</p>\n<p>$$\\ket{\\text{good}} = \\frac{1}{\\sqrt{M}}\\sum_{x,f(x)=1}{\\ket{x}}$$\n$$\\ket{\\text{bad}} = \\frac{1}{\\sqrt{N-M}}\\sum_{x,f(x)=0}{\\ket{x}}$$</p>\n<p>Now, the even superposition of all basis states can be rewritten as follows:\n$$\\ket{\\text{all}} = \\sqrt{\\frac{M}{N}}\\ket{\\text{good}} + \\sqrt{\\frac{N-M}{N}}\\ket{\\text{bad}}$$</p>\n<p>The amplutudes $\\sqrt{\\frac{M}{N}}$ and $\\sqrt{\\frac{N-M}{N}}$ can then be written in a trigonometric representation,\nas a sine and cosine of the angle $\\theta$:</p>\n<p>$$\\sin \\theta = \\sqrt{\\frac{M}{N}}, \\cos \\theta = \\sqrt{\\frac{N-M}{N}}$$</p>\n<p>With this replacement, the initial state can be written as</p>\n<p>$$\\ket{\\text{all}} = \\sin \\theta \\ket{\\text{good}} + \\cos \\theta \\ket{\\text{bad}}$$</p>\n<p>The states involved in the algorithm can be represented on a plane on which $\\ket{\\text{good}}$ and $\\ket{\\text{bad}}$ vectors correspond to vertical and horizontal axes, respectively.</p>\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<svg width=\"447\" height=\"370\" overflow=\"hidden\" version=\"1.1\" xml:space=\"preserve\" xmlns=\"http://www.w3.org/2000/svg\">\n<style>\n/* Default values, or theme set explicitly to light */\n:root, [data-theme=\"light\"] {\n --kata-svg-stroke: #222;\n --kata-svg-fill: #fff;\n --kata-svg-path: #777;\n --kata-svg-accent: #06c;\n}\n/* User has set OS preference for dark. (An explict light theme will override) */\n@media(prefers-color-scheme: dark) {\n :root {\n --kata-svg-stroke: #eee;\n --kata-svg-fill: #111;\n --kata-svg-path: #bbb;\n --kata-svg-accent: #08f;\n }\n}\n/* Explicit dark theme set (should match above dark preference values) */\n[data-theme=\"dark\"] {\n --kata-svg-stroke: #eee;\n --kata-svg-fill: #111;\n --kata-svg-path: #bbb;\n --kata-svg-accent: #08f;\n}\n/*** Kata specific styles ***/\n.kata_svg_path {\n stroke: var(--kata-svg-path);\n stroke-width: 2;\n stroke-linecap: round;\n fill: none;\n}\n.kata_svg_text {\n fill: var(--kata-svg-stroke);\n}\n.kata_svg_point {\n fill: var(--kata-svg-fill);\n stroke: var(--kata-svg-stroke);\n}\n.kata_svg_fill_accent {\n fill: var(--kata-svg-accent);\n}\n.kata_svg_stroke_accent {\n stroke: var(--kata-svg-accent);\n}\n</style>\n <g transform=\"matrix(.25 0 0 .25 -333 -132)\">\n <g class=\"kata_svg_path\">\n <!-- Main circle -->\n <circle cx=\"2020\" cy=\"1310\" r=\"670\" />\n <!-- x axis -->\n <path d=\"M2685 1310 h-1335 m1335 0 l-32 -32 m32 32 l-32 32\" />\n <!-- y axis -->\n <path d=\"m2020 645 v1323 m0 -1323 l-32 32 m32 -32 l32 32\" />\n <!-- Angle line -->\n <path d=\"m2665 1145 l-645 165 m645 -165 l-32 45 m32 -45 l -45 -25\" />\n <path d=\"m2357 1225c6.73 27 10.1 54.7 10.1 82.5\" fill-rule=\"evenodd\" />\n </g>\n <g class=\"kata_svg_text\">\n <!-- |all> ket -->\n <path d=\"m2735 1098h6.92v83.6h-6.92z\" />\n <path d=\"m2782 1122c-3.09 0-5.86 1.25-8.31 3.73-2.46 2.49-4.37 5.82-5.75 10s-2.07 8.35-2.07 12.5c0 3.11 0.43 5.44 1.28 6.98 0.86 1.55 2.29 2.32 4.29 2.32 1.98 0 3.92-0.86 5.82-2.56 1.9-1.71 3.86-4.14 5.88-7.3 2.03-3.16 3.46-6.81 4.29-10.9l0.41-1.93c0.27-1.23 0.45-2.29 0.54-3.19s0.13-1.87 0.13-2.92c0-2.31-0.49-4-1.48-5.08s-2.66-1.62-5.03-1.62zm-0.9-3.54c1.95 0 3.77 0.21 5.46 0.65 1.69 0.43 3.44 1.2 5.23 2.31l4.14-2.96 2.87 0.71-6.6 28.5c-0.66 2.85-0.99 5.07-0.99 6.65 0 1.2 0.19 2.07 0.58 2.61s1.01 0.81 1.84 0.81c0.9 0 1.86-0.37 2.88-1.1 1.02-0.74 2.45-2.14 4.31-4.2l2.56 2.51c-2.69 2.88-4.99 4.92-6.89 6.13-1.9 1.22-3.96 1.82-6.18 1.82-1.86 0-3.34-0.61-4.45-1.84s-1.66-2.86-1.66-4.9c0-1.64 0.34-3.39 1.03-5.25l-0.58-0.18c-2.88 4.19-5.6 7.26-8.18 9.21-2.57 1.94-5.3 2.92-8.17 2.92-3.3 0-5.85-1.24-7.66-3.73s-2.72-5.98-2.72-10.5c0-5.15 1.01-10.1 3.03-14.8 2.02-4.75 4.8-8.49 8.34-11.2 3.53-2.74 7.47-4.11 11.8-4.11zm75.2-20.5h2.65l-11 49.7c-0.66 2.97-0.99 5.19-0.99 6.65 0 1.2 0.2 2.07 0.58 2.61 0.39 0.54 1.01 0.81 1.85 0.81 0.89 0 1.85-0.37 2.87-1.1 1.02-0.74 2.46-2.14 4.31-4.2l2.56 2.51c-2.9 3.06-5.27 5.15-7.12 6.27-1.84 1.12-3.9 1.68-6.17 1.68-1.95 0-3.51-0.71-4.67-2.13-1.17-1.42-1.76-3.24-1.76-5.46 0-2.01 0.42-4.91 1.26-8.71l8.04-36c0.36-1.58 0.62-2.85 0.77-3.79 0.15-0.95 0.22-1.82 0.22-2.63 0-0.9-0.16-1.58-0.49-2.04-0.33-0.47-0.8-0.81-1.42-1.01-0.61-0.21-1.71-0.36-3.3-0.45l0.54-2.34zm-29 0h2.65l-11 49.7c-0.66 2.97-0.99 5.19-0.99 6.65 0 1.2 0.2 2.07 0.58 2.61 0.39 0.54 1.01 0.81 1.85 0.81 0.89 0 1.85-0.37 2.87-1.1 1.02-0.74 2.46-2.14 4.31-4.2l2.56 2.51c-2.9 3.06-5.27 5.15-7.12 6.27-1.84 1.12-3.9 1.68-6.17 1.68-1.95 0-3.51-0.71-4.67-2.13-1.17-1.42-1.76-3.24-1.76-5.46 0-2.01 0.42-4.91 1.26-8.71l8.04-36c0.36-1.58 0.62-2.85 0.77-3.79 0.15-0.95 0.22-1.82 0.22-2.63 0-0.9-0.16-1.58-0.49-2.04-0.33-0.47-0.8-0.81-1.42-1.01-0.61-0.21-1.71-0.36-3.3-0.45l0.54-2.34z\" />\n <path d=\"m2875 1098 17.9 40.3v3.32l-17.9 40.4-4.81-1.71 14.4-40.3-14.4-40.4z\" />\n <!-- |good> ket -->\n <path d=\"m1889 530h6.91v83.6h-6.91z\" />\n <path d=\"m2040 554c-3.11 0-5.83 1.3-8.15 3.91-2.32 2.6-4.15 6.33-5.48 11.2-1.33 4.85-2 9.03-2 12.5 0 3.29 0.68 5.73 2.05 7.3 1.36 1.57 3.42 2.36 6.17 2.36 2.94 0 5.54-1.32 7.82-3.95 2.28-2.64 4.07-6.33 5.39-11.1 1.32-4.75 1.98-8.92 1.98-12.5 0-3.24-0.63-5.67-1.89-7.3s-3.22-2.45-5.89-2.45zm-49 0c-3.11 0-5.83 1.3-8.15 3.91-2.32 2.6-4.15 6.33-5.48 11.2-1.33 4.85-2 9.03-2 12.5 0 3.29 0.68 5.73 2.05 7.3 1.36 1.57 3.42 2.36 6.17 2.36 2.94 0 5.54-1.32 7.82-3.95 2.28-2.64 4.07-6.33 5.39-11.1 1.32-4.75 1.98-8.92 1.98-12.5 0-3.24-0.63-5.67-1.89-7.3s-3.22-2.45-5.89-2.45zm98.3-0.09c-3.05 0-5.79 1.24-8.22 3.71-2.42 2.47-4.33 5.8-5.72 10-1.4 4.19-2.09 8.37-2.09 12.5 0 3.12 0.42 5.44 1.28 6.99 0.85 1.54 2.28 2.31 4.29 2.31 0.93 0 1.87-0.202 2.83-0.606 0.96-0.405 1.95-1.06 2.99-1.96 1.03-0.898 2.05-1.99 3.05-3.28s1.92-2.58 2.76-3.89c0.84-1.3 1.62-2.84 2.34-4.63 0.72-1.78 1.33-3.93 1.84-6.45l0.4-1.93c0.45-2.16 0.68-4.19 0.68-6.11 0-2.34-0.5-4.04-1.48-5.1-0.99-1.06-2.64-1.6-4.95-1.6zm-148 0c-3.05 0-5.79 1.24-8.22 3.71-2.42 2.47-4.33 5.8-5.72 10-1.4 4.19-2.09 8.37-2.09 12.5 0 3.12 0.45 5.44 1.35 6.99 0.89 1.54 2.3 2.31 4.22 2.31 1.53 0 3.05-0.524 4.56-1.57s3.13-2.71 4.85-4.99 3.06-4.45 4.02-6.51c0.96-2.07 1.77-4.64 2.45-7.73 0.67-3.08 1.01-5.76 1.01-8.04 0-2.34-0.5-4.04-1.48-5.1-0.99-1.06-2.64-1.6-4.95-1.6zm100-3.5c4.8 0 8.45 1.31 11 3.93 2.52 2.62 3.78 6.37 3.78 11.3 0 3.47-0.61 7.19-1.82 11.1s-2.88 7.28-4.99 9.97c-2.11 2.7-4.63 4.72-7.57 6.09-2.93 1.36-6.3 2.04-10.1 2.04-4.65 0-8.25-1.3-10.8-3.91-2.56-2.6-3.84-6.35-3.84-11.2 0-2.4 0.36-5.17 1.08-8.31 0.93-4.25 2.48-7.97 4.67-11.1 2.19-3.18 4.88-5.61 8.09-7.3 3.2-1.69 6.72-2.54 10.6-2.54zm-49 0c4.8 0 8.45 1.31 11 3.93 2.52 2.62 3.78 6.37 3.78 11.3 0 3.47-0.61 7.19-1.82 11.1s-2.88 7.28-4.99 9.97c-2.11 2.7-4.63 4.72-7.57 6.09-2.93 1.36-6.3 2.04-10.1 2.04-4.65 0-8.25-1.3-10.8-3.91-2.56-2.6-3.84-6.35-3.84-11.2 0-2.4 0.36-5.17 1.08-8.31 0.93-4.25 2.48-7.97 4.67-11.1 2.19-3.18 4.88-5.61 8.09-7.3 3.2-1.69 6.72-2.54 10.6-2.54zm-51.7-0.045c1.88 0 3.66 0.225 5.34 0.674s3.37 1.21 5.08 2.29l4.13-2.96 2.88 0.719-4.63 20.9c-0.33 1.47-1.08 5.17-2.25 11.1-1.23 6.29-2.22 10.7-2.98 13.3-0.77 2.6-1.72 4.98-2.86 7.12s-2.6 4-4.4 5.59c-1.8 1.59-3.98 2.82-6.54 3.68-2.56 0.869-5.59 1.3-9.09 1.3-11.3 0-17-3.29-17-9.88 0-1.68 0.51-3.2 1.52-4.58 1.02-1.38 2.65-2.83 4.9-4.36l3.24 2.6c-0.57 0.689-1.05 1.37-1.44 2.04s-0.67 1.32-0.83 1.95c-0.17 0.629-0.25 1.38-0.25 2.25 0 2.25 0.81 3.89 2.43 4.92 1.61 1.03 4.05 1.55 7.32 1.55 3.68 0 6.64-0.831 8.87-2.49s4.14-4.34 5.73-8.02c1.58-3.68 3.13-9.25 4.62-16.7l-0.62-0.134c-2.91 3.98-5.6 6.89-8.09 8.72-2.49 1.83-5.17 2.74-8.04 2.74-3.27 0-5.8-1.23-7.62-3.68-1.81-2.46-2.71-5.96-2.71-10.5 0-5.12 1-10 3.01-14.8 2-4.72 4.79-8.47 8.35-11.3 3.57-2.78 7.53-4.18 11.9-4.18zm168-20.5h2.65l-11.2 49.7c-0.66 2.94-0.99 5.15-0.99 6.65 0 1.2 0.2 2.07 0.59 2.6s1 0.809 1.84 0.809c0.9 0 1.86-0.367 2.87-1.1 1.02-0.734 2.46-2.13 4.32-4.2l2.56 2.52c-2.76 2.9-5.08 4.96-6.97 6.15-1.88 1.2-3.92 1.8-6.1 1.8-1.86 0-3.34-0.614-4.45-1.84s-1.66-2.86-1.66-4.9c0-1.65 0.34-3.4 1.03-5.26l-0.59-0.18c-2.87 4.19-5.6 7.26-8.17 9.21-2.58 1.95-5.3 2.92-8.18 2.92-3.29 0-5.84-1.24-7.66-3.73-1.81-2.48-2.71-5.97-2.71-10.5 0-5.15 1.01-10.1 3.03-14.8s4.8-8.49 8.33-11.2c3.54-2.74 7.47-4.11 11.8-4.11 1.82 0 3.51 0.187 5.05 0.562 1.54 0.374 3.12 1.02 4.74 1.95l2.33-10.4c0.24-0.989 0.45-2.08 0.63-3.28s0.27-2.25 0.27-3.14c0-0.899-0.16-1.58-0.49-2.04s-0.8-0.801-1.42-1.01c-0.61-0.209-1.71-0.359-3.3-0.449l0.54-2.34z\" />\n <path d=\"m2126 529 17.9 40.3v3.32l-17.9 40.4-4.81-1.71 14.4-40.3-14.4-40.4z\" />\n <!-- |bad> ket -->\n <path d=\"m2766 1275h6.92v83.6h-6.92z\" />\n <path d=\"m2816 1301c-1.43 0-2.85 0.46-4.24 1.37s-2.94 2.43-4.65 4.54-3.1 4.27-4.18 6.49-1.99 5.05-2.74 8.49l-0.4 1.89c-0.45 2.09-0.68 4.13-0.68 6.11 0 2.36 0.52 4.07 1.55 5.12 1.04 1.05 2.75 1.57 5.15 1.57 1.22 0 2.39-0.2 3.5-0.61 1.11-0.4 2.24-1.07 3.39-2 1.15-0.92 2.25-2.14 3.28-3.66 1.03-1.51 1.99-3.37 2.85-5.59 0.87-2.22 1.55-4.59 2.03-7.12 0.47-2.53 0.71-4.95 0.71-7.25 0-3.12-0.44-5.46-1.32-7.01-0.88-1.56-2.3-2.34-4.25-2.34zm97.4-1.75c-3.06 0-5.8 1.23-8.22 3.71-2.43 2.47-4.34 5.8-5.73 9.99s-2.09 8.37-2.09 12.5c0 3.12 0.43 5.45 1.28 6.99s2.29 2.31 4.29 2.31c0.93 0 1.87-0.2 2.83-0.6 0.96-0.41 1.96-1.06 2.99-1.96s2.05-1.99 3.05-3.28c1.01-1.29 1.93-2.58 2.77-3.88 0.83-1.31 1.61-2.85 2.33-4.63s1.33-3.93 1.84-6.45l0.41-1.93c0.45-2.15 0.67-4.19 0.67-6.11 0-2.33-0.49-4.03-1.48-5.1-0.99-1.06-2.64-1.59-4.94-1.59zm-50.9 0c-3.09 0-5.86 1.24-8.31 3.73-2.46 2.48-4.37 5.82-5.75 9.99-1.38 4.18-2.07 8.35-2.07 12.5 0 3.12 0.43 5.45 1.28 6.99s2.29 2.31 4.29 2.31c1.98 0 3.92-0.85 5.82-2.56 1.9-1.7 3.86-4.14 5.88-7.3 2.03-3.16 3.46-6.8 4.29-10.9l0.41-1.93c0.27-1.23 0.45-2.29 0.54-3.19s0.13-1.87 0.13-2.92c0-2.3-0.49-4-1.48-5.07-0.99-1.08-2.67-1.62-5.03-1.62zm-0.9-3.55c1.95 0 3.77 0.22 5.46 0.65 1.69 0.44 3.43 1.21 5.23 2.32l4.13-2.97 2.88 0.72-6.6 28.5c-0.66 2.84-0.99 5.06-0.99 6.65 0 1.2 0.19 2.06 0.58 2.6s1 0.81 1.84 0.81c0.9 0 1.86-0.36 2.88-1.1 1.02-0.73 2.45-2.13 4.31-4.2l2.56 2.52c-2.69 2.87-4.99 4.92-6.89 6.13-1.91 1.21-3.97 1.82-6.18 1.82-1.86 0-3.34-0.62-4.45-1.84-1.11-1.23-1.66-2.86-1.66-4.9 0-1.65 0.34-3.4 1.03-5.26l-0.58-0.18c-2.88 4.2-5.6 7.27-8.18 9.21-2.57 1.95-5.3 2.92-8.17 2.92-3.3 0-5.85-1.24-7.66-3.73-1.81-2.48-2.72-5.97-2.72-10.5 0-5.15 1.01-10.1 3.03-14.8s4.8-8.49 8.34-11.2c3.53-2.74 7.47-4.11 11.8-4.11zm70.6-20.5h2.65l-11.2 49.7c-0.66 2.93-0.99 5.15-0.99 6.65 0 1.2 0.19 2.06 0.58 2.6s1 0.81 1.84 0.81c0.9 0 1.86-0.36 2.88-1.1 1.02-0.73 2.45-2.13 4.31-4.2l2.56 2.52c-2.75 2.9-5.08 4.95-6.96 6.15-1.89 1.2-3.93 1.8-6.11 1.8-1.86 0-3.34-0.62-4.45-1.84-1.11-1.23-1.66-2.86-1.66-4.9 0-1.65 0.34-3.4 1.03-5.26l-0.58-0.18c-2.88 4.2-5.6 7.27-8.18 9.21-2.57 1.95-5.3 2.92-8.17 2.92-3.3 0-5.85-1.24-7.66-3.73-1.81-2.48-2.72-5.97-2.72-10.5 0-5.15 1.01-10.1 3.03-14.8s4.8-8.49 8.34-11.2c3.53-2.74 7.47-4.11 11.8-4.11 1.83 0 3.51 0.19 5.05 0.56 1.55 0.38 3.13 1.03 4.74 1.96l2.34-10.4c0.24-0.99 0.45-2.08 0.63-3.28s0.27-2.25 0.27-3.14c0-0.9-0.17-1.58-0.5-2.05-0.33-0.46-0.8-0.8-1.41-1.01-0.62-0.21-1.72-0.36-3.3-0.45l0.54-2.33zm-124 0h2.61l-7.91 31.4 0.59 0.18c2.9-3.9 5.63-6.71 8.17-8.45 2.55-1.74 5.09-2.6 7.64-2.6 3.26 0 5.8 1.24 7.61 3.72 1.81 2.49 2.72 5.99 2.72 10.5 0 4.94-1 9.8-2.99 14.6-1.99 4.78-4.75 8.57-8.29 11.4-3.53 2.8-7.48 4.2-11.8 4.2-3.99 0-7.52-0.98-10.6-2.92l-4.13 2.92-2.87-0.72 11.6-51.4c0.69-3.05 1.03-5.27 1.03-6.64 0-1.26-0.38-2.15-1.14-2.66-0.77-0.5-2.14-0.79-4.11-0.85l0.54-2.33z\" />\n <path d=\"m2950 1275 17.9 40.3v3.33l-17.9 40.4-4.8-1.71 14.4-40.3-14.4-40.4z\" />\n <!-- theta-->\n <path d=\"m2414 1260c-1.4 6.49-2.11 12-2.11 16.4 0 3.36 0.51 5.82 1.53 7.39s2.67 2.36 4.94 2.36c3.36 0 6.54-2.25 9.55-6.76s5.56-11 7.66-19.4zm18.4-30.3c-3.5 0-6.74 2.22-9.72 6.65s-5.52 10.8-7.62 19h21.5c1.41-6.31 2.11-11.8 2.11-16.3 0-3.15-0.51-5.5-1.53-7.06-1.01-1.55-2.6-2.33-4.76-2.33zm0.54-3.64c8.69 0 13 5.51 13 16.5 0 3.12-0.35 6.84-1.06 11.2-0.7 4.33-1.82 8.74-3.34 13.2-1.53 4.5-3.46 8.47-5.8 11.9-2.33 3.46-5.03 6.13-8.08 8.02-3.06 1.89-6.35 2.83-9.89 2.83-4.46 0-7.78-1.36-9.97-4.09-2.19-2.72-3.28-6.81-3.28-12.3 0-2.97 0.33-6.62 0.99-11 0.66-4.35 1.74-8.79 3.26-13.3 1.51-4.54 3.45-8.57 5.84-12.1 2.38-3.52 5.12-6.23 8.22-8.13s6.46-2.85 10.1-2.85z\" fill-rule=\"evenodd\" />\n </g>\n <g class=\"kata_svg_fill_accent kata_svg_stroke_accent\">\n <!-- Started state square braket -->\n <path d=\"m2722 1082v-49h89v-49 49h89v49\" fill=\"none\" stroke-width=\"6\" />\n <text transform=\"translate(2605 952)\" font-family=\"sans-serif\" font-size=\"64px\" font-weight=\"400\">The starting state</text>\n </g>\n </g>\n</svg>\n<h3>Grover's iteration</h3>\n<p>Each Grover's iteration consists of two operations.</p>\n<ol>\n<li>The phase oracle $U_f$.</li>\n<li>An operation called &quot;reflection about the mean&quot;.</li>\n</ol>\n<p>Applying the phase oracle to the state will flip the sign of all basis states in $\\ket{\\text{good}}$\nand leave all basis states in $\\ket{\\text{bad}}$ unchanged:</p>\n<p>$$U_f\\ket{\\text{good}} = -\\ket{\\text{good}}$$\n$$U_f\\ket{\\text{bad}} = \\ket{\\text{bad}}$$</p>\n<p>On the circle plot, this transformation leaves the horizontal component of the state vector unchanged and reverses its vertical component. In other words, this operation is a reflection along the horizontal axis.</p>\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<svg width=\"447\" height=\"370\" overflow=\"hidden\" version=\"1.1\" xml:space=\"preserve\" xmlns=\"http://www.w3.org/2000/svg\">\n<style>\n/* Default values, or theme set explicitly to light */\n:root, [data-theme=\"light\"] {\n --kata-svg-stroke: #222;\n --kata-svg-fill: #fff;\n --kata-svg-path: #777;\n --kata-svg-accent: #06c;\n}\n/* User has set OS preference for dark. (An explict light theme will override) */\n@media(prefers-color-scheme: dark) {\n :root {\n --kata-svg-stroke: #eee;\n --kata-svg-fill: #111;\n --kata-svg-path: #bbb;\n --kata-svg-accent: #08f;\n }\n}\n/* Explicit dark theme set (should match above dark preference values) */\n[data-theme=\"dark\"] {\n --kata-svg-stroke: #eee;\n --kata-svg-fill: #111;\n --kata-svg-path: #bbb;\n --kata-svg-accent: #08f;\n}\n/*** Kata specific styles ***/\n.kata_svg_path {\n stroke: var(--kata-svg-path);\n stroke-width: 2;\n stroke-linecap: round;\n fill: none;\n}\n.kata_svg_text {\n fill: var(--kata-svg-stroke);\n}\n.kata_svg_point {\n fill: var(--kata-svg-fill);\n stroke: var(--kata-svg-stroke);\n}\n.kata_svg_fill_accent {\n fill: var(--kata-svg-accent);\n}\n.kata_svg_stroke_accent {\n stroke: var(--kata-svg-accent);\n}\n</style>\n <g transform=\"matrix(.25 0 0 .25 -333 -132)\">\n <g class=\"kata_svg_path\">\n <!-- Main circle -->\n <circle cx=\"2020\" cy=\"1310\" r=\"670\" />\n <!-- x axis -->\n <path d=\"M2685 1310 h-1335 m1335 0 l-32 -32 m32 32 l-32 32\" />\n <!-- y axis -->\n <path d=\"m2020 645 v1323 m0 -1323 l-32 32 m32 -32 l32 32\" />\n <!-- Angle line -->\n <path d=\"m2665 1145 l-645 165 m645 -165 l-32 45 m32 -45 l -45 -25\" />\n <path d=\"m2357 1225c6.73 27 10.1 54.7 10.1 82.5\" fill-rule=\"evenodd\" />\n <!-- lower angle line -->\n <path d=\"m2665 1475 l-645 -165 m645 165 l-32 -45 m32 45 l -45 25\" class=\"kata_svg_stroke_accent\"/>\n <!-- lower angle curve -->\n <path transform=\"matrix(1 0 0 -1 1677 1655)\" d=\"m679 259c7.22 28.1 10.9 56.9 10.9 85.9\" class=\"kata_svg_stroke_accent\"/>\n </g>\n <g class=\"kata_svg_text\">\n <!-- |all> ket -->\n <path d=\"m2735 1098h6.92v83.6h-6.92z\" />\n <path d=\"m2782 1122c-3.09 0-5.86 1.25-8.31 3.73-2.46 2.49-4.37 5.82-5.75 10s-2.07 8.35-2.07 12.5c0 3.11 0.43 5.44 1.28 6.98 0.86 1.55 2.29 2.32 4.29 2.32 1.98 0 3.92-0.86 5.82-2.56 1.9-1.71 3.86-4.14 5.88-7.3 2.03-3.16 3.46-6.81 4.29-10.9l0.41-1.93c0.27-1.23 0.45-2.29 0.54-3.19s0.13-1.87 0.13-2.92c0-2.31-0.49-4-1.48-5.08s-2.66-1.62-5.03-1.62zm-0.9-3.54c1.95 0 3.77 0.21 5.46 0.65 1.69 0.43 3.44 1.2 5.23 2.31l4.14-2.96 2.87 0.71-6.6 28.5c-0.66 2.85-0.99 5.07-0.99 6.65 0 1.2 0.19 2.07 0.58 2.61s1.01 0.81 1.84 0.81c0.9 0 1.86-0.37 2.88-1.1 1.02-0.74 2.45-2.14 4.31-4.2l2.56 2.51c-2.69 2.88-4.99 4.92-6.89 6.13-1.9 1.22-3.96 1.82-6.18 1.82-1.86 0-3.34-0.61-4.45-1.84s-1.66-2.86-1.66-4.9c0-1.64 0.34-3.39 1.03-5.25l-0.58-0.18c-2.88 4.19-5.6 7.26-8.18 9.21-2.57 1.94-5.3 2.92-8.17 2.92-3.3 0-5.85-1.24-7.66-3.73s-2.72-5.98-2.72-10.5c0-5.15 1.01-10.1 3.03-14.8 2.02-4.75 4.8-8.49 8.34-11.2 3.53-2.74 7.47-4.11 11.8-4.11zm75.2-20.5h2.65l-11 49.7c-0.66 2.97-0.99 5.19-0.99 6.65 0 1.2 0.2 2.07 0.58 2.61 0.39 0.54 1.01 0.81 1.85 0.81 0.89 0 1.85-0.37 2.87-1.1 1.02-0.74 2.46-2.14 4.31-4.2l2.56 2.51c-2.9 3.06-5.27 5.15-7.12 6.27-1.84 1.12-3.9 1.68-6.17 1.68-1.95 0-3.51-0.71-4.67-2.13-1.17-1.42-1.76-3.24-1.76-5.46 0-2.01 0.42-4.91 1.26-8.71l8.04-36c0.36-1.58 0.62-2.85 0.77-3.79 0.15-0.95 0.22-1.82 0.22-2.63 0-0.9-0.16-1.58-0.49-2.04-0.33-0.47-0.8-0.81-1.42-1.01-0.61-0.21-1.71-0.36-3.3-0.45l0.54-2.34zm-29 0h2.65l-11 49.7c-0.66 2.97-0.99 5.19-0.99 6.65 0 1.2 0.2 2.07 0.58 2.61 0.39 0.54 1.01 0.81 1.85 0.81 0.89 0 1.85-0.37 2.87-1.1 1.02-0.74 2.46-2.14 4.31-4.2l2.56 2.51c-2.9 3.06-5.27 5.15-7.12 6.27-1.84 1.12-3.9 1.68-6.17 1.68-1.95 0-3.51-0.71-4.67-2.13-1.17-1.42-1.76-3.24-1.76-5.46 0-2.01 0.42-4.91 1.26-8.71l8.04-36c0.36-1.58 0.62-2.85 0.77-3.79 0.15-0.95 0.22-1.82 0.22-2.63 0-0.9-0.16-1.58-0.49-2.04-0.33-0.47-0.8-0.81-1.42-1.01-0.61-0.21-1.71-0.36-3.3-0.45l0.54-2.34z\" />\n <path d=\"m2875 1098 17.9 40.3v3.32l-17.9 40.4-4.81-1.71 14.4-40.3-14.4-40.4z\" />\n <!-- |good> ket -->\n <path d=\"m1889 530h6.91v83.6h-6.91z\" />\n <path d=\"m2040 554c-3.11 0-5.83 1.3-8.15 3.91-2.32 2.6-4.15 6.33-5.48 11.2-1.33 4.85-2 9.03-2 12.5 0 3.29 0.68 5.73 2.05 7.3 1.36 1.57 3.42 2.36 6.17 2.36 2.94 0 5.54-1.32 7.82-3.95 2.28-2.64 4.07-6.33 5.39-11.1 1.32-4.75 1.98-8.92 1.98-12.5 0-3.24-0.63-5.67-1.89-7.3s-3.22-2.45-5.89-2.45zm-49 0c-3.11 0-5.83 1.3-8.15 3.91-2.32 2.6-4.15 6.33-5.48 11.2-1.33 4.85-2 9.03-2 12.5 0 3.29 0.68 5.73 2.05 7.3 1.36 1.57 3.42 2.36 6.17 2.36 2.94 0 5.54-1.32 7.82-3.95 2.28-2.64 4.07-6.33 5.39-11.1 1.32-4.75 1.98-8.92 1.98-12.5 0-3.24-0.63-5.67-1.89-7.3s-3.22-2.45-5.89-2.45zm98.3-0.09c-3.05 0-5.79 1.24-8.22 3.71-2.42 2.47-4.33 5.8-5.72 10-1.4 4.19-2.09 8.37-2.09 12.5 0 3.12 0.42 5.44 1.28 6.99 0.85 1.54 2.28 2.31 4.29 2.31 0.93 0 1.87-0.202 2.83-0.606 0.96-0.405 1.95-1.06 2.99-1.96 1.03-0.898 2.05-1.99 3.05-3.28s1.92-2.58 2.76-3.89c0.84-1.3 1.62-2.84 2.34-4.63 0.72-1.78 1.33-3.93 1.84-6.45l0.4-1.93c0.45-2.16 0.68-4.19 0.68-6.11 0-2.34-0.5-4.04-1.48-5.1-0.99-1.06-2.64-1.6-4.95-1.6zm-148 0c-3.05 0-5.79 1.24-8.22 3.71-2.42 2.47-4.33 5.8-5.72 10-1.4 4.19-2.09 8.37-2.09 12.5 0 3.12 0.45 5.44 1.35 6.99 0.89 1.54 2.3 2.31 4.22 2.31 1.53 0 3.05-0.524 4.56-1.57s3.13-2.71 4.85-4.99 3.06-4.45 4.02-6.51c0.96-2.07 1.77-4.64 2.45-7.73 0.67-3.08 1.01-5.76 1.01-8.04 0-2.34-0.5-4.04-1.48-5.1-0.99-1.06-2.64-1.6-4.95-1.6zm100-3.5c4.8 0 8.45 1.31 11 3.93 2.52 2.62 3.78 6.37 3.78 11.3 0 3.47-0.61 7.19-1.82 11.1s-2.88 7.28-4.99 9.97c-2.11 2.7-4.63 4.72-7.57 6.09-2.93 1.36-6.3 2.04-10.1 2.04-4.65 0-8.25-1.3-10.8-3.91-2.56-2.6-3.84-6.35-3.84-11.2 0-2.4 0.36-5.17 1.08-8.31 0.93-4.25 2.48-7.97 4.67-11.1 2.19-3.18 4.88-5.61 8.09-7.3 3.2-1.69 6.72-2.54 10.6-2.54zm-49 0c4.8 0 8.45 1.31 11 3.93 2.52 2.62 3.78 6.37 3.78 11.3 0 3.47-0.61 7.19-1.82 11.1s-2.88 7.28-4.99 9.97c-2.11 2.7-4.63 4.72-7.57 6.09-2.93 1.36-6.3 2.04-10.1 2.04-4.65 0-8.25-1.3-10.8-3.91-2.56-2.6-3.84-6.35-3.84-11.2 0-2.4 0.36-5.17 1.08-8.31 0.93-4.25 2.48-7.97 4.67-11.1 2.19-3.18 4.88-5.61 8.09-7.3 3.2-1.69 6.72-2.54 10.6-2.54zm-51.7-0.045c1.88 0 3.66 0.225 5.34 0.674s3.37 1.21 5.08 2.29l4.13-2.96 2.88 0.719-4.63 20.9c-0.33 1.47-1.08 5.17-2.25 11.1-1.23 6.29-2.22 10.7-2.98 13.3-0.77 2.6-1.72 4.98-2.86 7.12s-2.6 4-4.4 5.59c-1.8 1.59-3.98 2.82-6.54 3.68-2.56 0.869-5.59 1.3-9.09 1.3-11.3 0-17-3.29-17-9.88 0-1.68 0.51-3.2 1.52-4.58 1.02-1.38 2.65-2.83 4.9-4.36l3.24 2.6c-0.57 0.689-1.05 1.37-1.44 2.04s-0.67 1.32-0.83 1.95c-0.17 0.629-0.25 1.38-0.25 2.25 0 2.25 0.81 3.89 2.43 4.92 1.61 1.03 4.05 1.55 7.32 1.55 3.68 0 6.64-0.831 8.87-2.49s4.14-4.34 5.73-8.02c1.58-3.68 3.13-9.25 4.62-16.7l-0.62-0.134c-2.91 3.98-5.6 6.89-8.09 8.72-2.49 1.83-5.17 2.74-8.04 2.74-3.27 0-5.8-1.23-7.62-3.68-1.81-2.46-2.71-5.96-2.71-10.5 0-5.12 1-10 3.01-14.8 2-4.72 4.79-8.47 8.35-11.3 3.57-2.78 7.53-4.18 11.9-4.18zm168-20.5h2.65l-11.2 49.7c-0.66 2.94-0.99 5.15-0.99 6.65 0 1.2 0.2 2.07 0.59 2.6s1 0.809 1.84 0.809c0.9 0 1.86-0.367 2.87-1.1 1.02-0.734 2.46-2.13 4.32-4.2l2.56 2.52c-2.76 2.9-5.08 4.96-6.97 6.15-1.88 1.2-3.92 1.8-6.1 1.8-1.86 0-3.34-0.614-4.45-1.84s-1.66-2.86-1.66-4.9c0-1.65 0.34-3.4 1.03-5.26l-0.59-0.18c-2.87 4.19-5.6 7.26-8.17 9.21-2.58 1.95-5.3 2.92-8.18 2.92-3.29 0-5.84-1.24-7.66-3.73-1.81-2.48-2.71-5.97-2.71-10.5 0-5.15 1.01-10.1 3.03-14.8s4.8-8.49 8.33-11.2c3.54-2.74 7.47-4.11 11.8-4.11 1.82 0 3.51 0.187 5.05 0.562 1.54 0.374 3.12 1.02 4.74 1.95l2.33-10.4c0.24-0.989 0.45-2.08 0.63-3.28s0.27-2.25 0.27-3.14c0-0.899-0.16-1.58-0.49-2.04s-0.8-0.801-1.42-1.01c-0.61-0.209-1.71-0.359-3.3-0.449l0.54-2.34z\" />\n <path d=\"m2126 529 17.9 40.3v3.32l-17.9 40.4-4.81-1.71 14.4-40.3-14.4-40.4z\" />\n <!-- |bad> ket -->\n <path d=\"m2766 1275h6.92v83.6h-6.92z\" />\n <path d=\"m2816 1301c-1.43 0-2.85 0.46-4.24 1.37s-2.94 2.43-4.65 4.54-3.1 4.27-4.18 6.49-1.99 5.05-2.74 8.49l-0.4 1.89c-0.45 2.09-0.68 4.13-0.68 6.11 0 2.36 0.52 4.07 1.55 5.12 1.04 1.05 2.75 1.57 5.15 1.57 1.22 0 2.39-0.2 3.5-0.61 1.11-0.4 2.24-1.07 3.39-2 1.15-0.92 2.25-2.14 3.28-3.66 1.03-1.51 1.99-3.37 2.85-5.59 0.87-2.22 1.55-4.59 2.03-7.12 0.47-2.53 0.71-4.95 0.71-7.25 0-3.12-0.44-5.46-1.32-7.01-0.88-1.56-2.3-2.34-4.25-2.34zm97.4-1.75c-3.06 0-5.8 1.23-8.22 3.71-2.43 2.47-4.34 5.8-5.73 9.99s-2.09 8.37-2.09 12.5c0 3.12 0.43 5.45 1.28 6.99s2.29 2.31 4.29 2.31c0.93 0 1.87-0.2 2.83-0.6 0.96-0.41 1.96-1.06 2.99-1.96s2.05-1.99 3.05-3.28c1.01-1.29 1.93-2.58 2.77-3.88 0.83-1.31 1.61-2.85 2.33-4.63s1.33-3.93 1.84-6.45l0.41-1.93c0.45-2.15 0.67-4.19 0.67-6.11 0-2.33-0.49-4.03-1.48-5.1-0.99-1.06-2.64-1.59-4.94-1.59zm-50.9 0c-3.09 0-5.86 1.24-8.31 3.73-2.46 2.48-4.37 5.82-5.75 9.99-1.38 4.18-2.07 8.35-2.07 12.5 0 3.12 0.43 5.45 1.28 6.99s2.29 2.31 4.29 2.31c1.98 0 3.92-0.85 5.82-2.56 1.9-1.7 3.86-4.14 5.88-7.3 2.03-3.16 3.46-6.8 4.29-10.9l0.41-1.93c0.27-1.23 0.45-2.29 0.54-3.19s0.13-1.87 0.13-2.92c0-2.3-0.49-4-1.48-5.07-0.99-1.08-2.67-1.62-5.03-1.62zm-0.9-3.55c1.95 0 3.77 0.22 5.46 0.65 1.69 0.44 3.43 1.21 5.23 2.32l4.13-2.97 2.88 0.72-6.6 28.5c-0.66 2.84-0.99 5.06-0.99 6.65 0 1.2 0.19 2.06 0.58 2.6s1 0.81 1.84 0.81c0.9 0 1.86-0.36 2.88-1.1 1.02-0.73 2.45-2.13 4.31-4.2l2.56 2.52c-2.69 2.87-4.99 4.92-6.89 6.13-1.91 1.21-3.97 1.82-6.18 1.82-1.86 0-3.34-0.62-4.45-1.84-1.11-1.23-1.66-2.86-1.66-4.9 0-1.65 0.34-3.4 1.03-5.26l-0.58-0.18c-2.88 4.2-5.6 7.27-8.18 9.21-2.57 1.95-5.3 2.92-8.17 2.92-3.3 0-5.85-1.24-7.66-3.73-1.81-2.48-2.72-5.97-2.72-10.5 0-5.15 1.01-10.1 3.03-14.8s4.8-8.49 8.34-11.2c3.53-2.74 7.47-4.11 11.8-4.11zm70.6-20.5h2.65l-11.2 49.7c-0.66 2.93-0.99 5.15-0.99 6.65 0 1.2 0.19 2.06 0.58 2.6s1 0.81 1.84 0.81c0.9 0 1.86-0.36 2.88-1.1 1.02-0.73 2.45-2.13 4.31-4.2l2.56 2.52c-2.75 2.9-5.08 4.95-6.96 6.15-1.89 1.2-3.93 1.8-6.11 1.8-1.86 0-3.34-0.62-4.45-1.84-1.11-1.23-1.66-2.86-1.66-4.9 0-1.65 0.34-3.4 1.03-5.26l-0.58-0.18c-2.88 4.2-5.6 7.27-8.18 9.21-2.57 1.95-5.3 2.92-8.17 2.92-3.3 0-5.85-1.24-7.66-3.73-1.81-2.48-2.72-5.97-2.72-10.5 0-5.15 1.01-10.1 3.03-14.8s4.8-8.49 8.34-11.2c3.53-2.74 7.47-4.11 11.8-4.11 1.83 0 3.51 0.19 5.05 0.56 1.55 0.38 3.13 1.03 4.74 1.96l2.34-10.4c0.24-0.99 0.45-2.08 0.63-3.28s0.27-2.25 0.27-3.14c0-0.9-0.17-1.58-0.5-2.05-0.33-0.46-0.8-0.8-1.41-1.01-0.62-0.21-1.72-0.36-3.3-0.45l0.54-2.33zm-124 0h2.61l-7.91 31.4 0.59 0.18c2.9-3.9 5.63-6.71 8.17-8.45 2.55-1.74 5.09-2.6 7.64-2.6 3.26 0 5.8 1.24 7.61 3.72 1.81 2.49 2.72 5.99 2.72 10.5 0 4.94-1 9.8-2.99 14.6-1.99 4.78-4.75 8.57-8.29 11.4-3.53 2.8-7.48 4.2-11.8 4.2-3.99 0-7.52-0.98-10.6-2.92l-4.13 2.92-2.87-0.72 11.6-51.4c0.69-3.05 1.03-5.27 1.03-6.64 0-1.26-0.38-2.15-1.14-2.66-0.77-0.5-2.14-0.79-4.11-0.85l0.54-2.33z\" />\n <path d=\"m2950 1275 17.9 40.3v3.33l-17.9 40.4-4.8-1.71 14.4-40.3-14.4-40.4z\" />\n <!-- theta-->\n <path d=\"m2414 1260c-1.4 6.49-2.11 12-2.11 16.4 0 3.36 0.51 5.82 1.53 7.39s2.67 2.36 4.94 2.36c3.36 0 6.54-2.25 9.55-6.76s5.56-11 7.66-19.4zm18.4-30.3c-3.5 0-6.74 2.22-9.72 6.65s-5.52 10.8-7.62 19h21.5c1.41-6.31 2.11-11.8 2.11-16.3 0-3.15-0.51-5.5-1.53-7.06-1.01-1.55-2.6-2.33-4.76-2.33zm0.54-3.64c8.69 0 13 5.51 13 16.5 0 3.12-0.35 6.84-1.06 11.2-0.7 4.33-1.82 8.74-3.34 13.2-1.53 4.5-3.46 8.47-5.8 11.9-2.33 3.46-5.03 6.13-8.08 8.02-3.06 1.89-6.35 2.83-9.89 2.83-4.46 0-7.78-1.36-9.97-4.09-2.19-2.72-3.28-6.81-3.28-12.3 0-2.97 0.33-6.62 0.99-11 0.66-4.35 1.74-8.79 3.26-13.3 1.51-4.54 3.45-8.57 5.84-12.1 2.38-3.52 5.12-6.23 8.22-8.13s6.46-2.85 10.1-2.85z\" fill-rule=\"evenodd\" />\n </g>\n <g class=\"kata_svg_fill_accent kata_svg_stroke_accent\" font-family=\"sans-serif\" font-size=\"64px\" font-weight=\"400\">\n <!-- theta -->\n <path d=\"m2415 1364c-1.41 6.5-2.11 12-2.11 16.4 0 3.35 0.51 5.82 1.52 7.39 1.02 1.57 2.67 2.36 4.95 2.36 3.35 0 6.53-2.26 9.54-6.76 3.01-4.51 5.56-11 7.66-19.4zm18.4-30.3c-3.51 0-6.75 2.21-9.73 6.64-2.98 4.44-5.52 10.8-7.61 19h21.5c1.41-6.32 2.12-11.8 2.12-16.3 0-3.14-0.51-5.49-1.53-7.05s-2.61-2.33-4.76-2.33zm0.54-3.64c8.68 0 13 5.51 13 16.5 0 3.11-0.35 6.83-1.05 11.2-0.71 4.33-1.82 8.74-3.35 13.2s-3.46 8.47-5.79 11.9c-2.34 3.46-5.04 6.13-8.09 8.02-3.06 1.88-6.35 2.83-9.88 2.83-4.47 0-7.79-1.37-9.98-4.09-2.18-2.73-3.28-6.82-3.28-12.3 0-2.96 0.33-6.61 0.99-11 0.66-4.34 1.75-8.78 3.26-13.3 1.51-4.53 3.46-8.56 5.84-12.1s5.12-6.23 8.22-8.13 6.46-2.85 10.1-2.85z\" />\n <text transform=\"translate(2734 1521)\">State after</text>\n <text transform=\"translate(2734 1598)\">the first </text>\n <text transform=\"translate(2734 1676)\">reflection</text>\n </g>\n </g>\n</svg>\n<p>&quot;Reflection about the mean&quot; is an operation for which the visual definition is much more intuitive than the mathematical one.\nIt is literally a reflection about the state $\\ket{\\text{all}}$ - the uniform superposition of all basis states in the search space.</p>\n<p>Mathematically, this operation is described as $2\\ket{\\text{all}}\\bra{\\text{all}} - I$: it leaves the component of the input state parallel to the state $\\ket{\\text{all}}$ unchanged and multiplies the component orthogonal to it by $-1$.</p>\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<svg width=\"447\" height=\"370\" overflow=\"hidden\" version=\"1.1\" xml:space=\"preserve\" xmlns=\"http://www.w3.org/2000/svg\">\n<style>\n/* Default values, or theme set explicitly to light */\n:root, [data-theme=\"light\"] {\n --kata-svg-stroke: #222;\n --kata-svg-fill: #fff;\n --kata-svg-path: #777;\n --kata-svg-accent: #06c;\n}\n/* User has set OS preference for dark. (An explict light theme will override) */\n@media(prefers-color-scheme: dark) {\n :root {\n --kata-svg-stroke: #eee;\n --kata-svg-fill: #111;\n --kata-svg-path: #bbb;\n --kata-svg-accent: #08f;\n }\n}\n/* Explicit dark theme set (should match above dark preference values) */\n[data-theme=\"dark\"] {\n --kata-svg-stroke: #eee;\n --kata-svg-fill: #111;\n --kata-svg-path: #bbb;\n --kata-svg-accent: #08f;\n}\n/*** Kata specific styles ***/\n.kata_svg_path {\n stroke: var(--kata-svg-path);\n stroke-width: 2;\n stroke-linecap: round;\n fill: none;\n}\n.kata_svg_text {\n fill: var(--kata-svg-stroke);\n}\n.kata_svg_point {\n fill: var(--kata-svg-fill);\n stroke: var(--kata-svg-stroke);\n}\n.kata_svg_fill_accent {\n fill: var(--kata-svg-accent);\n}\n.kata_svg_stroke_accent {\n stroke: var(--kata-svg-accent);\n}\n</style>\n <g transform=\"matrix(.25 0 0 .25 -333 -132)\">\n <g class=\"kata_svg_path\">\n <!-- Main circle -->\n <circle cx=\"2020\" cy=\"1310\" r=\"670\" />\n <!-- x axis -->\n <path d=\"M2685 1310 h-1335 m1335 0 l-32 -32 m32 32 l-32 32\" />\n <!-- y axis -->\n <path d=\"m2020 645 v1323 m0 -1323 l-32 32 m32 -32 l32 32\" />\n <!-- Angle line -->\n <path d=\"m2665 1145 l-645 165 m645 -165 l-32 45 m32 -45 l -45 -25\" />\n <path d=\"m2357 1225c6.73 27 10.1 54.7 10.1 82.5\" fill-rule=\"evenodd\" />\n <!-- lower angle line -->\n <path d=\"m2665 1475 l-645 -165 m645 165 l-32 -45 m32 45 l -45 25\" stroke-width=\"10\" stroke-linecap=\"round\" />\n <!-- lower angle curve -->\n <path transform=\"matrix(1 0 0 -1 1677 1655)\" d=\"m679 259c7.22 28.1 10.9 56.9 10.9 85.9\" fill=\"none\" fill-rule=\"evenodd\" stroke-width=\"10\" />\n <path d=\"M2490 845 l-470 465 m470 -465 l-50 0 m50 0 l0 50\" class=\"kata_svg_stroke_accent\"/>\n <!-- Accented angle line -->\n <path d=\"m2270 1065c43.2 45.4 73.5 102 88 160\" class=\"kata_svg_stroke_accent\"/>\n </g>\n <g class=\"kata_svg_text\">\n <!-- |all> ket -->\n <path d=\"m2735 1098h6.92v83.6h-6.92z\" />\n <path d=\"m2782 1122c-3.09 0-5.86 1.25-8.31 3.73-2.46 2.49-4.37 5.82-5.75 10s-2.07 8.35-2.07 12.5c0 3.11 0.43 5.44 1.28 6.98 0.86 1.55 2.29 2.32 4.29 2.32 1.98 0 3.92-0.86 5.82-2.56 1.9-1.71 3.86-4.14 5.88-7.3 2.03-3.16 3.46-6.81 4.29-10.9l0.41-1.93c0.27-1.23 0.45-2.29 0.54-3.19s0.13-1.87 0.13-2.92c0-2.31-0.49-4-1.48-5.08s-2.66-1.62-5.03-1.62zm-0.9-3.54c1.95 0 3.77 0.21 5.46 0.65 1.69 0.43 3.44 1.2 5.23 2.31l4.14-2.96 2.87 0.71-6.6 28.5c-0.66 2.85-0.99 5.07-0.99 6.65 0 1.2 0.19 2.07 0.58 2.61s1.01 0.81 1.84 0.81c0.9 0 1.86-0.37 2.88-1.1 1.02-0.74 2.45-2.14 4.31-4.2l2.56 2.51c-2.69 2.88-4.99 4.92-6.89 6.13-1.9 1.22-3.96 1.82-6.18 1.82-1.86 0-3.34-0.61-4.45-1.84s-1.66-2.86-1.66-4.9c0-1.64 0.34-3.39 1.03-5.25l-0.58-0.18c-2.88 4.19-5.6 7.26-8.18 9.21-2.57 1.94-5.3 2.92-8.17 2.92-3.3 0-5.85-1.24-7.66-3.73s-2.72-5.98-2.72-10.5c0-5.15 1.01-10.1 3.03-14.8 2.02-4.75 4.8-8.49 8.34-11.2 3.53-2.74 7.47-4.11 11.8-4.11zm75.2-20.5h2.65l-11 49.7c-0.66 2.97-0.99 5.19-0.99 6.65 0 1.2 0.2 2.07 0.58 2.61 0.39 0.54 1.01 0.81 1.85 0.81 0.89 0 1.85-0.37 2.87-1.1 1.02-0.74 2.46-2.14 4.31-4.2l2.56 2.51c-2.9 3.06-5.27 5.15-7.12 6.27-1.84 1.12-3.9 1.68-6.17 1.68-1.95 0-3.51-0.71-4.67-2.13-1.17-1.42-1.76-3.24-1.76-5.46 0-2.01 0.42-4.91 1.26-8.71l8.04-36c0.36-1.58 0.62-2.85 0.77-3.79 0.15-0.95 0.22-1.82 0.22-2.63 0-0.9-0.16-1.58-0.49-2.04-0.33-0.47-0.8-0.81-1.42-1.01-0.61-0.21-1.71-0.36-3.3-0.45l0.54-2.34zm-29 0h2.65l-11 49.7c-0.66 2.97-0.99 5.19-0.99 6.65 0 1.2 0.2 2.07 0.58 2.61 0.39 0.54 1.01 0.81 1.85 0.81 0.89 0 1.85-0.37 2.87-1.1 1.02-0.74 2.46-2.14 4.31-4.2l2.56 2.51c-2.9 3.06-5.27 5.15-7.12 6.27-1.84 1.12-3.9 1.68-6.17 1.68-1.95 0-3.51-0.71-4.67-2.13-1.17-1.42-1.76-3.24-1.76-5.46 0-2.01 0.42-4.91 1.26-8.71l8.04-36c0.36-1.58 0.62-2.85 0.77-3.79 0.15-0.95 0.22-1.82 0.22-2.63 0-0.9-0.16-1.58-0.49-2.04-0.33-0.47-0.8-0.81-1.42-1.01-0.61-0.21-1.71-0.36-3.3-0.45l0.54-2.34z\" />\n <path d=\"m2875 1098 17.9 40.3v3.32l-17.9 40.4-4.81-1.71 14.4-40.3-14.4-40.4z\" />\n <!-- |good> ket -->\n <path d=\"m1889 530h6.91v83.6h-6.91z\" />\n <path d=\"m2040 554c-3.11 0-5.83 1.3-8.15 3.91-2.32 2.6-4.15 6.33-5.48 11.2-1.33 4.85-2 9.03-2 12.5 0 3.29 0.68 5.73 2.05 7.3 1.36 1.57 3.42 2.36 6.17 2.36 2.94 0 5.54-1.32 7.82-3.95 2.28-2.64 4.07-6.33 5.39-11.1 1.32-4.75 1.98-8.92 1.98-12.5 0-3.24-0.63-5.67-1.89-7.3s-3.22-2.45-5.89-2.45zm-49 0c-3.11 0-5.83 1.3-8.15 3.91-2.32 2.6-4.15 6.33-5.48 11.2-1.33 4.85-2 9.03-2 12.5 0 3.29 0.68 5.73 2.05 7.3 1.36 1.57 3.42 2.36 6.17 2.36 2.94 0 5.54-1.32 7.82-3.95 2.28-2.64 4.07-6.33 5.39-11.1 1.32-4.75 1.98-8.92 1.98-12.5 0-3.24-0.63-5.67-1.89-7.3s-3.22-2.45-5.89-2.45zm98.3-0.09c-3.05 0-5.79 1.24-8.22 3.71-2.42 2.47-4.33 5.8-5.72 10-1.4 4.19-2.09 8.37-2.09 12.5 0 3.12 0.42 5.44 1.28 6.99 0.85 1.54 2.28 2.31 4.29 2.31 0.93 0 1.87-0.202 2.83-0.606 0.96-0.405 1.95-1.06 2.99-1.96 1.03-0.898 2.05-1.99 3.05-3.28s1.92-2.58 2.76-3.89c0.84-1.3 1.62-2.84 2.34-4.63 0.72-1.78 1.33-3.93 1.84-6.45l0.4-1.93c0.45-2.16 0.68-4.19 0.68-6.11 0-2.34-0.5-4.04-1.48-5.1-0.99-1.06-2.64-1.6-4.95-1.6zm-148 0c-3.05 0-5.79 1.24-8.22 3.71-2.42 2.47-4.33 5.8-5.72 10-1.4 4.19-2.09 8.37-2.09 12.5 0 3.12 0.45 5.44 1.35 6.99 0.89 1.54 2.3 2.31 4.22 2.31 1.53 0 3.05-0.524 4.56-1.57s3.13-2.71 4.85-4.99 3.06-4.45 4.02-6.51c0.96-2.07 1.77-4.64 2.45-7.73 0.67-3.08 1.01-5.76 1.01-8.04 0-2.34-0.5-4.04-1.48-5.1-0.99-1.06-2.64-1.6-4.95-1.6zm100-3.5c4.8 0 8.45 1.31 11 3.93 2.52 2.62 3.78 6.37 3.78 11.3 0 3.47-0.61 7.19-1.82 11.1s-2.88 7.28-4.99 9.97c-2.11 2.7-4.63 4.72-7.57 6.09-2.93 1.36-6.3 2.04-10.1 2.04-4.65 0-8.25-1.3-10.8-3.91-2.56-2.6-3.84-6.35-3.84-11.2 0-2.4 0.36-5.17 1.08-8.31 0.93-4.25 2.48-7.97 4.67-11.1 2.19-3.18 4.88-5.61 8.09-7.3 3.2-1.69 6.72-2.54 10.6-2.54zm-49 0c4.8 0 8.45 1.31 11 3.93 2.52 2.62 3.78 6.37 3.78 11.3 0 3.47-0.61 7.19-1.82 11.1s-2.88 7.28-4.99 9.97c-2.11 2.7-4.63 4.72-7.57 6.09-2.93 1.36-6.3 2.04-10.1 2.04-4.65 0-8.25-1.3-10.8-3.91-2.56-2.6-3.84-6.35-3.84-11.2 0-2.4 0.36-5.17 1.08-8.31 0.93-4.25 2.48-7.97 4.67-11.1 2.19-3.18 4.88-5.61 8.09-7.3 3.2-1.69 6.72-2.54 10.6-2.54zm-51.7-0.045c1.88 0 3.66 0.225 5.34 0.674s3.37 1.21 5.08 2.29l4.13-2.96 2.88 0.719-4.63 20.9c-0.33 1.47-1.08 5.17-2.25 11.1-1.23 6.29-2.22 10.7-2.98 13.3-0.77 2.6-1.72 4.98-2.86 7.12s-2.6 4-4.4 5.59c-1.8 1.59-3.98 2.82-6.54 3.68-2.56 0.869-5.59 1.3-9.09 1.3-11.3 0-17-3.29-17-9.88 0-1.68 0.51-3.2 1.52-4.58 1.02-1.38 2.65-2.83 4.9-4.36l3.24 2.6c-0.57 0.689-1.05 1.37-1.44 2.04s-0.67 1.32-0.83 1.95c-0.17 0.629-0.25 1.38-0.25 2.25 0 2.25 0.81 3.89 2.43 4.92 1.61 1.03 4.05 1.55 7.32 1.55 3.68 0 6.64-0.831 8.87-2.49s4.14-4.34 5.73-8.02c1.58-3.68 3.13-9.25 4.62-16.7l-0.62-0.134c-2.91 3.98-5.6 6.89-8.09 8.72-2.49 1.83-5.17 2.74-8.04 2.74-3.27 0-5.8-1.23-7.62-3.68-1.81-2.46-2.71-5.96-2.71-10.5 0-5.12 1-10 3.01-14.8 2-4.72 4.79-8.47 8.35-11.3 3.57-2.78 7.53-4.18 11.9-4.18zm168-20.5h2.65l-11.2 49.7c-0.66 2.94-0.99 5.15-0.99 6.65 0 1.2 0.2 2.07 0.59 2.6s1 0.809 1.84 0.809c0.9 0 1.86-0.367 2.87-1.1 1.02-0.734 2.46-2.13 4.32-4.2l2.56 2.52c-2.76 2.9-5.08 4.96-6.97 6.15-1.88 1.2-3.92 1.8-6.1 1.8-1.86 0-3.34-0.614-4.45-1.84s-1.66-2.86-1.66-4.9c0-1.65 0.34-3.4 1.03-5.26l-0.59-0.18c-2.87 4.19-5.6 7.26-8.17 9.21-2.58 1.95-5.3 2.92-8.18 2.92-3.29 0-5.84-1.24-7.66-3.73-1.81-2.48-2.71-5.97-2.71-10.5 0-5.15 1.01-10.1 3.03-14.8s4.8-8.49 8.33-11.2c3.54-2.74 7.47-4.11 11.8-4.11 1.82 0 3.51 0.187 5.05 0.562 1.54 0.374 3.12 1.02 4.74 1.95l2.33-10.4c0.24-0.989 0.45-2.08 0.63-3.28s0.27-2.25 0.27-3.14c0-0.899-0.16-1.58-0.49-2.04s-0.8-0.801-1.42-1.01c-0.61-0.209-1.71-0.359-3.3-0.449l0.54-2.34z\" />\n <path d=\"m2126 529 17.9 40.3v3.32l-17.9 40.4-4.81-1.71 14.4-40.3-14.4-40.4z\" />\n <!-- |bad> ket -->\n <path d=\"m2766 1275h6.92v83.6h-6.92z\" />\n <path d=\"m2816 1301c-1.43 0-2.85 0.46-4.24 1.37s-2.94 2.43-4.65 4.54-3.1 4.27-4.18 6.49-1.99 5.05-2.74 8.49l-0.4 1.89c-0.45 2.09-0.68 4.13-0.68 6.11 0 2.36 0.52 4.07 1.55 5.12 1.04 1.05 2.75 1.57 5.15 1.57 1.22 0 2.39-0.2 3.5-0.61 1.11-0.4 2.24-1.07 3.39-2 1.15-0.92 2.25-2.14 3.28-3.66 1.03-1.51 1.99-3.37 2.85-5.59 0.87-2.22 1.55-4.59 2.03-7.12 0.47-2.53 0.71-4.95 0.71-7.25 0-3.12-0.44-5.46-1.32-7.01-0.88-1.56-2.3-2.34-4.25-2.34zm97.4-1.75c-3.06 0-5.8 1.23-8.22 3.71-2.43 2.47-4.34 5.8-5.73 9.99s-2.09 8.37-2.09 12.5c0 3.12 0.43 5.45 1.28 6.99s2.29 2.31 4.29 2.31c0.93 0 1.87-0.2 2.83-0.6 0.96-0.41 1.96-1.06 2.99-1.96s2.05-1.99 3.05-3.28c1.01-1.29 1.93-2.58 2.77-3.88 0.83-1.31 1.61-2.85 2.33-4.63s1.33-3.93 1.84-6.45l0.41-1.93c0.45-2.15 0.67-4.19 0.67-6.11 0-2.33-0.49-4.03-1.48-5.1-0.99-1.06-2.64-1.59-4.94-1.59zm-50.9 0c-3.09 0-5.86 1.24-8.31 3.73-2.46 2.48-4.37 5.82-5.75 9.99-1.38 4.18-2.07 8.35-2.07 12.5 0 3.12 0.43 5.45 1.28 6.99s2.29 2.31 4.29 2.31c1.98 0 3.92-0.85 5.82-2.56 1.9-1.7 3.86-4.14 5.88-7.3 2.03-3.16 3.46-6.8 4.29-10.9l0.41-1.93c0.27-1.23 0.45-2.29 0.54-3.19s0.13-1.87 0.13-2.92c0-2.3-0.49-4-1.48-5.07-0.99-1.08-2.67-1.62-5.03-1.62zm-0.9-3.55c1.95 0 3.77 0.22 5.46 0.65 1.69 0.44 3.43 1.21 5.23 2.32l4.13-2.97 2.88 0.72-6.6 28.5c-0.66 2.84-0.99 5.06-0.99 6.65 0 1.2 0.19 2.06 0.58 2.6s1 0.81 1.84 0.81c0.9 0 1.86-0.36 2.88-1.1 1.02-0.73 2.45-2.13 4.31-4.2l2.56 2.52c-2.69 2.87-4.99 4.92-6.89 6.13-1.91 1.21-3.97 1.82-6.18 1.82-1.86 0-3.34-0.62-4.45-1.84-1.11-1.23-1.66-2.86-1.66-4.9 0-1.65 0.34-3.4 1.03-5.26l-0.58-0.18c-2.88 4.2-5.6 7.27-8.18 9.21-2.57 1.95-5.3 2.92-8.17 2.92-3.3 0-5.85-1.24-7.66-3.73-1.81-2.48-2.72-5.97-2.72-10.5 0-5.15 1.01-10.1 3.03-14.8s4.8-8.49 8.34-11.2c3.53-2.74 7.47-4.11 11.8-4.11zm70.6-20.5h2.65l-11.2 49.7c-0.66 2.93-0.99 5.15-0.99 6.65 0 1.2 0.19 2.06 0.58 2.6s1 0.81 1.84 0.81c0.9 0 1.86-0.36 2.88-1.1 1.02-0.73 2.45-2.13 4.31-4.2l2.56 2.52c-2.75 2.9-5.08 4.95-6.96 6.15-1.89 1.2-3.93 1.8-6.11 1.8-1.86 0-3.34-0.62-4.45-1.84-1.11-1.23-1.66-2.86-1.66-4.9 0-1.65 0.34-3.4 1.03-5.26l-0.58-0.18c-2.88 4.2-5.6 7.27-8.18 9.21-2.57 1.95-5.3 2.92-8.17 2.92-3.3 0-5.85-1.24-7.66-3.73-1.81-2.48-2.72-5.97-2.72-10.5 0-5.15 1.01-10.1 3.03-14.8s4.8-8.49 8.34-11.2c3.53-2.74 7.47-4.11 11.8-4.11 1.83 0 3.51 0.19 5.05 0.56 1.55 0.38 3.13 1.03 4.74 1.96l2.34-10.4c0.24-0.99 0.45-2.08 0.63-3.28s0.27-2.25 0.27-3.14c0-0.9-0.17-1.58-0.5-2.05-0.33-0.46-0.8-0.8-1.41-1.01-0.62-0.21-1.72-0.36-3.3-0.45l0.54-2.33zm-124 0h2.61l-7.91 31.4 0.59 0.18c2.9-3.9 5.63-6.71 8.17-8.45 2.55-1.74 5.09-2.6 7.64-2.6 3.26 0 5.8 1.24 7.61 3.72 1.81 2.49 2.72 5.99 2.72 10.5 0 4.94-1 9.8-2.99 14.6-1.99 4.78-4.75 8.57-8.29 11.4-3.53 2.8-7.48 4.2-11.8 4.2-3.99 0-7.52-0.98-10.6-2.92l-4.13 2.92-2.87-0.72 11.6-51.4c0.69-3.05 1.03-5.27 1.03-6.64 0-1.26-0.38-2.15-1.14-2.66-0.77-0.5-2.14-0.79-4.11-0.85l0.54-2.33z\" />\n <path d=\"m2950 1275 17.9 40.3v3.33l-17.9 40.4-4.8-1.71 14.4-40.3-14.4-40.4z\" />\n <!-- theta-->\n <path d=\"m2414 1260c-1.4 6.49-2.11 12-2.11 16.4 0 3.36 0.51 5.82 1.53 7.39s2.67 2.36 4.94 2.36c3.36 0 6.54-2.25 9.55-6.76s5.56-11 7.66-19.4zm18.4-30.3c-3.5 0-6.74 2.22-9.72 6.65s-5.52 10.8-7.62 19h21.5c1.41-6.31 2.11-11.8 2.11-16.3 0-3.15-0.51-5.5-1.53-7.06-1.01-1.55-2.6-2.33-4.76-2.33zm0.54-3.64c8.69 0 13 5.51 13 16.5 0 3.12-0.35 6.84-1.06 11.2-0.7 4.33-1.82 8.74-3.34 13.2-1.53 4.5-3.46 8.47-5.8 11.9-2.33 3.46-5.03 6.13-8.08 8.02-3.06 1.89-6.35 2.83-9.89 2.83-4.46 0-7.78-1.36-9.97-4.09-2.19-2.72-3.28-6.81-3.28-12.3 0-2.97 0.33-6.62 0.99-11 0.66-4.35 1.74-8.79 3.26-13.3 1.51-4.54 3.45-8.57 5.84-12.1 2.38-3.52 5.12-6.23 8.22-8.13s6.46-2.85 10.1-2.85z\" fill-rule=\"evenodd\" />\n <!-- lower theta -->\n <path d=\"m2415 1364c-1.41 6.5-2.11 12-2.11 16.4 0 3.35 0.51 5.82 1.52 7.39 1.02 1.57 2.67 2.36 4.95 2.36 3.35 0 6.53-2.26 9.54-6.76 3.01-4.51 5.56-11 7.66-19.4zm18.4-30.3c-3.51 0-6.75 2.21-9.73 6.64-2.98 4.44-5.52 10.8-7.61 19h21.5c1.41-6.32 2.12-11.8 2.12-16.3 0-3.14-0.51-5.49-1.53-7.05s-2.61-2.33-4.76-2.33zm0.54-3.64c8.68 0 13 5.51 13 16.5 0 3.11-0.35 6.83-1.05 11.2-0.71 4.33-1.82 8.74-3.35 13.2s-3.46 8.47-5.79 11.9c-2.34 3.46-5.04 6.13-8.09 8.02-3.06 1.88-6.35 2.83-9.88 2.83-4.47 0-7.79-1.37-9.98-4.09-2.18-2.73-3.28-6.82-3.28-12.3 0-2.96 0.33-6.61 0.99-11 0.66-4.34 1.75-8.78 3.26-13.3 1.51-4.53 3.46-8.56 5.84-12.1s5.12-6.23 8.22-8.13 6.46-2.85 10.1-2.85z\" />\n </g>\n <g class=\"kata_svg_fill_accent kata_svg_stroke_accent\" font-family=\"sans-serif\" font-size=\"64px\" font-weight=\"400\">\n <text transform=\"translate(2490 780)\">State after</text>\n <text transform=\"translate(2550 840)\">the second</text>\n <text transform=\"translate(2610 900)\">reflection</text>\n <!--2 \\theta and angle -->\n <path d=\"m2410 1105c-1.41 6.5-2.12 12-2.12 16.4 0 3.35 0.51 5.82 1.53 7.39s2.67 2.36 4.94 2.36c3.36 0 6.54-2.25 9.55-6.76s5.56-11 7.66-19.4zm18.4-30.3c-3.5 0-6.74 2.22-9.72 6.65s-5.52 10.8-7.62 19h21.5c1.41-6.32 2.11-11.8 2.11-16.3 0-3.14-0.51-5.5-1.52-7.05-1.02-1.56-2.61-2.34-4.77-2.34zm-58.2-2.7c5.57 0 9.77 1.22 12.6 3.66 2.84 2.44 4.27 5.98 4.27 10.6 0 1.59-0.16 3.05-0.48 4.38-0.31 1.33-0.8 2.65-1.48 3.95-0.67 1.3-1.58 2.7-2.72 4.2-1.13 1.5-2.39 3.02-3.77 4.56s-3.76 4.11-7.14 7.7c-5.69 6.02-9.87 11.3-12.5 15.8h16.9c1.47 0 2.65-0.09 3.55-0.27s1.62-0.479 2.16-0.898c0.54-0.42 0.96-0.966 1.28-1.64 0.31-0.674 0.71-1.72 1.19-3.12h3.82l-0.77 13.1h-37v-2.16c1.29-3.14 3.03-6.36 5.21-9.66 2.19-3.3 5.29-7.17 9.3-11.6 3.47-3.83 5.98-6.74 7.5-8.72 1.68-2.16 2.93-4.02 3.75-5.59 0.83-1.57 1.42-3.06 1.78-4.45s0.54-2.79 0.54-4.2c0-2.25-0.36-4.23-1.08-5.95s-1.82-3.08-3.3-4.07c-1.49-0.988-3.35-1.48-5.6-1.48-5.6 0-9.37 3.11-11.3 9.34h-5.43v-8.62c3.62-1.71 7.01-2.94 10.2-3.71 3.16-0.763 6-1.14 8.52-1.14zm58.7-0.943c8.69 0 13 5.51 13 16.5 0 3.11-0.35 6.84-1.06 11.2-0.7 4.33-1.82 8.74-3.34 13.2-1.53 4.49-3.46 8.47-5.8 11.9-2.33 3.46-5.03 6.13-8.08 8.02-3.06 1.89-6.35 2.83-9.89 2.83-4.46 0-7.78-1.36-9.97-4.09s-3.28-6.81-3.28-12.3c0-2.96 0.33-6.62 0.99-11 0.66-4.34 1.74-8.78 3.26-13.3 1.51-4.54 3.46-8.56 5.84-12.1s5.12-6.23 8.22-8.13 6.46-2.85 10.1-2.85z\" fill=\"#0070c0\" fill-rule=\"evenodd\" />\n </g>\n </g>\n</svg>\n<p>As we can see, the pair of these reflections combined amount to a counterclockwise rotation by an angle $2\\theta$.\nIf we repeat the Grover's iteration, reflecting the new state first along the horizontal axis and then along the $\\ket{\\text{all}}$ vector, it performs a rotation by $2\\theta$ again. The angle of this rotation depends only on the angle between the reflection axes and not on the state we reflect!</p>\n<p>Each iteration of Grover's search adds $2\\theta$ to the current angle in the expression of the system state as a superposition of $\\ket{\\text{good}}$ and $\\ket{\\text{bad}}$.\nAfter applying $R$ iterations of Grover's search the state of the system will become</p>\n<p>$$\\sin{(2R+1)\\theta}\\ket{\\text{good}} + \\cos{(2R+1)\\theta}\\ket{\\text{bad}}$$</p>\n<p>At firat, each iteration brings the state of the system closer to the vertical axis, increasing the probability of measuring one of the basis states that are part of $\\ket{\\text{good}}$ - the states that are solutions to the problem.</p>\n"
6983
+ "content": "<h3>Inputs</h3>\n<p>You are given the number of bits in the function input $n$ and the phase oracle for the problem we're solving - a &quot;black box&quot; quantum operation $U_f$ that implements a classical function $f(x)$.</p>\n<p>As usual, the phase oracle $U_f$ is defined by its effect on the individual values $x$ (represented as basis states $\\ket{x}$).\nIf the value of the function on the input $x$ $f(x) = 1$, the corresponding basis state $\\ket{x}$ is multiplied by $-1$; otherwise, the basis state is not changed.\nFormally, this can be written as follows:</p>\n<p>$$U_f \\ket{x} = (-1)^{f(x)} \\ket{x}$$</p>\n<blockquote>\n<p>Typically the oracle for Grover's search is implemented as a marking oracle and then converted into a phase oracle using the phase kickback trick.</p>\n</blockquote>\n<h3>Algorithm outline</h3>\n<p>The high-level outline of the algorithm is very simple:</p>\n<ol>\n<li>Initialize the quantum system to a well-known starting state.</li>\n<li>Apply a fixed sequence of &quot;Grover iterations&quot; several times. Each iteration is implemented as pair of operations that includes one call of the oracle &quot;black box&quot;.</li>\n<li>Finally, measuring all qubits will produce the desired output with high probability.</li>\n</ol>\n<p>Let's take a closer look at the algorithm.</p>\n<blockquote>\n<p>We will use a convenient visualization of the algorithm steps rather than mathematical derivation.\nThey are equivalent, but the visual representation is much easier to follow.</p>\n</blockquote>\n<h3>Initial state and definitions</h3>\n<p>Grover's search algorithm begins with a uniform superposition of all the states in the search space.\nTypically, the search space is defined as all $n$-bit bit strings, so this superposition is just an even superposition\nof all $N = 2^n$ basis states on $n$ qubits:\n$$\\ket{\\text{all}} = \\frac{1}{\\sqrt{N}}\\sum_{x=0}^{N-1}{\\ket{x}} $$</p>\n<p>When this superposition is considered in the context of the equation $f(x) = 1$,\nall the basis states can be split in two groups: &quot;good&quot; (solutions) and &quot;bad&quot; (non-solutions).\nIf the number of states for which $f(x)=1$ (the number of equation solutions) is $M$,\ntwo uniform superpositions of &quot;good&quot; and &quot;bad&quot; states can be defined as follows:</p>\n<p>$$\\ket{\\text{good}} = \\frac{1}{\\sqrt{M}}\\sum_{x,f(x)=1}{\\ket{x}}$$\n$$\\ket{\\text{bad}} = \\frac{1}{\\sqrt{N-M}}\\sum_{x,f(x)=0}{\\ket{x}}$$</p>\n<p>Now, the even superposition of all basis states can be rewritten as follows:\n$$\\ket{\\text{all}} = \\sqrt{\\frac{M}{N}}\\ket{\\text{good}} + \\sqrt{\\frac{N-M}{N}}\\ket{\\text{bad}}$$</p>\n<p>The amplutudes $\\sqrt{\\frac{M}{N}}$ and $\\sqrt{\\frac{N-M}{N}}$ can then be written in a trigonometric representation,\nas a sine and cosine of the angle $\\theta$:</p>\n<p>$$\\sin \\theta = \\sqrt{\\frac{M}{N}}, \\cos \\theta = \\sqrt{\\frac{N-M}{N}}$$</p>\n<p>With this replacement, the initial state can be written as</p>\n<p>$$\\ket{\\text{all}} = \\sin \\theta \\ket{\\text{good}} + \\cos \\theta \\ket{\\text{bad}}$$</p>\n<p>The states involved in the algorithm can be represented on a plane on which $\\ket{\\text{good}}$ and $\\ket{\\text{bad}}$ vectors correspond to vertical and horizontal axes, respectively.</p>\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<svg width=\"447\" height=\"370\" overflow=\"hidden\" version=\"1.1\" xml:space=\"preserve\" xmlns=\"http://www.w3.org/2000/svg\">\n<style>\n/* Default values, or theme set explicitly to light */\n:root, [data-theme=\"light\"] {\n --kata-svg-stroke: #222;\n --kata-svg-fill: #fff;\n --kata-svg-path: #777;\n --kata-svg-accent: #06c;\n}\n/* User has set OS preference for dark. (An explict light theme will override) */\n@media(prefers-color-scheme: dark) {\n :root {\n --kata-svg-stroke: #eee;\n --kata-svg-fill: #111;\n --kata-svg-path: #bbb;\n --kata-svg-accent: #08f;\n }\n}\n/* Explicit dark theme set (should match above dark preference values) */\n[data-theme=\"dark\"] {\n --kata-svg-stroke: #eee;\n --kata-svg-fill: #111;\n --kata-svg-path: #bbb;\n --kata-svg-accent: #08f;\n}\n/*** Kata specific styles ***/\n.kata_svg_path {\n stroke: var(--kata-svg-path);\n stroke-width: 2;\n stroke-linecap: round;\n fill: none;\n}\n.kata_svg_text {\n fill: var(--kata-svg-stroke);\n}\n.kata_svg_point {\n fill: var(--kata-svg-fill);\n stroke: var(--kata-svg-stroke);\n}\n.kata_svg_fill_accent {\n fill: var(--kata-svg-accent);\n}\n.kata_svg_stroke_accent {\n stroke: var(--kata-svg-accent);\n}\n</style>\n <g transform=\"matrix(.25 0 0 .25 -333 -132)\">\n <g class=\"kata_svg_path\">\n <!-- Main circle -->\n <circle cx=\"2020\" cy=\"1310\" r=\"670\" />\n <!-- x axis -->\n <path d=\"M2685 1310 h-1335 m1335 0 l-32 -32 m32 32 l-32 32\" />\n <!-- y axis -->\n <path d=\"m2020 645 v1323 m0 -1323 l-32 32 m32 -32 l32 32\" />\n <!-- Angle line -->\n <path d=\"m2665 1145 l-645 165 m645 -165 l-32 45 m32 -45 l -45 -25\" />\n <path d=\"m2357 1225c6.73 27 10.1 54.7 10.1 82.5\" fill-rule=\"evenodd\" />\n </g>\n <g class=\"kata_svg_text\">\n <!-- |all> ket -->\n <path d=\"m2735 1098h6.92v83.6h-6.92z\" />\n <path d=\"m2782 1122c-3.09 0-5.86 1.25-8.31 3.73-2.46 2.49-4.37 5.82-5.75 10s-2.07 8.35-2.07 12.5c0 3.11 0.43 5.44 1.28 6.98 0.86 1.55 2.29 2.32 4.29 2.32 1.98 0 3.92-0.86 5.82-2.56 1.9-1.71 3.86-4.14 5.88-7.3 2.03-3.16 3.46-6.81 4.29-10.9l0.41-1.93c0.27-1.23 0.45-2.29 0.54-3.19s0.13-1.87 0.13-2.92c0-2.31-0.49-4-1.48-5.08s-2.66-1.62-5.03-1.62zm-0.9-3.54c1.95 0 3.77 0.21 5.46 0.65 1.69 0.43 3.44 1.2 5.23 2.31l4.14-2.96 2.87 0.71-6.6 28.5c-0.66 2.85-0.99 5.07-0.99 6.65 0 1.2 0.19 2.07 0.58 2.61s1.01 0.81 1.84 0.81c0.9 0 1.86-0.37 2.88-1.1 1.02-0.74 2.45-2.14 4.31-4.2l2.56 2.51c-2.69 2.88-4.99 4.92-6.89 6.13-1.9 1.22-3.96 1.82-6.18 1.82-1.86 0-3.34-0.61-4.45-1.84s-1.66-2.86-1.66-4.9c0-1.64 0.34-3.39 1.03-5.25l-0.58-0.18c-2.88 4.19-5.6 7.26-8.18 9.21-2.57 1.94-5.3 2.92-8.17 2.92-3.3 0-5.85-1.24-7.66-3.73s-2.72-5.98-2.72-10.5c0-5.15 1.01-10.1 3.03-14.8 2.02-4.75 4.8-8.49 8.34-11.2 3.53-2.74 7.47-4.11 11.8-4.11zm75.2-20.5h2.65l-11 49.7c-0.66 2.97-0.99 5.19-0.99 6.65 0 1.2 0.2 2.07 0.58 2.61 0.39 0.54 1.01 0.81 1.85 0.81 0.89 0 1.85-0.37 2.87-1.1 1.02-0.74 2.46-2.14 4.31-4.2l2.56 2.51c-2.9 3.06-5.27 5.15-7.12 6.27-1.84 1.12-3.9 1.68-6.17 1.68-1.95 0-3.51-0.71-4.67-2.13-1.17-1.42-1.76-3.24-1.76-5.46 0-2.01 0.42-4.91 1.26-8.71l8.04-36c0.36-1.58 0.62-2.85 0.77-3.79 0.15-0.95 0.22-1.82 0.22-2.63 0-0.9-0.16-1.58-0.49-2.04-0.33-0.47-0.8-0.81-1.42-1.01-0.61-0.21-1.71-0.36-3.3-0.45l0.54-2.34zm-29 0h2.65l-11 49.7c-0.66 2.97-0.99 5.19-0.99 6.65 0 1.2 0.2 2.07 0.58 2.61 0.39 0.54 1.01 0.81 1.85 0.81 0.89 0 1.85-0.37 2.87-1.1 1.02-0.74 2.46-2.14 4.31-4.2l2.56 2.51c-2.9 3.06-5.27 5.15-7.12 6.27-1.84 1.12-3.9 1.68-6.17 1.68-1.95 0-3.51-0.71-4.67-2.13-1.17-1.42-1.76-3.24-1.76-5.46 0-2.01 0.42-4.91 1.26-8.71l8.04-36c0.36-1.58 0.62-2.85 0.77-3.79 0.15-0.95 0.22-1.82 0.22-2.63 0-0.9-0.16-1.58-0.49-2.04-0.33-0.47-0.8-0.81-1.42-1.01-0.61-0.21-1.71-0.36-3.3-0.45l0.54-2.34z\" />\n <path d=\"m2875 1098 17.9 40.3v3.32l-17.9 40.4-4.81-1.71 14.4-40.3-14.4-40.4z\" />\n <!-- |good> ket -->\n <path d=\"m1889 530h6.91v83.6h-6.91z\" />\n <path d=\"m2040 554c-3.11 0-5.83 1.3-8.15 3.91-2.32 2.6-4.15 6.33-5.48 11.2-1.33 4.85-2 9.03-2 12.5 0 3.29 0.68 5.73 2.05 7.3 1.36 1.57 3.42 2.36 6.17 2.36 2.94 0 5.54-1.32 7.82-3.95 2.28-2.64 4.07-6.33 5.39-11.1 1.32-4.75 1.98-8.92 1.98-12.5 0-3.24-0.63-5.67-1.89-7.3s-3.22-2.45-5.89-2.45zm-49 0c-3.11 0-5.83 1.3-8.15 3.91-2.32 2.6-4.15 6.33-5.48 11.2-1.33 4.85-2 9.03-2 12.5 0 3.29 0.68 5.73 2.05 7.3 1.36 1.57 3.42 2.36 6.17 2.36 2.94 0 5.54-1.32 7.82-3.95 2.28-2.64 4.07-6.33 5.39-11.1 1.32-4.75 1.98-8.92 1.98-12.5 0-3.24-0.63-5.67-1.89-7.3s-3.22-2.45-5.89-2.45zm98.3-0.09c-3.05 0-5.79 1.24-8.22 3.71-2.42 2.47-4.33 5.8-5.72 10-1.4 4.19-2.09 8.37-2.09 12.5 0 3.12 0.42 5.44 1.28 6.99 0.85 1.54 2.28 2.31 4.29 2.31 0.93 0 1.87-0.202 2.83-0.606 0.96-0.405 1.95-1.06 2.99-1.96 1.03-0.898 2.05-1.99 3.05-3.28s1.92-2.58 2.76-3.89c0.84-1.3 1.62-2.84 2.34-4.63 0.72-1.78 1.33-3.93 1.84-6.45l0.4-1.93c0.45-2.16 0.68-4.19 0.68-6.11 0-2.34-0.5-4.04-1.48-5.1-0.99-1.06-2.64-1.6-4.95-1.6zm-148 0c-3.05 0-5.79 1.24-8.22 3.71-2.42 2.47-4.33 5.8-5.72 10-1.4 4.19-2.09 8.37-2.09 12.5 0 3.12 0.45 5.44 1.35 6.99 0.89 1.54 2.3 2.31 4.22 2.31 1.53 0 3.05-0.524 4.56-1.57s3.13-2.71 4.85-4.99 3.06-4.45 4.02-6.51c0.96-2.07 1.77-4.64 2.45-7.73 0.67-3.08 1.01-5.76 1.01-8.04 0-2.34-0.5-4.04-1.48-5.1-0.99-1.06-2.64-1.6-4.95-1.6zm100-3.5c4.8 0 8.45 1.31 11 3.93 2.52 2.62 3.78 6.37 3.78 11.3 0 3.47-0.61 7.19-1.82 11.1s-2.88 7.28-4.99 9.97c-2.11 2.7-4.63 4.72-7.57 6.09-2.93 1.36-6.3 2.04-10.1 2.04-4.65 0-8.25-1.3-10.8-3.91-2.56-2.6-3.84-6.35-3.84-11.2 0-2.4 0.36-5.17 1.08-8.31 0.93-4.25 2.48-7.97 4.67-11.1 2.19-3.18 4.88-5.61 8.09-7.3 3.2-1.69 6.72-2.54 10.6-2.54zm-49 0c4.8 0 8.45 1.31 11 3.93 2.52 2.62 3.78 6.37 3.78 11.3 0 3.47-0.61 7.19-1.82 11.1s-2.88 7.28-4.99 9.97c-2.11 2.7-4.63 4.72-7.57 6.09-2.93 1.36-6.3 2.04-10.1 2.04-4.65 0-8.25-1.3-10.8-3.91-2.56-2.6-3.84-6.35-3.84-11.2 0-2.4 0.36-5.17 1.08-8.31 0.93-4.25 2.48-7.97 4.67-11.1 2.19-3.18 4.88-5.61 8.09-7.3 3.2-1.69 6.72-2.54 10.6-2.54zm-51.7-0.045c1.88 0 3.66 0.225 5.34 0.674s3.37 1.21 5.08 2.29l4.13-2.96 2.88 0.719-4.63 20.9c-0.33 1.47-1.08 5.17-2.25 11.1-1.23 6.29-2.22 10.7-2.98 13.3-0.77 2.6-1.72 4.98-2.86 7.12s-2.6 4-4.4 5.59c-1.8 1.59-3.98 2.82-6.54 3.68-2.56 0.869-5.59 1.3-9.09 1.3-11.3 0-17-3.29-17-9.88 0-1.68 0.51-3.2 1.52-4.58 1.02-1.38 2.65-2.83 4.9-4.36l3.24 2.6c-0.57 0.689-1.05 1.37-1.44 2.04s-0.67 1.32-0.83 1.95c-0.17 0.629-0.25 1.38-0.25 2.25 0 2.25 0.81 3.89 2.43 4.92 1.61 1.03 4.05 1.55 7.32 1.55 3.68 0 6.64-0.831 8.87-2.49s4.14-4.34 5.73-8.02c1.58-3.68 3.13-9.25 4.62-16.7l-0.62-0.134c-2.91 3.98-5.6 6.89-8.09 8.72-2.49 1.83-5.17 2.74-8.04 2.74-3.27 0-5.8-1.23-7.62-3.68-1.81-2.46-2.71-5.96-2.71-10.5 0-5.12 1-10 3.01-14.8 2-4.72 4.79-8.47 8.35-11.3 3.57-2.78 7.53-4.18 11.9-4.18zm168-20.5h2.65l-11.2 49.7c-0.66 2.94-0.99 5.15-0.99 6.65 0 1.2 0.2 2.07 0.59 2.6s1 0.809 1.84 0.809c0.9 0 1.86-0.367 2.87-1.1 1.02-0.734 2.46-2.13 4.32-4.2l2.56 2.52c-2.76 2.9-5.08 4.96-6.97 6.15-1.88 1.2-3.92 1.8-6.1 1.8-1.86 0-3.34-0.614-4.45-1.84s-1.66-2.86-1.66-4.9c0-1.65 0.34-3.4 1.03-5.26l-0.59-0.18c-2.87 4.19-5.6 7.26-8.17 9.21-2.58 1.95-5.3 2.92-8.18 2.92-3.29 0-5.84-1.24-7.66-3.73-1.81-2.48-2.71-5.97-2.71-10.5 0-5.15 1.01-10.1 3.03-14.8s4.8-8.49 8.33-11.2c3.54-2.74 7.47-4.11 11.8-4.11 1.82 0 3.51 0.187 5.05 0.562 1.54 0.374 3.12 1.02 4.74 1.95l2.33-10.4c0.24-0.989 0.45-2.08 0.63-3.28s0.27-2.25 0.27-3.14c0-0.899-0.16-1.58-0.49-2.04s-0.8-0.801-1.42-1.01c-0.61-0.209-1.71-0.359-3.3-0.449l0.54-2.34z\" />\n <path d=\"m2126 529 17.9 40.3v3.32l-17.9 40.4-4.81-1.71 14.4-40.3-14.4-40.4z\" />\n <!-- |bad> ket -->\n <path d=\"m2766 1275h6.92v83.6h-6.92z\" />\n <path d=\"m2816 1301c-1.43 0-2.85 0.46-4.24 1.37s-2.94 2.43-4.65 4.54-3.1 4.27-4.18 6.49-1.99 5.05-2.74 8.49l-0.4 1.89c-0.45 2.09-0.68 4.13-0.68 6.11 0 2.36 0.52 4.07 1.55 5.12 1.04 1.05 2.75 1.57 5.15 1.57 1.22 0 2.39-0.2 3.5-0.61 1.11-0.4 2.24-1.07 3.39-2 1.15-0.92 2.25-2.14 3.28-3.66 1.03-1.51 1.99-3.37 2.85-5.59 0.87-2.22 1.55-4.59 2.03-7.12 0.47-2.53 0.71-4.95 0.71-7.25 0-3.12-0.44-5.46-1.32-7.01-0.88-1.56-2.3-2.34-4.25-2.34zm97.4-1.75c-3.06 0-5.8 1.23-8.22 3.71-2.43 2.47-4.34 5.8-5.73 9.99s-2.09 8.37-2.09 12.5c0 3.12 0.43 5.45 1.28 6.99s2.29 2.31 4.29 2.31c0.93 0 1.87-0.2 2.83-0.6 0.96-0.41 1.96-1.06 2.99-1.96s2.05-1.99 3.05-3.28c1.01-1.29 1.93-2.58 2.77-3.88 0.83-1.31 1.61-2.85 2.33-4.63s1.33-3.93 1.84-6.45l0.41-1.93c0.45-2.15 0.67-4.19 0.67-6.11 0-2.33-0.49-4.03-1.48-5.1-0.99-1.06-2.64-1.59-4.94-1.59zm-50.9 0c-3.09 0-5.86 1.24-8.31 3.73-2.46 2.48-4.37 5.82-5.75 9.99-1.38 4.18-2.07 8.35-2.07 12.5 0 3.12 0.43 5.45 1.28 6.99s2.29 2.31 4.29 2.31c1.98 0 3.92-0.85 5.82-2.56 1.9-1.7 3.86-4.14 5.88-7.3 2.03-3.16 3.46-6.8 4.29-10.9l0.41-1.93c0.27-1.23 0.45-2.29 0.54-3.19s0.13-1.87 0.13-2.92c0-2.3-0.49-4-1.48-5.07-0.99-1.08-2.67-1.62-5.03-1.62zm-0.9-3.55c1.95 0 3.77 0.22 5.46 0.65 1.69 0.44 3.43 1.21 5.23 2.32l4.13-2.97 2.88 0.72-6.6 28.5c-0.66 2.84-0.99 5.06-0.99 6.65 0 1.2 0.19 2.06 0.58 2.6s1 0.81 1.84 0.81c0.9 0 1.86-0.36 2.88-1.1 1.02-0.73 2.45-2.13 4.31-4.2l2.56 2.52c-2.69 2.87-4.99 4.92-6.89 6.13-1.91 1.21-3.97 1.82-6.18 1.82-1.86 0-3.34-0.62-4.45-1.84-1.11-1.23-1.66-2.86-1.66-4.9 0-1.65 0.34-3.4 1.03-5.26l-0.58-0.18c-2.88 4.2-5.6 7.27-8.18 9.21-2.57 1.95-5.3 2.92-8.17 2.92-3.3 0-5.85-1.24-7.66-3.73-1.81-2.48-2.72-5.97-2.72-10.5 0-5.15 1.01-10.1 3.03-14.8s4.8-8.49 8.34-11.2c3.53-2.74 7.47-4.11 11.8-4.11zm70.6-20.5h2.65l-11.2 49.7c-0.66 2.93-0.99 5.15-0.99 6.65 0 1.2 0.19 2.06 0.58 2.6s1 0.81 1.84 0.81c0.9 0 1.86-0.36 2.88-1.1 1.02-0.73 2.45-2.13 4.31-4.2l2.56 2.52c-2.75 2.9-5.08 4.95-6.96 6.15-1.89 1.2-3.93 1.8-6.11 1.8-1.86 0-3.34-0.62-4.45-1.84-1.11-1.23-1.66-2.86-1.66-4.9 0-1.65 0.34-3.4 1.03-5.26l-0.58-0.18c-2.88 4.2-5.6 7.27-8.18 9.21-2.57 1.95-5.3 2.92-8.17 2.92-3.3 0-5.85-1.24-7.66-3.73-1.81-2.48-2.72-5.97-2.72-10.5 0-5.15 1.01-10.1 3.03-14.8s4.8-8.49 8.34-11.2c3.53-2.74 7.47-4.11 11.8-4.11 1.83 0 3.51 0.19 5.05 0.56 1.55 0.38 3.13 1.03 4.74 1.96l2.34-10.4c0.24-0.99 0.45-2.08 0.63-3.28s0.27-2.25 0.27-3.14c0-0.9-0.17-1.58-0.5-2.05-0.33-0.46-0.8-0.8-1.41-1.01-0.62-0.21-1.72-0.36-3.3-0.45l0.54-2.33zm-124 0h2.61l-7.91 31.4 0.59 0.18c2.9-3.9 5.63-6.71 8.17-8.45 2.55-1.74 5.09-2.6 7.64-2.6 3.26 0 5.8 1.24 7.61 3.72 1.81 2.49 2.72 5.99 2.72 10.5 0 4.94-1 9.8-2.99 14.6-1.99 4.78-4.75 8.57-8.29 11.4-3.53 2.8-7.48 4.2-11.8 4.2-3.99 0-7.52-0.98-10.6-2.92l-4.13 2.92-2.87-0.72 11.6-51.4c0.69-3.05 1.03-5.27 1.03-6.64 0-1.26-0.38-2.15-1.14-2.66-0.77-0.5-2.14-0.79-4.11-0.85l0.54-2.33z\" />\n <path d=\"m2950 1275 17.9 40.3v3.33l-17.9 40.4-4.8-1.71 14.4-40.3-14.4-40.4z\" />\n <!-- theta-->\n <path d=\"m2414 1260c-1.4 6.49-2.11 12-2.11 16.4 0 3.36 0.51 5.82 1.53 7.39s2.67 2.36 4.94 2.36c3.36 0 6.54-2.25 9.55-6.76s5.56-11 7.66-19.4zm18.4-30.3c-3.5 0-6.74 2.22-9.72 6.65s-5.52 10.8-7.62 19h21.5c1.41-6.31 2.11-11.8 2.11-16.3 0-3.15-0.51-5.5-1.53-7.06-1.01-1.55-2.6-2.33-4.76-2.33zm0.54-3.64c8.69 0 13 5.51 13 16.5 0 3.12-0.35 6.84-1.06 11.2-0.7 4.33-1.82 8.74-3.34 13.2-1.53 4.5-3.46 8.47-5.8 11.9-2.33 3.46-5.03 6.13-8.08 8.02-3.06 1.89-6.35 2.83-9.89 2.83-4.46 0-7.78-1.36-9.97-4.09-2.19-2.72-3.28-6.81-3.28-12.3 0-2.97 0.33-6.62 0.99-11 0.66-4.35 1.74-8.79 3.26-13.3 1.51-4.54 3.45-8.57 5.84-12.1 2.38-3.52 5.12-6.23 8.22-8.13s6.46-2.85 10.1-2.85z\" fill-rule=\"evenodd\" />\n </g>\n <g class=\"kata_svg_fill_accent kata_svg_stroke_accent\">\n <!-- Started state square braket -->\n <path d=\"m2722 1082v-49h89v-49 49h89v49\" fill=\"none\" stroke-width=\"6\" />\n <text transform=\"translate(2605 952)\" font-family=\"sans-serif\" font-size=\"64px\" font-weight=\"400\">The starting state</text>\n </g>\n </g>\n</svg>\n<h3>Grover's iteration</h3>\n<p>Each Grover's iteration consists of two operations.</p>\n<ol>\n<li>The phase oracle $U_f$.</li>\n<li>An operation called &quot;reflection about the mean&quot;.</li>\n</ol>\n<p>Applying the phase oracle to the state will flip the sign of all basis states in $\\ket{\\text{good}}$\nand leave all basis states in $\\ket{\\text{bad}}$ unchanged:</p>\n<p>$$U_f\\ket{\\text{good}} = -\\ket{\\text{good}}$$\n$$U_f\\ket{\\text{bad}} = \\ket{\\text{bad}}$$</p>\n<p>On the circle plot, this transformation leaves the horizontal component of the state vector unchanged and reverses its vertical component. In other words, this operation is a reflection along the horizontal axis.</p>\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<svg width=\"447\" height=\"370\" overflow=\"hidden\" version=\"1.1\" xml:space=\"preserve\" xmlns=\"http://www.w3.org/2000/svg\">\n<style>\n/* Default values, or theme set explicitly to light */\n:root, [data-theme=\"light\"] {\n --kata-svg-stroke: #222;\n --kata-svg-fill: #fff;\n --kata-svg-path: #777;\n --kata-svg-accent: #06c;\n}\n/* User has set OS preference for dark. (An explict light theme will override) */\n@media(prefers-color-scheme: dark) {\n :root {\n --kata-svg-stroke: #eee;\n --kata-svg-fill: #111;\n --kata-svg-path: #bbb;\n --kata-svg-accent: #08f;\n }\n}\n/* Explicit dark theme set (should match above dark preference values) */\n[data-theme=\"dark\"] {\n --kata-svg-stroke: #eee;\n --kata-svg-fill: #111;\n --kata-svg-path: #bbb;\n --kata-svg-accent: #08f;\n}\n/*** Kata specific styles ***/\n.kata_svg_path {\n stroke: var(--kata-svg-path);\n stroke-width: 2;\n stroke-linecap: round;\n fill: none;\n}\n.kata_svg_text {\n fill: var(--kata-svg-stroke);\n}\n.kata_svg_point {\n fill: var(--kata-svg-fill);\n stroke: var(--kata-svg-stroke);\n}\n.kata_svg_fill_accent {\n fill: var(--kata-svg-accent);\n}\n.kata_svg_stroke_accent {\n stroke: var(--kata-svg-accent);\n}\n</style>\n <g transform=\"matrix(.25 0 0 .25 -333 -132)\">\n <g class=\"kata_svg_path\">\n <!-- Main circle -->\n <circle cx=\"2020\" cy=\"1310\" r=\"670\" />\n <!-- x axis -->\n <path d=\"M2685 1310 h-1335 m1335 0 l-32 -32 m32 32 l-32 32\" />\n <!-- y axis -->\n <path d=\"m2020 645 v1323 m0 -1323 l-32 32 m32 -32 l32 32\" />\n <!-- Angle line -->\n <path d=\"m2665 1145 l-645 165 m645 -165 l-32 45 m32 -45 l -45 -25\" />\n <path d=\"m2357 1225c6.73 27 10.1 54.7 10.1 82.5\" fill-rule=\"evenodd\" />\n <!-- lower angle line -->\n <path d=\"m2665 1475 l-645 -165 m645 165 l-32 -45 m32 45 l -45 25\" class=\"kata_svg_stroke_accent\"/>\n <!-- lower angle curve -->\n <path transform=\"matrix(1 0 0 -1 1677 1655)\" d=\"m679 259c7.22 28.1 10.9 56.9 10.9 85.9\" class=\"kata_svg_stroke_accent\"/>\n </g>\n <g class=\"kata_svg_text\">\n <!-- |all> ket -->\n <path d=\"m2735 1098h6.92v83.6h-6.92z\" />\n <path d=\"m2782 1122c-3.09 0-5.86 1.25-8.31 3.73-2.46 2.49-4.37 5.82-5.75 10s-2.07 8.35-2.07 12.5c0 3.11 0.43 5.44 1.28 6.98 0.86 1.55 2.29 2.32 4.29 2.32 1.98 0 3.92-0.86 5.82-2.56 1.9-1.71 3.86-4.14 5.88-7.3 2.03-3.16 3.46-6.81 4.29-10.9l0.41-1.93c0.27-1.23 0.45-2.29 0.54-3.19s0.13-1.87 0.13-2.92c0-2.31-0.49-4-1.48-5.08s-2.66-1.62-5.03-1.62zm-0.9-3.54c1.95 0 3.77 0.21 5.46 0.65 1.69 0.43 3.44 1.2 5.23 2.31l4.14-2.96 2.87 0.71-6.6 28.5c-0.66 2.85-0.99 5.07-0.99 6.65 0 1.2 0.19 2.07 0.58 2.61s1.01 0.81 1.84 0.81c0.9 0 1.86-0.37 2.88-1.1 1.02-0.74 2.45-2.14 4.31-4.2l2.56 2.51c-2.69 2.88-4.99 4.92-6.89 6.13-1.9 1.22-3.96 1.82-6.18 1.82-1.86 0-3.34-0.61-4.45-1.84s-1.66-2.86-1.66-4.9c0-1.64 0.34-3.39 1.03-5.25l-0.58-0.18c-2.88 4.19-5.6 7.26-8.18 9.21-2.57 1.94-5.3 2.92-8.17 2.92-3.3 0-5.85-1.24-7.66-3.73s-2.72-5.98-2.72-10.5c0-5.15 1.01-10.1 3.03-14.8 2.02-4.75 4.8-8.49 8.34-11.2 3.53-2.74 7.47-4.11 11.8-4.11zm75.2-20.5h2.65l-11 49.7c-0.66 2.97-0.99 5.19-0.99 6.65 0 1.2 0.2 2.07 0.58 2.61 0.39 0.54 1.01 0.81 1.85 0.81 0.89 0 1.85-0.37 2.87-1.1 1.02-0.74 2.46-2.14 4.31-4.2l2.56 2.51c-2.9 3.06-5.27 5.15-7.12 6.27-1.84 1.12-3.9 1.68-6.17 1.68-1.95 0-3.51-0.71-4.67-2.13-1.17-1.42-1.76-3.24-1.76-5.46 0-2.01 0.42-4.91 1.26-8.71l8.04-36c0.36-1.58 0.62-2.85 0.77-3.79 0.15-0.95 0.22-1.82 0.22-2.63 0-0.9-0.16-1.58-0.49-2.04-0.33-0.47-0.8-0.81-1.42-1.01-0.61-0.21-1.71-0.36-3.3-0.45l0.54-2.34zm-29 0h2.65l-11 49.7c-0.66 2.97-0.99 5.19-0.99 6.65 0 1.2 0.2 2.07 0.58 2.61 0.39 0.54 1.01 0.81 1.85 0.81 0.89 0 1.85-0.37 2.87-1.1 1.02-0.74 2.46-2.14 4.31-4.2l2.56 2.51c-2.9 3.06-5.27 5.15-7.12 6.27-1.84 1.12-3.9 1.68-6.17 1.68-1.95 0-3.51-0.71-4.67-2.13-1.17-1.42-1.76-3.24-1.76-5.46 0-2.01 0.42-4.91 1.26-8.71l8.04-36c0.36-1.58 0.62-2.85 0.77-3.79 0.15-0.95 0.22-1.82 0.22-2.63 0-0.9-0.16-1.58-0.49-2.04-0.33-0.47-0.8-0.81-1.42-1.01-0.61-0.21-1.71-0.36-3.3-0.45l0.54-2.34z\" />\n <path d=\"m2875 1098 17.9 40.3v3.32l-17.9 40.4-4.81-1.71 14.4-40.3-14.4-40.4z\" />\n <!-- |good> ket -->\n <path d=\"m1889 530h6.91v83.6h-6.91z\" />\n <path d=\"m2040 554c-3.11 0-5.83 1.3-8.15 3.91-2.32 2.6-4.15 6.33-5.48 11.2-1.33 4.85-2 9.03-2 12.5 0 3.29 0.68 5.73 2.05 7.3 1.36 1.57 3.42 2.36 6.17 2.36 2.94 0 5.54-1.32 7.82-3.95 2.28-2.64 4.07-6.33 5.39-11.1 1.32-4.75 1.98-8.92 1.98-12.5 0-3.24-0.63-5.67-1.89-7.3s-3.22-2.45-5.89-2.45zm-49 0c-3.11 0-5.83 1.3-8.15 3.91-2.32 2.6-4.15 6.33-5.48 11.2-1.33 4.85-2 9.03-2 12.5 0 3.29 0.68 5.73 2.05 7.3 1.36 1.57 3.42 2.36 6.17 2.36 2.94 0 5.54-1.32 7.82-3.95 2.28-2.64 4.07-6.33 5.39-11.1 1.32-4.75 1.98-8.92 1.98-12.5 0-3.24-0.63-5.67-1.89-7.3s-3.22-2.45-5.89-2.45zm98.3-0.09c-3.05 0-5.79 1.24-8.22 3.71-2.42 2.47-4.33 5.8-5.72 10-1.4 4.19-2.09 8.37-2.09 12.5 0 3.12 0.42 5.44 1.28 6.99 0.85 1.54 2.28 2.31 4.29 2.31 0.93 0 1.87-0.202 2.83-0.606 0.96-0.405 1.95-1.06 2.99-1.96 1.03-0.898 2.05-1.99 3.05-3.28s1.92-2.58 2.76-3.89c0.84-1.3 1.62-2.84 2.34-4.63 0.72-1.78 1.33-3.93 1.84-6.45l0.4-1.93c0.45-2.16 0.68-4.19 0.68-6.11 0-2.34-0.5-4.04-1.48-5.1-0.99-1.06-2.64-1.6-4.95-1.6zm-148 0c-3.05 0-5.79 1.24-8.22 3.71-2.42 2.47-4.33 5.8-5.72 10-1.4 4.19-2.09 8.37-2.09 12.5 0 3.12 0.45 5.44 1.35 6.99 0.89 1.54 2.3 2.31 4.22 2.31 1.53 0 3.05-0.524 4.56-1.57s3.13-2.71 4.85-4.99 3.06-4.45 4.02-6.51c0.96-2.07 1.77-4.64 2.45-7.73 0.67-3.08 1.01-5.76 1.01-8.04 0-2.34-0.5-4.04-1.48-5.1-0.99-1.06-2.64-1.6-4.95-1.6zm100-3.5c4.8 0 8.45 1.31 11 3.93 2.52 2.62 3.78 6.37 3.78 11.3 0 3.47-0.61 7.19-1.82 11.1s-2.88 7.28-4.99 9.97c-2.11 2.7-4.63 4.72-7.57 6.09-2.93 1.36-6.3 2.04-10.1 2.04-4.65 0-8.25-1.3-10.8-3.91-2.56-2.6-3.84-6.35-3.84-11.2 0-2.4 0.36-5.17 1.08-8.31 0.93-4.25 2.48-7.97 4.67-11.1 2.19-3.18 4.88-5.61 8.09-7.3 3.2-1.69 6.72-2.54 10.6-2.54zm-49 0c4.8 0 8.45 1.31 11 3.93 2.52 2.62 3.78 6.37 3.78 11.3 0 3.47-0.61 7.19-1.82 11.1s-2.88 7.28-4.99 9.97c-2.11 2.7-4.63 4.72-7.57 6.09-2.93 1.36-6.3 2.04-10.1 2.04-4.65 0-8.25-1.3-10.8-3.91-2.56-2.6-3.84-6.35-3.84-11.2 0-2.4 0.36-5.17 1.08-8.31 0.93-4.25 2.48-7.97 4.67-11.1 2.19-3.18 4.88-5.61 8.09-7.3 3.2-1.69 6.72-2.54 10.6-2.54zm-51.7-0.045c1.88 0 3.66 0.225 5.34 0.674s3.37 1.21 5.08 2.29l4.13-2.96 2.88 0.719-4.63 20.9c-0.33 1.47-1.08 5.17-2.25 11.1-1.23 6.29-2.22 10.7-2.98 13.3-0.77 2.6-1.72 4.98-2.86 7.12s-2.6 4-4.4 5.59c-1.8 1.59-3.98 2.82-6.54 3.68-2.56 0.869-5.59 1.3-9.09 1.3-11.3 0-17-3.29-17-9.88 0-1.68 0.51-3.2 1.52-4.58 1.02-1.38 2.65-2.83 4.9-4.36l3.24 2.6c-0.57 0.689-1.05 1.37-1.44 2.04s-0.67 1.32-0.83 1.95c-0.17 0.629-0.25 1.38-0.25 2.25 0 2.25 0.81 3.89 2.43 4.92 1.61 1.03 4.05 1.55 7.32 1.55 3.68 0 6.64-0.831 8.87-2.49s4.14-4.34 5.73-8.02c1.58-3.68 3.13-9.25 4.62-16.7l-0.62-0.134c-2.91 3.98-5.6 6.89-8.09 8.72-2.49 1.83-5.17 2.74-8.04 2.74-3.27 0-5.8-1.23-7.62-3.68-1.81-2.46-2.71-5.96-2.71-10.5 0-5.12 1-10 3.01-14.8 2-4.72 4.79-8.47 8.35-11.3 3.57-2.78 7.53-4.18 11.9-4.18zm168-20.5h2.65l-11.2 49.7c-0.66 2.94-0.99 5.15-0.99 6.65 0 1.2 0.2 2.07 0.59 2.6s1 0.809 1.84 0.809c0.9 0 1.86-0.367 2.87-1.1 1.02-0.734 2.46-2.13 4.32-4.2l2.56 2.52c-2.76 2.9-5.08 4.96-6.97 6.15-1.88 1.2-3.92 1.8-6.1 1.8-1.86 0-3.34-0.614-4.45-1.84s-1.66-2.86-1.66-4.9c0-1.65 0.34-3.4 1.03-5.26l-0.59-0.18c-2.87 4.19-5.6 7.26-8.17 9.21-2.58 1.95-5.3 2.92-8.18 2.92-3.29 0-5.84-1.24-7.66-3.73-1.81-2.48-2.71-5.97-2.71-10.5 0-5.15 1.01-10.1 3.03-14.8s4.8-8.49 8.33-11.2c3.54-2.74 7.47-4.11 11.8-4.11 1.82 0 3.51 0.187 5.05 0.562 1.54 0.374 3.12 1.02 4.74 1.95l2.33-10.4c0.24-0.989 0.45-2.08 0.63-3.28s0.27-2.25 0.27-3.14c0-0.899-0.16-1.58-0.49-2.04s-0.8-0.801-1.42-1.01c-0.61-0.209-1.71-0.359-3.3-0.449l0.54-2.34z\" />\n <path d=\"m2126 529 17.9 40.3v3.32l-17.9 40.4-4.81-1.71 14.4-40.3-14.4-40.4z\" />\n <!-- |bad> ket -->\n <path d=\"m2766 1275h6.92v83.6h-6.92z\" />\n <path d=\"m2816 1301c-1.43 0-2.85 0.46-4.24 1.37s-2.94 2.43-4.65 4.54-3.1 4.27-4.18 6.49-1.99 5.05-2.74 8.49l-0.4 1.89c-0.45 2.09-0.68 4.13-0.68 6.11 0 2.36 0.52 4.07 1.55 5.12 1.04 1.05 2.75 1.57 5.15 1.57 1.22 0 2.39-0.2 3.5-0.61 1.11-0.4 2.24-1.07 3.39-2 1.15-0.92 2.25-2.14 3.28-3.66 1.03-1.51 1.99-3.37 2.85-5.59 0.87-2.22 1.55-4.59 2.03-7.12 0.47-2.53 0.71-4.95 0.71-7.25 0-3.12-0.44-5.46-1.32-7.01-0.88-1.56-2.3-2.34-4.25-2.34zm97.4-1.75c-3.06 0-5.8 1.23-8.22 3.71-2.43 2.47-4.34 5.8-5.73 9.99s-2.09 8.37-2.09 12.5c0 3.12 0.43 5.45 1.28 6.99s2.29 2.31 4.29 2.31c0.93 0 1.87-0.2 2.83-0.6 0.96-0.41 1.96-1.06 2.99-1.96s2.05-1.99 3.05-3.28c1.01-1.29 1.93-2.58 2.77-3.88 0.83-1.31 1.61-2.85 2.33-4.63s1.33-3.93 1.84-6.45l0.41-1.93c0.45-2.15 0.67-4.19 0.67-6.11 0-2.33-0.49-4.03-1.48-5.1-0.99-1.06-2.64-1.59-4.94-1.59zm-50.9 0c-3.09 0-5.86 1.24-8.31 3.73-2.46 2.48-4.37 5.82-5.75 9.99-1.38 4.18-2.07 8.35-2.07 12.5 0 3.12 0.43 5.45 1.28 6.99s2.29 2.31 4.29 2.31c1.98 0 3.92-0.85 5.82-2.56 1.9-1.7 3.86-4.14 5.88-7.3 2.03-3.16 3.46-6.8 4.29-10.9l0.41-1.93c0.27-1.23 0.45-2.29 0.54-3.19s0.13-1.87 0.13-2.92c0-2.3-0.49-4-1.48-5.07-0.99-1.08-2.67-1.62-5.03-1.62zm-0.9-3.55c1.95 0 3.77 0.22 5.46 0.65 1.69 0.44 3.43 1.21 5.23 2.32l4.13-2.97 2.88 0.72-6.6 28.5c-0.66 2.84-0.99 5.06-0.99 6.65 0 1.2 0.19 2.06 0.58 2.6s1 0.81 1.84 0.81c0.9 0 1.86-0.36 2.88-1.1 1.02-0.73 2.45-2.13 4.31-4.2l2.56 2.52c-2.69 2.87-4.99 4.92-6.89 6.13-1.91 1.21-3.97 1.82-6.18 1.82-1.86 0-3.34-0.62-4.45-1.84-1.11-1.23-1.66-2.86-1.66-4.9 0-1.65 0.34-3.4 1.03-5.26l-0.58-0.18c-2.88 4.2-5.6 7.27-8.18 9.21-2.57 1.95-5.3 2.92-8.17 2.92-3.3 0-5.85-1.24-7.66-3.73-1.81-2.48-2.72-5.97-2.72-10.5 0-5.15 1.01-10.1 3.03-14.8s4.8-8.49 8.34-11.2c3.53-2.74 7.47-4.11 11.8-4.11zm70.6-20.5h2.65l-11.2 49.7c-0.66 2.93-0.99 5.15-0.99 6.65 0 1.2 0.19 2.06 0.58 2.6s1 0.81 1.84 0.81c0.9 0 1.86-0.36 2.88-1.1 1.02-0.73 2.45-2.13 4.31-4.2l2.56 2.52c-2.75 2.9-5.08 4.95-6.96 6.15-1.89 1.2-3.93 1.8-6.11 1.8-1.86 0-3.34-0.62-4.45-1.84-1.11-1.23-1.66-2.86-1.66-4.9 0-1.65 0.34-3.4 1.03-5.26l-0.58-0.18c-2.88 4.2-5.6 7.27-8.18 9.21-2.57 1.95-5.3 2.92-8.17 2.92-3.3 0-5.85-1.24-7.66-3.73-1.81-2.48-2.72-5.97-2.72-10.5 0-5.15 1.01-10.1 3.03-14.8s4.8-8.49 8.34-11.2c3.53-2.74 7.47-4.11 11.8-4.11 1.83 0 3.51 0.19 5.05 0.56 1.55 0.38 3.13 1.03 4.74 1.96l2.34-10.4c0.24-0.99 0.45-2.08 0.63-3.28s0.27-2.25 0.27-3.14c0-0.9-0.17-1.58-0.5-2.05-0.33-0.46-0.8-0.8-1.41-1.01-0.62-0.21-1.72-0.36-3.3-0.45l0.54-2.33zm-124 0h2.61l-7.91 31.4 0.59 0.18c2.9-3.9 5.63-6.71 8.17-8.45 2.55-1.74 5.09-2.6 7.64-2.6 3.26 0 5.8 1.24 7.61 3.72 1.81 2.49 2.72 5.99 2.72 10.5 0 4.94-1 9.8-2.99 14.6-1.99 4.78-4.75 8.57-8.29 11.4-3.53 2.8-7.48 4.2-11.8 4.2-3.99 0-7.52-0.98-10.6-2.92l-4.13 2.92-2.87-0.72 11.6-51.4c0.69-3.05 1.03-5.27 1.03-6.64 0-1.26-0.38-2.15-1.14-2.66-0.77-0.5-2.14-0.79-4.11-0.85l0.54-2.33z\" />\n <path d=\"m2950 1275 17.9 40.3v3.33l-17.9 40.4-4.8-1.71 14.4-40.3-14.4-40.4z\" />\n <!-- theta-->\n <path d=\"m2414 1260c-1.4 6.49-2.11 12-2.11 16.4 0 3.36 0.51 5.82 1.53 7.39s2.67 2.36 4.94 2.36c3.36 0 6.54-2.25 9.55-6.76s5.56-11 7.66-19.4zm18.4-30.3c-3.5 0-6.74 2.22-9.72 6.65s-5.52 10.8-7.62 19h21.5c1.41-6.31 2.11-11.8 2.11-16.3 0-3.15-0.51-5.5-1.53-7.06-1.01-1.55-2.6-2.33-4.76-2.33zm0.54-3.64c8.69 0 13 5.51 13 16.5 0 3.12-0.35 6.84-1.06 11.2-0.7 4.33-1.82 8.74-3.34 13.2-1.53 4.5-3.46 8.47-5.8 11.9-2.33 3.46-5.03 6.13-8.08 8.02-3.06 1.89-6.35 2.83-9.89 2.83-4.46 0-7.78-1.36-9.97-4.09-2.19-2.72-3.28-6.81-3.28-12.3 0-2.97 0.33-6.62 0.99-11 0.66-4.35 1.74-8.79 3.26-13.3 1.51-4.54 3.45-8.57 5.84-12.1 2.38-3.52 5.12-6.23 8.22-8.13s6.46-2.85 10.1-2.85z\" fill-rule=\"evenodd\" />\n </g>\n <g class=\"kata_svg_fill_accent kata_svg_stroke_accent\" font-family=\"sans-serif\" font-size=\"64px\" font-weight=\"400\">\n <!-- theta -->\n <path d=\"m2415 1364c-1.41 6.5-2.11 12-2.11 16.4 0 3.35 0.51 5.82 1.52 7.39 1.02 1.57 2.67 2.36 4.95 2.36 3.35 0 6.53-2.26 9.54-6.76 3.01-4.51 5.56-11 7.66-19.4zm18.4-30.3c-3.51 0-6.75 2.21-9.73 6.64-2.98 4.44-5.52 10.8-7.61 19h21.5c1.41-6.32 2.12-11.8 2.12-16.3 0-3.14-0.51-5.49-1.53-7.05s-2.61-2.33-4.76-2.33zm0.54-3.64c8.68 0 13 5.51 13 16.5 0 3.11-0.35 6.83-1.05 11.2-0.71 4.33-1.82 8.74-3.35 13.2s-3.46 8.47-5.79 11.9c-2.34 3.46-5.04 6.13-8.09 8.02-3.06 1.88-6.35 2.83-9.88 2.83-4.47 0-7.79-1.37-9.98-4.09-2.18-2.73-3.28-6.82-3.28-12.3 0-2.96 0.33-6.61 0.99-11 0.66-4.34 1.75-8.78 3.26-13.3 1.51-4.53 3.46-8.56 5.84-12.1s5.12-6.23 8.22-8.13 6.46-2.85 10.1-2.85z\" />\n <text transform=\"translate(2734 1521)\">State after</text>\n <text transform=\"translate(2734 1598)\">the first </text>\n <text transform=\"translate(2734 1676)\">reflection</text>\n </g>\n </g>\n</svg>\n<p>&quot;Reflection about the mean&quot; is an operation for which the visual definition is much more intuitive than the mathematical one.\nIt is literally a reflection about the state $\\ket{\\text{all}}$ - the uniform superposition of all basis states in the search space.</p>\n<p>Mathematically, this operation is described as $2\\ket{\\text{all}}\\bra{\\text{all}} - I$: it leaves the component of the input state parallel to the state $\\ket{\\text{all}}$ unchanged and multiplies the component orthogonal to it by $-1$.</p>\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<svg width=\"447\" height=\"370\" overflow=\"hidden\" version=\"1.1\" xml:space=\"preserve\" xmlns=\"http://www.w3.org/2000/svg\">\n<style>\n/* Default values, or theme set explicitly to light */\n:root, [data-theme=\"light\"] {\n --kata-svg-stroke: #222;\n --kata-svg-fill: #fff;\n --kata-svg-path: #777;\n --kata-svg-accent: #06c;\n}\n/* User has set OS preference for dark. (An explict light theme will override) */\n@media(prefers-color-scheme: dark) {\n :root {\n --kata-svg-stroke: #eee;\n --kata-svg-fill: #111;\n --kata-svg-path: #bbb;\n --kata-svg-accent: #08f;\n }\n}\n/* Explicit dark theme set (should match above dark preference values) */\n[data-theme=\"dark\"] {\n --kata-svg-stroke: #eee;\n --kata-svg-fill: #111;\n --kata-svg-path: #bbb;\n --kata-svg-accent: #08f;\n}\n/*** Kata specific styles ***/\n.kata_svg_path {\n stroke: var(--kata-svg-path);\n stroke-width: 2;\n stroke-linecap: round;\n fill: none;\n}\n.kata_svg_text {\n fill: var(--kata-svg-stroke);\n}\n.kata_svg_point {\n fill: var(--kata-svg-fill);\n stroke: var(--kata-svg-stroke);\n}\n.kata_svg_fill_accent {\n fill: var(--kata-svg-accent);\n}\n.kata_svg_stroke_accent {\n stroke: var(--kata-svg-accent);\n}\n</style>\n <g transform=\"matrix(.25 0 0 .25 -333 -132)\">\n <g class=\"kata_svg_path\">\n <!-- Main circle -->\n <circle cx=\"2020\" cy=\"1310\" r=\"670\" />\n <!-- x axis -->\n <path d=\"M2685 1310 h-1335 m1335 0 l-32 -32 m32 32 l-32 32\" />\n <!-- y axis -->\n <path d=\"m2020 645 v1323 m0 -1323 l-32 32 m32 -32 l32 32\" />\n <!-- Angle line -->\n <path d=\"m2665 1145 l-645 165 m645 -165 l-32 45 m32 -45 l -45 -25\" />\n <path d=\"m2357 1225c6.73 27 10.1 54.7 10.1 82.5\" fill-rule=\"evenodd\" />\n <!-- lower angle line -->\n <path d=\"m2665 1475 l-645 -165 m645 165 l-32 -45 m32 45 l -45 25\" stroke-width=\"10\" stroke-linecap=\"round\" />\n <!-- lower angle curve -->\n <path transform=\"matrix(1 0 0 -1 1677 1655)\" d=\"m679 259c7.22 28.1 10.9 56.9 10.9 85.9\" fill=\"none\" fill-rule=\"evenodd\" stroke-width=\"10\" />\n <path d=\"M2490 845 l-470 465 m470 -465 l-50 0 m50 0 l0 50\" class=\"kata_svg_stroke_accent\"/>\n <!-- Accented angle line -->\n <path d=\"m2270 1065c43.2 45.4 73.5 102 88 160\" class=\"kata_svg_stroke_accent\"/>\n </g>\n <g class=\"kata_svg_text\">\n <!-- |all> ket -->\n <path d=\"m2735 1098h6.92v83.6h-6.92z\" />\n <path d=\"m2782 1122c-3.09 0-5.86 1.25-8.31 3.73-2.46 2.49-4.37 5.82-5.75 10s-2.07 8.35-2.07 12.5c0 3.11 0.43 5.44 1.28 6.98 0.86 1.55 2.29 2.32 4.29 2.32 1.98 0 3.92-0.86 5.82-2.56 1.9-1.71 3.86-4.14 5.88-7.3 2.03-3.16 3.46-6.81 4.29-10.9l0.41-1.93c0.27-1.23 0.45-2.29 0.54-3.19s0.13-1.87 0.13-2.92c0-2.31-0.49-4-1.48-5.08s-2.66-1.62-5.03-1.62zm-0.9-3.54c1.95 0 3.77 0.21 5.46 0.65 1.69 0.43 3.44 1.2 5.23 2.31l4.14-2.96 2.87 0.71-6.6 28.5c-0.66 2.85-0.99 5.07-0.99 6.65 0 1.2 0.19 2.07 0.58 2.61s1.01 0.81 1.84 0.81c0.9 0 1.86-0.37 2.88-1.1 1.02-0.74 2.45-2.14 4.31-4.2l2.56 2.51c-2.69 2.88-4.99 4.92-6.89 6.13-1.9 1.22-3.96 1.82-6.18 1.82-1.86 0-3.34-0.61-4.45-1.84s-1.66-2.86-1.66-4.9c0-1.64 0.34-3.39 1.03-5.25l-0.58-0.18c-2.88 4.19-5.6 7.26-8.18 9.21-2.57 1.94-5.3 2.92-8.17 2.92-3.3 0-5.85-1.24-7.66-3.73s-2.72-5.98-2.72-10.5c0-5.15 1.01-10.1 3.03-14.8 2.02-4.75 4.8-8.49 8.34-11.2 3.53-2.74 7.47-4.11 11.8-4.11zm75.2-20.5h2.65l-11 49.7c-0.66 2.97-0.99 5.19-0.99 6.65 0 1.2 0.2 2.07 0.58 2.61 0.39 0.54 1.01 0.81 1.85 0.81 0.89 0 1.85-0.37 2.87-1.1 1.02-0.74 2.46-2.14 4.31-4.2l2.56 2.51c-2.9 3.06-5.27 5.15-7.12 6.27-1.84 1.12-3.9 1.68-6.17 1.68-1.95 0-3.51-0.71-4.67-2.13-1.17-1.42-1.76-3.24-1.76-5.46 0-2.01 0.42-4.91 1.26-8.71l8.04-36c0.36-1.58 0.62-2.85 0.77-3.79 0.15-0.95 0.22-1.82 0.22-2.63 0-0.9-0.16-1.58-0.49-2.04-0.33-0.47-0.8-0.81-1.42-1.01-0.61-0.21-1.71-0.36-3.3-0.45l0.54-2.34zm-29 0h2.65l-11 49.7c-0.66 2.97-0.99 5.19-0.99 6.65 0 1.2 0.2 2.07 0.58 2.61 0.39 0.54 1.01 0.81 1.85 0.81 0.89 0 1.85-0.37 2.87-1.1 1.02-0.74 2.46-2.14 4.31-4.2l2.56 2.51c-2.9 3.06-5.27 5.15-7.12 6.27-1.84 1.12-3.9 1.68-6.17 1.68-1.95 0-3.51-0.71-4.67-2.13-1.17-1.42-1.76-3.24-1.76-5.46 0-2.01 0.42-4.91 1.26-8.71l8.04-36c0.36-1.58 0.62-2.85 0.77-3.79 0.15-0.95 0.22-1.82 0.22-2.63 0-0.9-0.16-1.58-0.49-2.04-0.33-0.47-0.8-0.81-1.42-1.01-0.61-0.21-1.71-0.36-3.3-0.45l0.54-2.34z\" />\n <path d=\"m2875 1098 17.9 40.3v3.32l-17.9 40.4-4.81-1.71 14.4-40.3-14.4-40.4z\" />\n <!-- |good> ket -->\n <path d=\"m1889 530h6.91v83.6h-6.91z\" />\n <path d=\"m2040 554c-3.11 0-5.83 1.3-8.15 3.91-2.32 2.6-4.15 6.33-5.48 11.2-1.33 4.85-2 9.03-2 12.5 0 3.29 0.68 5.73 2.05 7.3 1.36 1.57 3.42 2.36 6.17 2.36 2.94 0 5.54-1.32 7.82-3.95 2.28-2.64 4.07-6.33 5.39-11.1 1.32-4.75 1.98-8.92 1.98-12.5 0-3.24-0.63-5.67-1.89-7.3s-3.22-2.45-5.89-2.45zm-49 0c-3.11 0-5.83 1.3-8.15 3.91-2.32 2.6-4.15 6.33-5.48 11.2-1.33 4.85-2 9.03-2 12.5 0 3.29 0.68 5.73 2.05 7.3 1.36 1.57 3.42 2.36 6.17 2.36 2.94 0 5.54-1.32 7.82-3.95 2.28-2.64 4.07-6.33 5.39-11.1 1.32-4.75 1.98-8.92 1.98-12.5 0-3.24-0.63-5.67-1.89-7.3s-3.22-2.45-5.89-2.45zm98.3-0.09c-3.05 0-5.79 1.24-8.22 3.71-2.42 2.47-4.33 5.8-5.72 10-1.4 4.19-2.09 8.37-2.09 12.5 0 3.12 0.42 5.44 1.28 6.99 0.85 1.54 2.28 2.31 4.29 2.31 0.93 0 1.87-0.202 2.83-0.606 0.96-0.405 1.95-1.06 2.99-1.96 1.03-0.898 2.05-1.99 3.05-3.28s1.92-2.58 2.76-3.89c0.84-1.3 1.62-2.84 2.34-4.63 0.72-1.78 1.33-3.93 1.84-6.45l0.4-1.93c0.45-2.16 0.68-4.19 0.68-6.11 0-2.34-0.5-4.04-1.48-5.1-0.99-1.06-2.64-1.6-4.95-1.6zm-148 0c-3.05 0-5.79 1.24-8.22 3.71-2.42 2.47-4.33 5.8-5.72 10-1.4 4.19-2.09 8.37-2.09 12.5 0 3.12 0.45 5.44 1.35 6.99 0.89 1.54 2.3 2.31 4.22 2.31 1.53 0 3.05-0.524 4.56-1.57s3.13-2.71 4.85-4.99 3.06-4.45 4.02-6.51c0.96-2.07 1.77-4.64 2.45-7.73 0.67-3.08 1.01-5.76 1.01-8.04 0-2.34-0.5-4.04-1.48-5.1-0.99-1.06-2.64-1.6-4.95-1.6zm100-3.5c4.8 0 8.45 1.31 11 3.93 2.52 2.62 3.78 6.37 3.78 11.3 0 3.47-0.61 7.19-1.82 11.1s-2.88 7.28-4.99 9.97c-2.11 2.7-4.63 4.72-7.57 6.09-2.93 1.36-6.3 2.04-10.1 2.04-4.65 0-8.25-1.3-10.8-3.91-2.56-2.6-3.84-6.35-3.84-11.2 0-2.4 0.36-5.17 1.08-8.31 0.93-4.25 2.48-7.97 4.67-11.1 2.19-3.18 4.88-5.61 8.09-7.3 3.2-1.69 6.72-2.54 10.6-2.54zm-49 0c4.8 0 8.45 1.31 11 3.93 2.52 2.62 3.78 6.37 3.78 11.3 0 3.47-0.61 7.19-1.82 11.1s-2.88 7.28-4.99 9.97c-2.11 2.7-4.63 4.72-7.57 6.09-2.93 1.36-6.3 2.04-10.1 2.04-4.65 0-8.25-1.3-10.8-3.91-2.56-2.6-3.84-6.35-3.84-11.2 0-2.4 0.36-5.17 1.08-8.31 0.93-4.25 2.48-7.97 4.67-11.1 2.19-3.18 4.88-5.61 8.09-7.3 3.2-1.69 6.72-2.54 10.6-2.54zm-51.7-0.045c1.88 0 3.66 0.225 5.34 0.674s3.37 1.21 5.08 2.29l4.13-2.96 2.88 0.719-4.63 20.9c-0.33 1.47-1.08 5.17-2.25 11.1-1.23 6.29-2.22 10.7-2.98 13.3-0.77 2.6-1.72 4.98-2.86 7.12s-2.6 4-4.4 5.59c-1.8 1.59-3.98 2.82-6.54 3.68-2.56 0.869-5.59 1.3-9.09 1.3-11.3 0-17-3.29-17-9.88 0-1.68 0.51-3.2 1.52-4.58 1.02-1.38 2.65-2.83 4.9-4.36l3.24 2.6c-0.57 0.689-1.05 1.37-1.44 2.04s-0.67 1.32-0.83 1.95c-0.17 0.629-0.25 1.38-0.25 2.25 0 2.25 0.81 3.89 2.43 4.92 1.61 1.03 4.05 1.55 7.32 1.55 3.68 0 6.64-0.831 8.87-2.49s4.14-4.34 5.73-8.02c1.58-3.68 3.13-9.25 4.62-16.7l-0.62-0.134c-2.91 3.98-5.6 6.89-8.09 8.72-2.49 1.83-5.17 2.74-8.04 2.74-3.27 0-5.8-1.23-7.62-3.68-1.81-2.46-2.71-5.96-2.71-10.5 0-5.12 1-10 3.01-14.8 2-4.72 4.79-8.47 8.35-11.3 3.57-2.78 7.53-4.18 11.9-4.18zm168-20.5h2.65l-11.2 49.7c-0.66 2.94-0.99 5.15-0.99 6.65 0 1.2 0.2 2.07 0.59 2.6s1 0.809 1.84 0.809c0.9 0 1.86-0.367 2.87-1.1 1.02-0.734 2.46-2.13 4.32-4.2l2.56 2.52c-2.76 2.9-5.08 4.96-6.97 6.15-1.88 1.2-3.92 1.8-6.1 1.8-1.86 0-3.34-0.614-4.45-1.84s-1.66-2.86-1.66-4.9c0-1.65 0.34-3.4 1.03-5.26l-0.59-0.18c-2.87 4.19-5.6 7.26-8.17 9.21-2.58 1.95-5.3 2.92-8.18 2.92-3.29 0-5.84-1.24-7.66-3.73-1.81-2.48-2.71-5.97-2.71-10.5 0-5.15 1.01-10.1 3.03-14.8s4.8-8.49 8.33-11.2c3.54-2.74 7.47-4.11 11.8-4.11 1.82 0 3.51 0.187 5.05 0.562 1.54 0.374 3.12 1.02 4.74 1.95l2.33-10.4c0.24-0.989 0.45-2.08 0.63-3.28s0.27-2.25 0.27-3.14c0-0.899-0.16-1.58-0.49-2.04s-0.8-0.801-1.42-1.01c-0.61-0.209-1.71-0.359-3.3-0.449l0.54-2.34z\" />\n <path d=\"m2126 529 17.9 40.3v3.32l-17.9 40.4-4.81-1.71 14.4-40.3-14.4-40.4z\" />\n <!-- |bad> ket -->\n <path d=\"m2766 1275h6.92v83.6h-6.92z\" />\n <path d=\"m2816 1301c-1.43 0-2.85 0.46-4.24 1.37s-2.94 2.43-4.65 4.54-3.1 4.27-4.18 6.49-1.99 5.05-2.74 8.49l-0.4 1.89c-0.45 2.09-0.68 4.13-0.68 6.11 0 2.36 0.52 4.07 1.55 5.12 1.04 1.05 2.75 1.57 5.15 1.57 1.22 0 2.39-0.2 3.5-0.61 1.11-0.4 2.24-1.07 3.39-2 1.15-0.92 2.25-2.14 3.28-3.66 1.03-1.51 1.99-3.37 2.85-5.59 0.87-2.22 1.55-4.59 2.03-7.12 0.47-2.53 0.71-4.95 0.71-7.25 0-3.12-0.44-5.46-1.32-7.01-0.88-1.56-2.3-2.34-4.25-2.34zm97.4-1.75c-3.06 0-5.8 1.23-8.22 3.71-2.43 2.47-4.34 5.8-5.73 9.99s-2.09 8.37-2.09 12.5c0 3.12 0.43 5.45 1.28 6.99s2.29 2.31 4.29 2.31c0.93 0 1.87-0.2 2.83-0.6 0.96-0.41 1.96-1.06 2.99-1.96s2.05-1.99 3.05-3.28c1.01-1.29 1.93-2.58 2.77-3.88 0.83-1.31 1.61-2.85 2.33-4.63s1.33-3.93 1.84-6.45l0.41-1.93c0.45-2.15 0.67-4.19 0.67-6.11 0-2.33-0.49-4.03-1.48-5.1-0.99-1.06-2.64-1.59-4.94-1.59zm-50.9 0c-3.09 0-5.86 1.24-8.31 3.73-2.46 2.48-4.37 5.82-5.75 9.99-1.38 4.18-2.07 8.35-2.07 12.5 0 3.12 0.43 5.45 1.28 6.99s2.29 2.31 4.29 2.31c1.98 0 3.92-0.85 5.82-2.56 1.9-1.7 3.86-4.14 5.88-7.3 2.03-3.16 3.46-6.8 4.29-10.9l0.41-1.93c0.27-1.23 0.45-2.29 0.54-3.19s0.13-1.87 0.13-2.92c0-2.3-0.49-4-1.48-5.07-0.99-1.08-2.67-1.62-5.03-1.62zm-0.9-3.55c1.95 0 3.77 0.22 5.46 0.65 1.69 0.44 3.43 1.21 5.23 2.32l4.13-2.97 2.88 0.72-6.6 28.5c-0.66 2.84-0.99 5.06-0.99 6.65 0 1.2 0.19 2.06 0.58 2.6s1 0.81 1.84 0.81c0.9 0 1.86-0.36 2.88-1.1 1.02-0.73 2.45-2.13 4.31-4.2l2.56 2.52c-2.69 2.87-4.99 4.92-6.89 6.13-1.91 1.21-3.97 1.82-6.18 1.82-1.86 0-3.34-0.62-4.45-1.84-1.11-1.23-1.66-2.86-1.66-4.9 0-1.65 0.34-3.4 1.03-5.26l-0.58-0.18c-2.88 4.2-5.6 7.27-8.18 9.21-2.57 1.95-5.3 2.92-8.17 2.92-3.3 0-5.85-1.24-7.66-3.73-1.81-2.48-2.72-5.97-2.72-10.5 0-5.15 1.01-10.1 3.03-14.8s4.8-8.49 8.34-11.2c3.53-2.74 7.47-4.11 11.8-4.11zm70.6-20.5h2.65l-11.2 49.7c-0.66 2.93-0.99 5.15-0.99 6.65 0 1.2 0.19 2.06 0.58 2.6s1 0.81 1.84 0.81c0.9 0 1.86-0.36 2.88-1.1 1.02-0.73 2.45-2.13 4.31-4.2l2.56 2.52c-2.75 2.9-5.08 4.95-6.96 6.15-1.89 1.2-3.93 1.8-6.11 1.8-1.86 0-3.34-0.62-4.45-1.84-1.11-1.23-1.66-2.86-1.66-4.9 0-1.65 0.34-3.4 1.03-5.26l-0.58-0.18c-2.88 4.2-5.6 7.27-8.18 9.21-2.57 1.95-5.3 2.92-8.17 2.92-3.3 0-5.85-1.24-7.66-3.73-1.81-2.48-2.72-5.97-2.72-10.5 0-5.15 1.01-10.1 3.03-14.8s4.8-8.49 8.34-11.2c3.53-2.74 7.47-4.11 11.8-4.11 1.83 0 3.51 0.19 5.05 0.56 1.55 0.38 3.13 1.03 4.74 1.96l2.34-10.4c0.24-0.99 0.45-2.08 0.63-3.28s0.27-2.25 0.27-3.14c0-0.9-0.17-1.58-0.5-2.05-0.33-0.46-0.8-0.8-1.41-1.01-0.62-0.21-1.72-0.36-3.3-0.45l0.54-2.33zm-124 0h2.61l-7.91 31.4 0.59 0.18c2.9-3.9 5.63-6.71 8.17-8.45 2.55-1.74 5.09-2.6 7.64-2.6 3.26 0 5.8 1.24 7.61 3.72 1.81 2.49 2.72 5.99 2.72 10.5 0 4.94-1 9.8-2.99 14.6-1.99 4.78-4.75 8.57-8.29 11.4-3.53 2.8-7.48 4.2-11.8 4.2-3.99 0-7.52-0.98-10.6-2.92l-4.13 2.92-2.87-0.72 11.6-51.4c0.69-3.05 1.03-5.27 1.03-6.64 0-1.26-0.38-2.15-1.14-2.66-0.77-0.5-2.14-0.79-4.11-0.85l0.54-2.33z\" />\n <path d=\"m2950 1275 17.9 40.3v3.33l-17.9 40.4-4.8-1.71 14.4-40.3-14.4-40.4z\" />\n <!-- theta-->\n <path d=\"m2414 1260c-1.4 6.49-2.11 12-2.11 16.4 0 3.36 0.51 5.82 1.53 7.39s2.67 2.36 4.94 2.36c3.36 0 6.54-2.25 9.55-6.76s5.56-11 7.66-19.4zm18.4-30.3c-3.5 0-6.74 2.22-9.72 6.65s-5.52 10.8-7.62 19h21.5c1.41-6.31 2.11-11.8 2.11-16.3 0-3.15-0.51-5.5-1.53-7.06-1.01-1.55-2.6-2.33-4.76-2.33zm0.54-3.64c8.69 0 13 5.51 13 16.5 0 3.12-0.35 6.84-1.06 11.2-0.7 4.33-1.82 8.74-3.34 13.2-1.53 4.5-3.46 8.47-5.8 11.9-2.33 3.46-5.03 6.13-8.08 8.02-3.06 1.89-6.35 2.83-9.89 2.83-4.46 0-7.78-1.36-9.97-4.09-2.19-2.72-3.28-6.81-3.28-12.3 0-2.97 0.33-6.62 0.99-11 0.66-4.35 1.74-8.79 3.26-13.3 1.51-4.54 3.45-8.57 5.84-12.1 2.38-3.52 5.12-6.23 8.22-8.13s6.46-2.85 10.1-2.85z\" fill-rule=\"evenodd\" />\n <!-- lower theta -->\n <path d=\"m2415 1364c-1.41 6.5-2.11 12-2.11 16.4 0 3.35 0.51 5.82 1.52 7.39 1.02 1.57 2.67 2.36 4.95 2.36 3.35 0 6.53-2.26 9.54-6.76 3.01-4.51 5.56-11 7.66-19.4zm18.4-30.3c-3.51 0-6.75 2.21-9.73 6.64-2.98 4.44-5.52 10.8-7.61 19h21.5c1.41-6.32 2.12-11.8 2.12-16.3 0-3.14-0.51-5.49-1.53-7.05s-2.61-2.33-4.76-2.33zm0.54-3.64c8.68 0 13 5.51 13 16.5 0 3.11-0.35 6.83-1.05 11.2-0.71 4.33-1.82 8.74-3.35 13.2s-3.46 8.47-5.79 11.9c-2.34 3.46-5.04 6.13-8.09 8.02-3.06 1.88-6.35 2.83-9.88 2.83-4.47 0-7.79-1.37-9.98-4.09-2.18-2.73-3.28-6.82-3.28-12.3 0-2.96 0.33-6.61 0.99-11 0.66-4.34 1.75-8.78 3.26-13.3 1.51-4.53 3.46-8.56 5.84-12.1s5.12-6.23 8.22-8.13 6.46-2.85 10.1-2.85z\" />\n </g>\n <g class=\"kata_svg_fill_accent kata_svg_stroke_accent\" font-family=\"sans-serif\" font-size=\"64px\" font-weight=\"400\">\n <text transform=\"translate(2490 780)\">State after</text>\n <text transform=\"translate(2550 840)\">the second</text>\n <text transform=\"translate(2610 900)\">reflection</text>\n <!--2 \\theta and angle -->\n <path d=\"m2410 1105c-1.41 6.5-2.12 12-2.12 16.4 0 3.35 0.51 5.82 1.53 7.39s2.67 2.36 4.94 2.36c3.36 0 6.54-2.25 9.55-6.76s5.56-11 7.66-19.4zm18.4-30.3c-3.5 0-6.74 2.22-9.72 6.65s-5.52 10.8-7.62 19h21.5c1.41-6.32 2.11-11.8 2.11-16.3 0-3.14-0.51-5.5-1.52-7.05-1.02-1.56-2.61-2.34-4.77-2.34zm-58.2-2.7c5.57 0 9.77 1.22 12.6 3.66 2.84 2.44 4.27 5.98 4.27 10.6 0 1.59-0.16 3.05-0.48 4.38-0.31 1.33-0.8 2.65-1.48 3.95-0.67 1.3-1.58 2.7-2.72 4.2-1.13 1.5-2.39 3.02-3.77 4.56s-3.76 4.11-7.14 7.7c-5.69 6.02-9.87 11.3-12.5 15.8h16.9c1.47 0 2.65-0.09 3.55-0.27s1.62-0.479 2.16-0.898c0.54-0.42 0.96-0.966 1.28-1.64 0.31-0.674 0.71-1.72 1.19-3.12h3.82l-0.77 13.1h-37v-2.16c1.29-3.14 3.03-6.36 5.21-9.66 2.19-3.3 5.29-7.17 9.3-11.6 3.47-3.83 5.98-6.74 7.5-8.72 1.68-2.16 2.93-4.02 3.75-5.59 0.83-1.57 1.42-3.06 1.78-4.45s0.54-2.79 0.54-4.2c0-2.25-0.36-4.23-1.08-5.95s-1.82-3.08-3.3-4.07c-1.49-0.988-3.35-1.48-5.6-1.48-5.6 0-9.37 3.11-11.3 9.34h-5.43v-8.62c3.62-1.71 7.01-2.94 10.2-3.71 3.16-0.763 6-1.14 8.52-1.14zm58.7-0.943c8.69 0 13 5.51 13 16.5 0 3.11-0.35 6.84-1.06 11.2-0.7 4.33-1.82 8.74-3.34 13.2-1.53 4.49-3.46 8.47-5.8 11.9-2.33 3.46-5.03 6.13-8.08 8.02-3.06 1.89-6.35 2.83-9.89 2.83-4.46 0-7.78-1.36-9.97-4.09s-3.28-6.81-3.28-12.3c0-2.96 0.33-6.62 0.99-11 0.66-4.34 1.74-8.78 3.26-13.3 1.51-4.54 3.46-8.56 5.84-12.1s5.12-6.23 8.22-8.13 6.46-2.85 10.1-2.85z\" fill=\"#0070c0\" fill-rule=\"evenodd\" />\n </g>\n </g>\n</svg>\n<p>As we can see, the pair of these reflections combined amount to a counterclockwise rotation by an angle $2\\theta$.\nIf we repeat the Grover's iteration, reflecting the new state first along the horizontal axis and then along the $\\ket{\\text{all}}$ vector, it performs a rotation by $2\\theta$ again. The angle of this rotation depends only on the angle between the reflection axes and not on the state we reflect!</p>\n<p>Each iteration of Grover's search adds $2\\theta$ to the current angle in the expression of the system state as a superposition of $\\ket{\\text{good}}$ and $\\ket{\\text{bad}}$.\nAfter applying $R$ iterations of Grover's search the state of the system will become</p>\n<p>$$\\sin{(2R+1)\\theta}\\ket{\\text{good}} + \\cos{(2R+1)\\theta}\\ket{\\text{bad}}$$</p>\n<p>At first, each iteration brings the state of the system closer to the vertical axis, increasing the probability of measuring one of the basis states that are part of $\\ket{\\text{good}}$ - the states that are solutions to the problem.</p>\n"
6984
6984
  }
6985
6985
  ]
6986
6986
  },
@@ -7035,7 +7035,7 @@ export default {
7035
7035
  {
7036
7036
  "type": "solution",
7037
7037
  "id": "grovers_search__conditional_phase_flip_solution",
7038
- "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n operation ConditionalPhaseFlip(qs : Qubit[]) : Unit is Adj + Ctl {\n within {\n ApplyToEachA(X, qs);\n } apply {\n Controlled Z(qs[1 ...], qs[0]);\n }\n R(PauliI, 2.0 * PI(), qs[0]);\n }\n}\n"
7038
+ "code": "namespace Kata {\n import Std.Math.*;\n operation ConditionalPhaseFlip(qs : Qubit[]) : Unit is Adj + Ctl {\n within {\n ApplyToEachA(X, qs);\n } apply {\n Controlled Z(qs[1...], qs[0]);\n }\n R(PauliI, 2.0 * PI(), qs[0]);\n }\n}\n"
7039
7039
  }
7040
7040
  ]
7041
7041
  }
@@ -7052,7 +7052,7 @@ export default {
7052
7052
  "grovers_search__reflection_about_state__Verification.qs",
7053
7053
  "KatasLibrary.qs"
7054
7054
  ],
7055
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n operation ReflectionAboutState(\n qs : Qubit[],\n statePrep : Qubit[] => Unit is Adj + Ctl)\n : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n\n // You might find this helper operation from an earlier task useful.\n operation ConditionalPhaseFlip(qs : Qubit[]) : Unit is Adj + Ctl {\n within {\n ApplyToEachA(X, qs);\n } apply {\n Controlled Z(qs[1 ...], qs[0]);\n }\n R(PauliI, 2.0 * PI(), qs[0]);\n }\n}\n",
7055
+ "placeholderCode": "namespace Kata {\n import Std.Math.*;\n\n operation ReflectionAboutState(\n qs : Qubit[],\n statePrep : Qubit[] => Unit is Adj + Ctl\n ) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n\n // You might find this helper operation from an earlier task useful.\n operation ConditionalPhaseFlip(qs : Qubit[]) : Unit is Adj + Ctl {\n within {\n ApplyToEachA(X, qs);\n } apply {\n Controlled Z(qs[1...], qs[0]);\n }\n R(PauliI, 2.0 * PI(), qs[0]);\n }\n}\n",
7056
7056
  "explainedSolution": {
7057
7057
  "type": "explained-solution",
7058
7058
  "items": [
@@ -7063,7 +7063,7 @@ export default {
7063
7063
  {
7064
7064
  "type": "solution",
7065
7065
  "id": "grovers_search__reflection_about_state_solution",
7066
- "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n operation ReflectionAboutState(\n qs : Qubit[],\n statePrep : Qubit[] => Unit is Adj + Ctl)\n : Unit is Adj + Ctl {\n within {\n Adjoint statePrep(qs);\n } apply {\n ConditionalPhaseFlip(qs);\n }\n }\n\n operation ConditionalPhaseFlip(qs : Qubit[]) : Unit is Adj + Ctl {\n within {\n ApplyToEachA(X, qs);\n } apply {\n Controlled Z(qs[1 ...], qs[0]);\n }\n R(PauliI, 2.0 * PI(), qs[0]);\n }\n}\n"
7066
+ "code": "namespace Kata {\n import Std.Math.*;\n\n operation ReflectionAboutState(\n qs : Qubit[],\n statePrep : Qubit[] => Unit is Adj + Ctl\n ) : Unit is Adj + Ctl {\n within {\n Adjoint statePrep(qs);\n } apply {\n ConditionalPhaseFlip(qs);\n }\n }\n\n operation ConditionalPhaseFlip(qs : Qubit[]) : Unit is Adj + Ctl {\n within {\n ApplyToEachA(X, qs);\n } apply {\n Controlled Z(qs[1...], qs[0]);\n }\n R(PauliI, 2.0 * PI(), qs[0]);\n }\n}\n"
7067
7067
  }
7068
7068
  ]
7069
7069
  }
@@ -7075,16 +7075,16 @@ export default {
7075
7075
  "items": [
7076
7076
  {
7077
7077
  "type": "text-content",
7078
- "content": "<p>The optimal number of iterations to use in Grover's search algorithm is typically defined as the number of iterations\nafter which the success probability of the algorithm - the probability of measuring one of the &quot;good&quot; states - is maximized.</p>\n<p>Geometrically, this means that the state vector should be rotated to be as close to the vertical axis as possible.\nMathematically, this means maximizing the ampitude $\\sin{(2R+1)\\theta}$ of the state $\\ket{\\text{good}}$\nin the superposition.\nWith either definition, the goal is to have the angle $(2R+1)\\theta$ that describes the system after $R$ rotations\nas close to $\\frac{\\pi}{2}$ as possible:</p>\n<p>$$(2R+1)\\theta \\approx \\frac{\\pi}{2}$$</p>\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<svg width=\"447\" height=\"370\" overflow=\"hidden\" version=\"1.1\" xml:space=\"preserve\" xmlns=\"http://www.w3.org/2000/svg\">\n<style>\n/* Default values, or theme set explicitly to light */\n:root, [data-theme=\"light\"] {\n --kata-svg-stroke: #222;\n --kata-svg-fill: #fff;\n --kata-svg-path: #777;\n --kata-svg-accent: #06c;\n}\n/* User has set OS preference for dark. (An explict light theme will override) */\n@media(prefers-color-scheme: dark) {\n :root {\n --kata-svg-stroke: #eee;\n --kata-svg-fill: #111;\n --kata-svg-path: #bbb;\n --kata-svg-accent: #08f;\n }\n}\n/* Explicit dark theme set (should match above dark preference values) */\n[data-theme=\"dark\"] {\n --kata-svg-stroke: #eee;\n --kata-svg-fill: #111;\n --kata-svg-path: #bbb;\n --kata-svg-accent: #08f;\n}\n/*** Kata specific styles ***/\n.kata_svg_path {\n stroke: var(--kata-svg-path);\n stroke-width: 2;\n stroke-linecap: round;\n fill: none;\n}\n.kata_svg_text {\n fill: var(--kata-svg-stroke);\n}\n.kata_svg_point {\n fill: var(--kata-svg-fill);\n stroke: var(--kata-svg-stroke);\n}\n.kata_svg_fill_accent {\n fill: var(--kata-svg-accent);\n}\n.kata_svg_stroke_accent {\n stroke: var(--kata-svg-accent);\n}\n</style>\n <g transform=\"matrix(.25 0 0 .25 -333 -132)\">\n <g class=\"kata_svg_path\">\n <!-- Main circle -->\n <circle cx=\"2020\" cy=\"1310\" r=\"670\" />\n <!-- x axis -->\n <path d=\"M2685 1310 h-1335 m1335 0 l-32 -32 m32 32 l-32 32\" />\n <!-- y axis -->\n <path d=\"m2020 645 v1323 m0 -1323 l-32 32 m32 -32 l32 32\" />\n <!-- Angle line -->\n <path d=\"m2665 1145 l-645 165 m645 -165 l-32 45 m32 -45 l -45 -25\" />\n <path d=\"m2357 1225c6.73 27 10.1 54.7 10.1 82.5\" fill-rule=\"evenodd\" />\n <!-- upper angle line -->\n <path d=\"M2490 845 l-470 465 m470 -465 l-50 0 m50 0 l0 50\" stroke-width=\"10\" stroke-linecap=\"round\" />\n <path d=\"m2270 1065c43.2 45.4 73.5 102 88 160\" />\n <path d=\"M2150 655 l-130 655 m130 -655 l-45 30 m45 -30 l30 45\" stroke-width=\"10\" stroke-linecap=\"round\" class=\"kata_svg_stroke_accent\"/>\n <path d=\"m2090 956c67.8 16 129 51.7 177 103\" class=\"kata_svg_stroke_accent\"/>\n </g>\n <g class=\"kata_svg_text\">\n <!-- |good> ket -->\n <path d=\"m1889 530h6.91v83.6h-6.91z\" />\n <path d=\"m2040 554c-3.11 0-5.83 1.3-8.15 3.91-2.32 2.6-4.15 6.33-5.48 11.2-1.33 4.85-2 9.03-2 12.5 0 3.29 0.68 5.73 2.05 7.3 1.36 1.57 3.42 2.36 6.17 2.36 2.94 0 5.54-1.32 7.82-3.95 2.28-2.64 4.07-6.33 5.39-11.1 1.32-4.75 1.98-8.92 1.98-12.5 0-3.24-0.63-5.67-1.89-7.3s-3.22-2.45-5.89-2.45zm-49 0c-3.11 0-5.83 1.3-8.15 3.91-2.32 2.6-4.15 6.33-5.48 11.2-1.33 4.85-2 9.03-2 12.5 0 3.29 0.68 5.73 2.05 7.3 1.36 1.57 3.42 2.36 6.17 2.36 2.94 0 5.54-1.32 7.82-3.95 2.28-2.64 4.07-6.33 5.39-11.1 1.32-4.75 1.98-8.92 1.98-12.5 0-3.24-0.63-5.67-1.89-7.3s-3.22-2.45-5.89-2.45zm98.3-0.09c-3.05 0-5.79 1.24-8.22 3.71-2.42 2.47-4.33 5.8-5.72 10-1.4 4.19-2.09 8.37-2.09 12.5 0 3.12 0.42 5.44 1.28 6.99 0.85 1.54 2.28 2.31 4.29 2.31 0.93 0 1.87-0.202 2.83-0.606 0.96-0.405 1.95-1.06 2.99-1.96 1.03-0.898 2.05-1.99 3.05-3.28s1.92-2.58 2.76-3.89c0.84-1.3 1.62-2.84 2.34-4.63 0.72-1.78 1.33-3.93 1.84-6.45l0.4-1.93c0.45-2.16 0.68-4.19 0.68-6.11 0-2.34-0.5-4.04-1.48-5.1-0.99-1.06-2.64-1.6-4.95-1.6zm-148 0c-3.05 0-5.79 1.24-8.22 3.71-2.42 2.47-4.33 5.8-5.72 10-1.4 4.19-2.09 8.37-2.09 12.5 0 3.12 0.45 5.44 1.35 6.99 0.89 1.54 2.3 2.31 4.22 2.31 1.53 0 3.05-0.524 4.56-1.57s3.13-2.71 4.85-4.99 3.06-4.45 4.02-6.51c0.96-2.07 1.77-4.64 2.45-7.73 0.67-3.08 1.01-5.76 1.01-8.04 0-2.34-0.5-4.04-1.48-5.1-0.99-1.06-2.64-1.6-4.95-1.6zm100-3.5c4.8 0 8.45 1.31 11 3.93 2.52 2.62 3.78 6.37 3.78 11.3 0 3.47-0.61 7.19-1.82 11.1s-2.88 7.28-4.99 9.97c-2.11 2.7-4.63 4.72-7.57 6.09-2.93 1.36-6.3 2.04-10.1 2.04-4.65 0-8.25-1.3-10.8-3.91-2.56-2.6-3.84-6.35-3.84-11.2 0-2.4 0.36-5.17 1.08-8.31 0.93-4.25 2.48-7.97 4.67-11.1 2.19-3.18 4.88-5.61 8.09-7.3 3.2-1.69 6.72-2.54 10.6-2.54zm-49 0c4.8 0 8.45 1.31 11 3.93 2.52 2.62 3.78 6.37 3.78 11.3 0 3.47-0.61 7.19-1.82 11.1s-2.88 7.28-4.99 9.97c-2.11 2.7-4.63 4.72-7.57 6.09-2.93 1.36-6.3 2.04-10.1 2.04-4.65 0-8.25-1.3-10.8-3.91-2.56-2.6-3.84-6.35-3.84-11.2 0-2.4 0.36-5.17 1.08-8.31 0.93-4.25 2.48-7.97 4.67-11.1 2.19-3.18 4.88-5.61 8.09-7.3 3.2-1.69 6.72-2.54 10.6-2.54zm-51.7-0.045c1.88 0 3.66 0.225 5.34 0.674s3.37 1.21 5.08 2.29l4.13-2.96 2.88 0.719-4.63 20.9c-0.33 1.47-1.08 5.17-2.25 11.1-1.23 6.29-2.22 10.7-2.98 13.3-0.77 2.6-1.72 4.98-2.86 7.12s-2.6 4-4.4 5.59c-1.8 1.59-3.98 2.82-6.54 3.68-2.56 0.869-5.59 1.3-9.09 1.3-11.3 0-17-3.29-17-9.88 0-1.68 0.51-3.2 1.52-4.58 1.02-1.38 2.65-2.83 4.9-4.36l3.24 2.6c-0.57 0.689-1.05 1.37-1.44 2.04s-0.67 1.32-0.83 1.95c-0.17 0.629-0.25 1.38-0.25 2.25 0 2.25 0.81 3.89 2.43 4.92 1.61 1.03 4.05 1.55 7.32 1.55 3.68 0 6.64-0.831 8.87-2.49s4.14-4.34 5.73-8.02c1.58-3.68 3.13-9.25 4.62-16.7l-0.62-0.134c-2.91 3.98-5.6 6.89-8.09 8.72-2.49 1.83-5.17 2.74-8.04 2.74-3.27 0-5.8-1.23-7.62-3.68-1.81-2.46-2.71-5.96-2.71-10.5 0-5.12 1-10 3.01-14.8 2-4.72 4.79-8.47 8.35-11.3 3.57-2.78 7.53-4.18 11.9-4.18zm168-20.5h2.65l-11.2 49.7c-0.66 2.94-0.99 5.15-0.99 6.65 0 1.2 0.2 2.07 0.59 2.6s1 0.809 1.84 0.809c0.9 0 1.86-0.367 2.87-1.1 1.02-0.734 2.46-2.13 4.32-4.2l2.56 2.52c-2.76 2.9-5.08 4.96-6.97 6.15-1.88 1.2-3.92 1.8-6.1 1.8-1.86 0-3.34-0.614-4.45-1.84s-1.66-2.86-1.66-4.9c0-1.65 0.34-3.4 1.03-5.26l-0.59-0.18c-2.87 4.19-5.6 7.26-8.17 9.21-2.58 1.95-5.3 2.92-8.18 2.92-3.29 0-5.84-1.24-7.66-3.73-1.81-2.48-2.71-5.97-2.71-10.5 0-5.15 1.01-10.1 3.03-14.8s4.8-8.49 8.33-11.2c3.54-2.74 7.47-4.11 11.8-4.11 1.82 0 3.51 0.187 5.05 0.562 1.54 0.374 3.12 1.02 4.74 1.95l2.33-10.4c0.24-0.989 0.45-2.08 0.63-3.28s0.27-2.25 0.27-3.14c0-0.899-0.16-1.58-0.49-2.04s-0.8-0.801-1.42-1.01c-0.61-0.209-1.71-0.359-3.3-0.449l0.54-2.34z\" />\n <path d=\"m2126 529 17.9 40.3v3.32l-17.9 40.4-4.81-1.71 14.4-40.3-14.4-40.4z\" />\n <!-- |bad> ket -->\n <path d=\"m2766 1275h6.92v83.6h-6.92z\" />\n <path d=\"m2816 1301c-1.43 0-2.85 0.46-4.24 1.37s-2.94 2.43-4.65 4.54-3.1 4.27-4.18 6.49-1.99 5.05-2.74 8.49l-0.4 1.89c-0.45 2.09-0.68 4.13-0.68 6.11 0 2.36 0.52 4.07 1.55 5.12 1.04 1.05 2.75 1.57 5.15 1.57 1.22 0 2.39-0.2 3.5-0.61 1.11-0.4 2.24-1.07 3.39-2 1.15-0.92 2.25-2.14 3.28-3.66 1.03-1.51 1.99-3.37 2.85-5.59 0.87-2.22 1.55-4.59 2.03-7.12 0.47-2.53 0.71-4.95 0.71-7.25 0-3.12-0.44-5.46-1.32-7.01-0.88-1.56-2.3-2.34-4.25-2.34zm97.4-1.75c-3.06 0-5.8 1.23-8.22 3.71-2.43 2.47-4.34 5.8-5.73 9.99s-2.09 8.37-2.09 12.5c0 3.12 0.43 5.45 1.28 6.99s2.29 2.31 4.29 2.31c0.93 0 1.87-0.2 2.83-0.6 0.96-0.41 1.96-1.06 2.99-1.96s2.05-1.99 3.05-3.28c1.01-1.29 1.93-2.58 2.77-3.88 0.83-1.31 1.61-2.85 2.33-4.63s1.33-3.93 1.84-6.45l0.41-1.93c0.45-2.15 0.67-4.19 0.67-6.11 0-2.33-0.49-4.03-1.48-5.1-0.99-1.06-2.64-1.59-4.94-1.59zm-50.9 0c-3.09 0-5.86 1.24-8.31 3.73-2.46 2.48-4.37 5.82-5.75 9.99-1.38 4.18-2.07 8.35-2.07 12.5 0 3.12 0.43 5.45 1.28 6.99s2.29 2.31 4.29 2.31c1.98 0 3.92-0.85 5.82-2.56 1.9-1.7 3.86-4.14 5.88-7.3 2.03-3.16 3.46-6.8 4.29-10.9l0.41-1.93c0.27-1.23 0.45-2.29 0.54-3.19s0.13-1.87 0.13-2.92c0-2.3-0.49-4-1.48-5.07-0.99-1.08-2.67-1.62-5.03-1.62zm-0.9-3.55c1.95 0 3.77 0.22 5.46 0.65 1.69 0.44 3.43 1.21 5.23 2.32l4.13-2.97 2.88 0.72-6.6 28.5c-0.66 2.84-0.99 5.06-0.99 6.65 0 1.2 0.19 2.06 0.58 2.6s1 0.81 1.84 0.81c0.9 0 1.86-0.36 2.88-1.1 1.02-0.73 2.45-2.13 4.31-4.2l2.56 2.52c-2.69 2.87-4.99 4.92-6.89 6.13-1.91 1.21-3.97 1.82-6.18 1.82-1.86 0-3.34-0.62-4.45-1.84-1.11-1.23-1.66-2.86-1.66-4.9 0-1.65 0.34-3.4 1.03-5.26l-0.58-0.18c-2.88 4.2-5.6 7.27-8.18 9.21-2.57 1.95-5.3 2.92-8.17 2.92-3.3 0-5.85-1.24-7.66-3.73-1.81-2.48-2.72-5.97-2.72-10.5 0-5.15 1.01-10.1 3.03-14.8s4.8-8.49 8.34-11.2c3.53-2.74 7.47-4.11 11.8-4.11zm70.6-20.5h2.65l-11.2 49.7c-0.66 2.93-0.99 5.15-0.99 6.65 0 1.2 0.19 2.06 0.58 2.6s1 0.81 1.84 0.81c0.9 0 1.86-0.36 2.88-1.1 1.02-0.73 2.45-2.13 4.31-4.2l2.56 2.52c-2.75 2.9-5.08 4.95-6.96 6.15-1.89 1.2-3.93 1.8-6.11 1.8-1.86 0-3.34-0.62-4.45-1.84-1.11-1.23-1.66-2.86-1.66-4.9 0-1.65 0.34-3.4 1.03-5.26l-0.58-0.18c-2.88 4.2-5.6 7.27-8.18 9.21-2.57 1.95-5.3 2.92-8.17 2.92-3.3 0-5.85-1.24-7.66-3.73-1.81-2.48-2.72-5.97-2.72-10.5 0-5.15 1.01-10.1 3.03-14.8s4.8-8.49 8.34-11.2c3.53-2.74 7.47-4.11 11.8-4.11 1.83 0 3.51 0.19 5.05 0.56 1.55 0.38 3.13 1.03 4.74 1.96l2.34-10.4c0.24-0.99 0.45-2.08 0.63-3.28s0.27-2.25 0.27-3.14c0-0.9-0.17-1.58-0.5-2.05-0.33-0.46-0.8-0.8-1.41-1.01-0.62-0.21-1.72-0.36-3.3-0.45l0.54-2.33zm-124 0h2.61l-7.91 31.4 0.59 0.18c2.9-3.9 5.63-6.71 8.17-8.45 2.55-1.74 5.09-2.6 7.64-2.6 3.26 0 5.8 1.24 7.61 3.72 1.81 2.49 2.72 5.99 2.72 10.5 0 4.94-1 9.8-2.99 14.6-1.99 4.78-4.75 8.57-8.29 11.4-3.53 2.8-7.48 4.2-11.8 4.2-3.99 0-7.52-0.98-10.6-2.92l-4.13 2.92-2.87-0.72 11.6-51.4c0.69-3.05 1.03-5.27 1.03-6.64 0-1.26-0.38-2.15-1.14-2.66-0.77-0.5-2.14-0.79-4.11-0.85l0.54-2.33z\" />\n <path d=\"m2950 1275 17.9 40.3v3.33l-17.9 40.4-4.8-1.71 14.4-40.3-14.4-40.4z\" />\n <!-- theta-->\n <path d=\"m2414 1260c-1.4 6.49-2.11 12-2.11 16.4 0 3.36 0.51 5.82 1.53 7.39s2.67 2.36 4.94 2.36c3.36 0 6.54-2.25 9.55-6.76s5.56-11 7.66-19.4zm18.4-30.3c-3.5 0-6.74 2.22-9.72 6.65s-5.52 10.8-7.62 19h21.5c1.41-6.31 2.11-11.8 2.11-16.3 0-3.15-0.51-5.5-1.53-7.06-1.01-1.55-2.6-2.33-4.76-2.33zm0.54-3.64c8.69 0 13 5.51 13 16.5 0 3.12-0.35 6.84-1.06 11.2-0.7 4.33-1.82 8.74-3.34 13.2-1.53 4.5-3.46 8.47-5.8 11.9-2.33 3.46-5.03 6.13-8.08 8.02-3.06 1.89-6.35 2.83-9.89 2.83-4.46 0-7.78-1.36-9.97-4.09-2.19-2.72-3.28-6.81-3.28-12.3 0-2.97 0.33-6.62 0.99-11 0.66-4.35 1.74-8.79 3.26-13.3 1.51-4.54 3.45-8.57 5.84-12.1 2.38-3.52 5.12-6.23 8.22-8.13s6.46-2.85 10.1-2.85z\" fill-rule=\"evenodd\" />\n <!--2 \\theta and angle -->\n <path d=\"m2410 1105c-1.41 6.5-2.12 12-2.12 16.4 0 3.35 0.51 5.82 1.53 7.39s2.67 2.36 4.94 2.36c3.36 0 6.54-2.25 9.55-6.76s5.56-11 7.66-19.4zm18.4-30.3c-3.5 0-6.74 2.22-9.72 6.65s-5.52 10.8-7.62 19h21.5c1.41-6.32 2.11-11.8 2.11-16.3 0-3.14-0.51-5.5-1.52-7.05-1.02-1.56-2.61-2.34-4.77-2.34zm-58.2-2.7c5.57 0 9.77 1.22 12.6 3.66 2.84 2.44 4.27 5.98 4.27 10.6 0 1.59-0.16 3.05-0.48 4.38-0.31 1.33-0.8 2.65-1.48 3.95-0.67 1.3-1.58 2.7-2.72 4.2-1.13 1.5-2.39 3.02-3.77 4.56s-3.76 4.11-7.14 7.7c-5.69 6.02-9.87 11.3-12.5 15.8h16.9c1.47 0 2.65-0.09 3.55-0.27s1.62-0.479 2.16-0.898c0.54-0.42 0.96-0.966 1.28-1.64 0.31-0.674 0.71-1.72 1.19-3.12h3.82l-0.77 13.1h-37v-2.16c1.29-3.14 3.03-6.36 5.21-9.66 2.19-3.3 5.29-7.17 9.3-11.6 3.47-3.83 5.98-6.74 7.5-8.72 1.68-2.16 2.93-4.02 3.75-5.59 0.83-1.57 1.42-3.06 1.78-4.45s0.54-2.79 0.54-4.2c0-2.25-0.36-4.23-1.08-5.95s-1.82-3.08-3.3-4.07c-1.49-0.988-3.35-1.48-5.6-1.48-5.6 0-9.37 3.11-11.3 9.34h-5.43v-8.62c3.62-1.71 7.01-2.94 10.2-3.71 3.16-0.763 6-1.14 8.52-1.14zm58.7-0.943c8.69 0 13 5.51 13 16.5 0 3.11-0.35 6.84-1.06 11.2-0.7 4.33-1.82 8.74-3.34 13.2-1.53 4.49-3.46 8.47-5.8 11.9-2.33 3.46-5.03 6.13-8.08 8.02-3.06 1.89-6.35 2.83-9.89 2.83-4.46 0-7.78-1.36-9.97-4.09s-3.28-6.81-3.28-12.3c0-2.96 0.33-6.62 0.99-11 0.66-4.34 1.74-8.78 3.26-13.3 1.51-4.54 3.46-8.56 5.84-12.1s5.12-6.23 8.22-8.13 6.46-2.85 10.1-2.85z\" />\n </g>\n <g class=\"kata_svg_fill_accent kata_svg_stroke_accent\" font-family=\"sans-serif\" font-size=\"64px\" font-weight=\"400\">\n <text transform=\"translate(2220 580)\">Close enough</text>\n <text transform=\"translate(2220 650)\">to measure</text>\n <!--2 \\theta and angle -->\n <path d=\"m2210 905c-1.41 6.5-2.12 12-2.12 16.4 0 3.35 0.51 5.82 1.53 7.39s2.67 2.36 4.94 2.36c3.36 0 6.54-2.25 9.55-6.76s5.56-11 7.66-19.4zm18.4-30.3c-3.5 0-6.74 2.22-9.72 6.65s-5.52 10.8-7.62 19h21.5c1.41-6.32 2.11-11.8 2.11-16.3 0-3.14-0.51-5.5-1.52-7.05-1.02-1.56-2.61-2.34-4.77-2.34zm-58.2-2.7c5.57 0 9.77 1.22 12.6 3.66 2.84 2.44 4.27 5.98 4.27 10.6 0 1.59-0.16 3.05-0.48 4.38-0.31 1.33-0.8 2.65-1.48 3.95-0.67 1.3-1.58 2.7-2.72 4.2-1.13 1.5-2.39 3.02-3.77 4.56s-3.76 4.11-7.14 7.7c-5.69 6.02-9.87 11.3-12.5 15.8h16.9c1.47 0 2.65-0.09 3.55-0.27s1.62-0.479 2.16-0.898c0.54-0.42 0.96-0.966 1.28-1.64 0.31-0.674 0.71-1.72 1.19-3.12h3.82l-0.77 13.1h-37v-2.16c1.29-3.14 3.03-6.36 5.21-9.66 2.19-3.3 5.29-7.17 9.3-11.6 3.47-3.83 5.98-6.74 7.5-8.72 1.68-2.16 2.93-4.02 3.75-5.59 0.83-1.57 1.42-3.06 1.78-4.45s0.54-2.79 0.54-4.2c0-2.25-0.36-4.23-1.08-5.95s-1.82-3.08-3.3-4.07c-1.49-0.988-3.35-1.48-5.6-1.48-5.6 0-9.37 3.11-11.3 9.34h-5.43v-8.62c3.62-1.71 7.01-2.94 10.2-3.71 3.16-0.763 6-1.14 8.52-1.14zm58.7-0.943c8.69 0 13 5.51 13 16.5 0 3.11-0.35 6.84-1.06 11.2-0.7 4.33-1.82 8.74-3.34 13.2-1.53 4.49-3.46 8.47-5.8 11.9-2.33 3.46-5.03 6.13-8.08 8.02-3.06 1.89-6.35 2.83-9.89 2.83-4.46 0-7.78-1.36-9.97-4.09s-3.28-6.81-3.28-12.3c0-2.96 0.33-6.62 0.99-11 0.66-4.34 1.74-8.78 3.26-13.3 1.51-4.54 3.46-8.56 5.84-12.1s5.12-6.23 8.22-8.13 6.46-2.85 10.1-2.85z\" fill=\"#0070c0\" fill-rule=\"evenodd\" />\n </g>\n </g>\n</svg>\n<p>Now, recall that $\\theta = \\arcsin \\sqrt{\\frac{M}{N}}$. When $M$ is much smaller than $N$, $\\frac{M}{N}$ is close to 0, and $\\theta$ is a small angle that can approximated as $\\theta \\approx \\sqrt{\\frac{M}{N}}$. This gives the following equation for $R_{opt}$</p>\n<p>$$ 2R_{opt}+1 \\approx \\frac{\\pi}{2\\theta} = \\frac{\\pi}{2}\\sqrt{\\frac{N}{M}}$$\nSince $\\theta$ is small, $R_{opt}$ is large, and the $+1$ term next to $2R_{opt}$ can be ignored, giving the final formula:\n$$ R_{opt} \\approx \\frac{\\pi}{4}\\sqrt{\\frac{N}{M}}$$</p>\n<p>What happens if you use more iterations than is optimal? Each iteration will keep rotating the state $2\\theta$ counterclockwise,\ngetting it further away from the vertical axis and thus reducing the probability of measuring the correct answer.</p>\n<p>In this demo you will see how success probability of Grover's algorithm changes when it uses different numbers of iterations\nbefore the final measurements.</p>\n"
7078
+ "content": "<p>The optimal number of iterations to use in Grover's search algorithm is typically defined as the number of iterations\nafter which the success probability of the algorithm - the probability of measuring one of the &quot;good&quot; states - is maximized.</p>\n<p>Geometrically, this means that the state vector should be rotated to be as close to the vertical axis as possible.\nMathematically, this means maximizing the amplitude $\\sin{(2R+1)\\theta}$ of the state $\\ket{\\text{good}}$\nin the superposition.\nWith either definition, the goal is to have the angle $(2R+1)\\theta$ that describes the system after $R$ rotations\nas close to $\\frac{\\pi}{2}$ as possible:</p>\n<p>$$(2R+1)\\theta \\approx \\frac{\\pi}{2}$$</p>\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<svg width=\"447\" height=\"370\" overflow=\"hidden\" version=\"1.1\" xml:space=\"preserve\" xmlns=\"http://www.w3.org/2000/svg\">\n<style>\n/* Default values, or theme set explicitly to light */\n:root, [data-theme=\"light\"] {\n --kata-svg-stroke: #222;\n --kata-svg-fill: #fff;\n --kata-svg-path: #777;\n --kata-svg-accent: #06c;\n}\n/* User has set OS preference for dark. (An explict light theme will override) */\n@media(prefers-color-scheme: dark) {\n :root {\n --kata-svg-stroke: #eee;\n --kata-svg-fill: #111;\n --kata-svg-path: #bbb;\n --kata-svg-accent: #08f;\n }\n}\n/* Explicit dark theme set (should match above dark preference values) */\n[data-theme=\"dark\"] {\n --kata-svg-stroke: #eee;\n --kata-svg-fill: #111;\n --kata-svg-path: #bbb;\n --kata-svg-accent: #08f;\n}\n/*** Kata specific styles ***/\n.kata_svg_path {\n stroke: var(--kata-svg-path);\n stroke-width: 2;\n stroke-linecap: round;\n fill: none;\n}\n.kata_svg_text {\n fill: var(--kata-svg-stroke);\n}\n.kata_svg_point {\n fill: var(--kata-svg-fill);\n stroke: var(--kata-svg-stroke);\n}\n.kata_svg_fill_accent {\n fill: var(--kata-svg-accent);\n}\n.kata_svg_stroke_accent {\n stroke: var(--kata-svg-accent);\n}\n</style>\n <g transform=\"matrix(.25 0 0 .25 -333 -132)\">\n <g class=\"kata_svg_path\">\n <!-- Main circle -->\n <circle cx=\"2020\" cy=\"1310\" r=\"670\" />\n <!-- x axis -->\n <path d=\"M2685 1310 h-1335 m1335 0 l-32 -32 m32 32 l-32 32\" />\n <!-- y axis -->\n <path d=\"m2020 645 v1323 m0 -1323 l-32 32 m32 -32 l32 32\" />\n <!-- Angle line -->\n <path d=\"m2665 1145 l-645 165 m645 -165 l-32 45 m32 -45 l -45 -25\" />\n <path d=\"m2357 1225c6.73 27 10.1 54.7 10.1 82.5\" fill-rule=\"evenodd\" />\n <!-- upper angle line -->\n <path d=\"M2490 845 l-470 465 m470 -465 l-50 0 m50 0 l0 50\" stroke-width=\"10\" stroke-linecap=\"round\" />\n <path d=\"m2270 1065c43.2 45.4 73.5 102 88 160\" />\n <path d=\"M2150 655 l-130 655 m130 -655 l-45 30 m45 -30 l30 45\" stroke-width=\"10\" stroke-linecap=\"round\" class=\"kata_svg_stroke_accent\"/>\n <path d=\"m2090 956c67.8 16 129 51.7 177 103\" class=\"kata_svg_stroke_accent\"/>\n </g>\n <g class=\"kata_svg_text\">\n <!-- |good> ket -->\n <path d=\"m1889 530h6.91v83.6h-6.91z\" />\n <path d=\"m2040 554c-3.11 0-5.83 1.3-8.15 3.91-2.32 2.6-4.15 6.33-5.48 11.2-1.33 4.85-2 9.03-2 12.5 0 3.29 0.68 5.73 2.05 7.3 1.36 1.57 3.42 2.36 6.17 2.36 2.94 0 5.54-1.32 7.82-3.95 2.28-2.64 4.07-6.33 5.39-11.1 1.32-4.75 1.98-8.92 1.98-12.5 0-3.24-0.63-5.67-1.89-7.3s-3.22-2.45-5.89-2.45zm-49 0c-3.11 0-5.83 1.3-8.15 3.91-2.32 2.6-4.15 6.33-5.48 11.2-1.33 4.85-2 9.03-2 12.5 0 3.29 0.68 5.73 2.05 7.3 1.36 1.57 3.42 2.36 6.17 2.36 2.94 0 5.54-1.32 7.82-3.95 2.28-2.64 4.07-6.33 5.39-11.1 1.32-4.75 1.98-8.92 1.98-12.5 0-3.24-0.63-5.67-1.89-7.3s-3.22-2.45-5.89-2.45zm98.3-0.09c-3.05 0-5.79 1.24-8.22 3.71-2.42 2.47-4.33 5.8-5.72 10-1.4 4.19-2.09 8.37-2.09 12.5 0 3.12 0.42 5.44 1.28 6.99 0.85 1.54 2.28 2.31 4.29 2.31 0.93 0 1.87-0.202 2.83-0.606 0.96-0.405 1.95-1.06 2.99-1.96 1.03-0.898 2.05-1.99 3.05-3.28s1.92-2.58 2.76-3.89c0.84-1.3 1.62-2.84 2.34-4.63 0.72-1.78 1.33-3.93 1.84-6.45l0.4-1.93c0.45-2.16 0.68-4.19 0.68-6.11 0-2.34-0.5-4.04-1.48-5.1-0.99-1.06-2.64-1.6-4.95-1.6zm-148 0c-3.05 0-5.79 1.24-8.22 3.71-2.42 2.47-4.33 5.8-5.72 10-1.4 4.19-2.09 8.37-2.09 12.5 0 3.12 0.45 5.44 1.35 6.99 0.89 1.54 2.3 2.31 4.22 2.31 1.53 0 3.05-0.524 4.56-1.57s3.13-2.71 4.85-4.99 3.06-4.45 4.02-6.51c0.96-2.07 1.77-4.64 2.45-7.73 0.67-3.08 1.01-5.76 1.01-8.04 0-2.34-0.5-4.04-1.48-5.1-0.99-1.06-2.64-1.6-4.95-1.6zm100-3.5c4.8 0 8.45 1.31 11 3.93 2.52 2.62 3.78 6.37 3.78 11.3 0 3.47-0.61 7.19-1.82 11.1s-2.88 7.28-4.99 9.97c-2.11 2.7-4.63 4.72-7.57 6.09-2.93 1.36-6.3 2.04-10.1 2.04-4.65 0-8.25-1.3-10.8-3.91-2.56-2.6-3.84-6.35-3.84-11.2 0-2.4 0.36-5.17 1.08-8.31 0.93-4.25 2.48-7.97 4.67-11.1 2.19-3.18 4.88-5.61 8.09-7.3 3.2-1.69 6.72-2.54 10.6-2.54zm-49 0c4.8 0 8.45 1.31 11 3.93 2.52 2.62 3.78 6.37 3.78 11.3 0 3.47-0.61 7.19-1.82 11.1s-2.88 7.28-4.99 9.97c-2.11 2.7-4.63 4.72-7.57 6.09-2.93 1.36-6.3 2.04-10.1 2.04-4.65 0-8.25-1.3-10.8-3.91-2.56-2.6-3.84-6.35-3.84-11.2 0-2.4 0.36-5.17 1.08-8.31 0.93-4.25 2.48-7.97 4.67-11.1 2.19-3.18 4.88-5.61 8.09-7.3 3.2-1.69 6.72-2.54 10.6-2.54zm-51.7-0.045c1.88 0 3.66 0.225 5.34 0.674s3.37 1.21 5.08 2.29l4.13-2.96 2.88 0.719-4.63 20.9c-0.33 1.47-1.08 5.17-2.25 11.1-1.23 6.29-2.22 10.7-2.98 13.3-0.77 2.6-1.72 4.98-2.86 7.12s-2.6 4-4.4 5.59c-1.8 1.59-3.98 2.82-6.54 3.68-2.56 0.869-5.59 1.3-9.09 1.3-11.3 0-17-3.29-17-9.88 0-1.68 0.51-3.2 1.52-4.58 1.02-1.38 2.65-2.83 4.9-4.36l3.24 2.6c-0.57 0.689-1.05 1.37-1.44 2.04s-0.67 1.32-0.83 1.95c-0.17 0.629-0.25 1.38-0.25 2.25 0 2.25 0.81 3.89 2.43 4.92 1.61 1.03 4.05 1.55 7.32 1.55 3.68 0 6.64-0.831 8.87-2.49s4.14-4.34 5.73-8.02c1.58-3.68 3.13-9.25 4.62-16.7l-0.62-0.134c-2.91 3.98-5.6 6.89-8.09 8.72-2.49 1.83-5.17 2.74-8.04 2.74-3.27 0-5.8-1.23-7.62-3.68-1.81-2.46-2.71-5.96-2.71-10.5 0-5.12 1-10 3.01-14.8 2-4.72 4.79-8.47 8.35-11.3 3.57-2.78 7.53-4.18 11.9-4.18zm168-20.5h2.65l-11.2 49.7c-0.66 2.94-0.99 5.15-0.99 6.65 0 1.2 0.2 2.07 0.59 2.6s1 0.809 1.84 0.809c0.9 0 1.86-0.367 2.87-1.1 1.02-0.734 2.46-2.13 4.32-4.2l2.56 2.52c-2.76 2.9-5.08 4.96-6.97 6.15-1.88 1.2-3.92 1.8-6.1 1.8-1.86 0-3.34-0.614-4.45-1.84s-1.66-2.86-1.66-4.9c0-1.65 0.34-3.4 1.03-5.26l-0.59-0.18c-2.87 4.19-5.6 7.26-8.17 9.21-2.58 1.95-5.3 2.92-8.18 2.92-3.29 0-5.84-1.24-7.66-3.73-1.81-2.48-2.71-5.97-2.71-10.5 0-5.15 1.01-10.1 3.03-14.8s4.8-8.49 8.33-11.2c3.54-2.74 7.47-4.11 11.8-4.11 1.82 0 3.51 0.187 5.05 0.562 1.54 0.374 3.12 1.02 4.74 1.95l2.33-10.4c0.24-0.989 0.45-2.08 0.63-3.28s0.27-2.25 0.27-3.14c0-0.899-0.16-1.58-0.49-2.04s-0.8-0.801-1.42-1.01c-0.61-0.209-1.71-0.359-3.3-0.449l0.54-2.34z\" />\n <path d=\"m2126 529 17.9 40.3v3.32l-17.9 40.4-4.81-1.71 14.4-40.3-14.4-40.4z\" />\n <!-- |bad> ket -->\n <path d=\"m2766 1275h6.92v83.6h-6.92z\" />\n <path d=\"m2816 1301c-1.43 0-2.85 0.46-4.24 1.37s-2.94 2.43-4.65 4.54-3.1 4.27-4.18 6.49-1.99 5.05-2.74 8.49l-0.4 1.89c-0.45 2.09-0.68 4.13-0.68 6.11 0 2.36 0.52 4.07 1.55 5.12 1.04 1.05 2.75 1.57 5.15 1.57 1.22 0 2.39-0.2 3.5-0.61 1.11-0.4 2.24-1.07 3.39-2 1.15-0.92 2.25-2.14 3.28-3.66 1.03-1.51 1.99-3.37 2.85-5.59 0.87-2.22 1.55-4.59 2.03-7.12 0.47-2.53 0.71-4.95 0.71-7.25 0-3.12-0.44-5.46-1.32-7.01-0.88-1.56-2.3-2.34-4.25-2.34zm97.4-1.75c-3.06 0-5.8 1.23-8.22 3.71-2.43 2.47-4.34 5.8-5.73 9.99s-2.09 8.37-2.09 12.5c0 3.12 0.43 5.45 1.28 6.99s2.29 2.31 4.29 2.31c0.93 0 1.87-0.2 2.83-0.6 0.96-0.41 1.96-1.06 2.99-1.96s2.05-1.99 3.05-3.28c1.01-1.29 1.93-2.58 2.77-3.88 0.83-1.31 1.61-2.85 2.33-4.63s1.33-3.93 1.84-6.45l0.41-1.93c0.45-2.15 0.67-4.19 0.67-6.11 0-2.33-0.49-4.03-1.48-5.1-0.99-1.06-2.64-1.59-4.94-1.59zm-50.9 0c-3.09 0-5.86 1.24-8.31 3.73-2.46 2.48-4.37 5.82-5.75 9.99-1.38 4.18-2.07 8.35-2.07 12.5 0 3.12 0.43 5.45 1.28 6.99s2.29 2.31 4.29 2.31c1.98 0 3.92-0.85 5.82-2.56 1.9-1.7 3.86-4.14 5.88-7.3 2.03-3.16 3.46-6.8 4.29-10.9l0.41-1.93c0.27-1.23 0.45-2.29 0.54-3.19s0.13-1.87 0.13-2.92c0-2.3-0.49-4-1.48-5.07-0.99-1.08-2.67-1.62-5.03-1.62zm-0.9-3.55c1.95 0 3.77 0.22 5.46 0.65 1.69 0.44 3.43 1.21 5.23 2.32l4.13-2.97 2.88 0.72-6.6 28.5c-0.66 2.84-0.99 5.06-0.99 6.65 0 1.2 0.19 2.06 0.58 2.6s1 0.81 1.84 0.81c0.9 0 1.86-0.36 2.88-1.1 1.02-0.73 2.45-2.13 4.31-4.2l2.56 2.52c-2.69 2.87-4.99 4.92-6.89 6.13-1.91 1.21-3.97 1.82-6.18 1.82-1.86 0-3.34-0.62-4.45-1.84-1.11-1.23-1.66-2.86-1.66-4.9 0-1.65 0.34-3.4 1.03-5.26l-0.58-0.18c-2.88 4.2-5.6 7.27-8.18 9.21-2.57 1.95-5.3 2.92-8.17 2.92-3.3 0-5.85-1.24-7.66-3.73-1.81-2.48-2.72-5.97-2.72-10.5 0-5.15 1.01-10.1 3.03-14.8s4.8-8.49 8.34-11.2c3.53-2.74 7.47-4.11 11.8-4.11zm70.6-20.5h2.65l-11.2 49.7c-0.66 2.93-0.99 5.15-0.99 6.65 0 1.2 0.19 2.06 0.58 2.6s1 0.81 1.84 0.81c0.9 0 1.86-0.36 2.88-1.1 1.02-0.73 2.45-2.13 4.31-4.2l2.56 2.52c-2.75 2.9-5.08 4.95-6.96 6.15-1.89 1.2-3.93 1.8-6.11 1.8-1.86 0-3.34-0.62-4.45-1.84-1.11-1.23-1.66-2.86-1.66-4.9 0-1.65 0.34-3.4 1.03-5.26l-0.58-0.18c-2.88 4.2-5.6 7.27-8.18 9.21-2.57 1.95-5.3 2.92-8.17 2.92-3.3 0-5.85-1.24-7.66-3.73-1.81-2.48-2.72-5.97-2.72-10.5 0-5.15 1.01-10.1 3.03-14.8s4.8-8.49 8.34-11.2c3.53-2.74 7.47-4.11 11.8-4.11 1.83 0 3.51 0.19 5.05 0.56 1.55 0.38 3.13 1.03 4.74 1.96l2.34-10.4c0.24-0.99 0.45-2.08 0.63-3.28s0.27-2.25 0.27-3.14c0-0.9-0.17-1.58-0.5-2.05-0.33-0.46-0.8-0.8-1.41-1.01-0.62-0.21-1.72-0.36-3.3-0.45l0.54-2.33zm-124 0h2.61l-7.91 31.4 0.59 0.18c2.9-3.9 5.63-6.71 8.17-8.45 2.55-1.74 5.09-2.6 7.64-2.6 3.26 0 5.8 1.24 7.61 3.72 1.81 2.49 2.72 5.99 2.72 10.5 0 4.94-1 9.8-2.99 14.6-1.99 4.78-4.75 8.57-8.29 11.4-3.53 2.8-7.48 4.2-11.8 4.2-3.99 0-7.52-0.98-10.6-2.92l-4.13 2.92-2.87-0.72 11.6-51.4c0.69-3.05 1.03-5.27 1.03-6.64 0-1.26-0.38-2.15-1.14-2.66-0.77-0.5-2.14-0.79-4.11-0.85l0.54-2.33z\" />\n <path d=\"m2950 1275 17.9 40.3v3.33l-17.9 40.4-4.8-1.71 14.4-40.3-14.4-40.4z\" />\n <!-- theta-->\n <path d=\"m2414 1260c-1.4 6.49-2.11 12-2.11 16.4 0 3.36 0.51 5.82 1.53 7.39s2.67 2.36 4.94 2.36c3.36 0 6.54-2.25 9.55-6.76s5.56-11 7.66-19.4zm18.4-30.3c-3.5 0-6.74 2.22-9.72 6.65s-5.52 10.8-7.62 19h21.5c1.41-6.31 2.11-11.8 2.11-16.3 0-3.15-0.51-5.5-1.53-7.06-1.01-1.55-2.6-2.33-4.76-2.33zm0.54-3.64c8.69 0 13 5.51 13 16.5 0 3.12-0.35 6.84-1.06 11.2-0.7 4.33-1.82 8.74-3.34 13.2-1.53 4.5-3.46 8.47-5.8 11.9-2.33 3.46-5.03 6.13-8.08 8.02-3.06 1.89-6.35 2.83-9.89 2.83-4.46 0-7.78-1.36-9.97-4.09-2.19-2.72-3.28-6.81-3.28-12.3 0-2.97 0.33-6.62 0.99-11 0.66-4.35 1.74-8.79 3.26-13.3 1.51-4.54 3.45-8.57 5.84-12.1 2.38-3.52 5.12-6.23 8.22-8.13s6.46-2.85 10.1-2.85z\" fill-rule=\"evenodd\" />\n <!--2 \\theta and angle -->\n <path d=\"m2410 1105c-1.41 6.5-2.12 12-2.12 16.4 0 3.35 0.51 5.82 1.53 7.39s2.67 2.36 4.94 2.36c3.36 0 6.54-2.25 9.55-6.76s5.56-11 7.66-19.4zm18.4-30.3c-3.5 0-6.74 2.22-9.72 6.65s-5.52 10.8-7.62 19h21.5c1.41-6.32 2.11-11.8 2.11-16.3 0-3.14-0.51-5.5-1.52-7.05-1.02-1.56-2.61-2.34-4.77-2.34zm-58.2-2.7c5.57 0 9.77 1.22 12.6 3.66 2.84 2.44 4.27 5.98 4.27 10.6 0 1.59-0.16 3.05-0.48 4.38-0.31 1.33-0.8 2.65-1.48 3.95-0.67 1.3-1.58 2.7-2.72 4.2-1.13 1.5-2.39 3.02-3.77 4.56s-3.76 4.11-7.14 7.7c-5.69 6.02-9.87 11.3-12.5 15.8h16.9c1.47 0 2.65-0.09 3.55-0.27s1.62-0.479 2.16-0.898c0.54-0.42 0.96-0.966 1.28-1.64 0.31-0.674 0.71-1.72 1.19-3.12h3.82l-0.77 13.1h-37v-2.16c1.29-3.14 3.03-6.36 5.21-9.66 2.19-3.3 5.29-7.17 9.3-11.6 3.47-3.83 5.98-6.74 7.5-8.72 1.68-2.16 2.93-4.02 3.75-5.59 0.83-1.57 1.42-3.06 1.78-4.45s0.54-2.79 0.54-4.2c0-2.25-0.36-4.23-1.08-5.95s-1.82-3.08-3.3-4.07c-1.49-0.988-3.35-1.48-5.6-1.48-5.6 0-9.37 3.11-11.3 9.34h-5.43v-8.62c3.62-1.71 7.01-2.94 10.2-3.71 3.16-0.763 6-1.14 8.52-1.14zm58.7-0.943c8.69 0 13 5.51 13 16.5 0 3.11-0.35 6.84-1.06 11.2-0.7 4.33-1.82 8.74-3.34 13.2-1.53 4.49-3.46 8.47-5.8 11.9-2.33 3.46-5.03 6.13-8.08 8.02-3.06 1.89-6.35 2.83-9.89 2.83-4.46 0-7.78-1.36-9.97-4.09s-3.28-6.81-3.28-12.3c0-2.96 0.33-6.62 0.99-11 0.66-4.34 1.74-8.78 3.26-13.3 1.51-4.54 3.46-8.56 5.84-12.1s5.12-6.23 8.22-8.13 6.46-2.85 10.1-2.85z\" />\n </g>\n <g class=\"kata_svg_fill_accent kata_svg_stroke_accent\" font-family=\"sans-serif\" font-size=\"64px\" font-weight=\"400\">\n <text transform=\"translate(2220 580)\">Close enough</text>\n <text transform=\"translate(2220 650)\">to measure</text>\n <!--2 \\theta and angle -->\n <path d=\"m2210 905c-1.41 6.5-2.12 12-2.12 16.4 0 3.35 0.51 5.82 1.53 7.39s2.67 2.36 4.94 2.36c3.36 0 6.54-2.25 9.55-6.76s5.56-11 7.66-19.4zm18.4-30.3c-3.5 0-6.74 2.22-9.72 6.65s-5.52 10.8-7.62 19h21.5c1.41-6.32 2.11-11.8 2.11-16.3 0-3.14-0.51-5.5-1.52-7.05-1.02-1.56-2.61-2.34-4.77-2.34zm-58.2-2.7c5.57 0 9.77 1.22 12.6 3.66 2.84 2.44 4.27 5.98 4.27 10.6 0 1.59-0.16 3.05-0.48 4.38-0.31 1.33-0.8 2.65-1.48 3.95-0.67 1.3-1.58 2.7-2.72 4.2-1.13 1.5-2.39 3.02-3.77 4.56s-3.76 4.11-7.14 7.7c-5.69 6.02-9.87 11.3-12.5 15.8h16.9c1.47 0 2.65-0.09 3.55-0.27s1.62-0.479 2.16-0.898c0.54-0.42 0.96-0.966 1.28-1.64 0.31-0.674 0.71-1.72 1.19-3.12h3.82l-0.77 13.1h-37v-2.16c1.29-3.14 3.03-6.36 5.21-9.66 2.19-3.3 5.29-7.17 9.3-11.6 3.47-3.83 5.98-6.74 7.5-8.72 1.68-2.16 2.93-4.02 3.75-5.59 0.83-1.57 1.42-3.06 1.78-4.45s0.54-2.79 0.54-4.2c0-2.25-0.36-4.23-1.08-5.95s-1.82-3.08-3.3-4.07c-1.49-0.988-3.35-1.48-5.6-1.48-5.6 0-9.37 3.11-11.3 9.34h-5.43v-8.62c3.62-1.71 7.01-2.94 10.2-3.71 3.16-0.763 6-1.14 8.52-1.14zm58.7-0.943c8.69 0 13 5.51 13 16.5 0 3.11-0.35 6.84-1.06 11.2-0.7 4.33-1.82 8.74-3.34 13.2-1.53 4.49-3.46 8.47-5.8 11.9-2.33 3.46-5.03 6.13-8.08 8.02-3.06 1.89-6.35 2.83-9.89 2.83-4.46 0-7.78-1.36-9.97-4.09s-3.28-6.81-3.28-12.3c0-2.96 0.33-6.62 0.99-11 0.66-4.34 1.74-8.78 3.26-13.3 1.51-4.54 3.46-8.56 5.84-12.1s5.12-6.23 8.22-8.13 6.46-2.85 10.1-2.85z\" fill=\"#0070c0\" fill-rule=\"evenodd\" />\n </g>\n </g>\n</svg>\n<p>Now, recall that $\\theta = \\arcsin \\sqrt{\\frac{M}{N}}$. When $M$ is much smaller than $N$, $\\frac{M}{N}$ is close to 0, and $\\theta$ is a small angle that can approximated as $\\theta \\approx \\sqrt{\\frac{M}{N}}$. This gives the following equation for $R_{opt}$</p>\n<p>$$ 2R_{opt}+1 \\approx \\frac{\\pi}{2\\theta} = \\frac{\\pi}{2}\\sqrt{\\frac{N}{M}}$$\nSince $\\theta$ is small, $R_{opt}$ is large, and the $+1$ term next to $2R_{opt}$ can be ignored, giving the final formula:\n$$ R_{opt} \\approx \\frac{\\pi}{4}\\sqrt{\\frac{N}{M}}$$</p>\n<p>What happens if you use more iterations than is optimal? Each iteration will keep rotating the state $2\\theta$ counterclockwise,\ngetting it further away from the vertical axis and thus reducing the probability of measuring the correct answer.</p>\n<p>In this demo you will see how success probability of Grover's algorithm changes when it uses different numbers of iterations\nbefore the final measurements.</p>\n"
7079
7079
  },
7080
7080
  {
7081
7081
  "type": "example",
7082
7082
  "id": "grovers_search__e2edemo",
7083
- "code": "namespace Kata {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Math;\n\n @EntryPoint()\n operation GroversSearchAlgorithmDemo() : Unit {\n // Experiment with the parameters to explore algorithm behavior in different conditions!\n let n = 3;\n let prefix = [false, true, false];\n let markingOracle = Oracle_StartsWith(_, _, prefix);\n for iterations in 0 .. 9 {\n mutable success = 0;\n for _ in 1 .. 100 {\n let res = GroversSearch(n, markingOracle, iterations);\n if BoolArrayAsInt(prefix) == BoolArrayAsInt(res) {\n set success += 1;\n }\n }\n Message($\"{iterations} iterations - {success}% success rate\");\n }\n }\n\n operation GroversSearch(\n n : Int,\n markingOracle : (Qubit[], Qubit) => Unit is Adj + Ctl, \n iterations : Int\n ) : Bool[] {\n use qs = Qubit[n];\n\n // Operation that prepares the state |all⟩.\n let meanStatePrep = ApplyToEachCA(H, _);\n\n // The phase oracle.\n let phaseOracle = ApplyMarkingOracleAsPhaseOracle(markingOracle, _);\n\n // Prepare the system in the state |all⟩.\n meanStatePrep(qs);\n\n // Do Grover's iterations.\n for _ in 1 .. iterations {\n // Apply the phase oracle.\n phaseOracle(qs);\n\n // Apply \"reflection about the mean\".\n ReflectionAboutState(qs, meanStatePrep);\n }\n\n // Measure to get the result.\n return ResultArrayAsBoolArray(MResetEachZ(qs));\n }\n\n operation Oracle_StartsWith(x : Qubit[], y : Qubit, p : Bool[]) : Unit is Adj + Ctl {\n ApplyControlledOnBitString(p, X, x[... Length(p) - 1], y);\n }\n\n operation ApplyMarkingOracleAsPhaseOracle(\n markingOracle : (Qubit[], Qubit) => Unit is Adj + Ctl,\n qubits : Qubit[])\n : Unit is Adj + Ctl {\n use minus = Qubit();\n within {\n X(minus);\n H(minus);\n } apply {\n markingOracle(qubits, minus);\n }\n }\n\n operation ReflectionAboutState(\n qs : Qubit[],\n statePrep : Qubit[] => Unit is Adj + Ctl)\n : Unit is Adj + Ctl {\n within {\n Adjoint statePrep(qs);\n } apply {\n ConditionalPhaseFlip(qs);\n }\n }\n\n operation ConditionalPhaseFlip(qs : Qubit[]) : Unit is Adj + Ctl {\n within {\n ApplyToEachA(X, qs);\n } apply {\n Controlled Z(qs[1 ...], qs[0]);\n }\n R(PauliI, 2.0 * PI(), qs[0]);\n }\n}"
7083
+ "code": "namespace Kata {\n import Std.Convert.*;\n import Std.Diagnostics.*;\n import Std.Math.*;\n\n @EntryPoint()\n operation GroversSearchAlgorithmDemo() : Unit {\n // Experiment with the parameters to explore algorithm behavior in different conditions!\n let n = 3;\n let prefix = [false, true, false];\n let markingOracle = Oracle_StartsWith(_, _, prefix);\n for iterations in 0..9 {\n mutable success = 0;\n for _ in 1..100 {\n let res = GroversSearch(n, markingOracle, iterations);\n if BoolArrayAsInt(prefix) == BoolArrayAsInt(res) {\n set success += 1;\n }\n }\n Message($\"{iterations} iterations - {success}% success rate\");\n }\n }\n\n operation GroversSearch(\n n : Int,\n markingOracle : (Qubit[], Qubit) => Unit is Adj + Ctl,\n iterations : Int\n ) : Bool[] {\n use qs = Qubit[n];\n\n // Operation that prepares the state |all⟩.\n let meanStatePrep = ApplyToEachCA(H, _);\n\n // The phase oracle.\n let phaseOracle = ApplyMarkingOracleAsPhaseOracle(markingOracle, _);\n\n // Prepare the system in the state |all⟩.\n meanStatePrep(qs);\n\n // Do Grover's iterations.\n for _ in 1..iterations {\n // Apply the phase oracle.\n phaseOracle(qs);\n\n // Apply \"reflection about the mean\".\n ReflectionAboutState(qs, meanStatePrep);\n }\n\n // Measure to get the result.\n return ResultArrayAsBoolArray(MResetEachZ(qs));\n }\n\n operation Oracle_StartsWith(x : Qubit[], y : Qubit, p : Bool[]) : Unit is Adj + Ctl {\n ApplyControlledOnBitString(p, X, x[...Length(p) - 1], y);\n }\n\n operation ApplyMarkingOracleAsPhaseOracle(\n markingOracle : (Qubit[], Qubit) => Unit is Adj + Ctl,\n qubits : Qubit[]\n ) : Unit is Adj + Ctl {\n use minus = Qubit();\n within {\n X(minus);\n H(minus);\n } apply {\n markingOracle(qubits, minus);\n }\n }\n\n operation ReflectionAboutState(\n qs : Qubit[],\n statePrep : Qubit[] => Unit is Adj + Ctl\n ) : Unit is Adj + Ctl {\n within {\n Adjoint statePrep(qs);\n } apply {\n ConditionalPhaseFlip(qs);\n }\n }\n\n operation ConditionalPhaseFlip(qs : Qubit[]) : Unit is Adj + Ctl {\n within {\n ApplyToEachA(X, qs);\n } apply {\n Controlled Z(qs[1...], qs[0]);\n }\n R(PauliI, 2.0 * PI(), qs[0]);\n }\n}\n"
7084
7084
  },
7085
7085
  {
7086
7086
  "type": "text-content",
7087
- "content": "<h3>Verifying that algorithm output is correct</h3>\n<p>Notice that even when using the optimal number of iterations, you are not guaranteed a $100\\%$ success probability.\nGrover's search is a probabilistic algorithm, which means that even in the best case it has a non-zero failure probability.\nWhen you use it to solve a problem, you need to check that the output is correct before using it for any purpose.</p>\n<p>This can be done classically, if you have access to the classical description of the problem\n(in the example used in this kata, you would check that the prefix of the returned state matches the given one.)</p>\n<p>In general, the algorithm only gets the marking oracle as an input and doesn't have the information about the classical problem structure.\nHowever, all information necessary to verify the output is already contained in the oracle itself!<br />\nThe effect of the marking oracle on an input, encoded as a basis states of the qubit register, is defined as\n$$U_f \\ket{x} \\ket{y} = \\ket{x} \\ket{y \\oplus f(x)}$$</p>\n<p>This means that if you encode the return of the algorithm $x_0$ as a basis state of the qubit register,\nallocate an extra qubit in the $\\ket{0}$ state, and apply the oracle $U_f$ to these qubits, you'll get\n$$U_f \\ket{x} \\ket{0} = \\ket{x} \\ket{f(x)}$$</p>\n<p>If you measure the last qubit now, you'll get exactly $f(x)$: if it is 1, the algorithm produced a correct answer, otherwise it didn't. If the algorithm failed, you can re-run it from scratch and hopefully get a correct answer on the next attempt!</p>\n<h3>Special cases</h3>\n<p>This calculation for the optimal number of iterations is done under the assumption that $M$ is much smaller than $N$ but greater than $0$. In other words, the algorithm works if solutions to the search problem exist, but there are very few of them.</p>\n<p>What happens if these assumptions are not valid?</p>\n<h4>No solutions ($M = 0$)</h4>\n<p>In this case the starting system state $\\ket{\\psi} = \\ket{\\text{bad}}$, and $\\theta = \\arcsin \\sqrt{\\frac{M}{N}} = 0$.\nNo matter how many iterations we do, the probability of our measurement yielding a marked state is $0$.</p>\n<p>In practice this means that Grover's search will yield a random non-solution every time.\nTo detect that this is the case, we need to run the algorithm multiple times and note the results. If none of them are problem solutions, we can conclude that the problem doesn't have a solution.</p>\n<h4>Solutions make up half of the search space</h4>\n<p>If $M = \\frac{N}{2}$, then $\\theta = \\arcsin \\sqrt\\frac{N/2}{N} = \\arcsin \\sqrt\\frac{1}{2} = \\frac{\\pi}{4}$.<br />\nThis means that after an arbitrary number of iterations $R$ the amplitude of the basis state $\\ket{\\text{good}}$ in the system will be:</p>\n<p>$$\\sin{(2R+1)\\theta} = \\sin\\frac{(2R+1)\\pi}{4} = \\pm \\frac{1}{\\sqrt{2}}$$</p>\n<p>The probability of the measurement yielding a solution is then $P(\\ket{\\text{good}}) = \\sin^2\\frac{(2R+1)\\theta}{2} = (\\pm \\frac{1}{\\sqrt{2}})^2 = \\frac{1}{2}$</p>\n<p>You can see that the probability of measuring a state that is a solution remains constant regardless of the number of iterations.</p>\n<h4>Solutions make up more than half of the search space</h4>\n<p>If $\\frac{N}{2} &lt; M \\leq N$, then $\\frac{\\pi}{4} &lt; \\theta \\leq \\frac{\\pi}{4}$.\nNow using even one iteration doesn't always increase $P(\\ket{\\text{good}}) = \\sin^2{(2R+1)\\theta}$.\nIn fact, the first iteration is likely to decrease the probability of success!</p>\n<blockquote>\n<p>Have you ever wondered why all tutorials on Grover's search start with two-bit functions?\nThat's the reason why: if you have only one bit, you only have functions for which $M=0$, $M=\\frac{N}{2}$, or $M=N$, and none of these make for a good illustration of the algorithm!</p>\n</blockquote>\n<p>The last two scenarios are a lot easier to handle classically.\nIndeed, a randomly selected classical value has a probability of being a problem solution $p = \\frac{M}{N} &gt; \\frac{1}{2}$!\nIf we repeat the random selection of the variable $k$ times, the probability of success grows to $1-(1-p)^k$, thus by increasing $k$ we can get this probabilty as close to $1$ as we need.\nFor example, For $p=0.5$ and $k=10$ the probality of success is about $99.9\\%$.</p>\n<h4>Unknown number of solutions</h4>\n<p>In practical applications you don't usually know how many solutions your problem has before you start solving it.\nIn this case, you can pick the number of iterations as a random number between 1 and $\\frac{\\pi}{4} \\sqrt{N}$,\nand if the search did not yield the result on the first run, re-run it with a different number of iterations.\nSince Grover's algorithm is probabilistic, you don't nee to get the exact number of iterations to get a correct answer!</p>\n"
7087
+ "content": "<h3>Verifying that algorithm output is correct</h3>\n<p>Notice that even when using the optimal number of iterations, you are not guaranteed a $100\\%$ success probability.\nGrover's search is a probabilistic algorithm, which means that even in the best case it has a non-zero failure probability.\nWhen you use it to solve a problem, you need to check that the output is correct before using it for any purpose.</p>\n<p>This can be done classically, if you have access to the classical description of the problem\n(in the example used in this kata, you would check that the prefix of the returned state matches the given one.)</p>\n<p>In general, the algorithm only gets the marking oracle as an input and doesn't have the information about the classical problem structure.\nHowever, all information necessary to verify the output is already contained in the oracle itself!<br />\nThe effect of the marking oracle on an input, encoded as a basis states of the qubit register, is defined as\n$$U_f \\ket{x} \\ket{y} = \\ket{x} \\ket{y \\oplus f(x)}$$</p>\n<p>This means that if you encode the return of the algorithm $x_0$ as a basis state of the qubit register,\nallocate an extra qubit in the $\\ket{0}$ state, and apply the oracle $U_f$ to these qubits, you'll get\n$$U_f \\ket{x} \\ket{0} = \\ket{x} \\ket{f(x)}$$</p>\n<p>If you measure the last qubit now, you'll get exactly $f(x)$: if it is 1, the algorithm produced a correct answer, otherwise it didn't. If the algorithm failed, you can re-run it from scratch and hopefully get a correct answer on the next attempt!</p>\n<h3>Special cases</h3>\n<p>This calculation for the optimal number of iterations is done under the assumption that $M$ is much smaller than $N$ but greater than $0$. In other words, the algorithm works if solutions to the search problem exist, but there are very few of them.</p>\n<p>What happens if these assumptions are not valid?</p>\n<h4>No solutions ($M = 0$)</h4>\n<p>In this case the starting system state $\\ket{\\psi} = \\ket{\\text{bad}}$, and $\\theta = \\arcsin \\sqrt{\\frac{M}{N}} = 0$.\nNo matter how many iterations we do, the probability of our measurement yielding a marked state is $0$.</p>\n<p>In practice this means that Grover's search will yield a random non-solution every time.\nTo detect that this is the case, we need to run the algorithm multiple times and note the results. If none of them are problem solutions, we can conclude that the problem doesn't have a solution.</p>\n<h4>Solutions make up half of the search space</h4>\n<p>If $M = \\frac{N}{2}$, then $\\theta = \\arcsin \\sqrt\\frac{N/2}{N} = \\arcsin \\sqrt\\frac{1}{2} = \\frac{\\pi}{4}$.<br />\nThis means that after an arbitrary number of iterations $R$ the amplitude of the basis state $\\ket{\\text{good}}$ in the system will be:</p>\n<p>$$\\sin{(2R+1)\\theta} = \\sin\\frac{(2R+1)\\pi}{4} = \\pm \\frac{1}{\\sqrt{2}}$$</p>\n<p>The probability of the measurement yielding a solution is then $P(\\ket{\\text{good}}) = \\sin^2\\frac{(2R+1)\\theta}{2} = (\\pm \\frac{1}{\\sqrt{2}})^2 = \\frac{1}{2}$</p>\n<p>You can see that the probability of measuring a state that is a solution remains constant regardless of the number of iterations.</p>\n<h4>Solutions make up more than half of the search space</h4>\n<p>If $\\frac{N}{2} &lt; M \\leq N$, then $\\frac{\\pi}{4} &lt; \\theta \\leq \\frac{\\pi}{4}$.\nNow using even one iteration doesn't always increase $P(\\ket{\\text{good}}) = \\sin^2{(2R+1)\\theta}$.\nIn fact, the first iteration is likely to decrease the probability of success!</p>\n<blockquote>\n<p>Have you ever wondered why all tutorials on Grover's search start with two-bit functions?\nThat's the reason why: if you have only one bit, you only have functions for which $M=0$, $M=\\frac{N}{2}$, or $M=N$, and none of these make for a good illustration of the algorithm!</p>\n</blockquote>\n<p>The last two scenarios are a lot easier to handle classically.\nIndeed, a randomly selected classical value has a probability of being a problem solution $p = \\frac{M}{N} &gt; \\frac{1}{2}$!\nIf we repeat the random selection of the variable $k$ times, the probability of success grows to $1-(1-p)^k$, thus by increasing $k$ we can get this probability as close to $1$ as we need.\nFor example, For $p=0.5$ and $k=10$ the probability of success is about $99.9\\%$.</p>\n<h4>Unknown number of solutions</h4>\n<p>In practical applications you don't usually know how many solutions your problem has before you start solving it.\nIn this case, you can pick the number of iterations as a random number between 1 and $\\frac{\\pi}{4} \\sqrt{N}$,\nand if the search did not yield the result on the first run, re-run it with a different number of iterations.\nSince Grover's algorithm is probabilistic, you don't nee to get the exact number of iterations to get a correct answer!</p>\n"
7088
7088
  }
7089
7089
  ]
7090
7090
  },
@@ -7221,7 +7221,7 @@ export default {
7221
7221
  {
7222
7222
  "type": "solution",
7223
7223
  "id": "solving_sat__sat_clause_solution",
7224
- "code": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n operation Oracle_SATClause(x : Qubit[], y : Qubit, clause : (Int, Bool)[]) : Unit is Adj + Ctl {\n let clauseQubits = Mapped((ind, _) -> x[ind], clause);\n within {\n for (ind, positive) in clause {\n if not positive {\n X(x[ind]);\n }\n }\n } apply {\n Oracle_Or(clauseQubits, y);\n }\n }\n\n operation Oracle_Or(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n ApplyControlledOnInt(0, X, x, y);\n X(y);\n } \n}\n"
7224
+ "code": "namespace Kata {\n import Std.Arrays.*;\n operation Oracle_SATClause(x : Qubit[], y : Qubit, clause : (Int, Bool)[]) : Unit is Adj + Ctl {\n let clauseQubits = Mapped((ind, _) -> x[ind], clause);\n within {\n for (ind, positive) in clause {\n if not positive {\n X(x[ind]);\n }\n }\n } apply {\n Oracle_Or(clauseQubits, y);\n }\n }\n\n operation Oracle_Or(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n ApplyControlledOnInt(0, X, x, y);\n X(y);\n }\n}\n"
7225
7225
  }
7226
7226
  ]
7227
7227
  }
@@ -7239,7 +7239,7 @@ export default {
7239
7239
  "KatasLibrary.qs",
7240
7240
  "solving_sat__Common.qs"
7241
7241
  ],
7242
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n\n operation Oracle_SATFormula(x : Qubit[], y : Qubit, formula : (Int, Bool)[][]) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n\n // You might find these helper operations from earlier tasks useful.\n operation Oracle_SATClause(x : Qubit[], y : Qubit, clause : (Int, Bool)[]) : Unit is Adj + Ctl {\n let clauseQubits = Mapped((ind, _) -> x[ind], clause);\n within {\n for (ind, positive) in clause {\n if not positive {\n X(x[ind]);\n }\n }\n } apply {\n Oracle_Or(clauseQubits, y);\n }\n }\n\n operation Oracle_Or(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n ApplyControlledOnInt(0, X, x, y);\n X(y);\n } \n\n operation Oracle_And(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n Controlled X(x, y);\n } \n}\n",
7242
+ "placeholderCode": "namespace Kata {\n import Std.Arrays.*;\n\n operation Oracle_SATFormula(x : Qubit[], y : Qubit, formula : (Int, Bool)[][]) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n\n // You might find these helper operations from earlier tasks useful.\n operation Oracle_SATClause(x : Qubit[], y : Qubit, clause : (Int, Bool)[]) : Unit is Adj + Ctl {\n let clauseQubits = Mapped((ind, _) -> x[ind], clause);\n within {\n for (ind, positive) in clause {\n if not positive {\n X(x[ind]);\n }\n }\n } apply {\n Oracle_Or(clauseQubits, y);\n }\n }\n\n operation Oracle_Or(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n ApplyControlledOnInt(0, X, x, y);\n X(y);\n }\n\n operation Oracle_And(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n Controlled X(x, y);\n }\n}\n",
7243
7243
  "explainedSolution": {
7244
7244
  "type": "explained-solution",
7245
7245
  "items": [
@@ -7250,7 +7250,7 @@ export default {
7250
7250
  {
7251
7251
  "type": "solution",
7252
7252
  "id": "solving_sat__sat_formula_solution",
7253
- "code": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n\n operation Oracle_SATFormula(x : Qubit[], y : Qubit, formula : (Int, Bool)[][]) : Unit is Adj + Ctl {\n use aux = Qubit[Length(formula)];\n within {\n for i in 0 .. Length(formula) - 1 {\n Oracle_SATClause(x, aux[i], formula[i]);\n }\n } apply {\n Oracle_And(aux, y);\n }\n }\n\n operation Oracle_SATClause(x : Qubit[], y : Qubit, clause : (Int, Bool)[]) : Unit is Adj + Ctl {\n let clauseQubits = Mapped((ind, _) -> x[ind], clause);\n within {\n for (ind, positive) in clause {\n if not positive {\n X(x[ind]);\n }\n }\n } apply {\n Oracle_Or(clauseQubits, y);\n }\n }\n\n operation Oracle_Or(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n ApplyControlledOnInt(0, X, x, y);\n X(y);\n } \n\n operation Oracle_And(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n Controlled X(x, y);\n } \n}\n"
7253
+ "code": "namespace Kata {\n import Std.Arrays.*;\n\n operation Oracle_SATFormula(x : Qubit[], y : Qubit, formula : (Int, Bool)[][]) : Unit is Adj + Ctl {\n use aux = Qubit[Length(formula)];\n within {\n for i in 0..Length(formula) - 1 {\n Oracle_SATClause(x, aux[i], formula[i]);\n }\n } apply {\n Oracle_And(aux, y);\n }\n }\n\n operation Oracle_SATClause(x : Qubit[], y : Qubit, clause : (Int, Bool)[]) : Unit is Adj + Ctl {\n let clauseQubits = Mapped((ind, _) -> x[ind], clause);\n within {\n for (ind, positive) in clause {\n if not positive {\n X(x[ind]);\n }\n }\n } apply {\n Oracle_Or(clauseQubits, y);\n }\n }\n\n operation Oracle_Or(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n ApplyControlledOnInt(0, X, x, y);\n X(y);\n }\n\n operation Oracle_And(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n Controlled X(x, y);\n }\n}\n"
7254
7254
  }
7255
7255
  ]
7256
7256
  }
@@ -7308,7 +7308,7 @@ export default {
7308
7308
  "KatasLibrary.qs",
7309
7309
  "solving_sat__Common.qs"
7310
7310
  ],
7311
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n\n operation Oracle_Exactly13SATFormula(x : Qubit[], y : Qubit, formula : (Int, Bool)[][]) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n\n // You might want to implement this helper operation that evaluates a single clause and use it in your solution.\n operation Oracle_Exactly13SATClause(x : Qubit[], y : Qubit, clause : (Int, Bool)[]) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n\n // You might find these helper operations from earlier tasks useful.\n operation Oracle_Exactly1One(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n for i in 0 .. Length(x) - 1 {\n ApplyControlledOnInt(2 ^ i, X, x, y);\n }\n } \n\n operation Oracle_And(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n Controlled X(x, y);\n } \n}\n",
7311
+ "placeholderCode": "namespace Kata {\n import Std.Arrays.*;\n\n operation Oracle_Exactly13SATFormula(x : Qubit[], y : Qubit, formula : (Int, Bool)[][]) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n\n // You might want to implement this helper operation that evaluates a single clause and use it in your solution.\n operation Oracle_Exactly13SATClause(x : Qubit[], y : Qubit, clause : (Int, Bool)[]) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n\n // You might find these helper operations from earlier tasks useful.\n operation Oracle_Exactly1One(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n for i in 0..Length(x) - 1 {\n ApplyControlledOnInt(2^i, X, x, y);\n }\n }\n\n operation Oracle_And(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n Controlled X(x, y);\n }\n}\n",
7312
7312
  "explainedSolution": {
7313
7313
  "type": "explained-solution",
7314
7314
  "items": [
@@ -7319,7 +7319,7 @@ export default {
7319
7319
  {
7320
7320
  "type": "solution",
7321
7321
  "id": "solving_sat__exactly_one_one_formula_solution",
7322
- "code": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n\n operation Oracle_Exactly13SATFormula(x : Qubit[], y : Qubit, formula : (Int, Bool)[][]) : Unit is Adj + Ctl {\n use aux = Qubit[Length(formula)];\n within {\n for i in 0 .. Length(formula) - 1 {\n Oracle_Exactly13SATClause(x, aux[i], formula[i]);\n }\n } apply {\n Oracle_And(aux, y);\n }\n }\n\n // You might want to implement this helper operation that evaluates a single clause and use it in your solution.\n operation Oracle_Exactly13SATClause(x : Qubit[], y : Qubit, clause : (Int, Bool)[]) : Unit is Adj + Ctl {\n let clauseQubits = Mapped((ind, _) -> x[ind], clause);\n within {\n for (ind, positive) in clause {\n if not positive {\n X(x[ind]);\n }\n }\n } apply {\n Oracle_Exactly1One(clauseQubits, y);\n }\n }\n\n // You might find these helper operations from earlier tasks useful.\n operation Oracle_Exactly1One(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n for i in 0 .. Length(x) - 1 {\n ApplyControlledOnInt(2 ^ i, X, x, y);\n }\n } \n\n operation Oracle_And(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n Controlled X(x, y);\n } \n}\n"
7322
+ "code": "namespace Kata {\n import Std.Arrays.*;\n\n operation Oracle_Exactly13SATFormula(x : Qubit[], y : Qubit, formula : (Int, Bool)[][]) : Unit is Adj + Ctl {\n use aux = Qubit[Length(formula)];\n within {\n for i in 0..Length(formula) - 1 {\n Oracle_Exactly13SATClause(x, aux[i], formula[i]);\n }\n } apply {\n Oracle_And(aux, y);\n }\n }\n\n // You might want to implement this helper operation that evaluates a single clause and use it in your solution.\n operation Oracle_Exactly13SATClause(x : Qubit[], y : Qubit, clause : (Int, Bool)[]) : Unit is Adj + Ctl {\n let clauseQubits = Mapped((ind, _) -> x[ind], clause);\n within {\n for (ind, positive) in clause {\n if not positive {\n X(x[ind]);\n }\n }\n } apply {\n Oracle_Exactly1One(clauseQubits, y);\n }\n }\n\n // You might find these helper operations from earlier tasks useful.\n operation Oracle_Exactly1One(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n for i in 0..Length(x) - 1 {\n ApplyControlledOnInt(2^i, X, x, y);\n }\n }\n\n operation Oracle_And(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n Controlled X(x, y);\n }\n}\n"
7323
7323
  }
7324
7324
  ]
7325
7325
  }
@@ -7336,7 +7336,7 @@ export default {
7336
7336
  {
7337
7337
  "type": "example",
7338
7338
  "id": "solving_sat__e2edemo",
7339
- "code": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Math;\n\n @EntryPoint()\n operation SolvingSATWithGroverDemo() : Unit {\n // Experiment with the parameters to explore algorithm behavior in different conditions!\n let n = 3;\n // (x₀ ∨ x₁) ∧ (¬x₀ ∨ ¬x₁) ∧ ¬x₂\n let formula = [[(0, true), (1, true)], [(0, false), (1, false)], [(2, false)]];\n let markingOracle = Oracle_SATFormula(_, _, formula);\n for iterations in 0 .. 9 {\n mutable success = 0;\n for _ in 1 .. 100 {\n let res = GroversSearch(n, markingOracle, iterations);\n if F_SATFormula(res, formula) {\n set success += 1;\n }\n }\n Message($\"{iterations} iterations - {success}% success rate\");\n }\n }\n\n operation GroversSearch(\n n : Int,\n markingOracle : (Qubit[], Qubit) => Unit is Adj + Ctl, \n iterations : Int\n ) : Bool[] {\n use qs = Qubit[n];\n\n // Operation that prepares the state |all⟩.\n let meanStatePrep = ApplyToEachCA(H, _);\n\n // The phase oracle.\n let phaseOracle = ApplyMarkingOracleAsPhaseOracle(markingOracle, _);\n\n // Prepare the system in the state |all⟩.\n meanStatePrep(qs);\n\n // Do Grover's iterations.\n for _ in 1 .. iterations {\n // Apply the phase oracle.\n phaseOracle(qs);\n\n // Apply \"reflection about the mean\".\n ReflectionAboutState(qs, meanStatePrep);\n }\n\n // Measure to get the result.\n return ResultArrayAsBoolArray(MResetEachZ(qs));\n }\n\n operation ApplyMarkingOracleAsPhaseOracle(\n markingOracle : (Qubit[], Qubit) => Unit is Adj + Ctl,\n qubits : Qubit[])\n : Unit is Adj + Ctl {\n use minus = Qubit();\n within {\n X(minus);\n H(minus);\n } apply {\n markingOracle(qubits, minus);\n }\n }\n\n operation ReflectionAboutState(\n qs : Qubit[],\n statePrep : Qubit[] => Unit is Adj + Ctl)\n : Unit is Adj + Ctl {\n within {\n Adjoint statePrep(qs);\n } apply {\n ConditionalPhaseFlip(qs);\n }\n }\n\n operation ConditionalPhaseFlip(qs : Qubit[]) : Unit is Adj + Ctl {\n within {\n ApplyToEachA(X, qs);\n } apply {\n Controlled Z(qs[1 ...], qs[0]);\n }\n R(PauliI, 2.0 * PI(), qs[0]);\n }\n\n operation Oracle_SATFormula(x : Qubit[], y : Qubit, formula : (Int, Bool)[][]) : Unit is Adj + Ctl {\n use aux = Qubit[Length(formula)];\n within {\n for i in 0 .. Length(formula) - 1 {\n Oracle_SATClause(x, aux[i], formula[i]);\n }\n } apply {\n Oracle_And(aux, y);\n }\n }\n\n operation Oracle_SATClause(x : Qubit[], y : Qubit, clause : (Int, Bool)[]) : Unit is Adj + Ctl {\n let clauseQubits = Mapped((ind, _) -> x[ind], clause);\n within {\n for (ind, positive) in clause {\n if not positive {\n X(x[ind]);\n }\n }\n } apply {\n Oracle_Or(clauseQubits, y);\n }\n }\n\n operation Oracle_Or(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n ApplyControlledOnInt(0, X, x, y);\n X(y);\n } \n\n operation Oracle_And(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n Controlled X(x, y);\n }\n\n function F_SATClause(args : Bool[], clause : (Int, Bool)[]) : Bool {\n for (index, positive) in clause {\n if positive == args[index] {\n // one true literal is sufficient for the clause to be true\n return true;\n }\n }\n // none of the literals is true - the whole clause is false\n return false;\n }\n\n function F_SATFormula(args : Bool[], formula : (Int, Bool)[][]) : Bool {\n for clause in formula {\n // one false clause invalidates the whole formula\n if not F_SATClause(args, clause) {\n return false\n }\n }\n return true;\n }\n}"
7339
+ "code": "namespace Kata {\n import Std.Arrays.*;\n import Std.Convert.*;\n import Std.Diagnostics.*;\n import Std.Math.*;\n\n @EntryPoint()\n operation SolvingSATWithGroverDemo() : Unit {\n // Experiment with the parameters to explore algorithm behavior in different conditions!\n let n = 3;\n // (x₀ ∨ x₁) ∧ (¬x₀ ∨ ¬x₁) ∧ ¬x₂\n let formula = [[(0, true), (1, true)], [(0, false), (1, false)], [(2, false)]];\n let markingOracle = Oracle_SATFormula(_, _, formula);\n for iterations in 0..9 {\n mutable success = 0;\n for _ in 1..100 {\n let res = GroversSearch(n, markingOracle, iterations);\n if F_SATFormula(res, formula) {\n set success += 1;\n }\n }\n Message($\"{iterations} iterations - {success}% success rate\");\n }\n }\n\n operation GroversSearch(\n n : Int,\n markingOracle : (Qubit[], Qubit) => Unit is Adj + Ctl,\n iterations : Int\n ) : Bool[] {\n use qs = Qubit[n];\n\n // Operation that prepares the state |all⟩.\n let meanStatePrep = ApplyToEachCA(H, _);\n\n // The phase oracle.\n let phaseOracle = ApplyMarkingOracleAsPhaseOracle(markingOracle, _);\n\n // Prepare the system in the state |all⟩.\n meanStatePrep(qs);\n\n // Do Grover's iterations.\n for _ in 1..iterations {\n // Apply the phase oracle.\n phaseOracle(qs);\n\n // Apply \"reflection about the mean\".\n ReflectionAboutState(qs, meanStatePrep);\n }\n\n // Measure to get the result.\n return ResultArrayAsBoolArray(MResetEachZ(qs));\n }\n\n operation ApplyMarkingOracleAsPhaseOracle(\n markingOracle : (Qubit[], Qubit) => Unit is Adj + Ctl,\n qubits : Qubit[]\n ) : Unit is Adj + Ctl {\n use minus = Qubit();\n within {\n X(minus);\n H(minus);\n } apply {\n markingOracle(qubits, minus);\n }\n }\n\n operation ReflectionAboutState(\n qs : Qubit[],\n statePrep : Qubit[] => Unit is Adj + Ctl\n ) : Unit is Adj + Ctl {\n within {\n Adjoint statePrep(qs);\n } apply {\n ConditionalPhaseFlip(qs);\n }\n }\n\n operation ConditionalPhaseFlip(qs : Qubit[]) : Unit is Adj + Ctl {\n within {\n ApplyToEachA(X, qs);\n } apply {\n Controlled Z(qs[1...], qs[0]);\n }\n R(PauliI, 2.0 * PI(), qs[0]);\n }\n\n operation Oracle_SATFormula(x : Qubit[], y : Qubit, formula : (Int, Bool)[][]) : Unit is Adj + Ctl {\n use aux = Qubit[Length(formula)];\n within {\n for i in 0..Length(formula) - 1 {\n Oracle_SATClause(x, aux[i], formula[i]);\n }\n } apply {\n Oracle_And(aux, y);\n }\n }\n\n operation Oracle_SATClause(x : Qubit[], y : Qubit, clause : (Int, Bool)[]) : Unit is Adj + Ctl {\n let clauseQubits = Mapped((ind, _) -> x[ind], clause);\n within {\n for (ind, positive) in clause {\n if not positive {\n X(x[ind]);\n }\n }\n } apply {\n Oracle_Or(clauseQubits, y);\n }\n }\n\n operation Oracle_Or(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n ApplyControlledOnInt(0, X, x, y);\n X(y);\n }\n\n operation Oracle_And(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n Controlled X(x, y);\n }\n\n function F_SATClause(args : Bool[], clause : (Int, Bool)[]) : Bool {\n for (index, positive) in clause {\n if positive == args[index] {\n // one true literal is sufficient for the clause to be true\n return true;\n }\n }\n // none of the literals is true - the whole clause is false\n return false;\n }\n\n function F_SATFormula(args : Bool[], formula : (Int, Bool)[][]) : Bool {\n for clause in formula {\n // one false clause invalidates the whole formula\n if not F_SATClause(args, clause) {\n return false\n }\n }\n return true;\n }\n}\n"
7340
7340
  }
7341
7341
  ]
7342
7342
  },
@@ -7433,7 +7433,7 @@ export default {
7433
7433
  {
7434
7434
  "type": "solution",
7435
7435
  "id": "solving_graph_coloring__read_coloring_solution",
7436
- "code": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Convert;\n\n operation ReadColoring(nBits : Int, qs : Qubit[]) : Int[] {\n let colorPartitions = Chunks(nBits, qs);\n let measureColor = qs => ResultArrayAsInt(Reversed(MeasureEachZ(qs)));\n return ForEach(measureColor, colorPartitions);\n }\n}\n"
7436
+ "code": "namespace Kata {\n import Std.Arrays.*;\n import Std.Convert.*;\n\n operation ReadColoring(nBits : Int, qs : Qubit[]) : Int[] {\n let colorPartitions = Chunks(nBits, qs);\n let measureColor = qs => ResultArrayAsInt(Reversed(MeasureEachZ(qs)));\n return ForEach(measureColor, colorPartitions);\n }\n}\n"
7437
7437
  }
7438
7438
  ]
7439
7439
  }
@@ -7560,7 +7560,7 @@ export default {
7560
7560
  {
7561
7561
  "type": "solution",
7562
7562
  "id": "solving_graph_coloring__weak_coloring_one_vertex_solution",
7563
- "code": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n\n operation Oracle_WeakColoring_OneVertex(\n V : Int, edges: (Int, Int)[], x : Qubit[], y : Qubit, vertex : Int\n ) : Unit is Adj + Ctl {\n let neighborEdges = Filtered((a, b) -> a == vertex or b == vertex, edges);\n let nNeighbors = Length(neighborEdges);\n use sameColorChecks = Qubit[nNeighbors];\n within {\n for ((a, b), checkQubit) in Zipped(neighborEdges, sameColorChecks) {\n Oracle_ColorEquality(x[a * 2 .. a * 2 + 1],\n x[b * 2 .. b * 2 + 1], \n checkQubit);\n }\n } apply {\n X(y);\n if nNeighbors > 0 {\n Controlled X(sameColorChecks, y);\n }\n }\n }\n\n operation Oracle_ColorEquality(x0 : Qubit[], x1 : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n within {\n for i in 0..Length(x0) - 1 {\n CNOT(x0[i], x1[i]);\n }\n } apply {\n ApplyControlledOnInt(0, X, x1, y);\n }\n }\n}\n"
7563
+ "code": "namespace Kata {\n import Std.Arrays.*;\n\n operation Oracle_WeakColoring_OneVertex(\n V : Int,\n edges : (Int, Int)[],\n x : Qubit[],\n y : Qubit,\n vertex : Int\n ) : Unit is Adj + Ctl {\n let neighborEdges = Filtered((a, b) -> a == vertex or b == vertex, edges);\n let nNeighbors = Length(neighborEdges);\n use sameColorChecks = Qubit[nNeighbors];\n within {\n for ((a, b), checkQubit) in Zipped(neighborEdges, sameColorChecks) {\n Oracle_ColorEquality(\n x[a * 2 .. a * 2 + 1],\n x[b * 2 .. b * 2 + 1],\n checkQubit\n );\n }\n } apply {\n X(y);\n if nNeighbors > 0 {\n Controlled X(sameColorChecks, y);\n }\n }\n }\n\n operation Oracle_ColorEquality(x0 : Qubit[], x1 : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n within {\n for i in 0..Length(x0) - 1 {\n CNOT(x0[i], x1[i]);\n }\n } apply {\n ApplyControlledOnInt(0, X, x1, y);\n }\n }\n}\n"
7564
7564
  }
7565
7565
  ]
7566
7566
  }
@@ -7578,7 +7578,7 @@ export default {
7578
7578
  "KatasLibrary.qs",
7579
7579
  "solving_graph_coloring__Common.qs"
7580
7580
  ],
7581
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n\n operation Oracle_WeakColoring(\n V : Int, edges: (Int, Int)[], x : Qubit[], y : Qubit\n ) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n\n // You might find these helper operations from earlier tasks useful.\n operation Oracle_WeakColoring_OneVertex(\n V : Int, edges: (Int, Int)[], x : Qubit[], y : Qubit, vertex : Int\n ) : Unit is Adj + Ctl {\n let neighborEdges = Filtered((a, b) -> a == vertex or b == vertex, edges);\n let nNeighbors = Length(neighborEdges);\n use sameColorChecks = Qubit[nNeighbors];\n within {\n for ((a, b), checkQubit) in Zipped(neighborEdges, sameColorChecks) {\n Oracle_ColorEquality(x[a * 2 .. a * 2 + 1],\n x[b * 2 .. b * 2 + 1], \n checkQubit);\n }\n } apply {\n X(y);\n if nNeighbors > 0 {\n Controlled X(sameColorChecks, y);\n }\n }\n }\n\n operation Oracle_ColorEquality(x0 : Qubit[], x1 : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n within {\n for i in 0..Length(x0) - 1 {\n CNOT(x0[i], x1[i]);\n }\n } apply {\n ApplyControlledOnInt(0, X, x1, y);\n }\n }\n}\n",
7581
+ "placeholderCode": "namespace Kata {\n import Std.Arrays.*;\n\n operation Oracle_WeakColoring(\n V : Int,\n edges : (Int, Int)[],\n x : Qubit[],\n y : Qubit\n ) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n\n // You might find these helper operations from earlier tasks useful.\n operation Oracle_WeakColoring_OneVertex(\n V : Int,\n edges : (Int, Int)[],\n x : Qubit[],\n y : Qubit,\n vertex : Int\n ) : Unit is Adj + Ctl {\n let neighborEdges = Filtered((a, b) -> a == vertex or b == vertex, edges);\n let nNeighbors = Length(neighborEdges);\n use sameColorChecks = Qubit[nNeighbors];\n within {\n for ((a, b), checkQubit) in Zipped(neighborEdges, sameColorChecks) {\n Oracle_ColorEquality(\n x[a * 2 .. a * 2 + 1],\n x[b * 2 .. b * 2 + 1],\n checkQubit\n );\n }\n } apply {\n X(y);\n if nNeighbors > 0 {\n Controlled X(sameColorChecks, y);\n }\n }\n }\n\n operation Oracle_ColorEquality(x0 : Qubit[], x1 : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n within {\n for i in 0..Length(x0) - 1 {\n CNOT(x0[i], x1[i]);\n }\n } apply {\n ApplyControlledOnInt(0, X, x1, y);\n }\n }\n}\n",
7582
7582
  "explainedSolution": {
7583
7583
  "type": "explained-solution",
7584
7584
  "items": [
@@ -7589,7 +7589,7 @@ export default {
7589
7589
  {
7590
7590
  "type": "solution",
7591
7591
  "id": "solving_graph_coloring__weak_coloring_quantum_solution",
7592
- "code": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n\n operation Oracle_WeakColoring(\n V : Int, edges: (Int, Int)[], x : Qubit[], y : Qubit\n ) : Unit is Adj + Ctl {\n use vertexQubits = Qubit[V];\n within {\n for v in 0 .. V - 1 {\n Oracle_WeakColoring_OneVertex(V, edges, x, vertexQubits[v], v);\n }\n } apply {\n Controlled X(vertexQubits, y);\n }\n }\n\n // You might find these helper operations from earlier tasks useful.\n operation Oracle_WeakColoring_OneVertex(\n V : Int, edges: (Int, Int)[], x : Qubit[], y : Qubit, vertex : Int\n ) : Unit is Adj + Ctl {\n let neighborEdges = Filtered((a, b) -> a == vertex or b == vertex, edges);\n let nNeighbors = Length(neighborEdges);\n use sameColorChecks = Qubit[nNeighbors];\n within {\n for ((a, b), checkQubit) in Zipped(neighborEdges, sameColorChecks) {\n Oracle_ColorEquality(x[a * 2 .. a * 2 + 1],\n x[b * 2 .. b * 2 + 1], \n checkQubit);\n }\n } apply {\n X(y);\n if nNeighbors > 0 {\n Controlled X(sameColorChecks, y);\n }\n }\n }\n\n operation Oracle_ColorEquality(x0 : Qubit[], x1 : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n within {\n for i in 0..Length(x0) - 1 {\n CNOT(x0[i], x1[i]);\n }\n } apply {\n ApplyControlledOnInt(0, X, x1, y);\n }\n }\n}\n"
7592
+ "code": "namespace Kata {\n import Std.Arrays.*;\n\n operation Oracle_WeakColoring(\n V : Int,\n edges : (Int, Int)[],\n x : Qubit[],\n y : Qubit\n ) : Unit is Adj + Ctl {\n use vertexQubits = Qubit[V];\n within {\n for v in 0..V - 1 {\n Oracle_WeakColoring_OneVertex(V, edges, x, vertexQubits[v], v);\n }\n } apply {\n Controlled X(vertexQubits, y);\n }\n }\n\n // You might find these helper operations from earlier tasks useful.\n operation Oracle_WeakColoring_OneVertex(\n V : Int,\n edges : (Int, Int)[],\n x : Qubit[],\n y : Qubit,\n vertex : Int\n ) : Unit is Adj + Ctl {\n let neighborEdges = Filtered((a, b) -> a == vertex or b == vertex, edges);\n let nNeighbors = Length(neighborEdges);\n use sameColorChecks = Qubit[nNeighbors];\n within {\n for ((a, b), checkQubit) in Zipped(neighborEdges, sameColorChecks) {\n Oracle_ColorEquality(\n x[a * 2 .. a * 2 + 1],\n x[b * 2 .. b * 2 + 1],\n checkQubit\n );\n }\n } apply {\n X(y);\n if nNeighbors > 0 {\n Controlled X(sameColorChecks, y);\n }\n }\n }\n\n operation Oracle_ColorEquality(x0 : Qubit[], x1 : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n within {\n for i in 0..Length(x0) - 1 {\n CNOT(x0[i], x1[i]);\n }\n } apply {\n ApplyControlledOnInt(0, X, x1, y);\n }\n }\n}\n"
7593
7593
  }
7594
7594
  ]
7595
7595
  }
@@ -7606,7 +7606,7 @@ export default {
7606
7606
  {
7607
7607
  "type": "example",
7608
7608
  "id": "solving_graph_coloring__e2edemo",
7609
- "code": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Math;\n\n @EntryPoint()\n operation SolvingGraphColoringWithGroverDemo() : Unit {\n // Experiment with the parameters to explore algorithm behavior in different conditions!\n let V = 3;\n // The 0 -- 1 -- 2 graph from the examples\n let edges = [(0, 1), (1, 2)];\n let markingOracle = Oracle_VertexColoring(V, edges, _, _);\n for iterations in 0 .. 9 {\n mutable success = 0;\n for _ in 1 .. 100 {\n let res = GroversSearch(2 * V, markingOracle, iterations);\n // Convert measurement results to integers\n let colorPartitions = Chunks(2, res);\n let colors = Mapped(bits -> BoolArrayAsInt(Reversed(bits)), colorPartitions);\n if IsVertexColoringValid(V, edges, colors) {\n set success += 1;\n }\n }\n Message($\"{iterations} iterations - {success}% success rate\");\n }\n }\n\n operation GroversSearch(\n n : Int,\n markingOracle : (Qubit[], Qubit) => Unit is Adj + Ctl, \n iterations : Int\n ) : Bool[] {\n use qs = Qubit[n];\n\n // Operation that prepares the state |all⟩.\n let meanStatePrep = ApplyToEachCA(H, _);\n\n // The phase oracle.\n let phaseOracle = ApplyMarkingOracleAsPhaseOracle(markingOracle, _);\n\n // Prepare the system in the state |all⟩.\n meanStatePrep(qs);\n\n // Do Grover's iterations.\n for _ in 1 .. iterations {\n // Apply the phase oracle.\n phaseOracle(qs);\n\n // Apply \"reflection about the mean\".\n ReflectionAboutState(qs, meanStatePrep);\n }\n\n // Measure to get the result.\n return ResultArrayAsBoolArray(MResetEachZ(qs));\n }\n\n operation ApplyMarkingOracleAsPhaseOracle(\n markingOracle : (Qubit[], Qubit) => Unit is Adj + Ctl,\n qubits : Qubit[])\n : Unit is Adj + Ctl {\n use minus = Qubit();\n within {\n X(minus);\n H(minus);\n } apply {\n markingOracle(qubits, minus);\n }\n }\n\n operation ReflectionAboutState(\n qs : Qubit[],\n statePrep : Qubit[] => Unit is Adj + Ctl)\n : Unit is Adj + Ctl {\n within {\n Adjoint statePrep(qs);\n } apply {\n ConditionalPhaseFlip(qs);\n }\n }\n\n operation ConditionalPhaseFlip(qs : Qubit[]) : Unit is Adj + Ctl {\n within {\n ApplyToEachA(X, qs);\n } apply {\n Controlled Z(qs[1 ...], qs[0]);\n }\n R(PauliI, 2.0 * PI(), qs[0]);\n }\n\n operation Oracle_VertexColoring(V : Int, edges: (Int, Int)[], x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n let edgesNumber = Length(edges);\n use conflicts = Qubit[edgesNumber];\n within {\n for i in 0 .. edgesNumber - 1 {\n let (v0, v1) = edges[i];\n Oracle_ColorEquality(x[2 * v0 .. 2 * v0 + 1], \n x[2 * v1 .. 2 * v1 + 1], conflicts[i]);\n }\n } apply {\n ApplyControlledOnInt(0, X, conflicts, y);\n }\n }\n\n operation Oracle_ColorEquality(x0 : Qubit[], x1 : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n within {\n for i in 0..Length(x0) - 1 {\n CNOT(x0[i], x1[i]);\n }\n } apply {\n ApplyControlledOnInt(0, X, x1, y);\n }\n }\n\n function IsVertexColoringValid(V : Int, edges: (Int, Int)[], colors: Int[]) : Bool {\n for (v0, v1) in edges {\n if colors[v0] == colors[v1] {\n return false;\n }\n }\n return true;\n }\n}"
7609
+ "code": "namespace Kata {\n import Std.Arrays.*;\n import Std.Convert.*;\n import Std.Diagnostics.*;\n import Std.Math.*;\n\n @EntryPoint()\n operation SolvingGraphColoringWithGroverDemo() : Unit {\n // Experiment with the parameters to explore algorithm behavior in different conditions!\n let V = 3;\n // The 0 -- 1 -- 2 graph from the examples\n let edges = [(0, 1), (1, 2)];\n let markingOracle = Oracle_VertexColoring(V, edges, _, _);\n for iterations in 0..9 {\n mutable success = 0;\n for _ in 1..100 {\n let res = GroversSearch(2 * V, markingOracle, iterations);\n // Convert measurement results to integers\n let colorPartitions = Chunks(2, res);\n let colors = Mapped(bits -> BoolArrayAsInt(Reversed(bits)), colorPartitions);\n if IsVertexColoringValid(V, edges, colors) {\n set success += 1;\n }\n }\n Message($\"{iterations} iterations - {success}% success rate\");\n }\n }\n\n operation GroversSearch(\n n : Int,\n markingOracle : (Qubit[], Qubit) => Unit is Adj + Ctl,\n iterations : Int\n ) : Bool[] {\n use qs = Qubit[n];\n\n // Operation that prepares the state |all⟩.\n let meanStatePrep = ApplyToEachCA(H, _);\n\n // The phase oracle.\n let phaseOracle = ApplyMarkingOracleAsPhaseOracle(markingOracle, _);\n\n // Prepare the system in the state |all⟩.\n meanStatePrep(qs);\n\n // Do Grover's iterations.\n for _ in 1..iterations {\n // Apply the phase oracle.\n phaseOracle(qs);\n\n // Apply \"reflection about the mean\".\n ReflectionAboutState(qs, meanStatePrep);\n }\n\n // Measure to get the result.\n return ResultArrayAsBoolArray(MResetEachZ(qs));\n }\n\n operation ApplyMarkingOracleAsPhaseOracle(\n markingOracle : (Qubit[], Qubit) => Unit is Adj + Ctl,\n qubits : Qubit[]\n ) : Unit is Adj + Ctl {\n use minus = Qubit();\n within {\n X(minus);\n H(minus);\n } apply {\n markingOracle(qubits, minus);\n }\n }\n\n operation ReflectionAboutState(\n qs : Qubit[],\n statePrep : Qubit[] => Unit is Adj + Ctl\n ) : Unit is Adj + Ctl {\n within {\n Adjoint statePrep(qs);\n } apply {\n ConditionalPhaseFlip(qs);\n }\n }\n\n operation ConditionalPhaseFlip(qs : Qubit[]) : Unit is Adj + Ctl {\n within {\n ApplyToEachA(X, qs);\n } apply {\n Controlled Z(qs[1...], qs[0]);\n }\n R(PauliI, 2.0 * PI(), qs[0]);\n }\n\n operation Oracle_VertexColoring(V : Int, edges : (Int, Int)[], x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n let edgesNumber = Length(edges);\n use conflicts = Qubit[edgesNumber];\n within {\n for i in 0..edgesNumber - 1 {\n let (v0, v1) = edges[i];\n Oracle_ColorEquality(\n x[2 * v0 .. 2 * v0 + 1],\n x[2 * v1 .. 2 * v1 + 1],\n conflicts[i]\n );\n }\n } apply {\n ApplyControlledOnInt(0, X, conflicts, y);\n }\n }\n\n operation Oracle_ColorEquality(x0 : Qubit[], x1 : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n within {\n for i in 0..Length(x0) - 1 {\n CNOT(x0[i], x1[i]);\n }\n } apply {\n ApplyControlledOnInt(0, X, x1, y);\n }\n }\n\n function IsVertexColoringValid(V : Int, edges : (Int, Int)[], colors : Int[]) : Bool {\n for (v0, v1) in edges {\n if colors[v0] == colors[v1] {\n return false;\n }\n }\n return true;\n }\n}\n"
7610
7610
  }
7611
7611
  ]
7612
7612
  },
@@ -7738,7 +7738,7 @@ export default {
7738
7738
  {
7739
7739
  "type": "solution",
7740
7740
  "id": "qft__binary_fraction_classical_solution_b",
7741
- "code": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Math;\n\n operation BinaryFractionClassical(q : Qubit, j : Bool[]) : Unit is Adj + Ctl {\n let n = Length(j);\n let jIntBE = BoolArrayAsInt(Reversed(j));\n R1(2.0 * PI() * IntAsDouble(jIntBE) / IntAsDouble(1 <<< n), q);\n }\n}"
7741
+ "code": "namespace Kata {\n import Std.Arrays.*;\n import Std.Convert.*;\n import Std.Math.*;\n\n operation BinaryFractionClassical(q : Qubit, j : Bool[]) : Unit is Adj + Ctl {\n let n = Length(j);\n let jIntBE = BoolArrayAsInt(Reversed(j));\n R1(2.0 * PI() * IntAsDouble(jIntBE) / IntAsDouble(1 <<< n), q);\n }\n}\n"
7742
7742
  }
7743
7743
  ]
7744
7744
  }
@@ -7861,7 +7861,7 @@ export default {
7861
7861
  "qft__all_basis_vectors__Verification.qs",
7862
7862
  "KatasLibrary.qs"
7863
7863
  ],
7864
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n\n operation AllBasisVectors(qs : Qubit[]) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n\n // You might find this helper operation that implements QFT using a library operation useful.\n operation QFT(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n}\n",
7864
+ "placeholderCode": "namespace Kata {\n import Std.Arrays.*;\n\n operation AllBasisVectors(qs : Qubit[]) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n\n // You might find this helper operation that implements QFT using a library operation useful.\n operation QFT(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n}\n",
7865
7865
  "explainedSolution": {
7866
7866
  "type": "explained-solution",
7867
7867
  "items": [
@@ -7872,7 +7872,7 @@ export default {
7872
7872
  {
7873
7873
  "type": "solution",
7874
7874
  "id": "qft__all_basis_vectors_solution",
7875
- "code": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n\n operation AllBasisVectors(qs : Qubit[]) : Unit is Adj + Ctl {\n QFT(qs);\n }\n\n operation QFT(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n}\n"
7875
+ "code": "namespace Kata {\n import Std.Arrays.*;\n\n operation AllBasisVectors(qs : Qubit[]) : Unit is Adj + Ctl {\n QFT(qs);\n }\n\n operation QFT(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n}\n"
7876
7876
  }
7877
7877
  ]
7878
7878
  }
@@ -7889,7 +7889,7 @@ export default {
7889
7889
  "qft__periodic_state__Verification.qs",
7890
7890
  "KatasLibrary.qs"
7891
7891
  ],
7892
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n\n operation PeriodicState(qs : Qubit[], F : Int) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n\n // You might find this helper operation that implements QFT using a library operation useful.\n operation QFT(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n}\n",
7892
+ "placeholderCode": "namespace Kata {\n import Std.Arrays.*;\n\n operation PeriodicState(qs : Qubit[], F : Int) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n\n // You might find this helper operation that implements QFT using a library operation useful.\n operation QFT(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n}\n",
7893
7893
  "explainedSolution": {
7894
7894
  "type": "explained-solution",
7895
7895
  "items": [
@@ -7900,7 +7900,7 @@ export default {
7900
7900
  {
7901
7901
  "type": "solution",
7902
7902
  "id": "qft__periodic_state_solution",
7903
- "code": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Convert;\n\n operation PeriodicState(qs : Qubit[], F : Int) : Unit is Adj + Ctl {\n let bitsBE = Reversed(IntAsBoolArray(F, Length(qs)));\n ApplyPauliFromBitString(PauliX, true, bitsBE, qs);\n QFT(qs);\n }\n\n operation QFT(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n}\n"
7903
+ "code": "namespace Kata {\n import Std.Arrays.*;\n import Std.Convert.*;\n\n operation PeriodicState(qs : Qubit[], F : Int) : Unit is Adj + Ctl {\n let bitsBE = Reversed(IntAsBoolArray(F, Length(qs)));\n ApplyPauliFromBitString(PauliX, true, bitsBE, qs);\n QFT(qs);\n }\n\n operation QFT(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n}\n"
7904
7904
  }
7905
7905
  ]
7906
7906
  }
@@ -7917,7 +7917,7 @@ export default {
7917
7917
  "qft__alternating_amplitudes__Verification.qs",
7918
7918
  "KatasLibrary.qs"
7919
7919
  ],
7920
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n\n operation AlternatingAmplitudes(qs : Qubit[]) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n\n // You might find this helper operation that implements QFT using a library operation useful.\n operation QFT(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n}\n",
7920
+ "placeholderCode": "namespace Kata {\n import Std.Arrays.*;\n\n operation AlternatingAmplitudes(qs : Qubit[]) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n\n // You might find this helper operation that implements QFT using a library operation useful.\n operation QFT(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n}\n",
7921
7921
  "explainedSolution": {
7922
7922
  "type": "explained-solution",
7923
7923
  "items": [
@@ -7928,7 +7928,7 @@ export default {
7928
7928
  {
7929
7929
  "type": "solution",
7930
7930
  "id": "qft__alternating_amplitudes_solution",
7931
- "code": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n\n operation AlternatingAmplitudes(qs : Qubit[]) : Unit is Adj + Ctl {\n X(qs[0]);\n QFT(qs);\n }\n\n operation QFT(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n}\n"
7931
+ "code": "namespace Kata {\n import Std.Arrays.*;\n\n operation AlternatingAmplitudes(qs : Qubit[]) : Unit is Adj + Ctl {\n X(qs[0]);\n QFT(qs);\n }\n\n operation QFT(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n}\n"
7932
7932
  }
7933
7933
  ]
7934
7934
  }
@@ -7945,7 +7945,7 @@ export default {
7945
7945
  "qft__all_even_vectors__Verification.qs",
7946
7946
  "KatasLibrary.qs"
7947
7947
  ],
7948
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n\n operation AllEvenVectors(qs : Qubit[]) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n\n // You might find this helper operation that implements QFT using a library operation useful.\n operation QFT(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n}\n",
7948
+ "placeholderCode": "namespace Kata {\n import Std.Arrays.*;\n\n operation AllEvenVectors(qs : Qubit[]) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n\n // You might find this helper operation that implements QFT using a library operation useful.\n operation QFT(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n}\n",
7949
7949
  "explainedSolution": {
7950
7950
  "type": "explained-solution",
7951
7951
  "items": [
@@ -7956,7 +7956,7 @@ export default {
7956
7956
  {
7957
7957
  "type": "solution",
7958
7958
  "id": "qft__all_even_vectors_solution",
7959
- "code": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n\n operation AllEvenVectors(qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n QFT(qs);\n }\n\n operation QFT(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n}\n"
7959
+ "code": "namespace Kata {\n import Std.Arrays.*;\n\n operation AllEvenVectors(qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n QFT(qs);\n }\n\n operation QFT(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n}\n"
7960
7960
  }
7961
7961
  ]
7962
7962
  }
@@ -7973,7 +7973,7 @@ export default {
7973
7973
  "qft__square_wave__Verification.qs",
7974
7974
  "KatasLibrary.qs"
7975
7975
  ],
7976
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n\n operation SquareWave(qs : Qubit[]) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n\n // You might find this helper operation that implements QFT using a library operation useful.\n operation QFT(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n}\n",
7976
+ "placeholderCode": "namespace Kata {\n import Std.Arrays.*;\n\n operation SquareWave(qs : Qubit[]) : Unit is Adj + Ctl {\n // Implement your solution here...\n\n }\n\n // You might find this helper operation that implements QFT using a library operation useful.\n operation QFT(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n}\n",
7977
7977
  "explainedSolution": {
7978
7978
  "type": "explained-solution",
7979
7979
  "items": [
@@ -7984,7 +7984,7 @@ export default {
7984
7984
  {
7985
7985
  "type": "solution",
7986
7986
  "id": "qft__square_wave_solution",
7987
- "code": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n\n operation SquareWave(qs : Qubit[]) : Unit is Adj + Ctl {\n X(qs[1]);\n H(qs[0]);\n T(qs[0]);\n X(qs[0]);\n Adjoint T(qs[0]);\n X(qs[0]);\n QFT(qs);\n }\n\n operation QFT(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n}\n"
7987
+ "code": "namespace Kata {\n import Std.Arrays.*;\n\n operation SquareWave(qs : Qubit[]) : Unit is Adj + Ctl {\n X(qs[1]);\n H(qs[0]);\n T(qs[0]);\n X(qs[0]);\n Adjoint T(qs[0]);\n X(qs[0]);\n QFT(qs);\n }\n\n operation QFT(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n}\n"
7988
7988
  }
7989
7989
  ]
7990
7990
  }
@@ -8001,7 +8001,7 @@ export default {
8001
8001
  "qft__signal_frequency__Verification.qs",
8002
8002
  "KatasLibrary.qs"
8003
8003
  ],
8004
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n\n operation SignalFrequency(qs : Qubit[]) : Int {\n // Implement your solution here...\n \n return -1;\n }\n\n // You might find this helper operation that implements QFT using a library operation useful.\n operation QFT(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n}\n",
8004
+ "placeholderCode": "namespace Kata {\n import Std.Arrays.*;\n\n operation SignalFrequency(qs : Qubit[]) : Int {\n // Implement your solution here...\n\n return -1;\n }\n\n // You might find this helper operation that implements QFT using a library operation useful.\n operation QFT(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n}\n",
8005
8005
  "explainedSolution": {
8006
8006
  "type": "explained-solution",
8007
8007
  "items": [
@@ -8012,7 +8012,7 @@ export default {
8012
8012
  {
8013
8013
  "type": "solution",
8014
8014
  "id": "qft__signal_frequency_solution",
8015
- "code": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n\n operation SignalFrequency(qs : Qubit[]) : Int {\n Adjoint QFT(qs);\n return MeasureInteger(Reversed(qs));\n }\n\n operation QFT(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n}\n"
8015
+ "code": "namespace Kata {\n import Std.Arrays.*;\n\n operation SignalFrequency(qs : Qubit[]) : Int {\n Adjoint QFT(qs);\n return MeasureInteger(Reversed(qs));\n }\n\n operation QFT(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n}\n"
8016
8016
  }
8017
8017
  ]
8018
8018
  }
@@ -8069,7 +8069,7 @@ export default {
8069
8069
  "phase_estimation__eigenvalues_s__Verification.qs",
8070
8070
  "KatasLibrary.qs"
8071
8071
  ],
8072
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function EigenvaluesS() : Complex[] {\n // Replace the return value with correct answer.\n return [Complex(0.0, 0.0),\n Complex(0.0, 0.0)];\n }\n}\n",
8072
+ "placeholderCode": "namespace Kata {\n import Std.Math.*;\n\n function EigenvaluesS() : Complex[] {\n // Replace the return value with correct answer.\n return [\n Complex(0.0, 0.0),\n Complex(0.0, 0.0)\n ];\n }\n}\n",
8073
8073
  "explainedSolution": {
8074
8074
  "type": "explained-solution",
8075
8075
  "items": [
@@ -8080,7 +8080,7 @@ export default {
8080
8080
  {
8081
8081
  "type": "solution",
8082
8082
  "id": "phase_estimation__eigenvalues_s_solution",
8083
- "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function EigenvaluesS() : Complex[] {\n return [Complex(1.0, 0.0),\n Complex(0.0, 1.0)];\n }\n}\n"
8083
+ "code": "namespace Kata {\n import Std.Math.*;\n\n function EigenvaluesS() : Complex[] {\n return [\n Complex(1.0, 0.0),\n Complex(0.0, 1.0)\n ];\n }\n}\n"
8084
8084
  }
8085
8085
  ]
8086
8086
  }
@@ -8097,7 +8097,7 @@ export default {
8097
8097
  "phase_estimation__eigenvectors_x__Verification.qs",
8098
8098
  "KatasLibrary.qs"
8099
8099
  ],
8100
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function EigenvectorsX() : Double[][] {\n // Replace the return value with correct answer.\n return [[0.0, 0.0], \n [0.0, 0.0]];\n }\n}\n",
8100
+ "placeholderCode": "namespace Kata {\n import Std.Math.*;\n\n function EigenvectorsX() : Double[][] {\n // Replace the return value with correct answer.\n return [\n [0.0, 0.0],\n [0.0, 0.0]\n ];\n }\n}\n",
8101
8101
  "explainedSolution": {
8102
8102
  "type": "explained-solution",
8103
8103
  "items": [
@@ -8108,7 +8108,7 @@ export default {
8108
8108
  {
8109
8109
  "type": "solution",
8110
8110
  "id": "phase_estimation__eigenvectors_x_solution",
8111
- "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n function EigenvectorsX() : Double[][] {\n return [[1.0, 1.0], \n [1.0, -1.0]];\n }\n}\n"
8111
+ "code": "namespace Kata {\n import Std.Math.*;\n\n function EigenvectorsX() : Double[][] {\n return [\n [1.0, 1.0],\n [1.0, -1.0]\n ];\n }\n}\n"
8112
8112
  }
8113
8113
  ]
8114
8114
  }
@@ -8136,7 +8136,7 @@ export default {
8136
8136
  {
8137
8137
  "type": "solution",
8138
8138
  "id": "phase_estimation__state_eigenvector_solution",
8139
- "code": "namespace Kata {\n import Microsoft.Quantum.Diagnostics.CheckZero;\n operation IsEigenvector(U : Qubit => Unit, P : Qubit => Unit is Adj) : Bool {\n use q = Qubit();\n P(q);\n U(q);\n Adjoint P(q);\n let ret = CheckZero(q);\n Reset(q);\n return ret;\n }\n}\n"
8139
+ "code": "namespace Kata {\n import Std.Diagnostics.CheckZero;\n operation IsEigenvector(U : Qubit => Unit, P : Qubit => Unit is Adj) : Bool {\n use q = Qubit();\n P(q);\n U(q);\n Adjoint P(q);\n let ret = CheckZero(q);\n Reset(q);\n return ret;\n }\n}\n"
8140
8140
  }
8141
8141
  ]
8142
8142
  }
@@ -8231,7 +8231,7 @@ export default {
8231
8231
  {
8232
8232
  "type": "example",
8233
8233
  "id": "phase_estimation__e2edemo",
8234
- "code": "namespace Kata {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Math;\n\n @EntryPoint()\n operation QuantumPhaseEstimationDemo() : Unit {\n // Experiment with the parameters to explore algorithm behavior for different eigenphases!\n // Use R1Frac(k, n, _) for eigenvalue exp(2π𝑖 k/2^(n+1)), eigenphase k/2^(n+1)\n // or R1(theta, _) for eigenvalue exp(2π𝑖 theta/2), eigenphase theta/2\n // Here are some convenient unitaries and their eigenphases\n // R1Frac(1, 0, _) | 0.5\n // R1Frac(1, 1, _) | 0.25\n // R1Frac(1, 2, _) | 0.125\n // R1Frac(1, 3, _) | 0.0625\n let U = R1Frac(1, 3, _);\n let P = X; // |1⟩ basis state is convenient to experiment with R1 and R1Frac gates\n let n = 3;\n mutable counts = [0, size = 2 ^ n];\n for _ in 1 .. 100 {\n let res = PhaseEstimation(U, P, n);\n set counts w/= res <- counts[res] + 1;\n }\n for i in 0 .. 2 ^ n - 1 {\n if counts[i] > 0 {\n Message($\"Eigenphase {IntAsDouble(i) / IntAsDouble(2 ^ n)} - {counts[i]}%\");\n }\n }\n }\n\n operation PhaseEstimation(\n U : Qubit => Unit is Ctl, \n P : Qubit => Unit,\n n : Int\n ) : Int {\n use (phaseRegister, eigenstate) = (Qubit[n], Qubit());\n P(eigenstate);\n ApplyToEach(H, phaseRegister);\n for k in 0 .. n - 1 {\n for _ in 1 .. 1 <<< k {\n Controlled U([phaseRegister[k]], eigenstate);\n } \n }\n SwapReverseRegister(phaseRegister);\n Adjoint ApplyQFT(phaseRegister);\n\n Reset(eigenstate);\n return MeasureInteger(phaseRegister);\n }\n}"
8234
+ "code": "namespace Kata {\n import Std.Convert.*;\n import Std.Diagnostics.*;\n import Std.Math.*;\n\n @EntryPoint()\n operation QuantumPhaseEstimationDemo() : Unit {\n // Experiment with the parameters to explore algorithm behavior for different eigenphases!\n // Use R1Frac(k, n, _) for eigenvalue exp(2π𝑖 k/2^(n+1)), eigenphase k/2^(n+1)\n // or R1(theta, _) for eigenvalue exp(2π𝑖 theta/2), eigenphase theta/2\n // Here are some convenient unitaries and their eigenphases\n // R1Frac(1, 0, _) | 0.5\n // R1Frac(1, 1, _) | 0.25\n // R1Frac(1, 2, _) | 0.125\n // R1Frac(1, 3, _) | 0.0625\n let U = R1Frac(1, 3, _);\n let P = X; // |1⟩ basis state is convenient to experiment with R1 and R1Frac gates\n let n = 3;\n mutable counts = [0, size = 2^n];\n for _ in 1..100 {\n let res = PhaseEstimation(U, P, n);\n set counts w/= res <- counts[res] + 1;\n }\n for i in 0..2^n - 1 {\n if counts[i] > 0 {\n Message($\"Eigenphase {IntAsDouble(i) / IntAsDouble(2^n)} - {counts[i]}%\");\n }\n }\n }\n\n operation PhaseEstimation(\n U : Qubit => Unit is Ctl,\n P : Qubit => Unit,\n n : Int\n ) : Int {\n use (phaseRegister, eigenstate) = (Qubit[n], Qubit());\n P(eigenstate);\n ApplyToEach(H, phaseRegister);\n for k in 0..n - 1 {\n for _ in 1..1 <<< k {\n Controlled U([phaseRegister[k]], eigenstate);\n }\n }\n SwapReverseRegister(phaseRegister);\n Adjoint ApplyQFT(phaseRegister);\n\n Reset(eigenstate);\n return MeasureInteger(phaseRegister);\n }\n}\n"
8235
8235
  }
8236
8236
  ]
8237
8237
  },
@@ -8563,7 +8563,7 @@ export default {
8563
8563
  {
8564
8564
  "type": "example",
8565
8565
  "id": "qec_shor__shor_code_demo",
8566
- "code": "namespace Kata {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Math;\n\n @EntryPoint()\n operation RunExample() : Unit {\n // Allocate qubits and prepare initial logical state.\n use qs = Qubit[9];\n Ry(ArcCos(0.6), qs[0]);\n\n // Encode the logical state using Shor code.\n ShorEncode(qs);\n\n // Introduce an arbitrary error on one qubit.\n Rz(0.1, qs[5]);\n\n // Detect and correct error.\n let (ind, err) = ShorDetectError(qs);\n\n if ind > -1 {\n if err == PauliX {\n X(qs[ind]);\n } elif err == PauliY {\n Y(qs[ind]);\n } else {\n ForEach(q => Z(q), qs[ind * 3 .. ind * 3 + 2]);\n }\n }\n\n // Check that the state was error-corrected accurately by uncomputing\n // the encoding and state preparation to see that the result is the |0⟩ state.\n Adjoint ShorEncode(qs);\n Ry(-ArcCos(0.6), qs[0]);\n if not CheckAllZero(qs) {\n Message(\"State recovered incorrectly!\");\n ResetAll(qs);\n } else {\n Message(\"State recovered correctly!\");\n }\n }\n\n operation ShorEncode (qs : Qubit[]) : Unit is Adj + Ctl {\n BitflipEncode(qs[0 .. 3 .. 8]);\n ApplyToEachCA(H, qs[0 .. 3 .. 8]);\n for i in 0 .. 2 {\n BitflipEncode(qs[3 * i .. 3 * i + 2]);\n }\n }\n\n operation BitflipEncode (qs : Qubit[]) : Unit is Adj + Ctl {\n CNOT(qs[0], qs[1]);\n CNOT(qs[0], qs[2]);\n }\n\n operation ShorDetectError (qs : Qubit[]) : (Int, Pauli) {\n // Detect X error first\n mutable x_ind = -1;\n for i in 0 .. 2 {\n let err_ind = BitflipDetectError(qs[3 * i .. 3 * i + 2]);\n if err_ind > -1 {\n set x_ind = 3 * i + err_ind;\n }\n }\n\n // Detect Z error \n mutable z_ind = -1;\n let m1 = Measure([PauliX, size = 6], qs[0 .. 5]);\n let m2 = Measure([PauliX, size = 6], qs[3 .. 8]);\n\n if m1 == Zero and m2 == Zero {\n set z_ind = -1;\n } elif m1 == One and m2 == Zero {\n set z_ind = 0;\n } elif m1 == One and m2 == One {\n set z_ind = 1;\n } elif m1 == Zero and m2 == One {\n set z_ind = 2;\n }\n\n // Combine both errors into return value\n if x_ind == -1 and z_ind == -1 {\n return (-1, PauliI);\n }\n if x_ind > -1 and z_ind > -1 {\n return (x_ind, PauliY);\n }\n if x_ind > -1 {\n return (x_ind, PauliX);\n }\n return (z_ind, PauliZ);\n }\n\n operation BitflipDetectError (qs : Qubit[]) : Int {\n let m1 = Measure([PauliZ, PauliZ], qs[0 .. 1]);\n let m2 = Measure([PauliZ, PauliZ], qs[1 .. 2]);\n \n if m1 == One and m2 == Zero {\n return 0;\n } elif m1 == One and m2 == One {\n return 1;\n } elif m1 == Zero and m2 == One {\n return 2;\n } else {\n return -1;\n }\n }\n}\n"
8566
+ "code": "namespace Kata {\n import Std.Arrays.*;\n import Std.Diagnostics.*;\n import Std.Math.*;\n\n @EntryPoint()\n operation RunExample() : Unit {\n // Allocate qubits and prepare initial logical state.\n use qs = Qubit[9];\n Ry(ArcCos(0.6), qs[0]);\n\n // Encode the logical state using Shor code.\n ShorEncode(qs);\n\n // Introduce an arbitrary error on one qubit.\n Rz(0.1, qs[5]);\n\n // Detect and correct error.\n let (ind, err) = ShorDetectError(qs);\n\n if ind > -1 {\n if err == PauliX {\n X(qs[ind]);\n } elif err == PauliY {\n Y(qs[ind]);\n } else {\n ForEach(q => Z(q), qs[ind * 3..ind * 3 + 2]);\n }\n }\n\n // Check that the state was error-corrected accurately by uncomputing\n // the encoding and state preparation to see that the result is the |0⟩ state.\n Adjoint ShorEncode(qs);\n Ry(-ArcCos(0.6), qs[0]);\n if not CheckAllZero(qs) {\n Message(\"State recovered incorrectly!\");\n ResetAll(qs);\n } else {\n Message(\"State recovered correctly!\");\n }\n }\n\n operation ShorEncode(qs : Qubit[]) : Unit is Adj + Ctl {\n BitflipEncode(qs[0..3..8]);\n ApplyToEachCA(H, qs[0..3..8]);\n for i in 0..2 {\n BitflipEncode(qs[3 * i..3 * i + 2]);\n }\n }\n\n operation BitflipEncode(qs : Qubit[]) : Unit is Adj + Ctl {\n CNOT(qs[0], qs[1]);\n CNOT(qs[0], qs[2]);\n }\n\n operation ShorDetectError(qs : Qubit[]) : (Int, Pauli) {\n // Detect X error first\n mutable x_ind = -1;\n for i in 0..2 {\n let err_ind = BitflipDetectError(qs[3 * i..3 * i + 2]);\n if err_ind > -1 {\n set x_ind = 3 * i + err_ind;\n }\n }\n\n // Detect Z error\n mutable z_ind = -1;\n let m1 = Measure([PauliX, size = 6], qs[0..5]);\n let m2 = Measure([PauliX, size = 6], qs[3..8]);\n\n if m1 == Zero and m2 == Zero {\n set z_ind = -1;\n } elif m1 == One and m2 == Zero {\n set z_ind = 0;\n } elif m1 == One and m2 == One {\n set z_ind = 1;\n } elif m1 == Zero and m2 == One {\n set z_ind = 2;\n }\n\n // Combine both errors into return value\n if x_ind == -1 and z_ind == -1 {\n return (-1, PauliI);\n }\n if x_ind > -1 and z_ind > -1 {\n return (x_ind, PauliY);\n }\n if x_ind > -1 {\n return (x_ind, PauliX);\n }\n return (z_ind, PauliZ);\n }\n\n operation BitflipDetectError(qs : Qubit[]) : Int {\n let m1 = Measure([PauliZ, PauliZ], qs[0..1]);\n let m2 = Measure([PauliZ, PauliZ], qs[1..2]);\n\n if m1 == One and m2 == Zero {\n return 0;\n } elif m1 == One and m2 == One {\n return 1;\n } elif m1 == Zero and m2 == One {\n return 2;\n } else {\n return -1;\n }\n }\n}\n"
8567
8567
  }
8568
8568
  ]
8569
8569
  },
@@ -8703,7 +8703,7 @@ export default {
8703
8703
  "nonlocal_games__chsh_quantum_bob_strategy__Verification.qs",
8704
8704
  "KatasLibrary.qs"
8705
8705
  ],
8706
- "placeholderCode": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n operation BobQuantum (bit : Bool, qubit : Qubit) : Bool {\n // Implement your solution here...\n\n return false;\n }\n}\n",
8706
+ "placeholderCode": "namespace Kata {\n import Std.Math.*;\n\n operation BobQuantum(bit : Bool, qubit : Qubit) : Bool {\n // Implement your solution here...\n\n return false;\n }\n}\n",
8707
8707
  "explainedSolution": {
8708
8708
  "type": "explained-solution",
8709
8709
  "items": [
@@ -8714,7 +8714,7 @@ export default {
8714
8714
  {
8715
8715
  "type": "solution",
8716
8716
  "id": "nonlocal_games__chsh_quantum_bob_strategy_solution",
8717
- "code": "namespace Kata {\n open Microsoft.Quantum.Math;\n\n operation BobQuantum (bit : Bool, qubit : Qubit) : Bool {\n let angle = 2.0 * PI() / 8.0;\n Ry(not bit ? -angle | angle, qubit);\n return M(qubit) == One;\n }\n}\n"
8717
+ "code": "namespace Kata {\n import Std.Math.*;\n\n operation BobQuantum(bit : Bool, qubit : Qubit) : Bool {\n let angle = 2.0 * PI() / 8.0;\n Ry(not bit ? -angle | angle, qubit);\n return M(qubit) == One;\n }\n}\n"
8718
8718
  }
8719
8719
  ]
8720
8720
  }
@@ -8742,7 +8742,7 @@ export default {
8742
8742
  {
8743
8743
  "type": "example",
8744
8744
  "id": "nonlocal_games__chsh_e2edemo",
8745
- "code": "namespace Quantum.Kata.CHSHGame {\n open Microsoft.Quantum.Random;\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Convert;\n\n function WinCondition (x : Bool, y : Bool, a : Bool, b : Bool) : Bool {\n return (x and y) == (a != b);\n }\n\n function AliceClassical (x : Bool) : Bool {\n return false;\n }\n\n function BobClassical (y : Bool) : Bool {\n return false;\n }\n\n operation AliceQuantum (bit : Bool, qubit : Qubit) : Bool {\n if bit {\n return MResetX(qubit) == One;\n }\n return MResetZ(qubit) == One;\n }\n\n operation BobQuantum (bit : Bool, qubit : Qubit) : Bool {\n let angle = 2.0 * PI() / 8.0;\n Ry(not bit ? -angle | angle, qubit);\n return M(qubit) == One;\n }\n\n @EntryPoint()\n operation CHSH_GameDemo() : Unit {\n use (aliceQubit, bobQubit) = (Qubit(), Qubit());\n mutable classicalWins = 0;\n mutable quantumWins = 0;\n let iterations = 1000;\n for _ in 1 .. iterations {\n H(aliceQubit);\n CNOT(aliceQubit, bobQubit);\n let (x, y) = (DrawRandomBool(0.5), DrawRandomBool(0.5));\n if WinCondition(x, y, AliceClassical(x), BobClassical(y)) {\n set classicalWins += 1;\n }\n if WinCondition(x, y, AliceQuantum(x, aliceQubit), BobQuantum(y, bobQubit)) {\n set quantumWins += 1;\n }\n ResetAll([aliceQubit, bobQubit]);\n }\n Message($\"Percentage of classical wins is {100.0*IntAsDouble(classicalWins)/IntAsDouble(iterations)}%\");\n Message($\"Percentage of quantum wins is {100.0*IntAsDouble(quantumWins)/IntAsDouble(iterations)}%\");\n }\n}\n"
8745
+ "code": "namespace Quantum.Kata.CHSHGame {\n import Std.Random.*;\n import Std.Math.*;\n import Std.Convert.*;\n\n function WinCondition(x : Bool, y : Bool, a : Bool, b : Bool) : Bool {\n return (x and y) == (a != b);\n }\n\n function AliceClassical(x : Bool) : Bool {\n return false;\n }\n\n function BobClassical(y : Bool) : Bool {\n return false;\n }\n\n operation AliceQuantum(bit : Bool, qubit : Qubit) : Bool {\n if bit {\n return MResetX(qubit) == One;\n }\n return MResetZ(qubit) == One;\n }\n\n operation BobQuantum(bit : Bool, qubit : Qubit) : Bool {\n let angle = 2.0 * PI() / 8.0;\n Ry(not bit ? -angle | angle, qubit);\n return M(qubit) == One;\n }\n\n @EntryPoint()\n operation CHSH_GameDemo() : Unit {\n use (aliceQubit, bobQubit) = (Qubit(), Qubit());\n mutable classicalWins = 0;\n mutable quantumWins = 0;\n let iterations = 1000;\n for _ in 1..iterations {\n H(aliceQubit);\n CNOT(aliceQubit, bobQubit);\n let (x, y) = (DrawRandomBool(0.5), DrawRandomBool(0.5));\n if WinCondition(x, y, AliceClassical(x), BobClassical(y)) {\n set classicalWins += 1;\n }\n if WinCondition(x, y, AliceQuantum(x, aliceQubit), BobQuantum(y, bobQubit)) {\n set quantumWins += 1;\n }\n ResetAll([aliceQubit, bobQubit]);\n }\n Message($\"Percentage of classical wins is {100.0 * IntAsDouble(classicalWins) / IntAsDouble(iterations)}%\");\n Message($\"Percentage of quantum wins is {100.0 * IntAsDouble(quantumWins) / IntAsDouble(iterations)}%\");\n }\n}\n"
8746
8746
  }
8747
8747
  ]
8748
8748
  },
@@ -8859,11 +8859,11 @@ export default {
8859
8859
  "globalCodeSources": [
8860
8860
  {
8861
8861
  "id": "getting_started__flip_qubit__Verification.qs",
8862
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Diagnostics;\n\n operation FlipQubit(q : Qubit) : Unit is Adj + Ctl {\n X(q);\n }\n\n operation CheckSolution() : Bool {\n let solution = register => Kata.FlipQubit(register[0]);\n let reference = register => FlipQubit(register[0]);\n let isCorrect = CheckOperationsAreEqual(1, solution, reference);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n Message(\"Congratulations! You have solved your first exercise.\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Look out for hints when your solution is incorrect.\");\n Message(\"Hint: examine the effect your solution has on the |0〉 state and compare it with the effect it \" +\n \"is expected to have.\");\n ShowQuantumStateComparison(1, (qs => ()), solution, reference);\n }\n isCorrect\n }\n}\n"
8862
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Diagnostics.*;\n\n operation FlipQubit(q : Qubit) : Unit is Adj + Ctl {\n X(q);\n }\n\n operation CheckSolution() : Bool {\n let solution = register => Kata.FlipQubit(register[0]);\n let reference = register => FlipQubit(register[0]);\n let isCorrect = CheckOperationsAreEqual(1, solution, reference);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n Message(\"Congratulations! You have solved your first exercise.\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Look out for hints when your solution is incorrect.\");\n Message(\"Hint: examine the effect your solution has on the |0〉 state and compare it with the effect it \" +\n \"is expected to have.\");\n ShowQuantumStateComparison(1, (qs => ()), solution, reference);\n }\n isCorrect\n }\n}\n"
8863
8863
  },
8864
8864
  {
8865
8865
  "id": "KatasLibrary.qs",
8866
- "code": "// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nnamespace Microsoft.Quantum.Katas {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Random;\n\n /// # Summary\n /// Given two operations, checks whether they act identically (including global phase) for all input states.\n /// This is done through controlled versions of the operations instead of plain ones which convert the global phase\n /// into a relative phase that can be detected.\n operation CheckOperationsAreEqualStrict(\n inputSize : Int,\n op : (Qubit[] => Unit is Adj + Ctl),\n reference : (Qubit[] => Unit is Adj + Ctl))\n : Bool {\n Fact(inputSize > 0, \"`inputSize` must be positive\");\n let controlledOp = register => Controlled op(register[...0], register[1...]);\n let controlledReference = register => Controlled reference(register[...0], register[1...]);\n let areEquivalent = CheckOperationsAreEqual(inputSize + 1, controlledOp, controlledReference);\n areEquivalent\n }\n\n /// # Summary\n /// Given two operations, checks whether they act identically on the zero state |0〉 ⊗ |0〉 ⊗ ... ⊗ |0〉 composed of\n /// `inputSize` qubits.\n operation CheckOperationsEquivalenceOnZeroState(\n op : (Qubit[] => Unit),\n reference : (Qubit[] => Unit is Adj),\n inputSize : Int)\n : Bool {\n Fact(inputSize > 0, \"`inputSize` must be positive\");\n use target = Qubit[inputSize];\n op(target);\n Adjoint reference(target);\n let isCorrect = CheckAllZero(target);\n ResetAll(target);\n isCorrect\n }\n\n\n /// # Summary\n /// Given two operations, checks whether they act identically on the given initial state composed of `inputSize` qubits.\n /// The initial state is prepared by applying the `initialState` operation to the state |0〉 ⊗ |0〉 ⊗ ... ⊗ |0〉.\n /// This operation introduces a control qubit to convert a global phase into a relative phase to be able to detect it.\n /// `initialState` operation should be deterministic.\n operation CheckOperationsEquivalenceOnInitialStateStrict(\n initialState : Qubit[] => Unit is Adj,\n op : (Qubit[] => Unit is Adj + Ctl),\n reference : (Qubit[] => Unit is Adj + Ctl),\n inputSize : Int\n ) : Bool {\n use (control, target) = (Qubit(), Qubit[inputSize]);\n within {\n H(control);\n initialState(target);\n }\n apply {\n Controlled op([control], target);\n Adjoint Controlled reference([control], target);\n }\n\n let isCorrect = CheckAllZero([control] + target);\n ResetAll([control] + target);\n isCorrect\n }\n\n\n /// # Summary\n /// Given two operations, checks whether they act identically on the zero state |0〉 ⊗ |0〉 ⊗ ... ⊗ |0〉 composed of\n /// `inputSize` qubits.\n /// This operation introduces a control qubit to convert a global phase into a relative phase to be able to detect\n /// it.\n operation CheckOperationsEquivalenceOnZeroStateStrict(\n op : (Qubit[] => Unit is Adj + Ctl),\n reference : (Qubit[] => Unit is Adj + Ctl),\n inputSize : Int)\n : Bool {\n Fact(inputSize > 0, \"`inputSize` must be positive\");\n CheckOperationsEquivalenceOnInitialStateStrict(qs => (), op, reference, inputSize)\n }\n\n\n /// # Summary\n /// Shows the comparison of the quantum states produced by a specific operation and a reference operation\n /// when applied to the state prepared using deterministic operation `initialState`.\n operation ShowQuantumStateComparison(\n registerSize : Int,\n initialState : Qubit[] => Unit,\n op : Qubit[] => Unit,\n reference : Qubit[] => Unit)\n : Unit {\n {\n use register = Qubit[registerSize];\n initialState(register);\n\n Message(\"Initial quantum state:\");\n DumpMachine();\n\n // Apply the reference operation and dump the simulator state\n reference(register);\n Message(\"Expected quantum state after applying the operation:\");\n DumpMachine();\n ResetAll(register);\n }\n\n {\n use register = Qubit[registerSize];\n initialState(register);\n // Apply the comparison operation and dump the simulator state\n op(register);\n Message(\"Actual quantum state after applying the operation:\");\n DumpMachine();\n ResetAll(register);\n }\n }\n\n /// # Summary\n /// Given two operations, checks whether they act identically on the zero state |0〉 ⊗ |0〉 ⊗ ... ⊗ |0〉 composed of\n /// `inputSize` qubits. If they don't, prints user feedback.\n operation CheckOperationsEquivalenceOnZeroStateWithFeedback(\n testImpl : (Qubit[] => Unit),\n refImpl : (Qubit[] => Unit is Adj),\n inputSize : Int\n ) : Bool {\n\n let isCorrect = CheckOperationsEquivalenceOnZeroState(testImpl, refImpl, inputSize);\n\n // Output different feedback to the user depending on whether the exercise was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n ShowQuantumStateComparison(inputSize, (qs => ()), testImpl, refImpl);\n }\n isCorrect\n }\n\n\n internal operation EntangleRegisters(\n control : Qubit[],\n target : Qubit[]) : Unit is Adj + Ctl {\n Fact(\n Length(control) == Length(target),\n $\"The length of qubit registers must be the same.\");\n\n for index in IndexRange(control) {\n H(control[index]);\n CNOT(control[index], target[index]);\n }\n }\n\n\n /// # Summary\n /// Prepare a random uneven superposition state on the given qubit array.\n operation PrepDemoState(qs : Qubit[]) : Unit {\n Fact(Length(qs) <= 4, \"States with 5 qubits or more are not supported.\");\n let probs = [0.36, 0.25, 1. / 3., 1. / 5.][... Length(qs) - 1];\n for (q, prob) in Zipped(qs, probs) {\n Ry(ArcCos(Sqrt(prob)) * 2.0, q);\n }\n }\n\n\n // \"Framework\" operation for testing single-qubit tasks for distinguishing states of one qubit\n // with Bool return\n operation DistinguishTwoStates_SingleQubit(\n statePrep : ((Qubit, Int) => Unit is Adj),\n testImpl : (Qubit => Bool),\n stateNames : String[],\n preserveState : Bool) : Bool {\n\n let nTotal = 100;\n let nStates = 2;\n mutable misclassifications = [0, size=nStates];\n\n use q = Qubit();\n for _ in 1 .. nTotal {\n // get a random bit to define whether qubit will be in a state corresponding to true return (1) or to false one (0)\n // state = 0 false return\n // state = 1 true return\n let state = DrawRandomInt(0, 1);\n\n // do state prep: convert |0⟩ to outcome with false return or to outcome with true return depending on state\n statePrep(q, state);\n\n // get the solution's answer and verify if NOT a match, then differentiate what kind of mismatch\n let ans = testImpl(q);\n if ans != (state == 1) {\n set misclassifications w/= state <- misclassifications[state] + 1;\n }\n\n // If the final state is to be preserved, check if it was not modified\n if preserveState {\n Adjoint statePrep(q, state);\n if not CheckZero(q) {\n Message($\"Input quantum state {stateNames[state]} was not preserved during the measurement.\");\n Reset(q);\n return false;\n }\n } else {\n Reset(q);\n }\n }\n\n mutable totalMisclassifications = 0;\n for i in 0 .. nStates - 1 {\n if misclassifications[i] != 0 {\n set totalMisclassifications += misclassifications[i];\n Message($\"Misclassified {stateNames[i]} as {stateNames[1 - i]} in {misclassifications[i]} test runs.\");\n }\n }\n\n totalMisclassifications == 0\n }\n\n\n // \"Framework\" operation for testing multi-qubit tasks for distinguishing states of an array of qubits\n // with Int return\n operation DistinguishStates_MultiQubit(\n nQubits : Int,\n nStates : Int,\n statePrep : ((Qubit[], Int, Double) => Unit is Adj),\n testImpl : (Qubit[] => Int),\n preserveState : Bool,\n stateNames : String[]) : Bool {\n\n let nTotal = 100;\n // misclassifications will store the number of times state i has been classified as state j (dimension nStates^2)\n mutable misclassifications = [0, size = nStates * nStates];\n // unknownClassifications will store the number of times state i has been classified as some invalid state (index < 0 or >= nStates)\n mutable unknownClassifications = [0, size = nStates];\n\n use qs = Qubit[nQubits];\n for _ in 1 .. nTotal {\n // get a random integer to define the state of the qubits\n let state = DrawRandomInt(0, nStates - 1);\n // get a random rotation angle to define the exact state of the qubits\n // for some exercises, this value might be a dummy variable which does not matter\n let alpha = DrawRandomDouble(0.0, 1.0) * PI();\n\n // do state prep: convert |0...0⟩ to outcome with return equal to state\n statePrep(qs, state, alpha);\n\n // get the solution's answer and verify that it's a match, if not, increase the exact mismatch count\n let ans = testImpl(qs);\n if ans >= 0 and ans < nStates {\n // classification result is a valid state index - check if is it correct\n if ans != state {\n set misclassifications w/= ((state * nStates) + ans) <- (misclassifications[(state * nStates) + ans] + 1);\n }\n }\n else {\n // classification result is an invalid state index - file it separately\n set unknownClassifications w/= state <- (unknownClassifications[state] + 1);\n }\n\n if preserveState {\n // check that the state of the qubit after the operation is unchanged\n Adjoint statePrep(qs, state, alpha);\n if not CheckAllZero(qs) {\n Message($\"Input quantum state {stateNames[state]} was not preserved during the measurement.\");\n ResetAll(qs);\n return false;\n }\n } else {\n // we're not checking the state of the qubit after the operation\n ResetAll(qs);\n }\n }\n\n mutable totalMisclassifications = 0;\n for i in 0 .. nStates - 1 {\n for j in 0 .. nStates - 1 {\n if misclassifications[(i * nStates) + j] != 0 {\n set totalMisclassifications += misclassifications[i * nStates + j];\n Message($\"Misclassified {stateNames[i]} as {stateNames[j]} in {misclassifications[(i * nStates) + j]} test runs.\");\n }\n }\n if unknownClassifications[i] != 0 {\n set totalMisclassifications += unknownClassifications[i];\n Message($\"Misclassified {stateNames[i]} as Unknown State in {unknownClassifications[i]} test runs.\");\n }\n }\n totalMisclassifications == 0\n }\n\n // Helper function to convert a boolean array to its ket state representation\n function BoolArrayAsKetState (bits : Bool[]) : String {\n mutable stateName = \"|\";\n for i in 0 .. Length(bits) - 1 {\n set stateName += (bits[i] ? \"1\" | \"0\");\n }\n\n return stateName + \"⟩\";\n }\n\n // Helper function to convert an array of bit strings to its ket state representation\n function IntArrayAsStateName (\n qubits : Int,\n bitStrings : Bool[][]\n ) : String {\n mutable statename = \"\";\n for i in 0 .. Length(bitStrings) - 1 {\n if i > 0 {\n set statename += \" + \";\n }\n set statename += BoolArrayAsKetState(bitStrings[i]);\n }\n\n return statename;\n }\n\n /// # Summary\n /// Given a marking oracle acting on N inputs, and a classical function acting on N bits, \n /// checks whether the oracle effect matches that of the function on every classical input.\n operation CheckOracleImplementsFunction (\n N : Int, \n oracle : (Qubit[], Qubit) => Unit, \n f : Bool[] -> Bool\n ) : Bool {\n let size = 1 <<< N;\n use (input, target) = (Qubit[N], Qubit());\n for k in 0 .. size - 1 {\n // Prepare k-th bit vector\n let binaryLE = IntAsBoolArray(k, N);\n \n // \"binary\" is little-endian notation, so the second vector tried has qubit 0 in state 1 and the rest in state 0\n ApplyPauliFromBitString(PauliX, true, binaryLE, input);\n \n // Apply the operation\n oracle(input, target);\n \n // Calculate the expected classical result\n let val = f(binaryLE);\n\n // Apply operations that will revert the qubits to the 0 state if the oracle acted correctly.\n if val {\n X(target);\n }\n ApplyPauliFromBitString(PauliX, true, binaryLE, input);\n\n if not CheckAllZero(input + [target]) {\n Message($\"Unexpected result on input {BoolArrayAsKetState(binaryLE)}.\");\n if not CheckAllZero(input) {\n Message(\"The state of the input qubits changed, or they ended up entangled with the target qubit.\");\n Message(\"The state of the system after oracle application:\");\n DumpMachine();\n } else {\n Message($\"Expected result `{val}`, got `{not val}`.\");\n }\n ResetAll(input + [target]);\n return false;\n }\n }\n return true;\n } \n}\n"
8866
+ "code": "// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nnamespace Microsoft.Quantum.Katas {\n import KatasUtils.*;\n export\n CheckOperationsAreEqualStrict,\n CheckOperationsEquivalenceOnZeroState,\n CheckOperationsEquivalenceOnInitialStateStrict,\n CheckOperationsEquivalenceOnZeroStateStrict,\n ShowQuantumStateComparison,\n CheckOperationsEquivalenceOnZeroStateWithFeedback,\n EntangleRegisters,\n PrepDemoState,\n DistinguishTwoStates_SingleQubit,\n DistinguishStates_MultiQubit,\n BoolArrayAsKetState,\n IntArrayAsStateName,\n CheckOracleImplementsFunction;\n}\n\nnamespace KatasUtils {\n import Std.Arrays.*;\n import Std.Convert.*;\n import Std.Diagnostics.*;\n import Std.Math.*;\n import Std.Random.*;\n\n /// # Summary\n /// Given two operations, checks whether they act identically (including global phase) for all input states.\n /// This is done through controlled versions of the operations instead of plain ones which convert the global phase\n /// into a relative phase that can be detected.\n operation CheckOperationsAreEqualStrict(\n inputSize : Int,\n op : (Qubit[] => Unit is Adj + Ctl),\n reference : (Qubit[] => Unit is Adj + Ctl)\n ) : Bool {\n Fact(inputSize > 0, \"`inputSize` must be positive\");\n let controlledOp = register => Controlled op(register[...0], register[1...]);\n let controlledReference = register => Controlled reference(register[...0], register[1...]);\n let areEquivalent = CheckOperationsAreEqual(inputSize + 1, controlledOp, controlledReference);\n areEquivalent\n }\n\n /// # Summary\n /// Given two operations, checks whether they act identically on the zero state |0〉 ⊗ |0〉 ⊗ ... ⊗ |0〉 composed of\n /// `inputSize` qubits.\n operation CheckOperationsEquivalenceOnZeroState(\n op : (Qubit[] => Unit),\n reference : (Qubit[] => Unit is Adj),\n inputSize : Int\n ) : Bool {\n Fact(inputSize > 0, \"`inputSize` must be positive\");\n use target = Qubit[inputSize];\n op(target);\n Adjoint reference(target);\n let isCorrect = CheckAllZero(target);\n ResetAll(target);\n isCorrect\n }\n\n\n /// # Summary\n /// Given two operations, checks whether they act identically on the given initial state composed of `inputSize` qubits.\n /// The initial state is prepared by applying the `initialState` operation to the state |0〉 ⊗ |0〉 ⊗ ... ⊗ |0〉.\n /// This operation introduces a control qubit to convert a global phase into a relative phase to be able to detect it.\n /// `initialState` operation should be deterministic.\n operation CheckOperationsEquivalenceOnInitialStateStrict(\n initialState : Qubit[] => Unit is Adj,\n op : (Qubit[] => Unit is Adj + Ctl),\n reference : (Qubit[] => Unit is Adj + Ctl),\n inputSize : Int\n ) : Bool {\n use (control, target) = (Qubit(), Qubit[inputSize]);\n within {\n H(control);\n initialState(target);\n } apply {\n Controlled op([control], target);\n Adjoint Controlled reference([control], target);\n }\n\n let isCorrect = CheckAllZero([control] + target);\n ResetAll([control] + target);\n isCorrect\n }\n\n\n /// # Summary\n /// Given two operations, checks whether they act identically on the zero state |0〉 ⊗ |0〉 ⊗ ... ⊗ |0〉 composed of\n /// `inputSize` qubits.\n /// This operation introduces a control qubit to convert a global phase into a relative phase to be able to detect\n /// it.\n operation CheckOperationsEquivalenceOnZeroStateStrict(\n op : (Qubit[] => Unit is Adj + Ctl),\n reference : (Qubit[] => Unit is Adj + Ctl),\n inputSize : Int\n ) : Bool {\n Fact(inputSize > 0, \"`inputSize` must be positive\");\n CheckOperationsEquivalenceOnInitialStateStrict(qs => (), op, reference, inputSize)\n }\n\n\n /// # Summary\n /// Shows the comparison of the quantum states produced by a specific operation and a reference operation\n /// when applied to the state prepared using deterministic operation `initialState`.\n operation ShowQuantumStateComparison(\n registerSize : Int,\n initialState : Qubit[] => Unit,\n op : Qubit[] => Unit,\n reference : Qubit[] => Unit\n ) : Unit {\n {\n use register = Qubit[registerSize];\n initialState(register);\n\n Message(\"Initial quantum state:\");\n DumpMachine();\n\n // Apply the reference operation and dump the simulator state\n reference(register);\n Message(\"Expected quantum state after applying the operation:\");\n DumpMachine();\n ResetAll(register);\n }\n\n {\n use register = Qubit[registerSize];\n initialState(register);\n // Apply the comparison operation and dump the simulator state\n op(register);\n Message(\"Actual quantum state after applying the operation:\");\n DumpMachine();\n ResetAll(register);\n }\n }\n\n /// # Summary\n /// Given two operations, checks whether they act identically on the zero state |0〉 ⊗ |0〉 ⊗ ... ⊗ |0〉 composed of\n /// `inputSize` qubits. If they don't, prints user feedback.\n operation CheckOperationsEquivalenceOnZeroStateWithFeedback(\n testImpl : (Qubit[] => Unit),\n refImpl : (Qubit[] => Unit is Adj),\n inputSize : Int\n ) : Bool {\n\n let isCorrect = CheckOperationsEquivalenceOnZeroState(testImpl, refImpl, inputSize);\n\n // Output different feedback to the user depending on whether the exercise was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n ShowQuantumStateComparison(inputSize, (qs => ()), testImpl, refImpl);\n }\n isCorrect\n }\n\n\n internal operation EntangleRegisters(\n control : Qubit[],\n target : Qubit[]\n ) : Unit is Adj + Ctl {\n Fact(\n Length(control) == Length(target),\n $\"The length of qubit registers must be the same.\"\n );\n\n for index in IndexRange(control) {\n H(control[index]);\n CNOT(control[index], target[index]);\n }\n }\n\n\n /// # Summary\n /// Prepare a random uneven superposition state on the given qubit array.\n operation PrepDemoState(qs : Qubit[]) : Unit {\n Fact(Length(qs) <= 4, \"States with 5 qubits or more are not supported.\");\n let probs = [0.36, 0.25, 1. / 3., 1. / 5.][...Length(qs) - 1];\n for (q, prob) in Zipped(qs, probs) {\n Ry(ArcCos(Sqrt(prob)) * 2.0, q);\n }\n }\n\n\n // \"Framework\" operation for testing single-qubit tasks for distinguishing states of one qubit\n // with Bool return\n operation DistinguishTwoStates_SingleQubit(\n statePrep : ((Qubit, Int) => Unit is Adj),\n testImpl : (Qubit => Bool),\n stateNames : String[],\n preserveState : Bool\n ) : Bool {\n\n let nTotal = 100;\n let nStates = 2;\n mutable misclassifications = [0, size = nStates];\n\n use q = Qubit();\n for _ in 1..nTotal {\n // get a random bit to define whether qubit will be in a state corresponding to true return (1) or to false one (0)\n // state = 0 false return\n // state = 1 true return\n let state = DrawRandomInt(0, 1);\n\n // do state prep: convert |0⟩ to outcome with false return or to outcome with true return depending on state\n statePrep(q, state);\n\n // get the solution's answer and verify if NOT a match, then differentiate what kind of mismatch\n let ans = testImpl(q);\n if ans != (state == 1) {\n set misclassifications w/= state <- misclassifications[state] + 1;\n }\n\n // If the final state is to be preserved, check if it was not modified\n if preserveState {\n Adjoint statePrep(q, state);\n if not CheckZero(q) {\n Message($\"Input quantum state {stateNames[state]} was not preserved during the measurement.\");\n Reset(q);\n return false;\n }\n } else {\n Reset(q);\n }\n }\n\n mutable totalMisclassifications = 0;\n for i in 0..nStates - 1 {\n if misclassifications[i] != 0 {\n set totalMisclassifications += misclassifications[i];\n Message($\"Misclassified {stateNames[i]} as {stateNames[1 - i]} in {misclassifications[i]} test runs.\");\n }\n }\n\n totalMisclassifications == 0\n }\n\n\n // \"Framework\" operation for testing multi-qubit tasks for distinguishing states of an array of qubits\n // with Int return\n operation DistinguishStates_MultiQubit(\n nQubits : Int,\n nStates : Int,\n statePrep : ((Qubit[], Int, Double) => Unit is Adj),\n testImpl : (Qubit[] => Int),\n preserveState : Bool,\n stateNames : String[]\n ) : Bool {\n\n let nTotal = 100;\n // misclassifications will store the number of times state i has been classified as state j (dimension nStates^2)\n mutable misclassifications = [0, size = nStates * nStates];\n // unknownClassifications will store the number of times state i has been classified as some invalid state (index < 0 or >= nStates)\n mutable unknownClassifications = [0, size = nStates];\n\n use qs = Qubit[nQubits];\n for _ in 1..nTotal {\n // get a random integer to define the state of the qubits\n let state = DrawRandomInt(0, nStates - 1);\n // get a random rotation angle to define the exact state of the qubits\n // for some exercises, this value might be a dummy variable which does not matter\n let alpha = DrawRandomDouble(0.0, 1.0) * PI();\n\n // do state prep: convert |0...0⟩ to outcome with return equal to state\n statePrep(qs, state, alpha);\n\n // get the solution's answer and verify that it's a match, if not, increase the exact mismatch count\n let ans = testImpl(qs);\n if ans >= 0 and ans < nStates {\n // classification result is a valid state index - check if is it correct\n if ans != state {\n set misclassifications w/= ((state * nStates) + ans) <- (misclassifications[(state * nStates) + ans] + 1);\n }\n } else {\n // classification result is an invalid state index - file it separately\n set unknownClassifications w/= state <- (unknownClassifications[state] + 1);\n }\n\n if preserveState {\n // check that the state of the qubit after the operation is unchanged\n Adjoint statePrep(qs, state, alpha);\n if not CheckAllZero(qs) {\n Message($\"Input quantum state {stateNames[state]} was not preserved during the measurement.\");\n ResetAll(qs);\n return false;\n }\n } else {\n // we're not checking the state of the qubit after the operation\n ResetAll(qs);\n }\n }\n\n mutable totalMisclassifications = 0;\n for i in 0..nStates - 1 {\n for j in 0..nStates - 1 {\n if misclassifications[(i * nStates) + j] != 0 {\n set totalMisclassifications += misclassifications[i * nStates + j];\n Message($\"Misclassified {stateNames[i]} as {stateNames[j]} in {misclassifications[(i * nStates) + j]} test runs.\");\n }\n }\n if unknownClassifications[i] != 0 {\n set totalMisclassifications += unknownClassifications[i];\n Message($\"Misclassified {stateNames[i]} as Unknown State in {unknownClassifications[i]} test runs.\");\n }\n }\n totalMisclassifications == 0\n }\n\n // Helper function to convert a boolean array to its ket state representation\n function BoolArrayAsKetState(bits : Bool[]) : String {\n mutable stateName = \"|\";\n for i in 0..Length(bits) - 1 {\n set stateName += (bits[i] ? \"1\" | \"0\");\n }\n\n return stateName + \"⟩\";\n }\n\n // Helper function to convert an array of bit strings to its ket state representation\n function IntArrayAsStateName(\n qubits : Int,\n bitStrings : Bool[][]\n ) : String {\n mutable statename = \"\";\n for i in 0..Length(bitStrings) - 1 {\n if i > 0 {\n set statename += \" + \";\n }\n set statename += BoolArrayAsKetState(bitStrings[i]);\n }\n\n return statename;\n }\n\n /// # Summary\n /// Given a marking oracle acting on N inputs, and a classical function acting on N bits,\n /// checks whether the oracle effect matches that of the function on every classical input.\n operation CheckOracleImplementsFunction(\n N : Int,\n oracle : (Qubit[], Qubit) => Unit,\n f : Bool[] -> Bool\n ) : Bool {\n let size = 1 <<< N;\n use (input, target) = (Qubit[N], Qubit());\n for k in 0..size - 1 {\n // Prepare k-th bit vector\n let binaryLE = IntAsBoolArray(k, N);\n\n // \"binary\" is little-endian notation, so the second vector tried has qubit 0 in state 1 and the rest in state 0\n ApplyPauliFromBitString(PauliX, true, binaryLE, input);\n\n // Apply the operation\n oracle(input, target);\n\n // Calculate the expected classical result\n let val = f(binaryLE);\n\n // Apply operations that will revert the qubits to the 0 state if the oracle acted correctly.\n if val {\n X(target);\n }\n ApplyPauliFromBitString(PauliX, true, binaryLE, input);\n\n if not CheckAllZero(input + [target]) {\n Message($\"Unexpected result on input {BoolArrayAsKetState(binaryLE)}.\");\n if not CheckAllZero(input) {\n Message(\"The state of the input qubits changed, or they ended up entangled with the target qubit.\");\n Message(\"The state of the system after oracle application:\");\n DumpMachine();\n } else {\n Message($\"Expected result `{val}`, got `{not val}`.\");\n }\n ResetAll(input + [target]);\n return false;\n }\n }\n return true;\n }\n}\n"
8867
8867
  },
8868
8868
  {
8869
8869
  "id": "complex_arithmetic__powers_of_i__Verification.qs",
@@ -8871,47 +8871,47 @@ 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 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"
8874
+ "code": "namespace Kata.Verification {\n import Std.Math.*;\n import Std.Random.*;\n import Std.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",
8878
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckTwoComplexOpsAreSame(Kata.ComplexAdd, PlusC)\n }\n}\n"
8878
+ "code": "namespace Kata.Verification {\n import Std.Math.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckTwoComplexOpsAreSame(Kata.ComplexAdd, PlusC)\n }\n}\n"
8879
8879
  },
8880
8880
  {
8881
8881
  "id": "complex_arithmetic__complex_multiplication__Verification.qs",
8882
- "code": "namespace Kata.Verification { \n open Microsoft.Quantum.Math;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckTwoComplexOpsAreSame(Kata.ComplexMult, TimesC)\n }\n}"
8882
+ "code": "namespace Kata.Verification {\n import Std.Math.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckTwoComplexOpsAreSame(Kata.ComplexMult, TimesC)\n }\n}\n"
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}\n"
8886
+ "code": "namespace Kata.Verification {\n import Std.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",
8890
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckTwoComplexOpsAreSame(Kata.ComplexDiv, DividedByC)\n }\n}\n"
8890
+ "code": "namespace Kata.Verification {\n import Std.Math.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckTwoComplexOpsAreSame(Kata.ComplexDiv, DividedByC)\n }\n}\n"
8891
8891
  },
8892
8892
  {
8893
8893
  "id": "complex_arithmetic__complex_modulus__Verification.qs",
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"
8894
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import Std.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 import Std.Math.*;\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.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"
8902
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import Std.Math.*;\n import Std.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",
8906
- "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 = ComplexAsComplexPolar(x);\n let actual = Kata.ComplexToComplexPolar(x);\n \n if not ComplexPolarEqual(expected, actual) {\n Message(\"Incorrect\");\n Message($\"For x = {ComplexAsString(x)} expected return {ComplexPolarAsString(expected)}, actual return {ComplexPolarAsString(actual)}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}"
8906
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import Std.Math.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for _ in 0..24 {\n let x = DrawRandomComplex();\n\n let expected = ComplexAsComplexPolar(x);\n let actual = Kata.ComplexToComplexPolar(x);\n\n if not ComplexPolarEqual(expected, actual) {\n Message(\"Incorrect\");\n Message($\"For x = {ComplexAsString(x)} expected return {ComplexPolarAsString(expected)}, actual return {ComplexPolarAsString(actual)}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
8907
8907
  },
8908
8908
  {
8909
8909
  "id": "complex_arithmetic__polar_to_cartesian__Verification.qs",
8910
- "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 = ComplexAsComplexPolar(DrawRandomComplex());\n\n let expected = ComplexPolarAsComplex(x);\n let actual = Kata.ComplexPolarToComplex(x);\n \n if not ComplexEqual(expected, actual) {\n Message(\"Incorrect\");\n Message($\"For x = {ComplexPolarAsString(x)} expected return {ComplexAsString(expected)}, actual return {ComplexAsString(actual)}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}"
8910
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import Std.Math.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for _ in 0..24 {\n let x = ComplexAsComplexPolar(DrawRandomComplex());\n\n let expected = ComplexPolarAsComplex(x);\n let actual = Kata.ComplexPolarToComplex(x);\n\n if not ComplexEqual(expected, actual) {\n Message(\"Incorrect\");\n Message($\"For x = {ComplexPolarAsString(x)} expected return {ComplexAsString(expected)}, actual return {ComplexAsString(actual)}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
8911
8911
  },
8912
8912
  {
8913
8913
  "id": "complex_arithmetic__polar_multiplication__Verification.qs",
8914
- "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 let y = DrawRandomComplex();\n let xp = ComplexAsComplexPolar(x);\n let yp = ComplexAsComplexPolar(y);\n\n let expected = ComplexAsComplexPolar(TimesC(x, y));\n let actual = Kata.ComplexPolarMult(xp, yp);\n \n if not ComplexPolarEqual(expected, actual) {\n Message(\"Incorrect\");\n Message($\"For x = {ComplexPolarAsString(xp)}, y = {ComplexPolarAsString(yp)} \" + \n $\"expected return {ComplexPolarAsString(expected)}, actual return {ComplexPolarAsString(actual)}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}"
8914
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import Std.Math.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for _ in 0..24 {\n let x = DrawRandomComplex();\n let y = DrawRandomComplex();\n let xp = ComplexAsComplexPolar(x);\n let yp = ComplexAsComplexPolar(y);\n\n let expected = ComplexAsComplexPolar(TimesC(x, y));\n let actual = Kata.ComplexPolarMult(xp, yp);\n\n if not ComplexPolarEqual(expected, actual) {\n Message(\"Incorrect\");\n Message($\"For x = {ComplexPolarAsString(xp)}, y = {ComplexPolarAsString(yp)} \" + \n $\"expected return {ComplexPolarAsString(expected)}, actual return {ComplexPolarAsString(actual)}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
8915
8915
  },
8916
8916
  {
8917
8917
  "id": "linear_algebra__addition__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 import Std.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",
@@ -8939,163 +8939,163 @@ export default {
8939
8939
  },
8940
8940
  {
8941
8941
  "id": "linear_algebra__conjugate__Verification.qs",
8942
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n\n function Conjugate_Reference() : Complex[][] {\n return [[Complex(1., -5.), Complex(2., 0.)],\n [Complex(3., 6.), Complex(0., -4.)]];\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n ArraysEqualC(Kata.Conjugate(), Conjugate_Reference())\n }\n}\n"
8942
+ "code": "namespace Kata.Verification {\n import Std.Math.*;\n\n function Conjugate_Reference() : Complex[][] {\n return [\n [Complex(1., -5.), Complex(2., 0.)],\n [Complex(3., 6.), Complex(0., -4.)]\n ];\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n ArraysEqualC(Kata.Conjugate(), Conjugate_Reference())\n }\n}\n"
8943
8943
  },
8944
8944
  {
8945
8945
  "id": "linear_algebra__adjoint__Verification.qs",
8946
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n\n function MatrixAdjoint_Reference() : Complex[][] {\n return [[Complex(1., -5.), Complex(3., 6.)],\n [Complex(2., 0.), Complex(0., -4.)]];\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n ArraysEqualC(Kata.MatrixAdjoint(), MatrixAdjoint_Reference())\n }\n}\n"
8946
+ "code": "namespace Kata.Verification {\n import Std.Math.*;\n\n function MatrixAdjoint_Reference() : Complex[][] {\n return [\n [Complex(1., -5.), Complex(3., 6.)],\n [Complex(2., 0.), Complex(0., -4.)]\n ];\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n ArraysEqualC(Kata.MatrixAdjoint(), MatrixAdjoint_Reference())\n }\n}\n"
8947
8947
  },
8948
8948
  {
8949
8949
  "id": "linear_algebra__inner_product__Verification.qs",
8950
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n\n function InnerProduct_Reference() : Complex {\n return Complex(-18., 72.);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let actual = Kata.InnerProduct();\n let expected = InnerProduct_Reference();\n if AbsComplex(MinusC(actual, expected)) > 1e-9 {\n Message(\"Incorrect\");\n Message($\"Expected {ComplexAsString(expected)}, actual {ComplexAsString(actual)}\");\n return false;\n }\n \n Message(\"Correct!\");\n return true;\n }\n}\n"
8950
+ "code": "namespace Kata.Verification {\n import Std.Math.*;\n\n function InnerProduct_Reference() : Complex {\n return Complex(-18., 72.);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let actual = Kata.InnerProduct();\n let expected = InnerProduct_Reference();\n if AbsComplex(MinusC(actual, expected)) > 1e-9 {\n Message(\"Incorrect\");\n Message($\"Expected {ComplexAsString(expected)}, actual {ComplexAsString(actual)}\");\n return false;\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
8951
8951
  },
8952
8952
  {
8953
8953
  "id": "linear_algebra__normalized_vector__Verification.qs",
8954
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n\n function NormalizedVector_Reference() : Complex[][] {\n return [[Complex(-0.6, 0.)],\n [Complex(0., 0.8)]];\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n ArraysEqualC(Kata.NormalizedVector(), NormalizedVector_Reference())\n }\n}\n"
8954
+ "code": "namespace Kata.Verification {\n import Std.Math.*;\n\n function NormalizedVector_Reference() : Complex[][] {\n return [\n [Complex(-0.6, 0.)],\n [Complex(0., 0.8)]\n ];\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n ArraysEqualC(Kata.NormalizedVector(), NormalizedVector_Reference())\n }\n}\n"
8955
8955
  },
8956
8956
  {
8957
8957
  "id": "linear_algebra__outer_product__Verification.qs",
8958
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n\n function OuterProduct_Reference() : Complex[][] {\n return [[Complex(-27., 0.), Complex(0., -6.)],\n [Complex(0., -81.), Complex(18., 0.)]];\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n ArraysEqualC(Kata.OuterProduct(), OuterProduct_Reference())\n }\n}\n"
8958
+ "code": "namespace Kata.Verification {\n import Std.Math.*;\n\n function OuterProduct_Reference() : Complex[][] {\n return [\n [Complex(-27., 0.), Complex(0., -6.)],\n [Complex(0., -81.), Complex(18., 0.)]\n ];\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n ArraysEqualC(Kata.OuterProduct(), OuterProduct_Reference())\n }\n}\n"
8959
8959
  },
8960
8960
  {
8961
8961
  "id": "linear_algebra__tensor_product__Verification.qs",
8962
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n\n function TensorProduct_Reference() : Double[][] {\n return [[5., 6., 10., 12.],\n [7., 8., 14., 16.],\n [15., 18., 20., 24.],\n [21., 24., 28., 32.]];\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n ArraysEqualD(Kata.TensorProduct(), TensorProduct_Reference())\n }\n}\n"
8962
+ "code": "namespace Kata.Verification {\n import Std.Math.*;\n\n function TensorProduct_Reference() : Double[][] {\n return [\n [5., 6., 10., 12.],\n [7., 8., 14., 16.],\n [15., 18., 20., 24.],\n [21., 24., 28., 32.]\n ];\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n ArraysEqualD(Kata.TensorProduct(), TensorProduct_Reference())\n }\n}\n"
8963
8963
  },
8964
8964
  {
8965
8965
  "id": "qubit__learn_single_qubit_state__Verification.qs",
8966
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n use q = Qubit();\n\n // Prepare the state that will be passed to the solution.\n let angle = 0.5;\n Ry(angle, q);\n\n // Call the solution and get the answer.\n let (a, b) = Kata.LearnSingleQubitState(q);\n\n // Calculate the expected values based on the rotation angle.\n let (a_exp, b_exp) = (Cos(0.5 * angle), Sin(0.5 * angle));\n\n Reset(q);\n\n let isCorrect =\n (AbsD(a - a_exp) <= 0.001) and\n (AbsD(b - b_exp) <= 0.001);\n\n // Output different feedback to the user depending on whether the exercise was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"At least one of the amplitudes was too far from the expected value.\");\n }\n\n isCorrect\n }\n}\n"
8966
+ "code": "namespace Kata.Verification {\n import Std.Math.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n use q = Qubit();\n\n // Prepare the state that will be passed to the solution.\n let angle = 0.5;\n Ry(angle, q);\n\n // Call the solution and get the answer.\n let (a, b) = Kata.LearnSingleQubitState(q);\n\n // Calculate the expected values based on the rotation angle.\n let (a_exp, b_exp) = (Cos(0.5 * angle), Sin(0.5 * angle));\n\n Reset(q);\n\n let isCorrect =\n (AbsD(a - a_exp) <= 0.001) and\n (AbsD(b - b_exp) <= 0.001);\n\n // Output different feedback to the user depending on whether the exercise was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"At least one of the amplitudes was too far from the expected value.\");\n }\n\n isCorrect\n }\n}\n"
8967
8967
  },
8968
8968
  {
8969
8969
  "id": "single_qubit_gates__state_flip__Verification.qs",
8970
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation StateFlip(q : Qubit) : Unit is Adj + Ctl {\n X(q);\n }\n\n operation CheckSolution() : Bool {\n let solution = register => Kata.StateFlip(register[0]);\n let reference = register => StateFlip(register[0]);\n let isCorrect = CheckOperationsAreEqualStrict(1, solution, reference);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine the 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 }\n isCorrect\n }\n}"
8970
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Math.*;\n\n operation StateFlip(q : Qubit) : Unit is Adj + Ctl {\n X(q);\n }\n\n operation CheckSolution() : Bool {\n let solution = register => Kata.StateFlip(register[0]);\n let reference = register => StateFlip(register[0]);\n let isCorrect = CheckOperationsAreEqualStrict(1, solution, reference);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine the 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 }\n isCorrect\n }\n}\n"
8971
8971
  },
8972
8972
  {
8973
8973
  "id": "single_qubit_gates__sign_flip__Verification.qs",
8974
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation SignFlip(q : Qubit) : Unit is Adj + Ctl {\n Z(q);\n }\n\n operation CheckSolution() : Bool {\n let solution = register => Kata.SignFlip(register[0]);\n let reference = register => SignFlip(register[0]);\n let isCorrect = CheckOperationsAreEqualStrict(1, solution, reference);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine the 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 }\n isCorrect\n }\n}\n"
8974
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Math.*;\n\n operation SignFlip(q : Qubit) : Unit is Adj + Ctl {\n Z(q);\n }\n\n operation CheckSolution() : Bool {\n let solution = register => Kata.SignFlip(register[0]);\n let reference = register => SignFlip(register[0]);\n let isCorrect = CheckOperationsAreEqualStrict(1, solution, reference);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine the 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 }\n isCorrect\n }\n}\n"
8975
8975
  },
8976
8976
  {
8977
8977
  "id": "single_qubit_gates__y_gate__Verification.qs",
8978
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation ApplyY(q : Qubit) : Unit is Adj + Ctl {\n Y(q);\n }\n\n operation CheckSolution() : Bool {\n let solution = register => Kata.ApplyY(register[0]);\n let reference = register => ApplyY(register[0]);\n let isCorrect = CheckOperationsAreEqualStrict(1, solution, reference);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine the 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 }\n isCorrect\n }\n}\n"
8978
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Math.*;\n\n operation ApplyY(q : Qubit) : Unit is Adj + Ctl {\n Y(q);\n }\n\n operation CheckSolution() : Bool {\n let solution = register => Kata.ApplyY(register[0]);\n let reference = register => ApplyY(register[0]);\n let isCorrect = CheckOperationsAreEqualStrict(1, solution, reference);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine the 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 }\n isCorrect\n }\n}\n"
8979
8979
  },
8980
8980
  {
8981
8981
  "id": "single_qubit_gates__sign_flip_on_zero__Verification.qs",
8982
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation SignFlipOnZero(q : Qubit) : Unit is Adj + Ctl {\n X(q);\n Z(q);\n X(q);\n }\n\n operation CheckSolution() : Bool {\n let solution = register => Kata.SignFlipOnZero(register[0]);\n let reference = register => SignFlipOnZero(register[0]);\n let isCorrect = CheckOperationsAreEqualStrict(1, solution, reference);\n\n // Output different feedback to the user depending on whether the exercise was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n 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 }\n isCorrect\n }\n}\n"
8982
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Math.*;\n\n operation SignFlipOnZero(q : Qubit) : Unit is Adj + Ctl {\n X(q);\n Z(q);\n X(q);\n }\n\n operation CheckSolution() : Bool {\n let solution = register => Kata.SignFlipOnZero(register[0]);\n let reference = register => SignFlipOnZero(register[0]);\n let isCorrect = CheckOperationsAreEqualStrict(1, solution, reference);\n\n // Output different feedback to the user depending on whether the exercise was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n 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 }\n isCorrect\n }\n}\n"
8983
8983
  },
8984
8984
  {
8985
8985
  "id": "single_qubit_gates__global_phase_minusone__Verification.qs",
8986
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Convert;\n\n\n operation GlobalPhaseChange (q : Qubit) : Unit is Adj + Ctl {\n Z(q);\n X(q);\n Z(q);\n X(q);\n }\n\n operation CheckSolution() : Bool {\n let solution = register => Kata.GlobalPhaseChange(register[0]);\n let reference = register => GlobalPhaseChange(register[0]);\n let isCorrect = CheckOperationsAreEqualStrict(1, solution, reference);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine the 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 }\n isCorrect\n }\n}\n"
8986
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Math.*;\n import Std.Convert.*;\n\n\n operation GlobalPhaseChange(q : Qubit) : Unit is Adj + Ctl {\n Z(q);\n X(q);\n Z(q);\n X(q);\n }\n\n operation CheckSolution() : Bool {\n let solution = register => Kata.GlobalPhaseChange(register[0]);\n let reference = register => GlobalPhaseChange(register[0]);\n let isCorrect = CheckOperationsAreEqualStrict(1, solution, reference);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine the 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 }\n isCorrect\n }\n}\n"
8987
8987
  },
8988
8988
  {
8989
8989
  "id": "single_qubit_gates__global_phase_i__Verification.qs",
8990
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation GlobalPhaseI(q : Qubit) : Unit is Adj + Ctl {\n X(q);\n Z(q);\n Y(q);\n }\n\n operation CheckSolution() : Bool {\n let solution = register => Kata.GlobalPhaseI(register[0]);\n let reference = register => GlobalPhaseI(register[0]);\n let isCorrect = CheckOperationsAreEqualStrict(1, solution, reference);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine the 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 }\n isCorrect\n }\n}\n"
8990
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Math.*;\n\n operation GlobalPhaseI(q : Qubit) : Unit is Adj + Ctl {\n X(q);\n Z(q);\n Y(q);\n }\n\n operation CheckSolution() : Bool {\n let solution = register => Kata.GlobalPhaseI(register[0]);\n let reference = register => GlobalPhaseI(register[0]);\n let isCorrect = CheckOperationsAreEqualStrict(1, solution, reference);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine the 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 }\n isCorrect\n }\n}\n"
8991
8991
  },
8992
8992
  {
8993
8993
  "id": "single_qubit_gates__basis_change__Verification.qs",
8994
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation BasisChange(q : Qubit) : Unit is Adj + Ctl {\n H(q);\n }\n\n operation CheckSolution() : Bool {\n let solution = register => Kata.BasisChange(register[0]);\n let reference = register => BasisChange(register[0]);\n let isCorrect = CheckOperationsAreEqualStrict(1, solution, reference);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine the 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 }\n isCorrect\n }\n}\n"
8994
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Math.*;\n\n operation BasisChange(q : Qubit) : Unit is Adj + Ctl {\n H(q);\n }\n\n operation CheckSolution() : Bool {\n let solution = register => Kata.BasisChange(register[0]);\n let reference = register => BasisChange(register[0]);\n let isCorrect = CheckOperationsAreEqualStrict(1, solution, reference);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine the 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 }\n isCorrect\n }\n}\n"
8995
8995
  },
8996
8996
  {
8997
8997
  "id": "single_qubit_gates__prepare_minus__Verification.qs",
8998
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation PrepareMinus(q : Qubit) : Unit is Adj + Ctl {\n X(q);\n H(q);\n }\n\n operation CheckSolution() : Bool {\n let solution = register => Kata.PrepareMinus(register[0]);\n let reference = register => PrepareMinus(register[0]);\n let isCorrect = CheckOperationsEquivalenceOnZeroStateStrict(solution, reference, 1);\n\n // Output different feedback to the user depending on whether the exercise was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n 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 }\n isCorrect\n }\n}\n"
8998
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation PrepareMinus(q : Qubit) : Unit is Adj + Ctl {\n X(q);\n H(q);\n }\n\n operation CheckSolution() : Bool {\n let solution = register => Kata.PrepareMinus(register[0]);\n let reference = register => PrepareMinus(register[0]);\n let isCorrect = CheckOperationsEquivalenceOnZeroStateStrict(solution, reference, 1);\n\n // Output different feedback to the user depending on whether the exercise was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n 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 }\n isCorrect\n }\n}\n"
8999
8999
  },
9000
9000
  {
9001
9001
  "id": "single_qubit_gates__phase_i__Verification.qs",
9002
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Convert;\n\n operation PhaseFlip (q : Qubit) : Unit is Adj + Ctl {\n S(q);\n }\n\n operation CheckSolution() : Bool {\n let solution = register => Kata.PhaseFlip(register[0]);\n let reference = register => PhaseFlip(register[0]);\n let isCorrect = CheckOperationsAreEqualStrict(1, solution, reference);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine the 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 }\n isCorrect\n }\n}\n"
9002
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Math.*;\n import Std.Convert.*;\n\n operation PhaseFlip(q : Qubit) : Unit is Adj + Ctl {\n S(q);\n }\n\n operation CheckSolution() : Bool {\n let solution = register => Kata.PhaseFlip(register[0]);\n let reference = register => PhaseFlip(register[0]);\n let isCorrect = CheckOperationsAreEqualStrict(1, solution, reference);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine the 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 }\n isCorrect\n }\n}\n"
9003
9003
  },
9004
9004
  {
9005
9005
  "id": "single_qubit_gates__three_quarters_pi_phase__Verification.qs",
9006
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation ThreeQuartersPiPhase(q : Qubit) : Unit is Adj + Ctl {\n S(q);\n T(q);\n }\n\n operation CheckSolution() : Bool {\n let solution = register => Kata.ThreeQuartersPiPhase(register[0]);\n let reference = register => ThreeQuartersPiPhase(register[0]);\n let isCorrect = CheckOperationsAreEqualStrict(1, solution, reference);\n\n // Output different feedback to the user depending on whether the exercise was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n 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 }\n isCorrect\n }\n}\n"
9006
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Math.*;\n\n operation ThreeQuartersPiPhase(q : Qubit) : Unit is Adj + Ctl {\n S(q);\n T(q);\n }\n\n operation CheckSolution() : Bool {\n let solution = register => Kata.ThreeQuartersPiPhase(register[0]);\n let reference = register => ThreeQuartersPiPhase(register[0]);\n let isCorrect = CheckOperationsAreEqualStrict(1, solution, reference);\n\n // Output different feedback to the user depending on whether the exercise was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n 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 }\n isCorrect\n }\n}\n"
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 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}"
9010
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Math.*;\n import Std.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}\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 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"
9014
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Math.*;\n import Std.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 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"
9018
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import KatasUtils.*;\n import Std.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 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"
9022
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import KatasUtils.*;\n import Std.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",
9026
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n use qs = Qubit[2];\n\n // Prepare the state that will be passed to the solution.\n H(qs[0]);\n ApplyControlledOnInt(0, Ry(1.0, _), [qs[0]], qs[1]);\n ApplyControlledOnInt(1, Ry(2.0, _), [qs[0]], qs[1]);\n\n // Call the solution and get the answer.\n let (x1, x2) = Kata.LearnBasisStateAmplitudes(qs);\n\n // Calculate the expected values based on the rotation angle.\n // We convert |00⟩ + |10⟩ to |0⟩ Ry(1.0)|0⟩ + |1⟩ Ry(2.0)|0⟩, so\n // * the amplitude of |01⟩ is 2nd amp of Ry(1.0)|0⟩\n // * the amplitude of |10⟩ is 1st amp of Ry(2.0)|0⟩\n let (x1_exp, x2_exp) = (\n 1.0/Sqrt(2.0) * Sin(0.5 * 1.0),\n 1.0/Sqrt(2.0) * Cos(0.5 * 2.0));\n\n let isCorrect =\n (AbsD(x1 - x1_exp) <= 0.001) and\n (AbsD(x2 - x2_exp) <= 0.001);\n\n ResetAll(qs);\n\n // Output different feedback to the user depending on whether the exercise was correct.\n if isCorrect {\n Message(\"All tests passed.\");\n } else {\n Message(\"One of the amplitudes was too far from the expected value.\");\n }\n\n isCorrect\n }\n}\n"
9026
+ "code": "namespace Kata.Verification {\n import Std.Math.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n use qs = Qubit[2];\n\n // Prepare the state that will be passed to the solution.\n H(qs[0]);\n ApplyControlledOnInt(0, Ry(1.0, _), [qs[0]], qs[1]);\n ApplyControlledOnInt(1, Ry(2.0, _), [qs[0]], qs[1]);\n\n // Call the solution and get the answer.\n let (x1, x2) = Kata.LearnBasisStateAmplitudes(qs);\n\n // Calculate the expected values based on the rotation angle.\n // We convert |00⟩ + |10⟩ to |0⟩ Ry(1.0)|0⟩ + |1⟩ Ry(2.0)|0⟩, so\n // * the amplitude of |01⟩ is 2nd amp of Ry(1.0)|0⟩\n // * the amplitude of |10⟩ is 1st amp of Ry(2.0)|0⟩\n let (x1_exp, x2_exp) = (\n 1.0 / Sqrt(2.0) * Sin(0.5 * 1.0),\n 1.0 / Sqrt(2.0) * Cos(0.5 * 2.0)\n );\n\n let isCorrect =\n (AbsD(x1 - x1_exp) <= 0.001) and\n (AbsD(x2 - x2_exp) <= 0.001);\n\n ResetAll(qs);\n\n // Output different feedback to the user depending on whether the exercise was correct.\n if isCorrect {\n Message(\"All tests passed.\");\n } else {\n Message(\"One of the amplitudes was too far from the expected value.\");\n }\n\n isCorrect\n }\n}\n"
9027
9027
  },
9028
9028
  {
9029
9029
  "id": "multi_qubit_systems__prepare_basis_state__Verification.qs",
9030
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation PrepareBasisState_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n X(qs[0]);\n X(qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(Kata.PrepareBasisState, \n PrepareBasisState_Reference, 2)\n }\n}\n"
9030
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation PrepareBasisState_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n X(qs[0]);\n X(qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.PrepareBasisState,\n PrepareBasisState_Reference,\n 2\n )\n }\n}\n"
9031
9031
  },
9032
9032
  {
9033
9033
  "id": "multi_qubit_systems__prepare_superposition__Verification.qs",
9034
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation PrepareSuperposition_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n X(qs[1]);\n H(qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(Kata.PrepareSuperposition, \n PrepareSuperposition_Reference, 2)\n }\n}\n"
9034
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation PrepareSuperposition_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n X(qs[1]);\n H(qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.PrepareSuperposition,\n PrepareSuperposition_Reference,\n 2\n )\n }\n}\n"
9035
9035
  },
9036
9036
  {
9037
9037
  "id": "multi_qubit_systems__prepare_with_real__Verification.qs",
9038
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation PrepareWithReal_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n X(qs[1]);\n H(qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(Kata.PrepareWithReal, \n PrepareWithReal_Reference, 2)\n }\n}\n"
9038
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation PrepareWithReal_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n X(qs[1]);\n H(qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.PrepareWithReal,\n PrepareWithReal_Reference,\n 2\n )\n }\n}\n"
9039
9039
  },
9040
9040
  {
9041
9041
  "id": "multi_qubit_systems__prepare_with_complex__Verification.qs",
9042
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation PrepareWithComplex_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n H(qs[1]);\n S(qs[0]);\n T(qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(Kata.PrepareWithComplex, \n PrepareWithComplex_Reference, 2)\n }\n}\n"
9042
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation PrepareWithComplex_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n H(qs[1]);\n S(qs[0]);\n T(qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.PrepareWithComplex,\n PrepareWithComplex_Reference,\n 2\n )\n }\n}\n"
9043
9043
  },
9044
9044
  {
9045
9045
  "id": "multi_qubit_systems__bell_state_change_1__Verification.qs",
9046
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Katas;\n\n operation PrepareBellState(qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n CNOT(qs[0], qs[1]);\n }\n\n\n operation BellStateChange1_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n Z(qs[0]);\n }\n\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = CheckOperationsEquivalenceOnInitialStateStrict(\n PrepareBellState,\n Kata.BellStateChange1, \n BellStateChange1_Reference, \n 2);\n\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect\");\n ShowQuantumStateComparison(2, PrepareBellState, Kata.BellStateChange1, BellStateChange1_Reference);\n }\n\n return isCorrect;\n }\n}\n"
9046
+ "code": "namespace Kata.Verification {\n import Std.Diagnostics.*;\n import KatasUtils.*;\n\n operation PrepareBellState(qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n CNOT(qs[0], qs[1]);\n }\n\n\n operation BellStateChange1_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n Z(qs[0]);\n }\n\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = CheckOperationsEquivalenceOnInitialStateStrict(\n PrepareBellState,\n Kata.BellStateChange1,\n BellStateChange1_Reference,\n 2\n );\n\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect\");\n ShowQuantumStateComparison(2, PrepareBellState, Kata.BellStateChange1, BellStateChange1_Reference);\n }\n\n return isCorrect;\n }\n}\n"
9047
9047
  },
9048
9048
  {
9049
9049
  "id": "multi_qubit_systems__bell_state_change_2__Verification.qs",
9050
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Katas;\n\n operation PrepareBellState(qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n CNOT(qs[0], qs[1]);\n }\n\n\n operation BellStateChange2_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n X(qs[0]);\n }\n\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = CheckOperationsEquivalenceOnInitialStateStrict(\n PrepareBellState,\n Kata.BellStateChange2, \n BellStateChange2_Reference, \n 2);\n\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect\");\n ShowQuantumStateComparison(2, PrepareBellState, Kata.BellStateChange2, BellStateChange2_Reference);\n }\n\n return isCorrect;\n }\n}\n"
9050
+ "code": "namespace Kata.Verification {\n import Std.Diagnostics.*;\n import KatasUtils.*;\n\n operation PrepareBellState(qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n CNOT(qs[0], qs[1]);\n }\n\n\n operation BellStateChange2_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n X(qs[0]);\n }\n\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = CheckOperationsEquivalenceOnInitialStateStrict(\n PrepareBellState,\n Kata.BellStateChange2,\n BellStateChange2_Reference,\n 2\n );\n\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect\");\n ShowQuantumStateComparison(2, PrepareBellState, Kata.BellStateChange2, BellStateChange2_Reference);\n }\n\n return isCorrect;\n }\n}\n"
9051
9051
  },
9052
9052
  {
9053
9053
  "id": "multi_qubit_systems__bell_state_change_3__Verification.qs",
9054
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Katas;\n\n operation PrepareBellState(qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n CNOT(qs[0], qs[1]);\n }\n\n\n operation BellStateChange3_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n X(qs[0]);\n Z(qs[0]);\n }\n\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = CheckOperationsEquivalenceOnInitialStateStrict(\n PrepareBellState,\n Kata.BellStateChange3, \n BellStateChange3_Reference, \n 2);\n\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect\");\n ShowQuantumStateComparison(2, PrepareBellState, Kata.BellStateChange3, BellStateChange3_Reference);\n }\n\n return isCorrect;\n }\n}\n"
9054
+ "code": "namespace Kata.Verification {\n import Std.Diagnostics.*;\n import KatasUtils.*;\n\n operation PrepareBellState(qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n CNOT(qs[0], qs[1]);\n }\n\n\n operation BellStateChange3_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n X(qs[0]);\n Z(qs[0]);\n }\n\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = CheckOperationsEquivalenceOnInitialStateStrict(\n PrepareBellState,\n Kata.BellStateChange3,\n BellStateChange3_Reference,\n 2\n );\n\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect\");\n ShowQuantumStateComparison(2, PrepareBellState, Kata.BellStateChange3, BellStateChange3_Reference);\n }\n\n return isCorrect;\n }\n}\n"
9055
9055
  },
9056
9056
  {
9057
9057
  "id": "multi_qubit_gates__compound_gate__Verification.qs",
9058
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation CompoundGate (qs : Qubit[]) : Unit is Adj + Ctl {\n S(qs[0]);\n I(qs[1]); // this line can be omitted, since it doesn't change the qubit state\n Y(qs[2]);\n }\n\n operation CheckSolution() : Bool {\n let solution = Kata.CompoundGate;\n let reference = CompoundGate;\n let isCorrect = CheckOperationsAreEqualStrict(3, solution, reference);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine how your solution transforms the given state and compare it with the expected \" +\n \"transformation\");\n ShowQuantumStateComparison(3, PrepDemoState, solution, reference);\n }\n\n isCorrect\n }\n}"
9058
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Math.*;\n\n operation CompoundGate(qs : Qubit[]) : Unit is Adj + Ctl {\n S(qs[0]);\n I(qs[1]); // this line can be omitted, since it doesn't change the qubit state\n Y(qs[2]);\n }\n\n operation CheckSolution() : Bool {\n let solution = Kata.CompoundGate;\n let reference = CompoundGate;\n let isCorrect = CheckOperationsAreEqualStrict(3, solution, reference);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine how your solution transforms the given state and compare it with the expected \" +\n \"transformation\");\n ShowQuantumStateComparison(3, PrepDemoState, solution, reference);\n }\n\n isCorrect\n }\n}\n"
9059
9059
  },
9060
9060
  {
9061
9061
  "id": "multi_qubit_gates__entangle_qubits__Verification.qs",
9062
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Convert;\n\n operation EntangleQubits (qs : Qubit[]) : Unit is Adj + Ctl {\n CNOT(qs[0], qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let range = 10;\n for i in 0 .. range - 1 {\n let angle = 2.0 * PI() * IntAsDouble(i) / IntAsDouble(range);\n let initialState = qs => Ry(2.0 * angle, qs[0]);\n let isCorrect = CheckOperationsEquivalenceOnInitialStateStrict(\n initialState,\n Kata.EntangleQubits, \n EntangleQubits, \n 2);\n if not isCorrect {\n Message(\"Incorrect\");\n Message($\"Test fails for alpha = {Cos(angle)}, beta = {Sin(angle)}.\");\n ShowQuantumStateComparison(2, initialState, Kata.EntangleQubits, EntangleQubits);\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}"
9062
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Diagnostics.*;\n import Std.Math.*;\n import Std.Convert.*;\n\n operation EntangleQubits(qs : Qubit[]) : Unit is Adj + Ctl {\n CNOT(qs[0], qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let range = 10;\n for i in 0..range - 1 {\n let angle = 2.0 * PI() * IntAsDouble(i) / IntAsDouble(range);\n let initialState = qs => Ry(2.0 * angle, qs[0]);\n let isCorrect = CheckOperationsEquivalenceOnInitialStateStrict(\n initialState,\n Kata.EntangleQubits,\n EntangleQubits,\n 2\n );\n if not isCorrect {\n Message(\"Incorrect\");\n Message($\"Test fails for alpha = {Cos(angle)}, beta = {Sin(angle)}.\");\n ShowQuantumStateComparison(2, initialState, Kata.EntangleQubits, EntangleQubits);\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9063
9063
  },
9064
9064
  {
9065
9065
  "id": "multi_qubit_gates__preparing_bell_state__Verification.qs",
9066
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation BellState (qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n CNOT(qs[0], qs[1]);\n }\n\n operation CheckSolution() : Bool {\n let solution = Kata.BellState;\n let reference = BellState;\n let isCorrect = CheckOperationsEquivalenceOnZeroState(solution, reference, 2);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine the state prepared by your solution and compare it with the state it \" +\n \"is expected to prepare.\");\n ShowQuantumStateComparison(2, (qs => ()), solution, reference);\n }\n\n isCorrect\n }\n}"
9066
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation BellState(qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n CNOT(qs[0], qs[1]);\n }\n\n operation CheckSolution() : Bool {\n let solution = Kata.BellState;\n let reference = BellState;\n let isCorrect = CheckOperationsEquivalenceOnZeroState(solution, reference, 2);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine the state prepared by your solution and compare it with the state it \" +\n \"is expected to prepare.\");\n ShowQuantumStateComparison(2, (qs => ()), solution, reference);\n }\n\n isCorrect\n }\n}\n"
9067
9067
  },
9068
9068
  {
9069
9069
  "id": "multi_qubit_gates__relative_phase_minusone__Verification.qs",
9070
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Diagnostics;\n\n operation PrepareState(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyToEachCA(H, qs);\n }\n\n operation RelativePhaseMinusOne (qs : Qubit[]) : Unit is Adj + Ctl {\n CZ(qs[0], qs[1]);\n }\n\n \n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = CheckOperationsEquivalenceOnInitialStateStrict(\n PrepareState,\n Kata.RelativePhaseMinusOne, \n RelativePhaseMinusOne, \n 2);\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect\");\n ShowQuantumStateComparison(2, PrepareState, Kata.RelativePhaseMinusOne, RelativePhaseMinusOne);\n }\n\n return isCorrect;\n }\n}"
9070
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Diagnostics.*;\n\n operation PrepareState(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyToEachCA(H, qs);\n }\n\n operation RelativePhaseMinusOne(qs : Qubit[]) : Unit is Adj + Ctl {\n CZ(qs[0], qs[1]);\n }\n\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = CheckOperationsEquivalenceOnInitialStateStrict(\n PrepareState,\n Kata.RelativePhaseMinusOne,\n RelativePhaseMinusOne,\n 2\n );\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect\");\n ShowQuantumStateComparison(2, PrepareState, Kata.RelativePhaseMinusOne, RelativePhaseMinusOne);\n }\n\n return isCorrect;\n }\n}\n"
9071
9071
  },
9072
9072
  {
9073
9073
  "id": "multi_qubit_gates__qubit_swap__Verification.qs",
9074
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Katas;\n\n operation QubitSwap (qs : Qubit[], index1 : Int, index2 : Int) : Unit is Adj + Ctl {\n SWAP(qs[index1], qs[index2]);\n }\n\n operation CheckSolution() : Bool {\n for N in 2 .. 5 {\n for index1 in 0 .. N-2 {\n for index2 in index1+1 .. N-1 {\n let solution = register => Kata.QubitSwap(register, index1, index2);\n let reference = register => QubitSwap(register, index1, index2);\n if not CheckOperationsAreEqual(N, solution, reference) {\n Message(\"Incorrect.\");\n Message($\"Swapping qubits {index1} and {index2} out of $N$ didn't have the expected effect.\");\n return false;\n }\n }\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}"
9074
+ "code": "namespace Kata.Verification {\n import Std.Diagnostics.*;\n import KatasUtils.*;\n\n operation QubitSwap(qs : Qubit[], index1 : Int, index2 : Int) : Unit is Adj + Ctl {\n SWAP(qs[index1], qs[index2]);\n }\n\n operation CheckSolution() : Bool {\n for N in 2..5 {\n for index1 in 0..N-2 {\n for index2 in index1 + 1..N-1 {\n let solution = register => Kata.QubitSwap(register, index1, index2);\n let reference = register => QubitSwap(register, index1, index2);\n if not CheckOperationsAreEqual(N, solution, reference) {\n Message(\"Incorrect.\");\n Message($\"Swapping qubits {index1} and {index2} out of $N$ didn't have the expected effect.\");\n return false;\n }\n }\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9075
9075
  },
9076
9076
  {
9077
9077
  "id": "multi_qubit_gates__fredkin_gate__Verification.qs",
9078
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation FredkinGate (qs : Qubit[]) : Unit is Adj + Ctl {\n Controlled SWAP([qs[0]], (qs[1], qs[2]));\n }\n\n operation CheckSolution() : Bool {\n let solution = Kata.FredkinGate;\n let reference = FredkinGate;\n let isCorrect = CheckOperationsAreEqualStrict(3, solution, reference);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine the state prepared by your solution and compare it with the state it \" +\n \"is expected to prepare.\");\n ShowQuantumStateComparison(3, PrepDemoState, solution, reference);\n }\n\n isCorrect\n }\n}"
9078
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation FredkinGate(qs : Qubit[]) : Unit is Adj + Ctl {\n Controlled SWAP([qs[0]], (qs[1], qs[2]));\n }\n\n operation CheckSolution() : Bool {\n let solution = Kata.FredkinGate;\n let reference = FredkinGate;\n let isCorrect = CheckOperationsAreEqualStrict(3, solution, reference);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine the state prepared by your solution and compare it with the state it \" +\n \"is expected to prepare.\");\n ShowQuantumStateComparison(3, PrepDemoState, solution, reference);\n }\n\n isCorrect\n }\n}\n"
9079
9079
  },
9080
9080
  {
9081
9081
  "id": "multi_qubit_gates__controlled_rotation__Verification.qs",
9082
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Katas;\n\n operation ControlledRotation (qs : Qubit[], theta : Double) : Unit is Adj + Ctl {\n let controll = qs[0];\n let target = qs[1];\n Controlled Rx([controll], (theta, target));\n }\n\n operation CheckSolution() : Bool {\n for i in 0 .. 20 {\n let angle = IntAsDouble(i) / 10.0;\n let solution = register => Kata.ControlledRotation(register, angle);\n let reference = register => ControlledRotation(register, angle);\n if not CheckOperationsAreEqual(2, solution, reference) {\n let precision = 3;\n Message(\"Incorrect.\");\n Message($\"The test case for theta={DoubleAsStringWithPrecision(angle, precision)} did not pass\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}"
9082
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import Std.Diagnostics.*;\n import KatasUtils.*;\n\n operation ControlledRotation(qs : Qubit[], theta : Double) : Unit is Adj + Ctl {\n let controll = qs[0];\n let target = qs[1];\n Controlled Rx([controll], (theta, target));\n }\n\n operation CheckSolution() : Bool {\n for i in 0..20 {\n let angle = IntAsDouble(i) / 10.0;\n let solution = register => Kata.ControlledRotation(register, angle);\n let reference = register => ControlledRotation(register, angle);\n if not CheckOperationsAreEqual(2, solution, reference) {\n let precision = 3;\n Message(\"Incorrect.\");\n Message($\"The test case for theta={DoubleAsStringWithPrecision(angle, precision)} did not pass\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9083
9083
  },
9084
9084
  {
9085
9085
  "id": "multi_qubit_gates__toffoli_gate__Verification.qs",
9086
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation ToffoliGate (qs : Qubit[]) : Unit is Adj + Ctl {\n CCNOT(qs[0], qs[1], qs[2]);\n }\n\n operation CheckSolution() : Bool {\n let solution = Kata.ToffoliGate;\n let reference = ToffoliGate;\n let isCorrect = CheckOperationsAreEqualStrict(3, solution, reference);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine the state prepared by your solution and compare it with the state it \" +\n \"is expected to prepare.\");\n ShowQuantumStateComparison(3, PrepDemoState, solution, reference);\n }\n\n isCorrect\n }\n}"
9086
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation ToffoliGate(qs : Qubit[]) : Unit is Adj + Ctl {\n CCNOT(qs[0], qs[1], qs[2]);\n }\n\n operation CheckSolution() : Bool {\n let solution = Kata.ToffoliGate;\n let reference = ToffoliGate;\n let isCorrect = CheckOperationsAreEqualStrict(3, solution, reference);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine the state prepared by your solution and compare it with the state it \" +\n \"is expected to prepare.\");\n ShowQuantumStateComparison(3, PrepDemoState, solution, reference);\n }\n\n isCorrect\n }\n}\n"
9087
9087
  },
9088
9088
  {
9089
9089
  "id": "multi_qubit_gates__anti_controlled_gate__Verification.qs",
9090
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation AntiControlledGate (qs : Qubit[]) : Unit is Adj + Ctl {\n X(qs[1]);\n CNOT(qs[0], qs[1]);\n }\n\n operation CheckSolution() : Bool {\n let solution = Kata. AntiControlledGate;\n let reference = AntiControlledGate;\n let isCorrect = CheckOperationsAreEqualStrict(2, solution, reference);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine the state prepared by your solution and compare it with the state it \" +\n \"is expected to prepare.\");\n ShowQuantumStateComparison(2, PrepDemoState, solution, reference);\n }\n\n isCorrect\n }\n}\n"
9090
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation AntiControlledGate(qs : Qubit[]) : Unit is Adj + Ctl {\n X(qs[1]);\n CNOT(qs[0], qs[1]);\n }\n\n operation CheckSolution() : Bool {\n let solution = Kata.AntiControlledGate;\n let reference = AntiControlledGate;\n let isCorrect = CheckOperationsAreEqualStrict(2, solution, reference);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine the state prepared by your solution and compare it with the state it \" +\n \"is expected to prepare.\");\t\n ShowQuantumStateComparison(2, PrepDemoState, solution, reference);\n }\n\n isCorrect\n }\n}\n"
9091
9091
  },
9092
9092
  {
9093
9093
  "id": "multi_qubit_gates__arbitrary_controls__Verification.qs",
9094
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Katas;\n\n operation MultiControls(controls : Qubit[], target : Qubit, controlBits : Bool[]) : Unit is Adj + Ctl {\n within {\n for index in 0 .. Length(controls) - 1 {\n if controlBits[index] == false {\n X(controls[index]);\n }\n }\n } apply {\n Controlled X(controls,target);\n }\n }\n\n operation CheckSolution() : Bool {\n for i in 0 .. (2 ^ 4) - 1 {\n let bits = IntAsBoolArray(i, 4);\n let solution = register => Kata.MultiControls(Most(register), Tail(register), bits);\n let reference = register => MultiControls(Most(register), Tail(register), bits);\n if not CheckOperationsAreEqual(5, solution, reference) {\n Message(\"Incorrect.\");\n Message($\"The test case for controlBits = {bits} did not pass.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}"
9094
+ "code": "namespace Kata.Verification {\n import Std.Arrays.*;\n import Std.Convert.*;\n import Std.Diagnostics.*;\n import KatasUtils.*;\n\n operation MultiControls(controls : Qubit[], target : Qubit, controlBits : Bool[]) : Unit is Adj + Ctl {\n within {\n for index in 0..Length(controls) - 1 {\n if controlBits[index] == false {\n X(controls[index]);\n }\n }\n } apply {\n Controlled X(controls, target);\n }\n }\n\n operation CheckSolution() : Bool {\n for i in 0..(2^4) - 1 {\n let bits = IntAsBoolArray(i, 4);\n let solution = register => Kata.MultiControls(Most(register), Tail(register), bits);\n let reference = register => MultiControls(Most(register), Tail(register), bits);\n if not CheckOperationsAreEqual(5, solution, reference) {\n Message(\"Incorrect.\");\n Message($\"The test case for controlBits = {bits} did not pass.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9095
9095
  },
9096
9096
  {
9097
9097
  "id": "preparing_states__plus_state__Verification.qs",
9098
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation PlusState_Reference(q : Qubit) : Unit is Adj + Ctl {\n H(q);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(\n ApplyToFirstCA(Kata.PlusState, _), \n ApplyToFirstCA(PlusState_Reference, _),\n 1)\n }\n}\n"
9098
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation PlusState_Reference(q : Qubit) : Unit is Adj + Ctl {\n H(q);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(\n ApplyToFirstCA(Kata.PlusState, _),\n ApplyToFirstCA(PlusState_Reference, _),\n 1\n )\n }\n}\n"
9099
9099
  },
9100
9100
  {
9101
9101
  "id": "preparing_states__Common.qs",
@@ -9103,255 +9103,255 @@ export default {
9103
9103
  },
9104
9104
  {
9105
9105
  "id": "preparing_states__minus_state__Verification.qs",
9106
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation MinusState_Reference(q : Qubit) : Unit is Adj + Ctl {\n H(q);\n Z(q);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(\n ApplyToFirstCA(Kata.MinusState, _), \n ApplyToFirstCA(MinusState_Reference, _),\n 1)\n }\n}\n"
9106
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation MinusState_Reference(q : Qubit) : Unit is Adj + Ctl {\n H(q);\n Z(q);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(\n ApplyToFirstCA(Kata.MinusState, _),\n ApplyToFirstCA(MinusState_Reference, _),\n 1\n )\n }\n}\n"
9107
9107
  },
9108
9108
  {
9109
9109
  "id": "preparing_states__even_sup_two_qubits__Verification.qs",
9110
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation AllBasisVectors_TwoQubits_Reference (qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n H(qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.AllBasisVectors_TwoQubits,\n AllBasisVectors_TwoQubits_Reference,\n 2)\n }\n}\n"
9110
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation AllBasisVectors_TwoQubits_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n H(qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.AllBasisVectors_TwoQubits,\n AllBasisVectors_TwoQubits_Reference,\n 2\n )\n }\n}\n"
9111
9111
  },
9112
9112
  {
9113
9113
  "id": "preparing_states__even_sup_two_qubits_phase_flip__Verification.qs",
9114
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation AllBasisVectorsWithPhaseFlip_TwoQubits_Reference (qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n H(qs[1]);\n CZ(qs[0], qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.AllBasisVectorsWithPhaseFlip_TwoQubits,\n AllBasisVectorsWithPhaseFlip_TwoQubits_Reference,\n 2)\n }\n}\n"
9114
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation AllBasisVectorsWithPhaseFlip_TwoQubits_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n H(qs[1]);\n CZ(qs[0], qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.AllBasisVectorsWithPhaseFlip_TwoQubits,\n AllBasisVectorsWithPhaseFlip_TwoQubits_Reference,\n 2\n )\n }\n}\n"
9115
9115
  },
9116
9116
  {
9117
9117
  "id": "preparing_states__even_sup_two_qubits_complex_phases__Verification.qs",
9118
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation AllBasisVectorsWithComplexPhases_TwoQubits_Reference (qs : Qubit[]) : Unit is Adj + Ctl {\n // Qubit 0 is taken into |+⟩ and then z-rotated into |-⟩.\n H(qs[0]);\n Z(qs[0]);\n\n // Qubit 1 is taken into |+⟩ and then z-rotated into |i⟩.\n H(qs[1]);\n S(qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.AllBasisVectorsWithComplexPhases_TwoQubits,\n AllBasisVectorsWithComplexPhases_TwoQubits_Reference,\n 2)\n }\n}\n"
9118
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation AllBasisVectorsWithComplexPhases_TwoQubits_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n // Qubit 0 is taken into |+⟩ and then z-rotated into |-⟩.\n H(qs[0]);\n Z(qs[0]);\n\n // Qubit 1 is taken into |+⟩ and then z-rotated into |i⟩.\n H(qs[1]);\n S(qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.AllBasisVectorsWithComplexPhases_TwoQubits,\n AllBasisVectorsWithComplexPhases_TwoQubits_Reference,\n 2\n )\n }\n}\n"
9119
9119
  },
9120
9120
  {
9121
9121
  "id": "preparing_states__bell_state__Verification.qs",
9122
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation BellState_Reference (qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n CNOT(qs[0], qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.BellState,\n BellState_Reference,\n 2)\n }\n}\n"
9122
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation BellState_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n CNOT(qs[0], qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.BellState,\n BellState_Reference,\n 2\n )\n }\n}\n"
9123
9123
  },
9124
9124
  {
9125
9125
  "id": "preparing_states__all_bell_states__Verification.qs",
9126
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation AllBellStates_Reference (qs : Qubit[], index : Int) : Unit is Adj + Ctl {\n H(qs[0]);\n \n if index == 1 {\n Z(qs[0]);\n }\n if index == 2 {\n X(qs[1]);\n }\n if index == 3 {\n Z(qs[0]);\n X(qs[1]);\n }\n \n CNOT(qs[0], qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for index in 0 .. 3 {\n Message($\"Testing index = {index}...\"); \n if not CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.AllBellStates(_, index),\n AllBellStates_Reference(_, index),\n 2) {\n return false;\n }\n }\n\n return true;\n }\n}\n"
9126
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation AllBellStates_Reference(qs : Qubit[], index : Int) : Unit is Adj + Ctl {\n H(qs[0]);\n\n if index == 1 {\n Z(qs[0]);\n }\n if index == 2 {\n X(qs[1]);\n }\n if index == 3 {\n Z(qs[0]);\n X(qs[1]);\n }\n\n CNOT(qs[0], qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for index in 0..3 {\n Message($\"Testing index = {index}...\");\n if not CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.AllBellStates(_, index),\n AllBellStates_Reference(_, index),\n 2\n ) {\n return false;\n }\n }\n\n return true;\n }\n}\n"
9127
9127
  },
9128
9128
  {
9129
9129
  "id": "preparing_states__greenberger_horne_zeilinger__Verification.qs",
9130
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Arrays;\n\n operation GHZ_State_Reference (qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n \n for q in Rest(qs) {\n CNOT(qs[0], q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for i in 1 .. 5 {\n Message($\"Testing {i} qubit(s)...\");\n if not CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.GHZ_State,\n GHZ_State_Reference,\n i) {\n return false;\n }\n }\n\n return true;\n }\n}\n"
9130
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Arrays.*;\n\n operation GHZ_State_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n\n for q in Rest(qs) {\n CNOT(qs[0], q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for i in 1..5 {\n Message($\"Testing {i} qubit(s)...\");\n if not CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.GHZ_State,\n GHZ_State_Reference,\n i\n ) {\n return false;\n }\n }\n\n return true;\n }\n}\n"
9131
9131
  },
9132
9132
  {
9133
9133
  "id": "preparing_states__all_basis_vectors__Verification.qs",
9134
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation AllBasisVectorsSuperposition_Reference (qs : Qubit[]) : Unit is Adj + Ctl {\n for q in qs {\n H(q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for i in 1 .. 5 {\n Message($\"Testing {i} qubit(s)...\");\n if not CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.AllBasisVectorsSuperposition,\n AllBasisVectorsSuperposition_Reference,\n i) {\n return false;\n }\n }\n\n return true;\n\n\n }\n}\n"
9134
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation AllBasisVectorsSuperposition_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n for q in qs {\n H(q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for i in 1..5 {\n Message($\"Testing {i} qubit(s)...\");\n if not CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.AllBasisVectorsSuperposition,\n AllBasisVectorsSuperposition_Reference,\n i\n ) {\n return false;\n }\n }\n\n return true;\n\n\n }\n}\n"
9135
9135
  },
9136
9136
  {
9137
9137
  "id": "preparing_states__even_odd__Verification.qs",
9138
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation EvenOddNumbersSuperposition_Reference(qs : Qubit[], isEven : Bool) : Unit is Adj + Ctl {\n let N = Length(qs);\n\n for i in 0..N-2 {\n H(qs[i]);\n }\n\n if not isEven {\n X(qs[N-1]);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for i in 1 .. 5 {\n for boolVal in [false, true] {\n Message($\"Testing {i} qubit(s) where isEven = {boolVal}...\");\n if not CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.EvenOddNumbersSuperposition(_, boolVal),\n EvenOddNumbersSuperposition_Reference(_, boolVal),\n i) {\n return false;\n }\n }\n }\n\n return true;\n }\n}\n"
9138
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation EvenOddNumbersSuperposition_Reference(qs : Qubit[], isEven : Bool) : Unit is Adj + Ctl {\n let N = Length(qs);\n\n for i in 0..N-2 {\n H(qs[i]);\n }\n\n if not isEven {\n X(qs[N-1]);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for i in 1..5 {\n for boolVal in [false, true] {\n Message($\"Testing {i} qubit(s) where isEven = {boolVal}...\");\n if not CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.EvenOddNumbersSuperposition(_, boolVal),\n EvenOddNumbersSuperposition_Reference(_, boolVal),\n i\n ) {\n return false;\n }\n }\n }\n\n return true;\n }\n}\n"
9139
9139
  },
9140
9140
  {
9141
9141
  "id": "preparing_states__zero_and_bitstring__Verification.qs",
9142
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Katas;\n\n operation ZeroAndBitstringSuperposition_Reference (qs : Qubit[], bits : Bool[]) : Unit is Adj + Ctl {\n H(qs[0]);\n\n for i in 1 .. Length(qs) - 1 {\n if bits[i] {\n CNOT(qs[0], qs[i]);\n }\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let bitstrings = [\n [true],\n [true, false],\n [true, true],\n [true, false, false],\n [true, false, true],\n [true, true, false],\n [true, true, true]\n ];\n\n for bits in bitstrings {\n Message($\"Testing for bits = {bits}...\");\n if not CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.ZeroAndBitstringSuperposition(_, bits),\n ZeroAndBitstringSuperposition_Reference(_, bits),\n Length(bits)) {\n return false;\n }\n }\n\n true\n }\n}\n"
9142
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import KatasUtils.*;\n\n operation ZeroAndBitstringSuperposition_Reference(qs : Qubit[], bits : Bool[]) : Unit is Adj + Ctl {\n H(qs[0]);\n\n for i in 1..Length(qs) - 1 {\n if bits[i] {\n CNOT(qs[0], qs[i]);\n }\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let bitstrings = [\n [true],\n [true, false],\n [true, true],\n [true, false, false],\n [true, false, true],\n [true, true, false],\n [true, true, true]\n ];\n\n for bits in bitstrings {\n Message($\"Testing for bits = {bits}...\");\n if not CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.ZeroAndBitstringSuperposition(_, bits),\n ZeroAndBitstringSuperposition_Reference(_, bits),\n Length(bits)\n ) {\n return false;\n }\n }\n\n true\n }\n}\n"
9143
9143
  },
9144
9144
  {
9145
9145
  "id": "preparing_states__two_bitstrings__Verification.qs",
9146
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Katas;\n\n function FindFirstDiff (bits1 : Bool[], bits2 : Bool[]) : Int {\n for i in 0 .. Length(bits1) - 1 {\n if bits1[i] != bits2[i] {\n return i;\n }\n }\n return -1;\n }\n\n operation TwoBitstringSuperposition_Reference (qs : Qubit[], bits1 : Bool[], bits2 : Bool[]) : Unit is Adj + Ctl {\n // find the index of the first bit at which the bit strings are different\n let firstDiff = FindFirstDiff(bits1, bits2);\n\n // Hadamard corresponding qubit to create superposition\n H(qs[firstDiff]);\n\n // iterate through the bit strings again setting the final state of qubits\n for i in 0 .. Length(qs) - 1 {\n if bits1[i] == bits2[i] {\n // if two bits are the same, apply X or nothing\n if bits1[i] {\n X(qs[i]);\n }\n } else {\n // if two bits are different, set their difference using CNOT\n if i > firstDiff {\n CNOT(qs[firstDiff], qs[i]);\n if bits1[i] != bits1[firstDiff] {\n X(qs[i]);\n }\n }\n }\n }\n } \n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let bitstrings_one = [\n [false],\n [true],\n [false, false],\n [true, false],\n [false, false],\n [false, false, false],\n [false, false, false]\n ];\n\n let bitstrings_two = [\n [true],\n [false],\n [false, true],\n [false, true],\n [true, true],\n [false, false, true],\n [true, false, true]\n ];\n\n for i in 0 .. Length(bitstrings_one) - 1 {\n let bits1 = bitstrings_one[i];\n let bits2 = bitstrings_two[i];\n Message($\"Testing for bits1 = {bits1} and bits2 = {bits2}...\");\n if not CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.TwoBitstringSuperposition(_, bits1, bits2),\n TwoBitstringSuperposition_Reference(_, bits1, bits2),\n Length(bits1)) {\n return false;\n }\n }\n\n true\n }\n}\n"
9146
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import KatasUtils.*;\n\n function FindFirstDiff(bits1 : Bool[], bits2 : Bool[]) : Int {\n for i in 0..Length(bits1) - 1 {\n if bits1[i] != bits2[i] {\n return i;\n }\n }\n return -1;\n }\n\n operation TwoBitstringSuperposition_Reference(qs : Qubit[], bits1 : Bool[], bits2 : Bool[]) : Unit is Adj + Ctl {\n // find the index of the first bit at which the bit strings are different\n let firstDiff = FindFirstDiff(bits1, bits2);\n\n // Hadamard corresponding qubit to create superposition\n H(qs[firstDiff]);\n\n // iterate through the bit strings again setting the final state of qubits\n for i in 0..Length(qs) - 1 {\n if bits1[i] == bits2[i] {\n // if two bits are the same, apply X or nothing\n if bits1[i] {\n X(qs[i]);\n }\n } else {\n // if two bits are different, set their difference using CNOT\n if i > firstDiff {\n CNOT(qs[firstDiff], qs[i]);\n if bits1[i] != bits1[firstDiff] {\n X(qs[i]);\n }\n }\n }\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let bitstrings_one = [\n [false],\n [true],\n [false, false],\n [true, false],\n [false, false],\n [false, false, false],\n [false, false, false]\n ];\n\n let bitstrings_two = [\n [true],\n [false],\n [false, true],\n [false, true],\n [true, true],\n [false, false, true],\n [true, false, true]\n ];\n\n for i in 0..Length(bitstrings_one) - 1 {\n let bits1 = bitstrings_one[i];\n let bits2 = bitstrings_two[i];\n Message($\"Testing for bits1 = {bits1} and bits2 = {bits2}...\");\n if not CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.TwoBitstringSuperposition(_, bits1, bits2),\n TwoBitstringSuperposition_Reference(_, bits1, bits2),\n Length(bits1)\n ) {\n return false;\n }\n }\n\n true\n }\n}\n"
9147
9147
  },
9148
9148
  {
9149
9149
  "id": "preparing_states__four_bitstrings__Verification.qs",
9150
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Random;\n\n operation FourBitstringSuperposition_Reference (qs : Qubit[], bits : Bool[][]) : Unit is Adj {\n use anc = Qubit[2];\n ApplyToEachA(H, anc);\n\n for i in 0 .. 3 {\n for j in 0 .. Length(qs) - 1 {\n if bits[i][j] {\n ApplyControlledOnInt(i, X, anc, qs[j]);\n }\n }\n }\n\n for i in 0 .. 3 {\n if i % 2 == 1 {\n ApplyControlledOnBitString(bits[i], X, qs, anc[0]);\n }\n if i / 2 == 1 {\n ApplyControlledOnBitString(bits[i], X, qs, anc[1]);\n }\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n\n let bits = [[false, false], [false, true], [true, false], [true, true]];\n Message($\"Testing for bits = {bits}...\");\n if not CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.FourBitstringSuperposition(_, bits),\n ApplyToEachA(H, _),\n 2\n ) {\n return false;\n }\n\n let bitstrings = [\n [[false, true, false], [true, false, false], [false, false, true], [true, true, false]],\n [[true, false, false], [false, false, true], [false, true, false], [true, true, true]],\n [[false, false, false], [false, true, false], [true, true, false], [true, false, true]]\n ];\n\n for bitstring in bitstrings {\n Message($\"Testing for bits = {bitstring}...\");\n if not CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.FourBitstringSuperposition(_, bitstring),\n FourBitstringSuperposition_Reference(_, bitstring),\n 3\n ) {\n return false;\n }\n }\n\n return true;\n }\n}\n"
9150
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import KatasUtils.*;\n import Std.Math.*;\n import Std.Random.*;\n\n operation FourBitstringSuperposition_Reference(qs : Qubit[], bits : Bool[][]) : Unit is Adj {\n use anc = Qubit[2];\n ApplyToEachA(H, anc);\n\n for i in 0..3 {\n for j in 0..Length(qs) - 1 {\n if bits[i][j] {\n ApplyControlledOnInt(i, X, anc, qs[j]);\n }\n }\n }\n\n for i in 0..3 {\n if i % 2 == 1 {\n ApplyControlledOnBitString(bits[i], X, qs, anc[0]);\n }\n if i / 2 == 1 {\n ApplyControlledOnBitString(bits[i], X, qs, anc[1]);\n }\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n\n let bits = [[false, false], [false, true], [true, false], [true, true]];\n Message($\"Testing for bits = {bits}...\");\n if not CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.FourBitstringSuperposition(_, bits),\n ApplyToEachA(H, _),\n 2\n ) {\n return false;\n }\n\n let bitstrings = [\n [[false, true, false], [true, false, false], [false, false, true], [true, true, false]],\n [[true, false, false], [false, false, true], [false, true, false], [true, true, true]],\n [[false, false, false], [false, true, false], [true, true, false], [true, false, true]]\n ];\n\n for bitstring in bitstrings {\n Message($\"Testing for bits = {bitstring}...\");\n if not CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.FourBitstringSuperposition(_, bitstring),\n FourBitstringSuperposition_Reference(_, bitstring),\n 3\n ) {\n return false;\n }\n }\n\n return true;\n }\n}\n"
9151
9151
  },
9152
9152
  {
9153
9153
  "id": "preparing_states__parity_bitstrings__Verification.qs",
9154
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Katas;\n\n operation AllStatesWithParitySuperposition_Reference (qs : Qubit[], parity : Int) : Unit is Adj + Ctl {\n if Length(qs) == 1 {\n if parity == 1 {\n X(qs[0]);\n }\n } else {\n H(qs[0]);\n ApplyControlledOnInt(0, AllStatesWithParitySuperposition_Reference, qs[0 .. 0], (qs[1 ...], parity));\n ApplyControlledOnInt(1, AllStatesWithParitySuperposition_Reference, qs[0 .. 0], (qs[1 ...], 1 - parity));\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 2 .. 5 {\n for parity in 0 .. 1 {\n Message($\"Testing for N = {N}, with parity: {parity}...\");\n if not CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.AllStatesWithParitySuperposition(_, parity),\n AllStatesWithParitySuperposition_Reference(_, parity),\n N\n ) {\n return false;\n }\n }\n }\n\n return true;\n }\n}\n"
9154
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import KatasUtils.*;\n\n operation AllStatesWithParitySuperposition_Reference(qs : Qubit[], parity : Int) : Unit is Adj + Ctl {\n if Length(qs) == 1 {\n if parity == 1 {\n X(qs[0]);\n }\n } else {\n H(qs[0]);\n ApplyControlledOnInt(0, AllStatesWithParitySuperposition_Reference, qs[0..0], (qs[1...], parity));\n ApplyControlledOnInt(1, AllStatesWithParitySuperposition_Reference, qs[0..0], (qs[1...], 1 - parity));\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 2..5 {\n for parity in 0..1 {\n Message($\"Testing for N = {N}, with parity: {parity}...\");\n if not CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.AllStatesWithParitySuperposition(_, parity),\n AllStatesWithParitySuperposition_Reference(_, parity),\n N\n ) {\n return false;\n }\n }\n }\n\n return true;\n }\n}\n"
9155
9155
  },
9156
9156
  {
9157
9157
  "id": "preparing_states__unequal_superposition__Verification.qs",
9158
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation UnequalSuperposition_Reference(q : Qubit, alpha : Double) : Unit is Adj + Ctl {\n Ry(2.0 * alpha, q);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let limit = 36;\n let precision = 3;\n for i in 0 .. limit {\n let alpha = 2.0 * PI() * IntAsDouble(i) / IntAsDouble(limit);\n let solution = Kata.UnequalSuperposition(_, alpha);\n let reference = UnequalSuperposition_Reference(_, alpha);\n Message($\"Testing for alpha = {DoubleAsStringWithPrecision(alpha, precision)}...\");\n if not CheckOperationsEquivalenceOnZeroStateWithFeedback(\n qs => solution(qs[0]), \n qs => reference(qs[0]),\n 1) {\n return false;\n }\n }\n\n true\n }\n}\n"
9158
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import KatasUtils.*;\n import Std.Math.*;\n\n operation UnequalSuperposition_Reference(q : Qubit, alpha : Double) : Unit is Adj + Ctl {\n Ry(2.0 * alpha, q);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let limit = 36;\n let precision = 3;\n for i in 0..limit {\n let alpha = 2.0 * PI() * IntAsDouble(i) / IntAsDouble(limit);\n let solution = Kata.UnequalSuperposition(_, alpha);\n let reference = UnequalSuperposition_Reference(_, alpha);\n Message($\"Testing for alpha = {DoubleAsStringWithPrecision(alpha, precision)}...\");\n if not CheckOperationsEquivalenceOnZeroStateWithFeedback(\n qs => solution(qs[0]),\n qs => reference(qs[0]),\n 1\n ) {\n return false;\n }\n }\n\n true\n }\n}\n"
9159
9159
  },
9160
9160
  {
9161
9161
  "id": "preparing_states__controlled_rotation__Verification.qs",
9162
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation ControlledRotation_Reference (qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n Controlled H ([qs[0]], qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.ControlledRotation,\n ControlledRotation_Reference,\n 2)\n }\n}\n"
9162
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation ControlledRotation_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n H(qs[0]);\n Controlled H([qs[0]], qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.ControlledRotation,\n ControlledRotation_Reference,\n 2\n )\n }\n}\n"
9163
9163
  },
9164
9164
  {
9165
9165
  "id": "preparing_states__three_states_two_qubits__Verification.qs",
9166
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n // Reference solution that does not use measurements (to be adjointable)\n operation ThreeStates_TwoQubits_Reference (qs : Qubit[]) : Unit is Adj + Ctl {\n let theta = ArcSin(1.0 / Sqrt(3.0));\n Ry(2.0 * theta, qs[0]);\n ApplyControlledOnInt(0, H,[qs[0]], qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.ThreeStates_TwoQubits,\n ThreeStates_TwoQubits_Reference,\n 2)\n }\n}\n"
9166
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Math.*;\n\n // Reference solution that does not use measurements (to be adjointable)\n operation ThreeStates_TwoQubits_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n let theta = ArcSin(1.0 / Sqrt(3.0));\n Ry(2.0 * theta, qs[0]);\n ApplyControlledOnInt(0, H, [qs[0]], qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.ThreeStates_TwoQubits,\n ThreeStates_TwoQubits_Reference,\n 2\n )\n }\n}\n"
9167
9167
  },
9168
9168
  {
9169
9169
  "id": "preparing_states__three_states_two_qubits_phases__Verification.qs",
9170
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation ThreeStates_TwoQubits_Phases_Reference (qs : Qubit[]) : Unit is Adj {\n // First create (|00⟩ + |01⟩ + |10⟩) / sqrt(3) state\n ThreeStates_TwoQubits_Reference(qs);\n\n R1(4.0 * PI() / 3.0, qs[0]);\n R1(2.0 * PI() / 3.0, qs[1]);\n }\n\n operation ThreeStates_TwoQubits_Reference (qs : Qubit[]) : Unit is Adj {\n let theta = ArcSin(1.0 / Sqrt(3.0));\n Ry(2.0 * theta, qs[0]);\n ApplyControlledOnInt(0, H, [qs[0]], qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n return CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.ThreeStates_TwoQubits_Phases,\n ThreeStates_TwoQubits_Phases_Reference,\n 2\n );\n }\n}\n"
9170
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Math.*;\n\n operation ThreeStates_TwoQubits_Phases_Reference(qs : Qubit[]) : Unit is Adj {\n // First create (|00⟩ + |01⟩ + |10⟩) / sqrt(3) state\n ThreeStates_TwoQubits_Reference(qs);\n\n R1(4.0 * PI() / 3.0, qs[0]);\n R1(2.0 * PI() / 3.0, qs[1]);\n }\n\n operation ThreeStates_TwoQubits_Reference(qs : Qubit[]) : Unit is Adj {\n let theta = ArcSin(1.0 / Sqrt(3.0));\n Ry(2.0 * theta, qs[0]);\n ApplyControlledOnInt(0, H, [qs[0]], qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n return CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.ThreeStates_TwoQubits_Phases,\n ThreeStates_TwoQubits_Phases_Reference,\n 2\n );\n }\n}\n"
9171
9171
  },
9172
9172
  {
9173
9173
  "id": "preparing_states__hardy_state__Verification.qs",
9174
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation Hardy_State_Reference (qs : Qubit[]) : Unit is Adj {\n let theta = ArcCos(Sqrt(10.0/12.0));\n Ry(2.0 * theta, qs[0]);\n ApplyControlledOnInt(0, Ry, [qs[0]], (2.0 * ArcCos(3.0 / Sqrt(10.0)), qs[1]));\n ApplyControlledOnInt(1, Ry, [qs[0]], (2.0 * PI() / 4.0, qs[1]));\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n return CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.Hardy_State,\n Hardy_State_Reference,\n 2\n );\n }\n}\n"
9174
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Math.*;\n\n operation Hardy_State_Reference(qs : Qubit[]) : Unit is Adj {\n let theta = ArcCos(Sqrt(10.0 / 12.0));\n Ry(2.0 * theta, qs[0]);\n ApplyControlledOnInt(0, Ry, [qs[0]], (2.0 * ArcCos(3.0 / Sqrt(10.0)), qs[1]));\n ApplyControlledOnInt(1, Ry, [qs[0]], (2.0 * PI() / 4.0, qs[1]));\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n return CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.Hardy_State,\n Hardy_State_Reference,\n 2\n );\n }\n}\n"
9175
9175
  },
9176
9176
  {
9177
9177
  "id": "preparing_states__wstate_power_of_two__Verification.qs",
9178
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation WState_PowerOfTwo_Reference (qs : Qubit[]) : Unit is Adj + Ctl {\n let N = Length(qs);\n Ry(2.0 * ArcSin(Sqrt(1.0/IntAsDouble(N))), qs[0]);\n for i in 1 .. N - 1 {\n ApplyControlledOnInt(0, Ry(2.0 * ArcSin(Sqrt(1.0/IntAsDouble(N - i))), _), qs[0 .. i-1], qs[i]);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in [1, 2, 4, 8, 16] {\n Message($\"Testing for N = {n}...\");\n if not CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.WState_PowerOfTwo,\n WState_PowerOfTwo_Reference,\n n) {\n return false;\n }\n }\n\n return true;\n }\n}\n"
9178
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import KatasUtils.*;\n import Std.Math.*;\n\n operation WState_PowerOfTwo_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n let N = Length(qs);\n Ry(2.0 * ArcSin(Sqrt(1.0 / IntAsDouble(N))), qs[0]);\n for i in 1..N - 1 {\n ApplyControlledOnInt(0, Ry(2.0 * ArcSin(Sqrt(1.0 / IntAsDouble(N - i))), _), qs[0..i-1], qs[i]);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in [1, 2, 4, 8, 16] {\n Message($\"Testing for N = {n}...\");\n if not CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.WState_PowerOfTwo,\n WState_PowerOfTwo_Reference,\n n\n ) {\n return false;\n }\n }\n\n return true;\n }\n}\n"
9179
9179
  },
9180
9180
  {
9181
9181
  "id": "preparing_states__wstate_arbitrary__Verification.qs",
9182
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation WState_Arbitrary_Reference (qs : Qubit[]) : Unit is Adj + Ctl {\n let N = Length(qs);\n Ry(2.0 * ArcSin(Sqrt(1.0/IntAsDouble(N))), qs[0]);\n for i in 1 .. N - 1 {\n ApplyControlledOnInt(0, Ry(2.0 * ArcSin(Sqrt(1.0/IntAsDouble(N - i))), _), qs[0 .. i-1], qs[i]);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 1 .. 6 {\n Message($\"Testing for N = {n}...\");\n if not CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.WState_Arbitrary,\n WState_Arbitrary_Reference,\n n) {\n return false;\n }\n }\n\n return true;\n }\n}\n"
9182
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import KatasUtils.*;\n import Std.Math.*;\n\n operation WState_Arbitrary_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n let N = Length(qs);\n Ry(2.0 * ArcSin(Sqrt(1.0 / IntAsDouble(N))), qs[0]);\n for i in 1..N - 1 {\n ApplyControlledOnInt(0, Ry(2.0 * ArcSin(Sqrt(1.0 / IntAsDouble(N - i))), _), qs[0..i-1], qs[i]);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 1..6 {\n Message($\"Testing for N = {n}...\");\n if not CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Kata.WState_Arbitrary,\n WState_Arbitrary_Reference,\n n\n ) {\n return false;\n }\n }\n\n return true;\n }\n}\n"
9183
9183
  },
9184
9184
  {
9185
9185
  "id": "single_qubit_measurements__distinguish_0_and_1__Verification.qs",
9186
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Katas;\n\n operation StatePrep_IsQubitZero (q : Qubit, state : Int) : Unit is Adj {\n if state == 0 {\n // convert |0⟩ to |1⟩\n X(q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishTwoStates_SingleQubit(\n StatePrep_IsQubitZero,\n Kata.IsQubitZero,\n [\"|1⟩\", \"|0⟩\"],\n false);\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n isCorrect\n }\n\n}\n"
9186
+ "code": "namespace Kata.Verification {\n import Std.Diagnostics.*;\n import KatasUtils.*;\n\n operation StatePrep_IsQubitZero(q : Qubit, state : Int) : Unit is Adj {\n if state == 0 {\n // convert |0⟩ to |1⟩\n X(q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishTwoStates_SingleQubit(\n StatePrep_IsQubitZero,\n Kata.IsQubitZero,\n [\"|1⟩\", \"|0⟩\"],\n false\n );\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n isCorrect\n }\n\n}\n"
9187
9187
  },
9188
9188
  {
9189
9189
  "id": "single_qubit_measurements__distinguish_plus_and_minus__Verification.qs",
9190
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n // Distinguish |+❭ and |-❭ using Measure operation\n operation StatePrep_IsQubitMinus (q : Qubit, state : Int) : Unit is Adj {\n if state == 1 {\n // convert |0⟩ to |-⟩\n X(q);\n H(q);\n } else {\n // convert |0⟩ to |+⟩\n H(q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishTwoStates_SingleQubit(\n StatePrep_IsQubitMinus,\n Kata.IsQubitMinus,\n [\"|+⟩\", \"|-⟩\"],\n false);\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n isCorrect\n }\n\n}\n"
9190
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n // Distinguish |+❭ and |-❭ using Measure operation\n operation StatePrep_IsQubitMinus(q : Qubit, state : Int) : Unit is Adj {\n if state == 1 {\n // convert |0⟩ to |-⟩\n X(q);\n H(q);\n } else {\n // convert |0⟩ to |+⟩\n H(q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishTwoStates_SingleQubit(\n StatePrep_IsQubitMinus,\n Kata.IsQubitMinus,\n [\"|+⟩\", \"|-⟩\"],\n false\n );\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n isCorrect\n }\n\n}\n"
9191
9191
  },
9192
9192
  {
9193
9193
  "id": "single_qubit_measurements__distinguish_orthogonal_states_1__Verification.qs",
9194
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Katas;\n\n // Distinguish specific orthogonal states\n // |ψ₊⟩ = 0.6 * |0⟩ + 0.8 * |1⟩,\n // |ψ₋⟩ = -0.8 * |0⟩ + 0.6 * |1⟩.\n operation StatePrep_IsQubitPsiPlus(q : Qubit, state : Int) : Unit is Adj {\n if state == 0 {\n // convert |0⟩ to |ψ₋⟩\n X(q);\n Ry(2.0 * ArcTan2(0.8, 0.6), q);\n } else {\n // convert |0⟩ to |ψ₊⟩\n Ry(2.0 * ArcTan2(0.8, 0.6), q);\n }\n }\n\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishTwoStates_SingleQubit(\n StatePrep_IsQubitPsiPlus,\n Kata.IsQubitPsiPlus,\n [\"|ψ₋⟩\", \"|ψ₊⟩\"],\n false);\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n isCorrect\n }\n\n}\n"
9194
+ "code": "namespace Kata.Verification {\n import Std.Math.*;\n import KatasUtils.*;\n\n // Distinguish specific orthogonal states\n // |ψ₊⟩ = 0.6 * |0⟩ + 0.8 * |1⟩,\n // |ψ₋⟩ = -0.8 * |0⟩ + 0.6 * |1⟩.\n operation StatePrep_IsQubitPsiPlus(q : Qubit, state : Int) : Unit is Adj {\n if state == 0 {\n // convert |0⟩ to |ψ₋⟩\n X(q);\n Ry(2.0 * ArcTan2(0.8, 0.6), q);\n } else {\n // convert |0⟩ to |ψ₊⟩\n Ry(2.0 * ArcTan2(0.8, 0.6), q);\n }\n }\n\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishTwoStates_SingleQubit(\n StatePrep_IsQubitPsiPlus,\n Kata.IsQubitPsiPlus,\n [\"|ψ₋⟩\", \"|ψ₊⟩\"],\n false\n );\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n isCorrect\n }\n\n}\n"
9195
9195
  },
9196
9196
  {
9197
9197
  "id": "single_qubit_measurements__distinguish_orthogonal_states_2__Verification.qs",
9198
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Katas;\n\n // Distinguish states |A❭ and |B❭\n // |A⟩ = cos(alpha) * |0⟩ - i sin(alpha) * |1⟩,\n // |B⟩ = - i sin(alpha) * |0⟩ + cos(alpha) * |1⟩.\n operation StatePrep_IsQubitA(alpha : Double, q : Qubit, state : Int) : Unit is Adj {\n if state == 0 {\n // convert |0⟩ to |B⟩\n X(q);\n Rx(2.0 * alpha, q);\n } else {\n // convert |0⟩ to |A⟩\n Rx(2.0 * alpha, q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for i in 0 .. 10 {\n let alpha = (PI() * IntAsDouble(i)) / 10.0;\n let isCorrect = DistinguishTwoStates_SingleQubit(\n StatePrep_IsQubitA(alpha, _, _),\n Kata.IsQubitA(alpha, _),\n [$\"|B⟩ = -i sin({i}π/10)|0⟩ + cos({i}π/10)|1⟩\", $\"|A⟩ = cos({i}π/10)|0⟩ + i sin({i}π/10)|1⟩\"],\n false);\n if not isCorrect {\n Message(\"Incorrect.\");\n return false;\n }\n }\n Message(\"Correct!\");\n true\n }\n\n}\n"
9198
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import Std.Math.*;\n import KatasUtils.*;\n\n // Distinguish states |A❭ and |B❭\n // |A⟩ = cos(alpha) * |0⟩ - i sin(alpha) * |1⟩,\n // |B⟩ = - i sin(alpha) * |0⟩ + cos(alpha) * |1⟩.\n operation StatePrep_IsQubitA(alpha : Double, q : Qubit, state : Int) : Unit is Adj {\n if state == 0 {\n // convert |0⟩ to |B⟩\n X(q);\n Rx(2.0 * alpha, q);\n } else {\n // convert |0⟩ to |A⟩\n Rx(2.0 * alpha, q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for i in 0..10 {\n let alpha = (PI() * IntAsDouble(i)) / 10.0;\n let isCorrect = DistinguishTwoStates_SingleQubit(\n StatePrep_IsQubitA(alpha, _, _),\n Kata.IsQubitA(alpha, _),\n [$\"|B⟩ = -i sin({i}π/10)|0⟩ + cos({i}π/10)|1⟩\", $\"|A⟩ = cos({i}π/10)|0⟩ + i sin({i}π/10)|1⟩\"],\n false\n );\n if not isCorrect {\n Message(\"Incorrect.\");\n return false;\n }\n }\n Message(\"Correct!\");\n true\n }\n\n}\n"
9199
9199
  },
9200
9200
  {
9201
9201
  "id": "single_qubit_measurements__a_b_basis_measurements__Verification.qs",
9202
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Katas;\n\n // Measure state in {|A❭, |B❭} basis\n // |A⟩ = cos(alpha) * |0⟩ - i sin(alpha) * |1⟩,\n // |B⟩ = - i sin(alpha) * |0⟩ + cos(alpha) * |1⟩.\n operation StatePrep_IsQubitA (alpha : Double, q : Qubit, state : Int) : Unit is Adj {\n if state == 0 {\n // convert |0⟩ to |B⟩\n X(q);\n Rx(2.0 * alpha, q);\n } else {\n // convert |0⟩ to |A⟩\n Rx(2.0 * alpha, q);\n }\n }\n\n // We can use the StatePrep_IsQubitA operation for the testing\n operation CheckSolution() : Bool {\n for i in 0 .. 10 {\n let alpha = (PI() * IntAsDouble(i)) / 10.0;\n let isCorrect = DistinguishTwoStates_SingleQubit(\n StatePrep_IsQubitA(alpha, _, _),\n q => Kata.MeasureInABBasis(alpha, q) == Zero,\n [$\"|B⟩=(-i sin({i}π/10)|0⟩ + cos({i}π/10)|1⟩)\", $\"|A⟩=(cos({i}π/10)|0⟩ + i sin({i}π/10)|1⟩)\"],\n true);\n if not isCorrect {\n let precision = 3;\n Message($\"Test fails for alpha={DoubleAsStringWithPrecision(alpha, precision)}\");\n return false;\n }\n }\n Message(\"Correct!\");\n true\n }\n\n}\n"
9202
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import Std.Math.*;\n import KatasUtils.*;\n\n // Measure state in {|A❭, |B❭} basis\n // |A⟩ = cos(alpha) * |0⟩ - i sin(alpha) * |1⟩,\n // |B⟩ = - i sin(alpha) * |0⟩ + cos(alpha) * |1⟩.\n operation StatePrep_IsQubitA(alpha : Double, q : Qubit, state : Int) : Unit is Adj {\n if state == 0 {\n // convert |0⟩ to |B⟩\n X(q);\n Rx(2.0 * alpha, q);\n } else {\n // convert |0⟩ to |A⟩\n Rx(2.0 * alpha, q);\n }\n }\n\n // We can use the StatePrep_IsQubitA operation for the testing\n operation CheckSolution() : Bool {\n for i in 0..10 {\n let alpha = (PI() * IntAsDouble(i)) / 10.0;\n let isCorrect = DistinguishTwoStates_SingleQubit(\n StatePrep_IsQubitA(alpha, _, _),\n q => Kata.MeasureInABBasis(alpha, q) == Zero,\n [$\"|B⟩=(-i sin({i}π/10)|0⟩ + cos({i}π/10)|1⟩)\", $\"|A⟩=(cos({i}π/10)|0⟩ + i sin({i}π/10)|1⟩)\"],\n true\n );\n if not isCorrect {\n let precision = 3;\n Message($\"Test fails for alpha={DoubleAsStringWithPrecision(alpha, precision)}\");\n return false;\n }\n }\n Message(\"Correct!\");\n true\n }\n\n}\n"
9203
9203
  },
9204
9204
  {
9205
9205
  "id": "multi_qubit_measurements__full_measurements__Verification.qs",
9206
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n // Distinguish four basis states\n operation StatePrep_BasisStateMeasurement(qs : Qubit[], state : Int, dummyVar : Double) : Unit is Adj {\n if state / 2 == 1 {\n // |10⟩ or |11⟩\n X(qs[0]);\n }\n if state % 2 == 1 {\n // |01⟩ or |11⟩\n X(qs[1]);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishStates_MultiQubit(\n 2,\n 4,\n StatePrep_BasisStateMeasurement,\n Kata.BasisStateMeasurement,\n false,\n [\"|00⟩\", \"|01⟩\", \"|10⟩\", \"|11⟩\"]);\n\n if (isCorrect) {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n\n isCorrect\n }\n}\n"
9206
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n // Distinguish four basis states\n operation StatePrep_BasisStateMeasurement(qs : Qubit[], state : Int, dummyVar : Double) : Unit is Adj {\n if state / 2 == 1 {\n // |10⟩ or |11⟩\n X(qs[0]);\n }\n if state % 2 == 1 {\n // |01⟩ or |11⟩\n X(qs[1]);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishStates_MultiQubit(\n 2,\n 4,\n StatePrep_BasisStateMeasurement,\n Kata.BasisStateMeasurement,\n false,\n [\"|00⟩\", \"|01⟩\", \"|10⟩\", \"|11⟩\"]\n );\n\n if (isCorrect) {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n\n isCorrect\n }\n}\n"
9207
9207
  },
9208
9208
  {
9209
9209
  "id": "multi_qubit_measurements__partial_measurements_for_system__Verification.qs",
9210
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n // Distinguish orthogonal states using partial measurements\n operation StatePrep_IsPlusPlusMinus(qs : Qubit[], state : Int, dummyVar : Double) : Unit is Adj {\n if state == 0 {\n // Prepare the state |++-⟩\n X(qs[2]);\n } else {\n // Prepare the state |---⟩\n ApplyToEachA(X, qs);\n }\n ApplyToEachA(H, qs);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishStates_MultiQubit(\n 3,\n 2,\n StatePrep_IsPlusPlusMinus,\n Kata.IsPlusPlusMinus,\n true,\n [\"|++-⟩\", \"|---⟩\"]);\n if (isCorrect) {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n\n isCorrect \n }\n}\n"
9210
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n // Distinguish orthogonal states using partial measurements\n operation StatePrep_IsPlusPlusMinus(qs : Qubit[], state : Int, dummyVar : Double) : Unit is Adj {\n if state == 0 {\n // Prepare the state |++-⟩\n X(qs[2]);\n } else {\n // Prepare the state |---⟩\n ApplyToEachA(X, qs);\n }\n ApplyToEachA(H, qs);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishStates_MultiQubit(\n 3,\n 2,\n StatePrep_IsPlusPlusMinus,\n Kata.IsPlusPlusMinus,\n true,\n [\"|++-⟩\", \"|---⟩\"]\n );\n if (isCorrect) {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n\n isCorrect\n }\n}\n"
9211
9211
  },
9212
9212
  {
9213
9213
  "id": "multi_qubit_measurements__state_modification__Verification.qs",
9214
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Math;\n\n // State selection using partial measurements\n operation StateInitialize_StateSelection(alpha : Double, qs : Qubit[]) : Unit {\n // Prepare the state to be input to the testImplementation\n // set the second qubit in a superposition a |0⟩ + b|1⟩\n // with a = cos alpha, b = sin alpha\n Ry(2.0 * alpha, qs[1]);\n\n H(qs[0]);\n // Apply CX gate\n CX(qs[0], qs[1]);\n }\n\n // Prepare the expected state of the second qubit for the exercise.\n operation StatePrepare_StateSelection(alpha : Double, ind : Int, q : Qubit) : Unit is Adj {\n // set the second qubit in a superposition a|0⟩ + b|1⟩\n // with a = cos alpha, b = sin alpha\n Ry(2.0 * alpha, q);\n if ind == 1 {\n // change the state to b|0⟩ + a|1⟩\n X(q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n use qs = Qubit[2];\n for i in 0 .. 5 {\n let alpha = (PI() * IntAsDouble(i)) / 5.0;\n let params = $\"a = {Cos(alpha)}, b = {Sin(alpha)}\";\n\n for ind in 0 .. 1 {\n // Prepare the state to be input to the testImplementation\n StateInitialize_StateSelection(alpha, qs);\n\n Kata.StateSelection(qs, ind);\n\n // Apply adjoint of state preparation operation\n Adjoint StatePrepare_StateSelection(alpha, ind, qs[1]);\n\n // We only care about the state of the second qubit; \n // if it's still entangled with the first one or not in zero state, this check will fail.\n if not CheckZero(qs[1]) {\n ResetAll(qs);\n Message(\"Incorrect.\");\n Message($\"The state of the second qubit for {params}, ind = {ind} does not match expectation.\");\n return false;\n }\n\n Reset(qs[0]);\n }\n }\n Message(\"Correct!\");\n true\n }\n\n}\n"
9214
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import Std.Diagnostics.*;\n import Std.Math.*;\n\n // State selection using partial measurements\n operation StateInitialize_StateSelection(alpha : Double, qs : Qubit[]) : Unit {\n // Prepare the state to be input to the testImplementation\n // set the second qubit in a superposition a |0⟩ + b|1⟩\n // with a = cos alpha, b = sin alpha\n Ry(2.0 * alpha, qs[1]);\n\n H(qs[0]);\n // Apply CX gate\n CX(qs[0], qs[1]);\n }\n\n // Prepare the expected state of the second qubit for the exercise.\n operation StatePrepare_StateSelection(alpha : Double, ind : Int, q : Qubit) : Unit is Adj {\n // set the second qubit in a superposition a|0⟩ + b|1⟩\n // with a = cos alpha, b = sin alpha\n Ry(2.0 * alpha, q);\n if ind == 1 {\n // change the state to b|0⟩ + a|1⟩\n X(q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n use qs = Qubit[2];\n for i in 0..5 {\n let alpha = (PI() * IntAsDouble(i)) / 5.0;\n let params = $\"a = {Cos(alpha)}, b = {Sin(alpha)}\";\n\n for ind in 0..1 {\n // Prepare the state to be input to the testImplementation\n StateInitialize_StateSelection(alpha, qs);\n\n Kata.StateSelection(qs, ind);\n\n // Apply adjoint of state preparation operation\n Adjoint StatePrepare_StateSelection(alpha, ind, qs[1]);\n\n // We only care about the state of the second qubit;\n // if it's still entangled with the first one or not in zero state, this check will fail.\n if not CheckZero(qs[1]) {\n ResetAll(qs);\n Message(\"Incorrect.\");\n Message($\"The state of the second qubit for {params}, ind = {ind} does not match expectation.\");\n return false;\n }\n\n Reset(qs[0]);\n }\n }\n Message(\"Correct!\");\n true\n }\n\n}\n"
9215
9215
  },
9216
9216
  {
9217
9217
  "id": "multi_qubit_measurements__state_preparation__Verification.qs",
9218
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n // Reference solution that does not use measurements (to be adjointable)\n operation StatePrep_Rotations(qs : Qubit[]) : Unit is Adj {\n // Rotate first qubit to (sqrt(2) |0⟩ + |1⟩) / sqrt(3)\n let theta = ArcSin(1.0 / Sqrt(3.0));\n Ry(2.0 * theta, qs[0]);\n\n // Split the state sqrt(2) |0⟩ ⊗ |0⟩ into |00⟩ + |01⟩\n ApplyControlledOnInt(0, H, [qs[0]], qs[1]);\n }\n\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(Kata.PostSelection, StatePrep_Rotations, 2)\n }\n}\n"
9218
+ "code": "namespace Kata.Verification {\n import Std.Diagnostics.*;\n import KatasUtils.*;\n import Std.Math.*;\n\n // Reference solution that does not use measurements (to be adjointable)\n operation StatePrep_Rotations(qs : Qubit[]) : Unit is Adj {\n // Rotate first qubit to (sqrt(2) |0⟩ + |1⟩) / sqrt(3)\n let theta = ArcSin(1.0 / Sqrt(3.0));\n Ry(2.0 * theta, qs[0]);\n\n // Split the state sqrt(2) |0⟩ ⊗ |0⟩ into |00⟩ + |01⟩\n ApplyControlledOnInt(0, H, [qs[0]], qs[1]);\n }\n\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckOperationsEquivalenceOnZeroStateWithFeedback(Kata.PostSelection, StatePrep_Rotations, 2)\n }\n}\n"
9219
9219
  },
9220
9220
  {
9221
9221
  "id": "multi_qubit_measurements__joint_measurements__Verification.qs",
9222
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n // Two qubit parity Measurement\n operation StatePrep_ParityMeasurement(qs : Qubit[], state : Int, alpha : Double) : Unit is Adj {\n // prep cos(alpha) * |0..0⟩ + sin(alpha) * |1..1⟩\n Ry(2.0 * alpha, qs[0]);\n CNOT(qs[0], qs[1]);\n\n if state == 1 {\n // flip the state of the first qubit\n X(qs[0]);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishStates_MultiQubit(\n 2,\n 2,\n StatePrep_ParityMeasurement,\n Kata.ParityMeasurement,\n true,\n [\"α|00⟩ + β|11⟩\", \"α|01⟩ + β|10⟩\"]);\n\n if (isCorrect) {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n\n isCorrect\n }\n}\n"
9222
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n // Two qubit parity Measurement\n operation StatePrep_ParityMeasurement(qs : Qubit[], state : Int, alpha : Double) : Unit is Adj {\n // prep cos(alpha) * |0..0⟩ + sin(alpha) * |1..1⟩\n Ry(2.0 * alpha, qs[0]);\n CNOT(qs[0], qs[1]);\n\n if state == 1 {\n // flip the state of the first qubit\n X(qs[0]);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishStates_MultiQubit(\n 2,\n 2,\n StatePrep_ParityMeasurement,\n Kata.ParityMeasurement,\n true,\n [\"α|00⟩ + β|11⟩\", \"α|01⟩ + β|10⟩\"]\n );\n\n if (isCorrect) {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n\n isCorrect\n }\n}\n"
9223
9223
  },
9224
9224
  {
9225
9225
  "id": "distinguishing_states__zero_one__Verification.qs",
9226
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Katas;\n\n operation StatePrep_IsQubitOne (q : Qubit, state : Int) : Unit is Adj {\n if state == 1 {\n // convert |0⟩ to |1⟩\n X(q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishTwoStates_SingleQubit(\n StatePrep_IsQubitOne,\n Kata.IsQubitOne,\n [\"|0⟩\", \"|1⟩\"],\n false);\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n isCorrect\n }\n\n}\n"
9226
+ "code": "namespace Kata.Verification {\n import Std.Diagnostics.*;\n import KatasUtils.*;\n\n operation StatePrep_IsQubitOne(q : Qubit, state : Int) : Unit is Adj {\n if state == 1 {\n // convert |0⟩ to |1⟩\n X(q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishTwoStates_SingleQubit(\n StatePrep_IsQubitOne,\n Kata.IsQubitOne,\n [\"|0⟩\", \"|1⟩\"],\n false\n );\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n isCorrect\n }\n\n}\n"
9227
9227
  },
9228
9228
  {
9229
9229
  "id": "distinguishing_states__Common.qs",
9230
- "code": "namespace Kata.Verification{\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Intrinsic;\n open Microsoft.Quantum.Math;\n\n operation StatePrep_BasisStateMeasurement(\n qs : Qubit[],\n state : Int,\n dummyVar : Double\n ) : Unit is Adj {\n if state / 2 == 1 {\n // |10⟩ or |11⟩\n X(qs[0]);\n }\n\n if state % 2 == 1 {\n // |01⟩ or |11⟩\n X(qs[1]);\n }\n }\n\n operation WState_Arbitrary_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n let N = Length(qs);\n\n if N == 1 {\n // base case of recursion: |1⟩\n X(qs[0]);\n } else {\n // |W_N⟩ = |0⟩|W_(N-1)⟩ + |1⟩|0...0⟩\n // do a rotation on the first qubit to split it into |0⟩ and |1⟩ with proper weights\n // |0⟩ -> sqrt((N-1)/N) |0⟩ + 1/sqrt(N) |1⟩\n let theta = ArcSin(1.0 / Sqrt(IntAsDouble(N)));\n Ry(2.0 * theta, qs[0]);\n\n // do a zero-controlled W-state generation for qubits 1..N-1\n X(qs[0]);\n Controlled WState_Arbitrary_Reference(qs[0..0], qs[1..N - 1]);\n X(qs[0]);\n }\n }\n\n function StatePrep_FindFirstDiff (\n bits1 : Bool[],\n bits2 : Bool[]\n ) : Int {\n for i in 0 .. Length(bits1) - 1 {\n if bits1[i] != bits2[i] {\n return i;\n }\n }\n\n return -1;\n }\n\n operation StatePrep_SuperpositionMeasurement (\n qs : Qubit[],\n bits1 : Bool[][],\n bits2 : Bool[][],\n state : Int,\n dummyVar: Double\n ) : Unit is Adj {\n let bits = state == 0 ? bits1 | bits2;\n StatePrep_BitstringSuperposition(qs, bits);\n }\n\n // A combination of tasks 14 and 15 from the Superposition kata\n operation StatePrep_BitstringSuperposition (\n qs : Qubit[],\n bits : Bool[][]\n ) : Unit is Adj + Ctl {\n let L = Length(bits);\n Fact(L == 1 or L == 2 or L == 4, \"State preparation only supports arrays of 1, 2 or 4 bit strings.\");\n if L == 1 {\n for i in 0 .. Length(qs) - 1 {\n if bits[0][i] {\n X(qs[i]);\n }\n }\n }\n if L == 2 {\n // find the index of the first bit at which the bit strings are different\n let firstDiff = StatePrep_FindFirstDiff(bits[0], bits[1]);\n\n // Hadamard corresponding qubit to create superposition\n H(qs[firstDiff]);\n\n // iterate through the bit strings again setting the final state of qubits\n for i in 0 .. Length(qs) - 1 {\n if bits[0][i] == bits[1][i] {\n // if two bits are the same, apply X or nothing\n if bits[0][i] {\n X(qs[i]);\n }\n } else {\n // if two bits are different, set their difference using CNOT\n if i > firstDiff {\n CNOT(qs[firstDiff], qs[i]);\n if bits[0][i] != bits[0][firstDiff] {\n X(qs[i]);\n }\n }\n }\n }\n }\n if L == 4 {\n let N = Length(qs);\n\n use anc = Qubit[2];\n // Put two ancillas into equal superposition of 2-qubit basis states\n ApplyToEachCA(H, anc);\n\n // Set up the right pattern on the main qubits with control on ancillas\n for i in 0 .. 3 {\n for j in 0 .. N - 1 {\n if bits[i][j] {\n ApplyControlledOnInt(i, X, anc, qs[j]);\n }\n }\n }\n\n // Uncompute the ancillas, using patterns on main qubits as control\n for i in 0 .. 3 {\n if i % 2 == 1 {\n ApplyControlledOnBitString(bits[i], X, qs, anc[0]);\n }\n if i / 2 == 1 {\n ApplyControlledOnBitString(bits[i], X, qs, anc[1]);\n }\n }\n }\n }\n}\n"
9230
+ "code": "namespace Kata.Verification {\n import Std.Arrays.*;\n import Std.Convert.*;\n import Std.Diagnostics.*;\n import Std.Math.*;\n\n operation StatePrep_BasisStateMeasurement(\n qs : Qubit[],\n state : Int,\n dummyVar : Double\n ) : Unit is Adj {\n if state / 2 == 1 {\n // |10⟩ or |11⟩\n X(qs[0]);\n }\n\n if state % 2 == 1 {\n // |01⟩ or |11⟩\n X(qs[1]);\n }\n }\n\n operation WState_Arbitrary_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n let N = Length(qs);\n\n if N == 1 {\n // base case of recursion: |1⟩\n X(qs[0]);\n } else {\n // |W_N⟩ = |0⟩|W_(N-1)⟩ + |1⟩|0...0⟩\n // do a rotation on the first qubit to split it into |0⟩ and |1⟩ with proper weights\n // |0⟩ -> sqrt((N-1)/N) |0⟩ + 1/sqrt(N) |1⟩\n let theta = ArcSin(1.0 / Sqrt(IntAsDouble(N)));\n Ry(2.0 * theta, qs[0]);\n\n // do a zero-controlled W-state generation for qubits 1..N-1\n X(qs[0]);\n Controlled WState_Arbitrary_Reference(qs[0..0], qs[1..N - 1]);\n X(qs[0]);\n }\n }\n\n function StatePrep_FindFirstDiff(\n bits1 : Bool[],\n bits2 : Bool[]\n ) : Int {\n for i in 0..Length(bits1) - 1 {\n if bits1[i] != bits2[i] {\n return i;\n }\n }\n\n return -1;\n }\n\n operation StatePrep_SuperpositionMeasurement(\n qs : Qubit[],\n bits1 : Bool[][],\n bits2 : Bool[][],\n state : Int,\n dummyVar : Double\n ) : Unit is Adj {\n let bits = state == 0 ? bits1 | bits2;\n StatePrep_BitstringSuperposition(qs, bits);\n }\n\n // A combination of tasks 14 and 15 from the Superposition kata\n operation StatePrep_BitstringSuperposition(\n qs : Qubit[],\n bits : Bool[][]\n ) : Unit is Adj + Ctl {\n let L = Length(bits);\n Fact(L == 1 or L == 2 or L == 4, \"State preparation only supports arrays of 1, 2 or 4 bit strings.\");\n if L == 1 {\n for i in 0..Length(qs) - 1 {\n if bits[0][i] {\n X(qs[i]);\n }\n }\n }\n if L == 2 {\n // find the index of the first bit at which the bit strings are different\n let firstDiff = StatePrep_FindFirstDiff(bits[0], bits[1]);\n\n // Hadamard corresponding qubit to create superposition\n H(qs[firstDiff]);\n\n // iterate through the bit strings again setting the final state of qubits\n for i in 0..Length(qs) - 1 {\n if bits[0][i] == bits[1][i] {\n // if two bits are the same, apply X or nothing\n if bits[0][i] {\n X(qs[i]);\n }\n } else {\n // if two bits are different, set their difference using CNOT\n if i > firstDiff {\n CNOT(qs[firstDiff], qs[i]);\n if bits[0][i] != bits[0][firstDiff] {\n X(qs[i]);\n }\n }\n }\n }\n }\n if L == 4 {\n let N = Length(qs);\n\n use anc = Qubit[2];\n // Put two ancillas into equal superposition of 2-qubit basis states\n ApplyToEachCA(H, anc);\n\n // Set up the right pattern on the main qubits with control on ancillas\n for i in 0..3 {\n for j in 0..N - 1 {\n if bits[i][j] {\n ApplyControlledOnInt(i, X, anc, qs[j]);\n }\n }\n }\n\n // Uncompute the ancillas, using patterns on main qubits as control\n for i in 0..3 {\n if i % 2 == 1 {\n ApplyControlledOnBitString(bits[i], X, qs, anc[0]);\n }\n if i / 2 == 1 {\n ApplyControlledOnBitString(bits[i], X, qs, anc[1]);\n }\n }\n }\n }\n}\n"
9231
9231
  },
9232
9232
  {
9233
9233
  "id": "distinguishing_states__plus_minus__Verification.qs",
9234
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation StatePrep_IsQubitPlus(q : Qubit, state : Int) : Unit is Adj {\n if state == 1 {\n // convert |0⟩ to |+⟩\n H(q);\n } else {\n // convert |0⟩ to |-⟩\n X(q);\n H(q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishTwoStates_SingleQubit(\n StatePrep_IsQubitPlus,\n Kata.IsQubitPlus,\n [\"|-⟩\", \"|+⟩\"],\n false\n );\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n isCorrect\n }\n}\n"
9234
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation StatePrep_IsQubitPlus(q : Qubit, state : Int) : Unit is Adj {\n if state == 1 {\n // convert |0⟩ to |+⟩\n H(q);\n } else {\n // convert |0⟩ to |-⟩\n X(q);\n H(q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishTwoStates_SingleQubit(\n StatePrep_IsQubitPlus,\n Kata.IsQubitPlus,\n [\"|-⟩\", \"|+⟩\"],\n false\n );\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n isCorrect\n }\n}\n"
9235
9235
  },
9236
9236
  {
9237
9237
  "id": "distinguishing_states__a_b__Verification.qs",
9238
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Katas;\n\n // |A⟩ = cos(alpha) * |0⟩ + sin(alpha) * |1⟩,\n // |B⟩ = - sin(alpha) * |0⟩ + cos(alpha) * |1⟩.\n operation StatePrep_IsQubitA(alpha : Double, q : Qubit, state : Int) : Unit is Adj {\n if state == 0 {\n // convert |0⟩ to |B⟩\n X(q);\n Ry(2.0 * alpha, q);\n } else {\n // convert |0⟩ to |A⟩\n Ry(2.0 * alpha, q);\n }\n }\n\n // We can use the StatePrep_IsQubitA operation for the testing\n operation CheckSolution() : Bool {\n for i in 0 .. 10 {\n let alpha = (PI() * IntAsDouble(i)) / 10.0;\n let isCorrect = DistinguishTwoStates_SingleQubit(\n StatePrep_IsQubitA(alpha, _, _),\n Kata.IsQubitA(alpha, _),\n [$\"|B⟩=(-sin({i}π/10)|0⟩ + cos({i}π/10)|1⟩)\", $\"|A⟩=(cos({i}π/10)|0⟩ + sin({i}π/10)|1⟩)\"],\n false\n );\n\n if not isCorrect {\n let precision = 3;\n Message($\"Test fails for alpha={DoubleAsStringWithPrecision(alpha, precision)}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9238
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import Std.Math.*;\n import KatasUtils.*;\n\n // |A⟩ = cos(alpha) * |0⟩ + sin(alpha) * |1⟩,\n // |B⟩ = - sin(alpha) * |0⟩ + cos(alpha) * |1⟩.\n operation StatePrep_IsQubitA(alpha : Double, q : Qubit, state : Int) : Unit is Adj {\n if state == 0 {\n // convert |0⟩ to |B⟩\n X(q);\n Ry(2.0 * alpha, q);\n } else {\n // convert |0⟩ to |A⟩\n Ry(2.0 * alpha, q);\n }\n }\n\n // We can use the StatePrep_IsQubitA operation for the testing\n operation CheckSolution() : Bool {\n for i in 0..10 {\n let alpha = (PI() * IntAsDouble(i)) / 10.0;\n let isCorrect = DistinguishTwoStates_SingleQubit(\n StatePrep_IsQubitA(alpha, _, _),\n Kata.IsQubitA(alpha, _),\n [$\"|B⟩=(-sin({i}π/10)|0⟩ + cos({i}π/10)|1⟩)\", $\"|A⟩=(cos({i}π/10)|0⟩ + sin({i}π/10)|1⟩)\"],\n false\n );\n\n if not isCorrect {\n let precision = 3;\n Message($\"Test fails for alpha={DoubleAsStringWithPrecision(alpha, precision)}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9239
9239
  },
9240
9240
  {
9241
9241
  "id": "distinguishing_states__zerozero_oneone__Verification.qs",
9242
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation StatePrep_ZeroZeroOrOneOne(qs : Qubit[], state : Int, dummyVar : Double) : Unit is Adj {\n if state == 1 {\n // |11⟩\n X(qs[0]);\n X(qs[1]);\n }\n }\n\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishStates_MultiQubit(2, 2, StatePrep_ZeroZeroOrOneOne, Kata.ZeroZeroOrOneOne, true, [\"|00⟩\", \"|11⟩\"]);\n if (isCorrect) {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n\n isCorrect\n }\n}\n"
9242
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation StatePrep_ZeroZeroOrOneOne(qs : Qubit[], state : Int, dummyVar : Double) : Unit is Adj {\n if state == 1 {\n // |11⟩\n X(qs[0]);\n X(qs[1]);\n }\n }\n\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishStates_MultiQubit(2, 2, StatePrep_ZeroZeroOrOneOne, Kata.ZeroZeroOrOneOne, true, [\"|00⟩\", \"|11⟩\"]);\n if (isCorrect) {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n\n isCorrect\n }\n}\n"
9243
9243
  },
9244
9244
  {
9245
9245
  "id": "distinguishing_states__four_basis_states__Verification.qs",
9246
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Katas;\n\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishStates_MultiQubit(2, 4, StatePrep_BasisStateMeasurement, Kata.BasisStateMeasurement, false, [\"|00⟩\", \"|01⟩\", \"|10⟩\", \"|11⟩\"]);\n if (isCorrect) {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n\n isCorrect\n }\n}\n"
9246
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import Std.Math.*;\n import KatasUtils.*;\n\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishStates_MultiQubit(2, 4, StatePrep_BasisStateMeasurement, Kata.BasisStateMeasurement, false, [\"|00⟩\", \"|01⟩\", \"|10⟩\", \"|11⟩\"]);\n if (isCorrect) {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n\n isCorrect\n }\n}\n"
9247
9247
  },
9248
9248
  {
9249
9249
  "id": "distinguishing_states__two_basis_states_bit_strings__Verification.qs",
9250
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation StatePrep_Bitstring(\n qs : Qubit[],\n bits : Bool[]\n ) : Unit is Adj {\n for i in 0 .. Length(qs) - 1 {\n if bits[i] {\n X(qs[i]);\n }\n }\n }\n\n operation StatePrep_TwoBitstringsMeasurement(\n qs : Qubit[],\n bits1 : Bool[],\n bits2 : Bool[],\n state : Int,\n dummyVar : Double\n ) : Unit is Adj {\n let bits = state == 0 ? bits1 | bits2;\n StatePrep_Bitstring(qs, bits);\n }\n\n operation CheckTwoBitstringsMeasurement(b1 : Bool[], b2 : Bool[]) : Bool {\n let stateNames = [BoolArrayAsKetState(b1), BoolArrayAsKetState(b2)];\n let isCorrect = DistinguishStates_MultiQubit(\n Length(b1),\n 2,\n StatePrep_TwoBitstringsMeasurement(_, b1, b2, _, _),\n Kata.TwoBitstringsMeasurement(_, b1, b2),\n false,\n stateNames\n );\n\n if not isCorrect {\n Message($\"Incorrect for [{stateNames[0]}, {stateNames[1]}].\");\n }\n\n return isCorrect;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for (b1, b2) in [\n ([false, true], [true, false]),\n ([true, true, false], [false, true, true]),\n ([false, true, true, false], [false, true, true, true]),\n ([true, false, false, false], [true, false, true, true])\n ] {\n if not CheckTwoBitstringsMeasurement(b1, b2) {\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9250
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation StatePrep_Bitstring(\n qs : Qubit[],\n bits : Bool[]\n ) : Unit is Adj {\n for i in 0..Length(qs) - 1 {\n if bits[i] {\n X(qs[i]);\n }\n }\n }\n\n operation StatePrep_TwoBitstringsMeasurement(\n qs : Qubit[],\n bits1 : Bool[],\n bits2 : Bool[],\n state : Int,\n dummyVar : Double\n ) : Unit is Adj {\n let bits = state == 0 ? bits1 | bits2;\n StatePrep_Bitstring(qs, bits);\n }\n\n operation CheckTwoBitstringsMeasurement(b1 : Bool[], b2 : Bool[]) : Bool {\n let stateNames = [BoolArrayAsKetState(b1), BoolArrayAsKetState(b2)];\n let isCorrect = DistinguishStates_MultiQubit(\n Length(b1),\n 2,\n StatePrep_TwoBitstringsMeasurement(_, b1, b2, _, _),\n Kata.TwoBitstringsMeasurement(_, b1, b2),\n false,\n stateNames\n );\n\n if not isCorrect {\n Message($\"Incorrect for [{stateNames[0]}, {stateNames[1]}].\");\n }\n\n return isCorrect;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for (b1, b2) in [\n ([false, true], [true, false]),\n ([true, true, false], [false, true, true]),\n ([false, true, true, false], [false, true, true, true]),\n ([true, false, false, false], [true, false, true, true])\n ] {\n if not CheckTwoBitstringsMeasurement(b1, b2) {\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9251
9251
  },
9252
9252
  {
9253
9253
  "id": "distinguishing_states__two_superposition_states_bit_strings_one__Verification.qs",
9254
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Katas;\n\n operation CheckSuperpositionBitstringsOneMeasurement (\n nQubits : Int,\n ints1 : Int[],\n ints2 : Int[]\n ): Bool {\n let bits1 = Mapped(IntAsBoolArray(_, nQubits), ints1);\n let bits2 = Mapped(IntAsBoolArray(_, nQubits), ints2);\n\n let stateNames = [IntArrayAsStateName(nQubits, bits1), IntArrayAsStateName(nQubits, bits2)];\n\n let isCorrect = DistinguishStates_MultiQubit(\n nQubits,\n 2,\n StatePrep_SuperpositionMeasurement(_, bits1, bits2, _, _),\n Kata.SuperpositionOneMeasurement(_, bits1, bits2),\n false,\n stateNames\n );\n\n if not isCorrect {\n Message($\"Incorrect for: [{stateNames[0]}, {stateNames[1]}]\")\n }\n\n return isCorrect;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n // note that bit strings in the comments (big endian) are the reverse of the bit strings passed to the solutions (little endian)\n for (n, ints1, ints2) in [\n (2, [2], [1]), // [10] vs [01]\n (2, [2, 3], [1, 0]), // [10,11] vs [01,00]\n (2, [2], [1, 0]), // [10] vs [01,00]\n (2, [0, 2], [3]), // [00,10] vs [11]\n (3, [5, 7], [2]), // [101,111] vs [010]\n (4, [15, 7], [0, 8]), // [1111,0111] vs [0000,1000]\n (4, [15, 7], [0, 8, 10, 12]), // [1111,0111] vs [0000,1000,1010,1100]\n (4, [13, 11, 7, 3], [2, 4]), // [1101,1011,0111,0011] vs [0010,0100]\n (5, [30, 14, 10, 6], [1, 17, 21, 25]) // [11110,01110,01010,00110] vs [00001,10001,10101,11001]\n ] {\n if not CheckSuperpositionBitstringsOneMeasurement(n, ints1, ints2) {\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9254
+ "code": "namespace Kata.Verification {\n import Std.Arrays.*;\n import Std.Convert.*;\n import KatasUtils.*;\n\n operation CheckSuperpositionBitstringsOneMeasurement(\n nQubits : Int,\n ints1 : Int[],\n ints2 : Int[]\n ) : Bool {\n let bits1 = Mapped(IntAsBoolArray(_, nQubits), ints1);\n let bits2 = Mapped(IntAsBoolArray(_, nQubits), ints2);\n\n let stateNames = [IntArrayAsStateName(nQubits, bits1), IntArrayAsStateName(nQubits, bits2)];\n\n let isCorrect = DistinguishStates_MultiQubit(\n nQubits,\n 2,\n StatePrep_SuperpositionMeasurement(_, bits1, bits2, _, _),\n Kata.SuperpositionOneMeasurement(_, bits1, bits2),\n false,\n stateNames\n );\n\n if not isCorrect {\n Message($\"Incorrect for: [{stateNames[0]}, {stateNames[1]}]\")\n }\n\n return isCorrect;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n // note that bit strings in the comments (big endian) are the reverse of the bit strings passed to the solutions (little endian)\n for (n, ints1, ints2) in [\n (2, [2], [1]), // [10] vs [01]\n (2, [2, 3], [1, 0]), // [10,11] vs [01,00]\n (2, [2], [1, 0]), // [10] vs [01,00]\n (2, [0, 2], [3]), // [00,10] vs [11]\n (3, [5, 7], [2]), // [101,111] vs [010]\n (4, [15, 7], [0, 8]), // [1111,0111] vs [0000,1000]\n (4, [15, 7], [0, 8, 10, 12]), // [1111,0111] vs [0000,1000,1010,1100]\n (4, [13, 11, 7, 3], [2, 4]), // [1101,1011,0111,0011] vs [0010,0100]\n (5, [30, 14, 10, 6], [1, 17, 21, 25]) // [11110,01110,01010,00110] vs [00001,10001,10101,11001]\n ] {\n if not CheckSuperpositionBitstringsOneMeasurement(n, ints1, ints2) {\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9255
9255
  },
9256
9256
  {
9257
9257
  "id": "distinguishing_states__two_superposition_states_bit_strings__Verification.qs",
9258
- "code": "namespace Kata.Verification{\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Katas;\n\n operation CheckSuperpositionBitstringsMeasurement (\n nQubits : Int,\n ints1 : Int[],\n ints2 : Int[]\n ): Bool {\n let bits1 = Mapped(IntAsBoolArray(_, nQubits), ints1);\n let bits2 = Mapped(IntAsBoolArray(_, nQubits), ints2);\n\n let stateNames = [IntArrayAsStateName(nQubits, bits1), IntArrayAsStateName(nQubits, bits2)];\n\n let isCorrect = DistinguishStates_MultiQubit(nQubits, 2, StatePrep_SuperpositionMeasurement(_, bits1, bits2, _, _),\n Kata.SuperpositionMeasurement(_, bits1, bits2), false,\n stateNames);\n\n if not isCorrect {\n Message($\"Incorrect for: [{stateNames[0]}, {stateNames[1]}]\")\n }\n\n return isCorrect;\n }\n\n operation CheckSolution () : Bool {\n // note that bit strings in the comments (big endian) are the reverse of the bit strings passed to the solutions (little endian)\n for (n, ints1, ints2) in [\n (2, [2], [1]), // [10] vs [01]\n (2, [2, 1], [3, 0]), // [10,01] vs [11,00]\n (2, [2], [3, 0]), // [10] vs [11,00]\n (2, [1, 2], [3]), // [01,10] vs [11]\n (3, [5, 7], [2]), // [101,111] vs [010]\n (4, [15, 6], [0, 14]), // [1111,0110] vs [0000,1110]\n (4, [15, 7], [0, 8, 10, 13]), // [1111,0111] vs [0000,1000,1010,1101]\n (4, [13, 11, 7, 3], [2, 5]), // [1101,1011,0111,0011] vs [0010,0101]\n (5, [30, 14, 10, 7], [1, 17, 21, 27]) // [11110,01110,01010,00111] vs [00001,10001,10101,11011]\n ] {\n if not CheckSuperpositionBitstringsMeasurement(n, ints1, ints2) {\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9258
+ "code": "namespace Kata.Verification {\n import Std.Arrays.*;\n import Std.Convert.*;\n import KatasUtils.*;\n\n operation CheckSuperpositionBitstringsMeasurement(\n nQubits : Int,\n ints1 : Int[],\n ints2 : Int[]\n ) : Bool {\n let bits1 = Mapped(IntAsBoolArray(_, nQubits), ints1);\n let bits2 = Mapped(IntAsBoolArray(_, nQubits), ints2);\n\n let stateNames = [IntArrayAsStateName(nQubits, bits1), IntArrayAsStateName(nQubits, bits2)];\n\n let isCorrect = DistinguishStates_MultiQubit(\n nQubits,\n 2,\n StatePrep_SuperpositionMeasurement(_, bits1, bits2, _, _),\n Kata.SuperpositionMeasurement(_, bits1, bits2),\n false,\n stateNames\n );\n\n if not isCorrect {\n Message($\"Incorrect for: [{stateNames[0]}, {stateNames[1]}]\")\n }\n\n return isCorrect;\n }\n\n operation CheckSolution() : Bool {\n // note that bit strings in the comments (big endian) are the reverse of the bit strings passed to the solutions (little endian)\n for (n, ints1, ints2) in [\n (2, [2], [1]), // [10] vs [01]\n (2, [2, 1], [3, 0]), // [10,01] vs [11,00]\n (2, [2], [3, 0]), // [10] vs [11,00]\n (2, [1, 2], [3]), // [01,10] vs [11]\n (3, [5, 7], [2]), // [101,111] vs [010]\n (4, [15, 6], [0, 14]), // [1111,0110] vs [0000,1110]\n (4, [15, 7], [0, 8, 10, 13]), // [1111,0111] vs [0000,1000,1010,1101]\n (4, [13, 11, 7, 3], [2, 5]), // [1101,1011,0111,0011] vs [0010,0101]\n (5, [30, 14, 10, 7], [1, 17, 21, 27]) // [11110,01110,01010,00111] vs [00001,10001,10101,11011]\n ] {\n if not CheckSuperpositionBitstringsMeasurement(n, ints1, ints2) {\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9259
9259
  },
9260
9260
  {
9261
9261
  "id": "distinguishing_states__all_zeros_w__Verification.qs",
9262
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation StatePrep_AllZerosOrWState(\n qs : Qubit[],\n state : Int,\n dummyVar : Double\n ) : Unit is Adj {\n if state == 1 {\n // Prepare W state\n WState_Arbitrary_Reference(qs);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for i in 2 .. 6 {\n let isCorrect = DistinguishStates_MultiQubit(\n i, 2,\n StatePrep_AllZerosOrWState,\n Kata.AllZerosOrWState,\n false,\n [\"|0...0⟩\", \"|W⟩\"]);\n\n if not isCorrect {\n Message(\"Incorrect.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
9262
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation StatePrep_AllZerosOrWState(\n qs : Qubit[],\n state : Int,\n dummyVar : Double\n ) : Unit is Adj {\n if state == 1 {\n // Prepare W state\n WState_Arbitrary_Reference(qs);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for i in 2..6 {\n let isCorrect = DistinguishStates_MultiQubit(\n i,\n 2,\n StatePrep_AllZerosOrWState,\n Kata.AllZerosOrWState,\n false,\n [\"|0...0⟩\", \"|W⟩\"]\n );\n\n if not isCorrect {\n Message(\"Incorrect.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
9263
9263
  },
9264
9264
  {
9265
9265
  "id": "distinguishing_states__ghz_w__Verification.qs",
9266
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation GHZ_State_Reference(qs : Qubit[]) : Unit is Adj {\n H(qs[0]);\n for q in qs[1 ... ] {\n CNOT(qs[0], q);\n }\n }\n\n operation StatePrep_GHZOrWState(\n qs : Qubit[],\n state : Int,\n dummyVar : Double\n ) : Unit is Adj {\n if state == 0 {\n // Prepare GHZ state\n GHZ_State_Reference(qs);\n } else {\n // Prepare W state\n WState_Arbitrary_Reference(qs);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n\n for i in 2 .. 6 {\n let isCorrect = DistinguishStates_MultiQubit(\n i, 2,\n StatePrep_GHZOrWState,\n Kata.GHZOrWState,\n false,\n [\"|GHZ⟩\", \"|W⟩\"]);\n\n if not isCorrect {\n Message(\"Incorrect.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
9266
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation GHZ_State_Reference(qs : Qubit[]) : Unit is Adj {\n H(qs[0]);\n for q in qs[1...] {\n CNOT(qs[0], q);\n }\n }\n\n operation StatePrep_GHZOrWState(\n qs : Qubit[],\n state : Int,\n dummyVar : Double\n ) : Unit is Adj {\n if state == 0 {\n // Prepare GHZ state\n GHZ_State_Reference(qs);\n } else {\n // Prepare W state\n WState_Arbitrary_Reference(qs);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n\n for i in 2..6 {\n let isCorrect = DistinguishStates_MultiQubit(\n i,\n 2,\n StatePrep_GHZOrWState,\n Kata.GHZOrWState,\n false,\n [\"|GHZ⟩\", \"|W⟩\"]\n );\n\n if not isCorrect {\n Message(\"Incorrect.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
9267
9267
  },
9268
9268
  {
9269
9269
  "id": "distinguishing_states__four_bell_states__Verification.qs",
9270
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n // 0 - |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2)\n // 1 - |Φ⁻⟩ = (|00⟩ - |11⟩) / sqrt(2)\n // 2 - |Ψ⁺⟩ = (|01⟩ + |10⟩) / sqrt(2)\n // 3 - |Ψ⁻⟩ = (|01⟩ - |10⟩) / sqrt(2)\n operation StatePrep_BellState(\n qs : Qubit[],\n state : Int,\n dummyVar : Double\n ) : Unit is Adj {\n H(qs[0]);\n CNOT(qs[0], qs[1]);\n\n // Now we have |00⟩ + |11⟩ - modify it based on state arg\n if state % 2 == 1 {\n // negative phase\n Z(qs[1]);\n }\n if state / 2 == 1 {\n X(qs[1]);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishStates_MultiQubit(\n 2,\n 4,\n StatePrep_BellState,\n Kata.BellState,\n false,\n [\"|Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2)\", \"|Φ⁻⟩ = (|00⟩ - |11⟩) / sqrt(2)\", \"|Ψ⁺⟩ = (|01⟩ + |10⟩) / sqrt(2)\", \"|Ψ⁻⟩ = (|01⟩ - |10⟩) / sqrt(2)\"]\n );\n\n if not isCorrect {\n Message(\"Incorrect.\");\n } else {\n Message(\"Correct!\");\n }\n\n return isCorrect;\n }\n}\n"
9270
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n // 0 - |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2)\n // 1 - |Φ⁻⟩ = (|00⟩ - |11⟩) / sqrt(2)\n // 2 - |Ψ⁺⟩ = (|01⟩ + |10⟩) / sqrt(2)\n // 3 - |Ψ⁻⟩ = (|01⟩ - |10⟩) / sqrt(2)\n operation StatePrep_BellState(\n qs : Qubit[],\n state : Int,\n dummyVar : Double\n ) : Unit is Adj {\n H(qs[0]);\n CNOT(qs[0], qs[1]);\n\n // Now we have |00⟩ + |11⟩ - modify it based on state arg\n if state % 2 == 1 {\n // negative phase\n Z(qs[1]);\n }\n if state / 2 == 1 {\n X(qs[1]);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishStates_MultiQubit(\n 2,\n 4,\n StatePrep_BellState,\n Kata.BellState,\n false,\n [\"|Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2)\", \"|Φ⁻⟩ = (|00⟩ - |11⟩) / sqrt(2)\", \"|Ψ⁺⟩ = (|01⟩ + |10⟩) / sqrt(2)\", \"|Ψ⁻⟩ = (|01⟩ - |10⟩) / sqrt(2)\"]\n );\n\n if not isCorrect {\n Message(\"Incorrect.\");\n } else {\n Message(\"Correct!\");\n }\n\n return isCorrect;\n }\n}\n"
9271
9271
  },
9272
9272
  {
9273
9273
  "id": "distinguishing_states__four_orthogonal_two_qubit__Verification.qs",
9274
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n // 0 - (|00⟩ + |01⟩ + |10⟩ + |11⟩) / 2\n // 1 - (|00⟩ - |01⟩ + |10⟩ - |11⟩) / 2\n // 2 - (|00⟩ + |01⟩ - |10⟩ - |11⟩) / 2\n // 3 - (|00⟩ - |01⟩ - |10⟩ + |11⟩) / 2\n operation StatePrep_TwoQubitState(\n qs : Qubit[],\n state : Int,\n dummyVar : Double\n ) : Unit is Adj {\n StatePrep_BasisStateMeasurement(qs, state, dummyVar);\n\n H(qs[0]);\n H(qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishStates_MultiQubit(\n 2,\n 4,\n StatePrep_TwoQubitState,\n Kata.TwoQubitState,\n false,\n [\"(|00⟩ + |01⟩ + |10⟩ + |11⟩) / 2\", \"(|00⟩ - |01⟩ + |10⟩ - |11⟩) / 2\", \"(|00⟩ + |01⟩ - |10⟩ - |11⟩) / 2\", \"(|00⟩ - |01⟩ - |10⟩ + |11⟩) / 2\"]\n );\n\n if not isCorrect {\n Message(\"Incorrect.\");\n } else {\n Message(\"Correct!\");\n }\n\n return isCorrect\n }\n}\n"
9274
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n // 0 - (|00⟩ + |01⟩ + |10⟩ + |11⟩) / 2\n // 1 - (|00⟩ - |01⟩ + |10⟩ - |11⟩) / 2\n // 2 - (|00⟩ + |01⟩ - |10⟩ - |11⟩) / 2\n // 3 - (|00⟩ - |01⟩ - |10⟩ + |11⟩) / 2\n operation StatePrep_TwoQubitState(\n qs : Qubit[],\n state : Int,\n dummyVar : Double\n ) : Unit is Adj {\n StatePrep_BasisStateMeasurement(qs, state, dummyVar);\n\n H(qs[0]);\n H(qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishStates_MultiQubit(\n 2,\n 4,\n StatePrep_TwoQubitState,\n Kata.TwoQubitState,\n false,\n [\"(|00⟩ + |01⟩ + |10⟩ + |11⟩) / 2\", \"(|00⟩ - |01⟩ + |10⟩ - |11⟩) / 2\", \"(|00⟩ + |01⟩ - |10⟩ - |11⟩) / 2\", \"(|00⟩ - |01⟩ - |10⟩ + |11⟩) / 2\"]\n );\n\n if not isCorrect {\n Message(\"Incorrect.\");\n } else {\n Message(\"Correct!\");\n }\n\n return isCorrect\n }\n}\n"
9275
9275
  },
9276
9276
  {
9277
9277
  "id": "distinguishing_states__four_orthogonal_two_qubit_part_two__Verification.qs",
9278
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n // 0 - ( |00⟩ - |01⟩ - |10⟩ - |11⟩) / 2\n // 1 - (-|00⟩ + |01⟩ - |10⟩ - |11⟩) / 2\n // 2 - (-|00⟩ - |01⟩ + |10⟩ - |11⟩) / 2\n // 3 - (-|00⟩ - |01⟩ - |10⟩ + |11⟩) / 2\n operation StatePrep_TwoQubitStateTwo(\n qs : Qubit[],\n state : Int,\n dummyVar : Double\n ) : Unit is Adj {\n StatePrep_BasisStateMeasurement(qs, state, dummyVar);\n\n // Now apply all gates for unitary in reference impl (in reverse + adjoint)\n within {\n ApplyToEachA(X, qs);\n Controlled Z([qs[0]], qs[1]);\n ApplyToEachA(X, qs);\n } apply {\n ApplyToEachCA(H, qs);\n }\n\n SWAP(qs[0], qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishStates_MultiQubit(\n 2, 4,\n StatePrep_TwoQubitStateTwo,\n Kata.TwoQubitStateTwo,\n false,\n [\"(+|00⟩ - |01⟩ - |10⟩ - |11⟩) / 2\", \"(-|00⟩ + |01⟩ - |10⟩ - |11⟩) / 2\", \"(-|00⟩ - |01⟩ + |10⟩ - |11⟩) / 2\", \"(-|00⟩ - |01⟩ - |10⟩ + |11⟩) / 2\"]\n );\n\n if not isCorrect {\n Message(\"Incorrect.\");\n } else {\n Message(\"Correct!\");\n }\n\n return isCorrect\n }\n}\n"
9278
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n // 0 - ( |00⟩ - |01⟩ - |10⟩ - |11⟩) / 2\n // 1 - (-|00⟩ + |01⟩ - |10⟩ - |11⟩) / 2\n // 2 - (-|00⟩ - |01⟩ + |10⟩ - |11⟩) / 2\n // 3 - (-|00⟩ - |01⟩ - |10⟩ + |11⟩) / 2\n operation StatePrep_TwoQubitStateTwo(\n qs : Qubit[],\n state : Int,\n dummyVar : Double\n ) : Unit is Adj {\n StatePrep_BasisStateMeasurement(qs, state, dummyVar);\n\n // Now apply all gates for unitary in reference impl (in reverse + adjoint)\n within {\n ApplyToEachA(X, qs);\n Controlled Z([qs[0]], qs[1]);\n ApplyToEachA(X, qs);\n } apply {\n ApplyToEachCA(H, qs);\n }\n\n SWAP(qs[0], qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishStates_MultiQubit(\n 2,\n 4,\n StatePrep_TwoQubitStateTwo,\n Kata.TwoQubitStateTwo,\n false,\n [\"(+|00⟩ - |01⟩ - |10⟩ - |11⟩) / 2\", \"(-|00⟩ + |01⟩ - |10⟩ - |11⟩) / 2\", \"(-|00⟩ - |01⟩ + |10⟩ - |11⟩) / 2\", \"(-|00⟩ - |01⟩ - |10⟩ + |11⟩) / 2\"]\n );\n\n if not isCorrect {\n Message(\"Incorrect.\");\n } else {\n Message(\"Correct!\");\n }\n\n return isCorrect\n }\n}\n"
9279
9279
  },
9280
9280
  {
9281
9281
  "id": "distinguishing_states__two_orthogonal_three_qubit__Verification.qs",
9282
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation StatePrep_ThreeQubitMeasurement(\n qs : Qubit[],\n state : Int,\n dummyVar : Double\n ) : Unit is Adj {\n WState_Arbitrary_Reference(qs);\n\n if state == 0 {\n // prep 1/sqrt(3) ( |100⟩ + ω |010⟩ + ω² |001⟩ )\n R1(2.0 * PI() / 3.0, qs[1]);\n R1(4.0 * PI() / 3.0, qs[2]);\n } else {\n // prep 1/sqrt(3) ( |100⟩ + ω² |010⟩ + ω |001⟩ )\n R1(4.0 * PI() / 3.0, qs[1]);\n R1(2.0 * PI() / 3.0, qs[2]);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishStates_MultiQubit(\n 3, 2,\n StatePrep_ThreeQubitMeasurement,\n Kata.ThreeQubitMeasurement,\n false,\n [\"1/sqrt(3) (|100⟩ + ω |010⟩ + ω² |001⟩)\", \"1/sqrt(3) (|100⟩ + ω² |010⟩ + ω |001⟩)\"]);\n\n if not isCorrect {\n Message(\"Incorrect.\");\n } else {\n Message(\"Correct!\");\n }\n\n return isCorrect\n }\n}\n"
9282
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Math.*;\n\n operation StatePrep_ThreeQubitMeasurement(\n qs : Qubit[],\n state : Int,\n dummyVar : Double\n ) : Unit is Adj {\n WState_Arbitrary_Reference(qs);\n\n if state == 0 {\n // prep 1/sqrt(3) ( |100⟩ + ω |010⟩ + ω² |001⟩ )\n R1(2.0 * PI() / 3.0, qs[1]);\n R1(4.0 * PI() / 3.0, qs[2]);\n } else {\n // prep 1/sqrt(3) ( |100⟩ + ω² |010⟩ + ω |001⟩ )\n R1(4.0 * PI() / 3.0, qs[1]);\n R1(2.0 * PI() / 3.0, qs[2]);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishStates_MultiQubit(\n 3,\n 2,\n StatePrep_ThreeQubitMeasurement,\n Kata.ThreeQubitMeasurement,\n false,\n [\"1/sqrt(3) (|100⟩ + ω |010⟩ + ω² |001⟩)\", \"1/sqrt(3) (|100⟩ + ω² |010⟩ + ω |001⟩)\"]\n );\n\n if not isCorrect {\n Message(\"Incorrect.\");\n } else {\n Message(\"Correct!\");\n }\n\n return isCorrect\n }\n}\n"
9283
9283
  },
9284
9284
  {
9285
9285
  "id": "distinguishing_states__zero_plus__Verification.qs",
9286
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Random;\n\n\n operation SetQubitZeroOrPlus (q : Qubit, state : Int) : Unit {\n if state != 0 {\n H(q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution () : Bool {\n let nTotal = 1000;\n mutable nOk = 0;\n let threshold = 0.8;\n\n use qs = Qubit[1];\n for i in 1 .. nTotal {\n // get a random integer to define the state of the qubits\n let state = DrawRandomInt(0, 1);\n\n // do state prep: convert |0⟩ to outcome with return equal to state\n SetQubitZeroOrPlus(qs[0], state);\n\n // get the solution's answer and verify that it's a match\n let ans = Kata.IsQubitZeroOrPlus(qs[0]);\n if ans == (state == 0) {\n set nOk += 1;\n }\n\n // we're not checking the state of the qubit after the operation\n ResetAll(qs);\n }\n\n if IntAsDouble(nOk) < threshold * IntAsDouble(nTotal) {\n Message($\"{nTotal - nOk} test runs out of {nTotal} returned incorrect state, which does not meet the required threshold of at least {threshold * 100.0}%.\");\n Message(\"Incorrect.\");\n return false;\n } else {\n Message(\"Correct!\");\n return true;\n }\n }\n}\n"
9286
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Convert.*;\n import Std.Random.*;\n\n\n operation SetQubitZeroOrPlus(q : Qubit, state : Int) : Unit {\n if state != 0 {\n H(q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let nTotal = 1000;\n mutable nOk = 0;\n let threshold = 0.8;\n\n use qs = Qubit[1];\n for i in 1..nTotal {\n // get a random integer to define the state of the qubits\n let state = DrawRandomInt(0, 1);\n\n // do state prep: convert |0⟩ to outcome with return equal to state\n SetQubitZeroOrPlus(qs[0], state);\n\n // get the solution's answer and verify that it's a match\n let ans = Kata.IsQubitZeroOrPlus(qs[0]);\n if ans == (state == 0) {\n set nOk += 1;\n }\n\n // we're not checking the state of the qubit after the operation\n ResetAll(qs);\n }\n\n if IntAsDouble(nOk) < threshold * IntAsDouble(nTotal) {\n Message($\"{nTotal - nOk} test runs out of {nTotal} returned incorrect state, which does not meet the required threshold of at least {threshold * 100.0}%.\");\n Message(\"Incorrect.\");\n return false;\n } else {\n Message(\"Correct!\");\n return true;\n }\n }\n}\n"
9287
9287
  },
9288
9288
  {
9289
9289
  "id": "distinguishing_states__zero_plus_inc__Verification.qs",
9290
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Random;\n\n operation SetQubitZeroOrPlus (q : Qubit, state : Int) : Unit {\n if state != 0 {\n H(q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let nTotal = 10000;\n let thresholdInconcl = 0.8;\n let thresholdConcl = 0.1;\n mutable isCorrect = true;\n\n // counts total inconclusive answers\n mutable nInconc = 0;\n\n // counts total conclusive |0⟩ state identifications\n mutable nConclOne = 0;\n\n // counts total conclusive |+> state identifications\n mutable nConclPlus = 0;\n\n use qs = Qubit[1];\n for i in 1 .. nTotal {\n\n // get a random integer to define the state of the qubits\n let state = DrawRandomInt(0, 1);\n\n // do state prep: convert |0⟩ to outcome with return equal to state\n SetQubitZeroOrPlus(qs[0], state);\n\n // get the solution's answer and analyze it\n let ans = Kata.IsQubitZeroPlusOrInconclusive(qs[0]);\n\n // keep track of the number of inconclusive answers given\n if ans == -1 {\n set nInconc += 1;\n }\n\n if (ans == 0 and state == 0) {\n set nConclOne += 1;\n }\n\n if (ans == 1 and state == 1) {\n set nConclPlus += 1;\n }\n\n ResetAll(qs);\n }\n\n if IntAsDouble(nInconc) > thresholdInconcl * IntAsDouble(nTotal) {\n Message($\"{nInconc} test runs out of {nTotal} returned inconclusive which does not meet the required threshold of at most {thresholdInconcl * 100.0}%.\");\n set isCorrect = false;\n }\n\n if IntAsDouble(nConclOne) < thresholdConcl * IntAsDouble(nTotal) {\n Message($\"{nConclOne} test runs out of {nTotal} returned conclusive |0⟩ which does not meet the required threshold of at least {thresholdConcl * 100.0}%.\");\n set isCorrect = false;\n }\n\n if IntAsDouble(nConclPlus) < thresholdConcl * IntAsDouble(nTotal) {\n Message($\"{nConclPlus} test runs out of {nTotal} returned conclusive |+⟩ which does not meet the required threshold of at least {thresholdConcl * 100.0}%.\");\n set isCorrect = false;\n }\n \n if (isCorrect) {\n Message(\"Correct!\");\n return true;\n } else {\n Message(\"Incorrect\");\n return false;\n }\n }\n\n}\n"
9290
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Convert.*;\n import Std.Random.*;\n\n operation SetQubitZeroOrPlus(q : Qubit, state : Int) : Unit {\n if state != 0 {\n H(q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let nTotal = 10000;\n let thresholdInconcl = 0.8;\n let thresholdConcl = 0.1;\n mutable isCorrect = true;\n\n // counts total inconclusive answers\n mutable nInconc = 0;\n\n // counts total conclusive |0⟩ state identifications\n mutable nConclOne = 0;\n\n // counts total conclusive |+> state identifications\n mutable nConclPlus = 0;\n\n use qs = Qubit[1];\n for i in 1..nTotal {\n\n // get a random integer to define the state of the qubits\n let state = DrawRandomInt(0, 1);\n\n // do state prep: convert |0⟩ to outcome with return equal to state\n SetQubitZeroOrPlus(qs[0], state);\n\n // get the solution's answer and analyze it\n let ans = Kata.IsQubitZeroPlusOrInconclusive(qs[0]);\n\n // keep track of the number of inconclusive answers given\n if ans == -1 {\n set nInconc += 1;\n }\n\n if (ans == 0 and state == 0) {\n set nConclOne += 1;\n }\n\n if (ans == 1 and state == 1) {\n set nConclPlus += 1;\n }\n\n ResetAll(qs);\n }\n\n if IntAsDouble(nInconc) > thresholdInconcl * IntAsDouble(nTotal) {\n Message($\"{nInconc} test runs out of {nTotal} returned inconclusive which does not meet the required threshold of at most {thresholdInconcl * 100.0}%.\");\n set isCorrect = false;\n }\n\n if IntAsDouble(nConclOne) < thresholdConcl * IntAsDouble(nTotal) {\n Message($\"{nConclOne} test runs out of {nTotal} returned conclusive |0⟩ which does not meet the required threshold of at least {thresholdConcl * 100.0}%.\");\n set isCorrect = false;\n }\n\n if IntAsDouble(nConclPlus) < thresholdConcl * IntAsDouble(nTotal) {\n Message($\"{nConclPlus} test runs out of {nTotal} returned conclusive |+⟩ which does not meet the required threshold of at least {thresholdConcl * 100.0}%.\");\n set isCorrect = false;\n }\n\n if (isCorrect) {\n Message(\"Correct!\");\n return true;\n } else {\n Message(\"Incorrect\");\n return false;\n }\n }\n\n}\n"
9291
9291
  },
9292
9292
  {
9293
9293
  "id": "distinguishing_states__peres_wooters_game__Verification.qs",
9294
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Random;\n open Microsoft.Quantum.Math;\n\n operation StatePrep_IsQubitNotInABC (q : Qubit, state : Int) : Unit {\n let alpha = (2.0 * PI()) / 3.0;\n H(q);\n\n if state == 0 {\n // convert |0⟩ to 1/sqrt(2) (|0⟩ + |1⟩)\n }\n elif state == 1 {\n // convert |0⟩ to 1/sqrt(2) (|0⟩ + ω |1⟩), where ω = exp(2iπ/3)\n R1(alpha, q);\n }\n else {\n // convert |0⟩ to 1/sqrt(2) (|0⟩ + ω² |1⟩), where ω = exp(2iπ/3)\n R1(2.0 * alpha, q);\n }\n }\n\n\n @EntryPoint()\n operation CheckSolution () : Bool {\n let nTotal = 1000;\n mutable bad_value = 0;\n mutable wrong_state = 0;\n\n use qs = Qubit[1];\n\n for i in 1 .. nTotal {\n // get a random integer to define the state of the qubits\n let state = DrawRandomInt(0, 2);\n\n // do state prep: convert |0⟩ to outcome with return equal to state\n StatePrep_IsQubitNotInABC(qs[0], state);\n\n // get the solution's answer and verify that it's a match\n let ans = Kata.IsQubitNotInABC(qs[0]);\n\n // check that the value of ans is 0, 1 or 2\n if (ans < 0 or ans > 2) {\n set bad_value += 1;\n }\n\n // check if upon conclusive result the answer is actually correct\n if ans == state {\n set wrong_state += 1;\n }\n\n // we're not checking the state of the qubit after the operation\n ResetAll(qs);\n }\n\n if bad_value == 0 and wrong_state == 0 {\n Message(\"Correct!\");\n return true;\n } else {\n if bad_value > 0 {\n Message($\"Solution returned values other than 0, 1 or 2 {bad_value} times.\");\n }\n if wrong_state > 0 {\n Message($\"Solution gave incorrect response {wrong_state} times\");\n }\n Message(\"Incorrect.\");\n return false;\n }\n }\n}"
9294
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Convert.*;\n import Std.Random.*;\n import Std.Math.*;\n\n operation StatePrep_IsQubitNotInABC(q : Qubit, state : Int) : Unit {\n let alpha = (2.0 * PI()) / 3.0;\n H(q);\n\n if state == 0 {\n // convert |0⟩ to 1/sqrt(2) (|0⟩ + |1⟩)\n } elif state == 1 {\n // convert |0⟩ to 1/sqrt(2) (|0⟩ + ω |1⟩), where ω = exp(2iπ/3)\n R1(alpha, q);\n } else {\n // convert |0⟩ to 1/sqrt(2) (|0⟩ + ω² |1⟩), where ω = exp(2iπ/3)\n R1(2.0 * alpha, q);\n }\n }\n\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let nTotal = 1000;\n mutable bad_value = 0;\n mutable wrong_state = 0;\n\n use qs = Qubit[1];\n\n for i in 1..nTotal {\n // get a random integer to define the state of the qubits\n let state = DrawRandomInt(0, 2);\n\n // do state prep: convert |0⟩ to outcome with return equal to state\n StatePrep_IsQubitNotInABC(qs[0], state);\n\n // get the solution's answer and verify that it's a match\n let ans = Kata.IsQubitNotInABC(qs[0]);\n\n // check that the value of ans is 0, 1 or 2\n if (ans < 0 or ans > 2) {\n set bad_value += 1;\n }\n\n // check if upon conclusive result the answer is actually correct\n if ans == state {\n set wrong_state += 1;\n }\n\n // we're not checking the state of the qubit after the operation\n ResetAll(qs);\n }\n\n if bad_value == 0 and wrong_state == 0 {\n Message(\"Correct!\");\n return true;\n } else {\n if bad_value > 0 {\n Message($\"Solution returned values other than 0, 1 or 2 {bad_value} times.\");\n }\n if wrong_state > 0 {\n Message($\"Solution gave incorrect response {wrong_state} times\");\n }\n Message(\"Incorrect.\");\n return false;\n }\n }\n}\n"
9295
9295
  },
9296
9296
  {
9297
9297
  "id": "distinguishing_unitaries__i_x__Verification.qs",
9298
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n DistinguishUnitaries_Framework([I, X], Kata.DistinguishIfromX, [\"I\", \"X\"], 1)\n }\n}\n"
9298
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n DistinguishUnitaries_Framework([I, X], Kata.DistinguishIfromX, [\"I\", \"X\"], 1)\n }\n}\n"
9299
9299
  },
9300
9300
  {
9301
9301
  "id": "distinguishing_unitaries__Common.qs",
9302
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Random;\n\n // \"Framework\" operation for testing tasks for distinguishing unitaries\n // \"unitaries\" is the list of unitaries that can be passed to the task\n // \"testImpl\" - the solution to be tested\n // \"unitaryNames\" - the labels of unitaries in the list\n // \"maxCalls\" - max # of calls to the unitary that are allowed (-1 means unlimited) - currently unused, TODO use after #1154\n operation DistinguishUnitaries_Framework<'UInput> (\n unitaries : ('UInput => Unit is Adj + Ctl)[], \n testImpl : ('UInput => Unit is Adj + Ctl) => Int,\n unitaryNames : String[],\n maxCalls : Int\n ) : Bool {\n\n let nUnitaries = Length(unitaries);\n let nTotal = 100;\n mutable wrongClassifications = [0, size = nUnitaries * nUnitaries]; // [i * nU + j] number of times unitary i was classified as j\n mutable unknownClassifications = [0, size = nUnitaries]; // number of times unitary i was classified as something unknown\n \n for i in 1 .. nTotal {\n // get a random integer to define the unitary used\n let actualIndex = DrawRandomInt(0, nUnitaries - 1);\n \n // get the solution's answer and verify that it's a match\n let returnedIndex = testImpl(unitaries[actualIndex]);\n\n if returnedIndex != actualIndex {\n if returnedIndex < 0 or returnedIndex >= nUnitaries {\n set unknownClassifications w/= actualIndex <- unknownClassifications[actualIndex] + 1;\n } else {\n let index = actualIndex * nUnitaries + returnedIndex;\n set wrongClassifications w/= index <- wrongClassifications[index] + 1;\n }\n }\n }\n \n mutable totalMisclassifications = 0;\n for i in 0 .. nUnitaries - 1 {\n for j in 0 .. nUnitaries - 1 {\n let misclassifiedIasJ = wrongClassifications[(i * nUnitaries) + j];\n if misclassifiedIasJ != 0 {\n set totalMisclassifications += misclassifiedIasJ;\n Message($\"Misclassified {unitaryNames[i]} as {unitaryNames[j]} in {misclassifiedIasJ} test runs.\");\n }\n }\n if unknownClassifications[i] != 0 {\n set totalMisclassifications += unknownClassifications[i];\n Message($\"Failed to classify {unitaryNames[i]} in {unknownClassifications[i]} test runs.\");\n }\n }\n // This check will tell the total number of failed classifications\n if totalMisclassifications != 0 {\n Message($\"{totalMisclassifications} test runs out of {nTotal} returned incorrect state.\");\n Message(\"Incorrect.\");\n return false;\n }\n Message(\"Correct!\");\n true\n }\n\n operation MinusZ (q : Qubit) : Unit is Adj + Ctl {\n within { \n X(q);\n }\n apply {\n Z(q);\n }\n }\n\n operation XZ (q : Qubit) : Unit is Adj + Ctl {\n Z(q);\n X(q);\n }\n\n operation MinusY (q : Qubit) : Unit is Adj + Ctl {\n within {\n X(q);\n }\n apply {\n Y(q);\n }\n }\n\n operation MinusXZ (q : Qubit) : Unit is Adj + Ctl {\n X(q);\n Z(q);\n }\n}\n"
9302
+ "code": "namespace Kata.Verification {\n import Std.Math.*;\n import Std.Random.*;\n\n // \"Framework\" operation for testing tasks for distinguishing unitaries\n // \"unitaries\" is the list of unitaries that can be passed to the task\n // \"testImpl\" - the solution to be tested\n // \"unitaryNames\" - the labels of unitaries in the list\n // \"maxCalls\" - max # of calls to the unitary that are allowed (-1 means unlimited) - currently unused, TODO use after #1154\n operation DistinguishUnitaries_Framework<'UInput>(\n unitaries : ('UInput => Unit is Adj + Ctl)[],\n testImpl : ('UInput => Unit is Adj + Ctl) => Int,\n unitaryNames : String[],\n maxCalls : Int\n ) : Bool {\n\n let nUnitaries = Length(unitaries);\n let nTotal = 100;\n mutable wrongClassifications = [0, size = nUnitaries * nUnitaries]; // [i * nU + j] number of times unitary i was classified as j\n mutable unknownClassifications = [0, size = nUnitaries]; // number of times unitary i was classified as something unknown\n\n for i in 1..nTotal {\n // get a random integer to define the unitary used\n let actualIndex = DrawRandomInt(0, nUnitaries - 1);\n\n // get the solution's answer and verify that it's a match\n let returnedIndex = testImpl(unitaries[actualIndex]);\n\n if returnedIndex != actualIndex {\n if returnedIndex < 0 or returnedIndex >= nUnitaries {\n set unknownClassifications w/= actualIndex <- unknownClassifications[actualIndex] + 1;\n } else {\n let index = actualIndex * nUnitaries + returnedIndex;\n set wrongClassifications w/= index <- wrongClassifications[index] + 1;\n }\n }\n }\n\n mutable totalMisclassifications = 0;\n for i in 0..nUnitaries - 1 {\n for j in 0..nUnitaries - 1 {\n let misclassifiedIasJ = wrongClassifications[(i * nUnitaries) + j];\n if misclassifiedIasJ != 0 {\n set totalMisclassifications += misclassifiedIasJ;\n Message($\"Misclassified {unitaryNames[i]} as {unitaryNames[j]} in {misclassifiedIasJ} test runs.\");\n }\n }\n if unknownClassifications[i] != 0 {\n set totalMisclassifications += unknownClassifications[i];\n Message($\"Failed to classify {unitaryNames[i]} in {unknownClassifications[i]} test runs.\");\n }\n }\n // This check will tell the total number of failed classifications\n if totalMisclassifications != 0 {\n Message($\"{totalMisclassifications} test runs out of {nTotal} returned incorrect state.\");\n Message(\"Incorrect.\");\n return false;\n }\n Message(\"Correct!\");\n true\n }\n\n operation MinusZ(q : Qubit) : Unit is Adj + Ctl {\n within {\n X(q);\n } apply {\n Z(q);\n }\n }\n\n operation XZ(q : Qubit) : Unit is Adj + Ctl {\n Z(q);\n X(q);\n }\n\n operation MinusY(q : Qubit) : Unit is Adj + Ctl {\n within {\n X(q);\n } apply {\n Y(q);\n }\n }\n\n operation MinusXZ(q : Qubit) : Unit is Adj + Ctl {\n X(q);\n Z(q);\n }\n}\n"
9303
9303
  },
9304
9304
  {
9305
9305
  "id": "distinguishing_unitaries__i_z__Verification.qs",
9306
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n DistinguishUnitaries_Framework([I, Z], Kata.DistinguishIfromZ, [\"I\", \"Z\"], 1)\n }\n}\n"
9306
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n DistinguishUnitaries_Framework([I, Z], Kata.DistinguishIfromZ, [\"I\", \"Z\"], 1)\n }\n}\n"
9307
9307
  },
9308
9308
  {
9309
9309
  "id": "distinguishing_unitaries__z_s__Verification.qs",
9310
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n DistinguishUnitaries_Framework([Z, S], Kata.DistinguishZfromS, [\"Z\", \"S\"], 1)\n }\n}\n"
9310
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n DistinguishUnitaries_Framework([Z, S], Kata.DistinguishZfromS, [\"Z\", \"S\"], 1)\n }\n}\n"
9311
9311
  },
9312
9312
  {
9313
9313
  "id": "distinguishing_unitaries__h_x__Verification.qs",
9314
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n DistinguishUnitaries_Framework([H, X], Kata.DistinguishHfromX, [\"H\", \"X\"], 1)\n }\n}\n"
9314
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n DistinguishUnitaries_Framework([H, X], Kata.DistinguishHfromX, [\"H\", \"X\"], 1)\n }\n}\n"
9315
9315
  },
9316
9316
  {
9317
9317
  "id": "distinguishing_unitaries__z_minusz__Verification.qs",
9318
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n DistinguishUnitaries_Framework([Z, MinusZ], Kata.DistinguishZfromMinusZ, [\"Z\", \"-Z\"], 1)\n }\n}\n"
9318
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n DistinguishUnitaries_Framework([Z, MinusZ], Kata.DistinguishZfromMinusZ, [\"Z\", \"-Z\"], 1)\n }\n}\n"
9319
9319
  },
9320
9320
  {
9321
9321
  "id": "distinguishing_unitaries__rz_r1__Verification.qs",
9322
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n DistinguishUnitaries_Framework([Rz, R1], Kata.DistinguishRzFromR1, [\"Rz\", \"R1\"], 1)\n }\n}\n"
9322
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n DistinguishUnitaries_Framework([Rz, R1], Kata.DistinguishRzFromR1, [\"Rz\", \"R1\"], 1)\n }\n}\n"
9323
9323
  },
9324
9324
  {
9325
9325
  "id": "distinguishing_unitaries__y_xz__Verification.qs",
9326
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n DistinguishUnitaries_Framework([Y, XZ], Kata.DistinguishYfromXZ, [\"Y\", \"XZ\"], 1)\n }\n}\n"
9326
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n DistinguishUnitaries_Framework([Y, XZ], Kata.DistinguishYfromXZ, [\"Y\", \"XZ\"], 1)\n }\n}\n"
9327
9327
  },
9328
9328
  {
9329
9329
  "id": "distinguishing_unitaries__y_xz_minusy_minusxz__Verification.qs",
9330
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n DistinguishUnitaries_Framework([Y, MinusXZ, MinusY, XZ], Kata.DistinguishYfromXZWithPhases, [\"Y\", \"-XZ\", \"-Y\", \"XZ\"], 1)\n }\n}\n"
9330
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n DistinguishUnitaries_Framework([Y, MinusXZ, MinusY, XZ], Kata.DistinguishYfromXZWithPhases, [\"Y\", \"-XZ\", \"-Y\", \"XZ\"], 1)\n }\n}\n"
9331
9331
  },
9332
9332
  {
9333
9333
  "id": "distinguishing_unitaries__i_x_y_z__Verification.qs",
9334
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n DistinguishUnitaries_Framework([I, X, Y, Z], Kata.DistinguishPaulis, [\"I\", \"X\", \"Y\", \"Z\"], 1)\n }\n}\n"
9334
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n DistinguishUnitaries_Framework([I, X, Y, Z], Kata.DistinguishPaulis, [\"I\", \"X\", \"Y\", \"Z\"], 1)\n }\n}\n"
9335
9335
  },
9336
9336
  {
9337
9337
  "id": "distinguishing_unitaries__rz_ry__Verification.qs",
9338
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for theta in [0.04, 0.1, 0.25, 0.31, 0.5, 0.87, 1.05, 1.41, 1.66, 1.75, 2.0, 2.16, 2.22, 2.51, 2.93, 3.0, 3.1] {\n Message($\"Testing theta = {theta}...\"); \n if not DistinguishUnitaries_Framework([Rz(theta, _), Ry(theta, _)], Kata.DistinguishRzFromRy(theta, _), [\"Rz\", \"Ry\"], -1) {\n return false;\n }\n }\n return true;\n }\n}\n"
9338
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for theta in [0.04, 0.1, 0.25, 0.31, 0.5, 0.87, 1.05, 1.41, 1.66, 1.75, 2.0, 2.16, 2.22, 2.51, 2.93, 3.0, 3.1] {\n Message($\"Testing theta = {theta}...\");\n if not DistinguishUnitaries_Framework([Rz(theta, _), Ry(theta, _)], Kata.DistinguishRzFromRy(theta, _), [\"Rz\", \"Ry\"], -1) {\n return false;\n }\n }\n return true;\n }\n}\n"
9339
9339
  },
9340
9340
  {
9341
9341
  "id": "distinguishing_unitaries__ix_cnot__Verification.qs",
9342
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n DistinguishUnitaries_Framework([qs => X(qs[1]), qs => CNOT(qs[0], qs[1])], Kata.DistinguishIXfromCNOT, [\"I⊗X\", \"CNOT\"], 1)\n }\n}\n"
9342
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n DistinguishUnitaries_Framework([qs => X(qs[1]), qs => CNOT(qs[0], qs[1])], Kata.DistinguishIXfromCNOT, [\"I⊗X\", \"CNOT\"], 1)\n }\n}\n"
9343
9343
  },
9344
9344
  {
9345
9345
  "id": "distinguishing_unitaries__cnot_direction__Verification.qs",
9346
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n DistinguishUnitaries_Framework([qs => CNOT(qs[0], qs[1]), qs => CNOT(qs[1], qs[0])], Kata.CNOTDirection, [\"CNOT_12\", \"CNOT_21\"], 1)\n }\n}\n"
9346
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n DistinguishUnitaries_Framework([qs => CNOT(qs[0], qs[1]), qs => CNOT(qs[1], qs[0])], Kata.CNOTDirection, [\"CNOT_12\", \"CNOT_21\"], 1)\n }\n}\n"
9347
9347
  },
9348
9348
  {
9349
9349
  "id": "distinguishing_unitaries__cnot_swap__Verification.qs",
9350
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n DistinguishUnitaries_Framework([qs => CNOT(qs[0], qs[1]), qs => SWAP(qs[0], qs[1])], Kata.DistinguishCNOTfromSWAP, [\"CNOT\", \"SWAP\"], 1)\n }\n}\n"
9350
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n DistinguishUnitaries_Framework([qs => CNOT(qs[0], qs[1]), qs => SWAP(qs[0], qs[1])], Kata.DistinguishCNOTfromSWAP, [\"CNOT\", \"SWAP\"], 1)\n }\n}\n"
9351
9351
  },
9352
9352
  {
9353
9353
  "id": "distinguishing_unitaries__i_cnot_swap__Verification.qs",
9354
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n DistinguishUnitaries_Framework([ApplyToEachCA(I, _), qs => CNOT(qs[0], qs[1]), qs => CNOT(qs[1], qs[0]), qs => SWAP(qs[0], qs[1])], Kata.DistinguishTwoQubitUnitaries, [\"I⊗I\", \"CNOT_12\", \"CNOT_21\", \"SWAP\"], 2)\n }\n} \n"
9354
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n DistinguishUnitaries_Framework([ApplyToEachCA(I, _), qs => CNOT(qs[0], qs[1]), qs => CNOT(qs[1], qs[0]), qs => SWAP(qs[0], qs[1])], Kata.DistinguishTwoQubitUnitaries, [\"I⊗I\", \"CNOT_12\", \"CNOT_21\", \"SWAP\"], 2)\n }\n}\n"
9355
9355
  },
9356
9356
  {
9357
9357
  "id": "random_numbers__random_bit__Verification.qs",
@@ -9359,7 +9359,7 @@ export default {
9359
9359
  },
9360
9360
  {
9361
9361
  "id": "random_numbers__Common.qs",
9362
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Math;\n\n /// # Summary\n /// Helper operation that checks that the given RNG operation generates a uniform distribution.\n ///\n /// # Input\n /// ## randomGenerator\n /// Random number generation operation to be tested.\n /// The parameters to this operation are provided by the caller using Delay().\n /// ## min, max\n /// Minimal and maximal numbers in the range to be generated, inclusive.\n /// ## nRuns\n /// The number of random numbers to generate for test.\n ///\n /// # Output\n /// 0x0 if the generated distribution is uniform.\n /// 0x1 if a value was generated outside the specified range.\n /// 0x2 if the average of the distribution is outside the expected range.\n /// 0x3 if the median of the distribution is outside the expected range.\n /// 0x4 if the minimum count requirements were not met.\n operation CheckUniformDistribution (\n randomGenerator : (Unit => Int),\n min : Int,\n max : Int,\n nRuns : Int) \n : Int {\n let idealMean = 0.5 * IntAsDouble(max + min);\n let rangeDividedByTwo = 0.5 * IntAsDouble(max - min);\n // Variance = a*(a+1)/3, where a = (max-min)/2\n // For sample population : divide it by nRuns\n let varianceInSamplePopulation = (rangeDividedByTwo * (rangeDividedByTwo + 1.0)) / IntAsDouble(3 * nRuns);\n let standardDeviation = Sqrt(varianceInSamplePopulation);\n\n // lowRange : The lower bound of the median and average for generated dataset\n // highRange : The upper bound of the median and average for generated dataset\n // Set them with 3 units of std deviation for 99% accuracy.\n let lowRange = idealMean - 3.0 * standardDeviation;\n let highRange = idealMean + 3.0 * standardDeviation;\n\n let idealCopiesGenerated = IntAsDouble(nRuns) / IntAsDouble(max-min+1);\n let minimumCopiesGenerated = (0.8 * idealCopiesGenerated > 40.0) ? 0.8 * idealCopiesGenerated | 0.0;\n\n mutable counts = [0, size = max + 1];\n mutable average = 0.0;\n for i in 1..nRuns {\n let val = randomGenerator();\n if (val < min or val > max) {\n Message($\"Unexpected number generated. Expected values from {min} to {max}, generated {val}\");\n return 0x1;\n }\n set average += IntAsDouble(val);\n set counts w/= val <- counts[val] + 1;\n }\n\n set average = average / IntAsDouble(nRuns);\n if (average < lowRange or average > highRange) {\n Message($\"Unexpected average of generated numbers. Expected between {lowRange} and {highRange}, got {average}\");\n return 0x2;\n }\n\n let median = FindMedian (counts, max+1, nRuns);\n if (median < Floor(lowRange) or median > Ceiling(highRange)) {\n Message($\"Unexpected median of generated numbers. Expected between {Floor(lowRange)} and {Ceiling(highRange)}, got {median}.\");\n return 0x3;\n }\n\n for i in min..max {\n if counts[i] < Floor(minimumCopiesGenerated) {\n Message($\"Unexpectedly low number of {i}'s generated. Only {counts[i]} out of {nRuns} were {i}\");\n return 0x4;\n }\n }\n return 0x0;\n }\n\n operation FindMedian (counts : Int[], arrSize : Int, sampleSize : Int) : Int {\n mutable totalCount = 0;\n for i in 0 .. arrSize - 1 {\n set totalCount = totalCount + counts[i];\n if totalCount >= sampleSize / 2 {\n return i;\n }\n }\n return -1;\n }\n\n operation IsSufficientlyRandom(verifier : (Unit => Int)) : Bool {\n let results = RunRandomnessVerifier(verifier, 10);\n Tail(results) == 0x0\n }\n\n /// # Summary\n /// Helper operation that runs a randomness verifier up to a maximum number of times.\n /// A single run can fail with non-negligible probability even for a \"correct\" random generator.\n ///\n /// # Input\n /// ## verifier\n /// Operation which verifies the a random generator.\n /// ## maxAttempts\n /// Maximum number of times the verifier is run until a successful result occurs.\n ///\n /// # Output\n /// Array with the results of each verifier run.\n operation RunRandomnessVerifier(verifier : (Unit => Int), maxAttempts : Int) : Int[] {\n mutable attemptResults = [];\n mutable result = -1;\n repeat {\n set result = verifier();\n set attemptResults += [result];\n // If the result is 0x1, the generator returned an invalid result.\n // That's different from \"not random enough\" verdicts, so we break right away.\n } until result == 0 or result == 0x1 or Length(attemptResults) >= maxAttempts;\n\n attemptResults\n }\n}\n"
9362
+ "code": "namespace Kata.Verification {\n import Std.Arrays.*;\n import Std.Diagnostics.*;\n import Std.Convert.*;\n import Std.Math.*;\n\n /// # Summary\n /// Helper operation that checks that the given RNG operation generates a uniform distribution.\n ///\n /// # Input\n /// ## randomGenerator\n /// Random number generation operation to be tested.\n /// The parameters to this operation are provided by the caller using Delay().\n /// ## min, max\n /// Minimal and maximal numbers in the range to be generated, inclusive.\n /// ## nRuns\n /// The number of random numbers to generate for test.\n ///\n /// # Output\n /// 0x0 if the generated distribution is uniform.\n /// 0x1 if a value was generated outside the specified range.\n /// 0x2 if the average of the distribution is outside the expected range.\n /// 0x3 if the median of the distribution is outside the expected range.\n /// 0x4 if the minimum count requirements were not met.\n operation CheckUniformDistribution(\n randomGenerator : (Unit => Int),\n min : Int,\n max : Int,\n nRuns : Int\n ) : Int {\n let idealMean = 0.5 * IntAsDouble(max + min);\n let rangeDividedByTwo = 0.5 * IntAsDouble(max - min);\n // Variance = a*(a+1)/3, where a = (max-min)/2\n // For sample population : divide it by nRuns\n let varianceInSamplePopulation = (rangeDividedByTwo * (rangeDividedByTwo + 1.0)) / IntAsDouble(3 * nRuns);\n let standardDeviation = Sqrt(varianceInSamplePopulation);\n\n // lowRange : The lower bound of the median and average for generated dataset\n // highRange : The upper bound of the median and average for generated dataset\n // Set them with 3 units of std deviation for 99% accuracy.\n let lowRange = idealMean - 3.0 * standardDeviation;\n let highRange = idealMean + 3.0 * standardDeviation;\n\n let idealCopiesGenerated = IntAsDouble(nRuns) / IntAsDouble(max-min + 1);\n let minimumCopiesGenerated = (0.8 * idealCopiesGenerated > 40.0) ? 0.8 * idealCopiesGenerated | 0.0;\n\n mutable counts = [0, size = max + 1];\n mutable average = 0.0;\n for i in 1..nRuns {\n let val = randomGenerator();\n if (val < min or val > max) {\n Message($\"Unexpected number generated. Expected values from {min} to {max}, generated {val}\");\n return 0x1;\n }\n set average += IntAsDouble(val);\n set counts w/= val <- counts[val] + 1;\n }\n\n set average = average / IntAsDouble(nRuns);\n if (average < lowRange or average > highRange) {\n Message($\"Unexpected average of generated numbers. Expected between {lowRange} and {highRange}, got {average}\");\n return 0x2;\n }\n\n let median = FindMedian(counts, max + 1, nRuns);\n if (median < Floor(lowRange) or median > Ceiling(highRange)) {\n Message($\"Unexpected median of generated numbers. Expected between {Floor(lowRange)} and {Ceiling(highRange)}, got {median}.\");\n return 0x3;\n }\n\n for i in min..max {\n if counts[i] < Floor(minimumCopiesGenerated) {\n Message($\"Unexpectedly low number of {i}'s generated. Only {counts[i]} out of {nRuns} were {i}\");\n return 0x4;\n }\n }\n return 0x0;\n }\n\n operation FindMedian(counts : Int[], arrSize : Int, sampleSize : Int) : Int {\n mutable totalCount = 0;\n for i in 0..arrSize - 1 {\n set totalCount = totalCount + counts[i];\n if totalCount >= sampleSize / 2 {\n return i;\n }\n }\n return -1;\n }\n\n operation IsSufficientlyRandom(verifier : (Unit => Int)) : Bool {\n let results = RunRandomnessVerifier(verifier, 10);\n Tail(results) == 0x0\n }\n\n /// # Summary\n /// Helper operation that runs a randomness verifier up to a maximum number of times.\n /// A single run can fail with non-negligible probability even for a \"correct\" random generator.\n ///\n /// # Input\n /// ## verifier\n /// Operation which verifies the a random generator.\n /// ## maxAttempts\n /// Maximum number of times the verifier is run until a successful result occurs.\n ///\n /// # Output\n /// Array with the results of each verifier run.\n operation RunRandomnessVerifier(verifier : (Unit => Int), maxAttempts : Int) : Int[] {\n mutable attemptResults = [];\n mutable result = -1;\n repeat {\n set result = verifier();\n set attemptResults += [result];\n // If the result is 0x1, the generator returned an invalid result.\n // That's different from \"not random enough\" verdicts, so we break right away.\n } until result == 0 or result == 0x1 or Length(attemptResults) >= maxAttempts;\n\n attemptResults\n }\n}\n"
9363
9363
  },
9364
9364
  {
9365
9365
  "id": "random_numbers__random_two_bits__Verification.qs",
@@ -9371,7 +9371,7 @@ export default {
9371
9371
  },
9372
9372
  {
9373
9373
  "id": "random_numbers__weighted_random_bit__Verification.qs",
9374
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Convert;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for x in [0.0, 0.25, 0.5, 0.75, 1.0] {\n Message($\"Testing generating zero with {x*100.0}% probability...\");\n let randomnessVerifier = () => CheckXPercentZero(() => Kata.WeightedRandomBit(x), x);\n let isCorrect = IsSufficientlyRandom(randomnessVerifier);\n if not isCorrect {\n return false;\n }\n }\n Message(\"Correct!\");\n true\n }\n\n /// # Summary\n /// Helper operation that checks that the given RNG operation generates zero with x percent probability\n /// # Input\n /// ## op\n /// Random number generation operation to be tested.\n /// ## x\n /// Probability of generating zero\n operation CheckXPercentZero (op : (Unit => Int), x : Double) : Int {\n mutable oneCount = 0;\n let nRuns = 1000;\n for N in 1..nRuns {\n let val = op();\n if (val < 0 or val > 1) {\n Message($\"Unexpected number generated. Expected 0 or 1, instead generated {val}\");\n return 0x1;\n }\n set oneCount += val;\n }\n\n let zeroCount = nRuns - oneCount;\n let goalZeroCount = (x == 0.0) ? 0 | (x == 1.0) ? nRuns | Floor(x * IntAsDouble(nRuns));\n // We don't have tests with probabilities near 0.0 or 1.0, so for those the matching has to be exact\n if (goalZeroCount == 0 or goalZeroCount == nRuns) {\n if zeroCount != goalZeroCount {\n Message($\"Expected {x * 100.0}% 0's, instead got {zeroCount} 0's out of {nRuns}\");\n return 0x2;\n }\n } else {\n if zeroCount < goalZeroCount - 4 * nRuns / 100 {\n Message($\"Unexpectedly low number of 0's generated: expected around {x * IntAsDouble(nRuns)} 0's, got {zeroCount} out of {nRuns}\");\n return 0x3;\n } elif zeroCount > goalZeroCount + 4 * nRuns / 100 {\n Message($\"Unexpectedly high number of 0's generated: expected around {x * IntAsDouble(nRuns)} 0's, got {zeroCount} out of {nRuns}\");\n return 0x4;\n }\n }\n return 0x0;\n }\n\n}\n"
9374
+ "code": "namespace Kata.Verification {\n import Std.Math.*;\n import Std.Convert.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for x in [0.0, 0.25, 0.5, 0.75, 1.0] {\n Message($\"Testing generating zero with {x * 100.0}% probability...\");\n let randomnessVerifier = () => CheckXPercentZero(() => Kata.WeightedRandomBit(x), x);\n let isCorrect = IsSufficientlyRandom(randomnessVerifier);\n if not isCorrect {\n return false;\n }\n }\n Message(\"Correct!\");\n true\n }\n\n /// # Summary\n /// Helper operation that checks that the given RNG operation generates zero with x percent probability\n /// # Input\n /// ## op\n /// Random number generation operation to be tested.\n /// ## x\n /// Probability of generating zero\n operation CheckXPercentZero(op : (Unit => Int), x : Double) : Int {\n mutable oneCount = 0;\n let nRuns = 1000;\n for N in 1..nRuns {\n let val = op();\n if (val < 0 or val > 1) {\n Message($\"Unexpected number generated. Expected 0 or 1, instead generated {val}\");\n return 0x1;\n }\n set oneCount += val;\n }\n\n let zeroCount = nRuns - oneCount;\n let goalZeroCount = (x == 0.0) ? 0 | (x == 1.0) ? nRuns | Floor(x * IntAsDouble(nRuns));\n // We don't have tests with probabilities near 0.0 or 1.0, so for those the matching has to be exact\n if (goalZeroCount == 0 or goalZeroCount == nRuns) {\n if zeroCount != goalZeroCount {\n Message($\"Expected {x * 100.0}% 0's, instead got {zeroCount} 0's out of {nRuns}\");\n return 0x2;\n }\n } else {\n if zeroCount < goalZeroCount - 4 * nRuns / 100 {\n Message($\"Unexpectedly low number of 0's generated: expected around {x * IntAsDouble(nRuns)} 0's, got {zeroCount} out of {nRuns}\");\n return 0x3;\n } elif zeroCount > goalZeroCount + 4 * nRuns / 100 {\n Message($\"Unexpectedly high number of 0's generated: expected around {x * IntAsDouble(nRuns)} 0's, got {zeroCount} out of {nRuns}\");\n return 0x4;\n }\n }\n return 0x0;\n }\n\n}\n"
9375
9375
  },
9376
9376
  {
9377
9377
  "id": "random_numbers__random_number__Verification.qs",
@@ -9379,83 +9379,83 @@ export default {
9379
9379
  },
9380
9380
  {
9381
9381
  "id": "key_distribution__random_array__Verification.qs",
9382
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Convert;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n // The test only checks that the operation returns an array of correct length\n // and that it's not always the same. It doesn't analyze the distribution of true and false elements.\n let N = 20;\n let randomArrays = ForEach(Kata.RandomArray, [N, size = 10]);\n\n for array in randomArrays {\n if Length(array) != N {\n Message($\"Returned array should have length {N}, and it had length {Length(array)}.\");\n return false;\n }\n }\n\n let randomInts = Mapped(BoolArrayAsInt, randomArrays);\n mutable allSame = true;\n for int in randomInts {\n if int != randomInts[0] {\n set allSame = false;\n }\n }\n if allSame {\n Message($\"Random generation should not return a fixed array.\");\n return false;\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9382
+ "code": "namespace Kata.Verification {\n import Std.Arrays.*;\n import Std.Convert.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n // The test only checks that the operation returns an array of correct length\n // and that it's not always the same. It doesn't analyze the distribution of true and false elements.\n let N = 20;\n let randomArrays = ForEach(Kata.RandomArray, [N, size = 10]);\n\n for array in randomArrays {\n if Length(array) != N {\n Message($\"Returned array should have length {N}, and it had length {Length(array)}.\");\n return false;\n }\n }\n\n let randomInts = Mapped(BoolArrayAsInt, randomArrays);\n mutable allSame = true;\n for int in randomInts {\n if int != randomInts[0] {\n set allSame = false;\n }\n }\n if allSame {\n Message($\"Random generation should not return a fixed array.\");\n return false;\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9383
9383
  },
9384
9384
  {
9385
9385
  "id": "key_distribution__Common.qs",
9386
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Random;\n\n operation RandomArray(N : Int) : Bool[] {\n ForEach(x => DrawRandomInt(0, 1) == 0, [0, size = N])\n }\n\n operation BasisToString(base : Bool) : String {\n base ? \"Hadamard\" | \"computational\"\n }\n\n operation StateToString(base : Bool, bit : Bool) : String {\n if base { // ∣+⟩ / ∣-⟩\n return bit ? \"|-⟩\" | \"|+⟩\";\n } else { // ∣0⟩ / ∣1⟩\n return bit ? \"|1⟩\" | \"|0⟩\";\n }\n }\n\n operation PrepareQubits_Reference(qs : Qubit[], bases : Bool[], bits : Bool[]) : Unit is Adj {\n for i in 0 .. Length(qs) - 1 {\n if bits[i] {\n X(qs[i]);\n }\n if bases[i] {\n H(qs[i]);\n }\n }\n }\n}\n"
9386
+ "code": "namespace Kata.Verification {\n import Std.Arrays.*;\n import Std.Random.*;\n\n operation RandomArray(N : Int) : Bool[] {\n ForEach(x => DrawRandomInt(0, 1) == 0, [0, size = N])\n }\n\n operation BasisToString(base : Bool) : String {\n base ? \"Hadamard\" | \"computational\"\n }\n\n operation StateToString(base : Bool, bit : Bool) : String {\n if base {\n // ∣+⟩ / ∣-⟩\n return bit ? \"|-⟩\" | \"|+⟩\";\n } else {\n // ∣0⟩ / ∣1⟩\n return bit ? \"|1⟩\" | \"|0⟩\";\n }\n }\n\n operation PrepareQubits_Reference(qs : Qubit[], bases : Bool[], bits : Bool[]) : Unit is Adj {\n for i in 0..Length(qs) - 1 {\n if bits[i] {\n X(qs[i]);\n }\n if bases[i] {\n H(qs[i]);\n }\n }\n }\n}\n"
9387
9387
  },
9388
9388
  {
9389
9389
  "id": "key_distribution__prepare_qubits__Verification.qs",
9390
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 2 .. 10 {\n let (bases, bits) = (RandomArray(N), RandomArray(N));\n use qs = Qubit[N];\n Kata.PrepareQubits(qs, bases, bits);\n Adjoint PrepareQubits_Reference(qs, bases, bits);\n for i in 0 .. N - 1 {\n if not CheckZero(qs[i]) {\n Message($\"Qubit qs[{i}] prepared in incorrect state.\");\n Message($\"Expected state {StateToString(bases[i], bits[i])}; actual state\");\n PrepareQubits_Reference([qs[i]], [bases[i]], [bits[i]]);\n DumpRegister([qs[i]]);\n ResetAll(qs);\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9390
+ "code": "namespace Kata.Verification {\n import Std.Diagnostics.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 2..10 {\n let (bases, bits) = (RandomArray(N), RandomArray(N));\n use qs = Qubit[N];\n Kata.PrepareQubits(qs, bases, bits);\n Adjoint PrepareQubits_Reference(qs, bases, bits);\n for i in 0..N - 1 {\n if not CheckZero(qs[i]) {\n Message($\"Qubit qs[{i}] prepared in incorrect state.\");\n Message($\"Expected state {StateToString(bases[i], bits[i])}; actual state\");\n PrepareQubits_Reference([qs[i]], [bases[i]], [bits[i]]);\n DumpRegister([qs[i]]);\n ResetAll(qs);\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9391
9391
  },
9392
9392
  {
9393
9393
  "id": "key_distribution__measure_qubits__Verification.qs",
9394
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 2 .. 10 {\n let (bases, bits) = (RandomArray(N), RandomArray(N));\n use qs = Qubit[N];\n PrepareQubits_Reference(qs, bases, bits);\n let res = Kata.MeasureQubits(qs, bases);\n ResetAll(qs);\n\n if Length(res) != N {\n Message($\"The returned array should have length {N}, same as the inputs, and it had length {Length(res)}.\");\n return false;\n }\n\n for i in 0 .. N - 1 {\n if res[i] != bits[i] {\n Message($\"Qubit qs[{i}] measured in incorrect basis.\");\n Message($\"When measuring state {StateToString(bases[i], bits[i])} in the {BasisToString(bases[i])} basis, \" +\n $\"expected result is {bits[i]}, got {res[i]}.\");\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9394
+ "code": "namespace Kata.Verification {\n import Std.Diagnostics.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 2..10 {\n let (bases, bits) = (RandomArray(N), RandomArray(N));\n use qs = Qubit[N];\n PrepareQubits_Reference(qs, bases, bits);\n let res = Kata.MeasureQubits(qs, bases);\n ResetAll(qs);\n\n if Length(res) != N {\n Message($\"The returned array should have length {N}, same as the inputs, and it had length {Length(res)}.\");\n return false;\n }\n\n for i in 0..N - 1 {\n if res[i] != bits[i] {\n Message($\"Qubit qs[{i}] measured in incorrect basis.\");\n Message($\"When measuring state {StateToString(bases[i], bits[i])} in the {BasisToString(bases[i])} basis, \" +\n $\"expected result is {bits[i]}, got {res[i]}.\");\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9395
9395
  },
9396
9396
  {
9397
9397
  "id": "key_distribution__shared_key__Verification.qs",
9398
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n\n function GenerateSharedKey_Reference(basesAlice : Bool[], basesBob : Bool[], bits : Bool[]) : Bool[] {\n mutable key = [];\n for i in 0 .. Length(bits) - 1 {\n if basesAlice[i] == basesBob[i] {\n set key += [bits[i]];\n }\n }\n\n return key;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n\n for N in 8 .. 20 {\n let basesAlice = RandomArray(N);\n let basesBob = RandomArray(N);\n let bits = RandomArray(N);\n let expected = GenerateSharedKey_Reference(basesAlice, basesBob, bits);\n let result = Kata.GenerateSharedKey(basesAlice, basesBob, bits);\n\n if Length(result) != Length(expected) or BoolArrayAsInt(result) != BoolArrayAsInt(expected) {\n Message($\"Unexpected key value\");\n Message($\"Alice's bases: {basesAlice}\");\n Message($\"Bob's bases: {basesBob}\");\n Message($\"Got key {result}, expected {expected}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9398
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n\n function GenerateSharedKey_Reference(basesAlice : Bool[], basesBob : Bool[], bits : Bool[]) : Bool[] {\n mutable key = [];\n for i in 0..Length(bits) - 1 {\n if basesAlice[i] == basesBob[i] {\n set key += [bits[i]];\n }\n }\n\n return key;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n\n for N in 8..20 {\n let basesAlice = RandomArray(N);\n let basesBob = RandomArray(N);\n let bits = RandomArray(N);\n let expected = GenerateSharedKey_Reference(basesAlice, basesBob, bits);\n let result = Kata.GenerateSharedKey(basesAlice, basesBob, bits);\n\n if Length(result) != Length(expected) or BoolArrayAsInt(result) != BoolArrayAsInt(expected) {\n Message($\"Unexpected key value\");\n Message($\"Alice's bases: {basesAlice}\");\n Message($\"Bob's bases: {basesBob}\");\n Message($\"Got key {result}, expected {expected}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9399
9399
  },
9400
9400
  {
9401
9401
  "id": "teleportation__entangled_pair__Verification.qs",
9402
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Katas;\n\n operation Entangle_Wrapper (qs : Qubit[]) : Unit is Adj {\n Kata.Entangle(qs[0], qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n \n return CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Entangle_Wrapper,\n EntangleWrapper_Reference,\n 2\n );\n \n }\n}\n"
9402
+ "code": "namespace Kata.Verification {\n import Std.Diagnostics.*;\n import KatasUtils.*;\n\n operation Entangle_Wrapper(qs : Qubit[]) : Unit is Adj {\n Kata.Entangle(qs[0], qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n\n return CheckOperationsEquivalenceOnZeroStateWithFeedback(\n Entangle_Wrapper,\n EntangleWrapper_Reference,\n 2\n );\n\n }\n}\n"
9403
9403
  },
9404
9404
  {
9405
9405
  "id": "teleportation__Common.qs",
9406
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Math;\n\n operation EntangleWrapper_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n let (qAlice, qBob) = (qs[0], qs[1]);\n H(qAlice);\n CNOT(qAlice, qBob);\n }\n\n // ------------------------------------------------------\n // Helper which prepares proper Bell state on two qubits\n // 0 - |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2)\n // 1 - |Φ⁻⟩ = (|00⟩ - |11⟩) / sqrt(2)\n // 2 - |Ψ⁺⟩ = (|01⟩ + |10⟩) / sqrt(2)\n // 3 - |Ψ⁻⟩ = (|01⟩ - |10⟩) / sqrt(2)\n operation StatePrep_BellState(q1 : Qubit, q2 : Qubit, state : Int) : Unit {\n H(q1);\n CNOT(q1, q2);\n \n // now we have |00⟩ + |11⟩ - modify it based on state arg\n if state % 2 == 1 {\n // negative phase\n Z(q2);\n }\n \n if state / 2 == 1 {\n X(q2);\n }\n }\n\n // ------------------------------------------------------\n // Helper operation that run teleportation using the given operations to prepare the message qubit\n // and the entangled pair, and to run sender and receiver parts of the protocol.\n operation ComposeTeleportation(\n bellPrepOp : ((Qubit, Qubit) => Unit), \n getDescriptionOp : ((Qubit, Qubit) => (Bool, Bool)), \n reconstructOp : ((Qubit, (Bool, Bool)) => Unit),\n qAlice : Qubit, \n qBob : Qubit, \n qMessage : Qubit) : Unit {\n\n bellPrepOp(qAlice, qBob);\n let classicalBits = getDescriptionOp(qAlice, qMessage);\n \n // Alice sends the classical bits to Bob.\n // Bob uses these bits to transform his part of the entangled pair into the message.\n reconstructOp(qBob, classicalBits);\n }\n \n operation SendMessage_Reference(qAlice: Qubit, qMessage: Qubit) : (Bool, Bool) {\n CNOT(qMessage, qAlice);\n H(qMessage);\n return (M(qMessage) == One, M(qAlice) == One);\n }\n\n operation ReconstructMessage_Reference(qBob : Qubit, (b1 : Bool, b2 : Bool)) : Unit {\n if b1 {\n Z(qBob);\n }\n if b2 {\n X(qBob);\n }\n }\n\n // ------------------------------------------------------\n // Helper operation that runs a teleportation operation (specified by teleportOp).\n // The state to teleport is set up using an operation (specified by setupPsiOp).\n //\n // Specifying the state to teleport through an operation allows to get the inverse\n // which makes testing easier.\n operation TeleportTestHelper(\n teleportOp : ((Qubit, Qubit, Qubit) => Unit), \n setupPsiOp : (Qubit => Unit is Adj),\n psiName: String) : Bool {\n \n use (qMessage, qAlice, qBob) = (Qubit(), Qubit(), Qubit());\n setupPsiOp(qMessage);\n \n // This should modify qBob to be identical to the state\n // of qMessage before the function call.\n teleportOp(qAlice, qBob, qMessage);\n \n // Applying the inverse of the setup operation to qBob\n // should make it Zero.\n Adjoint setupPsiOp(qBob);\n if not CheckZero(qBob) {\n Message($\"Incorrect. The state {psiName} was teleported incorrectly.\");\n setupPsiOp(qBob);\n Message(\"The state of the qubits [qMessage, qAlice, qBob] after teleportation:\");\n DumpMachine();\n ResetAll([qMessage, qAlice, qBob]);\n return false;\n }\n ResetAll([qMessage, qAlice, qBob]);\n return true;\n }\n\n // ------------------------------------------------------\n // Run teleportation for a number of different states.\n // After each teleportation success is asserted.\n // Also repeats for each state several times as\n // code is expected to take different paths each time because\n // measurements done by Alice are not deterministic.\n operation TeleportTestLoop(teleportOp : ((Qubit, Qubit, Qubit) => Unit)) : Bool {\n // Define setup operations for the message qubit\n // on which to test teleportation: |0⟩, |1⟩, |0⟩ + |1⟩, unequal superposition.\n let setupPsiOps = [(I, \"|0⟩\"), (X, \"|1⟩\"), (H, \"|+⟩\"), (Ry(ArcCos(0.6) * 2.0, _), \"0.6|0⟩ + 0.8|1⟩\")];\n \n // As part of teleportation Alice runs some measurements\n // with nondeterministic outcome.\n // Depending on the outcomes different paths are taken on Bob's side.\n // We repeat each test run several times to ensure that all paths are checked.\n let numRepetitions = 100;\n for (psiOp, psiName) in setupPsiOps {\n for j in 1 .. numRepetitions {\n if not TeleportTestHelper(teleportOp, psiOp, psiName) {\n return false;\n }\n }\n }\n Message(\"Correct.\");\n return true;\n }\n\n operation PrepareAndSendMessage_Reference(qAlice : Qubit, basis : Pauli, state : Result) : (Bool, Bool) {\n use qMessage = Qubit();\n if state == One {\n X(qMessage);\n }\n if basis != PauliZ {\n H(qMessage);\n }\n if basis == PauliY {\n S(qMessage);\n }\n let classicalBits = SendMessage_Reference(qAlice, qMessage);\n Reset(qMessage);\n return classicalBits;\n }\n\n operation ReconstructAndMeasureMessage_Reference(qBob : Qubit, (b1 : Bool, b2 : Bool), basis : Pauli) : Result {\n ReconstructMessage_Reference(qBob, (b1, b2));\n return Measure([basis], [qBob]);\n }\n\n // ------------------------------------------------------\n // Runs teleportation for each state that is to be prepared and\n // sent by Alice. Success is asserted after each teleportation.\n // Also repeats for each state several times; this is because\n // code is expected to take different paths each time since\n // measurements done by Alice are not deterministic.\n operation TeleportPreparedStateTestLoop(\n prepareAndSendMessageOp : ((Qubit, Pauli, Result) => (Bool, Bool)), \n reconstructAndMeasureMessageOp : ((Qubit, (Bool, Bool), Pauli) => Result)\n ) : Bool {\n \n let messages = [(PauliX, Zero, \"|+⟩\"), \n (PauliX, One, \"|-⟩\"), \n (PauliY, Zero, \"|i⟩\"), \n (PauliY, One, \"|-i⟩\"), \n (PauliZ, Zero, \"|0⟩\"), \n (PauliZ, One, \"|1⟩\")];\n let numRepetitions = 100;\n use (qAlice, qBob) = (Qubit(), Qubit());\n for (basis, sentState, stateName) in messages {\n for j in 1 .. numRepetitions {\n StatePrep_BellState(qAlice, qBob, 0);\n let classicalBits = prepareAndSendMessageOp(qAlice, basis, sentState);\n let receivedState = reconstructAndMeasureMessageOp(qBob, classicalBits, basis);\n if sentState != receivedState {\n Message($\"Received incorrect basis state when sending {stateName} in the {basis} basis.\");\n ResetAll([qAlice, qBob]);\n return false;\n }\n ResetAll([qAlice, qBob]);\n }\n }\n Message($\"Correct\");\n return true;\n }\n\n operation EntangleThreeQubitsWrapper_Reference(qs : Qubit[]) : Unit is Adj {\n let (qAlice, qBob, qCharlie) = (qs[0], qs[1], qs[2]);\n // Starting with |000⟩\n H(qBob);\n // Now state is 1/sqrt(2) (|000⟩ + |010⟩)\n CNOT(qBob, qCharlie);\n // 1/sqrt(2) (|000⟩ + |011⟩)\n H(qAlice);\n // 1/2 (|000⟩ + |011⟩ + |100⟩ + |111⟩)\n CNOT(qAlice, qCharlie);\n // Final state: 1/2 (|000⟩ + |011⟩ + |101⟩ + |110⟩)\n }\n}"
9406
+ "code": "namespace Kata.Verification {\n import Std.Diagnostics.*;\n import Std.Math.*;\n\n operation EntangleWrapper_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n let (qAlice, qBob) = (qs[0], qs[1]);\n H(qAlice);\n CNOT(qAlice, qBob);\n }\n\n // ------------------------------------------------------\n // Helper which prepares proper Bell state on two qubits\n // 0 - |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2)\n // 1 - |Φ⁻⟩ = (|00⟩ - |11⟩) / sqrt(2)\n // 2 - |Ψ⁺⟩ = (|01⟩ + |10⟩) / sqrt(2)\n // 3 - |Ψ⁻⟩ = (|01⟩ - |10⟩) / sqrt(2)\n operation StatePrep_BellState(q1 : Qubit, q2 : Qubit, state : Int) : Unit {\n H(q1);\n CNOT(q1, q2);\n\n // now we have |00⟩ + |11⟩ - modify it based on state arg\n if state % 2 == 1 {\n // negative phase\n Z(q2);\n }\n\n if state / 2 == 1 {\n X(q2);\n }\n }\n\n // ------------------------------------------------------\n // Helper operation that run teleportation using the given operations to prepare the message qubit\n // and the entangled pair, and to run sender and receiver parts of the protocol.\n operation ComposeTeleportation(\n bellPrepOp : ((Qubit, Qubit) => Unit),\n getDescriptionOp : ((Qubit, Qubit) => (Bool, Bool)),\n reconstructOp : ((Qubit, (Bool, Bool)) => Unit),\n qAlice : Qubit,\n qBob : Qubit,\n qMessage : Qubit\n ) : Unit {\n\n bellPrepOp(qAlice, qBob);\n let classicalBits = getDescriptionOp(qAlice, qMessage);\n\n // Alice sends the classical bits to Bob.\n // Bob uses these bits to transform his part of the entangled pair into the message.\n reconstructOp(qBob, classicalBits);\n }\n\n operation SendMessage_Reference(qAlice : Qubit, qMessage : Qubit) : (Bool, Bool) {\n CNOT(qMessage, qAlice);\n H(qMessage);\n return (M(qMessage) == One, M(qAlice) == One);\n }\n\n operation ReconstructMessage_Reference(qBob : Qubit, (b1 : Bool, b2 : Bool)) : Unit {\n if b1 {\n Z(qBob);\n }\n if b2 {\n X(qBob);\n }\n }\n\n // ------------------------------------------------------\n // Helper operation that runs a teleportation operation (specified by teleportOp).\n // The state to teleport is set up using an operation (specified by setupPsiOp).\n //\n // Specifying the state to teleport through an operation allows to get the inverse\n // which makes testing easier.\n operation TeleportTestHelper(\n teleportOp : ((Qubit, Qubit, Qubit) => Unit),\n setupPsiOp : (Qubit => Unit is Adj),\n psiName : String\n ) : Bool {\n\n use (qMessage, qAlice, qBob) = (Qubit(), Qubit(), Qubit());\n setupPsiOp(qMessage);\n\n // This should modify qBob to be identical to the state\n // of qMessage before the function call.\n teleportOp(qAlice, qBob, qMessage);\n\n // Applying the inverse of the setup operation to qBob\n // should make it Zero.\n Adjoint setupPsiOp(qBob);\n if not CheckZero(qBob) {\n Message($\"Incorrect. The state {psiName} was teleported incorrectly.\");\n setupPsiOp(qBob);\n Message(\"The state of the qubits [qMessage, qAlice, qBob] after teleportation:\");\n DumpMachine();\n ResetAll([qMessage, qAlice, qBob]);\n return false;\n }\n ResetAll([qMessage, qAlice, qBob]);\n return true;\n }\n\n // ------------------------------------------------------\n // Run teleportation for a number of different states.\n // After each teleportation success is asserted.\n // Also repeats for each state several times as\n // code is expected to take different paths each time because\n // measurements done by Alice are not deterministic.\n operation TeleportTestLoop(teleportOp : ((Qubit, Qubit, Qubit) => Unit)) : Bool {\n // Define setup operations for the message qubit\n // on which to test teleportation: |0⟩, |1⟩, |0⟩ + |1⟩, unequal superposition.\n let setupPsiOps = [(I, \"|0⟩\"), (X, \"|1⟩\"), (H, \"|+⟩\"), (Ry(ArcCos(0.6) * 2.0, _), \"0.6|0⟩ + 0.8|1⟩\")];\n\n // As part of teleportation Alice runs some measurements\n // with nondeterministic outcome.\n // Depending on the outcomes different paths are taken on Bob's side.\n // We repeat each test run several times to ensure that all paths are checked.\n let numRepetitions = 100;\n for (psiOp, psiName) in setupPsiOps {\n for j in 1..numRepetitions {\n if not TeleportTestHelper(teleportOp, psiOp, psiName) {\n return false;\n }\n }\n }\n Message(\"Correct.\");\n return true;\n }\n\n operation PrepareAndSendMessage_Reference(qAlice : Qubit, basis : Pauli, state : Result) : (Bool, Bool) {\n use qMessage = Qubit();\n if state == One {\n X(qMessage);\n }\n if basis != PauliZ {\n H(qMessage);\n }\n if basis == PauliY {\n S(qMessage);\n }\n let classicalBits = SendMessage_Reference(qAlice, qMessage);\n Reset(qMessage);\n return classicalBits;\n }\n\n operation ReconstructAndMeasureMessage_Reference(qBob : Qubit, (b1 : Bool, b2 : Bool), basis : Pauli) : Result {\n ReconstructMessage_Reference(qBob, (b1, b2));\n return Measure([basis], [qBob]);\n }\n\n // ------------------------------------------------------\n // Runs teleportation for each state that is to be prepared and\n // sent by Alice. Success is asserted after each teleportation.\n // Also repeats for each state several times; this is because\n // code is expected to take different paths each time since\n // measurements done by Alice are not deterministic.\n operation TeleportPreparedStateTestLoop(\n prepareAndSendMessageOp : ((Qubit, Pauli, Result) => (Bool, Bool)),\n reconstructAndMeasureMessageOp : ((Qubit, (Bool, Bool), Pauli) => Result)\n ) : Bool {\n\n let messages = [\n (PauliX, Zero, \"|+⟩\"),\n (PauliX, One, \"|-⟩\"),\n (PauliY, Zero, \"|i⟩\"),\n (PauliY, One, \"|-i⟩\"),\n (PauliZ, Zero, \"|0⟩\"),\n (PauliZ, One, \"|1⟩\")\n ];\n let numRepetitions = 100;\n use (qAlice, qBob) = (Qubit(), Qubit());\n for (basis, sentState, stateName) in messages {\n for j in 1..numRepetitions {\n StatePrep_BellState(qAlice, qBob, 0);\n let classicalBits = prepareAndSendMessageOp(qAlice, basis, sentState);\n let receivedState = reconstructAndMeasureMessageOp(qBob, classicalBits, basis);\n if sentState != receivedState {\n Message($\"Received incorrect basis state when sending {stateName} in the {basis} basis.\");\n ResetAll([qAlice, qBob]);\n return false;\n }\n ResetAll([qAlice, qBob]);\n }\n }\n Message($\"Correct\");\n return true;\n }\n\n operation EntangleThreeQubitsWrapper_Reference(qs : Qubit[]) : Unit is Adj {\n let (qAlice, qBob, qCharlie) = (qs[0], qs[1], qs[2]);\n // Starting with |000⟩\n H(qBob);\n // Now state is 1/sqrt(2) (|000⟩ + |010⟩)\n CNOT(qBob, qCharlie);\n // 1/sqrt(2) (|000⟩ + |011⟩)\n H(qAlice);\n // 1/2 (|000⟩ + |011⟩ + |100⟩ + |111⟩)\n CNOT(qAlice, qCharlie);\n // Final state: 1/2 (|000⟩ + |011⟩ + |101⟩ + |110⟩)\n }\n}\n"
9407
9407
  },
9408
9408
  {
9409
9409
  "id": "teleportation__send_message__Verification.qs",
9410
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool { \n let teleport = ComposeTeleportation(StatePrep_BellState(_, _, 0), Kata.SendMessage, ReconstructMessage_Reference, _, _, _);\n return TeleportTestLoop(teleport); \n }\n}"
9410
+ "code": "namespace Kata.Verification {\n import Std.Diagnostics.*;\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let teleport = ComposeTeleportation(StatePrep_BellState(_, _, 0), Kata.SendMessage, ReconstructMessage_Reference, _, _, _);\n return TeleportTestLoop(teleport);\n }\n}\n"
9411
9411
  },
9412
9412
  {
9413
9413
  "id": "teleportation__reconstruct_message__Verification.qs",
9414
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let teleport = ComposeTeleportation(StatePrep_BellState(_, _, 0), SendMessage_Reference, Kata.ReconstructMessage, _, _, _);\n return TeleportTestLoop(teleport); \n }\n}"
9414
+ "code": "namespace Kata.Verification {\n import Std.Diagnostics.*;\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let teleport = ComposeTeleportation(StatePrep_BellState(_, _, 0), SendMessage_Reference, Kata.ReconstructMessage, _, _, _);\n return TeleportTestLoop(teleport);\n }\n}\n"
9415
9415
  },
9416
9416
  {
9417
9417
  "id": "teleportation__standard_teleportation_protocol__Verification.qs",
9418
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let teleport = Kata.StandardTeleport;\n return TeleportTestLoop(teleport);\n }\n\n}"
9418
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let teleport = Kata.StandardTeleport;\n return TeleportTestLoop(teleport);\n }\n\n}\n"
9419
9419
  },
9420
9420
  {
9421
9421
  "id": "teleportation__prepare_and_send_message__Verification.qs",
9422
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n return TeleportPreparedStateTestLoop(Kata.PrepareAndSendMessage, ReconstructAndMeasureMessage_Reference);\n }\n\n}"
9422
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n return TeleportPreparedStateTestLoop(Kata.PrepareAndSendMessage, ReconstructAndMeasureMessage_Reference);\n }\n\n}\n"
9423
9423
  },
9424
9424
  {
9425
9425
  "id": "teleportation__reconstruct_and_measure_message__Verification.qs",
9426
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n return TeleportPreparedStateTestLoop(PrepareAndSendMessage_Reference, Kata.ReconstructAndMeasureMessage);\n }\n\n}"
9426
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n return TeleportPreparedStateTestLoop(PrepareAndSendMessage_Reference, Kata.ReconstructAndMeasureMessage);\n }\n\n}\n"
9427
9427
  },
9428
9428
  {
9429
9429
  "id": "teleportation__reconstruct_message_phi_minus__Verification.qs",
9430
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let teleport = ComposeTeleportation(StatePrep_BellState(_, _, 1), SendMessage_Reference, Kata.ReconstructMessage_PhiMinus, _, _, _);\n return TeleportTestLoop(teleport); \n }\n}"
9430
+ "code": "namespace Kata.Verification {\n import Std.Diagnostics.*;\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let teleport = ComposeTeleportation(StatePrep_BellState(_, _, 1), SendMessage_Reference, Kata.ReconstructMessage_PhiMinus, _, _, _);\n return TeleportTestLoop(teleport);\n }\n}\n"
9431
9431
  },
9432
9432
  {
9433
9433
  "id": "teleportation__reconstruct_message_psi_plus__Verification.qs",
9434
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let teleport = ComposeTeleportation(StatePrep_BellState(_, _, 2), SendMessage_Reference, Kata.ReconstructMessage_PsiPlus, _, _, _);\n return TeleportTestLoop(teleport); \n }\n}"
9434
+ "code": "namespace Kata.Verification {\n import Std.Diagnostics.*;\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let teleport = ComposeTeleportation(StatePrep_BellState(_, _, 2), SendMessage_Reference, Kata.ReconstructMessage_PsiPlus, _, _, _);\n return TeleportTestLoop(teleport);\n }\n}\n"
9435
9435
  },
9436
9436
  {
9437
9437
  "id": "teleportation__reconstruct_message_psi_minus__Verification.qs",
9438
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let teleport = ComposeTeleportation(StatePrep_BellState(_, _, 3), SendMessage_Reference, Kata.ReconstructMessage_PsiMinus, _, _, _);\n return TeleportTestLoop(teleport); \n }\n}"
9438
+ "code": "namespace Kata.Verification {\n import Std.Diagnostics.*;\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let teleport = ComposeTeleportation(StatePrep_BellState(_, _, 3), SendMessage_Reference, Kata.ReconstructMessage_PsiMinus, _, _, _);\n return TeleportTestLoop(teleport);\n }\n}\n"
9439
9439
  },
9440
9440
  {
9441
9441
  "id": "teleportation__entanglement_swapping__Verification.qs",
9442
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Katas;\n\n operation TeleportEntanglementSwappingTestLoop(\n entanglementSwapping : ((Qubit, Qubit) => Int, (Qubit, Int) => Unit)\n ) : Bool {\n \n for i in 1 .. 15 {\n use (qAlice1, qAlice2) = (Qubit(), Qubit());\n EntangleWrapper_Reference([qAlice1, qAlice2]);\n \n use (qBob1, qBob2) = (Qubit(), Qubit());\n EntangleWrapper_Reference([qBob1, qBob2]);\n \n let (teleportOp, adjustOp) = entanglementSwapping;\n // Apply the operations returned by the solution: \n // first Charlie's side, then Bob's side.\n let result = teleportOp(qAlice1, qBob1);\n adjustOp(qBob2, result);\n\n // Apply adjoint of the operation that prepares the |Φ⁺⟩ state:\n // if the state of Alice's and Bob's qubits was |Φ⁺⟩,\n // their state should become |00⟩ now.\n Adjoint EntangleWrapper_Reference([qAlice2, qBob2]);\n \n // Assert that Alice's and Bob's qubits end up in |0⟩ state.\n if not CheckAllZero([qAlice2, qBob2]) {\n Message($\"Incorrect.\");\n Message($\"Entanglement swapping was not successful, as qubits qAlice2 and qBob2 didn't end up in the state |Φ⁺⟩ = 1/sqrt(2) (|00⟩ + |11⟩)\");\n EntangleWrapper_Reference([qAlice2, qBob2]);\n Message(\"The state of the qubits [qAlice1, qAlice2, qBob1, qBob2] after teleportation:\");\n DumpMachine();\n ResetAll([qAlice1, qAlice2, qBob1, qBob2]);\n return false;\n }\n \n Message($\"Correct.\");\n ResetAll([qAlice1, qAlice2, qBob1, qBob2]);\n return true;\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n return TeleportEntanglementSwappingTestLoop(Kata.EntanglementSwapping());\n }\n}"
9442
+ "code": "namespace Kata.Verification {\n import Std.Diagnostics.*;\n import KatasUtils.*;\n\n operation TeleportEntanglementSwappingTestLoop(\n entanglementSwapping : ((Qubit, Qubit) => Int, (Qubit, Int) => Unit)\n ) : Bool {\n\n for i in 1..15 {\n use (qAlice1, qAlice2) = (Qubit(), Qubit());\n EntangleWrapper_Reference([qAlice1, qAlice2]);\n\n use (qBob1, qBob2) = (Qubit(), Qubit());\n EntangleWrapper_Reference([qBob1, qBob2]);\n\n let (teleportOp, adjustOp) = entanglementSwapping;\n // Apply the operations returned by the solution:\n // first Charlie's side, then Bob's side.\n let result = teleportOp(qAlice1, qBob1);\n adjustOp(qBob2, result);\n\n // Apply adjoint of the operation that prepares the |Φ⁺⟩ state:\n // if the state of Alice's and Bob's qubits was |Φ⁺⟩,\n // their state should become |00⟩ now.\n Adjoint EntangleWrapper_Reference([qAlice2, qBob2]);\n\n // Assert that Alice's and Bob's qubits end up in |0⟩ state.\n if not CheckAllZero([qAlice2, qBob2]) {\n Message($\"Incorrect.\");\n Message($\"Entanglement swapping was not successful, as qubits qAlice2 and qBob2 didn't end up in the state |Φ⁺⟩ = 1/sqrt(2) (|00⟩ + |11⟩)\");\n EntangleWrapper_Reference([qAlice2, qBob2]);\n Message(\"The state of the qubits [qAlice1, qAlice2, qBob1, qBob2] after teleportation:\");\n DumpMachine();\n ResetAll([qAlice1, qAlice2, qBob1, qBob2]);\n return false;\n }\n\n Message($\"Correct.\");\n ResetAll([qAlice1, qAlice2, qBob1, qBob2]);\n return true;\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n return TeleportEntanglementSwappingTestLoop(Kata.EntanglementSwapping());\n }\n}\n"
9443
9443
  },
9444
9444
  {
9445
9445
  "id": "teleportation__entangled_trio__Verification.qs",
9446
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n \n operation EntangleThreeQubits_Wrapper(qs : Qubit[]) : Unit is Adj {\n Kata.EntangleThreeQubits(qs[0], qs[1], qs[2]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n return CheckOperationsEquivalenceOnZeroStateWithFeedback(\n EntangleThreeQubits_Wrapper,\n EntangleThreeQubitsWrapper_Reference,\n 3\n );\n \n }\n}"
9446
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation EntangleThreeQubits_Wrapper(qs : Qubit[]) : Unit is Adj {\n Kata.EntangleThreeQubits(qs[0], qs[1], qs[2]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n return CheckOperationsEquivalenceOnZeroStateWithFeedback(\n EntangleThreeQubits_Wrapper,\n EntangleThreeQubitsWrapper_Reference,\n 3\n );\n\n }\n}\n"
9447
9447
  },
9448
9448
  {
9449
9449
  "id": "teleportation__reconstruct_message_charlie__Verification.qs",
9450
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Katas;\n \n operation ReconstructMessageWhenThreeEntangledQubitsTestLoop(\n reconstructMessage : (Qubit, (Bool, Bool), Bool) => Unit\n ) : Bool {\n \n let setupPsiOps = [(I, \"|0⟩\"), (X, \"|1⟩\"), (H, \"|+⟩\"), (Ry(ArcCos(0.6) * 2.0, _), \"0.6|0⟩ + 0.8|1⟩\")];\n let numRepetitions = 100;\n \n for (psiOp, psiName) in setupPsiOps {\n for j in 1 .. numRepetitions {\n use (qMessage, qAlice, qBob, qCharlie) = (Qubit(), Qubit(), Qubit(), Qubit());\n psiOp(qMessage);\n EntangleThreeQubitsWrapper_Reference([qAlice, qBob, qCharlie]);\n let (b1, b2) = SendMessage_Reference(qAlice, qMessage);\n let b3 = ResultAsBool(M(qBob));\n reconstructMessage(qCharlie, (b1, b2), b3);\n Adjoint psiOp(qCharlie);\n if not CheckZero(qCharlie) {\n Message($\"Incorrect. The state {psiName} was teleported incorrectly.\");\n psiOp(qCharlie);\n Message(\"The state of the qubits [qMessage, qAlice, qBob, qCharlie] after teleportation:\");\n DumpMachine();\n ResetAll([qMessage, qAlice, qBob, qCharlie]);\n return false;\n }\n ResetAll([qMessage, qAlice, qBob, qCharlie]);\n }\n }\n Message($\"Correct!\");\n return true;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n return ReconstructMessageWhenThreeEntangledQubitsTestLoop(Kata.ReconstructMessageWhenThreeEntangledQubits);\n }\n}"
9450
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import Std.Diagnostics.*;\n import Std.Math.*;\n import KatasUtils.*;\n\n operation ReconstructMessageWhenThreeEntangledQubitsTestLoop(\n reconstructMessage : (Qubit, (Bool, Bool), Bool) => Unit\n ) : Bool {\n\n let setupPsiOps = [(I, \"|0⟩\"), (X, \"|1⟩\"), (H, \"|+⟩\"), (Ry(ArcCos(0.6) * 2.0, _), \"0.6|0⟩ + 0.8|1⟩\")];\n let numRepetitions = 100;\n\n for (psiOp, psiName) in setupPsiOps {\n for j in 1..numRepetitions {\n use (qMessage, qAlice, qBob, qCharlie) = (Qubit(), Qubit(), Qubit(), Qubit());\n psiOp(qMessage);\n EntangleThreeQubitsWrapper_Reference([qAlice, qBob, qCharlie]);\n let (b1, b2) = SendMessage_Reference(qAlice, qMessage);\n let b3 = ResultAsBool(M(qBob));\n reconstructMessage(qCharlie, (b1, b2), b3);\n Adjoint psiOp(qCharlie);\n if not CheckZero(qCharlie) {\n Message($\"Incorrect. The state {psiName} was teleported incorrectly.\");\n psiOp(qCharlie);\n Message(\"The state of the qubits [qMessage, qAlice, qBob, qCharlie] after teleportation:\");\n DumpMachine();\n ResetAll([qMessage, qAlice, qBob, qCharlie]);\n return false;\n }\n ResetAll([qMessage, qAlice, qBob, qCharlie]);\n }\n }\n Message($\"Correct!\");\n return true;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n return ReconstructMessageWhenThreeEntangledQubitsTestLoop(Kata.ReconstructMessageWhenThreeEntangledQubits);\n }\n}\n"
9451
9451
  },
9452
9452
  {
9453
9453
  "id": "teleportation__measurement_free_teleportation__Verification.qs",
9454
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Math;\n \n operation MeasurementFreeTeleportTestLoop(\n measurementFreeTeleport : (Qubit, Qubit, Qubit) => Unit\n ) : Bool {\n let setupPsiOps = [(I, \"|0⟩\"), (X, \"|1⟩\"), (H, \"|+⟩\"), (Ry(ArcCos(0.6) * 2.0, _), \"0.6|0⟩ + 0.8|1⟩\")];\n let numRepetitions = 100;\n \n for (psiOp, psiName) in setupPsiOps {\n for j in 1 .. numRepetitions {\n use (qMessage, qAlice, qBob) = (Qubit(), Qubit(), Qubit());\n psiOp(qMessage);\n StatePrep_BellState(qAlice, qBob, 0);\n measurementFreeTeleport(qAlice, qBob, qMessage);\n Adjoint psiOp(qBob);\n if not CheckZero(qBob) {\n Message($\"Incorrect. The state {psiName} was teleported incorrectly.\");\n psiOp(qBob);\n Message(\"The state of the qubits [qMessage, qAlice, qBob] after teleportation:\");\n DumpMachine();\n ResetAll([qMessage, qAlice, qBob]);\n return false;\n } \n ResetAll([qMessage, qAlice, qBob]);\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n return MeasurementFreeTeleportTestLoop(Kata.MeasurementFreeTeleport); \n }\n}"
9454
+ "code": "namespace Kata.Verification {\n import Std.Diagnostics.*;\n import Std.Math.*;\n\n operation MeasurementFreeTeleportTestLoop(\n measurementFreeTeleport : (Qubit, Qubit, Qubit) => Unit\n ) : Bool {\n let setupPsiOps = [(I, \"|0⟩\"), (X, \"|1⟩\"), (H, \"|+⟩\"), (Ry(ArcCos(0.6) * 2.0, _), \"0.6|0⟩ + 0.8|1⟩\")];\n let numRepetitions = 100;\n\n for (psiOp, psiName) in setupPsiOps {\n for j in 1..numRepetitions {\n use (qMessage, qAlice, qBob) = (Qubit(), Qubit(), Qubit());\n psiOp(qMessage);\n StatePrep_BellState(qAlice, qBob, 0);\n measurementFreeTeleport(qAlice, qBob, qMessage);\n Adjoint psiOp(qBob);\n if not CheckZero(qBob) {\n Message($\"Incorrect. The state {psiName} was teleported incorrectly.\");\n psiOp(qBob);\n Message(\"The state of the qubits [qMessage, qAlice, qBob] after teleportation:\");\n DumpMachine();\n ResetAll([qMessage, qAlice, qBob]);\n return false;\n }\n ResetAll([qMessage, qAlice, qBob]);\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n return MeasurementFreeTeleportTestLoop(Kata.MeasurementFreeTeleport);\n }\n}\n"
9455
9455
  },
9456
9456
  {
9457
9457
  "id": "superdense_coding__entangled_pair__Verification.qs",
9458
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation CreateEntangledPair_Wrapper(qs : Qubit[]) : Unit is Adj {\n Kata.CreateEntangledPair(qs[0], qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n\n return CheckOperationsEquivalenceOnZeroStateWithFeedback(\n CreateEntangledPair_Wrapper,\n CreateEntangledPairWrapper_Reference,\n 2\n );\n }\n}\n"
9458
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation CreateEntangledPair_Wrapper(qs : Qubit[]) : Unit is Adj {\n Kata.CreateEntangledPair(qs[0], qs[1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n\n return CheckOperationsEquivalenceOnZeroStateWithFeedback(\n CreateEntangledPair_Wrapper,\n CreateEntangledPairWrapper_Reference,\n 2\n );\n }\n}\n"
9459
9459
  },
9460
9460
  {
9461
9461
  "id": "superdense_coding__Common.qs",
@@ -9475,7 +9475,7 @@ export default {
9475
9475
  },
9476
9476
  {
9477
9477
  "id": "oracles__classical_oracles__Verification.qs",
9478
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n\n function IsSeven_Reference(x : Bool[]) : Bool {\n return BoolArrayAsInt(x) == 7;\n }\n\n @EntryPoint()\n function CheckSolution() : Bool {\n let N = 3;\n for k in 0 .. 2^N - 1 {\n let x = IntAsBoolArray(k, N);\n\n let actual = Kata.IsSeven(x);\n let expected = IsSeven_Reference(x);\n\n if actual != expected {\n Message($\"Failed on test case x = {x}: got {actual}, expected {expected}\");\n return false;\n }\n }\n Message(\"Correct!\");\n true\n }\n}\n"
9478
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n\n function IsSeven_Reference(x : Bool[]) : Bool {\n return BoolArrayAsInt(x) == 7;\n }\n\n @EntryPoint()\n function CheckSolution() : Bool {\n let N = 3;\n for k in 0..2^N - 1 {\n let x = IntAsBoolArray(k, N);\n\n let actual = Kata.IsSeven(x);\n let expected = IsSeven_Reference(x);\n\n if actual != expected {\n Message($\"Failed on test case x = {x}: got {actual}, expected {expected}\");\n return false;\n }\n }\n Message(\"Correct!\");\n true\n }\n}\n"
9479
9479
  },
9480
9480
  {
9481
9481
  "id": "oracles__Common.qs",
@@ -9483,43 +9483,43 @@ export default {
9483
9483
  },
9484
9484
  {
9485
9485
  "id": "oracles__phase_oracle_seven__Verification.qs",
9486
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation IsSeven_PhaseOracle_Reference(x : Qubit[]) : Unit is Adj + Ctl {\n Controlled Z(Most(x), Tail(x));\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let N = 3;\n let isCorrect = CheckOperationsAreEqualStrict(\n 3,\n Kata.IsSeven_PhaseOracle,\n IsSeven_PhaseOracle_Reference);\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine how your solution transforms the given state and compare it with the expected \" +\n \"transformation\");\n ShowQuantumStateComparison(3, PrepDemoState, Kata.IsSeven_PhaseOracle, IsSeven_PhaseOracle_Reference);\n }\n isCorrect\n }\n}\n"
9486
+ "code": "namespace Kata.Verification {\n import Std.Arrays.*;\n import KatasUtils.*;\n import Std.Math.*;\n\n operation IsSeven_PhaseOracle_Reference(x : Qubit[]) : Unit is Adj + Ctl {\n Controlled Z(Most(x), Tail(x));\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let N = 3;\n let isCorrect = CheckOperationsAreEqualStrict(\n 3,\n Kata.IsSeven_PhaseOracle,\n IsSeven_PhaseOracle_Reference\n );\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine how your solution transforms the given state and compare it with the expected \" +\n \"transformation\");\n ShowQuantumStateComparison(3, PrepDemoState, Kata.IsSeven_PhaseOracle, IsSeven_PhaseOracle_Reference);\n }\n isCorrect\n }\n}\n"
9487
9487
  },
9488
9488
  {
9489
9489
  "id": "oracles__marking_oracle_seven__Verification.qs",
9490
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation IsSeven_MarkingOracle_Reference(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n Controlled X(x, y);\n }\n\n // ------------------------------------------------------\n @EntryPoint()\n operation CheckSolution () : Bool {\n let N = 3;\n let sol = ApplyOracle(_, Kata.IsSeven_MarkingOracle);\n let ref = ApplyOracle(_, IsSeven_MarkingOracle_Reference);\n let isCorrect = CheckOperationsAreEqualStrict(N + 1, sol, ref);\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine how your solution transforms the given state and compare it with the expected \" +\n \"transformation\");\n ShowQuantumStateComparison(4, qs => PrepDemoState(qs[...2]), sol, ref);\n }\n isCorrect\n }\n}\n"
9490
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Math.*;\n\n operation IsSeven_MarkingOracle_Reference(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n Controlled X(x, y);\n }\n\n // ------------------------------------------------------\n @EntryPoint()\n operation CheckSolution() : Bool {\n let N = 3;\n let sol = ApplyOracle(_, Kata.IsSeven_MarkingOracle);\n let ref = ApplyOracle(_, IsSeven_MarkingOracle_Reference);\n let isCorrect = CheckOperationsAreEqualStrict(N + 1, sol, ref);\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine how your solution transforms the given state and compare it with the expected \" +\n \"transformation\");\n ShowQuantumStateComparison(4, qs => PrepDemoState(qs[...2]), sol, ref);\n }\n isCorrect\n }\n}\n"
9491
9491
  },
9492
9492
  {
9493
9493
  "id": "oracles__marking_oracle_as_phase__Verification.qs",
9494
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Random;\n\n operation ApplyMarkingOracleAsPhaseOracle_Reference(\n markingOracle : ((Qubit[], Qubit) => Unit is Adj + Ctl),\n qubits : Qubit[]) : Unit is Adj + Ctl {\n\n use minus = Qubit();\n within {\n X(minus);\n H(minus);\n } apply {\n markingOracle(qubits, minus);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 1 .. 3 {\n for k in 0 .. 2^N - 1 {\n let pattern = IntAsBoolArray(k, N);\n let marking = ApplyControlledOnBitString(pattern, X, _, _);\n let sol = Kata.ApplyMarkingOracleAsPhaseOracle(marking, _);\n let ref = ApplyMarkingOracleAsPhaseOracle_Reference(marking, _);\n\n let isCorrect = CheckOperationsAreEqualStrict(N, sol, ref);\n\n if not isCorrect {\n Message(\"Incorrect.\");\n Message(\"Hint: examine how your solution transforms the given state and compare it with the expected \" +\n $\"transformation for the {N}-bit oracle that marks the bit string {pattern}\");\n ShowQuantumStateComparison(N, PrepDemoState, sol, ref);\n return false;\n }\n }\n }\n Message(\"Correct!\");\n true\n }\n\n}\n"
9494
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import KatasUtils.*;\n import Std.Random.*;\n\n operation ApplyMarkingOracleAsPhaseOracle_Reference(\n markingOracle : ((Qubit[], Qubit) => Unit is Adj + Ctl),\n qubits : Qubit[]\n ) : Unit is Adj + Ctl {\n\n use minus = Qubit();\n within {\n X(minus);\n H(minus);\n } apply {\n markingOracle(qubits, minus);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 1..3 {\n for k in 0..2^N - 1 {\n let pattern = IntAsBoolArray(k, N);\n let marking = ApplyControlledOnBitString(pattern, X, _, _);\n let sol = Kata.ApplyMarkingOracleAsPhaseOracle(marking, _);\n let ref = ApplyMarkingOracleAsPhaseOracle_Reference(marking, _);\n\n let isCorrect = CheckOperationsAreEqualStrict(N, sol, ref);\n\n if not isCorrect {\n Message(\"Incorrect.\");\n Message(\"Hint: examine how your solution transforms the given state and compare it with the expected \" +\n $\"transformation for the {N}-bit oracle that marks the bit string {pattern}\");\n ShowQuantumStateComparison(N, PrepDemoState, sol, ref);\n return false;\n }\n }\n }\n Message(\"Correct!\");\n true\n }\n\n}\n"
9495
9495
  },
9496
9496
  {
9497
9497
  "id": "oracles__or_oracle__Verification.qs",
9498
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Random;\n\n operation Or_Oracle_Reference(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n X(y);\n ApplyControlledOnInt(0, X, x, y);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 1 .. 3 {\n let sol = ApplyOracle(_, Kata.Or_Oracle);\n let ref = ApplyOracle(_, Or_Oracle_Reference);\n let isCorrect = CheckOperationsAreEqualStrict(N + 1, sol, ref);\n\n if not isCorrect {\n Message(\"Incorrect.\");\n Message(\"Hint: examine how your solution transforms the given state and compare it with the expected \" +\n $\"transformation for the {N}-bit oracle\");\n ShowQuantumStateComparison(N + 1, qs => PrepDemoState(qs[...N - 1]), sol, ref);\n return false;\n }\n }\n Message(\"Correct!\");\n true\n }\n}\n"
9498
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Random.*;\n\n operation Or_Oracle_Reference(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n X(y);\n ApplyControlledOnInt(0, X, x, y);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 1..3 {\n let sol = ApplyOracle(_, Kata.Or_Oracle);\n let ref = ApplyOracle(_, Or_Oracle_Reference);\n let isCorrect = CheckOperationsAreEqualStrict(N + 1, sol, ref);\n\n if not isCorrect {\n Message(\"Incorrect.\");\n Message(\"Hint: examine how your solution transforms the given state and compare it with the expected \" +\n $\"transformation for the {N}-bit oracle\");\n ShowQuantumStateComparison(N + 1, qs => PrepDemoState(qs[...N - 1]), sol, ref);\n return false;\n }\n }\n Message(\"Correct!\");\n true\n }\n}\n"
9499
9499
  },
9500
9500
  {
9501
9501
  "id": "oracles__kth_bit_oracle__Verification.qs",
9502
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation KthBit_Oracle_Reference(x : Qubit[], k : Int) : Unit is Adj + Ctl {\n Z(x[k]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 1 .. 3 {\n for k in 0 .. N - 1 {\n let sol = Kata.KthBit_Oracle(_, k);\n let ref = KthBit_Oracle_Reference(_, k);\n let isCorrect = CheckOperationsAreEqualStrict(N, sol, ref);\n\n if not isCorrect {\n Message(\"Incorrect.\");\n Message(\"Hint: examine how your solution transforms the given state and compare it with the expected \" +\n $\"transformation for the {N}-bit oracle for k = {k}\");\n ShowQuantumStateComparison(N, PrepDemoState, sol, ref);\n return false;\n }\n }\n }\n Message(\"Correct!\");\n true\n }\n}\n"
9502
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation KthBit_Oracle_Reference(x : Qubit[], k : Int) : Unit is Adj + Ctl {\n Z(x[k]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 1..3 {\n for k in 0..N - 1 {\n let sol = Kata.KthBit_Oracle(_, k);\n let ref = KthBit_Oracle_Reference(_, k);\n let isCorrect = CheckOperationsAreEqualStrict(N, sol, ref);\n\n if not isCorrect {\n Message(\"Incorrect.\");\n Message(\"Hint: examine how your solution transforms the given state and compare it with the expected \" +\n $\"transformation for the {N}-bit oracle for k = {k}\");\n ShowQuantumStateComparison(N, PrepDemoState, sol, ref);\n return false;\n }\n }\n }\n Message(\"Correct!\");\n true\n }\n}\n"
9503
9503
  },
9504
9504
  {
9505
9505
  "id": "oracles__or_but_kth_oracle__Verification.qs",
9506
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation Or_Oracle_Reference(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n X(y);\n ApplyControlledOnInt(0, X, x, y);\n }\n\n operation OrOfBitsExceptKth_Oracle_Reference(x : Qubit[], k : Int) : Unit is Adj + Ctl {\n use minus = Qubit();\n within {\n X(minus);\n H(minus);\n } apply {\n Or_Oracle_Reference(x[...k-1] + x[k+1...], minus);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 2 .. 4 {\n for k in 0 .. N - 1 {\n let sol = Kata.OrOfBitsExceptKth_Oracle(_, k);\n let ref = OrOfBitsExceptKth_Oracle_Reference(_, k);\n let isCorrect = CheckOperationsAreEqualStrict(N, sol, ref);\n\n if not isCorrect {\n Message(\"Incorrect.\");\n Message(\"Hint: examine how your solution transforms the given state and compare it with the expected \" +\n $\"transformation for the {N}-bit oracle for k = {k}\");\n ShowQuantumStateComparison(N, PrepDemoState, sol, ref);\n return false;\n }\n }\n }\n Message(\"Correct!\");\n true\n }\n}\n"
9506
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation Or_Oracle_Reference(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n X(y);\n ApplyControlledOnInt(0, X, x, y);\n }\n\n operation OrOfBitsExceptKth_Oracle_Reference(x : Qubit[], k : Int) : Unit is Adj + Ctl {\n use minus = Qubit();\n within {\n X(minus);\n H(minus);\n } apply {\n Or_Oracle_Reference(x[...k-1] + x[k + 1...], minus);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 2..4 {\n for k in 0..N - 1 {\n let sol = Kata.OrOfBitsExceptKth_Oracle(_, k);\n let ref = OrOfBitsExceptKth_Oracle_Reference(_, k);\n let isCorrect = CheckOperationsAreEqualStrict(N, sol, ref);\n\n if not isCorrect {\n Message(\"Incorrect.\");\n Message(\"Hint: examine how your solution transforms the given state and compare it with the expected \" +\n $\"transformation for the {N}-bit oracle for k = {k}\");\n ShowQuantumStateComparison(N, PrepDemoState, sol, ref);\n return false;\n }\n }\n }\n Message(\"Correct!\");\n true\n }\n}\n"
9507
9507
  },
9508
9508
  {
9509
9509
  "id": "oracles__bit_pattern_oracle__Verification.qs",
9510
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Katas;\n\n operation ArbitraryBitPattern_Oracle_Reference(x : Qubit[], y : Qubit, pattern : Bool[]) : Unit is Adj + Ctl {\n ApplyControlledOnBitString(pattern, X, x, y);\n }\n\n // ------------------------------------------------------\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 1 .. 3 {\n for k in 0 .. 2^N - 1 {\n let pattern = IntAsBoolArray(k, N);\n\n let sol = ApplyOracle(_, Kata.ArbitraryBitPattern_Oracle(_, _, pattern));\n let ref = ApplyOracle(_, ArbitraryBitPattern_Oracle_Reference(_, _, pattern));\n let isCorrect = CheckOperationsAreEqualStrict(N + 1, sol, ref);\n\n if not isCorrect {\n Message(\"Incorrect.\");\n Message(\"Hint: examine how your solution transforms the given state and compare it with the expected \" +\n $\"transformation for the {N}-bit oracle for the pattern {pattern}\");\n ShowQuantumStateComparison(N + 1, qs => PrepDemoState(qs[...N - 1]), sol, ref);\n return false;\n }\n }\n }\n Message(\"Correct!\");\n true\n }\n\n}\n"
9510
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import KatasUtils.*;\n\n operation ArbitraryBitPattern_Oracle_Reference(x : Qubit[], y : Qubit, pattern : Bool[]) : Unit is Adj + Ctl {\n ApplyControlledOnBitString(pattern, X, x, y);\n }\n\n // ------------------------------------------------------\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 1..3 {\n for k in 0..2^N - 1 {\n let pattern = IntAsBoolArray(k, N);\n\n let sol = ApplyOracle(_, Kata.ArbitraryBitPattern_Oracle(_, _, pattern));\n let ref = ApplyOracle(_, ArbitraryBitPattern_Oracle_Reference(_, _, pattern));\n let isCorrect = CheckOperationsAreEqualStrict(N + 1, sol, ref);\n\n if not isCorrect {\n Message(\"Incorrect.\");\n Message(\"Hint: examine how your solution transforms the given state and compare it with the expected \" +\n $\"transformation for the {N}-bit oracle for the pattern {pattern}\");\n ShowQuantumStateComparison(N + 1, qs => PrepDemoState(qs[...N - 1]), sol, ref);\n return false;\n }\n }\n }\n Message(\"Correct!\");\n true\n }\n\n}\n"
9511
9511
  },
9512
9512
  {
9513
9513
  "id": "oracles__bit_pattern_challenge__Verification.qs",
9514
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Katas;\n\n operation ArbitraryBitPattern_Oracle_Challenge_Reference(x : Qubit[], pattern : Bool[]) : Unit is Adj + Ctl {\n within {\n for i in IndexRange(x) {\n if not pattern[i] {\n X(x[i]);\n }\n }\n } apply {\n Controlled Z(Most(x), Tail(x));\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 1 .. 3 {\n for k in 0 .. 2^N -1 {\n let pattern = IntAsBoolArray(k, N);\n\n let sol = Kata.ArbitraryBitPattern_Oracle_Challenge(_, pattern);\n let ref = ArbitraryBitPattern_Oracle_Challenge_Reference(_, pattern);\n let isCorrect = CheckOperationsAreEqualStrict(N, sol, ref);\n\n if not isCorrect {\n Message(\"Incorrect.\");\n Message(\"Hint: examine how your solution transforms the given state and compare it with the expected \" +\n $\"transformation for the {N}-bit oracle for pattern = {pattern}\");\n ShowQuantumStateComparison(N, PrepDemoState, sol, ref);\n return false;\n }\n }\n }\n Message(\"All tests passed.\");\n true\n }\n}\n"
9514
+ "code": "namespace Kata.Verification {\n import Std.Arrays.*;\n import Std.Convert.*;\n import KatasUtils.*;\n\n operation ArbitraryBitPattern_Oracle_Challenge_Reference(x : Qubit[], pattern : Bool[]) : Unit is Adj + Ctl {\n within {\n for i in IndexRange(x) {\n if not pattern[i] {\n X(x[i]);\n }\n }\n } apply {\n Controlled Z(Most(x), Tail(x));\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 1..3 {\n for k in 0..2^N -1 {\n let pattern = IntAsBoolArray(k, N);\n\n let sol = Kata.ArbitraryBitPattern_Oracle_Challenge(_, pattern);\n let ref = ArbitraryBitPattern_Oracle_Challenge_Reference(_, pattern);\n let isCorrect = CheckOperationsAreEqualStrict(N, sol, ref);\n\n if not isCorrect {\n Message(\"Incorrect.\");\n Message(\"Hint: examine how your solution transforms the given state and compare it with the expected \" +\n $\"transformation for the {N}-bit oracle for pattern = {pattern}\");\n ShowQuantumStateComparison(N, PrepDemoState, sol, ref);\n return false;\n }\n }\n }\n Message(\"All tests passed.\");\n true\n }\n}\n"
9515
9515
  },
9516
9516
  {
9517
9517
  "id": "oracles__meeting_oracle__Verification.qs",
9518
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Katas;\n\n operation Or_Oracle_Reference(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n X(y);\n ApplyControlledOnInt(0, X, x, y);\n }\n\n operation Meeting_Oracle_Reference(x : Qubit[], jasmine : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n use qs = Qubit[Length(x)];\n within {\n for i in IndexRange(qs) {\n // flip q[i] if both x and jasmine are free on the given day\n X(x[i]);\n X(jasmine[i]);\n CCNOT(x[i], jasmine[i], qs[i]);\n }\n } apply {\n Or_Oracle_Reference(qs, y);\n }\n }\n\n operation ApplyMeetingOracle(qs : Qubit[], oracle : (Qubit[], Qubit[], Qubit) => Unit is Adj + Ctl) : Unit is Adj + Ctl {\n let x = qs[0 .. 4];\n let jasmine = qs[5 .. 9];\n let target = qs[10];\n oracle(x, jasmine, target);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let N = 5;\n let sol = ApplyMeetingOracle(_, Kata.Meeting_Oracle);\n let ref = ApplyMeetingOracle(_, Meeting_Oracle_Reference);\n let isCorrect = CheckOperationsAreEqualStrict(2 * N + 1, sol, ref);\n\n if not isCorrect {\n Message(\"Incorrect.\");\n Message(\"Hint: check that you're flipping the state of the target qubit for the correct inputs, \" +\n \"that you're uncomputing any changes you did to the input qubits correctly, \" +\n \"and that you're returning any temporarily allocated qubits to the zero state.\");\n return false;\n }\n Message(\"Correct!\");\n true\n }\n}\n"
9518
+ "code": "namespace Kata.Verification {\n import Std.Arrays.*;\n import Std.Convert.*;\n import KatasUtils.*;\n\n operation Or_Oracle_Reference(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n X(y);\n ApplyControlledOnInt(0, X, x, y);\n }\n\n operation Meeting_Oracle_Reference(x : Qubit[], jasmine : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n use qs = Qubit[Length(x)];\n within {\n for i in IndexRange(qs) {\n // flip q[i] if both x and jasmine are free on the given day\n X(x[i]);\n X(jasmine[i]);\n CCNOT(x[i], jasmine[i], qs[i]);\n }\n } apply {\n Or_Oracle_Reference(qs, y);\n }\n }\n\n operation ApplyMeetingOracle(qs : Qubit[], oracle : (Qubit[], Qubit[], Qubit) => Unit is Adj + Ctl) : Unit is Adj + Ctl {\n let x = qs[0..4];\n let jasmine = qs[5..9];\n let target = qs[10];\n oracle(x, jasmine, target);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let N = 5;\n let sol = ApplyMeetingOracle(_, Kata.Meeting_Oracle);\n let ref = ApplyMeetingOracle(_, Meeting_Oracle_Reference);\n let isCorrect = CheckOperationsAreEqualStrict(2 * N + 1, sol, ref);\n\n if not isCorrect {\n Message(\"Incorrect.\");\n Message(\"Hint: check that you're flipping the state of the target qubit for the correct inputs, \" +\n \"that you're uncomputing any changes you did to the input qubits correctly, \" +\n \"and that you're returning any temporarily allocated qubits to the zero state.\");\n return false;\n }\n Message(\"Correct!\");\n true\n }\n}\n"
9519
9519
  },
9520
9520
  {
9521
9521
  "id": "marking_oracles__kth_bit__Verification.qs",
9522
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n function F_Kth_Bit(x : Bool[], k : Int) : Bool {\n x[k]\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 3 .. 5 {\n for k in 0 .. n - 1 {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_Kth_Bit(_, _, k), F_Kth_Bit(_, k)) {\n Message($\"Test failed for n = {n}, k = {k}\");\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9522
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n function F_Kth_Bit(x : Bool[], k : Int) : Bool {\n x[k]\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 3..5 {\n for k in 0..n - 1 {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_Kth_Bit(_, _, k), F_Kth_Bit(_, k)) {\n Message($\"Test failed for n = {n}, k = {k}\");\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9523
9523
  },
9524
9524
  {
9525
9525
  "id": "marking_oracles__Common.qs",
@@ -9527,283 +9527,283 @@ export default {
9527
9527
  },
9528
9528
  {
9529
9529
  "id": "marking_oracles__parity__Verification.qs",
9530
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n function F_Parity(x : Bool[]) : Bool {\n mutable parity = false;\n for xi in x {\n if xi {\n set parity = not parity;\n }\n }\n parity\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 1 .. 5 {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_Parity, F_Parity) {\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9530
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n function F_Parity(x : Bool[]) : Bool {\n mutable parity = false;\n for xi in x {\n if xi {\n set parity = not parity;\n }\n }\n parity\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 1..5 {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_Parity, F_Parity) {\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9531
9531
  },
9532
9532
  {
9533
9533
  "id": "marking_oracles__product__Verification.qs",
9534
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Katas;\n\n function F_Product(x : Bool[], r : Bool[]) : Bool {\n mutable product = false;\n for i in 0 .. Length(x) - 1 {\n if r[i] and x[i] {\n set product = not product;\n }\n }\n product\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let n = 3;\n for mask in 0 .. 2 ^ n - 1 {\n let r = IntAsBoolArray(mask, 3);\n if not CheckOracleImplementsFunction(n, Kata.Oracle_Product(_, _, r), F_Product(_, r)) {\n Message($\"Test failed for n = {n}, r = {r}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9534
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import KatasUtils.*;\n\n function F_Product(x : Bool[], r : Bool[]) : Bool {\n mutable product = false;\n for i in 0..Length(x) - 1 {\n if r[i] and x[i] {\n set product = not product;\n }\n }\n product\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let n = 3;\n for mask in 0..2^n - 1 {\n let r = IntAsBoolArray(mask, 3);\n if not CheckOracleImplementsFunction(n, Kata.Oracle_Product(_, _, r), F_Product(_, r)) {\n Message($\"Test failed for n = {n}, r = {r}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9535
9535
  },
9536
9536
  {
9537
9537
  "id": "marking_oracles__product_negation__Verification.qs",
9538
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Katas;\n\n function F_ProductWithNegation(x : Bool[], r : Bool[]) : Bool {\n mutable product = false;\n for i in 0 .. Length(x) - 1 {\n if r[i] and x[i] or (not r[i] and not x[i]) {\n set product = not product;\n }\n }\n product\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let n = 3;\n for mask in 0 .. 2 ^ n - 1 {\n let r = IntAsBoolArray(mask, 3);\n if not CheckOracleImplementsFunction(n, Kata.Oracle_ProductWithNegation(_, _, r), F_ProductWithNegation(_, r)) {\n Message($\"Test failed for n = {n}, r = {r}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9538
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import KatasUtils.*;\n\n function F_ProductWithNegation(x : Bool[], r : Bool[]) : Bool {\n mutable product = false;\n for i in 0..Length(x) - 1 {\n if r[i] and x[i] or (not r[i] and not x[i]) {\n set product = not product;\n }\n }\n product\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let n = 3;\n for mask in 0..2^n - 1 {\n let r = IntAsBoolArray(mask, 3);\n if not CheckOracleImplementsFunction(n, Kata.Oracle_ProductWithNegation(_, _, r), F_ProductWithNegation(_, r)) {\n Message($\"Test failed for n = {n}, r = {r}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9539
9539
  },
9540
9540
  {
9541
9541
  "id": "marking_oracles__palindrome__Verification.qs",
9542
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n function F_Palindrome(args : Bool[]) : Bool {\n let N = Length(args);\n for i in 0 .. N / 2 - 1 {\n if args[i] != args[N - i - 1] {\n return false;\n }\n }\n return true;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 2 .. 6 {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_Palindrome, F_Palindrome) {\n Message($\"Test failed for n = {n}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n } \n}\n"
9542
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n function F_Palindrome(args : Bool[]) : Bool {\n let N = Length(args);\n for i in 0..N / 2 - 1 {\n if args[i] != args[N - i - 1] {\n return false;\n }\n }\n return true;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 2..6 {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_Palindrome, F_Palindrome) {\n Message($\"Test failed for n = {n}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9543
9543
  },
9544
9544
  {
9545
9545
  "id": "marking_oracles__periodic_p__Verification.qs",
9546
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 2 .. 6 {\n for p in 2 .. n - 1 {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_PeriodicGivenPeriod(_, _, p), F_PeriodicGivenPeriod(_, p)) {\n Message($\"Test failed for n = {n}, p = {p}\");\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n true\n } \n}\n"
9546
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 2..6 {\n for p in 2..n - 1 {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_PeriodicGivenPeriod(_, _, p), F_PeriodicGivenPeriod(_, p)) {\n Message($\"Test failed for n = {n}, p = {p}\");\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9547
9547
  },
9548
9548
  {
9549
9549
  "id": "marking_oracles__periodic__Verification.qs",
9550
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n function F_Periodic(args : Bool[]) : Bool {\n let N = Length(args);\n for P in 1 .. N - 1 {\n if F_PeriodicGivenPeriod(args, P) {\n return true;\n }\n }\n return false;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 2 .. 6 {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_Periodic, F_Periodic) {\n Message($\"Test failed for n = {n}\");\n return false; \n }\n }\n\n Message(\"Correct!\");\n true\n } \n}\n"
9550
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n function F_Periodic(args : Bool[]) : Bool {\n let N = Length(args);\n for P in 1..N - 1 {\n if F_PeriodicGivenPeriod(args, P) {\n return true;\n }\n }\n return false;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 2..6 {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_Periodic, F_Periodic) {\n Message($\"Test failed for n = {n}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9551
9551
  },
9552
9552
  {
9553
9553
  "id": "marking_oracles__contains_substring_p__Verification.qs",
9554
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for (n, p, r) in [\n (2, 1, [true]),\n (3, 0, [false, true]),\n (4, 1, [true, true, false]),\n (5, 3, [false])\n ] {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_ContainsSubstringAtPosition(_, _, r, p), F_ContainsSubstringAtPosition(_, r, p)) {\n Message($\"Test failed for n = {n}, p = {p}, r = {r}\");\n return false; \n }\n }\n\n Message(\"Correct!\");\n true \n } \n}\n"
9554
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for (n, p, r) in [\n (2, 1, [true]),\n (3, 0, [false, true]),\n (4, 1, [true, true, false]),\n (5, 3, [false])\n ] {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_ContainsSubstringAtPosition(_, _, r, p), F_ContainsSubstringAtPosition(_, r, p)) {\n Message($\"Test failed for n = {n}, p = {p}, r = {r}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9555
9555
  },
9556
9556
  {
9557
9557
  "id": "marking_oracles__pattern_match__Verification.qs",
9558
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n function F_PatternMatching(args : Bool[], a : Int[], r : Bool[]) : Bool {\n for i in 0 .. Length(a) - 1 {\n if args[a[i]] != r[i] {\n return false;\n }\n }\n return true;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for (n, a, r) in [\n (2, [], []),\n (2, [1], [true]),\n (3, [0, 2], [false, true]),\n (4, [1, 3], [true, false]),\n (5, [0, 1, 4], [true, true, false])\n ] {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_PatternMatching(_, _, a, r), F_PatternMatching(_, a, r)) {\n Message($\"Test failed for n = {n}, a = {a}, r = {r}\");\n return false; \n }\n }\n\n Message(\"Correct!\");\n true \n } \n}\n"
9558
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n function F_PatternMatching(args : Bool[], a : Int[], r : Bool[]) : Bool {\n for i in 0..Length(a) - 1 {\n if args[a[i]] != r[i] {\n return false;\n }\n }\n return true;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for (n, a, r) in [\n (2, [], []),\n (2, [1], [true]),\n (3, [0, 2], [false, true]),\n (4, [1, 3], [true, false]),\n (5, [0, 1, 4], [true, true, false])\n ] {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_PatternMatching(_, _, a, r), F_PatternMatching(_, a, r)) {\n Message($\"Test failed for n = {n}, a = {a}, r = {r}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9559
9559
  },
9560
9560
  {
9561
9561
  "id": "marking_oracles__contains_substring__Verification.qs",
9562
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n function F_ContainsSubstring(args : Bool[], r : Bool[]) : Bool {\n let N = Length(args);\n let K = Length(r);\n for P in 0 .. N - K {\n if F_ContainsSubstringAtPosition(args, r, P) {\n return true;\n }\n }\n return false;\n } \n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for (n, r) in [\n (2, [true]),\n (3, [false, true]),\n (4, [true, true, false]),\n (5, [false, false])\n ] {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_ContainsSubstring(_, _, r), F_ContainsSubstring(_, r)) {\n Message($\"Test failed for n = {n}, r = {r}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true \n } \n}\n"
9562
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n function F_ContainsSubstring(args : Bool[], r : Bool[]) : Bool {\n let N = Length(args);\n let K = Length(r);\n for P in 0..N - K {\n if F_ContainsSubstringAtPosition(args, r, P) {\n return true;\n }\n }\n return false;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for (n, r) in [\n (2, [true]),\n (3, [false, true]),\n (4, [true, true, false]),\n (5, [false, false])\n ] {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_ContainsSubstring(_, _, r), F_ContainsSubstring(_, r)) {\n Message($\"Test failed for n = {n}, r = {r}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9563
9563
  },
9564
9564
  {
9565
9565
  "id": "marking_oracles__balanced__Verification.qs",
9566
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Arrays;\n\n function F_Balanced(args : Bool[]) : Bool {\n return Count(x -> x, args) == Length(args) / 2;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 2 .. 2 .. 6 {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_Balanced, F_Balanced) {\n Message($\"Test failed for n = {n}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9566
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Arrays.*;\n\n function F_Balanced(args : Bool[]) : Bool {\n return Count(x -> x, args) == Length(args) / 2;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 2..2..6 {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_Balanced, F_Balanced) {\n Message($\"Test failed for n = {n}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9567
9567
  },
9568
9568
  {
9569
9569
  "id": "marking_oracles__majority__Verification.qs",
9570
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Arrays;\n\n function F_Majority(args : Bool[]) : Bool {\n let N = Length(args);\n return Count(x -> x, args) > (N - 1) / 2;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in [3, 5, 7] {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_Majority, F_Majority) {\n Message($\"Test failed for n = {n}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9570
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Arrays.*;\n\n function F_Majority(args : Bool[]) : Bool {\n let N = Length(args);\n return Count(x -> x, args) > (N - 1) / 2;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in [3, 5, 7] {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_Majority, F_Majority) {\n Message($\"Test failed for n = {n}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9571
9571
  },
9572
9572
  {
9573
9573
  "id": "marking_oracles__bit_sum_div_3__Verification.qs",
9574
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Arrays;\n\n function F_BitSumDivisibleBy3(args : Bool[]) : Bool {\n return Count(x -> x, args) % 3 == 0;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 3 .. 6 {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_BitSumDivisibleBy3, F_BitSumDivisibleBy3) {\n Message($\"Test failed for n = {n}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9574
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Arrays.*;\n\n function F_BitSumDivisibleBy3(args : Bool[]) : Bool {\n return Count(x -> x, args) % 3 == 0;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 3..6 {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_BitSumDivisibleBy3, F_BitSumDivisibleBy3) {\n Message($\"Test failed for n = {n}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9575
9575
  },
9576
9576
  {
9577
9577
  "id": "marking_oracles__num_div_3__Verification.qs",
9578
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Convert;\n\n function F_DivisibleBy3(args : Bool[]) : Bool {\n return BoolArrayAsInt(args) % 3 == 0;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 2 .. 7 {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_DivisibleBy3, F_DivisibleBy3) {\n Message($\"Test failed for n = {n}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9578
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Convert.*;\n\n function F_DivisibleBy3(args : Bool[]) : Bool {\n return BoolArrayAsInt(args) % 3 == 0;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 2..7 {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_DivisibleBy3, F_DivisibleBy3) {\n Message($\"Test failed for n = {n}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9579
9579
  },
9580
9580
  {
9581
9581
  "id": "deutsch_algo__one_minus_x_oracle__Verification.qs",
9582
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation PhaseOracle_OneMinusX_Reference(x : Qubit) : Unit is Adj + Ctl {\n Z(x);\n R(PauliI, 2.0 * PI(), x);\n }\n\n operation CheckSolution() : Bool {\n let solution = register => Kata.PhaseOracle_OneMinusX(register[0]);\n let reference = register => PhaseOracle_OneMinusX_Reference(register[0]);\n let isCorrect = CheckOperationsAreEqualStrict(1, solution, reference);\n\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\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. Note that the simulator might drop the global phase -1, so if you're getting \" +\n \"verdict \\\"Incorrect\\\" but the actual state matches the expected one, check that you're handling the global phase correctly.\");\n ShowQuantumStateComparison(1, (qs => Ry(ArcTan2(0.8, 0.6) * 2.0, qs[0])), solution, reference);\n }\n isCorrect\n }\n}\n"
9582
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Math.*;\n\n operation PhaseOracle_OneMinusX_Reference(x : Qubit) : Unit is Adj + Ctl {\n Z(x);\n R(PauliI, 2.0 * PI(), x);\n }\n\n operation CheckSolution() : Bool {\n let solution = register => Kata.PhaseOracle_OneMinusX(register[0]);\n let reference = register => PhaseOracle_OneMinusX_Reference(register[0]);\n let isCorrect = CheckOperationsAreEqualStrict(1, solution, reference);\n\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\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. Note that the simulator might drop the global phase -1, so if you're getting \" +\n \"verdict \\\"Incorrect\\\" but the actual state matches the expected one, check that you're handling the global phase correctly.\");\n ShowQuantumStateComparison(1, (qs => Ry(ArcTan2(0.8, 0.6) * 2.0, qs[0])), solution, reference);\n }\n isCorrect\n }\n}\n"
9583
9583
  },
9584
9584
  {
9585
9585
  "id": "deutsch_algo__implement_algo__Verification.qs",
9586
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation CheckSolution() : Bool {\n for (oracle, expected, name) in [(I, true, \"f(x) = 0\"), \n (R(PauliI, 2.0 * PI(), _), true, \"f(x) = 1\"), \n (Z, false, \"f(x) = x\"), \n (PhaseOracle_OneMinusX, false, \"f(x) = 1 - x\")] {\n\n let actual = Kata.DeutschAlgorithm(oracle);\n if actual != expected {\n Message(\"Incorrect.\");\n let actualStr = ConstantOrVariable(actual);\n let expectedStr = ConstantOrVariable(expected);\n Message($\"{name} identified as {actualStr} but it is {expectedStr}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n\n function ConstantOrVariable (value : Bool) : String {\n return value ? \"constant\" | \"variable\";\n }\n\n operation PhaseOracle_OneMinusX(x : Qubit) : Unit is Adj + Ctl {\n Z(x);\n R(PauliI, 2.0 * PI(), x);\n }\n}\n"
9586
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Math.*;\n\n operation CheckSolution() : Bool {\n for (oracle, expected, name) in [\n (I, true, \"f(x) = 0\"),\n (R(PauliI, 2.0 * PI(), _), true, \"f(x) = 1\"),\n (Z, false, \"f(x) = x\"),\n (PhaseOracle_OneMinusX, false, \"f(x) = 1 - x\")\n ] {\n\n let actual = Kata.DeutschAlgorithm(oracle);\n if actual != expected {\n Message(\"Incorrect.\");\n let actualStr = ConstantOrVariable(actual);\n let expectedStr = ConstantOrVariable(expected);\n Message($\"{name} identified as {actualStr} but it is {expectedStr}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n\n function ConstantOrVariable(value : Bool) : String {\n return value ? \"constant\" | \"variable\";\n }\n\n operation PhaseOracle_OneMinusX(x : Qubit) : Unit is Adj + Ctl {\n Z(x);\n R(PauliI, 2.0 * PI(), x);\n }\n}\n"
9587
9587
  },
9588
9588
  {
9589
9589
  "id": "deutsch_jozsa__msb_oracle__Verification.qs",
9590
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation PhaseOracle_MostSignificantBit_Reference(x : Qubit[]) : Unit is Adj + Ctl {\n Z(x[0]);\n }\n\n operation CheckSolution() : Bool {\n let solution = Kata.PhaseOracle_MostSignificantBit;\n let reference = PhaseOracle_MostSignificantBit_Reference;\n for N in 1 .. 4 {\n if not CheckOperationsAreEqualStrict(N, solution, reference) {\n Message(\"Incorrect.\");\n Message($\"Hint: examine the effect your solution has on the {N}-qubit and compare it with the effect it \" +\n \"is expected to have. Note that the simulator might drop the global phase -1, so if you're getting \" +\n \"verdict \\\"Incorrect\\\" but the actual state matches the expected one, check that you're handling the global phase correctly.\");\n ShowQuantumStateComparison(N, PrepDemoState, solution, reference);\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9590
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Math.*;\n\n operation PhaseOracle_MostSignificantBit_Reference(x : Qubit[]) : Unit is Adj + Ctl {\n Z(x[0]);\n }\n\n operation CheckSolution() : Bool {\n let solution = Kata.PhaseOracle_MostSignificantBit;\n let reference = PhaseOracle_MostSignificantBit_Reference;\n for N in 1..4 {\n if not CheckOperationsAreEqualStrict(N, solution, reference) {\n Message(\"Incorrect.\");\n Message($\"Hint: examine the effect your solution has on the {N}-qubit and compare it with the effect it \" +\n \"is expected to have. Note that the simulator might drop the global phase -1, so if you're getting \" +\n \"verdict \\\"Incorrect\\\" but the actual state matches the expected one, check that you're handling the global phase correctly.\");\n ShowQuantumStateComparison(N, PrepDemoState, solution, reference);\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9591
9591
  },
9592
9592
  {
9593
9593
  "id": "deutsch_jozsa__parity_oracle__Verification.qs",
9594
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation PhaseOracle_Parity_Reference(x : Qubit[]) : Unit is Adj + Ctl {\n for xi in x {\n Z(xi);\n }\n }\n\n operation CheckSolution() : Bool {\n let solution = Kata.PhaseOracle_Parity;\n let reference = PhaseOracle_Parity_Reference;\n for N in 1 .. 4 {\n if not CheckOperationsAreEqualStrict(N, solution, reference) {\n Message(\"Incorrect.\");\n Message($\"Hint: examine the effect your solution has on the {N}-qubit state and compare it with the effect it \" +\n \"is expected to have. Note that the simulator might drop the global phase -1, so if you're getting \" +\n \"verdict \\\"Incorrect\\\" but the actual state matches the expected one, check that you're handling the global phase correctly.\");\n ShowQuantumStateComparison(N, PrepDemoState, solution, reference);\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9594
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Math.*;\n\n operation PhaseOracle_Parity_Reference(x : Qubit[]) : Unit is Adj + Ctl {\n for xi in x {\n Z(xi);\n }\n }\n\n operation CheckSolution() : Bool {\n let solution = Kata.PhaseOracle_Parity;\n let reference = PhaseOracle_Parity_Reference;\n for N in 1..4 {\n if not CheckOperationsAreEqualStrict(N, solution, reference) {\n Message(\"Incorrect.\");\n Message($\"Hint: examine the effect your solution has on the {N}-qubit state and compare it with the effect it \" +\n \"is expected to have. Note that the simulator might drop the global phase -1, so if you're getting \" +\n \"verdict \\\"Incorrect\\\" but the actual state matches the expected one, check that you're handling the global phase correctly.\");\n ShowQuantumStateComparison(N, PrepDemoState, solution, reference);\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9595
9595
  },
9596
9596
  {
9597
9597
  "id": "deutsch_jozsa__implement_dj__Verification.qs",
9598
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation CheckSolution() : Bool {\n for (n, oracle, expected, name) in [(2, qs => (), true, \"f(x) = 0\"), \n (3, qs => R(PauliI, 2.0 * PI(), qs[0]), true, \"f(x) = 1\"), \n (3, ApplyToEach(Z, _), false, \"f(x) = parity of x\"), \n (3, qs => Z(qs[0]), false, \"f(x) = most significant bit of x\"), \n (3, qs => Z(qs[Length(qs) - 1]), false, \"f(x) = x mod 2\"), \n ] {\n let actual = Kata.DeutschJozsaAlgorithm(n, oracle);\n if actual != expected {\n Message(\"Incorrect.\");\n let actualStr = ConstantOrBalanced(actual);\n let expectedStr = ConstantOrBalanced(expected);\n Message($\"{name} for {n} bits identified as {actualStr} but it is {expectedStr}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n\n function ConstantOrBalanced (value : Bool) : String {\n return value ? \"constant\" | \"balanced\";\n }\n}\n"
9598
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Math.*;\n\n operation CheckSolution() : Bool {\n for (n, oracle, expected, name) in [\n (2, qs => (), true, \"f(x) = 0\"),\n (3, qs => R(PauliI, 2.0 * PI(), qs[0]), true, \"f(x) = 1\"),\n (3, ApplyToEach(Z, _), false, \"f(x) = parity of x\"),\n (3, qs => Z(qs[0]), false, \"f(x) = most significant bit of x\"),\n (3, qs => Z(qs[Length(qs) - 1]), false, \"f(x) = x mod 2\"),\n ] {\n let actual = Kata.DeutschJozsaAlgorithm(n, oracle);\n if actual != expected {\n Message(\"Incorrect.\");\n let actualStr = ConstantOrBalanced(actual);\n let expectedStr = ConstantOrBalanced(expected);\n Message($\"{name} for {n} bits identified as {actualStr} but it is {expectedStr}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n\n function ConstantOrBalanced(value : Bool) : String {\n return value ? \"constant\" | \"balanced\";\n }\n}\n"
9599
9599
  },
9600
9600
  {
9601
9601
  "id": "deutsch_jozsa__implement_bv__Verification.qs",
9602
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation CheckSolution() : Bool {\n for (n, oracle, expected, name) in [(2, qs => (), [0, 0], \"f(x) = 0\"), \n (3, qs => (), [0, 0, 0], \"f(x) = 0\"), \n (2, ApplyToEach(Z, _), [1, 1], \"f(x) = parity of x\"), \n (3, ApplyToEach(Z, _), [1, 1, 1], \"f(x) = parity of x\"), \n (2, qs => Z(qs[0]), [1, 0], \"f(x) = most significant bit of x\"), \n (3, qs => Z(qs[2]), [0, 0, 1], \"f(x) = least significant bit of x\"), \n (3, qs => Z(qs[1]), [0, 1, 0], \"f(x) = middle bit of x\")\n ] {\n let actual = Kata.BernsteinVaziraniAlgorithm(n, oracle);\n if actual != expected {\n Message(\"Incorrect.\");\n Message($\"The bit string for {name} for {n} bits identified as {actual} but it is {expected}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9602
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Math.*;\n\n operation CheckSolution() : Bool {\n for (n, oracle, expected, name) in [\n (2, qs => (), [0, 0], \"f(x) = 0\"),\n (3, qs => (), [0, 0, 0], \"f(x) = 0\"),\n (2, ApplyToEach(Z, _), [1, 1], \"f(x) = parity of x\"),\n (3, ApplyToEach(Z, _), [1, 1, 1], \"f(x) = parity of x\"),\n (2, qs => Z(qs[0]), [1, 0], \"f(x) = most significant bit of x\"),\n (3, qs => Z(qs[2]), [0, 0, 1], \"f(x) = least significant bit of x\"),\n (3, qs => Z(qs[1]), [0, 1, 0], \"f(x) = middle bit of x\")\n ] {\n let actual = Kata.BernsteinVaziraniAlgorithm(n, oracle);\n if actual != expected {\n Message(\"Incorrect.\");\n Message($\"The bit string for {name} for {n} bits identified as {actual} but it is {expected}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9603
9603
  },
9604
9604
  {
9605
9605
  "id": "grovers_search__prefix_oracle__Verification.qs",
9606
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n function F_StartsWith(args : Bool[], p : Bool[]) : Bool {\n for i in 0 .. Length(p) - 1 {\n if p[i] != args[i] {\n return false;\n }\n }\n return true;\n } \n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for (n, p) in [\n (2, []),\n (2, [true]),\n (2, [true, false]),\n (3, [false, true]),\n (4, [true, true, false]),\n (5, [false])\n ] {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_StartsWith(_, _, p), F_StartsWith(_, p)) {\n Message($\"Test failed for N = {n}, p = {p}\");\n return false; \n }\n }\n\n Message(\"Correct!\");\n true \n } \n}\n"
9606
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n function F_StartsWith(args : Bool[], p : Bool[]) : Bool {\n for i in 0..Length(p) - 1 {\n if p[i] != args[i] {\n return false;\n }\n }\n return true;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for (n, p) in [\n (2, []),\n (2, [true]),\n (2, [true, false]),\n (3, [false, true]),\n (4, [true, true, false]),\n (5, [false])\n ] {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_StartsWith(_, _, p), F_StartsWith(_, p)) {\n Message($\"Test failed for N = {n}, p = {p}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9607
9607
  },
9608
9608
  {
9609
9609
  "id": "grovers_search__phase_oracle__Verification.qs",
9610
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Random;\n\n operation ApplyMarkingOracleAsPhaseOracle_Reference(\n markingOracle : ((Qubit[], Qubit) => Unit is Adj + Ctl),\n qubits : Qubit[]) : Unit is Adj + Ctl {\n\n use minus = Qubit();\n within {\n X(minus);\n H(minus);\n } apply {\n markingOracle(qubits, minus);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 1 .. 3 {\n for k in 0 .. 2^N - 1 {\n let pattern = IntAsBoolArray(k, N);\n let marking = ApplyControlledOnBitString(pattern, X, _, _);\n let sol = Kata.ApplyMarkingOracleAsPhaseOracle(marking, _);\n let ref = ApplyMarkingOracleAsPhaseOracle_Reference(marking, _);\n\n let isCorrect = CheckOperationsAreEqualStrict(N, sol, ref);\n\n if not isCorrect {\n Message(\"Incorrect.\");\n Message(\"Hint: examine how your solution transforms the given state and compare it with the expected \" +\n $\"transformation for the {N}-bit oracle that marks the bit string {pattern}\");\n ShowQuantumStateComparison(N, PrepDemoState, sol, ref);\n return false;\n }\n }\n }\n Message(\"Correct!\");\n true\n }\n\n}\n"
9610
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import KatasUtils.*;\n import Std.Random.*;\n\n operation ApplyMarkingOracleAsPhaseOracle_Reference(\n markingOracle : ((Qubit[], Qubit) => Unit is Adj + Ctl),\n qubits : Qubit[]\n ) : Unit is Adj + Ctl {\n\n use minus = Qubit();\n within {\n X(minus);\n H(minus);\n } apply {\n markingOracle(qubits, minus);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 1..3 {\n for k in 0..2^N - 1 {\n let pattern = IntAsBoolArray(k, N);\n let marking = ApplyControlledOnBitString(pattern, X, _, _);\n let sol = Kata.ApplyMarkingOracleAsPhaseOracle(marking, _);\n let ref = ApplyMarkingOracleAsPhaseOracle_Reference(marking, _);\n\n let isCorrect = CheckOperationsAreEqualStrict(N, sol, ref);\n\n if not isCorrect {\n Message(\"Incorrect.\");\n Message(\"Hint: examine how your solution transforms the given state and compare it with the expected \" +\n $\"transformation for the {N}-bit oracle that marks the bit string {pattern}\");\n ShowQuantumStateComparison(N, PrepDemoState, sol, ref);\n return false;\n }\n }\n }\n Message(\"Correct!\");\n true\n }\n\n}\n"
9611
9611
  },
9612
9612
  {
9613
9613
  "id": "grovers_search__conditional_phase_flip__Verification.qs",
9614
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n operation ConditionalPhaseFlip(qs : Qubit[]) : Unit is Adj + Ctl {\n within {\n ApplyToEachA(X, qs);\n } apply {\n Controlled Z(qs[1 ...], qs[0]);\n }\n R(PauliI, 2.0 * PI(), qs[0]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 2 .. 4 {\n if not CheckOperationsAreEqualStrict(N, Kata.ConditionalPhaseFlip, ConditionalPhaseFlip) {\n Message(\"Incorrect.\");\n Message(\"Hint: examine how your solution transforms the given state and compare it with the expected \" +\n $\"transformation for the {N}-bit inputs\");\n ShowQuantumStateComparison(N, PrepDemoState, Kata.ConditionalPhaseFlip, ConditionalPhaseFlip);\n return false;\n }\n }\n Message(\"Correct!\");\n true\n }\n\n}\n"
9614
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Math.*;\n operation ConditionalPhaseFlip(qs : Qubit[]) : Unit is Adj + Ctl {\n within {\n ApplyToEachA(X, qs);\n } apply {\n Controlled Z(qs[1...], qs[0]);\n }\n R(PauliI, 2.0 * PI(), qs[0]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for N in 2..4 {\n if not CheckOperationsAreEqualStrict(N, Kata.ConditionalPhaseFlip, ConditionalPhaseFlip) {\n Message(\"Incorrect.\");\n Message(\"Hint: examine how your solution transforms the given state and compare it with the expected \" +\n $\"transformation for the {N}-bit inputs\");\n ShowQuantumStateComparison(N, PrepDemoState, Kata.ConditionalPhaseFlip, ConditionalPhaseFlip);\n return false;\n }\n }\n Message(\"Correct!\");\n true\n }\n\n}\n"
9615
9615
  },
9616
9616
  {
9617
9617
  "id": "grovers_search__reflection_about_state__Verification.qs",
9618
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Katas;\n\n operation ReflectionAboutState(\n qs : Qubit[],\n statePrep : Qubit[] => Unit is Adj + Ctl)\n : Unit is Adj + Ctl {\n within {\n Adjoint statePrep(qs);\n } apply {\n ConditionalPhaseFlip(qs);\n }\n }\n\n // You might find this helper operation from an earlier task useful.\n operation ConditionalPhaseFlip(qs : Qubit[]) : Unit is Adj + Ctl {\n within {\n ApplyToEachA(X, qs);\n } apply {\n Controlled Z(qs[1 ...], qs[0]);\n }\n R(PauliI, 2.0 * PI(), qs[0]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for (N, statePrep) in [(2, qs => I(qs[0])), (3, ApplyToEachCA(H, _)), (1, qs => Ry(0.5, qs[0]))] {\n let sol = Kata.ReflectionAboutState(_, statePrep);\n let ref = ReflectionAboutState(_, statePrep);\n if not CheckOperationsAreEqualStrict(N, sol, ref) {\n Message(\"Incorrect.\");\n return false;\n }\n }\n Message(\"Correct!\");\n true\n }\n}\n"
9618
+ "code": "namespace Kata.Verification {\n import Std.Math.*;\n import KatasUtils.*;\n\n operation ReflectionAboutState(\n qs : Qubit[],\n statePrep : Qubit[] => Unit is Adj + Ctl\n ) : Unit is Adj + Ctl {\n within {\n Adjoint statePrep(qs);\n } apply {\n ConditionalPhaseFlip(qs);\n }\n }\n\n // You might find this helper operation from an earlier task useful.\n operation ConditionalPhaseFlip(qs : Qubit[]) : Unit is Adj + Ctl {\n within {\n ApplyToEachA(X, qs);\n } apply {\n Controlled Z(qs[1...], qs[0]);\n }\n R(PauliI, 2.0 * PI(), qs[0]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for (N, statePrep) in [(2, qs => I(qs[0])), (3, ApplyToEachCA(H, _)), (1, qs => Ry(0.5, qs[0]))] {\n let sol = Kata.ReflectionAboutState(_, statePrep);\n let ref = ReflectionAboutState(_, statePrep);\n if not CheckOperationsAreEqualStrict(N, sol, ref) {\n Message(\"Incorrect.\");\n return false;\n }\n }\n Message(\"Correct!\");\n true\n }\n}\n"
9619
9619
  },
9620
9620
  {
9621
9621
  "id": "solving_sat__and__Verification.qs",
9622
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Arrays;\n\n function F_And(args : Bool[]) : Bool {\n return Count(x -> not x, args) == 0;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 1..5 {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_And, F_And) {\n Message($\"Test failed for n = {n}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9622
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Arrays.*;\n\n function F_And(args : Bool[]) : Bool {\n return Count(x -> not x, args) == 0;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 1..5 {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_And, F_And) {\n Message($\"Test failed for n = {n}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9623
9623
  },
9624
9624
  {
9625
9625
  "id": "solving_sat__Common.qs",
9626
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Random;\n\n // Helper functions to pretty-print SAT formulas\n function SATVariableAsString (var : (Int, Bool)) : String {\n let (index, positive) = var;\n return (positive ? \"\" | \"¬\") + $\"x{index}\";\n }\n\n function SATClauseAsString (clause : (Int, Bool)[]) : String {\n mutable ret = SATVariableAsString(clause[0]);\n for ind in 1 .. Length(clause) - 1 {\n set ret = ret + \" ∨ \" + SATVariableAsString(clause[ind]);\n }\n return ret;\n }\n\n function SATFormulaAsString (formula : (Int, Bool)[][]) : String {\n mutable ret = \"(\" + SATClauseAsString(formula[0]) + \")\";\n for ind in 1 .. Length(formula) - 1 {\n set ret = ret + \" ∧ (\" + SATClauseAsString(formula[ind]) + \")\";\n }\n return ret;\n }\n\n // Helper operations to generate random SAT formulas\n operation Generate_SAT_Clause (nVar : Int, nTerms : Int) : (Int, Bool)[] {\n // number of terms in clause is either given or (if nTerms <= 0) chosen randomly\n mutable nVarInClause = (nTerms > 0) ? nTerms | DrawRandomInt(1, 4);\n if nVarInClause > nVar {\n set nVarInClause = nVar;\n }\n \n mutable clause = [(0, false), size = nVarInClause];\n mutable usedVariables = [false, size = nVar];\n // Make sure variables in the clause are distinct\n for k in 0 .. nVarInClause - 1 {\n mutable nextInd = -1;\n repeat { \n set nextInd = DrawRandomInt(0, nVar - 1);\n } until (not usedVariables[nextInd])\n fixup {}\n set clause w/= k <- (nextInd, DrawRandomBool(0.5));\n set usedVariables w/= nextInd <- true;\n }\n return clause;\n }\n\n operation GenerateSATInstance (nVar : Int, nClause : Int, nTerms : Int) : (Int, Bool)[][] {\n mutable problem = [[(0, false), size = 0], size = nClause];\n\n for j in 0 .. nClause - 1 {\n set problem w/= j <- Generate_SAT_Clause(nVar, nTerms);\n }\n return problem;\n }\n}"
9626
+ "code": "namespace Kata.Verification {\n import Std.Random.*;\n\n // Helper functions to pretty-print SAT formulas\n function SATVariableAsString(var : (Int, Bool)) : String {\n let (index, positive) = var;\n return (positive ? \"\" | \"¬\") + $\"x{index}\";\n }\n\n function SATClauseAsString(clause : (Int, Bool)[]) : String {\n mutable ret = SATVariableAsString(clause[0]);\n for ind in 1..Length(clause) - 1 {\n set ret = ret + \" ∨ \" + SATVariableAsString(clause[ind]);\n }\n return ret;\n }\n\n function SATFormulaAsString(formula : (Int, Bool)[][]) : String {\n mutable ret = \"(\" + SATClauseAsString(formula[0]) + \")\";\n for ind in 1..Length(formula) - 1 {\n set ret = ret + \" ∧ (\" + SATClauseAsString(formula[ind]) + \")\";\n }\n return ret;\n }\n\n // Helper operations to generate random SAT formulas\n operation Generate_SAT_Clause(nVar : Int, nTerms : Int) : (Int, Bool)[] {\n // number of terms in clause is either given or (if nTerms <= 0) chosen randomly\n mutable nVarInClause = (nTerms > 0) ? nTerms | DrawRandomInt(1, 4);\n if nVarInClause > nVar {\n set nVarInClause = nVar;\n }\n\n mutable clause = [(0, false), size = nVarInClause];\n mutable usedVariables = [false, size = nVar];\n // Make sure variables in the clause are distinct\n for k in 0..nVarInClause - 1 {\n mutable nextInd = -1;\n repeat {\n set nextInd = DrawRandomInt(0, nVar - 1);\n } until (not usedVariables[nextInd])\n fixup {}\n set clause w/= k <- (nextInd, DrawRandomBool(0.5));\n set usedVariables w/= nextInd <- true;\n }\n return clause;\n }\n\n operation GenerateSATInstance(nVar : Int, nClause : Int, nTerms : Int) : (Int, Bool)[][] {\n mutable problem = [[(0, false), size = 0], size = nClause];\n\n for j in 0..nClause - 1 {\n set problem w/= j <- Generate_SAT_Clause(nVar, nTerms);\n }\n return problem;\n }\n}\n"
9627
9627
  },
9628
9628
  {
9629
9629
  "id": "solving_sat__or__Verification.qs",
9630
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Arrays;\n\n function F_Or(args : Bool[]) : Bool {\n return Count(x -> x, args) > 0;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 1..5 {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_Or, F_Or) {\n Message($\"Test failed for n = {n}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9630
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Arrays.*;\n\n function F_Or(args : Bool[]) : Bool {\n return Count(x -> x, args) > 0;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 1..5 {\n if not CheckOracleImplementsFunction(n, Kata.Oracle_Or, F_Or) {\n Message($\"Test failed for n = {n}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9631
9631
  },
9632
9632
  {
9633
9633
  "id": "solving_sat__sat_clause__Verification.qs",
9634
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Random;\n\n function F_SATClause(args : Bool[], clause : (Int, Bool)[]) : Bool {\n for (index, positive) in clause {\n if positive == args[index] {\n // one true literal is sufficient for the clause to be true\n return true;\n }\n }\n // none of the literals is true - the whole clause is false\n return false;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for i in 1 .. 6 {\n let nVar = DrawRandomInt(3, 7);\n let clause = Generate_SAT_Clause(nVar, i);\n \n if not CheckOracleImplementsFunction(nVar, Kata.Oracle_SATClause(_, _, clause), F_SATClause(_, clause)) {\n Message($\"Test failed for SAT clause {SATClauseAsString(clause)}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9634
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Random.*;\n\n function F_SATClause(args : Bool[], clause : (Int, Bool)[]) : Bool {\n for (index, positive) in clause {\n if positive == args[index] {\n // one true literal is sufficient for the clause to be true\n return true;\n }\n }\n // none of the literals is true - the whole clause is false\n return false;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for i in 1..6 {\n let nVar = DrawRandomInt(3, 7);\n let clause = Generate_SAT_Clause(nVar, i);\n\n if not CheckOracleImplementsFunction(nVar, Kata.Oracle_SATClause(_, _, clause), F_SATClause(_, clause)) {\n Message($\"Test failed for SAT clause {SATClauseAsString(clause)}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9635
9635
  },
9636
9636
  {
9637
9637
  "id": "solving_sat__sat_formula__Verification.qs",
9638
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Random;\n\n function F_SATClause(args : Bool[], clause : (Int, Bool)[]) : Bool {\n for (index, positive) in clause {\n if positive == args[index] {\n // one true literal is sufficient for the clause to be true\n return true;\n }\n }\n // none of the literals is true - the whole clause is false\n return false;\n }\n\n function F_SATFormula(args : Bool[], formula : (Int, Bool)[][]) : Bool {\n for clause in formula {\n // one false clause invalidates the whole formula\n if not F_SATClause(args, clause) {\n return false\n }\n }\n return true;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for nVar in 2 .. 6 {\n for _ in 1 .. 3 {\n let formula = GenerateSATInstance(nVar, nVar - 1, -1);\n \n if not CheckOracleImplementsFunction(nVar, Kata.Oracle_SATFormula(_, _, formula), F_SATFormula(_, formula)) {\n Message($\"Test failed for SAT formula {SATFormulaAsString(formula)}\");\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9638
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Random.*;\n\n function F_SATClause(args : Bool[], clause : (Int, Bool)[]) : Bool {\n for (index, positive) in clause {\n if positive == args[index] {\n // one true literal is sufficient for the clause to be true\n return true;\n }\n }\n // none of the literals is true - the whole clause is false\n return false;\n }\n\n function F_SATFormula(args : Bool[], formula : (Int, Bool)[][]) : Bool {\n for clause in formula {\n // one false clause invalidates the whole formula\n if not F_SATClause(args, clause) {\n return false\n }\n }\n return true;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for nVar in 2..6 {\n for _ in 1..3 {\n let formula = GenerateSATInstance(nVar, nVar - 1, -1);\n\n if not CheckOracleImplementsFunction(nVar, Kata.Oracle_SATFormula(_, _, formula), F_SATFormula(_, formula)) {\n Message($\"Test failed for SAT formula {SATFormulaAsString(formula)}\");\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9639
9639
  },
9640
9640
  {
9641
9641
  "id": "solving_sat__exactly_one_one__Verification.qs",
9642
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Katas;\n\n function F_Exactly1One (args : Bool[]) : Bool {\n return Count(x -> x, args) == 1;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n if not CheckOracleImplementsFunction(3, Kata.Oracle_Exactly1One, F_Exactly1One) {\n return false;\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9642
+ "code": "namespace Kata.Verification {\n import Std.Arrays.*;\n import KatasUtils.*;\n\n function F_Exactly1One(args : Bool[]) : Bool {\n return Count(x -> x, args) == 1;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n if not CheckOracleImplementsFunction(3, Kata.Oracle_Exactly1One, F_Exactly1One) {\n return false;\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9643
9643
  },
9644
9644
  {
9645
9645
  "id": "solving_sat__exactly_one_one_formula__Verification.qs",
9646
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Random;\n\n function F_Exactly1SATClause (args : Bool[], clause : (Int, Bool)[]) : Bool {\n mutable nOnes = 0;\n for (index, isTrue) in clause {\n if isTrue == args[index] {\n set nOnes += 1;\n }\n }\n return nOnes == 1;\n }\n\n function F_Exactly1SATFormula (args : Bool[], formula : (Int, Bool)[][]) : Bool {\n for clause in formula {\n if not F_Exactly1SATClause(args, clause) {\n return false;\n }\n }\n return true;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for nVar in 3 .. 6 {\n for _ in 1 .. 3 {\n let formula = GenerateSATInstance(nVar, nVar - 1, 3);\n \n if not CheckOracleImplementsFunction(nVar, Kata.Oracle_Exactly13SATFormula(_, _, formula), F_Exactly1SATFormula(_, formula)) {\n Message($\"Test failed for SAT formula {SATFormulaAsString(formula)}\");\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9646
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Random.*;\n\n function F_Exactly1SATClause(args : Bool[], clause : (Int, Bool)[]) : Bool {\n mutable nOnes = 0;\n for (index, isTrue) in clause {\n if isTrue == args[index] {\n set nOnes += 1;\n }\n }\n return nOnes == 1;\n }\n\n function F_Exactly1SATFormula(args : Bool[], formula : (Int, Bool)[][]) : Bool {\n for clause in formula {\n if not F_Exactly1SATClause(args, clause) {\n return false;\n }\n }\n return true;\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for nVar in 3..6 {\n for _ in 1..3 {\n let formula = GenerateSATInstance(nVar, nVar - 1, 3);\n\n if not CheckOracleImplementsFunction(nVar, Kata.Oracle_Exactly13SATFormula(_, _, formula), F_Exactly1SATFormula(_, formula)) {\n Message($\"Test failed for SAT formula {SATFormulaAsString(formula)}\");\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9647
9647
  },
9648
9648
  {
9649
9649
  "id": "solving_graph_coloring__vertex_coloring_classical__Verification.qs",
9650
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let testGraphs = ExampleGraphs()[0 .. 4];\n let testColorings = [\n [([0, 0, 0], true), ([2, 1, 3], true)],\n [([0, 2, 1, 3], true), ([3, 0, 1, 2], true), ([0, 2, 1, 0], false)],\n [([0, 1, 2, 3, 4], true), ([0, 2, 1, 0, 3], true), ([1, 0, 1, 2, 1], false), ([0, 0, 0, 0, 0], false)],\n [([0, 1, 0, 2, 1], true), ([0, 2, 0, 1, 3], true), ([0, 1, 0, 1, 2], false)],\n [([1, 2, 3, 1, 2], true), ([1, 2, 3, 4, 1], false)]\n ];\n for ((V, edges), colorings) in Zipped(testGraphs, testColorings) {\n for (coloring, expected) in colorings {\n if Kata.IsVertexColoringValid(V, edges, coloring) != expected {\n Message($\"Coloring {coloring} evaluated incorrectly for graph V = {V}, edges = {edges}: expected {expected}\");\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9650
+ "code": "namespace Kata.Verification {\n import Std.Arrays.*;\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let testGraphs = ExampleGraphs()[0..4];\n let testColorings = [\n [([0, 0, 0], true), ([2, 1, 3], true)],\n [([0, 2, 1, 3], true), ([3, 0, 1, 2], true), ([0, 2, 1, 0], false)],\n [([0, 1, 2, 3, 4], true), ([0, 2, 1, 0, 3], true), ([1, 0, 1, 2, 1], false), ([0, 0, 0, 0, 0], false)],\n [([0, 1, 0, 2, 1], true), ([0, 2, 0, 1, 3], true), ([0, 1, 0, 1, 2], false)],\n [([1, 2, 3, 1, 2], true), ([1, 2, 3, 4, 1], false)]\n ];\n for ((V, edges), colorings) in Zipped(testGraphs, testColorings) {\n for (coloring, expected) in colorings {\n if Kata.IsVertexColoringValid(V, edges, coloring) != expected {\n Message($\"Coloring {coloring} evaluated incorrectly for graph V = {V}, edges = {edges}: expected {expected}\");\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9651
9651
  },
9652
9652
  {
9653
9653
  "id": "solving_graph_coloring__Common.qs",
9654
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Katas;\n\n // Hardcoded graphs used for testing the vertex coloring problem:\n // - trivial graph with zero edges\n // - complete graph with 4 vertices (4-colorable)\n // - disconnected graph\n // - random connected graph with more edges and vertices (3-colorable)\n // - regular-ish graph with 5 vertices (3-colorable, as shown at https://en.wikipedia.org/wiki/File:3-coloringEx.svg without one vertex)\n // - 6-vertex graph from https://en.wikipedia.org/wiki/File:3-coloringEx.svg\n function ExampleGraphs() : (Int, (Int, Int)[])[] {\n return [(3, []),\n (4, [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]),\n (5, [(4, 0), (2, 1), (3, 1), (3, 2)]),\n (5, [(0, 1), (1, 2), (1, 3), (3, 2), (4, 2), (3, 4)]),\n (5, [(0, 1), (0, 2), (0, 4), (1, 2), (1, 3), (2, 3), (2, 4), (3, 4)]),\n (6, [(0, 1), (0, 2), (0, 4), (0, 5), (1, 2), (1, 3), (1, 5), (2, 3), (2, 4), (3, 4), (3, 5), (4, 5)])];\n // Graphs with 6+ vertices can take several minutes to be processed; \n // in the interest of keeping test runtime reasonable we're limiting most of the testing to graphs with 5 vertices or fewer.\n }\n\n\n function IsVertexColoringValid_Reference (V : Int, edges: (Int, Int)[], colors: Int[]) : Bool {\n for (start, end) in edges {\n if colors[start] == colors[end] {\n return false;\n }\n }\n return true;\n }\n\n\n operation ReadColoring_Reference(nBits : Int, qs : Qubit[]) : Int[] {\n let colorPartitions = Chunks(nBits, qs);\n let measureColor = qs => ResultArrayAsInt(Reversed(MeasureEachZ(qs)));\n return ForEach(measureColor, colorPartitions);\n }\n\n\n // Helper function specific to Graph Coloring kata.\n operation CheckOracleRecognizesColoring (\n V : Int,\n edges : (Int, Int)[],\n oracle : (Int, (Int, Int)[],Qubit[], Qubit) => Unit,\n classicalFunction : (Int, (Int, Int)[], Int[]) -> Bool\n ) : Bool {\n // Message($\"Testing V = {V}, edges = {edges}\");\n let N = 2 * V;\n use (coloringRegister, target) = (Qubit[N], Qubit());\n // Try all possible colorings of 4 colors on V vertices and check if they are calculated correctly.\n // Hack: fix the color of the first vertex, since all colorings are agnostic to the specific colors used.\n for k in 0 .. (1 <<< (N - 2)) - 1 {\n // Prepare k-th coloring\n let binary = [false, false] + IntAsBoolArray(k, N - 2);\n ApplyPauliFromBitString(PauliX, true, binary, coloringRegister);\n\n // Read out the coloring (convert one bitmask into V integers) - does not change the state\n let coloring = ReadColoring_Reference(2, coloringRegister);\n\n // Apply the oracle\n oracle(V, edges, coloringRegister, target);\n\n // Check that the oracle result matches the classical result\n let val = classicalFunction(V, edges, coloring);\n if val {\n X(target);\n }\n // Uncompute\n ApplyPauliFromBitString(PauliX, true, binary, coloringRegister);\n\n if not CheckAllZero(coloringRegister + [target]) {\n Message($\"Incorrect result for V = {V}, edges = {edges}\");\n Message($\"For test case with input = {BoolArrayAsKetState(binary)}, coloring = {coloring}\");\n if not CheckZero(target) {\n Message($\"Expected answer = {val}, got {not val}\");\n } else {\n Message(\"The input state should not change\");\n }\n ResetAll(coloringRegister + [target]);\n return false;\n }\n }\n\n true\n }\n\n function IsWeakColoringValid_OneVertex_Reference (V : Int, edges: (Int, Int)[], colors: Int[], vertex : Int) : Bool {\n mutable neighborCount = 0;\n mutable hasDifferentNeighbor = false;\n\n for (start, end) in edges {\n if start == vertex or end == vertex {\n set neighborCount += 1;\n if colors[start] != colors[end] {\n set hasDifferentNeighbor = true;\n }\n }\n }\n\n return neighborCount == 0 or hasDifferentNeighbor;\n }\n\n function IsWeakColoringValid_Reference (V : Int, edges: (Int, Int)[], colors: Int[]) : Bool {\n for v in 0 .. V - 1 {\n if not IsWeakColoringValid_OneVertex_Reference(V, edges, colors, v) {\n return false;\n }\n }\n\n return true;\n }\n\n}"
9654
+ "code": "namespace Kata.Verification {\n import Std.Arrays.*;\n import Std.Convert.*;\n import Std.Diagnostics.*;\n import KatasUtils.*;\n\n // Hardcoded graphs used for testing the vertex coloring problem:\n // - trivial graph with zero edges\n // - complete graph with 4 vertices (4-colorable)\n // - disconnected graph\n // - random connected graph with more edges and vertices (3-colorable)\n // - regular-ish graph with 5 vertices (3-colorable, as shown at https://en.wikipedia.org/wiki/File:3-coloringEx.svg without one vertex)\n // - 6-vertex graph from https://en.wikipedia.org/wiki/File:3-coloringEx.svg\n function ExampleGraphs() : (Int, (Int, Int)[])[] {\n return [\n (3, []),\n (4, [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]),\n (5, [(4, 0), (2, 1), (3, 1), (3, 2)]),\n (5, [(0, 1), (1, 2), (1, 3), (3, 2), (4, 2), (3, 4)]),\n (5, [(0, 1), (0, 2), (0, 4), (1, 2), (1, 3), (2, 3), (2, 4), (3, 4)]),\n (6, [(0, 1), (0, 2), (0, 4), (0, 5), (1, 2), (1, 3), (1, 5), (2, 3), (2, 4), (3, 4), (3, 5), (4, 5)])\n ];\n // Graphs with 6+ vertices can take several minutes to be processed;\n // in the interest of keeping test runtime reasonable we're limiting most of the testing to graphs with 5 vertices or fewer.\n }\n\n\n function IsVertexColoringValid_Reference(V : Int, edges : (Int, Int)[], colors : Int[]) : Bool {\n for (start, end) in edges {\n if colors[start] == colors[end] {\n return false;\n }\n }\n return true;\n }\n\n\n operation ReadColoring_Reference(nBits : Int, qs : Qubit[]) : Int[] {\n let colorPartitions = Chunks(nBits, qs);\n let measureColor = qs => ResultArrayAsInt(Reversed(MeasureEachZ(qs)));\n return ForEach(measureColor, colorPartitions);\n }\n\n\n // Helper function specific to Graph Coloring kata.\n operation CheckOracleRecognizesColoring(\n V : Int,\n edges : (Int, Int)[],\n oracle : (Int, (Int, Int)[], Qubit[], Qubit) => Unit,\n classicalFunction : (Int, (Int, Int)[], Int[]) -> Bool\n ) : Bool {\n // Message($\"Testing V = {V}, edges = {edges}\");\n let N = 2 * V;\n use (coloringRegister, target) = (Qubit[N], Qubit());\n // Try all possible colorings of 4 colors on V vertices and check if they are calculated correctly.\n // Hack: fix the color of the first vertex, since all colorings are agnostic to the specific colors used.\n for k in 0..(1 <<< (N - 2)) - 1 {\n // Prepare k-th coloring\n let binary = [false, false] + IntAsBoolArray(k, N - 2);\n ApplyPauliFromBitString(PauliX, true, binary, coloringRegister);\n\n // Read out the coloring (convert one bitmask into V integers) - does not change the state\n let coloring = ReadColoring_Reference(2, coloringRegister);\n\n // Apply the oracle\n oracle(V, edges, coloringRegister, target);\n\n // Check that the oracle result matches the classical result\n let val = classicalFunction(V, edges, coloring);\n if val {\n X(target);\n }\n // Uncompute\n ApplyPauliFromBitString(PauliX, true, binary, coloringRegister);\n\n if not CheckAllZero(coloringRegister + [target]) {\n Message($\"Incorrect result for V = {V}, edges = {edges}\");\n Message($\"For test case with input = {BoolArrayAsKetState(binary)}, coloring = {coloring}\");\n if not CheckZero(target) {\n Message($\"Expected answer = {val}, got {not val}\");\n } else {\n Message(\"The input state should not change\");\n }\n ResetAll(coloringRegister + [target]);\n return false;\n }\n }\n\n true\n }\n\n function IsWeakColoringValid_OneVertex_Reference(V : Int, edges : (Int, Int)[], colors : Int[], vertex : Int) : Bool {\n mutable neighborCount = 0;\n mutable hasDifferentNeighbor = false;\n\n for (start, end) in edges {\n if start == vertex or end == vertex {\n set neighborCount += 1;\n if colors[start] != colors[end] {\n set hasDifferentNeighbor = true;\n }\n }\n }\n\n return neighborCount == 0 or hasDifferentNeighbor;\n }\n\n function IsWeakColoringValid_Reference(V : Int, edges : (Int, Int)[], colors : Int[]) : Bool {\n for v in 0..V - 1 {\n if not IsWeakColoringValid_OneVertex_Reference(V, edges, colors, v) {\n return false;\n }\n }\n\n return true;\n }\n\n}\n"
9655
9655
  },
9656
9656
  {
9657
9657
  "id": "solving_graph_coloring__read_coloring__Verification.qs",
9658
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for nBits in 1..3 {\n for V in 1..3 {\n use qs = Qubit[V * nBits];\n for state in 0..(1 <<< (V * nBits)) - 1 {\n // Prepare the register in the input state\n let binaryState = IntAsBoolArray(state, V * nBits);\n ApplyPauliFromBitString(PauliX, true, binaryState, qs);\n\n // Call the solution\n let result = Kata.ReadColoring(nBits, qs);\n\n // verify that the register remained in the same state\n ApplyPauliFromBitString(PauliX, true, binaryState, qs);\n if not CheckAllZero(qs) {\n Message(\"The input state should not change\");\n ResetAll(qs);\n return false;\n }\n\n // Get the expected coloring by splitting binaryState into parts and converting them into integers\n // (remember to use big endian)\n let partitions = Chunks(nBits, binaryState);\n let partitionToInt = bits -> BoolArrayAsInt(Reversed(bits));\n let expectedColors = Mapped(partitionToInt, partitions);\n\n // Verify the return value\n if Length(result) != V {\n Message($\"Unexpected number of colors for V = {V}, nBits = {nBits} : {Length(result)}\");\n return false;\n }\n for (expected, actual) in Zipped(expectedColors, result) {\n if expected != actual {\n Message($\"Unexpected colors for V = {V}, nBits = {nBits}, \" + \n $\"state = {BoolArrayAsKetState(binaryState)} : expected {expectedColors}, got {result}\");\n return false;\n }\n }\n }\n }\n }\n Message(\"Correct!\");\n true\n }\n}\n"
9658
+ "code": "namespace Kata.Verification {\n import Std.Diagnostics.*;\n import Std.Arrays.*;\n import Std.Convert.*;\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for nBits in 1..3 {\n for V in 1..3 {\n use qs = Qubit[V * nBits];\n for state in 0..(1 <<< (V * nBits)) - 1 {\n // Prepare the register in the input state\n let binaryState = IntAsBoolArray(state, V * nBits);\n ApplyPauliFromBitString(PauliX, true, binaryState, qs);\n\n // Call the solution\n let result = Kata.ReadColoring(nBits, qs);\n\n // verify that the register remained in the same state\n ApplyPauliFromBitString(PauliX, true, binaryState, qs);\n if not CheckAllZero(qs) {\n Message(\"The input state should not change\");\n ResetAll(qs);\n return false;\n }\n\n // Get the expected coloring by splitting binaryState into parts and converting them into integers\n // (remember to use big endian)\n let partitions = Chunks(nBits, binaryState);\n let partitionToInt = bits -> BoolArrayAsInt(Reversed(bits));\n let expectedColors = Mapped(partitionToInt, partitions);\n\n // Verify the return value\n if Length(result) != V {\n Message($\"Unexpected number of colors for V = {V}, nBits = {nBits} : {Length(result)}\");\n return false;\n }\n for (expected, actual) in Zipped(expectedColors, result) {\n if expected != actual {\n Message($\"Unexpected colors for V = {V}, nBits = {nBits}, \" +\n $\"state = {BoolArrayAsKetState(binaryState)} : expected {expectedColors}, got {result}\");\n return false;\n }\n }\n }\n }\n }\n Message(\"Correct!\");\n true\n }\n}\n"
9659
9659
  },
9660
9660
  {
9661
9661
  "id": "solving_graph_coloring__color_equality__Verification.qs",
9662
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Arrays;\n\n function F_ColorEquality(args : Bool[]) : Bool {\n // Check that the first half equals the second half\n let nBits = Length(args) / 2;\n for (f, s) in Zipped(args[...nBits - 1], args[nBits...]) {\n if f != s {\n return false;\n }\n }\n return true;\n }\n\n operation Oracle_ColorEquality_Wrapper(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n let nBits = Length(x) / 2;\n Kata.Oracle_ColorEquality(x[...nBits - 1], x[nBits...], y);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for nBits in 1..3 {\n if not CheckOracleImplementsFunction(2 * nBits, Oracle_ColorEquality_Wrapper, F_ColorEquality) {\n Message($\"Test failed for nBits = {nBits}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9662
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Arrays.*;\n\n function F_ColorEquality(args : Bool[]) : Bool {\n // Check that the first half equals the second half\n let nBits = Length(args) / 2;\n for (f, s) in Zipped(args[...nBits - 1], args[nBits...]) {\n if f != s {\n return false;\n }\n }\n return true;\n }\n\n operation Oracle_ColorEquality_Wrapper(x : Qubit[], y : Qubit) : Unit is Adj + Ctl {\n let nBits = Length(x) / 2;\n Kata.Oracle_ColorEquality(x[...nBits - 1], x[nBits...], y);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for nBits in 1..3 {\n if not CheckOracleImplementsFunction(2 * nBits, Oracle_ColorEquality_Wrapper, F_ColorEquality) {\n Message($\"Test failed for nBits = {nBits}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9663
9663
  },
9664
9664
  {
9665
9665
  "id": "solving_graph_coloring__vertex_coloring__Verification.qs",
9666
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for (V, edges) in Most(ExampleGraphs()) {\n if not CheckOracleRecognizesColoring(V, edges, Kata.Oracle_VertexColoring, IsVertexColoringValid_Reference) {\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9666
+ "code": "namespace Kata.Verification {\n import Std.Arrays.*;\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for (V, edges) in Most(ExampleGraphs()) {\n if not CheckOracleRecognizesColoring(V, edges, Kata.Oracle_VertexColoring, IsVertexColoringValid_Reference) {\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9667
9667
  },
9668
9668
  {
9669
9669
  "id": "solving_graph_coloring__weak_coloring_classical__Verification.qs",
9670
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let testGraphs = ExampleGraphs()[0 .. 4];\n let testColorings = [\n // Every coloring would pass on a disconnected graph of 3 vertices\n [([0, 0, 0], true), ([2, 1, 3], true)],\n // Every coloring would pass on a fully connected graph of 4 vertices,\n // except for the last coloring in which all vertices are of the same color.\n [([0, 2, 1, 3], true), ([3, 0, 1, 0], true), ([0, 0, 0, 0], false)],\n // The colorings for 5-vertex graphs:\n // - the first one is invalid for all graphs except disconnected\n // - the second one is valid for all types of graphs regardless of their structure\n // - two colorings that is valid or invalid depending on the graph\n [([0, 0, 0, 0, 0], false), ([0, 1, 2, 3, 4], true), ([0, 1, 1, 2, 0], false), ([0, 0, 1, 1, 1], true)],\n [([0, 0, 0, 0, 0], false), ([0, 1, 2, 3, 4], true), ([0, 1, 1, 2, 0], true), ([0, 0, 1, 1, 1], false)],\n [([0, 0, 0, 0, 0], false), ([0, 1, 2, 3, 4], true), ([0, 1, 1, 2, 0], true), ([0, 0, 1, 1, 1], true)]\n ];\n for ((V, edges), colorings) in Zipped(testGraphs, testColorings) {\n for (coloring, expected) in colorings {\n if Kata.IsWeakColoringValid(V, edges, coloring) != expected {\n Message($\"Weak coloring {coloring} evaluated incorrectly for graph V = {V}, edges = {edges}: expected {expected}\");\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9670
+ "code": "namespace Kata.Verification {\n import Std.Arrays.*;\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let testGraphs = ExampleGraphs()[0..4];\n let testColorings = [\n // Every coloring would pass on a disconnected graph of 3 vertices\n [([0, 0, 0], true), ([2, 1, 3], true)],\n // Every coloring would pass on a fully connected graph of 4 vertices,\n // except for the last coloring in which all vertices are of the same color.\n [([0, 2, 1, 3], true), ([3, 0, 1, 0], true), ([0, 0, 0, 0], false)],\n // The colorings for 5-vertex graphs:\n // - the first one is invalid for all graphs except disconnected\n // - the second one is valid for all types of graphs regardless of their structure\n // - two colorings that is valid or invalid depending on the graph\n [([0, 0, 0, 0, 0], false), ([0, 1, 2, 3, 4], true), ([0, 1, 1, 2, 0], false), ([0, 0, 1, 1, 1], true)],\n [([0, 0, 0, 0, 0], false), ([0, 1, 2, 3, 4], true), ([0, 1, 1, 2, 0], true), ([0, 0, 1, 1, 1], false)],\n [([0, 0, 0, 0, 0], false), ([0, 1, 2, 3, 4], true), ([0, 1, 1, 2, 0], true), ([0, 0, 1, 1, 1], true)]\n ];\n for ((V, edges), colorings) in Zipped(testGraphs, testColorings) {\n for (coloring, expected) in colorings {\n if Kata.IsWeakColoringValid(V, edges, coloring) != expected {\n Message($\"Weak coloring {coloring} evaluated incorrectly for graph V = {V}, edges = {edges}: expected {expected}\");\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9671
9671
  },
9672
9672
  {
9673
9673
  "id": "solving_graph_coloring__weak_coloring_one_vertex__Verification.qs",
9674
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for (V, edges) in Most(ExampleGraphs()) {\n for vertex in 0 .. V - 1 {\n if not CheckOracleRecognizesColoring(V, edges, \n Kata.Oracle_WeakColoring_OneVertex(_, _, _, _, vertex), \n IsWeakColoringValid_OneVertex_Reference(_, _, _, vertex)\n ) {\n Message($\"Testing vertex {vertex}\");\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9674
+ "code": "namespace Kata.Verification {\n import Std.Arrays.*;\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for (V, edges) in Most(ExampleGraphs()) {\n for vertex in 0..V - 1 {\n if not CheckOracleRecognizesColoring(\n V,\n edges,\n Kata.Oracle_WeakColoring_OneVertex(_, _, _, _, vertex),\n IsWeakColoringValid_OneVertex_Reference(_, _, _, vertex)\n ) {\n Message($\"Testing vertex {vertex}\");\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9675
9675
  },
9676
9676
  {
9677
9677
  "id": "solving_graph_coloring__weak_coloring__Verification.qs",
9678
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Katas;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for (V, edges) in Most(ExampleGraphs()) {\n if not CheckOracleRecognizesColoring(V, edges, \n Kata.Oracle_WeakColoring, IsWeakColoringValid_Reference\n ) {\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9678
+ "code": "namespace Kata.Verification {\n import Std.Arrays.*;\n import KatasUtils.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for (V, edges) in Most(ExampleGraphs()) {\n if not CheckOracleRecognizesColoring(V, edges, Kata.Oracle_WeakColoring, IsWeakColoringValid_Reference) {\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9679
9679
  },
9680
9680
  {
9681
9681
  "id": "qft__single_qubit__Verification.qs",
9682
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let solution = qs => Kata.SingleQubitQFT(qs[0]);\n let reference = qs => H(qs[0]);\n let isCorrect = CheckOperationsAreEqualStrict(1, solution, reference);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine the 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 }\n isCorrect\n }\n}\n"
9682
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Math.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let solution = qs => Kata.SingleQubitQFT(qs[0]);\n let reference = qs => H(qs[0]);\n let isCorrect = CheckOperationsAreEqualStrict(1, solution, reference);\n\n // Output different feedback to the user depending on whether the solution was correct.\n if isCorrect {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n Message(\"Hint: examine the 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 }\n isCorrect\n }\n}\n"
9683
9683
  },
9684
9684
  {
9685
9685
  "id": "qft__rotation_gate__Verification.qs",
9686
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for k in 0 .. 10 {\n let solution = qs => Kata.Rotation(qs[0], k);\n let reference = qs => R1Frac(2, k, qs[0]);\n if not CheckOperationsAreEqualStrict(1, solution, reference) {\n Message($\"Incorrect for k = {k}.\");\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"
9686
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n import Std.Math.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for k in 0..10 {\n let solution = qs => Kata.Rotation(qs[0], k);\n let reference = qs => R1Frac(2, k, qs[0]);\n if not CheckOperationsAreEqualStrict(1, solution, reference) {\n Message($\"Incorrect for k = {k}.\");\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"
9687
9687
  },
9688
9688
  {
9689
9689
  "id": "qft__binary_fraction_classical__Verification.qs",
9690
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation BinaryFractionClassical_Alternative (q : Qubit, j : Bool[]) : Unit is Adj+Ctl {\n // Convert the number to an integer and apply a single R1 rotation\n R1(2.0 * PI() * IntAsDouble(BoolArrayAsInt(Reversed(j))) / IntAsDouble(1 <<< Length(j)), q);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 1 .. 5 {\n for exp in 0 .. (1 <<< n) - 1 {\n let j = Reversed(IntAsBoolArray(exp, n));\n let solution = qs => Kata.BinaryFractionClassical(qs[0], j);\n let reference = qs => BinaryFractionClassical_Alternative(qs[0], j);\n if not CheckOperationsAreEqualStrict(1, solution, reference) {\n Message($\"Incorrect for j = {j}.\");\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\n Message(\"Correct!\");\n true\n }\n}\n"
9690
+ "code": "namespace Kata.Verification {\n import Std.Arrays.*;\n import Std.Convert.*;\n import KatasUtils.*;\n import Std.Math.*;\n\n operation BinaryFractionClassical_Alternative(q : Qubit, j : Bool[]) : Unit is Adj + Ctl {\n // Convert the number to an integer and apply a single R1 rotation\n R1(2.0 * PI() * IntAsDouble(BoolArrayAsInt(Reversed(j))) / IntAsDouble(1 <<< Length(j)), q);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 1..5 {\n for exp in 0..(1 <<< n) - 1 {\n let j = Reversed(IntAsBoolArray(exp, n));\n let solution = qs => Kata.BinaryFractionClassical(qs[0], j);\n let reference = qs => BinaryFractionClassical_Alternative(qs[0], j);\n if not CheckOperationsAreEqualStrict(1, solution, reference) {\n Message($\"Incorrect for j = {j}.\");\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\n Message(\"Correct!\");\n true\n }\n}\n"
9691
9691
  },
9692
9692
  {
9693
9693
  "id": "qft__binary_fraction_quantum__Verification.qs",
9694
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation BinaryFractionQuantum_Reference(q : Qubit, j : Qubit[]) : Unit is Adj + Ctl {\n for ind in 0 .. Length(j) - 1 {\n Controlled R1Frac([j[ind]], (2, ind + 1, q));\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 1 .. 5 {\n let solution = qs => Kata.BinaryFractionQuantum(qs[0], qs[1 ...]);\n let reference = qs => BinaryFractionQuantum_Reference(qs[0], qs[1 ...]);\n if not CheckOperationsAreEqualStrict(n + 1, solution, reference) {\n Message($\"Incorrect for n = {n}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9694
+ "code": "namespace Kata.Verification {\n import Std.Arrays.*;\n import Std.Convert.*;\n import KatasUtils.*;\n import Std.Math.*;\n\n operation BinaryFractionQuantum_Reference(q : Qubit, j : Qubit[]) : Unit is Adj + Ctl {\n for ind in 0..Length(j) - 1 {\n Controlled R1Frac([j[ind]], (2, ind + 1, q));\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 1..5 {\n let solution = qs => Kata.BinaryFractionQuantum(qs[0], qs[1...]);\n let reference = qs => BinaryFractionQuantum_Reference(qs[0], qs[1...]);\n if not CheckOperationsAreEqualStrict(n + 1, solution, reference) {\n Message($\"Incorrect for n = {n}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9695
9695
  },
9696
9696
  {
9697
9697
  "id": "qft__binary_fraction_inplace__Verification.qs",
9698
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation BinaryFractionQuantumInPlace_Reference(j : Qubit[]) : Unit is Adj + Ctl {\n H(j[0]);\n for ind in 1 .. Length(j) - 1 {\n Controlled R1Frac([j[ind]], (2, ind + 1, j[0]));\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 1 .. 5 {\n if not CheckOperationsAreEqualStrict(n, Kata.BinaryFractionQuantumInPlace, BinaryFractionQuantumInPlace_Reference) {\n Message($\"Incorrect for n = {n}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9698
+ "code": "namespace Kata.Verification {\n import Std.Arrays.*;\n import Std.Convert.*;\n import KatasUtils.*;\n import Std.Math.*;\n\n operation BinaryFractionQuantumInPlace_Reference(j : Qubit[]) : Unit is Adj + Ctl {\n H(j[0]);\n for ind in 1..Length(j) - 1 {\n Controlled R1Frac([j[ind]], (2, ind + 1, j[0]));\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 1..5 {\n if not CheckOperationsAreEqualStrict(n, Kata.BinaryFractionQuantumInPlace, BinaryFractionQuantumInPlace_Reference) {\n Message($\"Incorrect for n = {n}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9699
9699
  },
9700
9700
  {
9701
9701
  "id": "qft__qft__Verification.qs",
9702
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Katas;\n \n operation LibraryQFT(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 1 .. 5 {\n if not CheckOperationsAreEqualStrict(n, Kata.QuantumFourierTransform, LibraryQFT) {\n Message($\"Incorrect for n = {n}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9702
+ "code": "namespace Kata.Verification {\n import Std.Arrays.*;\n import KatasUtils.*;\n\n operation LibraryQFT(qs : Qubit[]) : Unit is Adj + Ctl {\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 1..5 {\n if not CheckOperationsAreEqualStrict(n, Kata.QuantumFourierTransform, LibraryQFT) {\n Message($\"Incorrect for n = {n}.\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9703
9703
  },
9704
9704
  {
9705
9705
  "id": "qft__all_basis_vectors__Verification.qs",
9706
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation AllBasisVectors_Reference (qs : Qubit[]) : Unit is Adj + Ctl {\n for q in qs {\n H(q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 1 .. 5 {\n let solution = Kata.AllBasisVectors;\n let reference = AllBasisVectors_Reference;\n if not CheckOperationsEquivalenceOnZeroState(solution, reference, n) {\n Message($\"Incorrect for {n} qubit(s)\");\n ShowQuantumStateComparison(n, qs => (), solution, reference);\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
9706
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation AllBasisVectors_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n for q in qs {\n H(q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 1..5 {\n let solution = Kata.AllBasisVectors;\n let reference = AllBasisVectors_Reference;\n if not CheckOperationsEquivalenceOnZeroState(solution, reference, n) {\n Message($\"Incorrect for {n} qubit(s)\");\n ShowQuantumStateComparison(n, qs => (), solution, reference);\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
9707
9707
  },
9708
9708
  {
9709
9709
  "id": "qft__periodic_state__Verification.qs",
9710
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Unstable.StatePreparation;\n\n operation PeriodicState_Reference (qs : Qubit[], F : Int) : Unit is Adj + Ctl {\n let n = Length(qs);\n let amps = MappedOverRange(\n k -> ComplexPolar(1.0, 2. * PI() * IntAsDouble(F * k) / IntAsDouble(2 ^ n)), \n 0 .. 2 ^ n - 1);\n ApproximatelyPreparePureStateCP(0.0, amps, qs);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 1 .. 3 {\n for F in 0 .. 2 ^ n - 1 {\n let solution = Kata.PeriodicState(_, F);\n let reference = PeriodicState_Reference(_, F);\n if not CheckOperationsEquivalenceOnZeroState(solution, reference, n) {\n Message($\"Incorrect for {n} qubit(s), F = {F}\");\n ShowQuantumStateComparison(n, qs => (), solution, reference);\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
9710
+ "code": "namespace Kata.Verification {\n import Std.Arrays.*;\n import Std.Convert.*;\n import KatasUtils.*;\n import Std.Math.*;\n import Std.StatePreparation.*;\n\n operation PeriodicState_Reference(qs : Qubit[], F : Int) : Unit is Adj + Ctl {\n let n = Length(qs);\n let amps = MappedOverRange(\n k -> ComplexPolar(1.0, 2. * PI() * IntAsDouble(F * k) / IntAsDouble(2^n)),\n 0..2^n - 1\n );\n ApproximatelyPreparePureStateCP(0.0, amps, qs);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 1..3 {\n for F in 0..2^n - 1 {\n let solution = Kata.PeriodicState(_, F);\n let reference = PeriodicState_Reference(_, F);\n if not CheckOperationsEquivalenceOnZeroState(solution, reference, n) {\n Message($\"Incorrect for {n} qubit(s), F = {F}\");\n ShowQuantumStateComparison(n, qs => (), solution, reference);\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
9711
9711
  },
9712
9712
  {
9713
9713
  "id": "qft__alternating_amplitudes__Verification.qs",
9714
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation AlternatingAmplitudes_Reference (qs : Qubit[]) : Unit is Adj + Ctl {\n for q in qs {\n H(q);\n }\n Z(qs[Length(qs) - 1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 1 .. 5 {\n let solution = Kata.AlternatingAmplitudes;\n let reference = AlternatingAmplitudes_Reference;\n if not CheckOperationsEquivalenceOnZeroState(solution, reference, n) {\n Message($\"Incorrect for {n} qubit(s)\");\n ShowQuantumStateComparison(n, qs => (), solution, reference);\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
9714
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation AlternatingAmplitudes_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n for q in qs {\n H(q);\n }\n Z(qs[Length(qs) - 1]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 1..5 {\n let solution = Kata.AlternatingAmplitudes;\n let reference = AlternatingAmplitudes_Reference;\n if not CheckOperationsEquivalenceOnZeroState(solution, reference, n) {\n Message($\"Incorrect for {n} qubit(s)\");\n ShowQuantumStateComparison(n, qs => (), solution, reference);\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
9715
9715
  },
9716
9716
  {
9717
9717
  "id": "qft__all_even_vectors__Verification.qs",
9718
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation AllEvenVectors_Reference (qs : Qubit[]) : Unit is Adj + Ctl {\n for q in qs[...Length(qs) - 2] {\n H(q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 1 .. 5 {\n let solution = Kata.AllEvenVectors;\n let reference = AllEvenVectors_Reference;\n if not CheckOperationsEquivalenceOnZeroState(solution, reference, n) {\n Message($\"Incorrect for {n} qubit(s)\");\n ShowQuantumStateComparison(n, qs => (), solution, reference);\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
9718
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation AllEvenVectors_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n for q in qs[...Length(qs) - 2] {\n H(q);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 1..5 {\n let solution = Kata.AllEvenVectors;\n let reference = AllEvenVectors_Reference;\n if not CheckOperationsEquivalenceOnZeroState(solution, reference, n) {\n Message($\"Incorrect for {n} qubit(s)\");\n ShowQuantumStateComparison(n, qs => (), solution, reference);\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
9719
9719
  },
9720
9720
  {
9721
9721
  "id": "qft__square_wave__Verification.qs",
9722
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation SquareWave_Reference (qs : Qubit[]) : Unit is Adj + Ctl {\n for q in qs {\n H(q);\n }\n Z(qs[Length(qs) - 2]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 2 .. 5 {\n let solution = Kata.SquareWave;\n let reference = SquareWave_Reference;\n if not CheckOperationsEquivalenceOnZeroState(solution, reference, n) {\n Message($\"Incorrect for {n} qubit(s)\");\n ShowQuantumStateComparison(n, qs => (), solution, reference);\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
9722
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation SquareWave_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n for q in qs {\n H(q);\n }\n Z(qs[Length(qs) - 2]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 2..5 {\n let solution = Kata.SquareWave;\n let reference = SquareWave_Reference;\n if not CheckOperationsEquivalenceOnZeroState(solution, reference, n) {\n Message($\"Incorrect for {n} qubit(s)\");\n ShowQuantumStateComparison(n, qs => (), solution, reference);\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
9723
9723
  },
9724
9724
  {
9725
9725
  "id": "qft__signal_frequency__Verification.qs",
9726
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Katas;\n\n operation PreparePeriodicState(qs : Qubit[], F : Int) : Unit is Adj + Ctl {\n let bitsBE = Reversed(IntAsBoolArray(F, Length(qs)));\n ApplyPauliFromBitString(PauliX, true, bitsBE, qs);\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 2 .. 4 {\n use qs = Qubit[n];\n for F in 0 .. 2 ^ n - 1 {\n PreparePeriodicState(qs, F);\n let fRes = Kata.SignalFrequency(qs);\n ResetAll(qs);\n if fRes != F {\n Message($\"Incorrect frequency for n = {n}, F = {F}: got {fRes}\");\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
9726
+ "code": "namespace Kata.Verification {\n import Std.Arrays.*;\n import Std.Convert.*;\n import KatasUtils.*;\n\n operation PreparePeriodicState(qs : Qubit[], F : Int) : Unit is Adj + Ctl {\n let bitsBE = Reversed(IntAsBoolArray(F, Length(qs)));\n ApplyPauliFromBitString(PauliX, true, bitsBE, qs);\n ApplyQFT(Reversed(qs));\n SwapReverseRegister(qs);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for n in 2..4 {\n use qs = Qubit[n];\n for F in 0..2^n - 1 {\n PreparePeriodicState(qs, F);\n let fRes = Kata.SignalFrequency(qs);\n ResetAll(qs);\n if fRes != F {\n Message($\"Incorrect frequency for n = {n}, F = {F}: got {fRes}\");\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
9727
9727
  },
9728
9728
  {
9729
9729
  "id": "phase_estimation__eigenvalues_s__Verification.qs",
9730
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\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\n @EntryPoint()\n operation CheckSolution() : Bool {\n let actual = Kata.EigenvaluesS();\n let expected = [Complex(1.0, 0.0), Complex(0.0, 1.0)];\n if Length(actual) != 2 {\n Message(\"The array of eigenvalues should have exactly two elements.\");\n return false;\n }\n if ComplexEqual(actual[0], expected[0]) and ComplexEqual(actual[1], expected[1]) or \n ComplexEqual(actual[0], expected[1]) and ComplexEqual(actual[1], expected[0]) {\n Message(\"Correct!\");\n return true;\n }\n Message(\"Incorrect value for one of the eigenvalues.\");\n return false;\n }\n}\n"
9730
+ "code": "namespace Kata.Verification {\n import Std.Math.*;\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\n @EntryPoint()\n operation CheckSolution() : Bool {\n let actual = Kata.EigenvaluesS();\n let expected = [Complex(1.0, 0.0), Complex(0.0, 1.0)];\n if Length(actual) != 2 {\n Message(\"The array of eigenvalues should have exactly two elements.\");\n return false;\n }\n if ComplexEqual(actual[0], expected[0]) and ComplexEqual(actual[1], expected[1]) or\n ComplexEqual(actual[0], expected[1]) and ComplexEqual(actual[1], expected[0]) {\n Message(\"Correct!\");\n return true;\n }\n Message(\"Incorrect value for one of the eigenvalues.\");\n return false;\n }\n}\n"
9731
9731
  },
9732
9732
  {
9733
9733
  "id": "phase_estimation__eigenvectors_x__Verification.qs",
9734
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let actual = Kata.EigenvectorsX();\n if Length(actual) != 2 {\n Message(\"The array of eigenvectors should have exactly two elements.\");\n return false;\n }\n for i in 0 .. 1 {\n if Length(actual[i]) != 2 {\n Message(\"Each eigenvector should have exactly two elements.\");\n return false;\n }\n if AbsD(actual[i][0]) + AbsD(actual[i][1]) < 1E-9 {\n Message(\"Each eigenvector should be non-zero.\");\n return false;\n }\n }\n\n // One eigenvector has to have equal components, the other one - opposite ones\n if AbsD(actual[0][0] - actual[0][1]) < 1e-9 and AbsD(actual[1][0] + actual[1][1]) < 1e-9 or \n AbsD(actual[0][0] + actual[0][1]) < 1e-9 and AbsD(actual[1][0] - actual[1][1]) < 1e-9 {\n Message(\"Correct!\");\n return true;\n }\n\n Message(\"Incorrect value for one of the eigenvectors.\");\n return false;\n }\n}\n"
9734
+ "code": "namespace Kata.Verification {\n import Std.Math.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let actual = Kata.EigenvectorsX();\n if Length(actual) != 2 {\n Message(\"The array of eigenvectors should have exactly two elements.\");\n return false;\n }\n for i in 0..1 {\n if Length(actual[i]) != 2 {\n Message(\"Each eigenvector should have exactly two elements.\");\n return false;\n }\n if AbsD(actual[i][0]) + AbsD(actual[i][1]) < 1E-9 {\n Message(\"Each eigenvector should be non-zero.\");\n return false;\n }\n }\n\n // One eigenvector has to have equal components, the other one - opposite ones\n if AbsD(actual[0][0] - actual[0][1]) < 1e-9 and AbsD(actual[1][0] + actual[1][1]) < 1e-9 or\n AbsD(actual[0][0] + actual[0][1]) < 1e-9 and AbsD(actual[1][0] - actual[1][1]) < 1e-9 {\n Message(\"Correct!\");\n return true;\n }\n\n Message(\"Incorrect value for one of the eigenvectors.\");\n return false;\n }\n}\n"
9735
9735
  },
9736
9736
  {
9737
9737
  "id": "phase_estimation__state_eigenvector__Verification.qs",
9738
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Unstable.StatePreparation;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let eigenvectors = [\n (Z, I, \"Z, |0⟩\"), \n (Z, X, \"Z, |1⟩\"), \n (S, I, \"S, |0⟩\"), \n (S, X, \"S, |1⟩\"), \n (X, H, \"X, |+⟩\"), \n (X, q => PreparePureStateD([1., -1.], [q]), \"X, |-⟩\")];\n for (U, P, msg) in eigenvectors {\n if not Kata.IsEigenvector(U, P) {\n Message($\"Incorrect for (U, P) = ({msg}): expected true\");\n return false;\n }\n }\n\n let notEigenvectors = [\n (Z, H, \"Z, |+⟩\"), \n (X, X, \"X, |1⟩\"), \n (X, Z, \"X, |0⟩\"), \n (Y, H, \"Y, |+⟩\"), \n (Y, X, \"Y, |1⟩\")];\n for (U, P, msg) in notEigenvectors {\n if Kata.IsEigenvector(U, P) {\n Message($\"Incorrect for (U, |ψ⟩) = ({msg}): expected false\");\n return false;\n }\n }\n \n Message(\"Correct!\");\n return true;\n }\n}\n"
9738
+ "code": "namespace Kata.Verification {\n import Std.StatePreparation.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let eigenvectors = [\n (Z, I, \"Z, |0⟩\"),\n (Z, X, \"Z, |1⟩\"),\n (S, I, \"S, |0⟩\"),\n (S, X, \"S, |1⟩\"),\n (X, H, \"X, |+⟩\"),\n (X, q => PreparePureStateD([1., -1.], [q]), \"X, |-⟩\")\n ];\n for (U, P, msg) in eigenvectors {\n if not Kata.IsEigenvector(U, P) {\n Message($\"Incorrect for (U, P) = ({msg}): expected true\");\n return false;\n }\n }\n\n let notEigenvectors = [\n (Z, H, \"Z, |+⟩\"),\n (X, X, \"X, |1⟩\"),\n (X, Z, \"X, |0⟩\"),\n (Y, H, \"Y, |+⟩\"),\n (Y, X, \"Y, |1⟩\")\n ];\n for (U, P, msg) in notEigenvectors {\n if Kata.IsEigenvector(U, P) {\n Message($\"Incorrect for (U, |ψ⟩) = ({msg}): expected false\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
9739
9739
  },
9740
9740
  {
9741
9741
  "id": "phase_estimation__one_bit_eigenphase__Verification.qs",
9742
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Unstable.StatePreparation;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let eigenvectors = [\n (Z, I, 1, \"Z, |0⟩\"), \n (Z, X, -1, \"Z, |1⟩\"), \n (S, I, 1, \"S, |0⟩\"), \n (X, H, 1, \"X, |+⟩\"), \n (X, q => PreparePureStateD([1., -1.], [q]), -1, \"X, |-⟩\")];\n for (U, P, expected, msg) in eigenvectors {\n let actual = Kata.OneBitPhaseEstimation(U, P);\n if actual != expected {\n Message($\"Incorrect eigenvalue for (U, |ψ⟩) = ({msg}): expected {expected}, got {actual}\");\n return false;\n }\n }\n \n Message(\"Correct!\");\n return true;\n }\n}\n"
9742
+ "code": "namespace Kata.Verification {\n import Std.StatePreparation.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let eigenvectors = [\n (Z, I, 1, \"Z, |0⟩\"),\n (Z, X, -1, \"Z, |1⟩\"),\n (S, I, 1, \"S, |0⟩\"),\n (X, H, 1, \"X, |+⟩\"),\n (X, q => PreparePureStateD([1., -1.], [q]), -1, \"X, |-⟩\")\n ];\n for (U, P, expected, msg) in eigenvectors {\n let actual = Kata.OneBitPhaseEstimation(U, P);\n if actual != expected {\n Message($\"Incorrect eigenvalue for (U, |ψ⟩) = ({msg}): expected {expected}, got {actual}\");\n return false;\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
9743
9743
  },
9744
9744
  {
9745
9745
  "id": "phase_estimation__implement_qpe__Verification.qs",
9746
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Unstable.StatePreparation;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let tests = [\n (Z, I, 1, 0, \"Z, |0⟩\"), \n (Z, X, 1, 1, \"Z, |1⟩\"), \n (X, H, 1, 0, \"X, |+⟩\"), \n (X, q => PreparePureStateD([1., -1.], [q]), 1, 1, \"X, |-⟩\"),\n (S, I, 2, 0, \"S, |0⟩\"), \n (S, X, 2, 1, \"S, |1⟩\"), \n (Z, X, 2, 2, \"Z, |1⟩\"), // Higher precision than necessary\n (T, I, 3, 0, \"T, |0⟩\"),\n (T, X, 3, 1, \"T, |1⟩\"),\n (S, X, 3, 2, \"S, |1⟩\"), // Higher precision than necessary\n (Z, X, 3, 4, \"Z, |1⟩\"), // Higher precision than necessary\n ];\n for (U, P, n, expected, msg) in tests {\n for _ in 1 .. 10 { // Repeat several times to catch probabilistic failures\n let actual = Kata.PhaseEstimation(U, P, n);\n if actual != expected {\n Message($\"Incorrect eigenphase for (U, |ψ⟩, n) = ({msg}, {n}): expected {expected}, got {actual}\");\n return false;\n }\n }\n }\n \n Message(\"Correct!\");\n return true;\n }\n}\n"
9746
+ "code": "namespace Kata.Verification {\n import Std.StatePreparation.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let tests = [\n (Z, I, 1, 0, \"Z, |0⟩\"),\n (Z, X, 1, 1, \"Z, |1⟩\"),\n (X, H, 1, 0, \"X, |+⟩\"),\n (X, q => PreparePureStateD([1., -1.], [q]), 1, 1, \"X, |-⟩\"),\n (S, I, 2, 0, \"S, |0⟩\"),\n (S, X, 2, 1, \"S, |1⟩\"),\n (Z, X, 2, 2, \"Z, |1⟩\"), // Higher precision than necessary\n (T, I, 3, 0, \"T, |0⟩\"),\n (T, X, 3, 1, \"T, |1⟩\"),\n (S, X, 3, 2, \"S, |1⟩\"), // Higher precision than necessary\n (Z, X, 3, 4, \"Z, |1⟩\"), // Higher precision than necessary\n ];\n for (U, P, n, expected, msg) in tests {\n for _ in 1..10 {\n // Repeat several times to catch probabilistic failures\n let actual = Kata.PhaseEstimation(U, P, n);\n if actual != expected {\n Message($\"Incorrect eigenphase for (U, |ψ⟩, n) = ({msg}, {n}): expected {expected}, got {actual}\");\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n return true;\n }\n}\n"
9747
9747
  },
9748
9748
  {
9749
9749
  "id": "qec_shor__zz_measurement__Verification.qs",
9750
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation StatePrep_ZZMeasurement(qs : Qubit[], state : Int, alpha : Double) : Unit is Adj {\n // prep cos(alpha) * |0..0⟩ + sin(alpha) * |1..1⟩\n Ry(2.0 * alpha, qs[0]);\n CNOT(qs[0], qs[1]);\n\n if state == 1 {\n X(qs[0]);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishStates_MultiQubit(\n 2,\n 2,\n StatePrep_ZZMeasurement,\n Kata.ZZMeasurement,\n true,\n [\"α|00⟩ + β|11⟩\", \"α|01⟩ + β|10⟩\"]);\n\n if (isCorrect) {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n\n isCorrect\n }\n}\n"
9750
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation StatePrep_ZZMeasurement(qs : Qubit[], state : Int, alpha : Double) : Unit is Adj {\n // prep cos(alpha) * |0..0⟩ + sin(alpha) * |1..1⟩\n Ry(2.0 * alpha, qs[0]);\n CNOT(qs[0], qs[1]);\n\n if state == 1 {\n X(qs[0]);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishStates_MultiQubit(\n 2,\n 2,\n StatePrep_ZZMeasurement,\n Kata.ZZMeasurement,\n true,\n [\"α|00⟩ + β|11⟩\", \"α|01⟩ + β|10⟩\"]\n );\n\n if (isCorrect) {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n\n isCorrect\n }\n}\n"
9751
9751
  },
9752
9752
  {
9753
9753
  "id": "qec_shor__Common.qs",
9754
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Random;\n\n operation CheckErrorDetection(\n n : Int,\n encode : (Qubit[] => Unit is Adj),\n error : (Qubit => Unit is Adj),\n detect : (Qubit[] => Int)\n ) : Bool {\n for err_ind in -1 .. n - 1 {\n for _ in 1 .. 10 {\n use qs = Qubit[n];\n let theta = DrawRandomDouble(0.0, 1.0);\n within {\n // Prepare logical state on first qubit\n Ry(2.0 * theta * PI(), qs[0]);\n // Encode the state in multiple qubits\n encode(qs);\n // Introduce X error\n if err_ind > -1 {\n error(qs[err_ind]);\n }\n } apply {\n // Call solution to detect index\n let detected = detect(qs);\n // Check that it is correct\n if detected != err_ind {\n Message(\"Incorrect.\");\n let actual = err_ind == -1 ? \"No error happened\" | $\"Error happened on qubit {err_ind}\";\n Message($\"{actual}, but solution returned {detected}\");\n ResetAll(qs);\n return false;\n }\n }\n // Check that the state was not modified by the solution\n if not CheckAllZero(qs) {\n Message(\"Incorrect.\");\n Message(\"The state of the qubits changed after the solution was applied\");\n ResetAll(qs);\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9754
+ "code": "namespace Kata.Verification {\n import Std.Diagnostics.*;\n import Std.Math.*;\n import Std.Random.*;\n\n operation CheckErrorDetection(\n n : Int,\n encode : (Qubit[] => Unit is Adj),\n error : (Qubit => Unit is Adj),\n detect : (Qubit[] => Int)\n ) : Bool {\n for err_ind in -1..n - 1 {\n for _ in 1..10 {\n use qs = Qubit[n];\n let theta = DrawRandomDouble(0.0, 1.0);\n within {\n // Prepare logical state on first qubit\n Ry(2.0 * theta * PI(), qs[0]);\n // Encode the state in multiple qubits\n encode(qs);\n // Introduce X error\n if err_ind > -1 {\n error(qs[err_ind]);\n }\n } apply {\n // Call solution to detect index\n let detected = detect(qs);\n // Check that it is correct\n if detected != err_ind {\n Message(\"Incorrect.\");\n let actual = err_ind == -1 ? \"No error happened\" | $\"Error happened on qubit {err_ind}\";\n Message($\"{actual}, but solution returned {detected}\");\n ResetAll(qs);\n return false;\n }\n }\n // Check that the state was not modified by the solution\n if not CheckAllZero(qs) {\n Message(\"Incorrect.\");\n Message(\"The state of the qubits changed after the solution was applied\");\n ResetAll(qs);\n return false;\n }\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9755
9755
  },
9756
9756
  {
9757
9757
  "id": "qec_shor__xx_measurement__Verification.qs",
9758
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation StatePrep_XXMeasurement(qs : Qubit[], state : Int, alpha : Double) : Unit is Adj {\n // prep cos(alpha) * |0..0⟩ + sin(alpha) * |1..1⟩\n Ry(2.0 * alpha, qs[0]);\n CNOT(qs[0], qs[1]);\n\n if state == 1 {\n X(qs[0]);\n }\n\n ApplyToEachA(H, qs);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishStates_MultiQubit(\n 2,\n 2,\n StatePrep_XXMeasurement,\n Kata.XXMeasurement,\n true,\n [\"α|++⟩ + β|--⟩\", \"α|+-⟩ + β|-+⟩\"]);\n\n if (isCorrect) {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n\n isCorrect\n }\n}\n"
9758
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation StatePrep_XXMeasurement(qs : Qubit[], state : Int, alpha : Double) : Unit is Adj {\n // prep cos(alpha) * |0..0⟩ + sin(alpha) * |1..1⟩\n Ry(2.0 * alpha, qs[0]);\n CNOT(qs[0], qs[1]);\n\n if state == 1 {\n X(qs[0]);\n }\n\n ApplyToEachA(H, qs);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let isCorrect = DistinguishStates_MultiQubit(\n 2,\n 2,\n StatePrep_XXMeasurement,\n Kata.XXMeasurement,\n true,\n [\"α|++⟩ + β|--⟩\", \"α|+-⟩ + β|-+⟩\"]\n );\n\n if (isCorrect) {\n Message(\"Correct!\");\n } else {\n Message(\"Incorrect.\");\n }\n\n isCorrect\n }\n}\n"
9759
9759
  },
9760
9760
  {
9761
9761
  "id": "qec_shor__bitflip_encode__Verification.qs",
9762
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation BitflipEncode_Reference (qs : Qubit[]) : Unit is Adj + Ctl {\n CNOT(qs[0], qs[1]);\n CNOT(qs[0], qs[2]);\n }\n\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let range = 10;\n for i in 0 .. range - 1 {\n let angle = 2.0 * PI() * IntAsDouble(i) / IntAsDouble(range);\n let initialState = qs => Ry(2.0 * angle, qs[0]);\n let isCorrect = CheckOperationsEquivalenceOnInitialStateStrict(\n initialState,\n Kata.BitflipEncode, \n BitflipEncode_Reference, \n 3);\n if not isCorrect {\n Message(\"Incorrect\");\n Message($\"Test fails for alpha = {Cos(angle)}, beta = {Sin(angle)}.\");\n ShowQuantumStateComparison(3, initialState, Kata.BitflipEncode, BitflipEncode_Reference);\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9762
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import Std.Diagnostics.*;\n import KatasUtils.*;\n import Std.Math.*;\n\n operation BitflipEncode_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n CNOT(qs[0], qs[1]);\n CNOT(qs[0], qs[2]);\n }\n\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let range = 10;\n for i in 0..range - 1 {\n let angle = 2.0 * PI() * IntAsDouble(i) / IntAsDouble(range);\n let initialState = qs => Ry(2.0 * angle, qs[0]);\n let isCorrect = CheckOperationsEquivalenceOnInitialStateStrict(\n initialState,\n Kata.BitflipEncode,\n BitflipEncode_Reference,\n 3\n );\n if not isCorrect {\n Message(\"Incorrect\");\n Message($\"Test fails for alpha = {Cos(angle)}, beta = {Sin(angle)}.\");\n ShowQuantumStateComparison(3, initialState, Kata.BitflipEncode, BitflipEncode_Reference);\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9763
9763
  },
9764
9764
  {
9765
9765
  "id": "qec_shor__bitflip_detect__Verification.qs",
9766
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation BitflipEncode (qs : Qubit[]) : Unit is Adj + Ctl {\n CNOT(qs[0], qs[1]);\n CNOT(qs[0], qs[2]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckErrorDetection(3, BitflipEncode, X, Kata.BitflipDetectError)\n }\n}\n"
9766
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation BitflipEncode(qs : Qubit[]) : Unit is Adj + Ctl {\n CNOT(qs[0], qs[1]);\n CNOT(qs[0], qs[2]);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckErrorDetection(3, BitflipEncode, X, Kata.BitflipDetectError)\n }\n}\n"
9767
9767
  },
9768
9768
  {
9769
9769
  "id": "qec_shor__phaseflip_encode__Verification.qs",
9770
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation PhaseflipEncode_Reference (qs : Qubit[]) : Unit is Adj + Ctl {\n CNOT(qs[0], qs[1]);\n CNOT(qs[0], qs[2]);\n ApplyToEachCA(H, qs);\n }\n\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let range = 10;\n for i in 0 .. range - 1 {\n let angle = 2.0 * PI() * IntAsDouble(i) / IntAsDouble(range);\n let initialState = qs => Ry(2.0 * angle, qs[0]);\n let isCorrect = CheckOperationsEquivalenceOnInitialStateStrict(\n initialState,\n Kata.PhaseflipEncode, \n PhaseflipEncode_Reference, \n 3);\n if not isCorrect {\n Message(\"Incorrect\");\n Message($\"Test fails for alpha = {Cos(angle)}, beta = {Sin(angle)}.\");\n ShowQuantumStateComparison(3, initialState, Kata.PhaseflipEncode, PhaseflipEncode_Reference);\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9770
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import Std.Diagnostics.*;\n import KatasUtils.*;\n import Std.Math.*;\n\n operation PhaseflipEncode_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n CNOT(qs[0], qs[1]);\n CNOT(qs[0], qs[2]);\n ApplyToEachCA(H, qs);\n }\n\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let range = 10;\n for i in 0..range - 1 {\n let angle = 2.0 * PI() * IntAsDouble(i) / IntAsDouble(range);\n let initialState = qs => Ry(2.0 * angle, qs[0]);\n let isCorrect = CheckOperationsEquivalenceOnInitialStateStrict(\n initialState,\n Kata.PhaseflipEncode,\n PhaseflipEncode_Reference,\n 3\n );\n if not isCorrect {\n Message(\"Incorrect\");\n Message($\"Test fails for alpha = {Cos(angle)}, beta = {Sin(angle)}.\");\n ShowQuantumStateComparison(3, initialState, Kata.PhaseflipEncode, PhaseflipEncode_Reference);\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9771
9771
  },
9772
9772
  {
9773
9773
  "id": "qec_shor__phaseflip_detect__Verification.qs",
9774
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Katas;\n\n operation PhaseflipEncode (qs : Qubit[]) : Unit is Adj + Ctl {\n CNOT(qs[0], qs[1]);\n CNOT(qs[0], qs[2]);\n ApplyToEachCA(H, qs);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckErrorDetection(3, PhaseflipEncode, Z, Kata.PhaseflipDetectError)\n }\n}\n"
9774
+ "code": "namespace Kata.Verification {\n import KatasUtils.*;\n\n operation PhaseflipEncode(qs : Qubit[]) : Unit is Adj + Ctl {\n CNOT(qs[0], qs[1]);\n CNOT(qs[0], qs[2]);\n ApplyToEachCA(H, qs);\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n CheckErrorDetection(3, PhaseflipEncode, Z, Kata.PhaseflipDetectError)\n }\n}\n"
9775
9775
  },
9776
9776
  {
9777
9777
  "id": "qec_shor__shor_encode__Verification.qs",
9778
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n\n operation ShorEncode_Reference (qs : Qubit[]) : Unit is Adj + Ctl {\n BitflipEncode(qs[0 .. 3 .. 8]);\n ApplyToEachCA(H, qs[0 .. 3 .. 8]);\n for i in 0 .. 2 {\n BitflipEncode(qs[3 * i .. 3 * i + 2]);\n }\n }\n\n operation BitflipEncode (qs : Qubit[]) : Unit is Adj + Ctl {\n CNOT(qs[0], qs[1]);\n CNOT(qs[0], qs[2]);\n }\n\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let range = 10;\n for i in 0 .. range - 1 {\n let angle = 2.0 * PI() * IntAsDouble(i) / IntAsDouble(range);\n let initialState = qs => Ry(2.0 * angle, qs[0]);\n let isCorrect = CheckOperationsEquivalenceOnInitialStateStrict(\n initialState,\n Kata.ShorEncode, \n ShorEncode_Reference, \n 9);\n if not isCorrect {\n Message(\"Incorrect\");\n Message($\"Test fails for alpha = {Cos(angle)}, beta = {Sin(angle)}.\");\n ShowQuantumStateComparison(9, initialState, Kata.ShorEncode, ShorEncode_Reference);\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9778
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import Std.Diagnostics.*;\n import KatasUtils.*;\n import Std.Math.*;\n\n operation ShorEncode_Reference(qs : Qubit[]) : Unit is Adj + Ctl {\n BitflipEncode(qs[0..3..8]);\n ApplyToEachCA(H, qs[0..3..8]);\n for i in 0..2 {\n BitflipEncode(qs[3 * i..3 * i + 2]);\n }\n }\n\n operation BitflipEncode(qs : Qubit[]) : Unit is Adj + Ctl {\n CNOT(qs[0], qs[1]);\n CNOT(qs[0], qs[2]);\n }\n\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let range = 10;\n for i in 0..range - 1 {\n let angle = 2.0 * PI() * IntAsDouble(i) / IntAsDouble(range);\n let initialState = qs => Ry(2.0 * angle, qs[0]);\n let isCorrect = CheckOperationsEquivalenceOnInitialStateStrict(\n initialState,\n Kata.ShorEncode,\n ShorEncode_Reference,\n 9\n );\n if not isCorrect {\n Message(\"Incorrect\");\n Message($\"Test fails for alpha = {Cos(angle)}, beta = {Sin(angle)}.\");\n ShowQuantumStateComparison(9, initialState, Kata.ShorEncode, ShorEncode_Reference);\n return false;\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9779
9779
  },
9780
9780
  {
9781
9781
  "id": "qec_shor__shor_detect__Verification.qs",
9782
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Diagnostics;\n open Microsoft.Quantum.Katas;\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Random;\n\n operation ShorEncode (qs : Qubit[]) : Unit is Adj + Ctl {\n BitflipEncode(qs[0 .. 3 .. 8]);\n ApplyToEachCA(H, qs[0 .. 3 .. 8]);\n for i in 0 .. 2 {\n BitflipEncode(qs[3 * i .. 3 * i + 2]);\n }\n }\n\n operation BitflipEncode (qs : Qubit[]) : Unit is Adj + Ctl {\n CNOT(qs[0], qs[1]);\n CNOT(qs[0], qs[2]);\n }\n\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for err_ind in -1 .. 8 {\n for err in [PauliX, PauliZ, PauliY] {\n for _ in 1 .. 10 {\n mutable correct = true;\n mutable msg = \"\";\n use qs = Qubit[9];\n let theta = DrawRandomDouble(0.0, 1.0);\n within {\n // Prepare logical state on first qubit\n Ry(2.0 * theta * PI(), qs[0]);\n // Encode the state in multiple qubits\n ShorEncode(qs);\n // Introduce the error\n if err_ind > -1 {\n if err == PauliX {\n X(qs[err_ind]);\n } elif err == PauliZ {\n Z(qs[err_ind]);\n } else {\n Y(qs[err_ind]);\n }\n }\n } apply {\n // Call solution to detect error\n let (detected_ind, detected_err) = Kata.ShorDetectError(qs);\n // Check that it is correct\n if err_ind == -1 {\n // No error\n if detected_ind != -1 {\n set correct = false;\n set msg = $\"There was no error, but the solution detected error at qubit {detected_ind}\";\n }\n } else {\n // There was an error\n if detected_err != err {\n set correct = false;\n set msg = $\"There was a {err} error, but the solution detected {detected_err} error\";\n } else {\n if err == PauliX or err == PauliY {\n if detected_ind != err_ind {\n set correct = false;\n set msg = $\"There was a {err} error at qubit {err_ind}, but the solution detected it at qubit {detected_ind}\";\n }\n } else {\n // For PauliZ errors, cannot say for certain in which qubit of the triplet it happened, so identify triplet\n if detected_ind != err_ind / 3 {\n set correct = false;\n set msg = $\"There was a {err} error at qubit {err_ind}, but the solution detected it at qubit triplet {detected_ind}\";\n }\n }\n }\n }\n }\n // Check that the state was not modified by the solution\n if not CheckAllZero(qs) {\n set correct = false;\n set msg = \"The state of the qubits changed after the solution was applied\";\n }\n\n if not correct {\n Message(\"Incorrect.\");\n Message(msg);\n ResetAll(qs);\n return false;\n }\n }\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9782
+ "code": "namespace Kata.Verification {\n import Std.Diagnostics.*;\n import KatasUtils.*;\n import Std.Math.*;\n import Std.Random.*;\n\n operation ShorEncode(qs : Qubit[]) : Unit is Adj + Ctl {\n BitflipEncode(qs[0..3..8]);\n ApplyToEachCA(H, qs[0..3..8]);\n for i in 0..2 {\n BitflipEncode(qs[3 * i..3 * i + 2]);\n }\n }\n\n operation BitflipEncode(qs : Qubit[]) : Unit is Adj + Ctl {\n CNOT(qs[0], qs[1]);\n CNOT(qs[0], qs[2]);\n }\n\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for err_ind in -1..8 {\n for err in [PauliX, PauliZ, PauliY] {\n for _ in 1..10 {\n mutable correct = true;\n mutable msg = \"\";\n use qs = Qubit[9];\n let theta = DrawRandomDouble(0.0, 1.0);\n within {\n // Prepare logical state on first qubit\n Ry(2.0 * theta * PI(), qs[0]);\n // Encode the state in multiple qubits\n ShorEncode(qs);\n // Introduce the error\n if err_ind > -1 {\n if err == PauliX {\n X(qs[err_ind]);\n } elif err == PauliZ {\n Z(qs[err_ind]);\n } else {\n Y(qs[err_ind]);\n }\n }\n } apply {\n // Call solution to detect error\n let (detected_ind, detected_err) = Kata.ShorDetectError(qs);\n // Check that it is correct\n if err_ind == -1 {\n // No error\n if detected_ind != -1 {\n set correct = false;\n set msg = $\"There was no error, but the solution detected error at qubit {detected_ind}\";\n }\n } else {\n // There was an error\n if detected_err != err {\n set correct = false;\n set msg = $\"There was a {err} error, but the solution detected {detected_err} error\";\n } else {\n if err == PauliX or err == PauliY {\n if detected_ind != err_ind {\n set correct = false;\n set msg = $\"There was a {err} error at qubit {err_ind}, but the solution detected it at qubit {detected_ind}\";\n }\n } else {\n // For PauliZ errors, cannot say for certain in which qubit of the triplet it happened, so identify triplet\n if detected_ind != err_ind / 3 {\n set correct = false;\n set msg = $\"There was a {err} error at qubit {err_ind}, but the solution detected it at qubit triplet {detected_ind}\";\n }\n }\n }\n }\n }\n // Check that the state was not modified by the solution\n if not CheckAllZero(qs) {\n set correct = false;\n set msg = \"The state of the qubits changed after the solution was applied\";\n }\n\n if not correct {\n Message(\"Incorrect.\");\n Message(msg);\n ResetAll(qs);\n return false;\n }\n }\n }\n }\n\n Message(\"Correct!\");\n true\n }\n}\n"
9783
9783
  },
9784
9784
  {
9785
9785
  "id": "nonlocal_games__chsh_classical_win_condition__Verification.qs",
9786
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n\n function WinCondition_Reference(x : Bool, y : Bool, a : Bool, b : Bool) : Bool {\n return (x and y) == (a != b);\n }\n\n @EntryPoint()\n function CheckSolution() : Bool {\n for i in 0..1 <<< 4 - 1 {\n let bits = IntAsBoolArray(i, 4);\n let expected = WinCondition_Reference(bits[0], bits[1], bits[2], bits[3]);\n let actual = Kata.WinCondition(bits[0], bits[1], bits[2], bits[3]);\n\n if actual != expected {\n Message($\"Win condition '{actual}' isn't as expected for X = {bits[0]}, Y = {bits[1]}, \" + \n\t\t $\"A = {bits[2]}, B = {bits[3]}\");\n return false;\n }\n }\n Message(\"Correct!\");\n true\n }\n}\n"
9786
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n\n function WinCondition_Reference(x : Bool, y : Bool, a : Bool, b : Bool) : Bool {\n return (x and y) == (a != b);\n }\n\n @EntryPoint()\n function CheckSolution() : Bool {\n for i in 0..1 <<< 4 - 1 {\n let bits = IntAsBoolArray(i, 4);\n let expected = WinCondition_Reference(bits[0], bits[1], bits[2], bits[3]);\n let actual = Kata.WinCondition(bits[0], bits[1], bits[2], bits[3]);\n\n if actual != expected {\n Message($\"Win condition '{actual}' isn't as expected for X = {bits[0]}, Y = {bits[1]}, \" +\n $\"A = {bits[2]}, B = {bits[3]}\");\n return false;\n }\n }\n Message(\"Correct!\");\n true\n }\n}\n"
9787
9787
  },
9788
9788
  {
9789
9789
  "id": "nonlocal_games__chsh_classical_strategy__Verification.qs",
9790
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Random;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n mutable wins = 0;\n for i in 1..1000 {\n let x = DrawRandomBool(0.5);\n let y = DrawRandomBool(0.5);\n let (a, b) = (Kata.AliceClassical(x), Kata.BobClassical(y));\n if ((x and y) == (a != b)) {\n set wins = wins + 1;\n }\n }\n Message($\"Win rate {IntAsDouble(wins) / 1000.}\");\n if (wins < 700) {\n Message(\"Alice and Bob's classical strategy is not optimal\");\n return false;\n }\n Message(\"Correct!\");\n true\n }\n\n}\n"
9790
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import Std.Random.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n mutable wins = 0;\n for i in 1..1000 {\n let x = DrawRandomBool(0.5);\n let y = DrawRandomBool(0.5);\n let (a, b) = (Kata.AliceClassical(x), Kata.BobClassical(y));\n if ((x and y) == (a != b)) {\n set wins = wins + 1;\n }\n }\n Message($\"Win rate {IntAsDouble(wins) / 1000.}\");\n if (wins < 700) {\n Message(\"Alice and Bob's classical strategy is not optimal\");\n return false;\n }\n Message(\"Correct!\");\n true\n }\n\n}\n"
9791
9791
  },
9792
9792
  {
9793
9793
  "id": "nonlocal_games__chsh_quantum_alice_strategy__Verification.qs",
9794
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Random;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n use q = Qubit();\n for _ in 1 .. 4 {\n // repeat 4 times since we are testing a measurement and wrong basis still might get\n // the correct answer, reduces probability of false positives\n let result = Kata.AliceQuantum(false, q);\n Reset(q);\n if (result != false) {\n Message(\"Measuring |0⟩ in the Z basis returned incorrect value; expected false\");\n return false;\n }\n\n // apply the Pauli X gate\n X(q);\n let result = Kata.AliceQuantum(false, q);\n Reset(q);\n if (result != true) {\n Message(\"Measuring |1⟩ in the Z basis returned incorrect value; expected true\");\n return false;\n }\n\n // apply the Hadamard gate\n H(q);\n let result = Kata.AliceQuantum(true, q);\n Reset(q);\n if (result != false) {\n Message(\"Measuring |+⟩ in the X basis returned incorrect value; expected false\");\n return false;\n }\n\n // apply the Pauli X and then the Hadamard gate\n X(q);\n H(q);\n let result = Kata.AliceQuantum(true, q);\n Reset(q);\n if (result != true) {\n Message(\"Measuring |-⟩ in the X basis returned incorrect value; expected true\");\n return false;\n }\n }\n Message(\"Correct!\");\n true\n }\n}\n"
9794
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n import Std.Random.*;\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n use q = Qubit();\n for _ in 1..4 {\n // repeat 4 times since we are testing a measurement and wrong basis still might get\n // the correct answer, reduces probability of false positives\n let result = Kata.AliceQuantum(false, q);\n Reset(q);\n if (result != false) {\n Message(\"Measuring |0⟩ in the Z basis returned incorrect value; expected false\");\n return false;\n }\n\n // apply the Pauli X gate\n X(q);\n let result = Kata.AliceQuantum(false, q);\n Reset(q);\n if (result != true) {\n Message(\"Measuring |1⟩ in the Z basis returned incorrect value; expected true\");\n return false;\n }\n\n // apply the Hadamard gate\n H(q);\n let result = Kata.AliceQuantum(true, q);\n Reset(q);\n if (result != false) {\n Message(\"Measuring |+⟩ in the X basis returned incorrect value; expected false\");\n return false;\n }\n\n // apply the Pauli X and then the Hadamard gate\n X(q);\n H(q);\n let result = Kata.AliceQuantum(true, q);\n Reset(q);\n if (result != true) {\n Message(\"Measuring |-⟩ in the X basis returned incorrect value; expected true\");\n return false;\n }\n }\n Message(\"Correct!\");\n true\n }\n}\n"
9795
9795
  },
9796
9796
  {
9797
9797
  "id": "nonlocal_games__chsh_quantum_bob_strategy__Verification.qs",
9798
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Math;\n\n operation RotateBobQubit (clockwise : Bool, qubit : Qubit) : Unit {\n if (clockwise) {\n Ry(-PI()/4.0, qubit);\n } else {\n Ry(PI()/4.0, qubit);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for _ in 1 .. 4 {\n // repeat 4 times since we are testing a measurement and wrong basis still might get\n // the correct answer, reduces probability of false positives\n use q = Qubit();\n RotateBobQubit(false, q);\n let result = Kata.BobQuantum(false, q);\n Reset(q);\n if (result != false) {\n Message(\"π/8 from |0⟩ not measured as false\");\n return false;\n }\n\n X(q);\n RotateBobQubit(false, q);\n let result = Kata.BobQuantum(false, q);\n Reset(q);\n if (result != true) {\n Message(\"π/8 from |1⟩ not measured as true\");\n return false;\n }\n\n RotateBobQubit(true, q);\n let result = Kata.BobQuantum(true, q);\n Reset(q);\n if (result != false) {\n Message(\"-π/8 from |0⟩ not measured as false\");\n return false;\n }\n\n X(q);\n RotateBobQubit(true, q);\n let result = Kata.BobQuantum(true, q);\n Reset(q);\n if (result != true) {\n Message(\"-π/8 from |1⟩ not measured as true\");\n return false;\n }\n }\n Message(\"Correct!\");\n true\n } \n}\n"
9798
+ "code": "namespace Kata.Verification {\n import Std.Math.*;\n\n operation RotateBobQubit(clockwise : Bool, qubit : Qubit) : Unit {\n if (clockwise) {\n Ry(-PI() / 4.0, qubit);\n } else {\n Ry(PI() / 4.0, qubit);\n }\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n for _ in 1..4 {\n // repeat 4 times since we are testing a measurement and wrong basis still might get\n // the correct answer, reduces probability of false positives\n use q = Qubit();\n RotateBobQubit(false, q);\n let result = Kata.BobQuantum(false, q);\n Reset(q);\n if (result != false) {\n Message(\"π/8 from |0⟩ not measured as false\");\n return false;\n }\n\n X(q);\n RotateBobQubit(false, q);\n let result = Kata.BobQuantum(false, q);\n Reset(q);\n if (result != true) {\n Message(\"π/8 from |1⟩ not measured as true\");\n return false;\n }\n\n RotateBobQubit(true, q);\n let result = Kata.BobQuantum(true, q);\n Reset(q);\n if (result != false) {\n Message(\"-π/8 from |0⟩ not measured as false\");\n return false;\n }\n\n X(q);\n RotateBobQubit(true, q);\n let result = Kata.BobQuantum(true, q);\n Reset(q);\n if (result != true) {\n Message(\"-π/8 from |1⟩ not measured as true\");\n return false;\n }\n }\n Message(\"Correct!\");\n true\n }\n}\n"
9799
9799
  },
9800
9800
  {
9801
9801
  "id": "nonlocal_games__ghz_win_condition__Verification.qs",
9802
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Convert;\n\n function WinCondition_Reference (rst : Bool[], abc : Bool[]) : Bool {\n return (rst[0] or rst[1] or rst[2]) == (abc[0] != abc[1] != abc[2]);\n }\n\n // All possible starting bits (r, s and t) that the referee can give\n // to Alice, Bob and Charlie.\n function RefereeBits () : Bool[][] {\n return [[false, false, false],\n [true, true, false],\n [false, true, true],\n [true, false, true]];\n }\n\n @EntryPoint()\n function CheckSolution() : Bool {\n for rst in RefereeBits() {\n for i in 0 .. 1 <<< 3 - 1 {\n let abc = IntAsBoolArray(i, 3);\n let expected = WinCondition_Reference(rst, abc);\n let actual = Kata.WinCondition(rst, abc);\n\n if actual != expected {\n Message($\"Win condition '{actual}' is wrong for rst={rst}, abc={abc}\");\n return false;\n }\n }\n }\n Message(\"Correct!\");\n true\n }\n}\n"
9802
+ "code": "namespace Kata.Verification {\n import Std.Convert.*;\n\n function WinCondition_Reference(rst : Bool[], abc : Bool[]) : Bool {\n return (rst[0] or rst[1] or rst[2]) == (abc[0] != abc[1] != abc[2]);\n }\n\n // All possible starting bits (r, s and t) that the referee can give\n // to Alice, Bob and Charlie.\n function RefereeBits() : Bool[][] {\n return [\n [false, false, false],\n [true, true, false],\n [false, true, true],\n [true, false, true]\n ];\n }\n\n @EntryPoint()\n function CheckSolution() : Bool {\n for rst in RefereeBits() {\n for i in 0..1 <<< 3 - 1 {\n let abc = IntAsBoolArray(i, 3);\n let expected = WinCondition_Reference(rst, abc);\n let actual = Kata.WinCondition(rst, abc);\n\n if actual != expected {\n Message($\"Win condition '{actual}' is wrong for rst={rst}, abc={abc}\");\n return false;\n }\n }\n }\n Message(\"Correct!\");\n true\n }\n}\n"
9803
9803
  },
9804
9804
  {
9805
9805
  "id": "nonlocal_games__ghz_classical_strategy__Verification.qs",
9806
- "code": "namespace Kata.Verification {\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Logical; \n open Microsoft.Quantum.Random;\n\n function WinCondition_Reference (rst : Bool[], abc : Bool[]) : Bool {\n return (rst[0] or rst[1] or rst[2]) == (abc[0] != abc[1] != abc[2]);\n }\n\n // All possible starting bits (r, s and t) that the referee can give\n // to Alice, Bob and Charlie.\n function RefereeBits () : Bool[][] {\n return [[false, false, false],\n [true, true, false],\n [false, true, true],\n [true, false, true]];\n }\n\n operation PlayClassicalGHZ_Reference (strategies : (Bool => Bool)[], inputs : Bool[]) : Bool[] {\n let r = inputs[0];\n let s = inputs[1];\n let t = inputs[2];\n let a = strategies[0](r);\n let b = strategies[1](s);\n let c = strategies[2](t);\n return [a, b, c];\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let inputs = RefereeBits();\n let strategies = [Kata.AliceClassical, Kata.BobClassical, Kata.CharlieClassical];\n\n let iterations = 1000;\n mutable wins = 0;\n for _ in 0 .. iterations - 1 {\n for bits in inputs {\n let abc = PlayClassicalGHZ_Reference(strategies, bits);\n if WinCondition_Reference(bits, abc) {\n set wins = wins + 1;\n }\n }\n }\n // The solution is correct if the players win 75% (3/4) of the time.\n if wins < iterations*Length(inputs)*3/4 {\n Message($\"Alice, Bob, and Charlie's classical strategy gets {wins} wins out of {iterations*Length(inputs)} possible inputs, which is not optimal\");\n return false;\n }\n Message(\"Correct!\");\n true\n }\n}\n"
9806
+ "code": "namespace Kata.Verification {\n import Std.Arrays.*;\n import Std.Convert.*;\n import Std.Logical.*;\n import Std.Random.*;\n\n function WinCondition_Reference(rst : Bool[], abc : Bool[]) : Bool {\n return (rst[0] or rst[1] or rst[2]) == (abc[0] != abc[1] != abc[2]);\n }\n\n // All possible starting bits (r, s and t) that the referee can give\n // to Alice, Bob and Charlie.\n function RefereeBits() : Bool[][] {\n return [\n [false, false, false],\n [true, true, false],\n [false, true, true],\n [true, false, true]\n ];\n }\n\n operation PlayClassicalGHZ_Reference(strategies : (Bool => Bool)[], inputs : Bool[]) : Bool[] {\n let r = inputs[0];\n let s = inputs[1];\n let t = inputs[2];\n let a = strategies[0](r);\n let b = strategies[1](s);\n let c = strategies[2](t);\n return [a, b, c];\n }\n\n @EntryPoint()\n operation CheckSolution() : Bool {\n let inputs = RefereeBits();\n let strategies = [Kata.AliceClassical, Kata.BobClassical, Kata.CharlieClassical];\n\n let iterations = 1000;\n mutable wins = 0;\n for _ in 0..iterations - 1 {\n for bits in inputs {\n let abc = PlayClassicalGHZ_Reference(strategies, bits);\n if WinCondition_Reference(bits, abc) {\n set wins = wins + 1;\n }\n }\n }\n // The solution is correct if the players win 75% (3/4) of the time.\n if wins < iterations * Length(inputs) * 3 / 4 {\n Message($\"Alice, Bob, and Charlie's classical strategy gets {wins} wins out of {iterations * Length(inputs)} possible inputs, which is not optimal\");\n return false;\n }\n Message(\"Correct!\");\n true\n }\n}\n"
9807
9807
  },
9808
9808
  {
9809
9809
  "id": "nonlocal_games__ghz_classical_game__Verification.qs",