Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick committed Apr 18, 2024
2 parents d016fea + 117dd55 commit 849d4c7
Show file tree
Hide file tree
Showing 36 changed files with 515 additions and 2,829 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/build_documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ jobs:
- name: Build documentation
working-directory: doc
run: |
make html SPHINXOPTS="-W --keep-going -T"
make html SPHINXOPTS="--keep-going -T"
# Above flags are:
# -W : turn warnings into errors
# --keep-going : do not stop after the first error
# -T : display a full traceback if a Python exception occurs
Expand Down
34 changes: 32 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ jobs:
os: windows-latest
qutip-version: "@master"
python-version: "3.10"
- case-name: ubuntu-python3.11
os: ubuntu-latest
qutip-version: "@master"
python-version: "3.11"
- case-name: macOs-python3.11
os: macOS-latest
qutip-version: "@master"
python-version: "3.11"
- case-name: windows-python3.11
os: windows-latest
qutip-version: "@master"
python-version: "3.11"
- case-name: ubuntu-python3.12
os: ubuntu-latest
qutip-version: "@master"
python-version: "3.12"
- case-name: macOs-python3.12
os: macOS-latest
qutip-version: "@master"
python-version: "3.12"
- case-name: windows-python3.12
os: windows-latest
qutip-version: "@master"
python-version: "3.12"

steps:
- uses: actions/checkout@v3
Expand All @@ -39,19 +63,25 @@ jobs:
run: |
python -m pip install 'git+https://github.com/qutip/qutip-qtrl.git'
- name: Install qutip-jax from GitHub
- name: Install jax
run: |
python -m pip install jax jaxlib
- name: Install qutip-jax
if: ${{ matrix.qutip-version == '@master'}}
run: |
python -m pip install 'git+https://github.com/qutip/qutip-jax.git'
python -m pip install qutip-jax
- name: Install qutip-qoc
# Installing in-place so that coveralls can locate the source code.
run: |
pip install -e .[full]
- name: Test with pytest and generate coverage report
run: |
pip install pytest-cov coveralls
pytest tests --strict-markers --cov=qutip_qoc --cov-report= --color=yes
- name: Upload to Coveralls
env:
GITHUB_TOKEN: ${{ secrets.github_token }}
Expand Down
7 changes: 4 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ repos:
- id: ruff
args: ["--fix", "--show-fixes"]
types_or: [python, pyi, jupyter]
exclude: "^src/qutip_qoc/[^/]*\\.ipynb$|^src/qutip_qoc/paper\\.py$"
exclude: "^src/qutip_qoc/[^/]*\\.ipynb"
- id: ruff-format
types_or: [python, pyi, jupyter]
exclude: "^src/qutip_qoc/[^/]*\\.ipynb$|^src/qutip_qoc/paper\\.py$"
exclude: "^src/qutip_qoc/[^/]*\\.ipynb"

# Also run Black on examples in the documentation
- repo: https://github.com/adamchainz/blacken-docs
Expand All @@ -80,14 +80,15 @@ repos:
hooks:
- id: mypy
files: ^(src|tests)
exclude: "^src/qutip_qoc/[^/]*\\.ipynb$|^src/qutip_qoc/paper\\.py$"
exclude: "^src/qutip_qoc/[^/]*\\.ipynb"
args: []
additional_dependencies:
- qutip
- qutip-qip
- qutip-qtrl
- numpy
- scipy
- jax
- jaxlib
- diffrax
- pytest
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ As with qutip-qtrl, the qutip-qoc package aims at providing advanced tools for t
Compared to other libraries for quantum optimal control, qutip-qoc puts additional emphasis on the physics layer and the interaction with the QuTiP package.
Along with the extended GOAT and JOPT algorithms the package offers support for both the CRAB and GRAPE methods defined in `qutip-qtrl`.

