public static long Calculate(long x, long n) { int l = n.BitsCeiling(); int t = OrderFindingTransform.GetPercision(n); var regs = OrderFindingTransform.Registers(t, l).ToArray(); IUnitaryTransform orderfinder = OrderFindingTransform.Get(x, n, t); var regTwo = MultiQubit.BasisVector(1, l); var regOne = new MultiQubit(Enumerable.Range(0, t).Select(i => Qubit.ClassicZero).ToArray()); var input = new MultiQubit(regOne, regTwo); long denom = (long)Math.Pow(2, t); while (true) { QuantumSim sim = new QuantumSim(orderfinder, regs); IDictionary <Register, long> res = sim.Simulate(input); long regValue = res.First().Value; // can't do anything if we get zero, cant reduce if (regValue == 0) { continue; } foreach (var(_, rCandidate) in FractionHelpers.GetContinuedFractionSequence(regValue, denom)) { if ((long)Math.Pow(x, rCandidate) % n == 1) { return(rCandidate); } } } }
public void PhaseHadamarTest() { long x = 2; long n = 3; int l = n.BitsCeiling(); int t = l;// OrderFindingTransform.GetPercision(n); var regs = OrderFindingTransform.Registers(t, l).ToArray(); IUnitaryTransform orderfinder = PhaseEstimator.GetPhaseHadamar(t, l); var regTwo = MultiQubit.BasisVector(1, l); var regOne = new MultiQubit(Enumerable.Range(0, t).Select(i => Qubit.ClassicZero).ToArray()); var input = new MultiQubit(regOne, regTwo); IQuantumState res = orderfinder.Transform(input); string inputStr = input.Print(regs); inputStr.Should().Be("+1.00|0>|1>"); string outStr = res.Print(regs); // first reg is unchanged, second reg is maximally mixed outStr.Should().Be("+0.50|0>|1>+0.50|1>|1>+0.50|2>|1>+0.50|3>|1>"); }
public void OrderFindTest() { long x = 2; long n = 3; int r = 2; // 2 ^ 2 = 3 + 1 int l = n.BitsCeiling(); int t = OrderFindingTransform.GetPercision(n); var regs = OrderFindingTransform.Registers(t, l).ToArray(); IUnitaryTransform orderfinder = OrderFindingTransform.Get(x, n, t); var regTwo = MultiQubit.BasisVector(1, l); var regOne = new MultiQubit(Enumerable.Range(0, t).Select(i => Qubit.ClassicZero).ToArray()); var input = new MultiQubit(regOne, regTwo); IQuantumState res = orderfinder.Transform(input); string inputStr = input.Print(regs); inputStr.Should().Be("+1.00|0>|1>"); string outStr = res.Print(regs); // all first register options evenly divide 2^t and reduce to fractiosn // with a denominator of r = 2 // in this case 0 and 32 over 2^6 equal 0 and 1/2 // therefore answer is 2 outStr.Should().Be("+0.50|0>|1>+0.50|0>|2>+0.50|32>|1>+-0.50|32>|2>"); }
public void OrderStartTest() { long x = 2; long n = 3; int l = n.BitsCeiling(); int t = l;// OrderFindingTransform.GetPercision(n); var regs = OrderFindingTransform.Registers(t, l).ToArray(); IUnitaryTransform orderfinder = OrderFindingTransform.GetStart(x, n, t); var regTwo = MultiQubit.BasisVector(1, l); var regOne = new MultiQubit(Enumerable.Range(0, t).Select(i => Qubit.ClassicZero).ToArray()); var input = new MultiQubit(regOne, regTwo); IQuantumState res = orderfinder.Transform(input); string inputStr = input.Print(regs); inputStr.Should().Be("+1.00|0>|1>"); string outStr = res.Print(regs); outStr.Should().Be("+0.50|0>|1>+0.50|1>|2>+0.50|2>|1>+0.50|3>|2>"); }
public void TensorPrintTest() { var state = MultiQubit.BasisVector(14, 8); var stateOther = MultiQubit.BasisVector(5, 4); var compState = new MultiQubit(state, stateOther); compState.Print(new[] { new Register { QubitIndexes = new[] { 1, 2, 3, 4, 5, 6, 7 } }, new Register { QubitIndexes = new[] { 8, 9, 10, 11 } } }).Should().Be("+1.00|14>|5>"); }
public static long Find(bool[] blackBoxFunc) { long numStates = blackBoxFunc.LongLength; int numQubits = numStates.BitsCeiling(); var input = new MultiQubit(Enumerable.Range(0, numQubits).Select(i => Qubit.ClassicZero).ToArray()); var reg = new QuantumStateExt.Register { QubitIndexes = Enumerable.Range(0, numQubits) }; var grover = GetGroverTransform(blackBoxFunc); var sim = new QuantumSim(grover, reg); while (true) { long res = sim.Simulate(input)[reg]; if (blackBoxFunc[res]) { return(res); } } }
public void OrderFindSimTest() { long x = 2; long n = 3; int r = 2; // 2 ^ 2 = 3 + 1 int l = n.BitsCeiling(); int t = OrderFindingTransform.GetPercision(n); var regs = OrderFindingTransform.Registers(t, l).ToArray(); IUnitaryTransform orderfinder = OrderFindingTransform.Get(x, n, t); var regTwo = MultiQubit.BasisVector(1, l); var regOne = new MultiQubit(Enumerable.Range(0, t).Select(i => Qubit.ClassicZero).ToArray()); var input = new MultiQubit(regOne, regTwo); var sim = new QuantumSim(orderfinder, regs); var res = sim.Simulate(input); res.First().Value.Should().BeOneOf(0, 32); }