Skip to content

Commit

Permalink
Docs preview for PR #1417.
Browse files Browse the repository at this point in the history
  • Loading branch information
cuda-quantum-bot committed Mar 22, 2024
1 parent 04e77b4 commit 43e03d7
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 9 deletions.
8 changes: 4 additions & 4 deletions pr-1417/api/languages/cpp_api.html
Original file line number Diff line number Diff line change
Expand Up @@ -1000,14 +1000,14 @@ <h2>Common<a class="headerlink" href="#common" title="Permalink to this heading"
<div class="breathe-sectiondef docutils container">
<p class="breathe-sectiondef-title rubric" id="breathe-section-title-public-functions">Public Functions</p>
<dl class="cpp function">
<dt class="sig sig-object cpp" id="_CPPv4N5cudaq14observe_result14observe_resultERdRK7spin_op">
<span class="target" id="classcudaq_1_1observe__result_1a10cbcb530bf05e5863a2afb153c9dca8"></span><span class="k"><span class="pre">inline</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">observe_result</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">double</span></span><span class="w"> </span><span class="p"><span class="pre">&amp;</span></span><span class="n sig-param"><span class="pre">e</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><a class="reference internal" href="#_CPPv4N5cudaq7spin_opE" title="cudaq::spin_op"><span class="n"><span class="pre">spin_op</span></span></a><span class="w"> </span><span class="p"><span class="pre">&amp;</span></span><span class="n sig-param"><span class="pre">H</span></span><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv4N5cudaq14observe_result14observe_resultERdRK7spin_op" title="Permalink to this definition">¶</a><br /></dt>
<dt class="sig sig-object cpp" id="_CPPv4N5cudaq14observe_result14observe_resultEdRK7spin_op">
<span class="target" id="classcudaq_1_1observe__result_1aba859a67c28ee73dee0943043de4b419"></span><span class="k"><span class="pre">inline</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">observe_result</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">double</span></span><span class="w"> </span><span class="n sig-param"><span class="pre">e</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><a class="reference internal" href="#_CPPv4N5cudaq7spin_opE" title="cudaq::spin_op"><span class="n"><span class="pre">spin_op</span></span></a><span class="w"> </span><span class="p"><span class="pre">&amp;</span></span><span class="n sig-param"><span class="pre">H</span></span><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv4N5cudaq14observe_result14observe_resultEdRK7spin_op" title="Permalink to this definition">¶</a><br /></dt>
<dd><p>Constructor, takes the precomputed expectation value for &lt;psi(x) | H | psi(x)&gt; for the given <a class="reference internal" href="#classcudaq_1_1spin__op"><span class="std std-ref">spin_op</span></a>. </p>
</dd></dl>

<dl class="cpp function">
<dt class="sig sig-object cpp" id="_CPPv4N5cudaq14observe_result14observe_resultERdRK7spin_op13sample_result">
<span class="target" id="classcudaq_1_1observe__result_1a5c45da1fa8c9d64c68d2cbe5c9598482"></span><span class="k"><span class="pre">inline</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">observe_result</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">double</span></span><span class="w"> </span><span class="p"><span class="pre">&amp;</span></span><span class="n sig-param"><span class="pre">e</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><a class="reference internal" href="#_CPPv4N5cudaq7spin_opE" title="cudaq::spin_op"><span class="n"><span class="pre">spin_op</span></span></a><span class="w"> </span><span class="p"><span class="pre">&amp;</span></span><span class="n sig-param"><span class="pre">H</span></span>, <a class="reference internal" href="#_CPPv4N5cudaq13sample_resultE" title="cudaq::sample_result"><span class="n"><span class="pre">sample_result</span></span></a><span class="w"> </span><span class="n sig-param"><span class="pre">counts</span></span><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv4N5cudaq14observe_result14observe_resultERdRK7spin_op13sample_result" title="Permalink to this definition">¶</a><br /></dt>
<dt class="sig sig-object cpp" id="_CPPv4N5cudaq14observe_result14observe_resultEdRK7spin_op13sample_result">
<span class="target" id="classcudaq_1_1observe__result_1a0f3241a87d3924049763257e7f4caece"></span><span class="k"><span class="pre">inline</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">observe_result</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">double</span></span><span class="w"> </span><span class="n sig-param"><span class="pre">e</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><a class="reference internal" href="#_CPPv4N5cudaq7spin_opE" title="cudaq::spin_op"><span class="n"><span class="pre">spin_op</span></span></a><span class="w"> </span><span class="p"><span class="pre">&amp;</span></span><span class="n sig-param"><span class="pre">H</span></span>, <a class="reference internal" href="#_CPPv4N5cudaq13sample_resultE" title="cudaq::sample_result"><span class="n"><span class="pre">sample_result</span></span></a><span class="w"> </span><span class="n sig-param"><span class="pre">counts</span></span><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv4N5cudaq14observe_result14observe_resultEdRK7spin_op13sample_result" title="Permalink to this definition">¶</a><br /></dt>
<dd><p>Constructor, takes the precomputed expectation value for &lt;psi(x) | H | psi(x)&gt; for the given <a class="reference internal" href="#classcudaq_1_1spin__op"><span class="std std-ref">spin_op</span></a>. If this execution was shots based, also provide the <a class="reference internal" href="#classcudaq_1_1sample__result"><span class="std std-ref">sample_result</span></a> data containing counts for each term in H. </p>
</dd></dl>

