Пример #1
0
        /// <summary>
        /// This is the method used to execute Jupyter "normal" cells. In this case, a normal
        /// cell is expected to have a Q# snippet, which gets compiled and we return the name of
        /// the operations found. These operations are then available for simulation and estimate.
        /// </summary>
        public override async Task <ExecutionResult> ExecuteMundane(string input, IChannel channel)
        {
            channel = channel.WithNewLines();

            return(await Task.Run(async() =>
            {
                try
                {
                    await Workspace.Initialization;

                    var code = Snippets.Compile(input);

                    foreach (var m in code.warnings)
                    {
                        channel.Stdout(m);
                    }

                    // Gets the names of all the operations found for this snippet
                    var opsNames =
                        code.Elements?
                        .Where(e => e.IsQsCallable)
                        .Select(e => e.ToFullName().WithoutNamespace(IQSharp.Snippets.SNIPPETS_NAMESPACE))
                        .ToArray();

                    return opsNames.ToExecutionResult();
                }
                catch (CompilationErrorsException c)
                {
                    foreach (var m in c.Errors)
                    {
                        channel.Stderr(m);
                    }
                    return ExecuteStatus.Error.ToExecutionResult();
                }
                catch (Exception e)
                {
                    Logger.LogWarning(e, "Exception while executing mundane input: {Input}", input);
                    channel.Stderr(e.Message);
                    return ExecuteStatus.Error.ToExecutionResult();
                }
                finally
                {
                    performanceMonitor.Report();
                }
            }));
        }
Пример #2
0
        /// <summary>
        /// Compiles the given code. Checks there is only one operation defined in the code,
        /// and returns its corresponding OperationInfo
        /// </summary>
        public virtual OperationInfo Compile(string code, IChannel channel)
        {
            try
            {
                var result = Snippets.Compile(code);

                foreach (var m in result.warnings)
                {
                    channel.Stdout(m);
                }

                // Gets the names of all the operations found for this snippet
                var opsNames =
                    result.Elements?
                    .Where(e => e.IsQsCallable)
                    .Select(e => e.ToFullName().WithoutNamespace(Microsoft.Quantum.IQSharp.Snippets.SNIPPETS_NAMESPACE))
                    .OrderBy(o => o)
                    .ToArray();

                if (opsNames.Length > 1)
                {
                    channel.Stdout("Expecting only one Q# operation in code. Using the first one");
                }

                return(Resolver.Resolve(opsNames.First()));
            }
            catch (CompilationErrorsException c)
            {
                foreach (var m in c.Errors)
                {
                    channel.Stderr(m);
                }
                return(null);
            }
            catch (Exception e)
            {
                Logger?.LogWarning(e, "Unexpected error.");
                channel.Stderr(e.Message);
                return(null);
            }
        }
Пример #3
0
        /// <summary>
        /// This is the method used to execute Jupyter "normal" cells. In this case, a normal
        /// cell is expected to have a Q# snippet, which gets compiled and we return the name of
        /// the operations found. These operations are then available for simulation and estimate.
        /// </summary>
        public override ExecutionResult ExecuteMundane(string input, IChannel channel)
        {
            channel = channel.WithNewLines();

            try
            {
                var code = Snippets.Compile(input);

                foreach (var m in code.warnings)
                {
                    channel.Stdout(m);
                }

                // Gets the names of all the operations found for this snippet
                var opsNames =
                    code.Elements?
                    .Where(e => e.IsQsCallable)
                    .Select(e => e.ToFullName().WithoutNamespace(IQSharp.Snippets.SNIPPETS_NAMESPACE))
                    .OrderBy(o => o)
                    .ToArray();

                return(opsNames.ToExecutionResult());
            }
            catch (CompilationErrorsException c)
            {
                foreach (var m in c.Errors)
                {
                    channel.Stderr(m);
                }
                return(ExecuteStatus.Error.ToExecutionResult());
            }
            catch (Exception e)
            {
                channel.Stderr(e.Message);
                return(ExecuteStatus.Error.ToExecutionResult());
            }
        }