Skip to content

Commit

Permalink
Migrate to pyQuil v3 (#107)
Browse files Browse the repository at this point in the history
* update pyquil to v3, add qcs-api-client

* replace ForestConnection with QCSClientConfiguration

* migrate qvm to pyquil v3

* reenable forest.qpu and migrate to pyquilv3

* migrate forest.(numpy_)wavefunction to pyquil v3

* fix how timeouts are handled in qvm

* first pass at documentation updates

* forest -> QCS

* get qvm tests passing

* get QPU tests passing

* relax pyquil requirement, doc suggestions

* make shot_range, bin_size optional to match QubitDevice, cleanup RESET
instruction

* let pyQuil set its own defaults add missing asserts to tests

* create separate doc for server config and include it where needed

* reformat with black

* remove RequestException as it is no longer used in pyQuil

* Update README.rst

Co-authored-by: Albert Mitjans <a.mitjanscoma@gmail.com>

* Update README.rst

Co-authored-by: Albert Mitjans <a.mitjanscoma@gmail.com>

* Update README.rst

Co-authored-by: Albert Mitjans <a.mitjanscoma@gmail.com>

* fix typo in qpu docs and correct notes on server configuration

* remove special mention of operations that are now available in pennylane

* remove reference to special operations, add lower bound for PennyLane

* add compiler and execution timeout to docstrings

* add docstring to extract_samples

* remove unused kwargs on ForestDevice and its children

* reformat with black

* remove comment on wrap_in_numshots_loop

* consolidate gate import, remove comment about unsupported gates

* use dict literal instead of dict()

* remove mention of special support for gates

* remove figure, add description to title-card directives

* import CPHASE

* remove extra params from super call

* increase shots on tests without parametric_compilation

* remove pytest.mark.xfail from converter tests

* fix jacobian call in differntiation test

* device refactor: move shared init logic into ForestDevice

* device refactor: move execute and apply into ForestDevice

* device refactor: move circuit_hash, compiled_program properties into
ForestDevice

* device refactor: consolidate reset method logic into ForestDevice

* device refactor: move generate and extract_samples into ForestDevice

* device refactor: QPUDevice inherits from ForestDevice

* device refactor: formatting and cleanup

* device refactor: lift shared logic for qvm qpu out of ForestDevice into
new QuantumComputerDevice

* device refactor: make noisy a qvm only param, cleanup docstrings

* device refactor: cleanup and reformat with black

* device refactor: cleanup a few pylint nits

* Fix tests

* set min pennylane version to 0.18

* replace plugin operations with pennylane gates

* fix pyquil.simulation import

* remove extra reset in QCDevice, add requested docstrings

* remove trailing newline, simplify simulation import

* simplify generate_samples and move `pyqvm` specific logic to QVMDevice

* update docs requirements

* bump quilc version in workflows

* fix typo in method name

* make sure program isn't compiled if it's been cached

* remove extraneous failing test that unnaturally overwrites private value

* pylint: initialize all instance params in __init__

* increase EXECUTION_TIMEOUT

Co-authored-by: Albert Mitjans <a.mitjanscoma@gmail.com>
  • Loading branch information
MarquessV and AlbertMitjans committed Oct 24, 2022
1 parent 1470933 commit dcc8eeb
Show file tree
Hide file tree
Showing 29 changed files with 740 additions and 1,053 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
python-version: ${{ matrix.python-version }}

- name: Run Forest Quilc
run: docker run --rm -d -p 5555:5555 rigetti/quilc:1.22.0 -R
run: docker run --rm -d -p 5555:5555 rigetti/quilc:1.23.0 -R

- name: Run Forest QVM
run: docker run --rm -d -p 5000:5000 rigetti/qvm -S
Expand Down Expand Up @@ -62,7 +62,7 @@ jobs:
access_token: ${{ github.token }}

- name: Run Forest Quilc
run: docker run --rm -d -p 5555:5555 rigetti/quilc:1.22.0 -R
run: docker run --rm -d -p 5555:5555 rigetti/quilc:1.23.0 -R

- name: Run Forest QVM
run: docker run --rm -d -p 5000:5000 rigetti/qvm -S
Expand Down
37 changes: 13 additions & 24 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ PennyLane Forest Plugin
.. header-start-inclusion-marker-do-not-remove
The PennyLane Forest plugin allows different Rigetti devices to work with
PennyLane --- the wavefunction simulator, and the Quantum Virtual Machine (QVM).
PennyLane --- the wavefunction simulator, the Quantum Virtual Machine (QVM), and Quantum Processing Units (QPUs).

`pyQuil <https://pyquil.readthedocs.io>`__ is a Python library for quantum programming using the
quantum instruction language (Quil) --- resulting quantum programs can be executed using the
`Rigetti Forest SDK <https://pyquil-docs.rigetti.com/en/stable/>`__ and the `Rigetti QCS
`Rigetti Forest SDK <https://pyquil-docs.rigetti.com/en/stable/>`__ and `Rigetti Quantum Cloud Services (QCS)
<https://qcs.rigetti.com/>`__.

`PennyLane <https://pennylane.readthedocs.io>`__ is a cross-platform Python library for quantum machine
Expand All @@ -46,23 +46,14 @@ The plugin documentation can be found here: `<https://docs.pennylane.ai/projects
Features
========

* Provides three devices to be used with PennyLane: ``forest.numpy_wavefunction``,
``forest.wavefunction``, and ``forest.qvm``. These provide access to the pyQVM
Numpy wavefunction simulator, Forest wavefunction simulator, and quantum
virtual machine (QVM) respectively.
* Provides four devices to be used with PennyLane: ``forest.numpy_wavefunction``,
``forest.wavefunction``, ``forest.qvm``, and ``forest.qpu``. These provide access to the pyQVM
Numpy wavefunction simulator, pyQuil wavefunction simulator, quantum
virtual machine (QVM), and quantum processing units (QPUs) respectively.


* All provided devices support all core qubit PennyLane operations and observables.


* Provides custom PennyLane operations to cover additional pyQuil operations:
``ISWAP``, ``PSWAP``, and ``CPHASE``. Every custom operation supports analytic
differentiation.

* Combine Forest and the Rigetti Cloud Services with PennyLane's automatic differentiation and
optimization.


.. installation-start-inclusion-marker-do-not-remove
Installation
Expand All @@ -86,14 +77,12 @@ Dependencies

PennyLane-Forest requires the following libraries be installed:

* `Python <http://python.org/>`__ >=3.6
* `Python <http://python.org/>`__ >=3.7

as well as the following Python packages:

* `PennyLane <http://pennylane.readthedocs.io/>`__
* `pyQuil <https://pyquil-docs.rigetti.com/en/stable/>`__ >=2.16, <2.28.3

Note that the latest PyQuil version 3.0 is not currently supported.
* `PennyLane <http://pennylane.readthedocs.io/>`__ >=0.15.0
* `pyQuil <https://pyquil-docs.rigetti.com/en/stable/>`__ >=3.3.1, <4.0.0

If you currently do not have Python 3 installed, we recommend
`Anaconda for Python 3 <https://www.anaconda.com/download/>`__, a distributed version
Expand All @@ -105,11 +94,11 @@ Forest software development kit (SDK):

* `Forest SDK <https://pyquil-docs.rigetti.com/en/stable/>`__

Alternatively, you may sign up for Rigetti's Quantum Cloud Services (QCS) to acquire a Quantum Machine
Image (QMI) which will allow you to compile your quantum code and run on real quantum processing units (QPUs),
or on a preinstalled QVM. Note that this requires a valid QCS account.
Alternatively, you may sign up for Rigetti's Quantum Cloud Services (QCS) which will allow you to compile your
quantum code and run on real QPUs. Note that this requires a valid QCS account and the QCS CLI:

* `Quantum Cloud Services <https://docs.rigetti.com/en/>`__
* `QCS <https://docs.rigetti.com/en/>`__
* `QCS CLI <https://docs.rigetti.com/qcs/guides/using-the-qcs-cli>`__

Tests
~~~~~
Expand Down
13 changes: 1 addition & 12 deletions doc/devices/numpy_wavefunction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,4 @@ array([0.97517033, 0.04904283])
Supported operations
~~~~~~~~~~~~~~~~~~~~

All Forest devices support all PennyLane `operations and observables <https://pennylane.readthedocs.io/en/stable/introduction/operations.html#qubit-operations>`_, with
the exception of the PennyLane ``QubitStateVector`` state preparation operation.

In addition, PennyLane-Forest provides the following PyQuil-specific operations for PennyLane.
These are all importable from :mod:`pennylane_forest.ops <.ops>`.

These operations include:

.. autosummary::
pennylane_forest.ops.CPHASE
pennylane_forest.ops.ISWAP
pennylane_forest.ops.PSWAP
All Forest devices support all PennyLane `operations and observables <https://pennylane.readthedocs.io/en/stable/introduction/operations.html#qubit-operations>`_, with the exception of the PennyLane ``QubitStateVector`` state preparation operation.
60 changes: 13 additions & 47 deletions doc/devices/qpu.rst
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
:orphan:

The QPU device
==============

The intention of the ``forest.qpu`` device is to construct a device that will allow for execution on an actual QPU.
Constructing and using this device is very similar to very similar in design and implementation as the
``forest.qvm`` device, with slight differences at initialization, such as not supporting the keyword argument ``noisy``.
Constructing and using this device is very similar in design and implementation as the ``forest.qvm`` device, with
slight differences at initialization, such as not supporting the keyword argument ``noisy``.

In addition, ``forest.qpu`` also accepts the optional ``active_reset`` keyword argument:

Expand All @@ -21,22 +19,21 @@ Usage
A QPU device can be created via:

>>> import pennylane as qml
>>> dev_qpu = qml.device('forest.qpu', device='Aspen-8', shots=1000)
>>> dev_qpu = qml.device('forest.qpu', device='Aspen-M-2', shots=1000)

Note that additional Quil gates not provided directly in PennyLane are importable from :mod:`~.ops`.
An example that demonstrates the use of the native :class:`~.PSWAP` plugin gate is this:
The QPU can then be used like this:

.. code-block:: python
import pennylane as qml
from pennylane import numpy as np
from pennylane_forest.ops import PSWAP
@qml.qnode(dev_qpu)
def func(x, y):
qml.BasisState(np.array([1, 1]), wires=0)
qml.RY(x, wires=0)
qml.RX(y, wires=1)
PSWAP(0.432, wires=[0, 1])
qml.PSWAP(0.432, wires=[0, 1])
qml.CNOT(wires=[0, 1])
return expval(qml.PauliZ(1))
Expand All @@ -51,46 +48,15 @@ We can then integrate the quantum hardware and PennyLane's automatic differentia
Supported operations
~~~~~~~~~~~~~~~~~~~~

All devices support all PennyLane `operations and observables <https://pennylane.readthedocs.io/en/stable/introduction/operations.html#qubit-operations>`_, with
he exception of the PennyLane ``QubitStateVector`` state preparation operation.

In addition, PennyLane-Forest provides the following PyQuil-specific operations for PennyLane.
These are all importable from :mod:`pennylane_forest.ops <.ops>`.

These operations include:

.. autosummary::
pennylane_forest.ops.CPHASE
pennylane_forest.ops.ISWAP
pennylane_forest.ops.PSWAP

Device options
~~~~~~~~~~~~~~
All devices support all PennyLane `operations and observables <https://pennylane.readthedocs.io/en/stable/introduction/operations.html#qubit-operations>`_, with the exception of the PennyLane ``QubitStateVector`` state preparation operation.

On initialization, the PennyLane-Forest devices accept additional keyword
arguments beyond the PennyLane default device arguments.

``forest_url`` (*str*)
the Forest URL server. Can also be set by
the environment variable ``FOREST_SERVER_URL``, or in the ``~/.qcs_config``
configuration file. Default value is ``"https://forest-server.qcs.rigetti.com"``.

``qvm_url`` (*str*)
the QVM server URL. Can also be set by the environment
variable ``QVM_URL``, or in the ``~/.forest_config`` configuration file.
Default value is ``"http://127.0.0.1:5000"``.

``compiler_url`` (*str*)
the compiler server URL. Can also be set by the environment
variable ``COMPILER_URL``, or in the ``~/.forest_config`` configuration file.
Default value is ``"http://127.0.0.1:6000"``.
quilc server configuration
~~~~~~~~~~~~~~~~~~~~~~~~~~

.. note::

If using the downloadable Forest SDK with the default server configurations
for the QVM and the Quil compiler (i.e., you launch them with the commands
``qvm -S`` and ``quilc -R``), then you will not need to set these keyword arguments.

Likewise, if you are running PennyLane using the Rigetti Quantum Cloud Service (QCS)
on a provided QMI, these environment variables are set automatically and will also
not need to be passed in PennyLane.
for the Quil compiler (i.e., ``quilc -R``), then no special configuration is needed.
If using a non-default port or host for the server, see the
`pyQuil configuration documentation <https://pyquil-docs.rigetti.com/en/stable/advanced_usage.html#pyquil-configuration>`_
for details on how to override the default values.
47 changes: 2 additions & 45 deletions doc/devices/qvm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,7 @@ Printing out the results of the three device expectation values:
Supported operations
~~~~~~~~~~~~~~~~~~~~

All devices support all PennyLane `operations and observables <https://pennylane.readthedocs.io/en/stable/introduction/operations.html#qubit-operations>`_, with
he exception of the PennyLane ``QubitStateVector`` state preparation operation.

In addition, PennyLane-Forest provides the following PyQuil-specific operations for PennyLane.
These are all importable from :mod:`pennylane_forest.ops <.ops>`.

These operations include:

.. autosummary::
pennylane_forest.ops.CPHASE
pennylane_forest.ops.ISWAP
pennylane_forest.ops.PSWAP
All devices support all PennyLane `operations and observables <https://pennylane.readthedocs.io/en/stable/introduction/operations.html#qubit-operations>`_, with the exception of the PennyLane ``QubitStateVector`` state preparation operation.

Supported observables
~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -190,36 +179,4 @@ After measuring the qubit state, we can determine the probability :math:`P_0` of

This process is done automatically behind the scenes in the QVM device when ``qml.expval(qml.Hermitian)`` is returned.




Device options
~~~~~~~~~~~~~~

On initialization, the PennyLane-Forest devices accept additional keyword
arguments beyond the PennyLane default device arguments.

``forest_url`` (*str*)
the Forest URL server. Can also be set by
the environment variable ``FOREST_SERVER_URL``, or in the ``~/.qcs_config``
configuration file. Default value is ``"https://forest-server.qcs.rigetti.com"``.

``qvm_url`` (*str*)
the QVM server URL. Can also be set by the environment
variable ``QVM_URL``, or in the ``~/.forest_config`` configuration file.
Default value is ``"http://127.0.0.1:5000"``.

``compiler_url`` (*str*)
the compiler server URL. Can also be set by the environment
variable ``COMPILER_URL``, or in the ``~/.forest_config`` configuration file.
Default value is ``"http://127.0.0.1:6000"``.

.. note::

If using the downloadable Forest SDK with the default server configurations
for the QVM and the Quil compiler (i.e., you launch them with the commands
``qvm -S`` and ``quilc -R``), then you will not need to set these keyword arguments.

Likewise, if you are running PennyLane using the Rigetti Quantum Cloud Service (QCS)
on a provided QMI, these environment variables are set automatically and will also
not need to be passed in PennyLane.
.. include:: ./qvm_and_quilc_server_configuration.rst
11 changes: 11 additions & 0 deletions doc/devices/qvm_and_quilc_server_configuration.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
QVM and quilc server configuration
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. note::

If using the downloadable Forest SDK with the default server configurations
for the QVM and the Quil compiler (i.e., you launch them with the commands
``qvm -S`` and ``quilc -R``), then no special configuration is needed.
If using a non-default port or host for either of the servers, see the
`pyQuil configuration documentation <https://pyquil-docs.rigetti.com/en/stable/advanced_usage.html#pyquil-configuration>`_
for details on how to override the default values.
44 changes: 2 additions & 42 deletions doc/devices/wavefunction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,49 +50,9 @@ You can then execute the circuit like any other function to get the quantum mech
>>> circuit(0.2, 0.1, 0.3)
array([0.97517033, 0.04904283])

Device options
~~~~~~~~~~~~~~

On initialization, the PennyLane-Forest devices accept additional keyword
arguments beyond the PennyLane default device arguments.

``forest_url`` (*str*)
the Forest URL server. Can also be set by
the environment variable ``FOREST_SERVER_URL``, or in the ``~/.qcs_config``
configuration file. Default value is ``"https://forest-server.qcs.rigetti.com"``.

``qvm_url`` (*str*)
the QVM server URL. Can also be set by the environment
variable ``QVM_URL``, or in the ``~/.forest_config`` configuration file.
Default value is ``"http://127.0.0.1:5000"``.

``compiler_url`` (*str*)
the compiler server URL. Can also be set by the environment
variable ``COMPILER_URL``, or in the ``~/.forest_config`` configuration file.
Default value is ``"http://127.0.0.1:6000"``.

.. note::

If using the downloadable Forest SDK with the default server configurations
for the QVM and the Quil compiler (i.e., you launch them with the commands
``qvm -S`` and ``quilc -R``), then you will not need to set these keyword arguments.

Likewise, if you are running PennyLane using the Rigetti Quantum Cloud Service (QCS)
on a provided QMI, these environment variables are set automatically and will also
not need to be passed in PennyLane.

Supported operations
~~~~~~~~~~~~~~~~~~~~

All devices support all PennyLane `operations and observables <https://pennylane.readthedocs.io/en/stable/introduction/operations.html#qubit-operations>`_, with
he exception of the PennyLane ``QubitStateVector`` state preparation operation.

In addition, PennyLane-Forest provides the following PyQuil-specific operations for PennyLane.
These are all importable from :mod:`pennylane_forest.ops <.ops>`.

These operations include:
All devices support all PennyLane `operations and observables <https://pennylane.readthedocs.io/en/stable/introduction/operations.html#qubit-operations>`_, with the exception of the PennyLane ``QubitStateVector`` state preparation operation.

.. autosummary::
pennylane_forest.ops.CPHASE
pennylane_forest.ops.ISWAP
pennylane_forest.ops.PSWAP
.. include:: ./qvm_and_quilc_server_configuration.rst
19 changes: 12 additions & 7 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@ Currently, PennyLane-Forest provides these Forest devices for PennyLane:

.. title-card::
:name: 'forest.numpy_wavefunction'
:description: Forest's Numpy wavefunction simulator backend.
:description: pyQuil's Numpy wavefunction simulator backend.
:link: devices/numpy_wavefunction.html

.. title-card::
:name: 'forest.wavefunction'
:description: The Forest SDK wavefunction simulator backend.
:description: The QCS wavefunction simulator backend.
:link: devices/wavefunction.html

.. title-card::
:name: 'forest.qvm'
:description: Forest's QVM and pyQuil pyQVM simulator.
:description: QCS QVM and pyQuil pyQVM simulator.
:link: devices/qvm.html

.. title-card::
:name: 'forest.qpu'
:description: QCS QPU.
:link: devices/qpu.html

.. raw:: html

<div style='clear:both'></div>
Expand All @@ -45,20 +50,19 @@ Check out these demos to see the PennyLane-Forest plugin in action:
<div class="row">

.. title-card::
:name: Ensemble classification with Forest and Qiskit devices
:figure: <img src="https://pennylane.ai/qml/_images/ensemble_diagram.png" width="100%" />
:name: Ensemble classification with QCS and Qiskit devices
:description: Use two QPUs in parallel to help solve a machine learning classification problem.
:link: https://pennylane.ai/qml/demos/ensemble_multi_qpu.html

.. title-card::
:name: PyTorch and noisy devices
:figure: <img src="https://pennylane.ai/qml/_images/bloch.gif" width="100%" />
:description: Use PyTorch and a noisy QVM to see how optimization responds to noisy qubits.
:link: https://pennylane.ai/qml/demos/pytorch_noise.html

.. raw:: html

</div></div><div style='clear:both'> <br/>


You can also try it out using any of the qubit based `demos from the PennyLane documentation
<https://pennylane.ai/qml/demonstrations.html>`_, for example the tutorial on
`qubit rotation <https://pennylane.ai/qml/demos/tutorial_qubit_rotation.html>`_.
Expand Down Expand Up @@ -89,6 +93,7 @@ hardware access.
devices/numpy_wavefunction
devices/wavefunction
devices/qvm
devices/qpu

.. toctree::
:maxdepth: 1
Expand Down
4 changes: 2 additions & 2 deletions doc/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
docutils==0.16
pennylane
pyquil>=2.16,<2.28.3
pennylane>=0.18
pyquil>=3.0.0,<4.0.0
sphinx-automodapi
pygments-github-lexers
# do not pin
Expand Down

0 comments on commit dcc8eeb

Please sign in to comment.