コード例 #1
0
        public ExecutionPath GetExecutionPath(string name, int depth = 1)
        {
            var ws = InitWorkspace();
            var op = ws.AssemblyInfo.Operations.SingleOrDefault(o => o.FullName == $"Tests.ExecutionPathTracer.{name}");

            Assert.IsNotNull(op);

            var tracer = new ExecutionPathTracer(depth);

            using var qsim = new QuantumSimulator().WithExecutionPathTracer(tracer);
            op.RunAsync(qsim, new Dictionary <string, string>()).Wait();

            return(tracer.GetExecutionPath());
        }
コード例 #2
0
        /// <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(depth);

            // 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);

            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());
        }