From 7500775b57e31a912eace5a04c7063803a74024d Mon Sep 17 00:00:00 2001 From: devikamehra Date: Sat, 24 Aug 2024 22:40:34 -0700 Subject: [PATCH 1/2] Added DoubleAsStringWithPrecision function --- katas/content/linear_algebra/Common.qs | 27 ++++++++++--------- .../linear_algebra/addition/Verification.qs | 4 ++- .../linear_algebra/adjoint/Verification.qs | 4 ++- .../linear_algebra/conjugate/Verification.qs | 4 ++- .../inner_product/Verification.qs | 4 ++- .../linear_algebra/inverse/Verification.qs | 4 ++- .../multiplication/Verification.qs | 4 ++- .../normalized_vector/Verification.qs | 4 ++- .../outer_product/Verification.qs | 4 ++- .../scalar_multiplication/Verification.qs | 4 ++- .../tensor_product/Verification.qs | 4 ++- .../linear_algebra/transpose/Verification.qs | 4 ++- 12 files changed, 47 insertions(+), 24 deletions(-) diff --git a/katas/content/linear_algebra/Common.qs b/katas/content/linear_algebra/Common.qs index 88a5054f4e..f99f5eb28a 100644 --- a/katas/content/linear_algebra/Common.qs +++ b/katas/content/linear_algebra/Common.qs @@ -1,7 +1,16 @@ namespace Kata.Verification { open Microsoft.Quantum.Math; + open Microsoft.Quantum.Convert; - function ArraysEqualD(actual : Double[][], expected : Double[][]) : Bool { + function ComplexAsString(x : Complex, precision: Int) : String { + if x::Imag < 0.0 { + $"{DoubleAsStringWithPrecision(x::Real,precision)} - {DoubleAsStringWithPrecision(AbsD(x::Imag),precision)}i" + } else { + $"{DoubleAsStringWithPrecision(x::Real,precision)} + {DoubleAsStringWithPrecision(x::Imag,precision)}i" + } + } + + function ArraysEqualD(actual : Double[][], expected : Double[][], precision: Int) : Bool { if Length(actual) != Length(expected) { Message("Incorrect"); Message($"Expected number of rows {Length(expected)}, actual {Length(actual)}"); @@ -14,10 +23,10 @@ namespace Kata.Verification { return false; } - for j in 0 .. Length(actual[i]) - 1 { + for j in 0 .. Length(actual[i]) - 1 { if AbsD(actual[i][j] - expected[i][j]) > 1e-9 { Message("Incorrect"); - Message($"For element in row {i}, column {j}, expected {expected[i][j]}, actual {actual[i][j]}"); + Message($"For element in row {i}, column {j}, expected {DoubleAsStringWithPrecision(expected[i][j],precision)}, actual {DoubleAsStringWithPrecision(actual[i][j],precision)}"); return false; } } @@ -27,15 +36,7 @@ namespace Kata.Verification { return true; } - function ComplexAsString(x : Complex) : String { - if x::Imag < 0.0 { - $"{x::Real} - {AbsD(x::Imag)}i" - } else { - $"{x::Real} + {x::Imag}i" - } - } - - function ArraysEqualC(actual : Complex[][], expected : Complex[][]) : Bool { + function ArraysEqualC(actual : Complex[][], expected : Complex[][], precision: Int) : Bool { if Length(actual) != Length(expected) { Message("Incorrect"); Message($"Expected number of rows {Length(expected)}, actual {Length(actual)}"); @@ -51,7 +52,7 @@ namespace Kata.Verification { for j in 0 .. Length(actual[i]) - 1 { if AbsComplex(MinusC(actual[i][j], expected[i][j])) > 1e-9 { Message("Incorrect"); - Message($"For element in row {i}, column {j}, expected {ComplexAsString(expected[i][j])}, actual {ComplexAsString(actual[i][j])}"); + Message($"For element in row {i}, column {j}, expected {ComplexAsString(expected[i][j],precision)}, actual {ComplexAsString(actual[i][j],precision)}"); return false; } } diff --git a/katas/content/linear_algebra/addition/Verification.qs b/katas/content/linear_algebra/addition/Verification.qs index 2139cd7421..eca981dade 100644 --- a/katas/content/linear_algebra/addition/Verification.qs +++ b/katas/content/linear_algebra/addition/Verification.qs @@ -6,6 +6,8 @@ namespace Kata.Verification { @EntryPoint() operation CheckSolution() : Bool { - ArraysEqualD(Kata.Addition(), Addition_Reference()) + // In case of an error, this value defines the precision with which complex numbers should be displayed + let precision = 2; + ArraysEqualD(Kata.Addition(), Addition_Reference(), precision) } } diff --git a/katas/content/linear_algebra/adjoint/Verification.qs b/katas/content/linear_algebra/adjoint/Verification.qs index c55ef5a1f8..2e08f53ebe 100644 --- a/katas/content/linear_algebra/adjoint/Verification.qs +++ b/katas/content/linear_algebra/adjoint/Verification.qs @@ -8,6 +8,8 @@ namespace Kata.Verification { @EntryPoint() operation CheckSolution() : Bool { - ArraysEqualC(Kata.MatrixAdjoint(), MatrixAdjoint_Reference()) + // In case of an error, this value defines the precision with which complex numbers should be displayed + let precision = 2; + ArraysEqualC(Kata.MatrixAdjoint(), MatrixAdjoint_Reference(), precision) } } diff --git a/katas/content/linear_algebra/conjugate/Verification.qs b/katas/content/linear_algebra/conjugate/Verification.qs index 37293cc1c7..cd385140d3 100644 --- a/katas/content/linear_algebra/conjugate/Verification.qs +++ b/katas/content/linear_algebra/conjugate/Verification.qs @@ -8,6 +8,8 @@ namespace Kata.Verification { @EntryPoint() operation CheckSolution() : Bool { - ArraysEqualC(Kata.Conjugate(), Conjugate_Reference()) + // In case of an error, this value defines the precision with which complex numbers should be displayed + let precision = 2; + ArraysEqualC(Kata.Conjugate(), Conjugate_Reference(), precision) } } diff --git a/katas/content/linear_algebra/inner_product/Verification.qs b/katas/content/linear_algebra/inner_product/Verification.qs index 3c026df72e..73017ce706 100644 --- a/katas/content/linear_algebra/inner_product/Verification.qs +++ b/katas/content/linear_algebra/inner_product/Verification.qs @@ -10,8 +10,10 @@ namespace Kata.Verification { let actual = Kata.InnerProduct(); let expected = InnerProduct_Reference(); if AbsComplex(MinusC(actual, expected)) > 1e-9 { + // In case of an error, this value defines the precision with which complex numbers should be displayed + let precision = 2; Message("Incorrect"); - Message($"Expected {ComplexAsString(expected)}, actual {ComplexAsString(actual)}"); + Message($"Expected {ComplexAsString(expected, precision)}, actual {ComplexAsString(actual, precision)}"); return false; } diff --git a/katas/content/linear_algebra/inverse/Verification.qs b/katas/content/linear_algebra/inverse/Verification.qs index 9d194fc08f..48b095fbe4 100644 --- a/katas/content/linear_algebra/inverse/Verification.qs +++ b/katas/content/linear_algebra/inverse/Verification.qs @@ -6,6 +6,8 @@ namespace Kata.Verification { @EntryPoint() operation CheckSolution() : Bool { - ArraysEqualD(Kata.Inverse(), Inverse_Reference()) + // In case of an error, this value defines the precision with which complex numbers should be displayed + let precision = 2; + ArraysEqualD(Kata.Inverse(), Inverse_Reference(), precision) } } diff --git a/katas/content/linear_algebra/multiplication/Verification.qs b/katas/content/linear_algebra/multiplication/Verification.qs index 874def4766..3a87e8f089 100644 --- a/katas/content/linear_algebra/multiplication/Verification.qs +++ b/katas/content/linear_algebra/multiplication/Verification.qs @@ -6,6 +6,8 @@ namespace Kata.Verification { @EntryPoint() operation CheckSolution() : Bool { - ArraysEqualD(Kata.Multiplication(), Multiplication_Reference()) + // In case of an error, this value defines the precision with which complex numbers should be displayed + let precision = 2; + ArraysEqualD(Kata.Multiplication(), Multiplication_Reference(), precision) } } diff --git a/katas/content/linear_algebra/normalized_vector/Verification.qs b/katas/content/linear_algebra/normalized_vector/Verification.qs index 2a24194606..01f3319cc8 100644 --- a/katas/content/linear_algebra/normalized_vector/Verification.qs +++ b/katas/content/linear_algebra/normalized_vector/Verification.qs @@ -8,6 +8,8 @@ namespace Kata.Verification { @EntryPoint() operation CheckSolution() : Bool { - ArraysEqualC(Kata.NormalizedVector(), NormalizedVector_Reference()) + // In case of an error, this value defines the precision with which complex numbers should be displayed + let precision = 2; + ArraysEqualC(Kata.NormalizedVector(), NormalizedVector_Reference(), precision) } } diff --git a/katas/content/linear_algebra/outer_product/Verification.qs b/katas/content/linear_algebra/outer_product/Verification.qs index 94ccb119e9..af804a1107 100644 --- a/katas/content/linear_algebra/outer_product/Verification.qs +++ b/katas/content/linear_algebra/outer_product/Verification.qs @@ -8,6 +8,8 @@ namespace Kata.Verification { @EntryPoint() operation CheckSolution() : Bool { - ArraysEqualC(Kata.OuterProduct(), OuterProduct_Reference()) + // In case of an error, this value defines the precision with which complex numbers should be displayed + let precision = 2; + ArraysEqualC(Kata.OuterProduct(), OuterProduct_Reference(), precision) } } diff --git a/katas/content/linear_algebra/scalar_multiplication/Verification.qs b/katas/content/linear_algebra/scalar_multiplication/Verification.qs index 1b93ab1492..e2be91ba34 100644 --- a/katas/content/linear_algebra/scalar_multiplication/Verification.qs +++ b/katas/content/linear_algebra/scalar_multiplication/Verification.qs @@ -6,6 +6,8 @@ namespace Kata.Verification { @EntryPoint() operation CheckSolution() : Bool { - ArraysEqualD(Kata.ScalarMultiplication(), ScalarMultiplication_Reference()) + // In case of an error, this value defines the precision with which complex numbers should be displayed + let precision = 2; + ArraysEqualD(Kata.ScalarMultiplication(), ScalarMultiplication_Reference(), precision) } } diff --git a/katas/content/linear_algebra/tensor_product/Verification.qs b/katas/content/linear_algebra/tensor_product/Verification.qs index ce302756a2..46be8d16b3 100644 --- a/katas/content/linear_algebra/tensor_product/Verification.qs +++ b/katas/content/linear_algebra/tensor_product/Verification.qs @@ -10,6 +10,8 @@ namespace Kata.Verification { @EntryPoint() operation CheckSolution() : Bool { - ArraysEqualD(Kata.TensorProduct(), TensorProduct_Reference()) + // In case of an error, this value defines the precision with which complex numbers should be displayed + let precision = 2; + ArraysEqualD(Kata.TensorProduct(), TensorProduct_Reference(), precision) } } diff --git a/katas/content/linear_algebra/transpose/Verification.qs b/katas/content/linear_algebra/transpose/Verification.qs index ea4699af08..65eaa58ef4 100644 --- a/katas/content/linear_algebra/transpose/Verification.qs +++ b/katas/content/linear_algebra/transpose/Verification.qs @@ -6,6 +6,8 @@ namespace Kata.Verification { @EntryPoint() operation CheckSolution() : Bool { - ArraysEqualD(Kata.Transpose(), Transpose_Reference()) + // In case of an error, this value defines the precision with which complex numbers should be displayed + let precision = 2; + ArraysEqualD(Kata.Transpose(), Transpose_Reference(), precision) } } From 64d6816e43f2bca2e45aecfb271948e1b2c5ab52 Mon Sep 17 00:00:00 2001 From: devikamehra Date: Mon, 26 Aug 2024 20:11:20 -0700 Subject: [PATCH 2/2] Moved parameter precision to common.qs --- katas/content/linear_algebra/Common.qs | 11 +++++++---- katas/content/linear_algebra/addition/Verification.qs | 4 +--- katas/content/linear_algebra/adjoint/Verification.qs | 4 +--- .../content/linear_algebra/conjugate/Verification.qs | 4 +--- .../linear_algebra/inner_product/Verification.qs | 4 +--- katas/content/linear_algebra/inverse/Verification.qs | 4 +--- .../linear_algebra/multiplication/Verification.qs | 4 +--- .../linear_algebra/normalized_vector/Verification.qs | 4 +--- .../linear_algebra/outer_product/Verification.qs | 4 +--- .../scalar_multiplication/Verification.qs | 4 +--- .../linear_algebra/tensor_product/Verification.qs | 4 +--- .../content/linear_algebra/transpose/Verification.qs | 4 +--- 12 files changed, 18 insertions(+), 37 deletions(-) diff --git a/katas/content/linear_algebra/Common.qs b/katas/content/linear_algebra/Common.qs index f99f5eb28a..38e938bb40 100644 --- a/katas/content/linear_algebra/Common.qs +++ b/katas/content/linear_algebra/Common.qs @@ -2,7 +2,8 @@ namespace Kata.Verification { open Microsoft.Quantum.Math; open Microsoft.Quantum.Convert; - function ComplexAsString(x : Complex, precision: Int) : String { + function ComplexAsString(x : Complex) : String { + let precision = 3; if x::Imag < 0.0 { $"{DoubleAsStringWithPrecision(x::Real,precision)} - {DoubleAsStringWithPrecision(AbsD(x::Imag),precision)}i" } else { @@ -10,7 +11,8 @@ namespace Kata.Verification { } } - function ArraysEqualD(actual : Double[][], expected : Double[][], precision: Int) : Bool { + function ArraysEqualD(actual : Double[][], expected : Double[][]) : Bool { + let precision = 3; if Length(actual) != Length(expected) { Message("Incorrect"); Message($"Expected number of rows {Length(expected)}, actual {Length(actual)}"); @@ -36,7 +38,8 @@ namespace Kata.Verification { return true; } - function ArraysEqualC(actual : Complex[][], expected : Complex[][], precision: Int) : Bool { + function ArraysEqualC(actual : Complex[][], expected : Complex[][]) : Bool { + let precision = 3; if Length(actual) != Length(expected) { Message("Incorrect"); Message($"Expected number of rows {Length(expected)}, actual {Length(actual)}"); @@ -52,7 +55,7 @@ namespace Kata.Verification { for j in 0 .. Length(actual[i]) - 1 { if AbsComplex(MinusC(actual[i][j], expected[i][j])) > 1e-9 { Message("Incorrect"); - Message($"For element in row {i}, column {j}, expected {ComplexAsString(expected[i][j],precision)}, actual {ComplexAsString(actual[i][j],precision)}"); + Message($"For element in row {i}, column {j}, expected {ComplexAsString(expected[i][j])}, actual {ComplexAsString(actual[i][j])}"); return false; } } diff --git a/katas/content/linear_algebra/addition/Verification.qs b/katas/content/linear_algebra/addition/Verification.qs index eca981dade..2139cd7421 100644 --- a/katas/content/linear_algebra/addition/Verification.qs +++ b/katas/content/linear_algebra/addition/Verification.qs @@ -6,8 +6,6 @@ namespace Kata.Verification { @EntryPoint() operation CheckSolution() : Bool { - // In case of an error, this value defines the precision with which complex numbers should be displayed - let precision = 2; - ArraysEqualD(Kata.Addition(), Addition_Reference(), precision) + ArraysEqualD(Kata.Addition(), Addition_Reference()) } } diff --git a/katas/content/linear_algebra/adjoint/Verification.qs b/katas/content/linear_algebra/adjoint/Verification.qs index 2e08f53ebe..c55ef5a1f8 100644 --- a/katas/content/linear_algebra/adjoint/Verification.qs +++ b/katas/content/linear_algebra/adjoint/Verification.qs @@ -8,8 +8,6 @@ namespace Kata.Verification { @EntryPoint() operation CheckSolution() : Bool { - // In case of an error, this value defines the precision with which complex numbers should be displayed - let precision = 2; - ArraysEqualC(Kata.MatrixAdjoint(), MatrixAdjoint_Reference(), precision) + ArraysEqualC(Kata.MatrixAdjoint(), MatrixAdjoint_Reference()) } } diff --git a/katas/content/linear_algebra/conjugate/Verification.qs b/katas/content/linear_algebra/conjugate/Verification.qs index cd385140d3..37293cc1c7 100644 --- a/katas/content/linear_algebra/conjugate/Verification.qs +++ b/katas/content/linear_algebra/conjugate/Verification.qs @@ -8,8 +8,6 @@ namespace Kata.Verification { @EntryPoint() operation CheckSolution() : Bool { - // In case of an error, this value defines the precision with which complex numbers should be displayed - let precision = 2; - ArraysEqualC(Kata.Conjugate(), Conjugate_Reference(), precision) + ArraysEqualC(Kata.Conjugate(), Conjugate_Reference()) } } diff --git a/katas/content/linear_algebra/inner_product/Verification.qs b/katas/content/linear_algebra/inner_product/Verification.qs index 73017ce706..3c026df72e 100644 --- a/katas/content/linear_algebra/inner_product/Verification.qs +++ b/katas/content/linear_algebra/inner_product/Verification.qs @@ -10,10 +10,8 @@ namespace Kata.Verification { let actual = Kata.InnerProduct(); let expected = InnerProduct_Reference(); if AbsComplex(MinusC(actual, expected)) > 1e-9 { - // In case of an error, this value defines the precision with which complex numbers should be displayed - let precision = 2; Message("Incorrect"); - Message($"Expected {ComplexAsString(expected, precision)}, actual {ComplexAsString(actual, precision)}"); + Message($"Expected {ComplexAsString(expected)}, actual {ComplexAsString(actual)}"); return false; } diff --git a/katas/content/linear_algebra/inverse/Verification.qs b/katas/content/linear_algebra/inverse/Verification.qs index 48b095fbe4..9d194fc08f 100644 --- a/katas/content/linear_algebra/inverse/Verification.qs +++ b/katas/content/linear_algebra/inverse/Verification.qs @@ -6,8 +6,6 @@ namespace Kata.Verification { @EntryPoint() operation CheckSolution() : Bool { - // In case of an error, this value defines the precision with which complex numbers should be displayed - let precision = 2; - ArraysEqualD(Kata.Inverse(), Inverse_Reference(), precision) + ArraysEqualD(Kata.Inverse(), Inverse_Reference()) } } diff --git a/katas/content/linear_algebra/multiplication/Verification.qs b/katas/content/linear_algebra/multiplication/Verification.qs index 3a87e8f089..874def4766 100644 --- a/katas/content/linear_algebra/multiplication/Verification.qs +++ b/katas/content/linear_algebra/multiplication/Verification.qs @@ -6,8 +6,6 @@ namespace Kata.Verification { @EntryPoint() operation CheckSolution() : Bool { - // In case of an error, this value defines the precision with which complex numbers should be displayed - let precision = 2; - ArraysEqualD(Kata.Multiplication(), Multiplication_Reference(), precision) + ArraysEqualD(Kata.Multiplication(), Multiplication_Reference()) } } diff --git a/katas/content/linear_algebra/normalized_vector/Verification.qs b/katas/content/linear_algebra/normalized_vector/Verification.qs index 01f3319cc8..2a24194606 100644 --- a/katas/content/linear_algebra/normalized_vector/Verification.qs +++ b/katas/content/linear_algebra/normalized_vector/Verification.qs @@ -8,8 +8,6 @@ namespace Kata.Verification { @EntryPoint() operation CheckSolution() : Bool { - // In case of an error, this value defines the precision with which complex numbers should be displayed - let precision = 2; - ArraysEqualC(Kata.NormalizedVector(), NormalizedVector_Reference(), precision) + ArraysEqualC(Kata.NormalizedVector(), NormalizedVector_Reference()) } } diff --git a/katas/content/linear_algebra/outer_product/Verification.qs b/katas/content/linear_algebra/outer_product/Verification.qs index af804a1107..94ccb119e9 100644 --- a/katas/content/linear_algebra/outer_product/Verification.qs +++ b/katas/content/linear_algebra/outer_product/Verification.qs @@ -8,8 +8,6 @@ namespace Kata.Verification { @EntryPoint() operation CheckSolution() : Bool { - // In case of an error, this value defines the precision with which complex numbers should be displayed - let precision = 2; - ArraysEqualC(Kata.OuterProduct(), OuterProduct_Reference(), precision) + ArraysEqualC(Kata.OuterProduct(), OuterProduct_Reference()) } } diff --git a/katas/content/linear_algebra/scalar_multiplication/Verification.qs b/katas/content/linear_algebra/scalar_multiplication/Verification.qs index e2be91ba34..1b93ab1492 100644 --- a/katas/content/linear_algebra/scalar_multiplication/Verification.qs +++ b/katas/content/linear_algebra/scalar_multiplication/Verification.qs @@ -6,8 +6,6 @@ namespace Kata.Verification { @EntryPoint() operation CheckSolution() : Bool { - // In case of an error, this value defines the precision with which complex numbers should be displayed - let precision = 2; - ArraysEqualD(Kata.ScalarMultiplication(), ScalarMultiplication_Reference(), precision) + ArraysEqualD(Kata.ScalarMultiplication(), ScalarMultiplication_Reference()) } } diff --git a/katas/content/linear_algebra/tensor_product/Verification.qs b/katas/content/linear_algebra/tensor_product/Verification.qs index 46be8d16b3..ce302756a2 100644 --- a/katas/content/linear_algebra/tensor_product/Verification.qs +++ b/katas/content/linear_algebra/tensor_product/Verification.qs @@ -10,8 +10,6 @@ namespace Kata.Verification { @EntryPoint() operation CheckSolution() : Bool { - // In case of an error, this value defines the precision with which complex numbers should be displayed - let precision = 2; - ArraysEqualD(Kata.TensorProduct(), TensorProduct_Reference(), precision) + ArraysEqualD(Kata.TensorProduct(), TensorProduct_Reference()) } } diff --git a/katas/content/linear_algebra/transpose/Verification.qs b/katas/content/linear_algebra/transpose/Verification.qs index 65eaa58ef4..ea4699af08 100644 --- a/katas/content/linear_algebra/transpose/Verification.qs +++ b/katas/content/linear_algebra/transpose/Verification.qs @@ -6,8 +6,6 @@ namespace Kata.Verification { @EntryPoint() operation CheckSolution() : Bool { - // In case of an error, this value defines the precision with which complex numbers should be displayed - let precision = 2; - ArraysEqualD(Kata.Transpose(), Transpose_Reference(), precision) + ArraysEqualD(Kata.Transpose(), Transpose_Reference()) } }