Ejemplo n.º 1
0
        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);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        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);
                }
            }
        }