コード例 #1
0
        public void TestPackages()
        {
            var engine   = Init();
            var snippets = engine.Snippets as Snippets;
            var pkgMagic = new PackageMagic(snippets.GlobalReferences);
            var channel  = new MockChannel();
            var response = pkgMagic.Execute("", channel);
            var result   = response.Output as string[];

            PrintResult(response, channel);
            Assert.AreEqual(ExecuteStatus.Ok, response.Status);
            Assert.AreEqual(0, channel.msgs.Count);
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Length);

            // Try compiling TrotterEstimateEnergy, it should fail due to the lack
            // of chemistry package.
            response = engine.ExecuteMundane(SNIPPETS.TrotterEstimateEnergy, channel);
            PrintResult(response, channel);
            Assert.AreEqual(ExecuteStatus.Error, response.Status);

            response = pkgMagic.Execute("microsoft.quantum.chemistry", channel);
            result   = response.Output as string[];
            PrintResult(response, channel);
            Assert.AreEqual(ExecuteStatus.Ok, response.Status);
            Assert.AreEqual(0, channel.msgs.Count);
            Assert.IsNotNull(result);
            Assert.AreEqual(2, result.Length);

            // Now it should compile:
            AssertCompile(engine, SNIPPETS.TrotterEstimateEnergy, "TrotterEstimateEnergy");
        }
コード例 #2
0
        public void TestWorkspace()
        {
            var engine   = Init("Workspace.Chemistry");
            var snippets = engine.Snippets as Snippets;

            var wsMagic  = new WorkspaceMagic(snippets.Workspace);
            var pkgMagic = new PackageMagic(snippets.GlobalReferences);

            var channel = new MockChannel();
            var result  = new string[0];

            // Check the workspace, it should be in error state:
            var response = wsMagic.Execute("reload", channel);

            PrintResult(response, channel);
            Assert.AreEqual(ExecuteStatus.Error, response.Status);

            response = wsMagic.Execute("", channel);
            PrintResult(response, channel);
            Assert.AreEqual(ExecuteStatus.Error, response.Status);

            // Try compiling a snippet that depends on a workspace that depends on the chemistry package:
            response = engine.ExecuteMundane(SNIPPETS.DependsOnChemistryWorkspace, channel);
            PrintResult(response, channel);
            Assert.AreEqual(ExecuteStatus.Error, response.Status);
            Assert.AreEqual(0, channel.msgs.Count);

            // Add dependencies:
            response = pkgMagic.Execute("microsoft.quantum.chemistry", channel);
            PrintResult(response, channel);
            Assert.AreEqual(ExecuteStatus.Ok, response.Status);
            response = pkgMagic.Execute("microsoft.quantum.research", channel);
            PrintResult(response, channel);
            Assert.AreEqual(ExecuteStatus.Ok, response.Status);

            // Reload workspace:
            response = wsMagic.Execute("reload", channel);
            PrintResult(response, channel);
            Assert.AreEqual(ExecuteStatus.Ok, response.Status);

            response = wsMagic.Execute("", channel);
            result   = response.Output as string[];
            PrintResult(response, channel);
            Assert.AreEqual(ExecuteStatus.Ok, response.Status);
            Assert.AreEqual(3, result.Length);

            // Now compilation must work:
            AssertCompile(engine, SNIPPETS.DependsOnChemistryWorkspace, "DependsOnChemistryWorkspace");

            // Check an invalid command
            response = wsMagic.Execute("foo", channel);
            PrintResult(response, channel);
            Assert.AreEqual(ExecuteStatus.Error, response.Status);

            // Check that everything still works:
            response = wsMagic.Execute("", channel);
            PrintResult(response, channel);
            Assert.AreEqual(ExecuteStatus.Ok, response.Status);
        }
