static void Main(string[] args) { using (var qsim = new QuantumSimulator()) { Entanglement.Run(qsim).Wait(); } }
static void Main(string[] args) { var ones = 0; var equal = 0; using (var qsim = new QuantumSimulator()) { for (int index = 0; index < 1000; index++) { var(qubitOne, qubitTwo) = Entanglement.Run(qsim).Result; if (qubitOne == Result.One) { ones++; } if (qubitOne == qubitTwo) { equal++; } } } Console.WriteLine("Entanglement Result: "); Console.WriteLine($"\t One: {ones}"); Console.WriteLine($"\t Zero: {1000-ones}"); Console.WriteLine($"\t Equal: {equal/1000*100}"); Console.ReadKey(); }
static void Main(string[] args) { var ones = 0; var same = 0; // We initialize a Quantum Simulator same as before. using (var qsim = new QuantumSimulator()) { // We run a loop for 1000 times to test the principle for (int i = 0; i < 1000; i++) { // We run the Entanglement operation and store the returned results in the respective varialbles var(qone_state, qtwo_state) = Entanglement.Run(qsim).Result; // Check to see how many times we got state value 1. This will confirm our Superposition that collapses to state 0 or 1 with 50% probability for each if (qone_state == Result.One) { ones++; } // Next we perform the comparison to check if the state values of the qubit match. They should, if they are entangled and result in 100% matches if (qone_state == qtwo_state) { same++; } } } // Fancy printing the results. Console.WriteLine("Entanglement Results: "); Console.WriteLine($"\t One: {ones}"); Console.WriteLine($"\t Zero: {1000 - ones}"); Console.WriteLine($"\t Same: {same / 1000 * 100}%"); Console.ReadKey(); }
static void Main(string[] args) { var numberOfSameState = 0; var numberOfDifferentStates = 0; var numberOfOnes = 0; var numberOfZeros = 0; using (var qsim = new QuantumSimulator()) { for (int k = 0; k < 1000; k++) { // Compare two Qubits colapsed state after entanglement var(qubitOneResult, qubitTwoResult) = Entanglement.Run(qsim).Result; if (qubitOneResult == qubitTwoResult) { numberOfSameState++; } else { numberOfDifferentStates++; } if (qubitOneResult == Result.One) { numberOfOnes++; } else { numberOfZeros++; } if (qubitTwoResult == Result.One) { numberOfOnes++; } else { numberOfZeros++; } } } Console.WriteLine("=============================================="); Console.WriteLine("========= Entanglement Calculations =========="); Console.WriteLine("=============================================="); Console.WriteLine(); Console.WriteLine($"Number of ONES: {numberOfOnes}, Number of ZEROS: {numberOfZeros}"); Console.WriteLine($"Entangled matched: {numberOfSameState} Entangled mismatched: {numberOfDifferentStates}"); Console.WriteLine($"Even though the value might be different we expect ALL entagled Qubits to match."); Console.WriteLine(); Console.ReadKey(); }
static void Main(string[] args) { /*using (var qsim = new QuantumSimulator()) // Only for hello quantum * { * HelloQ.Run(qsim).Wait(); * }*/ using (var qsim = new QuantumSimulator()) { Entanglement.Run(qsim).Wait(); } }
public void TestEntanglement(QuantumSimulator qsim) { Start("Entanglement"); System.Console.WriteLine($"Testing Entanglement operation"); Result[] init = new Result[] { Result.Zero, Result.One }; foreach (Result vector in init) { var res = Entanglement.Run(qsim, 1000, vector).Result; var(zeros, ones, equal) = res; System.Console.WriteLine( $"Init:{vector,-4} 0s={zeros,-4} 1s={ones,-4} agree={equal,-4}"); } End("Entanglement"); }
static void Main(string[] args) { using (var qsim = new QuantumSimulator()) { // 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. // However, Result blocks execution until task completes; return synchronously var res = HelloQ.Run(qsim).Result; } System.Console.WriteLine("Press any key to continue..."); System.Console.WriteLine("Next up: Superposition: "); Console.ReadKey(); // work with superposition using (var qsim = new QuantumSimulator()) { // Try initial values Result[] initials = new Result[] { Result.Zero, Result.One }; foreach (Result initial in initials) { // '.Result' blocks execution until the task completes and returns the result synchronously. var res = Superposition.Run(qsim, 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.WriteLine("Next up: Entanglement: "); Console.ReadKey(); // work with entanglement using (var qsim = new QuantumSimulator()) { Result[] initials = new Result[] { Result.Zero, Result.One }; foreach (Result initial in initials) { var res = Entanglement.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 Run_Entanglement(string[] args) { Console.WriteLine("Running: Entanglement Test"); // Initialise the quantum simulator using (var qsim = new QuantumSimulator()) { Result[] initials = { Result.Zero, Result.One }; foreach (var initial in initials) { // Execute the quantum function var res = Entanglement.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(); }
static void Run(Func func) { int opcode = 0; code.pos = func.location; currentFunc = func; Block currentBlock = null; Stack <Block> blockstack = new Stack <Block>(); while (running) { try { opcode = code.ReadInt(); } catch (Exception ex) { Log.Here().Warning(ex, "Insignificant exception during buffer-read."); } Log.Here().Verbose($"Current opcode: {opcode}"); if (opcode == Opcodes.pushInt) { stack.Push(code.ReadInt()); } else if (opcode == Opcodes.pushString) { stack.Push(code.ReadString()); } else if (opcode == Opcodes.pushVar) { stack.Push(GetVarValue(code.ReadString())); } else if (opcode == Opcodes.print) { Console.Write(stack.Pop()); } else if (opcode == Opcodes.printLine) { Console.WriteLine(stack.Pop()); } else if (opcode == Opcodes.read) { Console.Read(); } else if (opcode == Opcodes.readLine) { Console.ReadLine(); } else if (opcode == Opcodes.qc_randomBitGenerator) { using var qsim = new QuantumSimulator(); Console.WriteLine($"Random bit: {RandomBitGenerator.Run(qsim).Result}"); } else if (opcode == Opcodes.qc_randomNumberGenerator) { using var sim = new QuantumSimulator(); // First we initialize all the variables: var bitString = "0"; // To save the bit string int max = (int)(stack.Pop() ?? throw new NullReferenceException("What's the maximum of the random number you want to generate?")); // The maximum of the range int size = Convert.ToInt32(Math.Floor(Math.Log(max, 2.0) + 1)); // To calculate the amount of needed bits int output = max + 1; // Int to store the output while (output > max) // Loop to generate the number { bitString = "0"; // Restart the bit string if fails bitString = String.Join("", Enumerable.Range(0, size).Select(idx => RandomBitGenerator.Run(sim).Result == Result.One ? "1" : "0" ) ); // Generate and concatenate the bits using using the Q# operation output = Convert.ToInt32(bitString, 2); // Convert the bit string to an integer } // Print the result Console.WriteLine($"Random number: {output}"); } else if (opcode == Opcodes.qc_entanglement) { using var qsim = new QuantumSimulator(); Result[] initials = new Result[] { Result.Zero, Result.One }; foreach (Result initial in initials) { (long, long, long)res = Entanglement.Run(qsim, 1000, initial).Result; (long numZeros, long numOnes, long agree) = res; Console.WriteLine($"Init:{initial,-4} 0s={numZeros,-4} 1s={numOnes,-4} agree={agree,-4}"); } } else if (opcode == Opcodes.bc_createBlockchain) { Console.WriteLine(vars); stack.Push(new Blockchain()); Log.Here().Information("Initialized new blockchain."); } else if (opcode == Opcodes.bc_createTransaction) { Blockchain blockchain = stack.Pop() as Blockchain ?? throw new NullReferenceException("Create a blockchain before trying to create a transaction!"); //Blockchain blockchain = GetVarValue(code.ReadString()) as Blockchain // ?? throw new NullReferenceException("Create a blockchain before trying to create a transaction!"); string fromAddress = stack.Pop() as string ?? throw new NullReferenceException("Who is receiving abinCoins?"); string toAddress = (stack.Pop() as string) ?? throw new NullReferenceException("Who is transacting abinCoins?"); int amount = (int)(stack.Pop() ?? throw new NullReferenceException("How much coins do you want to transact?")); blockchain.CreateTransaction(new Transaction(fromAddress, toAddress, amount)); Log.Here().Information($"New transaction over '{amount}' abinCoins on Blockchain from '{fromAddress}' to '{toAddress}'."); } else if (opcode == Opcodes.bc_mine) { Blockchain blockchain = stack.Pop() as Blockchain ?? throw new NullReferenceException("Create a blockchain before trying to mine abinCoins!"); string miningAddress = stack.Pop() as string ?? throw new NullReferenceException("Who is mining abinCoins?"); blockchain.ProcessPendingTransactions(miningAddress); Log.Here().Information($"'{miningAddress}' is mining abin coins."); } else if (opcode == Opcodes.bc_isValid) { Blockchain blockchain = stack.Pop() as Blockchain ?? throw new NullReferenceException("Create a blockchain before trying to verify it's validation-state!"); if (blockchain.IsValid()) { Log.Here().Information("Blockchain is valid."); } else { Log.Here().Warning("Blockchain is NOT valid. Data corrupted."); } } else if (opcode == Opcodes.halt) { while (true) { } } else if (opcode == Opcodes.inputInt32) { stack.Push(Convert.ToInt32(Console.ReadLine())); } else if (opcode == Opcodes.inputString) { stack.Push(Console.ReadLine()); } else if (opcode == Opcodes.pop) { stack.Pop(); } else if (opcode == Opcodes.popa) { stack.Clear(); } else if (opcode == Opcodes.decVar) { vars.Add(new Var(code.ReadString())); } else if (opcode == Opcodes.setVar) { SetVarValue(code.ReadString(), stack.Pop()); } else if (opcode == Opcodes.add) { object value1 = stack.Pop(); object value2 = stack.Pop(); if (value1 is string && value2 is string) { string value = ((string)value2) + ((string)value1); stack.Push(value); } else if (value1 is int && value2 is int) { int value = ((int)value1) + ((int)value2); stack.Push(value); } } else if (opcode == Opcodes.sub) { int value1 = (int)stack.Pop(); int value2 = (int)stack.Pop(); stack.Push(value1 - value2); } else if (opcode == Opcodes.mul) { int value1 = (int)stack.Pop(); int value2 = (int)stack.Pop(); stack.Push(value1 * value2); } else if (opcode == Opcodes.div) { int value1 = (int)stack.Pop(); int value2 = (int)stack.Pop(); stack.Push(value1 / value2); } else if (opcode == Opcodes.clear) { Console.Clear(); } else if (opcode == Opcodes.ife) { int blockNumber = code.ReadInt(); IfBlock ifblock = GetIf(blockNumber); object value1 = stack.Pop(); object value2 = stack.Pop(); if (IfEqual(value1, value2)) { if (currentBlock == null) { currentBlock = ifblock; } else { blockstack.Push(currentBlock); currentBlock = ifblock; } IncVars(); ifWorked = true; } else { code.pos = ifblock.endBlock; ifWorked = false; } } else if (opcode == Opcodes.ifn) { int blockNumber = code.ReadInt(); IfBlock ifblock = GetIf(blockNumber); object value1 = stack.Pop(); object value2 = stack.Pop(); if (!IfEqual(value1, value2)) { if (currentBlock == null) { currentBlock = ifblock; } else { blockstack.Push(currentBlock); currentBlock = ifblock; } IncVars(); ifWorked = true; } else { code.pos = ifblock.endBlock; ifWorked = false; } } else if (opcode == Opcodes.elseife) { int blockNumber = code.ReadInt(); ElseIfBlock elseifblock = GetElseIf(blockNumber); if (!ifWorked) { object value1 = stack.Pop(); object value2 = stack.Pop(); if (IfEqual(value1, value2)) { if (currentBlock == null) { currentBlock = elseifblock; } else { blockstack.Push(currentBlock); currentBlock = elseifblock; } IncVars(); ifWorked = true; } else { code.pos = elseifblock.endBlock; ifWorked = false; } } else { code.pos = elseifblock.endBlock; } } else if (opcode == Opcodes.elseifn) { int blockNumber = code.ReadInt(); ElseIfBlock elseifblock = GetElseIf(blockNumber); if (!ifWorked) { object value1 = stack.Pop(); object value2 = stack.Pop(); if (!IfEqual(value1, value2)) { if (currentBlock == null) { currentBlock = elseifblock; } else { blockstack.Push(currentBlock); currentBlock = elseifblock; } IncVars(); ifWorked = true; } else { code.pos = elseifblock.endBlock; ifWorked = false; } } else { code.pos = elseifblock.endBlock; } } else if (opcode == Opcodes.els) { int blockNumber = code.ReadInt(); ElseBlock elseblock = GetElse(blockNumber); if (!ifWorked) { if (currentBlock == null) { currentBlock = elseblock; } else { blockstack.Push(currentBlock); currentBlock = elseblock; } IncVars(); } else { code.pos = elseblock.endBlock; } } else if (opcode == Opcodes.endif) { if (blockstack.Count > 0) { currentBlock = blockstack.Pop(); } else { currentBlock = null; } DecVars(); } else if (opcode == Opcodes.call) { string name = code.ReadString(); Func f = GetFunc(name); Call c = new Call(currentFunc, code.pos, new List <Var>(vars)); callstack.Push(c); currentFunc = f; code.pos = f.location; vars.Clear(); } else if (opcode == Opcodes.got) { string name = code.ReadString(); int location = GetLabel(name); code.pos = location; } else if (opcode == Opcodes.ret) { if (callstack.Count > 0) { Call c = callstack.Pop(); currentFunc = c.func; code.pos = c.ret; vars = c.vars; } else { running = false; } } } }