// register x - initially loaded with x // register b - initially loaded with 0 // after computation register b changes and contains: [(a*x) mod N] // Secure version: throws Exceptions if arguments are invalid public static void CMultModulo( this QuantumComputer comp, Register x, Register b, RegisterRef control, ulong valueA, ulong valueN) { if (comp.Group) { object[] parameters = new object[] { comp, x, b, control, valueA, valueN }; comp.AddParametricGate("CMultModulo", parameters); return; } else { comp.Group = true; } Validate(x, b, valueN); Register a = comp.NewRegister(0, x.Width - 1); Register c = comp.NewRegister(0, x.Width); Register N = comp.NewRegister(valueN, x.Width - 1); comp.CMultModulo(a, b, c, N, x, control, valueA, valueN); comp.DeleteRegister(ref N); comp.DeleteRegister(ref c); comp.DeleteRegister(ref a); }
public static void InverseAddModuloQFT(this QuantumComputer comp, ulong a, ulong N, RegisterRef ctrl, Register b, params RegisterRef[] controls) { Validate(a, b, N); comp.QFT(b); comp.InverseAddModuloQFTPhi(a, N, ctrl, b, controls); comp.InverseQFT(b); }
public static void Main() { QuantumComputer comp = QuantumComputer.GetInstance(); Register x = comp.NewRegister(0, 3); x.Toffoli(1, 2, 0); // Toffoli(<target_bit>, ... <control_bits> ...) }
public static void Main() { QuantumComputer comp = QuantumComputer.GetInstance(); int a = 25; int n = (int)Math.Ceiling(Math.Log(a, 2)); // create new register with initial value = 0, and width = 3 Register x = comp.NewRegister(1, n + 1); // Quantum solution for (int i = 0; i <= n; i++) { x.Hadamard(i); } for (int i = 1, tmp = a; i <= n; i++) { if (tmp % 2 == 1) { x.CNot(target: 0, control: i); } tmp /= 2; } // Quantum solution for (int i = 0; i <= n; i++) { x.Hadamard(i); } }
// register X - initially loaded with x // register X_1 - initially loaded with 1 // width(x1) = W + 1 // width(X) is 2 * W // after computation register X_1 changes and contains: [(a^x) mod N] // Secure version: throws Exceptions if arguments are invalid public static void ExpModulo( this QuantumComputer comp, Register x, Register x1, int valueA, int valueN) { if (comp.Group) { object[] parameters = new object[] { comp, x, x1, valueA, valueN }; comp.AddParametricGate("ExpModulo", parameters); return; } else { comp.Group = true; } Validate(x, x1, valueN); Register a = comp.NewRegister(0, x1.Width - 1); Register b = comp.NewRegister(0, x1.Width); Register c = comp.NewRegister(0, x1.Width); Register N = comp.NewRegister((ulong)valueN, x1.Width - 1); comp.ExpModulo(a, b, c, N, x1, x, valueA, valueN); comp.DeleteRegister(ref N); comp.DeleteRegister(ref c); comp.DeleteRegister(ref b); comp.DeleteRegister(ref a); }
public static void InverseAddQFTPhi(this QuantumComputer comp, ulong a, Register b, params RegisterRef[] controls) { bool[] aBin = Utils.getBinaryRepresentation(a, b.Width); //for (int j = 0; j < b.Width; j++) //{ // for (int i = 0; i <= j; i++) // { // //Console.WriteLine("InverseAdd i = {2}, N = {1}, a = {0}", a, j, i); // if (aBin[i]) // { // //HACK przerobić na PhaseKick (odwrotny argument!!!) // comp.InverseCPhaseShift(Math.Abs(j - i), b[j], controls); // } // } //} for (int i = 0; i < b.Width; i++) { double exp = 0.0; for (int j = i; j >= 0; j--) { if (aBin[j]) { exp += (double)1 / ((double)(1 << (i - j))); } } exp *= ((double)-1) * Math.PI; comp.PhaseKick(exp, b[i], controls); } }
public static void InverseAddQFT(this QuantumComputer comp, Register a, Register b, params RegisterRef[] controls) { Validate(a, b); comp.QFT(b); comp.InverseAddQFTPhi(a, b, controls); comp.InverseQFT(b); }
public static void Main() { QuantumComputer comp = QuantumComputer.GetInstance(); Register x = comp.NewRegister(0, 3); x.CNot(target: 2, control: 0); }
public static void Main() { QuantumComputer comp = QuantumComputer.GetInstance(); // Eingabe, der Pfad als Liste von Knoten (Knoten s und z können weggelassen werden, da sie immer im Pfad enthalten sein müssen). // // Knoten werden als 2 Bit Zahlen kodiert: // * a = |0000> // * b = |0001> // * c = |0010> // * d = |0011> // * e = |0100> // * f = |0101> // * g = |0110> // * h = |0111> // * i = |1000> // * j = |1001> // * k = |1010> // * l = |1011> // // x1x0 = Erster Knoten des Pfades // x3x2 = Zweiter Knoten des Pfades int anzahlKnoten = 3; int anzahlBitProKnoten = 4; int anzahlXBit = anzahlKnoten * anzahlBitProKnoten; Register x = comp.NewRegister(0, anzahlXBit); // Ausgabe von Uf Register y = comp.NewRegister(1, 1); comp.Walsh(x); comp.Hadamard(y); comp.Uf(x, y); comp.D2(x); }
/// <summary> /// Applies the Walsh-Hadamard transform on given register. /// In other words, applies Hadamard gate on every qubit in the register. /// </summary> /// <param name="comp">The <see cref="Quantum.QuantumComputer"/> instance.</param> /// <param name="register">The <see cref="Quantum.Register"/> on which the operation is performed.</param> public static void Walsh(this QuantumComputer comp, Register register) { for (int i = 0; i < register.Width; i++) { register.Hadamard(i); } }
// controlled loading a number into register // using Toffoli gates // if controlBits are empty, the number is loaded unconditionally public static void LoadNumber(this QuantumComputer comp, Register target, ulong number, params RegisterRef[] controlBits) { Validate(target, number); int controlLength = controlBits.Length; int i = 0; ulong tmpN = number; while (tmpN > 0) { int rest = (int)(tmpN % 2); tmpN = tmpN / 2; if (rest == 1) { if (controlLength > 1) { comp.Toffoli(target[i], controlBits); } else if (controlLength > 0) { comp.CNot(target[i], controlBits[0]); } else { target.SigmaX(i); } } i++; } }
public static void InverseControlledUaGate(this QuantumComputer comp, ulong a, ulong N, RegisterRef ctrl, Register x, Register reg0, RegisterRef control) { if (comp.Group) { object[] parameters = new object[] { comp, a, N, ctrl, x, reg0, control }; comp.AddParametricGate("InverseControlledUaGate", parameters); return; } else { comp.Group = true; } Validate(x, N); int?invA = Quantum.Utils.InversionModulo((int)a, (int)N); if (invA == null) { throw new ArgumentException("No inversion for specified a"); } comp.MultModuloQFT((ulong)invA, N, ctrl, x, reg0, control); comp.Swap(reg0, x, control); comp.InverseMultModuloQFT(a, N, ctrl, x, reg0, control); }
public static void Reverse(this QuantumComputer comp, Register a) { for (int i = 0; i < a.Width / 2; i++) { comp.Swap(a[i], a[a.Width - 1 - i]); } }
// InverseAdd(a, a+b, 0) -> (a, b, 0) public static void InverseAdd(this QuantumComputer comp, Register a, Register b, Register c) { if (comp.Group) { object[] parameters = new object[] { comp, a, b, c }; comp.AddParametricGate("InverseAdd", parameters); return; } else { comp.Group = true; } int width = a.Width; int i = 0; for (; i < width - 1; i++) { comp.Sum(c[i], a[i], b[i]); comp.Carry(c[i], a[i], b[i], c[i + 1]); } comp.Sum(c[i], a[i], b[i]); comp.CNot(b[i], a[i]); comp.InverseCarry(c[i], a[i], b[i], b[i + 1]); i--; for (; i >= 0; i--) { comp.InverseCarry(c[i], a[i], b[i], c[i + 1]); } }
// register A - initially loaded with a // register B - initially loaded with b // after computation in register B: (a+b) mod N // using scratch registers inside // register B must be exactly one bit wider than A to store carry bit // Secure version: throws Exceptions if arguments are invalid public static void AddModulo( this QuantumComputer comp, Register a, Register b, ulong valueN) { if (comp.Group) { object[] parameters = new object[] { comp, a, b, valueN }; comp.AddParametricGate("AddModulo", parameters); return; } else { comp.Group = true; } Validate(a, b, valueN); Register c = comp.NewRegister(0, a.Width + 1); Register N = comp.NewRegister(valueN, a.Width); comp.AddModulo(a, b, c, N, valueN); comp.DeleteRegister(ref N); comp.DeleteRegister(ref c); }
public void Initialize() { this.width = Utils.CalculateRegisterWidth((ulong)N); L = 2 * width; this.comp = QuantumComputer.GetInstance(); Register regTemp = comp.NewRegister(1, 4 * width + 2, (int)(Math.Pow(2, 2 * (width + 1)))); this.regX = regTemp[0, width]; this.reg0 = regTemp[width, width + 1]; this.regC = regTemp[2 * width + 1, 2 * width]; this.ctrl = regTemp[4 * width + 1, 1]; //this.regX = comp.NewRegister(1, width); //this.regC = comp.NewRegister(0, 1); //this.ctrl = comp.NewRegister(0, 1); //this.reg0 = comp.NewRegister(0, width + 1); //this.reg0 = comp.NewRegister(0, width); //this.reg0 = comp.NewRegister(0, width + 1, (int)(Math.Pow(2, 2 * (width + 1)))); result = new byte[L]; expTab = new ulong[L]; }
public static void Main() { int number = 5; int width = 4; // width of search space double iterations = Math.PI / 4 * Math.Sqrt(1 << width); QuantumComputer comp = QuantumComputer.GetInstance(); // input register set to 0 Register x = comp.NewRegister(0, width); // output 1-qubit-register, set to 1 Register y = comp.NewRegister(1, 1); comp.Walsh(x); comp.Hadamard(y[0]); Console.WriteLine("Iterations needed: PI/4 * Sqrt(2^n) = {0}", iterations); for (int i = 1; i <= iterations; i++) { Console.WriteLine("Iteration #{0}", i); comp.Grover(number, x, y); } }
public static int ExpModulo(int a, int x, int N) { // obliczamy ile bitow potrzeba na zapamiętanie N ulong ulongN = (ulong)N; int width = (int)Math.Ceiling(Math.Log(N, 2)); // inicjalizujemy komputer kwantowy QuantumComputer comp = QuantumComputer.GetInstance(); //inicjalizujemy rejestr wejsciowy Register regX = comp.NewRegister(0, 2 * width); // inicjalizujemy rejestr wyjsciowy Register regY = comp.NewRegister(1, width + 1); // ustawiamy wartosc rejestru wejsciowego na x regX.Reset((ulong)x); // ustawiamy wartosc rejestru wyjsciowego na 1 // potrzebne, gdy wywolujemy w petli regY.Reset(1); // obliczamy a^x mod N comp.ExpModulo(regX, regY, a, N); //mierzymy wartosc return((int)regY.Measure()); }
public static int CalculateModuloExponentiation(int N, int a, ulong x) { //a^x mod N // obliczamy ile bitow potrzeba na zapamiętanie N ulong ulongN = (ulong)N; int width = (int)Math.Ceiling(Math.Log(N, 2)); QuantumComputer comp = QuantumComputer.GetInstance(); Register regX = comp.NewRegister(0, 2 * width); Register regY = comp.NewRegister(1, width + 1); regX.Reset(x); regY.Reset(1); // obliczamy a^x mod N comp.ExpModulo(regX, regY, a, N); int valueMeasured = (int)regY.Measure(); Console.WriteLine("Dla {0} reszta to {1}", x, valueMeasured); return(valueMeasured); }
public static void Swap(this QuantumComputer comp, Register r1, Register r2, RegisterRef control) { if (comp.Group) { object[] parameters = new object[] { comp, r1, r2, control }; comp.AddParametricGate("Swap", parameters); return; } else { comp.Group = true; } Validate(r1, r2); Register root = comp.GetRootRegister(r1, r2, control); int target1 = r1.OffsetToRoot; int target2 = r2.OffsetToRoot; int ctrl = control.OffsetToRoot; for (int i = 0; i < r1.Width; i++) { //comp.Toffoli(root[target1 + i], root[target2 + i], root[ctrl]); //comp.Toffoli(root[target2 + i], root[target1 + i], root[ctrl]); //comp.Toffoli(root[target1 + i], root[target2 + i], root[ctrl]); root.Toffoli(target1 + i, target2 + i, ctrl); root.Toffoli(target2 + i, target1 + i, ctrl); root.Toffoli(target1 + i, target2 + i, ctrl); } }
// Inversion is working on n-width register public static void Inversion(this QuantumComputer comp, Register x) { for (int i = 0; i < x.Width; i++) { x.Hadamard(i); x.SigmaX(i); } x.Hadamard(0); if (x.Width == 2) { x.CNot(target: 0, control: 1); } else { int[] controlBits = Enumerable.Range(1, x.Width - 1).ToArray(); x.Toffoli(0, controlBits); // Toffoli(<target_bit>, ... <control_bits> ...) } x.Hadamard(0); for (int i = 0; i < x.Width; i++) { x.SigmaX(i); x.Hadamard(i); } }
public static void Main() { QuantumComputer comp = QuantumComputer.GetInstance(); Register x = comp.NewRegister(0, 3); //Qbit 2 is one we want to teleport //Qbits 1, 2 are helpers //First we entangle Qbits 0 and 1 //Qbit 1 is hold by Alice //Qbit 0 is send to Bob x.Hadamard(1); x.CNot(target: 0, control: 1); //Now we applay some transormations //Becouse qbits 0 and 1 are entangled it affects also qbit 1 x.CNot(target: 1, control: 2); x.Hadamard(2); //After this manipulation Bob may have teleported state //Or may need to do some additional transormation. Measurment will say x.Measure(1); x.Measure(2); //Controlled gates allows to do appropiate manipulation if required x.CNot(target: 0, control: 1); x.SigmaZ(0, 2); //Bob has finally his teleported state }
public static void InverseAddQFTPhi(this QuantumComputer comp, Register a, Register b, params RegisterRef[] controls) { if (comp.Group) { object[] parameters = new object[] { comp, a, b, controls }; comp.AddParametricGate("InverseAddQFTPhi", parameters); return; } else { comp.Group = true; } Validate(a, b); for (int j = 0; j < b.Width; j++) { for (int i = 0; i <= j; i++) { List <RegisterRef> list = controls.ToList <RegisterRef>(); list.Add(a[i]); RegisterRef[] controls2 = list.ToArray(); comp.InverseCPhaseShift(Math.Abs(j - i), b[j], controls2); } } }
public static void Main() { QuantumComputer comp = QuantumComputer.GetInstance(); Register x = comp.NewRegister(0, 4); //Qbits 1,2,3 are input for Uf //Qbit 0 is output of Uf x.SigmaX(0); x.Hadamard(0); x.Hadamard(1); x.Hadamard(2); x.Hadamard(3); //This is misterious Uf x.CNot(target: 0, control: 3); x.CNot(target: 0, control: 1); //end of Uf x.Hadamard(0); x.Hadamard(1); x.Hadamard(2); x.Hadamard(3); //Now qbits 1, 2, 3 descirbes "okres" modulo 2 }
public static void AddQFTPhi(this QuantumComputer comp, ulong a, Register b, params RegisterRef[] controls) { if (comp.Group) { object[] parameters = new object[] { comp, a, b, controls }; comp.AddParametricGate("AddQFTPhi", parameters); return; } else { comp.Group = true; } bool[] aBin = Quantum.Utils.getBinaryRepresentation(a, b.Width); for (int j = b.Width - 1; j >= 0; j--) { //comp.ClassicalCPhaseShift(b[j], aBin[j], b.Width - j, controls); for (int i = j; i >= 0; i--) { if (aBin[i]) { comp.CPhaseShift(Math.Abs(j - i), b[j], controls); } } } }
public static void InverseAddQFTPhi(this QuantumComputer comp, ulong a, Register b, params RegisterRef[] controls) { if (comp.Group) { object[] parameters = new object[] { comp, a, b, controls }; comp.AddParametricGate("InverseAddQFTPhi", parameters); return; } else { comp.Group = true; } bool[] aBin = Quantum.Utils.getBinaryRepresentation(a, b.Width); for (int j = 0; j < b.Width; j++) { for (int i = 0; i <= j; i++) { if (aBin[i]) { comp.InverseCPhaseShift(Math.Abs(j - i), b[j], controls); } } } }
// Insecure version: registers widths etc. are not checked public static void InverseAddModulo( this QuantumComputer comp, Register a, Register b, Register c, Register N, ulong valueN) { RegisterRef carry = b[b.Width - 1]; RegisterRef overflow = c[c.Width - 1]; comp.InverseAdd(a, b, c); comp.CNot(overflow, carry); comp.Add(a, b, c); //resetting N: comp.LoadNumber(N, valueN, overflow); comp.InverseAdd(N, b, c); //setting N back: comp.LoadNumber(N, valueN, overflow); comp.SigmaX(carry); comp.CNot(overflow, carry); comp.SigmaX(carry); comp.Add(N, b, c); comp.InverseAdd(a, b, c); }
// register A - initially loaded with a // register B - initially loaded with b // register C - initially 0, exactly one bit wider than A, stores overflow bit // register N - initially loaded with N // after computation in register B: (a+b) mod N // other registers dont change their states // register B must be exactly one bit wider than A to store carry bit // register B must be exactly one bit wider than N to store carry bit // registers A, N must be the same length // Insecure version: registers widths etc. are not checked public static void AddModulo( this QuantumComputer comp, Register a, Register b, Register c, Register N, ulong valueN) { RegisterRef carry = b[b.Width - 1]; RegisterRef overflow = c[c.Width - 1]; comp.Add(a, b, c); comp.InverseAdd(N, b, c); comp.SigmaX(carry); comp.CNot(overflow, carry); comp.SigmaX(carry); //resetting N comp.LoadNumber(N, valueN, overflow); comp.Add(N, b, c); // now we have [(a+b) mod N] in B register // next steps lead to recover the initial state of registers N and overflow bit //setting N back comp.LoadNumber(N, valueN, overflow); comp.InverseAdd(a, b, c); comp.CNot(overflow, carry); comp.Add(a, b, c); }
public static void Main() { QuantumComputer comp = QuantumComputer.GetInstance(); Register x = comp.NewRegister(0, 2); x.SigmaX(0); x.SigmaX(1); x.Hadamard(0); x.Hadamard(1); // f_00 // // f_01 // x.CNot(target: 0, control: 1); // f_10 // x.SigmaX(0); // x.CNot(target: 0, control: 1); // f_11 // x.SigmaX(0); x.Hadamard(1); }
public static void InverseControlledUaGate(this QuantumComputer comp, ulong a, ulong N, Register x, RegisterRef control) { Register ctrl = comp.NewRegister(0, 1); Register reg0 = comp.NewRegister(0, x.Width + 1); comp.InverseControlledUaGate(a, N, ctrl, x, reg0, control); }
public void Dispose() { comp.DeleteRegister(ref reg0); comp.DeleteRegister(ref regX); comp.DeleteRegister(ref regC); comp.DeleteRegister(ref ctrl); comp = null; }
public void Dispose() { comp.DeleteRegister(ref regX); comp.DeleteRegister(ref regX1); comp.DeleteRegister(ref rega); comp.DeleteRegister(ref regb); comp.DeleteRegister(ref regc); comp.DeleteRegister(ref regN); comp = null; }
private CircuitEvaluator() { _comp = QuantumComputer.GetInstance(); }
public void Initialize() { this.width = Utils.CalculateRegisterWidth((ulong)N); this.kMax = Math.Log(2 * width, 2); this.comp = QuantumComputer.GetInstance(); regX = comp.NewRegister(0, 2 * width/*, (int)(Math.Pow(2, 2 * width))*/); regX1 = comp.NewRegister(1, width + 1); rega = comp.NewRegister(0, regX1.Width - 1); regb = comp.NewRegister(0, regX1.Width); regc = comp.NewRegister(0, regX1.Width); regN = comp.NewRegister((ulong)N, regX1.Width - 1); //result = new byte[width]; //expTab = new ulong[width]; }