In this notebook I will show how to create and optimize a quantum circuit to prepare a Bell state, the simplest two-qubit example of quantum entanglement. Step by step, we will see how to:
- create a quantum circuit with trainable parameters;
- define a quantum device;
- define the observables to use for measurement;
- train the circuit parameters towards a target state;
- visualize the parameters' evolution through the training steps.
I will use PennyLane, a Python library for quantum machine learning, throughout the notebook. I will also highlight my contributions to the library.
-
Create a virtual environment:
$ python3 -m venv venv
-
Activate the environment:
$ source venv/bin/activate
-
Update
pip
:$ pip install --upgrade pip
-
Install the modules:
$ pip install -r requirements.txt
-
Clone PennyLane (since at this moment my contribution are only present on the master version):
$ git clone https://github.com/XanaduAI/pennylane
-
(Only necessary if running the pyQuil/Forest SDK examples) Checkout the
1a882984c8f5bc3ce13b74f8f395ce9262c0a975
commit (because of a bug I found with wires that would prevent thepennylane-forest
plugin from running):$ git checkout 1a882984c8f5bc3ce13b74f8f395ce9262c0a975
-
-
Build PennyLane locally:
$ cd pennylane $ pip install -e .
-
Launch Jupyter Notebook:
$ jupyter notebook
-
Open and run the
state_preparation_circuit_optimization.ipynb
notebook
This is only necessary when running the notebook with Rigetti QVM (the forest.qvm
device) and QPU (the forest.qpu
device). The two devices are included but commented out in the notebook.
-
Clone the PennyLane Forest plugin:
$ git clone https://github.com/rigetti/pennylane-forest
-
Build the plugin locally (since the new Aspen-8 device is only present on the master version):
$ cd pennylane-forest $ pip install -e .
The plugin requires both the quantum virtual machine (qvm
) and the Quil compiler (quilc
) to be running. The easiest way to run both is by using Docker images; on the other hand, if the configuration of the QVM needs to be customized (for instance to assign more memory to the process), it is necessary to build it from the source.
-
Install Docker
-
Make sure no service is running on ports 5000 and 5555
-
To run both
qvm
andquilc
interactively:-
Run the following on the console (may need
sudo
):$ docker run --rm -it -p 5000:5000 rigetti/qvm -S
-
Open a new console and run (may need
sudo
):$ docker run --rm -it -p 5555:5555 rigetti/quilc -R
-
-
Otherwise, to run them in the background, add
-d
to both the previous commands
Note: The qvm
process can crash due to memory issues; if that happens, try to run it with --default-allocation foreign
. Performance can also improve by adding the -c
(JIT compilation of Quil programs) parameter.
-
Clone the QVM repository:
$ git clone https://github.com/rigetti/qvm
-
Install the needed libraries, for instance on Ubuntu:
$ sudo apt-get update $ sudo apt-get install make pkg-config libffi-dev libblas-dev liblapack-dev sbcl
-
Build with
make
, optionally with a parameter for the memory:$ cd qvm/ $ make QVM_WORKSPACE=24576 qvm
-
Run
qvm
(the-c
parameter, which enables Quil programs JIT compilation, is optional but seems to improve the response speed):$ ./qvm -S -c`
Thanks to Tom Lubowe, the qpu_results
folder contains two copies of the notebook run on a real QPU, Rigetti's Aspen-8, using 100 shots and 1000 shots per optimization step. The 1000-shot notebook has incomplete results due to a timeout after about 250 steps; we plan to run it again soon.
Big thanks go to:
- Tom Bromley (Xanadu), for all the interesting discussions about PennyLane extensions, for guiding me through conventions and existing work, and for reviewing my code and notebook;
- Josh Izaac (Xanadu), for putting me in touch with the PennyLane team, giving many useful suggestions, and for reviewing my code;
- Tom Lubowe (Rigetti), for being my mentor, supporting the project, raising issues on the QVM interaction, and managing to run the notebook on Rigetti's QPU;
- Michał Stęchły (QOSF), for organizing the mentorship program and creating the inspiring QOSF community.