public async Task ReallocateQubitInGroundStateTest()
        {
            var sim      = new QuantumSimulator();
            var allocate = sim.Get <Intrinsic.Allocate>();
            var release  = sim.Get <Intrinsic.Release>();
            var q1       = allocate.Apply(1);
            var q1Id     = q1[0].Id;
            var gate     = sim.Get <Intrinsic.X>();
            var measure  = sim.Get <Intrinsic.M>();

            gate.Apply(q1[0]);
            var result1 = measure.Apply(q1[0]);

            //Check X operation
            Assert.Equal(result1, Result.One);
            release.Apply(q1[0]);
            var q2   = allocate.Apply(1);
            var q2Id = q2[0].Id;

            //Assert reallocated qubit has the same id as the one released
            Assert.Equal(q1Id, q2Id);
            var result2 = measure.Apply(q2[0]);

            //Assert reallocated qubit has is initialized in state |0>
            Assert.Equal(result2, Result.Zero);
        }
Example #2
0
        public void TestSimonOnWikiExample()
        {
            ICallable function = Simulator.Get <ICallable>(typeof(WikiTestFunction));

            bool[] answer = { true, true, false };

            IList <bool> secret = RunTest($"wiki example", function, 3, 0.99);

            Assert.Equal(answer, secret);
        }
Example #3
0
        static void Main(string[] args)
        {
            // We begin by making an instance of the simulator that we will use to run our Q# code.
            using (var qsim = new QuantumSimulator()) {
                #region Basic Definitions

                // Next, we give C# names to each operation defined in Q#.
                // In doing so, we ask the simulator to give us each operation
                // so that it has an opportunity to override operation definitions.
                var H2EstimateEnergyRPE = qsim.Get <H2EstimateEnergyRPE, H2EstimateEnergyRPE>();
                var H2BondLengths       = qsim.Get <H2BondLengths, H2BondLengths>();

                #endregion

                #region Calling into Q#

                // To call a Q# operation that takes unit `()` as its input, we need to grab
                // the QVoid.Instance value.
                var bondLengths = H2BondLengths.Body.Invoke(QVoid.Instance);

                // In Q#, we defined the operation that performs the actual estimation;
                // we can call it here, giving a structure tuple that corresponds to the
                // C# ValueTuple that it takes as its input. Since the Q# operation
                // has type (idxBondLength : Int, nBitsPrecision : Int, trotterStepSize : Double) => (Double),
                // we pass the index along with that we want six bits of precision and
                // step size of 1.
                //
                // The result of calling H2EstimateEnergyRPE is a Double, so we can minimize over
                // that to deal with the possibility that we accidentally entered into the excited
                // state instead of the ground state of interest.
                Func <int, Double> estAtBondLength = (idx) => Enumerable.Min(
                    from idxRep in Enumerable.Range(0, 3)
                    select H2EstimateEnergyRPE.Body.Invoke((idx, 6, 1.0))
                    );

                // We are now equipped to run the Q# simulation at each bond length
                // and print the answers out to the console.
                foreach (var idxBond in Enumerable.Range(0, 54))
                {
                    System.Console.WriteLine($"Estimating at bond length {bondLengths[idxBond]}:");
                    var est = estAtBondLength(idxBond);
                    System.Console.WriteLine($"\tEst: {est}\n");
                }

                #endregion
            }

            Console.WriteLine("Press Enter to continue...");
            Console.ReadLine();
        }
Example #4
0
 static void Main(string[] args)
 {
     using (var sim = new QuantumSimulator())
     {
         System.Console.WriteLine("BEGIN Running Teleportation - Arbitrary Unitary");
         TeleportArbitraryState.Run(sim, sim.Get <H, H> ()).Wait();
         System.Console.WriteLine("END Running Teleportation - Arbitrary Unitary\n\n");
     }
 }
Example #5
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);
                });
Example #6
0
        static void Main(string[] args)
        {
            using (var sim = new QuantumSimulator())
            {
                System.Console.WriteLine("BEGIN Running Teleportation - Simple");
                TeleportClassicalFlagSimple.Run(sim, true).Wait();
                TeleportClassicalFlagSimple.Run(sim, false).Wait();
                System.Console.WriteLine("END Running Teleportation - Simple\n\n");

                System.Console.WriteLine("BEGIN Running Teleportation - Clean");
                TeleportClassicalFlag.Run(sim, true).Wait();
                TeleportClassicalFlag.Run(sim, false).Wait();
                System.Console.WriteLine("END Running Teleportation - Clean\n\n");

                System.Console.WriteLine("BEGIN Running Teleportation - Arbitrary Unitary");
                TeleportArbitraryState.Run(sim, sim.Get <H, H> ()).Wait();
                System.Console.WriteLine("END Running Teleportation - Arbitrary Unitary\n\n");

                System.Console.WriteLine("BEGIN Teleport Half an Entangled Pair");
                TeleportEntangledQubit.Run(sim).Wait();
                System.Console.WriteLine("END Teleport Half an Entangled Pair\n\n");
            }
        }