/// <summary> /// Matrix ^ scalar /// </summary> /// <param name="value"></param> /// <returns></returns> public QsMatrix PowerScalar(QsScalar value) { QsMatrix Total = (QsMatrix)this.Identity; //first get the identity matrix of this matrix. int count = Qs.IntegerFromQsValue(value); if (count > 0) { for (int i = 1; i <= count; i++) { Total = Total.MultiplyMatrix(this); } } else if (count == 0) { return(Total); //which id identity already } else { count = Math.Abs(count); for (int i = 1; i <= count; i++) { Total = Total.MultiplyMatrix(this.Inverse); //multiply the inverses many times } } return(Total); }
protected internal Base(ConnectionStringSettings connectionStringSettings, string fileTable, string hashTable, bool debugMode) { _fileTable = new Regex(@"[^a-zA-Z0-9\._-]").Replace(fileTable.Trim(), ""); _hashTable = hashTable; qs = new Qs(connectionStringSettings); qs.DebugMode = debugMode; }
override public void initialize() { setProblemClass(); int total_nodes = this.Ranks.Length; maxcells = Convert.ToInt32(Math.Sqrt(total_nodes)); ncells = Convert.ToInt32(Math.Sqrt(total_nodes)); MAX_CELL_DIM = (problem_size / maxcells) + 1; IMAX = JMAX = KMAX = MAX_CELL_DIM; _grid_points_[0] = _grid_points_[1] = _grid_points_[2] = problem_size; IMAXP = IMAX / 2 * 2 + 1; JMAXP = JMAX / 2 * 2 + 1; set_constants(0, dt_default); U.initialize_field("u", maxcells, KMAX + 4, JMAXP + 4, IMAXP + 4, 5); Rhs.initialize_field("rhs", maxcells, KMAX + 2, JMAXP + 2, IMAXP + 2, 5); Lhs.initialize_field("lhs", maxcells, KMAX + 2, JMAXP + 2, IMAXP + 2, 15); Forcing.initialize_field("forcing", maxcells, KMAX + 2, JMAXP + 2, IMAXP + 2, 5); Us.initialize_field("us", maxcells, KMAX + 3, JMAX + 3, IMAX + 3); Vs.initialize_field("vs", maxcells, KMAX + 3, JMAX + 3, IMAX + 3); Ws.initialize_field("ws", maxcells, KMAX + 3, JMAX + 3, IMAX + 3); Qs.initialize_field("qs", maxcells, KMAX + 3, JMAX + 3, IMAX + 3); Ainv.initialize_field("ainv", maxcells, KMAX + 3, JMAX + 3, IMAX + 3); Rho.initialize_field("rho", maxcells, KMAX + 3, JMAX + 3, IMAX + 3); Speed.initialize_field("speed", maxcells, KMAX + 3, JMAX + 3, IMAX + 3); Square.initialize_field("square", maxcells, KMAX + 3, JMAX + 3, IMAX + 3); //Lhsa.initialize_field("lhsa", maxcells, KMAX+2, JMAXP+2, IMAXP+2, 15); //Lhsb.initialize_field("lhsb", maxcells, KMAX+2, JMAXP+2, IMAXP+2, 15); //Lhsc.initialize_field("lhsc", maxcells, KMAX+2, JMAXP+2, IMAXP+2, 15); }
public QsValue StdDeviation(int fromIndex, int toIndex) { var n = toIndex - fromIndex + 1; if (this.Parameters.Length > 0) { throw new QsException("Standard Deviation with symbolic quantities (I think you went so far ^_^ )", new NotImplementedException()); } else { FixIndices(ref fromIndex, ref toIndex); var mean = Average(fromIndex, toIndex); var Two = "2".ToScalarValue(); QsValue Total = (GetElementValue(fromIndex) - mean).PowerOperation(Two); for (int i = fromIndex + 1; i <= toIndex; i++) { var p = GetElementValue(i) - mean; var pp2 = p.PowerOperation(Two); Total = Total + pp2; } var count = new QsScalar { NumericalQuantity = Qs.ToQuantity((double)n) }; return(Total / count); } }
public override QsValue RightShiftOperation(QsValue times) { int itimes = Qs.IntegerFromQsValue((QsScalar)times); if (itimes > this.Text.Length) { itimes = itimes % this.Text.Length; } // 1 2 3 4 5 >> 2 == 4 5 1 2 3 StringBuilder vec = new StringBuilder(this.Text.Length); for (int i = this.Text.Length - itimes; i < this.Text.Length; i++) { vec.Append(this.Text[i]); } for (int i = 0; i < (this.Text.Length - itimes); i++) { vec.Append(this.Text[i]); } return(new QsText(vec.ToString())); }
public void ToStringTests() { Helper.RunWithMultipleSimulators((qsim) => { var _ = AbstractCallable._; var dump = qsim.Get <ICallable>(typeof(Microsoft.Quantum.Extensions.Diagnostics.DumpMachine <>)); var trace = qsim.Get <IUnitary>(typeof(Circuits.Generics.Trace <>)); var x = qsim.Get <Intrinsic.X>(); var q2 = new FreeQubit(2) as Qubit; var Q = new Q(q2); var Qs = new QArray <Qubit>(q2); var qs = new Qs(Qs); var udtOp = new U3(x); var udtQ = new Q(q2); var t1 = new QTuple <(long, Range, (Qubit, IUnitary))>((1L, new Range(10, -2, 4), (q2, x))); var t4 = new T4((3L, (1.1, false, Result.One))); var t5 = new T5((Pauli.PauliX, Qs, qs, Q)); var d_1 = dump.Partial(_); var d_2 = d_1.Partial(_); var x_1 = x.Partial(new Func <Qubit, Qubit>(q => q)); var x_2 = x_1.Partial(new Func <Qubit, Qubit>(q => q)); var x_3 = x.Partial <OperationPartial <Qubit, Qubit, QVoid> >(_); var t_1 = trace.Adjoint.Partial(_); var t_2 = t_1.Controlled.Partial(_); Assert.Equal("()", QVoid.Instance.ToString()); Assert.Equal("_", _.ToString()); Assert.Equal("U3(X)", udtOp.ToString()); Assert.Equal("q:2", q2.ToString()); Assert.Equal("Q(q:2)", udtQ.ToString()); Assert.Equal("(1, 10..-2..4, (q:2, X))", t1.ToString()); Assert.Equal("T4((3, (1.1, False, One)))", t4.ToString()); Assert.Equal("T5((PauliX, [q:2], Qs([q:2]), Q(q:2)))", t5.ToString()); Assert.Equal("X", x.ToString()); Assert.Equal("(Adjoint X)", x.Adjoint.ToString()); Assert.Equal("(Controlled X)", x.Controlled.ToString()); Assert.Equal("(Adjoint (Controlled X))", x.Controlled.Adjoint.ToString()); Assert.Equal("(Controlled (Adjoint X))", x.Adjoint.Controlled.ToString()); Assert.Equal("X{_}", x_1.ToString()); Assert.Equal("(Adjoint X{_})", x_1.Adjoint.ToString()); Assert.Equal("X{_}{_}", x_2.ToString()); Assert.Equal("X{_}", x_3.ToString()); Assert.Equal("DumpMachine", dump.ToString()); Assert.Equal("DumpMachine{_}", d_1.ToString()); Assert.Equal("DumpMachine{_}{_}", d_2.ToString()); Assert.Equal("Trace", trace.ToString()); Assert.Equal("(Adjoint Trace)", trace.Adjoint.ToString()); Assert.Equal("(Controlled Trace)", trace.Controlled.ToString()); Assert.Equal("(Adjoint (Controlled Trace))", trace.Controlled.Adjoint.ToString()); Assert.Equal("(Adjoint Trace){_}", t_1.ToString()); Assert.Equal("(Adjoint (Controlled (Adjoint Trace){_}){_})", t_2.Adjoint.ToString()); }); }
public QsValue Average(int fromIndex, int toIndex, QsValue arg0, QsValue arg1, QsValue arg2, QsValue arg3) { FixIndices(ref fromIndex, ref toIndex); var tot = SumElements(fromIndex, toIndex, arg0, arg1, arg2, arg3); var n = toIndex - fromIndex + 1; var count = new QsScalar { NumericalQuantity = Qs.ToQuantity((double)n) }; return(tot / count); }
/// <summary> /// QsValue ^x QsValue /// Power of multiple cross product operations /// </summary> /// <param name="value"></param> /// <returns></returns> virtual public QsValue PowerCrossOperation(QsValue value) { QsValue Total = this.Identity; int count = Qs.IntegerFromQsValue((QsScalar)value); for (int i = 1; i <= count; i++) { Total = Total.CrossProductOperation(this); } return(Total); }
public void CreateName() { if (Qs.Count > 0) { foreach (var q in Qs.OrderBy(o => o.Order)) { Name += $"{q.Q},"; } Name = Name.TrimEnd(','); Name = "{" + Name + "}"; } }
public SettingUp() { Zipfile = "twitterdata.zip"; //testdata file //CSVfile= "testdata.manual.2009.06.14.csv"; //real data - large file CSVfile = "training.1600000.processed.noemoticon.csv"; DBname = "TwitterTextDb"; CollectionName = "TweetDocs"; host = DetectDockerBridgeGateWay(); port = ":27017"; ConnectionString = "mongodb://" + host + port; client = new MongoClient(ConnectionString); mdb = client.GetDatabase(DBname); collection = mdb.GetCollection <TwitterData>(CollectionName); queries = new Qs(collection); }
public void QSharpTypeTests() { Helper.RunWithMultipleSimulators((qsim) => { var _ = AbstractCallable._; var x = qsim.Get <Intrinsic.X>(); var q2 = new FreeQubit(2) as Qubit; var Q = new Q(q2); var Qs = new QArray <Qubit>(q2); var qs = new Qs(Qs); var udtOp = new U3(x); var udtQ = new Q(q2); var t4 = new T4((3L, (1.1, false, Result.One))); var t5 = new T5((Pauli.PauliX, Qs, qs, Q)); var plain = qsim.Get <BPlain1>(); var adj = qsim.Get <BAdj1>(); var ctrl = qsim.Get <BCtrl1>(); var mapper = qsim.Get <Circuits.ClosedType.Map>(); Assert.Equal("()", typeof(QVoid).QSharpType()); Assert.Equal("_", _.GetType().QSharpType()); Assert.Equal("U3", udtOp.GetType().QSharpType()); Assert.Equal("Qubit => () : Adjoint, Controlled", udtOp.Data.GetType().QSharpType()); Assert.Equal("Qubit", q2.GetType().QSharpType()); Assert.Equal("Q", udtQ.GetType().QSharpType()); Assert.Equal("Qubit", udtQ.Data.GetType().QSharpType()); Assert.Equal("T4", t4.GetType().QSharpType()); Assert.Equal("(Int,(Double,Boolean,Result))", t4.Data.GetType().QSharpType()); Assert.Equal("T5", t5.GetType().QSharpType()); Assert.Equal("(Pauli,Qubit[],Qs,Q)", t5.Data.GetType().QSharpType()); Assert.Equal("Qubit => () : Adjoint, Controlled", x.GetType().QSharpType()); Assert.Equal("Qubit => () : Adjoint, Controlled", x.Adjoint.GetType().QSharpType()); Assert.Equal("(Qubit[],Qubit) => () : Adjoint, Controlled", x.Controlled.GetType().QSharpType()); Assert.Equal("(Qubit[],Qubit) => () : Adjoint, Controlled", x.Controlled.Adjoint.GetType().QSharpType()); Assert.Equal("(Int,Qubit,Callable) => ()", plain.GetType().QSharpType()); Assert.Equal("(Int,(Qubit,Qubit,Qubit[]),Adjointable) => () : Adjoint", adj.GetType().QSharpType()); Assert.Equal("(Int,Qs,Controllable) => () : Controlled", ctrl.GetType().QSharpType()); Assert.Equal("(Callable,Result[]) => String[]", mapper.GetType().QSharpType()); }); }
/// <summary> /// Take the average of the sequence. /// Corresponds To: S[i!!k] /// </summary> /// <param name="fromIndex"></param> /// <param name="toIndex"></param> /// <returns></returns> public QsValue Average(int fromIndex, int toIndex) { var n = toIndex - fromIndex + 1; if (this.Parameters.Length > 0) { // this is a call to form symbolic element // like g[n](x) ..> x^n // and calling g[0++2] // the output should be x^0 + x^1 + x^2 // and be parsed into function (QsFunction) string porma = string.Empty; // the parameters separated by comma ',' foreach (var prm in this.Parameters) { porma += prm.Name + ", "; } porma = porma.TrimEnd(',', ' '); string FunctionBody = "(" + JoinElementsWithOperation(fromIndex, toIndex, "+") + ")/" + n.ToString(CultureInfo.InvariantCulture); string FunctionDeclaration = "_(" + porma + ") = " + FunctionBody; QsFunction qs = QsFunction.ParseFunction(QsEvaluator.CurrentEvaluator, FunctionDeclaration); return(qs); } else { FixIndices(ref fromIndex, ref toIndex); var tot = SumElements(fromIndex, toIndex); var count = new QsScalar { NumericalQuantity = Qs.ToQuantity((double)n) }; return(tot / count); } }
public override QsValue LeftShiftOperation(QsValue vl) { QsValue times; if (vl is QsReference) { times = ((QsReference)vl).ContentValue; } else { times = vl; } int itimes = Qs.IntegerFromQsValue((QsScalar)times); int c = InitialValues.Count(); if (itimes > c) { itimes = itimes % c; } QsTupleValue[] NewValues = new QsTupleValue[c]; int ix = 0; for (int i = itimes; i < c; i++) { NewValues[ix] = InitialValues[i]; ix++; } for (int i = 0; i < itimes; i++) { NewValues[ix] = InitialValues[i]; ix++; } return(new QsFlowingTuple(NewValues)); }
public QsValue StdDeviation(int fromIndex, int toIndex, QsValue arg0, QsValue arg1, QsValue arg2) { var n = toIndex - fromIndex + 1; FixIndices(ref fromIndex, ref toIndex); var mean = Average(fromIndex, toIndex, arg0, arg1, arg2); var Two = "2".ToScalarValue(); QsValue Total = (GetElementValue(fromIndex, arg0, arg1, arg2) - mean).PowerOperation(Two); for (int i = fromIndex + 1; i <= toIndex; i++) { var p = GetElementValue(i, arg0, arg1, arg2) - mean; var pp2 = p.PowerOperation(Two); Total = Total + pp2; } var count = new QsScalar { NumericalQuantity = Qs.ToQuantity((double)n) }; return(Total / count); }
public override QsValue RightShiftOperation(QsValue vl) { QsValue times; if (vl is QsReference) { times = ((QsReference)vl).ContentValue; } else { times = vl; } int itimes = Qs.IntegerFromQsValue((QsScalar)times); if (itimes > this.Count) { itimes = itimes % this.Count; } // 1 2 3 4 5 >> 2 == 4 5 1 2 3 QsVector vec = new QsVector(this.Count); for (int i = this.Count - itimes; i < this.Count; i++) { vec.AddComponent(this[i]); } for (int i = 0; i < (this.Count - itimes); i++) { vec.AddComponent(this[i]); } return(vec); }