Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            #region Setup

            // We begin by defining a quantum simulator to be our target
            // machine.
            var sim = new QuantumSimulator(throwOnReleasingQubitsNotInZeroState: true);

            #endregion

            #region Measuring One Qubit
            // In this region, we call the MeasurementOneQubit operation
            // from Measurement.qs, which prepares a qubit in the |+〉 ≔ H|0〉
            // state and asserts that the probability of observing a Zero
            // result is 50%.

            // Thus, we will run the operation several times and report
            // the mean.

            var averageResult = Enumerable.Range(0, 100).Select((idx) =>
                                                                MeasureOneQubit.Run(sim).Result == Simulation.Core.Result.One ? 1 : 0
                                                                ).Average();
            System.Console.WriteLine($"Frequency of 〈0| given H|0〉: {averageResult}");

            Pause();
            #endregion

            #region Measuring Two Qubits
            // Next, we generalize to consider measuring two qubits, each
            // in the Z-basis. The MeasurementTwoQubits operation
            // returns a (Result, Result), one for each qubit; let's print
            // out a few such measurements.

            foreach (var idxMeasurement in Enumerable.Range(0, 8))
            {
                var results = MeasureTwoQubits.Run(sim).Result;
                System.Console.WriteLine($"Measured HH|00〉 and observed {results}.");
            }

            Pause();
            #endregion

            #region Measuring in the Bell Basis
            // Finally, we demonstrate that if we measure each half of
            // the entangled pair CNOT₀₁ · H |00〉 = (|00〉 + |11〉) / sqrt(2),
            // the parity of the observed results is always positive. That is,
            // unlike in the previous example, the two Result values are
            // always the same.

            foreach (var idxMeasurement in Enumerable.Range(0, 8))
            {
                var results = MeasureInBellBasis.Run(sim).Result;
                System.Console.WriteLine($"Measured CNOT₀₁ · H |00〉 and observed {results}.");
            }

            #endregion

            System.Console.WriteLine("\n\nPress Enter to continue...\n\n");
            System.Console.ReadLine();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sample to show that one can substitute the operation factory
        /// to run on different types of machines.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            var factory = new ConsoleDriver(); //Using different Factory

            Console.WriteLine("Hadamard to Qasm");
            MeasureOneQubit.Run(factory).Wait();
            Console.WriteLine("Press Enter to continue...");
            Console.ReadLine();

            Console.WriteLine("Measurement bell curve to Qasm");
            MeasureInBellBasis.Run(factory).Wait();
            Console.WriteLine("Press Enter to continue...");
            Console.ReadLine();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Sample to show that one can substitute the operation factory
        /// to run on different types of machines.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //You need to replace this with your own key from the quantum experience
            var apiKey = "4616efdc29c9d4d751b3cd23a2e7d677ef8a..........623614734c2e11e25f9";

            if (apiKey.Contains("."))
            {
                Console.Error.WriteLine("Did you put an api key in Driver.cs? Without that, it will not work.");
            }
            else
            {
                var factory = new IbmQx4(apiKey); //Using different Factory
                Console.WriteLine("Hadamard on IBMQx4");
                for (int i = 0; i < 1; i++)
                {
                    var result = MeasureOneQubit.Run(factory).Result;
                    Console.WriteLine($"Result of Hadamard is {result}");
                }
            }

            Console.WriteLine("Press Enter to continue...");
            Console.ReadLine();
        }