Exemple #1
0
        private static void validateExecutionPath(INode node, ExecutionPath path,
                                                  ComputationContext ctx, ref ValidationData result)
        {
            bool first = true;

            foreach (IEvaluable step in path)
            {
                if (!first)
                {
                    ctx.ValAssignTracker?.ResetBranches();
                }

                if (!result.UnreachableCodeFound && result.IsTerminated)
                {
                    result.UnreachableCodeFound = true;
                    ctx.AddError(ErrorCode.UnreachableCode, step);
                }

                ValidationData val = Validated(step, ctx);

                result.AddStep(val);

                first = false;
            }
        }
        public void SwapTest()
        {
            var path   = GetExecutionPath("SwapCirc");
            var qubits = new QubitDeclaration[]
            {
                new QubitDeclaration(0),
                new QubitDeclaration(1),
            };
            var operations = new Operation[]
            {
                new Operation()
                {
                    Gate    = "SWAP",
                    Targets = new List <Register>()
                    {
                        new QubitRegister(0), new QubitRegister(1)
                    },
                    Children = ImmutableList <Operation> .Empty.AddRange(
                        new [] {
                        ControlledX(new int[] { 0 }, 1),
                        ControlledX(new int[] { 1 }, 0),
                        ControlledX(new int[] { 0 }, 1),
                    }
                        )
                },
            };
            var expected = new ExecutionPath(qubits, operations);

            AssertExecutionPathsEqual(expected, path);
        }
Exemple #3
0
        public void ExecuteWorkflow(ExecutionPath route, int expectedTransitions, string expectedPath)
        {
            // Arrange
            var workplan = WorkplanDummy.CreateFull();
            var engine   = Workflow.CreateEngine(workplan, new NullContext());

            // Act
            var triggerCount = 0;
            var path         = string.Empty;

            engine.TransitionTriggered += delegate(object sender, ITransition transition)
            {
                var dummy = (DummyTransition)transition;
                if (route == ExecutionPath.Alternative)
                {
                    dummy.ResultOutput = dummy.Outputs.Length - 1;
                }
                triggerCount++;
                path += "->" + dummy.Name;
            };
            engine.Completed += EngineCompleted;
            engine.Start();
            // Synchronus execution means we are done here
            Workflow.Destroy(engine);

            // Assert
            Assert.IsTrue(_completed);
            Assert.AreEqual(expectedTransitions, triggerCount, "Less transitions triggered than expected!");
            Assert.AreEqual(expectedPath, path, "Workflow engine did not take the correct path!");
        }
Exemple #4
0
        public void ValidateSplitOutputStructure()
        {
            var tester = new FunctionTester <PdfOperationsProvider>();
            FunctionDesigner designer = tester.CreateDesigner();

            Assert.IsNull(designer.Output);

            Property operation = designer.Properties[PropertyNames.Operation];

            operation.Value = Operation.Split;

            Property loopResults = designer.Properties[PropertyNames.SplitLoopResults];

            loopResults.Value = false;

            Assert.AreEqual(0, designer.ExecutionPaths.Count);

            IEnumerable <ITypeProperty> properties = designer.Output.GetProperties();

            Assert.AreEqual(2, properties.Count());
            properties.ElementAt(0).AssertList(OutputNames.PageFiles, typeof(string));
            properties.ElementAt(1).AssertCompiled(OutputNames.NumberOfPages, typeof(int));

            loopResults.Value = true;

            Assert.AreEqual(1, designer.ExecutionPaths.Count);
            ExecutionPath executionPath = designer.ExecutionPaths[0];

            Assert.AreEqual(ExecutionPathNames.PageFiles, executionPath.Name);
            Assert.AreEqual(TypeReference.Create(typeof(string)), executionPath.Output);

            properties = designer.Output.GetProperties();
            Assert.AreEqual(1, properties.Count());
            properties.ElementAt(0).AssertCompiled(OutputNames.NumberOfPages, typeof(int));
        }
