Skip to content

Commit

Permalink
Fix circuit compilation tutorial to use new transforms syntax (#929)
Browse files Browse the repository at this point in the history
**Summary:**
As title says. `tutorial_circuit_compilation.py` was failing because of
deprecated transforms syntax for the transforms used in the demo. I
replaced the deprecated syntax with the new API, and it works locally.
([See failure
here](https://github.com/PennyLaneAI/qml/actions/runs/6179181147/job/16773713171#step:18:1518))

**Relevant references:**

**Possible Drawbacks:**

**Related GitHub Issues:**
  • Loading branch information
mudit2812 authored Sep 15, 2023
1 parent 6a47cfc commit 860e1e4
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions demonstrations/tutorial_circuit_compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
To illustrate their combined effect, let us consider the following circuit.
"""

from functools import partial
import pennylane as qml
import matplotlib.pyplot as plt

Expand Down Expand Up @@ -81,7 +82,7 @@ def circuit(angles):
# towards a ``direction`` (defaults to the right).
#

commuted_circuit = qml.transforms.commute_controlled(direction="right")(circuit)
commuted_circuit = qml.transforms.commute_controlled(circuit, direction="right")

qnode = qml.QNode(commuted_circuit, dev)
qml.draw_mpl(qnode, decimals=1, style="sketch")(angles)
Expand Down Expand Up @@ -110,7 +111,7 @@ def circuit(angles):
# Furthermore, the merged rotations with a resulting angle lower than ``atol`` are directly removed.
#

merged_circuit = qml.transforms.merge_rotations(atol=1e-8, include_gates=None)(cancelled_circuit)
merged_circuit = qml.transforms.merge_rotations(cancelled_circuit, atol=1e-8, include_gates=None)


qnode = qml.QNode(merged_circuit, dev)
Expand All @@ -128,9 +129,9 @@ def circuit(angles):


@qml.qnode(dev)
@qml.transforms.merge_rotations(atol=1e-8, include_gates=None)
@partial(qml.transforms.merge_rotations, atol=1e-8, include_gates=None)
@qml.transforms.cancel_inverses
@qml.transforms.commute_controlled(direction="right")
@partial(qml.transforms.commute_controlled, direction="right")
def q_fun(angles):
qml.Hadamard(wires=1)
qml.Hadamard(wires=2)
Expand Down Expand Up @@ -164,7 +165,7 @@ def q_fun(angles):
# :func:`~pennylane.compile` function, which yields the same final circuit.
#

compiled_circuit = qml.compile()(circuit)
compiled_circuit = qml.compile(circuit)

qnode = qml.QNode(compiled_circuit, dev)
qml.draw_mpl(qnode, decimals=1, style="sketch")(angles)
Expand All @@ -179,7 +180,7 @@ def q_fun(angles):
# Let us see the resulting circuit with two passes.
#

compiled_circuit = qml.compile(num_passes=2)(circuit)
compiled_circuit = qml.compile(circuit, num_passes=2)

qnode = qml.QNode(compiled_circuit, dev)
qml.draw_mpl(qnode, decimals=1, style="sketch")(angles)
Expand All @@ -194,13 +195,14 @@ def q_fun(angles):
#

compiled_circuit = qml.compile(
circuit,
pipeline=[
qml.transforms.commute_controlled(direction="left"), # Opposite direction
qml.transforms.merge_rotations(include_gates=["RZ"]), # Different threshold
partial(qml.transforms.commute_controlled, direction="left"), # Opposite direction
partial(qml.transforms.merge_rotations, include_gates=["RZ"]), # Different threshold
qml.transforms.cancel_inverses, # Cancel inverses after rotations
],
num_passes=3,
)(circuit)
)

qnode = qml.QNode(compiled_circuit, dev)
qml.draw_mpl(qnode, decimals=1, style="sketch")(angles)
Expand All @@ -216,7 +218,7 @@ def q_fun(angles):
# in terms of our basis, then apply the transforms.
#

compiled_circuit = qml.compile(basis_set=["CNOT", "RX", "RY", "RZ"], num_passes=2)(circuit)
compiled_circuit = qml.compile(circuit, basis_set=["CNOT", "RX", "RY", "RZ"], num_passes=2)

qnode = qml.QNode(compiled_circuit, dev)
qml.draw_mpl(qnode, decimals=1, style="sketch")(angles)
Expand Down

0 comments on commit 860e1e4

Please sign in to comment.