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 ) } ); }
/// <summary> /// Outputs a visualization of a runtime execution path of an operation given /// a string with its name and a JSON encoding of its arguments. /// </summary> public async Task <ExecutionResult> RunAsync(string input, IChannel channel) { // Parse input parameters var inputParameters = ParseInputParameters(input, firstParameterInferredName: ParameterNameOperationName); var name = inputParameters.DecodeParameter <string>(ParameterNameOperationName); var symbol = SymbolResolver.Resolve(name) as IQSharpSymbol; if (symbol == null) { throw new InvalidOperationException($"Invalid operation name: {name}"); } var depth = inputParameters.DecodeParameter <int>( ParameterNameDepth, defaultValue: this.ConfigurationSource.TraceVisualizationDefaultDepth ); if (depth <= 0) { throw new ArgumentOutOfRangeException($"Invalid depth: {depth}. Must be >= 1."); } var tracer = new ExecutionPathTracer.ExecutionPathTracer(); // Simulate operation and attach `ExecutionPathTracer` to trace out operations performed // in its execution path using var qsim = new QuantumSimulator() .WithJupyterDisplay(channel, ConfigurationSource) .WithStackTraceDisplay(channel) .WithExecutionPathTracer(tracer); var value = await symbol.Operation.RunAsync(qsim, inputParameters); // 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 }); // Render empty div with unique ID as cell output var divId = $"execution-path-container-{Guid.NewGuid().ToString()}"; var content = new ExecutionPathVisualizerContent( executionPathJToken, divId, depth, this.ConfigurationSource.TraceVisualizationStyle ); channel.DisplayUpdatable(new DisplayableHtmlElement($"<div id='{divId}' />")); // Send execution path to JavaScript via iopub for rendering channel.SendIoPubMessage( new Message { Header = new MessageHeader { MessageType = "render_execution_path" }, Content = content, } ); return(ExecuteStatus.Ok.ToExecutionResult()); }