Advertisement
dialga5555

Checking for equality with QPU (no data loss)

Dec 29th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. // Programming Quantum Computers
  2. // by Eric Johnston, Nic Harrigan and Mercedes Gimeno-Segovia
  3. // O'Reilly Media
  4.  
  5. // To run this online, go to http://oreilly-qc.github.io?p=3-4
  6.  
  7.  
  8. //Edits for clarity made by N. Halstead
  9.  
  10. qc.clearOutput();
  11. var results = "";
  12. for(var i=0; i < 50; i++){
  13. qc.reset(3);
  14. var input1 = qint.new(1, 'input1');
  15. var input2 = qint.new(1, 'input2');
  16. var output = qint.new(1, 'output');
  17. // Initialize the output register
  18. // Initialize input registers that we want to compare
  19. // In this example the swap test should reveal their equality
  20. qc.label('Initialize');
  21. input1.write(1);
  22. input2.write(0);
  23. qc.label('');
  24. qc.nop()
  25.  
  26. // The swap test itself
  27. qc.label('Swap test')
  28. output.write(0);
  29. output.had();
  30. // Now exchange the two inputs conditional on the output qubits.
  31. input1.exchange(input2, 0x1, output.bits());
  32. output.had();
  33. output.not();
  34. // The output register is 1 only if inputs are equal
  35. var ans = output.read();
  36. results+=ans;
  37. qc.label('');
  38. qc.nop();
  39. }
  40.  
  41. //check results
  42. qc.print(results+"\n");
  43. var different = false;
  44. for(var i = 0; i < results.length-1; i++){
  45. if(results.charAt(i)!==results.charAt(i+1)){
  46. different = true;
  47. }
  48. }
  49.  
  50. different ? qc.print("The inputs don't match!") : qc.print("The inputs match");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement