Skip to content

Commit

Permalink
Update documentation for the new Algo debugging feature (#5894)
Browse files Browse the repository at this point in the history
**Context:**
This is a follow up to this
[PR](#5789) which added
detailed documentation for the new PennyLane Debugger feature.

---------

Co-authored-by: Astral Cai <astral.cai@xanadu.ai>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Yushao Chen (Jerry) <chenys13@outlook.com>
Co-authored-by: Christina Lee <chrissie.c.l@gmail.com>
Co-authored-by: Mudit Pandey <mudit.pandey@xanadu.ai>
Co-authored-by: Thomas R. Bromley <49409390+trbromley@users.noreply.github.com>
Co-authored-by: soranjh <40344468+soranjh@users.noreply.github.com>
  • Loading branch information
8 people committed Jul 2, 2024
1 parent 31c6d60 commit bbe33ff
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 7 deletions.
24 changes: 19 additions & 5 deletions doc/code/qml_debugging.rst
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
.. role:: html(raw)
:format: html

qml.debugging
=============

.. automodapi:: pennylane.debugging
:no-heading:
:no-inherited-members:
:skip: PLDB
:skip: pldb_device_manager
This module contains functionality for debugging quantum programs on simulator devices.

:html:`<div class="summary-table">`

.. autosummary::
:nosignatures:

~pennylane.breakpoint
~pennylane.debug_expval
~pennylane.debug_probs
~pennylane.debug_state
~pennylane.debug_tape
~pennylane.snapshots

:html:`</div>`


Entering the Debugging Context
------------------------------
Expand Down
91 changes: 91 additions & 0 deletions doc/introduction/inspecting_circuits.rst
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,97 @@ results.
All snapshots are numbered with consecutive integers, and if no tag was provided,
the number of a snapshot is used as a key in the output dictionary instead.

Interactive Debugging on Simulators
-----------------------------------

PennyLane allows for more interactive debugging of quantum circuits in a programmatic
fashion using quantum breakpoints via :func:`~pennylane.breakpoint`. This feature is
currently supported on ``default.qubit`` and ``lightning.qubit`` devices.

Consider the following python script containing the quantum circuit with breakpoints.

.. code-block:: python3
dev = qml.device("default.qubit", wires=2)
@qml.qnode(dev)
def circuit(x):
qml.breakpoint()
qml.RX(x, wires=0)
qml.Hadamard(wires=1)
qml.breakpoint()
qml.CNOT(wires=[0, 1])
return qml.expval(qml.Z(0))
circuit(1.23)
Running the circuit above launches an interactive :code:`[pldb]:` prompt. Here we can
step through the circuit execution:

.. code-block:: console
> /Users/your/path/to/script.py(8)circuit()
-> qml.RX(x, wires=0)
[pldb]: list
3
4 @qml.qnode(dev)
5 def circuit(x):
6 qml.breakpoint()
7
8 -> qml.RX(x, wires=0)
9 qml.Hadamard(wires=1)
10
11 qml.breakpoint()
12
13 qml.CNOT(wires=[0, 1])
[pldb]: next
> /Users/your/path/to/script.py(9)circuit()
-> qml.Hadamard(wires=1)
We can extract information by making measurements which do not change the state of
the circuit in execution:

.. code-block:: console
[pldb]: qml.debug_state()
array([0.81677345+0.j , 0. +0.j ,
0. -0.57695852j, 0. +0.j ])
[pldb]: continue
> /Users/your/path/to/script.py(13)circuit()
-> qml.CNOT(wires=[0, 1])
[pldb]: next
> /Users/your/path/to/script.py(14)circuit()
-> return qml.expval(qml.Z(0))
[pldb]: list
8 qml.RX(x, wires=0)
9 qml.Hadamard(wires=1)
10
11 qml.breakpoint()
12
13 qml.CNOT(wires=[0, 1])
14 -> return qml.expval(qml.Z(0))
15
16 circuit(1.23)
[EOF]
We can also visualize the circuit and dynamically queue operations directly to the circuit:

.. code-block:: console
[pldb]: print(qml.debug_tape().draw())
0: ──RX─╭●─┤
1: ──H──╰X─┤
[pldb]: qml.RZ(-4.56, 1)
RZ(-4.56, wires=[1])
[pldb]: print(qml.debug_tape().draw())
0: ──RX─╭●─────┤
1: ──H──╰X──RZ─┤
See :doc:`/code/qml_debugging` for more information and detailed examples.

Graph representation
--------------------

Expand Down
3 changes: 1 addition & 2 deletions doc/releases/changelog-0.37.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
* Added a quantum debugger (`PLDB`) which interfaces via `qml.breakpoint()` and provides tools for
debugging quantum circuits. Users can step through the quantum circuit operations, dynamically
queue operations and make measurements using (`qml.debug_state()`, `qml.debug_probs()`,
`qml.debug_expval()`, and `qml.debug_tape()`). Consider the following python script
containing the quantum circuit with breakpoints.
`qml.debug_expval()`, and `qml.debug_tape()`).
[(#5680)](https://github.com/PennyLaneAI/pennylane/pull/5680)
[(#5749)](https://github.com/PennyLaneAI/pennylane/pull/5749)
[(#5789)](https://github.com/PennyLaneAI/pennylane/pull/5789)
Expand Down
11 changes: 11 additions & 0 deletions pennylane/debugging/debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ def breakpoint():
throught the circuit execution using Pdb like commands (:code:`list`, :code:`next`,
:code:`continue`, :code:`quit`).
.. seealso:: :doc:`/code/qml_debugging`
**Example**
Consider the following python script containing the quantum circuit with breakpoints.
Expand Down Expand Up @@ -227,6 +229,9 @@ def circuit(x):
def debug_state():
"""Compute the quantum state at the current point in the quantum circuit.
Debugging measurements do not alter the state, it remains the same until the
next operation in the circuit.
Returns:
Array(complex): quantum state of the circuit
Expand Down Expand Up @@ -280,6 +285,9 @@ def debug_expval(op):
"""Compute the expectation value of an observable at the
current point in the quantum circuit.
Debugging measurements do not alter the state, it remains the same until the
next operation in the circuit.
Args:
op (Operator): the observable to compute the expectation value for.
Expand Down Expand Up @@ -338,6 +346,9 @@ def debug_probs(wires=None, op=None):
"""Compute the probability distribution for the state at the current
point in the quantum circuit.
Debugging measurements do not alter the state, it remains the same until the
next operation in the circuit.
Args:
wires (Union[Iterable, int, str, list]): the wires the operation acts on
op (Union[Observable, MeasurementValue]): observable (with a ``diagonalizing_gates``
Expand Down

0 comments on commit bbe33ff

Please sign in to comment.