Skip to content

Commit

Permalink
Commutation dag (#1712)
Browse files Browse the repository at this point in the history
* remove newlines in docstring (#1647)

* First version.

* Working version.

* DAG successors.

* Update documentation and code quality.

* Black

* More black

* Protected access.

* Import order.

* Target_wires to tar_wires.

* Typo.

* Typo.

* More black.

* Add observable attribute and tests.

* Black.

* More black.

* Add more tests.

* Add more tests.

* Sort pred succ

* Add support SWAP.

* More coverage.

* Black.

* Update from review.

* Black.

* Update pennylane/commutation_dag.py

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update pennylane/commutation_dag.py

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update pennylane/commutation_dag.py

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update pennylane/commutation_dag.py

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update pennylane/commutation_dag.py

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Changes from review.

* Random codefactor

* Remove some changes.

* Black.

* Change target qpe, qmc

* More doc.

* Update param gates

* Param Ops

* Update parameters

* More ops

* Add templates.

* Update

* More tests.

* More tests.

* Update from refractor.

* Update

* black

* Update.

* Update.

* Better integration with future control update

* Black.

* Typo

* Adapt for pattern matching.

* Update coverage

* More coverage

* Update pennylane/transforms/commutation_dag.py

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update pennylane/transforms/commutation_dag.py

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update pennylane/transforms/commutation_dag.py

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update pennylane/transforms/commutation_dag.py

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update pennylane/transforms/commutation_dag.py

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update pennylane/transforms/commutation_dag.py

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update pennylane/transforms/commutation_dag.py

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update pennylane/transforms/commutation_dag.py

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update from review.

* Test simplify

* Test simplify

* Remove

* Simplify docstring

* Update doc/releases/changelog-dev.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update doc/releases/changelog-dev.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update pennylane/transforms/commutation_dag.py

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update pennylane/transforms/commutation_dag.py

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update pennylane/transforms/commutation_dag.py

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update pennylane/transforms/commutation_dag.py

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update pennylane/transforms/commutation_dag.py

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update pennylane/transforms/commutation_dag.py

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update pennylane/transforms/commutation_dag.py

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update pennylane/transforms/commutation_dag.py

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update pennylane/transforms/commutation_dag.py

Co-authored-by: Josh Izaac <josh146@gmail.com>

* black

* Update from review

* Update doc/releases/changelog-dev.md

* Update doc/releases/changelog-dev.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update doc/releases/changelog-dev.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update pennylane/transforms/commutation_dag.py

Co-authored-by: Josh Izaac <josh146@gmail.com>

* More

* Doc

Co-authored-by: Theodor <theodor@xanadu.ai>
Co-authored-by: Josh Izaac <josh146@gmail.com>
  • Loading branch information
3 people committed Mar 1, 2022
1 parent d4931e7 commit 5a6c2f5
Show file tree
Hide file tree
Showing 9 changed files with 2,532 additions and 3 deletions.
51 changes: 49 additions & 2 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,53 @@

<h3>New features since last release</h3>

* Transform a circuit from quantum tape, quantum function or quantum node to a pairwise
commutation DAG (directed acyclic graph). The node represents the quantum operations, and the edges represent
non commutation between two operations.
[(#1712)](https://github.com/PennyLaneAI/pennylane/pull/1712)

From the following quantum function,
```
def circuit(x, y, z):
qml.RX(x, wires=0)
qml.RX(y, wires=0)
qml.CNOT(wires=[1, 2])
qml.RY(y, wires=1)
qml.Hadamard(wires=2)
qml.CRZ(z, wires=[2, 0])
qml.RY(-y, wires=1)
return qml.expval(qml.PauliZ(0))
```
the commutation DAG can be returned by using the following code:
```
get_dag = commutation_dag(circuit)
theta = np.pi/4
phi = np.pi/3
psi = np.pi/2
dag = get_dag(theta, phi, psi)
```
You can access all nodes by using the ``get_nodes`` function in the form of a list ``(ID, CommutationDAGNode)``:
```
nodes = dag.get_nodes()
[(0, <pennylane.transforms.commutation_dag.CommutationDAGNode object at 0x132b03b20>), ...]
```

You can also access specific nodes ``CommutationDAGNode`` by using the ``get_node`` function. From the ``CommutationDAGNode``
you can directly access all node attributes.

```
>>> second_node = dag.get_node(2)
<pennylane.transforms.commutation_dag.CommutationDAGNode object at 0x136f8c4c0>
>>> second_operation = second_node.op
CNOT(wires=[1, 2])
>>> second_node_successors = second_node.successors
[3, 4, 5, 6]
>>> second_node_predecessors = second_node.predecessors
[]
```
* The text based drawer accessed via `qml.draw` has been overhauled.
[(#2128)](https://github.com/PennyLaneAI/pennylane/pull/2128)
[(#2198)](https://github.com/PennyLaneAI/pennylane/pull/2198)
Expand Down Expand Up @@ -317,6 +364,6 @@ The Operator class has undergone a major refactor with the following changes:
This release contains contributions from (in alphabetical order):

Thomas Bromley, Anthony Hayes, Josh Izaac, Christina Lee,
Maria Fernanda Morris, Zeyue Niu, Maria Schuld, Jay Soni, Antal Száva,
David Wierichs
Maria Fernanda Morris, Romain Moyard, Zeyue Niu, Maria Schuld, Jay Soni,
Antal Száva, David Wierichs

3 changes: 3 additions & 0 deletions pennylane/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@
single_tape_transform,
quantum_monte_carlo,
apply_controlled_Q,
commutation_dag,
is_commuting,
simplify,
)
from pennylane.optimize import *
from pennylane.vqe import ExpvalCost, VQECost
Expand Down
1 change: 1 addition & 0 deletions pennylane/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,7 @@ def grad_method(self):
s_1]=[-1/2, 1, -\pi/2]` is assumed for every parameter.
"""

# Attributes for compilation transforms
basis = None
"""str or None: The target operation for controlled gates.
target operation. If not ``None``, should take a value of ``"X"``, ``"Y"``,
Expand Down
1 change: 1 addition & 0 deletions pennylane/templates/subroutines/qpe.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def circuit():
grad_method = None

def __init__(self, unitary, target_wires, estimation_wires, do_queue=True, id=None):

target_wires = list(target_wires)
estimation_wires = list(estimation_wires)
wires = target_wires + estimation_wires
Expand Down
17 changes: 17 additions & 0 deletions pennylane/transforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@
~transforms.zyz_decomposition
~transforms.two_qubit_decomposition
~transforms.set_decomposition
~transforms.simplify
There are also utility functions that take a circuit and return a DAG.
.. autosummary::
:toctree: api
~transforms.commutation_dag
~transforms.CommutationDAG
~transforms.CommutationDAGNode
Transforms for circuit cutting
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -170,6 +180,13 @@
from .qmc import apply_controlled_Q, quantum_monte_carlo
from .unitary_to_rot import unitary_to_rot
from .get_unitary_matrix import get_unitary_matrix
from .commutation_dag import (
commutation_dag,
is_commuting,
CommutationDAG,
CommutationDAGNode,
simplify,
)
from .tape_expand import (
expand_invalid_trainable,
expand_multipar,
Expand Down
Loading

0 comments on commit 5a6c2f5

Please sign in to comment.