Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
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
82 changes: 81 additions & 1 deletion src/QsCompiler/Tests.Compiler/ClassicalControlTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1265,4 +1265,84 @@ type ClassicalControlTests () =
IsApplyIfElseArgsMatch args "[r1], [r2]" Bar NoOp
|> (fun (x, _, _, _, _) -> Assert.True(x, "ApplyConditionally did not have the correct arguments"))

Assert.True(IsTypeArgsMatch targs "Result, Unit", "ApplyConditionally did not have the correct type arguments")
Assert.True(IsTypeArgsMatch targs "Result, Unit", "ApplyConditionally did not have the correct type arguments")

[<Fact>]
[<Trait("Category","Inequality Condition")>]
member this.``Inequality with ApplyConditionally`` () =
let result = CompileClassicalControlTest 31

let original = GetCallableWithName result Signatures.ClassicalControlNs "Foo" |> GetBodyFromCallable
let lines = original |> GetLinesFromSpecialization

Assert.True(3 = Seq.length lines, sprintf "Callable %O(%A) did not have the expected number of statements" original.Parent original.Kind)

let (success, targs, args) = CheckIfLineIsCall BuiltIn.ApplyConditionally.Namespace.Value BuiltIn.ApplyConditionally.Name.Value lines.[2]
Assert.True(success, sprintf "Callable %O(%A) did not have expected content" original.Parent original.Kind)

let Bar = {Namespace = NonNullable<_>.New Signatures.ClassicalControlNs; Name = NonNullable<_>.New "Bar"}
let SubOp1 = {Namespace = NonNullable<_>.New "SubOps"; Name = NonNullable<_>.New "SubOp1"}

IsApplyIfElseArgsMatch args "[r1], [r2]" SubOp1 Bar
|> (fun (x, _, _, _, _) -> Assert.True(x, "ApplyConditionally did not have the correct arguments"))

Assert.True(IsTypeArgsMatch targs "Unit, Result", "ApplyConditionally did not have the correct type arguments")

[<Fact>]
[<Trait("Category","Inequality Condition")>]
member this.``Inequality with Apply If One Else Zero`` () =
let (targs, args) = CompileClassicalControlTest 32 |> ApplyIfElseTest

let Bar = {Namespace = NonNullable<_>.New Signatures.ClassicalControlNs; Name = NonNullable<_>.New "Bar"}
let SubOp1 = {Namespace = NonNullable<_>.New "SubOps"; Name = NonNullable<_>.New "SubOp1"}

IsApplyIfElseArgsMatch args "r" SubOp1 Bar
|> (fun (x, _, _, _, _) -> Assert.True(x, "ApplyIfElse did not have the correct arguments"))

Assert.True(IsTypeArgsMatch targs "Unit, Result", "ApplyIfElse did not have the correct type arguments")

[<Fact>]
[<Trait("Category","Inequality Condition")>]
member this.``Inequality with Apply If Zero Else One`` () =
let (targs, args) = CompileClassicalControlTest 33 |> ApplyIfElseTest

let Bar = {Namespace = NonNullable<_>.New Signatures.ClassicalControlNs; Name = NonNullable<_>.New "Bar"}
let SubOp1 = {Namespace = NonNullable<_>.New "SubOps"; Name = NonNullable<_>.New "SubOp1"}

IsApplyIfElseArgsMatch args "r" Bar SubOp1
|> (fun (x, _, _, _, _) -> Assert.True(x, "ApplyIfElse did not have the correct arguments"))

Assert.True(IsTypeArgsMatch targs "Result, Unit", "ApplyIfElse did not have the correct type arguments")

[<Fact>]
[<Trait("Category","Inequality Condition")>]
member this.``Inequality with ApplyIfOne`` () =
let result = CompileClassicalControlTest 34

let originalOp = GetCallableWithName result Signatures.ClassicalControlNs "Foo" |> GetBodyFromCallable

[ (1, BuiltIn.ApplyIfOne) ]
|> Seq.map ExpandBuiltInQualifiedSymbol
|> AssertSpecializationHasCalls originalOp

[<Fact>]
[<Trait("Category","Inequality Condition")>]
member this.``Inequality with ApplyIfZero`` () =
let result = CompileClassicalControlTest 35

