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
13 changes: 13 additions & 0 deletions src/Simulation/Simulators.Tests/StackTraceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,19 @@ public void ErrorLogTest()
Assert.StartsWith(" at Microsoft.Quantum.Simulation.Simulators.Tests.Circuits.AlwaysFail4 on", logs[5]);
Assert.Equal("", logs[6]);
}

logs.Clear();
sim.EnableStackTracePrinting = false;
try
{
QVoid res = sim.Execute<AlwaysFail4, QVoid, QVoid>(QVoid.Instance);
}
catch (ExecutionFailException)
{
Assert.Equal(2, logs.Count);
Assert.StartsWith("Unhandled exception. Microsoft.Quantum.Simulation.Core.ExecutionFailException: Always fail", logs[0]);
Assert.Equal("", logs[1]);
}
}
}
}
20 changes: 15 additions & 5 deletions src/Simulation/Simulators/QuantumSimulator/SimulatorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ public abstract class SimulatorBase : Factory<AbstractCallable>, IOperationFacto

public abstract string Name { get; }

/// <summary>
/// When exception printing is enabled, the value of this property controls stack trace printout.
/// When set to <c>true</c> (default), the stack trace is printed in addition to the exception message."
/// When set to <c>false</c>, the exception message is printed without the stack trace."
/// </summary>
public bool EnableStackTracePrinting { get; set; } = true;

/// <summary>
/// If the execution finishes in failure, this method returns the call-stack of the Q# operations
/// executed up to the point when the failure happened.
Expand Down Expand Up @@ -140,12 +147,15 @@ public override AbstractCallable CreateInstance(Type t)
protected void WriteStackTraceToLog(Exception exception, IEnumerable<StackFrame> callStack)
{
OnLog?.Invoke($"Unhandled exception. {exception.GetType().FullName}: {exception.Message}");
var first = true;
foreach (var sf in callStack)
if (EnableStackTracePrinting)
{
var msg = (first ? " ---> " : " at ") + sf.ToStringWithBestSourceLocation();
OnLog?.Invoke(msg);
first = false;
var first = true;
foreach (var sf in callStack)
{
var msg = (first ? " ---> " : " at ") + sf.ToStringWithBestSourceLocation();
OnLog?.Invoke(msg);
first = false;
}
}
OnLog?.Invoke("");
}
Expand Down