Skip to content

Commit

Permalink
Merge pull request #15 from MatP1337/main
Browse files Browse the repository at this point in the history
Fixed some typos and equation in QUBO tutorial
  • Loading branch information
bastibock authored Jan 25, 2024
2 parents e724ce8 + da2ffe5 commit a4c353c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ Again, since QAOA is probabilistic, every run returns different results. You can
Approximation ratio: 0.8639683333333332
Variance: 0.007950404719444411

As we can see, the TQA initialization tends to return more consistently better approximation ratios. Feel free to play around with the :meth:`benchmark <qrisp.qaoa.QAOAProblem.benchmark>` method (or leave it running over night) to try and compare the two approaches further.
As we can see, the TQA initialization tends to return better approximation ratios more consistently. Feel free to play around with the :meth:`benchmark <qrisp.qaoa.QAOAProblem.benchmark>` method (or leave it running over night) to try and compare the two approaches further.

Summary and motivation
----------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ QUBO as a QAOAProblem Problem Instance
In this tutorial you’ll be guided through the process of defining a new phase separator to be used within the scope of the :ref:`Alternating Operator Ansatz <AOA>` focussed on solving various QUBO problems with only needing the QUBO matrix $Q$ as an input.
QUBO, or `Quadratic Unconstrained Binary Optimization <https://en.wikipedia.org/wiki/Quadratic_unconstrained_binary_optimization>`_, is a type of problem that involves optimizing a quadratic function of binary variables.

After first translating the QUBO Hamiltonian $H_C$ from the binary basis $x_i$​ to the state basis with the translation $x_i\rightarrow \frac{\mathbb{I}-Z_i}{2}$ we'll construct the phase separator unitary and run QAOA to solve the set partitioning problem.
After first translating the QUBO Hamiltonian $H_C$ from the binary basis $x_i$​ to the state basis with the translation $x_i\rightarrow \frac{\mathbb{I}-Z_i}{2}$, we'll construct the phase separator unitary and run QAOA to solve the set partitioning problem.

Not to get too ahed of ourselves with how to do it, let's first show how to solve a QUBO problem using Qrisp. To obtain the optimal solutions to a problem, we only need to know the QUBO matrix $Q$. In order to get familiar with that, we propose having a look at the `Tutorial on Formulating and Using QUBO Models <https://arxiv.org/abs/1811.11538>`_, which explains how to derive such a matrix $Q$.
Not to get too ahead of ourselves with how to do it, let's first show how to solve a QUBO problem using Qrisp. To obtain the optimal solutions to a problem, we only need to know the QUBO matrix $Q$. In order to get familiar with that, we propose having a look at the `Tutorial on Formulating and Using QUBO Models <https://arxiv.org/abs/1811.11538>`_, which explains how to derive such a matrix $Q$.

One can then simply minimize the cost function $\min C(x)=\min x^TQx$ for binary variables $x_i\in\{0,1\}$. This is done either by sending the QUBO matrix $Q$ to the annealer/quantum computer, which calculates and returns the bitstrings with corresponding values of $C(x)$. The lower the $C(x)$, the better the solution. In other words, we want to find the Hamiltonian $H_C|x\rangle=C(x)|x\rangle$.

Let's borrow the QUBO matrix (explained in depth in the above mentioned `tutorial <https://arxiv.org/abs/1811.11538>`_) for the set partitioning problem. The QUBO matrix in that case is

$$Q = \\begin{pmatrix}-17&10&10&10&0&20\\\\10&-18&10&10&10&20\\\\10&10&-29&10&20&20\\\\10&10&10&-19&10&10\\\\0&10&20&10&-17&10\\\\20&20&20&10&10&-28\\end{pmatrix}$$

