Skip to content

Commit

Permalink
Update samples to reflect latest 1.7 changes; Update katas and stdlib…
Browse files Browse the repository at this point in the history
… to use structs (#1797)

This PR:
- Updates katas to use structs instead of newtypes
- Performs some minor katas formatting updates (consistency on spacing;
removing trailing whitespace; etc)
- Checks our samples for lint warnings or errors in the CI
- updates our samples to use the latest style from Q# 1.7
  - imports instead of opens
  - implicit entrypoint via `Main`
  - implicit namespaces
- Adds stricter lint configs to projects within this repo

Closes #1709 

Thanks @ScottCarda-MS and @swernli for collaborating on this PR!

---------

Co-authored-by: Scott Carda <[email protected]>
Co-authored-by: Scott Carda <[email protected]>
Co-authored-by: Stefan J. Wernli <[email protected]>
Co-authored-by: Mariia Mykhailova <[email protected]>
  • Loading branch information
5 people authored Aug 26, 2024
1 parent b2f7ddd commit ce8303b
Show file tree
Hide file tree
Showing 79 changed files with 1,512 additions and 1,585 deletions.
23 changes: 23 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::{HirLint, Lint, LintKind, LintLevel};
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,28 @@ 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,
// see https://github.com/microsoft/qsharp/pull/1627 for context
// on why we override this config
Some(&[qsc_linter::LintConfig {
kind: LintKind::Hir(HirLint::NeedlessOperation),
level: LintLevel::Warn,
}]),
)
} 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) = (x.Real, x.Imag);
// Implement your solution here...
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);
function ComplexConjugate(x : Complex) : Complex {
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.);
}
}
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
4 changes: 2 additions & 2 deletions katas/content/complex_arithmetic/complex_modulus/Solution.qs
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);
}
}
11 changes: 5 additions & 6 deletions katas/content/complex_arithmetic/complex_powers_real/Solution.qs
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.);
}

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

0 comments on commit ce8303b

Please sign in to comment.