# Quantum Computing and Quantum Machine Learning

*Morten Hjorth-Jensen*

*Lecture notes for FYS5419/9419, University of Oslo*

This is the executable edition of the lecture notes. Every chapter is a
Jupyter notebook that carries the full text of the printed chapter, its
equations with the same numbers, its figures and tables, and the programs
that produced them. The code cells are not illustrations added afterwards:
every number quoted in the text is printed by one of them.

## How the notes are organised

Part I (chapters 2 to 7) is about quantum computing for many-body
problems: the same four model Hamiltonians (Lipkin, pairing, Hubbard and
Heisenberg) are solved classically, mapped to qubits, evolved in time, and
then treated with phase estimation, with variational algorithms and with
spectral filters. Part II (chapters 8 to 13) is about quantum machine
learning: each chapter takes a classical model, derives it in enough detail
to implement it, replaces one of its components by a quantum circuit, and
runs both versions on the same data. Chapter 14 summarizes both parts.

| Chapter | Topic | Notebook |
|---|---|---|
| 1 | Introduction and motivation: the many-body problem and the learning problem, the history from Feynman's idea to quantum machine learning, the resource questions, the roadmap and the conventions | [chapter 1](chapter1/chapter1) |
| 2 | Fundamentals of quantum computation: Dirac notation, qubits and the Bloch sphere, measurements, density matrices, entanglement, gates, circuits and simple protocols | [chapter 2](chapter2/chapter2) |
| 3 | Quantum many-body physics primer: second quantization, the four model Hamiltonians, full configuration interaction, Hartree-Fock, coupled cluster and unitary coupled cluster; the Jordan-Wigner transformation, qubit encodings, Pauli rotations, Trotter steps, measuring a Pauli sum and a first variational circuit | [chapter 3](chapter3/chapter3) |
| 4 | Hamiltonian simulation and time evolution: product formulas of first, second and higher order and their commutator bounds, the effective Hamiltonian of a Trotter step, quenches of the Ising and Heisenberg chains, Trotter steps as circuits and their gate counts, energies from a time series, adiabatic state preparation, imaginary-time evolution, methods beyond product formulas | [chapter 4](chapter4/chapter4) |
| 5 | Quantum Fourier transform and phase estimation: QFT, QPE and its iterative form, energies of the model Hamiltonians, Trotter and trial-state errors, the HHL linear-system algorithm | [chapter 5](chapter5/chapter5) |
| 6 | Variational quantum algorithms: VQE, ansätze, measuring the energy, parameter-shift gradients, barren plateaus, the model Hamiltonians, ADAPT-VQE, a first treatment of QAOA | [chapter 6](chapter6/chapter6) |
| 7 | Advanced eigenstate algorithms: resolution refinement along a path of model spaces, the rodeo algorithm, the pairing model from coarse to fine, comparison with phase estimation and VQE, other models, Trotterized filters and gate budgets | [chapter 7](chapter7/chapter7) |
| 8 | Introduction to quantum machine learning: what QML is, why quantum, the challenges, variational circuits reconsidered, the relation to Part I, data encodings | [chapter 8](chapter8/chapter8) |
| 9 | Quantum support vector machines: SVM theory, kernels, quantum feature maps and kernels, kernel estimation, QSVM, kernel-target alignment | [chapter 9](chapter9/chapter9) |
| 10 | The quantum approximate optimization algorithm: QUBO and Ising, the circuit, gradients, a double-quantum-dot register, portfolio selection, clustering | [chapter 10](chapter10/chapter10) |
| 11 | Quantum neural networks: the model and its Fourier series, shift rules, quantum Fisher information and the natural gradient, barren plateaus, regression and classification, physics-informed QNNs for differential equations | [chapter 11](chapter11/chapter11) |
| 12 | Quantum Boltzmann machines: energy-based models and the RBM, contrastive divergence, the QBM and its gradient, classical versus quantum data, Hamiltonian learning, the free-energy principle and the variational quantum thermalizer | [chapter 12](chapter12/chapter12) |
| 13 | Reinforcement learning and quantum reinforcement learning (placeholder: scope and intended structure) | [chapter 13](chapter13/chapter13) |
| 14 | Summary and perspectives: what the two parts have shown, the reassessment of variational circuits, a priority list, hardware, alternatives to the gate model, open problems | [chapter 14](chapter14/chapter14) |
| A | Quantum computing tools and tutorials: the examples of the notes in Qiskit and in the QDK (Q#), the quantum neural network of chapter 11 in Qiskit, Q# and PennyLane, running on quantum computers, other software | [appendix A](appendixA/appendix1) |
| B | Mathematical background: norms and the condition number, linear systems (LU, conjugate gradients), the Hermitian eigenvalue problem, Householder and Lanczos, the singular value decomposition and the generalized eigenvalue problem of noisy subspace methods | [appendix B](appendixB/appendix2) |

Citations appear in the text as bracketed keys such as [Havlicek2019]; the
[reference list](references) is labelled by the same keys.

## Running the code

The notebooks import the programs of the notes rather than repeating them,
so they are meant to be run from a copy of the repository:

```bash
git clone https://github.com/CompPhysics/QuantumComputingMachineLearning.git
cd QuantumComputingMachineLearning/doc/LectureMaterial/Notebooks
pip install numpy matplotlib jupyterlab
jupyter lab chapter2/chapter2.ipynb
```

The programs are in the folder
[`LectureMaterial/BookPrograms/`](https://github.com/CompPhysics/QuantumComputingMachineLearning/tree/gh-pages/doc/LectureMaterial/BookPrograms)
of the repository, one folder per chapter and a `common/` folder for the
shared modules. The first code cell of every notebook puts
`LectureMaterial/BookPrograms/chapterN/` and
`LectureMaterial/BookPrograms/common/` on the Python path. Each chapter ends with one cell
per program of its *Programs for this chapter* list, which runs the program
and shows the figures it wrote to `LectureMaterial/BookFigures/chapterN/`. The programs
need only `numpy` and `matplotlib`: the qubit simulator, the mappings, the
optimizers, the feature maps and the solvers are all written out in the
notes' own classes, so that the mechanics stay visible. The optional
PennyLane, Qiskit and scikit-learn cells of Part II run when those packages
are installed and print a message when they are not. A chapter executes in
one to four minutes.

Appendix A is the exception: it repeats examples of the notes with IBM's
Qiskit, Microsoft's Quantum Development Kit and PennyLane and therefore
needs those packages
(`pip install -r ../BookPrograms/appendixA/requirements.txt`); it
executes in about ten minutes. Appendix B needs NumPy and matplotlib only and
executes in a quarter of a minute.

The pages shown here were executed when the notebooks were generated from
the LaTeX sources, and the outputs you see are the stored ones.
