-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSimulateModule.cs
86 lines (75 loc) · 2.66 KB
/
SimulateModule.cs
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
// <copyright file="SimulateModule.cs" company="https://qsharp.community/">
// Copyright (c) Chris Granade. Licensed under the MIT License.
// </copyright>
using System;
using System.Linq;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Microsoft.Extensions.Logging;
using Microsoft.Quantum.IQSharp;
using Microsoft.Quantum.IQSharp.Common;
using Microsoft.Quantum.Simulation.Simulators;
/// <summary>
/// Module with Discord bot commands relating to simulating Q# code.
/// </summary>
public class SimulateModule : ModuleBase<SocketCommandContext>
{
private readonly ISnippets snippets;
private readonly IWorkspace workspace;
private readonly ILogger logger;
private static readonly Emoji waiting = new Emoji("⌛");
public SimulateModule(ISnippets snippets, IWorkspace workspace, ILogger<SimulateModule> logger)
{
this.snippets = snippets;
this.workspace = workspace;
this.logger = logger;
}
[Command("simulate", RunMode = RunMode.Async)]
[Summary("Simulates a Q# snippet on the full-state simulator.")]
public async Task Simulate(
[Remainder]
[Summary("The code to be simulated.")]
string code
)
{
logger.LogDebug("Got new %simulate message.");
await this.Context.Message.AddReactionAsync(waiting);
try
{
await workspace.Initialization;
var snippet = snippets.Compile($@"operation RunDiscordCommand() : Unit {{
{code}
}}");
foreach (var m in snippet.warnings)
{
await ReplyAsync($"[WARNING] {m}");
}
// Now actually run it.
using var simulator = new QuantumSimulator();
simulator.DisableLogToConsole();
simulator.OnLog += async (msg) =>
{
var reply = await ReplyAsync($"➡ {msg}");
};
var operation = snippets.Operations.Single();
var runMethod = operation.RoslynType.GetMethod("Run");
if (runMethod == null)
{
throw new Exception("Run method not found on generated C# type.");
}
var result = runMethod.Invoke(null, new object[] { simulator });
var response = await (dynamic)result!;
await ReplyAsync(response.ToString());
}
catch (CompilationErrorsException cex)
{
await ReplyAsync($"Compilation error: {cex}");
}
finally
{
snippets.Items = Array.Empty<Snippet>();
await Context.Message.RemoveReactionAsync(waiting, Context.Client.CurrentUser);
}
}
}