Esempio n. 1
0
        public void CreateGenericControlledOperation()
        {
            Helper.RunWithMultipleSimulators((s) =>
            {
                var gen0 = new GenericCallable(s, typeof(Gen0 <>));

                Assert.Same(typeof(ControlledOperation <long, QVoid>), gen0.Controlled.FindCallable(typeof((QArray <Qubit>, long)), typeof(QVoid)).GetType());
                Assert.Same(typeof(ControlledOperation <long, QVoid>), gen0.Controlled.FindCallable(typeof((LittleEndian, long)), typeof(QVoid)).GetType());
                Assert.Same(typeof(ControlledOperation <(long, Result), QVoid>), gen0.Controlled.FindCallable(typeof((QArray <Qubit>, (long, Result))), typeof(QVoid)).GetType());
                Assert.Same(typeof(ControlledOperation <((Qubit, bool), Result), QVoid>), gen0.Controlled.FindCallable(typeof((QArray <Qubit>, ((TestQubit, bool), Result))), typeof(QVoid)).GetType());
                Assert.Same(typeof(ControlledOperation <long, QVoid>), gen0.Controlled.FindCallable(typeof((QArray <Qubit>, long)), typeof(QVoid)).GetType()); // Twice to check for caching.

                // A bunch of Debug.Asserts are hit, but Controlled will create something even if given the wrong type of Input/output,
                // here just for check backwards compatibility. Evaluate if we need to change this behavior.
                OperationsTestHelper.IgnoreDebugAssert(() =>
                {
                    Assert.Same(typeof(ControlledOperation <int, QVoid>), gen0.Controlled.FindCallable(typeof(Result), typeof(QVoid)).GetType());
                });

                var gen2 = new GenericCallable(s, typeof(Gen2 <,>));
                var r2   = gen2.Controlled.FindCallable(typeof((LittleEndian, (QArray <Qubit>, long, bool))), typeof(QVoid)) as ControlledOperation <(QArray <Qubit>, long, bool), QVoid>;
                Assert.NotNull(r2);
                Assert.Same(typeof(Gen2 <QArray <Qubit>, bool>), r2.BaseOp.GetType());

                var gen4 = new GenericCallable(s, typeof(Gen4 <,>));
                var r4   = gen4.Controlled.FindCallable(typeof((QArray <Qubit>, (Result, ((IUnitary <Qubit>, bool), (IAdjointable <(TestQubit, Result)>, Pauli)), Result))), typeof(QVoid)) as ControlledOperation <(Result, ((IUnitary, bool), (IAdjointable, Pauli)), Result), QVoid>;
                Assert.NotNull(r4);
                Assert.Same(typeof(Gen4 <(IUnitary, bool), (IAdjointable, Pauli)>), r4.BaseOp.GetType());
            });
        }
        public void ArraysByIndex()
        {
            var longs = new QArray <long> (2, 1, 0);

            Assert.NotEmpty(longs);
            Assert.Equal(3, longs.Length);
            Assert.Equal(2, longs[0]);
            Assert.Equal(1, longs[1]);
            Assert.Equal(0, longs[2]);

            longs = new QArray <long>(new long[5]);
            longs.Modify(0, 5);
            longs.Modify(2, 3);
            longs.Modify(4, 1);
            Assert.Equal(5, longs.Length);
            Assert.Equal(5, longs[0]);
            Assert.Equal(0, longs[1]);
            Assert.Equal(3, longs[2]);
            Assert.Equal(0, longs[3]);
            Assert.Equal(1, longs[4]);

            OperationsTestHelper.IgnoreDebugAssert(() =>
            {
                // Make sure we can call it with a long index:
                Assert.Throws <ArgumentOutOfRangeException>(() => longs[-1]);
                Assert.Throws <ArgumentOutOfRangeException>(() => longs[int.MaxValue]);
                Assert.Throws <ArgumentOutOfRangeException>(() => longs[(long)int.MaxValue + 1]);
            });
        }
        public void SliceArray()
        {
            var source   = new QArray <long>(0);
            var range    = new QRange(0, 0);
            var expected = new QArray <long>(0);
            var actual   = source.Slice(range);

            Assert.Equal(expected, actual);

            source   = new QArray <long>(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
            range    = new QRange(0, 2, 10);
            expected = new QArray <long>(0, 2, 4, 6, 8, 10);
            actual   = source.Slice(range);
            Assert.Equal(expected, actual);


            source   = new QArray <long>(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
            expected = new QArray <long>(1, 6);
            actual   = source.Slice(new QRange(1, 5, 10));
            Assert.Equal(expected, actual);

            OperationsTestHelper.IgnoreDebugAssert(() =>
            {
                source   = new QArray <long>(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
                range    = null;
                expected = new QArray <long>();
                actual   = source.Slice(range);
                Assert.Equal(expected, actual);
            });
        }
Esempio n. 4
0
 public void NativeIntrinsicOperation()
 {
     OperationsTestHelper.RunWithMultipleSimulators((sim) =>
     {
         var actual = IntrinsicBody.Run(sim).Result;
         Assert.Equal(IntrinsicBody.RESULT, actual);
     });
 }
Esempio n. 5
0
        public void NativeGenericOperation()
        {
            OperationsTestHelper.RunWithMultipleSimulators((sim) =>
            {
                var actual1 = DefaultBodyGeneric <string> .Run(sim, "hello").Result;
                Assert.Equal(IntrinsicBody.RESULT, actual1);

                var actual2 = DefaultBodyGeneric <long> .Run(sim, 1234).Result;
                Assert.Equal(1234, actual2);
            });
        }
Esempio n. 6
0
        public void NativeIntrinsicGenericOperation()
        {
            OperationsTestHelper.RunWithMultipleSimulators((sim) =>
            {
                var actual = IntrinsicBodyGeneric <string> .Run(sim, "hello").Result;
                Assert.Equal(IntrinsicBody.RESULT, actual);

                actual = IntrinsicBodyGeneric <long> .Run(sim, 1234).Result;
                Assert.Equal("1234", actual);
            });
        }
        public void JoinArrays()
        {
            var arr1     = new QArray <long>();
            var arr2     = new QArray <long>();
            var expected = new QArray <long>();
            var actual   = QArray <long> .Add(arr1, arr2);

            Assert.Equal(expected, actual);

            arr1     = new QArray <long>(1);
            arr2     = new QArray <long>();
            expected = new QArray <long>(1);
            actual   = QArray <long> .Add(arr1, arr2);

            Assert.Equal(expected, actual);

            arr1     = new QArray <long>();
            arr2     = new QArray <long>(1);
            expected = new QArray <long>(1);
            actual   = QArray <long> .Add(arr1, arr2);

            Assert.Equal(expected, actual);

            arr1     = new QArray <long>(1, 2, 3);
            arr2     = new QArray <long>(1, 4, 5, 3, 6);
            expected = new QArray <long>(1, 2, 3, 1, 4, 5, 3, 6);
            actual   = QArray <long> .Add(arr1, arr2);

            Assert.Equal(expected, actual);

            OperationsTestHelper.IgnoreDebugAssert(() =>
            {
                arr1     = null;
                arr2     = new QArray <long>(1, 4, 5, 3, 6);
                expected = new QArray <long>(1, 4, 5, 3, 6);
                actual   = QArray <long> .Add(arr1, arr2);
                Assert.Equal(expected, actual);

                arr1     = new QArray <long>(1, 2, 3);
                arr2     = null;
                expected = new QArray <long>(1, 2, 3);
                actual   = QArray <long> .Add(arr1, arr2);
                Assert.Equal(expected, actual);

                arr1     = null;
                arr2     = null;
                expected = null;
                actual   = QArray <long> .Add(arr1, arr2);
                Assert.Equal(expected, actual);
            });
        }
Esempio n. 8
0
        public void QSimX()
        {
            using (var sim = new QuantumSimulator(throwOnReleasingQubitsNotInZeroState: false))
            {
                var x       = sim.Get <Intrinsic.X>();
                var measure = sim.Get <Intrinsic.M>();
                var set     = sim.Get <Measurement.SetToBasisState>();

                var ctrlX = x.__ControlledBody__.AsAction();
                OperationsTestHelper.ctrlTestShell(sim, ctrlX, (enabled, ctrls, q) =>
                {
                    set.Apply((Result.Zero, q));
                    var result   = measure.Apply(q);
                    var expected = Result.Zero;
                    Assert.Equal(expected, result);

                    x.__ControlledBody__((ctrls, q));
                    result   = measure.Apply(q);
                    expected = (enabled) ? Result.One : Result.Zero;
                    Assert.Equal(expected, result);
                });
        public void ToffoliX()
        {
            var sim = new ToffoliSimulator();
            var x   = sim.Get <Intrinsic.X>();

            OperationsTestHelper.applyTestShell(sim, x, (q) =>
            {
                var measure = sim.Get <Intrinsic.M>();
                var set     = sim.Get <SetQubit>();

                set.Apply((Result.Zero, q));

                x.Apply(q);
                var result = measure.Apply(q);
                Assert.Equal(Result.One, result);

                x.Apply(q);
                result = measure.Apply(q);
                Assert.Equal(Result.Zero, result);

                x.Apply(q); // The test helper is going to apply another X before releasing
            });
Esempio n. 10
0
        public void TestQubitManager()
        {
            QubitManager qm = new QubitManager(20);

            // Test allocation of single qubit
            Qubit q1 = qm.Allocate();

            Assert.True(q1.Id == 0);

            // Test allocation of multiple qubits
            var qa1 = qm.Allocate(4);

            Assert.True(qa1.Length == 4);
            Assert.True(qa1[0].Id == 1);
            Assert.True(qa1[1].Id == 2);
            Assert.True(qa1[2].Id == 3);
            Assert.True(qa1[3].Id == 4);

            // Test reuse of deallocated qubits
            qm.Release(qa1[1]);

            Qubit q2 = qm.Allocate();

            Assert.True(q2.Id == 2);

            var qa2 = qm.Allocate(3);

            Assert.True(qa2.Length == 3);
            Assert.True(qa2[0].Id == 5);
            Assert.True(qa2[1].Id == 6);
            Assert.True(qa2[2].Id == 7);

            qm.Release(qa2);

            Qubit q3 = qm.Allocate();

            Assert.True(q3.Id == 7);

            Qubit q4 = qm.Allocate();

            Assert.True(q4.Id == 6);

            Qubit q5 = qm.Allocate();

            Assert.True(q5.Id == 5);

            // Test borrowing
            Qubit[] exclusion = new Qubit[4];
            exclusion[0] = qa1[0];
            exclusion[1] = qa1[2];
            exclusion[2] = q4;
            exclusion[3] = q3;

            long qubitsAvailable;

            qubitsAvailable = qm.GetFreeQubitsCount();
            var qab = qm.Borrow(5, exclusion);

            Assert.True(qubitsAvailable - qm.GetFreeQubitsCount() == 1);
            Assert.True(qab[0].Id == 0);
            Assert.True(qab[1].Id == 2);
            Assert.True(qab[2].Id == 4);
            Assert.True(qab[3].Id == 5);
            Assert.True(qab[4].Id == 8);

            Qubit q6 = qm.Allocate();

            Assert.True(q6.Id == 9);

            // Test borrowing of the same qubit again
            qubitsAvailable = qm.GetFreeQubitsCount();
            var qb1 = qm.Borrow(1, exclusion);

            Assert.True(qubitsAvailable - qm.GetFreeQubitsCount() == 0);
            Assert.True(qb1[0].Id == 0);
            qm.Return(qb1[0]);

            qm.Return(qab);

            // Test that qubit allocated for borrowing is freed after being returned
            Qubit q7 = qm.Allocate();

            Assert.True(q7.Id == 8);

            // Test allocation of qubits out of order.
            qm.Release(q4);
            qm.Release(qa1[2]);
            qm.Release(q1);

            var qa3 = qm.Allocate(4);

            Assert.True(qa3.Length == 4);
            Assert.True(qa3[0].Id == 0);
            Assert.True(qa3[1].Id == 3);
            Assert.True(qa3[2].Id == 6);
            Assert.True(qa3[3].Id == 10);

            // Test Disabling qubits
            Qubit q8 = qm.Allocate();

            Assert.True(q8.Id == 11);
            qm.Disable(q8);
            var qab2 = qm.Borrow(12, null);

            Assert.True(qab2[11].Id == 12);  // make sure 11 is not borrowed.
            qm.Release(q8);
            qm.Return(qab2);

            var qa4 = qm.Allocate(2);

            Assert.True(qa4[0].Id == 12); // make sure 11 is not reused.
            Assert.True(qa4[1].Id == 13); // make sure 11 is not reused.
            qm.Release(qa4);

            // Test allocating qubits over capacity
            OperationsTestHelper.IgnoreDebugAssert(() =>
            {
                IQArray <Qubit> n_q;

                Assert.Throws <NotEnoughQubits>(() => n_q = qm.Allocate(10));
                Assert.Throws <NotEnoughQubits>(() => n_q = qm.Borrow(25, exclusion));

                Assert.Throws <ArgumentException>(() => n_q = qm.Allocate(0));
                Assert.Throws <ArgumentException>(() => n_q = qm.Allocate(-2));
                Assert.Throws <ArgumentException>(() => n_q = qm.Borrow(0, exclusion));
                Assert.Throws <ArgumentException>(() => n_q = qm.Borrow(-2, exclusion));
            });
        }