Usually, QUBO matrices are upper triangular (by convention) - this means, that only elements above the diagonal (with the diagonal included) are not equal to zero. This is because in QUBO, the variables are binary, which results in the identity $q_iq_j=q_jq_i$ for binary variables. Because of this, the elements above the diagonal are doubled compared to the symmetrized version of $Q$ we wrote down above.
Usually, QUBO matrices are upper triangular (by convention) - this means that only elements above the diagonal (with the diagonal included) are not equal to zero. This is because the variables in QUBO problems are binary, which results in the identity $q_iq_j=q_jq_i$ for binary variables. Because of this, the elements above the diagonal are doubled compared to the symmetrized version of $Q$ we wrote down above.

Of course it's easy to go from the conventional QUBO $Q_{\text{up}\Delta}$ formulation to the symmetrized $Q_\text{sym}$ QUBO: $Q_\text{sym}=\frac{1}{2}\big(Q_{\text{up}\Delta}+Q_{\text{up}\Delta}^T\big)$, and back:
::
Expand Down Expand Up @@ -65,12 +65,12 @@ Swapping the indices $i$ and $j$ in the last sum, and using the identity $Z_iZ_i

$$\\begin{align}\\begin{split}H_C&=\\frac{1}{4}\\sum_{i\\neq j}Q_{ij}Z_iZ_j-\\frac{1}{4}\\sum_{i=1}^n\\sum_{j=1}^n(Q_{ij}+Q_{ji})Z_i&+\\frac{1}{4}\\sum_{i,j=1}^nQ_{ij}+\\frac{1}{4}\\sum_{i=1}^nQ_{ii}\\end{split}\\end{align}$$

Note that for each single $Z_i$ we sum the $i$th row and the $i$th column of the matrix $Q$.
Note that for each single $Z_i$ we sum the $i$-th row and the $i$-th column of the matrix $Q$.


For the cost operator $U_C$, which we feed into ``QAOAProblem``, we get

$$\\begin{align}\\begin{split}U_C=e^{-i\\gamma H_C}\&=\\prod_{i,j=1}^nR_{Z_iZ_j}\\Bigg(\\frac{\\gamma}{2}Q_{ij}\\Bigg)\\times\\prod_{i=1}^nR_{Z_i}\\Bigg(-\\frac{\\gamma}{2}\\bigg(\\sum_{j=1}^n(Q_{ij}+Q_{ji})\\bigg)\\Bigg)\\\\&=\\times\\exp\\Bigg(-\\frac{i\\gamma}{4}\\sum_{i,j=1}^nQ_{ij}-\\frac{i\\gamma}{4}\\sum_{i=1}^nQ_{ii}\\Bigg)\\end{split}\\end{align}$$
$$\\begin{align}\\begin{split}U_C=e^{-i\\gamma H_C}=\&\\prod_{i,j=1}^nR_{Z_iZ_j}\\Bigg(\\frac{\\gamma}{2}Q_{ij}\\Bigg)\\times\\prod_{i=1}^nR_{Z_i}\\Bigg(-\\frac{\\gamma}{2}\\bigg(\\sum_{j=1}^n(Q_{ij}+Q_{ji})\\bigg)\\Bigg)\\\\&\\times\\exp\\Bigg(-\\frac{i\\gamma}{4}\\sum_{i,j=1}^nQ_{ij}-\\frac{i\\gamma}{4}\\sum_{i=1}^nQ_{ii}\\Bigg)\\end{split}\\end{align}$$

Here, the last factor correspods to a global phase.

Expand Down Expand Up @@ -121,12 +121,12 @@ Like we did for :ref:`MaxCut <MaxCutQAOA>` and :ref:`M$\\kappa$CS <MkCSQAOA>` we
return QAOAProblem(create_QUBO_cost_operator(Q), RX_mixer, create_QUBO_cl_cost_function(Q),init_type=init_type)

That's it for the necessary ingredients you learned about in the :ref:`QAOA theory 101 section <QAOA101>`! Let's solve the set partitioning problem from above using this newly acquired information, and combine with how we already ran the QAOA algorithm using the ``.run`` method:
That's it for the necessary ingredients you learned about in the :ref:`QAOA theory 101 section <QAOA101>`! Let's solve the set partitioning problem from above using this newly acquired information, and combine with how we already ran the QAOA algorithm using the :meth:`run <qrisp.qaoa.QAOAProblem.run>` method:

