Skip to content

Commit

Permalink
Daily rc sync to master (PennyLaneAI#5962)
Browse files Browse the repository at this point in the history
Automatic sync from the release candidate to master during a feature
freeze.

---------

Co-authored-by: Jay Soni <jbsoni@uwaterloo.ca>
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>
Co-authored-by: Pietropaolo Frisoni <pietropaolo.frisoni@xanadu.ai>
Co-authored-by: Isaac De Vlugt <34751083+isaacdevlugt@users.noreply.github.com>
Co-authored-by: Vincent Michaud-Rioux <vincent.michaud-rioux@xanadu.ai>
Co-authored-by: lillian542 <38584660+lillian542@users.noreply.github.com>
Co-authored-by: Christina Lee <christina@xanadu.ai>
Co-authored-by: Diego <67476785+DSGuala@users.noreply.github.com>
Co-authored-by: trbromley <brotho02@gmail.com>
Co-authored-by: Korbinian Kottmann <43949391+Qottmann@users.noreply.github.com>
Co-authored-by: Diego <diego_guala@hotmail.com>
Co-authored-by: Cristian Emiliano Godinez Ramirez <57567043+EmilianoG-byte@users.noreply.github.com>
Co-authored-by: Ahmed Darwish <exclass9.24@gmail.com>
Co-authored-by: GitHub Actions Bot <>
  • Loading branch information
19 people authored and RenkeHuang committed Jul 11, 2024
1 parent 7f9b936 commit 27c117f
Show file tree
Hide file tree
Showing 49 changed files with 1,050 additions and 603 deletions.
80 changes: 40 additions & 40 deletions doc/code/qml_debugging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ the circuit operations are applied. The functionality is highlighted by the exam
circuit below.

.. code-block:: python
:linenos:
import pennylane as qml
Expand All @@ -50,7 +51,7 @@ circuit below.
circuit(1.2345)
Running the above python script opens up the interactive ``[pldb]:`` prompt in the terminal.
Running the above python script opens up the interactive ``[pldb]`` prompt in the terminal.
When this code reaches ``qml.breakpoint()`` it will pause and launch an interactive
debugging prompt. The prompt specifies the path to the script and the next line to be
executed after the breakpoint:
Expand All @@ -59,29 +60,29 @@ executed after the breakpoint:
> /Users/your/path/to/script.py(7)circuit()
-> qml.Hadamard(wires=0)
[pldb]:
[pldb]
Controlling Code Execution in the Debugging Context
---------------------------------------------------

The Pennylane Debugger (PLDB) is built from the native python debugger (Pdb), as such
The Pennylane Debugger (PLDB) is built on top of the native python debugger (PDB). As such,
it shares a similar interface. We can interact with the debugger using the
builtin commands: ``list``, ``longlist``, ``next``, ``continue``, and ``quit``. Any
variables defined in the scope of the quantum function can also be accessed from the
built-in commands such as ``list``, ``longlist``, ``next``, ``continue``, and ``quit``. Any
variables defined within the scope of the quantum function can also be accessed from the
debugger.

.. code-block:: console
[pldb]: print(x)
[pldb] print(x)
1.2345
The ``list`` (and ``longlist``) command will print a section of code around the
breakpoint, highlighting the next line to be executed. This can be used to determine
the location of execution in the circuit.
the location of the execution in the circuit.

.. code-block:: console
[pldb]: longlist
[pldb] longlist
3 @qml.qnode(qml.device('default.qubit', wires=(0,1,2)))
4 def circuit(x):
5 qml.breakpoint()
Expand All @@ -97,14 +98,14 @@ the location of execution in the circuit.
15 return qml.sample()
The ``next`` command will execute the next line of code, and print the following
line to be executed, e.g., the next operation to execute is ``CNOT``.
line to be executed. In this example, the next operation to execute is the ``CNOT``.

.. code-block:: console
[pldb]: next
[pldb] next
> /Users/your/path/to/script.py(8)circuit()
-> qml.CNOT(wires=(0,2))
[pldb]: list
[pldb] list
3 @qml.qnode(qml.device('default.qubit', wires=(0,1,2)))
4 def circuit(x):
5 qml.breakpoint()
Expand All @@ -117,16 +118,16 @@ line to be executed, e.g., the next operation to execute is ``CNOT``.
12
13 qml.breakpoint()
Alternative, the ``continue`` command allows for jumping between breakpoints. This command resumes
code execution until the next breakpoint is reached. Finally, the ``quit`` command
ends the debugging prompt.
Alternatively, the ``continue`` command allows for jumping between breakpoints. This command resumes
code execution until the next breakpoint is reached, or termination if there is none. Finally,
the ``quit`` command ends the debugging prompt and terminates the execution altogether.

.. code-block:: console
[pldb]: continue
[pldb] continue
> /Users/your/path/to/script.py(14)circuit()
-> qml.RX(-x, wires=1)
[pldb]: list
[pldb] list
9
10 for w in (0, 1, 2):
11 qml.RX(2*x, wires=w)
Expand All @@ -137,25 +138,24 @@ ends the debugging prompt.
16
17 circuit(1.2345)
[EOF]
[pldb]: quit
[pldb] quit
Extracting Circuit Information
------------------------------

While in the debugging prompt, we can extract information and perform measurements
on the qunatum circuit. Specifically we can make measurements using
:func:`~pennylane.debug_expval`, :func:`~pennylane.debug_state`,
:func:`~pennylane.debug_probs`, and access the gates in the circuit using
:func:`~pennylane.debug_tape`.
While in the debugging prompt, we can extract information about the current contents
of the quantum tape using :func:`~pennylane.debug_tape`. We can also perform measurements dynamically
on the quantum circuit using :func:`~pennylane.debug_expval`, :func:`~pennylane.debug_state`,
and :func:`~pennylane.debug_probs`.

Consider the circuit from above,

.. code-block:: console
> /Users/your/path/to/script.py(7)circuit()
-> qml.Hadamard(wires=0)
[pldb]: longlist
[pldb] longlist
3 @qml.qnode(qml.device('default.qubit', wires=(0,1,2)))
4 def circuit(x):
5 qml.breakpoint()
Expand All @@ -169,24 +169,24 @@ Consider the circuit from above,
13 qml.breakpoint()
14 qml.RX(-x, wires=1)
15 return qml.sample()
[pldb]: next
[pldb] next
> /Users/your/path/to/script.py(8)circuit()
-> qml.CNOT(wires=(0,2))
[pldb]: next
[pldb] next
> /Users/your/path/to/script.py(10)circuit()
-> for w in (0, 1, 2):
[pldb]:
[pldb]
All of the operations applied so far are tracked in the circuit's ``QuantumTape``
which is accessible using :func:`~pennylane.debug_tape`. This can be used to
*visually* debug the circuit.

.. code-block:: console
[pldb]: qtape = qml.debug_tape()
[pldb]: qtape.operations
[pldb] qtape = qml.debug_tape()
[pldb] qtape.operations
[Hadamard(wires=[0]), CNOT(wires=[0, 2])]
[pldb]: print(qtape.draw())
[pldb] print(qtape.draw())
0: ──H─╭●─┤
2: ────╰X─┤
Expand All @@ -196,41 +196,41 @@ for the wires of interest can be probed using :func:`~pennylane.debug_probs`.

.. code-block:: console
[pldb]: qml.debug_state()
[pldb] qml.debug_state()
array([0.70710678+0.j, 0. +0.j, 0. +0.j, 0. +0.j,
0. +0.j, 0.70710678+0.j, 0. +0.j, 0. +0.j])
[pldb]: qml.debug_probs(wires=(0,2))
[pldb] qml.debug_probs(wires=(0,2))
array([0.5, 0. , 0. , 0.5])
Another method for probing the system is by measuring observables via
:func:`~pennylane.debug_expval`.

.. code-block:: console
[pldb]: qml.debug_expval(qml.Z(0))
[pldb] qml.debug_expval(qml.Z(0))
0.0
[pldb]: qml.debug_expval(qml.X(0)@qml.X(2))
[pldb] qml.debug_expval(qml.X(0) @ qml.X(2))
0.9999999999999996
Additionally, the quantum circuit can be dynamically updated by adding gates directly
from the prompt. This allows users to modify the circuit *on-the-fly*!

.. code-block:: console
[pldb]: continue
[pldb] continue
> /Users/your/path/to/script.py(14)circuit()
-> qml.RX(-x, wires=1)
[pldb]: qtape = qml.debug_tape()
[pldb]: print(qtape.draw(wire_order=(0,1,2)))
[pldb] qtape = qml.debug_tape()
[pldb] print(qtape.draw(wire_order=(0,1,2)))
0: ──H─╭●──RX─┤
1: ────│───RX─┤
2: ────╰X──RX─┤
[pldb]: qml.RZ(0.5*x, wires=0)
[pldb] qml.RZ(0.5*x, wires=0)
RZ(0.61725, wires=[0])
[pldb]: qml.CZ(wires=(1,2))
[pldb] qml.CZ(wires=(1,2))
CZ(wires=[1, 2])
[pldb]: qtape = qml.debug_tape()
[pldb]: print(qtape.draw(wire_order=(0,1,2)))
[pldb] qtape = qml.debug_tape()
[pldb] print(qtape.draw(wire_order=(0,1,2)))
0: ──H─╭●──RX──RZ─┤
1: ────│───RX─╭●──┤
2: ────╰X──RX─╰Z──┤
23 changes: 16 additions & 7 deletions doc/code/qml_noise.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,17 @@ quantum circuit. One can construct standard Boolean functions using the followin
~wires_eq
~wires_in

For example, a Boolean function that checks of an operation is on wire ``0`` can be created
like so:
For example, a Boolean function that checks if an operation is on wire ``0`` can be created
as follows:

>>> fn = wires_eq(0)
>>> fn = qml.noise.wires_eq(0)
>>> op1, op2 = qml.PauliX(0), qml.PauliX(1)
>>> fn(op1)
False
>>> fn(op2)
True

>>> fn(op2)
False

Arbitrary Boolean functions can also be defined by wrapping the functional form
of custom conditions with the following decorator:

Expand All @@ -79,7 +80,7 @@ with a maximum parameter value:
def rx_condition(op, **metadata):
return isinstance(op, qml.RX) and op.parameters[0] < 1.0
Boolean functions can be combined using standard bit-wise operators, such as
Boolean functions can be combined using standard bitwise operators, such as
``&``, ``|``, ``^``, or ``~``. The result will be another Boolean function. It
is important to note that as Python will evaluate the expression in the order
of their combination, i.e., left to right, the order of composition could matter,
Expand Down Expand Up @@ -117,6 +118,7 @@ For example, a constant-valued over-rotation can be created using:
>>> rx_constant = qml.noise.partial_wires(qml.RX(0.1, wires=[0]))
>>> rx_constant(2)
RX(0.1, 2)

>>> qml.NoiseModel({rx_condition: rx_constant})
NoiseModel({
BooleanFn(rx_condition): RX(phi=0.1)
Expand Down Expand Up @@ -177,7 +179,7 @@ above, such as :func:`~.op_eq`. These objects do not need to be instantiated dir
~WiresEq
~WiresIn

Bitwise operations like `And` and `Or` are represented with the following classes in the
Bitwise operations like ``And`` and ``Or`` are represented with the following classes in the
:mod:`boolean_fn` module:

.. currentmodule:: pennylane.boolean_fn
Expand All @@ -193,8 +195,15 @@ Bitwise operations like `And` and `Or` are represented with the following classe
Class Inheritence Diagram
^^^^^^^^^^^^^^^^^^^^^^^^^

Note all child classes inherit from the same parent :class:`~.BooleanFn`,
but are just located in different modules.

**Noise Conditionals:**

.. inheritance-diagram:: pennylane.noise.conditionals
:parts: 1

**Boolean Fn conditionals:**

.. inheritance-diagram:: pennylane.boolean_fn
:parts: 1
13 changes: 13 additions & 0 deletions doc/code/qml_pytrees.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
qml.pytrees
===========

.. currentmodule:: pennylane.pytrees

.. warning::

Unless you are a PennyLane or plugin developer, you likely do not need
to use these functions.

.. automodapi:: pennylane.pytrees
:no-heading:
:include-all-objects:
20 changes: 4 additions & 16 deletions doc/code/qml_qchem.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ Overview

The quantum chemistry module provides the functionality to perform Hartree-Fock calculations
and construct observables such as molecular Hamiltonians as well as dipole moment, spin and particle
number observables.
number observables. It also includes functionalities to convert to and from Openfermion's
`QubitOperator <https://quantumai.google/reference/python/openfermion/ops/QubitOperator>`__ and
`FermionOperator <https://quantumai.google/reference/python/openfermion/ops/FermionOperator>`__.

.. currentmodule:: pennylane.qchem

Expand Down Expand Up @@ -63,21 +65,7 @@ We then construct the Hamiltonian.
hamiltonian, qubits = qml.qchem.molecular_hamiltonian(molecule, args=args)
>>> print(hamiltonian)
(-0.35968235922631075) [I0]
+ (-0.11496335836166222) [Z2]
+ (-0.11496335836166222) [Z3]
+ (0.13082414303722753) [Z1]
+ (0.13082414303722759) [Z0]
+ (0.1031689825163302) [Z0 Z2]
+ (0.1031689825163302) [Z1 Z3]
+ (0.15329959994281844) [Z0 Z3]
+ (0.15329959994281844) [Z1 Z2]
+ (0.15405495855815063) [Z0 Z1]
+ (0.1609686663987323) [Z2 Z3]
+ (-0.05013061742648825) [Y0 Y1 X2 X3]
+ (-0.05013061742648825) [X0 X1 Y2 Y3]
+ (0.05013061742648825) [Y0 X1 X2 Y3]
+ (0.05013061742648825) [X0 Y1 Y2 X3]
-0.3596823592263396 * I(0) + 0.1308241430372839 * Z(0) + -0.11496335836158356 * Z(2)+ 0.10316898251630022 * (Z(0) @ Z(2)) + 0.1308241430372839 * Z(1) + 0.15405495855812648 * (Z(0) @ Z(1)) + 0.050130617426473734 * (Y(0) @ X(1) @ X(2) @ Y(3)) + -0.050130617426473734 * (Y(0) @ Y(1) @ X(2) @ X(3)) + -0.050130617426473734 * (X(0) @ X(1) @ Y(2) @ Y(3)) + 0.050130617426473734 * (X(0) @ Y(1) @ Y(2) @ X(3)) + -0.11496335836158356 * Z(3) + 0.15329959994277395 * (Z(0) @ Z(3)) + 0.10316898251630022 * (Z(1) @ Z(3)) + 0.15329959994277395 * (Z(1) @ Z(2)) + 0.16096866639866414 * (Z(2) @ Z(3))

The generated Hamiltonian can be used in a circuit where the molecular geometry, the basis set
parameters, and the circuit parameters are optimized simultaneously. Further information about
Expand Down
1 change: 1 addition & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ PennyLane is **free** and **open source**, released under the Apache License, Ve
code/qml_capture
code/qml_devices
code/qml_measurements
code/qml_pytrees
code/qml_operation
code/qml_queuing
code/qml_tape
Expand Down
2 changes: 0 additions & 2 deletions doc/introduction/data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@ array([-1.5, -0.5, 0.5, 1.5])
For more details on reading and writing custom datasets, including metadata, please
see the :mod:`~.data` module documentation.

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

Quantum Datasets Functions and Classes
--------------------------------------

Expand Down
23 changes: 12 additions & 11 deletions doc/introduction/dynamic_quantum_circuits.rst
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,16 @@ scalings with respect to the number of mid-circuit measurements (and shots) are

.. rst-class:: tb

+--------------------------+-------------------------------------------+-----------------------------------------------------------+-------------------------------------------+--------------------+
| **Simulation technique** | **Memory** | **Time** | **Differentiation** | **shots/analytic** |
+==========================+===========================================+===========================================================+===========================================+====================+
| Deferred measurements | :rd:`\ ` :math:`\mathcal{O}(2^{n_{MCM}})` | :rd:`\ ` :math:`\mathcal{O}(2^{n_{MCM}})` | :gr:`\ ` yes \ :math:`{}^1` | :gr:`\ ` yes / yes |
+--------------------------+-------------------------------------------+-----------------------------------------------------------+-------------------------------------------+--------------------+
| Dynamic one-shot | :gr:`\ ` :math:`\mathcal{O}(1)` | :rd:`\ ` :math:`\mathcal{O}(n_{shots})` | :or:`\ ` finite differences\ :math:`{}^2` | :or:`\ ` yes / no |
+--------------------------+-------------------------------------------+-----------------------------------------------------------+-------------------------------------------+--------------------+
| Tree-traversal | :or:`\ ` :math:`\mathcal{O}(n_{MCM}+1)` | :or:`\ ` :math:`\mathcal{O}(min(n_{shots}, 2^{n_{MCM}}))` | :or:`\ ` finite differences\ :math:`{}^2` | :or:`\ ` yes / no |
+--------------------------+-------------------------------------------+-----------------------------------------------------------+-------------------------------------------+--------------------+
+--------------------------+-------------------------------------------+-----------------------------------------------------------+-------------------------------------------+--------------+--------------+
| **Simulation technique** | **Memory** | **Time** | **Differentiation** | **shots** | **analytic** |
+==========================+===========================================+===========================================================+===========================================+==============+==============+
| Deferred measurements | :rd:`\ ` :math:`\mathcal{O}(2^{n_{MCM}})` | :rd:`\ ` :math:`\mathcal{O}(2^{n_{MCM}})` | :gr:`\ ` yes \ :math:`{}^1` | :gr:`\ ` yes | :gr:`\ ` yes |
+--------------------------+-------------------------------------------+-----------------------------------------------------------+-------------------------------------------+--------------+--------------+
| Dynamic one-shot | :gr:`\ ` :math:`\mathcal{O}(1)` | :rd:`\ ` :math:`\mathcal{O}(n_{shots})` | :or:`\ ` finite differences\ :math:`{}^2` | :gr:`\ ` yes | :rd:`\ ` no |
+--------------------------+-------------------------------------------+-----------------------------------------------------------+-------------------------------------------+--------------+--------------+
| Tree-traversal | :or:`\ ` :math:`\mathcal{O}(n_{MCM}+1)` | :or:`\ ` :math:`\mathcal{O}(min(n_{shots}, 2^{n_{MCM}}))` | :or:`\ ` finite differences\ :math:`{}^2` | :gr:`\ ` yes | :rd:`\ ` no |
+--------------------------+-------------------------------------------+-----------------------------------------------------------+-------------------------------------------+--------------+--------------+


:math:`{}^1` Backpropagation and finite differences are fully supported. The adjoint method
and the parameter-shift rule are supported if no postselection is used.
Expand Down Expand Up @@ -323,11 +324,11 @@ before and after applying the transform:

.. code-block:: pycon
>>> qml.draw(my_qnode)(*pars)
>>> print(qml.draw(my_qnode)(*pars))
0: ──RY(0.64)─╭●───────RY(0.25)─┤ Probs
1: ───────────╰X──┤↗├──║────────┤
╚═══╩════════╡ <MCM>
>>> qml.draw(deferred_qnode)(*pars)
>>> print(qml.draw(deferred_qnode)(*pars))
0: ──RY(0.64)─╭●────╭RY(0.25)─┤ Probs
1: ───────────╰X─╭●─│─────────┤
2: ──────────────╰X─╰●────────┤ <None>
Expand Down
Loading

0 comments on commit 27c117f

Please sign in to comment.