Exemple #5
0
        private IFunctionData UpdateToVersion1(IFunctionData data)
        {
            bool isText = data.Properties[FileOpenShared.IsTextPropertyName].GetValue <bool>();
            var  updatedExecutionPath = new ExecutionPath
            {
                Key    = FileOpenShared.ExecutionPathName,
                Name   = FileOpenShared.ExecutionPathName,
                Output = TypeReference.CreateGeneratedType(
                    new TypeProperty(FileOpenShared.OutputFilePathPropertyName, typeof(string), AccessType.Read),
                    new TypeProperty(FileOpenShared.OutputFileHandlePropertyName, TypeReference.CreateResource(isText ? typeof(TextFileHandle) : typeof(BinaryFileHandle)), AccessType.Read)),
                IterationHint = Plugin.Common.IterationHint.Once
            };

            IExecutionPathData existingExecutionPath;

            if (data.TryFindExecutionPathByKey(FileOpenShared.ExecutionPathName, out existingExecutionPath))
            {
                data = data.ReplaceExecutionPath(existingExecutionPath, updatedExecutionPath);
            }
            else
            {
                data = data.AddExecutionPath(updatedExecutionPath);
            }

            return(data.UpdateVersion("1"));
        }
Exemple #6
0
        public void NestedTest()
        {
            var path       = GetExecutionPath("NestedCirc");
            var qubits     = new QubitDeclaration[] { new QubitDeclaration(0) };
            var operations = new Operation[]
            {
                new Operation()
                {
                    Gate    = "H",
                    Targets = new List <Register>()
                    {
                        new QubitRegister(0)
                    },
                },
                new Operation()
                {
                    Gate = "HCirc",
                },
                new Operation()
                {
                    Gate    = "Reset",
                    Targets = new List <Register>()
                    {
                        new QubitRegister(0)
                    },
                },
            };
            var expected = new ExecutionPath(qubits, operations);

            Assert.AreEqual(expected.ToJson(), path.ToJson());
        }
Exemple #7
0
            public Node Eval(ExecutionPath path)
            {
                Node output = null;

                while (Count + path.StackEvalDelta >= Array.Length)
                {
                    Resize(Array.Length * 2);
                }

                foreach (var node in path.EnterReturnNodes)
                {
                    if (node is EnterStateNode enterStateNode)
                    {
                        Array[Count++] = enterStateNode.SubGraph.Id;
                    }
                    else
                    {
                        output = _automataSubGraphRegistry[Array[--Count]].LeaveNode;
                    }
                }

                if (!(path.Output is ReturnStateNode))
                {
                    output = path.Output;
                }

                _hashCodeDirty = true;

                return(output);
            }
Exemple #8
0
        public void ControlledFooTest()
        {
            var path   = GetExecutionPath("ControlledFooCirc");
            var qubits = new QubitDeclaration[]
            {
                new QubitDeclaration(0),
                new QubitDeclaration(1),
            };
            var operations = new Operation[]
            {
                new Operation()
                {
                    Gate         = "Foo",
                    DisplayArgs  = "(2.1, (\"bar\"))",
                    IsControlled = true,
                    Controls     = new List <Register>()
                    {
                        new QubitRegister(0)
                    },
                    Targets = new List <Register>()
                    {
                        new QubitRegister(1)
                    },
                },
            };
            var expected = new ExecutionPath(qubits, operations);

            Assert.AreEqual(expected.ToJson(), path.ToJson());
        }
        public void SwapTest()
        {
            // NOTE: we only normalize the IsAdjoint property here, because
            //       self adjoint attribution is not propagated from the Q#
            //       AST to C# code generation.
            var path   = NormalizeAdjointness(GetExecutionPath("SwapCirc"));
            var qubits = new QubitDeclaration[]
            {
                new QubitDeclaration(0),
                new QubitDeclaration(1),
            };
            var operations = new Operation[]
            {
                new Operation()
                {
                    Gate    = "SWAP",
                    Targets = new List <Register>()
                    {
                        new QubitRegister(0), new QubitRegister(1)
                    },
                    Children = ImmutableList <Operation> .Empty.AddRange(
                        new [] {
                        ControlledX(new int[] { 0 }, 1),
                        ControlledX(new int[] { 1 }, 0),
                        ControlledX(new int[] { 0 }, 1),
                    }
                        )
                },
            };
            var expected = new ExecutionPath(qubits, operations);

            AssertExecutionPathsEqual(expected, path);
        }
        public void ControlledAdjointSTest()
        {
            var path   = GetExecutionPath("ControlledAdjointSCirc");
            var qubits = new QubitDeclaration[]
            {
                new QubitDeclaration(0, 1),
                new QubitDeclaration(1, 1),
            };
            var operations = new Operation[]
            {
                new Operation()
                {
                    Gate         = "S",
                    IsControlled = true,
                    IsAdjoint    = true,
                    Controls     = new List <Register>()
                    {
                        new QubitRegister(0)
                    },
                    Targets = new List <Register>()
                    {
                        new QubitRegister(1)
                    },
                },
                ResetAll(new int[] { 0, 1 }),
            };
            var expected = new ExecutionPath(qubits, operations);

            AssertExecutionPathsEqual(expected, path);
        }
