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 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); }
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()); }