Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dbi notebooks to documentation #1337

Merged
merged 23 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b867fec
doc: Add notebooks to sphinx
andrea-pasquale May 29, 2024
a82406d
Merge branch 'dbi_cost_functions' into dbi_cost_doc
andrea-pasquale May 29, 2024
4c88e60
fix: Fix energy fluctuations
andrea-pasquale May 29, 2024
7022262
tests: Code improvements and tests
andrea-pasquale May 29, 2024
30cbe29
feat: Tests passing on GPU
andrea-pasquale May 29, 2024
492202a
cleanup: Remove unused features
andrea-pasquale May 30, 2024
5e45c38
Merge branch 'dbi_cost_functions' into dbi_cost_doc
andrea-pasquale May 30, 2024
2547b21
feat: Add symblic link to doc
andrea-pasquale May 30, 2024
8beac7c
Merge branch 'dbi_cost_functions' into dbi_cost_doc
andrea-pasquale May 30, 2024
4d18650
feat: Update notebooks
andrea-pasquale May 30, 2024
0fbafa6
Merge branch 'dbi_cost_functions' into dbi_cost_doc
andrea-pasquale May 30, 2024
dfbad7f
doc: Update notebooks
andrea-pasquale May 30, 2024
01c99e8
fix: Fixing documentation
andrea-pasquale May 31, 2024
8c9c610
Update README.md
marekgluza May 31, 2024
5f24028
feat: Disable progressbar for hyperopt
andrea-pasquale May 31, 2024
392f939
fix: Fix README link
andrea-pasquale May 31, 2024
6dfa80a
more about the D operators
marekgluza May 31, 2024
90fbc03
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 31, 2024
5671d03
fix: Attempt to fix CI
andrea-pasquale May 31, 2024
410dcd0
test: execute and clean all dbi notebooks
MatteoRobbiati May 31, 2024
6130d09
fix: rm old dbi notebook from doc
MatteoRobbiati May 31, 2024
c4bfc1e
chore: Add ipykernel as doc dep
andrea-pasquale Jun 1, 2024
b5a4202
chore: Clean notebook
andrea-pasquale Jun 1, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion doc/source/code-examples/applications-by-algorithm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,13 @@ Diagonalization Algorithms
.. toctree::
:maxdepth: 1

tutorials/dbi/dbi.ipynb
tutorials/dbi/README.md

tutorials/dbi/dbi_cost_functions.ipynb
tutorials/dbi/dbi_gradient_descent_strategies.ipynb
tutorials/dbi/dbi_group_commutator_tests.ipynb
tutorials/dbi/dbi_scheduling.ipynb
tutorials/dbi/dbi_strategies_compare.ipynb
tutorials/dbi/dbi_strategy_Ising_model.ipynb
tutorials/dbi/dbi_strategy_Pauli-Z.ipynb
tutorials/dbi/dbi_tutorial_basic_intro.ipynb
1 change: 0 additions & 1 deletion doc/source/code-examples/applications-by-topic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ Quantum Physics
tutorials/bell-variational/README.md
tutorials/falqon/README.md
tutorials/grover/README.md
tutorials/dbi/dbi.ipynb

Quantum Machine Learning
^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 0 additions & 2 deletions doc/source/code-examples/applications.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ Quantum Physics
tutorials/bell-variational/README.md
tutorials/falqon/README.md
tutorials/grover/README.md
tutorials/dbi/dbi.ipynb


Quantum Machine Learning
^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
1 change: 1 addition & 0 deletions doc/source/code-examples/tutorials/dbi/README.md
1 change: 0 additions & 1 deletion doc/source/code-examples/tutorials/dbi/dbi.ipynb

This file was deleted.

1 change: 0 additions & 1 deletion double_bracket_flow_as_a_diagonalization_quantum_algorithm
Submodule double_bracket_flow_as_a_diagonalization_quantum_algorithm deleted from 808dd6
54 changes: 54 additions & 0 deletions examples/dbi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Double-bracket quantum algorithms

Qibo features a model implementing double-bracke quantum algorithms (DBQAs) which are helpful for approximating eigenstates based on the ability to run the evolution under the input Hamiltonian.

More specifically, given a Hamiltonian $H_0$, how can we find a circuit which after applying to the reference state (usually $|0\rangle^{\otimes L}$ for $L$ qubits) will approximate an eigenstate?

A standard way is to run variational quantum circuits. For example, Qibo already features the `VQE` model [2] which provides the implementation of the variational quantum eigensolver framework.
DBQAs allow to go beyond VQE in that they take a different approach to compiling the quantum circuit approximating the eigenstate.

## What is the unitary of DBQA?

Given $H_0$ we begin by assuming that we were given a diagonal and hermitian operator $D_0$ and a time $s_0$.
The `dbi` module provides numerical strategies for selecting them.
For any such choice we define the bracket
$$ W_0 = [D_0, H_0]$$
and the double-bracket rotation (DBR) of the input Hamiltonian to time $s$
$$H_0(s) = e^{sW} H e^{- s W}$$