Exemple #11
0
        public void MResetZTest()
        {
            var path   = GetExecutionPath("MResetZCirc");
            var qubits = new QubitDeclaration[]
            {
                new QubitDeclaration(0, 1),
            };
            var operations = new Operation[]
            {
                new Operation()
                {
                    Gate          = "MResetZ",
                    IsMeasurement = true,
                    Controls      = new List <Register>()
                    {
                        new QubitRegister(0)
                    },
                    Targets = new List <Register>()
                    {
                        new ClassicalRegister(0, 0)
                    },
                },
            };
            var expected = new ExecutionPath(qubits, operations);

            Assert.AreEqual(expected.ToJson(), path.ToJson());
        }
Exemple #12
0
            public void BuildExecutionGraph()
            {
                if (_executionGraphBuilt)
                {
                    return;
                }

                _executionGraphBuilt = true;

                //InitNode.EnsureReady();
                //EnterNode.SubGraph.Graph.BeginNode.EnsureReady();

                if (Graph.BeginNode.ReturnPath.IsInvalid)
                {
                    return;
                }

                EmptyPath = new ExecutionPath(InitNode, new[]
                {
                    EnterNode,
                    Graph.BeginNode,
                }.Concat(Graph.BeginNode.ReturnPath.Nodes).ToArray(), -1);

                Automata.RegisterExecutionPath(EmptyPath);
            }
Exemple #13
0
        public void RxTest()
        {
            var path   = GetExecutionPath("RxCirc");
            var qubits = new QubitDeclaration[]
            {
                new QubitDeclaration(0),
            };
            var operations = new Operation[]
            {
                new Operation()
                {
                    Gate        = "Rx",
                    DisplayArgs = "(2)",
                    Targets     = new List <Register>()
                    {
                        new QubitRegister(0)
                    },
                },
                new Operation()
                {
                    Gate    = "Reset",
                    Targets = new List <Register>()
                    {
                        new QubitRegister(0)
                    },
                },
            };
            var expected = new ExecutionPath(qubits, operations);

            Assert.AreEqual(expected.ToJson(), path.ToJson());
        }
Exemple #14
0
            private ForkNode BuildForkNode(int nodeIndex, ExecutionPath executionPath, PredicateResult predicateResult)
            {
                var forkNode = _processResources.ForkNodePool.Get().Mount(nodeIndex, executionPath, predicateResult);

                predicateResult.Dispose();

                return(forkNode);
            }
