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
51 changes: 42 additions & 9 deletions src/Simulation/CsharpGeneration.Tests/SimulationCodeTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3685,10 +3685,21 @@ namespace Microsoft.Quantum.Tests.UnitTests
baseSim.OnLog += this.Output.WriteLine;
}

sim.Run<UnitTest1, QVoid, QVoid>(QVoid.Instance).Wait();
if (sim is IDisposable disposeSim)
try
{
sim.Execute<UnitTest1, QVoid, QVoid>(QVoid.Instance);
}
catch (Exception e)
{
#line 22 "%%"
Xunit.Assert.True(false, "Q# Test failed. For details see the Standard output below.");
}
finally
{
disposeSim.Dispose();
if (sim is IDisposable disposeSim)
{
disposeSim.Dispose();
}
}
}
}
Expand Down Expand Up @@ -3717,10 +3728,21 @@ namespace Microsoft.Quantum.Tests.UnitTests
baseSim.OnLog += this.Output.WriteLine;
}

sim.Run<UnitTest1, QVoid, QVoid>(QVoid.Instance).Wait();
if (sim is IDisposable disposeSim)
try
{
disposeSim.Dispose();
sim.Execute<UnitTest1, QVoid, QVoid>(QVoid.Instance);
}
catch (Exception e)
{
#line 22 "%%"
Xunit.Assert.True(false, "Q# Test failed. For details see the Standard output below.");
}
finally
{
if (sim is IDisposable disposeSim)
{
disposeSim.Dispose();
}
}
}
}
Expand Down Expand Up @@ -3778,10 +3800,21 @@ namespace Microsoft.Quantum.Tests.UnitTests
baseSim.OnLog += this.Output.WriteLine;
}

sim.Run<UnitTest2, QVoid, QVoid>(QVoid.Instance).Wait();
if (sim is IDisposable disposeSim)
try
{
sim.Execute<UnitTest2, QVoid, QVoid>(QVoid.Instance);
}
catch (Exception e)
{
disposeSim.Dispose();
#line 26 "%%"
Xunit.Assert.True(false, "Q# Test failed. For details see the Standard output below.");
}
finally
{
if (sim is IDisposable disposeSim)
{
disposeSim.Dispose();
}
}
}
}
Expand Down
24 changes: 21 additions & 3 deletions src/Simulation/CsharpGeneration/SimulationCode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1240,19 +1240,37 @@ module SimulationCode =
let disposeSim = ``ident`` "disposeSim"
let ``this.Output`` = ``ident`` "this" <|.|> ``ident`` "Output"
let ``sim.OnLog`` = baseSim <|.|> ``ident`` "OnLog"
let Run = generic "Run" ``<<`` [opName; "QVoid"; "QVoid"] ``>>``
let Execute = generic "Execute" ``<<`` [opName; "QVoid"; "QVoid"] ``>>``

let simCond = sim |> ``is assign`` "Microsoft.Quantum.Simulation.Common.SimulatorBase" baseSim .&&. ``this.Output`` .!=. ``null``

let getSimulator = ``var`` "sim" (``:=`` <| ``new`` (``ident`` <| targetName.ToString()) ``(`` [] ``)``)
let assignLogEvent =
``if`` ``(`` simCond ``)``
[ ``sim.OnLog`` <+=> (``this.Output`` <|.|> ``ident`` "WriteLine") ] None
let ``sim.Run.Wait`` = sim <.> (Run, [ ``ident`` "QVoid" <|.|> ``ident`` "Instance"]) <.> ((``ident`` "Wait"), []) |> statement
let ``sim.Execute`` = sim <.> (Execute, [ ``ident`` "QVoid" <|.|> ``ident`` "Instance"]) |> statement
let disposeOfRun =
``if`` ``(`` (sim |> ``is assign`` "IDisposable" disposeSim) ``)``
[ disposeSim <.> ((``ident`` "Dispose"), []) |> statement ] None

let errMsg = ``literal`` "Q# Test failed. For details see the Standard output below."

let tryRunCatch =
``try``
[
``sim.Execute``
]
[
``catch`` (Some ("Exception", "e"))
[
(``ident`` "Xunit.Assert") <.> ((``ident`` "True"), [``false`` :> ExpressionSyntax; errMsg]) |> (statement >> ``#line`` (opStart + 1) opSourceFile)
]
]
(Some (``finally``
[
``disposeOfRun``
]))

``attributes``
[
``attribute`` None (``ident`` "Xunit.Fact") [];
Expand All @@ -1261,7 +1279,7 @@ module SimulationCode =
]
(``method`` "void" opName ``<<`` [] ``>>`` ``(`` [] ``)`` [``public``]
``{``
[getSimulator; assignLogEvent; ``sim.Run.Wait``; disposeOfRun]
[getSimulator; assignLogEvent; tryRunCatch]
``}``
|> ``with trivia`` (``#lineNr`` (opStart + 1) opSourceFile) // we need 1-based line numbers here, and opStart is zero-based
)
Expand Down