// Add(a, b, 0) -> (a, a+b, 0) // Registers a, b and c must not overlap // Registers a and b have the same width // Register c is used for storing carries and must be minimum one bit wider than register a (or b) // Initial value of c must be 0 public static void Add(this QuantumComputer comp, Register a, Register b, Register c) { if (comp.Group) { object[] parameters = new object[] { comp, a, b, c }; comp.AddParametricGate("Add", parameters); return; } else { comp.Group = true; } int width = a.Width; int i = 0; for (; i < width - 1; i++) { comp.Carry(c[i], a[i], b[i], c[i + 1]); } comp.Carry(c[i], a[i], b[i], b[i + 1]); comp.CNot(b[i], a[i]); comp.Sum(c[i], a[i], b[i]); i--; for (; i >= 0; i--) { comp.InverseCarry(c[i], a[i], b[i], c[i + 1]); comp.Sum(c[i], a[i], b[i]); } }
/// <summary> /// <para> /// Adds two registers. The result is stored in the second. An extra register is needed for storing carry bits. /// </para> /// <para> /// Add(a, b, 0) -> (a, a+b, 0) /// </para> /// <para> /// In order to improve performance, this method do not check if arguments are valid. /// They must satisfy following conditions: /// <list type="bullet"> /// <item>Registers a, b and c must not overlap</item> /// <item>Registers a and c must have the same width</item> /// <item>Register b must be exactly one bit wider than register a (or c)</item> /// <item>Initial value of c must be 0</item> /// </list> /// </para> /// </summary> /// <param name="comp">The QuantumComputer instance.</param> /// <param name="a">The first register to sum. Its value remains unchanged.</param> /// <param name="b">The second register to sum. After performing this operation, it contains the sum result.</param> /// <param name="c">The extra register for storing carry bits.</param> public static void Add(this QuantumComputer comp, Register a, Register b, Register c) { int width = a.Width; int i = 0; for (; i < width - 1; i++) { comp.Carry(c[i], a[i], b[i], c[i + 1]); } comp.Carry(c[i], a[i], b[i], b[i + 1]); comp.CNot(b[i], a[i]); comp.Sum(c[i], a[i], b[i]); i--; for (; i >= 0; i--) { comp.InverseCarry(c[i], a[i], b[i], c[i + 1]); comp.Sum(c[i], a[i], b[i]); } }