static void Main(string[] args) { // construct the quantum simulator using (var sim = new QuantumSimulator()) { // Try initial values Result[] initials = new Result[] { Result.Zero, Result.One }; foreach (Result initial in initials) { // we pass the simulator, initial count of 1000, and an initial qubit value // Note that the run method is asynchronous. // Each Q# operation has a corresponding C# class, for which a Run method belongs to the class // the Run method performs asynchronous executions of the given operations // note that our results for BellTest are strored in a tuple var results1 = XGate.Run(sim, 1000, initial).Result; // here we perform a Hadamard gate operation, for which our qubit is placed from the ground or exited state into // a superposition state of either |0> or |1> var results2 = HGate.Run(sim, 1000, initial).Result; // Places two bits a Bell State, which is in essence a superposition of two qubits that are entangled var res = BellTest.Run(sim, 1000, initial).Result; // caste results into a tuple, for easy access var(numZeros, numOnes, agree) = res; var play = Playground.Run(sim, 1000, initial).Result; var(nOnes, nZeros, agreement) = play; // System.Console.WriteLine($"Init:{initial,-4} 0s={numZeros,-4} 1s={numOnes,-4} agree={agree, -4}"); System.Console.WriteLine($"Init:{initial,-4} 0s={nZeros,-4} 1s={nOnes,-4} agree={agreement,-4}"); } } System.Console.WriteLine("Press any key to continue..."); System.Console.ReadKey(); }
static void Main(string[] args) { // Estimate quantum resources. Console.WriteLine("Resource estimation"); Console.WriteLine("-------------------"); var estimator = new ResourcesEstimator(); BellTest.Run(estimator, 1000, Result.Zero).Wait(); Console.WriteLine(estimator.ToTSV()); // Actually run the experiment (assuming the resources are enough). Console.WriteLine(); Console.WriteLine("Experiment"); Console.WriteLine("----------"); using (var qsim = new QuantumSimulator()) { // Try initial values var initials = new Result[] { Result.Zero, Result.One }; foreach (var initial in initials) { var res = BellTest.Run(qsim, 1000, initial).Result; var(numZeros, numOnes, agree) = res; Console.WriteLine( $"Init:{initial,-4} 0s={numZeros,-4} 1s={numOnes,-4} agree={agree,-4}"); } } Console.WriteLine(); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); }
static void Main(string[] args) { //Construct a quantum simulator sim -- scope inside using statement only using (var sim = new QuantumSimulator()) { Result[] initials = new Result[] { Result.Zero, Result.One }; foreach (Result initial in initials) { //Run method is used to call Quantum operation from C#-- it is asynchronously execution -- it means when fetch the Result property of Q# operation BellTest to variable res, this blocks execution until the task complets and return result synchronously var res = BellTest.Run(sim, 1000, initial).Result; var(numZeros, numOnes) = res; System.Console.WriteLine($"Init:{initial,-4} 0s={numZeros,-4} 1s={numOnes,-4}"); } } using (var sim = new QuantumSimulator()) { Result[] initials = new Result[] { Result.Zero, Result.One }; foreach (Result initial in initials) { //Run method is used to call Quantum operation from C#-- it is asynchronously execution -- it means when fetch the Result property of Q# operation BellTest2 to variable res, this blocks execution until the task complets and return result synchronously var res = BellTest_2.Run(sim, 1000, initial).Result; var(numZeros, numOnes, agree) = res; System.Console.WriteLine($"Init:{initial,-4} 0s={numZeros,-4} 1s={numOnes,-4} agree={agree,-4}"); } } System.Console.WriteLine("Press any key to continue..."); System.Console.ReadKey(); }
static void Main(string[] args) { using (var qsim = new QuantumSimulator()) { // Try initial values Result[] initials = new Result[] { Result.Zero, Result.One }; foreach (Result initial in initials) { var res = BellTest.Run(qsim, 1000, initial).Result; /** * Using estimator estimate of how many resources (qubits or certain gates) the program will use on a quantum computer */ // var estimator = new ResourcesEstimator(); // var res = BellTest.Run(estimator, 1000, initial).Result; // System.Console.WriteLine(estimator.ToTSV()); var(numZeros, numOnes, agree) = res; System.Console.WriteLine( $"Init:{initial,-4} 0s={numZeros,-4} 1s={numOnes,-4} agree={agree,-4}"); } } System.Console.WriteLine("Press any key to continue..."); Console.ReadKey(); }
static void Main(string[] args) { // The C# driver has four parts:+ // Construct a quantum simulator. In the example, sim is the simulator. // Compute any arguments required for the quantum algorithm.In the example, count is fixed at a 1000 and initial is the initial value of the qubit. // Run the quantum algorithm. Each Q# operation generates a C# class with the same name. This class has a Run method that asynchronously executes the operation. // The execution is asynchronous because execution on actual hardware will be asynchronous. // Because the Run method is asynchronous, we fetch the Result property; this blocks execution until the task completes and returns the result synchronously. // Process the result of the operation. In the example, res receives the result of the operation. // Here the result is a tuple of the number of zeros(numZeros) and number of ones(numOnes) measured by the simulator. // This is returned as a ValueTuple in C#. We deconstruct the tuple to get the two fields, print the results, and then wait for a keypress. using (var sim = new QuantumSimulator()) { // Try initial values Result[] initals = new Result[] { Result.Zero, Result.One }; foreach (Result initial in initals) { var res = BellTest.Run(sim, 1000, initial).Result; var(numZeros, numOnes, agree) = res; Console.WriteLine($"Init: {initial, -4} 0s={numZeros, -4} 1s={numOnes, -4} agree={agree, -4}"); } } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); }
// Bell Test private static void Bell() { System.Console.WriteLine("Press any key to measure some Bell states"); System.Console.ReadKey(); var n = 1000; using (var sim = new QuantumSimulator()) { System.Console.WriteLine($" ========== Bell State & Teleportation =========="); System.Console.WriteLine($"The following uses a Hadamard gate (no entanglement), " + $"to get a superposition of states that, when measured, agree ~50% of the time"); // Try initial values Result[] initials = new Result[] { Result.Zero, Result.One }; foreach (Result initial in initials) { var res = BellTest.Run(sim, n, initial, false).Result; var(numZeros, numOnes, agree) = res; System.Console.WriteLine( $"Init:{initial,-4} 0s={numZeros,-4} 1s={numOnes,-4} agree={agree * 100 / n}%"); } System.Console.WriteLine($"Now we add a CNOT Gate to get entanglement, and get agreement 100% of the time"); System.Console.WriteLine("Press any key..."); System.Console.ReadKey(); foreach (Result initial in initials) { var res = BellTest.Run(sim, n, initial, true).Result; var(numZeros, numOnes, agree) = res; System.Console.WriteLine( $"Init:{initial,-4} 0s={numZeros,-4} 1s={numOnes,-4} agree={agree * 100/n}%"); } } System.Console.ReadKey(); System.Console.WriteLine(); }
static void Main(string[] args) { var estimator = new ResourcesEstimator(); BellTest.Run(estimator, 1000, Result.Zero).Wait(); System.Console.WriteLine(estimator.ToTSV()); System.Console.WriteLine("Press any key to continue..."); Console.ReadKey(); }
private static void RunBellTest(SimulatorBase simulator) { // Try initial values Result[] initials = new Result[] { Result.Zero, Result.One }; foreach (Result initial in initials) { var res = BellTest.Run(simulator, 1000, initial).Result; var(numZeros, numOnes, agree) = res; Console.WriteLine($"Init:{initial,-4} 0s={numZeros,-4} 1s={numOnes,-4} agree={agree,-4}"); } }
static void Main(string[] args) { Task <(long, long)> task = null; using (var qsim = new QuantumSimulator()) { task = BellTest.Run(qsim, 100, Result.One); task.Wait(); } Console.WriteLine("Num of zeroes : {0}, num of ones : {1}", task.Result.Item1, task.Result.Item2); Console.ReadLine(); }
static void Main(string[] args) { using (var qsim = new QuantumSimulator()) { Result[] initals = new Result[] { Result.Zero, Result.One }; foreach (Result inital in initals) { var res = BellTest.Run(qsim, 1000, inital).Result; Console.WriteLine("for inital of {2} ==> 0: {0} 1: {1} - Agree:{3}", res.Item1, res.Item2, inital, res.Item3); } Console.ReadKey(); } }
static void Main(string[] args) { using (var qsim = new QuantumSimulator()) { // Try initial values var res = BellTest.Run(qsim).Result; var(numZeros, numOnes, agree) = res; Console.WriteLine($"# of 0s={numZeros,-4}"); Console.WriteLine($"# of 1s={numOnes,-4}"); Console.WriteLine($"agree={agree,-4}"); System.Console.WriteLine(); } }
static void Main(string[] args) { using (var sim = new QuantumSimulator()) { for (int i = 0; i < 100; i++) { var(s1, s2) = BellTest.Run(sim).Result; Console.WriteLine($"第{i}次:Q1状态 {s1,-5} Q2状态 {s2,-5}"); } } Console.WriteLine("按任意键继续..."); Console.ReadKey(); }
static void Main(string[] args) { /* * Объявление переменной `sim`, отвечающей за квантовый симулятор. * После выхода из блока `using` память, выделенная под `sim`, освобождается. */ using (var sim = new QuantumSimulator()) { /* * Объявление массива из двух элементов типа `Result`: * `Zero` и `One`. */ Result[] initials = new Result[] { Result.Zero, Result.One }; /* * Для каждого элемента массива `initials` провести 1000 * экспериментов программы `BellTest`. */ foreach (Result initial in initials) { /* * `BellTest` представляет собой класс, у которого есть * метод `Run` - для запуска эксперимента на симуляторе `sim`. * * Следующими аргументами передаются параметры для самой * операции `BellTest` из Q#-файла `Bell.qs` - число * экспериментов и начальное состояние нулевого кубита. */ var res = BellTest.Run(sim, 1000, initial).Result; /* * Переменная `res` - это кортеж (tuple) из: * числа измерений, в которых был получен ноль (|0>), * числа измерений, в которых была получена единица (|1>), * числа измерений, в которых состояния нулевого и первого кубитов совпадают. */ var(numZeros, numOnes, agree) = res; /* * Вывод на экран. */ System.Console.WriteLine( $"Init: {initial,-4} 0s={numZeros,-4} 1s={numOnes,-4} agree={agree,-4}" ); } } System.Console.WriteLine("Press any key to continue..."); System.Console.ReadKey(); }
static void Main(string[] args) { using (var qsim = new QuantumSimulator()) { // Try initial values Result[] initials = new Result[] { Result.Zero, Result.One }; foreach (Result initial in initials) { var res = BellTest.Run(qsim, 1000, initial).Result; var(numZeros, numOnes) = res; System.Console.WriteLine( $"Init:{initial,-4} 0s={numZeros,-4} 1s={numOnes,-4}"); } } }
static void Main(string[] args) { using (var sim = new QuantumSimulator()) { Result[] initials = new Result[] { Result.Zero, Result.One }; foreach (Result initial in initials) { var res = BellTest.Run(sim, 1000, initial).Result; var(numZeros, numOnes) = res; System.Console.WriteLine($"Init:{initial,-4} 0s={numZeros,-4} 1s={numOnes,-4}"); } } System.Console.WriteLine("Press any key to continue"); System.Console.ReadKey(); }
static void Main(string[] args) { // Task 2 - add code here using (var sim = new QuantumSimulator()) { Result[] initials = new Result[] { Result.Zero, Result.One }; foreach (Result initial in initials) { var count = 1000; var (numZeros, numOnes, same) = BellTest.Run(sim, count, initial).Result; System.Console.WriteLine($"Init:{initial,-4} 0s={numZeros, -4} 1s={numOnes, -4} same={same, -4}"); // System.Console.WriteLine("Press any key to continue..."); // System.Console.ReadKey(); } } }
static void Main(string[] args) { using (var qsim = new QuantumSimulator()) { var initials = new [] { Result.Zero, Result.One }; foreach (Result initial in initials) { var res = BellTest.Run(qsim, 1000, initial).Result; var(numZeros, numOnes, agree) = res; Console.WriteLine($"初期状態:{initial,-4} |0>={numZeros,-4} |1>={numOnes,-4} 同一={agree,-4}"); } } Console.WriteLine("キーを押したら終了します..."); Console.ReadKey(); }
static void Main(string[] args) { using (var qsim = new QuantumSimulator()) { // Try initial values Result[] initials = new Result[] { Result.Zero, Result.One }; foreach (Result initial in initials) { var res = BellTest.Run(qsim, 1000, initial).Result; var(numZeros, numOnes, agree) = res; Console.WriteLine($"Init: {initial, -4} 0s={numZeros, -4} 1s={numOnes, -4}, agree={agree, -4}"); } } Console.WriteLine("Press any key to continue..."); Console.ReadLine(); }
private static void Main() { using (var qsim = new QuantumSimulator()) { Clear(); HelloQ.Run(qsim).Wait(); // prova i valori iniziali Result[] initials = new Result[] { Result.Zero, Result.One }; foreach (Result initial in initials) { var res = BellTest.Run(qsim, 1000, initial).Result; var(numZeros, numOnes, agree) = res; WriteLine($"Init:{initial,-4} 0s={numZeros,-4} 1s={numOnes,-4} agree={agree,-4}"); } } ReadKey(); }
static void Main(string[] args) { //Erzeugen des Quantumsimulators using (var quantumSimulator = new QuantumSimulator()) { //Erzeugung zweier anfänglicher Messergebnisse(Resultate) von Null und Eins Result[] initials = new Result[] { Result.Zero, Result.One }; //Für jedes Messergebnis(Resultat), zwei an der Zahl, wird der Quantensimulator gestartet foreach (Result initial in initials) { var result = BellTest.Run(quantumSimulator, 1000, initial).Result; var(numZeros, numOnes, agree) = result; System.Console.WriteLine($"Init:{initial,-4} 0s={numZeros,-4} 1s={numOnes,-4} agree={agree, -4}"); } } System.Console.WriteLine("Press any key to continue..."); System.Console.ReadKey(); }
static void Main(string[] args) { using (var sim = new QuantumSimulator()) // 'sim' is the Q# quantum simulator { // Try initial values Result[] initials = new Result[] { Result.Zero, Result.One }; foreach (Result initial in initials) { var res = BellTest.Run(sim, 1000, initial).Result; // 'Run' is the method to run the quantum simulation // 'Run' is asynchronous, but fetching the 'Result' property blocks // execution until the task completes and makes this synchronous. var(numZeros, numOnes, agree) = res; System.Console.WriteLine( $"Init:{initial,-4} 0s={numZeros,-4} 1s={numOnes,-4} agree={agree,-4}"); } } System.Console.WriteLine("Press any key to continue..."); System.Console.ReadKey(); }
static void Main(string[] args) { using (var sim = new QuantumSimulator()) // sim is the simulator { // Try initial values Result[] initials = new Result[] { Result.Zero, Result.One }; foreach (Result initial in initials) //Compute any arguments required for the quantum algorithm. { var res = BellTest.Run(sim, 1000, initial).Result; //In the example, count is fixed at a 1000 and initial is the initial value of the qubit. var(numZeros, numOnes) = res; // var (numZeros, numOnes, agree) = res; System.Console.WriteLine( $"Init:{initial,-4} 0s={numZeros,-4} 1s={numOnes,-4}"); //System.Console.WriteLine( // $"Init:{initial,-4} 0s={numZeros,-4} 1s={numOnes,-4} agree={agree,-4}"); } } System.Console.WriteLine("Press any key to continue..."); System.Console.ReadKey(); }
static void Main(string[] args) { // aloca dinamicamente um simulador para executar o código quantico using (var SIM = new QuantumSimulator()) { Result[] estados_iniciais = new Result[] { Result.Zero, Result.One }; foreach (Result estado in estados_iniciais) { // computa o teste: cont=1000 inicial=estados_iniciais var resp = BellTest.Run(SIM, 1000, estado).Result; var(numZeros, numUns) = resp; System.Console.WriteLine($"Resultado:{estado, -4} 0s={numZeros, -4} 1s={numUns, -4}"); } } System.Console.WriteLine("Pressione qualquer tecla para continuar..."); System.Console.ReadKey(); }
static void Main(string[] args) { var estimator = new ResourcesEstimator(); BellTest.Run(estimator, 1000, Result.Zero).Wait(); System.Console.WriteLine(estimator.ToTSV()); /*using (var qsim = new QuantumSimulator()) * { * Result[] initials = new Result[] { Result.Zero, Result.One }; * foreach(Result initial in initials) * { * var res = BellTest.Run(qsim, 1000, initial).Result; * var (numZeros, numOnes, agree) = res; * System.Console.WriteLine( * $"Init:{initial,-4} 0s={numZeros,-4} 1s={numOnes,-4} agree={agree,-4}"); * } * }*/ System.Console.WriteLine("Press any key to continue..."); Console.ReadKey(); }
static void Main(string[] args) { var qsim = new ResourcesEstimator(); BellTest.Run(qsim, 1000, Result.Zero).Wait(); System.Console.WriteLine(qsim.ToTSV()); //using (var qsim = new QuantumSimulator()) //{ // //Try initial values // Result[] initials = new Result[] { Result.Zero, Result.One }; // foreach (Result initial in initials) // { // var res = BellTest.Run(qsim, 1000, initial).Result; // var (numZeroes, numOnes, agree) = res; // System.Console.WriteLine($"Init:{initial,-4} 0s={numZeroes,-4} 1s={numOnes,-4} agree={agree, -4}"); // } //} System.Console.WriteLine("Press any key to continue..."); Console.ReadKey(); }
static void Main(string[] args) { using (var qsim = new QuantumSimulator()) { Result[] initials = new Result[] { Result.Zero, Result.One }; foreach (Result initial in initials) { var res = BellTest.Run(qsim, 1000, initial).Result; var(numZeros, numOnes, agree) = res; System.Console.WriteLine( $"Init:{initial,-4} 0s={numZeros,-4} 1s={numOnes,-4} agree={agree,-4}"); } } //run this instead of the above using block for resource estimation //var estimator = new ResourcesEstimator(); //BellTest.Run(estimator, 1000, Result.Zero).Wait(); //System.Console.WriteLine(estimator.ToTSV()); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); }
static void Main(string[] args) { void PressKey(string name) { Console.WriteLine($"\n\nPress any key to start {name}\n\n"); Console.ReadKey(); } using (var qsim = new QuantumSimulator()) { PressKey("Reversable gate"); for (int i = 0; i < 5; ++i) { var initialValue = i % 2 == 0 ? Result.Zero : Result.One; var result = ReversableGate.Run(qsim, initialValue).Result; Console.WriteLine($"Reversable gate result is: " + $"{result}. Initial value: {initialValue}"); } PressKey("Measurement superposition collapsing"); for (int i = 0; i < 15; ++i) { var initialValue = i % 2 == 0 ? Result.One : Result.Zero; var result = MeasurementCollapsingSuperposition .Run(qsim, initialValue).Result; Console.WriteLine( $"Reversable gate result is: {result.Item1}. " + $"Result2: {result.Item2}. Inital value: {initialValue}"); } PressKey("Random number generator"); for (int i = 0; i < 10; ++i) { var randomNumber = GenerateRandomNumber.Run(qsim).Result; Console.WriteLine($"Random number is: {randomNumber}"); } PressKey("Bell test"); Result[] initials = new Result[] { Result.Zero, Result.One }; foreach (var initial in initials) { var res = BellTest.Run(qsim, 1000, initial).Result; var(numZeros, numOnes, agrees) = res; Console.WriteLine($"" + $"Init:{initial,-4} " + $"0s={numZeros,-4} " + $"1s={numOnes,-4} " + $"Agrees = {agrees,-4}"); } PressKey("Deutsch-Jozsa"); var const0Result = Constant0.Run(qsim).Result; var const1Result = Constant1.Run(qsim).Result; var negationResult = Negation.Run(qsim).Result; var identityResult = Identity.Run(qsim).Result; Console.WriteLine($"Const0: " + $"{const0Result.Item1} " + $"{const0Result.Item2} " + $"{const0Result.Item3}"); Console.WriteLine($"Const1: " + $"{const1Result.Item1} " + $"{const1Result.Item2} " + $"{const1Result.Item3}"); Console.WriteLine($"Identity: " + $"{identityResult.Item1} " + $"{identityResult.Item2} " + $"{identityResult.Item3}"); Console.WriteLine($"Negation: " + $"{negationResult.Item1} " + $"{negationResult.Item2} " + $"{negationResult.Item3}"); PressKey("Teleportation part 1"); for (int i = 0; i < 5; ++i) { Console.WriteLine($"Teleport (msg==false): " + $"{SendMessage.Run(qsim, false).Result}"); } PressKey("Teleportation part 2"); for (int i = 0; i < 5; ++i) { Console.WriteLine($"Teleport (msg==true): " + $"{SendMessage.Run(qsim, true).Result}"); } } Console.WriteLine("\n\nEnd"); Console.ReadKey(); }