This repository was archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathExtensions.cs
More file actions
143 lines (124 loc) · 5.27 KB
/
Extensions.cs
File metadata and controls
143 lines (124 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#nullable enable
using System.Text.RegularExpressions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Jupyter.Core.Protocol;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Microsoft.Quantum.IQSharp.Kernel
{
/// <summary>
/// Extension methods to be used with various IQ# and Jupyter objects.
/// </summary>
public static class Extensions
{
/// <summary>
/// Adds services required for the IQ# kernel to a given service
/// collection.
/// </summary>
public static T AddIQSharpKernel<T>(this T services)
where T: IServiceCollection
{
services.AddSingleton<ISymbolResolver, SymbolResolver>();
services.AddSingleton<IMagicSymbolResolver, MagicSymbolResolver>();
services.AddSingleton<IExecutionEngine, Kernel.IQSharpEngine>();
services.AddSingleton<IConfigurationSource, ConfigurationSource>();
services.AddSingleton<INoiseModelSource, NoiseModelSource>();
services.AddSingleton<ClientInfoListener>();
return services;
}
internal static void RenderExecutionPath(this ExecutionPathTracer.ExecutionPathTracer tracer,
IChannel channel,
string executionPathDivId,
int renderDepth,
TraceVisualizationStyle style)
{
// Retrieve the `ExecutionPath` traced out by the `ExecutionPathTracer`
var executionPath = tracer.GetExecutionPath();
// Convert executionPath to JToken for serialization
var executionPathJToken = JToken.FromObject(executionPath,
new JsonSerializer() { NullValueHandling = NullValueHandling.Ignore });
// Send execution path to JavaScript via iopub for rendering
channel.SendIoPubMessage(
new Message
{
Header = new MessageHeader
{
MessageType = "render_execution_path"
},
Content = new ExecutionPathVisualizerContent
(
executionPathJToken,
executionPathDivId,
renderDepth,
style
)
}
);
}
private readonly static Regex UserAgentVersionRegex = new Regex(@"\[(.*)\]");
internal static Version? GetUserAgentVersion(this IMetadataController? metadataController)
{
var userAgent = metadataController?.UserAgent;
if (string.IsNullOrWhiteSpace(userAgent))
return null;
var match = UserAgentVersionRegex.Match(userAgent);
if (match == null || !match.Success)
return null;
if (!Version.TryParse(match.Groups[1].Value, out Version version))
return null;
// return null for development versions that start with 0.0
if (version.Major == 0 && version.Minor == 0)
return null;
return version;
}
internal static int EditDistanceFrom(this string s1, string s2)
{
// Uses the approach at
// https://github.com/dotnet/samples/blob/main/csharp/parallel/EditDistance/Program.cs.
var dist = new int[s1.Length + 1, s2.Length + 1];
for (int i = 0; i <= s1.Length; i++) dist[i, 0] = i;
for (int j = 0; j <= s2.Length; j++) dist[0, j] = j;
for (int i = 1; i <= s1.Length; i++)
{
for (int j = 1; j <= s2.Length; j++)
{
dist[i, j] = (s1[i - 1] == s2[j - 1]) ?
dist[i - 1, j - 1] :
1 + System.Math.Min(dist[i - 1, j],
System.Math.Min(dist[i, j - 1],
dist[i - 1, j - 1]));
}
}
return dist[s1.Length, s2.Length];
}
internal static IServiceProvider AddBuiltInMagicSymbols(this IServiceProvider serviceProvider)
{
serviceProvider.GetRequiredService<IMagicSymbolResolver>()
.AddKernelAssembly<IQSharpKernelApp>()
.AddKernelAssembly<AzureClient.AzureClient>();
return serviceProvider;
}
internal static void DisplayFancyDiagnostics(this IChannel channel, IEnumerable<Diagnostic>? diagnostics, ISnippets snippets, string? input)
{
var defaultPath = new Snippet().FileName;
var sources = snippets.Items.ToDictionary(
s => s.FileName,
s => s.Code
);
foreach (var m in diagnostics ?? Enumerable.Empty<Diagnostic>())
{
var source = m.Source is {} path
? sources.TryGetValue(path, out var snippet)
? snippet
: path == defaultPath
? input
: null
: null;
channel.Display(new FancyError(source, m));
}
}
}
}