operations.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <!DOCTYPE html>
  2. <html >
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width,initial-scale=1">
  6. <title>Quantum Operations</title>
  7. <link rel="stylesheet" href="_static/pygments.css">
  8. <link rel="stylesheet" href="_static/theme.css">
  9. <script src="_static/theme-vendors.js"></script>
  10. <script src="_static/theme.js" defer></script>
  11. <link rel="index" title="Index" href="genindex.html" />
  12. <link rel="search" title="Search" href="search.html" />
  13. <link rel="prev" title="Quantum Gates" href="gates.html" />
  14. </head>
  15. <body><div id="app" class="theme-container" :class="pageClasses"><navbar @toggle-sidebar="toggleSidebar">
  16. <router-link to="index.html" class="home-link">
  17. <span class="site-name">QCGPU</span>
  18. </router-link>
  19. <div class="links">
  20. <navlinks class="can-hide">
  21. <div class="nav-item">
  22. <a href="install.html"
  23. class="nav-link ">
  24. None
  25. </a>
  26. </div>
  27. </navlinks>
  28. </div>
  29. </navbar>
  30. <div class="sidebar-mask" @click="toggleSidebar(false)">
  31. </div>
  32. <sidebar @toggle-sidebar="toggleSidebar">
  33. <navlinks>
  34. <div class="nav-item">
  35. <a href="install.html"
  36. class="nav-link ">
  37. None
  38. </a>
  39. </div>
  40. </navlinks><div class="sidebar-links" role="navigation" aria-label="main navigation">
  41. <div class="sidebar-group">
  42. <ul class="current">
  43. <li class="toctree-l1"><a class="reference internal" href="install.html">Installation</a></li>
  44. <li class="toctree-l1"><a class="reference internal" href="quickstart.html">Getting Started</a></li>
  45. <li class="toctree-l1 current"><a class="reference internal" href="guide.html">User Guide</a><ul class="current">
  46. <li class="toctree-l2"><a class="reference internal" href="registers.html">Quantum Registers</a></li>
  47. <li class="toctree-l2"><a class="reference internal" href="gates.html">Quantum Gates</a></li>
  48. <li class="toctree-l2 current"><a class="current reference internal" href="#">Quantum Operations</a></li>
  49. </ul>
  50. </li>
  51. </ul>
  52. </div>
  53. </div>
  54. </sidebar>
  55. <page>
  56. <div class="content">
  57. <div class="section" id="quantum-operations">
  58. <h1>Quantum Operations<a class="headerlink" href="#quantum-operations" title="Permalink to this headline">¶</a></h1>
  59. <p>There are a number of operations you can perform on quantum registers
  60. with QCGPU.</p>
  61. <div class="section" id="measurement">
  62. <h2>Measurement<a class="headerlink" href="#measurement" title="Permalink to this headline">¶</a></h2>
  63. <p>Measurement of a register in QCGPU doesn’t collapse the state (although
  64. this may be added in the future). When you measure the state, you can
  65. specify the number of times to sample. The output of this <code class="docutils literal notranslate"><span class="pre">measure</span></code>
  66. function is a dictionary with the bitstrings of the outputs, along with
  67. the number of times they were measured.</p>
  68. <p>You can measure a register as follows,</p>
  69. <div class="code python highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">qcgpu</span>
  70. <span class="n">register</span> <span class="o">=</span> <span class="n">qcgpu</span><span class="o">.</span><span class="n">State</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
  71. <span class="n">register</span><span class="o">.</span><span class="n">measure</span><span class="p">(</span><span class="n">samples</span><span class="o">=</span><span class="mi">1000</span><span class="p">)</span>
  72. <span class="c1"># {&#39;00000&#39;: 1000}</span>
  73. </pre></div>
  74. </div>
  75. <p>There is also a convenience method to measure only a single qubit.
  76. Again, the state is not collapsed</p>
  77. <div class="code python highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">qcgpu</span>
  78. <span class="n">register</span> <span class="o">=</span> <span class="n">qcgpu</span><span class="o">.</span><span class="n">State</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
  79. <span class="n">register</span><span class="o">.</span><span class="n">h</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
  80. <span class="n">register</span><span class="o">.</span><span class="n">measure_qubit</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">samples</span><span class="o">=</span><span class="mi">1000</span><span class="p">)</span>
  81. <span class="c1"># {&#39;1&#39;: 523, &#39;0&#39;: 477}</span>
  82. </pre></div>
  83. </div>
  84. </div>
  85. <div class="section" id="probability">
  86. <h2>Probability<a class="headerlink" href="#probability" title="Permalink to this headline">¶</a></h2>
  87. <p>QCGPU provides another method for getting the probability of each
  88. outcome.</p>
  89. <p>The probability of getting an outcome <span class="math notranslate nohighlight">\(j\)</span> from a state
  90. <span class="math notranslate nohighlight">\(\lvert \psi \rangle = \sum_{j = 0}^{2^n - 1} \alpha_j \lvert j \rangle\)</span>
  91. is</p>
  92. <div class="math notranslate nohighlight">
  93. \[P(j) = \lvert \alpha_j \lvert^2\]</div>
  94. <p>The method <code class="docutils literal notranslate"><span class="pre">probabilities</span></code> returns a numpy array with each of the
  95. values corresponding to <span class="math notranslate nohighlight">\(\lvert \alpha_j \lvert ^2\)</span> for each index
  96. <span class="math notranslate nohighlight">\(j\)</span>.</p>
  97. <div class="code python highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">qcgpu</span>
  98. <span class="n">register</span> <span class="o">=</span> <span class="n">qcgpu</span><span class="o">.</span><span class="n">State</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
  99. <span class="n">register</span><span class="o">.</span><span class="n">h</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
  100. <span class="n">register</span><span class="o">.</span><span class="n">probabilities</span><span class="p">()</span> <span class="c1"># [0.5, 0.5]</span>
  101. </pre></div>
  102. </div>
  103. <p>This method is particularly useful to plot the probabilities of each
  104. outcome.</p>
  105. </div>
  106. </div>
  107. </div>
  108. <div class="page-nav">
  109. <div class="inner">
  110. <span class="prev">
  111. <a href="gates.html"
  112. title="previous chapter">← Quantum Gates</a>
  113. </span>
  114. <script type="text/x-mathjax-config">
  115. MathJax.Hub.Config({
  116. tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']], displayMath: [['$$', '$$'], ['\\[', '\\]']]}
  117. });
  118. </script>
  119. <script type="text/javascript"
  120. src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
  121. </script>
  122. </div>
  123. </div>
  124. </page>
  125. </div></body>
  126. </html>