- define the QUBO matrix $Q$,
- define the quantum argument `qarg` as a :ref:`QuantumArray <QuantumArray>` of :ref:`QuantumVariables <QuantumVariable>`,
- create the QUBO instance using `QUBO_problem` we defined above,
- run the algorithm using the `.run method <file:///C:/Users/mat70593/Desktop/qrisp_gitlab/qrisp/documentation/build/html/reference/Algorithms/qaoa/generated/qrisp.qaoa.QAOAProblem.run.html#qrisp.qaoa.QAOAProblem.run>`_, and last but not least,
- define the quantum argument ``qarg`` as a :ref:`QuantumArray <QuantumArray>` of :ref:`QuantumVariables <QuantumVariable>`,
- create the QUBO instance using ``QUBO_problem`` we defined above,
- run the algorithm using the :meth:`run <qrisp.qaoa.QAOAProblem.run>` method, and last but not least,
- examine the QAOA solutions with the highest probabilities for classical post processing: compute the cost functions, sort the solutions by their cost in ascending order, and print the solutions with their costs.

These are exactly the pieces in the mosaic of code that ``solve_QUBO`` consists of and performs:
Expand Down
6 changes: 3 additions & 3 deletions documentation/source/reference/Examples/Shor.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Try running the code on the website yourself and feel free to try the algorithm
As we will see in the next example, number 65 is easy to crack in terms of the private and public key pairings, which is used for encryption. However, the bacis principles of encryption remain the same even with using much greater numbers.

A tale of encryption and decryption
----------------------------------
-----------------------------------

Imagine a scenario where two characters, Alice and Bob, are trying to exchange a secure message. They decide to use RSA encryption, a popular method that uses the product of two prime numbers as a key. In this case, they choose 5 and 13 as their private keys, and 7 as one of the public keys.
::
Expand All @@ -46,7 +46,7 @@ He ran the command and simply smirked at the result and said "You've got that ri
New adder, no problem
---------------------

Stories like the one above are fun and exciting way to showcase the elegant approach of utilizing Eclipse Qrisp's high level structure. Learning from existing frameworks, however, it is also of utmost importance to ask ourselves the serious, hard hitting question of how to futureproof such an implementation. You asked the question, we got the answer - look under the hood and delve into the nitty-gritty.
Stories like the one above are fun and exciting way to showcase the elegant approach of utilizing Eclipse Qrisp's high level structure. Learning from existing frameworks, however, it is also of utmost importance to ask ourselves the serious, hard hitting question of how to futureproof such an implementation. You've asked the question, we've got the answer - let's look under the hood and delve into the nitty-gritty!

As elaborated on in the :ref:`Fault-Tolerant compilation tutorial <ft_compilation_shor>`, the Qrisp implementation of Shor's algorithm allows you to provide an arbitrary adder for the execution of the required arithmetic. With our Qrispy structure one can write ones own adder, or implement a shiny new one future research publications might bring, and test its performance claims.

Expand All @@ -60,7 +60,7 @@ As of right now, the following list of adders have been pre-implemented:

* The :meth:`qcla <qrisp.qcla>` (`paper <https://arxiv.org/abs/2304.02921>`_) requires quite a lot of ancillae but has only logarithmic scaling when it comes to T-depth. It is faster than the Gidney adder for any input size larger than 7.

Using a diffent adder is as easy as adding an inpl_adder keyword to the QuantumModulus variable. Literally!
Using a diffent adder is as easy as adding an ``inpl_adder`` keyword to the :ref:`QuantumModulus <QuantumModulus>` variable. Literally!

Let's provide an example of benchmarking the :meth:`gidney_adder <qrisp.gidney_adder>` and compare it to the :meth:`qcla <qrisp.qcla>` on the operation most relevant for Shor's algorithm: Controlled modular in-place multiplication.

Expand Down

0 comments on commit a4c353c

Please sign in to comment.