Exemple #15
0
            public void Eval(ExecutionPath executionPath)
            {
                var pointer = InstructionPointer;

                Node = Stack.Eval(executionPath);
                InstructionStream.Move(executionPath.LookAheadMatch.Length, ref pointer);
                InstructionPointer = pointer;
            }
 public void AssertExecutionPathsEqual(ExecutionPath expected, ExecutionPath actual)
 {
     // Step in one depth lower
     actual = new ExecutionPath(actual.Qubits, actual.Operations.First().Children?.ToList() ?? new List <Operation>());
     // Prune non-deterministic gates as it's difficult to test
     PruneNonDeterministicGates(actual.Operations);
     Assert.AreEqual(expected.ToJson(), actual.ToJson());
 }
        public void EmptyTest()
        {
            var path       = GetExecutionPath("EmptyCirc");
            var qubits     = new QubitDeclaration[] { };
            var operations = new Operation[] { };
            var expected   = new ExecutionPath(qubits, operations);

            AssertExecutionPathsEqual(expected, path);
        }
Exemple #18
0
 public static ExecutionPathResult ToDto(ExecutionPath execPath, ICollection <FlowNodeInstance> flowNodesInstances)
 {
     return(new ExecutionPathResult
     {
         Id = execPath.Id,
         CreateDateTime = execPath.CreateDateTime,
         ExecutionPointers = execPath.Pointers.Select(_ => ExecutionPointerResult.ToDto(_, flowNodesInstances)).ToList()
     });
 }
Exemple #19
0
 public static ExecutionPathModel ToModel(this ExecutionPath executionPath)
 {
     return(new ExecutionPathModel
     {
         Id = executionPath.Id,
         CreateDateTime = executionPath.CreateDateTime,
         Pointers = executionPath.Pointers.Select(_ => _.ToModel()).ToList()
     });
 }
