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
6 changes: 3 additions & 3 deletions src/Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />
<PackageReference Include="Microsoft.Quantum.Compiler" Version="0.10.2001.2831" />
<PackageReference Include="Microsoft.Quantum.CsharpGeneration" Version="0.10.2001.2831" />
<PackageReference Include="Microsoft.Quantum.Simulators" Version="0.10.2001.2831" />
<PackageReference Include="Microsoft.Quantum.Compiler" Version="0.10.2003.1102-beta" />
<PackageReference Include="Microsoft.Quantum.CsharpGeneration" Version="0.10.2003.1102-beta" />
<PackageReference Include="Microsoft.Quantum.Simulators" Version="0.10.2003.1102-beta" />
<PackageReference Include="NuGet.Resolver" Version="5.1.0" />
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
</ItemGroup>
Expand Down
16 changes: 16 additions & 0 deletions src/Jupyter/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.Jupyter.Core;
using Microsoft.Jupyter.Core.Protocol;
using Microsoft.Quantum.IQSharp.Common;
using Microsoft.Quantum.Simulation.Common;
using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Simulation.Simulators;

Expand Down Expand Up @@ -153,5 +154,20 @@ public static QuantumSimulator WithJupyterDisplay(this QuantumSimulator simulato

return simulator;
}

public static T WithStackTraceDisplay<T>(this T simulator, IChannel channel)
where T: SimulatorBase
{
simulator.DisableExceptionPrinting();
simulator.OnException += (exception, stackTrace) =>
{
channel.Display(new DisplayableException
{
Exception = exception,
StackTrace = stackTrace
});
};
return simulator;
}
}
}
3 changes: 3 additions & 0 deletions src/Jupyter/IQSharpEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ IShellRouter shellRouter
RegisterDisplayEncoder(new StateVectorToTextResultEncoder(configurationSource));
RegisterDisplayEncoder(new DataTableToHtmlEncoder());
RegisterDisplayEncoder(new DataTableToTextEncoder());
RegisterDisplayEncoder(new DisplayableExceptionToHtmlEncoder());
RegisterDisplayEncoder(new DisplayableExceptionToTextEncoder());
RegisterJsonEncoder(JsonConverters.AllConverters);

RegisterSymbolResolver(this.SymbolsResolver);
Expand Down Expand Up @@ -119,3 +121,4 @@ public override ExecutionResult ExecuteMundane(string input, IChannel channel)
}
}
}

2 changes: 1 addition & 1 deletion src/Jupyter/Magic/EstimateMagic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public async Task<ExecutionResult> RunAsync(string input, IChannel channel)
var symbol = SymbolResolver.Resolve(name) as IQSharpSymbol;
if (symbol == null) throw new InvalidOperationException($"Invalid operation name: {name}");

var qsim = new ResourcesEstimator();
var qsim = new ResourcesEstimator().WithStackTraceDisplay(channel);
qsim.DisableLogToConsole();

await symbol.Operation.RunAsync(qsim, args);
Expand Down
4 changes: 3 additions & 1 deletion src/Jupyter/Magic/Simulate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public async Task<ExecutionResult> RunAsync(string input, IChannel channel)
var symbol = SymbolResolver.Resolve(name) as IQSharpSymbol;
if (symbol == null) throw new InvalidOperationException($"Invalid operation name: {name}");

using var qsim = new QuantumSimulator().WithJupyterDisplay(channel, ConfigurationSource);
using var qsim = new QuantumSimulator()
.WithJupyterDisplay(channel, ConfigurationSource)
.WithStackTraceDisplay(channel);
var value = await symbol.Operation.RunAsync(qsim, args);
return value.ToExecutionResult();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Jupyter/Magic/ToffoliMagic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task<ExecutionResult> RunAsync(string input, IChannel channel)
var symbol = SymbolResolver.Resolve(name) as IQSharpSymbol;
if (symbol == null) throw new InvalidOperationException($"Invalid operation name: {name}");

var qsim = new ToffoliSimulator();
var qsim = new ToffoliSimulator().WithStackTraceDisplay(channel);
qsim.DisableLogToConsole();
qsim.OnLog += channel.Stdout;

Expand Down
123 changes: 123 additions & 0 deletions src/Jupyter/Visualization/DisplayableExceptionEncoders.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#nullable enable

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Jupyter.Core;
using Microsoft.Quantum.QsCompiler.SyntaxTree;
using Microsoft.Quantum.Simulation.Common;
using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Simulation.Simulators;