Expand Down
2 changes: 1 addition & 1 deletion pr-1417/api/languages/python_api.html
Original file line number Diff line number Diff line change
Expand Up @@ -1696,7 +1696,7 @@ <h2>Data Types<a class="headerlink" href="#data-types" title="Permalink to this
<em class="property"><span class="pre">static</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">random</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#cudaq.SpinOperator.random" title="Permalink to this definition"></a></dt>
<dd><dl class="py function">
<dt class="sig sig-object py">
<span class="sig-name descname"><span class="pre">random</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">qubit_count</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.12)"><span class="pre">int</span></a></span></em>, <em class="sig-param"><span class="n"><span class="pre">term_count</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.12)"><span class="pre">int</span></a></span></em>, <em class="sig-param"><span class="n"><span class="pre">seed</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.12)"><span class="pre">int</span></a></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">3402724981</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><a class="reference internal" href="#cudaq.SpinOperator" title="cudaq.mlir._mlir_libs._quakeDialects.cudaq_runtime.SpinOperator"><span class="pre">cudaq.mlir._mlir_libs._quakeDialects.cudaq_runtime.SpinOperator</span></a></span></span></dt>
<span class="sig-name descname"><span class="pre">random</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">qubit_count</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.12)"><span class="pre">int</span></a></span></em>, <em class="sig-param"><span class="n"><span class="pre">term_count</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.12)"><span class="pre">int</span></a></span></em>, <em class="sig-param"><span class="n"><span class="pre">seed</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.12)"><span class="pre">int</span></a></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">2033498390</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><a class="reference internal" href="#cudaq.SpinOperator" title="cudaq.mlir._mlir_libs._quakeDialects.cudaq_runtime.SpinOperator"><span class="pre">cudaq.mlir._mlir_libs._quakeDialects.cudaq_runtime.SpinOperator</span></a></span></span></dt>
<dd></dd></dl>

<p>Return a random <a class="reference internal" href="#cudaq.SpinOperator" title="cudaq.SpinOperator"><code class="xref py py-class docutils literal notranslate"><span class="pre">SpinOperator</span></code></a> on the given number of qubits (<code class="code docutils literal notranslate"><span class="pre">qubit_count</span></code>) and composed of the given number of terms (<code class="code docutils literal notranslate"><span class="pre">term_count</span></code>). An optional seed value may also be provided.</p>
Expand Down
4 changes: 2 additions & 2 deletions pr-1417/examples/python/tutorials/deutschs_algorithm.html
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ <h2>XOR <span class="math notranslate nohighlight">\(\oplus\)</span><a class="he
</section>
<section id="Quantum-oracles">
<h2>Quantum oracles<a class="headerlink" href="#Quantum-oracles" title="Permalink to this heading"></a></h2>
<p><img alt="2d00651985974bfb875253697cf305c7" class="no-scaled-link" src="../../../_images/oracle.png" style="width: 300px; height: 150px;" /></p>
<p><img alt="14c50fc68bc1458490e417fab4c49687" class="no-scaled-link" src="../../../_images/oracle.png" style="width: 300px; height: 150px;" /></p>
<p>Suppose we have <span class="math notranslate nohighlight">\(f(x): \{0,1\} \longrightarrow \{0,1\}\)</span>. We can compute this function on a quantum computer using oracles which we treat as black box functions that yield the output with an appropriate sequence of logic gates.</p>
<p>Above you see an oracle represented as <span class="math notranslate nohighlight">\(U_f\)</span> which allows us to transform the state <span class="math notranslate nohighlight">\(\ket{x}\ket{y}\)</span> into:</p>
<div class="math notranslate nohighlight">
Expand Down Expand Up @@ -639,7 +639,7 @@ <h2>Quantum parallelism<a class="headerlink" href="#Quantum-parallelism" title="
<h2>Deutschs’ Algorithm:<a class="headerlink" href="#Deutschs'-Algorithm:" title="Permalink to this heading"></a></h2>
<p>Our aim is to find out if <span class="math notranslate nohighlight">\(f: \{0,1\} \longrightarrow \{0,1\}\)</span> is a constant or a balanced function? If constant, <span class="math notranslate nohighlight">\(f(0) = f(1)\)</span>, and if balanced, <span class="math notranslate nohighlight">\(f(0) \neq f(1)\)</span>.</p>
<p>We step through the circuit diagram below and follow the math after the application of each gate.</p>
<p><img alt="82601eabfe6848a491495d2ad5d7d5c5" class="no-scaled-link" src="../../../_images/deutsch.png" style="width: 500px; height: 210px;" /></p>
<p><img alt="e323b577c6a94daa9dd9fce808dd1b74" class="no-scaled-link" src="../../../_images/deutsch.png" style="width: 500px; height: 210px;" /></p>
<div class="math notranslate nohighlight">
\[\ket{\psi_0} = \ket{01}
\tag{1}\]</div>
Expand Down
2 changes: 1 addition & 1 deletion pr-1417/genindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ <h2 id="C">C</h2>
</li>
<li><a href="api/languages/cpp_api.html#_CPPv4N5cudaq14observe_result14id_coefficientEv">cudaq::observe_result::id_coefficient (C++ function)</a>
</li>
<li><a href="api/languages/cpp_api.html#_CPPv4N5cudaq14observe_result14observe_resultERdRK7spin_op">cudaq::observe_result::observe_result (C++ function)</a>, <a href="api/languages/cpp_api.html#_CPPv4N5cudaq14observe_result14observe_resultERdRK7spin_op13sample_result">[1]</a>
<li><a href="api/languages/cpp_api.html#_CPPv4N5cudaq14observe_result14observe_resultEdRK7spin_op">cudaq::observe_result::observe_result (C++ function)</a>, <a href="api/languages/cpp_api.html#_CPPv4N5cudaq14observe_result14observe_resultEdRK7spin_op13sample_result">[1]</a>
</li>
<li><a href="api/languages/cpp_api.html#_CPPv4N5cudaq14observe_resultcvdEv">cudaq::observe_result::operator double (C++ function)</a>
</li>
Expand Down
Binary file modified pr-1417/objects.inv
Binary file not shown.
2 changes: 1 addition & 1 deletion pr-1417/searchindex.js

