Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added DoubleAsStringWithPrecision function - Complex Arithmetics #1883

Merged
merged 6 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions katas/content/complex_arithmetic/Common.qs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ namespace Kata.Verification {
return Complex(real, imag);
}

function ComplexAsString(x : Complex) : String {
function ComplexAsString(x : Complex, precision : Int) : String {
if x::Imag < 0.0 {
$"{x::Real} - {AbsD(x::Imag)}i"
$"{DoubleAsStringWithPrecision(x::Real,precision)} - {DoubleAsStringWithPrecision(AbsD(x::Imag),precision)}i"
} else {
$"{x::Real} + {x::Imag}i"
$"{DoubleAsStringWithPrecision(x::Real,precision)} + {DoubleAsStringWithPrecision(x::Imag,precision)}i"
}
}

function ComplexPolarAsString(x : ComplexPolar) : String {
$"{x::Magnitude} * exp({x::Argument}i)"
function ComplexPolarAsString(x : ComplexPolar, precision: Int) : String {
$"{DoubleAsStringWithPrecision(x::Magnitude,precision)} * exp({DoubleAsStringWithPrecision(x::Argument,precision)}i)"
}

operation CheckTwoComplexOpsAreSame(sol : (Complex, Complex) -> Complex, ref : (Complex, Complex) -> Complex) : Bool {
operation CheckTwoComplexOpsAreSame(sol : (Complex, Complex) -> Complex, ref : (Complex, Complex) -> Complex, precision: Int) : Bool {
for _ in 0 .. 24 {
let x = DrawRandomComplex();
let y = DrawRandomComplex();
Expand All @@ -32,7 +32,7 @@ namespace Kata.Verification {

if not ComplexEqual(expected, actual) {
Message("Incorrect");
Message($"For x = {ComplexAsString(x)}, y = {ComplexAsString(y)} expected return {ComplexAsString(expected)}, actual return {ComplexAsString(actual)}.");
Message($"For x = {ComplexAsString(x,precision)}, y = {ComplexAsString(y,precision)} expected return {ComplexAsString(expected,precision)}, actual return {ComplexAsString(actual,precision)}.");
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ namespace Kata.Verification {
let expected = ComplexAsComplexPolar(x);
let actual = Kata.ComplexToComplexPolar(x);

if not ComplexPolarEqual(expected, actual) {
if not ComplexPolarEqual(expected, actual) {
// In case of an error, this value defines the precision with which complex numbers should be displayed
let precision = 6;
Message("Incorrect");
Message($"For x = {ComplexAsString(x)} expected return {ComplexPolarAsString(expected)}, actual return {ComplexPolarAsString(actual)}.");
Message($"For x = {ComplexAsString(x,precision)} expected return {ComplexPolarAsString(expected,precision)}, actual return {ComplexPolarAsString(actual,precision)}.");
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ namespace Kata.Verification {
open Microsoft.Quantum.Math;

@EntryPoint()
operation CheckSolution() : Bool {
CheckTwoComplexOpsAreSame(Kata.ComplexAdd, PlusC)
operation CheckSolution() : Bool {
// In case of an error, this value defines the precision with which complex numbers should be displayed
let precision = 6;
CheckTwoComplexOpsAreSame(Kata.ComplexAdd, PlusC, precision)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ namespace Kata.Verification {
let expected = ComplexConjugate_Reference(x);
let actual = Kata.ComplexConjugate(x);

if not ComplexEqual(expected, actual) {
if not ComplexEqual(expected, actual) {
// In case of an error, this value defines the precision with which complex numbers should be displayed
let precision = 6;
Message("Incorrect");
Message($"For x = {ComplexAsString(x)} expected return {ComplexAsString(expected)}, actual return {ComplexAsString(actual)}.");
Message($"For x = {ComplexAsString(x,precision)} expected return {ComplexAsString(expected,precision)}, actual return {ComplexAsString(actual,precision)}.");
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ namespace Kata.Verification {
open Microsoft.Quantum.Math;

@EntryPoint()
operation CheckSolution() : Bool {
CheckTwoComplexOpsAreSame(Kata.ComplexDiv, DividedByC)
operation CheckSolution() : Bool {
// In case of an error, this value defines the precision with which complex numbers should be displayed
let precision = 6;
CheckTwoComplexOpsAreSame(Kata.ComplexDiv, DividedByC, precision)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ namespace Kata.Verification {
let expected = ComplexExponent_Reference(x);
let actual = Kata.ComplexExponent(x);

if not ComplexEqual(expected, actual) {
if not ComplexEqual(expected, actual) {
// In case of an error, this value defines the precision with which complex numbers should be displayed
let precision = 6;
Message("Incorrect");
Message($"For x = {ComplexAsString(x)} expected return {ComplexAsString(expected)}, actual return {ComplexAsString(actual)}.");
Message($"For x = {ComplexAsString(x,precision)} expected return {ComplexAsString(expected,precision)}, actual return {ComplexAsString(actual,precision)}.");
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace Kata.Verification {
import Microsoft.Quantum.Convert.DoubleAsStringWithPrecision;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Convert;

@EntryPoint()
operation CheckSolution() : Bool {
Expand All @@ -9,9 +11,11 @@ namespace Kata.Verification {
let expected = AbsComplex(x);
let actual = Kata.ComplexModulus(x);

if AbsD(expected - actual) > 1e-6 {
if AbsD(expected - actual) > 1e-6 {
// In case of an error, this value defines the precision with which complex numbers should be displayed
let precision = 6;
Message("Incorrect");
Message($"For x = {ComplexAsString(x)} expected return {expected}, actual return {actual}.");
Message($"For x = {ComplexAsString(x,precision)} expected return {DoubleAsStringWithPrecision(expected,6)}, actual return {DoubleAsStringWithPrecision(actual,6)}.");
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ namespace Kata.Verification {
open Microsoft.Quantum.Math;

@EntryPoint()
operation CheckSolution() : Bool {
CheckTwoComplexOpsAreSame(Kata.ComplexMult, TimesC)
operation CheckSolution() : Bool {
// In case of an error, this value defines the precision with which complex numbers should be displayed
let precision = 6;
CheckTwoComplexOpsAreSame(Kata.ComplexMult, TimesC, precision)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Kata.Verification {
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Random;
open Microsoft.Quantum.Random;
open Microsoft.Quantum.Convert;

function ComplexExpReal_Reference(r : Double, x : Complex) : Complex {
if AbsD(r) < 1e-9 {
Expand All @@ -15,14 +16,16 @@ namespace Kata.Verification {
operation CheckSolution() : Bool {
for ind in 0 .. 24 {
let x = DrawRandomComplex();
let r = ind == 0 ? 0.0 | DrawRandomDouble(0., 10.);
let r = ind == 0 ? 0.0 | DrawRandomDouble(0., 10.);

let expected = ComplexExpReal_Reference(r, x);
let actual = Kata.ComplexExpReal(r, x);

if not ComplexEqual(expected, actual) {
if not ComplexEqual(expected, actual) {
// In case of an error, this value defines the precision with which complex numbers should be displayed
let precision = 6;
Message("Incorrect");
Message($"For x = {ComplexAsString(x)} and r = {r} expected return {ComplexAsString(expected)}, actual return {ComplexAsString(actual)}.");
Message($"For x = {ComplexAsString(x, precision)} and r = {DoubleAsStringWithPrecision(r,6)} expected return {ComplexAsString(expected, precision)}, actual return {ComplexAsString(actual, precision)}.");
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ namespace Kata.Verification {
let expected = ComplexAsComplexPolar(TimesC(x, y));
let actual = Kata.ComplexPolarMult(xp, yp);

if not ComplexPolarEqual(expected, actual) {
if not ComplexPolarEqual(expected, actual) {
// In case of an error, this value defines the precision with which complex numbers should be displayed
let precision = 6;
Message("Incorrect");
Message($"For x = {ComplexPolarAsString(xp)}, y = {ComplexPolarAsString(yp)} " +
$"expected return {ComplexPolarAsString(expected)}, actual return {ComplexPolarAsString(actual)}.");
Message($"For x = {ComplexPolarAsString(xp, precision)}, y = {ComplexPolarAsString(yp, precision)} " +
$"expected return {ComplexPolarAsString(expected, precision)}, actual return {ComplexPolarAsString(actual, precision)}.");
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ namespace Kata.Verification {
let expected = ComplexPolarAsComplex(x);
let actual = Kata.ComplexPolarToComplex(x);

if not ComplexEqual(expected, actual) {
if not ComplexEqual(expected, actual) {
// In case of an error, this value defines the precision with which complex numbers should be displayed
let precision = 6;
Message("Incorrect");
Message($"For x = {ComplexPolarAsString(x)} expected return {ComplexAsString(expected)}, actual return {ComplexAsString(actual)}.");
Message($"For x = {ComplexPolarAsString(x, precision)} expected return {ComplexAsString(expected, precision)}, actual return {ComplexAsString(actual, precision)}.");
return false;
}
}
Expand Down