コード例 #3
0
        public async Task TestPackages()
        {
            var engine   = Init();
            var snippets = engine.Snippets as Snippets;

            var pkgMagic     = new PackageMagic(snippets.GlobalReferences);
            var references   = ((References)pkgMagic.References);
            var packageCount = references.AutoLoadPackages.Count + references.DeferredLoadPackages.Count;
            var channel      = new MockChannel();
            var response     = await pkgMagic.Execute("", channel);

            var result = response.Output as string[];

            PrintResult(response, channel);
            Assert.AreEqual(ExecuteStatus.Ok, response.Status);
            Assert.AreEqual(0, channel.msgs.Count);
            Assert.AreEqual(packageCount, result.Length);
            Assert.AreEqual("Microsoft.Quantum.Standard::0.0.0", result[0]);
            Assert.AreEqual("Microsoft.Quantum.Standard.Visualization::0.0.0", result[1]);

            // Try compiling TrotterEstimateEnergy, it should fail due to the lack
            // of chemistry package.
            response = await engine.ExecuteMundane(SNIPPETS.UseJordanWignerEncodingData, channel);

            PrintResult(response, channel);
            Assert.AreEqual(ExecuteStatus.Error, response.Status);

            response = await pkgMagic.Execute("mock.chemistry", channel);

            result = response.Output as string[];
            PrintResult(response, channel);
            Assert.AreEqual(ExecuteStatus.Ok, response.Status);
            Assert.AreEqual(0, channel.msgs.Count);
            Assert.IsNotNull(result);
            Assert.AreEqual(packageCount + 1, result.Length);

            // Now it should compile:
            await AssertCompile(engine, SNIPPETS.UseJordanWignerEncodingData, "UseJordanWignerEncodingData");
        }
コード例 #4
0
        private async Task AssertTrace(string name, ExecutionPath expectedPath, int expectedDepth)
        {
            var engine = await Init("Workspace.ExecutionPathTracer");

            var snippets = engine.Snippets as Snippets;

            Assert.IsNotNull(snippets);
            Assert.IsNotNull(engine.SymbolsResolver);
            var configSource = new ConfigurationSource(skipLoading: true);

            var wsMagic    = new WorkspaceMagic(snippets !.Workspace, new UnitTestLogger <WorkspaceMagic>());
            var pkgMagic   = new PackageMagic(snippets.GlobalReferences, new UnitTestLogger <PackageMagic>());
            var traceMagic = new TraceMagic(engine.SymbolsResolver !, configSource, new UnitTestLogger <TraceMagic>());

            var channel = new MockChannel();

            // Add dependencies:
            var response = await pkgMagic.Execute("mock.standard", channel);

            PrintResult(response, channel);
            response.AssertIsOk();

            // Reload workspace:
            response = await wsMagic.Execute("reload", channel);

            PrintResult(response, channel);
            response.AssertIsOk();

            response = await traceMagic.Execute(name, channel);

            PrintResult(response, channel);
            response.AssertIsOk();

            var message = channel.iopubMessages.ElementAtOrDefault(0);

            Assert.IsNotNull(message);
            Assert.AreEqual("render_execution_path", message.Header.MessageType);

            var content = message.Content as ExecutionPathVisualizerContent;

            Assert.IsNotNull(content);

            Assert.AreEqual(expectedDepth, content?.RenderDepth);

            var path = content?.ExecutionPath.ToObject <ExecutionPath>();

            Assert.IsNotNull(path);
            Assert.AreEqual(expectedPath.ToJson(), path !.ToJson());
        }
コード例 #5
0
        public void TestInvalidPackages()
        {
            var engine   = Init();
            var snippets = engine.Snippets as Snippets;
            var pkgMagic = new PackageMagic(snippets.GlobalReferences);
            var channel  = new MockChannel();

            var response = pkgMagic.Execute("microsoft.quantum", channel);
            var result   = response.Output as string[];

            PrintResult(response, channel);
            Assert.AreEqual(ExecuteStatus.Error, response.Status);
            Assert.AreEqual(1, channel.errors.Count);
            Assert.IsTrue(channel.errors[0].StartsWith("Unable to find package 'microsoft.quantum'"));
            Assert.IsNull(result);
        }