Large diffs are not rendered by default.

90 changes: 90 additions & 0 deletions pr-1417/sphinx/examples/cpp/providers/nvqc_qml.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Compile and run with:
// ```
// nvq++ --target nvqc nvqc_qml.cpp -o out.x
// ./out.x
// ```
// Assumes a valid NVQC API key has been set in the `NVQC_API_KEY` environment
// variable. Please refer to the documentations for information about how to
// attain NVQC API key.
#include <ctime>
#include <cudaq.h>
#include <cudaq/algorithm.h>
#include <cudaq/optimizers.h>
#include <random>

struct kernel {

auto operator()(const int n_qubits,
const std::vector<double> parameters) __qpu__ {

cudaq::qvector qubits(n_qubits);
h(qubits);

for (size_t i = 0; i < n_qubits; i++) {
rx(parameters[i], qubits[i]);
}

for (size_t i = 0; i < n_qubits; i++) {
ry(parameters[i + n_qubits], qubits[i]);
}

h(qubits);

for (size_t i = 0; i < n_qubits; i++) {
rz(parameters[i + n_qubits * 2], qubits[i]);
}

for (size_t i = 0; i < n_qubits; i += 2) {
cx(qubits[i], qubits[i + 1]);
}

for (size_t i = 0; i < n_qubits; i++) {
rz(parameters[i + n_qubits * 2], qubits[i]);
}

for (size_t i = 0; i < n_qubits; i += 2) {
cy(qubits[i], qubits[i + 1]);
}

for (size_t i = 0; i < n_qubits; i++) {
ry(parameters[i + n_qubits], qubits[i]);
}

for (size_t i = 0; i < n_qubits; i += 2) {
cz(qubits[i], qubits[i + 1]);
}

x(qubits);
y(qubits);
h(qubits);
}
};

std::vector<double> initial_parameters(int n_parameters, int seed) {

std::default_random_engine generator(seed);
std::uniform_real_distribution<float> distribution(0.0, 1.0);
std::vector<double> parameters(n_parameters);

for (size_t i = 0; i < n_parameters; i++) {
parameters[i] = distribution(generator);
}
return parameters;
}

int main() {

const int n_qubits = 26;
const int n_parameters = 3 * n_qubits;
std::vector<double> parameters = initial_parameters(n_parameters, 13);
auto h = cudaq::spin::z(0);

auto start = clock();
auto exp_val = cudaq::observe(kernel{}, h, n_qubits, parameters);
auto end = clock();

printf("Expectation Value: %f \n", exp_val.expectation());
printf("Runtime: %fs \n", float(end - start) / float(CLOCKS_PER_SEC));

return 0;
}

0 comments on commit 43e03d7

Please sign in to comment.