namespace Microsoft.Quantum.IQSharp
{
internal struct DisplayableException
{
public Exception Exception;
public IEnumerable<StackFrame> StackTrace;

public string Header =>
$"Unhandled exception. {Exception.GetType().FullName}: {Exception.Message}";
}

internal static class StackFrameExtensions
{
internal static string ToFriendlyName(this ICallable callable)
{
var fullName = callable.FullName;
return fullName.StartsWith(Snippets.SNIPPETS_NAMESPACE)
? fullName.Substring(Snippets.SNIPPETS_NAMESPACE.Length + 1)
: fullName;
}

internal static string ToSourceLink(this StackFrame frame) =>
Regex.Match(frame.SourceFile, "snippet_[0-9]*.qs$").Success
? "(notebook)"
: $"<a href=\"{frame.GetBestSourceLocation()}\">{frame.SourceFile}:{frame.FailedLineNumber}</a>";
}

/// <summary>
/// Encodes exceptions augmented with Q# metadata into HTML tables.
/// </summary>
public class DisplayableExceptionToHtmlEncoder : IResultEncoder
{
/// <inheritdoc />
public string MimeType => MimeTypes.Html;

/// <inheritdoc />
public EncodedData? Encode(object displayable)
{
if (displayable is DisplayableException ex)
{
var rows = ex
.StackTrace
.Select(frame => $@"
<tr>
<td>{frame.ToSourceLink()}</td>
<td>{frame.Callable.ToFriendlyName()}</td>
</tr>
");
var table = $@"
<details>
<summary>
Unhandled exception of type {ex.Exception.GetType().FullName}: {ex.Exception.Message}
</summary>
<table>
<thead>
<tr>
<th>Source</th>
<th>Callable</th>
</tr>
</thead>

<tbody>
{String.Join("\n", rows)}
</tbody>
</table>
</details>
";
return table.ToEncodedData();
}
else return null;
}
}

/// <summary>
/// Encodes exceptions augmented with Q# metadata into plain text
/// tables.
/// </summary>
public class DisplayableExceptionToTextEncoder : IResultEncoder
{
/// <inheritdoc />
public string MimeType => MimeTypes.PlainText;

/// <inheritdoc />
public EncodedData? Encode(object displayable)
{
if (displayable is DisplayableException ex)
{
var builder = new StringBuilder();
builder.AppendLine(ex.Header);
var first = true;
foreach (var frame in ex.StackTrace)
{
builder.AppendLine(
(first ? " ---> " : " at ") +
frame.ToStringWithBestSourceLocation()
);
first = false;
}
return builder.ToString().ToEncodedData();
}
else return null;
}
}

}
20 changes: 10 additions & 10 deletions src/Tool/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
},
"AllowedHosts": "*",
"DefaultPackageVersions": [
"Microsoft.Quantum.Compiler::0.10.2001.2831",
"Microsoft.Quantum.Compiler::0.10.2003.1102-beta",

"Microsoft.Quantum.CsharpGeneration::0.10.2001.2831",
"Microsoft.Quantum.Development.Kit::0.10.2001.2831",
"Microsoft.Quantum.Simulators::0.10.2001.2831",
"Microsoft.Quantum.Xunit::0.10.2001.2831",
"Microsoft.Quantum.CsharpGeneration::0.10.2003.1102-beta",
"Microsoft.Quantum.Development.Kit::0.10.2003.1102-beta",
"Microsoft.Quantum.Simulators::0.10.2003.1102-beta",
"Microsoft.Quantum.Xunit::0.10.2003.1102-beta",

"Microsoft.Quantum.Standard::0.10.2001.2831",
"Microsoft.Quantum.Chemistry::0.10.2001.2831",
"Microsoft.Quantum.Chemistry.Jupyter::0.10.2001.2831",
"Microsoft.Quantum.Numerics::0.10.2001.2831",
"Microsoft.Quantum.Standard::0.10.2003.1102-beta",
"Microsoft.Quantum.Chemistry::0.10.2003.1102-beta",
"Microsoft.Quantum.Chemistry.Jupyter::0.10.2003.1102-beta",
"Microsoft.Quantum.Numerics::0.10.2003.1102-beta",

"Microsoft.Quantum.Research::0.10.2001.2831"
"Microsoft.Quantum.Research::0.10.2003.1102-beta"
]
}