public void UninitializeOrder2()
        {
            var order = new List <string>();

            var config = new Config
            {
                Modules =
                {
                    new ModuleInfo(new ModuleA(uninit: () => order.Add("moduleA"))),
                    new ModuleInfo(new ModuleB(uninit: () => order.Add("moduleB")))
                }
            };

            var application = new KernelApplication(config, false);

            application.Initialize();

            var moduleC = new ModuleC(uninit: () => order.Add("moduleC"));

            moduleC.Initialize();

            application.AddModule(moduleC);

            application.Uninitialize();

            Assert.AreEqual(3, order.Count);
            Assert.AreEqual("moduleB", order[0]);
            Assert.AreEqual("moduleA", order[1]);
            Assert.AreEqual("moduleC", order[2]);
        }
Esempio n. 2
0
        // Adds the Workspace settings to the "server" and "kernel" commands:
        public static KernelApplication AddWorkspaceOption(KernelApplication app)
        {
            var cacheOption = app.Option("--cacheFolder <FOLDER>",
                                         "Specifies the folder to use to create temporary cache files.", CommandOptionType.SingleValue);
            var workspaceOption = app.Option("-w|--workspace <FOLDER>",
                                             "Specifies the workspace root folder. " +
                                             "All .qs files inside this folder will be automatically compiled and the corresponding " +
                                             "operations available for simulation.", CommandOptionType.SingleValue);

            foreach (var command in app.Commands.Where(c => c.Name == "kernel" || c.Name == "server"))
            {
                command.Options.Add(cacheOption);
                command.Options.Add(workspaceOption);
            }

            return(app);
        }
Esempio n. 3
0
        public static void Start(KernelApplication app, IConfiguration config)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            if (!string.IsNullOrWhiteSpace(config?.GetValue <string>("TELEMETRY_OPT_OUT")))
            {
                Console.WriteLine("--> IQ# Telemetry opted-out. No telemetry data will be generated.");
                return;
            }

            _config = config;

            app.KernelStarted += OnKenerlStart;
            app.KernelStopped += OnKernelStop;
        }
        public void InitializeOrder()
        {
            var order = new List <string>();

            var config = new Config
            {
                Modules =
                {
                    new ModuleInfo(new ModuleA(() => order.Add("moduleA"))),
                    new ModuleInfo(new ModuleB(() => order.Add("moduleB")))
                }
            };

            var application = new KernelApplication(config, false);

            application.Initialize();

            Assert.AreEqual(2, order.Count);
            Assert.AreEqual("moduleA", order[0]);
            Assert.AreEqual("moduleB", order[1]);
        }
        public void CreateModules()
        {
            var config = new Config
            {
                Modules =
                {
                    new ModuleInfo(new ModuleA()),
                    new ModuleInfo(new ModuleB())
                }
            };

            var application = new KernelApplication(config, false);

            application.Initialize();

            bool result0 = application.TryGetModule(out ModuleA moduleA);
            bool result1 = application.TryGetModule(out ModuleB moduleB);

            Assert.True(result0);
            Assert.True(result1);
            Assert.NotNull(moduleA);
            Assert.NotNull(moduleB);
        }
Esempio n. 6
0
        public static int Main(string[] args)
        {
            try
            {
                Configuration = new ConfigurationBuilder()
                                .AddEnvironmentVariables()
                                .AddJsonFile("appsettings.json")
                                .AddCommandLine(
                    args,
                    // We provide explicit aliases for those command line
                    // options that specify client information, matching
                    // the placeholder options that we define below.
                    new Dictionary <string, string>()
                {
                    ["--user-agent"]  = "IQSHARP_USER_AGENT",
                    ["--hosting-env"] = "IQSHARP_HOSTING_ENV"
                }
                    )
                                .Build();

                var app = new KernelApplication(
                    Jupyter.Constants.IQSharpKernelProperties, new Startup().ConfigureServices
                    )
                          .WithKernelSpecResources <Jupyter.IQSharpEngine>(
                    new Dictionary <string, string>
                {
                    ["logo-64x64.png"] = "Microsoft.Quantum.IQSharp.Jupyter.res.logo-64x64.png",
                    ["kernel.js"]      = "Microsoft.Quantum.IQSharp.Jupyter.res.kernel.js"
                }
                    );
                app.Command(
                    "server",
                    cmd =>
                {
                    cmd.HelpOption();
                    cmd.Description = $"Runs IQSharp as an HTTP server.";
                    cmd.OnExecute(() =>
                    {
                        WebHost.CreateDefaultBuilder(args)
                        .UseUrls("http://localhost:8888")
                        .UseStartup <Startup>()
                        .Build()
                        .Run();
                        return(0);
                    });
                }
                    );

                AddWorkspaceOption(
                    app
                    .AddInstallCommand()
                    .AddKernelCommand(
                        // These command options will be consumed by the Program.Configuration
                        // object above, rather than by the kernel application object itself.
                        // Thus, we only need placeholders to prevent the kernel application
                        // from raising an exception when unrecognized options are passed.
                        kernelCmd => {
                    kernelCmd.Option <string>(
                        "--user-agent <AGENT>",
                        "Specifies which user agent has initiated this kernel instance.",
                        CommandOptionType.SingleValue
                        );
                    kernelCmd.Option <string>(
                        "--hosting-env <ENV>",
                        "Specifies the hosting environment that this kernel is being run in.",
                        CommandOptionType.SingleValue
                        );
                }
                        )
                    );

#if TELEMETRY
                Telemetry.Start(app, Configuration);
#endif

                return(app.Execute(args));
            }
            catch (CommandParsingException)
            {
                return(1);
            }
        }
