diff --git a/algorithms/bernstein_vazirani/bernstein_vazirani.ipynb b/algorithms/bernstein_vazirani/bernstein_vazirani.ipynb index b42406f3e..b59aa49a8 100644 --- a/algorithms/bernstein_vazirani/bernstein_vazirani.ipynb +++ b/algorithms/bernstein_vazirani/bernstein_vazirani.ipynb @@ -141,12 +141,12 @@ "@qfunc\n", "def bv_function(a: CInt, x: QArray):\n", " aux = QBit()\n", + " allocate(aux)\n", " within_apply(\n", " lambda: hadamard_transform(x),\n", - " lambda: within_apply(\n", - " lambda: (allocate(aux), X(aux), H(aux)), lambda: bv_predicate(a, x, aux)\n", - " ),\n", - " )" + " lambda: within_apply(lambda: (X(aux), H(aux)), lambda: bv_predicate(a, x, aux)),\n", + " )\n", + " free(aux)" ] }, { diff --git a/algorithms/bernstein_vazirani/bernstein_vazirani.qmod b/algorithms/bernstein_vazirani/bernstein_vazirani.qmod index b8286ab3c..55878b707 100644 --- a/algorithms/bernstein_vazirani/bernstein_vazirani.qmod +++ b/algorithms/bernstein_vazirani/bernstein_vazirani.qmod @@ -8,17 +8,18 @@ qperm bv_predicate(a: int, const x: qbit[], res: qbit) { qfunc bv_function(a: int, x: qbit[]) { aux: qbit; + allocate(aux); within { hadamard_transform(x); } apply { within { - allocate(aux); X(aux); H(aux); } apply { bv_predicate(a, x, aux); } } + free(aux); } qfunc main(output x: qnum<5>) { diff --git a/algorithms/simon/simon.ipynb b/algorithms/simon/simon.ipynb index f78755064..5da5a5bdf 100644 --- a/algorithms/simon/simon.ipynb +++ b/algorithms/simon/simon.ipynb @@ -72,7 +72,7 @@ "source": [ "### Quantum Part\n", "\n", - "The quantum part of the algorithm is rather simple, calling the quantum implementation of $f(x)$, between two calls of the hadamard transform. The call of $f$ is done out-of-place, onto a quantum variable $y$, whereas only the final state of $x$ is relevant to the classical postprocess to follow. " + "The quantum part of the algorithm is rather simple, calling the quantum implementation of $f(x)$, between two calls of the hadamard transform. The call of $f$ is done out-of-place, onto a quantum variable $res$, whereas only the final state of $x$ is relevant to the classical postprocess to follow. " ] }, { @@ -86,8 +86,7 @@ "\n", "\n", "@qfunc\n", - "def simon_qfunc(f_qfunc: QCallable[QNum, Output[QNum]], x: QNum):\n", - " res = QNum()\n", + "def simon_qfunc(f_qfunc: QCallable[QNum, Output[QNum]], x: QNum, res: Output[QNum]):\n", " within_apply(lambda: hadamard_transform(x), lambda: f_qfunc(x, res))" ] }, @@ -376,9 +375,9 @@ "\n", "\n", "@qfunc\n", - "def main(x: Output[QNum[NUM_QUBITS]]):\n", + "def main(x: Output[QNum[NUM_QUBITS]], res: Output[QNum]):\n", " allocate(x)\n", - " simon_qfunc(lambda x, res: simon_qfunc_simple(S_SECRET, x, res), x)\n", + " simon_qfunc(lambda x, res: simon_qfunc_simple(S_SECRET, x, res), x, res)\n", "\n", "\n", "qmod_2 = create_model(\n", @@ -411,7 +410,7 @@ "with ExecutionSession(qprog_2, execution_preferences=prefs_more_shots) as es:\n", " result_2 = es.sample()\n", "\n", - "bitstrings = result_2.dataframe[\"bitstring\"].tolist()\n", + "bitstrings = [f\"{x:0{NUM_QUBITS}b}\" for x in result_2.dataframe[\"x\"].tolist()]\n", "reversed_bitstrings = [b[::-1] for b in bitstrings]\n", "samples = [[int(bit) for bit in b] for b in reversed_bitstrings]" ] @@ -602,9 +601,11 @@ "outputs": [], "source": [ "@qfunc\n", - "def main(x: Output[QNum[NUM_QUBITS]]):\n", + "def main(x: Output[QNum[NUM_QUBITS]], res: Output[QNum]):\n", " allocate(x)\n", - " simon_qfunc(lambda x, res: simon_qfunc_with_bipartite_s(PARTITION_INDEX, x, res), x)\n", + " simon_qfunc(\n", + " lambda x, res: simon_qfunc_with_bipartite_s(PARTITION_INDEX, x, res), x, res\n", + " )\n", "\n", "\n", "qmod_4 = create_model(main)\n", @@ -643,7 +644,7 @@ "with ExecutionSession(qprog_4, execution_preferences=prefs_more_shots) as es:\n", " result_4 = es.sample()\n", "\n", - "bitstrings = result_4.dataframe[\"bitstring\"].tolist()\n", + "bitstrings = [f\"{x:0{NUM_QUBITS}b}\" for x in result_4.dataframe[\"x\"].tolist()]\n", "reversed_bitstrings = [b[::-1] for b in bitstrings]\n", "samples = [[int(bit) for bit in b] for b in reversed_bitstrings]" ] diff --git a/algorithms/simon/simon_example.qmod b/algorithms/simon/simon_example.qmod index 834053981..81159bfce 100644 --- a/algorithms/simon/simon_example.qmod +++ b/algorithms/simon/simon_example.qmod @@ -1,5 +1,4 @@ -qfunc simon_qfunc(f_qfunc: qfunc (qnum, output qnum), x: qnum) { - res: qnum; +qfunc simon_qfunc(f_qfunc: qfunc (qnum, output qnum), x: qnum, output res: qnum) { within { hadamard_transform(x); } apply { @@ -11,9 +10,9 @@ qperm simon_qfunc_simple(s: int, const x: qnum, output res: qnum) { res = min(x, x ^ s); } -qfunc main(output x: qnum<5>) { +qfunc main(output x: qnum<5>, output res: qnum) { allocate(x); simon_qfunc(lambda(x, res) { simon_qfunc_simple(6, x, res); - }, x); + }, x, res); } diff --git a/algorithms/simon/simon_shallow_example.qmod b/algorithms/simon/simon_shallow_example.qmod index d9305ae38..e34984140 100644 --- a/algorithms/simon/simon_shallow_example.qmod +++ b/algorithms/simon/simon_shallow_example.qmod @@ -1,5 +1,4 @@ -qfunc simon_qfunc(f_qfunc: qfunc (qnum, output qnum), x: qnum) { - res: qnum; +qfunc simon_qfunc(f_qfunc: qfunc (qnum, output qnum), x: qnum, output res: qnum) { within { hadamard_transform(x); } apply { @@ -18,9 +17,9 @@ qperm simon_qfunc_with_bipartite_s(partition_index: int, const x: qbit[], output } } -qfunc main(output x: qnum<6>) { +qfunc main(output x: qnum<6>, output res: qnum) { allocate(x); simon_qfunc(lambda(x, res) { simon_qfunc_with_bipartite_s(4, x, res); - }, x); + }, x, res); }