Exemple #20
0
        internal static ExecutionPath GetExecutionPath(HttpContext context)
        {
            var obj = new ExecutionPath();

            obj.PathInfo = context.Request.PathInfo;
            obj.RawUrl   = context.Request.RawUrl.ToLower();
            //读取配置信息
            var config = ConfigurationManager.GetSection("UrlRewriterConfig") as UrlRewriterConfig;

            //遍历配置信息并使用正则匹配
            foreach (UrlRewriterItem item in config.Rules)
            {
                if (item.RewriteMode == RewriteMode.Remote)
                {
                    obj.RawUrl = context.Request.Url.ToString();
                }
                var mc = Regex.Match(obj.RawUrl, item.Pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                if (mc.Success)
                {
                    //将匹配后的参数保存到一个数组或列表中
                    List <string> args = new List <string>();
                    for (int i = 1; i < mc.Groups.Count; i++)
                    {
                        args.Add(mc.Groups[i].Value);
                    }
                    //格式化重写路径,替换相应位置的参数
                    obj.RedirectUrl = string.Format(item.Redirect, args.ToArray());
                    if (!string.IsNullOrEmpty(item.SendTo))
                    {
                        obj.RewritePathAndQuery = string.Format(item.SendTo, args.ToArray());
                    }
                    args = null;
                    break;
                }
            }
            string[] arr;
            if (!string.IsNullOrEmpty(obj.RewritePathAndQuery))
            {
                arr = obj.RewritePathAndQuery.Split('?');
            }
            else
            {
                arr = obj.RawUrl.Split('?');
            }

            if (arr.Length >= 1 || arr.Length == 0)
            {
                obj.RewritePath = arr[0];
            }

            if (arr.Length == 2)
            {
                obj.QueryString = arr[1];
            }
            return(obj);
        }
        public void ApplyToEachTest()
        {
            var path   = GetExecutionPath("ApplyToEachCirc");
            var qubits = new QubitDeclaration[]
            {
                new QubitDeclaration(0, 1),
                new QubitDeclaration(1, 1),
                new QubitDeclaration(2, 1),
            };
            var operations = new Operation[]
            {
                new Operation()
                {
                    Gate        = "ApplyToEach",
                    DisplayArgs = "(H)",
                    Targets     = new List <Register>()
                    {
                        new QubitRegister(0),
                        new QubitRegister(1),
                        new QubitRegister(2),
                    },
                    Children = ImmutableList <Operation> .Empty.AddRange(
                        new [] {
                        new Operation()
                        {
                            Gate    = "H",
                            Targets = new List <Register>()
                            {
                                new QubitRegister(0)
                            },
                        },
                        new Operation()
                        {
                            Gate    = "H",
                            Targets = new List <Register>()
                            {
                                new QubitRegister(1)
                            },
                        },
                        new Operation()
                        {
                            Gate    = "H",
                            Targets = new List <Register>()
                            {
                                new QubitRegister(2)
                            },
                        },
                    }
                        )
                },
                ResetAll(new int[] { 0, 1, 2 }),
            };
            var expected = new ExecutionPath(qubits, operations);

            AssertExecutionPathsEqual(expected, path);
        }
Exemple #22
0
            private void ExecuteParallel(ExecutionPath executionPath)
            {
                _currentThread.EnqueuePath(executionPath);

                if (executionPath.ForkPredicatePath)
                {
                    executionPath.AddReference();
                }

                _currentThread.CurrentNode = _executionMethods.GetExecutionPathMethod(executionPath).ExecuteParallel(this, _currentThread.Stack);
            }
Exemple #23
0
            private DfaNode CreateDfaNode(Node node, DfaTransition transition, ExecutionPath predicatePath)
            {
                var tuple = (node, transition, predicatePath);

                if (_dfaNodesDictionary.TryGetValue(tuple, out var dfaNode))
                {
                    return(dfaNode);
                }

                return(_dfaNodesDictionary[tuple] = new DfaNode(node, transition, predicatePath));
            }
        public void ForEachMeasureCirc()
        {
            var path   = GetExecutionPath("ForEachMeasureCirc");
            var qubits = new QubitDeclaration[]
            {
                new QubitDeclaration(0, 1),
                new QubitDeclaration(1, 1),
            };
            var operations = new Operation[]
            {
                new Operation()
                {
                    Gate        = "ForEach",
                    DisplayArgs = "(MResetZ)",
                    Targets     = new List <Register>()
                    {
                        new QubitRegister(0), new QubitRegister(1)
                    },
                    Children = ImmutableList <Operation> .Empty.AddRange(
                        new [] {
                        new Operation()
                        {
                            Gate          = "MResetZ",
                            IsMeasurement = true,
                            Controls      = new List <Register>()
                            {
                                new QubitRegister(0)
                            },
                            Targets = new List <Register>()
                            {
                                new ClassicalRegister(0, 0)
                            },
                        },
                        new Operation()
                        {
                            Gate          = "MResetZ",
                            IsMeasurement = true,
                            Controls      = new List <Register>()
                            {
                                new QubitRegister(1)
                            },
                            Targets = new List <Register>()
                            {
                                new ClassicalRegister(1, 0)
                            },
                        },
                    }
                        ),
                },
            };
            var expected = new ExecutionPath(qubits, operations);

            AssertExecutionPathsEqual(expected, path);
        }
Exemple #25
0
            public ForkNode Mount(int nodeIndex, ExecutionPath executionPath, PredicateResult predicateResult)
            {
                NodeIndex       = nodeIndex;
                ExecutionPath   = executionPath;
                PredicateResult = predicateResult;

                var sourceNode = ExecutionPath.Nodes[NodeIndex];

                CopyLookup(sourceNode);

                return(this);
            }
Exemple #26
0
            public EntryPointSubGraph(Automata <TInstruction, TOperand> automata, FiniteState state) : base(automata, state, null)
            {
                InitNode = new InitStateNode(automata, this);
                EndNode  = new EndStateNode(automata, this);

                InitNode.OutEdges.Add(new Edge(InitNode, EnterNode));
                LeaveNode.OutEdges.Add(new Edge(LeaveNode, EndNode));
                EndPath = new ExecutionPath(LeaveNode, new Node[] { EndNode }, -1);
                Automata.RegisterExecutionPath(EndPath);

                //EndNode.EnsureReady();
            }
Exemple #27
0
        public void CcnotTest()
        {
            var path   = GetExecutionPath("CcnotCirc");
            var qubits = new QubitDeclaration[]
            {
                new QubitDeclaration(0),
                new QubitDeclaration(1),
                new QubitDeclaration(2),
            };
            var operations = new Operation[]
            {
                new Operation()
                {
                    Gate         = "X",
                    IsControlled = true,
                    Controls     = new List <Register>()
                    {
                        new QubitRegister(0), new QubitRegister(2)
                    },
                    Targets = new List <Register>()
                    {
                        new QubitRegister(1)
                    },
                },
                new Operation()
                {
                    Gate    = "Reset",
                    Targets = new List <Register>()
                    {
                        new QubitRegister(0)
                    },
                },
                new Operation()
                {
                    Gate    = "Reset",
                    Targets = new List <Register>()
                    {
                        new QubitRegister(1)
                    },
                },
                new Operation()
                {
                    Gate    = "Reset",
                    Targets = new List <Register>()
                    {
                        new QubitRegister(2)
                    },
                },
            };
            var expected = new ExecutionPath(qubits, operations);

            Assert.AreEqual(expected.ToJson(), path.ToJson());
        }
        public void PartialOpTest()
        {
            var path   = GetExecutionPath("PartialOpCirc");
            var qubits = new QubitDeclaration[]
            {
                new QubitDeclaration(0, 1),
                new QubitDeclaration(1, 1),
                new QubitDeclaration(2, 1),
            };
            var operations = new Operation[]
            {
                new Operation()
                {
                    Gate         = "H",
                    IsControlled = true,
                    Controls     = new List <Register>()
                    {
                        new QubitRegister(0), new QubitRegister(1)
                    },
                    Targets = new List <Register>()
                    {
                        new QubitRegister(2)
                    },
                },
                new Operation()
                {
                    Gate        = "Ry",
                    DisplayArgs = "(2.5)",
                    Targets     = new List <Register>()
                    {
                        new QubitRegister(0)
                    },
                    Children = ImmutableList <Operation> .Empty.AddRange(
                        new [] {
                        new Operation()
                        {
                            Gate        = "R",
                            DisplayArgs = "(PauliY, 2.5)",
                            Targets     = new List <Register>()
                            {
                                new QubitRegister(0)
                            },
                        }
                    }
                        )
                },
                ResetAll(new int[] { 0, 1, 2 }),
            };
            var expected = new ExecutionPath(qubits, operations);

            AssertExecutionPathsEqual(expected, path);
        }
Exemple #29
0
        public void EmptyTest()
        {
            var path       = GetExecutionPath("EmptyCirc");
            var qubits     = new QubitDeclaration[] { };
            var operations = new Operation[] {
                new Operation()
                {
                    Gate = "EmptyCirc",
                },
            };
            var expected = new ExecutionPath(qubits, operations);

            Assert.AreEqual(expected.ToJson(), path.ToJson());
        }
Exemple #30
0
        public void ControlledXTest()
        {
            var path   = GetExecutionPath("ControlledXCirc");
            var qubits = new QubitDeclaration[]
            {
                new QubitDeclaration(0),
                new QubitDeclaration(1),
            };
            var operations = new Operation[]
            {
                new Operation()
                {
                    Gate         = "X",
                    IsControlled = true,
                    Controls     = new List <Register>()
                    {
                        new QubitRegister(0)
                    },
                    Targets = new List <Register>()
                    {
                        new QubitRegister(1)
                    },
                },
                new Operation()
                {
                    Gate    = "Reset",
                    Targets = new List <Register>()
                    {
                        new QubitRegister(0)
                    },
                },
                new Operation()
                {
                    Gate    = "Reset",
                    Targets = new List <Register>()
                    {
                        new QubitRegister(1)
                    },
                },
            };
            var expected = new ExecutionPath(qubits, operations);

            Assert.AreEqual(expected.ToJson(), path.ToJson());

            // JSON should be the same as CNOT's
            var path2 = GetExecutionPath("CnotCirc");

            Assert.AreEqual(path.ToJson(), path2.ToJson());
        }