public void GenericDependencies()
        {
            var subject = new TrivialSimulator();

            // Can't get Gen because it depends on AssertMeasurement
            Assert.Throws <MemberAccessException>(() =>
            {
                subject.Get <Gen <string> >();
            });

            // TODO: because we can't check dependencies, this
            // is not throwing an Exception, even though Gen depends on AssertMeasurement:
            var gen1 = subject.Get <DependsOnGen>();

            Assert.NotNull(gen1);

            // Add an implementation of X:
            subject.Register(typeof(Diagnostics.AssertMeasurement), typeof(DummyAssertMeasurement));
            var gen2 = subject.Get <Gen <int> >();

            Assert.NotNull(gen1);

            // Asking for same T, should give same op, but asking for different T should not:
            var gen2a = subject.Get <Gen <int> >();
            var gen2b = subject.Get <Gen <string> >();

            Assert.Same(gen2, gen2a);
            Assert.NotSame(gen2, gen2b);
        }
Ejemplo n.º 2
0
        public void QuantumTestSuiteSelfTests(TestOperation op)
        {
            var sim = new TrivialSimulator(); //these tests do not do anything quantum

            sim.OnLog += (msg) => { output.WriteLine(msg); };
            op.TestOperationRunner(sim);
        }
        public void OverrideWithNoInheritance()
        {
            var subject = new TrivialSimulator();

            Assert.Equal(typeof(TrivialSimulator).Name, subject.Name);

            // If providing an interface, the original gate must implement that interface
            Assert.Throws <ArgumentException>(() =>
            {
                subject.Register(typeof(Intrinsic.CNOT), typeof(LikeX), typeof(IUnitary <Qubit>));
            });

            // If providing an interface, the replacement gate must implement that interface
            Assert.Throws <ArgumentException>(() =>
            {
                subject.Register(typeof(Intrinsic.CNOT), typeof(LikeX), typeof(IUnitary <(Qubit, Qubit)>));
            });

            // You can override without inheritance, as long as both implement the same interface:
            subject.Register(typeof(Intrinsic.X), typeof(LikeX), typeof(IUnitary <Qubit>));

            // Verify the replacement work
            var x = subject.Get <IUnitary <Qubit>, Intrinsic.X>();

            Assert.NotNull(x);
            Assert.Equal(typeof(LikeX), x.GetType());
        }
        public void OperationWithNotImplementedDependency()
        {
            var subject = new TrivialSimulator();

            Assert.Throws <MemberAccessException>(() =>
            {
                subject.Get <DependsOnAssertMeasurement>();
            });
        }
        public void OperationWithNoBody()
        {
            var subject = new TrivialSimulator();

            Assert.Equal(typeof(TrivialSimulator).Name, subject.Name);

            Assert.Throws <MemberAccessException>(() =>
            {
                subject.Get <Diagnostics.AssertMeasurement>();
            });
        }
        public void OperationsCache()
        {
            var subject = new TrivialSimulator();

            Assert.Equal(typeof(TrivialSimulator).Name, subject.Name);

            var allocate1 = subject.Get <Intrinsic.Allocate>();
            var allocate2 = subject.Get <Intrinsic.Allocate>();

            Assert.Same(allocate1, allocate2);
        }
Ejemplo n.º 7
0
        public void OperationWithNoBody()
        {
            var subject = new TrivialSimulator();

            Assert.Equal(typeof(TrivialSimulator).Name, subject.Name);

            Assert.Throws <MemberAccessException>(() =>
            {
                subject.Get <Intrinsic.X>();
            });
        }