If you would like to know the future development plan and ideas, have a look at the [qutip documentation for ideas](https://qutip.org/docs/latest/development/ideas.html).
If you would like to know the future development plan and ideas, have a look at the [qutip roadmap and ideas](https://qutip.readthedocs.io/en/latest/development/roadmap.html).

## Quick start

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.0.dev1
0.0.1
6 changes: 6 additions & 0 deletions doc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ help:
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

html:
sphinx-apidoc -f -e -P -o ./apidoc/ ../src/qutip_qoc/
@$(SPHINXBUILD) -M html "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
@echo
@echo "Build finished. The HTML pages are in $(BUILDDIR)/html."
12 changes: 5 additions & 7 deletions doc/apidoc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ Internal interfaces to the optimal control features.
:toctree: apidoc/
:template: autosummary/module.rst

qutip_qtrl.pulseoptim

qutip_qoc.optimizer
qutip_qoc.goat
qutip_qoc.jopt
qutip_qoc.grape
qutip_qoc.crab
qutip_qoc._optimizer
qutip_qoc._goat
qutip_qoc._jopt
qutip_qoc._grape
qutip_qoc._crab
6 changes: 6 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ Changelog
*********


Version 0.1.0 (April xx, 2024)
+++++++++++++++++++++++++++++++++

TODO: Add release notes


Version 0.0.0 (December 26, 2023)
+++++++++++++++++++++++++++++++++

Expand Down
4 changes: 2 additions & 2 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def qutip_qoc_version():
# apidoc folder. This is particularly useful for modules with lots of
# functions/classes. However, pay attention that some api docs files are
# adjusted manually for better illustration and should not be overwritten.
autosummary_generate = True
autosummary_generate = ["apidoc/*.rst"]
autosummary_imported_members = True

# -- Options for biblatex ---------------------------------------
Expand All @@ -86,5 +86,5 @@ def qutip_qoc_version():
# -- Options for intersphinx ---------------------------------------

intersphinx_mapping = {
"qutip": ("https://qutip.org/docs/latest/", None),
"qutip": ("https://qutip.readthedocs.io/en/latest/", None),
}
13 changes: 6 additions & 7 deletions doc/guide/guide-control.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ We can use optimal control algorithms to determine a set of :math:`u_j` that wil

The GOAT Algorithm
===================
To be described in detail.

The GOAT method, like CRAB, operates with analytical control functions :cite:`GOAT`.
It constructs a system of coupled equations of motion, enabling the calculation of the derivative of the evolution operator (time-ordered) with respect to the control parameters, following numerical forward integration.
This implementation supports arbitrary control functions along with their corresponding derivatives.
To further accelerate convergence, our implementation enhances the original algorithm by providing the option to optimize controls with respect to the overall pulse duration.

The GRAPE algorithm
===================
Expand All @@ -86,7 +88,7 @@ If the objective is state-to-state transfer then :math:`X_0=\ket{\psi_0}` and th
A *figure of merit* or *fidelity* is some measure of how close the evolution is to the target, based on the control amplitudes in the timeslots. The typical figure of merit for unitary systems is the normalised overlap of the evolution and the target.

.. math::
\DeclareMathOperator{\tr}{tr}
f_{PSU} = \tfrac{1}{d} \big| \tr \{X_{targ}^{\dagger} X(T)\} \big|
where :math:`d` is the system dimension. In this figure of merit the absolute value is taken to ignore any differences in global phase, and :math:`0 \le f \le 1`. Typically the fidelity error (or *infidelity*) is more useful, in this case defined as :math:`\varepsilon = 1 - f_{PSU}`. There are many other possible objectives, and hence figures of merit.
Expand Down Expand Up @@ -252,9 +254,6 @@ It requires some extra arguments to prepare the optimization.
# boundaries for the parameters
p_bounds = q_bounds = [(-1, 1), (-1, 1)]
# time interval for the evolution
t_interval = qoc.TimeInterval(evo_time=1.)
Eventually, the optimization for a desired `fid_err_targ` can be started by calling the ``optimize`` function.
Expand All @@ -266,7 +265,7 @@ Eventually, the optimization for a desired `fid_err_targ` can be started by call
"p": {"guess": p_guess, "bounds": p_bounds},
"q": {"guess": q_guess, "bounds": q_bounds}
},
time_interval=t_interval,
tlist=np.linspace(0, 1, 100),
algorithm_kwargs={
"alg": "GOAT",
"fid_err_targ": 0.1,
Expand Down
2 changes: 1 addition & 1 deletion doc/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ In particular, following packages are necessary for running qutip-qoc:

.. code-block:: bash
numpy scipy cython qutip qutip-jax qutip-qtrl
numpy scipy jax jaxlib cython qutip qutip-jax qutip-qtrl
The following to packages are used for plotting and testing:

Expand Down
2 changes: 2 additions & 0 deletions doc/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
numpy>=1.16.6
scipy>=1.10.1
jax>=0.4.23
jaxlib>=0.4.23
qutip==4.7.0
cython==0.29.33
sphinx==6.1.3
Expand Down
4 changes: 3 additions & 1 deletion doc/rtd-environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ channels:
dependencies:
- numpy>=1.16.6
- scipy>=1.10.1
- jax>=0.4.23
- jaxlib>=0.4.23
- cython==0.29.33
- sphinx==6.1.3
- sphinx_rtd_theme==1.2.0
Expand All @@ -15,5 +17,5 @@ dependencies:
- wheel==0.38.4
- pip
- pip:
- qutip==5.0.0a1
- qutip==5.0.1
- ..[full]
6 changes: 4 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
cython>=1.0
numpy>=1.16.6
scipy>=1.10.1
qutip @ git+https://github.com/qutip/qutip.git@master
jax>=0.4.23
jaxlib>=0.4.23
qutip>=5.0.1
qutip-qtrl @ git+https://github.com/flowerthrower/qutip-qtrl@patrick/parameterize_crab
qutip-jax @ git+https://github.com/qutip/qutip-jax.git@master
qutip-jax
pre-commit
10 changes: 6 additions & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ classifiers =
Operating System :: POSIX
Operating System :: Unix
Operating System :: Microsoft :: Windows
author = Alexander Pitchford, Paul D. Nation, Robert J. Johansson, Chris Granade, Arne Grimsmo, Nathan Shammah, Shahnawaz Ahmed, Neill Lambert, Eric Giguere, Boxi Li, Jake Lishman
author = Patrick Hopf, Paul Menczel, Alexander Pitchford, Neil Lambert
author_email = [email protected]
platforms = Linux, Mac OSX, Unix, Windows

Expand All @@ -28,10 +28,12 @@ package_dir=
packages = find:
include_package_data = True
install_requires =
qutip @ git+https://github.com/qutip/qutip.git@master
qutip-qtrl @ git+https://github.com/qutip/qutip-qtrl.git@master
qutip-jax @ git+https://github.com/qutip/qutip-jax.git@master
jax
jaxlib
packaging
qutip
qutip-qtrl @ git+https://github.com/flowerthrower/qutip-qtrl@patrick/parameterize_crab
qutip-jax
setup_requires =
cython>=1.0
packaging
Expand Down
65 changes: 65 additions & 0 deletions src/qutip_qoc/_crab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""
This module provides an interface to the CRAB optimization algorithm in qutip-qtrl.
It defines the _CRAB class, which uses a qutip_qtrl.optimizer.Optimizer object
to store the control problem and calculate the fidelity error function and its gradient
with respect to the control parameters, according to the CRAB algorithm.
"""

import qutip_qtrl.logging_utils as logging
import copy

logger = logging.get_logger()


class _CRAB:
"""
Class to interface with the CRAB optimization algorithm in qutip-qtrl.
It has an attribute `qtrl` that is a `qutip_qtrl.optimizer.Optimizer` object
for storing the control problem and calculating the fidelity error function
and its gradient wrt the control parameters, according to the CRAB algorithm.
The class does only provide the infidelity method, as the CRAB algorithm is
not a gradient-based optimization.
"""

def __init__(self, qtrl_optimizer):
self._qtrl = copy.deepcopy(qtrl_optimizer)
self.gradient = None

def infidelity(self, *args):
"""
This method is adapted from the original
`qutip_qtrl.optimizer.Optimizer.fid_err_func_wrapper`
Get the fidelity error achieved using the ctrl amplitudes passed
in as the first argument.
This is called by generic optimisation algorithm as the
func to the minimised. The argument is the current
variable values, i.e. control amplitudes, passed as
a flat array. Hence these are reshaped as [nTimeslots, n_ctrls]
and then used to update the stored ctrl values (if they have changed)
"""
self._qtrl.num_fid_func_calls += 1
# *** update stats ***
if self._qtrl.stats is not None:
self._qtrl.stats.num_fidelity_func_calls = self._qtrl.num_fid_func_calls
if self._qtrl.log_level <= logging.DEBUG:
logger.debug(
"fidelity error call {}".format(
self._qtrl.stats.num_fidelity_func_calls
)
)

amps = self._qtrl._get_ctrl_amps(args[0].copy())
self._qtrl.dynamics.update_ctrl_amps(amps)

err = self._qtrl.dynamics.fid_computer.get_fid_err()

if self._qtrl.iter_summary:
self._qtrl.iter_summary.fid_func_call_num = self._qtrl.num_fid_func_calls
self._qtrl.iter_summary.fid_err = err

if self._qtrl.dump and self._qtrl.dump.dump_fid_err:
self._qtrl.dump.update_fid_err_log(err)

return err
Loading

0 comments on commit 849d4c7

Please sign in to comment.