microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
katas/content/complex_arithmetic/complex_powers_real/Verification.qs
33lines · modecode
| 1 | namespace Kata.Verification { |
| 2 | open Microsoft.Quantum.Math; |
| 3 | open Microsoft.Quantum.Random; |
| 4 | |
| 5 | function ComplexExpReal_Reference(r : Double, x : Complex) : Complex { |
| 6 | if AbsD(r) < 1e-9 { |
| 7 | return Complex(0.0, 0.0); |
| 8 | } |
| 9 | let real = r ^ x::Real * Cos(x::Imag * Log(r)); |
| 10 | let imaginary = r ^ x::Real * Sin(x::Imag * Log(r)); |
| 11 | return Complex(real, imaginary); |
| 12 | } |
| 13 | |
| 14 | @EntryPoint() |
| 15 | operation CheckSolution() : Bool { |
| 16 | for ind in 0 .. 24 { |
| 17 | let x = DrawRandomComplex(); |
| 18 | let r = ind == 0 ? 0.0 | DrawRandomDouble(0., 10.); |
| 19 | |
| 20 | let expected = ComplexExpReal_Reference(r, x); |
| 21 | let actual = Kata.ComplexExpReal(r, x); |
| 22 | |
| 23 | if not ComplexEqual(expected, actual) { |
| 24 | Message("Incorrect"); |
| 25 | Message($"For x = {ComplexAsString(x)} and r = {r} expected return {ComplexAsString(expected)}, actual return {ComplexAsString(actual)}."); |
| 26 | return false; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | Message("Correct!"); |
| 31 | return true; |
| 32 | } |
| 33 | } |
| 34 | |