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

Update samples to reflect latest 1.7 changes; Update katas and stdlib to use structs #1797

Merged
merged 35 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
95eec8c
Update samples
ScottCarda-MS Jul 9, 2024
7e38397
katas
ScottCarda-MS Jul 9, 2024
71c77a8
Merge branch 'main' into sccarda/UpdateInternalQs
ScottCarda-MS Jul 9, 2024
20be2b7
libraries
ScottCarda-MS Jul 9, 2024
327255b
Merge branch 'main' into sccarda/UpdateInternalQs
ScottCarda-MS Jul 9, 2024
06c7cd2
fix formatting issue
ScottCarda-MS Jul 10, 2024
ccf7dbf
fixed mistype
ScottCarda-MS Jul 10, 2024
8195c6b
missed a
ScottCarda-MS Jul 10, 2024
36fc6a5
update language samples
sezna Jul 16, 2024
6ee235d
Merge branch 'main' into sccarda/UpdateInternalQs
ScottCarda-MS Jul 17, 2024
099d14f
merge from main
sezna Jul 26, 2024
eedb073
merge in scott's changes
sezna Jul 26, 2024
a57ccf8
Check lints on samples tests
swernli Jul 26, 2024
09d5bed
merge from main
sezna Aug 15, 2024
1fba1b9
format samples
sezna Aug 15, 2024
bad48cb
wip
sezna Aug 15, 2024
fbcc5a8
wip
sezna Aug 15, 2024
ee28c9e
wip
sezna Aug 15, 2024
05a05b7
format
sezna Aug 15, 2024
82e7271
fmt
sezna Aug 15, 2024
007afc6
Remove updated struct syntax in library and sample code
sezna Aug 16, 2024
7e07c61
revert change in katas
sezna Aug 16, 2024
4125de7
update katas example
sezna Aug 16, 2024
f37e2d3
merge
sezna Aug 19, 2024
373432b
Update samples/language/EntryPoint.qs
sezna Aug 20, 2024
84cf6d7
switch some operations to functions
sezna Aug 20, 2024
1e93e34
fix variables sample
sezna Aug 20, 2024
6b32aef
check for needless operation lint in samples
sezna Aug 20, 2024
ffb7539
Update samples/language/Variables.qs
sezna Aug 20, 2024
85d603d
Update samples/language/Unit.qs
sezna Aug 20, 2024
bf235e7
Update katas/content/complex_arithmetic/complex_addition/Placeholder.qs
sezna Aug 20, 2024
5761b2a
add copy and update example for structs
sezna Aug 22, 2024
8d0f18b
Merge branch 'alex/update-samples' of github.com:microsoft/qsharp int…
sezna Aug 22, 2024
3a5c53f
Merge branch 'main' of github.com:microsoft/qsharp into alex/update-s…
sezna Aug 26, 2024
d357ae9
feedback to remove lint that will go away soon
sezna Aug 26, 2024
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: 14 additions & 0 deletions compiler/qsc/src/interpret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub use qsc_eval::{
val::Value,
StepAction, StepResult,
};
use qsc_linter::Lint;
use qsc_lowerer::{map_fir_package_to_hir, map_hir_package_to_fir};
use qsc_partial_eval::ProgramEntry;
use qsc_rca::PackageStoreComputeProperties;
Expand Down Expand Up @@ -294,6 +295,19 @@ impl Interpreter {
pub fn set_classical_seed(&mut self, seed: Option<u64>) {
self.classical_seed = seed;
}

pub fn check_source_lints(&self) -> Vec<Lint> {
if let Some(compile_unit) = self
.compiler
.package_store()
.get(self.compiler.source_package_id())
{
qsc_linter::run_lints(self.compiler.package_store(), compile_unit, None)
} else {
Vec::new()
}
}

/// Executes the entry expression until the end of execution.
/// # Errors
/// Returns a vector of errors if evaluating the entry point fails.
Expand Down
24 changes: 12 additions & 12 deletions katas/content/complex_arithmetic/Common.qs
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
namespace Kata.Verification {
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Random;
open Microsoft.Quantum.Random;
open Microsoft.Quantum.Convert;

operation DrawRandomComplex() : Complex {
// Generates a random complex number.
// Generates a random complex number.
let real = DrawRandomDouble(-10., 10.);
let imag = DrawRandomDouble(-10., 10.);
return Complex(real, imag);
}

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

function ComplexPolarAsString(x : ComplexPolar) : String {
$"{x::Magnitude} * exp({x::Argument}i)"
$"{x.Magnitude} * exp({x.Argument}i)"
}

operation CheckTwoComplexOpsAreSame(sol : (Complex, Complex) -> Complex, ref : (Complex, Complex) -> Complex) : Bool {
for _ in 0 .. 24 {
for _ in 0..24 {
let x = DrawRandomComplex();
let y = DrawRandomComplex();

let expected = ref(x, y);
let actual = sol(x, y);

if not ComplexEqual(expected, actual) {
Message("Incorrect");
Message($"For x = {ComplexAsString(x)}, y = {ComplexAsString(y)} expected return {ComplexAsString(expected)}, actual return {ComplexAsString(actual)}.");
Expand All @@ -41,13 +41,13 @@ namespace Kata.Verification {
return true;
}

function ComplexEqual(x : Complex, y : Complex) : Bool {
function ComplexEqual(x : Complex, y : Complex) : Bool {
// Tests two complex numbers for equality.
AbsD(x::Real - y::Real) <= 0.001 and AbsD(x::Imag - y::Imag) <= 0.001
AbsD(x.Real - y.Real) <= 0.001 and AbsD(x.Imag - y.Imag) <= 0.001
}

function ComplexPolarEqual(x : ComplexPolar, y : ComplexPolar) : Bool {
function ComplexPolarEqual(x : ComplexPolar, y : ComplexPolar) : Bool {
// Tests two complex polar numbers for equality.
AbsD(x::Magnitude - y::Magnitude) <= 0.001 and AbsD(x::Argument - y::Argument) <= 0.001
AbsD(x.Magnitude - y.Magnitude) <= 0.001 and AbsD(x.Argument - y.Argument) <= 0.001
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace Kata {
open Microsoft.Quantum.Math;

function ComplexToComplexPolar(x : Complex) : ComplexPolar {
let (a, b) = x!;
let (a, b) = (x.Real, x.Imag);
return ComplexPolar(Sqrt(a * a + b * b), ArcTan2(b, a));
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
namespace Kata {
namespace Kata {
open Microsoft.Quantum.Math;
function ComplexAdd(x : Complex, y : Complex) : Complex {

function ComplexAdd(x : Complex, y : Complex) : Complex {
// Extract real and imaginary components of the inputs.
let (a, b) = x!;
let (c, d) = (y::Real, y::Imag);
let (a, b) = (y.Real, y.Imag);
// Implement your solution here...
sezna marked this conversation as resolved.
Show resolved Hide resolved
return Complex(0., 0.);
}
Expand Down
10 changes: 4 additions & 6 deletions katas/content/complex_arithmetic/complex_addition/Solution.qs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
namespace Kata {
namespace Kata {
open Microsoft.Quantum.Math;

function ComplexAdd(x : Complex, y: Complex) : Complex {
let (a, b) = x!;
let (c, d) = y!;
return Complex(a + c, b + d);

function ComplexAdd(x : Complex, y : Complex) : Complex {
Complex(x.Real + y.Real, x.Imag + y.Imag)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Kata {
namespace Kata {
open Microsoft.Quantum.Math;
function ComplexConjugate(x : Complex) : Complex {

function ComplexConjugate(x : Complex) : Complex {
// Implement your solution here...
return Complex(0., 0.);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Kata {
open Microsoft.Quantum.Math;
namespace Kata {
open Microsoft.Quantum.Math;

operation ComplexConjugate(x : Complex) : Complex {
return Complex(x::Real, -x::Imag);
sezna marked this conversation as resolved.
Show resolved Hide resolved
Complex(x.Real, -x.Imag)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ namespace Kata.Verification {
open Microsoft.Quantum.Math;

function ComplexConjugate_Reference(x : Complex) : Complex {
// Return the complex conjugate
Complex(x::Real, -x::Imag)
}
// Return the complex conjugate
Complex(x.Real, -x.Imag)
}

@EntryPoint()
operation CheckSolution() : Bool {
for _ in 0 .. 24 {
operation CheckSolution() : Bool {
for _ in 0..24 {
let x = DrawRandomComplex();

let expected = ComplexConjugate_Reference(x);
let actual = Kata.ComplexConjugate(x);

if not ComplexEqual(expected, actual) {
Message("Incorrect");
Message($"For x = {ComplexAsString(x)} expected return {ComplexAsString(expected)}, actual return {ComplexAsString(actual)}.");
Expand All @@ -24,4 +24,4 @@ namespace Kata.Verification {
Message("Correct!");
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Kata {
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Math;

function ComplexDiv(x : Complex, y : Complex) : Complex {
// Implement your solution here...
return Complex(0., 0.);
Expand Down
12 changes: 6 additions & 6 deletions katas/content/complex_arithmetic/complex_division/Solution.qs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace Kata {
namespace Kata {
open Microsoft.Quantum.Math;
function ComplexDiv(x : Complex, y: Complex) : Complex {
let (a, b) = x!;
let (c, d) = y!;

function ComplexDiv(x : Complex, y : Complex) : Complex {
let (a, b) = (x.Real, x.Imag);
let (c, d) = (y.Real, y.Imag);
let denominator = c * c + d * d;
let real = (a * c + b * d) / denominator;
let imag = (- a * d + b * c) / denominator;
return Complex(real, imag);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace Kata {
open Microsoft.Quantum.Math;

function ComplexExponent(x : Complex) : Complex {
// Implement your solution here...
return Complex(0.0, 0.0);
return Complex(0., 0.);
swernli marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace Kata {
namespace Kata {
open Microsoft.Quantum.Math;

function ComplexExponent (x : Complex) : Complex {
let (a, b) = x!;
return Complex(E()^a * Cos(b), E()^a * Sin(b));
function ComplexExponent(x : Complex) : Complex {
Complex(E()^x.Real * Cos(x.Imag), E()^x.Real * Sin(x.Imag))
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
namespace Kata.Verification {
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Random;
open Microsoft.Quantum.Random;

function ComplexExponent_Reference(x : Complex) : Complex {
let expa = E() ^ x::Real;
return Complex(expa * Cos(x::Imag), expa * Sin(x::Imag));
let expa = E()^x.Real;
return Complex(expa * Cos(x.Imag), expa * Sin(x.Imag));
}

@EntryPoint()
operation CheckSolution() : Bool {
for _ in 0 .. 24 {
for _ in 0..24 {
let x = DrawRandomComplex();

let expected = ComplexExponent_Reference(x);
let actual = Kata.ComplexExponent(x);

if not ComplexEqual(expected, actual) {
Message("Incorrect");
Message($"For x = {ComplexAsString(x)} expected return {ComplexAsString(expected)}, actual return {ComplexAsString(actual)}.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace Kata {
namespace Kata {
open Microsoft.Quantum.Math;

function ComplexModulus(x : Complex) : Double {
let (a, b) = x!;
let (a, b) = (x.Real, x.Imag);
return Sqrt(a * a + b * b);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Kata {
open Microsoft.Quantum.Math;
function ComplexMult(x : Complex, y: Complex) : Complex {

function ComplexMult(x : Complex, y : Complex) : Complex {
// Implement your solution here...
return Complex(0., 0.);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace Kata {
namespace Kata {
open Microsoft.Quantum.Math;
function ComplexMult(x : Complex, y: Complex) : Complex {
let (a, b) = x!;
let (c, d) = y!;

function ComplexMult(x : Complex, y : Complex) : Complex {
let (a, b) = (x.Real, x.Imag);
let (c, d) = (y.Real, y.Imag);
return Complex(a * c - b * d, a * d + b * c);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
namespace Kata {
namespace Kata {
open Microsoft.Quantum.Math;

function ComplexExpReal(r : Double, x : Complex) : Complex {
if AbsD(r) < 1e-9 {
return Complex(0.0, 0.0);
return Complex(0., 0.);
sezna marked this conversation as resolved.
Show resolved Hide resolved
}

let (a, b) = x!;
let ra = r ^ a;

let ra = r^x.Real;
let lnr = Log(r);
return Complex(ra * Cos(b * lnr), ra * Sin(b * lnr));
return Complex(ra * Cos(x.Imag * lnr), ra * Sin(x.Imag * lnr));
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
namespace Kata.Verification {
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Random;
open Microsoft.Quantum.Random;

function ComplexExpReal_Reference(r : Double, x : Complex) : Complex {
if AbsD(r) < 1e-9 {
return Complex(0.0, 0.0);
return Complex(0., 0.);
}
let real = r ^ x::Real * Cos(x::Imag * Log(r));
let imaginary = r ^ x::Real * Sin(x::Imag * Log(r));
let real = r^x.Real * Cos(x.Imag * Log(r));
let imaginary = r^x.Real * Sin(x.Imag * Log(r));
return Complex(real, imaginary);
}

@EntryPoint()
operation CheckSolution() : Bool {
for ind in 0 .. 24 {
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);

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

if not ComplexEqual(expected, actual) {
Message("Incorrect");
Message($"For x = {ComplexAsString(x)} and r = {r} expected return {ComplexAsString(expected)}, actual return {ComplexAsString(actual)}.");
return false;
}
}
}
}

Message("Correct!");
return true;
return true;
}
}
Loading
Loading