let originalOp = GetCallableWithName result Signatures.ClassicalControlNs "Foo" |> GetBodyFromCallable

[ (1, BuiltIn.ApplyIfZero) ]
|> Seq.map ExpandBuiltInQualifiedSymbol
|> AssertSpecializationHasCalls originalOp

[<Fact>]
[<Trait("Category","Condition API Conversion")>]
member this.``Literal on the Left`` () =
let result = CompileClassicalControlTest 36

let originalOp = GetCallableWithName result Signatures.ClassicalControlNs "Foo" |> GetBodyFromCallable

[(1, BuiltIn.ApplyIfZero)]
|> Seq.map ExpandBuiltInQualifiedSymbol
|> AssertSpecializationHasCalls originalOp
Original file line number Diff line number Diff line change
Expand Up @@ -909,4 +909,110 @@ namespace Microsoft.Quantum.Testing.ClassicalControl {
Bar(r1);
}
}
}

// =================================

// Inequality with ApplyConditionally
namespace Microsoft.Quantum.Testing.ClassicalControl {
open SubOps;

operation Bar(r : Result) : Unit { }

operation Foo() : Unit {
let r1 = Zero;
let r2 = Zero;

if (r1 != r2) {
Bar(r1);
}
else {
SubOp1();
}
}
}

// =================================

// Inequality with Apply If One Else Zero
namespace Microsoft.Quantum.Testing.ClassicalControl {
open SubOps;

operation Bar(r : Result) : Unit { }

operation Foo() : Unit {
let r = Zero;

if (r != Zero) {
Bar(r);
}
else {
SubOp1();
}
}
}

// =================================

// Inequality with Apply If Zero Else One
namespace Microsoft.Quantum.Testing.ClassicalControl {
open SubOps;

operation Bar(r : Result) : Unit { }

operation Foo() : Unit {
let r = Zero;

if (r != One) {
Bar(r);
}
else {
SubOp1();
}
}
}

// =================================

// Inequality with ApplyIfOne
namespace Microsoft.Quantum.Testing.ClassicalControl {
open SubOps;

operation Foo() : Unit {
let r = Zero;

if (r != Zero) {
SubOp1();
}
}
}

// =================================

// Inequality with ApplyIfZero
namespace Microsoft.Quantum.Testing.ClassicalControl {
open SubOps;

operation Foo() : Unit {
let r = Zero;

if (r != One) {
SubOp1();
}
}
}

// =================================

// Literal on the Left
namespace Microsoft.Quantum.Testing.ClassicalControl {
open SubOps;

operation Foo() : Unit {
let r = Zero;

if (Zero == r) {
SubOp1();
}
}
}
21 changes: 21 additions & 0 deletions src/QsCompiler/Tests.Compiler/TestUtils/Signatures.fs
Original file line number Diff line number Diff line change
Expand Up @@ -393,5 +393,26 @@ let public ClassicalControlSignatures =
ClassicalControlNs, "Bar", [|"Result"|], "Unit"
ClassicalControlNs, "Foo", [||], "Unit"
|])
(_DefaultTypes, [| // Inequality with ApplyConditionally
ClassicalControlNs, "Bar", [|"Result"|], "Unit"
ClassicalControlNs, "Foo", [||], "Unit"
|])
(_DefaultTypes, [| // Inequality with Apply If One Else Zero
ClassicalControlNs, "Bar", [|"Result"|], "Unit"
ClassicalControlNs, "Foo", [||], "Unit"
|])
(_DefaultTypes, [| // Inequality with Apply If Zero Else One
ClassicalControlNs, "Bar", [|"Result"|], "Unit"
ClassicalControlNs, "Foo", [||], "Unit"
|])
(_DefaultTypes, [| // Inequality with ApplyIfOne
ClassicalControlNs, "Foo", [||], "Unit"
|])
(_DefaultTypes, [| // Inequality with ApplyIfZero
ClassicalControlNs, "Foo", [||], "Unit"
|])
(_DefaultTypes, [| // Literal on the Left
ClassicalControlNs, "Foo", [||], "Unit"
|])
|]
|> _MakeSignatures
Loading