### Why are double-bracket rotations useful?
We can show that the magnitude of the off-diagonal norms will decrease.
For this let us set the notation that $\sigma(A)$ is the restriction to the off-diagonal of the matrix A.
In `numpy` this can be implemented by `\sigma(A) = A-np.diag(A)`. In Qibo we implement this as
https://github.com/qiboteam/qibo/blob/8c9c610f5f2190b243dc9120a518a7612709bdbc/src/qibo/models/dbi/double_bracket.py#L145-L147
which is part of the basic `DoubleBracketIteration` class in the `dbi` module.

With this notation we next use the Hilbert-Schmidt scalar product and norm to measure the progress of diagonalization
$$||\sigma(H_0(s))||^2- ||\sigma (H_0 )||^2= -2s \langle W, [H,\sigma(H)\rangle+O(s^2)$$
This equation tells us that as long as the scalar product $\langle W, [H,\sigma(H)\rangle$ is positive then after the DBR the magnitude of the off-diagonal couplings in $H_0(s)$ is less than in $H_0$.

For the implementation of the DBR unitary $U_0(s) = e^{-s W_0}$ see
https://github.com/qiboteam/qibo/blob/363a6e5e689e5b907a7602bd1cc8d9811c60ee69/src/qibo/models/dbi/double_bracket.py#L68

### How to choose $D$?

For theoretical considerations the canonical bracket is useful.
For this we need the notation of the dephasing channel $\Delta(H)$ which is equivalent to `np.diag(h)`.
$M = [\Delta(H),\sigma(H)]= [H,\sigma(H)]= [\Delta(H),H]$
The canonical bracket appears on its own in the monotonicity relation above and gives an unconditional reduction of the magnitude of the off-diagonal terms
$$||\sigma(H_0(s))||^2- ||\sigma (H_0 )||^2= -2s \|M\|^2+O(s^2)$$
- the multi qubit Pauli Z generator with $Z(\mu) = (Z_1)^{\mu_1}\ldots (Z_L)^{\mu_L}$ where we optimize over all binary strings $\mu\in \{0,1\}^L$
- the magnetic field $D = \sum_i B_i Z_i$
- the two qubit Ising model $D = \sum_i B_i Z_i + \sum_{i,j} J_{i,j} Z_i Z_j$, please follow the tutorial by Matteo and use the QIBO ising model for that with $h=0$


### How to choose s?






[1] https://arxiv.org/abs/2206.11772

[2] https://github.com/qiboteam/vqe-sun
252 changes: 35 additions & 217 deletions examples/dbi/dbi_cost_functions.ipynb

Large diffs are not rendered by default.

46 changes: 11 additions & 35 deletions examples/dbi/dbi_gradient_descent_strategies.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
"metadata": {},
"outputs": [],
"source": [
"from qibo.models.dbi.double_bracket import *\n",
"from qibo.models.dbi.utils import *\n",
"from qibo.models.dbi.utils_dbr_strategies import *\n",
"from qibo.models.dbi.double_bracket import DoubleBracketIteration, DoubleBracketGeneratorType, DoubleBracketScheduling, DoubleBracketCostFunction\n",
"from qibo.models.dbi.utils import generate_pauli_operator_dict, decompose_into_pauli_basis, params_to_diagonal_operator, ParameterizationTypes\n",
"from copy import deepcopy\n",
"from qibo.models.dbi.utils_dbr_strategies import gradient_descent\n",
"import numpy as np\n",
"from qibo import set_backend, hamiltonians\n",
"from qibo.hamiltonians import Hamiltonian\n",
Expand All @@ -44,7 +45,7 @@
" except TypeError:\n",
" im = ax.imshow(np.absolute(matrix.get()), cmap=\"inferno\")\n",
" fig.colorbar(im, ax=ax)\n",
" \n",
"\n",
"def s_hist_to_plot(s_hist):\n",
" # convert list of step durations taken to plotable\n",
" s_plot = [0] * len(s_hist)\n",
Expand Down Expand Up @@ -103,7 +104,7 @@
"pauli_operator_dict = generate_pauli_operator_dict(nqubits)\n",
"pauli_operators = list(pauli_operator_dict.values())\n",
"# let initial d be approximation of $\\Delta(H)\n",
"d_coef_pauli = decompose_into_Pauli_basis(dbi.diagonal_h_matrix, pauli_operators=pauli_operators)\n",
"d_coef_pauli = decompose_into_pauli_basis(dbi.diagonal_h_matrix, pauli_operators=pauli_operators)\n",
"d_pauli = sum([d_coef_pauli[i]*pauli_operators[i] for i in range(nqubits)])\n",
"\n",
"# Computational basis\n",
Expand Down Expand Up @@ -136,7 +137,7 @@
"outputs": [],
"source": [
"# 1. Pauli-basis\n",
"NSTEPS = 10\n",
"NSTEPS = 5\n",
"dbi_pauli = deepcopy(dbi)\n",
"loss_hist_pauli, d_params_hist_pauli, s_hist_pauli = gradient_descent(dbi_pauli, NSTEPS, d_coef_pauli, ParameterizationTypes.pauli, pauli_operator_dict=pauli_operator_dict)"
]
Expand Down Expand Up @@ -227,7 +228,7 @@
"pauli_operator_dict = generate_pauli_operator_dict(nqubits)\n",
"pauli_operators = list(pauli_operator_dict.values())\n",
"# let initial d be approximation of $\\Delta(H)\n",
"d_coef_pauli = decompose_into_Pauli_basis(dbi.diagonal_h_matrix, pauli_operators=pauli_operators)\n",
"d_coef_pauli = decompose_into_pauli_basis(dbi.diagonal_h_matrix, pauli_operators=pauli_operators)\n",
"d_pauli = sum([d_coef_pauli[i]*pauli_operators[i] for i in range(nqubits)])\n",
"\n",
"# Computational basis\n",
Expand All @@ -249,7 +250,7 @@
"outputs": [],
"source": [
"# 1. Pauli-basis\n",
"NSTEPS = 10\n",
"NSTEPS = 3\n",
"dbi_pauli = deepcopy(dbi)\n",
"loss_hist_pauli, d_params_hist_pauli, s_hist_pauli = gradient_descent(dbi_pauli, NSTEPS, d_coef_pauli, ParameterizationTypes.pauli, pauli_operator_dict=pauli_operator_dict)"
]
Expand Down Expand Up @@ -334,34 +335,9 @@
")\n",
"assert (operator_element.diagonal() == params).all()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "DBF_qibo",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
}
},
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
36 changes: 5 additions & 31 deletions examples/dbi/dbi_group_commutator_tests.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
"import matplotlib.pyplot as plt\n",
"\n",
"from qibo import hamiltonians, set_backend\n",
"from qibo.models.dbi.double_bracket import DoubleBracketGeneratorType, DoubleBracketScheduling, DoubleBracketIteration, DoubleBracketCostFunction\n",
"from qibo.models.dbi.utils import *\n",
"from qibo.models.dbi.utils_scheduling import *"
"from qibo.models.dbi.double_bracket import DoubleBracketGeneratorType, DoubleBracketScheduling, DoubleBracketIteration, DoubleBracketCostFunction"
]
},
{
Expand Down Expand Up @@ -61,8 +59,8 @@
" dbi_eval.mode = DoubleBracketGeneratorType.single_commutator\n",
" dbi_eval(s_space[s],d=d)\n",
" off_diagonal_norm_diff[s,0] = dbi_eval.off_diagonal_norm\n",
" \n",
" \n",
"\n",
"\n",
"\n",
"plt.figure()\n",
"plt.plot(s_space, off_diagonal_norm_diff[:,0],label=r'$e^{sW}$')\n",
Expand Down Expand Up @@ -118,31 +116,7 @@
]
}
],
"metadata": {
"kernelspec": {
"display_name": "dbi",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "43f1f904380137ff38e17e8a93371c4872e6bababc18e270d8a0497ea5c7ea38"
}
}
},
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
56 changes: 28 additions & 28 deletions examples/dbi/dbi_scheduling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
"\n",
"from qibo import hamiltonians, set_backend\n",
"from qibo.models.dbi.double_bracket import DoubleBracketGeneratorType, DoubleBracketScheduling, DoubleBracketIteration\n",
"from qibo.models.dbi.utils import *\n",
"from qibo.models.dbi.utils_scheduling import *"
"from qibo.hamiltonians import SymbolicHamiltonian\n",
"from qibo.models.dbi.utils import str_to_symbolic, generate_Z_operators\n",
"from qibo.models.dbi.utils_scheduling import polynomial_step\n",
"from qibo.models.dbi.utils_dbr_strategies import select_best_dbr_generator"
]
},
{
Expand Down Expand Up @@ -326,7 +328,7 @@
"metadata": {},
"outputs": [],
"source": [
"NSTEPS = 8\n",
"NSTEPS = 3\n",
"scheduling_list = [DoubleBracketScheduling.grid_search,\n",
" DoubleBracketScheduling.hyperopt,\n",
" DoubleBracketScheduling.polynomial_approximation,\n",
Expand Down Expand Up @@ -424,32 +426,30 @@
" print(n, step)\n",
"print(dbi.choose_step(n=1))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
},
"vscode": {
"interpreter": {
"hash": "48caf7dabad7b721a854729228548373f17e53f40870080394d552284aea7c35"
}
}
},
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
Loading