Esempio n. 7
0
        public static int Main(string[] args)
        {
            try
            {
                Configuration = new ConfigurationBuilder()
                                .AddJsonFile("appsettings.json")
                                .AddCommandLine(
                    args,
                    // We provide explicit aliases for those command line
                    // options that specify client information, matching
                    // the placeholder options that we define below.
                    new Dictionary <string, string>()
                {
                    ["--user-agent"]  = "UserAgent",
                    ["--hosting-env"] = "HostingEnvironment"
                }
                    )
                                .Add(new NormalizedEnvironmentVariableConfigurationSource
                {
                    Prefix  = "IQSHARP_",
                    Aliases = new Dictionary <string, string>
                    {
                        ["USER_AGENT"]  = "UserAgent",
                        ["HOSTING_ENV"] = "HostingEnvironment",
                        ["LOG_PATH"]    = "LogPath"
                    }
                })
                                .Build();

                var app = new KernelApplication(
                    Jupyter.Constants.IQSharpKernelProperties, new Startup().ConfigureServices
                    )
                          .ConfigureLogging(
                    loggingBuilder => {
                    // As per https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1#access-configuration-during-startup,
                    // we need to use an IConfiguration instance directly during
                    // startup, rather than using the nice binding methods
                    // like serviceCollection.Configure<TOptions>(configuration).
                    var options = Configuration.Get <LoggingOptions>();
                    if (options?.LogPath != null && options.LogPath.Length != 0)
                    {
                        loggingBuilder.AddFile(
                            options.LogPath,
                            minimumLevel: LogLevel.Debug
                            );
                    }
                }
                    )
                          .WithKernelSpecResources <Jupyter.IQSharpEngine>(
                    new Dictionary <string, string>
                {
                    ["logo-64x64.png"] = "Microsoft.Quantum.IQSharp.Jupyter.res.logo-64x64.png",
                    ["kernel.js"]      = "Microsoft.Quantum.IQSharp.Jupyter.res.kernel.js"
                }
                    );
                app.Command(
                    "server",
                    cmd =>
                {
                    cmd.HelpOption();
                    cmd.Description = $"Runs IQSharp as an HTTP server.";
                    cmd.OnExecute(() =>
                    {
                        GetHttpServer(args).Run();
                        return(0);
                    });
                }
                    );

                AddWorkspaceOption(
                    app
                    .AddInstallCommand()
                    .AddKernelCommand(
                        // These command options will be consumed by the Program.Configuration
                        // object above, rather than by the kernel application object itself.
                        // Thus, we only need placeholders to prevent the kernel application
                        // from raising an exception when unrecognized options are passed.
                        kernelCmd => {
                    kernelCmd.Option <string>(
                        "--user-agent <AGENT>",
                        "Specifies which user agent has initiated this kernel instance.",
                        CommandOptionType.SingleValue
                        );
                    kernelCmd.Option <string>(
                        "--hosting-env <ENV>",
                        "Specifies the hosting environment that this kernel is being run in.",
                        CommandOptionType.SingleValue
                        );
                }
                        )
                    );

#if TELEMETRY
                Telemetry.Start(app, Configuration);
#endif

                return(app.Execute(args));
            }
            catch (CommandParsingException)
            {
                return(1);
            }
        }