Ejemplo n.º 8
0
        public void UserDefinedOperations()
        {
            var subject = new TrivialSimulator();

            Assert.Equal(typeof(TrivialSimulator).Name, subject.Name);

            Assert.Throws <MemberAccessException>(() =>
            {
                subject.Get <Intrinsic.X>();
            });

            try
            {
                subject.Register(null, typeof(DummyX));
            }
            catch (ArgumentNullException e)
            {
                Assert.Equal("original", e.ParamName);
            }

            try
            {
                subject.Register(typeof(Intrinsic.Allocate), null);
            }
            catch (ArgumentNullException e)
            {
                Assert.Equal("replace", e.ParamName);
            }

            // You can only register an override of a class that extends Operation:
            Assert.Throws <ArgumentException>(() =>
            {
                subject.Register(typeof(Nothing), typeof(NothingSquared));
            });

            // By default, you can only register a Gate that is a subclass of the gate it's overriding:
            Assert.Throws <ArgumentException>(() =>
            {
                subject.Register(typeof(Intrinsic.Allocate), typeof(DummyX));
            });

            subject.Register(typeof(Intrinsic.X), typeof(DummyX));
            var customX = subject.Get <Intrinsic.X>();

            Assert.Equal(typeof(DummyX), customX.GetType());

            subject.Register(typeof(Intrinsic.M), typeof(DummyM));
            var customM = subject.Get <Intrinsic.M>();

            Assert.Equal(typeof(DummyM), customM.GetType());
        }
        public void TextRecursion()
        {
            var sim  = new TrivialSimulator();
            var res1 = Tests.Circuits.Factorial.Run(sim, 4L).Result;
            var res2 = Tests.Circuits.OpFactorial.Run(sim, 4L).Result;
            var res3 = Tests.Circuits.GenRecursion <long> .Run(sim, 6L, 2L).Result;

            var res4 = Tests.Circuits.GenRecursion <long> .Run(sim, 6L, 2L).Result;

            Assert.Equal(24L, res1);
            Assert.Equal(24L, res2);
            Assert.Equal(6L, res3);
            Assert.Equal(6L, res4);
        }
        public void OperationWithRecursiveDependencies()
        {
            var subject = new TrivialSimulator();

            var a = subject.Get <A>();
            var b = subject.Get <B>();

            // Make sure the last instance is correctly cached.
            var a2 = subject.Get <A>();
            var b2 = subject.Get <B>();

            Assert.Same(a, a2);
            Assert.Same(b, b2);
            Assert.Same(a, b.A);
            Assert.Same(b, a.B);
        }
        public void TestQubitCounts()
        {
            var sim = new TrivialSimulator();

            var baseCount  = 32L;
            var allocCount = 10L;

            var(initialUse, initialBorrow, afterUse, afterBorrow, subBorrow, innerBorrow) =
                Circuits.GetAvailableTest.Run(sim, allocCount).Result;

            Assert.Equal(baseCount, initialUse);
            Assert.Equal(baseCount, initialBorrow);
            Assert.Equal(baseCount - allocCount, afterUse);
            Assert.Equal(baseCount - allocCount, afterBorrow);
            Assert.Equal(baseCount, subBorrow);
            Assert.Equal(baseCount - 1, innerBorrow);
        }
        public void CheckQubits()
        {
            var sim      = new TrivialSimulator();
            var allocate = sim.Get <Intrinsic.Allocate>();
            var release  = sim.Get <Intrinsic.Release>();

            var qubits = allocate.Apply(1);
            var name   = "foo";

            // This should work:
            sim.CheckQubits(qubits, name);

            // Calling with null will throw an Exception
            Assert.Throws <ArgumentNullException>(name, () => sim.CheckQubits(null, name));

            // Calling after release will throw an Exception
            sim.CheckQubits(qubits, name);
            release.Apply(qubits);
            Assert.Throws <ArgumentException>($"{name}[0]", () => sim.CheckQubits(qubits, name));
        }
Ejemplo n.º 13
0
        public void SimulatorBuiltInOperations()
        {
            // The SimulatorBase class only implements operations for Qubit management,
            // i.e. Allocate/Release:
            var subject = new TrivialSimulator();

            Assert.Equal(typeof(TrivialSimulator).Name, subject.Name);

            var allocate = subject.Get <Intrinsic.Allocate>();
            var release  = subject.Get <Intrinsic.Release>();

            // Try the operations
            var qubits = allocate.Apply(3);

            Assert.Equal(3, qubits.Length);

            release.Apply(qubits);

            subject.CheckNoQubitLeak();
        }
Ejemplo n.º 14
0
        public void CatchFail()
        {
            int exceptionCount = 0;

            System.Action <System.Runtime.ExceptionServices.ExceptionDispatchInfo> inc = (System.Runtime.ExceptionServices.ExceptionDispatchInfo e) => exceptionCount++;
            var sim = new TrivialSimulator();

            sim.OnFail += inc; // increment exception counter when exception is caught
            var inst = sim.Get <Microsoft.Quantum.Simulation.Simulators.Tests.Circuits.AlwaysFail>();

            try
            {
                inst.Apply(QVoid.Instance);
                Assert.True(false); // make sure that exception actually happened
            }
            catch (System.Exception e)
            {
                Assert.True(true);           // make sure that exception actually happened
            }
            Assert.Equal(1, exceptionCount); // check that we cought exception once
        }
        public void SimulatorBuiltInOperations()
        {
            // The SimulatorBase class only implements operations for Qubit management,
            // i.e. Allocate/Release:
            var subject = new TrivialSimulator();

            Assert.Equal(typeof(TrivialSimulator).Name, subject.Name);

            // Check whether our events for allocation and deallocation
            // are actually called.
            var calledOnAllocate = false;
            var calledOnRelease  = false;

            subject.BeforeAllocateQubits += count =>
            {
                output.WriteLine($"Allocate count = {count}");
                calledOnAllocate = true;
            };
            subject.BeforeReleaseQubits += register =>
            {
                output.WriteLine($"Release qubits = {register}");
                calledOnRelease = true;
            };

            var allocate = subject.Get <Intrinsic.Allocate>();
            var release  = subject.Get <Intrinsic.Release>();

            // Try the operations
            var qubits = allocate.Apply(3);

            Assert.True(calledOnAllocate);
            Assert.Equal(3, qubits.Length);

            release.Apply(qubits);
            Assert.True(calledOnRelease);

            subject.CheckNoQubitLeak();
        }