Exemplo n.º 1
0
        public void AllocateQubit2Test()
        {
            using (var sim = new QuantumSimulator())
            {
                try
                {
                    IgnorableAssert.Disable();
                    QVoid res = sim.Execute <AllocateQubit2, QVoid, QVoid>(QVoid.Instance);
                }
                catch (ExecutionFailException)
                {
                    StackFrame[] stackFrames = sim.CallStack;

                    // The following assumes that Assert is on Q# stack.
                    Assert.Equal(2, stackFrames.Length);

                    Assert.Equal("Microsoft.Quantum.Intrinsic.Assert", stackFrames[0].Callable.FullName);
                    Assert.Equal(namespacePrefix + "AllocateQubit2", stackFrames[1].Callable.FullName);

                    Assert.Equal(OperationFunctor.Body, stackFrames[0].Callable.Variant);
                    Assert.Equal(OperationFunctor.Body, stackFrames[1].Callable.Variant);

                    Assert.Equal(94, stackFrames[1].FailedLineNumber);
                }
                finally
                {
                    IgnorableAssert.Enable();
                }
            }
        }
Exemplo n.º 2
0
        public void AllocateQubit2Test()
        {
            using var sim = new QuantumSimulator();
            try
            {
                IgnorableAssert.Disable();
                QVoid res = sim.Execute <AllocateQubit2, QVoid, QVoid>(QVoid.Instance);
            }
            catch (ExecutionFailException)
            {
                var stackFrames = sim.CallStack;

                // Make sure that the call stack isn't null before proceeding.
                Assert.NotNull(stackFrames);

                // The following assumes that Assert is on Q# stack.
                Assert.Equal(2, stackFrames !.Length);

                Assert.Equal("Microsoft.Quantum.Diagnostics.AssertMeasurement", stackFrames[0].Callable.FullName);
                Assert.Equal(namespacePrefix + "AllocateQubit2", stackFrames[1].Callable.FullName);

                Assert.Equal(OperationFunctor.Body, stackFrames[0].Callable.Variant);
                Assert.Equal(OperationFunctor.Body, stackFrames[1].Callable.Variant);

                Assert.Equal(94, stackFrames[1].FailedLineNumber);
            }
            finally
            {
                IgnorableAssert.Enable();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Allocates numToAllocate new qubits.
        /// </summary>
        public virtual IQArray <Qubit> Allocate(long numToAllocate)
        {
            IgnorableAssert.Assert(numToAllocate >= 0, "Attempt to allocate negative number of qubits.");
            if (numToAllocate < 0)
            {
                throw new ArgumentException("Attempt to allocate negative number of qubits.");
            }
            else if (numToAllocate == 0)
            {
                return(QArray <Qubit> .Create(0));
            }

            QArray <Qubit> result = QArray <Qubit> .Create(numToAllocate);

            for (int i = 0; i < numToAllocate; i++)
            {
                result.Modify(i, AllocateOneQubit(usedOnlyForBorrowing: false));
                if (result[i] == null)
                {
                    for (int k = 0; k < i; k++)
                    {
                        Release(result[k]);
                    }
                    throw new NotEnoughQubits(numToAllocate, GetFreeQubitsCount());
                }
            }

            return(result);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Borrows a number of qubits. Chooses them from among already allocated ones.
        /// Makes sure borrowed qubits are not on the exclusion list.
        /// Exclusion list is supposed to contain qubits that can not be borrowed,
        /// for example those, that are being used (or could be used) in the current context.
        /// If there are not enough qubits to borrow, allocates new ones.
        /// </summary>
        public virtual IQArray <Qubit> Borrow(long numToBorrow, IEnumerable <Qubit> excludedQubitsSortedById) // Note, excluded could be an array of Ids for efficiency, if it is convenient for compiler.
        {
            IgnorableAssert.Assert(numToBorrow > 0, "Attempt to borrow zero qubits.");
            if (numToBorrow <= 0)
            {
                throw new ArgumentException("Attempt to borrow zero qubits.");
            }

            if (DisableBorrowing)
            {
                return(Allocate(numToBorrow));
            }

            var result = QArray <Qubit> .Create(numToBorrow);

            long numBorrowed = TryBorrow(numToBorrow, result, excludedQubitsSortedById);

            if (numBorrowed < numToBorrow)
            { // Not enough qubits to borrow. Allocate what was not borrowed.
                for (long i = numBorrowed; i < numToBorrow; i++)
                {
                    result.Modify(i, AllocateOneQubit(usedOnlyForBorrowing: true));
                    if (result[(int)i] == null)
                    {
                        for (long k = numBorrowed; k < i; k++)
                        {
                            ReleaseOneQubit(result[(int)k], usedOnlyForBorrowing: true);
                        }
                        throw new NotEnoughQubits(numToBorrow, numBorrowed + GetFreeQubitsCount());
                    }
                }
            }

            return(result);
        }
Exemplo n.º 5
0
        public void SimpleRange()
        {
            Helper.RunWithMultipleSimulators((s) =>
            {
                IgnorableAssert.Disable();
                try
                {
                    //We have to call the method in the same thread for IgnorableAssert.Disable() to work
                    s.Get <SimpleRangeTest>().Apply(QVoid.Instance);
                }
                finally
                {
                    IgnorableAssert.Enable();
                }

                var tracer = s.GetTracer <long>();

                Assert.Equal(0, tracer.Log.GetNumberOfCalls(OperationFunctor.Body, (0L)));
                Assert.Equal(0, tracer.Log.GetNumberOfCalls(OperationFunctor.Body, (1L)));
                Assert.Equal(0, tracer.Log.GetNumberOfCalls(OperationFunctor.Body, (2L)));
                Assert.Equal(0, tracer.Log.GetNumberOfCalls(OperationFunctor.Body, (3L)));
                Assert.Equal(1, tracer.Log.GetNumberOfCalls(OperationFunctor.Body, (4L)));
                Assert.Equal(0, tracer.Log.GetNumberOfCalls(OperationFunctor.Body, (5L)));
                Assert.Equal(1, tracer.Log.GetNumberOfCalls(OperationFunctor.Body, (6L)));
                Assert.Equal(0, tracer.Log.GetNumberOfCalls(OperationFunctor.Body, (7L)));
                Assert.Equal(1, tracer.Log.GetNumberOfCalls(OperationFunctor.Body, (8L)));
                Assert.Equal(0, tracer.Log.GetNumberOfCalls(OperationFunctor.Body, (9L)));
                Assert.Equal(1, tracer.Log.GetNumberOfCalls(OperationFunctor.Body, (10L)));
                Assert.Equal(0, tracer.Log.GetNumberOfCalls(OperationFunctor.Body, (11L)));
            });
        }
Exemplo n.º 6
0
        protected virtual void DisableOneQubit(Qubit qubit)
        {
            // Note: Borrowed qubits cannot be disabled.
            IgnorableAssert.Assert(qubits[qubit.Id] == Allocated, "Attempt to disable qubit that has not been allocated.");
            qubits[qubit.Id] = Disabled;
            numDisabledQubits++;

            numAllocatedQubits--;
            Debug.Assert(numAllocatedQubits >= 0);
        }
Exemplo n.º 7
0
        /// <summary>
        ///     Makes sure the angle for a rotation or exp operation is not NaN or Infinity.
        /// </summary>
        static void CheckAngle(double angle)
        {
            IgnorableAssert.Assert(!(double.IsNaN(angle) || double.IsInfinity(angle)), "Invalid angle for rotation/exponentiation.");

            if (double.IsNaN(angle))
            {
                throw new ArgumentOutOfRangeException("angle", "Angle can't be NaN.");
            }
            if (double.IsInfinity(angle))
            {
                throw new ArgumentOutOfRangeException("angle", "Angle can't be Infity.");
            }
        }
Exemplo n.º 8
0
        protected virtual void ReleaseOneQubit(Qubit qubit, bool usedOnlyForBorrowing)
        {
            long Occupied = (usedOnlyForBorrowing? AllocatedForBorrowing : Allocated);

            IgnorableAssert.Assert(qubits[qubit.Id] == Occupied, "Attempt to free qubit that has not been allocated.");
            if (qubits[qubit.Id] != Occupied)
            {
                throw new ArgumentException("Attempt to free qubit that has not been allocated.");
            }
            qubits[qubit.Id] = free;
            free             = qubit.Id;

            numAllocatedQubits--;
            Debug.Assert(numAllocatedQubits >= 0);
        }
Exemplo n.º 9
0
        protected virtual void ReleaseOneQubit(Qubit qubit, bool usedOnlyForBorrowing)
        {
            if (qubits[qubit.Id] == Disabled)
            {
                // Nothing to do. It will stay disabled.
                // Alternatively, we could mark is as None here.
            }
            else
            {
                long Occupied = (usedOnlyForBorrowing ? AllocatedForBorrowing : Allocated);
                IgnorableAssert.Assert(qubits[qubit.Id] == Occupied, "Attempt to free qubit that has not been allocated.");
                if (qubits[qubit.Id] != Occupied)
                {
                    throw new ArgumentException("Attempt to free qubit that has not been allocated.");
                }
                qubits[qubit.Id] = free;
                free             = qubit.Id;

                numAllocatedQubits--;
                Debug.Assert(numAllocatedQubits >= 0);
            }
        }